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