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