blob: 90f1e6f51b75b5c7849a85a9665b34978ed8486c [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"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070036#include "MDnsSdListener.h"
San Mehatd1830422010-01-15 08:02:39 -080037
38static void coldboot(const char *path);
San Mehat5c1b8af2010-01-21 15:37:10 -080039static void sigchld_handler(int sig);
Selim Gurunc1044d42012-03-09 08:59:28 -080040static void blockSigpipe();
San Mehatd1830422010-01-15 08:02:39 -080041
42int main() {
43
44 CommandListener *cl;
45 NetlinkManager *nm;
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070046 DnsProxyListener *dpl;
Robert Greenwalt745e09f2012-03-29 14:45:54 -070047 MDnsSdListener *mdnsl;
San Mehatd1830422010-01-15 08:02:39 -080048
Steve Block08b58b62012-01-04 20:05:11 +000049 ALOGI("Netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080050
San Mehatf1c368a2010-01-27 17:13:32 -080051// signal(SIGCHLD, sigchld_handler);
Selim Gurunc1044d42012-03-09 08:59:28 -080052 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080053
San Mehatd1830422010-01-15 08:02:39 -080054 if (!(nm = NetlinkManager::Instance())) {
Steve Block5ea0c052012-01-06 19:18:11 +000055 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080056 exit(1);
57 };
58
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050059 cl = new CommandListener();
San Mehatd1830422010-01-15 08:02:39 -080060 nm->setBroadcaster((SocketListener *) cl);
61
62 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000063 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080064 exit(1);
65 }
66
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070067 // Set local DNS mode, to prevent bionic from proxying
68 // back to this service, recursively.
69 setenv("ANDROID_DNS_MODE", "local", 1);
Paul Jensen7dd69a12014-04-17 17:25:43 -040070 dpl = new DnsProxyListener(CommandListener::sNetCtrl, CommandListener::sPermissionsController);
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070071 if (dpl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000072 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070073 exit(1);
74 }
75
Robert Greenwalt745e09f2012-03-29 14:45:54 -070076 mdnsl = new MDnsSdListener();
77 if (mdnsl->startListener()) {
78 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
79 exit(1);
80 }
San Mehatd1830422010-01-15 08:02:39 -080081 /*
82 * Now that we're up, we can respond to commands
83 */
84 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000085 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080086 exit(1);
87 }
88
89 // Eventually we'll become the monitoring thread
90 while(1) {
91 sleep(1000);
92 }
93
Steve Block08b58b62012-01-04 20:05:11 +000094 ALOGI("Netd exiting");
San Mehatd1830422010-01-15 08:02:39 -080095 exit(0);
96}
97
98static void do_coldboot(DIR *d, int lvl)
99{
100 struct dirent *de;
101 int dfd, fd;
102
103 dfd = dirfd(d);
104
105 fd = openat(dfd, "uevent", O_WRONLY);
106 if(fd >= 0) {
107 write(fd, "add\n", 4);
108 close(fd);
109 }
110
111 while((de = readdir(d))) {
112 DIR *d2;
113
114 if (de->d_name[0] == '.')
115 continue;
116
117 if (de->d_type != DT_DIR && lvl > 0)
118 continue;
119
120 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
121 if(fd < 0)
122 continue;
123
124 d2 = fdopendir(fd);
125 if(d2 == 0)
126 close(fd);
127 else {
128 do_coldboot(d2, lvl + 1);
129 closedir(d2);
130 }
131 }
132}
133
134static void coldboot(const char *path)
135{
136 DIR *d = opendir(path);
137 if(d) {
138 do_coldboot(d, 0);
139 closedir(d);
140 }
141}
San Mehat5c1b8af2010-01-21 15:37:10 -0800142
143static void sigchld_handler(int sig) {
144 pid_t pid = wait(NULL);
Steve Block7b984e32011-12-20 16:22:42 +0000145 ALOGD("Child process %d exited", pid);
San Mehat5c1b8af2010-01-21 15:37:10 -0800146}
Selim Gurunc1044d42012-03-09 08:59:28 -0800147
148static void blockSigpipe()
149{
150 sigset_t mask;
151
152 sigemptyset(&mask);
153 sigaddset(&mask, SIGPIPE);
154 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
155 ALOGW("WARNING: SIGPIPE not blocked\n");
156}