2
0

iohandler.c 5.4 KB

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