OSDN Git Service

Inline implementation of qemu_pipe_open
authorVladimir Chtchetkine <vchtchetkine@google.com>
Wed, 3 Aug 2011 16:07:39 +0000 (09:07 -0700)
committerVladimir Chtchetkine <vchtchetkine@google.com>
Wed, 3 Aug 2011 16:07:39 +0000 (09:07 -0700)
Change-Id: Icbe0528b1ac2b215d2cebc5c35cce9c3c830b722

include/hardware/qemu_pipe.h

index fb0bf2f..570af70 100644 (file)
 #define ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H
 
 #include <sys/cdefs.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <hardware/qemud.h>
+#include <hardware/qemu_pipe.h>
+#include <pthread.h>  /* for pthread_once() */
+#include <stdlib.h>
+#include <stdio.h>
 
-__BEGIN_DECLS
+#ifndef D
+#  define  D(...)   do{}while(0)
+#endif
 
 /* Try to open a new Qemu fast-pipe. This function returns a file descriptor
  * that can be used to communicate with a named service managed by the
@@ -42,8 +52,41 @@ __BEGIN_DECLS
  * except for a few special cases (e.g. GSM modem), where EBUSY will be
  * returned if more than one client tries to connect to it.
  */
-extern int  qemu_pipe_open(const char*  pipeName);
+static __inline__ int
+qemu_pipe_open(const char*  pipeName)
+{
+    char  buff[256];
+    int   buffLen;
+    int   fd, ret;
 
-__END_DECLS
+    if (pipeName == NULL || pipeName[0] == '\0') {
+        errno = EINVAL;
+        return -1;
+    }
+
+    snprintf(buff, sizeof buff, "pipe:%s", pipeName);
+
+    fd = open("/dev/qemu_pipe", O_RDWR);
+    if (fd < 0) {
+        D("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__, strerror(errno));
+        errno = ENOSYS;
+        return -1;
+    }
+
+    buffLen = strlen(buff);
+
+    ret = TEMP_FAILURE_RETRY(write(fd, buff, buffLen+1));
+    if (ret != buffLen+1) {
+        D("%s: Could not connect to %s pipe service: %s", __FUNCTION__, pipeName, strerror(errno));
+        if (ret == 0) {
+            errno = ECONNRESET;
+        } else if (ret > 0) {
+            errno = EINVAL;
+        }
+        return -1;
+    }
+
+    return fd;
+}
 
 #endif /* ANDROID_INCLUDE_HARDWARE_QEMUD_PIPE_H */