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