Fix interrupt exclusion via SSTEP_NOIRQ
[qemu/mini2440.git] / qemu-char.c
blobceffbed655f4116e8fe4fd71ce4722ecd0fb285e
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
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>
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/socket.h>
49 #include <netinet/in.h>
50 #include <dirent.h>
51 #include <netdb.h>
52 #include <sys/select.h>
53 #include <arpa/inet.h>
54 #ifdef _BSD
55 #include <sys/stat.h>
56 #if !defined(__APPLE__) && !defined(__OpenBSD__)
57 #include <libutil.h>
58 #endif
59 #ifdef __OpenBSD__
60 #include <net/if.h>
61 #endif
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
64 #else
65 #ifdef __linux__
66 #include <linux/if.h>
67 #include <pty.h>
69 #include <linux/ppdev.h>
70 #include <linux/parport.h>
71 #endif
72 #ifdef __sun__
73 #include <sys/stat.h>
74 #include <sys/ethernet.h>
75 #include <sys/sockio.h>
76 #include <netinet/arp.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_icmp.h> // must come after ip.h
81 #include <netinet/udp.h>
82 #include <netinet/tcp.h>
83 #include <net/if.h>
84 #include <syslog.h>
85 #include <stropts.h>
86 #endif
87 #endif
88 #endif
90 #include "qemu_socket.h"
92 /***********************************************************/
93 /* character device */
95 static void qemu_chr_event(CharDriverState *s, int event)
97 if (!s->chr_event)
98 return;
99 s->chr_event(s->handler_opaque, event);
102 static void qemu_chr_reset_bh(void *opaque)
104 CharDriverState *s = opaque;
105 qemu_chr_event(s, CHR_EVENT_RESET);
106 qemu_bh_delete(s->bh);
107 s->bh = NULL;
110 void qemu_chr_reset(CharDriverState *s)
112 if (s->bh == NULL) {
113 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
114 qemu_bh_schedule(s->bh);
118 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
120 return s->chr_write(s, buf, len);
123 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
125 if (!s->chr_ioctl)
126 return -ENOTSUP;
127 return s->chr_ioctl(s, cmd, arg);
130 int qemu_chr_can_read(CharDriverState *s)
132 if (!s->chr_can_read)
133 return 0;
134 return s->chr_can_read(s->handler_opaque);
137 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
139 s->chr_read(s->handler_opaque, buf, len);
142 void qemu_chr_accept_input(CharDriverState *s)
144 if (s->chr_accept_input)
145 s->chr_accept_input(s);
148 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
150 char buf[4096];
151 va_list ap;
152 va_start(ap, fmt);
153 vsnprintf(buf, sizeof(buf), fmt, ap);
154 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
155 va_end(ap);
158 void qemu_chr_send_event(CharDriverState *s, int event)
160 if (s->chr_send_event)
161 s->chr_send_event(s, event);
164 void qemu_chr_add_handlers(CharDriverState *s,
165 IOCanRWHandler *fd_can_read,
166 IOReadHandler *fd_read,
167 IOEventHandler *fd_event,
168 void *opaque)
170 s->chr_can_read = fd_can_read;
171 s->chr_read = fd_read;
172 s->chr_event = fd_event;
173 s->handler_opaque = opaque;
174 if (s->chr_update_read_handler)
175 s->chr_update_read_handler(s);
178 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
180 return len;
183 static CharDriverState *qemu_chr_open_null(void)
185 CharDriverState *chr;
187 chr = qemu_mallocz(sizeof(CharDriverState));
188 if (!chr)
189 return NULL;
190 chr->chr_write = null_chr_write;
191 return chr;
194 /* MUX driver for serial I/O splitting */
195 static int term_timestamps;
196 static int64_t term_timestamps_start;
197 #define MAX_MUX 4
198 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
199 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
200 typedef struct {
201 IOCanRWHandler *chr_can_read[MAX_MUX];
202 IOReadHandler *chr_read[MAX_MUX];
203 IOEventHandler *chr_event[MAX_MUX];
204 void *ext_opaque[MAX_MUX];
205 CharDriverState *drv;
206 unsigned char buffer[MUX_BUFFER_SIZE];
207 int prod;
208 int cons;
209 int mux_cnt;
210 int term_got_escape;
211 int max_size;
212 } MuxDriver;
215 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
217 MuxDriver *d = chr->opaque;
218 int ret;
219 if (!term_timestamps) {
220 ret = d->drv->chr_write(d->drv, buf, len);
221 } else {
222 int i;
224 ret = 0;
225 for(i = 0; i < len; i++) {
226 ret += d->drv->chr_write(d->drv, buf+i, 1);
227 if (buf[i] == '\n') {
228 char buf1[64];
229 int64_t ti;
230 int secs;
232 ti = qemu_get_clock(rt_clock);
233 if (term_timestamps_start == -1)
234 term_timestamps_start = ti;
235 ti -= term_timestamps_start;
236 secs = ti / 1000000000;
237 snprintf(buf1, sizeof(buf1),
238 "[%02d:%02d:%02d.%03d] ",
239 secs / 3600,
240 (secs / 60) % 60,
241 secs % 60,
242 (int)((ti / 1000000) % 1000));
243 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
247 return ret;
250 static const char * const mux_help[] = {
251 "% h print this help\n\r",
252 "% x exit emulator\n\r",
253 "% s save disk data back to file (if -snapshot)\n\r",
254 "% t toggle console timestamps\n\r"
255 "% b send break (magic sysrq)\n\r",
256 "% c switch between console and monitor\n\r",
257 "% % sends %\n\r",
258 NULL
261 int term_escape_char = 0x01; /* ctrl-a is used for escape */
262 static void mux_print_help(CharDriverState *chr)
264 int i, j;
265 char ebuf[15] = "Escape-Char";
266 char cbuf[50] = "\n\r";
268 if (term_escape_char > 0 && term_escape_char < 26) {
269 snprintf(cbuf, sizeof(cbuf), "\n\r");
270 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
271 } else {
272 snprintf(cbuf, sizeof(cbuf),
273 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
274 term_escape_char);
276 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
277 for (i = 0; mux_help[i] != NULL; i++) {
278 for (j=0; mux_help[i][j] != '\0'; j++) {
279 if (mux_help[i][j] == '%')
280 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
281 else
282 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
287 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
289 if (d->term_got_escape) {
290 d->term_got_escape = 0;
291 if (ch == term_escape_char)
292 goto send_char;
293 switch(ch) {
294 case '?':
295 case 'h':
296 mux_print_help(chr);
297 break;
298 case 'x':
300 const char *term = "QEMU: Terminated\n\r";
301 chr->chr_write(chr,(uint8_t *)term,strlen(term));
302 exit(0);
303 break;
305 case 's':
307 int i;
308 for (i = 0; i < nb_drives; i++) {
309 bdrv_commit(drives_table[i].bdrv);
312 break;
313 case 'b':
314 qemu_chr_event(chr, CHR_EVENT_BREAK);
315 break;
316 case 'c':
317 /* Switch to the next registered device */
318 chr->focus++;
319 if (chr->focus >= d->mux_cnt)
320 chr->focus = 0;
321 break;
322 case 't':
323 term_timestamps = !term_timestamps;
324 term_timestamps_start = -1;
325 break;
327 } else if (ch == term_escape_char) {
328 d->term_got_escape = 1;
329 } else {
330 send_char:
331 return 1;
333 return 0;
336 static void mux_chr_accept_input(CharDriverState *chr)
338 int m = chr->focus;
339 MuxDriver *d = chr->opaque;
341 while (d->prod != d->cons &&
342 d->chr_can_read[m] &&
343 d->chr_can_read[m](d->ext_opaque[m])) {
344 d->chr_read[m](d->ext_opaque[m],
345 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
349 static int mux_chr_can_read(void *opaque)
351 CharDriverState *chr = opaque;
352 MuxDriver *d = chr->opaque;
354 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
355 return 1;
356 if (d->chr_can_read[chr->focus])
357 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
358 return 0;
361 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
363 CharDriverState *chr = opaque;
364 MuxDriver *d = chr->opaque;
365 int m = chr->focus;
366 int i;
368 mux_chr_accept_input (opaque);
370 for(i = 0; i < size; i++)
371 if (mux_proc_byte(chr, d, buf[i])) {
372 if (d->prod == d->cons &&
373 d->chr_can_read[m] &&
374 d->chr_can_read[m](d->ext_opaque[m]))
375 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
376 else
377 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
381 static void mux_chr_event(void *opaque, int event)
383 CharDriverState *chr = opaque;
384 MuxDriver *d = chr->opaque;
385 int i;
387 /* Send the event to all registered listeners */
388 for (i = 0; i < d->mux_cnt; i++)
389 if (d->chr_event[i])
390 d->chr_event[i](d->ext_opaque[i], event);
393 static void mux_chr_update_read_handler(CharDriverState *chr)
395 MuxDriver *d = chr->opaque;
397 if (d->mux_cnt >= MAX_MUX) {
398 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
399 return;
401 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
402 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
403 d->chr_read[d->mux_cnt] = chr->chr_read;
404 d->chr_event[d->mux_cnt] = chr->chr_event;
405 /* Fix up the real driver with mux routines */
406 if (d->mux_cnt == 0) {
407 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
408 mux_chr_event, chr);
410 chr->focus = d->mux_cnt;
411 d->mux_cnt++;
414 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
416 CharDriverState *chr;
417 MuxDriver *d;
419 chr = qemu_mallocz(sizeof(CharDriverState));
420 if (!chr)
421 return NULL;
422 d = qemu_mallocz(sizeof(MuxDriver));
423 if (!d) {
424 free(chr);
425 return NULL;
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;
438 #ifdef _WIN32
439 int send_all(int fd, const uint8_t *buf, int len1)
441 int ret, len;
443 len = len1;
444 while (len > 0) {
445 ret = send(fd, buf, len, 0);
446 if (ret < 0) {
447 int errno;
448 errno = WSAGetLastError();
449 if (errno != WSAEWOULDBLOCK) {
450 return -1;
452 } else if (ret == 0) {
453 break;
454 } else {
455 buf += ret;
456 len -= ret;
459 return len1 - len;
462 #else
464 static int unix_write(int fd, const uint8_t *buf, int len1)
466 int ret, len;
468 len = len1;
469 while (len > 0) {
470 ret = write(fd, buf, len);
471 if (ret < 0) {
472 if (errno != EINTR && errno != EAGAIN)
473 return -1;
474 } else if (ret == 0) {
475 break;
476 } else {
477 buf += ret;
478 len -= ret;
481 return len1 - len;
484 int send_all(int fd, const uint8_t *buf, int len1)
486 return unix_write(fd, buf, len1);
488 #endif /* !_WIN32 */
490 #ifndef _WIN32
492 typedef struct {
493 int fd_in, fd_out;
494 int max_size;
495 } FDCharDriver;
497 #define STDIO_MAX_CLIENTS 1
498 static int stdio_nb_clients = 0;
500 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
502 FDCharDriver *s = chr->opaque;
503 return send_all(s->fd_out, buf, len);
506 static int fd_chr_read_poll(void *opaque)
508 CharDriverState *chr = opaque;
509 FDCharDriver *s = chr->opaque;
511 s->max_size = qemu_chr_can_read(chr);
512 return s->max_size;
515 static void fd_chr_read(void *opaque)
517 CharDriverState *chr = opaque;
518 FDCharDriver *s = chr->opaque;
519 int size, len;
520 uint8_t buf[1024];
522 len = sizeof(buf);
523 if (len > s->max_size)
524 len = s->max_size;
525 if (len == 0)
526 return;
527 size = read(s->fd_in, buf, len);
528 if (size == 0) {
529 /* FD has been closed. Remove it from the active list. */
530 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
531 return;
533 if (size > 0) {
534 qemu_chr_read(chr, buf, size);
538 static void fd_chr_update_read_handler(CharDriverState *chr)
540 FDCharDriver *s = chr->opaque;
542 if (s->fd_in >= 0) {
543 if (nographic && s->fd_in == 0) {
544 } else {
545 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
546 fd_chr_read, NULL, chr);
551 static void fd_chr_close(struct CharDriverState *chr)
553 FDCharDriver *s = chr->opaque;
555 if (s->fd_in >= 0) {
556 if (nographic && s->fd_in == 0) {
557 } else {
558 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
562 qemu_free(s);
565 /* open a character device to a unix fd */
566 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
568 CharDriverState *chr;
569 FDCharDriver *s;
571 chr = qemu_mallocz(sizeof(CharDriverState));
572 if (!chr)
573 return NULL;
574 s = qemu_mallocz(sizeof(FDCharDriver));
575 if (!s) {
576 free(chr);
577 return NULL;
579 s->fd_in = fd_in;
580 s->fd_out = fd_out;
581 chr->opaque = s;
582 chr->chr_write = fd_chr_write;
583 chr->chr_update_read_handler = fd_chr_update_read_handler;
584 chr->chr_close = fd_chr_close;
586 qemu_chr_reset(chr);
588 return chr;
591 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
593 int fd_out;
595 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
596 if (fd_out < 0)
597 return NULL;
598 return qemu_chr_open_fd(-1, fd_out);
601 static CharDriverState *qemu_chr_open_pipe(const char *filename)
603 int fd_in, fd_out;
604 char filename_in[256], filename_out[256];
606 snprintf(filename_in, 256, "%s.in", filename);
607 snprintf(filename_out, 256, "%s.out", filename);
608 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
609 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
610 if (fd_in < 0 || fd_out < 0) {
611 if (fd_in >= 0)
612 close(fd_in);
613 if (fd_out >= 0)
614 close(fd_out);
615 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
616 if (fd_in < 0)
617 return NULL;
619 return qemu_chr_open_fd(fd_in, fd_out);
623 /* for STDIO, we handle the case where several clients use it
624 (nographic mode) */
626 #define TERM_FIFO_MAX_SIZE 1
628 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
629 static int term_fifo_size;
631 static int stdio_read_poll(void *opaque)
633 CharDriverState *chr = opaque;
635 /* try to flush the queue if needed */
636 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
637 qemu_chr_read(chr, term_fifo, 1);
638 term_fifo_size = 0;
640 /* see if we can absorb more chars */
641 if (term_fifo_size == 0)
642 return 1;
643 else
644 return 0;
647 static void stdio_read(void *opaque)
649 int size;
650 uint8_t buf[1];
651 CharDriverState *chr = opaque;
653 size = read(0, buf, 1);
654 if (size == 0) {
655 /* stdin has been closed. Remove it from the active list. */
656 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
657 return;
659 if (size > 0) {
660 if (qemu_chr_can_read(chr) > 0) {
661 qemu_chr_read(chr, buf, 1);
662 } else if (term_fifo_size == 0) {
663 term_fifo[term_fifo_size++] = buf[0];
668 /* init terminal so that we can grab keys */
669 static struct termios oldtty;
670 static int old_fd0_flags;
671 static int term_atexit_done;
673 static void term_exit(void)
675 tcsetattr (0, TCSANOW, &oldtty);
676 fcntl(0, F_SETFL, old_fd0_flags);
679 static void term_init(void)
681 struct termios tty;
683 tcgetattr (0, &tty);
684 oldtty = tty;
685 old_fd0_flags = fcntl(0, F_GETFL);
687 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
688 |INLCR|IGNCR|ICRNL|IXON);
689 tty.c_oflag |= OPOST;
690 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
691 /* if graphical mode, we allow Ctrl-C handling */
692 if (nographic)
693 tty.c_lflag &= ~ISIG;
694 tty.c_cflag &= ~(CSIZE|PARENB);
695 tty.c_cflag |= CS8;
696 tty.c_cc[VMIN] = 1;
697 tty.c_cc[VTIME] = 0;
699 tcsetattr (0, TCSANOW, &tty);
701 if (!term_atexit_done++)
702 atexit(term_exit);
704 fcntl(0, F_SETFL, O_NONBLOCK);
707 static void qemu_chr_close_stdio(struct CharDriverState *chr)
709 term_exit();
710 stdio_nb_clients--;
711 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
712 fd_chr_close(chr);
715 static CharDriverState *qemu_chr_open_stdio(void)
717 CharDriverState *chr;
719 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
720 return NULL;
721 chr = qemu_chr_open_fd(0, 1);
722 chr->chr_close = qemu_chr_close_stdio;
723 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
724 stdio_nb_clients++;
725 term_init();
727 return chr;
730 #ifdef __sun__
731 /* Once Solaris has openpty(), this is going to be removed. */
732 int openpty(int *amaster, int *aslave, char *name,
733 struct termios *termp, struct winsize *winp)
735 const char *slave;
736 int mfd = -1, sfd = -1;
738 *amaster = *aslave = -1;
740 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
741 if (mfd < 0)
742 goto err;
744 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
745 goto err;
747 if ((slave = ptsname(mfd)) == NULL)
748 goto err;
750 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
751 goto err;
753 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
754 (termp != NULL && tcgetattr(sfd, termp) < 0))
755 goto err;
757 if (amaster)
758 *amaster = mfd;
759 if (aslave)
760 *aslave = sfd;
761 if (winp)
762 ioctl(sfd, TIOCSWINSZ, winp);
764 return 0;
766 err:
767 if (sfd != -1)
768 close(sfd);
769 close(mfd);
770 return -1;
773 void cfmakeraw (struct termios *termios_p)
775 termios_p->c_iflag &=
776 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
777 termios_p->c_oflag &= ~OPOST;
778 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
779 termios_p->c_cflag &= ~(CSIZE|PARENB);
780 termios_p->c_cflag |= CS8;
782 termios_p->c_cc[VMIN] = 0;
783 termios_p->c_cc[VTIME] = 0;
785 #endif
787 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
788 || defined(__NetBSD__) || defined(__OpenBSD__)
790 typedef struct {
791 int fd;
792 int connected;
793 int polling;
794 int read_bytes;
795 QEMUTimer *timer;
796 } PtyCharDriver;
798 static void pty_chr_update_read_handler(CharDriverState *chr);
799 static void pty_chr_state(CharDriverState *chr, int connected);
801 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
803 PtyCharDriver *s = chr->opaque;
805 if (!s->connected) {
806 /* guest sends data, check for (re-)connect */
807 pty_chr_update_read_handler(chr);
808 return 0;
810 return send_all(s->fd, buf, len);
813 static int pty_chr_read_poll(void *opaque)
815 CharDriverState *chr = opaque;
816 PtyCharDriver *s = chr->opaque;
818 s->read_bytes = qemu_chr_can_read(chr);
819 return s->read_bytes;
822 static void pty_chr_read(void *opaque)
824 CharDriverState *chr = opaque;
825 PtyCharDriver *s = chr->opaque;
826 int size, len;
827 uint8_t buf[1024];
829 len = sizeof(buf);
830 if (len > s->read_bytes)
831 len = s->read_bytes;
832 if (len == 0)
833 return;
834 size = read(s->fd, buf, len);
835 if ((size == -1 && errno == EIO) ||
836 (size == 0)) {
837 pty_chr_state(chr, 0);
838 return;
840 if (size > 0) {
841 pty_chr_state(chr, 1);
842 qemu_chr_read(chr, buf, size);
846 static void pty_chr_update_read_handler(CharDriverState *chr)
848 PtyCharDriver *s = chr->opaque;
850 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
851 pty_chr_read, NULL, chr);
852 s->polling = 1;
854 * Short timeout here: just need wait long enougth that qemu makes
855 * it through the poll loop once. When reconnected we want a
856 * short timeout so we notice it almost instantly. Otherwise
857 * read() gives us -EIO instantly, making pty_chr_state() reset the
858 * timeout to the normal (much longer) poll interval before the
859 * timer triggers.
861 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
864 static void pty_chr_state(CharDriverState *chr, int connected)
866 PtyCharDriver *s = chr->opaque;
868 if (!connected) {
869 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
870 s->connected = 0;
871 s->polling = 0;
872 /* (re-)connect poll interval for idle guests: once per second.
873 * We check more frequently in case the guests sends data to
874 * the virtual device linked to our pty. */
875 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
876 } else {
877 if (!s->connected)
878 qemu_chr_reset(chr);
879 s->connected = 1;
883 static void pty_chr_timer(void *opaque)
885 struct CharDriverState *chr = opaque;
886 PtyCharDriver *s = chr->opaque;
888 if (s->connected)
889 return;
890 if (s->polling) {
891 /* If we arrive here without polling being cleared due
892 * read returning -EIO, then we are (re-)connected */
893 pty_chr_state(chr, 1);
894 return;
897 /* Next poll ... */
898 pty_chr_update_read_handler(chr);
901 static void pty_chr_close(struct CharDriverState *chr)
903 PtyCharDriver *s = chr->opaque;
905 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
906 close(s->fd);
907 qemu_free(s);
910 static CharDriverState *qemu_chr_open_pty(void)
912 CharDriverState *chr;
913 PtyCharDriver *s;
914 struct termios tty;
915 int slave_fd, len;
916 #if defined(__OpenBSD__)
917 char pty_name[PATH_MAX];
918 #define q_ptsname(x) pty_name
919 #else
920 char *pty_name = NULL;
921 #define q_ptsname(x) ptsname(x)
922 #endif
924 chr = qemu_mallocz(sizeof(CharDriverState));
925 if (!chr)
926 return NULL;
927 s = qemu_mallocz(sizeof(PtyCharDriver));
928 if (!s) {
929 qemu_free(chr);
930 return NULL;
933 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
934 return NULL;
937 /* Set raw attributes on the pty. */
938 cfmakeraw(&tty);
939 tcsetattr(slave_fd, TCSAFLUSH, &tty);
940 close(slave_fd);
942 len = strlen(q_ptsname(s->fd)) + 5;
943 chr->filename = qemu_malloc(len);
944 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
945 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
947 chr->opaque = s;
948 chr->chr_write = pty_chr_write;
949 chr->chr_update_read_handler = pty_chr_update_read_handler;
950 chr->chr_close = pty_chr_close;
952 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
954 return chr;
957 static void tty_serial_init(int fd, int speed,
958 int parity, int data_bits, int stop_bits)
960 struct termios tty;
961 speed_t spd;
963 #if 0
964 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
965 speed, parity, data_bits, stop_bits);
966 #endif
967 tcgetattr (fd, &tty);
969 #define MARGIN 1.1
970 if (speed <= 50 * MARGIN)
971 spd = B50;
972 else if (speed <= 75 * MARGIN)
973 spd = B75;
974 else if (speed <= 300 * MARGIN)
975 spd = B300;
976 else if (speed <= 600 * MARGIN)
977 spd = B600;
978 else if (speed <= 1200 * MARGIN)
979 spd = B1200;
980 else if (speed <= 2400 * MARGIN)
981 spd = B2400;
982 else if (speed <= 4800 * MARGIN)
983 spd = B4800;
984 else if (speed <= 9600 * MARGIN)
985 spd = B9600;
986 else if (speed <= 19200 * MARGIN)
987 spd = B19200;
988 else if (speed <= 38400 * MARGIN)
989 spd = B38400;
990 else if (speed <= 57600 * MARGIN)
991 spd = B57600;
992 else if (speed <= 115200 * MARGIN)
993 spd = B115200;
994 else
995 spd = B115200;
997 cfsetispeed(&tty, spd);
998 cfsetospeed(&tty, spd);
1000 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1001 |INLCR|IGNCR|ICRNL|IXON);
1002 tty.c_oflag |= OPOST;
1003 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1004 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1005 switch(data_bits) {
1006 default:
1007 case 8:
1008 tty.c_cflag |= CS8;
1009 break;
1010 case 7:
1011 tty.c_cflag |= CS7;
1012 break;
1013 case 6:
1014 tty.c_cflag |= CS6;
1015 break;
1016 case 5:
1017 tty.c_cflag |= CS5;
1018 break;
1020 switch(parity) {
1021 default:
1022 case 'N':
1023 break;
1024 case 'E':
1025 tty.c_cflag |= PARENB;
1026 break;
1027 case 'O':
1028 tty.c_cflag |= PARENB | PARODD;
1029 break;
1031 if (stop_bits == 2)
1032 tty.c_cflag |= CSTOPB;
1034 tcsetattr (fd, TCSANOW, &tty);
1037 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1039 FDCharDriver *s = chr->opaque;
1041 switch(cmd) {
1042 case CHR_IOCTL_SERIAL_SET_PARAMS:
1044 QEMUSerialSetParams *ssp = arg;
1045 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1046 ssp->data_bits, ssp->stop_bits);
1048 break;
1049 case CHR_IOCTL_SERIAL_SET_BREAK:
1051 int enable = *(int *)arg;
1052 if (enable)
1053 tcsendbreak(s->fd_in, 1);
1055 break;
1056 case CHR_IOCTL_SERIAL_GET_TIOCM:
1058 int sarg = 0;
1059 int *targ = (int *)arg;
1060 ioctl(s->fd_in, TIOCMGET, &sarg);
1061 *targ = 0;
1062 if (sarg | TIOCM_CTS)
1063 *targ |= CHR_TIOCM_CTS;
1064 if (sarg | TIOCM_CAR)
1065 *targ |= CHR_TIOCM_CAR;
1066 if (sarg | TIOCM_DSR)
1067 *targ |= CHR_TIOCM_DSR;
1068 if (sarg | TIOCM_RI)
1069 *targ |= CHR_TIOCM_RI;
1070 if (sarg | TIOCM_DTR)
1071 *targ |= CHR_TIOCM_DTR;
1072 if (sarg | TIOCM_RTS)
1073 *targ |= CHR_TIOCM_RTS;
1075 break;
1076 case CHR_IOCTL_SERIAL_SET_TIOCM:
1078 int sarg = *(int *)arg;
1079 int targ = 0;
1080 if (sarg | CHR_TIOCM_DTR)
1081 targ |= TIOCM_DTR;
1082 if (sarg | CHR_TIOCM_RTS)
1083 targ |= TIOCM_RTS;
1084 ioctl(s->fd_in, TIOCMSET, &targ);
1086 break;
1087 default:
1088 return -ENOTSUP;
1090 return 0;
1093 static CharDriverState *qemu_chr_open_tty(const char *filename)
1095 CharDriverState *chr;
1096 int fd;
1098 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1099 tty_serial_init(fd, 115200, 'N', 8, 1);
1100 chr = qemu_chr_open_fd(fd, fd);
1101 if (!chr) {
1102 close(fd);
1103 return NULL;
1105 chr->chr_ioctl = tty_serial_ioctl;
1106 qemu_chr_reset(chr);
1107 return chr;
1109 #else /* ! __linux__ && ! __sun__ */
1110 static CharDriverState *qemu_chr_open_pty(void)
1112 return NULL;
1114 #endif /* __linux__ || __sun__ */
1116 #if defined(__linux__)
1117 typedef struct {
1118 int fd;
1119 int mode;
1120 } ParallelCharDriver;
1122 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1124 if (s->mode != mode) {
1125 int m = mode;
1126 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1127 return 0;
1128 s->mode = mode;
1130 return 1;
1133 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1135 ParallelCharDriver *drv = chr->opaque;
1136 int fd = drv->fd;
1137 uint8_t b;
1139 switch(cmd) {
1140 case CHR_IOCTL_PP_READ_DATA:
1141 if (ioctl(fd, PPRDATA, &b) < 0)
1142 return -ENOTSUP;
1143 *(uint8_t *)arg = b;
1144 break;
1145 case CHR_IOCTL_PP_WRITE_DATA:
1146 b = *(uint8_t *)arg;
1147 if (ioctl(fd, PPWDATA, &b) < 0)
1148 return -ENOTSUP;
1149 break;
1150 case CHR_IOCTL_PP_READ_CONTROL:
1151 if (ioctl(fd, PPRCONTROL, &b) < 0)
1152 return -ENOTSUP;
1153 /* Linux gives only the lowest bits, and no way to know data
1154 direction! For better compatibility set the fixed upper
1155 bits. */
1156 *(uint8_t *)arg = b | 0xc0;
1157 break;
1158 case CHR_IOCTL_PP_WRITE_CONTROL:
1159 b = *(uint8_t *)arg;
1160 if (ioctl(fd, PPWCONTROL, &b) < 0)
1161 return -ENOTSUP;
1162 break;
1163 case CHR_IOCTL_PP_READ_STATUS:
1164 if (ioctl(fd, PPRSTATUS, &b) < 0)
1165 return -ENOTSUP;
1166 *(uint8_t *)arg = b;
1167 break;
1168 case CHR_IOCTL_PP_DATA_DIR:
1169 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1170 return -ENOTSUP;
1171 break;
1172 case CHR_IOCTL_PP_EPP_READ_ADDR:
1173 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1174 struct ParallelIOArg *parg = arg;
1175 int n = read(fd, parg->buffer, parg->count);
1176 if (n != parg->count) {
1177 return -EIO;
1180 break;
1181 case CHR_IOCTL_PP_EPP_READ:
1182 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1183 struct ParallelIOArg *parg = arg;
1184 int n = read(fd, parg->buffer, parg->count);
1185 if (n != parg->count) {
1186 return -EIO;
1189 break;
1190 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1191 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1192 struct ParallelIOArg *parg = arg;
1193 int n = write(fd, parg->buffer, parg->count);
1194 if (n != parg->count) {
1195 return -EIO;
1198 break;
1199 case CHR_IOCTL_PP_EPP_WRITE:
1200 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1201 struct ParallelIOArg *parg = arg;
1202 int n = write(fd, parg->buffer, parg->count);
1203 if (n != parg->count) {
1204 return -EIO;
1207 break;
1208 default:
1209 return -ENOTSUP;
1211 return 0;
1214 static void pp_close(CharDriverState *chr)
1216 ParallelCharDriver *drv = chr->opaque;
1217 int fd = drv->fd;
1219 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1220 ioctl(fd, PPRELEASE);
1221 close(fd);
1222 qemu_free(drv);
1225 static CharDriverState *qemu_chr_open_pp(const char *filename)
1227 CharDriverState *chr;
1228 ParallelCharDriver *drv;
1229 int fd;
1231 TFR(fd = open(filename, O_RDWR));
1232 if (fd < 0)
1233 return NULL;
1235 if (ioctl(fd, PPCLAIM) < 0) {
1236 close(fd);
1237 return NULL;
1240 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1241 if (!drv) {
1242 close(fd);
1243 return NULL;
1245 drv->fd = fd;
1246 drv->mode = IEEE1284_MODE_COMPAT;
1248 chr = qemu_mallocz(sizeof(CharDriverState));
1249 if (!chr) {
1250 qemu_free(drv);
1251 close(fd);
1252 return NULL;
1254 chr->chr_write = null_chr_write;
1255 chr->chr_ioctl = pp_ioctl;
1256 chr->chr_close = pp_close;
1257 chr->opaque = drv;
1259 qemu_chr_reset(chr);
1261 return chr;
1263 #endif /* __linux__ */
1265 #else /* _WIN32 */
1267 typedef struct {
1268 int max_size;
1269 HANDLE hcom, hrecv, hsend;
1270 OVERLAPPED orecv, osend;
1271 BOOL fpipe;
1272 DWORD len;
1273 } WinCharState;
1275 #define NSENDBUF 2048
1276 #define NRECVBUF 2048
1277 #define MAXCONNECT 1
1278 #define NTIMEOUT 5000
1280 static int win_chr_poll(void *opaque);
1281 static int win_chr_pipe_poll(void *opaque);
1283 static void win_chr_close(CharDriverState *chr)
1285 WinCharState *s = chr->opaque;
1287 if (s->hsend) {
1288 CloseHandle(s->hsend);
1289 s->hsend = NULL;
1291 if (s->hrecv) {
1292 CloseHandle(s->hrecv);
1293 s->hrecv = NULL;
1295 if (s->hcom) {
1296 CloseHandle(s->hcom);
1297 s->hcom = NULL;
1299 if (s->fpipe)
1300 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1301 else
1302 qemu_del_polling_cb(win_chr_poll, chr);
1305 static int win_chr_init(CharDriverState *chr, const char *filename)
1307 WinCharState *s = chr->opaque;
1308 COMMCONFIG comcfg;
1309 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1310 COMSTAT comstat;
1311 DWORD size;
1312 DWORD err;
1314 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1315 if (!s->hsend) {
1316 fprintf(stderr, "Failed CreateEvent\n");
1317 goto fail;
1319 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1320 if (!s->hrecv) {
1321 fprintf(stderr, "Failed CreateEvent\n");
1322 goto fail;
1325 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1326 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1327 if (s->hcom == INVALID_HANDLE_VALUE) {
1328 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1329 s->hcom = NULL;
1330 goto fail;
1333 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1334 fprintf(stderr, "Failed SetupComm\n");
1335 goto fail;
1338 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1339 size = sizeof(COMMCONFIG);
1340 GetDefaultCommConfig(filename, &comcfg, &size);
1341 comcfg.dcb.DCBlength = sizeof(DCB);
1342 CommConfigDialog(filename, NULL, &comcfg);
1344 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1345 fprintf(stderr, "Failed SetCommState\n");
1346 goto fail;
1349 if (!SetCommMask(s->hcom, EV_ERR)) {
1350 fprintf(stderr, "Failed SetCommMask\n");
1351 goto fail;
1354 cto.ReadIntervalTimeout = MAXDWORD;
1355 if (!SetCommTimeouts(s->hcom, &cto)) {
1356 fprintf(stderr, "Failed SetCommTimeouts\n");
1357 goto fail;
1360 if (!ClearCommError(s->hcom, &err, &comstat)) {
1361 fprintf(stderr, "Failed ClearCommError\n");
1362 goto fail;
1364 qemu_add_polling_cb(win_chr_poll, chr);
1365 return 0;
1367 fail:
1368 win_chr_close(chr);
1369 return -1;
1372 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1374 WinCharState *s = chr->opaque;
1375 DWORD len, ret, size, err;
1377 len = len1;
1378 ZeroMemory(&s->osend, sizeof(s->osend));
1379 s->osend.hEvent = s->hsend;
1380 while (len > 0) {
1381 if (s->hsend)
1382 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1383 else
1384 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1385 if (!ret) {
1386 err = GetLastError();
1387 if (err == ERROR_IO_PENDING) {
1388 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1389 if (ret) {
1390 buf += size;
1391 len -= size;
1392 } else {
1393 break;
1395 } else {
1396 break;
1398 } else {
1399 buf += size;
1400 len -= size;
1403 return len1 - len;
1406 static int win_chr_read_poll(CharDriverState *chr)
1408 WinCharState *s = chr->opaque;
1410 s->max_size = qemu_chr_can_read(chr);
1411 return s->max_size;
1414 static void win_chr_readfile(CharDriverState *chr)
1416 WinCharState *s = chr->opaque;
1417 int ret, err;
1418 uint8_t buf[1024];
1419 DWORD size;
1421 ZeroMemory(&s->orecv, sizeof(s->orecv));
1422 s->orecv.hEvent = s->hrecv;
1423 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1424 if (!ret) {
1425 err = GetLastError();
1426 if (err == ERROR_IO_PENDING) {
1427 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1431 if (size > 0) {
1432 qemu_chr_read(chr, buf, size);
1436 static void win_chr_read(CharDriverState *chr)
1438 WinCharState *s = chr->opaque;
1440 if (s->len > s->max_size)
1441 s->len = s->max_size;
1442 if (s->len == 0)
1443 return;
1445 win_chr_readfile(chr);
1448 static int win_chr_poll(void *opaque)
1450 CharDriverState *chr = opaque;
1451 WinCharState *s = chr->opaque;
1452 COMSTAT status;
1453 DWORD comerr;
1455 ClearCommError(s->hcom, &comerr, &status);
1456 if (status.cbInQue > 0) {
1457 s->len = status.cbInQue;
1458 win_chr_read_poll(chr);
1459 win_chr_read(chr);
1460 return 1;
1462 return 0;
1465 static CharDriverState *qemu_chr_open_win(const char *filename)
1467 CharDriverState *chr;
1468 WinCharState *s;
1470 chr = qemu_mallocz(sizeof(CharDriverState));
1471 if (!chr)
1472 return NULL;
1473 s = qemu_mallocz(sizeof(WinCharState));
1474 if (!s) {
1475 free(chr);
1476 return NULL;
1478 chr->opaque = s;
1479 chr->chr_write = win_chr_write;
1480 chr->chr_close = win_chr_close;
1482 if (win_chr_init(chr, filename) < 0) {
1483 free(s);
1484 free(chr);
1485 return NULL;
1487 qemu_chr_reset(chr);
1488 return chr;
1491 static int win_chr_pipe_poll(void *opaque)
1493 CharDriverState *chr = opaque;
1494 WinCharState *s = chr->opaque;
1495 DWORD size;
1497 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1498 if (size > 0) {
1499 s->len = size;
1500 win_chr_read_poll(chr);
1501 win_chr_read(chr);
1502 return 1;
1504 return 0;
1507 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1509 WinCharState *s = chr->opaque;
1510 OVERLAPPED ov;
1511 int ret;
1512 DWORD size;
1513 char openname[256];
1515 s->fpipe = TRUE;
1517 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1518 if (!s->hsend) {
1519 fprintf(stderr, "Failed CreateEvent\n");
1520 goto fail;
1522 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1523 if (!s->hrecv) {
1524 fprintf(stderr, "Failed CreateEvent\n");
1525 goto fail;
1528 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1529 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1530 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1531 PIPE_WAIT,
1532 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1533 if (s->hcom == INVALID_HANDLE_VALUE) {
1534 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1535 s->hcom = NULL;
1536 goto fail;
1539 ZeroMemory(&ov, sizeof(ov));
1540 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1541 ret = ConnectNamedPipe(s->hcom, &ov);
1542 if (ret) {
1543 fprintf(stderr, "Failed ConnectNamedPipe\n");
1544 goto fail;
1547 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1548 if (!ret) {
1549 fprintf(stderr, "Failed GetOverlappedResult\n");
1550 if (ov.hEvent) {
1551 CloseHandle(ov.hEvent);
1552 ov.hEvent = NULL;
1554 goto fail;
1557 if (ov.hEvent) {
1558 CloseHandle(ov.hEvent);
1559 ov.hEvent = NULL;
1561 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1562 return 0;
1564 fail:
1565 win_chr_close(chr);
1566 return -1;
1570 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1572 CharDriverState *chr;
1573 WinCharState *s;
1575 chr = qemu_mallocz(sizeof(CharDriverState));
1576 if (!chr)
1577 return NULL;
1578 s = qemu_mallocz(sizeof(WinCharState));
1579 if (!s) {
1580 free(chr);
1581 return NULL;
1583 chr->opaque = s;
1584 chr->chr_write = win_chr_write;
1585 chr->chr_close = win_chr_close;
1587 if (win_chr_pipe_init(chr, filename) < 0) {
1588 free(s);
1589 free(chr);
1590 return NULL;
1592 qemu_chr_reset(chr);
1593 return chr;
1596 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1598 CharDriverState *chr;
1599 WinCharState *s;
1601 chr = qemu_mallocz(sizeof(CharDriverState));
1602 if (!chr)
1603 return NULL;
1604 s = qemu_mallocz(sizeof(WinCharState));
1605 if (!s) {
1606 free(chr);
1607 return NULL;
1609 s->hcom = fd_out;
1610 chr->opaque = s;
1611 chr->chr_write = win_chr_write;
1612 qemu_chr_reset(chr);
1613 return chr;
1616 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1618 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1621 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1623 HANDLE fd_out;
1625 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1626 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1627 if (fd_out == INVALID_HANDLE_VALUE)
1628 return NULL;
1630 return qemu_chr_open_win_file(fd_out);
1632 #endif /* !_WIN32 */
1634 /***********************************************************/
1635 /* UDP Net console */
1637 typedef struct {
1638 int fd;
1639 struct sockaddr_in daddr;
1640 uint8_t buf[1024];
1641 int bufcnt;
1642 int bufptr;
1643 int max_size;
1644 } NetCharDriver;
1646 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1648 NetCharDriver *s = chr->opaque;
1650 return sendto(s->fd, buf, len, 0,
1651 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1654 static int udp_chr_read_poll(void *opaque)
1656 CharDriverState *chr = opaque;
1657 NetCharDriver *s = chr->opaque;
1659 s->max_size = qemu_chr_can_read(chr);
1661 /* If there were any stray characters in the queue process them
1662 * first
1664 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1665 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1666 s->bufptr++;
1667 s->max_size = qemu_chr_can_read(chr);
1669 return s->max_size;
1672 static void udp_chr_read(void *opaque)
1674 CharDriverState *chr = opaque;
1675 NetCharDriver *s = chr->opaque;
1677 if (s->max_size == 0)
1678 return;
1679 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1680 s->bufptr = s->bufcnt;
1681 if (s->bufcnt <= 0)
1682 return;
1684 s->bufptr = 0;
1685 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1686 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1687 s->bufptr++;
1688 s->max_size = qemu_chr_can_read(chr);
1692 static void udp_chr_update_read_handler(CharDriverState *chr)
1694 NetCharDriver *s = chr->opaque;
1696 if (s->fd >= 0) {
1697 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1698 udp_chr_read, NULL, chr);
1702 static CharDriverState *qemu_chr_open_udp(const char *def)
1704 CharDriverState *chr = NULL;
1705 NetCharDriver *s = NULL;
1706 int fd = -1;
1707 struct sockaddr_in saddr;
1709 chr = qemu_mallocz(sizeof(CharDriverState));
1710 if (!chr)
1711 goto return_err;
1712 s = qemu_mallocz(sizeof(NetCharDriver));
1713 if (!s)
1714 goto return_err;
1716 fd = socket(PF_INET, SOCK_DGRAM, 0);
1717 if (fd < 0) {
1718 perror("socket(PF_INET, SOCK_DGRAM)");
1719 goto return_err;
1722 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1723 printf("Could not parse: %s\n", def);
1724 goto return_err;
1727 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1729 perror("bind");
1730 goto return_err;
1733 s->fd = fd;
1734 s->bufcnt = 0;
1735 s->bufptr = 0;
1736 chr->opaque = s;
1737 chr->chr_write = udp_chr_write;
1738 chr->chr_update_read_handler = udp_chr_update_read_handler;
1739 return chr;
1741 return_err:
1742 if (chr)
1743 free(chr);
1744 if (s)
1745 free(s);
1746 if (fd >= 0)
1747 closesocket(fd);
1748 return NULL;
1751 /***********************************************************/
1752 /* TCP Net console */
1754 typedef struct {
1755 int fd, listen_fd;
1756 int connected;
1757 int max_size;
1758 int do_telnetopt;
1759 int do_nodelay;
1760 int is_unix;
1761 } TCPCharDriver;
1763 static void tcp_chr_accept(void *opaque);
1765 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1767 TCPCharDriver *s = chr->opaque;
1768 if (s->connected) {
1769 return send_all(s->fd, buf, len);
1770 } else {
1771 /* XXX: indicate an error ? */
1772 return len;
1776 static int tcp_chr_read_poll(void *opaque)
1778 CharDriverState *chr = opaque;
1779 TCPCharDriver *s = chr->opaque;
1780 if (!s->connected)
1781 return 0;
1782 s->max_size = qemu_chr_can_read(chr);
1783 return s->max_size;
1786 #define IAC 255
1787 #define IAC_BREAK 243
1788 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1789 TCPCharDriver *s,
1790 uint8_t *buf, int *size)
1792 /* Handle any telnet client's basic IAC options to satisfy char by
1793 * char mode with no echo. All IAC options will be removed from
1794 * the buf and the do_telnetopt variable will be used to track the
1795 * state of the width of the IAC information.
1797 * IAC commands come in sets of 3 bytes with the exception of the
1798 * "IAC BREAK" command and the double IAC.
1801 int i;
1802 int j = 0;
1804 for (i = 0; i < *size; i++) {
1805 if (s->do_telnetopt > 1) {
1806 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1807 /* Double IAC means send an IAC */
1808 if (j != i)
1809 buf[j] = buf[i];
1810 j++;
1811 s->do_telnetopt = 1;
1812 } else {
1813 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1814 /* Handle IAC break commands by sending a serial break */
1815 qemu_chr_event(chr, CHR_EVENT_BREAK);
1816 s->do_telnetopt++;
1818 s->do_telnetopt++;
1820 if (s->do_telnetopt >= 4) {
1821 s->do_telnetopt = 1;
1823 } else {
1824 if ((unsigned char)buf[i] == IAC) {
1825 s->do_telnetopt = 2;
1826 } else {
1827 if (j != i)
1828 buf[j] = buf[i];
1829 j++;
1833 *size = j;
1836 static void tcp_chr_read(void *opaque)
1838 CharDriverState *chr = opaque;
1839 TCPCharDriver *s = chr->opaque;
1840 uint8_t buf[1024];
1841 int len, size;
1843 if (!s->connected || s->max_size <= 0)
1844 return;
1845 len = sizeof(buf);
1846 if (len > s->max_size)
1847 len = s->max_size;
1848 size = recv(s->fd, buf, len, 0);
1849 if (size == 0) {
1850 /* connection closed */
1851 s->connected = 0;
1852 if (s->listen_fd >= 0) {
1853 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1855 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1856 closesocket(s->fd);
1857 s->fd = -1;
1858 } else if (size > 0) {
1859 if (s->do_telnetopt)
1860 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1861 if (size > 0)
1862 qemu_chr_read(chr, buf, size);
1866 static void tcp_chr_connect(void *opaque)
1868 CharDriverState *chr = opaque;
1869 TCPCharDriver *s = chr->opaque;
1871 s->connected = 1;
1872 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1873 tcp_chr_read, NULL, chr);
1874 qemu_chr_reset(chr);
1877 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1878 static void tcp_chr_telnet_init(int fd)
1880 char buf[3];
1881 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1882 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1883 send(fd, (char *)buf, 3, 0);
1884 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1885 send(fd, (char *)buf, 3, 0);
1886 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1887 send(fd, (char *)buf, 3, 0);
1888 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1889 send(fd, (char *)buf, 3, 0);
1892 static void socket_set_nodelay(int fd)
1894 int val = 1;
1895 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1898 static void tcp_chr_accept(void *opaque)
1900 CharDriverState *chr = opaque;
1901 TCPCharDriver *s = chr->opaque;
1902 struct sockaddr_in saddr;
1903 #ifndef _WIN32
1904 struct sockaddr_un uaddr;
1905 #endif
1906 struct sockaddr *addr;
1907 socklen_t len;
1908 int fd;
1910 for(;;) {
1911 #ifndef _WIN32
1912 if (s->is_unix) {
1913 len = sizeof(uaddr);
1914 addr = (struct sockaddr *)&uaddr;
1915 } else
1916 #endif
1918 len = sizeof(saddr);
1919 addr = (struct sockaddr *)&saddr;
1921 fd = accept(s->listen_fd, addr, &len);
1922 if (fd < 0 && errno != EINTR) {
1923 return;
1924 } else if (fd >= 0) {
1925 if (s->do_telnetopt)
1926 tcp_chr_telnet_init(fd);
1927 break;
1930 socket_set_nonblock(fd);
1931 if (s->do_nodelay)
1932 socket_set_nodelay(fd);
1933 s->fd = fd;
1934 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
1935 tcp_chr_connect(chr);
1938 static void tcp_chr_close(CharDriverState *chr)
1940 TCPCharDriver *s = chr->opaque;
1941 if (s->fd >= 0)
1942 closesocket(s->fd);
1943 if (s->listen_fd >= 0)
1944 closesocket(s->listen_fd);
1945 qemu_free(s);
1948 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
1949 int is_telnet,
1950 int is_unix)
1952 CharDriverState *chr = NULL;
1953 TCPCharDriver *s = NULL;
1954 int fd = -1, ret, err, val;
1955 int is_listen = 0;
1956 int is_waitconnect = 1;
1957 int do_nodelay = 0;
1958 const char *ptr;
1959 struct sockaddr_in saddr;
1960 #ifndef _WIN32
1961 struct sockaddr_un uaddr;
1962 #endif
1963 struct sockaddr *addr;
1964 socklen_t addrlen;
1966 #ifndef _WIN32
1967 if (is_unix) {
1968 addr = (struct sockaddr *)&uaddr;
1969 addrlen = sizeof(uaddr);
1970 if (parse_unix_path(&uaddr, host_str) < 0)
1971 goto fail;
1972 } else
1973 #endif
1975 addr = (struct sockaddr *)&saddr;
1976 addrlen = sizeof(saddr);
1977 if (parse_host_port(&saddr, host_str) < 0)
1978 goto fail;
1981 ptr = host_str;
1982 while((ptr = strchr(ptr,','))) {
1983 ptr++;
1984 if (!strncmp(ptr,"server",6)) {
1985 is_listen = 1;
1986 } else if (!strncmp(ptr,"nowait",6)) {
1987 is_waitconnect = 0;
1988 } else if (!strncmp(ptr,"nodelay",6)) {
1989 do_nodelay = 1;
1990 } else {
1991 printf("Unknown option: %s\n", ptr);
1992 goto fail;
1995 if (!is_listen)
1996 is_waitconnect = 0;
1998 chr = qemu_mallocz(sizeof(CharDriverState));
1999 if (!chr)
2000 goto fail;
2001 s = qemu_mallocz(sizeof(TCPCharDriver));
2002 if (!s)
2003 goto fail;
2005 #ifndef _WIN32
2006 if (is_unix)
2007 fd = socket(PF_UNIX, SOCK_STREAM, 0);
2008 else
2009 #endif
2010 fd = socket(PF_INET, SOCK_STREAM, 0);
2012 if (fd < 0)
2013 goto fail;
2015 if (!is_waitconnect)
2016 socket_set_nonblock(fd);
2018 s->connected = 0;
2019 s->fd = -1;
2020 s->listen_fd = -1;
2021 s->is_unix = is_unix;
2022 s->do_nodelay = do_nodelay && !is_unix;
2024 chr->opaque = s;
2025 chr->chr_write = tcp_chr_write;
2026 chr->chr_close = tcp_chr_close;
2028 if (is_listen) {
2029 /* allow fast reuse */
2030 #ifndef _WIN32
2031 if (is_unix) {
2032 char path[109];
2033 pstrcpy(path, sizeof(path), uaddr.sun_path);
2034 unlink(path);
2035 } else
2036 #endif
2038 val = 1;
2039 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
2042 ret = bind(fd, addr, addrlen);
2043 if (ret < 0)
2044 goto fail;
2046 ret = listen(fd, 0);
2047 if (ret < 0)
2048 goto fail;
2050 s->listen_fd = fd;
2051 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2052 if (is_telnet)
2053 s->do_telnetopt = 1;
2054 } else {
2055 for(;;) {
2056 ret = connect(fd, addr, addrlen);
2057 if (ret < 0) {
2058 err = socket_error();
2059 if (err == EINTR || err == EWOULDBLOCK) {
2060 } else if (err == EINPROGRESS) {
2061 break;
2062 #ifdef _WIN32
2063 } else if (err == WSAEALREADY) {
2064 break;
2065 #endif
2066 } else {
2067 goto fail;
2069 } else {
2070 s->connected = 1;
2071 break;
2074 s->fd = fd;
2075 socket_set_nodelay(fd);
2076 if (s->connected)
2077 tcp_chr_connect(chr);
2078 else
2079 qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
2082 if (is_listen && is_waitconnect) {
2083 printf("QEMU waiting for connection on: %s\n", host_str);
2084 tcp_chr_accept(chr);
2085 socket_set_nonblock(s->listen_fd);
2088 return chr;
2089 fail:
2090 if (fd >= 0)
2091 closesocket(fd);
2092 qemu_free(s);
2093 qemu_free(chr);
2094 return NULL;
2097 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
2098 = TAILQ_HEAD_INITIALIZER(chardevs);
2100 CharDriverState *qemu_chr_open(const char *label, const char *filename)
2102 const char *p;
2103 CharDriverState *chr;
2105 if (!strcmp(filename, "vc")) {
2106 chr = text_console_init(&display_state, 0);
2107 } else
2108 if (strstart(filename, "vc:", &p)) {
2109 chr = text_console_init(&display_state, p);
2110 } else
2111 if (!strcmp(filename, "null")) {
2112 chr = qemu_chr_open_null();
2113 } else
2114 if (strstart(filename, "tcp:", &p)) {
2115 chr = qemu_chr_open_tcp(p, 0, 0);
2116 } else
2117 if (strstart(filename, "telnet:", &p)) {
2118 chr = qemu_chr_open_tcp(p, 1, 0);
2119 } else
2120 if (strstart(filename, "udp:", &p)) {
2121 chr = qemu_chr_open_udp(p);
2122 } else
2123 if (strstart(filename, "mon:", &p)) {
2124 chr = qemu_chr_open(label, p);
2125 if (chr) {
2126 chr = qemu_chr_open_mux(chr);
2127 monitor_init(chr, !nographic);
2128 } else {
2129 printf("Unable to open driver: %s\n", p);
2131 } else
2132 #ifndef _WIN32
2133 if (strstart(filename, "unix:", &p)) {
2134 chr = qemu_chr_open_tcp(p, 0, 1);
2135 } else if (strstart(filename, "file:", &p)) {
2136 chr = qemu_chr_open_file_out(p);
2137 } else if (strstart(filename, "pipe:", &p)) {
2138 chr = qemu_chr_open_pipe(p);
2139 } else if (!strcmp(filename, "pty")) {
2140 chr = qemu_chr_open_pty();
2141 } else if (!strcmp(filename, "stdio")) {
2142 chr = qemu_chr_open_stdio();
2143 } else
2144 #if defined(__linux__)
2145 if (strstart(filename, "/dev/parport", NULL)) {
2146 chr = qemu_chr_open_pp(filename);
2147 } else
2148 #endif
2149 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2150 || defined(__NetBSD__) || defined(__OpenBSD__)
2151 if (strstart(filename, "/dev/", NULL)) {
2152 chr = qemu_chr_open_tty(filename);
2153 } else
2154 #endif
2155 #else /* !_WIN32 */
2156 if (strstart(filename, "COM", NULL)) {
2157 chr = qemu_chr_open_win(filename);
2158 } else
2159 if (strstart(filename, "pipe:", &p)) {
2160 chr = qemu_chr_open_win_pipe(p);
2161 } else
2162 if (strstart(filename, "con:", NULL)) {
2163 chr = qemu_chr_open_win_con(filename);
2164 } else
2165 if (strstart(filename, "file:", &p)) {
2166 chr = qemu_chr_open_win_file_out(p);
2167 } else
2168 #endif
2169 #ifdef CONFIG_BRLAPI
2170 if (!strcmp(filename, "braille")) {
2171 chr = chr_baum_init();
2172 } else
2173 #endif
2175 chr = NULL;
2178 if (chr) {
2179 if (!chr->filename)
2180 chr->filename = qemu_strdup(filename);
2181 chr->label = qemu_strdup(label);
2182 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2184 return chr;
2187 void qemu_chr_close(CharDriverState *chr)
2189 TAILQ_REMOVE(&chardevs, chr, next);
2190 if (chr->chr_close)
2191 chr->chr_close(chr);
2192 qemu_free(chr->filename);
2193 qemu_free(chr->label);
2194 qemu_free(chr);
2197 void qemu_chr_info(void)
2199 CharDriverState *chr;
2201 TAILQ_FOREACH(chr, &chardevs, next) {
2202 term_printf("%s: filename=%s\n", chr->label, chr->filename);