Merge "Fix misc-macro-parentheses warnings in hardware/ril."
diff --git a/reference-ril/reference-ril.c b/reference-ril/reference-ril.c
index b9baef1..7b1dc16 100644
--- a/reference-ril/reference-ril.c
+++ b/reference-ril/reference-ril.c
@@ -17,6 +17,7 @@
 
 #include <telephony/ril_cdma_sms.h>
 #include <telephony/librilutils.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <assert.h>
 #include <string.h>
@@ -36,9 +37,9 @@
 #include <cutils/sockets.h>
 #include <termios.h>
 #include <sys/system_properties.h>
+#include <system/qemu_pipe.h>
 
 #include "ril.h"
-#include "hardware/qemu_pipe.h"
 
 #define LOG_TAG "RIL"
 #include <utils/Log.h>
@@ -239,6 +240,16 @@
 static int s_expectAnswer = 0;
 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
 
+// Returns true iff running this process in an emulator VM
+static bool isInEmulator(void) {
+    static int inQemu = -1;
+    if (inQemu < 0) {
+        char propValue[PROP_VALUE_MAX];
+        inQemu = (__system_property_get("ro.kernel.qemu", propValue) != 0);
+    }
+    return inQemu == 1;
+}
+
 static int s_cell_info_rate_ms = INT_MAX;
 static int s_mcc = 0;
 static int s_mnc = 0;
@@ -550,52 +561,50 @@
         responses[i].addresses = alloca(strlen(out) + 1);
         strcpy(responses[i].addresses, out);
 
-        {
-            char  propValue[PROP_VALUE_MAX];
+        if (isInEmulator()) {
+            /* We are in the emulator - the dns servers are listed
+                * by the following system properties, setup in
+                * /system/etc/init.goldfish.sh:
+                *  - net.eth0.dns1
+                *  - net.eth0.dns2
+                *  - net.eth0.dns3
+                *  - net.eth0.dns4
+                */
+            const int   dnslist_sz = 128;
+            char*       dnslist = alloca(dnslist_sz);
+            const char* separator = "";
+            int         nn;
 
-            if (__system_property_get("ro.kernel.qemu", propValue) != 0) {
-                /* We are in the emulator - the dns servers are listed
-                 * by the following system properties, setup in
-                 * /system/etc/init.goldfish.sh:
-                 *  - net.eth0.dns1
-                 *  - net.eth0.dns2
-                 *  - net.eth0.dns3
-                 *  - net.eth0.dns4
-                 */
-                const int   dnslist_sz = 128;
-                char*       dnslist = alloca(dnslist_sz);
-                const char* separator = "";
-                int         nn;
+            dnslist[0] = 0;
+            for (nn = 1; nn <= 4; nn++) {
+                /* Probe net.eth0.dns<n> */
+                char  propName[PROP_NAME_MAX];
+                char  propValue[PROP_VALUE_MAX];
 
-                dnslist[0] = 0;
-                for (nn = 1; nn <= 4; nn++) {
-                    /* Probe net.eth0.dns<n> */
-                    char  propName[PROP_NAME_MAX];
-                    snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
+                snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
 
-                    /* Ignore if undefined */
-                    if (__system_property_get(propName, propValue) == 0) {
-                        continue;
-                    }
-
-                    /* Append the DNS IP address */
-                    strlcat(dnslist, separator, dnslist_sz);
-                    strlcat(dnslist, propValue, dnslist_sz);
-                    separator = " ";
+                /* Ignore if undefined */
+                if (__system_property_get(propName, propValue) == 0) {
+                    continue;
                 }
-                responses[i].dnses = dnslist;
 
-                /* There is only on gateway in the emulator */
-                responses[i].gateways = "10.0.2.2";
-                responses[i].mtu = DEFAULT_MTU;
+                /* Append the DNS IP address */
+                strlcat(dnslist, separator, dnslist_sz);
+                strlcat(dnslist, propValue, dnslist_sz);
+                separator = " ";
             }
-            else {
-                /* I don't know where we are, so use the public Google DNS
-                 * servers by default and no gateway.
-                 */
-                responses[i].dnses = "8.8.8.8 8.8.4.4";
-                responses[i].gateways = "";
-            }
+            responses[i].dnses = dnslist;
+
+            /* There is only on gateway in the emulator */
+            responses[i].gateways = "10.0.2.2";
+            responses[i].mtu = DEFAULT_MTU;
+        }
+        else {
+            /* I don't know where we are, so use the public Google DNS
+                * servers by default and no gateway.
+                */
+            responses[i].dnses = "8.8.8.8 8.8.4.4";
+            responses[i].gateways = "";
         }
     }
 
@@ -3296,37 +3305,14 @@
     for (;;) {
         fd = -1;
         while  (fd < 0) {
-            if (s_port > 0) {
+            if (isInEmulator()) {
+                fd = qemu_pipe_open("pipe:qemud:gsm");
+            } else if (s_port > 0) {
                 fd = socket_loopback_client(s_port, SOCK_STREAM);
             } else if (s_device_socket) {
-                if (!strcmp(s_device_path, "/dev/socket/qemud")) {
-                    /* Before trying to connect to /dev/socket/qemud (which is
-                     * now another "legacy" way of communicating with the
-                     * emulator), we will try to connecto to gsm service via
-                     * qemu pipe. */
-                    fd = qemu_pipe_open("qemud:gsm");
-                    if (fd < 0) {
-                        /* Qemu-specific control socket */
-                        fd = socket_local_client( "qemud",
-                                                  ANDROID_SOCKET_NAMESPACE_RESERVED,
-                                                  SOCK_STREAM );
-                        if (fd >= 0 ) {
-                            char  answer[2];
-
-                            if ( write(fd, "gsm", 3) != 3 ||
-                                 read(fd, answer, 2) != 2 ||
-                                 memcmp(answer, "OK", 2) != 0)
-                            {
-                                close(fd);
-                                fd = -1;
-                            }
-                       }
-                    }
-                }
-                else
-                    fd = socket_local_client( s_device_path,
-                                            ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
-                                            SOCK_STREAM );
+                fd = socket_local_client(s_device_path,
+                                         ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
+                                         SOCK_STREAM);
             } else if (s_device_path != NULL) {
                 fd = open (s_device_path, O_RDWR);
                 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
@@ -3409,7 +3395,7 @@
         }
     }
 
-    if (s_port < 0 && s_device_path == NULL) {
+    if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
         usage(argv[0]);
         return NULL;
     }
@@ -3458,7 +3444,7 @@
         }
     }
 
-    if (s_port < 0 && s_device_path == NULL) {
+    if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
         usage(argv[0]);
     }
 
diff --git a/rild/rild.c b/rild/rild.c
index c63da38..3717deb 100644
--- a/rild/rild.c
+++ b/rild/rild.c
@@ -36,7 +36,6 @@
 #include <libril/ril_ex.h>
 
 #include <private/android_filesystem_config.h>
-#include "hardware/qemu_pipe.h"
 
 #define LIB_PATH_PROPERTY   "rild.libpath"
 #define LIB_ARGS_PROPERTY   "rild.libargs"
@@ -142,7 +141,7 @@
     void *dlHandle;
     const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
     const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
-    char *err_str = NULL;
+    const char *err_str = NULL;
 
     const RIL_RadioFunctions *funcs;
     char libPath[PROPERTY_VALUE_MAX];
@@ -190,113 +189,6 @@
         }
     }
 
