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