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