blob: c1e4b13dc42c93fc3b72eb374bcebf9dc8615047 [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 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}
Dan Albertbd0b7502015-02-18 18:22:45 -0800151#endif /* ADB_HOST */
152
153/* Constructs a local name of form tcp:port.
154 * target_str points to the target string, it's content will be overwritten.
155 * target_size is the capacity of the target string.
156 * server_port is the port number to use for the local name.
157 */
158void build_local_name(char* target_str, size_t target_size, int server_port)
159{
160 snprintf(target_str, target_size, "tcp:%d", server_port);
161}
162
163void start_logging(void)
164{
165#if defined(_WIN32)
166 char temp[ MAX_PATH ];
167 FILE* fnul;
168 FILE* flog;
169
170 GetTempPath( sizeof(temp) - 8, temp );
171 strcat( temp, "adb.log" );
172
173 /* Win32 specific redirections */
174 fnul = fopen( "NUL", "rt" );
175 if (fnul != NULL)
176 stdin[0] = fnul[0];
177
178 flog = fopen( temp, "at" );
179 if (flog == NULL)
180 flog = fnul;
181
182 setvbuf( flog, NULL, _IONBF, 0 );
183
184 stdout[0] = flog[0];
185 stderr[0] = flog[0];
186 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
187#else
188 int fd;
189
190 fd = unix_open("/dev/null", O_RDONLY);
191 dup2(fd, 0);
192 adb_close(fd);
193
194 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
195 if(fd < 0) {
196 fd = unix_open("/dev/null", O_WRONLY);
197 }
198 dup2(fd, 1);
199 dup2(fd, 2);
200 adb_close(fd);
201 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
202#endif
203}
204
205int adb_main(int is_daemon, int server_port)
206{
207#if !ADB_HOST
208 int port;
209 char value[PROPERTY_VALUE_MAX];
210
211 umask(000);
212#endif
213
214 atexit(adb_cleanup);
215#if defined(_WIN32)
216 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
217#else
218 // No SIGCHLD. Let the service subproc handle its children.
219 signal(SIGPIPE, SIG_IGN);
220#endif
221
222 init_transport_registration();
223
224#if ADB_HOST
225 HOST = 1;
226
227#ifdef WORKAROUND_BUG6558362
228 if(is_daemon) adb_set_affinity();
229#endif
230 usb_init();
231 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
232 adb_auth_init();
233
234 char local_name[30];
235 build_local_name(local_name, sizeof(local_name), server_port);
236 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
237 exit(1);
238 }
239#else
Pavel Labath64d9adc2015-03-17 11:03:36 -0700240 // We need to call this even if auth isn't enabled because the file
241 // descriptor will always be open.
242 adbd_cloexec_auth_socket();
243
Dan Albertbd0b7502015-02-18 18:22:45 -0800244 property_get("ro.adb.secure", value, "0");
245 auth_enabled = !strcmp(value, "1");
246 if (auth_enabled)
Pavel Labath64d9adc2015-03-17 11:03:36 -0700247 adbd_auth_init();
Dan Albertbd0b7502015-02-18 18:22:45 -0800248
249 // Our external storage path may be different than apps, since
250 // we aren't able to bind mount after dropping root.
251 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
252 if (NULL != adb_external_storage) {
253 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
254 } else {
255 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
256 " unchanged.\n");
257 }
258
259 /* add extra groups:
260 ** AID_ADB to access the USB driver
261 ** AID_LOG to read system logs (adb logcat)
262 ** AID_INPUT to diagnose input issues (getevent)
263 ** AID_INET to diagnose network issues (ping)
264 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
265 ** AID_SDCARD_R to allow reading from the SD card
266 ** AID_SDCARD_RW to allow writing to the SD card
267 ** AID_NET_BW_STATS to read out qtaguid statistics
268 */
269 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
270 AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
271 AID_NET_BW_STATS };
272 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
273 exit(1);
274 }
275
276 /* don't listen on a port (default 5037) if running in secure mode */
277 /* don't run as root if we are running in secure mode */
278 if (should_drop_privileges()) {
279 drop_capabilities_bounding_set_if_needed();
280
281 /* then switch user and group to "shell" */
282 if (setgid(AID_SHELL) != 0) {
283 exit(1);
284 }
285 if (setuid(AID_SHELL) != 0) {
286 exit(1);
287 }
288
289 D("Local port disabled\n");
290 } else {
291 char local_name[30];
292 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
293 // b/12587913: fix setcon to allow const pointers
294 if (setcon((char *)root_seclabel) < 0) {
295 exit(1);
296 }
297 }
298 build_local_name(local_name, sizeof(local_name), server_port);
299 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
300 exit(1);
301 }
302 }
303
304 int usb = 0;
305 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
306 // listen on USB
307 usb_init();
308 usb = 1;
309 }
310
311 // If one of these properties is set, also listen on that port
312 // If one of the properties isn't set and we couldn't listen on usb,
313 // listen on the default port.
314 property_get("service.adb.tcp.port", value, "");
315 if (!value[0]) {
316 property_get("persist.adb.tcp.port", value, "");
317 }
318 if (sscanf(value, "%d", &port) == 1 && port > 0) {
319 printf("using port=%d\n", port);
320 // listen on TCP port specified by service.adb.tcp.port property
321 local_init(port);
322 } else if (!usb) {
323 // listen on default port
324 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
325 }
326
327 D("adb_main(): pre init_jdwp()\n");
328 init_jdwp();
329 D("adb_main(): post init_jdwp()\n");
330#endif
331
332 if (is_daemon)
333 {
334 // inform our parent that we are up and running.
335#if defined(_WIN32)
336 DWORD count;
337 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
338#else
339 fprintf(stderr, "OK\n");
340#endif
341 start_logging();
342 }
343 D("Event loop starting\n");
344
345 fdevent_loop();
346
347 usb_cleanup();
348
349 return 0;
350}
351
Dan Albertea2175a2015-03-08 21:12:08 -0700352int main(int argc, char **argv) {
Dan Albertbd0b7502015-02-18 18:22:45 -0800353#if ADB_HOST
354 adb_sysdeps_init();
Dan Albertea2175a2015-03-08 21:12:08 -0700355#endif
Dan Albertbd0b7502015-02-18 18:22:45 -0800356 adb_trace_init();
Dan Albertea2175a2015-03-08 21:12:08 -0700357
358#if ADB_HOST
Dan Albertbd0b7502015-02-18 18:22:45 -0800359 D("Handling commandline()\n");
Dan Albertbac34742015-02-25 17:51:28 -0800360 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
Dan Albertbd0b7502015-02-18 18:22:45 -0800361#else
362 /* If adbd runs inside the emulator this will enable adb tracing via
363 * adb-debug qemud service in the emulator. */
364 adb_qemu_trace_init();
Dan Albertea2175a2015-03-08 21:12:08 -0700365 while (1) {
Dan Albertbd0b7502015-02-18 18:22:45 -0800366 int c;
367 int option_index = 0;
368 static struct option opts[] = {
369 {"root_seclabel", required_argument, 0, 's' },
370 {"device_banner", required_argument, 0, 'b' }
371 };
372 c = getopt_long(argc, argv, "", opts, &option_index);
373 if (c == -1)
374 break;
375 switch (c) {
376 case 's':
377 root_seclabel = optarg;
378 break;
379 case 'b':
380 adb_device_banner = optarg;
381 break;
382 default:
383 break;
384 }
385 }
386
Dan Albertbd0b7502015-02-18 18:22:45 -0800387 D("Handling main()\n");
388 return adb_main(0, DEFAULT_ADB_PORT);
389#endif
390}