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