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