• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "utils/RWLock.h"
33 
34 #include <binder/IPCThreadState.h>
35 #include <binder/IServiceManager.h>
36 #include <binder/ProcessState.h>
37 
38 #include "CommandListener.h"
39 #include "Controllers.h"
40 #include "DnsProxyListener.h"
41 #include "FwmarkServer.h"
42 #include "MDnsSdListener.h"
43 #include "NFLogListener.h"
44 #include "NetdConstants.h"
45 #include "NetdHwService.h"
46 #include "NetdNativeService.h"
47 #include "NetlinkManager.h"
48 #include "Stopwatch.h"
49 
50 using android::status_t;
51 using android::sp;
52 using android::IPCThreadState;
53 using android::ProcessState;
54 using android::defaultServiceManager;
55 using android::net::CommandListener;
56 using android::net::DnsProxyListener;
57 using android::net::FwmarkServer;
58 using android::net::NetdHwService;
59 using android::net::NetdNativeService;
60 using android::net::NetlinkManager;
61 using android::net::NFLogListener;
62 using android::net::makeNFLogListener;
63 
64 static void remove_pid_file();
65 static bool write_pid_file();
66 
67 const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
68 const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
69 const mode_t PID_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;  // mode 0644, rw-r--r--
70 
71 android::RWLock android::net::gBigNetdLock;
72 
main()73 int main() {
74     using android::net::gCtls;
75     Stopwatch s;
76 
77     ALOGI("Netd 1.0 starting");
78     remove_pid_file();
79 
80     blockSigpipe();
81 
82     // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
83     // FrameworkListener does this on initialization as well, but we only initialize these
84     // components after having initialized other subsystems that can fork.
85     for (const auto& sock : { CommandListener::SOCKET_NAME,
86                               DnsProxyListener::SOCKET_NAME,
87                               FwmarkServer::SOCKET_NAME,
88                               MDnsSdListener::SOCKET_NAME }) {
89         setCloseOnExec(sock);
90     }
91 
92     NetlinkManager *nm = NetlinkManager::Instance();
93     if (nm == nullptr) {
94         ALOGE("Unable to create NetlinkManager");
95         exit(1);
96     };
97 
98     gCtls = new android::net::Controllers();
99     gCtls->init();
100 
101     CommandListener cl;
102     nm->setBroadcaster((SocketListener *) &cl);
103 
104     if (nm->start()) {
105         ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
106         exit(1);
107     }
108 
109     std::unique_ptr<NFLogListener> logListener;
110     {
111         auto result = makeNFLogListener();
112         if (!isOk(result)) {
113             ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
114             exit(1);
115         }
116         logListener = std::move(result.value());
117         auto status = gCtls->wakeupCtrl.init(logListener.get());
118         if (!isOk(result)) {
119             ALOGE("Unable to init WakeupController: %s", toString(result).c_str());
120             // We can still continue without wakeup packet logging.
121         }
122     }
123 
124     // Set local DNS mode, to prevent bionic from proxying
125     // back to this service, recursively.
126     setenv("ANDROID_DNS_MODE", "local", 1);
127     DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
128     if (dpl.startListener()) {
129         ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
130         exit(1);
131     }
132 
133     MDnsSdListener mdnsl;
134     if (mdnsl.startListener()) {
135         ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
136         exit(1);
137     }
138 
139     FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
140     if (fwmarkServer.startListener()) {
141         ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
142         exit(1);
143     }
144 
145     Stopwatch subTime;
146     status_t ret;
147     if ((ret = NetdNativeService::start()) != android::OK) {
148         ALOGE("Unable to start NetdNativeService: %d", ret);
149         exit(1);
150     }
151     ALOGI("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
152 
153     /*
154      * Now that we're up, we can respond to commands. Starting the listener also tells
155      * NetworkManagementService that we are up and that our binder interface is ready.
156      */
157     if (cl.startListener()) {
158         ALOGE("Unable to start CommandListener (%s)", strerror(errno));
159         exit(1);
160     }
161     ALOGI("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
162 
163     write_pid_file();
164 
165     // Now that netd is ready to process commands, advertise service
166     // availability for HAL clients.
167     NetdHwService mHwSvc;
168     if ((ret = mHwSvc.start()) != android::OK) {
169         ALOGE("Unable to start NetdHwService: %d", ret);
170         exit(1);
171     }
172     ALOGI("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
173 
174     ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken()));
175 
176     IPCThreadState::self()->joinThreadPool();
177 
178     ALOGI("Netd exiting");
179 
180     remove_pid_file();
181 
182     exit(0);
183 }
184 
write_pid_file()185 static bool write_pid_file() {
186     char pid_buf[INT32_STRLEN];
187     snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
188 
189     int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
190     if (fd == -1) {
191         ALOGE("Unable to create pid file (%s)", strerror(errno));
192         return false;
193     }
194 
195     // File creation is affected by umask, so make sure the right mode bits are set.
196     if (fchmod(fd, PID_FILE_MODE) == -1) {
197         ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
198         close(fd);
199         remove_pid_file();
200         return false;
201     }
202 
203     if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
204         ALOGE("Unable to write to pid file (%s)", strerror(errno));
205         close(fd);
206         remove_pid_file();
207         return false;
208     }
209     close(fd);
210     return true;
211 }
212 
remove_pid_file()213 static void remove_pid_file() {
214     unlink(PID_FILE_PATH);
215 }
216