iohandler.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 = g_malloc0(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. typedef struct IOTrampoline
  76. {
  77. GIOChannel *chan;
  78. IOHandler *fd_read;
  79. IOHandler *fd_write;
  80. void *opaque;
  81. guint tag;
  82. } IOTrampoline;
  83. static gboolean fd_trampoline(GIOChannel *chan, GIOCondition cond, gpointer opaque)
  84. {
  85. IOTrampoline *tramp = opaque;
  86. if (tramp->opaque == NULL) {
  87. return FALSE;
  88. }
  89. if ((cond & G_IO_IN) && tramp->fd_read) {
  90. tramp->fd_read(tramp->opaque);
  91. }
  92. if ((cond & G_IO_OUT) && tramp->fd_write) {
  93. tramp->fd_write(tramp->opaque);
  94. }
  95. return TRUE;
  96. }
  97. int qemu_set_fd_handler(int fd,
  98. IOHandler *fd_read,
  99. IOHandler *fd_write,
  100. void *opaque)
  101. {
  102. static IOTrampoline fd_trampolines[FD_SETSIZE];
  103. IOTrampoline *tramp = &fd_trampolines[fd];
  104. if (tramp->tag != 0) {
  105. g_io_channel_unref(tramp->chan);
  106. g_source_remove(tramp->tag);
  107. }
  108. if (opaque) {
  109. GIOCondition cond = 0;
  110. tramp->fd_read = fd_read;
  111. tramp->fd_write = fd_write;
  112. tramp->opaque = opaque;
  113. if (fd_read) {
  114. cond |= G_IO_IN | G_IO_ERR;
  115. }
  116. if (fd_write) {
  117. cond |= G_IO_OUT | G_IO_ERR;
  118. }
  119. tramp->chan = g_io_channel_unix_new(fd);
  120. tramp->tag = g_io_add_watch(tramp->chan, cond, fd_trampoline, tramp);
  121. }
  122. return 0;
  123. }
  124. void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
  125. {
  126. IOHandlerRecord *ioh;
  127. QLIST_FOREACH(ioh, &io_handlers, next) {
  128. if (ioh->deleted)
  129. continue;
  130. if (ioh->fd_read &&
  131. (!ioh->fd_read_poll ||
  132. ioh->fd_read_poll(ioh->opaque) != 0)) {
  133. FD_SET(ioh->fd, readfds);
  134. if (ioh->fd > *pnfds)
  135. *pnfds = ioh->fd;
  136. }
  137. if (ioh->fd_write) {
  138. FD_SET(ioh->fd, writefds);
  139. if (ioh->fd > *pnfds)
  140. *pnfds = ioh->fd;
  141. }
  142. }
  143. }
  144. void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
  145. {
  146. if (ret > 0) {
  147. IOHandlerRecord *pioh, *ioh;
  148. QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
  149. if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
  150. ioh->fd_read(ioh->opaque);
  151. }
  152. if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
  153. ioh->fd_write(ioh->opaque);
  154. }
  155. /* Do this last in case read/write handlers marked it for deletion */
  156. if (ioh->deleted) {
  157. QLIST_REMOVE(ioh, next);
  158. g_free(ioh);
  159. }
  160. }
  161. }
  162. }
  163. /* reaping of zombies. right now we're not passing the status to
  164. anyone, but it would be possible to add a callback. */
  165. #ifndef _WIN32
  166. typedef struct ChildProcessRecord {
  167. int pid;
  168. QLIST_ENTRY(ChildProcessRecord) next;
  169. } ChildProcessRecord;
  170. static QLIST_HEAD(, ChildProcessRecord) child_watches =
  171. QLIST_HEAD_INITIALIZER(child_watches);
  172. static QEMUBH *sigchld_bh;
  173. static void sigchld_handler(int signal)
  174. {
  175. qemu_bh_schedule(sigchld_bh);
  176. }
  177. static void sigchld_bh_handler(void *opaque)
  178. {
  179. ChildProcessRecord *rec, *next;
  180. QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
  181. if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
  182. QLIST_REMOVE(rec, next);
  183. g_free(rec);
  184. }
  185. }
  186. }
  187. static void qemu_init_child_watch(void)
  188. {
  189. struct sigaction act;
  190. sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
  191. act.sa_handler = sigchld_handler;
  192. act.sa_flags = SA_NOCLDSTOP;
  193. sigaction(SIGCHLD, &act, NULL);
  194. }
  195. int qemu_add_child_watch(pid_t pid)
  196. {
  197. ChildProcessRecord *rec;
  198. if (!sigchld_bh) {
  199. qemu_init_child_watch();
  200. }
  201. QLIST_FOREACH(rec, &child_watches, next) {
  202. if (rec->pid == pid) {
  203. return 1;
  204. }
  205. }
  206. rec = g_malloc0(sizeof(ChildProcessRecord));
  207. rec->pid = pid;
  208. QLIST_INSERT_HEAD(&child_watches, rec, next);
  209. return 0;
  210. }
  211. #endif