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