blob: 5e189ccadc30e7619c396c0d34218184a004ef19 [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"
32
33#include "CommandListener.h"
34#include "NetlinkManager.h"
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070035#include "DnsProxyListener.h"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070036#include "MDnsSdListener.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070037#include "FwmarkServer.h"
San Mehatd1830422010-01-15 08:02:39 -080038
Selim Gurunc1044d42012-03-09 08:59:28 -080039static void blockSigpipe();
Robert Greenwalt347f6932014-10-31 18:54:06 -070040static void remove_pid_file();
41static bool write_pid_file();
42
43const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
44const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
45const 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 -080046
47int main() {
48
49 CommandListener *cl;
50 NetlinkManager *nm;
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070051 DnsProxyListener *dpl;
Robert Greenwalt745e09f2012-03-29 14:45:54 -070052 MDnsSdListener *mdnsl;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070053 FwmarkServer* fwmarkServer;
San Mehatd1830422010-01-15 08:02:39 -080054
Steve Block08b58b62012-01-04 20:05:11 +000055 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070056 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080057
Selim Gurunc1044d42012-03-09 08:59:28 -080058 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080059
San Mehatd1830422010-01-15 08:02:39 -080060 if (!(nm = NetlinkManager::Instance())) {
Steve Block5ea0c052012-01-06 19:18:11 +000061 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080062 exit(1);
63 };
64
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050065 cl = new CommandListener();
San Mehatd1830422010-01-15 08:02:39 -080066 nm->setBroadcaster((SocketListener *) cl);
67
68 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000069 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080070 exit(1);
71 }
72
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070073 // Set local DNS mode, to prevent bionic from proxying
74 // back to this service, recursively.
75 setenv("ANDROID_DNS_MODE", "local", 1);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070076 dpl = new DnsProxyListener(CommandListener::sNetCtrl);
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070077 if (dpl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000078 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070079 exit(1);
80 }
81
Robert Greenwalt745e09f2012-03-29 14:45:54 -070082 mdnsl = new MDnsSdListener();
83 if (mdnsl->startListener()) {
84 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
85 exit(1);
86 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070087
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070088 fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070089 if (fwmarkServer->startListener()) {
90 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
91 exit(1);
92 }
93
San Mehatd1830422010-01-15 08:02:39 -080094 /*
95 * Now that we're up, we can respond to commands
96 */
97 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000098 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080099 exit(1);
100 }
101
Robert Greenwalt347f6932014-10-31 18:54:06 -0700102 bool wrote_pid = write_pid_file();
103
San Mehatd1830422010-01-15 08:02:39 -0800104 while(1) {
Robert Greenwalt347f6932014-10-31 18:54:06 -0700105 sleep(30); // 30 sec
106 if (!wrote_pid) {
107 wrote_pid = write_pid_file();
108 }
San Mehatd1830422010-01-15 08:02:39 -0800109 }
110
Steve Block08b58b62012-01-04 20:05:11 +0000111 ALOGI("Netd exiting");
Robert Greenwalt347f6932014-10-31 18:54:06 -0700112 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -0800113 exit(0);
114}
115
Robert Greenwalt347f6932014-10-31 18:54:06 -0700116static bool write_pid_file() {
117 char pid_buf[20]; // current pid_max is 32768, so plenty of room
118 snprintf(pid_buf, sizeof(pid_buf), "%ld\n", (long)getpid());
119
120 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
121 if (fd == -1) {
122 ALOGE("Unable to create pid file (%s)", strerror(errno));
123 return false;
124 }
125
126 // File creation is affected by umask, so make sure the right mode bits are set.
127 if (fchmod(fd, PID_FILE_MODE) == -1) {
128 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
129 close(fd);
130 remove_pid_file();
131 return false;
132 }
133
134 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
135 ALOGE("Unable to write to pid file (%s)", strerror(errno));
136 close(fd);
137 remove_pid_file();
138 return false;
139 }
140 close(fd);
141 return true;
142}
143
144static void remove_pid_file() {
145 unlink(PID_FILE_PATH);
146}
147
Selim Gurunc1044d42012-03-09 08:59:28 -0800148static void blockSigpipe()
149{
150 sigset_t mask;
151
152 sigemptyset(&mask);
153 sigaddset(&mask, SIGPIPE);
154 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
155 ALOGW("WARNING: SIGPIPE not blocked\n");
156}