blob: 14eb729d02e360decdcd517e14a5682df7e3c36b [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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 Mehat5c1b8af2010-01-21 15:37:10 -080019#include <signal.h>
San Mehatd1830422010-01-15 08:02:39 -080020#include <errno.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080024#include <sys/wait.h>
San Mehatd1830422010-01-15 08:02:39 -080025
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 Fitzpatrick007e9872010-10-27 11:39:52 -070035#include "DnsProxyListener.h"
San Mehatd1830422010-01-15 08:02:39 -080036
37static void coldboot(const char *path);
San Mehat5c1b8af2010-01-21 15:37:10 -080038static void sigchld_handler(int sig);
Selim Gurunc1044d42012-03-09 08:59:28 -080039static void blockSigpipe();
San Mehatd1830422010-01-15 08:02:39 -080040
41int main() {
42
43 CommandListener *cl;
44 NetlinkManager *nm;
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070045 DnsProxyListener *dpl;
San Mehatd1830422010-01-15 08:02:39 -080046
Steve Block08b58b62012-01-04 20:05:11 +000047 ALOGI("Netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080048
San Mehatf1c368a2010-01-27 17:13:32 -080049// signal(SIGCHLD, sigchld_handler);
Selim Gurunc1044d42012-03-09 08:59:28 -080050 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080051
San Mehatd1830422010-01-15 08:02:39 -080052 if (!(nm = NetlinkManager::Instance())) {
Steve Block5ea0c052012-01-06 19:18:11 +000053 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080054 exit(1);
55 };
56
57
58 cl = new CommandListener();
59 nm->setBroadcaster((SocketListener *) cl);
60
61 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000062 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080063 exit(1);
64 }
65
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070066 // 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()) {
Steve Block5ea0c052012-01-06 19:18:11 +000071 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070072 exit(1);
73 }
74
San Mehatd1830422010-01-15 08:02:39 -080075 /*
76 * Now that we're up, we can respond to commands
77 */
78 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000079 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080080 exit(1);
81 }
82
83 // Eventually we'll become the monitoring thread
84 while(1) {
85 sleep(1000);
86 }
87
Steve Block08b58b62012-01-04 20:05:11 +000088 ALOGI("Netd exiting");
San Mehatd1830422010-01-15 08:02:39 -080089 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}
San Mehat5c1b8af2010-01-21 15:37:10 -0800136
137static void sigchld_handler(int sig) {
138 pid_t pid = wait(NULL);
Steve Block7b984e32011-12-20 16:22:42 +0000139 ALOGD("Child process %d exited", pid);
San Mehat5c1b8af2010-01-21 15:37:10 -0800140}
Selim Gurunc1044d42012-03-09 08:59:28 -0800141
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}