San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 1 | /* |
| 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 Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 19 | #include <signal.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 24 | #include <sys/wait.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 25 | |
| 26 | #include <fcntl.h> |
| 27 | #include <dirent.h> |
| 28 | |
| 29 | #define LOG_TAG "Netd" |
| 30 | |
| 31 | #include "cutils/log.h" |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 32 | #include "utils/RWLock.h" |
| 33 | |
| 34 | #include <binder/IPCThreadState.h> |
| 35 | #include <binder/IServiceManager.h> |
| 36 | #include <binder/ProcessState.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 37 | |
| 38 | #include "CommandListener.h" |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 39 | #include "NetdConstants.h" |
| 40 | #include "NetdNativeService.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 41 | #include "NetlinkManager.h" |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 42 | #include "DnsProxyListener.h" |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 43 | #include "MDnsSdListener.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 44 | #include "FwmarkServer.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 45 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 46 | using android::status_t; |
| 47 | using android::sp; |
| 48 | using android::IPCThreadState; |
| 49 | using android::ProcessState; |
| 50 | using android::defaultServiceManager; |
| 51 | using android::net::NetdNativeService; |
| 52 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 53 | static void blockSigpipe(); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 54 | static void remove_pid_file(); |
| 55 | static bool write_pid_file(); |
| 56 | |
| 57 | const char* const PID_FILE_PATH = "/data/misc/net/netd_pid"; |
| 58 | const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC; |
| 59 | const mode_t PID_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r-- |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 60 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 61 | |
| 62 | android::RWLock android::net::gBigNetdLock; |
| 63 | |
| 64 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 65 | int main() { |
| 66 | |
| 67 | CommandListener *cl; |
| 68 | NetlinkManager *nm; |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 69 | DnsProxyListener *dpl; |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 70 | MDnsSdListener *mdnsl; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 71 | FwmarkServer* fwmarkServer; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 72 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 73 | ALOGI("Netd 1.0 starting"); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 74 | remove_pid_file(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 75 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 76 | blockSigpipe(); |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 77 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 78 | if (!(nm = NetlinkManager::Instance())) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 79 | ALOGE("Unable to create NetlinkManager"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 80 | exit(1); |
| 81 | }; |
| 82 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 83 | cl = new CommandListener(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 84 | nm->setBroadcaster((SocketListener *) cl); |
| 85 | |
| 86 | if (nm->start()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 87 | ALOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 88 | exit(1); |
| 89 | } |
| 90 | |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 91 | // Set local DNS mode, to prevent bionic from proxying |
| 92 | // back to this service, recursively. |
| 93 | setenv("ANDROID_DNS_MODE", "local", 1); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 94 | dpl = new DnsProxyListener(CommandListener::sNetCtrl); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 95 | if (dpl->startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 96 | ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno)); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 97 | exit(1); |
| 98 | } |
| 99 | |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 100 | mdnsl = new MDnsSdListener(); |
| 101 | if (mdnsl->startListener()) { |
| 102 | ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno)); |
| 103 | exit(1); |
| 104 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 105 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 106 | fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 107 | if (fwmarkServer->startListener()) { |
| 108 | ALOGE("Unable to start FwmarkServer (%s)", strerror(errno)); |
| 109 | exit(1); |
| 110 | } |
| 111 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 112 | /* |
| 113 | * Now that we're up, we can respond to commands |
| 114 | */ |
| 115 | if (cl->startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 116 | ALOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 117 | exit(1); |
| 118 | } |
| 119 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 120 | write_pid_file(); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 121 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 122 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 123 | NetdNativeService::publishAndJoinThreadPool(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 124 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 125 | ALOGI("Netd exiting"); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 126 | |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 127 | remove_pid_file(); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame^] | 128 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 129 | exit(0); |
| 130 | } |
| 131 | |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 132 | static 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 | |
| 160 | static void remove_pid_file() { |
| 161 | unlink(PID_FILE_PATH); |
| 162 | } |
| 163 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 164 | static 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 | } |