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