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