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