Use a per-process server address for the GLES server

Previously we used a hardcoded address (tcp port, unix pipe path,
etc.) for the OpenGLRender system. Multiple emulators would all try to
listen on the same address, with the system non-deterministically (?)
choosing which one accepted each new connection. This resulted in
frames going to the wrong emulator window, one emulator shutting down
another's OpenGL system, etc.

Now the OpenGLRender server requests an unused tcp port or derives a
path from the pid, and reports the address back to the emulator client
to use for future connections from the guest.

Change-Id: I139d32615200b36b87f2d2ede4abb4060ec02776
diff --git a/android/opengles.c b/android/opengles.c
index 7405750..1fa6421 100644
--- a/android/opengles.c
+++ b/android/opengles.c
@@ -61,6 +61,7 @@
 
 static ADynamicLibrary*  rendererLib;
 static int               rendererStarted;
+static char              rendererAddress[256];
 
 /* Define the function pointers */
 #define DYNLINK_FUNC(name) \
@@ -152,7 +153,7 @@
         return 0;
     }
 
-    if (!initOpenGLRenderer(width, height, ANDROID_OPENGLES_BASE_PORT)) {
+    if (!initOpenGLRenderer(width, height, rendererAddress, sizeof(rendererAddress))) {
         D("Can't start OpenGLES renderer?");
         return -1;
     }
@@ -177,7 +178,6 @@
 
 static void extractBaseString(char* dst, const char* src, size_t dstSize)
 {
-    size_t len = strlen(src);
     const char* begin = strchr(src, '(');
     const char* end = strrchr(src, ')');
 
@@ -211,7 +211,7 @@
 
     if (!rendererStarted) {
         D("Can't get OpenGL ES hardware strings when renderer not started");
-        vendor[0] = renderer[0] = version = '\0';
+        vendor[0] = renderer[0] = version[0] = '\0';
         return;
     }
 
@@ -274,18 +274,9 @@
 }
 
 void
-android_gles_unix_path(char* buff, size_t buffsize, int port)
+android_gles_server_path(char* buff, size_t buffsize)
 {
-    const char* user = getenv("USER");
-    char *p = buff, *end = buff + buffsize;
-
-    /* The logic here must correspond to the one inside
-     * development/tools/emulator/opengl/shared/libOpenglCodecCommon/UnixStream.cpp */
-    p = bufprint(p, end, "/tmp/");
-    if (user && user[0]) {
-        p = bufprint(p, end, "android-%s/", user);
-    }
-    p = bufprint(p, end, "qemu-gles-%d", port);
+    strncpy_safe(buff, rendererAddress, buffsize);
 }
 
 #else // CONFIG_ANDROID_OPENGLES
@@ -330,7 +321,7 @@
 void android_redrawOpenglesWindow(void)
 {}
 
-void android_gles_unix_path(char* buff, size_t buffsize, int port)
+void android_gles_server_path(char* buff, size_t buffsize)
 {
     buff[0] = '\0';
 }