-
Notifications
You must be signed in to change notification settings - Fork 0
/
androresolvd.c
182 lines (160 loc) · 5.41 KB
/
androresolvd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (C) 2012 Sven-Ola, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Small user space daemon to get the Android DNS server setting and
* maintain the resolver config on a regulary basis. Note, that this
* daemon has to be started by some running android programs, in order
* to have the ANDROID_PROPERTY_WORKSPACE environment variable and
* the /dev/ashmem file descriptor (usually #9) that is inherited from
* the calling process. Another note: cannot be started from a login
* shell, because "bash --login" closes all inherited file descriptors.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "sys/system_properties.h"
/* For futex syscall */
#include <linux/futex.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
/*
* This syscall is required by Android's properties handling in system_properties.c
*/
int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout)
{
return syscall(SYS_futex, ftx, FUTEX_WAIT, val, (void *)timeout, NULL, NULL);
}
#define DNS1_PROP "net.dns1"
#define DNS2_PROP "net.dns2"
#define SBIN_RESOLV_CONF "/sbin/resolvconf"
static int debug = 0;
static int update_resolvconf(int add, const char* iface, const char* dns1, const char* dns2) {
FILE* f = 0;
char s[256];
snprintf(s, sizeof(s), "%s %s %s", SBIN_RESOLV_CONF, add ? "-a" : "-d", iface);
if (0 != (f = popen(s, "w"))) {
if (debug) printf("piping to \"%s\":\n", s);
if (dns1 && *dns1) {
if (debug) printf("nameserver %s\n", dns1);
fprintf(f, "nameserver %s\n", dns1);
}
if (dns2 && *dns2) {
if (debug) printf("nameserver %s\n", dns2);
fprintf(f, "nameserver %s\n", dns2);
}
if (0 <= fclose(f)) return 0;
perror("pclose");
}
else {
perror(s);
}
return -1;
}
static void daemonize(void)
{
int i;
pid_t pid, sid;
/* already a daemon */
if (getppid() == 1) return;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
perror("cannot fork");
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) exit(EXIT_SUCCESS);
/* At this point we are executing as the child process */
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) exit(EXIT_FAILURE);
/* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */
if ((chdir("/")) < 0) exit(EXIT_FAILURE);
/* Close all file descriptors, otherwise e.g. "adb shell" hangs during exit */
for(i = 0; i < getdtablesize(); i++) {
close(i);
}
}
int __system_properties_init(void);
static void sig_handler(int signum)
{
switch(signum) {
case SIGINT:
case SIGTERM:
update_resolvconf(0 == 1, "android", 0, 0);
exit(0);
break;
}
}
int main(int argc, char* argv[]) {
int ret;
debug = (1 < argc && 0 == strcmp("-d", argv[1]));
if (0 == (ret = __system_properties_init())) {
char dns1[PROP_VALUE_MAX];
char dns2[PROP_VALUE_MAX];
if (0 == __system_property_get(DNS1_PROP, dns1)) dns1[0] = 0;
if (0 == __system_property_get(DNS2_PROP, dns2)) dns2[0] = 0;
if (0 == (ret = update_resolvconf(0 == 0, "android", dns1, dns2))) {
struct sigaction sa;
if (!debug) daemonize();
memset(&sa, sizeof(sa), 0);
sa.sa_handler = sig_handler;
sa.sa_flags = SA_RESETHAND;
sigemptyset(&sa.sa_mask);
if (-1 == sigaction(SIGINT, &sa, NULL) || -1 == sigaction(SIGTERM, &sa, NULL)) {
perror("sigaction");
exit(EXIT_FAILURE);
}
while(1) {
char new_dns1[PROP_VALUE_MAX];
char new_dns2[PROP_VALUE_MAX];
sleep(2);
if (0 == __system_property_get(DNS1_PROP, new_dns1)) new_dns1[0] = 0;
if (0 == __system_property_get(DNS2_PROP, new_dns2)) new_dns2[0] = 0;
if (0 != strcmp(dns1, new_dns1) || 0 != strcmp(dns2, new_dns2)) {
strcpy(dns1, new_dns1);
strcpy(dns2, new_dns2);
update_resolvconf(0 == 0, "android", dns1, dns2);
}
}
}
else {
perror("run "SBIN_RESOLV_CONF" failed");
}
}
else {
fprintf(stderr, "Cannot access system properties via ANDROID_PROPERTY_WORKSPACE environment setting.\n");
}
return ret;
}