aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 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 "qemu-common.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 25 | #include "monitor/monitor.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 26 | #include "ui/console.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 27 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/timer.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 29 | #include "sysemu/char.h" |
aurel32 | cf3ebac | 2008-11-01 00:53:09 +0000 | [diff] [blame] | 30 | #include "hw/usb.h" |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 31 | #include "qmp-commands.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 32 | |
| 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 35 | #include <time.h> |
| 36 | #include <errno.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <zlib.h> |
| 39 | |
| 40 | #ifndef _WIN32 |
| 41 | #include <sys/times.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <termios.h> |
| 44 | #include <sys/mman.h> |
| 45 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 46 | #include <sys/resource.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 47 | #include <sys/socket.h> |
| 48 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 49 | #include <net/if.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 50 | #include <arpa/inet.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 51 | #include <dirent.h> |
| 52 | #include <netdb.h> |
| 53 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 54 | #ifdef CONFIG_BSD |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Michael Tokarev | 3294ce1 | 2012-06-02 23:43:33 +0400 | [diff] [blame] | 56 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 57 | #include <dev/ppbus/ppi.h> |
| 58 | #include <dev/ppbus/ppbconf.h> |
| 59 | #elif defined(__DragonFly__) |
| 60 | #include <dev/misc/ppi/ppi.h> |
| 61 | #include <bus/ppbus/ppbconf.h> |
| 62 | #endif |
Aurelien Jarno | bbe813a | 2009-11-30 15:42:59 +0100 | [diff] [blame] | 63 | #else |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 64 | #ifdef __linux__ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 65 | #include <linux/ppdev.h> |
| 66 | #include <linux/parport.h> |
| 67 | #endif |
| 68 | #ifdef __sun__ |
| 69 | #include <sys/stat.h> |
| 70 | #include <sys/ethernet.h> |
| 71 | #include <sys/sockio.h> |
| 72 | #include <netinet/arp.h> |
| 73 | #include <netinet/in.h> |
| 74 | #include <netinet/in_systm.h> |
| 75 | #include <netinet/ip.h> |
| 76 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 77 | #include <netinet/udp.h> |
| 78 | #include <netinet/tcp.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 79 | #endif |
| 80 | #endif |
| 81 | #endif |
| 82 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 83 | #include "qemu/sockets.h" |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 84 | #include "ui/qemu-spice.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 85 | |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 86 | #define READ_BUF_LEN 4096 |
| 87 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 88 | /***********************************************************/ |
| 89 | /* character device */ |
| 90 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 91 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
| 92 | QTAILQ_HEAD_INITIALIZER(chardevs); |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 93 | |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 94 | void qemu_chr_be_event(CharDriverState *s, int event) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 95 | { |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 96 | /* Keep track if the char device is open */ |
| 97 | switch (event) { |
| 98 | case CHR_EVENT_OPENED: |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 99 | s->be_open = 1; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 100 | break; |
| 101 | case CHR_EVENT_CLOSED: |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 102 | s->be_open = 0; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 106 | if (!s->chr_event) |
| 107 | return; |
| 108 | s->chr_event(s->handler_opaque, event); |
| 109 | } |
| 110 | |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 111 | void qemu_chr_be_generic_open(CharDriverState *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 112 | { |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 113 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 116 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 117 | { |
| 118 | return s->chr_write(s, buf, len); |
| 119 | } |
| 120 | |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 121 | int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) |
| 122 | { |
| 123 | int offset = 0; |
| 124 | int res; |
| 125 | |
| 126 | while (offset < len) { |
| 127 | do { |
| 128 | res = s->chr_write(s, buf + offset, len - offset); |
| 129 | if (res == -1 && errno == EAGAIN) { |
| 130 | g_usleep(100); |
| 131 | } |
| 132 | } while (res == -1 && errno == EAGAIN); |
| 133 | |
| 134 | if (res == 0) { |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (res < 0) { |
| 139 | return res; |
| 140 | } |
| 141 | |
| 142 | offset += res; |
| 143 | } |
| 144 | |
| 145 | return offset; |
| 146 | } |
| 147 | |
Anthony Liguori | 41084f1 | 2011-08-15 11:17:34 -0500 | [diff] [blame] | 148 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 149 | { |
| 150 | if (!s->chr_ioctl) |
| 151 | return -ENOTSUP; |
| 152 | return s->chr_ioctl(s, cmd, arg); |
| 153 | } |
| 154 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 155 | int qemu_chr_be_can_write(CharDriverState *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 156 | { |
| 157 | if (!s->chr_can_read) |
| 158 | return 0; |
| 159 | return s->chr_can_read(s->handler_opaque); |
| 160 | } |
| 161 | |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 162 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 163 | { |
Stefan Weil | ac31073 | 2012-04-19 22:27:14 +0200 | [diff] [blame] | 164 | if (s->chr_read) { |
| 165 | s->chr_read(s->handler_opaque, buf, len); |
| 166 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Anthony Liguori | 74c0d6f | 2011-08-15 11:17:39 -0500 | [diff] [blame] | 169 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 170 | { |
| 171 | return s->get_msgfd ? s->get_msgfd(s) : -1; |
| 172 | } |
| 173 | |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 174 | int qemu_chr_add_client(CharDriverState *s, int fd) |
| 175 | { |
| 176 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; |
| 177 | } |
| 178 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 179 | void qemu_chr_accept_input(CharDriverState *s) |
| 180 | { |
| 181 | if (s->chr_accept_input) |
| 182 | s->chr_accept_input(s); |
Jan Kiszka | 98c8ee1 | 2012-03-16 13:18:00 +0100 | [diff] [blame] | 183 | qemu_notify_event(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Anthony Liguori | e7e71b0 | 2011-08-15 11:17:29 -0500 | [diff] [blame] | 186 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 187 | { |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 188 | char buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 189 | va_list ap; |
| 190 | va_start(ap, fmt); |
| 191 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 192 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 193 | va_end(ap); |
| 194 | } |
| 195 | |
Amit Shah | 386a5a1 | 2013-08-28 15:24:05 +0530 | [diff] [blame] | 196 | static void remove_fd_in_watch(CharDriverState *chr); |
| 197 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 198 | void qemu_chr_add_handlers(CharDriverState *s, |
Juan Quintela | 7b27a76 | 2010-03-11 17:55:39 +0100 | [diff] [blame] | 199 | IOCanReadHandler *fd_can_read, |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 200 | IOReadHandler *fd_read, |
| 201 | IOEventHandler *fd_event, |
| 202 | void *opaque) |
| 203 | { |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 204 | int fe_open; |
| 205 | |
Amit Shah | da7d998 | 2011-04-25 15:18:22 +0530 | [diff] [blame] | 206 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 207 | fe_open = 0; |
Amit Shah | 386a5a1 | 2013-08-28 15:24:05 +0530 | [diff] [blame] | 208 | remove_fd_in_watch(s); |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 209 | } else { |
| 210 | fe_open = 1; |
Amit Shah | 2d6c1ef | 2011-02-10 12:55:20 +0530 | [diff] [blame] | 211 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 212 | s->chr_can_read = fd_can_read; |
| 213 | s->chr_read = fd_read; |
| 214 | s->chr_event = fd_event; |
| 215 | s->handler_opaque = opaque; |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 216 | if (fe_open && s->chr_update_read_handler) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 217 | s->chr_update_read_handler(s); |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 218 | |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 219 | if (!s->explicit_fe_open) { |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 220 | qemu_chr_fe_set_open(s, fe_open); |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 223 | /* We're connecting to an already opened device, so let's make sure we |
| 224 | also get the open event */ |
Hans de Goede | a59bcd3 | 2013-03-26 11:08:00 +0100 | [diff] [blame] | 225 | if (fe_open && s->be_open) { |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 226 | qemu_chr_be_generic_open(s); |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 227 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 231 | { |
| 232 | return len; |
| 233 | } |
| 234 | |
Gerd Hoffmann | 80dca9e | 2013-02-21 11:41:26 +0100 | [diff] [blame] | 235 | static CharDriverState *qemu_chr_open_null(void) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 236 | { |
| 237 | CharDriverState *chr; |
| 238 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 239 | chr = g_malloc0(sizeof(CharDriverState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 240 | chr->chr_write = null_chr_write; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 241 | chr->explicit_be_open = true; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 242 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* MUX driver for serial I/O splitting */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 246 | #define MAX_MUX 4 |
| 247 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
| 248 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
| 249 | typedef struct { |
Juan Quintela | 7b27a76 | 2010-03-11 17:55:39 +0100 | [diff] [blame] | 250 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 251 | IOReadHandler *chr_read[MAX_MUX]; |
| 252 | IOEventHandler *chr_event[MAX_MUX]; |
| 253 | void *ext_opaque[MAX_MUX]; |
| 254 | CharDriverState *drv; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 255 | int focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 256 | int mux_cnt; |
| 257 | int term_got_escape; |
| 258 | int max_size; |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 259 | /* Intermediate input buffer allows to catch escape sequences even if the |
| 260 | currently active device is not accepting any input - but only until it |
| 261 | is full as well. */ |
| 262 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; |
| 263 | int prod[MAX_MUX]; |
| 264 | int cons[MAX_MUX]; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 265 | int timestamps; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 266 | int linestart; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 267 | int64_t timestamps_start; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 268 | } MuxDriver; |
| 269 | |
| 270 | |
| 271 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 272 | { |
| 273 | MuxDriver *d = chr->opaque; |
| 274 | int ret; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 275 | if (!d->timestamps) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 276 | ret = d->drv->chr_write(d->drv, buf, len); |
| 277 | } else { |
| 278 | int i; |
| 279 | |
| 280 | ret = 0; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 281 | for (i = 0; i < len; i++) { |
| 282 | if (d->linestart) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 283 | char buf1[64]; |
| 284 | int64_t ti; |
| 285 | int secs; |
| 286 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 287 | ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 288 | if (d->timestamps_start == -1) |
| 289 | d->timestamps_start = ti; |
| 290 | ti -= d->timestamps_start; |
aliguori | a4bb1db | 2009-01-22 17:15:16 +0000 | [diff] [blame] | 291 | secs = ti / 1000; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 292 | snprintf(buf1, sizeof(buf1), |
| 293 | "[%02d:%02d:%02d.%03d] ", |
| 294 | secs / 3600, |
| 295 | (secs / 60) % 60, |
| 296 | secs % 60, |
aliguori | a4bb1db | 2009-01-22 17:15:16 +0000 | [diff] [blame] | 297 | (int)(ti % 1000)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 298 | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 299 | d->linestart = 0; |
| 300 | } |
| 301 | ret += d->drv->chr_write(d->drv, buf+i, 1); |
| 302 | if (buf[i] == '\n') { |
| 303 | d->linestart = 1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | } |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static const char * const mux_help[] = { |
| 311 | "% h print this help\n\r", |
| 312 | "% x exit emulator\n\r", |
| 313 | "% s save disk data back to file (if -snapshot)\n\r", |
| 314 | "% t toggle console timestamps\n\r" |
| 315 | "% b send break (magic sysrq)\n\r", |
| 316 | "% c switch between console and monitor\n\r", |
| 317 | "% % sends %\n\r", |
| 318 | NULL |
| 319 | }; |
| 320 | |
| 321 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 322 | static void mux_print_help(CharDriverState *chr) |
| 323 | { |
| 324 | int i, j; |
| 325 | char ebuf[15] = "Escape-Char"; |
| 326 | char cbuf[50] = "\n\r"; |
| 327 | |
| 328 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 329 | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
| 330 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
| 331 | } else { |
| 332 | snprintf(cbuf, sizeof(cbuf), |
| 333 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", |
| 334 | term_escape_char); |
| 335 | } |
| 336 | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
| 337 | for (i = 0; mux_help[i] != NULL; i++) { |
| 338 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 339 | if (mux_help[i][j] == '%') |
| 340 | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
| 341 | else |
| 342 | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 347 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
| 348 | { |
| 349 | if (d->chr_event[mux_nr]) |
| 350 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); |
| 351 | } |
| 352 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 353 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 354 | { |
| 355 | if (d->term_got_escape) { |
| 356 | d->term_got_escape = 0; |
| 357 | if (ch == term_escape_char) |
| 358 | goto send_char; |
| 359 | switch(ch) { |
| 360 | case '?': |
| 361 | case 'h': |
| 362 | mux_print_help(chr); |
| 363 | break; |
| 364 | case 'x': |
| 365 | { |
| 366 | const char *term = "QEMU: Terminated\n\r"; |
| 367 | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
| 368 | exit(0); |
| 369 | break; |
| 370 | } |
| 371 | case 's': |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 372 | bdrv_commit_all(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 373 | break; |
| 374 | case 'b': |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 375 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 376 | break; |
| 377 | case 'c': |
| 378 | /* Switch to the next registered device */ |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 379 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
| 380 | d->focus++; |
| 381 | if (d->focus >= d->mux_cnt) |
| 382 | d->focus = 0; |
| 383 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 384 | break; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 385 | case 't': |
| 386 | d->timestamps = !d->timestamps; |
| 387 | d->timestamps_start = -1; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 388 | d->linestart = 0; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 389 | break; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 390 | } |
| 391 | } else if (ch == term_escape_char) { |
| 392 | d->term_got_escape = 1; |
| 393 | } else { |
| 394 | send_char: |
| 395 | return 1; |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static void mux_chr_accept_input(CharDriverState *chr) |
| 401 | { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 402 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 403 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 404 | |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 405 | while (d->prod[m] != d->cons[m] && |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 406 | d->chr_can_read[m] && |
| 407 | d->chr_can_read[m](d->ext_opaque[m])) { |
| 408 | d->chr_read[m](d->ext_opaque[m], |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 409 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | static int mux_chr_can_read(void *opaque) |
| 414 | { |
| 415 | CharDriverState *chr = opaque; |
| 416 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 417 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 418 | |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 419 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 420 | return 1; |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 421 | if (d->chr_can_read[m]) |
| 422 | return d->chr_can_read[m](d->ext_opaque[m]); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 427 | { |
| 428 | CharDriverState *chr = opaque; |
| 429 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 430 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 431 | int i; |
| 432 | |
| 433 | mux_chr_accept_input (opaque); |
| 434 | |
| 435 | for(i = 0; i < size; i++) |
| 436 | if (mux_proc_byte(chr, d, buf[i])) { |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 437 | if (d->prod[m] == d->cons[m] && |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 438 | d->chr_can_read[m] && |
| 439 | d->chr_can_read[m](d->ext_opaque[m])) |
| 440 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); |
| 441 | else |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 442 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | static void mux_chr_event(void *opaque, int event) |
| 447 | { |
| 448 | CharDriverState *chr = opaque; |
| 449 | MuxDriver *d = chr->opaque; |
| 450 | int i; |
| 451 | |
| 452 | /* Send the event to all registered listeners */ |
| 453 | for (i = 0; i < d->mux_cnt; i++) |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 454 | mux_chr_send_event(d, i, event); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 458 | { |
| 459 | MuxDriver *d = chr->opaque; |
| 460 | |
| 461 | if (d->mux_cnt >= MAX_MUX) { |
| 462 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 463 | return; |
| 464 | } |
| 465 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 466 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 467 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 468 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 469 | /* Fix up the real driver with mux routines */ |
| 470 | if (d->mux_cnt == 0) { |
| 471 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 472 | mux_chr_event, chr); |
| 473 | } |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 474 | if (d->focus != -1) { |
| 475 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 476 | } |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 477 | d->focus = d->mux_cnt; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 478 | d->mux_cnt++; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 479 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 482 | static bool muxes_realized; |
| 483 | |
| 484 | /** |
| 485 | * Called after processing of default and command-line-specified |
| 486 | * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached |
| 487 | * to a mux chardev. This is done here to ensure that |
| 488 | * output/prompts/banners are only displayed for the FE that has |
| 489 | * focus when initial command-line processing/machine init is |
| 490 | * completed. |
| 491 | * |
| 492 | * After this point, any new FE attached to any new or existing |
| 493 | * mux will receive CHR_EVENT_OPENED notifications for the BE |
| 494 | * immediately. |
| 495 | */ |
| 496 | static void muxes_realize_done(Notifier *notifier, void *unused) |
| 497 | { |
| 498 | CharDriverState *chr; |
| 499 | |
| 500 | QTAILQ_FOREACH(chr, &chardevs, next) { |
| 501 | if (chr->is_mux) { |
| 502 | MuxDriver *d = chr->opaque; |
| 503 | int i; |
| 504 | |
| 505 | /* send OPENED to all already-attached FEs */ |
| 506 | for (i = 0; i < d->mux_cnt; i++) { |
| 507 | mux_chr_send_event(d, i, CHR_EVENT_OPENED); |
| 508 | } |
| 509 | /* mark mux as OPENED so any new FEs will immediately receive |
| 510 | * OPENED event |
| 511 | */ |
| 512 | qemu_chr_be_generic_open(chr); |
| 513 | } |
| 514 | } |
| 515 | muxes_realized = true; |
| 516 | } |
| 517 | |
| 518 | static Notifier muxes_realize_notify = { |
| 519 | .notify = muxes_realize_done, |
| 520 | }; |
| 521 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 522 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
| 523 | { |
| 524 | CharDriverState *chr; |
| 525 | MuxDriver *d; |
| 526 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 527 | chr = g_malloc0(sizeof(CharDriverState)); |
| 528 | d = g_malloc0(sizeof(MuxDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 529 | |
| 530 | chr->opaque = d; |
| 531 | d->drv = drv; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 532 | d->focus = -1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 533 | chr->chr_write = mux_chr_write; |
| 534 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
| 535 | chr->chr_accept_input = mux_chr_accept_input; |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 536 | /* Frontend guest-open / -close notification is not support with muxes */ |
Hans de Goede | 574b711 | 2013-03-26 11:07:58 +0100 | [diff] [blame] | 537 | chr->chr_set_fe_open = NULL; |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 538 | /* only default to opened state if we've realized the initial |
| 539 | * set of muxes |
| 540 | */ |
| 541 | chr->explicit_be_open = muxes_realized ? 0 : 1; |
| 542 | chr->is_mux = 1; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 543 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 544 | return chr; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | #ifdef _WIN32 |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 549 | int send_all(int fd, const void *buf, int len1) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 550 | { |
| 551 | int ret, len; |
| 552 | |
| 553 | len = len1; |
| 554 | while (len > 0) { |
| 555 | ret = send(fd, buf, len, 0); |
| 556 | if (ret < 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 557 | errno = WSAGetLastError(); |
| 558 | if (errno != WSAEWOULDBLOCK) { |
| 559 | return -1; |
| 560 | } |
| 561 | } else if (ret == 0) { |
| 562 | break; |
| 563 | } else { |
| 564 | buf += ret; |
| 565 | len -= ret; |
| 566 | } |
| 567 | } |
| 568 | return len1 - len; |
| 569 | } |
| 570 | |
| 571 | #else |
| 572 | |
Jes Sorensen | 5fc9cfe | 2010-11-01 20:02:23 +0100 | [diff] [blame] | 573 | int send_all(int fd, const void *_buf, int len1) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 574 | { |
| 575 | int ret, len; |
Jes Sorensen | 5fc9cfe | 2010-11-01 20:02:23 +0100 | [diff] [blame] | 576 | const uint8_t *buf = _buf; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 577 | |
| 578 | len = len1; |
| 579 | while (len > 0) { |
| 580 | ret = write(fd, buf, len); |
| 581 | if (ret < 0) { |
| 582 | if (errno != EINTR && errno != EAGAIN) |
| 583 | return -1; |
| 584 | } else if (ret == 0) { |
| 585 | break; |
| 586 | } else { |
| 587 | buf += ret; |
| 588 | len -= ret; |
| 589 | } |
| 590 | } |
| 591 | return len1 - len; |
| 592 | } |
Stefan Berger | 4549a8b | 2013-02-27 12:47:53 -0500 | [diff] [blame] | 593 | |
| 594 | int recv_all(int fd, void *_buf, int len1, bool single_read) |
| 595 | { |
| 596 | int ret, len; |
| 597 | uint8_t *buf = _buf; |
| 598 | |
| 599 | len = len1; |
| 600 | while ((len > 0) && (ret = read(fd, buf, len)) != 0) { |
| 601 | if (ret < 0) { |
| 602 | if (errno != EINTR && errno != EAGAIN) { |
| 603 | return -1; |
| 604 | } |
| 605 | continue; |
| 606 | } else { |
| 607 | if (single_read) { |
| 608 | return ret; |
| 609 | } |
| 610 | buf += ret; |
| 611 | len -= ret; |
| 612 | } |
| 613 | } |
| 614 | return len1 - len; |
| 615 | } |
| 616 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 617 | #endif /* !_WIN32 */ |
| 618 | |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 619 | typedef struct IOWatchPoll |
| 620 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 621 | GSource parent; |
| 622 | |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 623 | GIOChannel *channel; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 624 | GSource *src; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 625 | |
| 626 | IOCanReadHandler *fd_can_read; |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 627 | GSourceFunc fd_read; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 628 | void *opaque; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 629 | } IOWatchPoll; |
| 630 | |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 631 | static IOWatchPoll *io_watch_poll_from_source(GSource *source) |
| 632 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 633 | return container_of(source, IOWatchPoll, parent); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_) |
| 637 | { |
| 638 | IOWatchPoll *iwp = io_watch_poll_from_source(source); |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 639 | bool now_active = iwp->fd_can_read(iwp->opaque) > 0; |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 640 | bool was_active = iwp->src != NULL; |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 641 | if (was_active == now_active) { |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 642 | return FALSE; |
| 643 | } |
| 644 | |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 645 | if (now_active) { |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 646 | iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP); |
| 647 | g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 648 | g_source_attach(iwp->src, NULL); |
| 649 | } else { |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 650 | g_source_destroy(iwp->src); |
| 651 | g_source_unref(iwp->src); |
| 652 | iwp->src = NULL; |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 653 | } |
| 654 | return FALSE; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static gboolean io_watch_poll_check(GSource *source) |
| 658 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 659 | return FALSE; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, |
| 663 | gpointer user_data) |
| 664 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 665 | abort(); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static void io_watch_poll_finalize(GSource *source) |
| 669 | { |
Paolo Bonzini | 2b31677 | 2013-04-19 17:32:09 +0200 | [diff] [blame] | 670 | /* Due to a glib bug, removing the last reference to a source |
| 671 | * inside a finalize callback causes recursive locking (and a |
| 672 | * deadlock). This is not a problem inside other callbacks, |
| 673 | * including dispatch callbacks, so we call io_remove_watch_poll |
| 674 | * to remove this source. At this point, iwp->src must |
| 675 | * be NULL, or we would leak it. |
| 676 | * |
| 677 | * This would be solved much more elegantly by child sources, |
| 678 | * but we support older glib versions that do not have them. |
| 679 | */ |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 680 | IOWatchPoll *iwp = io_watch_poll_from_source(source); |
Paolo Bonzini | 2b31677 | 2013-04-19 17:32:09 +0200 | [diff] [blame] | 681 | assert(iwp->src == NULL); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static GSourceFuncs io_watch_poll_funcs = { |
| 685 | .prepare = io_watch_poll_prepare, |
| 686 | .check = io_watch_poll_check, |
| 687 | .dispatch = io_watch_poll_dispatch, |
| 688 | .finalize = io_watch_poll_finalize, |
| 689 | }; |
| 690 | |
| 691 | /* Can only be used for read */ |
| 692 | static guint io_add_watch_poll(GIOChannel *channel, |
| 693 | IOCanReadHandler *fd_can_read, |
| 694 | GIOFunc fd_read, |
| 695 | gpointer user_data) |
| 696 | { |
| 697 | IOWatchPoll *iwp; |
Paolo Bonzini | 0ca5aa4 | 2013-04-10 15:23:27 +0200 | [diff] [blame] | 698 | int tag; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 699 | |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 700 | iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll)); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 701 | iwp->fd_can_read = fd_can_read; |
| 702 | iwp->opaque = user_data; |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 703 | iwp->channel = channel; |
| 704 | iwp->fd_read = (GSourceFunc) fd_read; |
| 705 | iwp->src = NULL; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 706 | |
Paolo Bonzini | 0ca5aa4 | 2013-04-10 15:23:27 +0200 | [diff] [blame] | 707 | tag = g_source_attach(&iwp->parent, NULL); |
| 708 | g_source_unref(&iwp->parent); |
| 709 | return tag; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 710 | } |
| 711 | |
Paolo Bonzini | 2b31677 | 2013-04-19 17:32:09 +0200 | [diff] [blame] | 712 | static void io_remove_watch_poll(guint tag) |
| 713 | { |
| 714 | GSource *source; |
| 715 | IOWatchPoll *iwp; |
| 716 | |
| 717 | g_return_if_fail (tag > 0); |
| 718 | |
| 719 | source = g_main_context_find_source_by_id(NULL, tag); |
| 720 | g_return_if_fail (source != NULL); |
| 721 | |
| 722 | iwp = io_watch_poll_from_source(source); |
| 723 | if (iwp->src) { |
| 724 | g_source_destroy(iwp->src); |
| 725 | g_source_unref(iwp->src); |
| 726 | iwp->src = NULL; |
| 727 | } |
| 728 | g_source_destroy(&iwp->parent); |
| 729 | } |
| 730 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 731 | static void remove_fd_in_watch(CharDriverState *chr) |
| 732 | { |
| 733 | if (chr->fd_in_tag) { |
| 734 | io_remove_watch_poll(chr->fd_in_tag); |
| 735 | chr->fd_in_tag = 0; |
| 736 | } |
| 737 | } |
| 738 | |
Blue Swirl | 44ab9ed | 2013-03-09 09:56:04 +0000 | [diff] [blame] | 739 | #ifndef _WIN32 |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 740 | static GIOChannel *io_channel_from_fd(int fd) |
| 741 | { |
| 742 | GIOChannel *chan; |
| 743 | |
| 744 | if (fd == -1) { |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | chan = g_io_channel_unix_new(fd); |
| 749 | |
| 750 | g_io_channel_set_encoding(chan, NULL, NULL); |
| 751 | g_io_channel_set_buffered(chan, FALSE); |
| 752 | |
| 753 | return chan; |
| 754 | } |
Blue Swirl | 44ab9ed | 2013-03-09 09:56:04 +0000 | [diff] [blame] | 755 | #endif |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 756 | |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 757 | static GIOChannel *io_channel_from_socket(int fd) |
| 758 | { |
| 759 | GIOChannel *chan; |
| 760 | |
| 761 | if (fd == -1) { |
| 762 | return NULL; |
| 763 | } |
| 764 | |
| 765 | #ifdef _WIN32 |
| 766 | chan = g_io_channel_win32_new_socket(fd); |
| 767 | #else |
| 768 | chan = g_io_channel_unix_new(fd); |
| 769 | #endif |
| 770 | |
| 771 | g_io_channel_set_encoding(chan, NULL, NULL); |
| 772 | g_io_channel_set_buffered(chan, FALSE); |
| 773 | |
| 774 | return chan; |
| 775 | } |
| 776 | |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 777 | static int io_channel_send(GIOChannel *fd, const void *buf, size_t len) |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 778 | { |
Laszlo Ersek | ac8c26f | 2013-07-16 20:19:40 +0200 | [diff] [blame] | 779 | size_t offset = 0; |
| 780 | GIOStatus status = G_IO_STATUS_NORMAL; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 781 | |
Laszlo Ersek | ac8c26f | 2013-07-16 20:19:40 +0200 | [diff] [blame] | 782 | while (offset < len && status == G_IO_STATUS_NORMAL) { |
| 783 | gsize bytes_written = 0; |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 784 | |
| 785 | status = g_io_channel_write_chars(fd, buf + offset, len - offset, |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 786 | &bytes_written, NULL); |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 787 | offset += bytes_written; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 788 | } |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 789 | |
Laszlo Ersek | ac8c26f | 2013-07-16 20:19:40 +0200 | [diff] [blame] | 790 | if (offset > 0) { |
| 791 | return offset; |
| 792 | } |
| 793 | switch (status) { |
| 794 | case G_IO_STATUS_NORMAL: |
| 795 | g_assert(len == 0); |
| 796 | return 0; |
| 797 | case G_IO_STATUS_AGAIN: |
| 798 | errno = EAGAIN; |
| 799 | return -1; |
| 800 | default: |
| 801 | break; |
| 802 | } |
| 803 | errno = EINVAL; |
| 804 | return -1; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 805 | } |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 806 | |
Blue Swirl | 44ab9ed | 2013-03-09 09:56:04 +0000 | [diff] [blame] | 807 | #ifndef _WIN32 |
| 808 | |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 809 | typedef struct FDCharDriver { |
| 810 | CharDriverState *chr; |
| 811 | GIOChannel *fd_in, *fd_out; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 812 | int max_size; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 813 | QTAILQ_ENTRY(FDCharDriver) node; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 814 | } FDCharDriver; |
| 815 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 816 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 817 | { |
| 818 | FDCharDriver *s = chr->opaque; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 819 | |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 820 | return io_channel_send(s->fd_out, buf, len); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
| 824 | { |
| 825 | CharDriverState *chr = opaque; |
| 826 | FDCharDriver *s = chr->opaque; |
| 827 | int len; |
| 828 | uint8_t buf[READ_BUF_LEN]; |
| 829 | GIOStatus status; |
| 830 | gsize bytes_read; |
| 831 | |
| 832 | len = sizeof(buf); |
| 833 | if (len > s->max_size) { |
| 834 | len = s->max_size; |
| 835 | } |
| 836 | if (len == 0) { |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 837 | return TRUE; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | status = g_io_channel_read_chars(chan, (gchar *)buf, |
| 841 | len, &bytes_read, NULL); |
| 842 | if (status == G_IO_STATUS_EOF) { |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 843 | remove_fd_in_watch(chr); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 844 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
| 845 | return FALSE; |
| 846 | } |
| 847 | if (status == G_IO_STATUS_NORMAL) { |
| 848 | qemu_chr_be_write(chr, buf, bytes_read); |
| 849 | } |
| 850 | |
| 851 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static int fd_chr_read_poll(void *opaque) |
| 855 | { |
| 856 | CharDriverState *chr = opaque; |
| 857 | FDCharDriver *s = chr->opaque; |
| 858 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 859 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 860 | return s->max_size; |
| 861 | } |
| 862 | |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 863 | static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
| 864 | { |
| 865 | FDCharDriver *s = chr->opaque; |
| 866 | return g_io_create_watch(s->fd_out, cond); |
| 867 | } |
| 868 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 869 | static void fd_chr_update_read_handler(CharDriverState *chr) |
| 870 | { |
| 871 | FDCharDriver *s = chr->opaque; |
| 872 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 873 | remove_fd_in_watch(chr); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 874 | if (s->fd_in) { |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 875 | chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, |
| 876 | fd_chr_read, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | static void fd_chr_close(struct CharDriverState *chr) |
| 881 | { |
| 882 | FDCharDriver *s = chr->opaque; |
| 883 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 884 | remove_fd_in_watch(chr); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 885 | if (s->fd_in) { |
| 886 | g_io_channel_unref(s->fd_in); |
| 887 | } |
| 888 | if (s->fd_out) { |
| 889 | g_io_channel_unref(s->fd_out); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 892 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 893 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* open a character device to a unix fd */ |
| 897 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
| 898 | { |
| 899 | CharDriverState *chr; |
| 900 | FDCharDriver *s; |
| 901 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 902 | chr = g_malloc0(sizeof(CharDriverState)); |
| 903 | s = g_malloc0(sizeof(FDCharDriver)); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 904 | s->fd_in = io_channel_from_fd(fd_in); |
| 905 | s->fd_out = io_channel_from_fd(fd_out); |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 906 | fcntl(fd_out, F_SETFL, O_NONBLOCK); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 907 | s->chr = chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 908 | chr->opaque = s; |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 909 | chr->chr_add_watch = fd_chr_add_watch; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 910 | chr->chr_write = fd_chr_write; |
| 911 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
| 912 | chr->chr_close = fd_chr_close; |
| 913 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 914 | return chr; |
| 915 | } |
| 916 | |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 917 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 918 | { |
| 919 | int fd_in, fd_out; |
| 920 | char filename_in[256], filename_out[256]; |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 921 | const char *filename = opts->device; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 922 | |
| 923 | if (filename == NULL) { |
| 924 | fprintf(stderr, "chardev: pipe: no filename given\n"); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 925 | return NULL; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 926 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 927 | |
| 928 | snprintf(filename_in, 256, "%s.in", filename); |
| 929 | snprintf(filename_out, 256, "%s.out", filename); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 930 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
| 931 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 932 | if (fd_in < 0 || fd_out < 0) { |
| 933 | if (fd_in >= 0) |
| 934 | close(fd_in); |
| 935 | if (fd_out >= 0) |
| 936 | close(fd_out); |
Markus Armbruster | b181e04 | 2012-02-07 15:09:09 +0100 | [diff] [blame] | 937 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
Markus Armbruster | a89dd6c | 2012-02-07 15:09:10 +0100 | [diff] [blame] | 938 | if (fd_in < 0) { |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 939 | return NULL; |
Markus Armbruster | a89dd6c | 2012-02-07 15:09:10 +0100 | [diff] [blame] | 940 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 941 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 942 | return qemu_chr_open_fd(fd_in, fd_out); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 943 | } |
| 944 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 945 | /* init terminal so that we can grab keys */ |
| 946 | static struct termios oldtty; |
| 947 | static int old_fd0_flags; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 948 | static bool stdio_allow_signal; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 949 | |
| 950 | static void term_exit(void) |
| 951 | { |
| 952 | tcsetattr (0, TCSANOW, &oldtty); |
| 953 | fcntl(0, F_SETFL, old_fd0_flags); |
| 954 | } |
| 955 | |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 956 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 957 | { |
| 958 | struct termios tty; |
| 959 | |
Paolo Bonzini | 0369364 | 2010-12-23 13:42:49 +0100 | [diff] [blame] | 960 | tty = oldtty; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 961 | if (!echo) { |
| 962 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 963 | |INLCR|IGNCR|ICRNL|IXON); |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 964 | tty.c_oflag |= OPOST; |
| 965 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 966 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 967 | tty.c_cflag |= CS8; |
| 968 | tty.c_cc[VMIN] = 1; |
| 969 | tty.c_cc[VTIME] = 0; |
| 970 | } |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 971 | if (!stdio_allow_signal) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 972 | tty.c_lflag &= ~ISIG; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 973 | |
| 974 | tcsetattr (0, TCSANOW, &tty); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
| 978 | { |
| 979 | term_exit(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 980 | fd_chr_close(chr); |
| 981 | } |
| 982 | |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 983 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 984 | { |
| 985 | CharDriverState *chr; |
| 986 | |
Michael Tokarev | ab51b1d | 2012-12-30 12:48:14 +0400 | [diff] [blame] | 987 | if (is_daemonized()) { |
| 988 | error_report("cannot use stdio with -daemonize"); |
| 989 | return NULL; |
| 990 | } |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 991 | old_fd0_flags = fcntl(0, F_GETFL); |
| 992 | tcgetattr (0, &oldtty); |
| 993 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 994 | atexit(term_exit); |
Paolo Bonzini | 0369364 | 2010-12-23 13:42:49 +0100 | [diff] [blame] | 995 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 996 | chr = qemu_chr_open_fd(0, 1); |
| 997 | chr->chr_close = qemu_chr_close_stdio; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 998 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 999 | if (opts->has_signal) { |
| 1000 | stdio_allow_signal = opts->signal; |
| 1001 | } |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 1002 | qemu_chr_fe_set_echo(chr, false); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1003 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1004 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1007 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 1008 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
| 1009 | || defined(__GLIBC__) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1010 | |
Gerd Hoffmann | e551498 | 2012-12-19 16:35:42 +0100 | [diff] [blame] | 1011 | #define HAVE_CHARDEV_TTY 1 |
| 1012 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1013 | typedef struct { |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1014 | GIOChannel *fd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1015 | int connected; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1016 | int read_bytes; |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1017 | guint timer_tag; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1018 | } PtyCharDriver; |
| 1019 | |
| 1020 | static void pty_chr_update_read_handler(CharDriverState *chr); |
| 1021 | static void pty_chr_state(CharDriverState *chr, int connected); |
| 1022 | |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1023 | static gboolean pty_chr_timer(gpointer opaque) |
| 1024 | { |
| 1025 | struct CharDriverState *chr = opaque; |
| 1026 | PtyCharDriver *s = chr->opaque; |
| 1027 | |
Hans de Goede | 79f2007 | 2013-04-25 13:53:02 +0200 | [diff] [blame] | 1028 | s->timer_tag = 0; |
Gerd Hoffmann | b0d768c | 2013-08-22 11:43:58 +0200 | [diff] [blame] | 1029 | if (!s->connected) { |
| 1030 | /* Next poll ... */ |
| 1031 | pty_chr_update_read_handler(chr); |
| 1032 | } |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1033 | return FALSE; |
| 1034 | } |
| 1035 | |
| 1036 | static void pty_chr_rearm_timer(CharDriverState *chr, int ms) |
| 1037 | { |
| 1038 | PtyCharDriver *s = chr->opaque; |
| 1039 | |
| 1040 | if (s->timer_tag) { |
| 1041 | g_source_remove(s->timer_tag); |
| 1042 | s->timer_tag = 0; |
| 1043 | } |
| 1044 | |
| 1045 | if (ms == 1000) { |
| 1046 | s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); |
| 1047 | } else { |
| 1048 | s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); |
| 1049 | } |
| 1050 | } |
| 1051 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1052 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1053 | { |
| 1054 | PtyCharDriver *s = chr->opaque; |
| 1055 | |
| 1056 | if (!s->connected) { |
| 1057 | /* guest sends data, check for (re-)connect */ |
| 1058 | pty_chr_update_read_handler(chr); |
| 1059 | return 0; |
| 1060 | } |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 1061 | return io_channel_send(s->fd, buf, len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Anthony Liguori | e6a87ed | 2013-03-05 23:21:24 +0530 | [diff] [blame] | 1064 | static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
| 1065 | { |
| 1066 | PtyCharDriver *s = chr->opaque; |
| 1067 | return g_io_create_watch(s->fd, cond); |
| 1068 | } |
| 1069 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1070 | static int pty_chr_read_poll(void *opaque) |
| 1071 | { |
| 1072 | CharDriverState *chr = opaque; |
| 1073 | PtyCharDriver *s = chr->opaque; |
| 1074 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 1075 | s->read_bytes = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1076 | return s->read_bytes; |
| 1077 | } |
| 1078 | |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1079 | static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1080 | { |
| 1081 | CharDriverState *chr = opaque; |
| 1082 | PtyCharDriver *s = chr->opaque; |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1083 | gsize size, len; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 1084 | uint8_t buf[READ_BUF_LEN]; |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1085 | GIOStatus status; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1086 | |
| 1087 | len = sizeof(buf); |
| 1088 | if (len > s->read_bytes) |
| 1089 | len = s->read_bytes; |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 1090 | if (len == 0) { |
| 1091 | return TRUE; |
| 1092 | } |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1093 | status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL); |
| 1094 | if (status != G_IO_STATUS_NORMAL) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1095 | pty_chr_state(chr, 0); |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1096 | return FALSE; |
| 1097 | } else { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1098 | pty_chr_state(chr, 1); |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1099 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1100 | } |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1101 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static void pty_chr_update_read_handler(CharDriverState *chr) |
| 1105 | { |
| 1106 | PtyCharDriver *s = chr->opaque; |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1107 | GPollFD pfd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1108 | |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1109 | pfd.fd = g_io_channel_unix_get_fd(s->fd); |
| 1110 | pfd.events = G_IO_OUT; |
| 1111 | pfd.revents = 0; |
| 1112 | g_poll(&pfd, 1, 0); |
| 1113 | if (pfd.revents & G_IO_HUP) { |
| 1114 | pty_chr_state(chr, 0); |
| 1115 | } else { |
| 1116 | pty_chr_state(chr, 1); |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1117 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | static void pty_chr_state(CharDriverState *chr, int connected) |
| 1121 | { |
| 1122 | PtyCharDriver *s = chr->opaque; |
| 1123 | |
| 1124 | if (!connected) { |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 1125 | remove_fd_in_watch(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1126 | s->connected = 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1127 | /* (re-)connect poll interval for idle guests: once per second. |
| 1128 | * We check more frequently in case the guests sends data to |
| 1129 | * the virtual device linked to our pty. */ |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1130 | pty_chr_rearm_timer(chr, 1000); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1131 | } else { |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1132 | if (s->timer_tag) { |
| 1133 | g_source_remove(s->timer_tag); |
| 1134 | s->timer_tag = 0; |
| 1135 | } |
| 1136 | if (!s->connected) { |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1137 | s->connected = 1; |
James Hogan | 3a3567d | 2013-08-08 12:09:38 +0100 | [diff] [blame] | 1138 | qemu_chr_be_generic_open(chr); |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 1139 | } |
| 1140 | if (!chr->fd_in_tag) { |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 1141 | chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, |
| 1142 | pty_chr_read, chr); |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1143 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1147 | static void pty_chr_close(struct CharDriverState *chr) |
| 1148 | { |
| 1149 | PtyCharDriver *s = chr->opaque; |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1150 | int fd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1151 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 1152 | remove_fd_in_watch(chr); |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1153 | fd = g_io_channel_unix_get_fd(s->fd); |
| 1154 | g_io_channel_unref(s->fd); |
| 1155 | close(fd); |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1156 | if (s->timer_tag) { |
| 1157 | g_source_remove(s->timer_tag); |
Paolo Bonzini | 910b636 | 2013-04-19 17:32:06 +0200 | [diff] [blame] | 1158 | s->timer_tag = 0; |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1159 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1160 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1161 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1164 | static CharDriverState *qemu_chr_open_pty(const char *id, |
| 1165 | ChardevReturn *ret) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1166 | { |
| 1167 | CharDriverState *chr; |
| 1168 | PtyCharDriver *s; |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1169 | int master_fd, slave_fd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1170 | char pty_name[PATH_MAX]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1171 | |
Michael Tokarev | 4efeabb | 2013-06-05 18:44:54 +0400 | [diff] [blame] | 1172 | master_fd = qemu_openpty_raw(&slave_fd, pty_name); |
| 1173 | if (master_fd < 0) { |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1174 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1177 | close(slave_fd); |
| 1178 | |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 1179 | chr = g_malloc0(sizeof(CharDriverState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1180 | |
Michael Tokarev | 4efeabb | 2013-06-05 18:44:54 +0400 | [diff] [blame] | 1181 | chr->filename = g_strdup_printf("pty:%s", pty_name); |
| 1182 | ret->pty = g_strdup(pty_name); |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1183 | ret->has_pty = true; |
Lei Li | 5865021 | 2012-12-21 12:26:38 +0800 | [diff] [blame] | 1184 | |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1185 | fprintf(stderr, "char device redirected to %s (label %s)\n", |
Michael Tokarev | 4efeabb | 2013-06-05 18:44:54 +0400 | [diff] [blame] | 1186 | pty_name, id); |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 1187 | |
| 1188 | s = g_malloc0(sizeof(PtyCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1189 | chr->opaque = s; |
| 1190 | chr->chr_write = pty_chr_write; |
| 1191 | chr->chr_update_read_handler = pty_chr_update_read_handler; |
| 1192 | chr->chr_close = pty_chr_close; |
Anthony Liguori | e6a87ed | 2013-03-05 23:21:24 +0530 | [diff] [blame] | 1193 | chr->chr_add_watch = pty_chr_add_watch; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 1194 | chr->explicit_be_open = true; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1195 | |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1196 | s->fd = io_channel_from_fd(master_fd); |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1197 | s->timer_tag = 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1198 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1199 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static void tty_serial_init(int fd, int speed, |
| 1203 | int parity, int data_bits, int stop_bits) |
| 1204 | { |
| 1205 | struct termios tty; |
| 1206 | speed_t spd; |
| 1207 | |
| 1208 | #if 0 |
| 1209 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1210 | speed, parity, data_bits, stop_bits); |
| 1211 | #endif |
| 1212 | tcgetattr (fd, &tty); |
| 1213 | |
Stefan Weil | 45eea13 | 2009-10-26 16:10:10 +0100 | [diff] [blame] | 1214 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
| 1215 | speed = speed * 10 / 11; |
| 1216 | do { |
| 1217 | check_speed(50); |
| 1218 | check_speed(75); |
| 1219 | check_speed(110); |
| 1220 | check_speed(134); |
| 1221 | check_speed(150); |
| 1222 | check_speed(200); |
| 1223 | check_speed(300); |
| 1224 | check_speed(600); |
| 1225 | check_speed(1200); |
| 1226 | check_speed(1800); |
| 1227 | check_speed(2400); |
| 1228 | check_speed(4800); |
| 1229 | check_speed(9600); |
| 1230 | check_speed(19200); |
| 1231 | check_speed(38400); |
| 1232 | /* Non-Posix values follow. They may be unsupported on some systems. */ |
| 1233 | check_speed(57600); |
| 1234 | check_speed(115200); |
| 1235 | #ifdef B230400 |
| 1236 | check_speed(230400); |
| 1237 | #endif |
| 1238 | #ifdef B460800 |
| 1239 | check_speed(460800); |
| 1240 | #endif |
| 1241 | #ifdef B500000 |
| 1242 | check_speed(500000); |
| 1243 | #endif |
| 1244 | #ifdef B576000 |
| 1245 | check_speed(576000); |
| 1246 | #endif |
| 1247 | #ifdef B921600 |
| 1248 | check_speed(921600); |
| 1249 | #endif |
| 1250 | #ifdef B1000000 |
| 1251 | check_speed(1000000); |
| 1252 | #endif |
| 1253 | #ifdef B1152000 |
| 1254 | check_speed(1152000); |
| 1255 | #endif |
| 1256 | #ifdef B1500000 |
| 1257 | check_speed(1500000); |
| 1258 | #endif |
| 1259 | #ifdef B2000000 |
| 1260 | check_speed(2000000); |
| 1261 | #endif |
| 1262 | #ifdef B2500000 |
| 1263 | check_speed(2500000); |
| 1264 | #endif |
| 1265 | #ifdef B3000000 |
| 1266 | check_speed(3000000); |
| 1267 | #endif |
| 1268 | #ifdef B3500000 |
| 1269 | check_speed(3500000); |
| 1270 | #endif |
| 1271 | #ifdef B4000000 |
| 1272 | check_speed(4000000); |
| 1273 | #endif |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1274 | spd = B115200; |
Stefan Weil | 45eea13 | 2009-10-26 16:10:10 +0100 | [diff] [blame] | 1275 | } while (0); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1276 | |
| 1277 | cfsetispeed(&tty, spd); |
| 1278 | cfsetospeed(&tty, spd); |
| 1279 | |
| 1280 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1281 | |INLCR|IGNCR|ICRNL|IXON); |
| 1282 | tty.c_oflag |= OPOST; |
| 1283 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
| 1284 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
| 1285 | switch(data_bits) { |
| 1286 | default: |
| 1287 | case 8: |
| 1288 | tty.c_cflag |= CS8; |
| 1289 | break; |
| 1290 | case 7: |
| 1291 | tty.c_cflag |= CS7; |
| 1292 | break; |
| 1293 | case 6: |
| 1294 | tty.c_cflag |= CS6; |
| 1295 | break; |
| 1296 | case 5: |
| 1297 | tty.c_cflag |= CS5; |
| 1298 | break; |
| 1299 | } |
| 1300 | switch(parity) { |
| 1301 | default: |
| 1302 | case 'N': |
| 1303 | break; |
| 1304 | case 'E': |
| 1305 | tty.c_cflag |= PARENB; |
| 1306 | break; |
| 1307 | case 'O': |
| 1308 | tty.c_cflag |= PARENB | PARODD; |
| 1309 | break; |
| 1310 | } |
| 1311 | if (stop_bits == 2) |
| 1312 | tty.c_cflag |= CSTOPB; |
| 1313 | |
| 1314 | tcsetattr (fd, TCSANOW, &tty); |
| 1315 | } |
| 1316 | |
| 1317 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1318 | { |
| 1319 | FDCharDriver *s = chr->opaque; |
| 1320 | |
| 1321 | switch(cmd) { |
| 1322 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1323 | { |
| 1324 | QEMUSerialSetParams *ssp = arg; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1325 | tty_serial_init(g_io_channel_unix_get_fd(s->fd_in), |
| 1326 | ssp->speed, ssp->parity, |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1327 | ssp->data_bits, ssp->stop_bits); |
| 1328 | } |
| 1329 | break; |
| 1330 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1331 | { |
| 1332 | int enable = *(int *)arg; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1333 | if (enable) { |
| 1334 | tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1); |
| 1335 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1336 | } |
| 1337 | break; |
| 1338 | case CHR_IOCTL_SERIAL_GET_TIOCM: |
| 1339 | { |
| 1340 | int sarg = 0; |
| 1341 | int *targ = (int *)arg; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1342 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1343 | *targ = 0; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1344 | if (sarg & TIOCM_CTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1345 | *targ |= CHR_TIOCM_CTS; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1346 | if (sarg & TIOCM_CAR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1347 | *targ |= CHR_TIOCM_CAR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1348 | if (sarg & TIOCM_DSR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1349 | *targ |= CHR_TIOCM_DSR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1350 | if (sarg & TIOCM_RI) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1351 | *targ |= CHR_TIOCM_RI; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1352 | if (sarg & TIOCM_DTR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1353 | *targ |= CHR_TIOCM_DTR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1354 | if (sarg & TIOCM_RTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1355 | *targ |= CHR_TIOCM_RTS; |
| 1356 | } |
| 1357 | break; |
| 1358 | case CHR_IOCTL_SERIAL_SET_TIOCM: |
| 1359 | { |
| 1360 | int sarg = *(int *)arg; |
| 1361 | int targ = 0; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1362 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ); |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1363 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
| 1364 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); |
| 1365 | if (sarg & CHR_TIOCM_CTS) |
| 1366 | targ |= TIOCM_CTS; |
| 1367 | if (sarg & CHR_TIOCM_CAR) |
| 1368 | targ |= TIOCM_CAR; |
| 1369 | if (sarg & CHR_TIOCM_DSR) |
| 1370 | targ |= TIOCM_DSR; |
| 1371 | if (sarg & CHR_TIOCM_RI) |
| 1372 | targ |= TIOCM_RI; |
| 1373 | if (sarg & CHR_TIOCM_DTR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1374 | targ |= TIOCM_DTR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1375 | if (sarg & CHR_TIOCM_RTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1376 | targ |= TIOCM_RTS; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1377 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1378 | } |
| 1379 | break; |
| 1380 | default: |
| 1381 | return -ENOTSUP; |
| 1382 | } |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1386 | static void qemu_chr_close_tty(CharDriverState *chr) |
| 1387 | { |
| 1388 | FDCharDriver *s = chr->opaque; |
| 1389 | int fd = -1; |
| 1390 | |
| 1391 | if (s) { |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1392 | fd = g_io_channel_unix_get_fd(s->fd_in); |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | fd_chr_close(chr); |
| 1396 | |
| 1397 | if (fd >= 0) { |
| 1398 | close(fd); |
| 1399 | } |
| 1400 | } |
| 1401 | |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 1402 | static CharDriverState *qemu_chr_open_tty_fd(int fd) |
| 1403 | { |
| 1404 | CharDriverState *chr; |
| 1405 | |
| 1406 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 1407 | chr = qemu_chr_open_fd(fd, fd); |
| 1408 | chr->chr_ioctl = tty_serial_ioctl; |
| 1409 | chr->chr_close = qemu_chr_close_tty; |
| 1410 | return chr; |
| 1411 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1412 | #endif /* __linux__ || __sun__ */ |
| 1413 | |
| 1414 | #if defined(__linux__) |
Gerd Hoffmann | e551498 | 2012-12-19 16:35:42 +0100 | [diff] [blame] | 1415 | |
| 1416 | #define HAVE_CHARDEV_PARPORT 1 |
| 1417 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1418 | typedef struct { |
| 1419 | int fd; |
| 1420 | int mode; |
| 1421 | } ParallelCharDriver; |
| 1422 | |
| 1423 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 1424 | { |
| 1425 | if (s->mode != mode) { |
| 1426 | int m = mode; |
| 1427 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 1428 | return 0; |
| 1429 | s->mode = mode; |
| 1430 | } |
| 1431 | return 1; |
| 1432 | } |
| 1433 | |
| 1434 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1435 | { |
| 1436 | ParallelCharDriver *drv = chr->opaque; |
| 1437 | int fd = drv->fd; |
| 1438 | uint8_t b; |
| 1439 | |
| 1440 | switch(cmd) { |
| 1441 | case CHR_IOCTL_PP_READ_DATA: |
| 1442 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1443 | return -ENOTSUP; |
| 1444 | *(uint8_t *)arg = b; |
| 1445 | break; |
| 1446 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1447 | b = *(uint8_t *)arg; |
| 1448 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1449 | return -ENOTSUP; |
| 1450 | break; |
| 1451 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1452 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1453 | return -ENOTSUP; |
| 1454 | /* Linux gives only the lowest bits, and no way to know data |
| 1455 | direction! For better compatibility set the fixed upper |
| 1456 | bits. */ |
| 1457 | *(uint8_t *)arg = b | 0xc0; |
| 1458 | break; |
| 1459 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1460 | b = *(uint8_t *)arg; |
| 1461 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1462 | return -ENOTSUP; |
| 1463 | break; |
| 1464 | case CHR_IOCTL_PP_READ_STATUS: |
| 1465 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1466 | return -ENOTSUP; |
| 1467 | *(uint8_t *)arg = b; |
| 1468 | break; |
| 1469 | case CHR_IOCTL_PP_DATA_DIR: |
| 1470 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
| 1471 | return -ENOTSUP; |
| 1472 | break; |
| 1473 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 1474 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1475 | struct ParallelIOArg *parg = arg; |
| 1476 | int n = read(fd, parg->buffer, parg->count); |
| 1477 | if (n != parg->count) { |
| 1478 | return -EIO; |
| 1479 | } |
| 1480 | } |
| 1481 | break; |
| 1482 | case CHR_IOCTL_PP_EPP_READ: |
| 1483 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1484 | struct ParallelIOArg *parg = arg; |
| 1485 | int n = read(fd, parg->buffer, parg->count); |
| 1486 | if (n != parg->count) { |
| 1487 | return -EIO; |
| 1488 | } |
| 1489 | } |
| 1490 | break; |
| 1491 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 1492 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1493 | struct ParallelIOArg *parg = arg; |
| 1494 | int n = write(fd, parg->buffer, parg->count); |
| 1495 | if (n != parg->count) { |
| 1496 | return -EIO; |
| 1497 | } |
| 1498 | } |
| 1499 | break; |
| 1500 | case CHR_IOCTL_PP_EPP_WRITE: |
| 1501 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1502 | struct ParallelIOArg *parg = arg; |
| 1503 | int n = write(fd, parg->buffer, parg->count); |
| 1504 | if (n != parg->count) { |
| 1505 | return -EIO; |
| 1506 | } |
| 1507 | } |
| 1508 | break; |
| 1509 | default: |
| 1510 | return -ENOTSUP; |
| 1511 | } |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | static void pp_close(CharDriverState *chr) |
| 1516 | { |
| 1517 | ParallelCharDriver *drv = chr->opaque; |
| 1518 | int fd = drv->fd; |
| 1519 | |
| 1520 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 1521 | ioctl(fd, PPRELEASE); |
| 1522 | close(fd); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1523 | g_free(drv); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1524 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Gerd Hoffmann | 88a946d | 2013-01-10 14:20:58 +0100 | [diff] [blame] | 1527 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1528 | { |
| 1529 | CharDriverState *chr; |
| 1530 | ParallelCharDriver *drv; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (ioctl(fd, PPCLAIM) < 0) { |
| 1533 | close(fd); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1534 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1537 | drv = g_malloc0(sizeof(ParallelCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1538 | drv->fd = fd; |
| 1539 | drv->mode = IEEE1284_MODE_COMPAT; |
| 1540 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1541 | chr = g_malloc0(sizeof(CharDriverState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1542 | chr->chr_write = null_chr_write; |
| 1543 | chr->chr_ioctl = pp_ioctl; |
| 1544 | chr->chr_close = pp_close; |
| 1545 | chr->opaque = drv; |
| 1546 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1547 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1548 | } |
| 1549 | #endif /* __linux__ */ |
| 1550 | |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 1551 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Gerd Hoffmann | e551498 | 2012-12-19 16:35:42 +0100 | [diff] [blame] | 1552 | |
| 1553 | #define HAVE_CHARDEV_PARPORT 1 |
| 1554 | |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1555 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1556 | { |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 1557 | int fd = (int)(intptr_t)chr->opaque; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1558 | uint8_t b; |
| 1559 | |
| 1560 | switch(cmd) { |
| 1561 | case CHR_IOCTL_PP_READ_DATA: |
| 1562 | if (ioctl(fd, PPIGDATA, &b) < 0) |
| 1563 | return -ENOTSUP; |
| 1564 | *(uint8_t *)arg = b; |
| 1565 | break; |
| 1566 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1567 | b = *(uint8_t *)arg; |
| 1568 | if (ioctl(fd, PPISDATA, &b) < 0) |
| 1569 | return -ENOTSUP; |
| 1570 | break; |
| 1571 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1572 | if (ioctl(fd, PPIGCTRL, &b) < 0) |
| 1573 | return -ENOTSUP; |
| 1574 | *(uint8_t *)arg = b; |
| 1575 | break; |
| 1576 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1577 | b = *(uint8_t *)arg; |
| 1578 | if (ioctl(fd, PPISCTRL, &b) < 0) |
| 1579 | return -ENOTSUP; |
| 1580 | break; |
| 1581 | case CHR_IOCTL_PP_READ_STATUS: |
| 1582 | if (ioctl(fd, PPIGSTATUS, &b) < 0) |
| 1583 | return -ENOTSUP; |
| 1584 | *(uint8_t *)arg = b; |
| 1585 | break; |
| 1586 | default: |
| 1587 | return -ENOTSUP; |
| 1588 | } |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Gerd Hoffmann | 88a946d | 2013-01-10 14:20:58 +0100 | [diff] [blame] | 1592 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1593 | { |
| 1594 | CharDriverState *chr; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1595 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1596 | chr = g_malloc0(sizeof(CharDriverState)); |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 1597 | chr->opaque = (void *)(intptr_t)fd; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1598 | chr->chr_write = null_chr_write; |
| 1599 | chr->chr_ioctl = pp_ioctl; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 1600 | chr->explicit_be_open = true; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1601 | return chr; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1602 | } |
| 1603 | #endif |
| 1604 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1605 | #else /* _WIN32 */ |
| 1606 | |
| 1607 | typedef struct { |
| 1608 | int max_size; |
| 1609 | HANDLE hcom, hrecv, hsend; |
| 1610 | OVERLAPPED orecv, osend; |
| 1611 | BOOL fpipe; |
| 1612 | DWORD len; |
| 1613 | } WinCharState; |
| 1614 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1615 | typedef struct { |
| 1616 | HANDLE hStdIn; |
| 1617 | HANDLE hInputReadyEvent; |
| 1618 | HANDLE hInputDoneEvent; |
| 1619 | HANDLE hInputThread; |
| 1620 | uint8_t win_stdio_buf; |
| 1621 | } WinStdioCharState; |
| 1622 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1623 | #define NSENDBUF 2048 |
| 1624 | #define NRECVBUF 2048 |
| 1625 | #define MAXCONNECT 1 |
| 1626 | #define NTIMEOUT 5000 |
| 1627 | |
| 1628 | static int win_chr_poll(void *opaque); |
| 1629 | static int win_chr_pipe_poll(void *opaque); |
| 1630 | |
| 1631 | static void win_chr_close(CharDriverState *chr) |
| 1632 | { |
| 1633 | WinCharState *s = chr->opaque; |
| 1634 | |
| 1635 | if (s->hsend) { |
| 1636 | CloseHandle(s->hsend); |
| 1637 | s->hsend = NULL; |
| 1638 | } |
| 1639 | if (s->hrecv) { |
| 1640 | CloseHandle(s->hrecv); |
| 1641 | s->hrecv = NULL; |
| 1642 | } |
| 1643 | if (s->hcom) { |
| 1644 | CloseHandle(s->hcom); |
| 1645 | s->hcom = NULL; |
| 1646 | } |
| 1647 | if (s->fpipe) |
| 1648 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
| 1649 | else |
| 1650 | qemu_del_polling_cb(win_chr_poll, chr); |
Amit Shah | 793cbfb | 2009-08-11 21:27:48 +0530 | [diff] [blame] | 1651 | |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1652 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static int win_chr_init(CharDriverState *chr, const char *filename) |
| 1656 | { |
| 1657 | WinCharState *s = chr->opaque; |
| 1658 | COMMCONFIG comcfg; |
| 1659 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 1660 | COMSTAT comstat; |
| 1661 | DWORD size; |
| 1662 | DWORD err; |
| 1663 | |
| 1664 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1665 | if (!s->hsend) { |
| 1666 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1667 | goto fail; |
| 1668 | } |
| 1669 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1670 | if (!s->hrecv) { |
| 1671 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1672 | goto fail; |
| 1673 | } |
| 1674 | |
| 1675 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 1676 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 1677 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 1678 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 1679 | s->hcom = NULL; |
| 1680 | goto fail; |
| 1681 | } |
| 1682 | |
| 1683 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 1684 | fprintf(stderr, "Failed SetupComm\n"); |
| 1685 | goto fail; |
| 1686 | } |
| 1687 | |
| 1688 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 1689 | size = sizeof(COMMCONFIG); |
| 1690 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 1691 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 1692 | CommConfigDialog(filename, NULL, &comcfg); |
| 1693 | |
| 1694 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 1695 | fprintf(stderr, "Failed SetCommState\n"); |
| 1696 | goto fail; |
| 1697 | } |
| 1698 | |
| 1699 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 1700 | fprintf(stderr, "Failed SetCommMask\n"); |
| 1701 | goto fail; |
| 1702 | } |
| 1703 | |
| 1704 | cto.ReadIntervalTimeout = MAXDWORD; |
| 1705 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 1706 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 1707 | goto fail; |
| 1708 | } |
| 1709 | |
| 1710 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 1711 | fprintf(stderr, "Failed ClearCommError\n"); |
| 1712 | goto fail; |
| 1713 | } |
| 1714 | qemu_add_polling_cb(win_chr_poll, chr); |
| 1715 | return 0; |
| 1716 | |
| 1717 | fail: |
| 1718 | win_chr_close(chr); |
| 1719 | return -1; |
| 1720 | } |
| 1721 | |
| 1722 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 1723 | { |
| 1724 | WinCharState *s = chr->opaque; |
| 1725 | DWORD len, ret, size, err; |
| 1726 | |
| 1727 | len = len1; |
| 1728 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 1729 | s->osend.hEvent = s->hsend; |
| 1730 | while (len > 0) { |
| 1731 | if (s->hsend) |
| 1732 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 1733 | else |
| 1734 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 1735 | if (!ret) { |
| 1736 | err = GetLastError(); |
| 1737 | if (err == ERROR_IO_PENDING) { |
| 1738 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 1739 | if (ret) { |
| 1740 | buf += size; |
| 1741 | len -= size; |
| 1742 | } else { |
| 1743 | break; |
| 1744 | } |
| 1745 | } else { |
| 1746 | break; |
| 1747 | } |
| 1748 | } else { |
| 1749 | buf += size; |
| 1750 | len -= size; |
| 1751 | } |
| 1752 | } |
| 1753 | return len1 - len; |
| 1754 | } |
| 1755 | |
| 1756 | static int win_chr_read_poll(CharDriverState *chr) |
| 1757 | { |
| 1758 | WinCharState *s = chr->opaque; |
| 1759 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 1760 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1761 | return s->max_size; |
| 1762 | } |
| 1763 | |
| 1764 | static void win_chr_readfile(CharDriverState *chr) |
| 1765 | { |
| 1766 | WinCharState *s = chr->opaque; |
| 1767 | int ret, err; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 1768 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1769 | DWORD size; |
| 1770 | |
| 1771 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 1772 | s->orecv.hEvent = s->hrecv; |
| 1773 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 1774 | if (!ret) { |
| 1775 | err = GetLastError(); |
| 1776 | if (err == ERROR_IO_PENDING) { |
| 1777 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | if (size > 0) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1782 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | static void win_chr_read(CharDriverState *chr) |
| 1787 | { |
| 1788 | WinCharState *s = chr->opaque; |
| 1789 | |
| 1790 | if (s->len > s->max_size) |
| 1791 | s->len = s->max_size; |
| 1792 | if (s->len == 0) |
| 1793 | return; |
| 1794 | |
| 1795 | win_chr_readfile(chr); |
| 1796 | } |
| 1797 | |
| 1798 | static int win_chr_poll(void *opaque) |
| 1799 | { |
| 1800 | CharDriverState *chr = opaque; |
| 1801 | WinCharState *s = chr->opaque; |
| 1802 | COMSTAT status; |
| 1803 | DWORD comerr; |
| 1804 | |
| 1805 | ClearCommError(s->hcom, &comerr, &status); |
| 1806 | if (status.cbInQue > 0) { |
| 1807 | s->len = status.cbInQue; |
| 1808 | win_chr_read_poll(chr); |
| 1809 | win_chr_read(chr); |
| 1810 | return 1; |
| 1811 | } |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 1815 | static CharDriverState *qemu_chr_open_win_path(const char *filename) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1816 | { |
| 1817 | CharDriverState *chr; |
| 1818 | WinCharState *s; |
| 1819 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1820 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1821 | s = g_malloc0(sizeof(WinCharState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1822 | chr->opaque = s; |
| 1823 | chr->chr_write = win_chr_write; |
| 1824 | chr->chr_close = win_chr_close; |
| 1825 | |
| 1826 | if (win_chr_init(chr, filename) < 0) { |
Stefan Weil | 2e02e18 | 2011-10-07 07:38:46 +0200 | [diff] [blame] | 1827 | g_free(s); |
| 1828 | g_free(chr); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1829 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1830 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1831 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | static int win_chr_pipe_poll(void *opaque) |
| 1835 | { |
| 1836 | CharDriverState *chr = opaque; |
| 1837 | WinCharState *s = chr->opaque; |
| 1838 | DWORD size; |
| 1839 | |
| 1840 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 1841 | if (size > 0) { |
| 1842 | s->len = size; |
| 1843 | win_chr_read_poll(chr); |
| 1844 | win_chr_read(chr); |
| 1845 | return 1; |
| 1846 | } |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
| 1851 | { |
| 1852 | WinCharState *s = chr->opaque; |
| 1853 | OVERLAPPED ov; |
| 1854 | int ret; |
| 1855 | DWORD size; |
| 1856 | char openname[256]; |
| 1857 | |
| 1858 | s->fpipe = TRUE; |
| 1859 | |
| 1860 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1861 | if (!s->hsend) { |
| 1862 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1863 | goto fail; |
| 1864 | } |
| 1865 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1866 | if (!s->hrecv) { |
| 1867 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1868 | goto fail; |
| 1869 | } |
| 1870 | |
| 1871 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 1872 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 1873 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 1874 | PIPE_WAIT, |
| 1875 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 1876 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 1877 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 1878 | s->hcom = NULL; |
| 1879 | goto fail; |
| 1880 | } |
| 1881 | |
| 1882 | ZeroMemory(&ov, sizeof(ov)); |
| 1883 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1884 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 1885 | if (ret) { |
| 1886 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 1887 | goto fail; |
| 1888 | } |
| 1889 | |
| 1890 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 1891 | if (!ret) { |
| 1892 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 1893 | if (ov.hEvent) { |
| 1894 | CloseHandle(ov.hEvent); |
| 1895 | ov.hEvent = NULL; |
| 1896 | } |
| 1897 | goto fail; |
| 1898 | } |
| 1899 | |
| 1900 | if (ov.hEvent) { |
| 1901 | CloseHandle(ov.hEvent); |
| 1902 | ov.hEvent = NULL; |
| 1903 | } |
| 1904 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
| 1905 | return 0; |
| 1906 | |
| 1907 | fail: |
| 1908 | win_chr_close(chr); |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
| 1912 | |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 1913 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1914 | { |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 1915 | const char *filename = opts->device; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1916 | CharDriverState *chr; |
| 1917 | WinCharState *s; |
| 1918 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1919 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1920 | s = g_malloc0(sizeof(WinCharState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1921 | chr->opaque = s; |
| 1922 | chr->chr_write = win_chr_write; |
| 1923 | chr->chr_close = win_chr_close; |
| 1924 | |
| 1925 | if (win_chr_pipe_init(chr, filename) < 0) { |
Stefan Weil | 2e02e18 | 2011-10-07 07:38:46 +0200 | [diff] [blame] | 1926 | g_free(s); |
| 1927 | g_free(chr); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1928 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1929 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1930 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1933 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1934 | { |
| 1935 | CharDriverState *chr; |
| 1936 | WinCharState *s; |
| 1937 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1938 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1939 | s = g_malloc0(sizeof(WinCharState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1940 | s->hcom = fd_out; |
| 1941 | chr->opaque = s; |
| 1942 | chr->chr_write = win_chr_write; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1943 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Gerd Hoffmann | d9ac374 | 2013-02-25 11:48:06 +0100 | [diff] [blame] | 1946 | static CharDriverState *qemu_chr_open_win_con(void) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1947 | { |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1948 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1951 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1952 | { |
| 1953 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 1954 | DWORD dwSize; |
| 1955 | int len1; |
| 1956 | |
| 1957 | len1 = len; |
| 1958 | |
| 1959 | while (len1 > 0) { |
| 1960 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { |
| 1961 | break; |
| 1962 | } |
| 1963 | buf += dwSize; |
| 1964 | len1 -= dwSize; |
| 1965 | } |
| 1966 | |
| 1967 | return len - len1; |
| 1968 | } |
| 1969 | |
| 1970 | static void win_stdio_wait_func(void *opaque) |
| 1971 | { |
| 1972 | CharDriverState *chr = opaque; |
| 1973 | WinStdioCharState *stdio = chr->opaque; |
| 1974 | INPUT_RECORD buf[4]; |
| 1975 | int ret; |
| 1976 | DWORD dwSize; |
| 1977 | int i; |
| 1978 | |
Stefan Weil | dff7424 | 2013-12-07 14:48:04 +0100 | [diff] [blame] | 1979 | ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1980 | |
| 1981 | if (!ret) { |
| 1982 | /* Avoid error storm */ |
| 1983 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); |
| 1984 | return; |
| 1985 | } |
| 1986 | |
| 1987 | for (i = 0; i < dwSize; i++) { |
| 1988 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; |
| 1989 | |
| 1990 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { |
| 1991 | int j; |
| 1992 | if (kev->uChar.AsciiChar != 0) { |
| 1993 | for (j = 0; j < kev->wRepeatCount; j++) { |
| 1994 | if (qemu_chr_be_can_write(chr)) { |
| 1995 | uint8_t c = kev->uChar.AsciiChar; |
| 1996 | qemu_chr_be_write(chr, &c, 1); |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | static DWORD WINAPI win_stdio_thread(LPVOID param) |
| 2005 | { |
| 2006 | CharDriverState *chr = param; |
| 2007 | WinStdioCharState *stdio = chr->opaque; |
| 2008 | int ret; |
| 2009 | DWORD dwSize; |
| 2010 | |
| 2011 | while (1) { |
| 2012 | |
| 2013 | /* Wait for one byte */ |
| 2014 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); |
| 2015 | |
| 2016 | /* Exit in case of error, continue if nothing read */ |
| 2017 | if (!ret) { |
| 2018 | break; |
| 2019 | } |
| 2020 | if (!dwSize) { |
| 2021 | continue; |
| 2022 | } |
| 2023 | |
| 2024 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ |
| 2025 | if (stdio->win_stdio_buf == '\r') { |
| 2026 | continue; |
| 2027 | } |
| 2028 | |
| 2029 | /* Signal the main thread and wait until the byte was eaten */ |
| 2030 | if (!SetEvent(stdio->hInputReadyEvent)) { |
| 2031 | break; |
| 2032 | } |
| 2033 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) |
| 2034 | != WAIT_OBJECT_0) { |
| 2035 | break; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); |
| 2040 | return 0; |
| 2041 | } |
| 2042 | |
| 2043 | static void win_stdio_thread_wait_func(void *opaque) |
| 2044 | { |
| 2045 | CharDriverState *chr = opaque; |
| 2046 | WinStdioCharState *stdio = chr->opaque; |
| 2047 | |
| 2048 | if (qemu_chr_be_can_write(chr)) { |
| 2049 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); |
| 2050 | } |
| 2051 | |
| 2052 | SetEvent(stdio->hInputDoneEvent); |
| 2053 | } |
| 2054 | |
| 2055 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) |
| 2056 | { |
| 2057 | WinStdioCharState *stdio = chr->opaque; |
| 2058 | DWORD dwMode = 0; |
| 2059 | |
| 2060 | GetConsoleMode(stdio->hStdIn, &dwMode); |
| 2061 | |
| 2062 | if (echo) { |
| 2063 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); |
| 2064 | } else { |
| 2065 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | static void win_stdio_close(CharDriverState *chr) |
| 2070 | { |
| 2071 | WinStdioCharState *stdio = chr->opaque; |
| 2072 | |
| 2073 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { |
| 2074 | CloseHandle(stdio->hInputReadyEvent); |
| 2075 | } |
| 2076 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { |
| 2077 | CloseHandle(stdio->hInputDoneEvent); |
| 2078 | } |
| 2079 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { |
| 2080 | TerminateThread(stdio->hInputThread, 0); |
| 2081 | } |
| 2082 | |
| 2083 | g_free(chr->opaque); |
| 2084 | g_free(chr); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2085 | } |
| 2086 | |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 2087 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2088 | { |
| 2089 | CharDriverState *chr; |
| 2090 | WinStdioCharState *stdio; |
| 2091 | DWORD dwMode; |
| 2092 | int is_console = 0; |
| 2093 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2094 | chr = g_malloc0(sizeof(CharDriverState)); |
| 2095 | stdio = g_malloc0(sizeof(WinStdioCharState)); |
| 2096 | |
| 2097 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); |
| 2098 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { |
| 2099 | fprintf(stderr, "cannot open stdio: invalid handle\n"); |
| 2100 | exit(1); |
| 2101 | } |
| 2102 | |
| 2103 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; |
| 2104 | |
| 2105 | chr->opaque = stdio; |
| 2106 | chr->chr_write = win_stdio_write; |
| 2107 | chr->chr_close = win_stdio_close; |
| 2108 | |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2109 | if (is_console) { |
| 2110 | if (qemu_add_wait_object(stdio->hStdIn, |
| 2111 | win_stdio_wait_func, chr)) { |
| 2112 | fprintf(stderr, "qemu_add_wait_object: failed\n"); |
| 2113 | } |
| 2114 | } else { |
| 2115 | DWORD dwId; |
| 2116 | |
| 2117 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 2118 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 2119 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, |
| 2120 | chr, 0, &dwId); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2121 | |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2122 | if (stdio->hInputThread == INVALID_HANDLE_VALUE |
| 2123 | || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE |
| 2124 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { |
| 2125 | fprintf(stderr, "cannot create stdio thread or event\n"); |
| 2126 | exit(1); |
| 2127 | } |
| 2128 | if (qemu_add_wait_object(stdio->hInputReadyEvent, |
| 2129 | win_stdio_thread_wait_func, chr)) { |
| 2130 | fprintf(stderr, "qemu_add_wait_object: failed\n"); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | dwMode |= ENABLE_LINE_INPUT; |
| 2135 | |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2136 | if (is_console) { |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2137 | /* set the terminal in raw mode */ |
| 2138 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ |
| 2139 | dwMode |= ENABLE_PROCESSED_INPUT; |
| 2140 | } |
| 2141 | |
| 2142 | SetConsoleMode(stdio->hStdIn, dwMode); |
| 2143 | |
| 2144 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; |
| 2145 | qemu_chr_fe_set_echo(chr, false); |
| 2146 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2147 | return chr; |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2148 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2149 | #endif /* !_WIN32 */ |
| 2150 | |
Anthony Liguori | 5ab8211 | 2013-03-05 23:21:31 +0530 | [diff] [blame] | 2151 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2152 | /***********************************************************/ |
| 2153 | /* UDP Net console */ |
| 2154 | |
| 2155 | typedef struct { |
| 2156 | int fd; |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2157 | GIOChannel *chan; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2158 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2159 | int bufcnt; |
| 2160 | int bufptr; |
| 2161 | int max_size; |
| 2162 | } NetCharDriver; |
| 2163 | |
| 2164 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2165 | { |
| 2166 | NetCharDriver *s = chr->opaque; |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2167 | gsize bytes_written; |
| 2168 | GIOStatus status; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2169 | |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2170 | status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL); |
| 2171 | if (status == G_IO_STATUS_EOF) { |
| 2172 | return 0; |
| 2173 | } else if (status != G_IO_STATUS_NORMAL) { |
| 2174 | return -1; |
| 2175 | } |
| 2176 | |
| 2177 | return bytes_written; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | static int udp_chr_read_poll(void *opaque) |
| 2181 | { |
| 2182 | CharDriverState *chr = opaque; |
| 2183 | NetCharDriver *s = chr->opaque; |
| 2184 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2185 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2186 | |
| 2187 | /* If there were any stray characters in the queue process them |
| 2188 | * first |
| 2189 | */ |
| 2190 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2191 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2192 | s->bufptr++; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2193 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2194 | } |
| 2195 | return s->max_size; |
| 2196 | } |
| 2197 | |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2198 | static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2199 | { |
| 2200 | CharDriverState *chr = opaque; |
| 2201 | NetCharDriver *s = chr->opaque; |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2202 | gsize bytes_read = 0; |
| 2203 | GIOStatus status; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2204 | |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 2205 | if (s->max_size == 0) { |
| 2206 | return TRUE; |
| 2207 | } |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2208 | status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf), |
| 2209 | &bytes_read, NULL); |
| 2210 | s->bufcnt = bytes_read; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2211 | s->bufptr = s->bufcnt; |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2212 | if (status != G_IO_STATUS_NORMAL) { |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2213 | remove_fd_in_watch(chr); |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2214 | return FALSE; |
| 2215 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2216 | |
| 2217 | s->bufptr = 0; |
| 2218 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2219 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2220 | s->bufptr++; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2221 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2222 | } |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2223 | |
| 2224 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | static void udp_chr_update_read_handler(CharDriverState *chr) |
| 2228 | { |
| 2229 | NetCharDriver *s = chr->opaque; |
| 2230 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2231 | remove_fd_in_watch(chr); |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2232 | if (s->chan) { |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 2233 | chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll, |
| 2234 | udp_chr_read, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2238 | static void udp_chr_close(CharDriverState *chr) |
| 2239 | { |
| 2240 | NetCharDriver *s = chr->opaque; |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2241 | |
| 2242 | remove_fd_in_watch(chr); |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2243 | if (s->chan) { |
| 2244 | g_io_channel_unref(s->chan); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2245 | closesocket(s->fd); |
| 2246 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2247 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2248 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 2251 | static CharDriverState *qemu_chr_open_udp_fd(int fd) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2252 | { |
| 2253 | CharDriverState *chr = NULL; |
| 2254 | NetCharDriver *s = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2255 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2256 | chr = g_malloc0(sizeof(CharDriverState)); |
| 2257 | s = g_malloc0(sizeof(NetCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2258 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2259 | s->fd = fd; |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2260 | s->chan = io_channel_from_socket(s->fd); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2261 | s->bufcnt = 0; |
| 2262 | s->bufptr = 0; |
| 2263 | chr->opaque = s; |
| 2264 | chr->chr_write = udp_chr_write; |
| 2265 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2266 | chr->chr_close = udp_chr_close; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 2267 | /* be isn't opened until we get a connection */ |
| 2268 | chr->explicit_be_open = true; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2269 | return chr; |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 2270 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2271 | |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 2272 | static CharDriverState *qemu_chr_open_udp(QemuOpts *opts) |
| 2273 | { |
| 2274 | Error *local_err = NULL; |
| 2275 | int fd = -1; |
| 2276 | |
| 2277 | fd = inet_dgram_opts(opts, &local_err); |
| 2278 | if (fd < 0) { |
Gerd Hoffmann | 58a3714 | 2013-06-24 08:39:55 +0200 | [diff] [blame] | 2279 | qerror_report_err(local_err); |
| 2280 | error_free(local_err); |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 2281 | return NULL; |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2282 | } |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 2283 | return qemu_chr_open_udp_fd(fd); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /***********************************************************/ |
| 2287 | /* TCP Net console */ |
| 2288 | |
| 2289 | typedef struct { |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2290 | |
| 2291 | GIOChannel *chan, *listen_chan; |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 2292 | guint listen_tag; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2293 | int fd, listen_fd; |
| 2294 | int connected; |
| 2295 | int max_size; |
| 2296 | int do_telnetopt; |
| 2297 | int do_nodelay; |
| 2298 | int is_unix; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2299 | int msgfd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2300 | } TCPCharDriver; |
| 2301 | |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2302 | static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2303 | |
| 2304 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2305 | { |
| 2306 | TCPCharDriver *s = chr->opaque; |
| 2307 | if (s->connected) { |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 2308 | return io_channel_send(s->chan, buf, len); |
Anthony Liguori | 455aa1e | 2012-09-05 13:52:49 -0500 | [diff] [blame] | 2309 | } else { |
Anthony Liguori | 6db0fdc | 2012-09-12 14:34:07 -0500 | [diff] [blame] | 2310 | /* XXX: indicate an error ? */ |
Anthony Liguori | 455aa1e | 2012-09-05 13:52:49 -0500 | [diff] [blame] | 2311 | return len; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | static int tcp_chr_read_poll(void *opaque) |
| 2316 | { |
| 2317 | CharDriverState *chr = opaque; |
| 2318 | TCPCharDriver *s = chr->opaque; |
| 2319 | if (!s->connected) |
| 2320 | return 0; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2321 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2322 | return s->max_size; |
| 2323 | } |
| 2324 | |
| 2325 | #define IAC 255 |
| 2326 | #define IAC_BREAK 243 |
| 2327 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2328 | TCPCharDriver *s, |
| 2329 | uint8_t *buf, int *size) |
| 2330 | { |
| 2331 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2332 | * char mode with no echo. All IAC options will be removed from |
| 2333 | * the buf and the do_telnetopt variable will be used to track the |
| 2334 | * state of the width of the IAC information. |
| 2335 | * |
| 2336 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2337 | * "IAC BREAK" command and the double IAC. |
| 2338 | */ |
| 2339 | |
| 2340 | int i; |
| 2341 | int j = 0; |
| 2342 | |
| 2343 | for (i = 0; i < *size; i++) { |
| 2344 | if (s->do_telnetopt > 1) { |
| 2345 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2346 | /* Double IAC means send an IAC */ |
| 2347 | if (j != i) |
| 2348 | buf[j] = buf[i]; |
| 2349 | j++; |
| 2350 | s->do_telnetopt = 1; |
| 2351 | } else { |
| 2352 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2353 | /* Handle IAC break commands by sending a serial break */ |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2354 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2355 | s->do_telnetopt++; |
| 2356 | } |
| 2357 | s->do_telnetopt++; |
| 2358 | } |
| 2359 | if (s->do_telnetopt >= 4) { |
| 2360 | s->do_telnetopt = 1; |
| 2361 | } |
| 2362 | } else { |
| 2363 | if ((unsigned char)buf[i] == IAC) { |
| 2364 | s->do_telnetopt = 2; |
| 2365 | } else { |
| 2366 | if (j != i) |
| 2367 | buf[j] = buf[i]; |
| 2368 | j++; |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | *size = j; |
| 2373 | } |
| 2374 | |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2375 | static int tcp_get_msgfd(CharDriverState *chr) |
| 2376 | { |
| 2377 | TCPCharDriver *s = chr->opaque; |
Paolo Bonzini | e53f27b | 2010-04-16 17:25:23 +0200 | [diff] [blame] | 2378 | int fd = s->msgfd; |
| 2379 | s->msgfd = -1; |
| 2380 | return fd; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2381 | } |
| 2382 | |
Anthony Liguori | 73bcc2a | 2009-07-27 14:55:25 -0500 | [diff] [blame] | 2383 | #ifndef _WIN32 |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2384 | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
| 2385 | { |
| 2386 | TCPCharDriver *s = chr->opaque; |
| 2387 | struct cmsghdr *cmsg; |
| 2388 | |
| 2389 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 2390 | int fd; |
| 2391 | |
| 2392 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || |
| 2393 | cmsg->cmsg_level != SOL_SOCKET || |
| 2394 | cmsg->cmsg_type != SCM_RIGHTS) |
| 2395 | continue; |
| 2396 | |
| 2397 | fd = *((int *)CMSG_DATA(cmsg)); |
| 2398 | if (fd < 0) |
| 2399 | continue; |
| 2400 | |
Stefan Hajnoczi | 9b938c7 | 2013-03-27 10:10:46 +0100 | [diff] [blame] | 2401 | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ |
| 2402 | qemu_set_block(fd); |
| 2403 | |
Corey Bryant | 0613865 | 2012-08-14 16:43:42 -0400 | [diff] [blame] | 2404 | #ifndef MSG_CMSG_CLOEXEC |
| 2405 | qemu_set_cloexec(fd); |
| 2406 | #endif |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2407 | if (s->msgfd != -1) |
| 2408 | close(s->msgfd); |
| 2409 | s->msgfd = fd; |
| 2410 | } |
| 2411 | } |
| 2412 | |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2413 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
| 2414 | { |
| 2415 | TCPCharDriver *s = chr->opaque; |
Blue Swirl | 7cba04f | 2009-08-01 10:13:20 +0000 | [diff] [blame] | 2416 | struct msghdr msg = { NULL, }; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2417 | struct iovec iov[1]; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2418 | union { |
| 2419 | struct cmsghdr cmsg; |
| 2420 | char control[CMSG_SPACE(sizeof(int))]; |
| 2421 | } msg_control; |
Corey Bryant | 0613865 | 2012-08-14 16:43:42 -0400 | [diff] [blame] | 2422 | int flags = 0; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2423 | ssize_t ret; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2424 | |
| 2425 | iov[0].iov_base = buf; |
| 2426 | iov[0].iov_len = len; |
| 2427 | |
| 2428 | msg.msg_iov = iov; |
| 2429 | msg.msg_iovlen = 1; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2430 | msg.msg_control = &msg_control; |
| 2431 | msg.msg_controllen = sizeof(msg_control); |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2432 | |
Corey Bryant | 0613865 | 2012-08-14 16:43:42 -0400 | [diff] [blame] | 2433 | #ifdef MSG_CMSG_CLOEXEC |
| 2434 | flags |= MSG_CMSG_CLOEXEC; |
| 2435 | #endif |
| 2436 | ret = recvmsg(s->fd, &msg, flags); |
| 2437 | if (ret > 0 && s->is_unix) { |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2438 | unix_process_msgfd(chr, &msg); |
Corey Bryant | 0613865 | 2012-08-14 16:43:42 -0400 | [diff] [blame] | 2439 | } |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2440 | |
| 2441 | return ret; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2442 | } |
| 2443 | #else |
| 2444 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
| 2445 | { |
| 2446 | TCPCharDriver *s = chr->opaque; |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 2447 | return qemu_recv(s->fd, buf, len, 0); |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2448 | } |
| 2449 | #endif |
| 2450 | |
Amit Shah | d3cc5bc | 2013-03-05 23:21:25 +0530 | [diff] [blame] | 2451 | static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
| 2452 | { |
| 2453 | TCPCharDriver *s = chr->opaque; |
| 2454 | return g_io_create_watch(s->chan, cond); |
| 2455 | } |
| 2456 | |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2457 | static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2458 | { |
| 2459 | CharDriverState *chr = opaque; |
| 2460 | TCPCharDriver *s = chr->opaque; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2461 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2462 | int len, size; |
| 2463 | |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2464 | if (!s->connected || s->max_size <= 0) { |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 2465 | return TRUE; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2466 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2467 | len = sizeof(buf); |
| 2468 | if (len > s->max_size) |
| 2469 | len = s->max_size; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2470 | size = tcp_chr_recv(chr, (void *)buf, len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2471 | if (size == 0) { |
| 2472 | /* connection closed */ |
| 2473 | s->connected = 0; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2474 | if (s->listen_chan) { |
| 2475 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2476 | } |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2477 | remove_fd_in_watch(chr); |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2478 | g_io_channel_unref(s->chan); |
| 2479 | s->chan = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2480 | closesocket(s->fd); |
| 2481 | s->fd = -1; |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2482 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2483 | } else if (size > 0) { |
| 2484 | if (s->do_telnetopt) |
| 2485 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2486 | if (size > 0) |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2487 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2488 | } |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2489 | |
| 2490 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Blue Swirl | 68c18d1 | 2010-08-15 09:46:24 +0000 | [diff] [blame] | 2493 | #ifndef _WIN32 |
| 2494 | CharDriverState *qemu_chr_open_eventfd(int eventfd) |
| 2495 | { |
Cam Macdonell | 6cbf4c8 | 2010-07-27 10:54:13 -0600 | [diff] [blame] | 2496 | return qemu_chr_open_fd(eventfd, eventfd); |
Cam Macdonell | 6cbf4c8 | 2010-07-27 10:54:13 -0600 | [diff] [blame] | 2497 | } |
Blue Swirl | 68c18d1 | 2010-08-15 09:46:24 +0000 | [diff] [blame] | 2498 | #endif |
Cam Macdonell | 6cbf4c8 | 2010-07-27 10:54:13 -0600 | [diff] [blame] | 2499 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2500 | static void tcp_chr_connect(void *opaque) |
| 2501 | { |
| 2502 | CharDriverState *chr = opaque; |
| 2503 | TCPCharDriver *s = chr->opaque; |
| 2504 | |
| 2505 | s->connected = 1; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2506 | if (s->chan) { |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 2507 | chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, |
| 2508 | tcp_chr_read, chr); |
David Gibson | bbdd2ad | 2012-09-10 12:30:56 +1000 | [diff] [blame] | 2509 | } |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 2510 | qemu_chr_be_generic_open(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 2513 | static void tcp_chr_update_read_handler(CharDriverState *chr) |
| 2514 | { |
| 2515 | TCPCharDriver *s = chr->opaque; |
| 2516 | |
| 2517 | remove_fd_in_watch(chr); |
| 2518 | if (s->chan) { |
| 2519 | chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, |
| 2520 | tcp_chr_read, chr); |
| 2521 | } |
| 2522 | } |
| 2523 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2524 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 2525 | static void tcp_chr_telnet_init(int fd) |
| 2526 | { |
| 2527 | char buf[3]; |
| 2528 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 2529 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 2530 | send(fd, (char *)buf, 3, 0); |
| 2531 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 2532 | send(fd, (char *)buf, 3, 0); |
| 2533 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 2534 | send(fd, (char *)buf, 3, 0); |
| 2535 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 2536 | send(fd, (char *)buf, 3, 0); |
| 2537 | } |
| 2538 | |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2539 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
| 2540 | { |
| 2541 | TCPCharDriver *s = chr->opaque; |
| 2542 | if (s->fd != -1) |
| 2543 | return -1; |
| 2544 | |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 2545 | qemu_set_nonblock(fd); |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2546 | if (s->do_nodelay) |
| 2547 | socket_set_nodelay(fd); |
| 2548 | s->fd = fd; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2549 | s->chan = io_channel_from_socket(fd); |
Paolo Bonzini | 910b636 | 2013-04-19 17:32:06 +0200 | [diff] [blame] | 2550 | if (s->listen_tag) { |
| 2551 | g_source_remove(s->listen_tag); |
| 2552 | s->listen_tag = 0; |
| 2553 | } |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2554 | tcp_chr_connect(chr); |
| 2555 | |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2559 | static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2560 | { |
| 2561 | CharDriverState *chr = opaque; |
| 2562 | TCPCharDriver *s = chr->opaque; |
| 2563 | struct sockaddr_in saddr; |
| 2564 | #ifndef _WIN32 |
| 2565 | struct sockaddr_un uaddr; |
| 2566 | #endif |
| 2567 | struct sockaddr *addr; |
| 2568 | socklen_t len; |
| 2569 | int fd; |
| 2570 | |
| 2571 | for(;;) { |
| 2572 | #ifndef _WIN32 |
| 2573 | if (s->is_unix) { |
| 2574 | len = sizeof(uaddr); |
| 2575 | addr = (struct sockaddr *)&uaddr; |
| 2576 | } else |
| 2577 | #endif |
| 2578 | { |
| 2579 | len = sizeof(saddr); |
| 2580 | addr = (struct sockaddr *)&saddr; |
| 2581 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 2582 | fd = qemu_accept(s->listen_fd, addr, &len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2583 | if (fd < 0 && errno != EINTR) { |
Hans de Goede | 79f2007 | 2013-04-25 13:53:02 +0200 | [diff] [blame] | 2584 | s->listen_tag = 0; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2585 | return FALSE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2586 | } else if (fd >= 0) { |
| 2587 | if (s->do_telnetopt) |
| 2588 | tcp_chr_telnet_init(fd); |
| 2589 | break; |
| 2590 | } |
| 2591 | } |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2592 | if (tcp_chr_add_client(chr, fd) < 0) |
| 2593 | close(fd); |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2594 | |
| 2595 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | static void tcp_chr_close(CharDriverState *chr) |
| 2599 | { |
| 2600 | TCPCharDriver *s = chr->opaque; |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2601 | if (s->fd >= 0) { |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2602 | remove_fd_in_watch(chr); |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2603 | if (s->chan) { |
| 2604 | g_io_channel_unref(s->chan); |
| 2605 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2606 | closesocket(s->fd); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2607 | } |
| 2608 | if (s->listen_fd >= 0) { |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2609 | if (s->listen_tag) { |
| 2610 | g_source_remove(s->listen_tag); |
Paolo Bonzini | 910b636 | 2013-04-19 17:32:06 +0200 | [diff] [blame] | 2611 | s->listen_tag = 0; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2612 | } |
| 2613 | if (s->listen_chan) { |
| 2614 | g_io_channel_unref(s->listen_chan); |
| 2615 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2616 | closesocket(s->listen_fd); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2617 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2618 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2619 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2622 | static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay, |
| 2623 | bool is_listen, bool is_telnet, |
| 2624 | bool is_waitconnect, |
| 2625 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2626 | { |
| 2627 | CharDriverState *chr = NULL; |
| 2628 | TCPCharDriver *s = NULL; |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2629 | char host[NI_MAXHOST], serv[NI_MAXSERV]; |
| 2630 | const char *left = "", *right = ""; |
| 2631 | struct sockaddr_storage ss; |
| 2632 | socklen_t ss_len = sizeof(ss); |
| 2633 | |
| 2634 | memset(&ss, 0, ss_len); |
| 2635 | if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) { |
Gerd Hoffmann | 20c3976 | 2013-06-24 08:39:48 +0200 | [diff] [blame] | 2636 | error_setg_errno(errp, errno, "getsockname"); |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2637 | return NULL; |
| 2638 | } |
| 2639 | |
| 2640 | chr = g_malloc0(sizeof(CharDriverState)); |
| 2641 | s = g_malloc0(sizeof(TCPCharDriver)); |
| 2642 | |
| 2643 | s->connected = 0; |
| 2644 | s->fd = -1; |
| 2645 | s->listen_fd = -1; |
| 2646 | s->msgfd = -1; |
| 2647 | |
| 2648 | chr->filename = g_malloc(256); |
| 2649 | switch (ss.ss_family) { |
| 2650 | #ifndef _WIN32 |
| 2651 | case AF_UNIX: |
| 2652 | s->is_unix = 1; |
| 2653 | snprintf(chr->filename, 256, "unix:%s%s", |
| 2654 | ((struct sockaddr_un *)(&ss))->sun_path, |
| 2655 | is_listen ? ",server" : ""); |
| 2656 | break; |
| 2657 | #endif |
| 2658 | case AF_INET6: |
| 2659 | left = "["; |
| 2660 | right = "]"; |
| 2661 | /* fall through */ |
| 2662 | case AF_INET: |
| 2663 | s->do_nodelay = do_nodelay; |
| 2664 | getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host), |
| 2665 | serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV); |
Igor Mitsyanko | e554585 | 2013-03-10 17:58:05 +0400 | [diff] [blame] | 2666 | snprintf(chr->filename, 256, "%s:%s%s%s:%s%s", |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2667 | is_telnet ? "telnet" : "tcp", |
| 2668 | left, host, right, serv, |
| 2669 | is_listen ? ",server" : ""); |
| 2670 | break; |
| 2671 | } |
| 2672 | |
| 2673 | chr->opaque = s; |
| 2674 | chr->chr_write = tcp_chr_write; |
| 2675 | chr->chr_close = tcp_chr_close; |
| 2676 | chr->get_msgfd = tcp_get_msgfd; |
| 2677 | chr->chr_add_client = tcp_chr_add_client; |
Amit Shah | d3cc5bc | 2013-03-05 23:21:25 +0530 | [diff] [blame] | 2678 | chr->chr_add_watch = tcp_chr_add_watch; |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 2679 | chr->chr_update_read_handler = tcp_chr_update_read_handler; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 2680 | /* be isn't opened until we get a connection */ |
| 2681 | chr->explicit_be_open = true; |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2682 | |
| 2683 | if (is_listen) { |
| 2684 | s->listen_fd = fd; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2685 | s->listen_chan = io_channel_from_socket(s->listen_fd); |
| 2686 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr); |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2687 | if (is_telnet) { |
| 2688 | s->do_telnetopt = 1; |
| 2689 | } |
| 2690 | } else { |
| 2691 | s->connected = 1; |
| 2692 | s->fd = fd; |
| 2693 | socket_set_nodelay(fd); |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2694 | s->chan = io_channel_from_socket(s->fd); |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2695 | tcp_chr_connect(chr); |
| 2696 | } |
| 2697 | |
| 2698 | if (is_listen && is_waitconnect) { |
Gerd Hoffmann | fdca212 | 2013-06-24 08:39:49 +0200 | [diff] [blame] | 2699 | fprintf(stderr, "QEMU waiting for connection on: %s\n", |
| 2700 | chr->filename); |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2701 | tcp_chr_accept(s->listen_chan, G_IO_IN, chr); |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 2702 | qemu_set_nonblock(s->listen_fd); |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2703 | } |
| 2704 | return chr; |
| 2705 | } |
| 2706 | |
| 2707 | static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) |
| 2708 | { |
| 2709 | CharDriverState *chr = NULL; |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2710 | Error *local_err = NULL; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2711 | int fd = -1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2712 | |
liguang | e990a39 | 2013-06-18 11:45:35 +0800 | [diff] [blame] | 2713 | bool is_listen = qemu_opt_get_bool(opts, "server", false); |
| 2714 | bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true); |
| 2715 | bool is_telnet = qemu_opt_get_bool(opts, "telnet", false); |
| 2716 | bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true); |
| 2717 | bool is_unix = qemu_opt_get(opts, "path") != NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2718 | |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2719 | if (is_unix) { |
| 2720 | if (is_listen) { |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2721 | fd = unix_listen_opts(opts, &local_err); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2722 | } else { |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2723 | fd = unix_connect_opts(opts, &local_err, NULL, NULL); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2724 | } |
| 2725 | } else { |
| 2726 | if (is_listen) { |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2727 | fd = inet_listen_opts(opts, 0, &local_err); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2728 | } else { |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2729 | fd = inet_connect_opts(opts, &local_err, NULL, NULL); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2730 | } |
| 2731 | } |
Markus Armbruster | a89dd6c | 2012-02-07 15:09:10 +0100 | [diff] [blame] | 2732 | if (fd < 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2733 | goto fail; |
Markus Armbruster | a89dd6c | 2012-02-07 15:09:10 +0100 | [diff] [blame] | 2734 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2735 | |
| 2736 | if (!is_waitconnect) |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 2737 | qemu_set_nonblock(fd); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2738 | |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2739 | chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet, |
| 2740 | is_waitconnect, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2741 | if (local_err) { |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2742 | goto fail; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2743 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2744 | return chr; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2745 | |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2746 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2747 | fail: |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2748 | if (local_err) { |
| 2749 | qerror_report_err(local_err); |
| 2750 | error_free(local_err); |
| 2751 | } |
| 2752 | if (fd >= 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2753 | closesocket(fd); |
Paolo Bonzini | 87d5f24 | 2012-10-02 09:16:49 +0200 | [diff] [blame] | 2754 | } |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 2755 | if (chr) { |
| 2756 | g_free(chr->opaque); |
| 2757 | g_free(chr); |
| 2758 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2759 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2762 | /*********************************************************/ |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2763 | /* Ring buffer chardev */ |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2764 | |
| 2765 | typedef struct { |
| 2766 | size_t size; |
| 2767 | size_t prod; |
| 2768 | size_t cons; |
| 2769 | uint8_t *cbuf; |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2770 | } RingBufCharDriver; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2771 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2772 | static size_t ringbuf_count(const CharDriverState *chr) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2773 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2774 | const RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2775 | |
Markus Armbruster | 5c23010 | 2013-02-06 21:27:23 +0100 | [diff] [blame] | 2776 | return d->prod - d->cons; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2777 | } |
| 2778 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2779 | static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2780 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2781 | RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2782 | int i; |
| 2783 | |
| 2784 | if (!buf || (len < 0)) { |
| 2785 | return -1; |
| 2786 | } |
| 2787 | |
| 2788 | for (i = 0; i < len; i++ ) { |
Markus Armbruster | 5c23010 | 2013-02-06 21:27:23 +0100 | [diff] [blame] | 2789 | d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; |
| 2790 | if (d->prod - d->cons > d->size) { |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2791 | d->cons = d->prod - d->size; |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2798 | static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2799 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2800 | RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2801 | int i; |
| 2802 | |
Markus Armbruster | 5c23010 | 2013-02-06 21:27:23 +0100 | [diff] [blame] | 2803 | for (i = 0; i < len && d->cons != d->prod; i++) { |
| 2804 | buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | return i; |
| 2808 | } |
| 2809 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2810 | static void ringbuf_chr_close(struct CharDriverState *chr) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2811 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2812 | RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2813 | |
| 2814 | g_free(d->cbuf); |
| 2815 | g_free(d); |
| 2816 | chr->opaque = NULL; |
| 2817 | } |
| 2818 | |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 2819 | static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts, |
| 2820 | Error **errp) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2821 | { |
| 2822 | CharDriverState *chr; |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2823 | RingBufCharDriver *d; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2824 | |
| 2825 | chr = g_malloc0(sizeof(CharDriverState)); |
| 2826 | d = g_malloc(sizeof(*d)); |
| 2827 | |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 2828 | d->size = opts->has_size ? opts->size : 65536; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2829 | |
| 2830 | /* The size must be power of 2 */ |
| 2831 | if (d->size & (d->size - 1)) { |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 2832 | error_setg(errp, "size of ringbuf chardev must be power of two"); |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2833 | goto fail; |
| 2834 | } |
| 2835 | |
| 2836 | d->prod = 0; |
| 2837 | d->cons = 0; |
| 2838 | d->cbuf = g_malloc0(d->size); |
| 2839 | |
| 2840 | chr->opaque = d; |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2841 | chr->chr_write = ringbuf_chr_write; |
| 2842 | chr->chr_close = ringbuf_chr_close; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 2843 | |
| 2844 | return chr; |
| 2845 | |
| 2846 | fail: |
| 2847 | g_free(d); |
| 2848 | g_free(chr); |
| 2849 | return NULL; |
| 2850 | } |
| 2851 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2852 | static bool chr_is_ringbuf(const CharDriverState *chr) |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2853 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2854 | return chr->chr_write == ringbuf_chr_write; |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2857 | void qmp_ringbuf_write(const char *device, const char *data, |
Markus Armbruster | 82e59a6 | 2013-02-06 21:27:14 +0100 | [diff] [blame] | 2858 | bool has_format, enum DataFormat format, |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2859 | Error **errp) |
| 2860 | { |
| 2861 | CharDriverState *chr; |
Markus Armbruster | c4f331b | 2013-02-06 21:27:17 +0100 | [diff] [blame] | 2862 | const uint8_t *write_data; |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2863 | int ret; |
Peter Crosthwaite | 3d1bba2 | 2013-05-22 13:01:43 +1000 | [diff] [blame] | 2864 | gsize write_count; |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2865 | |
| 2866 | chr = qemu_chr_find(device); |
| 2867 | if (!chr) { |
Markus Armbruster | 1a69278 | 2013-02-06 21:27:16 +0100 | [diff] [blame] | 2868 | error_setg(errp, "Device '%s' not found", device); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2869 | return; |
| 2870 | } |
| 2871 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2872 | if (!chr_is_ringbuf(chr)) { |
| 2873 | error_setg(errp,"%s is not a ringbuf device", device); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2874 | return; |
| 2875 | } |
| 2876 | |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2877 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
| 2878 | write_data = g_base64_decode(data, &write_count); |
| 2879 | } else { |
| 2880 | write_data = (uint8_t *)data; |
Markus Armbruster | 82e59a6 | 2013-02-06 21:27:14 +0100 | [diff] [blame] | 2881 | write_count = strlen(data); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2882 | } |
| 2883 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2884 | ret = ringbuf_chr_write(chr, write_data, write_count); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2885 | |
Markus Armbruster | 13289fb | 2013-02-06 21:27:18 +0100 | [diff] [blame] | 2886 | if (write_data != (uint8_t *)data) { |
| 2887 | g_free((void *)write_data); |
| 2888 | } |
| 2889 | |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 2890 | if (ret < 0) { |
| 2891 | error_setg(errp, "Failed to write to device %s", device); |
| 2892 | return; |
| 2893 | } |
| 2894 | } |
| 2895 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2896 | char *qmp_ringbuf_read(const char *device, int64_t size, |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 2897 | bool has_format, enum DataFormat format, |
| 2898 | Error **errp) |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2899 | { |
| 2900 | CharDriverState *chr; |
Markus Armbruster | c4f331b | 2013-02-06 21:27:17 +0100 | [diff] [blame] | 2901 | uint8_t *read_data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2902 | size_t count; |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 2903 | char *data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2904 | |
| 2905 | chr = qemu_chr_find(device); |
| 2906 | if (!chr) { |
Markus Armbruster | 1a69278 | 2013-02-06 21:27:16 +0100 | [diff] [blame] | 2907 | error_setg(errp, "Device '%s' not found", device); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2908 | return NULL; |
| 2909 | } |
| 2910 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2911 | if (!chr_is_ringbuf(chr)) { |
| 2912 | error_setg(errp,"%s is not a ringbuf device", device); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2913 | return NULL; |
| 2914 | } |
| 2915 | |
| 2916 | if (size <= 0) { |
| 2917 | error_setg(errp, "size must be greater than zero"); |
| 2918 | return NULL; |
| 2919 | } |
| 2920 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2921 | count = ringbuf_count(chr); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2922 | size = size > count ? count : size; |
Markus Armbruster | 44f3bcd | 2013-02-06 21:27:20 +0100 | [diff] [blame] | 2923 | read_data = g_malloc(size + 1); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2924 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2925 | ringbuf_chr_read(chr, read_data, size); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2926 | |
| 2927 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 2928 | data = g_base64_encode(read_data, size); |
Markus Armbruster | 13289fb | 2013-02-06 21:27:18 +0100 | [diff] [blame] | 2929 | g_free(read_data); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2930 | } else { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 2931 | /* |
| 2932 | * FIXME should read only complete, valid UTF-8 characters up |
| 2933 | * to @size bytes. Invalid sequences should be replaced by a |
| 2934 | * suitable replacement character. Except when (and only |
| 2935 | * when) ring buffer lost characters since last read, initial |
| 2936 | * continuation characters should be dropped. |
| 2937 | */ |
Markus Armbruster | 44f3bcd | 2013-02-06 21:27:20 +0100 | [diff] [blame] | 2938 | read_data[size] = 0; |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 2939 | data = (char *)read_data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2940 | } |
| 2941 | |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 2942 | return data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 2943 | } |
| 2944 | |
Gerd Hoffmann | 3352163 | 2009-12-08 13:11:49 +0100 | [diff] [blame] | 2945 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2946 | { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2947 | char host[65], port[33], width[8], height[8]; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2948 | int pos; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 2949 | const char *p; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2950 | QemuOpts *opts; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 2951 | Error *local_err = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2952 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 2953 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2954 | if (local_err) { |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 2955 | qerror_report_err(local_err); |
| 2956 | error_free(local_err); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2957 | return NULL; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 2958 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2959 | |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 2960 | if (strstart(filename, "mon:", &p)) { |
| 2961 | filename = p; |
| 2962 | qemu_opt_set(opts, "mux", "on"); |
Paolo Bonzini | 02c4bdf | 2013-07-03 20:29:45 +0400 | [diff] [blame] | 2963 | if (strcmp(filename, "stdio") == 0) { |
| 2964 | /* Monitor is muxed to stdio: do not exit on Ctrl+C by default |
| 2965 | * but pass it to the guest. Handle this only for compat syntax, |
| 2966 | * for -chardev syntax we have special option for this. |
| 2967 | * This is what -nographic did, redirecting+muxing serial+monitor |
| 2968 | * to stdio causing Ctrl+C to be passed to guest. */ |
| 2969 | qemu_opt_set(opts, "signal", "off"); |
| 2970 | } |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 2971 | } |
| 2972 | |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 2973 | if (strcmp(filename, "null") == 0 || |
| 2974 | strcmp(filename, "pty") == 0 || |
| 2975 | strcmp(filename, "msmouse") == 0 || |
Gerd Hoffmann | dc1c21e | 2009-09-10 10:58:46 +0200 | [diff] [blame] | 2976 | strcmp(filename, "braille") == 0 || |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 2977 | strcmp(filename, "stdio") == 0) { |
Gerd Hoffmann | 4490dad | 2009-09-10 10:58:43 +0200 | [diff] [blame] | 2978 | qemu_opt_set(opts, "backend", filename); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2979 | return opts; |
| 2980 | } |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2981 | if (strstart(filename, "vc", &p)) { |
| 2982 | qemu_opt_set(opts, "backend", "vc"); |
| 2983 | if (*p == ':') { |
Stefan Weil | 49aa405 | 2013-09-30 23:04:49 +0200 | [diff] [blame] | 2984 | if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2985 | /* pixels */ |
| 2986 | qemu_opt_set(opts, "width", width); |
| 2987 | qemu_opt_set(opts, "height", height); |
Stefan Weil | 49aa405 | 2013-09-30 23:04:49 +0200 | [diff] [blame] | 2988 | } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2989 | /* chars */ |
| 2990 | qemu_opt_set(opts, "cols", width); |
| 2991 | qemu_opt_set(opts, "rows", height); |
| 2992 | } else { |
| 2993 | goto fail; |
| 2994 | } |
| 2995 | } |
| 2996 | return opts; |
| 2997 | } |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 2998 | if (strcmp(filename, "con:") == 0) { |
| 2999 | qemu_opt_set(opts, "backend", "console"); |
| 3000 | return opts; |
| 3001 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3002 | if (strstart(filename, "COM", NULL)) { |
| 3003 | qemu_opt_set(opts, "backend", "serial"); |
| 3004 | qemu_opt_set(opts, "path", filename); |
| 3005 | return opts; |
| 3006 | } |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 3007 | if (strstart(filename, "file:", &p)) { |
| 3008 | qemu_opt_set(opts, "backend", "file"); |
| 3009 | qemu_opt_set(opts, "path", p); |
| 3010 | return opts; |
| 3011 | } |
| 3012 | if (strstart(filename, "pipe:", &p)) { |
| 3013 | qemu_opt_set(opts, "backend", "pipe"); |
| 3014 | qemu_opt_set(opts, "path", p); |
| 3015 | return opts; |
| 3016 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3017 | if (strstart(filename, "tcp:", &p) || |
| 3018 | strstart(filename, "telnet:", &p)) { |
| 3019 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 3020 | host[0] = 0; |
| 3021 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) |
| 3022 | goto fail; |
| 3023 | } |
| 3024 | qemu_opt_set(opts, "backend", "socket"); |
| 3025 | qemu_opt_set(opts, "host", host); |
| 3026 | qemu_opt_set(opts, "port", port); |
| 3027 | if (p[pos] == ',') { |
| 3028 | if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0) |
| 3029 | goto fail; |
| 3030 | } |
| 3031 | if (strstart(filename, "telnet:", &p)) |
| 3032 | qemu_opt_set(opts, "telnet", "on"); |
| 3033 | return opts; |
| 3034 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3035 | if (strstart(filename, "udp:", &p)) { |
| 3036 | qemu_opt_set(opts, "backend", "udp"); |
| 3037 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
| 3038 | host[0] = 0; |
Jan Kiszka | 39324ca | 2010-03-07 11:28:48 +0100 | [diff] [blame] | 3039 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3040 | goto fail; |
| 3041 | } |
| 3042 | } |
| 3043 | qemu_opt_set(opts, "host", host); |
| 3044 | qemu_opt_set(opts, "port", port); |
| 3045 | if (p[pos] == '@') { |
| 3046 | p += pos + 1; |
| 3047 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 3048 | host[0] = 0; |
| 3049 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3050 | goto fail; |
| 3051 | } |
| 3052 | } |
| 3053 | qemu_opt_set(opts, "localaddr", host); |
| 3054 | qemu_opt_set(opts, "localport", port); |
| 3055 | } |
| 3056 | return opts; |
| 3057 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3058 | if (strstart(filename, "unix:", &p)) { |
| 3059 | qemu_opt_set(opts, "backend", "socket"); |
| 3060 | if (qemu_opts_do_parse(opts, p, "path") != 0) |
| 3061 | goto fail; |
| 3062 | return opts; |
| 3063 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3064 | if (strstart(filename, "/dev/parport", NULL) || |
| 3065 | strstart(filename, "/dev/ppi", NULL)) { |
| 3066 | qemu_opt_set(opts, "backend", "parport"); |
| 3067 | qemu_opt_set(opts, "path", filename); |
| 3068 | return opts; |
| 3069 | } |
| 3070 | if (strstart(filename, "/dev/", NULL)) { |
| 3071 | qemu_opt_set(opts, "backend", "tty"); |
| 3072 | qemu_opt_set(opts, "path", filename); |
| 3073 | return opts; |
| 3074 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3075 | |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3076 | fail: |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3077 | qemu_opts_del(opts); |
| 3078 | return NULL; |
| 3079 | } |
| 3080 | |
Gerd Hoffmann | 846e2e4 | 2013-02-21 12:07:14 +0100 | [diff] [blame] | 3081 | static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, |
| 3082 | Error **errp) |
| 3083 | { |
| 3084 | const char *path = qemu_opt_get(opts, "path"); |
| 3085 | |
| 3086 | if (path == NULL) { |
| 3087 | error_setg(errp, "chardev: file: no filename given"); |
| 3088 | return; |
| 3089 | } |
| 3090 | backend->file = g_new0(ChardevFile, 1); |
| 3091 | backend->file->out = g_strdup(path); |
| 3092 | } |
| 3093 | |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 3094 | static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, |
| 3095 | Error **errp) |
| 3096 | { |
| 3097 | backend->stdio = g_new0(ChardevStdio, 1); |
| 3098 | backend->stdio->has_signal = true; |
Paolo Bonzini | 02c4bdf | 2013-07-03 20:29:45 +0400 | [diff] [blame] | 3099 | backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true); |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 3100 | } |
| 3101 | |
Gerd Hoffmann | 0f1cb51 | 2013-02-22 15:48:05 +0100 | [diff] [blame] | 3102 | static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, |
| 3103 | Error **errp) |
| 3104 | { |
| 3105 | const char *device = qemu_opt_get(opts, "path"); |
| 3106 | |
| 3107 | if (device == NULL) { |
| 3108 | error_setg(errp, "chardev: serial/tty: no device path given"); |
| 3109 | return; |
| 3110 | } |
| 3111 | backend->serial = g_new0(ChardevHostdev, 1); |
| 3112 | backend->serial->device = g_strdup(device); |
| 3113 | } |
| 3114 | |
Gerd Hoffmann | dc37509 | 2013-02-22 16:17:01 +0100 | [diff] [blame] | 3115 | static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, |
| 3116 | Error **errp) |
| 3117 | { |
| 3118 | const char *device = qemu_opt_get(opts, "path"); |
| 3119 | |
| 3120 | if (device == NULL) { |
| 3121 | error_setg(errp, "chardev: parallel: no device path given"); |
| 3122 | return; |
| 3123 | } |
| 3124 | backend->parallel = g_new0(ChardevHostdev, 1); |
| 3125 | backend->parallel->device = g_strdup(device); |
| 3126 | } |
| 3127 | |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 3128 | static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, |
| 3129 | Error **errp) |
| 3130 | { |
| 3131 | const char *device = qemu_opt_get(opts, "path"); |
| 3132 | |
| 3133 | if (device == NULL) { |
| 3134 | error_setg(errp, "chardev: pipe: no device path given"); |
| 3135 | return; |
| 3136 | } |
| 3137 | backend->pipe = g_new0(ChardevHostdev, 1); |
| 3138 | backend->pipe->device = g_strdup(device); |
| 3139 | } |
| 3140 | |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 3141 | static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, |
| 3142 | Error **errp) |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3143 | { |
| 3144 | int val; |
| 3145 | |
Markus Armbruster | 3a1da42 | 2013-07-26 16:44:34 +0200 | [diff] [blame] | 3146 | backend->ringbuf = g_new0(ChardevRingbuf, 1); |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3147 | |
Markus Armbruster | 0f95305 | 2013-06-27 16:22:07 +0200 | [diff] [blame] | 3148 | val = qemu_opt_get_size(opts, "size", 0); |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3149 | if (val != 0) { |
Markus Armbruster | 3a1da42 | 2013-07-26 16:44:34 +0200 | [diff] [blame] | 3150 | backend->ringbuf->has_size = true; |
| 3151 | backend->ringbuf->size = val; |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3152 | } |
| 3153 | } |
| 3154 | |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 3155 | static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend, |
| 3156 | Error **errp) |
| 3157 | { |
| 3158 | const char *chardev = qemu_opt_get(opts, "chardev"); |
| 3159 | |
| 3160 | if (chardev == NULL) { |
| 3161 | error_setg(errp, "chardev: mux: no chardev given"); |
| 3162 | return; |
| 3163 | } |
| 3164 | backend->mux = g_new0(ChardevMux, 1); |
| 3165 | backend->mux->chardev = g_strdup(chardev); |
| 3166 | } |
| 3167 | |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3168 | typedef struct CharDriver { |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3169 | const char *name; |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3170 | /* old, pre qapi */ |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 3171 | CharDriverState *(*open)(QemuOpts *opts); |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3172 | /* new, qapi-based */ |
Gerd Hoffmann | 99aec01 | 2013-06-24 08:39:52 +0200 | [diff] [blame] | 3173 | ChardevBackendKind kind; |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3174 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3175 | } CharDriver; |
| 3176 | |
| 3177 | static GSList *backends; |
| 3178 | |
| 3179 | void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *)) |
| 3180 | { |
| 3181 | CharDriver *s; |
| 3182 | |
| 3183 | s = g_malloc0(sizeof(*s)); |
| 3184 | s->name = g_strdup(name); |
| 3185 | s->open = open; |
| 3186 | |
| 3187 | backends = g_slist_append(backends, s); |
| 3188 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3189 | |
Gerd Hoffmann | 99aec01 | 2013-06-24 08:39:52 +0200 | [diff] [blame] | 3190 | void register_char_driver_qapi(const char *name, ChardevBackendKind kind, |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3191 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp)) |
| 3192 | { |
| 3193 | CharDriver *s; |
| 3194 | |
| 3195 | s = g_malloc0(sizeof(*s)); |
| 3196 | s->name = g_strdup(name); |
| 3197 | s->kind = kind; |
| 3198 | s->parse = parse; |
| 3199 | |
| 3200 | backends = g_slist_append(backends, s); |
| 3201 | } |
| 3202 | |
Anthony Liguori | f69554b | 2011-08-15 11:17:37 -0500 | [diff] [blame] | 3203 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3204 | void (*init)(struct CharDriverState *s), |
| 3205 | Error **errp) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3206 | { |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3207 | CharDriver *cd; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3208 | CharDriverState *chr; |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3209 | GSList *i; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3210 | |
| 3211 | if (qemu_opts_id(opts) == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3212 | error_setg(errp, "chardev: no id specified"); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3213 | goto err; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3214 | } |
| 3215 | |
Stefan Hajnoczi | 1bbd185 | 2011-01-22 13:07:26 +0000 | [diff] [blame] | 3216 | if (qemu_opt_get(opts, "backend") == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3217 | error_setg(errp, "chardev: \"%s\" missing backend", |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3218 | qemu_opts_id(opts)); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3219 | goto err; |
Stefan Hajnoczi | 1bbd185 | 2011-01-22 13:07:26 +0000 | [diff] [blame] | 3220 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3221 | for (i = backends; i; i = i->next) { |
| 3222 | cd = i->data; |
| 3223 | |
| 3224 | if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) { |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3225 | break; |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3226 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3227 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3228 | if (i == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3229 | error_setg(errp, "chardev: backend \"%s\" not found", |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3230 | qemu_opt_get(opts, "backend")); |
Gerd Hoffmann | e668287 | 2013-06-24 08:39:51 +0200 | [diff] [blame] | 3231 | goto err; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3232 | } |
| 3233 | |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3234 | if (!cd->open) { |
| 3235 | /* using new, qapi init */ |
| 3236 | ChardevBackend *backend = g_new0(ChardevBackend, 1); |
| 3237 | ChardevReturn *ret = NULL; |
| 3238 | const char *id = qemu_opts_id(opts); |
Gerd Hoffmann | dc2c4ec | 2013-06-24 08:39:53 +0200 | [diff] [blame] | 3239 | char *bid = NULL; |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3240 | |
| 3241 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
| 3242 | bid = g_strdup_printf("%s-base", id); |
| 3243 | } |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3244 | |
| 3245 | chr = NULL; |
| 3246 | backend->kind = cd->kind; |
| 3247 | if (cd->parse) { |
| 3248 | cd->parse(opts, backend, errp); |
| 3249 | if (error_is_set(errp)) { |
| 3250 | goto qapi_out; |
| 3251 | } |
| 3252 | } |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3253 | ret = qmp_chardev_add(bid ? bid : id, backend, errp); |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3254 | if (error_is_set(errp)) { |
| 3255 | goto qapi_out; |
| 3256 | } |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3257 | |
| 3258 | if (bid) { |
| 3259 | qapi_free_ChardevBackend(backend); |
| 3260 | qapi_free_ChardevReturn(ret); |
| 3261 | backend = g_new0(ChardevBackend, 1); |
| 3262 | backend->mux = g_new0(ChardevMux, 1); |
| 3263 | backend->kind = CHARDEV_BACKEND_KIND_MUX; |
| 3264 | backend->mux->chardev = g_strdup(bid); |
| 3265 | ret = qmp_chardev_add(id, backend, errp); |
Gerd Hoffmann | ee6ee83 | 2013-09-13 12:48:47 +0200 | [diff] [blame] | 3266 | if (error_is_set(errp)) { |
| 3267 | chr = qemu_chr_find(bid); |
| 3268 | qemu_chr_delete(chr); |
| 3269 | chr = NULL; |
| 3270 | goto qapi_out; |
| 3271 | } |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3272 | } |
| 3273 | |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3274 | chr = qemu_chr_find(id); |
Markus Armbruster | 2ea3e2c | 2013-06-27 15:25:12 +0200 | [diff] [blame] | 3275 | chr->opts = opts; |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3276 | |
| 3277 | qapi_out: |
| 3278 | qapi_free_ChardevBackend(backend); |
| 3279 | qapi_free_ChardevReturn(ret); |
Gerd Hoffmann | dc2c4ec | 2013-06-24 08:39:53 +0200 | [diff] [blame] | 3280 | g_free(bid); |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3281 | return chr; |
| 3282 | } |
| 3283 | |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3284 | chr = cd->open(opts); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 3285 | if (!chr) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3286 | error_setg(errp, "chardev: opening backend \"%s\" failed", |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3287 | qemu_opt_get(opts, "backend")); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3288 | goto err; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | if (!chr->filename) |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3292 | chr->filename = g_strdup(qemu_opt_get(opts, "backend")); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3293 | chr->init = init; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 3294 | /* if we didn't create the chardev via qmp_chardev_add, we |
| 3295 | * need to send the OPENED event here |
| 3296 | */ |
| 3297 | if (!chr->explicit_be_open) { |
| 3298 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 3299 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3300 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3301 | |
| 3302 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
| 3303 | CharDriverState *base = chr; |
| 3304 | int len = strlen(qemu_opts_id(opts)) + 6; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3305 | base->label = g_malloc(len); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3306 | snprintf(base->label, len, "%s-base", qemu_opts_id(opts)); |
| 3307 | chr = qemu_chr_open_mux(base); |
| 3308 | chr->filename = base->filename; |
Kusanagi Kouichi | d5b2716 | 2011-04-26 19:19:26 +0900 | [diff] [blame] | 3309 | chr->avail_connections = MAX_MUX; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3310 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
Kusanagi Kouichi | d5b2716 | 2011-04-26 19:19:26 +0900 | [diff] [blame] | 3311 | } else { |
| 3312 | chr->avail_connections = 1; |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3313 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3314 | chr->label = g_strdup(qemu_opts_id(opts)); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3315 | chr->opts = opts; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3316 | return chr; |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3317 | |
| 3318 | err: |
| 3319 | qemu_opts_del(opts); |
| 3320 | return NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3321 | } |
| 3322 | |
Anthony Liguori | 27143a4 | 2011-08-15 11:17:36 -0500 | [diff] [blame] | 3323 | CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3324 | { |
| 3325 | const char *p; |
| 3326 | CharDriverState *chr; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3327 | QemuOpts *opts; |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3328 | Error *err = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3329 | |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 3330 | if (strstart(filename, "chardev:", &p)) { |
| 3331 | return qemu_chr_find(p); |
| 3332 | } |
| 3333 | |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3334 | opts = qemu_chr_parse_compat(label, filename); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3335 | if (!opts) |
| 3336 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3337 | |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3338 | chr = qemu_chr_new_from_opts(opts, init, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3339 | if (err) { |
Seiji Aguchi | 4a44d85 | 2013-08-05 15:40:44 -0400 | [diff] [blame] | 3340 | error_report("%s", error_get_pretty(err)); |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3341 | error_free(err); |
| 3342 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3343 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
Hans de Goede | 456d606 | 2013-03-27 20:29:40 +0100 | [diff] [blame] | 3344 | qemu_chr_fe_claim_no_fail(chr); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3345 | monitor_init(chr, MONITOR_USE_READLINE); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3346 | } |
| 3347 | return chr; |
| 3348 | } |
| 3349 | |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 3350 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
Paolo Bonzini | c48855e | 2010-12-23 13:42:48 +0100 | [diff] [blame] | 3351 | { |
| 3352 | if (chr->chr_set_echo) { |
| 3353 | chr->chr_set_echo(chr, echo); |
| 3354 | } |
| 3355 | } |
| 3356 | |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 3357 | void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open) |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 3358 | { |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 3359 | if (chr->fe_open == fe_open) { |
Hans de Goede | c0c4bd2 | 2013-03-26 11:07:55 +0100 | [diff] [blame] | 3360 | return; |
| 3361 | } |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 3362 | chr->fe_open = fe_open; |
Hans de Goede | 574b711 | 2013-03-26 11:07:58 +0100 | [diff] [blame] | 3363 | if (chr->chr_set_fe_open) { |
| 3364 | chr->chr_set_fe_open(chr, fe_open); |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | |
Marc-André Lureau | d61b0c9 | 2013-12-01 22:23:39 +0100 | [diff] [blame] | 3368 | void qemu_chr_fe_event(struct CharDriverState *chr, int event) |
| 3369 | { |
| 3370 | if (chr->chr_fe_event) { |
| 3371 | chr->chr_fe_event(chr, event); |
| 3372 | } |
| 3373 | } |
| 3374 | |
Kevin Wolf | 2c8a594 | 2013-03-19 13:38:09 +0100 | [diff] [blame] | 3375 | int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond, |
| 3376 | GIOFunc func, void *user_data) |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 3377 | { |
| 3378 | GSource *src; |
| 3379 | guint tag; |
| 3380 | |
| 3381 | if (s->chr_add_watch == NULL) { |
| 3382 | return -ENOSYS; |
| 3383 | } |
| 3384 | |
| 3385 | src = s->chr_add_watch(s, cond); |
| 3386 | g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); |
| 3387 | tag = g_source_attach(src, NULL); |
| 3388 | g_source_unref(src); |
| 3389 | |
| 3390 | return tag; |
| 3391 | } |
| 3392 | |
Hans de Goede | 44c473d | 2013-03-27 20:29:39 +0100 | [diff] [blame] | 3393 | int qemu_chr_fe_claim(CharDriverState *s) |
| 3394 | { |
| 3395 | if (s->avail_connections < 1) { |
| 3396 | return -1; |
| 3397 | } |
| 3398 | s->avail_connections--; |
| 3399 | return 0; |
| 3400 | } |
| 3401 | |
| 3402 | void qemu_chr_fe_claim_no_fail(CharDriverState *s) |
| 3403 | { |
| 3404 | if (qemu_chr_fe_claim(s) != 0) { |
| 3405 | fprintf(stderr, "%s: error chardev \"%s\" already used\n", |
| 3406 | __func__, s->label); |
| 3407 | exit(1); |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | void qemu_chr_fe_release(CharDriverState *s) |
| 3412 | { |
| 3413 | s->avail_connections++; |
| 3414 | } |
| 3415 | |
Anthony Liguori | 70f24fb | 2011-08-15 11:17:38 -0500 | [diff] [blame] | 3416 | void qemu_chr_delete(CharDriverState *chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3417 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3418 | QTAILQ_REMOVE(&chardevs, chr, next); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3419 | if (chr->chr_close) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3420 | chr->chr_close(chr); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3421 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3422 | g_free(chr->filename); |
| 3423 | g_free(chr->label); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3424 | if (chr->opts) { |
| 3425 | qemu_opts_del(chr->opts); |
| 3426 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3427 | g_free(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 3430 | ChardevInfoList *qmp_query_chardev(Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3431 | { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 3432 | ChardevInfoList *chr_list = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3433 | CharDriverState *chr; |
| 3434 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3435 | QTAILQ_FOREACH(chr, &chardevs, next) { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 3436 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
| 3437 | info->value = g_malloc0(sizeof(*info->value)); |
| 3438 | info->value->label = g_strdup(chr->label); |
| 3439 | info->value->filename = g_strdup(chr->filename); |
| 3440 | |
| 3441 | info->next = chr_list; |
| 3442 | chr_list = info; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3443 | } |
Luiz Capitulino | 588b383 | 2009-12-10 17:16:08 -0200 | [diff] [blame] | 3444 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 3445 | return chr_list; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3446 | } |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 3447 | |
Martin Kletzander | 77d1c3c | 2014-02-01 12:52:42 +0100 | [diff] [blame] | 3448 | ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) |
| 3449 | { |
| 3450 | ChardevBackendInfoList *backend_list = NULL; |
| 3451 | CharDriver *c = NULL; |
| 3452 | GSList *i = NULL; |
| 3453 | |
| 3454 | for (i = backends; i; i = i->next) { |
| 3455 | ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); |
| 3456 | c = i->data; |
| 3457 | info->value = g_malloc0(sizeof(*info->value)); |
| 3458 | info->value->name = g_strdup(c->name); |
| 3459 | |
| 3460 | info->next = backend_list; |
| 3461 | backend_list = info; |
| 3462 | } |
| 3463 | |
| 3464 | return backend_list; |
| 3465 | } |
| 3466 | |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 3467 | CharDriverState *qemu_chr_find(const char *name) |
| 3468 | { |
| 3469 | CharDriverState *chr; |
| 3470 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3471 | QTAILQ_FOREACH(chr, &chardevs, next) { |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 3472 | if (strcmp(chr->label, name) != 0) |
| 3473 | continue; |
| 3474 | return chr; |
| 3475 | } |
| 3476 | return NULL; |
| 3477 | } |
Anthony Liguori | 0beb494 | 2011-12-22 15:29:25 -0600 | [diff] [blame] | 3478 | |
| 3479 | /* Get a character (serial) device interface. */ |
| 3480 | CharDriverState *qemu_char_get_next_serial(void) |
| 3481 | { |
| 3482 | static int next_serial; |
Hans de Goede | 456d606 | 2013-03-27 20:29:40 +0100 | [diff] [blame] | 3483 | CharDriverState *chr; |
Anthony Liguori | 0beb494 | 2011-12-22 15:29:25 -0600 | [diff] [blame] | 3484 | |
| 3485 | /* FIXME: This function needs to go away: use chardev properties! */ |
Hans de Goede | 456d606 | 2013-03-27 20:29:40 +0100 | [diff] [blame] | 3486 | |
| 3487 | while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) { |
| 3488 | chr = serial_hds[next_serial++]; |
| 3489 | qemu_chr_fe_claim_no_fail(chr); |
| 3490 | return chr; |
| 3491 | } |
| 3492 | return NULL; |
Anthony Liguori | 0beb494 | 2011-12-22 15:29:25 -0600 | [diff] [blame] | 3493 | } |
| 3494 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 3495 | QemuOptsList qemu_chardev_opts = { |
| 3496 | .name = "chardev", |
| 3497 | .implied_opt_name = "backend", |
| 3498 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), |
| 3499 | .desc = { |
| 3500 | { |
| 3501 | .name = "backend", |
| 3502 | .type = QEMU_OPT_STRING, |
| 3503 | },{ |
| 3504 | .name = "path", |
| 3505 | .type = QEMU_OPT_STRING, |
| 3506 | },{ |
| 3507 | .name = "host", |
| 3508 | .type = QEMU_OPT_STRING, |
| 3509 | },{ |
| 3510 | .name = "port", |
| 3511 | .type = QEMU_OPT_STRING, |
| 3512 | },{ |
| 3513 | .name = "localaddr", |
| 3514 | .type = QEMU_OPT_STRING, |
| 3515 | },{ |
| 3516 | .name = "localport", |
| 3517 | .type = QEMU_OPT_STRING, |
| 3518 | },{ |
| 3519 | .name = "to", |
| 3520 | .type = QEMU_OPT_NUMBER, |
| 3521 | },{ |
| 3522 | .name = "ipv4", |
| 3523 | .type = QEMU_OPT_BOOL, |
| 3524 | },{ |
| 3525 | .name = "ipv6", |
| 3526 | .type = QEMU_OPT_BOOL, |
| 3527 | },{ |
| 3528 | .name = "wait", |
| 3529 | .type = QEMU_OPT_BOOL, |
| 3530 | },{ |
| 3531 | .name = "server", |
| 3532 | .type = QEMU_OPT_BOOL, |
| 3533 | },{ |
| 3534 | .name = "delay", |
| 3535 | .type = QEMU_OPT_BOOL, |
| 3536 | },{ |
| 3537 | .name = "telnet", |
| 3538 | .type = QEMU_OPT_BOOL, |
| 3539 | },{ |
| 3540 | .name = "width", |
| 3541 | .type = QEMU_OPT_NUMBER, |
| 3542 | },{ |
| 3543 | .name = "height", |
| 3544 | .type = QEMU_OPT_NUMBER, |
| 3545 | },{ |
| 3546 | .name = "cols", |
| 3547 | .type = QEMU_OPT_NUMBER, |
| 3548 | },{ |
| 3549 | .name = "rows", |
| 3550 | .type = QEMU_OPT_NUMBER, |
| 3551 | },{ |
| 3552 | .name = "mux", |
| 3553 | .type = QEMU_OPT_BOOL, |
| 3554 | },{ |
| 3555 | .name = "signal", |
| 3556 | .type = QEMU_OPT_BOOL, |
| 3557 | },{ |
| 3558 | .name = "name", |
| 3559 | .type = QEMU_OPT_STRING, |
| 3560 | },{ |
| 3561 | .name = "debug", |
| 3562 | .type = QEMU_OPT_NUMBER, |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3563 | },{ |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3564 | .name = "size", |
Markus Armbruster | de1cc36 | 2013-02-06 21:27:25 +0100 | [diff] [blame] | 3565 | .type = QEMU_OPT_SIZE, |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 3566 | },{ |
| 3567 | .name = "chardev", |
| 3568 | .type = QEMU_OPT_STRING, |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 3569 | }, |
| 3570 | { /* end of list */ } |
| 3571 | }, |
| 3572 | }; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3573 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3574 | #ifdef _WIN32 |
| 3575 | |
| 3576 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) |
| 3577 | { |
| 3578 | HANDLE out; |
| 3579 | |
Gerd Hoffmann | e859eda | 2013-06-24 08:39:47 +0200 | [diff] [blame] | 3580 | if (file->has_in) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3581 | error_setg(errp, "input file not supported"); |
| 3582 | return NULL; |
| 3583 | } |
| 3584 | |
| 3585 | out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 3586 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 3587 | if (out == INVALID_HANDLE_VALUE) { |
| 3588 | error_setg(errp, "open %s failed", file->out); |
| 3589 | return NULL; |
| 3590 | } |
| 3591 | return qemu_chr_open_win_file(out); |
| 3592 | } |
| 3593 | |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 3594 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
| 3595 | Error **errp) |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3596 | { |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 3597 | return qemu_chr_open_win_path(serial->device); |
| 3598 | } |
| 3599 | |
| 3600 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, |
| 3601 | Error **errp) |
| 3602 | { |
| 3603 | error_setg(errp, "character device backend type 'parallel' not supported"); |
| 3604 | return NULL; |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3605 | } |
| 3606 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3607 | #else /* WIN32 */ |
| 3608 | |
| 3609 | static int qmp_chardev_open_file_source(char *src, int flags, |
| 3610 | Error **errp) |
| 3611 | { |
| 3612 | int fd = -1; |
| 3613 | |
| 3614 | TFR(fd = qemu_open(src, flags, 0666)); |
| 3615 | if (fd == -1) { |
Gerd Hoffmann | 20c3976 | 2013-06-24 08:39:48 +0200 | [diff] [blame] | 3616 | error_setg_file_open(errp, errno, src); |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3617 | } |
| 3618 | return fd; |
| 3619 | } |
| 3620 | |
| 3621 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) |
| 3622 | { |
| 3623 | int flags, in = -1, out = -1; |
| 3624 | |
| 3625 | flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY; |
| 3626 | out = qmp_chardev_open_file_source(file->out, flags, errp); |
| 3627 | if (error_is_set(errp)) { |
| 3628 | return NULL; |
| 3629 | } |
| 3630 | |
Gerd Hoffmann | e859eda | 2013-06-24 08:39:47 +0200 | [diff] [blame] | 3631 | if (file->has_in) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3632 | flags = O_RDONLY; |
| 3633 | in = qmp_chardev_open_file_source(file->in, flags, errp); |
| 3634 | if (error_is_set(errp)) { |
| 3635 | qemu_close(out); |
| 3636 | return NULL; |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | return qemu_chr_open_fd(in, out); |
| 3641 | } |
| 3642 | |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 3643 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
| 3644 | Error **errp) |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3645 | { |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3646 | #ifdef HAVE_CHARDEV_TTY |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 3647 | int fd; |
| 3648 | |
| 3649 | fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); |
| 3650 | if (error_is_set(errp)) { |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3651 | return NULL; |
| 3652 | } |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 3653 | qemu_set_nonblock(fd); |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 3654 | return qemu_chr_open_tty_fd(fd); |
| 3655 | #else |
| 3656 | error_setg(errp, "character device backend type 'serial' not supported"); |
| 3657 | return NULL; |
| 3658 | #endif |
| 3659 | } |
| 3660 | |
| 3661 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, |
| 3662 | Error **errp) |
| 3663 | { |
| 3664 | #ifdef HAVE_CHARDEV_PARPORT |
| 3665 | int fd; |
| 3666 | |
| 3667 | fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); |
| 3668 | if (error_is_set(errp)) { |
| 3669 | return NULL; |
| 3670 | } |
| 3671 | return qemu_chr_open_pp_fd(fd); |
| 3672 | #else |
| 3673 | error_setg(errp, "character device backend type 'parallel' not supported"); |
| 3674 | return NULL; |
| 3675 | #endif |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3676 | } |
| 3677 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3678 | #endif /* WIN32 */ |
| 3679 | |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 3680 | static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock, |
| 3681 | Error **errp) |
| 3682 | { |
| 3683 | SocketAddress *addr = sock->addr; |
| 3684 | bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; |
| 3685 | bool is_listen = sock->has_server ? sock->server : true; |
| 3686 | bool is_telnet = sock->has_telnet ? sock->telnet : false; |
| 3687 | bool is_waitconnect = sock->has_wait ? sock->wait : false; |
| 3688 | int fd; |
| 3689 | |
| 3690 | if (is_listen) { |
| 3691 | fd = socket_listen(addr, errp); |
| 3692 | } else { |
| 3693 | fd = socket_connect(addr, errp, NULL, NULL); |
| 3694 | } |
| 3695 | if (error_is_set(errp)) { |
| 3696 | return NULL; |
| 3697 | } |
| 3698 | return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, |
| 3699 | is_telnet, is_waitconnect, errp); |
| 3700 | } |
| 3701 | |
Lei Li | 08d0ab3 | 2013-05-20 14:51:03 +0800 | [diff] [blame] | 3702 | static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp, |
| 3703 | Error **errp) |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 3704 | { |
| 3705 | int fd; |
| 3706 | |
Lei Li | 08d0ab3 | 2013-05-20 14:51:03 +0800 | [diff] [blame] | 3707 | fd = socket_dgram(udp->remote, udp->local, errp); |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 3708 | if (error_is_set(errp)) { |
| 3709 | return NULL; |
| 3710 | } |
| 3711 | return qemu_chr_open_udp_fd(fd); |
| 3712 | } |
| 3713 | |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3714 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, |
| 3715 | Error **errp) |
| 3716 | { |
| 3717 | ChardevReturn *ret = g_new0(ChardevReturn, 1); |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3718 | CharDriverState *base, *chr = NULL; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3719 | |
| 3720 | chr = qemu_chr_find(id); |
| 3721 | if (chr) { |
| 3722 | error_setg(errp, "Chardev '%s' already exists", id); |
| 3723 | g_free(ret); |
| 3724 | return NULL; |
| 3725 | } |
| 3726 | |
| 3727 | switch (backend->kind) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 3728 | case CHARDEV_BACKEND_KIND_FILE: |
| 3729 | chr = qmp_chardev_open_file(backend->file, errp); |
| 3730 | break; |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 3731 | case CHARDEV_BACKEND_KIND_SERIAL: |
| 3732 | chr = qmp_chardev_open_serial(backend->serial, errp); |
| 3733 | break; |
| 3734 | case CHARDEV_BACKEND_KIND_PARALLEL: |
| 3735 | chr = qmp_chardev_open_parallel(backend->parallel, errp); |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 3736 | break; |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 3737 | case CHARDEV_BACKEND_KIND_PIPE: |
| 3738 | chr = qemu_chr_open_pipe(backend->pipe); |
| 3739 | break; |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 3740 | case CHARDEV_BACKEND_KIND_SOCKET: |
| 3741 | chr = qmp_chardev_open_socket(backend->socket, errp); |
| 3742 | break; |
Lei Li | 08d0ab3 | 2013-05-20 14:51:03 +0800 | [diff] [blame] | 3743 | case CHARDEV_BACKEND_KIND_UDP: |
| 3744 | chr = qmp_chardev_open_udp(backend->udp, errp); |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 3745 | break; |
Gerd Hoffmann | 0a1a7fa | 2012-12-20 14:39:13 +0100 | [diff] [blame] | 3746 | #ifdef HAVE_CHARDEV_TTY |
| 3747 | case CHARDEV_BACKEND_KIND_PTY: |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 3748 | chr = qemu_chr_open_pty(id, ret); |
Gerd Hoffmann | 0a1a7fa | 2012-12-20 14:39:13 +0100 | [diff] [blame] | 3749 | break; |
Gerd Hoffmann | 0a1a7fa | 2012-12-20 14:39:13 +0100 | [diff] [blame] | 3750 | #endif |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3751 | case CHARDEV_BACKEND_KIND_NULL: |
Gerd Hoffmann | 80dca9e | 2013-02-21 11:41:26 +0100 | [diff] [blame] | 3752 | chr = qemu_chr_open_null(); |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3753 | break; |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3754 | case CHARDEV_BACKEND_KIND_MUX: |
| 3755 | base = qemu_chr_find(backend->mux->chardev); |
| 3756 | if (base == NULL) { |
| 3757 | error_setg(errp, "mux: base chardev %s not found", |
| 3758 | backend->mux->chardev); |
| 3759 | break; |
| 3760 | } |
| 3761 | chr = qemu_chr_open_mux(base); |
| 3762 | break; |
Gerd Hoffmann | f5a51ca | 2013-02-21 11:58:44 +0100 | [diff] [blame] | 3763 | case CHARDEV_BACKEND_KIND_MSMOUSE: |
| 3764 | chr = qemu_chr_open_msmouse(); |
| 3765 | break; |
Gerd Hoffmann | 2d57286 | 2013-02-21 12:56:10 +0100 | [diff] [blame] | 3766 | #ifdef CONFIG_BRLAPI |
| 3767 | case CHARDEV_BACKEND_KIND_BRAILLE: |
| 3768 | chr = chr_baum_init(); |
| 3769 | break; |
| 3770 | #endif |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 3771 | case CHARDEV_BACKEND_KIND_STDIO: |
| 3772 | chr = qemu_chr_open_stdio(backend->stdio); |
| 3773 | break; |
Gerd Hoffmann | d9ac374 | 2013-02-25 11:48:06 +0100 | [diff] [blame] | 3774 | #ifdef _WIN32 |
| 3775 | case CHARDEV_BACKEND_KIND_CONSOLE: |
| 3776 | chr = qemu_chr_open_win_con(); |
| 3777 | break; |
| 3778 | #endif |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 3779 | #ifdef CONFIG_SPICE |
| 3780 | case CHARDEV_BACKEND_KIND_SPICEVMC: |
| 3781 | chr = qemu_chr_open_spice_vmc(backend->spicevmc->type); |
| 3782 | break; |
| 3783 | case CHARDEV_BACKEND_KIND_SPICEPORT: |
| 3784 | chr = qemu_chr_open_spice_port(backend->spiceport->fqdn); |
| 3785 | break; |
| 3786 | #endif |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 3787 | case CHARDEV_BACKEND_KIND_VC: |
| 3788 | chr = vc_init(backend->vc); |
| 3789 | break; |
Markus Armbruster | 3a1da42 | 2013-07-26 16:44:34 +0200 | [diff] [blame] | 3790 | case CHARDEV_BACKEND_KIND_RINGBUF: |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3791 | case CHARDEV_BACKEND_KIND_MEMORY: |
Markus Armbruster | 3a1da42 | 2013-07-26 16:44:34 +0200 | [diff] [blame] | 3792 | chr = qemu_chr_open_ringbuf(backend->ringbuf, errp); |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3793 | break; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3794 | default: |
| 3795 | error_setg(errp, "unknown chardev backend (%d)", backend->kind); |
| 3796 | break; |
| 3797 | } |
| 3798 | |
| 3799 | if (chr == NULL && !error_is_set(errp)) { |
| 3800 | error_setg(errp, "Failed to create chardev"); |
| 3801 | } |
| 3802 | if (chr) { |
| 3803 | chr->label = g_strdup(id); |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 3804 | chr->avail_connections = |
| 3805 | (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1; |
Gerd Hoffmann | 60d9538 | 2013-05-27 12:41:24 +0200 | [diff] [blame] | 3806 | if (!chr->filename) { |
| 3807 | chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]); |
| 3808 | } |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 3809 | if (!chr->explicit_be_open) { |
| 3810 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 3811 | } |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 3812 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
| 3813 | return ret; |
| 3814 | } else { |
| 3815 | g_free(ret); |
| 3816 | return NULL; |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | void qmp_chardev_remove(const char *id, Error **errp) |
| 3821 | { |
| 3822 | CharDriverState *chr; |
| 3823 | |
| 3824 | chr = qemu_chr_find(id); |
| 3825 | if (NULL == chr) { |
| 3826 | error_setg(errp, "Chardev '%s' not found", id); |
| 3827 | return; |
| 3828 | } |
| 3829 | if (chr->chr_can_read || chr->chr_read || |
| 3830 | chr->chr_event || chr->handler_opaque) { |
| 3831 | error_setg(errp, "Chardev '%s' is busy", id); |
| 3832 | return; |
| 3833 | } |
| 3834 | qemu_chr_delete(chr); |
| 3835 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3836 | |
| 3837 | static void register_types(void) |
| 3838 | { |
Gerd Hoffmann | 80dca9e | 2013-02-21 11:41:26 +0100 | [diff] [blame] | 3839 | register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL); |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3840 | register_char_driver("socket", qemu_chr_open_socket); |
| 3841 | register_char_driver("udp", qemu_chr_open_udp); |
Markus Armbruster | 3a1da42 | 2013-07-26 16:44:34 +0200 | [diff] [blame] | 3842 | register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF, |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 3843 | qemu_chr_parse_ringbuf); |
Gerd Hoffmann | 846e2e4 | 2013-02-21 12:07:14 +0100 | [diff] [blame] | 3844 | register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE, |
| 3845 | qemu_chr_parse_file_out); |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 3846 | register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO, |
| 3847 | qemu_chr_parse_stdio); |
Gerd Hoffmann | 0f1cb51 | 2013-02-22 15:48:05 +0100 | [diff] [blame] | 3848 | register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL, |
| 3849 | qemu_chr_parse_serial); |
| 3850 | register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL, |
| 3851 | qemu_chr_parse_serial); |
Gerd Hoffmann | dc37509 | 2013-02-22 16:17:01 +0100 | [diff] [blame] | 3852 | register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL, |
| 3853 | qemu_chr_parse_parallel); |
| 3854 | register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL, |
| 3855 | qemu_chr_parse_parallel); |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 3856 | register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL); |
Gerd Hoffmann | d9ac374 | 2013-02-25 11:48:06 +0100 | [diff] [blame] | 3857 | register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL); |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 3858 | register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE, |
| 3859 | qemu_chr_parse_pipe); |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 3860 | register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX, |
| 3861 | qemu_chr_parse_mux); |
Markus Armbruster | c11ed96 | 2013-07-26 16:44:33 +0200 | [diff] [blame] | 3862 | /* Bug-compatibility: */ |
| 3863 | register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY, |
| 3864 | qemu_chr_parse_ringbuf); |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 3865 | /* this must be done after machine init, since we register FEs with muxes |
| 3866 | * as part of realize functions like serial_isa_realizefn when -nographic |
| 3867 | * is specified |
| 3868 | */ |
| 3869 | qemu_add_machine_init_done_notifier(&muxes_realize_notify); |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | type_init(register_types); |