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