iohandler.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * QEMU System Emulator - managing I/O handler
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config-host.h"
  25. #include "qemu-common.h"
  26. #include "qemu/queue.h"
  27. #include "block/aio.h"
  28. #include "qemu/main-loop.h"
  29. #ifndef _WIN32
  30. #include <sys/wait.h>
  31. #endif
  32. typedef struct IOHandlerRecord {
  33. IOCanReadHandler *fd_read_poll;
  34. IOHandler *fd_read;
  35. IOHandler *fd_write;
  36. void *opaque;
  37. QLIST_ENTRY(IOHandlerRecord) next;
  38. int fd;
  39. int pollfds_idx;
  40. bool deleted;
  41. } IOHandlerRecord;
  42. static QLIST_HEAD(, IOHandlerRecord) io_handlers =
  43. QLIST_HEAD_INITIALIZER(io_handlers);
  44. /* XXX: fd_read_poll should be suppressed, but an API change is
  45. necessary in the character devices to suppress fd_can_read(). */
  46. int qemu_set_fd_handler2(int fd,
  47. IOCanReadHandler *fd_read_poll,
  48. IOHandler *fd_read,
  49. IOHandler *fd_write,
  50. void *opaque)
  51. {
  52. IOHandlerRecord *ioh;
  53. assert(fd >= 0);
  54. if (!fd_read && !fd_write) {
  55. QLIST_FOREACH(ioh, &io_handlers, next) {
  56. if (ioh->fd == fd) {
  57. ioh->deleted = 1;
  58. break;
  59. }
  60. }
  61. } else {
  62. QLIST_FOREACH(ioh, &io_handlers, next) {
  63. if (ioh->fd == fd)
  64. goto found;
  65. }
  66. ioh = g_malloc0(sizeof(IOHandlerRecord));
  67. QLIST_INSERT_HEAD(&io_handlers, ioh, next);
  68. found:
  69. ioh->fd = fd;
  70. ioh->fd_read_poll = fd_read_poll;
  71. ioh->fd_read = fd_read;
  72. ioh->fd_write = fd_write;
  73. ioh->opaque = opaque;
  74. ioh->pollfds_idx = -1;
  75. ioh->deleted = 0;
  76. qemu_notify_event();
  77. }
  78. return 0;
  79. }
  80. int qemu_set_fd_handler(int fd,
  81. IOHandler *fd_read,
  82. IOHandler *fd_write,
  83. void *opaque)
  84. {
  85. return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
  86. }
  87. void qemu_iohandler_fill(GArray *pollfds)
  88. {
  89. IOHandlerRecord *ioh;
  90. QLIST_FOREACH(ioh, &io_handlers, next) {
  91. int events = 0;
  92. if (ioh->deleted)
  93. continue;
  94. if (ioh->fd_read &&
  95. (!ioh->fd_read_poll ||
  96. ioh->fd_read_poll(ioh->opaque) != 0)) {
  97. events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
  98. }
  99. if (ioh->fd_write) {
  100. events |= G_IO_OUT | G_IO_ERR;
  101. }
  102. if (events) {
  103. GPollFD pfd = {
  104. .fd = ioh->fd,
  105. .events = events,
  106. };
  107. ioh->pollfds_idx = pollfds->len;
  108. g_array_append_val(pollfds, pfd);
  109. } else {
  110. ioh->pollfds_idx = -1;
  111. }
  112. }
  113. }
  114. void qemu_iohandler_poll(GArray *pollfds, int ret)
  115. {
  116. if (ret > 0) {
  117. IOHandlerRecord *pioh, *ioh;
  118. QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
  119. int revents = 0;
  120. if (!ioh->deleted && ioh->pollfds_idx != -1) {
  121. GPollFD *pfd = &g_array_index(pollfds, GPollFD,
  122. ioh->pollfds_idx);
  123. revents = pfd->revents;
  124. }
  125. if (!ioh->deleted && ioh->fd_read &&
  126. (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
  127. ioh->fd_read(ioh->opaque);
  128. }
  129. if (!ioh->deleted && ioh->fd_write &&
  130. (revents & (G_IO_OUT | G_IO_ERR))) {
  131. ioh->fd_write(ioh->opaque);
  132. }
  133. /* Do this last in case read/write handlers marked it for deletion */
  134. if (ioh->deleted) {
  135. QLIST_REMOVE(ioh, next);
  136. g_free(ioh);
  137. }
  138. }
  139. }
  140. }
  141. /* reaping of zombies. right now we're not passing the status to
  142. anyone, but it would be possible to add a callback. */
  143. #ifndef _WIN32
  144. typedef struct ChildProcessRecord {
  145. int pid;
  146. QLIST_ENTRY(ChildProcessRecord) next;
  147. } ChildProcessRecord;
  148. static QLIST_HEAD(, ChildProcessRecord) child_watches =
  149. QLIST_HEAD_INITIALIZER(child_watches);
  150. static QEMUBH *sigchld_bh;
  151. static void sigchld_handler(int signal)
  152. {
  153. qemu_bh_schedule(sigchld_bh);
  154. }
  155. static void sigchld_bh_handler(void *opaque)
  156. {
  157. ChildProcessRecord *rec, *next;
  158. QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
  159. if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
  160. QLIST_REMOVE(rec, next);
  161. g_free(rec);
  162. }
  163. }
  164. }
  165. static void qemu_init_child_watch(void)
  166. {
  167. struct sigaction act;
  168. sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
  169. act.sa_handler = sigchld_handler;
  170. act.sa_flags = SA_NOCLDSTOP;
  171. sigaction(SIGCHLD, &act, NULL);
  172. }
  173. int qemu_add_child_watch(pid_t pid)
  174. {
  175. ChildProcessRecord *rec;
  176. if (!sigchld_bh) {
  177. qemu_init_child_watch();
  178. }
  179. QLIST_FOREACH(rec, &child_watches, next) {
  180. if (rec->pid == pid) {
  181. return 1;
  182. }
  183. }
  184. rec = g_malloc0(sizeof(ChildProcessRecord));
  185. rec->pid = pid;
  186. QLIST_INSERT_HEAD(&child_watches, rec, next);
  187. return 0;
  188. }
  189. #endif