2
0

iohandler.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-char.h"
  27. #include "qemu-queue.h"
  28. #include "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. bool deleted;
  40. } IOHandlerRecord;
  41. static QLIST_HEAD(, IOHandlerRecord) io_handlers =
  42. QLIST_HEAD_INITIALIZER(io_handlers);
  43. /* XXX: fd_read_poll should be suppressed, but an API change is
  44. necessary in the character devices to suppress fd_can_read(). */
  45. int qemu_set_fd_handler2(int fd,
  46. IOCanReadHandler *fd_read_poll,
  47. IOHandler *fd_read,
  48. IOHandler *fd_write,
  49. void *opaque)
  50. {
  51. IOHandlerRecord *ioh;
  52. assert(fd >= 0);
  53. if (!fd_read && !fd_write) {
  54. QLIST_FOREACH(ioh, &io_handlers, next) {
  55. if (ioh->fd == fd) {
  56. ioh->deleted = 1;
  57. break;
  58. }
  59. }
  60. } else {
  61. QLIST_FOREACH(ioh, &io_handlers, next) {
  62. if (ioh->fd == fd)
  63. goto found;
  64. }
  65. ioh = g_malloc0(sizeof(IOHandlerRecord));
  66. QLIST_INSERT_HEAD(&io_handlers, ioh, next);
  67. found:
  68. ioh->fd = fd;
  69. ioh->fd_read_poll = fd_read_poll;
  70. ioh->fd_read = fd_read;
  71. ioh->fd_write = fd_write;
  72. ioh->opaque = opaque;
  73. ioh->deleted = 0;
  74. qemu_notify_event();
  75. }
  76. return 0;
  77. }
  78. int qemu_set_fd_handler(int fd,
  79. IOHandler *fd_read,
  80. IOHandler *fd_write,
  81. void *opaque)
  82. {
  83. return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
  84. }
  85. void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
  86. {
  87. IOHandlerRecord *ioh;
  88. QLIST_FOREACH(ioh, &io_handlers, next) {
  89. if (ioh->deleted)
  90. continue;
  91. if (ioh->fd_read &&
  92. (!ioh->fd_read_poll ||
  93. ioh->fd_read_poll(ioh->opaque) != 0)) {
  94. FD_SET(ioh->fd, readfds);
  95. if (ioh->fd > *pnfds)
  96. *pnfds = ioh->fd;
  97. }
  98. if (ioh->fd_write) {
  99. FD_SET(ioh->fd, writefds);
  100. if (ioh->fd > *pnfds)
  101. *pnfds = ioh->fd;
  102. }
  103. }
  104. }
  105. void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
  106. {
  107. if (ret > 0) {
  108. IOHandlerRecord *pioh, *ioh;
  109. QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
  110. if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
  111. ioh->fd_read(ioh->opaque);
  112. }
  113. if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
  114. ioh->fd_write(ioh->opaque);
  115. }
  116. /* Do this last in case read/write handlers marked it for deletion */
  117. if (ioh->deleted) {
  118. QLIST_REMOVE(ioh, next);
  119. g_free(ioh);
  120. }
  121. }
  122. }
  123. }
  124. /* reaping of zombies. right now we're not passing the status to
  125. anyone, but it would be possible to add a callback. */
  126. #ifndef _WIN32
  127. typedef struct ChildProcessRecord {
  128. int pid;
  129. QLIST_ENTRY(ChildProcessRecord) next;
  130. } ChildProcessRecord;
  131. static QLIST_HEAD(, ChildProcessRecord) child_watches =
  132. QLIST_HEAD_INITIALIZER(child_watches);
  133. static QEMUBH *sigchld_bh;
  134. static void sigchld_handler(int signal)
  135. {
  136. qemu_bh_schedule(sigchld_bh);
  137. }
  138. static void sigchld_bh_handler(void *opaque)
  139. {
  140. ChildProcessRecord *rec, *next;
  141. QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
  142. if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
  143. QLIST_REMOVE(rec, next);
  144. g_free(rec);
  145. }
  146. }
  147. }
  148. static void qemu_init_child_watch(void)
  149. {
  150. struct sigaction act;
  151. sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
  152. act.sa_handler = sigchld_handler;
  153. act.sa_flags = SA_NOCLDSTOP;
  154. sigaction(SIGCHLD, &act, NULL);
  155. }
  156. int qemu_add_child_watch(pid_t pid)
  157. {
  158. ChildProcessRecord *rec;
  159. if (!sigchld_bh) {
  160. qemu_init_child_watch();
  161. }
  162. QLIST_FOREACH(rec, &child_watches, next) {
  163. if (rec->pid == pid) {
  164. return 1;
  165. }
  166. }
  167. rec = g_malloc0(sizeof(ChildProcessRecord));
  168. rec->pid = pid;
  169. QLIST_INSERT_HEAD(&child_watches, rec, next);
  170. return 0;
  171. }
  172. #endif