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