-    /* special override when in the emulator */
-#if 1
-    {
-        static char*  arg_overrides[5];
-        static char   arg_device[32];
-        int           done = 0;
-
-#define  REFERENCE_RIL_PATH  "libreference-ril.so"
-
-        /* first, read /proc/cmdline into memory */
-        char          buffer[1024], *p, *q;
-        int           len;
-        int           fd = open("/proc/cmdline",O_RDONLY);
-
-        if (fd < 0) {
-            RLOGD("could not open /proc/cmdline:%s", strerror(errno));
-            goto OpenLib;
-        }
-
-        do {
-            len = read(fd,buffer,sizeof(buffer)); }
-        while (len == -1 && errno == EINTR);
-
-        if (len < 0) {
-            RLOGD("could not read /proc/cmdline:%s", strerror(errno));
-            close(fd);
-            goto OpenLib;
-        }
-        close(fd);
-
-        if (strstr(buffer, "android.qemud=") != NULL)
-        {
-            /* the qemud daemon is launched after rild, so
-            * give it some time to create its GSM socket
-            */
-            int  tries = 5;
-#define  QEMUD_SOCKET_NAME    "qemud"
-
-            while (1) {
-                int  fd;
-
-                sleep(1);
-
-                fd = qemu_pipe_open("qemud:gsm");
-                if (fd < 0) {
-                    fd = socket_local_client(
-                                QEMUD_SOCKET_NAME,
-                                ANDROID_SOCKET_NAMESPACE_RESERVED,
-                                SOCK_STREAM );
-                }
-                if (fd >= 0) {
-                    close(fd);
-                    snprintf( arg_device, sizeof(arg_device), "%s/%s",
-                                ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
-
-                    arg_overrides[1] = "-s";
-                    arg_overrides[2] = arg_device;
-                    done = 1;
-                    break;
-                }
-                RLOGD("could not connect to %s socket: %s",
-                    QEMUD_SOCKET_NAME, strerror(errno));
-                if (--tries == 0)
-                    break;
-            }
-            if (!done) {
-                RLOGE("could not connect to %s socket (giving up): %s",
-                    QEMUD_SOCKET_NAME, strerror(errno));
-                while(1)
-                    sleep(0x00ffffff);
-            }
-        }
-
-        /* otherwise, try to see if we passed a device name from the kernel */
-        if (!done) do {
-#define  KERNEL_OPTION  "android.ril="
-#define  DEV_PREFIX     "/dev/"
-
-            p = strstr( buffer, KERNEL_OPTION );
-            if (p == NULL)
-                break;
-
-            p += sizeof(KERNEL_OPTION)-1;
-            q  = strpbrk( p, " \t\n\r" );
-            if (q != NULL)
-                *q = 0;
-
-            snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
-            arg_device[sizeof(arg_device)-1] = 0;
-            arg_overrides[1] = "-d";
-            arg_overrides[2] = arg_device;
-            done = 1;
-
-        } while (0);
-
-        if (done) {
-            argv = arg_overrides;
-            argc = 3;
-            i    = 1;
-            hasLibArgs = 1;
-            rilLibPath = REFERENCE_RIL_PATH;
-
-            RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
-        }
-    }
-OpenLib:
-#endif
     switchUser();
 
     dlHandle = dlopen(rilLibPath, RTLD_NOW);