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" |
| 25 | #include "net.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 26 | #include "monitor.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 27 | #include "console.h" |
| 28 | #include "sysemu.h" |
| 29 | #include "qemu-timer.h" |
| 30 | #include "qemu-char.h" |
aurel32 | cf3ebac | 2008-11-01 00:53:09 +0000 | [diff] [blame] | 31 | #include "hw/usb.h" |
| 32 | #include "hw/baum.h" |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 33 | #include "hw/msmouse.h" |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 34 | #include "qmp-commands.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 35 | |
| 36 | #include <unistd.h> |
| 37 | #include <fcntl.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 38 | #include <time.h> |
| 39 | #include <errno.h> |
| 40 | #include <sys/time.h> |
| 41 | #include <zlib.h> |
| 42 | |
| 43 | #ifndef _WIN32 |
| 44 | #include <sys/times.h> |
| 45 | #include <sys/wait.h> |
| 46 | #include <termios.h> |
| 47 | #include <sys/mman.h> |
| 48 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 49 | #include <sys/resource.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 50 | #include <sys/socket.h> |
| 51 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 52 | #include <net/if.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 53 | #include <arpa/inet.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 54 | #include <dirent.h> |
| 55 | #include <netdb.h> |
| 56 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 57 | #ifdef CONFIG_BSD |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 58 | #include <sys/stat.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 59 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 60 | #include <libutil.h> |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 61 | #include <dev/ppbus/ppi.h> |
| 62 | #include <dev/ppbus/ppbconf.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 63 | #if defined(__GLIBC__) |
| 64 | #include <pty.h> |
| 65 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 66 | #elif defined(__DragonFly__) |
| 67 | #include <libutil.h> |
| 68 | #include <dev/misc/ppi/ppi.h> |
| 69 | #include <bus/ppbus/ppbconf.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 70 | #else |
| 71 | #include <util.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 72 | #endif |
Aurelien Jarno | bbe813a | 2009-11-30 15:42:59 +0100 | [diff] [blame] | 73 | #else |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 74 | #ifdef __linux__ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 75 | #include <pty.h> |
| 76 | |
| 77 | #include <linux/ppdev.h> |
| 78 | #include <linux/parport.h> |
| 79 | #endif |
| 80 | #ifdef __sun__ |
| 81 | #include <sys/stat.h> |
| 82 | #include <sys/ethernet.h> |
| 83 | #include <sys/sockio.h> |
| 84 | #include <netinet/arp.h> |
| 85 | #include <netinet/in.h> |
| 86 | #include <netinet/in_systm.h> |
| 87 | #include <netinet/ip.h> |
| 88 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 89 | #include <netinet/udp.h> |
| 90 | #include <netinet/tcp.h> |
| 91 | #include <net/if.h> |
| 92 | #include <syslog.h> |
| 93 | #include <stropts.h> |
| 94 | #endif |
| 95 | #endif |
| 96 | #endif |
| 97 | |
| 98 | #include "qemu_socket.h" |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 99 | #include "ui/qemu-spice.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 100 | |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 101 | #define READ_BUF_LEN 4096 |
| 102 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 103 | /***********************************************************/ |
| 104 | /* character device */ |
| 105 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 106 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
| 107 | QTAILQ_HEAD_INITIALIZER(chardevs); |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 108 | |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 109 | void qemu_chr_be_event(CharDriverState *s, int event) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 110 | { |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 111 | /* Keep track if the char device is open */ |
| 112 | switch (event) { |
| 113 | case CHR_EVENT_OPENED: |
| 114 | s->opened = 1; |
| 115 | break; |
| 116 | case CHR_EVENT_CLOSED: |
| 117 | s->opened = 0; |
| 118 | break; |
| 119 | } |
| 120 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 121 | if (!s->chr_event) |
| 122 | return; |
| 123 | s->chr_event(s->handler_opaque, event); |
| 124 | } |
| 125 | |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 126 | static void qemu_chr_generic_open_bh(void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 127 | { |
| 128 | CharDriverState *s = opaque; |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 129 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 130 | qemu_bh_delete(s->bh); |
| 131 | s->bh = NULL; |
| 132 | } |
| 133 | |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 134 | void qemu_chr_generic_open(CharDriverState *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 135 | { |
Amit Shah | 69795d6 | 2009-10-07 18:31:15 +0530 | [diff] [blame] | 136 | if (s->bh == NULL) { |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 137 | s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 138 | qemu_bh_schedule(s->bh); |
| 139 | } |
| 140 | } |
| 141 | |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 142 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 143 | { |
| 144 | return s->chr_write(s, buf, len); |
| 145 | } |
| 146 | |
Anthony Liguori | 41084f1 | 2011-08-15 11:17:34 -0500 | [diff] [blame] | 147 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 148 | { |
| 149 | if (!s->chr_ioctl) |
| 150 | return -ENOTSUP; |
| 151 | return s->chr_ioctl(s, cmd, arg); |
| 152 | } |
| 153 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 154 | int qemu_chr_be_can_write(CharDriverState *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 155 | { |
| 156 | if (!s->chr_can_read) |
| 157 | return 0; |
| 158 | return s->chr_can_read(s->handler_opaque); |
| 159 | } |
| 160 | |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 161 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 162 | { |
| 163 | s->chr_read(s->handler_opaque, buf, len); |
| 164 | } |
| 165 | |
Anthony Liguori | 74c0d6f | 2011-08-15 11:17:39 -0500 | [diff] [blame] | 166 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 167 | { |
| 168 | return s->get_msgfd ? s->get_msgfd(s) : -1; |
| 169 | } |
| 170 | |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 171 | int qemu_chr_add_client(CharDriverState *s, int fd) |
| 172 | { |
| 173 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; |
| 174 | } |
| 175 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 176 | void qemu_chr_accept_input(CharDriverState *s) |
| 177 | { |
| 178 | if (s->chr_accept_input) |
| 179 | s->chr_accept_input(s); |
| 180 | } |
| 181 | |
Anthony Liguori | e7e71b0 | 2011-08-15 11:17:29 -0500 | [diff] [blame] | 182 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 183 | { |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 184 | char buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 185 | va_list ap; |
| 186 | va_start(ap, fmt); |
| 187 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 188 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 189 | va_end(ap); |
| 190 | } |
| 191 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 192 | void qemu_chr_add_handlers(CharDriverState *s, |
Juan Quintela | 7b27a76 | 2010-03-11 17:55:39 +0100 | [diff] [blame] | 193 | IOCanReadHandler *fd_can_read, |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 194 | IOReadHandler *fd_read, |
| 195 | IOEventHandler *fd_event, |
| 196 | void *opaque) |
| 197 | { |
Amit Shah | da7d998 | 2011-04-25 15:18:22 +0530 | [diff] [blame] | 198 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
Amit Shah | 2d6c1ef | 2011-02-10 12:55:20 +0530 | [diff] [blame] | 199 | /* chr driver being released. */ |
Kusanagi Kouichi | d5b2716 | 2011-04-26 19:19:26 +0900 | [diff] [blame] | 200 | ++s->avail_connections; |
Amit Shah | 2d6c1ef | 2011-02-10 12:55:20 +0530 | [diff] [blame] | 201 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 202 | s->chr_can_read = fd_can_read; |
| 203 | s->chr_read = fd_read; |
| 204 | s->chr_event = fd_event; |
| 205 | s->handler_opaque = opaque; |
| 206 | if (s->chr_update_read_handler) |
| 207 | s->chr_update_read_handler(s); |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 208 | |
| 209 | /* We're connecting to an already opened device, so let's make sure we |
| 210 | also get the open event */ |
| 211 | if (s->opened) { |
| 212 | qemu_chr_generic_open(s); |
| 213 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 217 | { |
| 218 | return len; |
| 219 | } |
| 220 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 221 | static int qemu_chr_open_null(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 222 | { |
| 223 | CharDriverState *chr; |
| 224 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 225 | chr = g_malloc0(sizeof(CharDriverState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 226 | chr->chr_write = null_chr_write; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 227 | |
| 228 | *_chr= chr; |
| 229 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* MUX driver for serial I/O splitting */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 233 | #define MAX_MUX 4 |
| 234 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
| 235 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
| 236 | typedef struct { |
Juan Quintela | 7b27a76 | 2010-03-11 17:55:39 +0100 | [diff] [blame] | 237 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 238 | IOReadHandler *chr_read[MAX_MUX]; |
| 239 | IOEventHandler *chr_event[MAX_MUX]; |
| 240 | void *ext_opaque[MAX_MUX]; |
| 241 | CharDriverState *drv; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 242 | int focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 243 | int mux_cnt; |
| 244 | int term_got_escape; |
| 245 | int max_size; |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 246 | /* Intermediate input buffer allows to catch escape sequences even if the |
| 247 | currently active device is not accepting any input - but only until it |
| 248 | is full as well. */ |
| 249 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; |
| 250 | int prod[MAX_MUX]; |
| 251 | int cons[MAX_MUX]; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 252 | int timestamps; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 253 | int linestart; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 254 | int64_t timestamps_start; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 255 | } MuxDriver; |
| 256 | |
| 257 | |
| 258 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 259 | { |
| 260 | MuxDriver *d = chr->opaque; |
| 261 | int ret; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 262 | if (!d->timestamps) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 263 | ret = d->drv->chr_write(d->drv, buf, len); |
| 264 | } else { |
| 265 | int i; |
| 266 | |
| 267 | ret = 0; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 268 | for (i = 0; i < len; i++) { |
| 269 | if (d->linestart) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 270 | char buf1[64]; |
| 271 | int64_t ti; |
| 272 | int secs; |
| 273 | |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 274 | ti = qemu_get_clock_ms(rt_clock); |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 275 | if (d->timestamps_start == -1) |
| 276 | d->timestamps_start = ti; |
| 277 | ti -= d->timestamps_start; |
aliguori | a4bb1db | 2009-01-22 17:15:16 +0000 | [diff] [blame] | 278 | secs = ti / 1000; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 279 | snprintf(buf1, sizeof(buf1), |
| 280 | "[%02d:%02d:%02d.%03d] ", |
| 281 | secs / 3600, |
| 282 | (secs / 60) % 60, |
| 283 | secs % 60, |
aliguori | a4bb1db | 2009-01-22 17:15:16 +0000 | [diff] [blame] | 284 | (int)(ti % 1000)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 285 | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 286 | d->linestart = 0; |
| 287 | } |
| 288 | ret += d->drv->chr_write(d->drv, buf+i, 1); |
| 289 | if (buf[i] == '\n') { |
| 290 | d->linestart = 1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static const char * const mux_help[] = { |
| 298 | "% h print this help\n\r", |
| 299 | "% x exit emulator\n\r", |
| 300 | "% s save disk data back to file (if -snapshot)\n\r", |
| 301 | "% t toggle console timestamps\n\r" |
| 302 | "% b send break (magic sysrq)\n\r", |
| 303 | "% c switch between console and monitor\n\r", |
| 304 | "% % sends %\n\r", |
| 305 | NULL |
| 306 | }; |
| 307 | |
| 308 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 309 | static void mux_print_help(CharDriverState *chr) |
| 310 | { |
| 311 | int i, j; |
| 312 | char ebuf[15] = "Escape-Char"; |
| 313 | char cbuf[50] = "\n\r"; |
| 314 | |
| 315 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 316 | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
| 317 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
| 318 | } else { |
| 319 | snprintf(cbuf, sizeof(cbuf), |
| 320 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", |
| 321 | term_escape_char); |
| 322 | } |
| 323 | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
| 324 | for (i = 0; mux_help[i] != NULL; i++) { |
| 325 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 326 | if (mux_help[i][j] == '%') |
| 327 | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
| 328 | else |
| 329 | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 334 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
| 335 | { |
| 336 | if (d->chr_event[mux_nr]) |
| 337 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); |
| 338 | } |
| 339 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 340 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 341 | { |
| 342 | if (d->term_got_escape) { |
| 343 | d->term_got_escape = 0; |
| 344 | if (ch == term_escape_char) |
| 345 | goto send_char; |
| 346 | switch(ch) { |
| 347 | case '?': |
| 348 | case 'h': |
| 349 | mux_print_help(chr); |
| 350 | break; |
| 351 | case 'x': |
| 352 | { |
| 353 | const char *term = "QEMU: Terminated\n\r"; |
| 354 | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
| 355 | exit(0); |
| 356 | break; |
| 357 | } |
| 358 | case 's': |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 359 | bdrv_commit_all(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 360 | break; |
| 361 | case 'b': |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 362 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 363 | break; |
| 364 | case 'c': |
| 365 | /* Switch to the next registered device */ |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 366 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
| 367 | d->focus++; |
| 368 | if (d->focus >= d->mux_cnt) |
| 369 | d->focus = 0; |
| 370 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 371 | break; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 372 | case 't': |
| 373 | d->timestamps = !d->timestamps; |
| 374 | d->timestamps_start = -1; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 375 | d->linestart = 0; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 376 | break; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 377 | } |
| 378 | } else if (ch == term_escape_char) { |
| 379 | d->term_got_escape = 1; |
| 380 | } else { |
| 381 | send_char: |
| 382 | return 1; |
| 383 | } |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static void mux_chr_accept_input(CharDriverState *chr) |
| 388 | { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 389 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 390 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 391 | |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 392 | while (d->prod[m] != d->cons[m] && |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 393 | d->chr_can_read[m] && |
| 394 | d->chr_can_read[m](d->ext_opaque[m])) { |
| 395 | d->chr_read[m](d->ext_opaque[m], |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 396 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | static int mux_chr_can_read(void *opaque) |
| 401 | { |
| 402 | CharDriverState *chr = opaque; |
| 403 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 404 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 405 | |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 406 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 407 | return 1; |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 408 | if (d->chr_can_read[m]) |
| 409 | return d->chr_can_read[m](d->ext_opaque[m]); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 414 | { |
| 415 | CharDriverState *chr = opaque; |
| 416 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 417 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 418 | int i; |
| 419 | |
| 420 | mux_chr_accept_input (opaque); |
| 421 | |
| 422 | for(i = 0; i < size; i++) |
| 423 | if (mux_proc_byte(chr, d, buf[i])) { |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 424 | if (d->prod[m] == d->cons[m] && |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 425 | d->chr_can_read[m] && |
| 426 | d->chr_can_read[m](d->ext_opaque[m])) |
| 427 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); |
| 428 | else |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 429 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | static void mux_chr_event(void *opaque, int event) |
| 434 | { |
| 435 | CharDriverState *chr = opaque; |
| 436 | MuxDriver *d = chr->opaque; |
| 437 | int i; |
| 438 | |
| 439 | /* Send the event to all registered listeners */ |
| 440 | for (i = 0; i < d->mux_cnt; i++) |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 441 | mux_chr_send_event(d, i, event); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 445 | { |
| 446 | MuxDriver *d = chr->opaque; |
| 447 | |
| 448 | if (d->mux_cnt >= MAX_MUX) { |
| 449 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 450 | return; |
| 451 | } |
| 452 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 453 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 454 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 455 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 456 | /* Fix up the real driver with mux routines */ |
| 457 | if (d->mux_cnt == 0) { |
| 458 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 459 | mux_chr_event, chr); |
| 460 | } |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 461 | if (d->focus != -1) { |
| 462 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 463 | } |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 464 | d->focus = d->mux_cnt; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 465 | d->mux_cnt++; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 466 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
| 470 | { |
| 471 | CharDriverState *chr; |
| 472 | MuxDriver *d; |
| 473 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 474 | chr = g_malloc0(sizeof(CharDriverState)); |
| 475 | d = g_malloc0(sizeof(MuxDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 476 | |
| 477 | chr->opaque = d; |
| 478 | d->drv = drv; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 479 | d->focus = -1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 480 | chr->chr_write = mux_chr_write; |
| 481 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
| 482 | chr->chr_accept_input = mux_chr_accept_input; |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 483 | /* Frontend guest-open / -close notification is not support with muxes */ |
| 484 | chr->chr_guest_open = NULL; |
| 485 | chr->chr_guest_close = NULL; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 486 | |
| 487 | /* Muxes are always open on creation */ |
| 488 | qemu_chr_generic_open(chr); |
| 489 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 490 | return chr; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | #ifdef _WIN32 |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 495 | int send_all(int fd, const void *buf, int len1) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 496 | { |
| 497 | int ret, len; |
| 498 | |
| 499 | len = len1; |
| 500 | while (len > 0) { |
| 501 | ret = send(fd, buf, len, 0); |
| 502 | if (ret < 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 503 | errno = WSAGetLastError(); |
| 504 | if (errno != WSAEWOULDBLOCK) { |
| 505 | return -1; |
| 506 | } |
| 507 | } else if (ret == 0) { |
| 508 | break; |
| 509 | } else { |
| 510 | buf += ret; |
| 511 | len -= ret; |
| 512 | } |
| 513 | } |
| 514 | return len1 - len; |
| 515 | } |
| 516 | |
| 517 | #else |
| 518 | |
Jes Sorensen | 5fc9cfe | 2010-11-01 20:02:23 +0100 | [diff] [blame] | 519 | int send_all(int fd, const void *_buf, int len1) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 520 | { |
| 521 | int ret, len; |
Jes Sorensen | 5fc9cfe | 2010-11-01 20:02:23 +0100 | [diff] [blame] | 522 | const uint8_t *buf = _buf; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 523 | |
| 524 | len = len1; |
| 525 | while (len > 0) { |
| 526 | ret = write(fd, buf, len); |
| 527 | if (ret < 0) { |
| 528 | if (errno != EINTR && errno != EAGAIN) |
| 529 | return -1; |
| 530 | } else if (ret == 0) { |
| 531 | break; |
| 532 | } else { |
| 533 | buf += ret; |
| 534 | len -= ret; |
| 535 | } |
| 536 | } |
| 537 | return len1 - len; |
| 538 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 539 | #endif /* !_WIN32 */ |
| 540 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 541 | #define STDIO_MAX_CLIENTS 1 |
| 542 | static int stdio_nb_clients; |
| 543 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 544 | #ifndef _WIN32 |
| 545 | |
| 546 | typedef struct { |
| 547 | int fd_in, fd_out; |
| 548 | int max_size; |
| 549 | } FDCharDriver; |
| 550 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 551 | |
| 552 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 553 | { |
| 554 | FDCharDriver *s = chr->opaque; |
| 555 | return send_all(s->fd_out, buf, len); |
| 556 | } |
| 557 | |
| 558 | static int fd_chr_read_poll(void *opaque) |
| 559 | { |
| 560 | CharDriverState *chr = opaque; |
| 561 | FDCharDriver *s = chr->opaque; |
| 562 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 563 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 564 | return s->max_size; |
| 565 | } |
| 566 | |
| 567 | static void fd_chr_read(void *opaque) |
| 568 | { |
| 569 | CharDriverState *chr = opaque; |
| 570 | FDCharDriver *s = chr->opaque; |
| 571 | int size, len; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 572 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 573 | |
| 574 | len = sizeof(buf); |
| 575 | if (len > s->max_size) |
| 576 | len = s->max_size; |
| 577 | if (len == 0) |
| 578 | return; |
| 579 | size = read(s->fd_in, buf, len); |
| 580 | if (size == 0) { |
| 581 | /* FD has been closed. Remove it from the active list. */ |
| 582 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 583 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | if (size > 0) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 587 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | static void fd_chr_update_read_handler(CharDriverState *chr) |
| 592 | { |
| 593 | FDCharDriver *s = chr->opaque; |
| 594 | |
| 595 | if (s->fd_in >= 0) { |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 596 | if (display_type == DT_NOGRAPHIC && s->fd_in == 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 597 | } else { |
| 598 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
| 599 | fd_chr_read, NULL, chr); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static void fd_chr_close(struct CharDriverState *chr) |
| 605 | { |
| 606 | FDCharDriver *s = chr->opaque; |
| 607 | |
| 608 | if (s->fd_in >= 0) { |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 609 | if (display_type == DT_NOGRAPHIC && s->fd_in == 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 610 | } else { |
| 611 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 612 | } |
| 613 | } |
| 614 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 615 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 616 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* open a character device to a unix fd */ |
| 620 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
| 621 | { |
| 622 | CharDriverState *chr; |
| 623 | FDCharDriver *s; |
| 624 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 625 | chr = g_malloc0(sizeof(CharDriverState)); |
| 626 | s = g_malloc0(sizeof(FDCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 627 | s->fd_in = fd_in; |
| 628 | s->fd_out = fd_out; |
| 629 | chr->opaque = s; |
| 630 | chr->chr_write = fd_chr_write; |
| 631 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
| 632 | chr->chr_close = fd_chr_close; |
| 633 | |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 634 | qemu_chr_generic_open(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 635 | |
| 636 | return chr; |
| 637 | } |
| 638 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 639 | static int qemu_chr_open_file_out(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 640 | { |
| 641 | int fd_out; |
| 642 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 643 | TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"), |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 644 | O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666)); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 645 | if (fd_out < 0) { |
| 646 | return -errno; |
| 647 | } |
| 648 | |
| 649 | *_chr = qemu_chr_open_fd(-1, fd_out); |
| 650 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 653 | static int qemu_chr_open_pipe(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 654 | { |
| 655 | int fd_in, fd_out; |
| 656 | char filename_in[256], filename_out[256]; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 657 | const char *filename = qemu_opt_get(opts, "path"); |
| 658 | |
| 659 | if (filename == NULL) { |
| 660 | fprintf(stderr, "chardev: pipe: no filename given\n"); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 661 | return -EINVAL; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 662 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 663 | |
| 664 | snprintf(filename_in, 256, "%s.in", filename); |
| 665 | snprintf(filename_out, 256, "%s.out", filename); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 666 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
| 667 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 668 | if (fd_in < 0 || fd_out < 0) { |
| 669 | if (fd_in >= 0) |
| 670 | close(fd_in); |
| 671 | if (fd_out >= 0) |
| 672 | close(fd_out); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 673 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
| 674 | if (fd_in < 0) { |
| 675 | return -errno; |
| 676 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 677 | } |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 678 | |
| 679 | *_chr = qemu_chr_open_fd(fd_in, fd_out); |
| 680 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | |
| 684 | /* for STDIO, we handle the case where several clients use it |
| 685 | (nographic mode) */ |
| 686 | |
| 687 | #define TERM_FIFO_MAX_SIZE 1 |
| 688 | |
| 689 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
| 690 | static int term_fifo_size; |
| 691 | |
| 692 | static int stdio_read_poll(void *opaque) |
| 693 | { |
| 694 | CharDriverState *chr = opaque; |
| 695 | |
| 696 | /* try to flush the queue if needed */ |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 697 | if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 698 | qemu_chr_be_write(chr, term_fifo, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 699 | term_fifo_size = 0; |
| 700 | } |
| 701 | /* see if we can absorb more chars */ |
| 702 | if (term_fifo_size == 0) |
| 703 | return 1; |
| 704 | else |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static void stdio_read(void *opaque) |
| 709 | { |
| 710 | int size; |
| 711 | uint8_t buf[1]; |
| 712 | CharDriverState *chr = opaque; |
| 713 | |
| 714 | size = read(0, buf, 1); |
| 715 | if (size == 0) { |
| 716 | /* stdin has been closed. Remove it from the active list. */ |
| 717 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 718 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 719 | return; |
| 720 | } |
| 721 | if (size > 0) { |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 722 | if (qemu_chr_be_can_write(chr) > 0) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 723 | qemu_chr_be_write(chr, buf, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 724 | } else if (term_fifo_size == 0) { |
| 725 | term_fifo[term_fifo_size++] = buf[0]; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* init terminal so that we can grab keys */ |
| 731 | static struct termios oldtty; |
| 732 | static int old_fd0_flags; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 733 | static bool stdio_allow_signal; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 734 | |
| 735 | static void term_exit(void) |
| 736 | { |
| 737 | tcsetattr (0, TCSANOW, &oldtty); |
| 738 | fcntl(0, F_SETFL, old_fd0_flags); |
| 739 | } |
| 740 | |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 741 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 742 | { |
| 743 | struct termios tty; |
| 744 | |
Paolo Bonzini | 0369364 | 2010-12-23 13:42:49 +0100 | [diff] [blame] | 745 | tty = oldtty; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 746 | if (!echo) { |
| 747 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 748 | |INLCR|IGNCR|ICRNL|IXON); |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 749 | tty.c_oflag |= OPOST; |
| 750 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 751 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 752 | tty.c_cflag |= CS8; |
| 753 | tty.c_cc[VMIN] = 1; |
| 754 | tty.c_cc[VTIME] = 0; |
| 755 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 756 | /* if graphical mode, we allow Ctrl-C handling */ |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 757 | if (!stdio_allow_signal) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 758 | tty.c_lflag &= ~ISIG; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 759 | |
| 760 | tcsetattr (0, TCSANOW, &tty); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
| 764 | { |
| 765 | term_exit(); |
| 766 | stdio_nb_clients--; |
| 767 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 768 | fd_chr_close(chr); |
| 769 | } |
| 770 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 771 | static int qemu_chr_open_stdio(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 772 | { |
| 773 | CharDriverState *chr; |
| 774 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 775 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) { |
| 776 | return -EBUSY; |
| 777 | } |
| 778 | |
Paolo Bonzini | 0369364 | 2010-12-23 13:42:49 +0100 | [diff] [blame] | 779 | if (stdio_nb_clients == 0) { |
| 780 | old_fd0_flags = fcntl(0, F_GETFL); |
| 781 | tcgetattr (0, &oldtty); |
| 782 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 783 | atexit(term_exit); |
| 784 | } |
| 785 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 786 | chr = qemu_chr_open_fd(0, 1); |
| 787 | chr->chr_close = qemu_chr_close_stdio; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 788 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 789 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
| 790 | stdio_nb_clients++; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 791 | stdio_allow_signal = qemu_opt_get_bool(opts, "signal", |
| 792 | display_type != DT_NOGRAPHIC); |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 793 | qemu_chr_fe_set_echo(chr, false); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 794 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 795 | *_chr = chr; |
| 796 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | #ifdef __sun__ |
| 800 | /* Once Solaris has openpty(), this is going to be removed. */ |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 801 | static int openpty(int *amaster, int *aslave, char *name, |
| 802 | struct termios *termp, struct winsize *winp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 803 | { |
| 804 | const char *slave; |
| 805 | int mfd = -1, sfd = -1; |
| 806 | |
| 807 | *amaster = *aslave = -1; |
| 808 | |
| 809 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); |
| 810 | if (mfd < 0) |
| 811 | goto err; |
| 812 | |
| 813 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
| 814 | goto err; |
| 815 | |
| 816 | if ((slave = ptsname(mfd)) == NULL) |
| 817 | goto err; |
| 818 | |
| 819 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
| 820 | goto err; |
| 821 | |
| 822 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
| 823 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
| 824 | goto err; |
| 825 | |
| 826 | if (amaster) |
| 827 | *amaster = mfd; |
| 828 | if (aslave) |
| 829 | *aslave = sfd; |
| 830 | if (winp) |
| 831 | ioctl(sfd, TIOCSWINSZ, winp); |
| 832 | |
| 833 | return 0; |
| 834 | |
| 835 | err: |
| 836 | if (sfd != -1) |
| 837 | close(sfd); |
| 838 | close(mfd); |
| 839 | return -1; |
| 840 | } |
| 841 | |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 842 | static void cfmakeraw (struct termios *termios_p) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 843 | { |
| 844 | termios_p->c_iflag &= |
| 845 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
| 846 | termios_p->c_oflag &= ~OPOST; |
| 847 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
| 848 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
| 849 | termios_p->c_cflag |= CS8; |
| 850 | |
| 851 | termios_p->c_cc[VMIN] = 0; |
| 852 | termios_p->c_cc[VTIME] = 0; |
| 853 | } |
| 854 | #endif |
| 855 | |
| 856 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 857 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
| 858 | || defined(__GLIBC__) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 859 | |
| 860 | typedef struct { |
| 861 | int fd; |
| 862 | int connected; |
| 863 | int polling; |
| 864 | int read_bytes; |
| 865 | QEMUTimer *timer; |
| 866 | } PtyCharDriver; |
| 867 | |
| 868 | static void pty_chr_update_read_handler(CharDriverState *chr); |
| 869 | static void pty_chr_state(CharDriverState *chr, int connected); |
| 870 | |
| 871 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 872 | { |
| 873 | PtyCharDriver *s = chr->opaque; |
| 874 | |
| 875 | if (!s->connected) { |
| 876 | /* guest sends data, check for (re-)connect */ |
| 877 | pty_chr_update_read_handler(chr); |
| 878 | return 0; |
| 879 | } |
| 880 | return send_all(s->fd, buf, len); |
| 881 | } |
| 882 | |
| 883 | static int pty_chr_read_poll(void *opaque) |
| 884 | { |
| 885 | CharDriverState *chr = opaque; |
| 886 | PtyCharDriver *s = chr->opaque; |
| 887 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 888 | s->read_bytes = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 889 | return s->read_bytes; |
| 890 | } |
| 891 | |
| 892 | static void pty_chr_read(void *opaque) |
| 893 | { |
| 894 | CharDriverState *chr = opaque; |
| 895 | PtyCharDriver *s = chr->opaque; |
| 896 | int size, len; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 897 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 898 | |
| 899 | len = sizeof(buf); |
| 900 | if (len > s->read_bytes) |
| 901 | len = s->read_bytes; |
| 902 | if (len == 0) |
| 903 | return; |
| 904 | size = read(s->fd, buf, len); |
| 905 | if ((size == -1 && errno == EIO) || |
| 906 | (size == 0)) { |
| 907 | pty_chr_state(chr, 0); |
| 908 | return; |
| 909 | } |
| 910 | if (size > 0) { |
| 911 | pty_chr_state(chr, 1); |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 912 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | static void pty_chr_update_read_handler(CharDriverState *chr) |
| 917 | { |
| 918 | PtyCharDriver *s = chr->opaque; |
| 919 | |
| 920 | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
| 921 | pty_chr_read, NULL, chr); |
| 922 | s->polling = 1; |
| 923 | /* |
| 924 | * Short timeout here: just need wait long enougth that qemu makes |
| 925 | * it through the poll loop once. When reconnected we want a |
| 926 | * short timeout so we notice it almost instantly. Otherwise |
| 927 | * read() gives us -EIO instantly, making pty_chr_state() reset the |
| 928 | * timeout to the normal (much longer) poll interval before the |
| 929 | * timer triggers. |
| 930 | */ |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 931 | qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static void pty_chr_state(CharDriverState *chr, int connected) |
| 935 | { |
| 936 | PtyCharDriver *s = chr->opaque; |
| 937 | |
| 938 | if (!connected) { |
| 939 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 940 | s->connected = 0; |
| 941 | s->polling = 0; |
| 942 | /* (re-)connect poll interval for idle guests: once per second. |
| 943 | * We check more frequently in case the guests sends data to |
| 944 | * the virtual device linked to our pty. */ |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 945 | qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 946 | } else { |
| 947 | if (!s->connected) |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 948 | qemu_chr_generic_open(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 949 | s->connected = 1; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | static void pty_chr_timer(void *opaque) |
| 954 | { |
| 955 | struct CharDriverState *chr = opaque; |
| 956 | PtyCharDriver *s = chr->opaque; |
| 957 | |
| 958 | if (s->connected) |
| 959 | return; |
| 960 | if (s->polling) { |
| 961 | /* If we arrive here without polling being cleared due |
| 962 | * read returning -EIO, then we are (re-)connected */ |
| 963 | pty_chr_state(chr, 1); |
| 964 | return; |
| 965 | } |
| 966 | |
| 967 | /* Next poll ... */ |
| 968 | pty_chr_update_read_handler(chr); |
| 969 | } |
| 970 | |
| 971 | static void pty_chr_close(struct CharDriverState *chr) |
| 972 | { |
| 973 | PtyCharDriver *s = chr->opaque; |
| 974 | |
| 975 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 976 | close(s->fd); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 977 | qemu_del_timer(s->timer); |
| 978 | qemu_free_timer(s->timer); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 979 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 980 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 983 | static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 984 | { |
| 985 | CharDriverState *chr; |
| 986 | PtyCharDriver *s; |
| 987 | struct termios tty; |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 988 | int master_fd, slave_fd, len; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 989 | #if defined(__OpenBSD__) || defined(__DragonFly__) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 990 | char pty_name[PATH_MAX]; |
| 991 | #define q_ptsname(x) pty_name |
| 992 | #else |
| 993 | char *pty_name = NULL; |
| 994 | #define q_ptsname(x) ptsname(x) |
| 995 | #endif |
| 996 | |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 997 | if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 998 | return -errno; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* Set raw attributes on the pty. */ |
aliguori | c3b972c | 2008-11-12 15:00:36 +0000 | [diff] [blame] | 1002 | tcgetattr(slave_fd, &tty); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1003 | cfmakeraw(&tty); |
| 1004 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
| 1005 | close(slave_fd); |
| 1006 | |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 1007 | chr = g_malloc0(sizeof(CharDriverState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1008 | |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 1009 | len = strlen(q_ptsname(master_fd)) + 5; |
| 1010 | chr->filename = g_malloc(len); |
| 1011 | snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd)); |
| 1012 | qemu_opt_set(opts, "path", q_ptsname(master_fd)); |
| 1013 | fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd)); |
| 1014 | |
| 1015 | s = g_malloc0(sizeof(PtyCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1016 | chr->opaque = s; |
| 1017 | chr->chr_write = pty_chr_write; |
| 1018 | chr->chr_update_read_handler = pty_chr_update_read_handler; |
| 1019 | chr->chr_close = pty_chr_close; |
| 1020 | |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 1021 | s->fd = master_fd; |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 1022 | s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1023 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1024 | *_chr = chr; |
| 1025 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | static void tty_serial_init(int fd, int speed, |
| 1029 | int parity, int data_bits, int stop_bits) |
| 1030 | { |
| 1031 | struct termios tty; |
| 1032 | speed_t spd; |
| 1033 | |
| 1034 | #if 0 |
| 1035 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1036 | speed, parity, data_bits, stop_bits); |
| 1037 | #endif |
| 1038 | tcgetattr (fd, &tty); |
| 1039 | |
Stefan Weil | 45eea13 | 2009-10-26 16:10:10 +0100 | [diff] [blame] | 1040 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
| 1041 | speed = speed * 10 / 11; |
| 1042 | do { |
| 1043 | check_speed(50); |
| 1044 | check_speed(75); |
| 1045 | check_speed(110); |
| 1046 | check_speed(134); |
| 1047 | check_speed(150); |
| 1048 | check_speed(200); |
| 1049 | check_speed(300); |
| 1050 | check_speed(600); |
| 1051 | check_speed(1200); |
| 1052 | check_speed(1800); |
| 1053 | check_speed(2400); |
| 1054 | check_speed(4800); |
| 1055 | check_speed(9600); |
| 1056 | check_speed(19200); |
| 1057 | check_speed(38400); |
| 1058 | /* Non-Posix values follow. They may be unsupported on some systems. */ |
| 1059 | check_speed(57600); |
| 1060 | check_speed(115200); |
| 1061 | #ifdef B230400 |
| 1062 | check_speed(230400); |
| 1063 | #endif |
| 1064 | #ifdef B460800 |
| 1065 | check_speed(460800); |
| 1066 | #endif |
| 1067 | #ifdef B500000 |
| 1068 | check_speed(500000); |
| 1069 | #endif |
| 1070 | #ifdef B576000 |
| 1071 | check_speed(576000); |
| 1072 | #endif |
| 1073 | #ifdef B921600 |
| 1074 | check_speed(921600); |
| 1075 | #endif |
| 1076 | #ifdef B1000000 |
| 1077 | check_speed(1000000); |
| 1078 | #endif |
| 1079 | #ifdef B1152000 |
| 1080 | check_speed(1152000); |
| 1081 | #endif |
| 1082 | #ifdef B1500000 |
| 1083 | check_speed(1500000); |
| 1084 | #endif |
| 1085 | #ifdef B2000000 |
| 1086 | check_speed(2000000); |
| 1087 | #endif |
| 1088 | #ifdef B2500000 |
| 1089 | check_speed(2500000); |
| 1090 | #endif |
| 1091 | #ifdef B3000000 |
| 1092 | check_speed(3000000); |
| 1093 | #endif |
| 1094 | #ifdef B3500000 |
| 1095 | check_speed(3500000); |
| 1096 | #endif |
| 1097 | #ifdef B4000000 |
| 1098 | check_speed(4000000); |
| 1099 | #endif |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1100 | spd = B115200; |
Stefan Weil | 45eea13 | 2009-10-26 16:10:10 +0100 | [diff] [blame] | 1101 | } while (0); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1102 | |
| 1103 | cfsetispeed(&tty, spd); |
| 1104 | cfsetospeed(&tty, spd); |
| 1105 | |
| 1106 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1107 | |INLCR|IGNCR|ICRNL|IXON); |
| 1108 | tty.c_oflag |= OPOST; |
| 1109 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
| 1110 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
| 1111 | switch(data_bits) { |
| 1112 | default: |
| 1113 | case 8: |
| 1114 | tty.c_cflag |= CS8; |
| 1115 | break; |
| 1116 | case 7: |
| 1117 | tty.c_cflag |= CS7; |
| 1118 | break; |
| 1119 | case 6: |
| 1120 | tty.c_cflag |= CS6; |
| 1121 | break; |
| 1122 | case 5: |
| 1123 | tty.c_cflag |= CS5; |
| 1124 | break; |
| 1125 | } |
| 1126 | switch(parity) { |
| 1127 | default: |
| 1128 | case 'N': |
| 1129 | break; |
| 1130 | case 'E': |
| 1131 | tty.c_cflag |= PARENB; |
| 1132 | break; |
| 1133 | case 'O': |
| 1134 | tty.c_cflag |= PARENB | PARODD; |
| 1135 | break; |
| 1136 | } |
| 1137 | if (stop_bits == 2) |
| 1138 | tty.c_cflag |= CSTOPB; |
| 1139 | |
| 1140 | tcsetattr (fd, TCSANOW, &tty); |
| 1141 | } |
| 1142 | |
| 1143 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1144 | { |
| 1145 | FDCharDriver *s = chr->opaque; |
| 1146 | |
| 1147 | switch(cmd) { |
| 1148 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1149 | { |
| 1150 | QEMUSerialSetParams *ssp = arg; |
| 1151 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
| 1152 | ssp->data_bits, ssp->stop_bits); |
| 1153 | } |
| 1154 | break; |
| 1155 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1156 | { |
| 1157 | int enable = *(int *)arg; |
| 1158 | if (enable) |
| 1159 | tcsendbreak(s->fd_in, 1); |
| 1160 | } |
| 1161 | break; |
| 1162 | case CHR_IOCTL_SERIAL_GET_TIOCM: |
| 1163 | { |
| 1164 | int sarg = 0; |
| 1165 | int *targ = (int *)arg; |
| 1166 | ioctl(s->fd_in, TIOCMGET, &sarg); |
| 1167 | *targ = 0; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1168 | if (sarg & TIOCM_CTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1169 | *targ |= CHR_TIOCM_CTS; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1170 | if (sarg & TIOCM_CAR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1171 | *targ |= CHR_TIOCM_CAR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1172 | if (sarg & TIOCM_DSR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1173 | *targ |= CHR_TIOCM_DSR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1174 | if (sarg & TIOCM_RI) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1175 | *targ |= CHR_TIOCM_RI; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1176 | if (sarg & TIOCM_DTR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1177 | *targ |= CHR_TIOCM_DTR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1178 | if (sarg & TIOCM_RTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1179 | *targ |= CHR_TIOCM_RTS; |
| 1180 | } |
| 1181 | break; |
| 1182 | case CHR_IOCTL_SERIAL_SET_TIOCM: |
| 1183 | { |
| 1184 | int sarg = *(int *)arg; |
| 1185 | int targ = 0; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1186 | ioctl(s->fd_in, TIOCMGET, &targ); |
| 1187 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
| 1188 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); |
| 1189 | if (sarg & CHR_TIOCM_CTS) |
| 1190 | targ |= TIOCM_CTS; |
| 1191 | if (sarg & CHR_TIOCM_CAR) |
| 1192 | targ |= TIOCM_CAR; |
| 1193 | if (sarg & CHR_TIOCM_DSR) |
| 1194 | targ |= TIOCM_DSR; |
| 1195 | if (sarg & CHR_TIOCM_RI) |
| 1196 | targ |= TIOCM_RI; |
| 1197 | if (sarg & CHR_TIOCM_DTR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1198 | targ |= TIOCM_DTR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1199 | if (sarg & CHR_TIOCM_RTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1200 | targ |= TIOCM_RTS; |
| 1201 | ioctl(s->fd_in, TIOCMSET, &targ); |
| 1202 | } |
| 1203 | break; |
| 1204 | default: |
| 1205 | return -ENOTSUP; |
| 1206 | } |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1210 | static void qemu_chr_close_tty(CharDriverState *chr) |
| 1211 | { |
| 1212 | FDCharDriver *s = chr->opaque; |
| 1213 | int fd = -1; |
| 1214 | |
| 1215 | if (s) { |
| 1216 | fd = s->fd_in; |
| 1217 | } |
| 1218 | |
| 1219 | fd_chr_close(chr); |
| 1220 | |
| 1221 | if (fd >= 0) { |
| 1222 | close(fd); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1226 | static int qemu_chr_open_tty(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1227 | { |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 1228 | const char *filename = qemu_opt_get(opts, "path"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1229 | CharDriverState *chr; |
| 1230 | int fd; |
| 1231 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1232 | TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK)); |
Evgeniy Dushistov | afc535a | 2010-01-28 21:44:46 +0300 | [diff] [blame] | 1233 | if (fd < 0) { |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1234 | return -errno; |
Evgeniy Dushistov | afc535a | 2010-01-28 21:44:46 +0300 | [diff] [blame] | 1235 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1236 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 1237 | chr = qemu_chr_open_fd(fd, fd); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1238 | chr->chr_ioctl = tty_serial_ioctl; |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1239 | chr->chr_close = qemu_chr_close_tty; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1240 | |
| 1241 | *_chr = chr; |
| 1242 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1243 | } |
| 1244 | #else /* ! __linux__ && ! __sun__ */ |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1245 | static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1246 | { |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1247 | return -ENOTSUP; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1248 | } |
| 1249 | #endif /* __linux__ || __sun__ */ |
| 1250 | |
| 1251 | #if defined(__linux__) |
| 1252 | typedef struct { |
| 1253 | int fd; |
| 1254 | int mode; |
| 1255 | } ParallelCharDriver; |
| 1256 | |
| 1257 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 1258 | { |
| 1259 | if (s->mode != mode) { |
| 1260 | int m = mode; |
| 1261 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 1262 | return 0; |
| 1263 | s->mode = mode; |
| 1264 | } |
| 1265 | return 1; |
| 1266 | } |
| 1267 | |
| 1268 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1269 | { |
| 1270 | ParallelCharDriver *drv = chr->opaque; |
| 1271 | int fd = drv->fd; |
| 1272 | uint8_t b; |
| 1273 | |
| 1274 | switch(cmd) { |
| 1275 | case CHR_IOCTL_PP_READ_DATA: |
| 1276 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1277 | return -ENOTSUP; |
| 1278 | *(uint8_t *)arg = b; |
| 1279 | break; |
| 1280 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1281 | b = *(uint8_t *)arg; |
| 1282 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1283 | return -ENOTSUP; |
| 1284 | break; |
| 1285 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1286 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1287 | return -ENOTSUP; |
| 1288 | /* Linux gives only the lowest bits, and no way to know data |
| 1289 | direction! For better compatibility set the fixed upper |
| 1290 | bits. */ |
| 1291 | *(uint8_t *)arg = b | 0xc0; |
| 1292 | break; |
| 1293 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1294 | b = *(uint8_t *)arg; |
| 1295 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1296 | return -ENOTSUP; |
| 1297 | break; |
| 1298 | case CHR_IOCTL_PP_READ_STATUS: |
| 1299 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1300 | return -ENOTSUP; |
| 1301 | *(uint8_t *)arg = b; |
| 1302 | break; |
| 1303 | case CHR_IOCTL_PP_DATA_DIR: |
| 1304 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
| 1305 | return -ENOTSUP; |
| 1306 | break; |
| 1307 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 1308 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1309 | struct ParallelIOArg *parg = arg; |
| 1310 | int n = read(fd, parg->buffer, parg->count); |
| 1311 | if (n != parg->count) { |
| 1312 | return -EIO; |
| 1313 | } |
| 1314 | } |
| 1315 | break; |
| 1316 | case CHR_IOCTL_PP_EPP_READ: |
| 1317 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1318 | struct ParallelIOArg *parg = arg; |
| 1319 | int n = read(fd, parg->buffer, parg->count); |
| 1320 | if (n != parg->count) { |
| 1321 | return -EIO; |
| 1322 | } |
| 1323 | } |
| 1324 | break; |
| 1325 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 1326 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1327 | struct ParallelIOArg *parg = arg; |
| 1328 | int n = write(fd, parg->buffer, parg->count); |
| 1329 | if (n != parg->count) { |
| 1330 | return -EIO; |
| 1331 | } |
| 1332 | } |
| 1333 | break; |
| 1334 | case CHR_IOCTL_PP_EPP_WRITE: |
| 1335 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1336 | struct ParallelIOArg *parg = arg; |
| 1337 | int n = write(fd, parg->buffer, parg->count); |
| 1338 | if (n != parg->count) { |
| 1339 | return -EIO; |
| 1340 | } |
| 1341 | } |
| 1342 | break; |
| 1343 | default: |
| 1344 | return -ENOTSUP; |
| 1345 | } |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static void pp_close(CharDriverState *chr) |
| 1350 | { |
| 1351 | ParallelCharDriver *drv = chr->opaque; |
| 1352 | int fd = drv->fd; |
| 1353 | |
| 1354 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 1355 | ioctl(fd, PPRELEASE); |
| 1356 | close(fd); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1357 | g_free(drv); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1358 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1361 | static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1362 | { |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 1363 | const char *filename = qemu_opt_get(opts, "path"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1364 | CharDriverState *chr; |
| 1365 | ParallelCharDriver *drv; |
| 1366 | int fd; |
| 1367 | |
| 1368 | TFR(fd = open(filename, O_RDWR)); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1369 | if (fd < 0) { |
| 1370 | return -errno; |
| 1371 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1372 | |
| 1373 | if (ioctl(fd, PPCLAIM) < 0) { |
| 1374 | close(fd); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1375 | return -errno; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1378 | drv = g_malloc0(sizeof(ParallelCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1379 | drv->fd = fd; |
| 1380 | drv->mode = IEEE1284_MODE_COMPAT; |
| 1381 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1382 | chr = g_malloc0(sizeof(CharDriverState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1383 | chr->chr_write = null_chr_write; |
| 1384 | chr->chr_ioctl = pp_ioctl; |
| 1385 | chr->chr_close = pp_close; |
| 1386 | chr->opaque = drv; |
| 1387 | |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 1388 | qemu_chr_generic_open(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1389 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1390 | *_chr = chr; |
| 1391 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1392 | } |
| 1393 | #endif /* __linux__ */ |
| 1394 | |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 1395 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1396 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1397 | { |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 1398 | int fd = (int)(intptr_t)chr->opaque; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1399 | uint8_t b; |
| 1400 | |
| 1401 | switch(cmd) { |
| 1402 | case CHR_IOCTL_PP_READ_DATA: |
| 1403 | if (ioctl(fd, PPIGDATA, &b) < 0) |
| 1404 | return -ENOTSUP; |
| 1405 | *(uint8_t *)arg = b; |
| 1406 | break; |
| 1407 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1408 | b = *(uint8_t *)arg; |
| 1409 | if (ioctl(fd, PPISDATA, &b) < 0) |
| 1410 | return -ENOTSUP; |
| 1411 | break; |
| 1412 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1413 | if (ioctl(fd, PPIGCTRL, &b) < 0) |
| 1414 | return -ENOTSUP; |
| 1415 | *(uint8_t *)arg = b; |
| 1416 | break; |
| 1417 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1418 | b = *(uint8_t *)arg; |
| 1419 | if (ioctl(fd, PPISCTRL, &b) < 0) |
| 1420 | return -ENOTSUP; |
| 1421 | break; |
| 1422 | case CHR_IOCTL_PP_READ_STATUS: |
| 1423 | if (ioctl(fd, PPIGSTATUS, &b) < 0) |
| 1424 | return -ENOTSUP; |
| 1425 | *(uint8_t *)arg = b; |
| 1426 | break; |
| 1427 | default: |
| 1428 | return -ENOTSUP; |
| 1429 | } |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1433 | static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr) |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +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"); |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1436 | CharDriverState *chr; |
| 1437 | int fd; |
| 1438 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1439 | fd = qemu_open(filename, O_RDWR); |
| 1440 | if (fd < 0) { |
| 1441 | return -errno; |
| 1442 | } |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1443 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1444 | chr = g_malloc0(sizeof(CharDriverState)); |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 1445 | chr->opaque = (void *)(intptr_t)fd; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1446 | chr->chr_write = null_chr_write; |
| 1447 | chr->chr_ioctl = pp_ioctl; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1448 | |
| 1449 | *_chr = chr; |
| 1450 | return 0; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1451 | } |
| 1452 | #endif |
| 1453 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1454 | #else /* _WIN32 */ |
| 1455 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1456 | static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS]; |
| 1457 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1458 | typedef struct { |
| 1459 | int max_size; |
| 1460 | HANDLE hcom, hrecv, hsend; |
| 1461 | OVERLAPPED orecv, osend; |
| 1462 | BOOL fpipe; |
| 1463 | DWORD len; |
| 1464 | } WinCharState; |
| 1465 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1466 | typedef struct { |
| 1467 | HANDLE hStdIn; |
| 1468 | HANDLE hInputReadyEvent; |
| 1469 | HANDLE hInputDoneEvent; |
| 1470 | HANDLE hInputThread; |
| 1471 | uint8_t win_stdio_buf; |
| 1472 | } WinStdioCharState; |
| 1473 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1474 | #define NSENDBUF 2048 |
| 1475 | #define NRECVBUF 2048 |
| 1476 | #define MAXCONNECT 1 |
| 1477 | #define NTIMEOUT 5000 |
| 1478 | |
| 1479 | static int win_chr_poll(void *opaque); |
| 1480 | static int win_chr_pipe_poll(void *opaque); |
| 1481 | |
| 1482 | static void win_chr_close(CharDriverState *chr) |
| 1483 | { |
| 1484 | WinCharState *s = chr->opaque; |
| 1485 | |
| 1486 | if (s->hsend) { |
| 1487 | CloseHandle(s->hsend); |
| 1488 | s->hsend = NULL; |
| 1489 | } |
| 1490 | if (s->hrecv) { |
| 1491 | CloseHandle(s->hrecv); |
| 1492 | s->hrecv = NULL; |
| 1493 | } |
| 1494 | if (s->hcom) { |
| 1495 | CloseHandle(s->hcom); |
| 1496 | s->hcom = NULL; |
| 1497 | } |
| 1498 | if (s->fpipe) |
| 1499 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
| 1500 | else |
| 1501 | qemu_del_polling_cb(win_chr_poll, chr); |
Amit Shah | 793cbfb | 2009-08-11 21:27:48 +0530 | [diff] [blame] | 1502 | |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1503 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static int win_chr_init(CharDriverState *chr, const char *filename) |
| 1507 | { |
| 1508 | WinCharState *s = chr->opaque; |
| 1509 | COMMCONFIG comcfg; |
| 1510 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 1511 | COMSTAT comstat; |
| 1512 | DWORD size; |
| 1513 | DWORD err; |
| 1514 | |
| 1515 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1516 | if (!s->hsend) { |
| 1517 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1518 | goto fail; |
| 1519 | } |
| 1520 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1521 | if (!s->hrecv) { |
| 1522 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1523 | goto fail; |
| 1524 | } |
| 1525 | |
| 1526 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 1527 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 1528 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 1529 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 1530 | s->hcom = NULL; |
| 1531 | goto fail; |
| 1532 | } |
| 1533 | |
| 1534 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 1535 | fprintf(stderr, "Failed SetupComm\n"); |
| 1536 | goto fail; |
| 1537 | } |
| 1538 | |
| 1539 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 1540 | size = sizeof(COMMCONFIG); |
| 1541 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 1542 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 1543 | CommConfigDialog(filename, NULL, &comcfg); |
| 1544 | |
| 1545 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 1546 | fprintf(stderr, "Failed SetCommState\n"); |
| 1547 | goto fail; |
| 1548 | } |
| 1549 | |
| 1550 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 1551 | fprintf(stderr, "Failed SetCommMask\n"); |
| 1552 | goto fail; |
| 1553 | } |
| 1554 | |
| 1555 | cto.ReadIntervalTimeout = MAXDWORD; |
| 1556 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 1557 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 1558 | goto fail; |
| 1559 | } |
| 1560 | |
| 1561 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 1562 | fprintf(stderr, "Failed ClearCommError\n"); |
| 1563 | goto fail; |
| 1564 | } |
| 1565 | qemu_add_polling_cb(win_chr_poll, chr); |
| 1566 | return 0; |
| 1567 | |
| 1568 | fail: |
| 1569 | win_chr_close(chr); |
| 1570 | return -1; |
| 1571 | } |
| 1572 | |
| 1573 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 1574 | { |
| 1575 | WinCharState *s = chr->opaque; |
| 1576 | DWORD len, ret, size, err; |
| 1577 | |
| 1578 | len = len1; |
| 1579 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 1580 | s->osend.hEvent = s->hsend; |
| 1581 | while (len > 0) { |
| 1582 | if (s->hsend) |
| 1583 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 1584 | else |
| 1585 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 1586 | if (!ret) { |
| 1587 | err = GetLastError(); |
| 1588 | if (err == ERROR_IO_PENDING) { |
| 1589 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 1590 | if (ret) { |
| 1591 | buf += size; |
| 1592 | len -= size; |
| 1593 | } else { |
| 1594 | break; |
| 1595 | } |
| 1596 | } else { |
| 1597 | break; |
| 1598 | } |
| 1599 | } else { |
| 1600 | buf += size; |
| 1601 | len -= size; |
| 1602 | } |
| 1603 | } |
| 1604 | return len1 - len; |
| 1605 | } |
| 1606 | |
| 1607 | static int win_chr_read_poll(CharDriverState *chr) |
| 1608 | { |
| 1609 | WinCharState *s = chr->opaque; |
| 1610 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 1611 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1612 | return s->max_size; |
| 1613 | } |
| 1614 | |
| 1615 | static void win_chr_readfile(CharDriverState *chr) |
| 1616 | { |
| 1617 | WinCharState *s = chr->opaque; |
| 1618 | int ret, err; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 1619 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1620 | DWORD size; |
| 1621 | |
| 1622 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 1623 | s->orecv.hEvent = s->hrecv; |
| 1624 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 1625 | if (!ret) { |
| 1626 | err = GetLastError(); |
| 1627 | if (err == ERROR_IO_PENDING) { |
| 1628 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | if (size > 0) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1633 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | static void win_chr_read(CharDriverState *chr) |
| 1638 | { |
| 1639 | WinCharState *s = chr->opaque; |
| 1640 | |
| 1641 | if (s->len > s->max_size) |
| 1642 | s->len = s->max_size; |
| 1643 | if (s->len == 0) |
| 1644 | return; |
| 1645 | |
| 1646 | win_chr_readfile(chr); |
| 1647 | } |
| 1648 | |
| 1649 | static int win_chr_poll(void *opaque) |
| 1650 | { |
| 1651 | CharDriverState *chr = opaque; |
| 1652 | WinCharState *s = chr->opaque; |
| 1653 | COMSTAT status; |
| 1654 | DWORD comerr; |
| 1655 | |
| 1656 | ClearCommError(s->hcom, &comerr, &status); |
| 1657 | if (status.cbInQue > 0) { |
| 1658 | s->len = status.cbInQue; |
| 1659 | win_chr_read_poll(chr); |
| 1660 | win_chr_read(chr); |
| 1661 | return 1; |
| 1662 | } |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1666 | static int qemu_chr_open_win(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1667 | { |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 1668 | const char *filename = qemu_opt_get(opts, "path"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1669 | CharDriverState *chr; |
| 1670 | WinCharState *s; |
| 1671 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1672 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1673 | s = g_malloc0(sizeof(WinCharState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1674 | chr->opaque = s; |
| 1675 | chr->chr_write = win_chr_write; |
| 1676 | chr->chr_close = win_chr_close; |
| 1677 | |
| 1678 | if (win_chr_init(chr, filename) < 0) { |
Stefan Weil | 2e02e18 | 2011-10-07 07:38:46 +0200 | [diff] [blame] | 1679 | g_free(s); |
| 1680 | g_free(chr); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1681 | return -EIO; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1682 | } |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 1683 | qemu_chr_generic_open(chr); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1684 | |
| 1685 | *_chr = chr; |
| 1686 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static int win_chr_pipe_poll(void *opaque) |
| 1690 | { |
| 1691 | CharDriverState *chr = opaque; |
| 1692 | WinCharState *s = chr->opaque; |
| 1693 | DWORD size; |
| 1694 | |
| 1695 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 1696 | if (size > 0) { |
| 1697 | s->len = size; |
| 1698 | win_chr_read_poll(chr); |
| 1699 | win_chr_read(chr); |
| 1700 | return 1; |
| 1701 | } |
| 1702 | return 0; |
| 1703 | } |
| 1704 | |
| 1705 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
| 1706 | { |
| 1707 | WinCharState *s = chr->opaque; |
| 1708 | OVERLAPPED ov; |
| 1709 | int ret; |
| 1710 | DWORD size; |
| 1711 | char openname[256]; |
| 1712 | |
| 1713 | s->fpipe = TRUE; |
| 1714 | |
| 1715 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1716 | if (!s->hsend) { |
| 1717 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1718 | goto fail; |
| 1719 | } |
| 1720 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1721 | if (!s->hrecv) { |
| 1722 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1723 | goto fail; |
| 1724 | } |
| 1725 | |
| 1726 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 1727 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 1728 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 1729 | PIPE_WAIT, |
| 1730 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 1731 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 1732 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 1733 | s->hcom = NULL; |
| 1734 | goto fail; |
| 1735 | } |
| 1736 | |
| 1737 | ZeroMemory(&ov, sizeof(ov)); |
| 1738 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1739 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 1740 | if (ret) { |
| 1741 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 1742 | goto fail; |
| 1743 | } |
| 1744 | |
| 1745 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 1746 | if (!ret) { |
| 1747 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 1748 | if (ov.hEvent) { |
| 1749 | CloseHandle(ov.hEvent); |
| 1750 | ov.hEvent = NULL; |
| 1751 | } |
| 1752 | goto fail; |
| 1753 | } |
| 1754 | |
| 1755 | if (ov.hEvent) { |
| 1756 | CloseHandle(ov.hEvent); |
| 1757 | ov.hEvent = NULL; |
| 1758 | } |
| 1759 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
| 1760 | return 0; |
| 1761 | |
| 1762 | fail: |
| 1763 | win_chr_close(chr); |
| 1764 | return -1; |
| 1765 | } |
| 1766 | |
| 1767 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1768 | static int qemu_chr_open_win_pipe(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1769 | { |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 1770 | const char *filename = qemu_opt_get(opts, "path"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1771 | CharDriverState *chr; |
| 1772 | WinCharState *s; |
| 1773 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1774 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1775 | s = g_malloc0(sizeof(WinCharState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1776 | chr->opaque = s; |
| 1777 | chr->chr_write = win_chr_write; |
| 1778 | chr->chr_close = win_chr_close; |
| 1779 | |
| 1780 | if (win_chr_pipe_init(chr, filename) < 0) { |
Stefan Weil | 2e02e18 | 2011-10-07 07:38:46 +0200 | [diff] [blame] | 1781 | g_free(s); |
| 1782 | g_free(chr); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1783 | return -EIO; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1784 | } |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 1785 | qemu_chr_generic_open(chr); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1786 | |
| 1787 | *_chr = chr; |
| 1788 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Blue Swirl | aad04cd | 2011-07-23 19:26:08 +0000 | [diff] [blame] | 1791 | static int qemu_chr_open_win_file(HANDLE fd_out, CharDriverState **pchr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1792 | { |
| 1793 | CharDriverState *chr; |
| 1794 | WinCharState *s; |
| 1795 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1796 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1797 | s = g_malloc0(sizeof(WinCharState)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1798 | s->hcom = fd_out; |
| 1799 | chr->opaque = s; |
| 1800 | chr->chr_write = win_chr_write; |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 1801 | qemu_chr_generic_open(chr); |
Blue Swirl | aad04cd | 2011-07-23 19:26:08 +0000 | [diff] [blame] | 1802 | *pchr = chr; |
| 1803 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Blue Swirl | aad04cd | 2011-07-23 19:26:08 +0000 | [diff] [blame] | 1806 | static int qemu_chr_open_win_con(QemuOpts *opts, CharDriverState **chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1807 | { |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1808 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE), chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1811 | static int qemu_chr_open_win_file_out(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1812 | { |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 1813 | const char *file_out = qemu_opt_get(opts, "path"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1814 | HANDLE fd_out; |
| 1815 | |
| 1816 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 1817 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1818 | if (fd_out == INVALID_HANDLE_VALUE) { |
| 1819 | return -EIO; |
| 1820 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1821 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 1822 | return qemu_chr_open_win_file(fd_out, _chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1823 | } |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1824 | |
| 1825 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1826 | { |
| 1827 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 1828 | DWORD dwSize; |
| 1829 | int len1; |
| 1830 | |
| 1831 | len1 = len; |
| 1832 | |
| 1833 | while (len1 > 0) { |
| 1834 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { |
| 1835 | break; |
| 1836 | } |
| 1837 | buf += dwSize; |
| 1838 | len1 -= dwSize; |
| 1839 | } |
| 1840 | |
| 1841 | return len - len1; |
| 1842 | } |
| 1843 | |
| 1844 | static void win_stdio_wait_func(void *opaque) |
| 1845 | { |
| 1846 | CharDriverState *chr = opaque; |
| 1847 | WinStdioCharState *stdio = chr->opaque; |
| 1848 | INPUT_RECORD buf[4]; |
| 1849 | int ret; |
| 1850 | DWORD dwSize; |
| 1851 | int i; |
| 1852 | |
| 1853 | ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf), |
| 1854 | &dwSize); |
| 1855 | |
| 1856 | if (!ret) { |
| 1857 | /* Avoid error storm */ |
| 1858 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); |
| 1859 | return; |
| 1860 | } |
| 1861 | |
| 1862 | for (i = 0; i < dwSize; i++) { |
| 1863 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; |
| 1864 | |
| 1865 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { |
| 1866 | int j; |
| 1867 | if (kev->uChar.AsciiChar != 0) { |
| 1868 | for (j = 0; j < kev->wRepeatCount; j++) { |
| 1869 | if (qemu_chr_be_can_write(chr)) { |
| 1870 | uint8_t c = kev->uChar.AsciiChar; |
| 1871 | qemu_chr_be_write(chr, &c, 1); |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | static DWORD WINAPI win_stdio_thread(LPVOID param) |
| 1880 | { |
| 1881 | CharDriverState *chr = param; |
| 1882 | WinStdioCharState *stdio = chr->opaque; |
| 1883 | int ret; |
| 1884 | DWORD dwSize; |
| 1885 | |
| 1886 | while (1) { |
| 1887 | |
| 1888 | /* Wait for one byte */ |
| 1889 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); |
| 1890 | |
| 1891 | /* Exit in case of error, continue if nothing read */ |
| 1892 | if (!ret) { |
| 1893 | break; |
| 1894 | } |
| 1895 | if (!dwSize) { |
| 1896 | continue; |
| 1897 | } |
| 1898 | |
| 1899 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ |
| 1900 | if (stdio->win_stdio_buf == '\r') { |
| 1901 | continue; |
| 1902 | } |
| 1903 | |
| 1904 | /* Signal the main thread and wait until the byte was eaten */ |
| 1905 | if (!SetEvent(stdio->hInputReadyEvent)) { |
| 1906 | break; |
| 1907 | } |
| 1908 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) |
| 1909 | != WAIT_OBJECT_0) { |
| 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
| 1918 | static void win_stdio_thread_wait_func(void *opaque) |
| 1919 | { |
| 1920 | CharDriverState *chr = opaque; |
| 1921 | WinStdioCharState *stdio = chr->opaque; |
| 1922 | |
| 1923 | if (qemu_chr_be_can_write(chr)) { |
| 1924 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); |
| 1925 | } |
| 1926 | |
| 1927 | SetEvent(stdio->hInputDoneEvent); |
| 1928 | } |
| 1929 | |
| 1930 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) |
| 1931 | { |
| 1932 | WinStdioCharState *stdio = chr->opaque; |
| 1933 | DWORD dwMode = 0; |
| 1934 | |
| 1935 | GetConsoleMode(stdio->hStdIn, &dwMode); |
| 1936 | |
| 1937 | if (echo) { |
| 1938 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); |
| 1939 | } else { |
| 1940 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | static void win_stdio_close(CharDriverState *chr) |
| 1945 | { |
| 1946 | WinStdioCharState *stdio = chr->opaque; |
| 1947 | |
| 1948 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { |
| 1949 | CloseHandle(stdio->hInputReadyEvent); |
| 1950 | } |
| 1951 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { |
| 1952 | CloseHandle(stdio->hInputDoneEvent); |
| 1953 | } |
| 1954 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { |
| 1955 | TerminateThread(stdio->hInputThread, 0); |
| 1956 | } |
| 1957 | |
| 1958 | g_free(chr->opaque); |
| 1959 | g_free(chr); |
| 1960 | stdio_nb_clients--; |
| 1961 | } |
| 1962 | |
| 1963 | static int qemu_chr_open_win_stdio(QemuOpts *opts, CharDriverState **_chr) |
| 1964 | { |
| 1965 | CharDriverState *chr; |
| 1966 | WinStdioCharState *stdio; |
| 1967 | DWORD dwMode; |
| 1968 | int is_console = 0; |
| 1969 | |
| 1970 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS |
| 1971 | || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) { |
| 1972 | return -EIO; |
| 1973 | } |
| 1974 | |
| 1975 | chr = g_malloc0(sizeof(CharDriverState)); |
| 1976 | stdio = g_malloc0(sizeof(WinStdioCharState)); |
| 1977 | |
| 1978 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); |
| 1979 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { |
| 1980 | fprintf(stderr, "cannot open stdio: invalid handle\n"); |
| 1981 | exit(1); |
| 1982 | } |
| 1983 | |
| 1984 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; |
| 1985 | |
| 1986 | chr->opaque = stdio; |
| 1987 | chr->chr_write = win_stdio_write; |
| 1988 | chr->chr_close = win_stdio_close; |
| 1989 | |
| 1990 | if (stdio_nb_clients == 0) { |
| 1991 | if (is_console) { |
| 1992 | if (qemu_add_wait_object(stdio->hStdIn, |
| 1993 | win_stdio_wait_func, chr)) { |
| 1994 | fprintf(stderr, "qemu_add_wait_object: failed\n"); |
| 1995 | } |
| 1996 | } else { |
| 1997 | DWORD dwId; |
| 1998 | |
| 1999 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 2000 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 2001 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, |
| 2002 | chr, 0, &dwId); |
| 2003 | |
| 2004 | if (stdio->hInputThread == INVALID_HANDLE_VALUE |
| 2005 | || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE |
| 2006 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { |
| 2007 | fprintf(stderr, "cannot create stdio thread or event\n"); |
| 2008 | exit(1); |
| 2009 | } |
| 2010 | if (qemu_add_wait_object(stdio->hInputReadyEvent, |
| 2011 | win_stdio_thread_wait_func, chr)) { |
| 2012 | fprintf(stderr, "qemu_add_wait_object: failed\n"); |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | dwMode |= ENABLE_LINE_INPUT; |
| 2018 | |
| 2019 | stdio_clients[stdio_nb_clients++] = chr; |
| 2020 | if (stdio_nb_clients == 1 && is_console) { |
| 2021 | /* set the terminal in raw mode */ |
| 2022 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ |
| 2023 | dwMode |= ENABLE_PROCESSED_INPUT; |
| 2024 | } |
| 2025 | |
| 2026 | SetConsoleMode(stdio->hStdIn, dwMode); |
| 2027 | |
| 2028 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; |
| 2029 | qemu_chr_fe_set_echo(chr, false); |
| 2030 | |
| 2031 | *_chr = chr; |
| 2032 | |
| 2033 | return 0; |
| 2034 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2035 | #endif /* !_WIN32 */ |
| 2036 | |
| 2037 | /***********************************************************/ |
| 2038 | /* UDP Net console */ |
| 2039 | |
| 2040 | typedef struct { |
| 2041 | int fd; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2042 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2043 | int bufcnt; |
| 2044 | int bufptr; |
| 2045 | int max_size; |
| 2046 | } NetCharDriver; |
| 2047 | |
| 2048 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2049 | { |
| 2050 | NetCharDriver *s = chr->opaque; |
| 2051 | |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2052 | return send(s->fd, (const void *)buf, len, 0); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static int udp_chr_read_poll(void *opaque) |
| 2056 | { |
| 2057 | CharDriverState *chr = opaque; |
| 2058 | NetCharDriver *s = chr->opaque; |
| 2059 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2060 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2061 | |
| 2062 | /* If there were any stray characters in the queue process them |
| 2063 | * first |
| 2064 | */ |
| 2065 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2066 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2067 | s->bufptr++; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2068 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2069 | } |
| 2070 | return s->max_size; |
| 2071 | } |
| 2072 | |
| 2073 | static void udp_chr_read(void *opaque) |
| 2074 | { |
| 2075 | CharDriverState *chr = opaque; |
| 2076 | NetCharDriver *s = chr->opaque; |
| 2077 | |
| 2078 | if (s->max_size == 0) |
| 2079 | return; |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 2080 | s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2081 | s->bufptr = s->bufcnt; |
| 2082 | if (s->bufcnt <= 0) |
| 2083 | return; |
| 2084 | |
| 2085 | s->bufptr = 0; |
| 2086 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2087 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2088 | s->bufptr++; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2089 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | static void udp_chr_update_read_handler(CharDriverState *chr) |
| 2094 | { |
| 2095 | NetCharDriver *s = chr->opaque; |
| 2096 | |
| 2097 | if (s->fd >= 0) { |
| 2098 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
| 2099 | udp_chr_read, NULL, chr); |
| 2100 | } |
| 2101 | } |
| 2102 | |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2103 | static void udp_chr_close(CharDriverState *chr) |
| 2104 | { |
| 2105 | NetCharDriver *s = chr->opaque; |
| 2106 | if (s->fd >= 0) { |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2107 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2108 | closesocket(s->fd); |
| 2109 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2110 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2111 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2114 | static int qemu_chr_open_udp(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2115 | { |
| 2116 | CharDriverState *chr = NULL; |
| 2117 | NetCharDriver *s = NULL; |
| 2118 | int fd = -1; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2119 | int ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2120 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2121 | chr = g_malloc0(sizeof(CharDriverState)); |
| 2122 | s = g_malloc0(sizeof(NetCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2123 | |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2124 | fd = inet_dgram_opts(opts); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2125 | if (fd < 0) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2126 | fprintf(stderr, "inet_dgram_opts failed\n"); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2127 | ret = -errno; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2128 | goto return_err; |
| 2129 | } |
| 2130 | |
| 2131 | s->fd = fd; |
| 2132 | s->bufcnt = 0; |
| 2133 | s->bufptr = 0; |
| 2134 | chr->opaque = s; |
| 2135 | chr->chr_write = udp_chr_write; |
| 2136 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2137 | chr->chr_close = udp_chr_close; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2138 | |
| 2139 | *_chr = chr; |
| 2140 | return 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2141 | |
| 2142 | return_err: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2143 | g_free(chr); |
| 2144 | g_free(s); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2145 | if (fd >= 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2146 | closesocket(fd); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2147 | } |
| 2148 | return ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /***********************************************************/ |
| 2152 | /* TCP Net console */ |
| 2153 | |
| 2154 | typedef struct { |
| 2155 | int fd, listen_fd; |
| 2156 | int connected; |
| 2157 | int max_size; |
| 2158 | int do_telnetopt; |
| 2159 | int do_nodelay; |
| 2160 | int is_unix; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2161 | int msgfd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2162 | } TCPCharDriver; |
| 2163 | |
| 2164 | static void tcp_chr_accept(void *opaque); |
| 2165 | |
| 2166 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2167 | { |
| 2168 | TCPCharDriver *s = chr->opaque; |
| 2169 | if (s->connected) { |
| 2170 | return send_all(s->fd, buf, len); |
| 2171 | } else { |
| 2172 | /* XXX: indicate an error ? */ |
| 2173 | return len; |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | static int tcp_chr_read_poll(void *opaque) |
| 2178 | { |
| 2179 | CharDriverState *chr = opaque; |
| 2180 | TCPCharDriver *s = chr->opaque; |
| 2181 | if (!s->connected) |
| 2182 | return 0; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2183 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2184 | return s->max_size; |
| 2185 | } |
| 2186 | |
| 2187 | #define IAC 255 |
| 2188 | #define IAC_BREAK 243 |
| 2189 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2190 | TCPCharDriver *s, |
| 2191 | uint8_t *buf, int *size) |
| 2192 | { |
| 2193 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2194 | * char mode with no echo. All IAC options will be removed from |
| 2195 | * the buf and the do_telnetopt variable will be used to track the |
| 2196 | * state of the width of the IAC information. |
| 2197 | * |
| 2198 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2199 | * "IAC BREAK" command and the double IAC. |
| 2200 | */ |
| 2201 | |
| 2202 | int i; |
| 2203 | int j = 0; |
| 2204 | |
| 2205 | for (i = 0; i < *size; i++) { |
| 2206 | if (s->do_telnetopt > 1) { |
| 2207 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2208 | /* Double IAC means send an IAC */ |
| 2209 | if (j != i) |
| 2210 | buf[j] = buf[i]; |
| 2211 | j++; |
| 2212 | s->do_telnetopt = 1; |
| 2213 | } else { |
| 2214 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2215 | /* Handle IAC break commands by sending a serial break */ |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2216 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2217 | s->do_telnetopt++; |
| 2218 | } |
| 2219 | s->do_telnetopt++; |
| 2220 | } |
| 2221 | if (s->do_telnetopt >= 4) { |
| 2222 | s->do_telnetopt = 1; |
| 2223 | } |
| 2224 | } else { |
| 2225 | if ((unsigned char)buf[i] == IAC) { |
| 2226 | s->do_telnetopt = 2; |
| 2227 | } else { |
| 2228 | if (j != i) |
| 2229 | buf[j] = buf[i]; |
| 2230 | j++; |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | *size = j; |
| 2235 | } |
| 2236 | |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2237 | static int tcp_get_msgfd(CharDriverState *chr) |
| 2238 | { |
| 2239 | TCPCharDriver *s = chr->opaque; |
Paolo Bonzini | e53f27b | 2010-04-16 17:25:23 +0200 | [diff] [blame] | 2240 | int fd = s->msgfd; |
| 2241 | s->msgfd = -1; |
| 2242 | return fd; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
Anthony Liguori | 73bcc2a | 2009-07-27 14:55:25 -0500 | [diff] [blame] | 2245 | #ifndef _WIN32 |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2246 | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
| 2247 | { |
| 2248 | TCPCharDriver *s = chr->opaque; |
| 2249 | struct cmsghdr *cmsg; |
| 2250 | |
| 2251 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 2252 | int fd; |
| 2253 | |
| 2254 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || |
| 2255 | cmsg->cmsg_level != SOL_SOCKET || |
| 2256 | cmsg->cmsg_type != SCM_RIGHTS) |
| 2257 | continue; |
| 2258 | |
| 2259 | fd = *((int *)CMSG_DATA(cmsg)); |
| 2260 | if (fd < 0) |
| 2261 | continue; |
| 2262 | |
| 2263 | if (s->msgfd != -1) |
| 2264 | close(s->msgfd); |
| 2265 | s->msgfd = fd; |
| 2266 | } |
| 2267 | } |
| 2268 | |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2269 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
| 2270 | { |
| 2271 | TCPCharDriver *s = chr->opaque; |
Blue Swirl | 7cba04f | 2009-08-01 10:13:20 +0000 | [diff] [blame] | 2272 | struct msghdr msg = { NULL, }; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2273 | struct iovec iov[1]; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2274 | union { |
| 2275 | struct cmsghdr cmsg; |
| 2276 | char control[CMSG_SPACE(sizeof(int))]; |
| 2277 | } msg_control; |
| 2278 | ssize_t ret; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2279 | |
| 2280 | iov[0].iov_base = buf; |
| 2281 | iov[0].iov_len = len; |
| 2282 | |
| 2283 | msg.msg_iov = iov; |
| 2284 | msg.msg_iovlen = 1; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2285 | msg.msg_control = &msg_control; |
| 2286 | msg.msg_controllen = sizeof(msg_control); |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2287 | |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2288 | ret = recvmsg(s->fd, &msg, 0); |
| 2289 | if (ret > 0 && s->is_unix) |
| 2290 | unix_process_msgfd(chr, &msg); |
| 2291 | |
| 2292 | return ret; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2293 | } |
| 2294 | #else |
| 2295 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
| 2296 | { |
| 2297 | TCPCharDriver *s = chr->opaque; |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 2298 | return qemu_recv(s->fd, buf, len, 0); |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2299 | } |
| 2300 | #endif |
| 2301 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2302 | static void tcp_chr_read(void *opaque) |
| 2303 | { |
| 2304 | CharDriverState *chr = opaque; |
| 2305 | TCPCharDriver *s = chr->opaque; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2306 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2307 | int len, size; |
| 2308 | |
| 2309 | if (!s->connected || s->max_size <= 0) |
| 2310 | return; |
| 2311 | len = sizeof(buf); |
| 2312 | if (len > s->max_size) |
| 2313 | len = s->max_size; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2314 | size = tcp_chr_recv(chr, (void *)buf, len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2315 | if (size == 0) { |
| 2316 | /* connection closed */ |
| 2317 | s->connected = 0; |
| 2318 | if (s->listen_fd >= 0) { |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2319 | qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2320 | } |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2321 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2322 | closesocket(s->fd); |
| 2323 | s->fd = -1; |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2324 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2325 | } else if (size > 0) { |
| 2326 | if (s->do_telnetopt) |
| 2327 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2328 | if (size > 0) |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2329 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
Blue Swirl | 68c18d1 | 2010-08-15 09:46:24 +0000 | [diff] [blame] | 2333 | #ifndef _WIN32 |
| 2334 | CharDriverState *qemu_chr_open_eventfd(int eventfd) |
| 2335 | { |
Cam Macdonell | 6cbf4c8 | 2010-07-27 10:54:13 -0600 | [diff] [blame] | 2336 | return qemu_chr_open_fd(eventfd, eventfd); |
Cam Macdonell | 6cbf4c8 | 2010-07-27 10:54:13 -0600 | [diff] [blame] | 2337 | } |
Blue Swirl | 68c18d1 | 2010-08-15 09:46:24 +0000 | [diff] [blame] | 2338 | #endif |
Cam Macdonell | 6cbf4c8 | 2010-07-27 10:54:13 -0600 | [diff] [blame] | 2339 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2340 | static void tcp_chr_connect(void *opaque) |
| 2341 | { |
| 2342 | CharDriverState *chr = opaque; |
| 2343 | TCPCharDriver *s = chr->opaque; |
| 2344 | |
| 2345 | s->connected = 1; |
| 2346 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
| 2347 | tcp_chr_read, NULL, chr); |
Amit Shah | 127338e | 2009-11-03 19:59:56 +0530 | [diff] [blame] | 2348 | qemu_chr_generic_open(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 2352 | static void tcp_chr_telnet_init(int fd) |
| 2353 | { |
| 2354 | char buf[3]; |
| 2355 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 2356 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 2357 | send(fd, (char *)buf, 3, 0); |
| 2358 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 2359 | send(fd, (char *)buf, 3, 0); |
| 2360 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 2361 | send(fd, (char *)buf, 3, 0); |
| 2362 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 2363 | send(fd, (char *)buf, 3, 0); |
| 2364 | } |
| 2365 | |
| 2366 | static void socket_set_nodelay(int fd) |
| 2367 | { |
| 2368 | int val = 1; |
| 2369 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
| 2370 | } |
| 2371 | |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2372 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
| 2373 | { |
| 2374 | TCPCharDriver *s = chr->opaque; |
| 2375 | if (s->fd != -1) |
| 2376 | return -1; |
| 2377 | |
| 2378 | socket_set_nonblock(fd); |
| 2379 | if (s->do_nodelay) |
| 2380 | socket_set_nodelay(fd); |
| 2381 | s->fd = fd; |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2382 | qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL); |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2383 | tcp_chr_connect(chr); |
| 2384 | |
| 2385 | return 0; |
| 2386 | } |
| 2387 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2388 | static void tcp_chr_accept(void *opaque) |
| 2389 | { |
| 2390 | CharDriverState *chr = opaque; |
| 2391 | TCPCharDriver *s = chr->opaque; |
| 2392 | struct sockaddr_in saddr; |
| 2393 | #ifndef _WIN32 |
| 2394 | struct sockaddr_un uaddr; |
| 2395 | #endif |
| 2396 | struct sockaddr *addr; |
| 2397 | socklen_t len; |
| 2398 | int fd; |
| 2399 | |
| 2400 | for(;;) { |
| 2401 | #ifndef _WIN32 |
| 2402 | if (s->is_unix) { |
| 2403 | len = sizeof(uaddr); |
| 2404 | addr = (struct sockaddr *)&uaddr; |
| 2405 | } else |
| 2406 | #endif |
| 2407 | { |
| 2408 | len = sizeof(saddr); |
| 2409 | addr = (struct sockaddr *)&saddr; |
| 2410 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 2411 | fd = qemu_accept(s->listen_fd, addr, &len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2412 | if (fd < 0 && errno != EINTR) { |
| 2413 | return; |
| 2414 | } else if (fd >= 0) { |
| 2415 | if (s->do_telnetopt) |
| 2416 | tcp_chr_telnet_init(fd); |
| 2417 | break; |
| 2418 | } |
| 2419 | } |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2420 | if (tcp_chr_add_client(chr, fd) < 0) |
| 2421 | close(fd); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | static void tcp_chr_close(CharDriverState *chr) |
| 2425 | { |
| 2426 | TCPCharDriver *s = chr->opaque; |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2427 | if (s->fd >= 0) { |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2428 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2429 | closesocket(s->fd); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2430 | } |
| 2431 | if (s->listen_fd >= 0) { |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2432 | qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2433 | closesocket(s->listen_fd); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2434 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2435 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2436 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2439 | static int qemu_chr_open_socket(QemuOpts *opts, CharDriverState **_chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2440 | { |
| 2441 | CharDriverState *chr = NULL; |
| 2442 | TCPCharDriver *s = NULL; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2443 | int fd = -1; |
| 2444 | int is_listen; |
| 2445 | int is_waitconnect; |
| 2446 | int do_nodelay; |
| 2447 | int is_unix; |
| 2448 | int is_telnet; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2449 | int ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2450 | |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2451 | is_listen = qemu_opt_get_bool(opts, "server", 0); |
| 2452 | is_waitconnect = qemu_opt_get_bool(opts, "wait", 1); |
| 2453 | is_telnet = qemu_opt_get_bool(opts, "telnet", 0); |
| 2454 | do_nodelay = !qemu_opt_get_bool(opts, "delay", 1); |
| 2455 | is_unix = qemu_opt_get(opts, "path") != NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2456 | if (!is_listen) |
| 2457 | is_waitconnect = 0; |
| 2458 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2459 | chr = g_malloc0(sizeof(CharDriverState)); |
| 2460 | s = g_malloc0(sizeof(TCPCharDriver)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2461 | |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2462 | if (is_unix) { |
| 2463 | if (is_listen) { |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2464 | fd = unix_listen_opts(opts); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2465 | } else { |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2466 | fd = unix_connect_opts(opts); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2467 | } |
| 2468 | } else { |
| 2469 | if (is_listen) { |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2470 | fd = inet_listen_opts(opts, 0); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2471 | } else { |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2472 | fd = inet_connect_opts(opts); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2475 | if (fd < 0) { |
| 2476 | ret = -errno; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2477 | goto fail; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2478 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2479 | |
| 2480 | if (!is_waitconnect) |
| 2481 | socket_set_nonblock(fd); |
| 2482 | |
| 2483 | s->connected = 0; |
| 2484 | s->fd = -1; |
| 2485 | s->listen_fd = -1; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2486 | s->msgfd = -1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2487 | s->is_unix = is_unix; |
| 2488 | s->do_nodelay = do_nodelay && !is_unix; |
| 2489 | |
| 2490 | chr->opaque = s; |
| 2491 | chr->chr_write = tcp_chr_write; |
| 2492 | chr->chr_close = tcp_chr_close; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2493 | chr->get_msgfd = tcp_get_msgfd; |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 2494 | chr->chr_add_client = tcp_chr_add_client; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2495 | |
| 2496 | if (is_listen) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2497 | s->listen_fd = fd; |
Marcelo Tosatti | 069c159 | 2011-09-16 18:19:55 -0300 | [diff] [blame] | 2498 | qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2499 | if (is_telnet) |
| 2500 | s->do_telnetopt = 1; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2501 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2502 | } else { |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2503 | s->connected = 1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2504 | s->fd = fd; |
| 2505 | socket_set_nodelay(fd); |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2506 | tcp_chr_connect(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2509 | /* for "info chardev" monitor command */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2510 | chr->filename = g_malloc(256); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2511 | if (is_unix) { |
| 2512 | snprintf(chr->filename, 256, "unix:%s%s", |
| 2513 | qemu_opt_get(opts, "path"), |
| 2514 | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); |
| 2515 | } else if (is_telnet) { |
| 2516 | snprintf(chr->filename, 256, "telnet:%s:%s%s", |
| 2517 | qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"), |
| 2518 | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); |
| 2519 | } else { |
| 2520 | snprintf(chr->filename, 256, "tcp:%s:%s%s", |
| 2521 | qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"), |
| 2522 | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); |
| 2523 | } |
| 2524 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2525 | if (is_listen && is_waitconnect) { |
aliguori | f07b600 | 2008-11-11 20:54:09 +0000 | [diff] [blame] | 2526 | printf("QEMU waiting for connection on: %s\n", |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2527 | chr->filename); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2528 | tcp_chr_accept(chr); |
| 2529 | socket_set_nonblock(s->listen_fd); |
| 2530 | } |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2531 | |
| 2532 | *_chr = chr; |
| 2533 | return 0; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2534 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2535 | fail: |
| 2536 | if (fd >= 0) |
| 2537 | closesocket(fd); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2538 | g_free(s); |
| 2539 | g_free(chr); |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2540 | return ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Luiz Capitulino | 999bd67 | 2010-10-22 16:09:05 -0200 | [diff] [blame] | 2543 | /***********************************************************/ |
| 2544 | /* Memory chardev */ |
| 2545 | typedef struct { |
| 2546 | size_t outbuf_size; |
| 2547 | size_t outbuf_capacity; |
| 2548 | uint8_t *outbuf; |
| 2549 | } MemoryDriver; |
| 2550 | |
| 2551 | static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2552 | { |
| 2553 | MemoryDriver *d = chr->opaque; |
| 2554 | |
| 2555 | /* TODO: the QString implementation has the same code, we should |
| 2556 | * introduce a generic way to do this in cutils.c */ |
| 2557 | if (d->outbuf_capacity < d->outbuf_size + len) { |
| 2558 | /* grow outbuf */ |
| 2559 | d->outbuf_capacity += len; |
| 2560 | d->outbuf_capacity *= 2; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2561 | d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity); |
Luiz Capitulino | 999bd67 | 2010-10-22 16:09:05 -0200 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | memcpy(d->outbuf + d->outbuf_size, buf, len); |
| 2565 | d->outbuf_size += len; |
| 2566 | |
| 2567 | return len; |
| 2568 | } |
| 2569 | |
| 2570 | void qemu_chr_init_mem(CharDriverState *chr) |
| 2571 | { |
| 2572 | MemoryDriver *d; |
| 2573 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2574 | d = g_malloc(sizeof(*d)); |
Luiz Capitulino | 999bd67 | 2010-10-22 16:09:05 -0200 | [diff] [blame] | 2575 | d->outbuf_size = 0; |
| 2576 | d->outbuf_capacity = 4096; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2577 | d->outbuf = g_malloc0(d->outbuf_capacity); |
Luiz Capitulino | 999bd67 | 2010-10-22 16:09:05 -0200 | [diff] [blame] | 2578 | |
| 2579 | memset(chr, 0, sizeof(*chr)); |
| 2580 | chr->opaque = d; |
| 2581 | chr->chr_write = mem_chr_write; |
| 2582 | } |
| 2583 | |
| 2584 | QString *qemu_chr_mem_to_qs(CharDriverState *chr) |
| 2585 | { |
| 2586 | MemoryDriver *d = chr->opaque; |
| 2587 | return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1); |
| 2588 | } |
| 2589 | |
Anthony Liguori | 70f24fb | 2011-08-15 11:17:38 -0500 | [diff] [blame] | 2590 | /* NOTE: this driver can not be closed with qemu_chr_delete()! */ |
Luiz Capitulino | 999bd67 | 2010-10-22 16:09:05 -0200 | [diff] [blame] | 2591 | void qemu_chr_close_mem(CharDriverState *chr) |
| 2592 | { |
| 2593 | MemoryDriver *d = chr->opaque; |
| 2594 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2595 | g_free(d->outbuf); |
| 2596 | g_free(chr->opaque); |
Luiz Capitulino | 999bd67 | 2010-10-22 16:09:05 -0200 | [diff] [blame] | 2597 | chr->opaque = NULL; |
| 2598 | chr->chr_write = NULL; |
| 2599 | } |
| 2600 | |
| 2601 | size_t qemu_chr_mem_osize(const CharDriverState *chr) |
| 2602 | { |
| 2603 | const MemoryDriver *d = chr->opaque; |
| 2604 | return d->outbuf_size; |
| 2605 | } |
| 2606 | |
Gerd Hoffmann | 3352163 | 2009-12-08 13:11:49 +0100 | [diff] [blame] | 2607 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2608 | { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2609 | char host[65], port[33], width[8], height[8]; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2610 | int pos; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 2611 | const char *p; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2612 | QemuOpts *opts; |
| 2613 | |
Gerd Hoffmann | 3329f07 | 2010-08-20 13:52:01 +0200 | [diff] [blame] | 2614 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2615 | if (NULL == opts) |
| 2616 | return NULL; |
| 2617 | |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 2618 | if (strstart(filename, "mon:", &p)) { |
| 2619 | filename = p; |
| 2620 | qemu_opt_set(opts, "mux", "on"); |
| 2621 | } |
| 2622 | |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 2623 | if (strcmp(filename, "null") == 0 || |
| 2624 | strcmp(filename, "pty") == 0 || |
| 2625 | strcmp(filename, "msmouse") == 0 || |
Gerd Hoffmann | dc1c21e | 2009-09-10 10:58:46 +0200 | [diff] [blame] | 2626 | strcmp(filename, "braille") == 0 || |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 2627 | strcmp(filename, "stdio") == 0) { |
Gerd Hoffmann | 4490dad | 2009-09-10 10:58:43 +0200 | [diff] [blame] | 2628 | qemu_opt_set(opts, "backend", filename); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2629 | return opts; |
| 2630 | } |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2631 | if (strstart(filename, "vc", &p)) { |
| 2632 | qemu_opt_set(opts, "backend", "vc"); |
| 2633 | if (*p == ':') { |
| 2634 | if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) { |
| 2635 | /* pixels */ |
| 2636 | qemu_opt_set(opts, "width", width); |
| 2637 | qemu_opt_set(opts, "height", height); |
| 2638 | } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) { |
| 2639 | /* chars */ |
| 2640 | qemu_opt_set(opts, "cols", width); |
| 2641 | qemu_opt_set(opts, "rows", height); |
| 2642 | } else { |
| 2643 | goto fail; |
| 2644 | } |
| 2645 | } |
| 2646 | return opts; |
| 2647 | } |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 2648 | if (strcmp(filename, "con:") == 0) { |
| 2649 | qemu_opt_set(opts, "backend", "console"); |
| 2650 | return opts; |
| 2651 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 2652 | if (strstart(filename, "COM", NULL)) { |
| 2653 | qemu_opt_set(opts, "backend", "serial"); |
| 2654 | qemu_opt_set(opts, "path", filename); |
| 2655 | return opts; |
| 2656 | } |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 2657 | if (strstart(filename, "file:", &p)) { |
| 2658 | qemu_opt_set(opts, "backend", "file"); |
| 2659 | qemu_opt_set(opts, "path", p); |
| 2660 | return opts; |
| 2661 | } |
| 2662 | if (strstart(filename, "pipe:", &p)) { |
| 2663 | qemu_opt_set(opts, "backend", "pipe"); |
| 2664 | qemu_opt_set(opts, "path", p); |
| 2665 | return opts; |
| 2666 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2667 | if (strstart(filename, "tcp:", &p) || |
| 2668 | strstart(filename, "telnet:", &p)) { |
| 2669 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 2670 | host[0] = 0; |
| 2671 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) |
| 2672 | goto fail; |
| 2673 | } |
| 2674 | qemu_opt_set(opts, "backend", "socket"); |
| 2675 | qemu_opt_set(opts, "host", host); |
| 2676 | qemu_opt_set(opts, "port", port); |
| 2677 | if (p[pos] == ',') { |
| 2678 | if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0) |
| 2679 | goto fail; |
| 2680 | } |
| 2681 | if (strstart(filename, "telnet:", &p)) |
| 2682 | qemu_opt_set(opts, "telnet", "on"); |
| 2683 | return opts; |
| 2684 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2685 | if (strstart(filename, "udp:", &p)) { |
| 2686 | qemu_opt_set(opts, "backend", "udp"); |
| 2687 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
| 2688 | host[0] = 0; |
Jan Kiszka | 39324ca | 2010-03-07 11:28:48 +0100 | [diff] [blame] | 2689 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2690 | goto fail; |
| 2691 | } |
| 2692 | } |
| 2693 | qemu_opt_set(opts, "host", host); |
| 2694 | qemu_opt_set(opts, "port", port); |
| 2695 | if (p[pos] == '@') { |
| 2696 | p += pos + 1; |
| 2697 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 2698 | host[0] = 0; |
| 2699 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2700 | goto fail; |
| 2701 | } |
| 2702 | } |
| 2703 | qemu_opt_set(opts, "localaddr", host); |
| 2704 | qemu_opt_set(opts, "localport", port); |
| 2705 | } |
| 2706 | return opts; |
| 2707 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2708 | if (strstart(filename, "unix:", &p)) { |
| 2709 | qemu_opt_set(opts, "backend", "socket"); |
| 2710 | if (qemu_opts_do_parse(opts, p, "path") != 0) |
| 2711 | goto fail; |
| 2712 | return opts; |
| 2713 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 2714 | if (strstart(filename, "/dev/parport", NULL) || |
| 2715 | strstart(filename, "/dev/ppi", NULL)) { |
| 2716 | qemu_opt_set(opts, "backend", "parport"); |
| 2717 | qemu_opt_set(opts, "path", filename); |
| 2718 | return opts; |
| 2719 | } |
| 2720 | if (strstart(filename, "/dev/", NULL)) { |
| 2721 | qemu_opt_set(opts, "backend", "tty"); |
| 2722 | qemu_opt_set(opts, "path", filename); |
| 2723 | return opts; |
| 2724 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2725 | |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2726 | fail: |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2727 | qemu_opts_del(opts); |
| 2728 | return NULL; |
| 2729 | } |
| 2730 | |
| 2731 | static const struct { |
| 2732 | const char *name; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2733 | int (*open)(QemuOpts *opts, CharDriverState **chr); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2734 | } backend_table[] = { |
| 2735 | { .name = "null", .open = qemu_chr_open_null }, |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 2736 | { .name = "socket", .open = qemu_chr_open_socket }, |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2737 | { .name = "udp", .open = qemu_chr_open_udp }, |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 2738 | { .name = "msmouse", .open = qemu_chr_open_msmouse }, |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 2739 | { .name = "vc", .open = text_console_init }, |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 2740 | #ifdef _WIN32 |
| 2741 | { .name = "file", .open = qemu_chr_open_win_file_out }, |
| 2742 | { .name = "pipe", .open = qemu_chr_open_win_pipe }, |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 2743 | { .name = "console", .open = qemu_chr_open_win_con }, |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 2744 | { .name = "serial", .open = qemu_chr_open_win }, |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2745 | { .name = "stdio", .open = qemu_chr_open_win_stdio }, |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 2746 | #else |
| 2747 | { .name = "file", .open = qemu_chr_open_file_out }, |
| 2748 | { .name = "pipe", .open = qemu_chr_open_pipe }, |
Gerd Hoffmann | 4490dad | 2009-09-10 10:58:43 +0200 | [diff] [blame] | 2749 | { .name = "pty", .open = qemu_chr_open_pty }, |
Gerd Hoffmann | 3c17aff | 2009-09-10 10:58:44 +0200 | [diff] [blame] | 2750 | { .name = "stdio", .open = qemu_chr_open_stdio }, |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 2751 | #endif |
Gerd Hoffmann | dc1c21e | 2009-09-10 10:58:46 +0200 | [diff] [blame] | 2752 | #ifdef CONFIG_BRLAPI |
| 2753 | { .name = "braille", .open = chr_baum_init }, |
| 2754 | #endif |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 2755 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 2756 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
| 2757 | || defined(__FreeBSD_kernel__) |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 2758 | { .name = "tty", .open = qemu_chr_open_tty }, |
| 2759 | #endif |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 2760 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 2761 | || defined(__FreeBSD_kernel__) |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 2762 | { .name = "parport", .open = qemu_chr_open_pp }, |
| 2763 | #endif |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 2764 | #ifdef CONFIG_SPICE |
| 2765 | { .name = "spicevmc", .open = qemu_chr_open_spice }, |
| 2766 | #endif |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2767 | }; |
| 2768 | |
Anthony Liguori | f69554b | 2011-08-15 11:17:37 -0500 | [diff] [blame] | 2769 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2770 | void (*init)(struct CharDriverState *s)) |
| 2771 | { |
| 2772 | CharDriverState *chr; |
| 2773 | int i; |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2774 | int ret; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2775 | |
| 2776 | if (qemu_opts_id(opts) == NULL) { |
| 2777 | fprintf(stderr, "chardev: no id specified\n"); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
Stefan Hajnoczi | 1bbd185 | 2011-01-22 13:07:26 +0000 | [diff] [blame] | 2781 | if (qemu_opt_get(opts, "backend") == NULL) { |
| 2782 | fprintf(stderr, "chardev: \"%s\" missing backend\n", |
| 2783 | qemu_opts_id(opts)); |
| 2784 | return NULL; |
| 2785 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2786 | for (i = 0; i < ARRAY_SIZE(backend_table); i++) { |
| 2787 | if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0) |
| 2788 | break; |
| 2789 | } |
| 2790 | if (i == ARRAY_SIZE(backend_table)) { |
| 2791 | fprintf(stderr, "chardev: backend \"%s\" not found\n", |
| 2792 | qemu_opt_get(opts, "backend")); |
| 2793 | return NULL; |
| 2794 | } |
| 2795 | |
Kevin Wolf | 6e1db57 | 2011-06-01 13:29:11 +0200 | [diff] [blame] | 2796 | ret = backend_table[i].open(opts, &chr); |
| 2797 | if (ret < 0) { |
| 2798 | fprintf(stderr, "chardev: opening backend \"%s\" failed: %s\n", |
| 2799 | qemu_opt_get(opts, "backend"), strerror(-ret)); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2800 | return NULL; |
| 2801 | } |
| 2802 | |
| 2803 | if (!chr->filename) |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2804 | chr->filename = g_strdup(qemu_opt_get(opts, "backend")); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2805 | chr->init = init; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2806 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 2807 | |
| 2808 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
| 2809 | CharDriverState *base = chr; |
| 2810 | int len = strlen(qemu_opts_id(opts)) + 6; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2811 | base->label = g_malloc(len); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 2812 | snprintf(base->label, len, "%s-base", qemu_opts_id(opts)); |
| 2813 | chr = qemu_chr_open_mux(base); |
| 2814 | chr->filename = base->filename; |
Kusanagi Kouichi | d5b2716 | 2011-04-26 19:19:26 +0900 | [diff] [blame] | 2815 | chr->avail_connections = MAX_MUX; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2816 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
Kusanagi Kouichi | d5b2716 | 2011-04-26 19:19:26 +0900 | [diff] [blame] | 2817 | } else { |
| 2818 | chr->avail_connections = 1; |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 2819 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2820 | chr->label = g_strdup(qemu_opts_id(opts)); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2821 | return chr; |
| 2822 | } |
| 2823 | |
Anthony Liguori | 27143a4 | 2011-08-15 11:17:36 -0500 | [diff] [blame] | 2824 | 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] | 2825 | { |
| 2826 | const char *p; |
| 2827 | CharDriverState *chr; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2828 | QemuOpts *opts; |
| 2829 | |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 2830 | if (strstart(filename, "chardev:", &p)) { |
| 2831 | return qemu_chr_find(p); |
| 2832 | } |
| 2833 | |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 2834 | opts = qemu_chr_parse_compat(label, filename); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2835 | if (!opts) |
| 2836 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2837 | |
Anthony Liguori | f69554b | 2011-08-15 11:17:37 -0500 | [diff] [blame] | 2838 | chr = qemu_chr_new_from_opts(opts, init); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 2839 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
| 2840 | monitor_init(chr, MONITOR_USE_READLINE); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2841 | } |
Paolo Bonzini | 363f8cb | 2010-12-23 13:42:54 +0100 | [diff] [blame] | 2842 | qemu_opts_del(opts); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2843 | return chr; |
| 2844 | } |
| 2845 | |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 2846 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
Paolo Bonzini | c48855e | 2010-12-23 13:42:48 +0100 | [diff] [blame] | 2847 | { |
| 2848 | if (chr->chr_set_echo) { |
| 2849 | chr->chr_set_echo(chr, echo); |
| 2850 | } |
| 2851 | } |
| 2852 | |
Anthony Liguori | c9d830e | 2011-08-15 11:17:32 -0500 | [diff] [blame] | 2853 | void qemu_chr_fe_open(struct CharDriverState *chr) |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 2854 | { |
| 2855 | if (chr->chr_guest_open) { |
| 2856 | chr->chr_guest_open(chr); |
| 2857 | } |
| 2858 | } |
| 2859 | |
Anthony Liguori | 2817822 | 2011-08-15 11:17:33 -0500 | [diff] [blame] | 2860 | void qemu_chr_fe_close(struct CharDriverState *chr) |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 2861 | { |
| 2862 | if (chr->chr_guest_close) { |
| 2863 | chr->chr_guest_close(chr); |
| 2864 | } |
| 2865 | } |
| 2866 | |
Anthony Liguori | 70f24fb | 2011-08-15 11:17:38 -0500 | [diff] [blame] | 2867 | void qemu_chr_delete(CharDriverState *chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2868 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2869 | QTAILQ_REMOVE(&chardevs, chr, next); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2870 | if (chr->chr_close) |
| 2871 | chr->chr_close(chr); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2872 | g_free(chr->filename); |
| 2873 | g_free(chr->label); |
| 2874 | g_free(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 2877 | ChardevInfoList *qmp_query_chardev(Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2878 | { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 2879 | ChardevInfoList *chr_list = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2880 | CharDriverState *chr; |
| 2881 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2882 | QTAILQ_FOREACH(chr, &chardevs, next) { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 2883 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
| 2884 | info->value = g_malloc0(sizeof(*info->value)); |
| 2885 | info->value->label = g_strdup(chr->label); |
| 2886 | info->value->filename = g_strdup(chr->filename); |
| 2887 | |
| 2888 | info->next = chr_list; |
| 2889 | chr_list = info; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2890 | } |
Luiz Capitulino | 588b383 | 2009-12-10 17:16:08 -0200 | [diff] [blame] | 2891 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 2892 | return chr_list; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2893 | } |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 2894 | |
| 2895 | CharDriverState *qemu_chr_find(const char *name) |
| 2896 | { |
| 2897 | CharDriverState *chr; |
| 2898 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2899 | QTAILQ_FOREACH(chr, &chardevs, next) { |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 2900 | if (strcmp(chr->label, name) != 0) |
| 2901 | continue; |
| 2902 | return chr; |
| 2903 | } |
| 2904 | return NULL; |
| 2905 | } |