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