blob: 4a57b428dd45ff6727d5d154ca75ae32119c0698 [file] [log] [blame]
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -07001/* Copyright (C) 2006-2010 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
12
13#include "libslirp.h"
14#include "qemu-common.h"
15#include "sysemu.h"
16#include "modem_driver.h"
17#include "proxy_http.h"
18
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -070019#include "android/android.h"
20#include "android/globals.h"
21#include "android/hw-sensors.h"
22#include "android/utils/debug.h"
23#include "android/utils/path.h"
24#include "android/utils/system.h"
25#include "android/utils/bufprint.h"
26
27#define D(...) do { if (VERBOSE_CHECK(init)) dprint(__VA_ARGS__); } while (0)
28
29#ifdef ANDROID_SDK_TOOLS_REVISION
30# define VERSION_STRING STRINGIFY(ANDROID_SDK_TOOLS_REVISION)".0"
31#else
32# define VERSION_STRING "standalone"
33#endif
34
35extern int control_console_start( int port ); /* in control.c */
36
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -070037/* Contains arguments for -android-ports option. */
38char* android_op_ports = NULL;
39/* Contains arguments for -android-port option. */
40char* android_op_port = NULL;
41/* Contains arguments for -android-report-console option. */
42char* android_op_report_console = NULL;
43/* Contains arguments for -http-proxy option. */
44char* op_http_proxy = NULL;
45
46
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -070047/*** APPLICATION DIRECTORY
48 *** Where are we ?
49 ***/
50
51const char* get_app_dir(void)
52{
53 char buffer[1024];
54 char* p = buffer;
55 char* end = p + sizeof(buffer);
56 p = bufprint_app_dir(p, end);
57 if (p >= end)
58 return NULL;
59
60 return strdup(buffer);
61}
62
63enum {
64 REPORT_CONSOLE_SERVER = (1 << 0),
65 REPORT_CONSOLE_MAX = (1 << 1)
66};
67
68static int
69get_report_console_options( char* end, int *maxtries )
70{
71 int flags = 0;
72
73 if (end == NULL || *end == 0)
74 return 0;
75
76 if (end[0] != ',') {
77 derror( "socket port/path can be followed by [,<option>]+ only\n");
78 exit(3);
79 }
80 end += 1;
81 while (*end) {
82 char* p = strchr(end, ',');
83 if (p == NULL)
84 p = end + strlen(end);
85
86 if (memcmp( end, "server", p-end ) == 0)
87 flags |= REPORT_CONSOLE_SERVER;
88 else if (memcmp( end, "max=", 4) == 0) {
89 end += 4;
90 *maxtries = strtol( end, NULL, 10 );
91 flags |= REPORT_CONSOLE_MAX;
92 } else {
93 derror( "socket port/path can be followed by [,server][,max=<count>] only\n");
94 exit(3);
95 }
96
97 end = p;
98 if (*end)
99 end += 1;
100 }
101 return flags;
102}
103
104static void
105report_console( const char* proto_port, int console_port )
106{
107 int s = -1, s2;
108 int maxtries = 10;
109 int flags = 0;
110 signal_state_t sigstate;
111
112 disable_sigalrm( &sigstate );
113
114 if ( !strncmp( proto_port, "tcp:", 4) ) {
115 char* end;
116 long port = strtol(proto_port + 4, &end, 10);
117
118 flags = get_report_console_options( end, &maxtries );
119
120 if (flags & REPORT_CONSOLE_SERVER) {
121 s = socket_loopback_server( port, SOCKET_STREAM );
122 if (s < 0) {
123 fprintf(stderr, "could not create server socket on TCP:%ld: %s\n",
124 port, errno_str);
125 exit(3);
126 }
127 } else {
128 for ( ; maxtries > 0; maxtries-- ) {
129 D("trying to find console-report client on tcp:%d", port);
130 s = socket_loopback_client( port, SOCKET_STREAM );
131 if (s >= 0)
132 break;
133
134 sleep_ms(1000);
135 }
136 if (s < 0) {
137 fprintf(stderr, "could not connect to server on TCP:%ld: %s\n",
138 port, errno_str);
139 exit(3);
140 }
141 }
142 } else if ( !strncmp( proto_port, "unix:", 5) ) {
143#ifdef _WIN32
144 fprintf(stderr, "sorry, the unix: protocol is not supported on Win32\n");
145 exit(3);
146#else
147 char* path = strdup(proto_port+5);
148 char* end = strchr(path, ',');
149 if (end != NULL) {
150 flags = get_report_console_options( end, &maxtries );
151 *end = 0;
152 }
153 if (flags & REPORT_CONSOLE_SERVER) {
154 s = socket_unix_server( path, SOCKET_STREAM );
155 if (s < 0) {
156 fprintf(stderr, "could not bind unix socket on '%s': %s\n",
157 proto_port+5, errno_str);
158 exit(3);
159 }
160 } else {
161 for ( ; maxtries > 0; maxtries-- ) {
162 s = socket_unix_client( path, SOCKET_STREAM );
163 if (s >= 0)
164 break;
165
166 sleep_ms(1000);
167 }
168 if (s < 0) {
169 fprintf(stderr, "could not connect to unix socket on '%s': %s\n",
170 path, errno_str);
171 exit(3);
172 }
173 }
174 free(path);
175#endif
176 } else {
177 fprintf(stderr, "-report-console must be followed by a 'tcp:<port>' or 'unix:<path>'\n");
178 exit(3);
179 }
180
181 if (flags & REPORT_CONSOLE_SERVER) {
182 int tries = 3;
183 D( "waiting for console-reporting client" );
184 do {
185 s2 = socket_accept(s, NULL);
186 } while (s2 < 0 && --tries > 0);
187
188 if (s2 < 0) {
189 fprintf(stderr, "could not accept console-reporting client connection: %s\n",
190 errno_str);
191 exit(3);
192 }
193
194 socket_close(s);
195 s = s2;
196 }
197
198 /* simply send the console port in text */
199 {
200 char temp[12];
201 snprintf( temp, sizeof(temp), "%d", console_port );
202
203 if (socket_send(s, temp, strlen(temp)) < 0) {
204 fprintf(stderr, "could not send console number report: %d: %s\n",
205 errno, errno_str );
206 exit(3);
207 }
208 socket_close(s);
209 }
210 D( "console port number sent to remote. resuming boot" );
211
212 restore_sigalrm (&sigstate);
213}
214
215/* this function is called from qemu_main() once all arguments have been parsed
216 * it should be used to setup any Android-specific items in the emulation before the
217 * main loop runs
218 */
219void android_emulation_setup( void )
220{
221 int tries = 16;
222 int base_port = 5554;
223 int adb_host_port = 5037; // adb's default
224 int success = 0;
225 int s;
226 uint32_t guest_ip;
227
228 /* Set the port where the emulator expects adb to run on the host
229 * machine */
230 char* adb_host_port_str = getenv( "ANDROID_ADB_SERVER_PORT" );
231 if ( adb_host_port_str && strlen( adb_host_port_str ) > 0 ) {
232 adb_host_port = (int) strtol( adb_host_port_str, NULL, 0 );
233 if ( adb_host_port <= 0 ) {
234 derror( "env var ANDROID_ADB_SERVER_PORT must be a number > 0. Got \"%s\"\n",
235 adb_host_port_str );
236 exit(1);
237 }
238 }
239
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700240 inet_strtoip("10.0.2.15", &guest_ip);
241
242#if 0
243 if (opts->adb_port) {
244 fprintf( stderr, "option -adb-port is obsolete, use -port instead\n" );
245 exit(1);
246 }
247#endif
248
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700249 if (android_op_port && android_op_ports) {
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700250 fprintf( stderr, "options -port and -ports cannot be used together.\n");
251 exit(1);
252 }
253
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700254 if (android_op_ports) {
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700255 char* comma_location;
256 char* end;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700257 int console_port = strtol( android_op_ports, &comma_location, 0 );
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700258
259 if ( comma_location == NULL || *comma_location != ',' ) {
260 derror( "option -ports must be followed by two comma separated positive integer numbers" );
261 exit(1);
262 }
263
264 int adb_port = strtol( comma_location+1, &end, 0 );
265
266 if ( end == NULL || *end ) {
267 derror( "option -ports must be followed by two comma separated positive integer numbers" );
268 exit(1);
269 }
270
271 if ( console_port == adb_port ) {
272 derror( "option -ports must be followed by two different integer numbers" );
273 exit(1);
274 }
275
276 // Set up redirect from host to guest system. adbd on the guest listens
277 // on 5555.
278 slirp_redir( 0, adb_port, guest_ip, 5555 );
279 if ( control_console_start( console_port ) < 0 ) {
280 slirp_unredir( 0, adb_port );
281 }
282
283 base_port = console_port;
284 } else {
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700285 if (android_op_port) {
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700286 char* end;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700287 int port = strtol( android_op_port, &end, 0 );
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700288 if ( end == NULL || *end ||
289 (unsigned)((port - base_port) >> 1) >= (unsigned)tries ) {
290 derror( "option -port must be followed by an even integer number between %d and %d\n",
291 base_port, base_port + (tries-1)*2 );
292 exit(1);
293 }
294 if ( (port & 1) != 0 ) {
295 port &= ~1;
296 dwarning( "option -port must be followed by an even integer, using port number %d\n",
297 port );
298 }
299 base_port = port;
300 tries = 1;
301 }
302
303 for ( ; tries > 0; tries--, base_port += 2 ) {
304
305 /* setup first redirection for ADB, the Android Debug Bridge */
306 if ( slirp_redir( 0, base_port+1, guest_ip, 5555 ) < 0 )
307 continue;
308
309 /* setup second redirection for the emulator console */
310 if ( control_console_start( base_port ) < 0 ) {
311 slirp_unredir( 0, base_port+1 );
312 continue;
313 }
314
315 D( "control console listening on port %d, ADB on port %d", base_port, base_port+1 );
316 success = 1;
317 break;
318 }
319
320 if (!success) {
321 fprintf(stderr, "it seems too many emulator instances are running on this machine. Aborting\n" );
322 exit(1);
323 }
324 }
325
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700326 if (android_op_report_console) {
327 report_console(android_op_report_console, base_port);
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700328 }
329
330 android_modem_init( base_port );
331
332 android_base_port = base_port;
333 /* send a simple message to the ADB host server to tell it we just started.
334 * it should be listening on port 5037. if we can't reach it, don't bother
335 */
336 do
337 {
338 SockAddress addr;
339 char tmp[32];
340
341 s = socket_create_inet( SOCKET_STREAM );
342 if (s < 0) {
343 D("can't create socket to talk to the ADB server");
344 break;
345 }
346
347 sock_address_init_inet( &addr, SOCK_ADDRESS_INET_LOOPBACK, adb_host_port );
348 if (socket_connect( s, &addr ) < 0) {
349 D("can't connect to ADB server: %s", errno_str );
350 break;
351 }
352
353 sprintf(tmp,"0012host:emulator:%d",base_port+1);
354 socket_send(s, tmp, 18+4);
355 D("sent '%s' to ADB server", tmp);
356 }
357 while (0);
358
359 if (s >= 0)
360 socket_close(s);
361
362 /* setup the http proxy, if any */
363 if (VERBOSE_CHECK(proxy))
364 proxy_set_verbose(1);
365
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700366 if (!op_http_proxy) {
367 op_http_proxy = getenv("http_proxy");
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700368 }
369
370 do
371 {
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700372 const char* env = op_http_proxy;
Vladimir Chtchetkined27aca12010-05-13 11:04:42 -0700373 int envlen;
374 ProxyOption option_tab[4];
375 ProxyOption* option = option_tab;
376 char* p;
377 char* q;
378 const char* proxy_name;
379 int proxy_name_len;
380 int proxy_port;
381
382 if (!env)
383 break;
384
385 envlen = strlen(env);
386
387 /* skip the 'http://' header, if present */
388 if (envlen >= 7 && !memcmp(env, "http://", 7)) {
389 env += 7;
390 envlen -= 7;
391 }
392
393 /* do we have a username:password pair ? */
394 p = strchr(env, '@');
395 if (p != 0) {
396 q = strchr(env, ':');
397 if (q == NULL) {
398 BadHttpProxyFormat:
399 dprint("http_proxy format unsupported, try 'proxy:port' or 'username:password@proxy:port'");
400 break;
401 }
402
403 option->type = PROXY_OPTION_AUTH_USERNAME;
404 option->string = env;
405 option->string_len = q - env;
406 option++;
407
408 option->type = PROXY_OPTION_AUTH_PASSWORD;
409 option->string = q+1;
410 option->string_len = p - (q+1);
411 option++;
412
413 env = p+1;
414 }
415
416 p = strchr(env,':');
417 if (p == NULL)
418 goto BadHttpProxyFormat;
419
420 proxy_name = env;
421 proxy_name_len = p - env;
422 proxy_port = atoi(p+1);
423
424 D( "setting up http proxy: server=%.*s port=%d",
425 proxy_name_len, proxy_name, proxy_port );
426
427 if ( proxy_http_setup( proxy_name, proxy_name_len, proxy_port,
428 option - option_tab, option_tab ) < 0 )
429 {
430 dprint( "http proxy setup failed, check your $http_proxy variable");
431 }
432 }
433 while (0);
434
435 /* initialize sensors, this must be done here due to timer issues */
436 android_hw_sensors_init();
437
438 /* cool, now try to run the "ddms ping" command, which will take care of pinging usage
439 * if the user agreed for it. the emulator itself never sends anything to any outside
440 * machine
441 */
442 {
443#ifdef _WIN32
444# define _ANDROID_PING_PROGRAM "ddms.bat"
445#else
446# define _ANDROID_PING_PROGRAM "ddms"
447#endif
448
449 char tmp[PATH_MAX];
450 const char* appdir = get_app_dir();
451
452 if (snprintf( tmp, PATH_MAX, "%s%s%s", appdir, PATH_SEP,
453 _ANDROID_PING_PROGRAM ) >= PATH_MAX) {
454 dprint( "Application directory too long: %s", appdir);
455 return;
456 }
457
458 /* if the program isn't there, don't bother */
459 D( "ping program: %s", tmp);
460 if (path_exists(tmp)) {
461#ifdef _WIN32
462 STARTUPINFO startup;
463 PROCESS_INFORMATION pinfo;
464
465 ZeroMemory( &startup, sizeof(startup) );
466 startup.cb = sizeof(startup);
467 startup.dwFlags = STARTF_USESHOWWINDOW;
468 startup.wShowWindow = SW_SHOWMINIMIZED;
469
470 ZeroMemory( &pinfo, sizeof(pinfo) );
471
472 char* comspec = getenv("COMSPEC");
473 if (!comspec) comspec = "cmd.exe";
474
475 // Run
476 char args[PATH_MAX + 30];
477 if (snprintf( args, PATH_MAX, "/C \"%s\" ping emulator " VERSION_STRING,
478 tmp) >= PATH_MAX ) {
479 D( "DDMS path too long: %s", tmp);
480 return;
481 }
482
483 CreateProcess(
484 comspec, /* program path */
485 args, /* command line args */
486 NULL, /* process handle is not inheritable */
487 NULL, /* thread handle is not inheritable */
488 FALSE, /* no, don't inherit any handles */
489 DETACHED_PROCESS, /* the new process doesn't have a console */
490 NULL, /* use parent's environment block */
491 NULL, /* use parent's starting directory */
492 &startup, /* startup info, i.e. std handles */
493 &pinfo );
494
495 D( "ping command: %s %s", comspec, args );
496#else
497 int pid;
498
499 /* disable SIGALRM for the fork(), the periodic signal seems to
500 * interefere badly with the fork() implementation on Linux running
501 * under VMWare.
502 */
503 BEGIN_NOSIGALRM
504 pid = fork();
505 if (pid == 0) {
506 int fd = open("/dev/null", O_WRONLY);
507 dup2(fd, 1);
508 dup2(fd, 2);
509 execl( tmp, _ANDROID_PING_PROGRAM, "ping", "emulator", VERSION_STRING, NULL );
510 }
511 END_NOSIGALRM
512
513 /* don't do anything in the parent or in case of error */
514 strncat( tmp, " ping emulator " VERSION_STRING, PATH_MAX - strlen(tmp) );
515 D( "ping command: %s", tmp );
516#endif
517 }
518 }
519}
520
521