Make adb's daemon-port on the host machine configurable.

This is the first CL of a somewhat larger effort which, among other things,
will involve changing the emulator and ddms to talk to adb running on a
configurable port.

The port can be configured using environment variable ANDROID_ADB_SERVER_PORT.

Further CLs will also address the set of ports used for the local transport.

Change-Id: Ib2f431801f0adcd9f2dd290a28005644a36a780a
diff --git a/adb.c b/adb.c
index 446c70e..4655d7c 100644
--- a/adb.c
+++ b/adb.c
@@ -687,7 +687,7 @@
 #endif
 
 #if ADB_HOST
-int launch_server()
+int launch_server(int server_port)
 {
 #ifdef HAVE_WIN32_PROC
     /* we need to start the server in the background                    */
@@ -822,7 +822,17 @@
 }
 #endif
 
-int adb_main(int is_daemon)
+/* Constructs a local name of form tcp:port.
+ * target_str points to the target string, it's content will be overwritten.
+ * target_size is the capacity of the target string.
+ * server_port is the port number to use for the local name.
+ */
+void build_local_name(char* target_str, size_t target_size, int server_port)
+{
+  snprintf(target_str, target_size, "tcp:%d", server_port);
+}
+
+int adb_main(int is_daemon, int server_port)
 {
 #if !ADB_HOST
     int secure = 0;
@@ -845,9 +855,11 @@
     HOST = 1;
     usb_vendors_init();
     usb_init();
-    local_init(ADB_LOCAL_TRANSPORT_PORT);
+    local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
 
-    if(install_listener("tcp:5037", "*smartsocket*", NULL)) {
+    char local_name[30];
+    build_local_name(local_name, sizeof(local_name), server_port);
+    if(install_listener(local_name, "*smartsocket*", NULL)) {
         exit(1);
     }
 #else
@@ -873,7 +885,7 @@
         }
     }
 
-    /* don't listen on port 5037 if we are running in secure mode */
+    /* don't listen on a port (default 5037) if running in secure mode */
     /* don't run as root if we are running in secure mode */
     if (secure) {
         struct __user_cap_header_struct header;
@@ -905,9 +917,11 @@
         cap.inheritable = 0;
         capset(&header, &cap);
 
-        D("Local port 5037 disabled\n");
+        D("Local port disabled\n");
     } else {
-        if(install_listener("tcp:5037", "*smartsocket*", NULL)) {
+        char local_name[30];
+        build_local_name(local_name, sizeof(local_name), server_port);
+        if(install_listener(local_name, "*smartsocket*", NULL)) {
             exit(1);
         }
     }
@@ -928,7 +942,7 @@
         usb_init();
     } else {
         // listen on default port
-        local_init(ADB_LOCAL_TRANSPORT_PORT);
+        local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
     }
     init_jdwp();
 #endif
@@ -1169,6 +1183,6 @@
     }
 
     start_device_log();
-    return adb_main(0);
+    return adb_main(0, DEFAULT_ADB_PORT);
 #endif
 }