blob: 02acae28c2e2b98755d7c52b98098390634e7ccb [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
111 // The emulator is never secure, so don't drop privileges there.
112 // TODO: this seems like a bug --- shouldn't the emulator behave like a device?
113 property_get("ro.kernel.qemu", value, "");
114 if (strcmp(value, "1") == 0) {
115 return false;
116 }
117
Dan Albert13f9c402015-02-19 11:03:26 -0800118 // The properties that affect `adb root` and `adb unroot` are ro.secure and
119 // ro.debuggable. In this context the names don't make the expected behavior
120 // particularly obvious.
121 //
122 // ro.debuggable:
123 // Allowed to become root, but not necessarily the default. Set to 1 on
124 // eng and userdebug builds.
125 //
126 // ro.secure:
127 // Drop privileges by default. Set to 1 on userdebug and user builds.
Dan Albertbd0b7502015-02-18 18:22:45 -0800128 property_get("ro.secure", value, "1");
129 bool ro_secure = (strcmp(value, "1") == 0);
130
Dan Albert13f9c402015-02-19 11:03:26 -0800131 property_get("ro.debuggable", value, "");
132 bool ro_debuggable = (strcmp(value, "1") == 0);
133
Dan Albertbd0b7502015-02-18 18:22:45 -0800134 // Drop privileges if ro.secure is set...
135 bool drop = ro_secure;
136
Dan Albertbd0b7502015-02-18 18:22:45 -0800137 property_get("service.adb.root", value, "");
138 bool adb_root = (strcmp(value, "1") == 0);
139 bool adb_unroot = (strcmp(value, "0") == 0);
140
141 // ...except "adb root" lets you keep privileges in a debuggable build.
142 if (ro_debuggable && adb_root) {
143 drop = false;
144 }
145
146 // ...and "adb unroot" lets you explicitly drop privileges.
147 if (adb_unroot) {
148 drop = true;
149 }
150
151 return drop;
152#else
153 return true; // "adb root" not allowed, always drop privileges.
154#endif /* ALLOW_ADBD_ROOT */
155}
156
157void start_device_log(void)
158{
159 int fd;
160 char path[PATH_MAX];
161 struct tm now;
162 time_t t;
163 char value[PROPERTY_VALUE_MAX];
164
165 // read the trace mask from persistent property persist.adb.trace_mask
166 // give up if the property is not set or cannot be parsed
167 property_get("persist.adb.trace_mask", value, "");
168 if (sscanf(value, "%x", &adb_trace_mask) != 1)
169 return;
170
171 adb_mkdir("/data/adb", 0775);
172 tzset();
173 time(&t);
174 localtime_r(&t, &now);
175 strftime(path, sizeof(path),
176 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
177 &now);
178 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
179 if (fd < 0)
180 return;
181
182 // redirect stdout and stderr to the log file
183 dup2(fd, 1);
184 dup2(fd, 2);
185 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
186 adb_close(fd);
187
188 fd = unix_open("/dev/null", O_RDONLY);
189 dup2(fd, 0);
190 adb_close(fd);
191}
192#endif /* ADB_HOST */
193
194/* Constructs a local name of form tcp:port.
195 * target_str points to the target string, it's content will be overwritten.
196 * target_size is the capacity of the target string.
197 * server_port is the port number to use for the local name.
198 */
199void build_local_name(char* target_str, size_t target_size, int server_port)
200{
201 snprintf(target_str, target_size, "tcp:%d", server_port);
202}
203
204void start_logging(void)
205{
206#if defined(_WIN32)
207 char temp[ MAX_PATH ];
208 FILE* fnul;
209 FILE* flog;
210
211 GetTempPath( sizeof(temp) - 8, temp );
212 strcat( temp, "adb.log" );
213
214 /* Win32 specific redirections */
215 fnul = fopen( "NUL", "rt" );
216 if (fnul != NULL)
217 stdin[0] = fnul[0];
218
219 flog = fopen( temp, "at" );
220 if (flog == NULL)
221 flog = fnul;
222
223 setvbuf( flog, NULL, _IONBF, 0 );
224
225 stdout[0] = flog[0];
226 stderr[0] = flog[0];
227 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
228#else
229 int fd;
230
231 fd = unix_open("/dev/null", O_RDONLY);
232 dup2(fd, 0);
233 adb_close(fd);
234
235 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
236 if(fd < 0) {
237 fd = unix_open("/dev/null", O_WRONLY);
238 }
239 dup2(fd, 1);
240 dup2(fd, 2);
241 adb_close(fd);
242 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
243#endif
244}
245
246int adb_main(int is_daemon, int server_port)
247{
248#if !ADB_HOST
249 int port;
250 char value[PROPERTY_VALUE_MAX];
251
252 umask(000);
253#endif
254
255 atexit(adb_cleanup);
256#if defined(_WIN32)
257 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
258#else
259 // No SIGCHLD. Let the service subproc handle its children.
260 signal(SIGPIPE, SIG_IGN);
261#endif
262
263 init_transport_registration();
264
265#if ADB_HOST
266 HOST = 1;
267
268#ifdef WORKAROUND_BUG6558362
269 if(is_daemon) adb_set_affinity();
270#endif
271 usb_init();
272 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
273 adb_auth_init();
274
275 char local_name[30];
276 build_local_name(local_name, sizeof(local_name), server_port);
277 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
278 exit(1);
279 }
280#else
281 property_get("ro.adb.secure", value, "0");
282 auth_enabled = !strcmp(value, "1");
283 if (auth_enabled)
284 adb_auth_init();
285
286 // Our external storage path may be different than apps, since
287 // we aren't able to bind mount after dropping root.
288 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
289 if (NULL != adb_external_storage) {
290 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
291 } else {
292 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
293 " unchanged.\n");
294 }
295
296 /* add extra groups:
297 ** AID_ADB to access the USB driver
298 ** AID_LOG to read system logs (adb logcat)
299 ** AID_INPUT to diagnose input issues (getevent)
300 ** AID_INET to diagnose network issues (ping)
301 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
302 ** AID_SDCARD_R to allow reading from the SD card
303 ** AID_SDCARD_RW to allow writing to the SD card
304 ** AID_NET_BW_STATS to read out qtaguid statistics
305 */
306 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
307 AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
308 AID_NET_BW_STATS };
309 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
310 exit(1);
311 }
312
313 /* don't listen on a port (default 5037) if running in secure mode */
314 /* don't run as root if we are running in secure mode */
315 if (should_drop_privileges()) {
316 drop_capabilities_bounding_set_if_needed();
317
318 /* then switch user and group to "shell" */
319 if (setgid(AID_SHELL) != 0) {
320 exit(1);
321 }
322 if (setuid(AID_SHELL) != 0) {
323 exit(1);
324 }
325
326 D("Local port disabled\n");
327 } else {
328 char local_name[30];
329 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
330 // b/12587913: fix setcon to allow const pointers
331 if (setcon((char *)root_seclabel) < 0) {
332 exit(1);
333 }
334 }
335 build_local_name(local_name, sizeof(local_name), server_port);
336 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
337 exit(1);
338 }
339 }
340
341 int usb = 0;
342 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
343 // listen on USB
344 usb_init();
345 usb = 1;
346 }
347
348 // If one of these properties is set, also listen on that port
349 // If one of the properties isn't set and we couldn't listen on usb,
350 // listen on the default port.
351 property_get("service.adb.tcp.port", value, "");
352 if (!value[0]) {
353 property_get("persist.adb.tcp.port", value, "");
354 }
355 if (sscanf(value, "%d", &port) == 1 && port > 0) {
356 printf("using port=%d\n", port);
357 // listen on TCP port specified by service.adb.tcp.port property
358 local_init(port);
359 } else if (!usb) {
360 // listen on default port
361 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
362 }
363
364 D("adb_main(): pre init_jdwp()\n");
365 init_jdwp();
366 D("adb_main(): post init_jdwp()\n");
367#endif
368
369 if (is_daemon)
370 {
371 // inform our parent that we are up and running.
372#if defined(_WIN32)
373 DWORD count;
374 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
375#else
376 fprintf(stderr, "OK\n");
377#endif
378 start_logging();
379 }
380 D("Event loop starting\n");
381
382 fdevent_loop();
383
384 usb_cleanup();
385
386 return 0;
387}
388
389int main(int argc, char **argv)
390{
391#if ADB_HOST
392 adb_sysdeps_init();
393 adb_trace_init();
394 D("Handling commandline()\n");
395 return adb_commandline(argc - 1, argv + 1);
396#else
397 /* If adbd runs inside the emulator this will enable adb tracing via
398 * adb-debug qemud service in the emulator. */
399 adb_qemu_trace_init();
400 while(1) {
401 int c;
402 int option_index = 0;
403 static struct option opts[] = {
404 {"root_seclabel", required_argument, 0, 's' },
405 {"device_banner", required_argument, 0, 'b' }
406 };
407 c = getopt_long(argc, argv, "", opts, &option_index);
408 if (c == -1)
409 break;
410 switch (c) {
411 case 's':
412 root_seclabel = optarg;
413 break;
414 case 'b':
415 adb_device_banner = optarg;
416 break;
417 default:
418 break;
419 }
420 }
421
422 start_device_log();
423 D("Handling main()\n");
424 return adb_main(0, DEFAULT_ADB_PORT);
425#endif
426}