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" |
| 32 | |
| 33 | #include "CommandListener.h" |
| 34 | #include "NetlinkManager.h" |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 35 | #include "DnsProxyListener.h" |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 36 | #include "MDnsSdListener.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 37 | #include "FwmarkServer.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 38 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 39 | static void blockSigpipe(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 40 | |
| 41 | int main() { |
| 42 | |
| 43 | CommandListener *cl; |
| 44 | NetlinkManager *nm; |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 45 | DnsProxyListener *dpl; |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 46 | MDnsSdListener *mdnsl; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 47 | FwmarkServer* fwmarkServer; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 48 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 49 | ALOGI("Netd 1.0 starting"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 50 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 51 | blockSigpipe(); |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 52 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 53 | if (!(nm = NetlinkManager::Instance())) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 54 | ALOGE("Unable to create NetlinkManager"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 55 | exit(1); |
| 56 | }; |
| 57 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 58 | cl = new CommandListener(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 59 | nm->setBroadcaster((SocketListener *) cl); |
| 60 | |
| 61 | if (nm->start()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 62 | ALOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 63 | exit(1); |
| 64 | } |
| 65 | |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 66 | // Set local DNS mode, to prevent bionic from proxying |
| 67 | // back to this service, recursively. |
| 68 | setenv("ANDROID_DNS_MODE", "local", 1); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 69 | dpl = new DnsProxyListener(CommandListener::sNetCtrl); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 70 | if (dpl->startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 71 | ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno)); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 72 | exit(1); |
| 73 | } |
| 74 | |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 75 | mdnsl = new MDnsSdListener(); |
| 76 | if (mdnsl->startListener()) { |
| 77 | ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno)); |
| 78 | exit(1); |
| 79 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 80 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 81 | fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 82 | if (fwmarkServer->startListener()) { |
| 83 | ALOGE("Unable to start FwmarkServer (%s)", strerror(errno)); |
| 84 | exit(1); |
| 85 | } |
| 86 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 87 | /* |
| 88 | * Now that we're up, we can respond to commands |
| 89 | */ |
| 90 | if (cl->startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 91 | ALOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | // Eventually we'll become the monitoring thread |
| 96 | while(1) { |
| 97 | sleep(1000); |
| 98 | } |
| 99 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 100 | ALOGI("Netd exiting"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 101 | exit(0); |
| 102 | } |
| 103 | |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 104 | static void blockSigpipe() |
| 105 | { |
| 106 | sigset_t mask; |
| 107 | |
| 108 | sigemptyset(&mask); |
| 109 | sigaddset(&mask, SIGPIPE); |
| 110 | if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) |
| 111 | ALOGW("WARNING: SIGPIPE not blocked\n"); |
| 112 | } |