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