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