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