]> git.proxmox.com Git - qemu.git/blame - qemu-char.c
chardev: add serial chardev support to chardev-add (qmp)
[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 858
e551498e
GH
859#define HAVE_CHARDEV_TTY 1
860
6f97dba0
AL
861typedef struct {
862 int fd;
863 int connected;
864 int polling;
865 int read_bytes;
866 QEMUTimer *timer;
867} PtyCharDriver;
868
869static void pty_chr_update_read_handler(CharDriverState *chr);
870static void pty_chr_state(CharDriverState *chr, int connected);
871
872static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
873{
874 PtyCharDriver *s = chr->opaque;
875
876 if (!s->connected) {
877 /* guest sends data, check for (re-)connect */
878 pty_chr_update_read_handler(chr);
879 return 0;
880 }
881 return send_all(s->fd, buf, len);
882}
883
884static int pty_chr_read_poll(void *opaque)
885{
886 CharDriverState *chr = opaque;
887 PtyCharDriver *s = chr->opaque;
888
909cda12 889 s->read_bytes = qemu_chr_be_can_write(chr);
6f97dba0
AL
890 return s->read_bytes;
891}
892
893static void pty_chr_read(void *opaque)
894{
895 CharDriverState *chr = opaque;
896 PtyCharDriver *s = chr->opaque;
897 int size, len;
9bd7854e 898 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
899
900 len = sizeof(buf);
901 if (len > s->read_bytes)
902 len = s->read_bytes;
903 if (len == 0)
904 return;
905 size = read(s->fd, buf, len);
906 if ((size == -1 && errno == EIO) ||
907 (size == 0)) {
908 pty_chr_state(chr, 0);
909 return;
910 }
911 if (size > 0) {
912 pty_chr_state(chr, 1);
fa5efccb 913 qemu_chr_be_write(chr, buf, size);
6f97dba0
AL
914 }
915}
916
917static void pty_chr_update_read_handler(CharDriverState *chr)
918{
919 PtyCharDriver *s = chr->opaque;
920
921 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
922 pty_chr_read, NULL, chr);
923 s->polling = 1;
924 /*
925 * Short timeout here: just need wait long enougth that qemu makes
926 * it through the poll loop once. When reconnected we want a
927 * short timeout so we notice it almost instantly. Otherwise
928 * read() gives us -EIO instantly, making pty_chr_state() reset the
929 * timeout to the normal (much longer) poll interval before the
930 * timer triggers.
931 */
7bd427d8 932 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
6f97dba0
AL
933}
934
935static void pty_chr_state(CharDriverState *chr, int connected)
936{
937 PtyCharDriver *s = chr->opaque;
938
939 if (!connected) {
940 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
941 s->connected = 0;
942 s->polling = 0;
943 /* (re-)connect poll interval for idle guests: once per second.
944 * We check more frequently in case the guests sends data to
945 * the virtual device linked to our pty. */
7bd427d8 946 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
6f97dba0
AL
947 } else {
948 if (!s->connected)
127338e6 949 qemu_chr_generic_open(chr);
6f97dba0
AL
950 s->connected = 1;
951 }
952}
953
954static void pty_chr_timer(void *opaque)
955{
956 struct CharDriverState *chr = opaque;
957 PtyCharDriver *s = chr->opaque;
958
959 if (s->connected)
960 return;
961 if (s->polling) {
962 /* If we arrive here without polling being cleared due
963 * read returning -EIO, then we are (re-)connected */
964 pty_chr_state(chr, 1);
965 return;
966 }
967
968 /* Next poll ... */
969 pty_chr_update_read_handler(chr);
970}
971
972static void pty_chr_close(struct CharDriverState *chr)
973{
974 PtyCharDriver *s = chr->opaque;
975
976 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
977 close(s->fd);
819f56b7
AL
978 qemu_del_timer(s->timer);
979 qemu_free_timer(s->timer);
7267c094 980 g_free(s);
a425d23f 981 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
982}
983
1f51470d 984static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
6f97dba0
AL
985{
986 CharDriverState *chr;
987 PtyCharDriver *s;
988 struct termios tty;
58650218 989 const char *label;
a4e26048 990 int master_fd, slave_fd, len;
c5e97233 991#if defined(__OpenBSD__) || defined(__DragonFly__)
6f97dba0
AL
992 char pty_name[PATH_MAX];
993#define q_ptsname(x) pty_name
994#else
995 char *pty_name = NULL;
996#define q_ptsname(x) ptsname(x)
997#endif
998
a4e26048 999 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
1f51470d 1000 return NULL;
6f97dba0
AL
1001 }
1002
1003 /* Set raw attributes on the pty. */
c3b972c3 1004 tcgetattr(slave_fd, &tty);
6f97dba0
AL
1005 cfmakeraw(&tty);
1006 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1007 close(slave_fd);
1008
a4e26048
MA
1009 chr = g_malloc0(sizeof(CharDriverState));
1010
1011 len = strlen(q_ptsname(master_fd)) + 5;
7267c094 1012 chr->filename = g_malloc(len);
a4e26048
MA
1013 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1014 qemu_opt_set(opts, "path", q_ptsname(master_fd));
58650218
LL
1015
1016 label = qemu_opts_id(opts);
25bbf61e
GH
1017 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1018 q_ptsname(master_fd),
1019 label ? " (label " : "",
1020 label ? label : "",
1021 label ? ")" : "");
6f97dba0 1022
a4e26048 1023 s = g_malloc0(sizeof(PtyCharDriver));
6f97dba0
AL
1024 chr->opaque = s;
1025 chr->chr_write = pty_chr_write;
1026 chr->chr_update_read_handler = pty_chr_update_read_handler;
1027 chr->chr_close = pty_chr_close;
1028
a4e26048 1029 s->fd = master_fd;
7bd427d8 1030 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
6f97dba0 1031
1f51470d 1032 return chr;
6f97dba0
AL
1033}
1034
1035static void tty_serial_init(int fd, int speed,
1036 int parity, int data_bits, int stop_bits)
1037{
1038 struct termios tty;
1039 speed_t spd;
1040
1041#if 0
1042 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1043 speed, parity, data_bits, stop_bits);
1044#endif
1045 tcgetattr (fd, &tty);
1046
45eea13b
SW
1047#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1048 speed = speed * 10 / 11;
1049 do {
1050 check_speed(50);
1051 check_speed(75);
1052 check_speed(110);
1053 check_speed(134);
1054 check_speed(150);
1055 check_speed(200);
1056 check_speed(300);
1057 check_speed(600);
1058 check_speed(1200);
1059 check_speed(1800);
1060 check_speed(2400);
1061 check_speed(4800);
1062 check_speed(9600);
1063 check_speed(19200);
1064 check_speed(38400);
1065 /* Non-Posix values follow. They may be unsupported on some systems. */
1066 check_speed(57600);
1067 check_speed(115200);
1068#ifdef B230400
1069 check_speed(230400);
1070#endif
1071#ifdef B460800
1072 check_speed(460800);
1073#endif
1074#ifdef B500000
1075 check_speed(500000);
1076#endif
1077#ifdef B576000
1078 check_speed(576000);
1079#endif
1080#ifdef B921600
1081 check_speed(921600);
1082#endif
1083#ifdef B1000000
1084 check_speed(1000000);
1085#endif
1086#ifdef B1152000
1087 check_speed(1152000);
1088#endif
1089#ifdef B1500000
1090 check_speed(1500000);
1091#endif
1092#ifdef B2000000
1093 check_speed(2000000);
1094#endif
1095#ifdef B2500000
1096 check_speed(2500000);
1097#endif
1098#ifdef B3000000
1099 check_speed(3000000);
1100#endif
1101#ifdef B3500000
1102 check_speed(3500000);
1103#endif
1104#ifdef B4000000
1105 check_speed(4000000);
1106#endif
6f97dba0 1107 spd = B115200;
45eea13b 1108 } while (0);
6f97dba0
AL
1109
1110 cfsetispeed(&tty, spd);
1111 cfsetospeed(&tty, spd);
1112
1113 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1114 |INLCR|IGNCR|ICRNL|IXON);
1115 tty.c_oflag |= OPOST;
1116 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1117 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1118 switch(data_bits) {
1119 default:
1120 case 8:
1121 tty.c_cflag |= CS8;
1122 break;
1123 case 7:
1124 tty.c_cflag |= CS7;
1125 break;
1126 case 6:
1127 tty.c_cflag |= CS6;
1128 break;
1129 case 5:
1130 tty.c_cflag |= CS5;
1131 break;
1132 }
1133 switch(parity) {
1134 default:
1135 case 'N':
1136 break;
1137 case 'E':
1138 tty.c_cflag |= PARENB;
1139 break;
1140 case 'O':
1141 tty.c_cflag |= PARENB | PARODD;
1142 break;
1143 }
1144 if (stop_bits == 2)
1145 tty.c_cflag |= CSTOPB;
1146
1147 tcsetattr (fd, TCSANOW, &tty);
1148}
1149
1150static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1151{
1152 FDCharDriver *s = chr->opaque;
1153
1154 switch(cmd) {
1155 case CHR_IOCTL_SERIAL_SET_PARAMS:
1156 {
1157 QEMUSerialSetParams *ssp = arg;
1158 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1159 ssp->data_bits, ssp->stop_bits);
1160 }
1161 break;
1162 case CHR_IOCTL_SERIAL_SET_BREAK:
1163 {
1164 int enable = *(int *)arg;
1165 if (enable)
1166 tcsendbreak(s->fd_in, 1);
1167 }
1168 break;
1169 case CHR_IOCTL_SERIAL_GET_TIOCM:
1170 {
1171 int sarg = 0;
1172 int *targ = (int *)arg;
1173 ioctl(s->fd_in, TIOCMGET, &sarg);
1174 *targ = 0;
b4abdfa4 1175 if (sarg & TIOCM_CTS)
6f97dba0 1176 *targ |= CHR_TIOCM_CTS;
b4abdfa4 1177 if (sarg & TIOCM_CAR)
6f97dba0 1178 *targ |= CHR_TIOCM_CAR;
b4abdfa4 1179 if (sarg & TIOCM_DSR)
6f97dba0 1180 *targ |= CHR_TIOCM_DSR;
b4abdfa4 1181 if (sarg & TIOCM_RI)
6f97dba0 1182 *targ |= CHR_TIOCM_RI;
b4abdfa4 1183 if (sarg & TIOCM_DTR)
6f97dba0 1184 *targ |= CHR_TIOCM_DTR;
b4abdfa4 1185 if (sarg & TIOCM_RTS)
6f97dba0
AL
1186 *targ |= CHR_TIOCM_RTS;
1187 }
1188 break;
1189 case CHR_IOCTL_SERIAL_SET_TIOCM:
1190 {
1191 int sarg = *(int *)arg;
1192 int targ = 0;
b4abdfa4
AJ
1193 ioctl(s->fd_in, TIOCMGET, &targ);
1194 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1195 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1196 if (sarg & CHR_TIOCM_CTS)
1197 targ |= TIOCM_CTS;
1198 if (sarg & CHR_TIOCM_CAR)
1199 targ |= TIOCM_CAR;
1200 if (sarg & CHR_TIOCM_DSR)
1201 targ |= TIOCM_DSR;
1202 if (sarg & CHR_TIOCM_RI)
1203 targ |= TIOCM_RI;
1204 if (sarg & CHR_TIOCM_DTR)
6f97dba0 1205 targ |= TIOCM_DTR;
b4abdfa4 1206 if (sarg & CHR_TIOCM_RTS)
6f97dba0
AL
1207 targ |= TIOCM_RTS;
1208 ioctl(s->fd_in, TIOCMSET, &targ);
1209 }
1210 break;
1211 default:
1212 return -ENOTSUP;
1213 }
1214 return 0;
1215}
1216
4266a134
DA
1217static void qemu_chr_close_tty(CharDriverState *chr)
1218{
1219 FDCharDriver *s = chr->opaque;
1220 int fd = -1;
1221
1222 if (s) {
1223 fd = s->fd_in;
1224 }
1225
1226 fd_chr_close(chr);
1227
1228 if (fd >= 0) {
1229 close(fd);
1230 }
1231}
1232
d59044ef
GH
1233static CharDriverState *qemu_chr_open_tty_fd(int fd)
1234{
1235 CharDriverState *chr;
1236
1237 tty_serial_init(fd, 115200, 'N', 8, 1);
1238 chr = qemu_chr_open_fd(fd, fd);
1239 chr->chr_ioctl = tty_serial_ioctl;
1240 chr->chr_close = qemu_chr_close_tty;
1241 return chr;
1242}
1243
1f51470d 1244static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
6f97dba0 1245{
48b76496 1246 const char *filename = qemu_opt_get(opts, "path");
6f97dba0
AL
1247 int fd;
1248
b181e047 1249 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
afc535ac 1250 if (fd < 0) {
1f51470d 1251 return NULL;
afc535ac 1252 }
d59044ef 1253 return qemu_chr_open_tty_fd(fd);
6f97dba0 1254}
6f97dba0
AL
1255#endif /* __linux__ || __sun__ */
1256
1257#if defined(__linux__)
e551498e
GH
1258
1259#define HAVE_CHARDEV_PARPORT 1
1260
6f97dba0
AL
1261typedef struct {
1262 int fd;
1263 int mode;
1264} ParallelCharDriver;
1265
1266static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1267{
1268 if (s->mode != mode) {
1269 int m = mode;
1270 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1271 return 0;
1272 s->mode = mode;
1273 }
1274 return 1;
1275}
1276
1277static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1278{
1279 ParallelCharDriver *drv = chr->opaque;
1280 int fd = drv->fd;
1281 uint8_t b;
1282
1283 switch(cmd) {
1284 case CHR_IOCTL_PP_READ_DATA:
1285 if (ioctl(fd, PPRDATA, &b) < 0)
1286 return -ENOTSUP;
1287 *(uint8_t *)arg = b;
1288 break;
1289 case CHR_IOCTL_PP_WRITE_DATA:
1290 b = *(uint8_t *)arg;
1291 if (ioctl(fd, PPWDATA, &b) < 0)
1292 return -ENOTSUP;
1293 break;
1294 case CHR_IOCTL_PP_READ_CONTROL:
1295 if (ioctl(fd, PPRCONTROL, &b) < 0)
1296 return -ENOTSUP;
1297 /* Linux gives only the lowest bits, and no way to know data
1298 direction! For better compatibility set the fixed upper
1299 bits. */
1300 *(uint8_t *)arg = b | 0xc0;
1301 break;
1302 case CHR_IOCTL_PP_WRITE_CONTROL:
1303 b = *(uint8_t *)arg;
1304 if (ioctl(fd, PPWCONTROL, &b) < 0)
1305 return -ENOTSUP;
1306 break;
1307 case CHR_IOCTL_PP_READ_STATUS:
1308 if (ioctl(fd, PPRSTATUS, &b) < 0)
1309 return -ENOTSUP;
1310 *(uint8_t *)arg = b;
1311 break;
1312 case CHR_IOCTL_PP_DATA_DIR:
1313 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1314 return -ENOTSUP;
1315 break;
1316 case CHR_IOCTL_PP_EPP_READ_ADDR:
1317 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1318 struct ParallelIOArg *parg = arg;
1319 int n = read(fd, parg->buffer, parg->count);
1320 if (n != parg->count) {
1321 return -EIO;
1322 }
1323 }
1324 break;
1325 case CHR_IOCTL_PP_EPP_READ:
1326 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1327 struct ParallelIOArg *parg = arg;
1328 int n = read(fd, parg->buffer, parg->count);
1329 if (n != parg->count) {
1330 return -EIO;
1331 }
1332 }
1333 break;
1334 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1335 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1336 struct ParallelIOArg *parg = arg;
1337 int n = write(fd, parg->buffer, parg->count);
1338 if (n != parg->count) {
1339 return -EIO;
1340 }
1341 }
1342 break;
1343 case CHR_IOCTL_PP_EPP_WRITE:
1344 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1345 struct ParallelIOArg *parg = arg;
1346 int n = write(fd, parg->buffer, parg->count);
1347 if (n != parg->count) {
1348 return -EIO;
1349 }
1350 }
1351 break;
1352 default:
1353 return -ENOTSUP;
1354 }
1355 return 0;
1356}
1357
1358static void pp_close(CharDriverState *chr)
1359{
1360 ParallelCharDriver *drv = chr->opaque;
1361 int fd = drv->fd;
1362
1363 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1364 ioctl(fd, PPRELEASE);
1365 close(fd);
7267c094 1366 g_free(drv);
a425d23f 1367 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
1368}
1369
1f51470d 1370static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
6f97dba0 1371{
48b76496 1372 const char *filename = qemu_opt_get(opts, "path");
6f97dba0
AL
1373 CharDriverState *chr;
1374 ParallelCharDriver *drv;
1375 int fd;
1376
b181e047 1377 TFR(fd = qemu_open(filename, O_RDWR));
a89dd6c3 1378 if (fd < 0) {
1f51470d 1379 return NULL;
a89dd6c3 1380 }
6f97dba0
AL
1381
1382 if (ioctl(fd, PPCLAIM) < 0) {
1383 close(fd);
1f51470d 1384 return NULL;
6f97dba0
AL
1385 }
1386
7267c094 1387 drv = g_malloc0(sizeof(ParallelCharDriver));
6f97dba0
AL
1388 drv->fd = fd;
1389 drv->mode = IEEE1284_MODE_COMPAT;
1390
7267c094 1391 chr = g_malloc0(sizeof(CharDriverState));
6f97dba0
AL
1392 chr->chr_write = null_chr_write;
1393 chr->chr_ioctl = pp_ioctl;
1394 chr->chr_close = pp_close;
1395 chr->opaque = drv;
1396
127338e6 1397 qemu_chr_generic_open(chr);
6f97dba0 1398
1f51470d 1399 return chr;
6f97dba0
AL
1400}
1401#endif /* __linux__ */
1402
a167ba50 1403#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
e551498e
GH
1404
1405#define HAVE_CHARDEV_PARPORT 1
1406
6972f935
BS
1407static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1408{
e0efb993 1409 int fd = (int)(intptr_t)chr->opaque;
6972f935
BS
1410 uint8_t b;
1411
1412 switch(cmd) {
1413 case CHR_IOCTL_PP_READ_DATA:
1414 if (ioctl(fd, PPIGDATA, &b) < 0)
1415 return -ENOTSUP;
1416 *(uint8_t *)arg = b;
1417 break;
1418 case CHR_IOCTL_PP_WRITE_DATA:
1419 b = *(uint8_t *)arg;
1420 if (ioctl(fd, PPISDATA, &b) < 0)
1421 return -ENOTSUP;
1422 break;
1423 case CHR_IOCTL_PP_READ_CONTROL:
1424 if (ioctl(fd, PPIGCTRL, &b) < 0)
1425 return -ENOTSUP;
1426 *(uint8_t *)arg = b;
1427 break;
1428 case CHR_IOCTL_PP_WRITE_CONTROL:
1429 b = *(uint8_t *)arg;
1430 if (ioctl(fd, PPISCTRL, &b) < 0)
1431 return -ENOTSUP;
1432 break;
1433 case CHR_IOCTL_PP_READ_STATUS:
1434 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1435 return -ENOTSUP;
1436 *(uint8_t *)arg = b;
1437 break;
1438 default:
1439 return -ENOTSUP;
1440 }
1441 return 0;
1442}
1443
1f51470d 1444static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
6972f935 1445{
48b76496 1446 const char *filename = qemu_opt_get(opts, "path");
6972f935
BS
1447 CharDriverState *chr;
1448 int fd;
1449
b181e047 1450 fd = qemu_open(filename, O_RDWR);
a89dd6c3 1451 if (fd < 0) {
1f51470d 1452 return NULL;
a89dd6c3 1453 }
6972f935 1454
7267c094 1455 chr = g_malloc0(sizeof(CharDriverState));
e0efb993 1456 chr->opaque = (void *)(intptr_t)fd;
6972f935
BS
1457 chr->chr_write = null_chr_write;
1458 chr->chr_ioctl = pp_ioctl;
1f51470d 1459 return chr;
6972f935
BS
1460}
1461#endif
1462
6f97dba0
AL
1463#else /* _WIN32 */
1464
db418a0a
FC
1465static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1466
6f97dba0
AL
1467typedef struct {
1468 int max_size;
1469 HANDLE hcom, hrecv, hsend;
1470 OVERLAPPED orecv, osend;
1471 BOOL fpipe;
1472 DWORD len;
1473} WinCharState;
1474
db418a0a
FC
1475typedef struct {
1476 HANDLE hStdIn;
1477 HANDLE hInputReadyEvent;
1478 HANDLE hInputDoneEvent;
1479 HANDLE hInputThread;
1480 uint8_t win_stdio_buf;
1481} WinStdioCharState;
1482
6f97dba0
AL
1483#define NSENDBUF 2048
1484#define NRECVBUF 2048
1485#define MAXCONNECT 1
1486#define NTIMEOUT 5000
1487
1488static int win_chr_poll(void *opaque);
1489static int win_chr_pipe_poll(void *opaque);
1490
1491static void win_chr_close(CharDriverState *chr)
1492{
1493 WinCharState *s = chr->opaque;
1494
1495 if (s->hsend) {
1496 CloseHandle(s->hsend);
1497 s->hsend = NULL;
1498 }
1499 if (s->hrecv) {
1500 CloseHandle(s->hrecv);
1501 s->hrecv = NULL;
1502 }
1503 if (s->hcom) {
1504 CloseHandle(s->hcom);
1505 s->hcom = NULL;
1506 }
1507 if (s->fpipe)
1508 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1509 else
1510 qemu_del_polling_cb(win_chr_poll, chr);
793cbfb5 1511
a425d23f 1512 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
1513}
1514
1515static int win_chr_init(CharDriverState *chr, const char *filename)
1516{
1517 WinCharState *s = chr->opaque;
1518 COMMCONFIG comcfg;
1519 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1520 COMSTAT comstat;
1521 DWORD size;
1522 DWORD err;
1523
1524 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1525 if (!s->hsend) {
1526 fprintf(stderr, "Failed CreateEvent\n");
1527 goto fail;
1528 }
1529 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1530 if (!s->hrecv) {
1531 fprintf(stderr, "Failed CreateEvent\n");
1532 goto fail;
1533 }
1534
1535 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1536 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1537 if (s->hcom == INVALID_HANDLE_VALUE) {
1538 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1539 s->hcom = NULL;
1540 goto fail;
1541 }
1542
1543 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1544 fprintf(stderr, "Failed SetupComm\n");
1545 goto fail;
1546 }
1547
1548 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1549 size = sizeof(COMMCONFIG);
1550 GetDefaultCommConfig(filename, &comcfg, &size);
1551 comcfg.dcb.DCBlength = sizeof(DCB);
1552 CommConfigDialog(filename, NULL, &comcfg);
1553
1554 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1555 fprintf(stderr, "Failed SetCommState\n");
1556 goto fail;
1557 }
1558
1559 if (!SetCommMask(s->hcom, EV_ERR)) {
1560 fprintf(stderr, "Failed SetCommMask\n");
1561 goto fail;
1562 }
1563
1564 cto.ReadIntervalTimeout = MAXDWORD;
1565 if (!SetCommTimeouts(s->hcom, &cto)) {
1566 fprintf(stderr, "Failed SetCommTimeouts\n");
1567 goto fail;
1568 }
1569
1570 if (!ClearCommError(s->hcom, &err, &comstat)) {
1571 fprintf(stderr, "Failed ClearCommError\n");
1572 goto fail;
1573 }
1574 qemu_add_polling_cb(win_chr_poll, chr);
1575 return 0;
1576
1577 fail:
1578 win_chr_close(chr);
1579 return -1;
1580}
1581
1582static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1583{
1584 WinCharState *s = chr->opaque;
1585 DWORD len, ret, size, err;
1586
1587 len = len1;
1588 ZeroMemory(&s->osend, sizeof(s->osend));
1589 s->osend.hEvent = s->hsend;
1590 while (len > 0) {
1591 if (s->hsend)
1592 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1593 else
1594 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1595 if (!ret) {
1596 err = GetLastError();
1597 if (err == ERROR_IO_PENDING) {
1598 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1599 if (ret) {
1600 buf += size;
1601 len -= size;
1602 } else {
1603 break;
1604 }
1605 } else {
1606 break;
1607 }
1608 } else {
1609 buf += size;
1610 len -= size;
1611 }
1612 }
1613 return len1 - len;
1614}
1615
1616static int win_chr_read_poll(CharDriverState *chr)
1617{
1618 WinCharState *s = chr->opaque;
1619
909cda12 1620 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
1621 return s->max_size;
1622}
1623
1624static void win_chr_readfile(CharDriverState *chr)
1625{
1626 WinCharState *s = chr->opaque;
1627 int ret, err;
9bd7854e 1628 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
1629 DWORD size;
1630
1631 ZeroMemory(&s->orecv, sizeof(s->orecv));
1632 s->orecv.hEvent = s->hrecv;
1633 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1634 if (!ret) {
1635 err = GetLastError();
1636 if (err == ERROR_IO_PENDING) {
1637 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1638 }
1639 }
1640
1641 if (size > 0) {
fa5efccb 1642 qemu_chr_be_write(chr, buf, size);
6f97dba0
AL
1643 }
1644}
1645
1646static void win_chr_read(CharDriverState *chr)
1647{
1648 WinCharState *s = chr->opaque;
1649
1650 if (s->len > s->max_size)
1651 s->len = s->max_size;
1652 if (s->len == 0)
1653 return;
1654
1655 win_chr_readfile(chr);
1656}
1657
1658static int win_chr_poll(void *opaque)
1659{
1660 CharDriverState *chr = opaque;
1661 WinCharState *s = chr->opaque;
1662 COMSTAT status;
1663 DWORD comerr;
1664
1665 ClearCommError(s->hcom, &comerr, &status);
1666 if (status.cbInQue > 0) {
1667 s->len = status.cbInQue;
1668 win_chr_read_poll(chr);
1669 win_chr_read(chr);
1670 return 1;
1671 }
1672 return 0;
1673}
1674
d59044ef 1675static CharDriverState *qemu_chr_open_win_path(const char *filename)
6f97dba0
AL
1676{
1677 CharDriverState *chr;
1678 WinCharState *s;
1679
7267c094
AL
1680 chr = g_malloc0(sizeof(CharDriverState));
1681 s = g_malloc0(sizeof(WinCharState));
6f97dba0
AL
1682 chr->opaque = s;
1683 chr->chr_write = win_chr_write;
1684 chr->chr_close = win_chr_close;
1685
1686 if (win_chr_init(chr, filename) < 0) {
2e02e18b
SW
1687 g_free(s);
1688 g_free(chr);
1f51470d 1689 return NULL;
6f97dba0 1690 }
127338e6 1691 qemu_chr_generic_open(chr);
1f51470d 1692 return chr;
6f97dba0
AL
1693}
1694
d59044ef
GH
1695static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1696{
1697 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1698}
1699
6f97dba0
AL
1700static int win_chr_pipe_poll(void *opaque)
1701{
1702 CharDriverState *chr = opaque;
1703 WinCharState *s = chr->opaque;
1704 DWORD size;
1705
1706 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1707 if (size > 0) {
1708 s->len = size;
1709 win_chr_read_poll(chr);
1710 win_chr_read(chr);
1711 return 1;
1712 }
1713 return 0;
1714}
1715
1716static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1717{
1718 WinCharState *s = chr->opaque;
1719 OVERLAPPED ov;
1720 int ret;
1721 DWORD size;
1722 char openname[256];
1723
1724 s->fpipe = TRUE;
1725
1726 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1727 if (!s->hsend) {
1728 fprintf(stderr, "Failed CreateEvent\n");
1729 goto fail;
1730 }
1731 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1732 if (!s->hrecv) {
1733 fprintf(stderr, "Failed CreateEvent\n");
1734 goto fail;
1735 }
1736
1737 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1738 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1739 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1740 PIPE_WAIT,
1741 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1742 if (s->hcom == INVALID_HANDLE_VALUE) {
1743 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1744 s->hcom = NULL;
1745 goto fail;
1746 }
1747
1748 ZeroMemory(&ov, sizeof(ov));
1749 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1750 ret = ConnectNamedPipe(s->hcom, &ov);
1751 if (ret) {
1752 fprintf(stderr, "Failed ConnectNamedPipe\n");
1753 goto fail;
1754 }
1755
1756 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1757 if (!ret) {
1758 fprintf(stderr, "Failed GetOverlappedResult\n");
1759 if (ov.hEvent) {
1760 CloseHandle(ov.hEvent);
1761 ov.hEvent = NULL;
1762 }
1763 goto fail;
1764 }
1765
1766 if (ov.hEvent) {
1767 CloseHandle(ov.hEvent);
1768 ov.hEvent = NULL;
1769 }
1770 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1771 return 0;
1772
1773 fail:
1774 win_chr_close(chr);
1775 return -1;
1776}
1777
1778
1f51470d 1779static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
6f97dba0 1780{
7d31544f 1781 const char *filename = qemu_opt_get(opts, "path");
6f97dba0
AL
1782 CharDriverState *chr;
1783 WinCharState *s;
1784
7267c094
AL
1785 chr = g_malloc0(sizeof(CharDriverState));
1786 s = g_malloc0(sizeof(WinCharState));
6f97dba0
AL
1787 chr->opaque = s;
1788 chr->chr_write = win_chr_write;
1789 chr->chr_close = win_chr_close;
1790
1791 if (win_chr_pipe_init(chr, filename) < 0) {
2e02e18b
SW
1792 g_free(s);
1793 g_free(chr);
1f51470d 1794 return NULL;
6f97dba0 1795 }
127338e6 1796 qemu_chr_generic_open(chr);
1f51470d 1797 return chr;
6f97dba0
AL
1798}
1799
1f51470d 1800static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
6f97dba0
AL
1801{
1802 CharDriverState *chr;
1803 WinCharState *s;
1804
7267c094
AL
1805 chr = g_malloc0(sizeof(CharDriverState));
1806 s = g_malloc0(sizeof(WinCharState));
6f97dba0
AL
1807 s->hcom = fd_out;
1808 chr->opaque = s;
1809 chr->chr_write = win_chr_write;
127338e6 1810 qemu_chr_generic_open(chr);
1f51470d 1811 return chr;
6f97dba0
AL
1812}
1813
1f51470d 1814static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
6f97dba0 1815{
1f51470d 1816 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
6f97dba0
AL
1817}
1818
1f51470d 1819static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
6f97dba0 1820{
7d31544f 1821 const char *file_out = qemu_opt_get(opts, "path");
6f97dba0
AL
1822 HANDLE fd_out;
1823
1824 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1825 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
a89dd6c3 1826 if (fd_out == INVALID_HANDLE_VALUE) {
1f51470d 1827 return NULL;
a89dd6c3 1828 }
6f97dba0 1829
1f51470d 1830 return qemu_chr_open_win_file(fd_out);
6f97dba0 1831}
db418a0a
FC
1832
1833static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1834{
1835 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1836 DWORD dwSize;
1837 int len1;
1838
1839 len1 = len;
1840
1841 while (len1 > 0) {
1842 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1843 break;
1844 }
1845 buf += dwSize;
1846 len1 -= dwSize;
1847 }
1848
1849 return len - len1;
1850}
1851
1852static void win_stdio_wait_func(void *opaque)
1853{
1854 CharDriverState *chr = opaque;
1855 WinStdioCharState *stdio = chr->opaque;
1856 INPUT_RECORD buf[4];
1857 int ret;
1858 DWORD dwSize;
1859 int i;
1860
1861 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1862 &dwSize);
1863
1864 if (!ret) {
1865 /* Avoid error storm */
1866 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1867 return;
1868 }
1869
1870 for (i = 0; i < dwSize; i++) {
1871 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1872
1873 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1874 int j;
1875 if (kev->uChar.AsciiChar != 0) {
1876 for (j = 0; j < kev->wRepeatCount; j++) {
1877 if (qemu_chr_be_can_write(chr)) {
1878 uint8_t c = kev->uChar.AsciiChar;
1879 qemu_chr_be_write(chr, &c, 1);
1880 }
1881 }
1882 }
1883 }
1884 }
1885}
1886
1887static DWORD WINAPI win_stdio_thread(LPVOID param)
1888{
1889 CharDriverState *chr = param;
1890 WinStdioCharState *stdio = chr->opaque;
1891 int ret;
1892 DWORD dwSize;
1893
1894 while (1) {
1895
1896 /* Wait for one byte */
1897 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1898
1899 /* Exit in case of error, continue if nothing read */
1900 if (!ret) {
1901 break;
1902 }
1903 if (!dwSize) {
1904 continue;
1905 }
1906
1907 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1908 if (stdio->win_stdio_buf == '\r') {
1909 continue;
1910 }
1911
1912 /* Signal the main thread and wait until the byte was eaten */
1913 if (!SetEvent(stdio->hInputReadyEvent)) {
1914 break;
1915 }
1916 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1917 != WAIT_OBJECT_0) {
1918 break;
1919 }
1920 }
1921
1922 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
1923 return 0;
1924}
1925
1926static void win_stdio_thread_wait_func(void *opaque)
1927{
1928 CharDriverState *chr = opaque;
1929 WinStdioCharState *stdio = chr->opaque;
1930
1931 if (qemu_chr_be_can_write(chr)) {
1932 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
1933 }
1934
1935 SetEvent(stdio->hInputDoneEvent);
1936}
1937
1938static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
1939{
1940 WinStdioCharState *stdio = chr->opaque;
1941 DWORD dwMode = 0;
1942
1943 GetConsoleMode(stdio->hStdIn, &dwMode);
1944
1945 if (echo) {
1946 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
1947 } else {
1948 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
1949 }
1950}
1951
1952static void win_stdio_close(CharDriverState *chr)
1953{
1954 WinStdioCharState *stdio = chr->opaque;
1955
1956 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
1957 CloseHandle(stdio->hInputReadyEvent);
1958 }
1959 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
1960 CloseHandle(stdio->hInputDoneEvent);
1961 }
1962 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
1963 TerminateThread(stdio->hInputThread, 0);
1964 }
1965
1966 g_free(chr->opaque);
1967 g_free(chr);
1968 stdio_nb_clients--;
1969}
1970
1f51470d 1971static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
db418a0a
FC
1972{
1973 CharDriverState *chr;
1974 WinStdioCharState *stdio;
1975 DWORD dwMode;
1976 int is_console = 0;
1977
1978 if (stdio_nb_clients >= STDIO_MAX_CLIENTS
1979 || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
1f51470d 1980 return NULL;
db418a0a
FC
1981 }
1982
1983 chr = g_malloc0(sizeof(CharDriverState));
1984 stdio = g_malloc0(sizeof(WinStdioCharState));
1985
1986 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1987 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
1988 fprintf(stderr, "cannot open stdio: invalid handle\n");
1989 exit(1);
1990 }
1991
1992 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
1993
1994 chr->opaque = stdio;
1995 chr->chr_write = win_stdio_write;
1996 chr->chr_close = win_stdio_close;
1997
1998 if (stdio_nb_clients == 0) {
1999 if (is_console) {
2000 if (qemu_add_wait_object(stdio->hStdIn,
2001 win_stdio_wait_func, chr)) {
2002 fprintf(stderr, "qemu_add_wait_object: failed\n");
2003 }
2004 } else {
2005 DWORD dwId;
2006
2007 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2008 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2009 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2010 chr, 0, &dwId);
2011
2012 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2013 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2014 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2015 fprintf(stderr, "cannot create stdio thread or event\n");
2016 exit(1);
2017 }
2018 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2019 win_stdio_thread_wait_func, chr)) {
2020 fprintf(stderr, "qemu_add_wait_object: failed\n");
2021 }
2022 }
2023 }
2024
2025 dwMode |= ENABLE_LINE_INPUT;
2026
2027 stdio_clients[stdio_nb_clients++] = chr;
2028 if (stdio_nb_clients == 1 && is_console) {
2029 /* set the terminal in raw mode */
2030 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2031 dwMode |= ENABLE_PROCESSED_INPUT;
2032 }
2033
2034 SetConsoleMode(stdio->hStdIn, dwMode);
2035
2036 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2037 qemu_chr_fe_set_echo(chr, false);
2038
1f51470d 2039 return chr;
db418a0a 2040}
6f97dba0
AL
2041#endif /* !_WIN32 */
2042
2043/***********************************************************/
2044/* UDP Net console */
2045
2046typedef struct {
2047 int fd;
9bd7854e 2048 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
2049 int bufcnt;
2050 int bufptr;
2051 int max_size;
2052} NetCharDriver;
2053
2054static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2055{
2056 NetCharDriver *s = chr->opaque;
2057
7e1b35b4 2058 return send(s->fd, (const void *)buf, len, 0);
6f97dba0
AL
2059}
2060
2061static int udp_chr_read_poll(void *opaque)
2062{
2063 CharDriverState *chr = opaque;
2064 NetCharDriver *s = chr->opaque;
2065
909cda12 2066 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2067
2068 /* If there were any stray characters in the queue process them
2069 * first
2070 */
2071 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
fa5efccb 2072 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
6f97dba0 2073 s->bufptr++;
909cda12 2074 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2075 }
2076 return s->max_size;
2077}
2078
2079static void udp_chr_read(void *opaque)
2080{
2081 CharDriverState *chr = opaque;
2082 NetCharDriver *s = chr->opaque;
2083
2084 if (s->max_size == 0)
2085 return;
00aa0040 2086 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
6f97dba0
AL
2087 s->bufptr = s->bufcnt;
2088 if (s->bufcnt <= 0)
2089 return;
2090
2091 s->bufptr = 0;
2092 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
fa5efccb 2093 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
6f97dba0 2094 s->bufptr++;
909cda12 2095 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2096 }
2097}
2098
2099static void udp_chr_update_read_handler(CharDriverState *chr)
2100{
2101 NetCharDriver *s = chr->opaque;
2102
2103 if (s->fd >= 0) {
2104 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2105 udp_chr_read, NULL, chr);
2106 }
2107}
2108
819f56b7
AL
2109static void udp_chr_close(CharDriverState *chr)
2110{
2111 NetCharDriver *s = chr->opaque;
2112 if (s->fd >= 0) {
069c159e 2113 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
819f56b7
AL
2114 closesocket(s->fd);
2115 }
7267c094 2116 g_free(s);
a425d23f 2117 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
819f56b7
AL
2118}
2119
1f51470d 2120static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
6f97dba0
AL
2121{
2122 CharDriverState *chr = NULL;
2123 NetCharDriver *s = NULL;
87d5f24f 2124 Error *local_err = NULL;
6f97dba0 2125 int fd = -1;
6f97dba0 2126
7267c094
AL
2127 chr = g_malloc0(sizeof(CharDriverState));
2128 s = g_malloc0(sizeof(NetCharDriver));
6f97dba0 2129
87d5f24f 2130 fd = inet_dgram_opts(opts, &local_err);
6f97dba0 2131 if (fd < 0) {
6f97dba0
AL
2132 goto return_err;
2133 }
2134
2135 s->fd = fd;
2136 s->bufcnt = 0;
2137 s->bufptr = 0;
2138 chr->opaque = s;
2139 chr->chr_write = udp_chr_write;
2140 chr->chr_update_read_handler = udp_chr_update_read_handler;
819f56b7 2141 chr->chr_close = udp_chr_close;
1f51470d 2142 return chr;
6f97dba0
AL
2143
2144return_err:
87d5f24f
PB
2145 if (local_err) {
2146 qerror_report_err(local_err);
2147 error_free(local_err);
2148 }
7267c094
AL
2149 g_free(chr);
2150 g_free(s);
6e1db57b 2151 if (fd >= 0) {
6f97dba0 2152 closesocket(fd);
6e1db57b 2153 }
1f51470d 2154 return NULL;
6f97dba0
AL
2155}
2156
2157/***********************************************************/
2158/* TCP Net console */
2159
2160typedef struct {
2161 int fd, listen_fd;
2162 int connected;
2163 int max_size;
2164 int do_telnetopt;
2165 int do_nodelay;
2166 int is_unix;
7d174059 2167 int msgfd;
6f97dba0
AL
2168} TCPCharDriver;
2169
2170static void tcp_chr_accept(void *opaque);
2171
2172static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2173{
2174 TCPCharDriver *s = chr->opaque;
2175 if (s->connected) {
2176 return send_all(s->fd, buf, len);
455aa1e0 2177 } else {
6db0fdce 2178 /* XXX: indicate an error ? */
455aa1e0 2179 return len;
6f97dba0
AL
2180 }
2181}
2182
2183static int tcp_chr_read_poll(void *opaque)
2184{
2185 CharDriverState *chr = opaque;
2186 TCPCharDriver *s = chr->opaque;
2187 if (!s->connected)
2188 return 0;
909cda12 2189 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2190 return s->max_size;
2191}
2192
2193#define IAC 255
2194#define IAC_BREAK 243
2195static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2196 TCPCharDriver *s,
2197 uint8_t *buf, int *size)
2198{
2199 /* Handle any telnet client's basic IAC options to satisfy char by
2200 * char mode with no echo. All IAC options will be removed from
2201 * the buf and the do_telnetopt variable will be used to track the
2202 * state of the width of the IAC information.
2203 *
2204 * IAC commands come in sets of 3 bytes with the exception of the
2205 * "IAC BREAK" command and the double IAC.
2206 */
2207
2208 int i;
2209 int j = 0;
2210
2211 for (i = 0; i < *size; i++) {
2212 if (s->do_telnetopt > 1) {
2213 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2214 /* Double IAC means send an IAC */
2215 if (j != i)
2216 buf[j] = buf[i];
2217 j++;
2218 s->do_telnetopt = 1;
2219 } else {
2220 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2221 /* Handle IAC break commands by sending a serial break */
a425d23f 2222 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
6f97dba0
AL
2223 s->do_telnetopt++;
2224 }
2225 s->do_telnetopt++;
2226 }
2227 if (s->do_telnetopt >= 4) {
2228 s->do_telnetopt = 1;
2229 }
2230 } else {
2231 if ((unsigned char)buf[i] == IAC) {
2232 s->do_telnetopt = 2;
2233 } else {
2234 if (j != i)
2235 buf[j] = buf[i];
2236 j++;
2237 }
2238 }
2239 }
2240 *size = j;
2241}
2242
7d174059
MM
2243static int tcp_get_msgfd(CharDriverState *chr)
2244{
2245 TCPCharDriver *s = chr->opaque;
e53f27b9
PB
2246 int fd = s->msgfd;
2247 s->msgfd = -1;
2248 return fd;
7d174059
MM
2249}
2250
73bcc2ac 2251#ifndef _WIN32
7d174059
MM
2252static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2253{
2254 TCPCharDriver *s = chr->opaque;
2255 struct cmsghdr *cmsg;
2256
2257 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2258 int fd;
2259
2260 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2261 cmsg->cmsg_level != SOL_SOCKET ||
2262 cmsg->cmsg_type != SCM_RIGHTS)
2263 continue;
2264
2265 fd = *((int *)CMSG_DATA(cmsg));
2266 if (fd < 0)
2267 continue;
2268
06138651
CB
2269#ifndef MSG_CMSG_CLOEXEC
2270 qemu_set_cloexec(fd);
2271#endif
7d174059
MM
2272 if (s->msgfd != -1)
2273 close(s->msgfd);
2274 s->msgfd = fd;
2275 }
2276}
2277
9977c894
MM
2278static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2279{
2280 TCPCharDriver *s = chr->opaque;
7cba04f6 2281 struct msghdr msg = { NULL, };
9977c894 2282 struct iovec iov[1];
7d174059
MM
2283 union {
2284 struct cmsghdr cmsg;
2285 char control[CMSG_SPACE(sizeof(int))];
2286 } msg_control;
06138651 2287 int flags = 0;
7d174059 2288 ssize_t ret;
9977c894
MM
2289
2290 iov[0].iov_base = buf;
2291 iov[0].iov_len = len;
2292
2293 msg.msg_iov = iov;
2294 msg.msg_iovlen = 1;
7d174059
MM
2295 msg.msg_control = &msg_control;
2296 msg.msg_controllen = sizeof(msg_control);
2297
06138651
CB
2298#ifdef MSG_CMSG_CLOEXEC
2299 flags |= MSG_CMSG_CLOEXEC;
2300#endif
2301 ret = recvmsg(s->fd, &msg, flags);
2302 if (ret > 0 && s->is_unix) {
7d174059 2303 unix_process_msgfd(chr, &msg);
06138651 2304 }
9977c894 2305
7d174059 2306 return ret;
9977c894
MM
2307}
2308#else
2309static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2310{
2311 TCPCharDriver *s = chr->opaque;
00aa0040 2312 return qemu_recv(s->fd, buf, len, 0);
9977c894
MM
2313}
2314#endif
2315
6f97dba0
AL
2316static void tcp_chr_read(void *opaque)
2317{
2318 CharDriverState *chr = opaque;
2319 TCPCharDriver *s = chr->opaque;
9bd7854e 2320 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
2321 int len, size;
2322
2323 if (!s->connected || s->max_size <= 0)
2324 return;
2325 len = sizeof(buf);
2326 if (len > s->max_size)
2327 len = s->max_size;
9977c894 2328 size = tcp_chr_recv(chr, (void *)buf, len);
6f97dba0
AL
2329 if (size == 0) {
2330 /* connection closed */
2331 s->connected = 0;
2332 if (s->listen_fd >= 0) {
069c159e 2333 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
6f97dba0 2334 }
069c159e 2335 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
6f97dba0
AL
2336 closesocket(s->fd);
2337 s->fd = -1;
a425d23f 2338 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
2339 } else if (size > 0) {
2340 if (s->do_telnetopt)
2341 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2342 if (size > 0)
fa5efccb 2343 qemu_chr_be_write(chr, buf, size);
6f97dba0
AL
2344 }
2345}
2346
68c18d1c
BS
2347#ifndef _WIN32
2348CharDriverState *qemu_chr_open_eventfd(int eventfd)
2349{
6cbf4c8c 2350 return qemu_chr_open_fd(eventfd, eventfd);
6cbf4c8c 2351}
68c18d1c 2352#endif
6cbf4c8c 2353
6f97dba0
AL
2354static void tcp_chr_connect(void *opaque)
2355{
2356 CharDriverState *chr = opaque;
2357 TCPCharDriver *s = chr->opaque;
2358
2359 s->connected = 1;
bbdd2ad0
DG
2360 if (s->fd >= 0) {
2361 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2362 tcp_chr_read, NULL, chr);
2363 }
127338e6 2364 qemu_chr_generic_open(chr);
6f97dba0
AL
2365}
2366
2367#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2368static void tcp_chr_telnet_init(int fd)
2369{
2370 char buf[3];
2371 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2372 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2373 send(fd, (char *)buf, 3, 0);
2374 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2375 send(fd, (char *)buf, 3, 0);
2376 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2377 send(fd, (char *)buf, 3, 0);
2378 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2379 send(fd, (char *)buf, 3, 0);
2380}
2381
2382static void socket_set_nodelay(int fd)
2383{
2384 int val = 1;
2385 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2386}
2387
13661089
DB
2388static int tcp_chr_add_client(CharDriverState *chr, int fd)
2389{
2390 TCPCharDriver *s = chr->opaque;
2391 if (s->fd != -1)
2392 return -1;
2393
2394 socket_set_nonblock(fd);
2395 if (s->do_nodelay)
2396 socket_set_nodelay(fd);
2397 s->fd = fd;
069c159e 2398 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
13661089
DB
2399 tcp_chr_connect(chr);
2400
2401 return 0;
2402}
2403
6f97dba0
AL
2404static void tcp_chr_accept(void *opaque)
2405{
2406 CharDriverState *chr = opaque;
2407 TCPCharDriver *s = chr->opaque;
2408 struct sockaddr_in saddr;
2409#ifndef _WIN32
2410 struct sockaddr_un uaddr;
2411#endif
2412 struct sockaddr *addr;
2413 socklen_t len;
2414 int fd;
2415
2416 for(;;) {
2417#ifndef _WIN32
2418 if (s->is_unix) {
2419 len = sizeof(uaddr);
2420 addr = (struct sockaddr *)&uaddr;
2421 } else
2422#endif
2423 {
2424 len = sizeof(saddr);
2425 addr = (struct sockaddr *)&saddr;
2426 }
40ff6d7e 2427 fd = qemu_accept(s->listen_fd, addr, &len);
6f97dba0
AL
2428 if (fd < 0 && errno != EINTR) {
2429 return;
2430 } else if (fd >= 0) {
2431 if (s->do_telnetopt)
2432 tcp_chr_telnet_init(fd);
2433 break;
2434 }
2435 }
13661089
DB
2436 if (tcp_chr_add_client(chr, fd) < 0)
2437 close(fd);
6f97dba0
AL
2438}
2439
2440static void tcp_chr_close(CharDriverState *chr)
2441{
2442 TCPCharDriver *s = chr->opaque;
819f56b7 2443 if (s->fd >= 0) {
069c159e 2444 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
6f97dba0 2445 closesocket(s->fd);
819f56b7
AL
2446 }
2447 if (s->listen_fd >= 0) {
069c159e 2448 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
6f97dba0 2449 closesocket(s->listen_fd);
819f56b7 2450 }
7267c094 2451 g_free(s);
a425d23f 2452 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
2453}
2454
1f51470d 2455static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
6f97dba0
AL
2456{
2457 CharDriverState *chr = NULL;
2458 TCPCharDriver *s = NULL;
87d5f24f 2459 Error *local_err = NULL;
aeb2c47a
GH
2460 int fd = -1;
2461 int is_listen;
2462 int is_waitconnect;
2463 int do_nodelay;
2464 int is_unix;
2465 int is_telnet;
2466
2467 is_listen = qemu_opt_get_bool(opts, "server", 0);
2468 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2469 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2470 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2471 is_unix = qemu_opt_get(opts, "path") != NULL;
6f97dba0
AL
2472 if (!is_listen)
2473 is_waitconnect = 0;
2474
7267c094
AL
2475 chr = g_malloc0(sizeof(CharDriverState));
2476 s = g_malloc0(sizeof(TCPCharDriver));
6f97dba0 2477
f07b6003
AL
2478 if (is_unix) {
2479 if (is_listen) {
87d5f24f 2480 fd = unix_listen_opts(opts, &local_err);
f07b6003 2481 } else {
87d5f24f 2482 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
f07b6003
AL
2483 }
2484 } else {
2485 if (is_listen) {
87d5f24f 2486 fd = inet_listen_opts(opts, 0, &local_err);
f07b6003 2487 } else {
87d5f24f 2488 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
f07b6003
AL
2489 }
2490 }
a89dd6c3 2491 if (fd < 0) {
6f97dba0 2492 goto fail;
a89dd6c3 2493 }
6f97dba0
AL
2494
2495 if (!is_waitconnect)
2496 socket_set_nonblock(fd);
2497
2498 s->connected = 0;
2499 s->fd = -1;
2500 s->listen_fd = -1;
7d174059 2501 s->msgfd = -1;
6f97dba0
AL
2502 s->is_unix = is_unix;
2503 s->do_nodelay = do_nodelay && !is_unix;
2504
2505 chr->opaque = s;
2506 chr->chr_write = tcp_chr_write;
2507 chr->chr_close = tcp_chr_close;
7d174059 2508 chr->get_msgfd = tcp_get_msgfd;
13661089 2509 chr->chr_add_client = tcp_chr_add_client;
6f97dba0
AL
2510
2511 if (is_listen) {
6f97dba0 2512 s->listen_fd = fd;
069c159e 2513 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
6f97dba0
AL
2514 if (is_telnet)
2515 s->do_telnetopt = 1;
aeb2c47a 2516
6f97dba0 2517 } else {
f07b6003 2518 s->connected = 1;
6f97dba0
AL
2519 s->fd = fd;
2520 socket_set_nodelay(fd);
f07b6003 2521 tcp_chr_connect(chr);
6f97dba0
AL
2522 }
2523
aeb2c47a 2524 /* for "info chardev" monitor command */
7267c094 2525 chr->filename = g_malloc(256);
aeb2c47a
GH
2526 if (is_unix) {
2527 snprintf(chr->filename, 256, "unix:%s%s",
2528 qemu_opt_get(opts, "path"),
2529 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2530 } else if (is_telnet) {
2531 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2532 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2533 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2534 } else {
2535 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2536 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2537 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2538 }
2539
6f97dba0 2540 if (is_listen && is_waitconnect) {
f07b6003 2541 printf("QEMU waiting for connection on: %s\n",
aeb2c47a 2542 chr->filename);
6f97dba0
AL
2543 tcp_chr_accept(chr);
2544 socket_set_nonblock(s->listen_fd);
2545 }
1f51470d 2546 return chr;
aeb2c47a 2547
6f97dba0 2548 fail:
87d5f24f
PB
2549 if (local_err) {
2550 qerror_report_err(local_err);
2551 error_free(local_err);
2552 }
2553 if (fd >= 0) {
6f97dba0 2554 closesocket(fd);
87d5f24f 2555 }
7267c094
AL
2556 g_free(s);
2557 g_free(chr);
1f51470d 2558 return NULL;
6f97dba0
AL
2559}
2560
999bd67c
LC
2561/***********************************************************/
2562/* Memory chardev */
2563typedef struct {
2564 size_t outbuf_size;
2565 size_t outbuf_capacity;
2566 uint8_t *outbuf;
2567} MemoryDriver;
2568
2569static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2570{
2571 MemoryDriver *d = chr->opaque;
2572
2573 /* TODO: the QString implementation has the same code, we should
2574 * introduce a generic way to do this in cutils.c */
2575 if (d->outbuf_capacity < d->outbuf_size + len) {
2576 /* grow outbuf */
2577 d->outbuf_capacity += len;
2578 d->outbuf_capacity *= 2;
7267c094 2579 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
999bd67c
LC
2580 }
2581
2582 memcpy(d->outbuf + d->outbuf_size, buf, len);
2583 d->outbuf_size += len;
2584
2585 return len;
2586}
2587
2588void qemu_chr_init_mem(CharDriverState *chr)
2589{
2590 MemoryDriver *d;
2591
7267c094 2592 d = g_malloc(sizeof(*d));
999bd67c
LC
2593 d->outbuf_size = 0;
2594 d->outbuf_capacity = 4096;
7267c094 2595 d->outbuf = g_malloc0(d->outbuf_capacity);
999bd67c
LC
2596
2597 memset(chr, 0, sizeof(*chr));
2598 chr->opaque = d;
2599 chr->chr_write = mem_chr_write;
2600}
2601
2602QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2603{
2604 MemoryDriver *d = chr->opaque;
2605 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2606}
2607
70f24fb6 2608/* NOTE: this driver can not be closed with qemu_chr_delete()! */
999bd67c
LC
2609void qemu_chr_close_mem(CharDriverState *chr)
2610{
2611 MemoryDriver *d = chr->opaque;
2612
7267c094
AL
2613 g_free(d->outbuf);
2614 g_free(chr->opaque);
999bd67c
LC
2615 chr->opaque = NULL;
2616 chr->chr_write = NULL;
2617}
2618
2619size_t qemu_chr_mem_osize(const CharDriverState *chr)
2620{
2621 const MemoryDriver *d = chr->opaque;
2622 return d->outbuf_size;
2623}
2624
33521634 2625QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
191bc01b 2626{
6ea314d9 2627 char host[65], port[33], width[8], height[8];
aeb2c47a 2628 int pos;
7d31544f 2629 const char *p;
191bc01b 2630 QemuOpts *opts;
8be7e7e4 2631 Error *local_err = NULL;
191bc01b 2632
8be7e7e4
LC
2633 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2634 if (error_is_set(&local_err)) {
2635 qerror_report_err(local_err);
2636 error_free(local_err);
191bc01b 2637 return NULL;
8be7e7e4 2638 }
191bc01b 2639
7591c5c1
GH
2640 if (strstart(filename, "mon:", &p)) {
2641 filename = p;
2642 qemu_opt_set(opts, "mux", "on");
2643 }
2644
f0457e8d
GH
2645 if (strcmp(filename, "null") == 0 ||
2646 strcmp(filename, "pty") == 0 ||
2647 strcmp(filename, "msmouse") == 0 ||
dc1c21e6 2648 strcmp(filename, "braille") == 0 ||
f0457e8d 2649 strcmp(filename, "stdio") == 0) {
4490dadf 2650 qemu_opt_set(opts, "backend", filename);
191bc01b
GH
2651 return opts;
2652 }
6ea314d9
GH
2653 if (strstart(filename, "vc", &p)) {
2654 qemu_opt_set(opts, "backend", "vc");
2655 if (*p == ':') {
2656 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2657 /* pixels */
2658 qemu_opt_set(opts, "width", width);
2659 qemu_opt_set(opts, "height", height);
2660 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2661 /* chars */
2662 qemu_opt_set(opts, "cols", width);
2663 qemu_opt_set(opts, "rows", height);
2664 } else {
2665 goto fail;
2666 }
2667 }
2668 return opts;
2669 }
d6c983cd
GH
2670 if (strcmp(filename, "con:") == 0) {
2671 qemu_opt_set(opts, "backend", "console");
2672 return opts;
2673 }
48b76496
GH
2674 if (strstart(filename, "COM", NULL)) {
2675 qemu_opt_set(opts, "backend", "serial");
2676 qemu_opt_set(opts, "path", filename);
2677 return opts;
2678 }
7d31544f
GH
2679 if (strstart(filename, "file:", &p)) {
2680 qemu_opt_set(opts, "backend", "file");
2681 qemu_opt_set(opts, "path", p);
2682 return opts;
2683 }
2684 if (strstart(filename, "pipe:", &p)) {
2685 qemu_opt_set(opts, "backend", "pipe");
2686 qemu_opt_set(opts, "path", p);
2687 return opts;
2688 }
aeb2c47a
GH
2689 if (strstart(filename, "tcp:", &p) ||
2690 strstart(filename, "telnet:", &p)) {
2691 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2692 host[0] = 0;
2693 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2694 goto fail;
2695 }
2696 qemu_opt_set(opts, "backend", "socket");
2697 qemu_opt_set(opts, "host", host);
2698 qemu_opt_set(opts, "port", port);
2699 if (p[pos] == ',') {
2700 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2701 goto fail;
2702 }
2703 if (strstart(filename, "telnet:", &p))
2704 qemu_opt_set(opts, "telnet", "on");
2705 return opts;
2706 }
7e1b35b4
GH
2707 if (strstart(filename, "udp:", &p)) {
2708 qemu_opt_set(opts, "backend", "udp");
2709 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2710 host[0] = 0;
39324ca4 2711 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
7e1b35b4
GH
2712 goto fail;
2713 }
2714 }
2715 qemu_opt_set(opts, "host", host);
2716 qemu_opt_set(opts, "port", port);
2717 if (p[pos] == '@') {
2718 p += pos + 1;
2719 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2720 host[0] = 0;
2721 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
7e1b35b4
GH
2722 goto fail;
2723 }
2724 }
2725 qemu_opt_set(opts, "localaddr", host);
2726 qemu_opt_set(opts, "localport", port);
2727 }
2728 return opts;
2729 }
aeb2c47a
GH
2730 if (strstart(filename, "unix:", &p)) {
2731 qemu_opt_set(opts, "backend", "socket");
2732 if (qemu_opts_do_parse(opts, p, "path") != 0)
2733 goto fail;
2734 return opts;
2735 }
48b76496
GH
2736 if (strstart(filename, "/dev/parport", NULL) ||
2737 strstart(filename, "/dev/ppi", NULL)) {
2738 qemu_opt_set(opts, "backend", "parport");
2739 qemu_opt_set(opts, "path", filename);
2740 return opts;
2741 }
2742 if (strstart(filename, "/dev/", NULL)) {
2743 qemu_opt_set(opts, "backend", "tty");
2744 qemu_opt_set(opts, "path", filename);
2745 return opts;
2746 }
191bc01b 2747
aeb2c47a 2748fail:
191bc01b
GH
2749 qemu_opts_del(opts);
2750 return NULL;
2751}
2752
2753static const struct {
2754 const char *name;
1f51470d 2755 CharDriverState *(*open)(QemuOpts *opts);
191bc01b
GH
2756} backend_table[] = {
2757 { .name = "null", .open = qemu_chr_open_null },
aeb2c47a 2758 { .name = "socket", .open = qemu_chr_open_socket },
7e1b35b4 2759 { .name = "udp", .open = qemu_chr_open_udp },
f0457e8d 2760 { .name = "msmouse", .open = qemu_chr_open_msmouse },
6ea314d9 2761 { .name = "vc", .open = text_console_init },
7d31544f
GH
2762#ifdef _WIN32
2763 { .name = "file", .open = qemu_chr_open_win_file_out },
2764 { .name = "pipe", .open = qemu_chr_open_win_pipe },
d6c983cd 2765 { .name = "console", .open = qemu_chr_open_win_con },
48b76496 2766 { .name = "serial", .open = qemu_chr_open_win },
db418a0a 2767 { .name = "stdio", .open = qemu_chr_open_win_stdio },
7d31544f
GH
2768#else
2769 { .name = "file", .open = qemu_chr_open_file_out },
2770 { .name = "pipe", .open = qemu_chr_open_pipe },
3c17affb 2771 { .name = "stdio", .open = qemu_chr_open_stdio },
7d31544f 2772#endif
dc1c21e6
GH
2773#ifdef CONFIG_BRLAPI
2774 { .name = "braille", .open = chr_baum_init },
2775#endif
e551498e 2776#ifdef HAVE_CHARDEV_TTY
48b76496 2777 { .name = "tty", .open = qemu_chr_open_tty },
d59044ef 2778 { .name = "serial", .open = qemu_chr_open_tty },
e551498e 2779 { .name = "pty", .open = qemu_chr_open_pty },
48b76496 2780#endif
e551498e 2781#ifdef HAVE_CHARDEV_PARPORT
48b76496
GH
2782 { .name = "parport", .open = qemu_chr_open_pp },
2783#endif
cbcc6336
AL
2784#ifdef CONFIG_SPICE
2785 { .name = "spicevmc", .open = qemu_chr_open_spice },
5a49d3e9
MAL
2786#if SPICE_SERVER_VERSION >= 0x000c02
2787 { .name = "spiceport", .open = qemu_chr_open_spice_port },
2788#endif
cbcc6336 2789#endif
191bc01b
GH
2790};
2791
f69554b9 2792CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
bd2d80b2
GH
2793 void (*init)(struct CharDriverState *s),
2794 Error **errp)
191bc01b
GH
2795{
2796 CharDriverState *chr;
2797 int i;
2798
2799 if (qemu_opts_id(opts) == NULL) {
bd2d80b2 2800 error_setg(errp, "chardev: no id specified\n");
2274ae9d 2801 goto err;
191bc01b
GH
2802 }
2803
1bbd185f 2804 if (qemu_opt_get(opts, "backend") == NULL) {
bd2d80b2
GH
2805 error_setg(errp, "chardev: \"%s\" missing backend\n",
2806 qemu_opts_id(opts));
2274ae9d 2807 goto err;
1bbd185f 2808 }
191bc01b
GH
2809 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2810 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2811 break;
2812 }
2813 if (i == ARRAY_SIZE(backend_table)) {
bd2d80b2
GH
2814 error_setg(errp, "chardev: backend \"%s\" not found\n",
2815 qemu_opt_get(opts, "backend"));
2274ae9d 2816 goto err;
191bc01b
GH
2817 }
2818
1f51470d
MA
2819 chr = backend_table[i].open(opts);
2820 if (!chr) {
bd2d80b2
GH
2821 error_setg(errp, "chardev: opening backend \"%s\" failed\n",
2822 qemu_opt_get(opts, "backend"));
2274ae9d 2823 goto err;
191bc01b
GH
2824 }
2825
2826 if (!chr->filename)
7267c094 2827 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
191bc01b 2828 chr->init = init;
72cf2d4f 2829 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
7591c5c1
GH
2830
2831 if (qemu_opt_get_bool(opts, "mux", 0)) {
2832 CharDriverState *base = chr;
2833 int len = strlen(qemu_opts_id(opts)) + 6;
7267c094 2834 base->label = g_malloc(len);
7591c5c1
GH
2835 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2836 chr = qemu_chr_open_mux(base);
2837 chr->filename = base->filename;
d5b27167 2838 chr->avail_connections = MAX_MUX;
72cf2d4f 2839 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
d5b27167
KK
2840 } else {
2841 chr->avail_connections = 1;
7591c5c1 2842 }
7267c094 2843 chr->label = g_strdup(qemu_opts_id(opts));
2274ae9d 2844 chr->opts = opts;
191bc01b 2845 return chr;
2274ae9d
GH
2846
2847err:
2848 qemu_opts_del(opts);
2849 return NULL;
191bc01b
GH
2850}
2851
27143a44 2852CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
6f97dba0
AL
2853{
2854 const char *p;
2855 CharDriverState *chr;
191bc01b 2856 QemuOpts *opts;
bd2d80b2 2857 Error *err = NULL;
191bc01b 2858
c845f401
GH
2859 if (strstart(filename, "chardev:", &p)) {
2860 return qemu_chr_find(p);
2861 }
2862
191bc01b 2863 opts = qemu_chr_parse_compat(label, filename);
7e1b35b4
GH
2864 if (!opts)
2865 return NULL;
6f97dba0 2866
bd2d80b2
GH
2867 chr = qemu_chr_new_from_opts(opts, init, &err);
2868 if (error_is_set(&err)) {
2869 fprintf(stderr, "%s\n", error_get_pretty(err));
2870 error_free(err);
2871 }
7e1b35b4
GH
2872 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2873 monitor_init(chr, MONITOR_USE_READLINE);
6f97dba0
AL
2874 }
2875 return chr;
2876}
2877
15f31519 2878void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
c48855e1
PB
2879{
2880 if (chr->chr_set_echo) {
2881 chr->chr_set_echo(chr, echo);
2882 }
2883}
2884
c9d830ed 2885void qemu_chr_fe_open(struct CharDriverState *chr)
7c32c4fe
HG
2886{
2887 if (chr->chr_guest_open) {
2888 chr->chr_guest_open(chr);
2889 }
2890}
2891
2817822d 2892void qemu_chr_fe_close(struct CharDriverState *chr)
7c32c4fe
HG
2893{
2894 if (chr->chr_guest_close) {
2895 chr->chr_guest_close(chr);
2896 }
2897}
2898
70f24fb6 2899void qemu_chr_delete(CharDriverState *chr)
6f97dba0 2900{
72cf2d4f 2901 QTAILQ_REMOVE(&chardevs, chr, next);
2274ae9d 2902 if (chr->chr_close) {
6f97dba0 2903 chr->chr_close(chr);
2274ae9d 2904 }
7267c094
AL
2905 g_free(chr->filename);
2906 g_free(chr->label);
2274ae9d
GH
2907 if (chr->opts) {
2908 qemu_opts_del(chr->opts);
2909 }
7267c094 2910 g_free(chr);
6f97dba0
AL
2911}
2912
c5a415a0 2913ChardevInfoList *qmp_query_chardev(Error **errp)
6f97dba0 2914{
c5a415a0 2915 ChardevInfoList *chr_list = NULL;
6f97dba0
AL
2916 CharDriverState *chr;
2917
72cf2d4f 2918 QTAILQ_FOREACH(chr, &chardevs, next) {
c5a415a0
LC
2919 ChardevInfoList *info = g_malloc0(sizeof(*info));
2920 info->value = g_malloc0(sizeof(*info->value));
2921 info->value->label = g_strdup(chr->label);
2922 info->value->filename = g_strdup(chr->filename);
2923
2924 info->next = chr_list;
2925 chr_list = info;
6f97dba0 2926 }
588b3832 2927
c5a415a0 2928 return chr_list;
6f97dba0 2929}
c845f401
GH
2930
2931CharDriverState *qemu_chr_find(const char *name)
2932{
2933 CharDriverState *chr;
2934
72cf2d4f 2935 QTAILQ_FOREACH(chr, &chardevs, next) {
c845f401
GH
2936 if (strcmp(chr->label, name) != 0)
2937 continue;
2938 return chr;
2939 }
2940 return NULL;
2941}
0beb4942
AL
2942
2943/* Get a character (serial) device interface. */
2944CharDriverState *qemu_char_get_next_serial(void)
2945{
2946 static int next_serial;
2947
2948 /* FIXME: This function needs to go away: use chardev properties! */
2949 return serial_hds[next_serial++];
2950}
2951
4d454574
PB
2952QemuOptsList qemu_chardev_opts = {
2953 .name = "chardev",
2954 .implied_opt_name = "backend",
2955 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
2956 .desc = {
2957 {
2958 .name = "backend",
2959 .type = QEMU_OPT_STRING,
2960 },{
2961 .name = "path",
2962 .type = QEMU_OPT_STRING,
2963 },{
2964 .name = "host",
2965 .type = QEMU_OPT_STRING,
2966 },{
2967 .name = "port",
2968 .type = QEMU_OPT_STRING,
2969 },{
2970 .name = "localaddr",
2971 .type = QEMU_OPT_STRING,
2972 },{
2973 .name = "localport",
2974 .type = QEMU_OPT_STRING,
2975 },{
2976 .name = "to",
2977 .type = QEMU_OPT_NUMBER,
2978 },{
2979 .name = "ipv4",
2980 .type = QEMU_OPT_BOOL,
2981 },{
2982 .name = "ipv6",
2983 .type = QEMU_OPT_BOOL,
2984 },{
2985 .name = "wait",
2986 .type = QEMU_OPT_BOOL,
2987 },{
2988 .name = "server",
2989 .type = QEMU_OPT_BOOL,
2990 },{
2991 .name = "delay",
2992 .type = QEMU_OPT_BOOL,
2993 },{
2994 .name = "telnet",
2995 .type = QEMU_OPT_BOOL,
2996 },{
2997 .name = "width",
2998 .type = QEMU_OPT_NUMBER,
2999 },{
3000 .name = "height",
3001 .type = QEMU_OPT_NUMBER,
3002 },{
3003 .name = "cols",
3004 .type = QEMU_OPT_NUMBER,
3005 },{
3006 .name = "rows",
3007 .type = QEMU_OPT_NUMBER,
3008 },{
3009 .name = "mux",
3010 .type = QEMU_OPT_BOOL,
3011 },{
3012 .name = "signal",
3013 .type = QEMU_OPT_BOOL,
3014 },{
3015 .name = "name",
3016 .type = QEMU_OPT_STRING,
3017 },{
3018 .name = "debug",
3019 .type = QEMU_OPT_NUMBER,
3020 },
3021 { /* end of list */ }
3022 },
3023};
f1a1a356 3024
ffbdbe59
GH
3025#ifdef _WIN32
3026
3027static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3028{
3029 HANDLE out;
3030
3031 if (file->in) {
3032 error_setg(errp, "input file not supported");
3033 return NULL;
3034 }
3035
3036 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3037 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3038 if (out == INVALID_HANDLE_VALUE) {
3039 error_setg(errp, "open %s failed", file->out);
3040 return NULL;
3041 }
3042 return qemu_chr_open_win_file(out);
3043}
3044
d59044ef
GH
3045static CharDriverState *qmp_chardev_open_port(ChardevPort *port, Error **errp)
3046{
3047 switch (port->type) {
3048 case CHARDEV_PORT_KIND_SERIAL:
3049 return qemu_chr_open_win_path(port->device);
3050 default:
3051 error_setg(errp, "unknown chardev port (%d)", port->type);
3052 return NULL;
3053 }
3054}
3055
ffbdbe59
GH
3056#else /* WIN32 */
3057
3058static int qmp_chardev_open_file_source(char *src, int flags,
3059 Error **errp)
3060{
3061 int fd = -1;
3062
3063 TFR(fd = qemu_open(src, flags, 0666));
3064 if (fd == -1) {
3065 error_setg(errp, "open %s: %s", src, strerror(errno));
3066 }
3067 return fd;
3068}
3069
3070static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3071{
3072 int flags, in = -1, out = -1;
3073
3074 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3075 out = qmp_chardev_open_file_source(file->out, flags, errp);
3076 if (error_is_set(errp)) {
3077 return NULL;
3078 }
3079
3080 if (file->in) {
3081 flags = O_RDONLY;
3082 in = qmp_chardev_open_file_source(file->in, flags, errp);
3083 if (error_is_set(errp)) {
3084 qemu_close(out);
3085 return NULL;
3086 }
3087 }
3088
3089 return qemu_chr_open_fd(in, out);
3090}
3091
d59044ef
GH
3092static CharDriverState *qmp_chardev_open_port(ChardevPort *port, Error **errp)
3093{
3094 int flags, fd;
3095
3096 switch (port->type) {
3097#ifdef HAVE_CHARDEV_TTY
3098 case CHARDEV_PORT_KIND_SERIAL:
3099 flags = O_RDWR;
3100 fd = qmp_chardev_open_file_source(port->device, flags, errp);
3101 if (error_is_set(errp)) {
3102 return NULL;
3103 }
3104 socket_set_nonblock(fd);
3105 return qemu_chr_open_tty_fd(fd);
3106#endif
3107 default:
3108 error_setg(errp, "unknown chardev port (%d)", port->type);
3109 return NULL;
3110 }
3111}
3112
ffbdbe59
GH
3113#endif /* WIN32 */
3114
f1a1a356
GH
3115ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3116 Error **errp)
3117{
3118 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3119 CharDriverState *chr = NULL;
3120
3121 chr = qemu_chr_find(id);
3122 if (chr) {
3123 error_setg(errp, "Chardev '%s' already exists", id);
3124 g_free(ret);
3125 return NULL;
3126 }
3127
3128 switch (backend->kind) {
ffbdbe59
GH
3129 case CHARDEV_BACKEND_KIND_FILE:
3130 chr = qmp_chardev_open_file(backend->file, errp);
3131 break;
d59044ef
GH
3132 case CHARDEV_BACKEND_KIND_PORT:
3133 chr = qmp_chardev_open_port(backend->port, errp);
3134 break;
f1a1a356
GH
3135 case CHARDEV_BACKEND_KIND_NULL:
3136 chr = qemu_chr_open_null(NULL);
3137 break;
3138 default:
3139 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3140 break;
3141 }
3142
3143 if (chr == NULL && !error_is_set(errp)) {
3144 error_setg(errp, "Failed to create chardev");
3145 }
3146 if (chr) {
3147 chr->label = g_strdup(id);
3148 chr->avail_connections = 1;
3149 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3150 return ret;
3151 } else {
3152 g_free(ret);
3153 return NULL;
3154 }
3155}
3156
3157void qmp_chardev_remove(const char *id, Error **errp)
3158{
3159 CharDriverState *chr;
3160
3161 chr = qemu_chr_find(id);
3162 if (NULL == chr) {
3163 error_setg(errp, "Chardev '%s' not found", id);
3164 return;
3165 }
3166 if (chr->chr_can_read || chr->chr_read ||
3167 chr->chr_event || chr->handler_opaque) {
3168 error_setg(errp, "Chardev '%s' is busy", id);
3169 return;
3170 }
3171 qemu_chr_delete(chr);
3172}