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 | |
| 39 | static void coldboot(const char *path); |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 40 | static void sigchld_handler(int sig); |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 41 | static void blockSigpipe(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 42 | |
| 43 | int main() { |
| 44 | |
| 45 | CommandListener *cl; |
| 46 | NetlinkManager *nm; |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 47 | DnsProxyListener *dpl; |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 48 | MDnsSdListener *mdnsl; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame^] | 49 | FwmarkServer* fwmarkServer; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 50 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 51 | ALOGI("Netd 1.0 starting"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 52 | |
San Mehat | f1c368a | 2010-01-27 17:13:32 -0800 | [diff] [blame] | 53 | // signal(SIGCHLD, sigchld_handler); |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 54 | blockSigpipe(); |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 55 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 56 | if (!(nm = NetlinkManager::Instance())) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 57 | ALOGE("Unable to create NetlinkManager"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 58 | exit(1); |
| 59 | }; |
| 60 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 61 | cl = new CommandListener(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 62 | nm->setBroadcaster((SocketListener *) cl); |
| 63 | |
| 64 | if (nm->start()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 65 | ALOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 66 | exit(1); |
| 67 | } |
| 68 | |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 69 | // Set local DNS mode, to prevent bionic from proxying |
| 70 | // back to this service, recursively. |
| 71 | setenv("ANDROID_DNS_MODE", "local", 1); |
Paul Jensen | 7dd69a1 | 2014-04-17 17:25:43 -0400 | [diff] [blame] | 72 | dpl = new DnsProxyListener(CommandListener::sNetCtrl, CommandListener::sPermissionsController); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 73 | if (dpl->startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 74 | ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno)); |
Brad Fitzpatrick | 007e987 | 2010-10-27 11:39:52 -0700 | [diff] [blame] | 75 | exit(1); |
| 76 | } |
| 77 | |
Robert Greenwalt | 745e09f | 2012-03-29 14:45:54 -0700 | [diff] [blame] | 78 | mdnsl = new MDnsSdListener(); |
| 79 | if (mdnsl->startListener()) { |
| 80 | ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno)); |
| 81 | exit(1); |
| 82 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame^] | 83 | |
| 84 | fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl, |
| 85 | CommandListener::sPermissionsController); |
| 86 | if (fwmarkServer->startListener()) { |
| 87 | ALOGE("Unable to start FwmarkServer (%s)", strerror(errno)); |
| 88 | exit(1); |
| 89 | } |
| 90 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Now that we're up, we can respond to commands |
| 93 | */ |
| 94 | if (cl->startListener()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 95 | ALOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 96 | exit(1); |
| 97 | } |
| 98 | |
| 99 | // Eventually we'll become the monitoring thread |
| 100 | while(1) { |
| 101 | sleep(1000); |
| 102 | } |
| 103 | |
Steve Block | 08b58b6 | 2012-01-04 20:05:11 +0000 | [diff] [blame] | 104 | ALOGI("Netd exiting"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 105 | exit(0); |
| 106 | } |
| 107 | |
| 108 | static void do_coldboot(DIR *d, int lvl) |
| 109 | { |
| 110 | struct dirent *de; |
| 111 | int dfd, fd; |
| 112 | |
| 113 | dfd = dirfd(d); |
| 114 | |
| 115 | fd = openat(dfd, "uevent", O_WRONLY); |
| 116 | if(fd >= 0) { |
| 117 | write(fd, "add\n", 4); |
| 118 | close(fd); |
| 119 | } |
| 120 | |
| 121 | while((de = readdir(d))) { |
| 122 | DIR *d2; |
| 123 | |
| 124 | if (de->d_name[0] == '.') |
| 125 | continue; |
| 126 | |
| 127 | if (de->d_type != DT_DIR && lvl > 0) |
| 128 | continue; |
| 129 | |
| 130 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 131 | if(fd < 0) |
| 132 | continue; |
| 133 | |
| 134 | d2 = fdopendir(fd); |
| 135 | if(d2 == 0) |
| 136 | close(fd); |
| 137 | else { |
| 138 | do_coldboot(d2, lvl + 1); |
| 139 | closedir(d2); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static void coldboot(const char *path) |
| 145 | { |
| 146 | DIR *d = opendir(path); |
| 147 | if(d) { |
| 148 | do_coldboot(d, 0); |
| 149 | closedir(d); |
| 150 | } |
| 151 | } |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 152 | |
| 153 | static void sigchld_handler(int sig) { |
| 154 | pid_t pid = wait(NULL); |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 155 | ALOGD("Child process %d exited", pid); |
San Mehat | 5c1b8af | 2010-01-21 15:37:10 -0800 | [diff] [blame] | 156 | } |
Selim Gurun | c1044d4 | 2012-03-09 08:59:28 -0800 | [diff] [blame] | 157 | |
| 158 | static void blockSigpipe() |
| 159 | { |
| 160 | sigset_t mask; |
| 161 | |
| 162 | sigemptyset(&mask); |
| 163 | sigaddset(&mask, SIGPIPE); |
| 164 | if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) |
| 165 | ALOGW("WARNING: SIGPIPE not blocked\n"); |
| 166 | } |