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