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