iohandler.c 5.5 KB

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