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