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