2
0

event_notifier-posix.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * event notifier support
  3. *
  4. * Copyright Red Hat, Inc. 2010
  5. *
  6. * Authors:
  7. * Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/cutils.h"
  14. #include "qemu/event_notifier.h"
  15. #include "qemu/main-loop.h"
  16. #ifdef CONFIG_EVENTFD
  17. #include <sys/eventfd.h>
  18. #endif
  19. #ifdef CONFIG_EVENTFD
  20. /*
  21. * Initialize @e with existing file descriptor @fd.
  22. * @fd must be a genuine eventfd object, emulation with pipe won't do.
  23. */
  24. void event_notifier_init_fd(EventNotifier *e, int fd)
  25. {
  26. e->rfd = fd;
  27. e->wfd = fd;
  28. e->initialized = true;
  29. }
  30. #endif
  31. int event_notifier_init(EventNotifier *e, int active)
  32. {
  33. int fds[2];
  34. int ret;
  35. #ifdef CONFIG_EVENTFD
  36. ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
  37. #else
  38. ret = -1;
  39. errno = ENOSYS;
  40. #endif
  41. if (ret >= 0) {
  42. e->rfd = e->wfd = ret;
  43. } else {
  44. if (errno != ENOSYS) {
  45. return -errno;
  46. }
  47. if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
  48. return -errno;
  49. }
  50. if (!g_unix_set_fd_nonblocking(fds[0], true, NULL)) {
  51. ret = -errno;
  52. goto fail;
  53. }
  54. if (!g_unix_set_fd_nonblocking(fds[1], true, NULL)) {
  55. ret = -errno;
  56. goto fail;
  57. }
  58. e->rfd = fds[0];
  59. e->wfd = fds[1];
  60. }
  61. e->initialized = true;
  62. if (active) {
  63. event_notifier_set(e);
  64. }
  65. return 0;
  66. fail:
  67. close(fds[0]);
  68. close(fds[1]);
  69. return ret;
  70. }
  71. void event_notifier_cleanup(EventNotifier *e)
  72. {
  73. if (!e->initialized) {
  74. return;
  75. }
  76. if (e->rfd != e->wfd) {
  77. close(e->rfd);
  78. }
  79. e->rfd = -1;
  80. close(e->wfd);
  81. e->wfd = -1;
  82. e->initialized = false;
  83. }
  84. int event_notifier_get_fd(const EventNotifier *e)
  85. {
  86. return e->rfd;
  87. }
  88. int event_notifier_get_wfd(const EventNotifier *e)
  89. {
  90. return e->wfd;
  91. }
  92. int event_notifier_set(EventNotifier *e)
  93. {
  94. static const uint64_t value = 1;
  95. ssize_t ret;
  96. if (!e->initialized) {
  97. return -1;
  98. }
  99. do {
  100. ret = write(e->wfd, &value, sizeof(value));
  101. } while (ret < 0 && errno == EINTR);
  102. /* EAGAIN is fine, a read must be pending. */
  103. if (ret < 0 && errno != EAGAIN) {
  104. return -errno;
  105. }
  106. return 0;
  107. }
  108. int event_notifier_test_and_clear(EventNotifier *e)
  109. {
  110. int value;
  111. ssize_t len;
  112. char buffer[512];
  113. if (!e->initialized) {
  114. return 0;
  115. }
  116. /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
  117. value = 0;
  118. do {
  119. len = read(e->rfd, buffer, sizeof(buffer));
  120. value |= (len > 0);
  121. } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
  122. return value;
  123. }