• 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 "Controllers.h"
39 #include "CommandListener.h"
40 #include "NetdConstants.h"
41 #include "NetdNativeService.h"
42 #include "NetlinkManager.h"
43 #include "DnsProxyListener.h"
44 #include "MDnsSdListener.h"
45 #include "FwmarkServer.h"
46 
47 using android::status_t;
48 using android::sp;
49 using android::IPCThreadState;
50 using android::ProcessState;
51 using android::defaultServiceManager;
52 using android::net::NetdNativeService;
53 
54 static void blockSigpipe();
55 static void remove_pid_file();
56 static bool write_pid_file();
57 
58 const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
59 const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
60 const mode_t PID_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;  // mode 0644, rw-r--r--
61 
62 android::RWLock android::net::gBigNetdLock;
63 
main()64 int main() {
65     using android::net::gCtls;
66 
67     ALOGI("Netd 1.0 starting");
68     remove_pid_file();
69 
70     blockSigpipe();
71 
72     NetlinkManager *nm = NetlinkManager::Instance();
73     if (nm == nullptr) {
74         ALOGE("Unable to create NetlinkManager");
75         exit(1);
76     };
77 
78     gCtls = new android::net::Controllers();
79     CommandListener cl;
80     nm->setBroadcaster((SocketListener *) &cl);
81 
82     if (nm->start()) {
83         ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
84         exit(1);
85     }
86 
87     // Set local DNS mode, to prevent bionic from proxying
88     // back to this service, recursively.
89     setenv("ANDROID_DNS_MODE", "local", 1);
90     DnsProxyListener dpl(&gCtls->netCtrl);
91     if (dpl.startListener()) {
92         ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
93         exit(1);
94     }
95 
96     MDnsSdListener mdnsl;
97     if (mdnsl.startListener()) {
98         ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
99         exit(1);
100     }
101 
102     FwmarkServer fwmarkServer(&gCtls->netCtrl);
103     if (fwmarkServer.startListener()) {
104         ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
105         exit(1);
106     }
107 
108     status_t ret;
109     if ((ret = NetdNativeService::start()) != android::OK) {
110         ALOGE("Unable to start NetdNativeService: %d", ret);
111         exit(1);
112     }
113 
114     /*
115      * Now that we're up, we can respond to commands. Starting the listener also tells
116      * NetworkManagementService that we are up and that our binder interface is ready.
117      */
118     if (cl.startListener()) {
119         ALOGE("Unable to start CommandListener (%s)", strerror(errno));
120         exit(1);
121     }
122 
123     write_pid_file();
124 
125     IPCThreadState::self()->joinThreadPool();
126 
127     ALOGI("Netd exiting");
128 
129     remove_pid_file();
130 
131     exit(0);
132 }
133 
write_pid_file()134 static bool write_pid_file() {
135     char pid_buf[INT32_STRLEN];
136     snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
137 
138     int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
139     if (fd == -1) {
140         ALOGE("Unable to create pid file (%s)", strerror(errno));
141         return false;
142     }
143 
144     // File creation is affected by umask, so make sure the right mode bits are set.
145     if (fchmod(fd, PID_FILE_MODE) == -1) {
146         ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
147         close(fd);
148         remove_pid_file();
149         return false;
150     }
151 
152     if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
153         ALOGE("Unable to write to pid file (%s)", strerror(errno));
154         close(fd);
155         remove_pid_file();
156         return false;
157     }
158     close(fd);
159     return true;
160 }
161 
remove_pid_file()162 static void remove_pid_file() {
163     unlink(PID_FILE_PATH);
164 }
165 
blockSigpipe()166 static void blockSigpipe()
167 {
168     sigset_t mask;
169 
170     sigemptyset(&mask);
171     sigaddset(&mask, SIGPIPE);
172     if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
173         ALOGW("WARNING: SIGPIPE not blocked\n");
174 }
175