blob: 72168ec3675bf26acc0d291b025384af04be558d [file] [log] [blame]
Dan Albertbd0b7502015-02-18 18:22:45 -08001/*
2 * Copyright (C) 2015 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#define TRACE_TAG TRACE_ADB
18
19#include <errno.h>
20#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/prctl.h>
24
25#include "adb.h"
26#include "adb_auth.h"
27#include "adb_listeners.h"
28#include "sysdeps.h"
29
30#if !ADB_HOST
31#include <getopt.h>
32
33#include "cutils/properties.h"
34#include "private/android_filesystem_config.h"
35#include "selinux/selinux.h"
36
37#include "qemu_tracing.h"
38#endif
39
40static void adb_cleanup(void)
41{
42 usb_cleanup();
43}
44
45#if ADB_HOST
46#ifdef WORKAROUND_BUG6558362
47#include <sched.h>
48#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
49void adb_set_affinity(void)
50{
51 cpu_set_t cpu_set;
52 const char* cpunum_str = getenv(AFFINITY_ENVVAR);
53 char* strtol_res;
54 int cpu_num;
55
56 if (!cpunum_str || !*cpunum_str)
57 return;
58 cpu_num = strtol(cpunum_str, &strtol_res, 0);
59 if (*strtol_res != '\0')
60 fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
61
62 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
63 D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
64 CPU_ZERO(&cpu_set);
65 CPU_SET(cpu_num, &cpu_set);
66 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
67 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
68 D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
69}
70#endif
71#else /* ADB_HOST */
72static const char *root_seclabel = NULL;
73
74static void drop_capabilities_bounding_set_if_needed() {
75#ifdef ALLOW_ADBD_ROOT
76 char value[PROPERTY_VALUE_MAX];
77 property_get("ro.debuggable", value, "");
78 if (strcmp(value, "1") == 0) {
79 return;
80 }
81#endif
82 int i;
83 for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
84 if (i == CAP_SETUID || i == CAP_SETGID) {
85 // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
86 continue;
87 }
88 int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
89
90 // Some kernels don't have file capabilities compiled in, and
91 // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
92 // die when we see such misconfigured kernels.
93 if ((err < 0) && (errno != EINVAL)) {
94 exit(1);
95 }
96 }
97}
98
99static bool should_drop_privileges() {
100#if defined(ALLOW_ADBD_ROOT)
101 char value[PROPERTY_VALUE_MAX];
102
103 // The emulator is never secure, so don't drop privileges there.
104 // TODO: this seems like a bug --- shouldn't the emulator behave like a device?
105 property_get("ro.kernel.qemu", value, "");
106 if (strcmp(value, "1") == 0) {
107 return false;
108 }
109
110 property_get("ro.secure", value, "1");
111 bool ro_secure = (strcmp(value, "1") == 0);
112
113 // Drop privileges if ro.secure is set...
114 bool drop = ro_secure;
115
116 property_get("ro.debuggable", value, "");
117 bool ro_debuggable = (strcmp(value, "1") == 0);
118 property_get("service.adb.root", value, "");
119 bool adb_root = (strcmp(value, "1") == 0);
120 bool adb_unroot = (strcmp(value, "0") == 0);
121
122 // ...except "adb root" lets you keep privileges in a debuggable build.
123 if (ro_debuggable && adb_root) {
124 drop = false;
125 }
126
127 // ...and "adb unroot" lets you explicitly drop privileges.
128 if (adb_unroot) {
129 drop = true;
130 }
131
132 return drop;
133#else
134 return true; // "adb root" not allowed, always drop privileges.
135#endif /* ALLOW_ADBD_ROOT */
136}
137
138void start_device_log(void)
139{
140 int fd;
141 char path[PATH_MAX];
142 struct tm now;
143 time_t t;
144 char value[PROPERTY_VALUE_MAX];
145
146 // read the trace mask from persistent property persist.adb.trace_mask
147 // give up if the property is not set or cannot be parsed
148 property_get("persist.adb.trace_mask", value, "");
149 if (sscanf(value, "%x", &adb_trace_mask) != 1)
150 return;
151
152 adb_mkdir("/data/adb", 0775);
153 tzset();
154 time(&t);
155 localtime_r(&t, &now);
156 strftime(path, sizeof(path),
157 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
158 &now);
159 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
160 if (fd < 0)
161 return;
162
163 // redirect stdout and stderr to the log file
164 dup2(fd, 1);
165 dup2(fd, 2);
166 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
167 adb_close(fd);
168
169 fd = unix_open("/dev/null", O_RDONLY);
170 dup2(fd, 0);
171 adb_close(fd);
172}
173#endif /* ADB_HOST */
174
175/* Constructs a local name of form tcp:port.
176 * target_str points to the target string, it's content will be overwritten.
177 * target_size is the capacity of the target string.
178 * server_port is the port number to use for the local name.
179 */
180void build_local_name(char* target_str, size_t target_size, int server_port)
181{
182 snprintf(target_str, target_size, "tcp:%d", server_port);
183}
184
185void start_logging(void)
186{
187#if defined(_WIN32)
188 char temp[ MAX_PATH ];
189 FILE* fnul;
190 FILE* flog;
191
192 GetTempPath( sizeof(temp) - 8, temp );
193 strcat( temp, "adb.log" );
194
195 /* Win32 specific redirections */
196 fnul = fopen( "NUL", "rt" );
197 if (fnul != NULL)
198 stdin[0] = fnul[0];
199
200 flog = fopen( temp, "at" );
201 if (flog == NULL)
202 flog = fnul;
203
204 setvbuf( flog, NULL, _IONBF, 0 );
205
206 stdout[0] = flog[0];
207 stderr[0] = flog[0];
208 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
209#else
210 int fd;
211
212 fd = unix_open("/dev/null", O_RDONLY);
213 dup2(fd, 0);
214 adb_close(fd);
215
216 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
217 if(fd < 0) {
218 fd = unix_open("/dev/null", O_WRONLY);
219 }
220 dup2(fd, 1);
221 dup2(fd, 2);
222 adb_close(fd);
223 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
224#endif
225}
226
227int adb_main(int is_daemon, int server_port)
228{
229#if !ADB_HOST
230 int port;
231 char value[PROPERTY_VALUE_MAX];
232
233 umask(000);
234#endif
235
236 atexit(adb_cleanup);
237#if defined(_WIN32)
238 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
239#else
240 // No SIGCHLD. Let the service subproc handle its children.
241 signal(SIGPIPE, SIG_IGN);
242#endif
243
244 init_transport_registration();
245
246#if ADB_HOST
247 HOST = 1;
248
249#ifdef WORKAROUND_BUG6558362
250 if(is_daemon) adb_set_affinity();
251#endif
252 usb_init();
253 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
254 adb_auth_init();
255
256 char local_name[30];
257 build_local_name(local_name, sizeof(local_name), server_port);
258 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
259 exit(1);
260 }
261#else
262 property_get("ro.adb.secure", value, "0");
263 auth_enabled = !strcmp(value, "1");
264 if (auth_enabled)
265 adb_auth_init();
266
267 // Our external storage path may be different than apps, since
268 // we aren't able to bind mount after dropping root.
269 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
270 if (NULL != adb_external_storage) {
271 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
272 } else {
273 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
274 " unchanged.\n");
275 }
276
277 /* add extra groups:
278 ** AID_ADB to access the USB driver
279 ** AID_LOG to read system logs (adb logcat)
280 ** AID_INPUT to diagnose input issues (getevent)
281 ** AID_INET to diagnose network issues (ping)
282 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
283 ** AID_SDCARD_R to allow reading from the SD card
284 ** AID_SDCARD_RW to allow writing to the SD card
285 ** AID_NET_BW_STATS to read out qtaguid statistics
286 */
287 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
288 AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
289 AID_NET_BW_STATS };
290 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
291 exit(1);
292 }
293
294 /* don't listen on a port (default 5037) if running in secure mode */
295 /* don't run as root if we are running in secure mode */
296 if (should_drop_privileges()) {
297 drop_capabilities_bounding_set_if_needed();
298
299 /* then switch user and group to "shell" */
300 if (setgid(AID_SHELL) != 0) {
301 exit(1);
302 }
303 if (setuid(AID_SHELL) != 0) {
304 exit(1);
305 }
306
307 D("Local port disabled\n");
308 } else {
309 char local_name[30];
310 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
311 // b/12587913: fix setcon to allow const pointers
312 if (setcon((char *)root_seclabel) < 0) {
313 exit(1);
314 }
315 }
316 build_local_name(local_name, sizeof(local_name), server_port);
317 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
318 exit(1);
319 }
320 }
321
322 int usb = 0;
323 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
324 // listen on USB
325 usb_init();
326 usb = 1;
327 }
328
329 // If one of these properties is set, also listen on that port
330 // If one of the properties isn't set and we couldn't listen on usb,
331 // listen on the default port.
332 property_get("service.adb.tcp.port", value, "");
333 if (!value[0]) {
334 property_get("persist.adb.tcp.port", value, "");
335 }
336 if (sscanf(value, "%d", &port) == 1 && port > 0) {
337 printf("using port=%d\n", port);
338 // listen on TCP port specified by service.adb.tcp.port property
339 local_init(port);
340 } else if (!usb) {
341 // listen on default port
342 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
343 }
344
345 D("adb_main(): pre init_jdwp()\n");
346 init_jdwp();
347 D("adb_main(): post init_jdwp()\n");
348#endif
349
350 if (is_daemon)
351 {
352 // inform our parent that we are up and running.
353#if defined(_WIN32)
354 DWORD count;
355 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
356#else
357 fprintf(stderr, "OK\n");
358#endif
359 start_logging();
360 }
361 D("Event loop starting\n");
362
363 fdevent_loop();
364
365 usb_cleanup();
366
367 return 0;
368}
369
370int main(int argc, char **argv)
371{
372#if ADB_HOST
373 adb_sysdeps_init();
374 adb_trace_init();
375 D("Handling commandline()\n");
376 return adb_commandline(argc - 1, argv + 1);
377#else
378 /* If adbd runs inside the emulator this will enable adb tracing via
379 * adb-debug qemud service in the emulator. */
380 adb_qemu_trace_init();
381 while(1) {
382 int c;
383 int option_index = 0;
384 static struct option opts[] = {
385 {"root_seclabel", required_argument, 0, 's' },
386 {"device_banner", required_argument, 0, 'b' }
387 };
388 c = getopt_long(argc, argv, "", opts, &option_index);
389 if (c == -1)
390 break;
391 switch (c) {
392 case 's':
393 root_seclabel = optarg;
394 break;
395 case 'b':
396 adb_device_banner = optarg;
397 break;
398 default:
399 break;
400 }
401 }
402
403 start_device_log();
404 D("Handling main()\n");
405 return adb_main(0, DEFAULT_ADB_PORT);
406#endif
407}