]> git.proxmox.com Git - qemu.git/blob - qemu-char.c
Fix some build issues for BSD.
[qemu.git] / qemu-char.c
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"
26 #include "console.h"
27 #include "sysemu.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "block.h"
31 #include "hw/usb.h"
32 #include "hw/baum.h"
33
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <time.h>
38 #include <errno.h>
39 #include <sys/time.h>
40 #include <zlib.h>
41
42 #ifndef _WIN32
43 #include <sys/times.h>
44 #include <sys/wait.h>
45 #include <termios.h>
46 #include <sys/mman.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <net/if.h>
52 #ifdef __NetBSD__
53 #include <net/if_tap.h>
54 #endif
55 #ifdef __linux__
56 #include <linux/if_tun.h>
57 #endif
58 #include <arpa/inet.h>
59 #include <dirent.h>
60 #include <netdb.h>
61 #include <sys/select.h>
62 #ifdef _BSD
63 #include <sys/stat.h>
64 #ifdef __FreeBSD__
65 #include <libutil.h>
66 #else
67 #include <util.h>
68 #endif
69 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
70 #include <freebsd/stdlib.h>
71 #else
72 #ifdef __linux__
73 #include <pty.h>
74
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
77 #endif
78 #ifdef __sun__
79 #include <sys/stat.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
89 #include <net/if.h>
90 #include <syslog.h>
91 #include <stropts.h>
92 #endif
93 #endif
94 #endif
95
96 #include "qemu_socket.h"
97
98 /***********************************************************/
99 /* character device */
100
101 static void qemu_chr_event(CharDriverState *s, int event)
102 {
103 if (!s->chr_event)
104 return;
105 s->chr_event(s->handler_opaque, event);
106 }
107
108 static void qemu_chr_reset_bh(void *opaque)
109 {
110 CharDriverState *s = opaque;
111 qemu_chr_event(s, CHR_EVENT_RESET);
112 qemu_bh_delete(s->bh);
113 s->bh = NULL;
114 }
115
116 void qemu_chr_reset(CharDriverState *s)
117 {
118 if (s->bh == NULL) {
119 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
120 qemu_bh_schedule(s->bh);
121 }
122 }
123
124 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
125 {
126 return s->chr_write(s, buf, len);
127 }
128
129 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
130 {
131 if (!s->chr_ioctl)
132 return -ENOTSUP;
133 return s->chr_ioctl(s, cmd, arg);
134 }
135
136 int qemu_chr_can_read(CharDriverState *s)
137 {
138 if (!s->chr_can_read)
139 return 0;
140 return s->chr_can_read(s->handler_opaque);
141 }
142
143 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
144 {
145 s->chr_read(s->handler_opaque, buf, len);
146 }
147
148 void qemu_chr_accept_input(CharDriverState *s)
149 {
150 if (s->chr_accept_input)
151 s->chr_accept_input(s);
152 }
153
154 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
155 {
156 char buf[4096];
157 va_list ap;
158 va_start(ap, fmt);
159 vsnprintf(buf, sizeof(buf), fmt, ap);
160 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
161 va_end(ap);
162 }
163
164 void qemu_chr_send_event(CharDriverState *s, int event)
165 {
166 if (s->chr_send_event)
167 s->chr_send_event(s, event);
168 }
169
170 void qemu_chr_add_handlers(CharDriverState *s,
171 IOCanRWHandler *fd_can_read,
172 IOReadHandler *fd_read,
173 IOEventHandler *fd_event,
174 void *opaque)
175 {
176 s->chr_can_read = fd_can_read;
177 s->chr_read = fd_read;
178 s->chr_event = fd_event;
179 s->handler_opaque = opaque;
180 if (s->chr_update_read_handler)
181 s->chr_update_read_handler(s);
182 }
183
184 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
185 {
186 return len;
187 }
188
189 static CharDriverState *qemu_chr_open_null(void)
190 {
191 CharDriverState *chr;
192
193 chr = qemu_mallocz(sizeof(CharDriverState));
194 if (!chr)
195 return NULL;
196 chr->chr_write = null_chr_write;
197 return chr;
198 }
199
200 /* MUX driver for serial I/O splitting */
201 static int term_timestamps;
202 static int64_t term_timestamps_start;
203 #define MAX_MUX 4
204 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
205 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
206 typedef struct {
207 IOCanRWHandler *chr_can_read[MAX_MUX];
208 IOReadHandler *chr_read[MAX_MUX];
209 IOEventHandler *chr_event[MAX_MUX];
210 void *ext_opaque[MAX_MUX];
211 CharDriverState *drv;
212 unsigned char buffer[MUX_BUFFER_SIZE];
213 int prod;
214 int cons;
215 int mux_cnt;
216 int term_got_escape;
217 int max_size;
218 } MuxDriver;
219
220
221 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
222 {
223 MuxDriver *d = chr->opaque;
224 int ret;
225 if (!term_timestamps) {
226 ret = d->drv->chr_write(d->drv, buf, len);
227 } else {
228 int i;
229
230 ret = 0;
231 for(i = 0; i < len; i++) {
232 ret += d->drv->chr_write(d->drv, buf+i, 1);
233 if (buf[i] == '\n') {
234 char buf1[64];
235 int64_t ti;
236 int secs;
237
238 ti = qemu_get_clock(rt_clock);
239 if (term_timestamps_start == -1)
240 term_timestamps_start = ti;
241 ti -= term_timestamps_start;
242 secs = ti / 1000000000;
243 snprintf(buf1, sizeof(buf1),
244 "[%02d:%02d:%02d.%03d] ",
245 secs / 3600,
246 (secs / 60) % 60,
247 secs % 60,
248 (int)((ti / 1000000) % 1000));
249 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
250 }
251 }
252 }
253 return ret;
254 }
255
256 static const char * const mux_help[] = {
257 "% h print this help\n\r",
258 "% x exit emulator\n\r",
259 "% s save disk data back to file (if -snapshot)\n\r",
260 "% t toggle console timestamps\n\r"
261 "% b send break (magic sysrq)\n\r",
262 "% c switch between console and monitor\n\r",
263 "% % sends %\n\r",
264 NULL
265 };
266
267 int term_escape_char = 0x01; /* ctrl-a is used for escape */
268 static void mux_print_help(CharDriverState *chr)
269 {
270 int i, j;
271 char ebuf[15] = "Escape-Char";
272 char cbuf[50] = "\n\r";
273
274 if (term_escape_char > 0 && term_escape_char < 26) {
275 snprintf(cbuf, sizeof(cbuf), "\n\r");
276 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
277 } else {
278 snprintf(cbuf, sizeof(cbuf),
279 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
280 term_escape_char);
281 }
282 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
283 for (i = 0; mux_help[i] != NULL; i++) {
284 for (j=0; mux_help[i][j] != '\0'; j++) {
285 if (mux_help[i][j] == '%')
286 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
287 else
288 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
289 }
290 }
291 }
292
293 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
294 {
295 if (d->term_got_escape) {
296 d->term_got_escape = 0;
297 if (ch == term_escape_char)
298 goto send_char;
299 switch(ch) {
300 case '?':
301 case 'h':
302 mux_print_help(chr);
303 break;
304 case 'x':
305 {
306 const char *term = "QEMU: Terminated\n\r";
307 chr->chr_write(chr,(uint8_t *)term,strlen(term));
308 exit(0);
309 break;
310 }
311 case 's':
312 {
313 int i;
314 for (i = 0; i < nb_drives; i++) {
315 bdrv_commit(drives_table[i].bdrv);
316 }
317 }
318 break;
319 case 'b':
320 qemu_chr_event(chr, CHR_EVENT_BREAK);
321 break;
322 case 'c':
323 /* Switch to the next registered device */
324 chr->focus++;
325 if (chr->focus >= d->mux_cnt)
326 chr->focus = 0;
327 break;
328 case 't':
329 term_timestamps = !term_timestamps;
330 term_timestamps_start = -1;
331 break;
332 }
333 } else if (ch == term_escape_char) {
334 d->term_got_escape = 1;
335 } else {
336 send_char:
337 return 1;
338 }
339 return 0;
340 }
341
342 static void mux_chr_accept_input(CharDriverState *chr)
343 {
344 int m = chr->focus;
345 MuxDriver *d = chr->opaque;
346
347 while (d->prod != d->cons &&
348 d->chr_can_read[m] &&
349 d->chr_can_read[m](d->ext_opaque[m])) {
350 d->chr_read[m](d->ext_opaque[m],
351 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
352 }
353 }
354
355 static int mux_chr_can_read(void *opaque)
356 {
357 CharDriverState *chr = opaque;
358 MuxDriver *d = chr->opaque;
359
360 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
361 return 1;
362 if (d->chr_can_read[chr->focus])
363 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
364 return 0;
365 }
366
367 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
368 {
369 CharDriverState *chr = opaque;
370 MuxDriver *d = chr->opaque;
371 int m = chr->focus;
372 int i;
373
374 mux_chr_accept_input (opaque);
375
376 for(i = 0; i < size; i++)
377 if (mux_proc_byte(chr, d, buf[i])) {
378 if (d->prod == d->cons &&
379 d->chr_can_read[m] &&
380 d->chr_can_read[m](d->ext_opaque[m]))
381 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
382 else
383 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
384 }
385 }
386
387 static void mux_chr_event(void *opaque, int event)
388 {
389 CharDriverState *chr = opaque;
390 MuxDriver *d = chr->opaque;
391 int i;
392
393 /* Send the event to all registered listeners */
394 for (i = 0; i < d->mux_cnt; i++)
395 if (d->chr_event[i])
396 d->chr_event[i](d->ext_opaque[i], event);
397 }
398
399 static void mux_chr_update_read_handler(CharDriverState *chr)
400 {
401 MuxDriver *d = chr->opaque;
402
403 if (d->mux_cnt >= MAX_MUX) {
404 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
405 return;
406 }
407 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
408 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
409 d->chr_read[d->mux_cnt] = chr->chr_read;
410 d->chr_event[d->mux_cnt] = chr->chr_event;
411 /* Fix up the real driver with mux routines */
412 if (d->mux_cnt == 0) {
413 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
414 mux_chr_event, chr);
415 }
416 chr->focus = d->mux_cnt;
417 d->mux_cnt++;
418 }
419
420 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
421 {
422 CharDriverState *chr;
423 MuxDriver *d;
424
425 chr = qemu_mallocz(sizeof(CharDriverState));
426 if (!chr)
427 return NULL;
428 d = qemu_mallocz(sizeof(MuxDriver));
429 if (!d) {
430 free(chr);
431 return NULL;
432 }
433
434 chr->opaque = d;
435 d->drv = drv;
436 chr->focus = -1;
437 chr->chr_write = mux_chr_write;
438 chr->chr_update_read_handler = mux_chr_update_read_handler;
439 chr->chr_accept_input = mux_chr_accept_input;
440 return chr;
441 }
442
443
444 #ifdef _WIN32
445 int send_all(int fd, const uint8_t *buf, int len1)
446 {
447 int ret, len;
448
449 len = len1;
450 while (len > 0) {
451 ret = send(fd, buf, len, 0);
452 if (ret < 0) {
453 int errno;
454 errno = WSAGetLastError();
455 if (errno != WSAEWOULDBLOCK) {
456 return -1;
457 }
458 } else if (ret == 0) {
459 break;
460 } else {
461 buf += ret;
462 len -= ret;
463 }
464 }
465 return len1 - len;
466 }
467
468 #else
469
470 static int unix_write(int fd, const uint8_t *buf, int len1)
471 {
472 int ret, len;
473
474 len = len1;
475 while (len > 0) {
476 ret = write(fd, buf, len);
477 if (ret < 0) {
478 if (errno != EINTR && errno != EAGAIN)
479 return -1;
480 } else if (ret == 0) {
481 break;
482 } else {
483 buf += ret;
484 len -= ret;
485 }
486 }
487 return len1 - len;
488 }
489
490 int send_all(int fd, const uint8_t *buf, int len1)
491 {
492 return unix_write(fd, buf, len1);
493 }
494 #endif /* !_WIN32 */
495
496 #ifndef _WIN32
497
498 typedef struct {
499 int fd_in, fd_out;
500 int max_size;
501 } FDCharDriver;
502
503 #define STDIO_MAX_CLIENTS 1
504 static int stdio_nb_clients = 0;
505
506 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
507 {
508 FDCharDriver *s = chr->opaque;
509 return send_all(s->fd_out, buf, len);
510 }
511
512 static int fd_chr_read_poll(void *opaque)
513 {
514 CharDriverState *chr = opaque;
515 FDCharDriver *s = chr->opaque;
516
517 s->max_size = qemu_chr_can_read(chr);
518 return s->max_size;
519 }
520
521 static void fd_chr_read(void *opaque)
522 {
523 CharDriverState *chr = opaque;
524 FDCharDriver *s = chr->opaque;
525 int size, len;
526 uint8_t buf[1024];
527
528 len = sizeof(buf);
529 if (len > s->max_size)
530 len = s->max_size;
531 if (len == 0)
532 return;
533 size = read(s->fd_in, buf, len);
534 if (size == 0) {
535 /* FD has been closed. Remove it from the active list. */
536 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
537 return;
538 }
539 if (size > 0) {
540 qemu_chr_read(chr, buf, size);
541 }
542 }
543
544 static void fd_chr_update_read_handler(CharDriverState *chr)
545 {
546 FDCharDriver *s = chr->opaque;
547
548 if (s->fd_in >= 0) {
549 if (nographic && s->fd_in == 0) {
550 } else {
551 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
552 fd_chr_read, NULL, chr);
553 }
554 }
555 }
556
557 static void fd_chr_close(struct CharDriverState *chr)
558 {
559 FDCharDriver *s = chr->opaque;
560
561 if (s->fd_in >= 0) {
562 if (nographic && s->fd_in == 0) {
563 } else {
564 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
565 }
566 }
567
568 qemu_free(s);
569 }
570
571 /* open a character device to a unix fd */
572 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
573 {
574 CharDriverState *chr;
575 FDCharDriver *s;
576
577 chr = qemu_mallocz(sizeof(CharDriverState));
578 if (!chr)
579 return NULL;
580 s = qemu_mallocz(sizeof(FDCharDriver));
581 if (!s) {
582 free(chr);
583 return NULL;
584 }
585 s->fd_in = fd_in;
586 s->fd_out = fd_out;
587 chr->opaque = s;
588 chr->chr_write = fd_chr_write;
589 chr->chr_update_read_handler = fd_chr_update_read_handler;
590 chr->chr_close = fd_chr_close;
591
592 qemu_chr_reset(chr);
593
594 return chr;
595 }
596
597 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
598 {
599 int fd_out;
600
601 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
602 if (fd_out < 0)
603 return NULL;
604 return qemu_chr_open_fd(-1, fd_out);
605 }
606
607 static CharDriverState *qemu_chr_open_pipe(const char *filename)
608 {
609 int fd_in, fd_out;
610 char filename_in[256], filename_out[256];
611
612 snprintf(filename_in, 256, "%s.in", filename);
613 snprintf(filename_out, 256, "%s.out", filename);
614 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
615 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
616 if (fd_in < 0 || fd_out < 0) {
617 if (fd_in >= 0)
618 close(fd_in);
619 if (fd_out >= 0)
620 close(fd_out);
621 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
622 if (fd_in < 0)
623 return NULL;
624 }
625 return qemu_chr_open_fd(fd_in, fd_out);
626 }
627
628
629 /* for STDIO, we handle the case where several clients use it
630 (nographic mode) */
631
632 #define TERM_FIFO_MAX_SIZE 1
633
634 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
635 static int term_fifo_size;
636
637 static int stdio_read_poll(void *opaque)
638 {
639 CharDriverState *chr = opaque;
640
641 /* try to flush the queue if needed */
642 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
643 qemu_chr_read(chr, term_fifo, 1);
644 term_fifo_size = 0;
645 }
646 /* see if we can absorb more chars */
647 if (term_fifo_size == 0)
648 return 1;
649 else
650 return 0;
651 }
652
653 static void stdio_read(void *opaque)
654 {
655 int size;
656 uint8_t buf[1];
657 CharDriverState *chr = opaque;
658
659 size = read(0, buf, 1);
660 if (size == 0) {
661 /* stdin has been closed. Remove it from the active list. */
662 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
663 return;
664 }
665 if (size > 0) {
666 if (qemu_chr_can_read(chr) > 0) {
667 qemu_chr_read(chr, buf, 1);
668 } else if (term_fifo_size == 0) {
669 term_fifo[term_fifo_size++] = buf[0];
670 }
671 }
672 }
673
674 /* init terminal so that we can grab keys */
675 static struct termios oldtty;
676 static int old_fd0_flags;
677 static int term_atexit_done;
678
679 static void term_exit(void)
680 {
681 tcsetattr (0, TCSANOW, &oldtty);
682 fcntl(0, F_SETFL, old_fd0_flags);
683 }
684
685 static void term_init(void)
686 {
687 struct termios tty;
688
689 tcgetattr (0, &tty);
690 oldtty = tty;
691 old_fd0_flags = fcntl(0, F_GETFL);
692
693 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
694 |INLCR|IGNCR|ICRNL|IXON);
695 tty.c_oflag |= OPOST;
696 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
697 /* if graphical mode, we allow Ctrl-C handling */
698 if (nographic)
699 tty.c_lflag &= ~ISIG;
700 tty.c_cflag &= ~(CSIZE|PARENB);
701 tty.c_cflag |= CS8;
702 tty.c_cc[VMIN] = 1;
703 tty.c_cc[VTIME] = 0;
704
705 tcsetattr (0, TCSANOW, &tty);
706
707 if (!term_atexit_done++)
708 atexit(term_exit);
709
710 fcntl(0, F_SETFL, O_NONBLOCK);
711 }
712
713 static void qemu_chr_close_stdio(struct CharDriverState *chr)
714 {
715 term_exit();
716 stdio_nb_clients--;
717 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
718 fd_chr_close(chr);
719 }
720
721 static CharDriverState *qemu_chr_open_stdio(void)
722 {
723 CharDriverState *chr;
724
725 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
726 return NULL;
727 chr = qemu_chr_open_fd(0, 1);
728 chr->chr_close = qemu_chr_close_stdio;
729 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
730 stdio_nb_clients++;
731 term_init();
732
733 return chr;
734 }
735
736 #ifdef __sun__
737 /* Once Solaris has openpty(), this is going to be removed. */
738 int openpty(int *amaster, int *aslave, char *name,
739 struct termios *termp, struct winsize *winp)
740 {
741 const char *slave;
742 int mfd = -1, sfd = -1;
743
744 *amaster = *aslave = -1;
745
746 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
747 if (mfd < 0)
748 goto err;
749
750 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
751 goto err;
752
753 if ((slave = ptsname(mfd)) == NULL)
754 goto err;
755
756 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
757 goto err;
758
759 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
760 (termp != NULL && tcgetattr(sfd, termp) < 0))
761 goto err;
762
763 if (amaster)
764 *amaster = mfd;
765 if (aslave)
766 *aslave = sfd;
767 if (winp)
768 ioctl(sfd, TIOCSWINSZ, winp);
769
770 return 0;
771
772 err:
773 if (sfd != -1)
774 close(sfd);
775 close(mfd);
776 return -1;
777 }
778
779 void cfmakeraw (struct termios *termios_p)
780 {
781 termios_p->c_iflag &=
782 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
783 termios_p->c_oflag &= ~OPOST;
784 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
785 termios_p->c_cflag &= ~(CSIZE|PARENB);
786 termios_p->c_cflag |= CS8;
787
788 termios_p->c_cc[VMIN] = 0;
789 termios_p->c_cc[VTIME] = 0;
790 }
791 #endif
792
793 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
794 || defined(__NetBSD__) || defined(__OpenBSD__)
795
796 typedef struct {
797 int fd;
798 int connected;
799 int polling;
800 int read_bytes;
801 QEMUTimer *timer;
802 } PtyCharDriver;
803
804 static void pty_chr_update_read_handler(CharDriverState *chr);
805 static void pty_chr_state(CharDriverState *chr, int connected);
806
807 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
808 {
809 PtyCharDriver *s = chr->opaque;
810
811 if (!s->connected) {
812 /* guest sends data, check for (re-)connect */
813 pty_chr_update_read_handler(chr);
814 return 0;
815 }
816 return send_all(s->fd, buf, len);
817 }
818
819 static int pty_chr_read_poll(void *opaque)
820 {
821 CharDriverState *chr = opaque;
822 PtyCharDriver *s = chr->opaque;
823
824 s->read_bytes = qemu_chr_can_read(chr);
825 return s->read_bytes;
826 }
827
828 static void pty_chr_read(void *opaque)
829 {
830 CharDriverState *chr = opaque;
831 PtyCharDriver *s = chr->opaque;
832 int size, len;
833 uint8_t buf[1024];
834
835 len = sizeof(buf);
836 if (len > s->read_bytes)
837 len = s->read_bytes;
838 if (len == 0)
839 return;
840 size = read(s->fd, buf, len);
841 if ((size == -1 && errno == EIO) ||
842 (size == 0)) {
843 pty_chr_state(chr, 0);
844 return;
845 }
846 if (size > 0) {
847 pty_chr_state(chr, 1);
848 qemu_chr_read(chr, buf, size);
849 }
850 }
851
852 static void pty_chr_update_read_handler(CharDriverState *chr)
853 {
854 PtyCharDriver *s = chr->opaque;
855
856 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
857 pty_chr_read, NULL, chr);
858 s->polling = 1;
859 /*
860 * Short timeout here: just need wait long enougth that qemu makes
861 * it through the poll loop once. When reconnected we want a
862 * short timeout so we notice it almost instantly. Otherwise
863 * read() gives us -EIO instantly, making pty_chr_state() reset the
864 * timeout to the normal (much longer) poll interval before the
865 * timer triggers.
866 */
867 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
868 }
869
870 static void pty_chr_state(CharDriverState *chr, int connected)
871 {
872 PtyCharDriver *s = chr->opaque;
873
874 if (!connected) {
875 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
876 s->connected = 0;
877 s->polling = 0;
878 /* (re-)connect poll interval for idle guests: once per second.
879 * We check more frequently in case the guests sends data to
880 * the virtual device linked to our pty. */
881 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
882 } else {
883 if (!s->connected)
884 qemu_chr_reset(chr);
885 s->connected = 1;
886 }
887 }
888
889 static void pty_chr_timer(void *opaque)
890 {
891 struct CharDriverState *chr = opaque;
892 PtyCharDriver *s = chr->opaque;
893
894 if (s->connected)
895 return;
896 if (s->polling) {
897 /* If we arrive here without polling being cleared due
898 * read returning -EIO, then we are (re-)connected */
899 pty_chr_state(chr, 1);
900 return;
901 }
902
903 /* Next poll ... */
904 pty_chr_update_read_handler(chr);
905 }
906
907 static void pty_chr_close(struct CharDriverState *chr)
908 {
909 PtyCharDriver *s = chr->opaque;
910
911 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
912 close(s->fd);
913 qemu_free(s);
914 }
915
916 static CharDriverState *qemu_chr_open_pty(void)
917 {
918 CharDriverState *chr;
919 PtyCharDriver *s;
920 struct termios tty;
921 int slave_fd, len;
922 #if defined(__OpenBSD__)
923 char pty_name[PATH_MAX];
924 #define q_ptsname(x) pty_name
925 #else
926 char *pty_name = NULL;
927 #define q_ptsname(x) ptsname(x)
928 #endif
929
930 chr = qemu_mallocz(sizeof(CharDriverState));
931 if (!chr)
932 return NULL;
933 s = qemu_mallocz(sizeof(PtyCharDriver));
934 if (!s) {
935 qemu_free(chr);
936 return NULL;
937 }
938
939 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
940 return NULL;
941 }
942
943 /* Set raw attributes on the pty. */
944 cfmakeraw(&tty);
945 tcsetattr(slave_fd, TCSAFLUSH, &tty);
946 close(slave_fd);
947
948 len = strlen(q_ptsname(s->fd)) + 5;
949 chr->filename = qemu_malloc(len);
950 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
951 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
952
953 chr->opaque = s;
954 chr->chr_write = pty_chr_write;
955 chr->chr_update_read_handler = pty_chr_update_read_handler;
956 chr->chr_close = pty_chr_close;
957
958 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
959
960 return chr;
961 }
962
963 static void tty_serial_init(int fd, int speed,
964 int parity, int data_bits, int stop_bits)
965 {
966 struct termios tty;
967 speed_t spd;
968
969 #if 0
970 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
971 speed, parity, data_bits, stop_bits);
972 #endif
973 tcgetattr (fd, &tty);
974
975 #define MARGIN 1.1
976 if (speed <= 50 * MARGIN)
977 spd = B50;
978 else if (speed <= 75 * MARGIN)
979 spd = B75;
980 else if (speed <= 300 * MARGIN)
981 spd = B300;
982 else if (speed <= 600 * MARGIN)
983 spd = B600;
984 else if (speed <= 1200 * MARGIN)
985 spd = B1200;
986 else if (speed <= 2400 * MARGIN)
987 spd = B2400;
988 else if (speed <= 4800 * MARGIN)
989 spd = B4800;
990 else if (speed <= 9600 * MARGIN)
991 spd = B9600;
992 else if (speed <= 19200 * MARGIN)
993 spd = B19200;
994 else if (speed <= 38400 * MARGIN)
995 spd = B38400;
996 else if (speed <= 57600 * MARGIN)
997 spd = B57600;
998 else if (speed <= 115200 * MARGIN)
999 spd = B115200;
1000 else
1001 spd = B115200;
1002
1003 cfsetispeed(&tty, spd);
1004 cfsetospeed(&tty, spd);
1005
1006 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1007 |INLCR|IGNCR|ICRNL|IXON);
1008 tty.c_oflag |= OPOST;
1009 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1010 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1011 switch(data_bits) {
1012 default:
1013 case 8:
1014 tty.c_cflag |= CS8;
1015 break;
1016 case 7:
1017 tty.c_cflag |= CS7;
1018 break;
1019 case 6:
1020 tty.c_cflag |= CS6;
1021 break;
1022 case 5:
1023 tty.c_cflag |= CS5;
1024 break;
1025 }
1026 switch(parity) {
1027 default:
1028 case 'N':
1029 break;
1030 case 'E':
1031 tty.c_cflag |= PARENB;
1032 break;
1033 case 'O':
1034 tty.c_cflag |= PARENB | PARODD;
1035 break;
1036 }
1037 if (stop_bits == 2)
1038 tty.c_cflag |= CSTOPB;
1039
1040 tcsetattr (fd, TCSANOW, &tty);
1041 }
1042
1043 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1044 {
1045 FDCharDriver *s = chr->opaque;
1046
1047 switch(cmd) {
1048 case CHR_IOCTL_SERIAL_SET_PARAMS:
1049 {
1050 QEMUSerialSetParams *ssp = arg;
1051 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1052 ssp->data_bits, ssp->stop_bits);
1053 }
1054 break;
1055 case CHR_IOCTL_SERIAL_SET_BREAK:
1056 {
1057 int enable = *(int *)arg;
1058 if (enable)
1059 tcsendbreak(s->fd_in, 1);
1060 }
1061 break;
1062 case CHR_IOCTL_SERIAL_GET_TIOCM:
1063 {
1064 int sarg = 0;
1065 int *targ = (int *)arg;
1066 ioctl(s->fd_in, TIOCMGET, &sarg);
1067 *targ = 0;
1068 if (sarg | TIOCM_CTS)
1069 *targ |= CHR_TIOCM_CTS;
1070 if (sarg | TIOCM_CAR)
1071 *targ |= CHR_TIOCM_CAR;
1072 if (sarg | TIOCM_DSR)
1073 *targ |= CHR_TIOCM_DSR;
1074 if (sarg | TIOCM_RI)
1075 *targ |= CHR_TIOCM_RI;
1076 if (sarg | TIOCM_DTR)
1077 *targ |= CHR_TIOCM_DTR;
1078 if (sarg | TIOCM_RTS)
1079 *targ |= CHR_TIOCM_RTS;
1080 }
1081 break;
1082 case CHR_IOCTL_SERIAL_SET_TIOCM:
1083 {
1084 int sarg = *(int *)arg;
1085 int targ = 0;
1086 if (sarg | CHR_TIOCM_DTR)
1087 targ |= TIOCM_DTR;
1088 if (sarg | CHR_TIOCM_RTS)
1089 targ |= TIOCM_RTS;
1090 ioctl(s->fd_in, TIOCMSET, &targ);
1091 }
1092 break;
1093 default:
1094 return -ENOTSUP;
1095 }
1096 return 0;
1097 }
1098
1099 static CharDriverState *qemu_chr_open_tty(const char *filename)
1100 {
1101 CharDriverState *chr;
1102 int fd;
1103
1104 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1105 tty_serial_init(fd, 115200, 'N', 8, 1);
1106 chr = qemu_chr_open_fd(fd, fd);
1107 if (!chr) {
1108 close(fd);
1109 return NULL;
1110 }
1111 chr->chr_ioctl = tty_serial_ioctl;
1112 qemu_chr_reset(chr);
1113 return chr;
1114 }
1115 #else /* ! __linux__ && ! __sun__ */
1116 static CharDriverState *qemu_chr_open_pty(void)
1117 {
1118 return NULL;
1119 }
1120 #endif /* __linux__ || __sun__ */
1121
1122 #if defined(__linux__)
1123 typedef struct {
1124 int fd;
1125 int mode;
1126 } ParallelCharDriver;
1127
1128 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1129 {
1130 if (s->mode != mode) {
1131 int m = mode;
1132 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1133 return 0;
1134 s->mode = mode;
1135 }
1136 return 1;
1137 }
1138
1139 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1140 {
1141 ParallelCharDriver *drv = chr->opaque;
1142 int fd = drv->fd;
1143 uint8_t b;
1144
1145 switch(cmd) {
1146 case CHR_IOCTL_PP_READ_DATA:
1147 if (ioctl(fd, PPRDATA, &b) < 0)
1148 return -ENOTSUP;
1149 *(uint8_t *)arg = b;
1150 break;
1151 case CHR_IOCTL_PP_WRITE_DATA:
1152 b = *(uint8_t *)arg;
1153 if (ioctl(fd, PPWDATA, &b) < 0)
1154 return -ENOTSUP;
1155 break;
1156 case CHR_IOCTL_PP_READ_CONTROL:
1157 if (ioctl(fd, PPRCONTROL, &b) < 0)
1158 return -ENOTSUP;
1159 /* Linux gives only the lowest bits, and no way to know data
1160 direction! For better compatibility set the fixed upper
1161 bits. */
1162 *(uint8_t *)arg = b | 0xc0;
1163 break;
1164 case CHR_IOCTL_PP_WRITE_CONTROL:
1165 b = *(uint8_t *)arg;
1166 if (ioctl(fd, PPWCONTROL, &b) < 0)
1167 return -ENOTSUP;
1168 break;
1169 case CHR_IOCTL_PP_READ_STATUS:
1170 if (ioctl(fd, PPRSTATUS, &b) < 0)
1171 return -ENOTSUP;
1172 *(uint8_t *)arg = b;
1173 break;
1174 case CHR_IOCTL_PP_DATA_DIR:
1175 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1176 return -ENOTSUP;
1177 break;
1178 case CHR_IOCTL_PP_EPP_READ_ADDR:
1179 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1180 struct ParallelIOArg *parg = arg;
1181 int n = read(fd, parg->buffer, parg->count);
1182 if (n != parg->count) {
1183 return -EIO;
1184 }
1185 }
1186 break;
1187 case CHR_IOCTL_PP_EPP_READ:
1188 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1189 struct ParallelIOArg *parg = arg;
1190 int n = read(fd, parg->buffer, parg->count);
1191 if (n != parg->count) {
1192 return -EIO;
1193 }
1194 }
1195 break;
1196 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1197 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1198 struct ParallelIOArg *parg = arg;
1199 int n = write(fd, parg->buffer, parg->count);
1200 if (n != parg->count) {
1201 return -EIO;
1202 }
1203 }
1204 break;
1205 case CHR_IOCTL_PP_EPP_WRITE:
1206 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1207 struct ParallelIOArg *parg = arg;
1208 int n = write(fd, parg->buffer, parg->count);
1209 if (n != parg->count) {
1210 return -EIO;
1211 }
1212 }
1213 break;
1214 default:
1215 return -ENOTSUP;
1216 }
1217 return 0;
1218 }
1219
1220 static void pp_close(CharDriverState *chr)
1221 {
1222 ParallelCharDriver *drv = chr->opaque;
1223 int fd = drv->fd;
1224
1225 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1226 ioctl(fd, PPRELEASE);
1227 close(fd);
1228 qemu_free(drv);
1229 }
1230
1231 static CharDriverState *qemu_chr_open_pp(const char *filename)
1232 {
1233 CharDriverState *chr;
1234 ParallelCharDriver *drv;
1235 int fd;
1236
1237 TFR(fd = open(filename, O_RDWR));
1238 if (fd < 0)
1239 return NULL;
1240
1241 if (ioctl(fd, PPCLAIM) < 0) {
1242 close(fd);
1243 return NULL;
1244 }
1245
1246 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1247 if (!drv) {
1248 close(fd);
1249 return NULL;
1250 }
1251 drv->fd = fd;
1252 drv->mode = IEEE1284_MODE_COMPAT;
1253
1254 chr = qemu_mallocz(sizeof(CharDriverState));
1255 if (!chr) {
1256 qemu_free(drv);
1257 close(fd);
1258 return NULL;
1259 }
1260 chr->chr_write = null_chr_write;
1261 chr->chr_ioctl = pp_ioctl;
1262 chr->chr_close = pp_close;
1263 chr->opaque = drv;
1264
1265 qemu_chr_reset(chr);
1266
1267 return chr;
1268 }
1269 #endif /* __linux__ */
1270
1271 #else /* _WIN32 */
1272
1273 typedef struct {
1274 int max_size;
1275 HANDLE hcom, hrecv, hsend;
1276 OVERLAPPED orecv, osend;
1277 BOOL fpipe;
1278 DWORD len;
1279 } WinCharState;
1280
1281 #define NSENDBUF 2048
1282 #define NRECVBUF 2048
1283 #define MAXCONNECT 1
1284 #define NTIMEOUT 5000
1285
1286 static int win_chr_poll(void *opaque);
1287 static int win_chr_pipe_poll(void *opaque);
1288
1289 static void win_chr_close(CharDriverState *chr)
1290 {
1291 WinCharState *s = chr->opaque;
1292
1293 if (s->hsend) {
1294 CloseHandle(s->hsend);
1295 s->hsend = NULL;
1296 }
1297 if (s->hrecv) {
1298 CloseHandle(s->hrecv);
1299 s->hrecv = NULL;
1300 }
1301 if (s->hcom) {
1302 CloseHandle(s->hcom);
1303 s->hcom = NULL;
1304 }
1305 if (s->fpipe)
1306 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1307 else
1308 qemu_del_polling_cb(win_chr_poll, chr);
1309 }
1310
1311 static int win_chr_init(CharDriverState *chr, const char *filename)
1312 {
1313 WinCharState *s = chr->opaque;
1314 COMMCONFIG comcfg;
1315 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1316 COMSTAT comstat;
1317 DWORD size;
1318 DWORD err;
1319
1320 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1321 if (!s->hsend) {
1322 fprintf(stderr, "Failed CreateEvent\n");
1323 goto fail;
1324 }
1325 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1326 if (!s->hrecv) {
1327 fprintf(stderr, "Failed CreateEvent\n");
1328 goto fail;
1329 }
1330
1331 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1332 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1333 if (s->hcom == INVALID_HANDLE_VALUE) {
1334 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1335 s->hcom = NULL;
1336 goto fail;
1337 }
1338
1339 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1340 fprintf(stderr, "Failed SetupComm\n");
1341 goto fail;
1342 }
1343
1344 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1345 size = sizeof(COMMCONFIG);
1346 GetDefaultCommConfig(filename, &comcfg, &size);
1347 comcfg.dcb.DCBlength = sizeof(DCB);
1348 CommConfigDialog(filename, NULL, &comcfg);
1349
1350 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1351 fprintf(stderr, "Failed SetCommState\n");
1352 goto fail;
1353 }
1354
1355 if (!SetCommMask(s->hcom, EV_ERR)) {
1356 fprintf(stderr, "Failed SetCommMask\n");
1357 goto fail;
1358 }
1359
1360 cto.ReadIntervalTimeout = MAXDWORD;
1361 if (!SetCommTimeouts(s->hcom, &cto)) {
1362 fprintf(stderr, "Failed SetCommTimeouts\n");
1363 goto fail;
1364 }
1365
1366 if (!ClearCommError(s->hcom, &err, &comstat)) {
1367 fprintf(stderr, "Failed ClearCommError\n");
1368 goto fail;
1369 }
1370 qemu_add_polling_cb(win_chr_poll, chr);
1371 return 0;
1372
1373 fail:
1374 win_chr_close(chr);
1375 return -1;
1376 }
1377
1378 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1379 {
1380 WinCharState *s = chr->opaque;
1381 DWORD len, ret, size, err;
1382
1383 len = len1;
1384 ZeroMemory(&s->osend, sizeof(s->osend));
1385 s->osend.hEvent = s->hsend;
1386 while (len > 0) {
1387 if (s->hsend)
1388 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1389 else
1390 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1391 if (!ret) {
1392 err = GetLastError();
1393 if (err == ERROR_IO_PENDING) {
1394 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1395 if (ret) {
1396 buf += size;
1397 len -= size;
1398 } else {
1399 break;
1400 }
1401 } else {
1402 break;
1403 }
1404 } else {
1405 buf += size;
1406 len -= size;
1407 }
1408 }
1409 return len1 - len;
1410 }
1411
1412 static int win_chr_read_poll(CharDriverState *chr)
1413 {
1414 WinCharState *s = chr->opaque;
1415
1416 s->max_size = qemu_chr_can_read(chr);
1417 return s->max_size;
1418 }
1419
1420 static void win_chr_readfile(CharDriverState *chr)
1421 {
1422 WinCharState *s = chr->opaque;
1423 int ret, err;
1424 uint8_t buf[1024];
1425 DWORD size;
1426
1427 ZeroMemory(&s->orecv, sizeof(s->orecv));
1428 s->orecv.hEvent = s->hrecv;
1429 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1430 if (!ret) {
1431 err = GetLastError();
1432 if (err == ERROR_IO_PENDING) {
1433 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1434 }
1435 }
1436
1437 if (size > 0) {
1438 qemu_chr_read(chr, buf, size);
1439 }
1440 }
1441
1442 static void win_chr_read(CharDriverState *chr)
1443 {
1444 WinCharState *s = chr->opaque;
1445
1446 if (s->len > s->max_size)
1447 s->len = s->max_size;
1448 if (s->len == 0)
1449 return;
1450
1451 win_chr_readfile(chr);
1452 }
1453
1454 static int win_chr_poll(void *opaque)
1455 {
1456 CharDriverState *chr = opaque;
1457 WinCharState *s = chr->opaque;
1458 COMSTAT status;
1459 DWORD comerr;
1460
1461 ClearCommError(s->hcom, &comerr, &status);
1462 if (status.cbInQue > 0) {
1463 s->len = status.cbInQue;
1464 win_chr_read_poll(chr);
1465 win_chr_read(chr);
1466 return 1;
1467 }
1468 return 0;
1469 }
1470
1471 static CharDriverState *qemu_chr_open_win(const char *filename)
1472 {
1473 CharDriverState *chr;
1474 WinCharState *s;
1475
1476 chr = qemu_mallocz(sizeof(CharDriverState));
1477 if (!chr)
1478 return NULL;
1479 s = qemu_mallocz(sizeof(WinCharState));
1480 if (!s) {
1481 free(chr);
1482 return NULL;
1483 }
1484 chr->opaque = s;
1485 chr->chr_write = win_chr_write;
1486 chr->chr_close = win_chr_close;
1487
1488 if (win_chr_init(chr, filename) < 0) {
1489 free(s);
1490 free(chr);
1491 return NULL;
1492 }
1493 qemu_chr_reset(chr);
1494 return chr;
1495 }
1496
1497 static int win_chr_pipe_poll(void *opaque)
1498 {
1499 CharDriverState *chr = opaque;
1500 WinCharState *s = chr->opaque;
1501 DWORD size;
1502
1503 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1504 if (size > 0) {
1505 s->len = size;
1506 win_chr_read_poll(chr);
1507 win_chr_read(chr);
1508 return 1;
1509 }
1510 return 0;
1511 }
1512
1513 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1514 {
1515 WinCharState *s = chr->opaque;
1516 OVERLAPPED ov;
1517 int ret;
1518 DWORD size;
1519 char openname[256];
1520
1521 s->fpipe = TRUE;
1522
1523 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1524 if (!s->hsend) {
1525 fprintf(stderr, "Failed CreateEvent\n");
1526 goto fail;
1527 }
1528 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1529 if (!s->hrecv) {
1530 fprintf(stderr, "Failed CreateEvent\n");
1531 goto fail;
1532 }
1533
1534 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1535 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1536 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1537 PIPE_WAIT,
1538 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1539 if (s->hcom == INVALID_HANDLE_VALUE) {
1540 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1541 s->hcom = NULL;
1542 goto fail;
1543 }
1544
1545 ZeroMemory(&ov, sizeof(ov));
1546 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1547 ret = ConnectNamedPipe(s->hcom, &ov);
1548 if (ret) {
1549 fprintf(stderr, "Failed ConnectNamedPipe\n");
1550 goto fail;
1551 }
1552
1553 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1554 if (!ret) {
1555 fprintf(stderr, "Failed GetOverlappedResult\n");
1556 if (ov.hEvent) {
1557 CloseHandle(ov.hEvent);
1558 ov.hEvent = NULL;
1559 }
1560 goto fail;
1561 }
1562
1563 if (ov.hEvent) {
1564 CloseHandle(ov.hEvent);
1565 ov.hEvent = NULL;
1566 }
1567 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1568 return 0;
1569
1570 fail:
1571 win_chr_close(chr);
1572 return -1;
1573 }
1574
1575
1576 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1577 {
1578 CharDriverState *chr;
1579 WinCharState *s;
1580
1581 chr = qemu_mallocz(sizeof(CharDriverState));
1582 if (!chr)
1583 return NULL;
1584 s = qemu_mallocz(sizeof(WinCharState));
1585 if (!s) {
1586 free(chr);
1587 return NULL;
1588 }
1589 chr->opaque = s;
1590 chr->chr_write = win_chr_write;
1591 chr->chr_close = win_chr_close;
1592
1593 if (win_chr_pipe_init(chr, filename) < 0) {
1594 free(s);
1595 free(chr);
1596 return NULL;
1597 }
1598 qemu_chr_reset(chr);
1599 return chr;
1600 }
1601
1602 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1603 {
1604 CharDriverState *chr;
1605 WinCharState *s;
1606
1607 chr = qemu_mallocz(sizeof(CharDriverState));
1608 if (!chr)
1609 return NULL;
1610 s = qemu_mallocz(sizeof(WinCharState));
1611 if (!s) {
1612 free(chr);
1613 return NULL;
1614 }
1615 s->hcom = fd_out;
1616 chr->opaque = s;
1617 chr->chr_write = win_chr_write;
1618 qemu_chr_reset(chr);
1619 return chr;
1620 }
1621
1622 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1623 {
1624 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1625 }
1626
1627 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1628 {
1629 HANDLE fd_out;
1630
1631 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1632 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1633 if (fd_out == INVALID_HANDLE_VALUE)
1634 return NULL;
1635
1636 return qemu_chr_open_win_file(fd_out);
1637 }
1638 #endif /* !_WIN32 */
1639
1640 /***********************************************************/
1641 /* UDP Net console */
1642
1643 typedef struct {
1644 int fd;
1645 struct sockaddr_in daddr;
1646 uint8_t buf[1024];
1647 int bufcnt;
1648 int bufptr;
1649 int max_size;
1650 } NetCharDriver;
1651
1652 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1653 {
1654 NetCharDriver *s = chr->opaque;
1655
1656 return sendto(s->fd, buf, len, 0,
1657 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1658 }
1659
1660 static int udp_chr_read_poll(void *opaque)
1661 {
1662 CharDriverState *chr = opaque;
1663 NetCharDriver *s = chr->opaque;
1664
1665 s->max_size = qemu_chr_can_read(chr);
1666
1667 /* If there were any stray characters in the queue process them
1668 * first
1669 */
1670 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1671 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1672 s->bufptr++;
1673 s->max_size = qemu_chr_can_read(chr);
1674 }
1675 return s->max_size;
1676 }
1677
1678 static void udp_chr_read(void *opaque)
1679 {
1680 CharDriverState *chr = opaque;
1681 NetCharDriver *s = chr->opaque;
1682
1683 if (s->max_size == 0)
1684 return;
1685 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1686 s->bufptr = s->bufcnt;
1687 if (s->bufcnt <= 0)
1688 return;
1689
1690 s->bufptr = 0;
1691 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1692 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1693 s->bufptr++;
1694 s->max_size = qemu_chr_can_read(chr);
1695 }
1696 }
1697
1698 static void udp_chr_update_read_handler(CharDriverState *chr)
1699 {
1700 NetCharDriver *s = chr->opaque;
1701
1702 if (s->fd >= 0) {
1703 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1704 udp_chr_read, NULL, chr);
1705 }
1706 }
1707
1708 static CharDriverState *qemu_chr_open_udp(const char *def)
1709 {
1710 CharDriverState *chr = NULL;
1711 NetCharDriver *s = NULL;
1712 int fd = -1;
1713 struct sockaddr_in saddr;
1714
1715 chr = qemu_mallocz(sizeof(CharDriverState));
1716 if (!chr)
1717 goto return_err;
1718 s = qemu_mallocz(sizeof(NetCharDriver));
1719 if (!s)
1720 goto return_err;
1721
1722 fd = socket(PF_INET, SOCK_DGRAM, 0);
1723 if (fd < 0) {
1724 perror("socket(PF_INET, SOCK_DGRAM)");
1725 goto return_err;
1726 }
1727
1728 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1729 printf("Could not parse: %s\n", def);
1730 goto return_err;
1731 }
1732
1733 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1734 {
1735 perror("bind");
1736 goto return_err;
1737 }
1738
1739 s->fd = fd;
1740 s->bufcnt = 0;
1741 s->bufptr = 0;
1742 chr->opaque = s;
1743 chr->chr_write = udp_chr_write;
1744 chr->chr_update_read_handler = udp_chr_update_read_handler;
1745 return chr;
1746
1747 return_err:
1748 if (chr)
1749 free(chr);
1750 if (s)
1751 free(s);
1752 if (fd >= 0)
1753 closesocket(fd);
1754 return NULL;
1755 }
1756
1757 /***********************************************************/
1758 /* TCP Net console */
1759
1760 typedef struct {
1761 int fd, listen_fd;
1762 int connected;
1763 int max_size;
1764 int do_telnetopt;
1765 int do_nodelay;
1766 int is_unix;
1767 } TCPCharDriver;
1768
1769 static void tcp_chr_accept(void *opaque);
1770
1771 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1772 {
1773 TCPCharDriver *s = chr->opaque;
1774 if (s->connected) {
1775 return send_all(s->fd, buf, len);
1776 } else {
1777 /* XXX: indicate an error ? */
1778 return len;
1779 }
1780 }
1781
1782 static int tcp_chr_read_poll(void *opaque)
1783 {
1784 CharDriverState *chr = opaque;
1785 TCPCharDriver *s = chr->opaque;
1786 if (!s->connected)
1787 return 0;
1788 s->max_size = qemu_chr_can_read(chr);
1789 return s->max_size;
1790 }
1791
1792 #define IAC 255
1793 #define IAC_BREAK 243
1794 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1795 TCPCharDriver *s,
1796 uint8_t *buf, int *size)
1797 {
1798 /* Handle any telnet client's basic IAC options to satisfy char by
1799 * char mode with no echo. All IAC options will be removed from
1800 * the buf and the do_telnetopt variable will be used to track the
1801 * state of the width of the IAC information.
1802 *
1803 * IAC commands come in sets of 3 bytes with the exception of the
1804 * "IAC BREAK" command and the double IAC.
1805 */
1806
1807 int i;
1808 int j = 0;
1809
1810 for (i = 0; i < *size; i++) {
1811 if (s->do_telnetopt > 1) {
1812 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1813 /* Double IAC means send an IAC */
1814 if (j != i)
1815 buf[j] = buf[i];
1816 j++;
1817 s->do_telnetopt = 1;
1818 } else {
1819 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1820 /* Handle IAC break commands by sending a serial break */
1821 qemu_chr_event(chr, CHR_EVENT_BREAK);
1822 s->do_telnetopt++;
1823 }
1824 s->do_telnetopt++;
1825 }
1826 if (s->do_telnetopt >= 4) {
1827 s->do_telnetopt = 1;
1828 }
1829 } else {
1830 if ((unsigned char)buf[i] == IAC) {
1831 s->do_telnetopt = 2;
1832 } else {
1833 if (j != i)
1834 buf[j] = buf[i];
1835 j++;
1836 }
1837 }
1838 }
1839 *size = j;
1840 }
1841
1842 static void tcp_chr_read(void *opaque)
1843 {
1844 CharDriverState *chr = opaque;
1845 TCPCharDriver *s = chr->opaque;
1846 uint8_t buf[1024];
1847 int len, size;
1848
1849 if (!s->connected || s->max_size <= 0)
1850 return;
1851 len = sizeof(buf);
1852 if (len > s->max_size)
1853 len = s->max_size;
1854 size = recv(s->fd, buf, len, 0);
1855 if (size == 0) {
1856 /* connection closed */
1857 s->connected = 0;
1858 if (s->listen_fd >= 0) {
1859 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1860 }
1861 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1862 closesocket(s->fd);
1863 s->fd = -1;
1864 } else if (size > 0) {
1865 if (s->do_telnetopt)
1866 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1867 if (size > 0)
1868 qemu_chr_read(chr, buf, size);
1869 }
1870 }
1871
1872 static void tcp_chr_connect(void *opaque)
1873 {
1874 CharDriverState *chr = opaque;
1875 TCPCharDriver *s = chr->opaque;
1876
1877 s->connected = 1;
1878 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1879 tcp_chr_read, NULL, chr);
1880 qemu_chr_reset(chr);
1881 }
1882
1883 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1884 static void tcp_chr_telnet_init(int fd)
1885 {
1886 char buf[3];
1887 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1888 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1889 send(fd, (char *)buf, 3, 0);
1890 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1891 send(fd, (char *)buf, 3, 0);
1892 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1893 send(fd, (char *)buf, 3, 0);
1894 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1895 send(fd, (char *)buf, 3, 0);
1896 }
1897
1898 static void socket_set_nodelay(int fd)
1899 {
1900 int val = 1;
1901 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1902 }
1903
1904 static void tcp_chr_accept(void *opaque)
1905 {
1906 CharDriverState *chr = opaque;
1907 TCPCharDriver *s = chr->opaque;
1908 struct sockaddr_in saddr;
1909 #ifndef _WIN32
1910 struct sockaddr_un uaddr;
1911 #endif
1912 struct sockaddr *addr;
1913 socklen_t len;
1914 int fd;
1915
1916 for(;;) {
1917 #ifndef _WIN32
1918 if (s->is_unix) {
1919 len = sizeof(uaddr);
1920 addr = (struct sockaddr *)&uaddr;
1921 } else
1922 #endif
1923 {
1924 len = sizeof(saddr);
1925 addr = (struct sockaddr *)&saddr;
1926 }
1927 fd = accept(s->listen_fd, addr, &len);
1928 if (fd < 0 && errno != EINTR) {
1929 return;
1930 } else if (fd >= 0) {
1931 if (s->do_telnetopt)
1932 tcp_chr_telnet_init(fd);
1933 break;
1934 }
1935 }
1936 socket_set_nonblock(fd);
1937 if (s->do_nodelay)
1938 socket_set_nodelay(fd);
1939 s->fd = fd;
1940 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
1941 tcp_chr_connect(chr);
1942 }
1943
1944 static void tcp_chr_close(CharDriverState *chr)
1945 {
1946 TCPCharDriver *s = chr->opaque;
1947 if (s->fd >= 0)
1948 closesocket(s->fd);
1949 if (s->listen_fd >= 0)
1950 closesocket(s->listen_fd);
1951 qemu_free(s);
1952 }
1953
1954 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
1955 int is_telnet,
1956 int is_unix)
1957 {
1958 CharDriverState *chr = NULL;
1959 TCPCharDriver *s = NULL;
1960 int fd = -1, ret, err, val;
1961 int is_listen = 0;
1962 int is_waitconnect = 1;
1963 int do_nodelay = 0;
1964 const char *ptr;
1965 struct sockaddr_in saddr;
1966 #ifndef _WIN32
1967 struct sockaddr_un uaddr;
1968 #endif
1969 struct sockaddr *addr;
1970 socklen_t addrlen;
1971
1972 #ifndef _WIN32
1973 if (is_unix) {
1974 addr = (struct sockaddr *)&uaddr;
1975 addrlen = sizeof(uaddr);
1976 if (parse_unix_path(&uaddr, host_str) < 0)
1977 goto fail;
1978 } else
1979 #endif
1980 {
1981 addr = (struct sockaddr *)&saddr;
1982 addrlen = sizeof(saddr);
1983 if (parse_host_port(&saddr, host_str) < 0)
1984 goto fail;
1985 }
1986
1987 ptr = host_str;
1988 while((ptr = strchr(ptr,','))) {
1989 ptr++;
1990 if (!strncmp(ptr,"server",6)) {
1991 is_listen = 1;
1992 } else if (!strncmp(ptr,"nowait",6)) {
1993 is_waitconnect = 0;
1994 } else if (!strncmp(ptr,"nodelay",6)) {
1995 do_nodelay = 1;
1996 } else {
1997 printf("Unknown option: %s\n", ptr);
1998 goto fail;
1999 }
2000 }
2001 if (!is_listen)
2002 is_waitconnect = 0;
2003
2004 chr = qemu_mallocz(sizeof(CharDriverState));
2005 if (!chr)
2006 goto fail;
2007 s = qemu_mallocz(sizeof(TCPCharDriver));
2008 if (!s)
2009 goto fail;
2010
2011 #ifndef _WIN32
2012 if (is_unix)
2013 fd = socket(PF_UNIX, SOCK_STREAM, 0);
2014 else
2015 #endif
2016 fd = socket(PF_INET, SOCK_STREAM, 0);
2017
2018 if (fd < 0)
2019 goto fail;
2020
2021 if (!is_waitconnect)
2022 socket_set_nonblock(fd);
2023
2024 s->connected = 0;
2025 s->fd = -1;
2026 s->listen_fd = -1;
2027 s->is_unix = is_unix;
2028 s->do_nodelay = do_nodelay && !is_unix;
2029
2030 chr->opaque = s;
2031 chr->chr_write = tcp_chr_write;
2032 chr->chr_close = tcp_chr_close;
2033
2034 if (is_listen) {
2035 /* allow fast reuse */
2036 #ifndef _WIN32
2037 if (is_unix) {
2038 char path[109];
2039 pstrcpy(path, sizeof(path), uaddr.sun_path);
2040 unlink(path);
2041 } else
2042 #endif
2043 {
2044 val = 1;
2045 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
2046 }
2047
2048 ret = bind(fd, addr, addrlen);
2049 if (ret < 0)
2050 goto fail;
2051
2052 ret = listen(fd, 0);
2053 if (ret < 0)
2054 goto fail;
2055
2056 s->listen_fd = fd;
2057 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2058 if (is_telnet)
2059 s->do_telnetopt = 1;
2060 } else {
2061 for(;;) {
2062 ret = connect(fd, addr, addrlen);
2063 if (ret < 0) {
2064 err = socket_error();
2065 if (err == EINTR || err == EWOULDBLOCK) {
2066 } else if (err == EINPROGRESS) {
2067 break;
2068 #ifdef _WIN32
2069 } else if (err == WSAEALREADY) {
2070 break;
2071 #endif
2072 } else {
2073 goto fail;
2074 }
2075 } else {
2076 s->connected = 1;
2077 break;
2078 }
2079 }
2080 s->fd = fd;
2081 socket_set_nodelay(fd);
2082 if (s->connected)
2083 tcp_chr_connect(chr);
2084 else
2085 qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
2086 }
2087
2088 if (is_listen && is_waitconnect) {
2089 printf("QEMU waiting for connection on: %s\n", host_str);
2090 tcp_chr_accept(chr);
2091 socket_set_nonblock(s->listen_fd);
2092 }
2093
2094 return chr;
2095 fail:
2096 if (fd >= 0)
2097 closesocket(fd);
2098 qemu_free(s);
2099 qemu_free(chr);
2100 return NULL;
2101 }
2102
2103 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
2104 = TAILQ_HEAD_INITIALIZER(chardevs);
2105
2106 CharDriverState *qemu_chr_open(const char *label, const char *filename)
2107 {
2108 const char *p;
2109 CharDriverState *chr;
2110
2111 if (!strcmp(filename, "vc")) {
2112 chr = text_console_init(&display_state, 0);
2113 } else
2114 if (strstart(filename, "vc:", &p)) {
2115 chr = text_console_init(&display_state, p);
2116 } else
2117 if (!strcmp(filename, "null")) {
2118 chr = qemu_chr_open_null();
2119 } else
2120 if (strstart(filename, "tcp:", &p)) {
2121 chr = qemu_chr_open_tcp(p, 0, 0);
2122 } else
2123 if (strstart(filename, "telnet:", &p)) {
2124 chr = qemu_chr_open_tcp(p, 1, 0);
2125 } else
2126 if (strstart(filename, "udp:", &p)) {
2127 chr = qemu_chr_open_udp(p);
2128 } else
2129 if (strstart(filename, "mon:", &p)) {
2130 chr = qemu_chr_open(label, p);
2131 if (chr) {
2132 chr = qemu_chr_open_mux(chr);
2133 monitor_init(chr, !nographic);
2134 } else {
2135 printf("Unable to open driver: %s\n", p);
2136 }
2137 } else
2138 #ifndef _WIN32
2139 if (strstart(filename, "unix:", &p)) {
2140 chr = qemu_chr_open_tcp(p, 0, 1);
2141 } else if (strstart(filename, "file:", &p)) {
2142 chr = qemu_chr_open_file_out(p);
2143 } else if (strstart(filename, "pipe:", &p)) {
2144 chr = qemu_chr_open_pipe(p);
2145 } else if (!strcmp(filename, "pty")) {
2146 chr = qemu_chr_open_pty();
2147 } else if (!strcmp(filename, "stdio")) {
2148 chr = qemu_chr_open_stdio();
2149 } else
2150 #if defined(__linux__)
2151 if (strstart(filename, "/dev/parport", NULL)) {
2152 chr = qemu_chr_open_pp(filename);
2153 } else
2154 #endif
2155 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2156 || defined(__NetBSD__) || defined(__OpenBSD__)
2157 if (strstart(filename, "/dev/", NULL)) {
2158 chr = qemu_chr_open_tty(filename);
2159 } else
2160 #endif
2161 #else /* !_WIN32 */
2162 if (strstart(filename, "COM", NULL)) {
2163 chr = qemu_chr_open_win(filename);
2164 } else
2165 if (strstart(filename, "pipe:", &p)) {
2166 chr = qemu_chr_open_win_pipe(p);
2167 } else
2168 if (strstart(filename, "con:", NULL)) {
2169 chr = qemu_chr_open_win_con(filename);
2170 } else
2171 if (strstart(filename, "file:", &p)) {
2172 chr = qemu_chr_open_win_file_out(p);
2173 } else
2174 #endif
2175 #ifdef CONFIG_BRLAPI
2176 if (!strcmp(filename, "braille")) {
2177 chr = chr_baum_init();
2178 } else
2179 #endif
2180 {
2181 chr = NULL;
2182 }
2183
2184 if (chr) {
2185 if (!chr->filename)
2186 chr->filename = qemu_strdup(filename);
2187 chr->label = qemu_strdup(label);
2188 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2189 }
2190 return chr;
2191 }
2192
2193 void qemu_chr_close(CharDriverState *chr)
2194 {
2195 TAILQ_REMOVE(&chardevs, chr, next);
2196 if (chr->chr_close)
2197 chr->chr_close(chr);
2198 qemu_free(chr->filename);
2199 qemu_free(chr->label);
2200 qemu_free(chr);
2201 }
2202
2203 void qemu_chr_info(void)
2204 {
2205 CharDriverState *chr;
2206
2207 TAILQ_FOREACH(chr, &chardevs, next) {
2208 term_printf("%s: filename=%s\n", chr->label, chr->filename);
2209 }
2210 }