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