Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 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 | |
| 25 | #include "config-host.h" |
| 26 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 27 | #include "qemu/queue.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 28 | #include "block/aio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 29 | #include "qemu/main-loop.h" |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 30 | |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 31 | #ifndef _WIN32 |
| 32 | #include <sys/wait.h> |
| 33 | #endif |
| 34 | |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 35 | typedef struct IOHandlerRecord { |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 36 | IOCanReadHandler *fd_read_poll; |
| 37 | IOHandler *fd_read; |
| 38 | IOHandler *fd_write; |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 39 | void *opaque; |
| 40 | QLIST_ENTRY(IOHandlerRecord) next; |
Stefan Weil | c97feed | 2012-04-29 19:08:46 +0200 | [diff] [blame] | 41 | int fd; |
| 42 | bool deleted; |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 43 | } IOHandlerRecord; |
| 44 | |
| 45 | static QLIST_HEAD(, IOHandlerRecord) io_handlers = |
| 46 | QLIST_HEAD_INITIALIZER(io_handlers); |
| 47 | |
| 48 | |
| 49 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 50 | necessary in the character devices to suppress fd_can_read(). */ |
| 51 | int qemu_set_fd_handler2(int fd, |
| 52 | IOCanReadHandler *fd_read_poll, |
| 53 | IOHandler *fd_read, |
| 54 | IOHandler *fd_write, |
| 55 | void *opaque) |
| 56 | { |
| 57 | IOHandlerRecord *ioh; |
| 58 | |
David Gibson | bbdd2ad | 2012-09-10 12:30:56 +1000 | [diff] [blame] | 59 | assert(fd >= 0); |
| 60 | |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 61 | if (!fd_read && !fd_write) { |
| 62 | QLIST_FOREACH(ioh, &io_handlers, next) { |
| 63 | if (ioh->fd == fd) { |
| 64 | ioh->deleted = 1; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } else { |
| 69 | QLIST_FOREACH(ioh, &io_handlers, next) { |
| 70 | if (ioh->fd == fd) |
| 71 | goto found; |
| 72 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 73 | ioh = g_malloc0(sizeof(IOHandlerRecord)); |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 74 | QLIST_INSERT_HEAD(&io_handlers, ioh, next); |
| 75 | found: |
| 76 | ioh->fd = fd; |
| 77 | ioh->fd_read_poll = fd_read_poll; |
| 78 | ioh->fd_read = fd_read; |
| 79 | ioh->fd_write = fd_write; |
| 80 | ioh->opaque = opaque; |
| 81 | ioh->deleted = 0; |
Alexey Kardashevskiy | 55ce75f | 2012-07-18 22:52:04 +1000 | [diff] [blame] | 82 | qemu_notify_event(); |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int qemu_set_fd_handler(int fd, |
| 88 | IOHandler *fd_read, |
| 89 | IOHandler *fd_write, |
| 90 | void *opaque) |
| 91 | { |
Paolo Bonzini | be08e65 | 2011-09-12 14:59:42 +0200 | [diff] [blame] | 92 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds) |
| 96 | { |
| 97 | IOHandlerRecord *ioh; |
| 98 | |
| 99 | QLIST_FOREACH(ioh, &io_handlers, next) { |
| 100 | if (ioh->deleted) |
| 101 | continue; |
| 102 | if (ioh->fd_read && |
| 103 | (!ioh->fd_read_poll || |
| 104 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 105 | FD_SET(ioh->fd, readfds); |
| 106 | if (ioh->fd > *pnfds) |
| 107 | *pnfds = ioh->fd; |
| 108 | } |
| 109 | if (ioh->fd_write) { |
| 110 | FD_SET(ioh->fd, writefds); |
| 111 | if (ioh->fd > *pnfds) |
| 112 | *pnfds = ioh->fd; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret) |
| 118 | { |
| 119 | if (ret > 0) { |
| 120 | IOHandlerRecord *pioh, *ioh; |
| 121 | |
| 122 | QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { |
| 123 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) { |
| 124 | ioh->fd_read(ioh->opaque); |
| 125 | } |
| 126 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) { |
| 127 | ioh->fd_write(ioh->opaque); |
| 128 | } |
| 129 | |
| 130 | /* Do this last in case read/write handlers marked it for deletion */ |
| 131 | if (ioh->deleted) { |
| 132 | QLIST_REMOVE(ioh, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 133 | g_free(ioh); |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 138 | |
| 139 | /* reaping of zombies. right now we're not passing the status to |
| 140 | anyone, but it would be possible to add a callback. */ |
| 141 | #ifndef _WIN32 |
| 142 | typedef struct ChildProcessRecord { |
| 143 | int pid; |
| 144 | QLIST_ENTRY(ChildProcessRecord) next; |
| 145 | } ChildProcessRecord; |
| 146 | |
| 147 | static QLIST_HEAD(, ChildProcessRecord) child_watches = |
| 148 | QLIST_HEAD_INITIALIZER(child_watches); |
| 149 | |
| 150 | static QEMUBH *sigchld_bh; |
| 151 | |
| 152 | static void sigchld_handler(int signal) |
| 153 | { |
| 154 | qemu_bh_schedule(sigchld_bh); |
| 155 | } |
| 156 | |
| 157 | static void sigchld_bh_handler(void *opaque) |
| 158 | { |
| 159 | ChildProcessRecord *rec, *next; |
| 160 | |
| 161 | QLIST_FOREACH_SAFE(rec, &child_watches, next, next) { |
| 162 | if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) { |
| 163 | QLIST_REMOVE(rec, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 164 | g_free(rec); |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void qemu_init_child_watch(void) |
| 170 | { |
| 171 | struct sigaction act; |
| 172 | sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL); |
| 173 | |
| 174 | act.sa_handler = sigchld_handler; |
| 175 | act.sa_flags = SA_NOCLDSTOP; |
| 176 | sigaction(SIGCHLD, &act, NULL); |
| 177 | } |
| 178 | |
| 179 | int qemu_add_child_watch(pid_t pid) |
| 180 | { |
| 181 | ChildProcessRecord *rec; |
| 182 | |
| 183 | if (!sigchld_bh) { |
| 184 | qemu_init_child_watch(); |
| 185 | } |
| 186 | |
| 187 | QLIST_FOREACH(rec, &child_watches, next) { |
| 188 | if (rec->pid == pid) { |
| 189 | return 1; |
| 190 | } |
| 191 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 192 | rec = g_malloc0(sizeof(ChildProcessRecord)); |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 193 | rec->pid = pid; |
| 194 | QLIST_INSERT_HEAD(&child_watches, rec, next); |
| 195 | return 0; |
| 196 | } |
| 197 | #endif |