]> git.proxmox.com Git - qemu.git/blame - qemu-char.c
compiler: fix warning with GCC 4.8.0
[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 30#include "hw/usb.h"
c5a415a0 31#include "qmp-commands.h"
6f97dba0
AL
32
33#include <unistd.h>
34#include <fcntl.h>
6f97dba0
AL
35#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
24646c7e 46#include <sys/resource.h>
6f97dba0
AL
47#include <sys/socket.h>
48#include <netinet/in.h>
24646c7e 49#include <net/if.h>
24646c7e 50#include <arpa/inet.h>
6f97dba0
AL
51#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
71e72a19 54#ifdef CONFIG_BSD
6f97dba0 55#include <sys/stat.h>
a167ba50
AJ
56#if defined(__GLIBC__)
57#include <pty.h>
3294ce18
MT
58#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
59#include <libutil.h>
60#else
61#include <util.h>
a167ba50 62#endif
3294ce18
MT
63#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64#include <dev/ppbus/ppi.h>
65#include <dev/ppbus/ppbconf.h>
c5e97233 66#elif defined(__DragonFly__)
c5e97233
BS
67#include <dev/misc/ppi/ppi.h>
68#include <bus/ppbus/ppbconf.h>
6f97dba0 69#endif
bbe813a2 70#else
6f97dba0 71#ifdef __linux__
6f97dba0
AL
72#include <pty.h>
73
74#include <linux/ppdev.h>
75#include <linux/parport.h>
76#endif
77#ifdef __sun__
78#include <sys/stat.h>
79#include <sys/ethernet.h>
80#include <sys/sockio.h>
81#include <netinet/arp.h>
82#include <netinet/in.h>
83#include <netinet/in_systm.h>
84#include <netinet/ip.h>
85#include <netinet/ip_icmp.h> // must come after ip.h
86#include <netinet/udp.h>
87#include <netinet/tcp.h>
88#include <net/if.h>
89#include <syslog.h>
90#include <stropts.h>
91#endif
92#endif
93#endif
94
1de7afc9 95#include "qemu/sockets.h"
cbcc6336 96#include "ui/qemu-spice.h"
6f97dba0 97
9bd7854e
AS
98#define READ_BUF_LEN 4096
99
6f97dba0
AL
100/***********************************************************/
101/* character device */
102
72cf2d4f
BS
103static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
2970a6c9 105
a425d23f 106void qemu_chr_be_event(CharDriverState *s, int event)
6f97dba0 107{
73cdf3f2
AG
108 /* Keep track if the char device is open */
109 switch (event) {
110 case CHR_EVENT_OPENED:
111 s->opened = 1;
112 break;
113 case CHR_EVENT_CLOSED:
114 s->opened = 0;
115 break;
116 }
117
6f97dba0
AL
118 if (!s->chr_event)
119 return;
120 s->chr_event(s->handler_opaque, event);
121}
122
9f939df9 123static gboolean qemu_chr_generic_open_bh(gpointer opaque)
6f97dba0
AL
124{
125 CharDriverState *s = opaque;
a425d23f 126 qemu_chr_be_event(s, CHR_EVENT_OPENED);
9f939df9
AL
127 s->idle_tag = 0;
128 return FALSE;
6f97dba0
AL
129}
130
127338e6 131void qemu_chr_generic_open(CharDriverState *s)
6f97dba0 132{
9f939df9
AL
133 if (s->idle_tag == 0) {
134 s->idle_tag = g_idle_add(qemu_chr_generic_open_bh, s);
6f97dba0
AL
135 }
136}
137
2cc6e0a1 138int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
6f97dba0
AL
139{
140 return s->chr_write(s, buf, len);
141}
142
cd18720a
AL
143int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
144{
145 int offset = 0;
146 int res;
147
148 while (offset < len) {
149 do {
150 res = s->chr_write(s, buf + offset, len - offset);
151 if (res == -1 && errno == EAGAIN) {
152 g_usleep(100);
153 }
154 } while (res == -1 && errno == EAGAIN);
155
156 if (res == 0) {
157 break;
158 }
159
160 if (res < 0) {
161 return res;
162 }
163
164 offset += res;
165 }
166
167 return offset;
168}
169
41084f1b 170int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
6f97dba0
AL
171{
172 if (!s->chr_ioctl)
173 return -ENOTSUP;
174 return s->chr_ioctl(s, cmd, arg);
175}
176
909cda12 177int qemu_chr_be_can_write(CharDriverState *s)
6f97dba0
AL
178{
179 if (!s->chr_can_read)
180 return 0;
181 return s->chr_can_read(s->handler_opaque);
182}
183
fa5efccb 184void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
6f97dba0 185{
ac310734
SW
186 if (s->chr_read) {
187 s->chr_read(s->handler_opaque, buf, len);
188 }
6f97dba0
AL
189}
190
74c0d6f0 191int qemu_chr_fe_get_msgfd(CharDriverState *s)
7d174059
MM
192{
193 return s->get_msgfd ? s->get_msgfd(s) : -1;
194}
195
13661089
DB
196int qemu_chr_add_client(CharDriverState *s, int fd)
197{
198 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
199}
200
6f97dba0
AL
201void qemu_chr_accept_input(CharDriverState *s)
202{
203 if (s->chr_accept_input)
204 s->chr_accept_input(s);
98c8ee1d 205 qemu_notify_event();
6f97dba0
AL
206}
207
e7e71b0e 208void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
6f97dba0 209{
9bd7854e 210 char buf[READ_BUF_LEN];
6f97dba0
AL
211 va_list ap;
212 va_start(ap, fmt);
213 vsnprintf(buf, sizeof(buf), fmt, ap);
2cc6e0a1 214 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
6f97dba0
AL
215 va_end(ap);
216}
217
6f97dba0 218void qemu_chr_add_handlers(CharDriverState *s,
7b27a769 219 IOCanReadHandler *fd_can_read,
6f97dba0
AL
220 IOReadHandler *fd_read,
221 IOEventHandler *fd_event,
222 void *opaque)
223{
da7d998b 224 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
2d6c1ef4 225 /* chr driver being released. */
d5b27167 226 ++s->avail_connections;
2d6c1ef4 227 }
6f97dba0
AL
228 s->chr_can_read = fd_can_read;
229 s->chr_read = fd_read;
230 s->chr_event = fd_event;
231 s->handler_opaque = opaque;
232 if (s->chr_update_read_handler)
233 s->chr_update_read_handler(s);
73cdf3f2
AG
234
235 /* We're connecting to an already opened device, so let's make sure we
236 also get the open event */
237 if (s->opened) {
238 qemu_chr_generic_open(s);
239 }
6f97dba0
AL
240}
241
242static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
243{
244 return len;
245}
246
80dca9e6 247static CharDriverState *qemu_chr_open_null(void)
6f97dba0
AL
248{
249 CharDriverState *chr;
250
7267c094 251 chr = g_malloc0(sizeof(CharDriverState));
6f97dba0 252 chr->chr_write = null_chr_write;
1f51470d 253 return chr;
6f97dba0
AL
254}
255
256/* MUX driver for serial I/O splitting */
6f97dba0
AL
257#define MAX_MUX 4
258#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
259#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
260typedef struct {
7b27a769 261 IOCanReadHandler *chr_can_read[MAX_MUX];
6f97dba0
AL
262 IOReadHandler *chr_read[MAX_MUX];
263 IOEventHandler *chr_event[MAX_MUX];
264 void *ext_opaque[MAX_MUX];
265 CharDriverState *drv;
799f1f23 266 int focus;
6f97dba0
AL
267 int mux_cnt;
268 int term_got_escape;
269 int max_size;
a80bf99f
AL
270 /* Intermediate input buffer allows to catch escape sequences even if the
271 currently active device is not accepting any input - but only until it
272 is full as well. */
273 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
274 int prod[MAX_MUX];
275 int cons[MAX_MUX];
2d22959d 276 int timestamps;
4ab312f7 277 int linestart;
2d22959d 278 int64_t timestamps_start;
6f97dba0
AL
279} MuxDriver;
280
281
282static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
283{
284 MuxDriver *d = chr->opaque;
285 int ret;
2d22959d 286 if (!d->timestamps) {
6f97dba0
AL
287 ret = d->drv->chr_write(d->drv, buf, len);
288 } else {
289 int i;
290
291 ret = 0;
4ab312f7
JK
292 for (i = 0; i < len; i++) {
293 if (d->linestart) {
6f97dba0
AL
294 char buf1[64];
295 int64_t ti;
296 int secs;
297
7bd427d8 298 ti = qemu_get_clock_ms(rt_clock);
2d22959d
JK
299 if (d->timestamps_start == -1)
300 d->timestamps_start = ti;
301 ti -= d->timestamps_start;
a4bb1db8 302 secs = ti / 1000;
6f97dba0
AL
303 snprintf(buf1, sizeof(buf1),
304 "[%02d:%02d:%02d.%03d] ",
305 secs / 3600,
306 (secs / 60) % 60,
307 secs % 60,
a4bb1db8 308 (int)(ti % 1000));
6f97dba0 309 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
4ab312f7
JK
310 d->linestart = 0;
311 }
312 ret += d->drv->chr_write(d->drv, buf+i, 1);
313 if (buf[i] == '\n') {
314 d->linestart = 1;
6f97dba0
AL
315 }
316 }
317 }
318 return ret;
319}
320
321static const char * const mux_help[] = {
322 "% h print this help\n\r",
323 "% x exit emulator\n\r",
324 "% s save disk data back to file (if -snapshot)\n\r",
325 "% t toggle console timestamps\n\r"
326 "% b send break (magic sysrq)\n\r",
327 "% c switch between console and monitor\n\r",
328 "% % sends %\n\r",
329 NULL
330};
331
332int term_escape_char = 0x01; /* ctrl-a is used for escape */
333static void mux_print_help(CharDriverState *chr)
334{
335 int i, j;
336 char ebuf[15] = "Escape-Char";
337 char cbuf[50] = "\n\r";
338
339 if (term_escape_char > 0 && term_escape_char < 26) {
340 snprintf(cbuf, sizeof(cbuf), "\n\r");
341 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
342 } else {
343 snprintf(cbuf, sizeof(cbuf),
344 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
345 term_escape_char);
346 }
347 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
348 for (i = 0; mux_help[i] != NULL; i++) {
349 for (j=0; mux_help[i][j] != '\0'; j++) {
350 if (mux_help[i][j] == '%')
351 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
352 else
353 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
354 }
355 }
356}
357
2724b180
AL
358static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
359{
360 if (d->chr_event[mux_nr])
361 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
362}
363
6f97dba0
AL
364static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
365{
366 if (d->term_got_escape) {
367 d->term_got_escape = 0;
368 if (ch == term_escape_char)
369 goto send_char;
370 switch(ch) {
371 case '?':
372 case 'h':
373 mux_print_help(chr);
374 break;
375 case 'x':
376 {
377 const char *term = "QEMU: Terminated\n\r";
378 chr->chr_write(chr,(uint8_t *)term,strlen(term));
379 exit(0);
380 break;
381 }
382 case 's':
6ab4b5ab 383 bdrv_commit_all();
6f97dba0
AL
384 break;
385 case 'b':
a425d23f 386 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
6f97dba0
AL
387 break;
388 case 'c':
389 /* Switch to the next registered device */
799f1f23
GH
390 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
391 d->focus++;
392 if (d->focus >= d->mux_cnt)
393 d->focus = 0;
394 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
6f97dba0 395 break;
2d22959d
JK
396 case 't':
397 d->timestamps = !d->timestamps;
398 d->timestamps_start = -1;
4ab312f7 399 d->linestart = 0;
2d22959d 400 break;
6f97dba0
AL
401 }
402 } else if (ch == term_escape_char) {
403 d->term_got_escape = 1;
404 } else {
405 send_char:
406 return 1;
407 }
408 return 0;
409}
410
411static void mux_chr_accept_input(CharDriverState *chr)
412{
6f97dba0 413 MuxDriver *d = chr->opaque;
799f1f23 414 int m = d->focus;
6f97dba0 415
a80bf99f 416 while (d->prod[m] != d->cons[m] &&
6f97dba0
AL
417 d->chr_can_read[m] &&
418 d->chr_can_read[m](d->ext_opaque[m])) {
419 d->chr_read[m](d->ext_opaque[m],
a80bf99f 420 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
6f97dba0
AL
421 }
422}
423
424static int mux_chr_can_read(void *opaque)
425{
426 CharDriverState *chr = opaque;
427 MuxDriver *d = chr->opaque;
799f1f23 428 int m = d->focus;
6f97dba0 429
a80bf99f 430 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
6f97dba0 431 return 1;
a80bf99f
AL
432 if (d->chr_can_read[m])
433 return d->chr_can_read[m](d->ext_opaque[m]);
6f97dba0
AL
434 return 0;
435}
436
437static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
438{
439 CharDriverState *chr = opaque;
440 MuxDriver *d = chr->opaque;
799f1f23 441 int m = d->focus;
6f97dba0
AL
442 int i;
443
444 mux_chr_accept_input (opaque);
445
446 for(i = 0; i < size; i++)
447 if (mux_proc_byte(chr, d, buf[i])) {
a80bf99f 448 if (d->prod[m] == d->cons[m] &&
6f97dba0
AL
449 d->chr_can_read[m] &&
450 d->chr_can_read[m](d->ext_opaque[m]))
451 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
452 else
a80bf99f 453 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
6f97dba0
AL
454 }
455}
456
457static void mux_chr_event(void *opaque, int event)
458{
459 CharDriverState *chr = opaque;
460 MuxDriver *d = chr->opaque;
461 int i;
462
463 /* Send the event to all registered listeners */
464 for (i = 0; i < d->mux_cnt; i++)
2724b180 465 mux_chr_send_event(d, i, event);
6f97dba0
AL
466}
467
468static void mux_chr_update_read_handler(CharDriverState *chr)
469{
470 MuxDriver *d = chr->opaque;
471
472 if (d->mux_cnt >= MAX_MUX) {
473 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
474 return;
475 }
476 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
477 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
478 d->chr_read[d->mux_cnt] = chr->chr_read;
479 d->chr_event[d->mux_cnt] = chr->chr_event;
480 /* Fix up the real driver with mux routines */
481 if (d->mux_cnt == 0) {
482 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
483 mux_chr_event, chr);
484 }
799f1f23
GH
485 if (d->focus != -1) {
486 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
a7aec5da 487 }
799f1f23 488 d->focus = d->mux_cnt;
6f97dba0 489 d->mux_cnt++;
799f1f23 490 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
6f97dba0
AL
491}
492
493static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
494{
495 CharDriverState *chr;
496 MuxDriver *d;
497
7267c094
AL
498 chr = g_malloc0(sizeof(CharDriverState));
499 d = g_malloc0(sizeof(MuxDriver));
6f97dba0
AL
500
501 chr->opaque = d;
502 d->drv = drv;
799f1f23 503 d->focus = -1;
6f97dba0
AL
504 chr->chr_write = mux_chr_write;
505 chr->chr_update_read_handler = mux_chr_update_read_handler;
506 chr->chr_accept_input = mux_chr_accept_input;
7c32c4fe
HG
507 /* Frontend guest-open / -close notification is not support with muxes */
508 chr->chr_guest_open = NULL;
509 chr->chr_guest_close = NULL;
73cdf3f2
AG
510
511 /* Muxes are always open on creation */
512 qemu_chr_generic_open(chr);
513
6f97dba0
AL
514 return chr;
515}
516
517
518#ifdef _WIN32
d247d25f 519int send_all(int fd, const void *buf, int len1)
6f97dba0
AL
520{
521 int ret, len;
522
523 len = len1;
524 while (len > 0) {
525 ret = send(fd, buf, len, 0);
526 if (ret < 0) {
6f97dba0
AL
527 errno = WSAGetLastError();
528 if (errno != WSAEWOULDBLOCK) {
529 return -1;
530 }
531 } else if (ret == 0) {
532 break;
533 } else {
534 buf += ret;
535 len -= ret;
536 }
537 }
538 return len1 - len;
539}
540
541#else
542
5fc9cfed 543int send_all(int fd, const void *_buf, int len1)
6f97dba0
AL
544{
545 int ret, len;
5fc9cfed 546 const uint8_t *buf = _buf;
6f97dba0
AL
547
548 len = len1;
549 while (len > 0) {
550 ret = write(fd, buf, len);
551 if (ret < 0) {
552 if (errno != EINTR && errno != EAGAIN)
553 return -1;
554 } else if (ret == 0) {
555 break;
556 } else {
557 buf += ret;
558 len -= ret;
559 }
560 }
561 return len1 - len;
562}
4549a8b7
SB
563
564int recv_all(int fd, void *_buf, int len1, bool single_read)
565{
566 int ret, len;
567 uint8_t *buf = _buf;
568
569 len = len1;
570 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
571 if (ret < 0) {
572 if (errno != EINTR && errno != EAGAIN) {
573 return -1;
574 }
575 continue;
576 } else {
577 if (single_read) {
578 return ret;
579 }
580 buf += ret;
581 len -= ret;
582 }
583 }
584 return len1 - len;
585}
586
6f97dba0
AL
587#endif /* !_WIN32 */
588
96c63847
AL
589typedef struct IOWatchPoll
590{
591 GSource *src;
592 int max_size;
593
594 IOCanReadHandler *fd_can_read;
595 void *opaque;
596
597 QTAILQ_ENTRY(IOWatchPoll) node;
598} IOWatchPoll;
599
600static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
601 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);
602
603static IOWatchPoll *io_watch_poll_from_source(GSource *source)
604{
605 IOWatchPoll *i;
606
607 QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
608 if (i->src == source) {
609 return i;
610 }
611 }
612
613 return NULL;
614}
615
616static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
617{
618 IOWatchPoll *iwp = io_watch_poll_from_source(source);
619
620 iwp->max_size = iwp->fd_can_read(iwp->opaque);
621 if (iwp->max_size == 0) {
622 return FALSE;
623 }
624
625 return g_io_watch_funcs.prepare(source, timeout_);
626}
627
628static gboolean io_watch_poll_check(GSource *source)
629{
630 IOWatchPoll *iwp = io_watch_poll_from_source(source);
631
632 if (iwp->max_size == 0) {
633 return FALSE;
634 }
635
636 return g_io_watch_funcs.check(source);
637}
638
639static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
640 gpointer user_data)
641{
642 return g_io_watch_funcs.dispatch(source, callback, user_data);
643}
644
645static void io_watch_poll_finalize(GSource *source)
646{
647 IOWatchPoll *iwp = io_watch_poll_from_source(source);
648 QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
649 g_io_watch_funcs.finalize(source);
650}
651
652static GSourceFuncs io_watch_poll_funcs = {
653 .prepare = io_watch_poll_prepare,
654 .check = io_watch_poll_check,
655 .dispatch = io_watch_poll_dispatch,
656 .finalize = io_watch_poll_finalize,
657};
658
659/* Can only be used for read */
660static guint io_add_watch_poll(GIOChannel *channel,
661 IOCanReadHandler *fd_can_read,
662 GIOFunc fd_read,
663 gpointer user_data)
664{
665 IOWatchPoll *iwp;
666 GSource *src;
667 guint tag;
668
669 src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
670 g_source_set_funcs(src, &io_watch_poll_funcs);
671 g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
672 tag = g_source_attach(src, NULL);
673 g_source_unref(src);
674
675 iwp = g_malloc0(sizeof(*iwp));
676 iwp->src = src;
677 iwp->max_size = 0;
678 iwp->fd_can_read = fd_can_read;
679 iwp->opaque = user_data;
680
681 QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);
682
683 return tag;
684}
685
44ab9ed4 686#ifndef _WIN32
96c63847
AL
687static GIOChannel *io_channel_from_fd(int fd)
688{
689 GIOChannel *chan;
690
691 if (fd == -1) {
692 return NULL;
693 }
694
695 chan = g_io_channel_unix_new(fd);
696
697 g_io_channel_set_encoding(chan, NULL, NULL);
698 g_io_channel_set_buffered(chan, FALSE);
699
700 return chan;
701}
44ab9ed4 702#endif
96c63847 703
76a9644b
AL
704static GIOChannel *io_channel_from_socket(int fd)
705{
706 GIOChannel *chan;
707
708 if (fd == -1) {
709 return NULL;
710 }
711
712#ifdef _WIN32
713 chan = g_io_channel_win32_new_socket(fd);
714#else
715 chan = g_io_channel_unix_new(fd);
716#endif
717
718 g_io_channel_set_encoding(chan, NULL, NULL);
719 g_io_channel_set_buffered(chan, FALSE);
720
721 return chan;
722}
723
96c63847
AL
724static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
725{
726 GIOStatus status;
727 gsize bytes_written;
728 int len;
729 const uint8_t *buf = _buf;
730
731 len = len1;
732 while (len > 0) {
733 status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
734 &bytes_written, NULL);
735 if (status != G_IO_STATUS_NORMAL) {
23673ca7
AL
736 if (status == G_IO_STATUS_AGAIN) {
737 errno = EAGAIN;
738 return -1;
739 } else {
740 errno = EINVAL;
96c63847
AL
741 return -1;
742 }
743 } else if (status == G_IO_STATUS_EOF) {
744 break;
745 } else {
746 buf += bytes_written;
747 len -= bytes_written;
748 }
749 }
750 return len1 - len;
751}
96c63847 752
44ab9ed4
BS
753#ifndef _WIN32
754
a29753f8
AL
755typedef struct FDCharDriver {
756 CharDriverState *chr;
757 GIOChannel *fd_in, *fd_out;
758 guint fd_in_tag;
6f97dba0 759 int max_size;
a29753f8 760 QTAILQ_ENTRY(FDCharDriver) node;
6f97dba0
AL
761} FDCharDriver;
762
6f97dba0
AL
763static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
764{
765 FDCharDriver *s = chr->opaque;
a29753f8
AL
766
767 return io_channel_send_all(s->fd_out, buf, len);
6f97dba0
AL
768}
769
a29753f8 770static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
6f97dba0
AL
771{
772 CharDriverState *chr = opaque;
773 FDCharDriver *s = chr->opaque;
a29753f8 774 int len;
9bd7854e 775 uint8_t buf[READ_BUF_LEN];
a29753f8
AL
776 GIOStatus status;
777 gsize bytes_read;
6f97dba0
AL
778
779 len = sizeof(buf);
a29753f8 780 if (len > s->max_size) {
6f97dba0 781 len = s->max_size;
a29753f8
AL
782 }
783 if (len == 0) {
784 return FALSE;
785 }
786
787 status = g_io_channel_read_chars(chan, (gchar *)buf,
788 len, &bytes_read, NULL);
789 if (status == G_IO_STATUS_EOF) {
a425d23f 790 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
a29753f8 791 return FALSE;
6f97dba0 792 }
a29753f8
AL
793 if (status == G_IO_STATUS_NORMAL) {
794 qemu_chr_be_write(chr, buf, bytes_read);
6f97dba0 795 }
a29753f8
AL
796
797 return TRUE;
798}
799
800static int fd_chr_read_poll(void *opaque)
801{
802 CharDriverState *chr = opaque;
803 FDCharDriver *s = chr->opaque;
804
805 s->max_size = qemu_chr_be_can_write(chr);
806 return s->max_size;
6f97dba0
AL
807}
808
23673ca7
AL
809static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
810{
811 FDCharDriver *s = chr->opaque;
812 return g_io_create_watch(s->fd_out, cond);
813}
814
6f97dba0
AL
815static void fd_chr_update_read_handler(CharDriverState *chr)
816{
817 FDCharDriver *s = chr->opaque;
818
a29753f8
AL
819 if (s->fd_in_tag) {
820 g_source_remove(s->fd_in_tag);
821 }
822
823 if (s->fd_in) {
824 s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
6f97dba0
AL
825 }
826}
827
828static void fd_chr_close(struct CharDriverState *chr)
829{
830 FDCharDriver *s = chr->opaque;
831
a29753f8
AL
832 if (s->fd_in_tag) {
833 g_source_remove(s->fd_in_tag);
834 s->fd_in_tag = 0;
835 }
836
837 if (s->fd_in) {
838 g_io_channel_unref(s->fd_in);
839 }
840 if (s->fd_out) {
841 g_io_channel_unref(s->fd_out);
6f97dba0
AL
842 }
843
7267c094 844 g_free(s);
a425d23f 845 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
846}
847
848/* open a character device to a unix fd */
849static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
850{
851 CharDriverState *chr;
852 FDCharDriver *s;
853
7267c094
AL
854 chr = g_malloc0(sizeof(CharDriverState));
855 s = g_malloc0(sizeof(FDCharDriver));
a29753f8
AL
856 s->fd_in = io_channel_from_fd(fd_in);
857 s->fd_out = io_channel_from_fd(fd_out);
23673ca7 858 fcntl(fd_out, F_SETFL, O_NONBLOCK);
a29753f8 859 s->chr = chr;
6f97dba0 860 chr->opaque = s;
23673ca7 861 chr->chr_add_watch = fd_chr_add_watch;
6f97dba0
AL
862 chr->chr_write = fd_chr_write;
863 chr->chr_update_read_handler = fd_chr_update_read_handler;
864 chr->chr_close = fd_chr_close;
865
127338e6 866 qemu_chr_generic_open(chr);
6f97dba0
AL
867
868 return chr;
869}
870
548cbb36 871static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
6f97dba0
AL
872{
873 int fd_in, fd_out;
874 char filename_in[256], filename_out[256];
548cbb36 875 const char *filename = opts->device;
7d31544f
GH
876
877 if (filename == NULL) {
878 fprintf(stderr, "chardev: pipe: no filename given\n");
1f51470d 879 return NULL;
7d31544f 880 }
6f97dba0
AL
881
882 snprintf(filename_in, 256, "%s.in", filename);
883 snprintf(filename_out, 256, "%s.out", filename);
40ff6d7e
KW
884 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
885 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
6f97dba0
AL
886 if (fd_in < 0 || fd_out < 0) {
887 if (fd_in >= 0)
888 close(fd_in);
889 if (fd_out >= 0)
890 close(fd_out);
b181e047 891 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
a89dd6c3 892 if (fd_in < 0) {
1f51470d 893 return NULL;
a89dd6c3 894 }
6f97dba0 895 }
1f51470d 896 return qemu_chr_open_fd(fd_in, fd_out);
6f97dba0
AL
897}
898
6f97dba0
AL
899/* init terminal so that we can grab keys */
900static struct termios oldtty;
901static int old_fd0_flags;
bb002513 902static bool stdio_allow_signal;
6f97dba0
AL
903
904static void term_exit(void)
905{
906 tcsetattr (0, TCSANOW, &oldtty);
907 fcntl(0, F_SETFL, old_fd0_flags);
908}
909
bb002513 910static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
6f97dba0
AL
911{
912 struct termios tty;
913
0369364b 914 tty = oldtty;
bb002513
PB
915 if (!echo) {
916 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
6f97dba0 917 |INLCR|IGNCR|ICRNL|IXON);
bb002513
PB
918 tty.c_oflag |= OPOST;
919 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
920 tty.c_cflag &= ~(CSIZE|PARENB);
921 tty.c_cflag |= CS8;
922 tty.c_cc[VMIN] = 1;
923 tty.c_cc[VTIME] = 0;
924 }
6f97dba0 925 /* if graphical mode, we allow Ctrl-C handling */
bb002513 926 if (!stdio_allow_signal)
6f97dba0 927 tty.c_lflag &= ~ISIG;
6f97dba0
AL
928
929 tcsetattr (0, TCSANOW, &tty);
6f97dba0
AL
930}
931
932static void qemu_chr_close_stdio(struct CharDriverState *chr)
933{
934 term_exit();
6f97dba0
AL
935 fd_chr_close(chr);
936}
937
7c358031 938static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
6f97dba0
AL
939{
940 CharDriverState *chr;
941
ab51b1d5
MT
942 if (is_daemonized()) {
943 error_report("cannot use stdio with -daemonize");
944 return NULL;
945 }
ed7a1540
AL
946 old_fd0_flags = fcntl(0, F_GETFL);
947 tcgetattr (0, &oldtty);
948 fcntl(0, F_SETFL, O_NONBLOCK);
949 atexit(term_exit);
0369364b 950
6f97dba0
AL
951 chr = qemu_chr_open_fd(0, 1);
952 chr->chr_close = qemu_chr_close_stdio;
bb002513 953 chr->chr_set_echo = qemu_chr_set_echo_stdio;
7c358031
GH
954 stdio_allow_signal = display_type != DT_NOGRAPHIC;
955 if (opts->has_signal) {
956 stdio_allow_signal = opts->signal;
957 }
15f31519 958 qemu_chr_fe_set_echo(chr, false);
6f97dba0 959
1f51470d 960 return chr;
6f97dba0
AL
961}
962
963#ifdef __sun__
964/* Once Solaris has openpty(), this is going to be removed. */
3f4cb3d3
BS
965static int openpty(int *amaster, int *aslave, char *name,
966 struct termios *termp, struct winsize *winp)
6f97dba0
AL
967{
968 const char *slave;
969 int mfd = -1, sfd = -1;
970
971 *amaster = *aslave = -1;
972
973 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
974 if (mfd < 0)
975 goto err;
976
977 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
978 goto err;
979
980 if ((slave = ptsname(mfd)) == NULL)
981 goto err;
982
983 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
984 goto err;
985
986 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
987 (termp != NULL && tcgetattr(sfd, termp) < 0))
988 goto err;
989
990 if (amaster)
991 *amaster = mfd;
992 if (aslave)
993 *aslave = sfd;
994 if (winp)
995 ioctl(sfd, TIOCSWINSZ, winp);
996
997 return 0;
998
999err:
1000 if (sfd != -1)
1001 close(sfd);
1002 close(mfd);
1003 return -1;
1004}
1005
3f4cb3d3 1006static void cfmakeraw (struct termios *termios_p)
6f97dba0
AL
1007{
1008 termios_p->c_iflag &=
1009 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
1010 termios_p->c_oflag &= ~OPOST;
1011 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1012 termios_p->c_cflag &= ~(CSIZE|PARENB);
1013 termios_p->c_cflag |= CS8;
1014
1015 termios_p->c_cc[VMIN] = 0;
1016 termios_p->c_cc[VTIME] = 0;
1017}
1018#endif
1019
1020#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
a167ba50
AJ
1021 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1022 || defined(__GLIBC__)
6f97dba0 1023
e551498e
GH
1024#define HAVE_CHARDEV_TTY 1
1025
6f97dba0 1026typedef struct {
093d3a20
AL
1027 GIOChannel *fd;
1028 guint fd_tag;
6f97dba0
AL
1029 int connected;
1030 int polling;
1031 int read_bytes;
8aa33caf 1032 guint timer_tag;
6f97dba0
AL
1033} PtyCharDriver;
1034
1035static void pty_chr_update_read_handler(CharDriverState *chr);
1036static void pty_chr_state(CharDriverState *chr, int connected);
1037
8aa33caf
AL
1038static gboolean pty_chr_timer(gpointer opaque)
1039{
1040 struct CharDriverState *chr = opaque;
1041 PtyCharDriver *s = chr->opaque;
1042
1043 if (s->connected) {
1044 goto out;
1045 }
1046 if (s->polling) {
1047 /* If we arrive here without polling being cleared due
1048 * read returning -EIO, then we are (re-)connected */
1049 pty_chr_state(chr, 1);
1050 goto out;
1051 }
1052
1053 /* Next poll ... */
1054 pty_chr_update_read_handler(chr);
1055
1056out:
1057 return FALSE;
1058}
1059
1060static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1061{
1062 PtyCharDriver *s = chr->opaque;
1063
1064 if (s->timer_tag) {
1065 g_source_remove(s->timer_tag);
1066 s->timer_tag = 0;
1067 }
1068
1069 if (ms == 1000) {
1070 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1071 } else {
1072 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1073 }
1074}
1075
6f97dba0
AL
1076static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1077{
1078 PtyCharDriver *s = chr->opaque;
1079
1080 if (!s->connected) {
1081 /* guest sends data, check for (re-)connect */
1082 pty_chr_update_read_handler(chr);
1083 return 0;
1084 }
093d3a20 1085 return io_channel_send_all(s->fd, buf, len);
6f97dba0
AL
1086}
1087
e6a87ed8
AL
1088static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1089{
1090 PtyCharDriver *s = chr->opaque;
1091 return g_io_create_watch(s->fd, cond);
1092}
1093
6f97dba0
AL
1094static int pty_chr_read_poll(void *opaque)
1095{
1096 CharDriverState *chr = opaque;
1097 PtyCharDriver *s = chr->opaque;
1098
909cda12 1099 s->read_bytes = qemu_chr_be_can_write(chr);
6f97dba0
AL
1100 return s->read_bytes;
1101}
1102
093d3a20 1103static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
6f97dba0
AL
1104{
1105 CharDriverState *chr = opaque;
1106 PtyCharDriver *s = chr->opaque;
093d3a20 1107 gsize size, len;
9bd7854e 1108 uint8_t buf[READ_BUF_LEN];
093d3a20 1109 GIOStatus status;
6f97dba0
AL
1110
1111 len = sizeof(buf);
1112 if (len > s->read_bytes)
1113 len = s->read_bytes;
1114 if (len == 0)
093d3a20
AL
1115 return FALSE;
1116 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1117 if (status != G_IO_STATUS_NORMAL) {
6f97dba0 1118 pty_chr_state(chr, 0);
093d3a20
AL
1119 return FALSE;
1120 } else {
6f97dba0 1121 pty_chr_state(chr, 1);
fa5efccb 1122 qemu_chr_be_write(chr, buf, size);
6f97dba0 1123 }
093d3a20 1124 return TRUE;
6f97dba0
AL
1125}
1126
1127static void pty_chr_update_read_handler(CharDriverState *chr)
1128{
1129 PtyCharDriver *s = chr->opaque;
1130
093d3a20
AL
1131 if (s->fd_tag) {
1132 g_source_remove(s->fd_tag);
1133 }
1134
1135 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
6f97dba0
AL
1136 s->polling = 1;
1137 /*
1138 * Short timeout here: just need wait long enougth that qemu makes
1139 * it through the poll loop once. When reconnected we want a
1140 * short timeout so we notice it almost instantly. Otherwise
1141 * read() gives us -EIO instantly, making pty_chr_state() reset the
1142 * timeout to the normal (much longer) poll interval before the
1143 * timer triggers.
1144 */
8aa33caf 1145 pty_chr_rearm_timer(chr, 10);
6f97dba0
AL
1146}
1147
1148static void pty_chr_state(CharDriverState *chr, int connected)
1149{
1150 PtyCharDriver *s = chr->opaque;
1151
1152 if (!connected) {
093d3a20
AL
1153 g_source_remove(s->fd_tag);
1154 s->fd_tag = 0;
6f97dba0
AL
1155 s->connected = 0;
1156 s->polling = 0;
1157 /* (re-)connect poll interval for idle guests: once per second.
1158 * We check more frequently in case the guests sends data to
1159 * the virtual device linked to our pty. */
8aa33caf 1160 pty_chr_rearm_timer(chr, 1000);
6f97dba0
AL
1161 } else {
1162 if (!s->connected)
127338e6 1163 qemu_chr_generic_open(chr);
6f97dba0
AL
1164 s->connected = 1;
1165 }
1166}
1167
6f97dba0
AL
1168
1169static void pty_chr_close(struct CharDriverState *chr)
1170{
1171 PtyCharDriver *s = chr->opaque;
093d3a20 1172 int fd;
6f97dba0 1173
093d3a20
AL
1174 if (s->fd_tag) {
1175 g_source_remove(s->fd_tag);
1176 }
1177 fd = g_io_channel_unix_get_fd(s->fd);
1178 g_io_channel_unref(s->fd);
1179 close(fd);
8aa33caf
AL
1180 if (s->timer_tag) {
1181 g_source_remove(s->timer_tag);
1182 }
7267c094 1183 g_free(s);
a425d23f 1184 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
1185}
1186
e68c5958
GH
1187static CharDriverState *qemu_chr_open_pty(const char *id,
1188 ChardevReturn *ret)
6f97dba0
AL
1189{
1190 CharDriverState *chr;
1191 PtyCharDriver *s;
1192 struct termios tty;
e68c5958 1193 int master_fd, slave_fd;
c5e97233 1194#if defined(__OpenBSD__) || defined(__DragonFly__)
6f97dba0
AL
1195 char pty_name[PATH_MAX];
1196#define q_ptsname(x) pty_name
1197#else
1198 char *pty_name = NULL;
1199#define q_ptsname(x) ptsname(x)
1200#endif
1201
a4e26048 1202 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
1f51470d 1203 return NULL;
6f97dba0
AL
1204 }
1205
1206 /* Set raw attributes on the pty. */
c3b972c3 1207 tcgetattr(slave_fd, &tty);
6f97dba0
AL
1208 cfmakeraw(&tty);
1209 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1210 close(slave_fd);
1211
a4e26048
MA
1212 chr = g_malloc0(sizeof(CharDriverState));
1213
e68c5958
GH
1214 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1215 ret->pty = g_strdup(q_ptsname(master_fd));
1216 ret->has_pty = true;
58650218 1217
e68c5958
GH
1218 fprintf(stderr, "char device redirected to %s (label %s)\n",
1219 q_ptsname(master_fd), id);
6f97dba0 1220
a4e26048 1221 s = g_malloc0(sizeof(PtyCharDriver));
6f97dba0
AL
1222 chr->opaque = s;
1223 chr->chr_write = pty_chr_write;
1224 chr->chr_update_read_handler = pty_chr_update_read_handler;
1225 chr->chr_close = pty_chr_close;
e6a87ed8 1226 chr->chr_add_watch = pty_chr_add_watch;
6f97dba0 1227
093d3a20 1228 s->fd = io_channel_from_fd(master_fd);
8aa33caf 1229 s->timer_tag = 0;
6f97dba0 1230
1f51470d 1231 return chr;
6f97dba0
AL
1232}
1233
1234static void tty_serial_init(int fd, int speed,
1235 int parity, int data_bits, int stop_bits)
1236{
1237 struct termios tty;
1238 speed_t spd;
1239
1240#if 0
1241 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1242 speed, parity, data_bits, stop_bits);
1243#endif
1244 tcgetattr (fd, &tty);
1245
45eea13b
SW
1246#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1247 speed = speed * 10 / 11;
1248 do {
1249 check_speed(50);
1250 check_speed(75);
1251 check_speed(110);
1252 check_speed(134);
1253 check_speed(150);
1254 check_speed(200);
1255 check_speed(300);
1256 check_speed(600);
1257 check_speed(1200);
1258 check_speed(1800);
1259 check_speed(2400);
1260 check_speed(4800);
1261 check_speed(9600);
1262 check_speed(19200);
1263 check_speed(38400);
1264 /* Non-Posix values follow. They may be unsupported on some systems. */
1265 check_speed(57600);
1266 check_speed(115200);
1267#ifdef B230400
1268 check_speed(230400);
1269#endif
1270#ifdef B460800
1271 check_speed(460800);
1272#endif
1273#ifdef B500000
1274 check_speed(500000);
1275#endif
1276#ifdef B576000
1277 check_speed(576000);
1278#endif
1279#ifdef B921600
1280 check_speed(921600);
1281#endif
1282#ifdef B1000000
1283 check_speed(1000000);
1284#endif
1285#ifdef B1152000
1286 check_speed(1152000);
1287#endif
1288#ifdef B1500000
1289 check_speed(1500000);
1290#endif
1291#ifdef B2000000
1292 check_speed(2000000);
1293#endif
1294#ifdef B2500000
1295 check_speed(2500000);
1296#endif
1297#ifdef B3000000
1298 check_speed(3000000);
1299#endif
1300#ifdef B3500000
1301 check_speed(3500000);
1302#endif
1303#ifdef B4000000
1304 check_speed(4000000);
1305#endif
6f97dba0 1306 spd = B115200;
45eea13b 1307 } while (0);
6f97dba0
AL
1308
1309 cfsetispeed(&tty, spd);
1310 cfsetospeed(&tty, spd);
1311
1312 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1313 |INLCR|IGNCR|ICRNL|IXON);
1314 tty.c_oflag |= OPOST;
1315 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1316 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1317 switch(data_bits) {
1318 default:
1319 case 8:
1320 tty.c_cflag |= CS8;
1321 break;
1322 case 7:
1323 tty.c_cflag |= CS7;
1324 break;
1325 case 6:
1326 tty.c_cflag |= CS6;
1327 break;
1328 case 5:
1329 tty.c_cflag |= CS5;
1330 break;
1331 }
1332 switch(parity) {
1333 default:
1334 case 'N':
1335 break;
1336 case 'E':
1337 tty.c_cflag |= PARENB;
1338 break;
1339 case 'O':
1340 tty.c_cflag |= PARENB | PARODD;
1341 break;
1342 }
1343 if (stop_bits == 2)
1344 tty.c_cflag |= CSTOPB;
1345
1346 tcsetattr (fd, TCSANOW, &tty);
1347}
1348
1349static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1350{
1351 FDCharDriver *s = chr->opaque;
1352
1353 switch(cmd) {
1354 case CHR_IOCTL_SERIAL_SET_PARAMS:
1355 {
1356 QEMUSerialSetParams *ssp = arg;
a29753f8
AL
1357 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1358 ssp->speed, ssp->parity,
6f97dba0
AL
1359 ssp->data_bits, ssp->stop_bits);
1360 }
1361 break;
1362 case CHR_IOCTL_SERIAL_SET_BREAK:
1363 {
1364 int enable = *(int *)arg;
a29753f8
AL
1365 if (enable) {
1366 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1367 }
6f97dba0
AL
1368 }
1369 break;
1370 case CHR_IOCTL_SERIAL_GET_TIOCM:
1371 {
1372 int sarg = 0;
1373 int *targ = (int *)arg;
a29753f8 1374 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
6f97dba0 1375 *targ = 0;
b4abdfa4 1376 if (sarg & TIOCM_CTS)
6f97dba0 1377 *targ |= CHR_TIOCM_CTS;
b4abdfa4 1378 if (sarg & TIOCM_CAR)
6f97dba0 1379 *targ |= CHR_TIOCM_CAR;
b4abdfa4 1380 if (sarg & TIOCM_DSR)
6f97dba0 1381 *targ |= CHR_TIOCM_DSR;
b4abdfa4 1382 if (sarg & TIOCM_RI)
6f97dba0 1383 *targ |= CHR_TIOCM_RI;
b4abdfa4 1384 if (sarg & TIOCM_DTR)
6f97dba0 1385 *targ |= CHR_TIOCM_DTR;
b4abdfa4 1386 if (sarg & TIOCM_RTS)
6f97dba0
AL
1387 *targ |= CHR_TIOCM_RTS;
1388 }
1389 break;
1390 case CHR_IOCTL_SERIAL_SET_TIOCM:
1391 {
1392 int sarg = *(int *)arg;
1393 int targ = 0;
a29753f8 1394 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
b4abdfa4
AJ
1395 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1396 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1397 if (sarg & CHR_TIOCM_CTS)
1398 targ |= TIOCM_CTS;
1399 if (sarg & CHR_TIOCM_CAR)
1400 targ |= TIOCM_CAR;
1401 if (sarg & CHR_TIOCM_DSR)
1402 targ |= TIOCM_DSR;
1403 if (sarg & CHR_TIOCM_RI)
1404 targ |= TIOCM_RI;
1405 if (sarg & CHR_TIOCM_DTR)
6f97dba0 1406 targ |= TIOCM_DTR;
b4abdfa4 1407 if (sarg & CHR_TIOCM_RTS)
6f97dba0 1408 targ |= TIOCM_RTS;
a29753f8 1409 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
6f97dba0
AL
1410 }
1411 break;
1412 default:
1413 return -ENOTSUP;
1414 }
1415 return 0;
1416}
1417
4266a134
DA
1418static void qemu_chr_close_tty(CharDriverState *chr)
1419{
1420 FDCharDriver *s = chr->opaque;
1421 int fd = -1;
1422
1423 if (s) {
a29753f8 1424 fd = g_io_channel_unix_get_fd(s->fd_in);
4266a134
DA
1425 }
1426
1427 fd_chr_close(chr);
1428
1429 if (fd >= 0) {
1430 close(fd);
1431 }
1432}
1433
d59044ef
GH
1434static CharDriverState *qemu_chr_open_tty_fd(int fd)
1435{
1436 CharDriverState *chr;
1437
1438 tty_serial_init(fd, 115200, 'N', 8, 1);
1439 chr = qemu_chr_open_fd(fd, fd);
1440 chr->chr_ioctl = tty_serial_ioctl;
1441 chr->chr_close = qemu_chr_close_tty;
1442 return chr;
1443}
6f97dba0
AL
1444#endif /* __linux__ || __sun__ */
1445
1446#if defined(__linux__)
e551498e
GH
1447
1448#define HAVE_CHARDEV_PARPORT 1
1449
6f97dba0
AL
1450typedef struct {
1451 int fd;
1452 int mode;
1453} ParallelCharDriver;
1454
1455static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1456{
1457 if (s->mode != mode) {
1458 int m = mode;
1459 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1460 return 0;
1461 s->mode = mode;
1462 }
1463 return 1;
1464}
1465
1466static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1467{
1468 ParallelCharDriver *drv = chr->opaque;
1469 int fd = drv->fd;
1470 uint8_t b;
1471
1472 switch(cmd) {
1473 case CHR_IOCTL_PP_READ_DATA:
1474 if (ioctl(fd, PPRDATA, &b) < 0)
1475 return -ENOTSUP;
1476 *(uint8_t *)arg = b;
1477 break;
1478 case CHR_IOCTL_PP_WRITE_DATA:
1479 b = *(uint8_t *)arg;
1480 if (ioctl(fd, PPWDATA, &b) < 0)
1481 return -ENOTSUP;
1482 break;
1483 case CHR_IOCTL_PP_READ_CONTROL:
1484 if (ioctl(fd, PPRCONTROL, &b) < 0)
1485 return -ENOTSUP;
1486 /* Linux gives only the lowest bits, and no way to know data
1487 direction! For better compatibility set the fixed upper
1488 bits. */
1489 *(uint8_t *)arg = b | 0xc0;
1490 break;
1491 case CHR_IOCTL_PP_WRITE_CONTROL:
1492 b = *(uint8_t *)arg;
1493 if (ioctl(fd, PPWCONTROL, &b) < 0)
1494 return -ENOTSUP;
1495 break;
1496 case CHR_IOCTL_PP_READ_STATUS:
1497 if (ioctl(fd, PPRSTATUS, &b) < 0)
1498 return -ENOTSUP;
1499 *(uint8_t *)arg = b;
1500 break;
1501 case CHR_IOCTL_PP_DATA_DIR:
1502 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1503 return -ENOTSUP;
1504 break;
1505 case CHR_IOCTL_PP_EPP_READ_ADDR:
1506 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1507 struct ParallelIOArg *parg = arg;
1508 int n = read(fd, parg->buffer, parg->count);
1509 if (n != parg->count) {
1510 return -EIO;
1511 }
1512 }
1513 break;
1514 case CHR_IOCTL_PP_EPP_READ:
1515 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1516 struct ParallelIOArg *parg = arg;
1517 int n = read(fd, parg->buffer, parg->count);
1518 if (n != parg->count) {
1519 return -EIO;
1520 }
1521 }
1522 break;
1523 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1524 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1525 struct ParallelIOArg *parg = arg;
1526 int n = write(fd, parg->buffer, parg->count);
1527 if (n != parg->count) {
1528 return -EIO;
1529 }
1530 }
1531 break;
1532 case CHR_IOCTL_PP_EPP_WRITE:
1533 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1534 struct ParallelIOArg *parg = arg;
1535 int n = write(fd, parg->buffer, parg->count);
1536 if (n != parg->count) {
1537 return -EIO;
1538 }
1539 }
1540 break;
1541 default:
1542 return -ENOTSUP;
1543 }
1544 return 0;
1545}
1546
1547static void pp_close(CharDriverState *chr)
1548{
1549 ParallelCharDriver *drv = chr->opaque;
1550 int fd = drv->fd;
1551
1552 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1553 ioctl(fd, PPRELEASE);
1554 close(fd);
7267c094 1555 g_free(drv);
a425d23f 1556 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
1557}
1558
88a946d3 1559static CharDriverState *qemu_chr_open_pp_fd(int fd)
6f97dba0
AL
1560{
1561 CharDriverState *chr;
1562 ParallelCharDriver *drv;
6f97dba0
AL
1563
1564 if (ioctl(fd, PPCLAIM) < 0) {
1565 close(fd);
1f51470d 1566 return NULL;
6f97dba0
AL
1567 }
1568
7267c094 1569 drv = g_malloc0(sizeof(ParallelCharDriver));
6f97dba0
AL
1570 drv->fd = fd;
1571 drv->mode = IEEE1284_MODE_COMPAT;
1572
7267c094 1573 chr = g_malloc0(sizeof(CharDriverState));
6f97dba0
AL
1574 chr->chr_write = null_chr_write;
1575 chr->chr_ioctl = pp_ioctl;
1576 chr->chr_close = pp_close;
1577 chr->opaque = drv;
1578
127338e6 1579 qemu_chr_generic_open(chr);
6f97dba0 1580
1f51470d 1581 return chr;
6f97dba0
AL
1582}
1583#endif /* __linux__ */
1584
a167ba50 1585#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
e551498e
GH
1586
1587#define HAVE_CHARDEV_PARPORT 1
1588
6972f935
BS
1589static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1590{
e0efb993 1591 int fd = (int)(intptr_t)chr->opaque;
6972f935
BS
1592 uint8_t b;
1593
1594 switch(cmd) {
1595 case CHR_IOCTL_PP_READ_DATA:
1596 if (ioctl(fd, PPIGDATA, &b) < 0)
1597 return -ENOTSUP;
1598 *(uint8_t *)arg = b;
1599 break;
1600 case CHR_IOCTL_PP_WRITE_DATA:
1601 b = *(uint8_t *)arg;
1602 if (ioctl(fd, PPISDATA, &b) < 0)
1603 return -ENOTSUP;
1604 break;
1605 case CHR_IOCTL_PP_READ_CONTROL:
1606 if (ioctl(fd, PPIGCTRL, &b) < 0)
1607 return -ENOTSUP;
1608 *(uint8_t *)arg = b;
1609 break;
1610 case CHR_IOCTL_PP_WRITE_CONTROL:
1611 b = *(uint8_t *)arg;
1612 if (ioctl(fd, PPISCTRL, &b) < 0)
1613 return -ENOTSUP;
1614 break;
1615 case CHR_IOCTL_PP_READ_STATUS:
1616 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1617 return -ENOTSUP;
1618 *(uint8_t *)arg = b;
1619 break;
1620 default:
1621 return -ENOTSUP;
1622 }
1623 return 0;
1624}
1625
88a946d3 1626static CharDriverState *qemu_chr_open_pp_fd(int fd)
6972f935
BS
1627{
1628 CharDriverState *chr;
6972f935 1629
7267c094 1630 chr = g_malloc0(sizeof(CharDriverState));
e0efb993 1631 chr->opaque = (void *)(intptr_t)fd;
6972f935
BS
1632 chr->chr_write = null_chr_write;
1633 chr->chr_ioctl = pp_ioctl;
1f51470d 1634 return chr;
6972f935
BS
1635}
1636#endif
1637
6f97dba0
AL
1638#else /* _WIN32 */
1639
1640typedef struct {
1641 int max_size;
1642 HANDLE hcom, hrecv, hsend;
1643 OVERLAPPED orecv, osend;
1644 BOOL fpipe;
1645 DWORD len;
1646} WinCharState;
1647
db418a0a
FC
1648typedef struct {
1649 HANDLE hStdIn;
1650 HANDLE hInputReadyEvent;
1651 HANDLE hInputDoneEvent;
1652 HANDLE hInputThread;
1653 uint8_t win_stdio_buf;
1654} WinStdioCharState;
1655
6f97dba0
AL
1656#define NSENDBUF 2048
1657#define NRECVBUF 2048
1658#define MAXCONNECT 1
1659#define NTIMEOUT 5000
1660
1661static int win_chr_poll(void *opaque);
1662static int win_chr_pipe_poll(void *opaque);
1663
1664static void win_chr_close(CharDriverState *chr)
1665{
1666 WinCharState *s = chr->opaque;
1667
1668 if (s->hsend) {
1669 CloseHandle(s->hsend);
1670 s->hsend = NULL;
1671 }
1672 if (s->hrecv) {
1673 CloseHandle(s->hrecv);
1674 s->hrecv = NULL;
1675 }
1676 if (s->hcom) {
1677 CloseHandle(s->hcom);
1678 s->hcom = NULL;
1679 }
1680 if (s->fpipe)
1681 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1682 else
1683 qemu_del_polling_cb(win_chr_poll, chr);
793cbfb5 1684
a425d23f 1685 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
1686}
1687
1688static int win_chr_init(CharDriverState *chr, const char *filename)
1689{
1690 WinCharState *s = chr->opaque;
1691 COMMCONFIG comcfg;
1692 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1693 COMSTAT comstat;
1694 DWORD size;
1695 DWORD err;
1696
1697 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1698 if (!s->hsend) {
1699 fprintf(stderr, "Failed CreateEvent\n");
1700 goto fail;
1701 }
1702 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1703 if (!s->hrecv) {
1704 fprintf(stderr, "Failed CreateEvent\n");
1705 goto fail;
1706 }
1707
1708 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1709 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1710 if (s->hcom == INVALID_HANDLE_VALUE) {
1711 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1712 s->hcom = NULL;
1713 goto fail;
1714 }
1715
1716 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1717 fprintf(stderr, "Failed SetupComm\n");
1718 goto fail;
1719 }
1720
1721 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1722 size = sizeof(COMMCONFIG);
1723 GetDefaultCommConfig(filename, &comcfg, &size);
1724 comcfg.dcb.DCBlength = sizeof(DCB);
1725 CommConfigDialog(filename, NULL, &comcfg);
1726
1727 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1728 fprintf(stderr, "Failed SetCommState\n");
1729 goto fail;
1730 }
1731
1732 if (!SetCommMask(s->hcom, EV_ERR)) {
1733 fprintf(stderr, "Failed SetCommMask\n");
1734 goto fail;
1735 }
1736
1737 cto.ReadIntervalTimeout = MAXDWORD;
1738 if (!SetCommTimeouts(s->hcom, &cto)) {
1739 fprintf(stderr, "Failed SetCommTimeouts\n");
1740 goto fail;
1741 }
1742
1743 if (!ClearCommError(s->hcom, &err, &comstat)) {
1744 fprintf(stderr, "Failed ClearCommError\n");
1745 goto fail;
1746 }
1747 qemu_add_polling_cb(win_chr_poll, chr);
1748 return 0;
1749
1750 fail:
1751 win_chr_close(chr);
1752 return -1;
1753}
1754
1755static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1756{
1757 WinCharState *s = chr->opaque;
1758 DWORD len, ret, size, err;
1759
1760 len = len1;
1761 ZeroMemory(&s->osend, sizeof(s->osend));
1762 s->osend.hEvent = s->hsend;
1763 while (len > 0) {
1764 if (s->hsend)
1765 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1766 else
1767 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1768 if (!ret) {
1769 err = GetLastError();
1770 if (err == ERROR_IO_PENDING) {
1771 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1772 if (ret) {
1773 buf += size;
1774 len -= size;
1775 } else {
1776 break;
1777 }
1778 } else {
1779 break;
1780 }
1781 } else {
1782 buf += size;
1783 len -= size;
1784 }
1785 }
1786 return len1 - len;
1787}
1788
1789static int win_chr_read_poll(CharDriverState *chr)
1790{
1791 WinCharState *s = chr->opaque;
1792
909cda12 1793 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
1794 return s->max_size;
1795}
1796
1797static void win_chr_readfile(CharDriverState *chr)
1798{
1799 WinCharState *s = chr->opaque;
1800 int ret, err;
9bd7854e 1801 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
1802 DWORD size;
1803
1804 ZeroMemory(&s->orecv, sizeof(s->orecv));
1805 s->orecv.hEvent = s->hrecv;
1806 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1807 if (!ret) {
1808 err = GetLastError();
1809 if (err == ERROR_IO_PENDING) {
1810 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1811 }
1812 }
1813
1814 if (size > 0) {
fa5efccb 1815 qemu_chr_be_write(chr, buf, size);
6f97dba0
AL
1816 }
1817}
1818
1819static void win_chr_read(CharDriverState *chr)
1820{
1821 WinCharState *s = chr->opaque;
1822
1823 if (s->len > s->max_size)
1824 s->len = s->max_size;
1825 if (s->len == 0)
1826 return;
1827
1828 win_chr_readfile(chr);
1829}
1830
1831static int win_chr_poll(void *opaque)
1832{
1833 CharDriverState *chr = opaque;
1834 WinCharState *s = chr->opaque;
1835 COMSTAT status;
1836 DWORD comerr;
1837
1838 ClearCommError(s->hcom, &comerr, &status);
1839 if (status.cbInQue > 0) {
1840 s->len = status.cbInQue;
1841 win_chr_read_poll(chr);
1842 win_chr_read(chr);
1843 return 1;
1844 }
1845 return 0;
1846}
1847
d59044ef 1848static CharDriverState *qemu_chr_open_win_path(const char *filename)
6f97dba0
AL
1849{
1850 CharDriverState *chr;
1851 WinCharState *s;
1852
7267c094
AL
1853 chr = g_malloc0(sizeof(CharDriverState));
1854 s = g_malloc0(sizeof(WinCharState));
6f97dba0
AL
1855 chr->opaque = s;
1856 chr->chr_write = win_chr_write;
1857 chr->chr_close = win_chr_close;
1858
1859 if (win_chr_init(chr, filename) < 0) {
2e02e18b
SW
1860 g_free(s);
1861 g_free(chr);
1f51470d 1862 return NULL;
6f97dba0 1863 }
127338e6 1864 qemu_chr_generic_open(chr);
1f51470d 1865 return chr;
6f97dba0
AL
1866}
1867
1868static int win_chr_pipe_poll(void *opaque)
1869{
1870 CharDriverState *chr = opaque;
1871 WinCharState *s = chr->opaque;
1872 DWORD size;
1873
1874 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1875 if (size > 0) {
1876 s->len = size;
1877 win_chr_read_poll(chr);
1878 win_chr_read(chr);
1879 return 1;
1880 }
1881 return 0;
1882}
1883
1884static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1885{
1886 WinCharState *s = chr->opaque;
1887 OVERLAPPED ov;
1888 int ret;
1889 DWORD size;
1890 char openname[256];
1891
1892 s->fpipe = TRUE;
1893
1894 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1895 if (!s->hsend) {
1896 fprintf(stderr, "Failed CreateEvent\n");
1897 goto fail;
1898 }
1899 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1900 if (!s->hrecv) {
1901 fprintf(stderr, "Failed CreateEvent\n");
1902 goto fail;
1903 }
1904
1905 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1906 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1907 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1908 PIPE_WAIT,
1909 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1910 if (s->hcom == INVALID_HANDLE_VALUE) {
1911 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1912 s->hcom = NULL;
1913 goto fail;
1914 }
1915
1916 ZeroMemory(&ov, sizeof(ov));
1917 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1918 ret = ConnectNamedPipe(s->hcom, &ov);
1919 if (ret) {
1920 fprintf(stderr, "Failed ConnectNamedPipe\n");
1921 goto fail;
1922 }
1923
1924 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1925 if (!ret) {
1926 fprintf(stderr, "Failed GetOverlappedResult\n");
1927 if (ov.hEvent) {
1928 CloseHandle(ov.hEvent);
1929 ov.hEvent = NULL;
1930 }
1931 goto fail;
1932 }
1933
1934 if (ov.hEvent) {
1935 CloseHandle(ov.hEvent);
1936 ov.hEvent = NULL;
1937 }
1938 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1939 return 0;
1940
1941 fail:
1942 win_chr_close(chr);
1943 return -1;
1944}
1945
1946
548cbb36 1947static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
6f97dba0 1948{
548cbb36 1949 const char *filename = opts->device;
6f97dba0
AL
1950 CharDriverState *chr;
1951 WinCharState *s;
1952
7267c094
AL
1953 chr = g_malloc0(sizeof(CharDriverState));
1954 s = g_malloc0(sizeof(WinCharState));
6f97dba0
AL
1955 chr->opaque = s;
1956 chr->chr_write = win_chr_write;
1957 chr->chr_close = win_chr_close;
1958
1959 if (win_chr_pipe_init(chr, filename) < 0) {
2e02e18b
SW
1960 g_free(s);
1961 g_free(chr);
1f51470d 1962 return NULL;
6f97dba0 1963 }
127338e6 1964 qemu_chr_generic_open(chr);
1f51470d 1965 return chr;
6f97dba0
AL
1966}
1967
1f51470d 1968static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
6f97dba0
AL
1969{
1970 CharDriverState *chr;
1971 WinCharState *s;
1972
7267c094
AL
1973 chr = g_malloc0(sizeof(CharDriverState));
1974 s = g_malloc0(sizeof(WinCharState));
6f97dba0
AL
1975 s->hcom = fd_out;
1976 chr->opaque = s;
1977 chr->chr_write = win_chr_write;
127338e6 1978 qemu_chr_generic_open(chr);
1f51470d 1979 return chr;
6f97dba0
AL
1980}
1981
d9ac374f 1982static CharDriverState *qemu_chr_open_win_con(void)
6f97dba0 1983{
1f51470d 1984 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
6f97dba0
AL
1985}
1986
db418a0a
FC
1987static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1988{
1989 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1990 DWORD dwSize;
1991 int len1;
1992
1993 len1 = len;
1994
1995 while (len1 > 0) {
1996 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1997 break;
1998 }
1999 buf += dwSize;
2000 len1 -= dwSize;
2001 }
2002
2003 return len - len1;
2004}
2005
2006static void win_stdio_wait_func(void *opaque)
2007{
2008 CharDriverState *chr = opaque;
2009 WinStdioCharState *stdio = chr->opaque;
2010 INPUT_RECORD buf[4];
2011 int ret;
2012 DWORD dwSize;
2013 int i;
2014
2015 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2016 &dwSize);
2017
2018 if (!ret) {
2019 /* Avoid error storm */
2020 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2021 return;
2022 }
2023
2024 for (i = 0; i < dwSize; i++) {
2025 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2026
2027 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2028 int j;
2029 if (kev->uChar.AsciiChar != 0) {
2030 for (j = 0; j < kev->wRepeatCount; j++) {
2031 if (qemu_chr_be_can_write(chr)) {
2032 uint8_t c = kev->uChar.AsciiChar;
2033 qemu_chr_be_write(chr, &c, 1);
2034 }
2035 }
2036 }
2037 }
2038 }
2039}
2040
2041static DWORD WINAPI win_stdio_thread(LPVOID param)
2042{
2043 CharDriverState *chr = param;
2044 WinStdioCharState *stdio = chr->opaque;
2045 int ret;
2046 DWORD dwSize;
2047
2048 while (1) {
2049
2050 /* Wait for one byte */
2051 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2052
2053 /* Exit in case of error, continue if nothing read */
2054 if (!ret) {
2055 break;
2056 }
2057 if (!dwSize) {
2058 continue;
2059 }
2060
2061 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2062 if (stdio->win_stdio_buf == '\r') {
2063 continue;
2064 }
2065
2066 /* Signal the main thread and wait until the byte was eaten */
2067 if (!SetEvent(stdio->hInputReadyEvent)) {
2068 break;
2069 }
2070 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2071 != WAIT_OBJECT_0) {
2072 break;
2073 }
2074 }
2075
2076 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2077 return 0;
2078}
2079
2080static void win_stdio_thread_wait_func(void *opaque)
2081{
2082 CharDriverState *chr = opaque;
2083 WinStdioCharState *stdio = chr->opaque;
2084
2085 if (qemu_chr_be_can_write(chr)) {
2086 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2087 }
2088
2089 SetEvent(stdio->hInputDoneEvent);
2090}
2091
2092static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2093{
2094 WinStdioCharState *stdio = chr->opaque;
2095 DWORD dwMode = 0;
2096
2097 GetConsoleMode(stdio->hStdIn, &dwMode);
2098
2099 if (echo) {
2100 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2101 } else {
2102 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2103 }
2104}
2105
2106static void win_stdio_close(CharDriverState *chr)
2107{
2108 WinStdioCharState *stdio = chr->opaque;
2109
2110 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2111 CloseHandle(stdio->hInputReadyEvent);
2112 }
2113 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2114 CloseHandle(stdio->hInputDoneEvent);
2115 }
2116 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2117 TerminateThread(stdio->hInputThread, 0);
2118 }
2119
2120 g_free(chr->opaque);
2121 g_free(chr);
db418a0a
FC
2122}
2123
7c358031 2124static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
db418a0a
FC
2125{
2126 CharDriverState *chr;
2127 WinStdioCharState *stdio;
2128 DWORD dwMode;
2129 int is_console = 0;
2130
db418a0a
FC
2131 chr = g_malloc0(sizeof(CharDriverState));
2132 stdio = g_malloc0(sizeof(WinStdioCharState));
2133
2134 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2135 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2136 fprintf(stderr, "cannot open stdio: invalid handle\n");
2137 exit(1);
2138 }
2139
2140 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2141
2142 chr->opaque = stdio;
2143 chr->chr_write = win_stdio_write;
2144 chr->chr_close = win_stdio_close;
2145
ed7a1540
AL
2146 if (is_console) {
2147 if (qemu_add_wait_object(stdio->hStdIn,
2148 win_stdio_wait_func, chr)) {
2149 fprintf(stderr, "qemu_add_wait_object: failed\n");
2150 }
2151 } else {
2152 DWORD dwId;
2153
2154 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2155 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2156 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2157 chr, 0, &dwId);
2158
2159 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2160 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2161 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2162 fprintf(stderr, "cannot create stdio thread or event\n");
2163 exit(1);
2164 }
2165 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2166 win_stdio_thread_wait_func, chr)) {
2167 fprintf(stderr, "qemu_add_wait_object: failed\n");
db418a0a
FC
2168 }
2169 }
2170
2171 dwMode |= ENABLE_LINE_INPUT;
2172
ed7a1540 2173 if (is_console) {
db418a0a
FC
2174 /* set the terminal in raw mode */
2175 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2176 dwMode |= ENABLE_PROCESSED_INPUT;
2177 }
2178
2179 SetConsoleMode(stdio->hStdIn, dwMode);
2180
2181 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2182 qemu_chr_fe_set_echo(chr, false);
2183
1f51470d 2184 return chr;
db418a0a 2185}
6f97dba0
AL
2186#endif /* !_WIN32 */
2187
5ab8211b 2188
6f97dba0
AL
2189/***********************************************************/
2190/* UDP Net console */
2191
2192typedef struct {
2193 int fd;
76a9644b
AL
2194 GIOChannel *chan;
2195 guint tag;
9bd7854e 2196 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
2197 int bufcnt;
2198 int bufptr;
2199 int max_size;
2200} NetCharDriver;
2201
2202static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2203{
2204 NetCharDriver *s = chr->opaque;
76a9644b
AL
2205 gsize bytes_written;
2206 GIOStatus status;
2207
2208 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2209 if (status == G_IO_STATUS_EOF) {
2210 return 0;
2211 } else if (status != G_IO_STATUS_NORMAL) {
2212 return -1;
2213 }
6f97dba0 2214
76a9644b 2215 return bytes_written;
6f97dba0
AL
2216}
2217
2218static int udp_chr_read_poll(void *opaque)
2219{
2220 CharDriverState *chr = opaque;
2221 NetCharDriver *s = chr->opaque;
2222
909cda12 2223 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2224
2225 /* If there were any stray characters in the queue process them
2226 * first
2227 */
2228 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
fa5efccb 2229 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
6f97dba0 2230 s->bufptr++;
909cda12 2231 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2232 }
2233 return s->max_size;
2234}
2235
76a9644b 2236static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
6f97dba0
AL
2237{
2238 CharDriverState *chr = opaque;
2239 NetCharDriver *s = chr->opaque;
76a9644b
AL
2240 gsize bytes_read = 0;
2241 GIOStatus status;
6f97dba0
AL
2242
2243 if (s->max_size == 0)
76a9644b
AL
2244 return FALSE;
2245 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2246 &bytes_read, NULL);
2247 s->bufcnt = bytes_read;
6f97dba0 2248 s->bufptr = s->bufcnt;
76a9644b
AL
2249 if (status != G_IO_STATUS_NORMAL) {
2250 return FALSE;
2251 }
6f97dba0
AL
2252
2253 s->bufptr = 0;
2254 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
fa5efccb 2255 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
6f97dba0 2256 s->bufptr++;
909cda12 2257 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0 2258 }
76a9644b
AL
2259
2260 return TRUE;
6f97dba0
AL
2261}
2262
2263static void udp_chr_update_read_handler(CharDriverState *chr)
2264{
2265 NetCharDriver *s = chr->opaque;
2266
76a9644b
AL
2267 if (s->tag) {
2268 g_source_remove(s->tag);
2269 s->tag = 0;
2270 }
2271
2272 if (s->chan) {
2273 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
6f97dba0
AL
2274 }
2275}
2276
819f56b7
AL
2277static void udp_chr_close(CharDriverState *chr)
2278{
2279 NetCharDriver *s = chr->opaque;
76a9644b
AL
2280 if (s->tag) {
2281 g_source_remove(s->tag);
2282 }
2283 if (s->chan) {
2284 g_io_channel_unref(s->chan);
819f56b7
AL
2285 closesocket(s->fd);
2286 }
7267c094 2287 g_free(s);
a425d23f 2288 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
819f56b7
AL
2289}
2290
3ecc059d 2291static CharDriverState *qemu_chr_open_udp_fd(int fd)
6f97dba0
AL
2292{
2293 CharDriverState *chr = NULL;
2294 NetCharDriver *s = NULL;
6f97dba0 2295
7267c094
AL
2296 chr = g_malloc0(sizeof(CharDriverState));
2297 s = g_malloc0(sizeof(NetCharDriver));
6f97dba0 2298
6f97dba0 2299 s->fd = fd;
76a9644b 2300 s->chan = io_channel_from_socket(s->fd);
6f97dba0
AL
2301 s->bufcnt = 0;
2302 s->bufptr = 0;
2303 chr->opaque = s;
2304 chr->chr_write = udp_chr_write;
2305 chr->chr_update_read_handler = udp_chr_update_read_handler;
819f56b7 2306 chr->chr_close = udp_chr_close;
1f51470d 2307 return chr;
3ecc059d 2308}
6f97dba0 2309
3ecc059d
GH
2310static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2311{
2312 Error *local_err = NULL;
2313 int fd = -1;
2314
2315 fd = inet_dgram_opts(opts, &local_err);
2316 if (fd < 0) {
2317 return NULL;
6e1db57b 2318 }
3ecc059d 2319 return qemu_chr_open_udp_fd(fd);
6f97dba0
AL
2320}
2321
2322/***********************************************************/
2323/* TCP Net console */
2324
2325typedef struct {
2ea5a7af
AL
2326
2327 GIOChannel *chan, *listen_chan;
2328 guint tag, listen_tag;
6f97dba0
AL
2329 int fd, listen_fd;
2330 int connected;
2331 int max_size;
2332 int do_telnetopt;
2333 int do_nodelay;
2334 int is_unix;
7d174059 2335 int msgfd;
6f97dba0
AL
2336} TCPCharDriver;
2337
2ea5a7af 2338static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
6f97dba0
AL
2339
2340static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2341{
2342 TCPCharDriver *s = chr->opaque;
2343 if (s->connected) {
2ea5a7af 2344 return io_channel_send_all(s->chan, buf, len);
455aa1e0 2345 } else {
6db0fdce 2346 /* XXX: indicate an error ? */
455aa1e0 2347 return len;
6f97dba0
AL
2348 }
2349}
2350
2351static int tcp_chr_read_poll(void *opaque)
2352{
2353 CharDriverState *chr = opaque;
2354 TCPCharDriver *s = chr->opaque;
2355 if (!s->connected)
2356 return 0;
909cda12 2357 s->max_size = qemu_chr_be_can_write(chr);
6f97dba0
AL
2358 return s->max_size;
2359}
2360
2361#define IAC 255
2362#define IAC_BREAK 243
2363static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2364 TCPCharDriver *s,
2365 uint8_t *buf, int *size)
2366{
2367 /* Handle any telnet client's basic IAC options to satisfy char by
2368 * char mode with no echo. All IAC options will be removed from
2369 * the buf and the do_telnetopt variable will be used to track the
2370 * state of the width of the IAC information.
2371 *
2372 * IAC commands come in sets of 3 bytes with the exception of the
2373 * "IAC BREAK" command and the double IAC.
2374 */
2375
2376 int i;
2377 int j = 0;
2378
2379 for (i = 0; i < *size; i++) {
2380 if (s->do_telnetopt > 1) {
2381 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2382 /* Double IAC means send an IAC */
2383 if (j != i)
2384 buf[j] = buf[i];
2385 j++;
2386 s->do_telnetopt = 1;
2387 } else {
2388 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2389 /* Handle IAC break commands by sending a serial break */
a425d23f 2390 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
6f97dba0
AL
2391 s->do_telnetopt++;
2392 }
2393 s->do_telnetopt++;
2394 }
2395 if (s->do_telnetopt >= 4) {
2396 s->do_telnetopt = 1;
2397 }
2398 } else {
2399 if ((unsigned char)buf[i] == IAC) {
2400 s->do_telnetopt = 2;
2401 } else {
2402 if (j != i)
2403 buf[j] = buf[i];
2404 j++;
2405 }
2406 }
2407 }
2408 *size = j;
2409}
2410
7d174059
MM
2411static int tcp_get_msgfd(CharDriverState *chr)
2412{
2413 TCPCharDriver *s = chr->opaque;
e53f27b9
PB
2414 int fd = s->msgfd;
2415 s->msgfd = -1;
2416 return fd;
7d174059
MM
2417}
2418
73bcc2ac 2419#ifndef _WIN32
7d174059
MM
2420static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2421{
2422 TCPCharDriver *s = chr->opaque;
2423 struct cmsghdr *cmsg;
2424
2425 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2426 int fd;
2427
2428 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2429 cmsg->cmsg_level != SOL_SOCKET ||
2430 cmsg->cmsg_type != SCM_RIGHTS)
2431 continue;
2432
2433 fd = *((int *)CMSG_DATA(cmsg));
2434 if (fd < 0)
2435 continue;
2436
06138651
CB
2437#ifndef MSG_CMSG_CLOEXEC
2438 qemu_set_cloexec(fd);
2439#endif
7d174059
MM
2440 if (s->msgfd != -1)
2441 close(s->msgfd);
2442 s->msgfd = fd;
2443 }
2444}
2445
9977c894
MM
2446static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2447{
2448 TCPCharDriver *s = chr->opaque;
7cba04f6 2449 struct msghdr msg = { NULL, };
9977c894 2450 struct iovec iov[1];
7d174059
MM
2451 union {
2452 struct cmsghdr cmsg;
2453 char control[CMSG_SPACE(sizeof(int))];
2454 } msg_control;
06138651 2455 int flags = 0;
7d174059 2456 ssize_t ret;
9977c894
MM
2457
2458 iov[0].iov_base = buf;
2459 iov[0].iov_len = len;
2460
2461 msg.msg_iov = iov;
2462 msg.msg_iovlen = 1;
7d174059
MM
2463 msg.msg_control = &msg_control;
2464 msg.msg_controllen = sizeof(msg_control);
2465
06138651
CB
2466#ifdef MSG_CMSG_CLOEXEC
2467 flags |= MSG_CMSG_CLOEXEC;
2468#endif
2469 ret = recvmsg(s->fd, &msg, flags);
2470 if (ret > 0 && s->is_unix) {
7d174059 2471 unix_process_msgfd(chr, &msg);
06138651 2472 }
9977c894 2473
7d174059 2474 return ret;
9977c894
MM
2475}
2476#else
2477static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2478{
2479 TCPCharDriver *s = chr->opaque;
00aa0040 2480 return qemu_recv(s->fd, buf, len, 0);
9977c894
MM
2481}
2482#endif
2483
d3cc5bc4
AS
2484static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2485{
2486 TCPCharDriver *s = chr->opaque;
2487 return g_io_create_watch(s->chan, cond);
2488}
2489
2ea5a7af 2490static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
6f97dba0
AL
2491{
2492 CharDriverState *chr = opaque;
2493 TCPCharDriver *s = chr->opaque;
9bd7854e 2494 uint8_t buf[READ_BUF_LEN];
6f97dba0
AL
2495 int len, size;
2496
2ea5a7af
AL
2497 if (!s->connected || s->max_size <= 0) {
2498 return FALSE;
2499 }
6f97dba0
AL
2500 len = sizeof(buf);
2501 if (len > s->max_size)
2502 len = s->max_size;
9977c894 2503 size = tcp_chr_recv(chr, (void *)buf, len);
6f97dba0
AL
2504 if (size == 0) {
2505 /* connection closed */
2506 s->connected = 0;
2ea5a7af
AL
2507 if (s->listen_chan) {
2508 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
6f97dba0 2509 }
2ea5a7af
AL
2510 g_source_remove(s->tag);
2511 s->tag = 0;
2512 g_io_channel_unref(s->chan);
2513 s->chan = NULL;
6f97dba0
AL
2514 closesocket(s->fd);
2515 s->fd = -1;
a425d23f 2516 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
2517 } else if (size > 0) {
2518 if (s->do_telnetopt)
2519 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2520 if (size > 0)
fa5efccb 2521 qemu_chr_be_write(chr, buf, size);
6f97dba0 2522 }
2ea5a7af
AL
2523
2524 return TRUE;
6f97dba0
AL
2525}
2526
68c18d1c
BS
2527#ifndef _WIN32
2528CharDriverState *qemu_chr_open_eventfd(int eventfd)
2529{
6cbf4c8c 2530 return qemu_chr_open_fd(eventfd, eventfd);
6cbf4c8c 2531}
68c18d1c 2532#endif
6cbf4c8c 2533
6f97dba0
AL
2534static void tcp_chr_connect(void *opaque)
2535{
2536 CharDriverState *chr = opaque;
2537 TCPCharDriver *s = chr->opaque;
2538
2539 s->connected = 1;
2ea5a7af
AL
2540 if (s->chan) {
2541 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
bbdd2ad0 2542 }
127338e6 2543 qemu_chr_generic_open(chr);
6f97dba0
AL
2544}
2545
2546#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2547static void tcp_chr_telnet_init(int fd)
2548{
2549 char buf[3];
2550 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2551 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2552 send(fd, (char *)buf, 3, 0);
2553 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2554 send(fd, (char *)buf, 3, 0);
2555 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2556 send(fd, (char *)buf, 3, 0);
2557 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2558 send(fd, (char *)buf, 3, 0);
2559}
2560
13661089
DB
2561static int tcp_chr_add_client(CharDriverState *chr, int fd)
2562{
2563 TCPCharDriver *s = chr->opaque;
2564 if (s->fd != -1)
2565 return -1;
2566
2567 socket_set_nonblock(fd);
2568 if (s->do_nodelay)
2569 socket_set_nodelay(fd);
2570 s->fd = fd;
2ea5a7af
AL
2571 s->chan = io_channel_from_socket(fd);
2572 g_source_remove(s->listen_tag);
2573 s->listen_tag = 0;
13661089
DB
2574 tcp_chr_connect(chr);
2575
2576 return 0;
2577}
2578
2ea5a7af 2579static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
6f97dba0
AL
2580{
2581 CharDriverState *chr = opaque;
2582 TCPCharDriver *s = chr->opaque;
2583 struct sockaddr_in saddr;
2584#ifndef _WIN32
2585 struct sockaddr_un uaddr;
2586#endif
2587 struct sockaddr *addr;
2588 socklen_t len;
2589 int fd;
2590
2591 for(;;) {
2592#ifndef _WIN32
2593 if (s->is_unix) {
2594 len = sizeof(uaddr);
2595 addr = (struct sockaddr *)&uaddr;
2596 } else
2597#endif
2598 {
2599 len = sizeof(saddr);
2600 addr = (struct sockaddr *)&saddr;
2601 }
40ff6d7e 2602 fd = qemu_accept(s->listen_fd, addr, &len);
6f97dba0 2603 if (fd < 0 && errno != EINTR) {
2ea5a7af 2604 return FALSE;
6f97dba0
AL
2605 } else if (fd >= 0) {
2606 if (s->do_telnetopt)
2607 tcp_chr_telnet_init(fd);
2608 break;
2609 }
2610 }
13661089
DB
2611 if (tcp_chr_add_client(chr, fd) < 0)
2612 close(fd);
2ea5a7af
AL
2613
2614 return TRUE;
6f97dba0
AL
2615}
2616
2617static void tcp_chr_close(CharDriverState *chr)
2618{
2619 TCPCharDriver *s = chr->opaque;
819f56b7 2620 if (s->fd >= 0) {
2ea5a7af
AL
2621 if (s->tag) {
2622 g_source_remove(s->tag);
2623 }
2624 if (s->chan) {
2625 g_io_channel_unref(s->chan);
2626 }
6f97dba0 2627 closesocket(s->fd);
819f56b7
AL
2628 }
2629 if (s->listen_fd >= 0) {
2ea5a7af
AL
2630 if (s->listen_tag) {
2631 g_source_remove(s->listen_tag);
2632 }
2633 if (s->listen_chan) {
2634 g_io_channel_unref(s->listen_chan);
2635 }
6f97dba0 2636 closesocket(s->listen_fd);
819f56b7 2637 }
7267c094 2638 g_free(s);
a425d23f 2639 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
6f97dba0
AL
2640}
2641
f6bd5d6e
GH
2642static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2643 bool is_listen, bool is_telnet,
2644 bool is_waitconnect,
2645 Error **errp)
6f97dba0
AL
2646{
2647 CharDriverState *chr = NULL;
2648 TCPCharDriver *s = NULL;
f6bd5d6e
GH
2649 char host[NI_MAXHOST], serv[NI_MAXSERV];
2650 const char *left = "", *right = "";
2651 struct sockaddr_storage ss;
2652 socklen_t ss_len = sizeof(ss);
2653
2654 memset(&ss, 0, ss_len);
2655 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2656 error_setg(errp, "getsockname: %s", strerror(errno));
2657 return NULL;
2658 }
2659
2660 chr = g_malloc0(sizeof(CharDriverState));
2661 s = g_malloc0(sizeof(TCPCharDriver));
2662
2663 s->connected = 0;
2664 s->fd = -1;
2665 s->listen_fd = -1;
2666 s->msgfd = -1;
2667
2668 chr->filename = g_malloc(256);
2669 switch (ss.ss_family) {
2670#ifndef _WIN32
2671 case AF_UNIX:
2672 s->is_unix = 1;
2673 snprintf(chr->filename, 256, "unix:%s%s",
2674 ((struct sockaddr_un *)(&ss))->sun_path,
2675 is_listen ? ",server" : "");
2676 break;
2677#endif
2678 case AF_INET6:
2679 left = "[";
2680 right = "]";
2681 /* fall through */
2682 case AF_INET:
2683 s->do_nodelay = do_nodelay;
2684 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2685 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
e5545854 2686 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
f6bd5d6e
GH
2687 is_telnet ? "telnet" : "tcp",
2688 left, host, right, serv,
2689 is_listen ? ",server" : "");
2690 break;
2691 }
2692
2693 chr->opaque = s;
2694 chr->chr_write = tcp_chr_write;
2695 chr->chr_close = tcp_chr_close;
2696 chr->get_msgfd = tcp_get_msgfd;
2697 chr->chr_add_client = tcp_chr_add_client;
d3cc5bc4 2698 chr->chr_add_watch = tcp_chr_add_watch;
f6bd5d6e
GH
2699
2700 if (is_listen) {
2701 s->listen_fd = fd;
2ea5a7af
AL
2702 s->listen_chan = io_channel_from_socket(s->listen_fd);
2703 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
f6bd5d6e
GH
2704 if (is_telnet) {
2705 s->do_telnetopt = 1;
2706 }
2707 } else {
2708 s->connected = 1;
2709 s->fd = fd;
2710 socket_set_nodelay(fd);
2ea5a7af 2711 s->chan = io_channel_from_socket(s->fd);
f6bd5d6e
GH
2712 tcp_chr_connect(chr);
2713 }
2714
2715 if (is_listen && is_waitconnect) {
2716 printf("QEMU waiting for connection on: %s\n",
2717 chr->filename);
2ea5a7af 2718 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
f6bd5d6e
GH
2719 socket_set_nonblock(s->listen_fd);
2720 }
2721 return chr;
2722}
2723
2724static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2725{
2726 CharDriverState *chr = NULL;
87d5f24f 2727 Error *local_err = NULL;
aeb2c47a
GH
2728 int fd = -1;
2729 int is_listen;
2730 int is_waitconnect;
2731 int do_nodelay;
2732 int is_unix;
2733 int is_telnet;
2734
2735 is_listen = qemu_opt_get_bool(opts, "server", 0);
2736 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2737 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2738 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2739 is_unix = qemu_opt_get(opts, "path") != NULL;
6f97dba0
AL
2740 if (!is_listen)
2741 is_waitconnect = 0;
2742
f07b6003
AL
2743 if (is_unix) {
2744 if (is_listen) {
87d5f24f 2745 fd = unix_listen_opts(opts, &local_err);
f07b6003 2746 } else {
87d5f24f 2747 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
f07b6003
AL
2748 }
2749 } else {
2750 if (is_listen) {
87d5f24f 2751 fd = inet_listen_opts(opts, 0, &local_err);
f07b6003 2752 } else {
87d5f24f 2753 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
f07b6003
AL
2754 }
2755 }
a89dd6c3 2756 if (fd < 0) {
6f97dba0 2757 goto fail;
a89dd6c3 2758 }
6f97dba0
AL
2759
2760 if (!is_waitconnect)
2761 socket_set_nonblock(fd);
2762
f6bd5d6e
GH
2763 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2764 is_waitconnect, &local_err);
2765 if (error_is_set(&local_err)) {
2766 goto fail;
6f97dba0 2767 }
1f51470d 2768 return chr;
aeb2c47a 2769
f6bd5d6e 2770
6f97dba0 2771 fail:
87d5f24f
PB
2772 if (local_err) {
2773 qerror_report_err(local_err);
2774 error_free(local_err);
2775 }
2776 if (fd >= 0) {
6f97dba0 2777 closesocket(fd);
87d5f24f 2778 }
f6bd5d6e
GH
2779 if (chr) {
2780 g_free(chr->opaque);
2781 g_free(chr);
2782 }
1f51470d 2783 return NULL;
6f97dba0
AL
2784}
2785
999bd67c
LC
2786/***********************************************************/
2787/* Memory chardev */
2788typedef struct {
2789 size_t outbuf_size;
2790 size_t outbuf_capacity;
2791 uint8_t *outbuf;
2792} MemoryDriver;
2793
2794static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2795{
2796 MemoryDriver *d = chr->opaque;
2797
2798 /* TODO: the QString implementation has the same code, we should
2799 * introduce a generic way to do this in cutils.c */
2800 if (d->outbuf_capacity < d->outbuf_size + len) {
2801 /* grow outbuf */
2802 d->outbuf_capacity += len;
2803 d->outbuf_capacity *= 2;
7267c094 2804 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
999bd67c
LC
2805 }
2806
2807 memcpy(d->outbuf + d->outbuf_size, buf, len);
2808 d->outbuf_size += len;
2809
2810 return len;
2811}
2812
2813void qemu_chr_init_mem(CharDriverState *chr)
2814{
2815 MemoryDriver *d;
2816
7267c094 2817 d = g_malloc(sizeof(*d));
999bd67c
LC
2818 d->outbuf_size = 0;
2819 d->outbuf_capacity = 4096;
7267c094 2820 d->outbuf = g_malloc0(d->outbuf_capacity);
999bd67c
LC
2821
2822 memset(chr, 0, sizeof(*chr));
2823 chr->opaque = d;
2824 chr->chr_write = mem_chr_write;
2825}
2826
2827QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2828{
2829 MemoryDriver *d = chr->opaque;
2830 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2831}
2832
70f24fb6 2833/* NOTE: this driver can not be closed with qemu_chr_delete()! */
999bd67c
LC
2834void qemu_chr_close_mem(CharDriverState *chr)
2835{
2836 MemoryDriver *d = chr->opaque;
2837
7267c094
AL
2838 g_free(d->outbuf);
2839 g_free(chr->opaque);
999bd67c
LC
2840 chr->opaque = NULL;
2841 chr->chr_write = NULL;
2842}
2843
2844size_t qemu_chr_mem_osize(const CharDriverState *chr)
2845{
2846 const MemoryDriver *d = chr->opaque;
2847 return d->outbuf_size;
2848}
2849
51767e7c 2850/*********************************************************/
3949e594 2851/* Ring buffer chardev */
51767e7c
LL
2852
2853typedef struct {
2854 size_t size;
2855 size_t prod;
2856 size_t cons;
2857 uint8_t *cbuf;
3949e594 2858} RingBufCharDriver;
51767e7c 2859
3949e594 2860static size_t ringbuf_count(const CharDriverState *chr)
51767e7c 2861{
3949e594 2862 const RingBufCharDriver *d = chr->opaque;
51767e7c 2863
5c230105 2864 return d->prod - d->cons;
51767e7c
LL
2865}
2866
3949e594 2867static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
51767e7c 2868{
3949e594 2869 RingBufCharDriver *d = chr->opaque;
51767e7c
LL
2870 int i;
2871
2872 if (!buf || (len < 0)) {
2873 return -1;
2874 }
2875
2876 for (i = 0; i < len; i++ ) {
5c230105
MA
2877 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2878 if (d->prod - d->cons > d->size) {
51767e7c
LL
2879 d->cons = d->prod - d->size;
2880 }
2881 }
2882
2883 return 0;
2884}
2885
3949e594 2886static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
51767e7c 2887{
3949e594 2888 RingBufCharDriver *d = chr->opaque;
51767e7c
LL
2889 int i;
2890
5c230105
MA
2891 for (i = 0; i < len && d->cons != d->prod; i++) {
2892 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
51767e7c
LL
2893 }
2894
2895 return i;
2896}
2897
3949e594 2898static void ringbuf_chr_close(struct CharDriverState *chr)
51767e7c 2899{
3949e594 2900 RingBufCharDriver *d = chr->opaque;
51767e7c
LL
2901
2902 g_free(d->cbuf);
2903 g_free(d);
2904 chr->opaque = NULL;
2905}
2906
1da48c65
GH
2907static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2908 Error **errp)
51767e7c
LL
2909{
2910 CharDriverState *chr;
3949e594 2911 RingBufCharDriver *d;
51767e7c
LL
2912
2913 chr = g_malloc0(sizeof(CharDriverState));
2914 d = g_malloc(sizeof(*d));
2915
1da48c65 2916 d->size = opts->has_size ? opts->size : 65536;
51767e7c
LL
2917
2918 /* The size must be power of 2 */
2919 if (d->size & (d->size - 1)) {
1da48c65 2920 error_setg(errp, "size of ringbuf chardev must be power of two");
51767e7c
LL
2921 goto fail;
2922 }
2923
2924 d->prod = 0;
2925 d->cons = 0;
2926 d->cbuf = g_malloc0(d->size);
2927
2928 chr->opaque = d;
3949e594
MA
2929 chr->chr_write = ringbuf_chr_write;
2930 chr->chr_close = ringbuf_chr_close;
51767e7c
LL
2931
2932 return chr;
2933
2934fail:
2935 g_free(d);
2936 g_free(chr);
2937 return NULL;
2938}
2939
3949e594 2940static bool chr_is_ringbuf(const CharDriverState *chr)
1f590cf9 2941{
3949e594 2942 return chr->chr_write == ringbuf_chr_write;
1f590cf9
LL
2943}
2944
3949e594 2945void qmp_ringbuf_write(const char *device, const char *data,
82e59a67 2946 bool has_format, enum DataFormat format,
1f590cf9
LL
2947 Error **errp)
2948{
2949 CharDriverState *chr;
c4f331b6 2950 const uint8_t *write_data;
1f590cf9 2951 int ret;
c4f331b6 2952 size_t write_count;
1f590cf9
LL
2953
2954 chr = qemu_chr_find(device);
2955 if (!chr) {
1a69278e 2956 error_setg(errp, "Device '%s' not found", device);
1f590cf9
LL
2957 return;
2958 }
2959
3949e594
MA
2960 if (!chr_is_ringbuf(chr)) {
2961 error_setg(errp,"%s is not a ringbuf device", device);
1f590cf9
LL
2962 return;
2963 }
2964
1f590cf9
LL
2965 if (has_format && (format == DATA_FORMAT_BASE64)) {
2966 write_data = g_base64_decode(data, &write_count);
2967 } else {
2968 write_data = (uint8_t *)data;
82e59a67 2969 write_count = strlen(data);
1f590cf9
LL
2970 }
2971
3949e594 2972 ret = ringbuf_chr_write(chr, write_data, write_count);
1f590cf9 2973
13289fb5
MA
2974 if (write_data != (uint8_t *)data) {
2975 g_free((void *)write_data);
2976 }
2977
1f590cf9
LL
2978 if (ret < 0) {
2979 error_setg(errp, "Failed to write to device %s", device);
2980 return;
2981 }
2982}
2983
3949e594 2984char *qmp_ringbuf_read(const char *device, int64_t size,
3ab651fc
MA
2985 bool has_format, enum DataFormat format,
2986 Error **errp)
49b6d722
LL
2987{
2988 CharDriverState *chr;
c4f331b6 2989 uint8_t *read_data;
49b6d722 2990 size_t count;
3ab651fc 2991 char *data;
49b6d722
LL
2992
2993 chr = qemu_chr_find(device);
2994 if (!chr) {
1a69278e 2995 error_setg(errp, "Device '%s' not found", device);
49b6d722
LL
2996 return NULL;
2997 }
2998
3949e594
MA
2999 if (!chr_is_ringbuf(chr)) {
3000 error_setg(errp,"%s is not a ringbuf device", device);
49b6d722
LL
3001 return NULL;
3002 }
3003
3004 if (size <= 0) {
3005 error_setg(errp, "size must be greater than zero");
3006 return NULL;
3007 }
3008
3949e594 3009 count = ringbuf_count(chr);
49b6d722 3010 size = size > count ? count : size;
44f3bcd2 3011 read_data = g_malloc(size + 1);
49b6d722 3012
3949e594 3013 ringbuf_chr_read(chr, read_data, size);
49b6d722
LL
3014
3015 if (has_format && (format == DATA_FORMAT_BASE64)) {
3ab651fc 3016 data = g_base64_encode(read_data, size);
13289fb5 3017 g_free(read_data);
49b6d722 3018 } else {
3949e594
MA
3019 /*
3020 * FIXME should read only complete, valid UTF-8 characters up
3021 * to @size bytes. Invalid sequences should be replaced by a
3022 * suitable replacement character. Except when (and only
3023 * when) ring buffer lost characters since last read, initial
3024 * continuation characters should be dropped.
3025 */
44f3bcd2 3026 read_data[size] = 0;
3ab651fc 3027 data = (char *)read_data;
49b6d722
LL
3028 }
3029
3ab651fc 3030 return data;
49b6d722
LL
3031}
3032
33521634 3033QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
191bc01b 3034{
6ea314d9 3035 char host[65], port[33], width[8], height[8];
aeb2c47a 3036 int pos;
7d31544f 3037 const char *p;
191bc01b 3038 QemuOpts *opts;
8be7e7e4 3039 Error *local_err = NULL;
191bc01b 3040
8be7e7e4
LC
3041 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3042 if (error_is_set(&local_err)) {
3043 qerror_report_err(local_err);
3044 error_free(local_err);
191bc01b 3045 return NULL;
8be7e7e4 3046 }
191bc01b 3047
7591c5c1
GH
3048 if (strstart(filename, "mon:", &p)) {
3049 filename = p;
3050 qemu_opt_set(opts, "mux", "on");
3051 }
3052
f0457e8d
GH
3053 if (strcmp(filename, "null") == 0 ||
3054 strcmp(filename, "pty") == 0 ||
3055 strcmp(filename, "msmouse") == 0 ||
dc1c21e6 3056 strcmp(filename, "braille") == 0 ||
f0457e8d 3057 strcmp(filename, "stdio") == 0) {
4490dadf 3058 qemu_opt_set(opts, "backend", filename);
191bc01b
GH
3059 return opts;
3060 }
6ea314d9
GH
3061 if (strstart(filename, "vc", &p)) {
3062 qemu_opt_set(opts, "backend", "vc");
3063 if (*p == ':') {
3064 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3065 /* pixels */
3066 qemu_opt_set(opts, "width", width);
3067 qemu_opt_set(opts, "height", height);
3068 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3069 /* chars */
3070 qemu_opt_set(opts, "cols", width);
3071 qemu_opt_set(opts, "rows", height);
3072 } else {
3073 goto fail;
3074 }
3075 }
3076 return opts;
3077 }
d6c983cd
GH
3078 if (strcmp(filename, "con:") == 0) {
3079 qemu_opt_set(opts, "backend", "console");
3080 return opts;
3081 }
48b76496
GH
3082 if (strstart(filename, "COM", NULL)) {
3083 qemu_opt_set(opts, "backend", "serial");
3084 qemu_opt_set(opts, "path", filename);
3085 return opts;
3086 }
7d31544f
GH
3087 if (strstart(filename, "file:", &p)) {
3088 qemu_opt_set(opts, "backend", "file");
3089 qemu_opt_set(opts, "path", p);
3090 return opts;
3091 }
3092 if (strstart(filename, "pipe:", &p)) {
3093 qemu_opt_set(opts, "backend", "pipe");
3094 qemu_opt_set(opts, "path", p);
3095 return opts;
3096 }
aeb2c47a
GH
3097 if (strstart(filename, "tcp:", &p) ||
3098 strstart(filename, "telnet:", &p)) {
3099 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3100 host[0] = 0;
3101 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3102 goto fail;
3103 }
3104 qemu_opt_set(opts, "backend", "socket");
3105 qemu_opt_set(opts, "host", host);
3106 qemu_opt_set(opts, "port", port);
3107 if (p[pos] == ',') {
3108 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3109 goto fail;
3110 }
3111 if (strstart(filename, "telnet:", &p))
3112 qemu_opt_set(opts, "telnet", "on");
3113 return opts;
3114 }
7e1b35b4
GH
3115 if (strstart(filename, "udp:", &p)) {
3116 qemu_opt_set(opts, "backend", "udp");
3117 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3118 host[0] = 0;
39324ca4 3119 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
7e1b35b4
GH
3120 goto fail;
3121 }
3122 }
3123 qemu_opt_set(opts, "host", host);
3124 qemu_opt_set(opts, "port", port);
3125 if (p[pos] == '@') {
3126 p += pos + 1;
3127 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3128 host[0] = 0;
3129 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
7e1b35b4
GH
3130 goto fail;
3131 }
3132 }
3133 qemu_opt_set(opts, "localaddr", host);
3134 qemu_opt_set(opts, "localport", port);
3135 }
3136 return opts;
3137 }
aeb2c47a
GH
3138 if (strstart(filename, "unix:", &p)) {
3139 qemu_opt_set(opts, "backend", "socket");
3140 if (qemu_opts_do_parse(opts, p, "path") != 0)
3141 goto fail;
3142 return opts;
3143 }
48b76496
GH
3144 if (strstart(filename, "/dev/parport", NULL) ||
3145 strstart(filename, "/dev/ppi", NULL)) {
3146 qemu_opt_set(opts, "backend", "parport");
3147 qemu_opt_set(opts, "path", filename);
3148 return opts;
3149 }
3150 if (strstart(filename, "/dev/", NULL)) {
3151 qemu_opt_set(opts, "backend", "tty");
3152 qemu_opt_set(opts, "path", filename);
3153 return opts;
3154 }
191bc01b 3155
aeb2c47a 3156fail:
191bc01b
GH
3157 qemu_opts_del(opts);
3158 return NULL;
3159}
3160
846e2e49
GH
3161static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3162 Error **errp)
3163{
3164 const char *path = qemu_opt_get(opts, "path");
3165
3166 if (path == NULL) {
3167 error_setg(errp, "chardev: file: no filename given");
3168 return;
3169 }
3170 backend->file = g_new0(ChardevFile, 1);
3171 backend->file->out = g_strdup(path);
3172}
3173
7c358031
GH
3174static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3175 Error **errp)
3176{
3177 backend->stdio = g_new0(ChardevStdio, 1);
3178 backend->stdio->has_signal = true;
3179 backend->stdio->signal =
3180 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3181}
3182
0f1cb51d
GH
3183static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3184 Error **errp)
3185{
3186 const char *device = qemu_opt_get(opts, "path");
3187
3188 if (device == NULL) {
3189 error_setg(errp, "chardev: serial/tty: no device path given");
3190 return;
3191 }
3192 backend->serial = g_new0(ChardevHostdev, 1);
3193 backend->serial->device = g_strdup(device);
3194}
3195
dc375097
GH
3196static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3197 Error **errp)
3198{
3199 const char *device = qemu_opt_get(opts, "path");
3200
3201 if (device == NULL) {
3202 error_setg(errp, "chardev: parallel: no device path given");
3203 return;
3204 }
3205 backend->parallel = g_new0(ChardevHostdev, 1);
3206 backend->parallel->device = g_strdup(device);
3207}
3208
548cbb36
GH
3209static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3210 Error **errp)
3211{
3212 const char *device = qemu_opt_get(opts, "path");
3213
3214 if (device == NULL) {
3215 error_setg(errp, "chardev: pipe: no device path given");
3216 return;
3217 }
3218 backend->pipe = g_new0(ChardevHostdev, 1);
3219 backend->pipe->device = g_strdup(device);
3220}
3221
1da48c65
GH
3222static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3223 Error **errp)
3224{
3225 int val;
3226
3227 backend->memory = g_new0(ChardevRingbuf, 1);
3228
3229 val = qemu_opt_get_number(opts, "size", 0);
3230 if (val != 0) {
3231 backend->memory->has_size = true;
3232 backend->memory->size = val;
3233 }
3234}
3235
d654f34e 3236typedef struct CharDriver {
191bc01b 3237 const char *name;
2c5f4882 3238 /* old, pre qapi */
1f51470d 3239 CharDriverState *(*open)(QemuOpts *opts);
2c5f4882
GH
3240 /* new, qapi-based */
3241 int kind;
3242 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
d654f34e
AL
3243} CharDriver;
3244
3245static GSList *backends;
3246
3247void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3248{
3249 CharDriver *s;
3250
3251 s = g_malloc0(sizeof(*s));
3252 s->name = g_strdup(name);
3253 s->open = open;
3254
3255 backends = g_slist_append(backends, s);
3256}
191bc01b 3257
2c5f4882
GH
3258void register_char_driver_qapi(const char *name, int kind,
3259 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3260{
3261 CharDriver *s;
3262
3263 s = g_malloc0(sizeof(*s));
3264 s->name = g_strdup(name);
3265 s->kind = kind;
3266 s->parse = parse;
3267
3268 backends = g_slist_append(backends, s);
3269}
3270
f69554b9 3271CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
bd2d80b2
GH
3272 void (*init)(struct CharDriverState *s),
3273 Error **errp)
191bc01b 3274{
d654f34e 3275 CharDriver *cd;
191bc01b 3276 CharDriverState *chr;
d654f34e 3277 GSList *i;
191bc01b
GH
3278
3279 if (qemu_opts_id(opts) == NULL) {
312fd5f2 3280 error_setg(errp, "chardev: no id specified");
2274ae9d 3281 goto err;
191bc01b
GH
3282 }
3283
1bbd185f 3284 if (qemu_opt_get(opts, "backend") == NULL) {
312fd5f2 3285 error_setg(errp, "chardev: \"%s\" missing backend",
bd2d80b2 3286 qemu_opts_id(opts));
2274ae9d 3287 goto err;
1bbd185f 3288 }
d654f34e
AL
3289 for (i = backends; i; i = i->next) {
3290 cd = i->data;
3291
3292 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
191bc01b 3293 break;
d654f34e 3294 }
191bc01b 3295 }
d654f34e 3296 if (i == NULL) {
312fd5f2 3297 error_setg(errp, "chardev: backend \"%s\" not found",
bd2d80b2 3298 qemu_opt_get(opts, "backend"));
d654f34e 3299 return NULL;
191bc01b
GH
3300 }
3301
2c5f4882
GH
3302 if (!cd->open) {
3303 /* using new, qapi init */
3304 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3305 ChardevReturn *ret = NULL;
3306 const char *id = qemu_opts_id(opts);
edb2fb3c
GH
3307 const char *bid = NULL;
3308
3309 if (qemu_opt_get_bool(opts, "mux", 0)) {
3310 bid = g_strdup_printf("%s-base", id);
3311 }
2c5f4882
GH
3312
3313 chr = NULL;
3314 backend->kind = cd->kind;
3315 if (cd->parse) {
3316 cd->parse(opts, backend, errp);
3317 if (error_is_set(errp)) {
3318 goto qapi_out;
3319 }
3320 }
edb2fb3c 3321 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
2c5f4882
GH
3322 if (error_is_set(errp)) {
3323 goto qapi_out;
3324 }
edb2fb3c
GH
3325
3326 if (bid) {
3327 qapi_free_ChardevBackend(backend);
3328 qapi_free_ChardevReturn(ret);
3329 backend = g_new0(ChardevBackend, 1);
3330 backend->mux = g_new0(ChardevMux, 1);
3331 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3332 backend->mux->chardev = g_strdup(bid);
3333 ret = qmp_chardev_add(id, backend, errp);
3334 if (error_is_set(errp)) {
3335 goto qapi_out;
3336 }
3337 }
3338
2c5f4882
GH
3339 chr = qemu_chr_find(id);
3340
3341 qapi_out:
3342 qapi_free_ChardevBackend(backend);
3343 qapi_free_ChardevReturn(ret);
3344 return chr;
3345 }
3346
d654f34e 3347 chr = cd->open(opts);
1f51470d 3348 if (!chr) {
312fd5f2 3349 error_setg(errp, "chardev: opening backend \"%s\" failed",
bd2d80b2 3350 qemu_opt_get(opts, "backend"));
2274ae9d 3351 goto err;
191bc01b
GH
3352 }
3353
3354 if (!chr->filename)
7267c094 3355 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
191bc01b 3356 chr->init = init;
72cf2d4f 3357 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
7591c5c1
GH
3358
3359 if (qemu_opt_get_bool(opts, "mux", 0)) {
3360 CharDriverState *base = chr;
3361 int len = strlen(qemu_opts_id(opts)) + 6;
7267c094 3362 base->label = g_malloc(len);
7591c5c1
GH
3363 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3364 chr = qemu_chr_open_mux(base);
3365 chr->filename = base->filename;
d5b27167 3366 chr->avail_connections = MAX_MUX;
72cf2d4f 3367 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
d5b27167
KK
3368 } else {
3369 chr->avail_connections = 1;
7591c5c1 3370 }
7267c094 3371 chr->label = g_strdup(qemu_opts_id(opts));
2274ae9d 3372 chr->opts = opts;
191bc01b 3373 return chr;
2274ae9d
GH
3374
3375err:
3376 qemu_opts_del(opts);
3377 return NULL;
191bc01b
GH
3378}
3379
27143a44 3380CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
6f97dba0
AL
3381{
3382 const char *p;
3383 CharDriverState *chr;
191bc01b 3384 QemuOpts *opts;
bd2d80b2 3385 Error *err = NULL;
191bc01b 3386
c845f401
GH
3387 if (strstart(filename, "chardev:", &p)) {
3388 return qemu_chr_find(p);
3389 }
3390
191bc01b 3391 opts = qemu_chr_parse_compat(label, filename);
7e1b35b4
GH
3392 if (!opts)
3393 return NULL;
6f97dba0 3394
bd2d80b2
GH
3395 chr = qemu_chr_new_from_opts(opts, init, &err);
3396 if (error_is_set(&err)) {
3397 fprintf(stderr, "%s\n", error_get_pretty(err));
3398 error_free(err);
3399 }
7e1b35b4
GH
3400 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3401 monitor_init(chr, MONITOR_USE_READLINE);
6f97dba0
AL
3402 }
3403 return chr;
3404}
3405
15f31519 3406void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
c48855e1
PB
3407{
3408 if (chr->chr_set_echo) {
3409 chr->chr_set_echo(chr, echo);
3410 }
3411}
3412
c9d830ed 3413void qemu_chr_fe_open(struct CharDriverState *chr)
7c32c4fe
HG
3414{
3415 if (chr->chr_guest_open) {
3416 chr->chr_guest_open(chr);
3417 }
3418}
3419
2817822d 3420void qemu_chr_fe_close(struct CharDriverState *chr)
7c32c4fe
HG
3421{
3422 if (chr->chr_guest_close) {
3423 chr->chr_guest_close(chr);
3424 }
3425}
3426
2c8a5942
KW
3427int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3428 GIOFunc func, void *user_data)
23673ca7
AL
3429{
3430 GSource *src;
3431 guint tag;
3432
3433 if (s->chr_add_watch == NULL) {
3434 return -ENOSYS;
3435 }
3436
3437 src = s->chr_add_watch(s, cond);
3438 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3439 tag = g_source_attach(src, NULL);
3440 g_source_unref(src);
3441
3442 return tag;
3443}
3444
70f24fb6 3445void qemu_chr_delete(CharDriverState *chr)
6f97dba0 3446{
72cf2d4f 3447 QTAILQ_REMOVE(&chardevs, chr, next);
2274ae9d 3448 if (chr->chr_close) {
6f97dba0 3449 chr->chr_close(chr);
2274ae9d 3450 }
7267c094
AL
3451 g_free(chr->filename);
3452 g_free(chr->label);
2274ae9d
GH
3453 if (chr->opts) {
3454 qemu_opts_del(chr->opts);
3455 }
7267c094 3456 g_free(chr);
6f97dba0
AL
3457}
3458
c5a415a0 3459ChardevInfoList *qmp_query_chardev(Error **errp)
6f97dba0 3460{
c5a415a0 3461 ChardevInfoList *chr_list = NULL;
6f97dba0
AL
3462 CharDriverState *chr;
3463
72cf2d4f 3464 QTAILQ_FOREACH(chr, &chardevs, next) {
c5a415a0
LC
3465 ChardevInfoList *info = g_malloc0(sizeof(*info));
3466 info->value = g_malloc0(sizeof(*info->value));
3467 info->value->label = g_strdup(chr->label);
3468 info->value->filename = g_strdup(chr->filename);
3469
3470 info->next = chr_list;
3471 chr_list = info;
6f97dba0 3472 }
588b3832 3473
c5a415a0 3474 return chr_list;
6f97dba0 3475}
c845f401
GH
3476
3477CharDriverState *qemu_chr_find(const char *name)
3478{
3479 CharDriverState *chr;
3480
72cf2d4f 3481 QTAILQ_FOREACH(chr, &chardevs, next) {
c845f401
GH
3482 if (strcmp(chr->label, name) != 0)
3483 continue;
3484 return chr;
3485 }
3486 return NULL;
3487}
0beb4942
AL
3488
3489/* Get a character (serial) device interface. */
3490CharDriverState *qemu_char_get_next_serial(void)
3491{
3492 static int next_serial;
3493
3494 /* FIXME: This function needs to go away: use chardev properties! */
3495 return serial_hds[next_serial++];
3496}
3497
4d454574
PB
3498QemuOptsList qemu_chardev_opts = {
3499 .name = "chardev",
3500 .implied_opt_name = "backend",
3501 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3502 .desc = {
3503 {
3504 .name = "backend",
3505 .type = QEMU_OPT_STRING,
3506 },{
3507 .name = "path",
3508 .type = QEMU_OPT_STRING,
3509 },{
3510 .name = "host",
3511 .type = QEMU_OPT_STRING,
3512 },{
3513 .name = "port",
3514 .type = QEMU_OPT_STRING,
3515 },{
3516 .name = "localaddr",
3517 .type = QEMU_OPT_STRING,
3518 },{
3519 .name = "localport",
3520 .type = QEMU_OPT_STRING,
3521 },{
3522 .name = "to",
3523 .type = QEMU_OPT_NUMBER,
3524 },{
3525 .name = "ipv4",
3526 .type = QEMU_OPT_BOOL,
3527 },{
3528 .name = "ipv6",
3529 .type = QEMU_OPT_BOOL,
3530 },{
3531 .name = "wait",
3532 .type = QEMU_OPT_BOOL,
3533 },{
3534 .name = "server",
3535 .type = QEMU_OPT_BOOL,
3536 },{
3537 .name = "delay",
3538 .type = QEMU_OPT_BOOL,
3539 },{
3540 .name = "telnet",
3541 .type = QEMU_OPT_BOOL,
3542 },{
3543 .name = "width",
3544 .type = QEMU_OPT_NUMBER,
3545 },{
3546 .name = "height",
3547 .type = QEMU_OPT_NUMBER,
3548 },{
3549 .name = "cols",
3550 .type = QEMU_OPT_NUMBER,
3551 },{
3552 .name = "rows",
3553 .type = QEMU_OPT_NUMBER,
3554 },{
3555 .name = "mux",
3556 .type = QEMU_OPT_BOOL,
3557 },{
3558 .name = "signal",
3559 .type = QEMU_OPT_BOOL,
3560 },{
3561 .name = "name",
3562 .type = QEMU_OPT_STRING,
3563 },{
3564 .name = "debug",
3565 .type = QEMU_OPT_NUMBER,
51767e7c 3566 },{
3949e594 3567 .name = "size",
de1cc36e 3568 .type = QEMU_OPT_SIZE,
4d454574
PB
3569 },
3570 { /* end of list */ }
3571 },
3572};
f1a1a356 3573
ffbdbe59
GH
3574#ifdef _WIN32
3575
3576static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3577{
3578 HANDLE out;
3579
3580 if (file->in) {
3581 error_setg(errp, "input file not supported");
3582 return NULL;
3583 }
3584
3585 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3586 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3587 if (out == INVALID_HANDLE_VALUE) {
3588 error_setg(errp, "open %s failed", file->out);
3589 return NULL;
3590 }
3591 return qemu_chr_open_win_file(out);
3592}
3593
d36b2b90
MA
3594static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3595 Error **errp)
d59044ef 3596{
d36b2b90
MA
3597 return qemu_chr_open_win_path(serial->device);
3598}
3599
3600static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3601 Error **errp)
3602{
3603 error_setg(errp, "character device backend type 'parallel' not supported");
3604 return NULL;
d59044ef
GH
3605}
3606
ffbdbe59
GH
3607#else /* WIN32 */
3608
3609static int qmp_chardev_open_file_source(char *src, int flags,
3610 Error **errp)
3611{
3612 int fd = -1;
3613
3614 TFR(fd = qemu_open(src, flags, 0666));
3615 if (fd == -1) {
3616 error_setg(errp, "open %s: %s", src, strerror(errno));
3617 }
3618 return fd;
3619}
3620
3621static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3622{
3623 int flags, in = -1, out = -1;
3624
3625 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3626 out = qmp_chardev_open_file_source(file->out, flags, errp);
3627 if (error_is_set(errp)) {
3628 return NULL;
3629 }
3630
3631 if (file->in) {
3632 flags = O_RDONLY;
3633 in = qmp_chardev_open_file_source(file->in, flags, errp);
3634 if (error_is_set(errp)) {
3635 qemu_close(out);
3636 return NULL;
3637 }
3638 }
3639
3640 return qemu_chr_open_fd(in, out);
3641}
3642
d36b2b90
MA
3643static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3644 Error **errp)
d59044ef 3645{
d59044ef 3646#ifdef HAVE_CHARDEV_TTY
d36b2b90
MA
3647 int fd;
3648
3649 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3650 if (error_is_set(errp)) {
3651 return NULL;
39099991 3652 }
d36b2b90
MA
3653 socket_set_nonblock(fd);
3654 return qemu_chr_open_tty_fd(fd);
3655#else
3656 error_setg(errp, "character device backend type 'serial' not supported");
3657 return NULL;
88a946d3 3658#endif
d36b2b90
MA
3659}
3660
3661static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3662 Error **errp)
3663{
88a946d3 3664#ifdef HAVE_CHARDEV_PARPORT
d36b2b90
MA
3665 int fd;
3666
3667 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3668 if (error_is_set(errp)) {
d59044ef
GH
3669 return NULL;
3670 }
d36b2b90
MA
3671 return qemu_chr_open_pp_fd(fd);
3672#else
3673 error_setg(errp, "character device backend type 'parallel' not supported");
3674 return NULL;
3675#endif
d59044ef
GH
3676}
3677
ffbdbe59
GH
3678#endif /* WIN32 */
3679
f6bd5d6e
GH
3680static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3681 Error **errp)
3682{
3683 SocketAddress *addr = sock->addr;
3684 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3685 bool is_listen = sock->has_server ? sock->server : true;
3686 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3687 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3688 int fd;
3689
3690 if (is_listen) {
3691 fd = socket_listen(addr, errp);
3692 } else {
3693 fd = socket_connect(addr, errp, NULL, NULL);
3694 }
3695 if (error_is_set(errp)) {
3696 return NULL;
3697 }
3698 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3699 is_telnet, is_waitconnect, errp);
3700}
3701
3ecc059d
GH
3702static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3703 Error **errp)
3704{
3705 int fd;
3706
3707 fd = socket_dgram(dgram->remote, dgram->local, errp);
3708 if (error_is_set(errp)) {
3709 return NULL;
3710 }
3711 return qemu_chr_open_udp_fd(fd);
3712}
3713
f1a1a356
GH
3714ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3715 Error **errp)
3716{
3717 ChardevReturn *ret = g_new0(ChardevReturn, 1);
edb2fb3c 3718 CharDriverState *base, *chr = NULL;
f1a1a356
GH
3719
3720 chr = qemu_chr_find(id);
3721 if (chr) {
3722 error_setg(errp, "Chardev '%s' already exists", id);
3723 g_free(ret);
3724 return NULL;
3725 }
3726
3727 switch (backend->kind) {
ffbdbe59
GH
3728 case CHARDEV_BACKEND_KIND_FILE:
3729 chr = qmp_chardev_open_file(backend->file, errp);
3730 break;
d36b2b90
MA
3731 case CHARDEV_BACKEND_KIND_SERIAL:
3732 chr = qmp_chardev_open_serial(backend->serial, errp);
3733 break;
3734 case CHARDEV_BACKEND_KIND_PARALLEL:
3735 chr = qmp_chardev_open_parallel(backend->parallel, errp);
d59044ef 3736 break;
548cbb36
GH
3737 case CHARDEV_BACKEND_KIND_PIPE:
3738 chr = qemu_chr_open_pipe(backend->pipe);
3739 break;
f6bd5d6e
GH
3740 case CHARDEV_BACKEND_KIND_SOCKET:
3741 chr = qmp_chardev_open_socket(backend->socket, errp);
3742 break;
3ecc059d
GH
3743 case CHARDEV_BACKEND_KIND_DGRAM:
3744 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3745 break;
0a1a7fab
GH
3746#ifdef HAVE_CHARDEV_TTY
3747 case CHARDEV_BACKEND_KIND_PTY:
e68c5958 3748 chr = qemu_chr_open_pty(id, ret);
0a1a7fab 3749 break;
0a1a7fab 3750#endif
f1a1a356 3751 case CHARDEV_BACKEND_KIND_NULL:
80dca9e6 3752 chr = qemu_chr_open_null();
f1a1a356 3753 break;
edb2fb3c
GH
3754 case CHARDEV_BACKEND_KIND_MUX:
3755 base = qemu_chr_find(backend->mux->chardev);
3756 if (base == NULL) {
3757 error_setg(errp, "mux: base chardev %s not found",
3758 backend->mux->chardev);
3759 break;
3760 }
3761 chr = qemu_chr_open_mux(base);
3762 break;
f5a51cab
GH
3763 case CHARDEV_BACKEND_KIND_MSMOUSE:
3764 chr = qemu_chr_open_msmouse();
3765 break;
2d57286d
GH
3766#ifdef CONFIG_BRLAPI
3767 case CHARDEV_BACKEND_KIND_BRAILLE:
3768 chr = chr_baum_init();
3769 break;
3770#endif
7c358031
GH
3771 case CHARDEV_BACKEND_KIND_STDIO:
3772 chr = qemu_chr_open_stdio(backend->stdio);
3773 break;
d9ac374f
GH
3774#ifdef _WIN32
3775 case CHARDEV_BACKEND_KIND_CONSOLE:
3776 chr = qemu_chr_open_win_con();
3777 break;
cd153e2a
GH
3778#endif
3779#ifdef CONFIG_SPICE
3780 case CHARDEV_BACKEND_KIND_SPICEVMC:
3781 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3782 break;
3783 case CHARDEV_BACKEND_KIND_SPICEPORT:
3784 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3785 break;
d9ac374f 3786#endif
702ec69c
GH
3787 case CHARDEV_BACKEND_KIND_VC:
3788 chr = vc_init(backend->vc);
3789 break;
1da48c65
GH
3790 case CHARDEV_BACKEND_KIND_MEMORY:
3791 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3792 break;
f1a1a356
GH
3793 default:
3794 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3795 break;
3796 }
3797
3798 if (chr == NULL && !error_is_set(errp)) {
3799 error_setg(errp, "Failed to create chardev");
3800 }
3801 if (chr) {
3802 chr->label = g_strdup(id);
edb2fb3c
GH
3803 chr->avail_connections =
3804 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
f1a1a356
GH
3805 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3806 return ret;
3807 } else {
3808 g_free(ret);
3809 return NULL;
3810 }
3811}
3812
3813void qmp_chardev_remove(const char *id, Error **errp)
3814{
3815 CharDriverState *chr;
3816
3817 chr = qemu_chr_find(id);
3818 if (NULL == chr) {
3819 error_setg(errp, "Chardev '%s' not found", id);
3820 return;
3821 }
3822 if (chr->chr_can_read || chr->chr_read ||
3823 chr->chr_event || chr->handler_opaque) {
3824 error_setg(errp, "Chardev '%s' is busy", id);
3825 return;
3826 }
3827 qemu_chr_delete(chr);
3828}
d654f34e
AL
3829
3830static void register_types(void)
3831{
80dca9e6 3832 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
d654f34e
AL
3833 register_char_driver("socket", qemu_chr_open_socket);
3834 register_char_driver("udp", qemu_chr_open_udp);
1da48c65
GH
3835 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3836 qemu_chr_parse_ringbuf);
846e2e49
GH
3837 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3838 qemu_chr_parse_file_out);
7c358031
GH
3839 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3840 qemu_chr_parse_stdio);
0f1cb51d
GH
3841 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3842 qemu_chr_parse_serial);
3843 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3844 qemu_chr_parse_serial);
dc375097
GH
3845 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3846 qemu_chr_parse_parallel);
3847 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3848 qemu_chr_parse_parallel);
e68c5958 3849 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
d9ac374f 3850 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
548cbb36
GH
3851 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3852 qemu_chr_parse_pipe);
d654f34e
AL
3853}
3854
3855type_init(register_types);