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