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