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