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