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