iohandler.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/queue.h"
  27. #include "block/aio.h"
  28. #include "qemu/main-loop.h"
  29. #ifndef _WIN32
  30. #include <sys/wait.h>
  31. #endif
  32. /* This context runs on top of main loop. We can't reuse qemu_aio_context
  33. * because iohandlers mustn't be polled by aio_poll(qemu_aio_context). */
  34. static AioContext *iohandler_ctx;
  35. static void iohandler_init(void)
  36. {
  37. if (!iohandler_ctx) {
  38. iohandler_ctx = aio_context_new(&error_abort);
  39. }
  40. }
  41. GSource *iohandler_get_g_source(void)
  42. {
  43. iohandler_init();
  44. return aio_get_g_source(iohandler_ctx);
  45. }
  46. void qemu_set_fd_handler(int fd,
  47. IOHandler *fd_read,
  48. IOHandler *fd_write,
  49. void *opaque)
  50. {
  51. iohandler_init();
  52. aio_set_fd_handler(iohandler_ctx, fd, fd_read, fd_write, opaque);
  53. }
  54. /* reaping of zombies. right now we're not passing the status to
  55. anyone, but it would be possible to add a callback. */
  56. #ifndef _WIN32
  57. typedef struct ChildProcessRecord {
  58. int pid;
  59. QLIST_ENTRY(ChildProcessRecord) next;
  60. } ChildProcessRecord;
  61. static QLIST_HEAD(, ChildProcessRecord) child_watches =
  62. QLIST_HEAD_INITIALIZER(child_watches);
  63. static QEMUBH *sigchld_bh;
  64. static void sigchld_handler(int signal)
  65. {
  66. qemu_bh_schedule(sigchld_bh);
  67. }
  68. static void sigchld_bh_handler(void *opaque)
  69. {
  70. ChildProcessRecord *rec, *next;
  71. QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
  72. if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
  73. QLIST_REMOVE(rec, next);
  74. g_free(rec);
  75. }
  76. }
  77. }
  78. static void qemu_init_child_watch(void)
  79. {
  80. struct sigaction act;
  81. sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
  82. memset(&act, 0, sizeof(act));
  83. act.sa_handler = sigchld_handler;
  84. act.sa_flags = SA_NOCLDSTOP;
  85. sigaction(SIGCHLD, &act, NULL);
  86. }
  87. int qemu_add_child_watch(pid_t pid)
  88. {
  89. ChildProcessRecord *rec;
  90. if (!sigchld_bh) {
  91. qemu_init_child_watch();
  92. }
  93. QLIST_FOREACH(rec, &child_watches, next) {
  94. if (rec->pid == pid) {
  95. return 1;
  96. }
  97. }
  98. rec = g_malloc0(sizeof(ChildProcessRecord));
  99. rec->pid = pid;
  100. QLIST_INSERT_HEAD(&child_watches, rec, next);
  101. return 0;
  102. }
  103. #endif