qemu_pipe.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H
  17. #define ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H
  18. #include <sys/cdefs.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/mman.h>
  22. #include <pthread.h> /* for pthread_once() */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #ifndef D
  27. # define D(...) do{}while(0)
  28. #endif
  29. /*
  30. * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
  31. * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
  32. * not already defined, then define it here.
  33. */
  34. #ifndef TEMP_FAILURE_RETRY
  35. /* Used to retry syscalls that can return EINTR. */
  36. #define TEMP_FAILURE_RETRY(exp) ({ \
  37. typeof (exp) _rc; \
  38. do { \
  39. _rc = (exp); \
  40. } while (_rc == -1 && errno == EINTR); \
  41. _rc; })
  42. #endif
  43. /* Try to open a new Qemu fast-pipe. This function returns a file descriptor
  44. * that can be used to communicate with a named service managed by the
  45. * emulator.
  46. *
  47. * This file descriptor can be used as a standard pipe/socket descriptor.
  48. *
  49. * 'pipeName' is the name of the emulator service you want to connect to.
  50. * E.g. 'opengles' or 'camera'.
  51. *
  52. * On success, return a valid file descriptor
  53. * Returns -1 on error, and errno gives the error code, e.g.:
  54. *
  55. * EINVAL -> unknown/unsupported pipeName
  56. * ENOSYS -> fast pipes not available in this system.
  57. *
  58. * ENOSYS should never happen, except if you're trying to run within a
  59. * misconfigured emulator.
  60. *
  61. * You should be able to open several pipes to the same pipe service,
  62. * except for a few special cases (e.g. GSM modem), where EBUSY will be
  63. * returned if more than one client tries to connect to it.
  64. */
  65. static __inline__ int
  66. qemu_pipe_open(const char* pipeName)
  67. {
  68. char buff[256];
  69. int buffLen;
  70. int fd, ret;
  71. if (pipeName == NULL || pipeName[0] == '\0') {
  72. errno = EINVAL;
  73. return -1;
  74. }
  75. snprintf(buff, sizeof buff, "pipe:%s", pipeName);
  76. fd = open("/dev/qemu_pipe", O_RDWR);
  77. if (fd < 0) {
  78. D("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__, strerror(errno));
  79. //errno = ENOSYS;
  80. return -1;
  81. }
  82. buffLen = strlen(buff);
  83. ret = TEMP_FAILURE_RETRY(write(fd, buff, buffLen+1));
  84. if (ret != buffLen+1) {
  85. D("%s: Could not connect to %s pipe service: %s", __FUNCTION__, pipeName, strerror(errno));
  86. if (ret == 0) {
  87. errno = ECONNRESET;
  88. } else if (ret > 0) {
  89. errno = EINVAL;
  90. }
  91. return -1;
  92. }
  93. return fd;
  94. }
  95. #endif /* ANDROID_INCLUDE_HARDWARE_QEMUD_PIPE_H */