main.cpp revision c1044d4507a79166f3cfac9cacf669888f049ea9
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> 19#include <signal.h> 20#include <errno.h> 21#include <string.h> 22#include <sys/stat.h> 23#include <sys/types.h> 24#include <sys/wait.h> 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" 35#include "DnsProxyListener.h" 36 37static void coldboot(const char *path); 38static void sigchld_handler(int sig); 39static void blockSigpipe(); 40 41int main() { 42 43 CommandListener *cl; 44 NetlinkManager *nm; 45 DnsProxyListener *dpl; 46 47 ALOGI("Netd 1.0 starting"); 48 49// signal(SIGCHLD, sigchld_handler); 50 blockSigpipe(); 51 52 if (!(nm = NetlinkManager::Instance())) { 53 ALOGE("Unable to create NetlinkManager"); 54 exit(1); 55 }; 56 57 58 cl = new CommandListener(); 59 nm->setBroadcaster((SocketListener *) cl); 60 61 if (nm->start()) { 62 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno)); 63 exit(1); 64 } 65 66 // Set local DNS mode, to prevent bionic from proxying 67 // back to this service, recursively. 68 setenv("ANDROID_DNS_MODE", "local", 1); 69 dpl = new DnsProxyListener(); 70 if (dpl->startListener()) { 71 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno)); 72 exit(1); 73 } 74 75 /* 76 * Now that we're up, we can respond to commands 77 */ 78 if (cl->startListener()) { 79 ALOGE("Unable to start CommandListener (%s)", strerror(errno)); 80 exit(1); 81 } 82 83 // Eventually we'll become the monitoring thread 84 while(1) { 85 sleep(1000); 86 } 87 88 ALOGI("Netd exiting"); 89 exit(0); 90} 91 92static void do_coldboot(DIR *d, int lvl) 93{ 94 struct dirent *de; 95 int dfd, fd; 96 97 dfd = dirfd(d); 98 99 fd = openat(dfd, "uevent", O_WRONLY); 100 if(fd >= 0) { 101 write(fd, "add\n", 4); 102 close(fd); 103 } 104 105 while((de = readdir(d))) { 106 DIR *d2; 107 108 if (de->d_name[0] == '.') 109 continue; 110 111 if (de->d_type != DT_DIR && lvl > 0) 112 continue; 113 114 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 115 if(fd < 0) 116 continue; 117 118 d2 = fdopendir(fd); 119 if(d2 == 0) 120 close(fd); 121 else { 122 do_coldboot(d2, lvl + 1); 123 closedir(d2); 124 } 125 } 126} 127 128static void coldboot(const char *path) 129{ 130 DIR *d = opendir(path); 131 if(d) { 132 do_coldboot(d, 0); 133 closedir(d); 134 } 135} 136 137static void sigchld_handler(int sig) { 138 pid_t pid = wait(NULL); 139 ALOGD("Child process %d exited", pid); 140} 141 142static void blockSigpipe() 143{ 144 sigset_t mask; 145 146 sigemptyset(&mask); 147 sigaddset(&mask, SIGPIPE); 148 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) 149 ALOGW("WARNING: SIGPIPE not blocked\n"); 150} 151