2
0

iohandler.c 5.5 KB

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