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