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> |
Lorenzo Colitti | 4362bb2 | 2017-01-21 15:00:36 +0900 | [diff] [blame] | 21 | #include <math.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 25 | #include <sys/wait.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 26 | |
| 27 | #include <fcntl.h> |
| 28 | #include <dirent.h> |
| 29 | |
| 30 | #define LOG_TAG "Netd" |
| 31 | |
| 32 | #include "cutils/log.h" |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 33 | #include "utils/RWLock.h" |
| 34 | |
| 35 | #include <binder/IPCThreadState.h> |
| 36 | #include <binder/IServiceManager.h> |
| 37 | #include <binder/ProcessState.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 38 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 39 | #include "Controllers.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 40 | #include "CommandListener.h" |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 41 | #include "NetdConstants.h" |
| 42 | #include "NetdNativeService.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 43 | #include "NetlinkManager.h" |
Lorenzo Colitti | 4362bb2 | 2017-01-21 15:00:36 +0900 | [diff] [blame] | 44 | #include "Stopwatch.h" |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 45 | #include "DnsProxyListener.h" |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 46 | #include "MDnsSdListener.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 47 | #include "FwmarkServer.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 48 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 49 | using android::status_t; |
| 50 | using android::sp; |
| 51 | using android::IPCThreadState; |
| 52 | using android::ProcessState; |
| 53 | using android::defaultServiceManager; |
| 54 | using android::net::NetdNativeService; |
| 55 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 56 | static void blockSigpipe(); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 57 | static void remove_pid_file(); |
| 58 | static bool write_pid_file(); |
| 59 | |
| 60 | const char* const PID_FILE_PATH = "/data/misc/net/netd_pid"; |
| 61 | const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC; |
| 62 | 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] | 63 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 64 | android::RWLock android::net::gBigNetdLock; |
| 65 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 66 | int main() { |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 67 | using android::net::gCtls; |
Lorenzo Colitti | 4362bb2 | 2017-01-21 15:00:36 +0900 | [diff] [blame] | 68 | Stopwatch s; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 69 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 70 | ALOGI("Netd 1.0 starting"); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 71 | remove_pid_file(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 72 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 73 | blockSigpipe(); |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 74 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 75 | NetlinkManager *nm = NetlinkManager::Instance(); |
| 76 | if (nm == nullptr) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 77 | ALOGE("Unable to create NetlinkManager"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 78 | exit(1); |
| 79 | }; |
| 80 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 81 | gCtls = new android::net::Controllers(); |
Lorenzo Colitti | 1ed96e2 | 2017-02-02 12:21:56 +0900 | [diff] [blame^] | 82 | gCtls->init(); |
| 83 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 84 | CommandListener cl; |
| 85 | nm->setBroadcaster((SocketListener *) &cl); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 86 | |
| 87 | if (nm->start()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 88 | ALOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 89 | exit(1); |
| 90 | } |
| 91 | |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 92 | // Set local DNS mode, to prevent bionic from proxying |
| 93 | // back to this service, recursively. |
| 94 | setenv("ANDROID_DNS_MODE", "local", 1); |
Michal Karpinski | d544011 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 95 | DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter); |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 96 | if (dpl.startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 97 | ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno)); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 98 | exit(1); |
| 99 | } |
| 100 | |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 101 | MDnsSdListener mdnsl; |
| 102 | if (mdnsl.startListener()) { |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 103 | ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno)); |
| 104 | exit(1); |
| 105 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 106 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 107 | FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter); |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 108 | if (fwmarkServer.startListener()) { |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 109 | ALOGE("Unable to start FwmarkServer (%s)", strerror(errno)); |
| 110 | exit(1); |
| 111 | } |
| 112 | |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 113 | status_t ret; |
| 114 | if ((ret = NetdNativeService::start()) != android::OK) { |
| 115 | ALOGE("Unable to start NetdNativeService: %d", ret); |
| 116 | exit(1); |
| 117 | } |
| 118 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 119 | /* |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 120 | * Now that we're up, we can respond to commands. Starting the listener also tells |
| 121 | * NetworkManagementService that we are up and that our binder interface is ready. |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 122 | */ |
Pierre Imai | 1cfa543 | 2016-02-24 18:00:03 +0900 | [diff] [blame] | 123 | if (cl.startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 124 | ALOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 125 | exit(1); |
| 126 | } |
| 127 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 128 | write_pid_file(); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 129 | |
Lorenzo Colitti | 4362bb2 | 2017-01-21 15:00:36 +0900 | [diff] [blame] | 130 | ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken())); |
| 131 | |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 132 | IPCThreadState::self()->joinThreadPool(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 133 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 134 | ALOGI("Netd exiting"); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 135 | |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 136 | remove_pid_file(); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 137 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 138 | exit(0); |
| 139 | } |
| 140 | |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 141 | static bool write_pid_file() { |
Lorenzo Colitti | 0a3eb85 | 2016-02-23 16:59:21 +0900 | [diff] [blame] | 142 | char pid_buf[INT32_STRLEN]; |
| 143 | snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid()); |
Robert Greenwalt | 347f693 | 2014-10-31 18:54:06 -0700 | [diff] [blame] | 144 | |
| 145 | int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE); |
| 146 | if (fd == -1) { |
| 147 | ALOGE("Unable to create pid file (%s)", strerror(errno)); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // File creation is affected by umask, so make sure the right mode bits are set. |
| 152 | if (fchmod(fd, PID_FILE_MODE) == -1) { |
| 153 | ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno)); |
| 154 | close(fd); |
| 155 | remove_pid_file(); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) { |
| 160 | ALOGE("Unable to write to pid file (%s)", strerror(errno)); |
| 161 | close(fd); |
| 162 | remove_pid_file(); |
| 163 | return false; |
| 164 | } |
| 165 | close(fd); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | static void remove_pid_file() { |
| 170 | unlink(PID_FILE_PATH); |
| 171 | } |
| 172 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 173 | static void blockSigpipe() |
| 174 | { |
| 175 | sigset_t mask; |
| 176 | |
| 177 | sigemptyset(&mask); |
| 178 | sigaddset(&mask, SIGPIPE); |
| 179 | if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) |
| 180 | ALOGW("WARNING: SIGPIPE not blocked\n"); |
| 181 | } |