blob: c25863a15fd230af2f8b526384a99654ee217507 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
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"
aliguori376253e2009-03-05 23:01:23 +000026#include "monitor.h"
aliguori6f97dba2008-10-31 18:49:55 +000027#include "console.h"
28#include "sysemu.h"
29#include "qemu-timer.h"
30#include "qemu-char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000031#include "hw/usb.h"
32#include "hw/baum.h"
aurel32aa71cf82009-02-08 15:53:20 +000033#include "hw/msmouse.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030034#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
36#include <unistd.h>
37#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000038#include <time.h>
39#include <errno.h>
40#include <sys/time.h>
41#include <zlib.h>
42
43#ifndef _WIN32
44#include <sys/times.h>
45#include <sys/wait.h>
46#include <termios.h>
47#include <sys/mman.h>
48#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <sys/socket.h>
51#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000054#include <dirent.h>
55#include <netdb.h>
56#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020057#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000058#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010059#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
aliguori6f97dba2008-10-31 18:49:55 +000060#include <libutil.h>
blueswir16972f932008-11-22 20:49:12 +000061#include <dev/ppbus/ppi.h>
62#include <dev/ppbus/ppbconf.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010063#if defined(__GLIBC__)
64#include <pty.h>
65#endif
blueswir1c5e97232009-03-07 20:06:23 +000066#elif defined(__DragonFly__)
67#include <libutil.h>
68#include <dev/misc/ppi/ppi.h>
69#include <bus/ppbus/ppbconf.h>
blueswir124646c72008-11-07 16:55:48 +000070#else
71#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000072#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010073#else
aliguori6f97dba2008-10-31 18:49:55 +000074#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000075#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"
Alon Levycbcc6332011-01-19 10:49:50 +020099#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +0000100
Amit Shah9bd78542009-11-03 19:59:54 +0530101#define READ_BUF_LEN 4096
102
aliguori6f97dba2008-10-31 18:49:55 +0000103/***********************************************************/
104/* character device */
105
Blue Swirl72cf2d42009-09-12 07:36:22 +0000106static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
107 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000108
Hans de Goedea425d232011-11-19 10:22:43 +0100109void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000110{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200111 /* Keep track if the char device is open */
112 switch (event) {
113 case CHR_EVENT_OPENED:
114 s->opened = 1;
115 break;
116 case CHR_EVENT_CLOSED:
117 s->opened = 0;
118 break;
119 }
120
aliguori6f97dba2008-10-31 18:49:55 +0000121 if (!s->chr_event)
122 return;
123 s->chr_event(s->handler_opaque, event);
124}
125
Amit Shah127338e2009-11-03 19:59:56 +0530126static void qemu_chr_generic_open_bh(void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000127{
128 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100129 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000130 qemu_bh_delete(s->bh);
131 s->bh = NULL;
132}
133
Amit Shah127338e2009-11-03 19:59:56 +0530134void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000135{
Amit Shah69795d62009-10-07 18:31:15 +0530136 if (s->bh == NULL) {
Amit Shah127338e2009-11-03 19:59:56 +0530137 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000138 qemu_bh_schedule(s->bh);
139 }
140}
141
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500142int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000143{
144 return s->chr_write(s, buf, len);
145}
146
Anthony Liguori41084f12011-08-15 11:17:34 -0500147int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000148{
149 if (!s->chr_ioctl)
150 return -ENOTSUP;
151 return s->chr_ioctl(s, cmd, arg);
152}
153
Anthony Liguori909cda12011-08-15 11:17:31 -0500154int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000155{
156 if (!s->chr_can_read)
157 return 0;
158 return s->chr_can_read(s->handler_opaque);
159}
160
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500161void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000162{
163 s->chr_read(s->handler_opaque, buf, len);
164}
165
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500166int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100167{
168 return s->get_msgfd ? s->get_msgfd(s) : -1;
169}
170
Daniel P. Berrange13661082011-06-23 13:31:42 +0100171int qemu_chr_add_client(CharDriverState *s, int fd)
172{
173 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
174}
175
aliguori6f97dba2008-10-31 18:49:55 +0000176void qemu_chr_accept_input(CharDriverState *s)
177{
178 if (s->chr_accept_input)
179 s->chr_accept_input(s);
180}
181
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500182void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000183{
Amit Shah9bd78542009-11-03 19:59:54 +0530184 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000185 va_list ap;
186 va_start(ap, fmt);
187 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500188 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000189 va_end(ap);
190}
191
aliguori6f97dba2008-10-31 18:49:55 +0000192void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100193 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000194 IOReadHandler *fd_read,
195 IOEventHandler *fd_event,
196 void *opaque)
197{
Amit Shahda7d9982011-04-25 15:18:22 +0530198 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530199 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900200 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530201 }
aliguori6f97dba2008-10-31 18:49:55 +0000202 s->chr_can_read = fd_can_read;
203 s->chr_read = fd_read;
204 s->chr_event = fd_event;
205 s->handler_opaque = opaque;
206 if (s->chr_update_read_handler)
207 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200208
209 /* We're connecting to an already opened device, so let's make sure we
210 also get the open event */
211 if (s->opened) {
212 qemu_chr_generic_open(s);
213 }
aliguori6f97dba2008-10-31 18:49:55 +0000214}
215
216static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
217{
218 return len;
219}
220
Markus Armbruster1f514702012-02-07 15:09:08 +0100221static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000222{
223 CharDriverState *chr;
224
Anthony Liguori7267c092011-08-20 22:09:37 -0500225 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000226 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100227 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000228}
229
230/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000231#define MAX_MUX 4
232#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
233#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
234typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100235 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000236 IOReadHandler *chr_read[MAX_MUX];
237 IOEventHandler *chr_event[MAX_MUX];
238 void *ext_opaque[MAX_MUX];
239 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200240 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000241 int mux_cnt;
242 int term_got_escape;
243 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000244 /* Intermediate input buffer allows to catch escape sequences even if the
245 currently active device is not accepting any input - but only until it
246 is full as well. */
247 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
248 int prod[MAX_MUX];
249 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200250 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200251 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200252 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000253} MuxDriver;
254
255
256static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
257{
258 MuxDriver *d = chr->opaque;
259 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200260 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000261 ret = d->drv->chr_write(d->drv, buf, len);
262 } else {
263 int i;
264
265 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200266 for (i = 0; i < len; i++) {
267 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000268 char buf1[64];
269 int64_t ti;
270 int secs;
271
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100272 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200273 if (d->timestamps_start == -1)
274 d->timestamps_start = ti;
275 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000276 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000277 snprintf(buf1, sizeof(buf1),
278 "[%02d:%02d:%02d.%03d] ",
279 secs / 3600,
280 (secs / 60) % 60,
281 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000282 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000283 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200284 d->linestart = 0;
285 }
286 ret += d->drv->chr_write(d->drv, buf+i, 1);
287 if (buf[i] == '\n') {
288 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000289 }
290 }
291 }
292 return ret;
293}
294
295static const char * const mux_help[] = {
296 "% h print this help\n\r",
297 "% x exit emulator\n\r",
298 "% s save disk data back to file (if -snapshot)\n\r",
299 "% t toggle console timestamps\n\r"
300 "% b send break (magic sysrq)\n\r",
301 "% c switch between console and monitor\n\r",
302 "% % sends %\n\r",
303 NULL
304};
305
306int term_escape_char = 0x01; /* ctrl-a is used for escape */
307static void mux_print_help(CharDriverState *chr)
308{
309 int i, j;
310 char ebuf[15] = "Escape-Char";
311 char cbuf[50] = "\n\r";
312
313 if (term_escape_char > 0 && term_escape_char < 26) {
314 snprintf(cbuf, sizeof(cbuf), "\n\r");
315 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
316 } else {
317 snprintf(cbuf, sizeof(cbuf),
318 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
319 term_escape_char);
320 }
321 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
322 for (i = 0; mux_help[i] != NULL; i++) {
323 for (j=0; mux_help[i][j] != '\0'; j++) {
324 if (mux_help[i][j] == '%')
325 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
326 else
327 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
328 }
329 }
330}
331
aliguori2724b182009-03-05 23:01:47 +0000332static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
333{
334 if (d->chr_event[mux_nr])
335 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
336}
337
aliguori6f97dba2008-10-31 18:49:55 +0000338static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
339{
340 if (d->term_got_escape) {
341 d->term_got_escape = 0;
342 if (ch == term_escape_char)
343 goto send_char;
344 switch(ch) {
345 case '?':
346 case 'h':
347 mux_print_help(chr);
348 break;
349 case 'x':
350 {
351 const char *term = "QEMU: Terminated\n\r";
352 chr->chr_write(chr,(uint8_t *)term,strlen(term));
353 exit(0);
354 break;
355 }
356 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200357 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000358 break;
359 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100360 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000361 break;
362 case 'c':
363 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200364 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
365 d->focus++;
366 if (d->focus >= d->mux_cnt)
367 d->focus = 0;
368 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000369 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200370 case 't':
371 d->timestamps = !d->timestamps;
372 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200373 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200374 break;
aliguori6f97dba2008-10-31 18:49:55 +0000375 }
376 } else if (ch == term_escape_char) {
377 d->term_got_escape = 1;
378 } else {
379 send_char:
380 return 1;
381 }
382 return 0;
383}
384
385static void mux_chr_accept_input(CharDriverState *chr)
386{
aliguori6f97dba2008-10-31 18:49:55 +0000387 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200388 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000389
aliguoria80bf992009-03-05 23:00:02 +0000390 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000391 d->chr_can_read[m] &&
392 d->chr_can_read[m](d->ext_opaque[m])) {
393 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000394 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000395 }
396}
397
398static int mux_chr_can_read(void *opaque)
399{
400 CharDriverState *chr = opaque;
401 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200402 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000403
aliguoria80bf992009-03-05 23:00:02 +0000404 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000405 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000406 if (d->chr_can_read[m])
407 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000408 return 0;
409}
410
411static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
412{
413 CharDriverState *chr = opaque;
414 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200415 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000416 int i;
417
418 mux_chr_accept_input (opaque);
419
420 for(i = 0; i < size; i++)
421 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000422 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000423 d->chr_can_read[m] &&
424 d->chr_can_read[m](d->ext_opaque[m]))
425 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
426 else
aliguoria80bf992009-03-05 23:00:02 +0000427 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000428 }
429}
430
431static void mux_chr_event(void *opaque, int event)
432{
433 CharDriverState *chr = opaque;
434 MuxDriver *d = chr->opaque;
435 int i;
436
437 /* Send the event to all registered listeners */
438 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000439 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000440}
441
442static void mux_chr_update_read_handler(CharDriverState *chr)
443{
444 MuxDriver *d = chr->opaque;
445
446 if (d->mux_cnt >= MAX_MUX) {
447 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
448 return;
449 }
450 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
451 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
452 d->chr_read[d->mux_cnt] = chr->chr_read;
453 d->chr_event[d->mux_cnt] = chr->chr_event;
454 /* Fix up the real driver with mux routines */
455 if (d->mux_cnt == 0) {
456 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
457 mux_chr_event, chr);
458 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200459 if (d->focus != -1) {
460 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200461 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200462 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000463 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200464 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000465}
466
467static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
468{
469 CharDriverState *chr;
470 MuxDriver *d;
471
Anthony Liguori7267c092011-08-20 22:09:37 -0500472 chr = g_malloc0(sizeof(CharDriverState));
473 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000474
475 chr->opaque = d;
476 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200477 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000478 chr->chr_write = mux_chr_write;
479 chr->chr_update_read_handler = mux_chr_update_read_handler;
480 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100481 /* Frontend guest-open / -close notification is not support with muxes */
482 chr->chr_guest_open = NULL;
483 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200484
485 /* Muxes are always open on creation */
486 qemu_chr_generic_open(chr);
487
aliguori6f97dba2008-10-31 18:49:55 +0000488 return chr;
489}
490
491
492#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000493int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000494{
495 int ret, len;
496
497 len = len1;
498 while (len > 0) {
499 ret = send(fd, buf, len, 0);
500 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000501 errno = WSAGetLastError();
502 if (errno != WSAEWOULDBLOCK) {
503 return -1;
504 }
505 } else if (ret == 0) {
506 break;
507 } else {
508 buf += ret;
509 len -= ret;
510 }
511 }
512 return len1 - len;
513}
514
515#else
516
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100517int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000518{
519 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100520 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000521
522 len = len1;
523 while (len > 0) {
524 ret = write(fd, buf, len);
525 if (ret < 0) {
526 if (errno != EINTR && errno != EAGAIN)
527 return -1;
528 } else if (ret == 0) {
529 break;
530 } else {
531 buf += ret;
532 len -= ret;
533 }
534 }
535 return len1 - len;
536}
aliguori6f97dba2008-10-31 18:49:55 +0000537#endif /* !_WIN32 */
538
Fabien Chouteaudb418a02011-10-06 16:37:51 +0200539#define STDIO_MAX_CLIENTS 1
540static int stdio_nb_clients;
541
aliguori6f97dba2008-10-31 18:49:55 +0000542#ifndef _WIN32
543
544typedef struct {
545 int fd_in, fd_out;
546 int max_size;
547} FDCharDriver;
548
aliguori6f97dba2008-10-31 18:49:55 +0000549
550static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
551{
552 FDCharDriver *s = chr->opaque;
553 return send_all(s->fd_out, buf, len);
554}
555
556static int fd_chr_read_poll(void *opaque)
557{
558 CharDriverState *chr = opaque;
559 FDCharDriver *s = chr->opaque;
560
Anthony Liguori909cda12011-08-15 11:17:31 -0500561 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000562 return s->max_size;
563}
564
565static void fd_chr_read(void *opaque)
566{
567 CharDriverState *chr = opaque;
568 FDCharDriver *s = chr->opaque;
569 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530570 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000571
572 len = sizeof(buf);
573 if (len > s->max_size)
574 len = s->max_size;
575 if (len == 0)
576 return;
577 size = read(s->fd_in, buf, len);
578 if (size == 0) {
579 /* FD has been closed. Remove it from the active list. */
580 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100581 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000582 return;
583 }
584 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500585 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000586 }
587}
588
589static void fd_chr_update_read_handler(CharDriverState *chr)
590{
591 FDCharDriver *s = chr->opaque;
592
593 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500594 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000595 } else {
596 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
597 fd_chr_read, NULL, chr);
598 }
599 }
600}
601
602static void fd_chr_close(struct CharDriverState *chr)
603{
604 FDCharDriver *s = chr->opaque;
605
606 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500607 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000608 } else {
609 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
610 }
611 }
612
Anthony Liguori7267c092011-08-20 22:09:37 -0500613 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100614 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000615}
616
617/* open a character device to a unix fd */
618static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
619{
620 CharDriverState *chr;
621 FDCharDriver *s;
622
Anthony Liguori7267c092011-08-20 22:09:37 -0500623 chr = g_malloc0(sizeof(CharDriverState));
624 s = g_malloc0(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000625 s->fd_in = fd_in;
626 s->fd_out = fd_out;
627 chr->opaque = s;
628 chr->chr_write = fd_chr_write;
629 chr->chr_update_read_handler = fd_chr_update_read_handler;
630 chr->chr_close = fd_chr_close;
631
Amit Shah127338e2009-11-03 19:59:56 +0530632 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000633
634 return chr;
635}
636
Markus Armbruster1f514702012-02-07 15:09:08 +0100637static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000638{
639 int fd_out;
640
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100641 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200642 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100643 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100644 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100645 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100646 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000647}
648
Markus Armbruster1f514702012-02-07 15:09:08 +0100649static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000650{
651 int fd_in, fd_out;
652 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200653 const char *filename = qemu_opt_get(opts, "path");
654
655 if (filename == NULL) {
656 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100657 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200658 }
aliguori6f97dba2008-10-31 18:49:55 +0000659
660 snprintf(filename_in, 256, "%s.in", filename);
661 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100662 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
663 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000664 if (fd_in < 0 || fd_out < 0) {
665 if (fd_in >= 0)
666 close(fd_in);
667 if (fd_out >= 0)
668 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100669 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100670 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100671 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100672 }
aliguori6f97dba2008-10-31 18:49:55 +0000673 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100674 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000675}
676
677
678/* for STDIO, we handle the case where several clients use it
679 (nographic mode) */
680
681#define TERM_FIFO_MAX_SIZE 1
682
683static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
684static int term_fifo_size;
685
686static int stdio_read_poll(void *opaque)
687{
688 CharDriverState *chr = opaque;
689
690 /* try to flush the queue if needed */
Anthony Liguori909cda12011-08-15 11:17:31 -0500691 if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500692 qemu_chr_be_write(chr, term_fifo, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000693 term_fifo_size = 0;
694 }
695 /* see if we can absorb more chars */
696 if (term_fifo_size == 0)
697 return 1;
698 else
699 return 0;
700}
701
702static void stdio_read(void *opaque)
703{
704 int size;
705 uint8_t buf[1];
706 CharDriverState *chr = opaque;
707
708 size = read(0, buf, 1);
709 if (size == 0) {
710 /* stdin has been closed. Remove it from the active list. */
711 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100712 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000713 return;
714 }
715 if (size > 0) {
Anthony Liguori909cda12011-08-15 11:17:31 -0500716 if (qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500717 qemu_chr_be_write(chr, buf, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000718 } else if (term_fifo_size == 0) {
719 term_fifo[term_fifo_size++] = buf[0];
720 }
721 }
722}
723
724/* init terminal so that we can grab keys */
725static struct termios oldtty;
726static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100727static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000728
729static void term_exit(void)
730{
731 tcsetattr (0, TCSANOW, &oldtty);
732 fcntl(0, F_SETFL, old_fd0_flags);
733}
734
Paolo Bonzinibb002512010-12-23 13:42:50 +0100735static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000736{
737 struct termios tty;
738
Paolo Bonzini03693642010-12-23 13:42:49 +0100739 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100740 if (!echo) {
741 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000742 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100743 tty.c_oflag |= OPOST;
744 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
745 tty.c_cflag &= ~(CSIZE|PARENB);
746 tty.c_cflag |= CS8;
747 tty.c_cc[VMIN] = 1;
748 tty.c_cc[VTIME] = 0;
749 }
aliguori6f97dba2008-10-31 18:49:55 +0000750 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100751 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000752 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000753
754 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000755}
756
757static void qemu_chr_close_stdio(struct CharDriverState *chr)
758{
759 term_exit();
760 stdio_nb_clients--;
761 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
762 fd_chr_close(chr);
763}
764
Markus Armbruster1f514702012-02-07 15:09:08 +0100765static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000766{
767 CharDriverState *chr;
768
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100769 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100770 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100771 }
Paolo Bonzini03693642010-12-23 13:42:49 +0100772 if (stdio_nb_clients == 0) {
773 old_fd0_flags = fcntl(0, F_GETFL);
774 tcgetattr (0, &oldtty);
775 fcntl(0, F_SETFL, O_NONBLOCK);
776 atexit(term_exit);
777 }
778
aliguori6f97dba2008-10-31 18:49:55 +0000779 chr = qemu_chr_open_fd(0, 1);
780 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100781 chr->chr_set_echo = qemu_chr_set_echo_stdio;
aliguori6f97dba2008-10-31 18:49:55 +0000782 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
783 stdio_nb_clients++;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100784 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
785 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500786 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000787
Markus Armbruster1f514702012-02-07 15:09:08 +0100788 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000789}
790
791#ifdef __sun__
792/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000793static int openpty(int *amaster, int *aslave, char *name,
794 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000795{
796 const char *slave;
797 int mfd = -1, sfd = -1;
798
799 *amaster = *aslave = -1;
800
801 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
802 if (mfd < 0)
803 goto err;
804
805 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
806 goto err;
807
808 if ((slave = ptsname(mfd)) == NULL)
809 goto err;
810
811 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
812 goto err;
813
814 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
815 (termp != NULL && tcgetattr(sfd, termp) < 0))
816 goto err;
817
818 if (amaster)
819 *amaster = mfd;
820 if (aslave)
821 *aslave = sfd;
822 if (winp)
823 ioctl(sfd, TIOCSWINSZ, winp);
824
825 return 0;
826
827err:
828 if (sfd != -1)
829 close(sfd);
830 close(mfd);
831 return -1;
832}
833
blueswir13f4cb3d2009-04-13 16:31:01 +0000834static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000835{
836 termios_p->c_iflag &=
837 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
838 termios_p->c_oflag &= ~OPOST;
839 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
840 termios_p->c_cflag &= ~(CSIZE|PARENB);
841 termios_p->c_cflag |= CS8;
842
843 termios_p->c_cc[VMIN] = 0;
844 termios_p->c_cc[VTIME] = 0;
845}
846#endif
847
848#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100849 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
850 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000851
852typedef struct {
853 int fd;
854 int connected;
855 int polling;
856 int read_bytes;
857 QEMUTimer *timer;
858} PtyCharDriver;
859
860static void pty_chr_update_read_handler(CharDriverState *chr);
861static void pty_chr_state(CharDriverState *chr, int connected);
862
863static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
864{
865 PtyCharDriver *s = chr->opaque;
866
867 if (!s->connected) {
868 /* guest sends data, check for (re-)connect */
869 pty_chr_update_read_handler(chr);
870 return 0;
871 }
872 return send_all(s->fd, buf, len);
873}
874
875static int pty_chr_read_poll(void *opaque)
876{
877 CharDriverState *chr = opaque;
878 PtyCharDriver *s = chr->opaque;
879
Anthony Liguori909cda12011-08-15 11:17:31 -0500880 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000881 return s->read_bytes;
882}
883
884static void pty_chr_read(void *opaque)
885{
886 CharDriverState *chr = opaque;
887 PtyCharDriver *s = chr->opaque;
888 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530889 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000890
891 len = sizeof(buf);
892 if (len > s->read_bytes)
893 len = s->read_bytes;
894 if (len == 0)
895 return;
896 size = read(s->fd, buf, len);
897 if ((size == -1 && errno == EIO) ||
898 (size == 0)) {
899 pty_chr_state(chr, 0);
900 return;
901 }
902 if (size > 0) {
903 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500904 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000905 }
906}
907
908static void pty_chr_update_read_handler(CharDriverState *chr)
909{
910 PtyCharDriver *s = chr->opaque;
911
912 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
913 pty_chr_read, NULL, chr);
914 s->polling = 1;
915 /*
916 * Short timeout here: just need wait long enougth that qemu makes
917 * it through the poll loop once. When reconnected we want a
918 * short timeout so we notice it almost instantly. Otherwise
919 * read() gives us -EIO instantly, making pty_chr_state() reset the
920 * timeout to the normal (much longer) poll interval before the
921 * timer triggers.
922 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100923 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +0000924}
925
926static void pty_chr_state(CharDriverState *chr, int connected)
927{
928 PtyCharDriver *s = chr->opaque;
929
930 if (!connected) {
931 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
932 s->connected = 0;
933 s->polling = 0;
934 /* (re-)connect poll interval for idle guests: once per second.
935 * We check more frequently in case the guests sends data to
936 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100937 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +0000938 } else {
939 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530940 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000941 s->connected = 1;
942 }
943}
944
945static void pty_chr_timer(void *opaque)
946{
947 struct CharDriverState *chr = opaque;
948 PtyCharDriver *s = chr->opaque;
949
950 if (s->connected)
951 return;
952 if (s->polling) {
953 /* If we arrive here without polling being cleared due
954 * read returning -EIO, then we are (re-)connected */
955 pty_chr_state(chr, 1);
956 return;
957 }
958
959 /* Next poll ... */
960 pty_chr_update_read_handler(chr);
961}
962
963static void pty_chr_close(struct CharDriverState *chr)
964{
965 PtyCharDriver *s = chr->opaque;
966
967 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
968 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000969 qemu_del_timer(s->timer);
970 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -0500971 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100972 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000973}
974
Markus Armbruster1f514702012-02-07 15:09:08 +0100975static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000976{
977 CharDriverState *chr;
978 PtyCharDriver *s;
979 struct termios tty;
Markus Armbrustera4e26042011-11-11 10:40:05 +0100980 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000981#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000982 char pty_name[PATH_MAX];
983#define q_ptsname(x) pty_name
984#else
985 char *pty_name = NULL;
986#define q_ptsname(x) ptsname(x)
987#endif
988
Markus Armbrustera4e26042011-11-11 10:40:05 +0100989 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100990 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000991 }
992
993 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +0000994 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000995 cfmakeraw(&tty);
996 tcsetattr(slave_fd, TCSAFLUSH, &tty);
997 close(slave_fd);
998
Markus Armbrustera4e26042011-11-11 10:40:05 +0100999 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001000
Markus Armbrustera4e26042011-11-11 10:40:05 +01001001 len = strlen(q_ptsname(master_fd)) + 5;
1002 chr->filename = g_malloc(len);
1003 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1004 qemu_opt_set(opts, "path", q_ptsname(master_fd));
1005 fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
1006
1007 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001008 chr->opaque = s;
1009 chr->chr_write = pty_chr_write;
1010 chr->chr_update_read_handler = pty_chr_update_read_handler;
1011 chr->chr_close = pty_chr_close;
1012
Markus Armbrustera4e26042011-11-11 10:40:05 +01001013 s->fd = master_fd;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001014 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001015
Markus Armbruster1f514702012-02-07 15:09:08 +01001016 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001017}
1018
1019static void tty_serial_init(int fd, int speed,
1020 int parity, int data_bits, int stop_bits)
1021{
1022 struct termios tty;
1023 speed_t spd;
1024
1025#if 0
1026 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1027 speed, parity, data_bits, stop_bits);
1028#endif
1029 tcgetattr (fd, &tty);
1030
Stefan Weil45eea132009-10-26 16:10:10 +01001031#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1032 speed = speed * 10 / 11;
1033 do {
1034 check_speed(50);
1035 check_speed(75);
1036 check_speed(110);
1037 check_speed(134);
1038 check_speed(150);
1039 check_speed(200);
1040 check_speed(300);
1041 check_speed(600);
1042 check_speed(1200);
1043 check_speed(1800);
1044 check_speed(2400);
1045 check_speed(4800);
1046 check_speed(9600);
1047 check_speed(19200);
1048 check_speed(38400);
1049 /* Non-Posix values follow. They may be unsupported on some systems. */
1050 check_speed(57600);
1051 check_speed(115200);
1052#ifdef B230400
1053 check_speed(230400);
1054#endif
1055#ifdef B460800
1056 check_speed(460800);
1057#endif
1058#ifdef B500000
1059 check_speed(500000);
1060#endif
1061#ifdef B576000
1062 check_speed(576000);
1063#endif
1064#ifdef B921600
1065 check_speed(921600);
1066#endif
1067#ifdef B1000000
1068 check_speed(1000000);
1069#endif
1070#ifdef B1152000
1071 check_speed(1152000);
1072#endif
1073#ifdef B1500000
1074 check_speed(1500000);
1075#endif
1076#ifdef B2000000
1077 check_speed(2000000);
1078#endif
1079#ifdef B2500000
1080 check_speed(2500000);
1081#endif
1082#ifdef B3000000
1083 check_speed(3000000);
1084#endif
1085#ifdef B3500000
1086 check_speed(3500000);
1087#endif
1088#ifdef B4000000
1089 check_speed(4000000);
1090#endif
aliguori6f97dba2008-10-31 18:49:55 +00001091 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001092 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001093
1094 cfsetispeed(&tty, spd);
1095 cfsetospeed(&tty, spd);
1096
1097 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1098 |INLCR|IGNCR|ICRNL|IXON);
1099 tty.c_oflag |= OPOST;
1100 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1101 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1102 switch(data_bits) {
1103 default:
1104 case 8:
1105 tty.c_cflag |= CS8;
1106 break;
1107 case 7:
1108 tty.c_cflag |= CS7;
1109 break;
1110 case 6:
1111 tty.c_cflag |= CS6;
1112 break;
1113 case 5:
1114 tty.c_cflag |= CS5;
1115 break;
1116 }
1117 switch(parity) {
1118 default:
1119 case 'N':
1120 break;
1121 case 'E':
1122 tty.c_cflag |= PARENB;
1123 break;
1124 case 'O':
1125 tty.c_cflag |= PARENB | PARODD;
1126 break;
1127 }
1128 if (stop_bits == 2)
1129 tty.c_cflag |= CSTOPB;
1130
1131 tcsetattr (fd, TCSANOW, &tty);
1132}
1133
1134static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1135{
1136 FDCharDriver *s = chr->opaque;
1137
1138 switch(cmd) {
1139 case CHR_IOCTL_SERIAL_SET_PARAMS:
1140 {
1141 QEMUSerialSetParams *ssp = arg;
1142 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1143 ssp->data_bits, ssp->stop_bits);
1144 }
1145 break;
1146 case CHR_IOCTL_SERIAL_SET_BREAK:
1147 {
1148 int enable = *(int *)arg;
1149 if (enable)
1150 tcsendbreak(s->fd_in, 1);
1151 }
1152 break;
1153 case CHR_IOCTL_SERIAL_GET_TIOCM:
1154 {
1155 int sarg = 0;
1156 int *targ = (int *)arg;
1157 ioctl(s->fd_in, TIOCMGET, &sarg);
1158 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001159 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001160 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001161 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001162 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001163 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001164 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001165 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001166 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001167 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001168 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001169 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001170 *targ |= CHR_TIOCM_RTS;
1171 }
1172 break;
1173 case CHR_IOCTL_SERIAL_SET_TIOCM:
1174 {
1175 int sarg = *(int *)arg;
1176 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001177 ioctl(s->fd_in, TIOCMGET, &targ);
1178 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1179 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1180 if (sarg & CHR_TIOCM_CTS)
1181 targ |= TIOCM_CTS;
1182 if (sarg & CHR_TIOCM_CAR)
1183 targ |= TIOCM_CAR;
1184 if (sarg & CHR_TIOCM_DSR)
1185 targ |= TIOCM_DSR;
1186 if (sarg & CHR_TIOCM_RI)
1187 targ |= TIOCM_RI;
1188 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001189 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001190 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001191 targ |= TIOCM_RTS;
1192 ioctl(s->fd_in, TIOCMSET, &targ);
1193 }
1194 break;
1195 default:
1196 return -ENOTSUP;
1197 }
1198 return 0;
1199}
1200
David Ahern4266a132010-02-10 18:27:17 -07001201static void qemu_chr_close_tty(CharDriverState *chr)
1202{
1203 FDCharDriver *s = chr->opaque;
1204 int fd = -1;
1205
1206 if (s) {
1207 fd = s->fd_in;
1208 }
1209
1210 fd_chr_close(chr);
1211
1212 if (fd >= 0) {
1213 close(fd);
1214 }
1215}
1216
Markus Armbruster1f514702012-02-07 15:09:08 +01001217static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001218{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001219 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001220 CharDriverState *chr;
1221 int fd;
1222
Markus Armbrusterb181e042012-02-07 15:09:09 +01001223 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001224 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001225 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001226 }
aliguori6f97dba2008-10-31 18:49:55 +00001227 tty_serial_init(fd, 115200, 'N', 8, 1);
1228 chr = qemu_chr_open_fd(fd, fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001229 if (!chr) {
1230 close(fd);
1231 return NULL;
1232 }
aliguori6f97dba2008-10-31 18:49:55 +00001233 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001234 chr->chr_close = qemu_chr_close_tty;
Markus Armbruster1f514702012-02-07 15:09:08 +01001235 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001236}
1237#else /* ! __linux__ && ! __sun__ */
Markus Armbruster1f514702012-02-07 15:09:08 +01001238static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001239{
Markus Armbruster1f514702012-02-07 15:09:08 +01001240 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001241}
1242#endif /* __linux__ || __sun__ */
1243
1244#if defined(__linux__)
1245typedef struct {
1246 int fd;
1247 int mode;
1248} ParallelCharDriver;
1249
1250static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1251{
1252 if (s->mode != mode) {
1253 int m = mode;
1254 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1255 return 0;
1256 s->mode = mode;
1257 }
1258 return 1;
1259}
1260
1261static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1262{
1263 ParallelCharDriver *drv = chr->opaque;
1264 int fd = drv->fd;
1265 uint8_t b;
1266
1267 switch(cmd) {
1268 case CHR_IOCTL_PP_READ_DATA:
1269 if (ioctl(fd, PPRDATA, &b) < 0)
1270 return -ENOTSUP;
1271 *(uint8_t *)arg = b;
1272 break;
1273 case CHR_IOCTL_PP_WRITE_DATA:
1274 b = *(uint8_t *)arg;
1275 if (ioctl(fd, PPWDATA, &b) < 0)
1276 return -ENOTSUP;
1277 break;
1278 case CHR_IOCTL_PP_READ_CONTROL:
1279 if (ioctl(fd, PPRCONTROL, &b) < 0)
1280 return -ENOTSUP;
1281 /* Linux gives only the lowest bits, and no way to know data
1282 direction! For better compatibility set the fixed upper
1283 bits. */
1284 *(uint8_t *)arg = b | 0xc0;
1285 break;
1286 case CHR_IOCTL_PP_WRITE_CONTROL:
1287 b = *(uint8_t *)arg;
1288 if (ioctl(fd, PPWCONTROL, &b) < 0)
1289 return -ENOTSUP;
1290 break;
1291 case CHR_IOCTL_PP_READ_STATUS:
1292 if (ioctl(fd, PPRSTATUS, &b) < 0)
1293 return -ENOTSUP;
1294 *(uint8_t *)arg = b;
1295 break;
1296 case CHR_IOCTL_PP_DATA_DIR:
1297 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1298 return -ENOTSUP;
1299 break;
1300 case CHR_IOCTL_PP_EPP_READ_ADDR:
1301 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1302 struct ParallelIOArg *parg = arg;
1303 int n = read(fd, parg->buffer, parg->count);
1304 if (n != parg->count) {
1305 return -EIO;
1306 }
1307 }
1308 break;
1309 case CHR_IOCTL_PP_EPP_READ:
1310 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1311 struct ParallelIOArg *parg = arg;
1312 int n = read(fd, parg->buffer, parg->count);
1313 if (n != parg->count) {
1314 return -EIO;
1315 }
1316 }
1317 break;
1318 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1319 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1320 struct ParallelIOArg *parg = arg;
1321 int n = write(fd, parg->buffer, parg->count);
1322 if (n != parg->count) {
1323 return -EIO;
1324 }
1325 }
1326 break;
1327 case CHR_IOCTL_PP_EPP_WRITE:
1328 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1329 struct ParallelIOArg *parg = arg;
1330 int n = write(fd, parg->buffer, parg->count);
1331 if (n != parg->count) {
1332 return -EIO;
1333 }
1334 }
1335 break;
1336 default:
1337 return -ENOTSUP;
1338 }
1339 return 0;
1340}
1341
1342static void pp_close(CharDriverState *chr)
1343{
1344 ParallelCharDriver *drv = chr->opaque;
1345 int fd = drv->fd;
1346
1347 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1348 ioctl(fd, PPRELEASE);
1349 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001350 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001351 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001352}
1353
Markus Armbruster1f514702012-02-07 15:09:08 +01001354static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001355{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001356 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001357 CharDriverState *chr;
1358 ParallelCharDriver *drv;
1359 int fd;
1360
Markus Armbrusterb181e042012-02-07 15:09:09 +01001361 TFR(fd = qemu_open(filename, O_RDWR));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001362 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001363 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001364 }
aliguori6f97dba2008-10-31 18:49:55 +00001365
1366 if (ioctl(fd, PPCLAIM) < 0) {
1367 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001368 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001369 }
1370
Anthony Liguori7267c092011-08-20 22:09:37 -05001371 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001372 drv->fd = fd;
1373 drv->mode = IEEE1284_MODE_COMPAT;
1374
Anthony Liguori7267c092011-08-20 22:09:37 -05001375 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001376 chr->chr_write = null_chr_write;
1377 chr->chr_ioctl = pp_ioctl;
1378 chr->chr_close = pp_close;
1379 chr->opaque = drv;
1380
Amit Shah127338e2009-11-03 19:59:56 +05301381 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001382
Markus Armbruster1f514702012-02-07 15:09:08 +01001383 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001384}
1385#endif /* __linux__ */
1386
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001387#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001388static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1389{
Stefan Weile0efb992011-02-23 19:09:16 +01001390 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001391 uint8_t b;
1392
1393 switch(cmd) {
1394 case CHR_IOCTL_PP_READ_DATA:
1395 if (ioctl(fd, PPIGDATA, &b) < 0)
1396 return -ENOTSUP;
1397 *(uint8_t *)arg = b;
1398 break;
1399 case CHR_IOCTL_PP_WRITE_DATA:
1400 b = *(uint8_t *)arg;
1401 if (ioctl(fd, PPISDATA, &b) < 0)
1402 return -ENOTSUP;
1403 break;
1404 case CHR_IOCTL_PP_READ_CONTROL:
1405 if (ioctl(fd, PPIGCTRL, &b) < 0)
1406 return -ENOTSUP;
1407 *(uint8_t *)arg = b;
1408 break;
1409 case CHR_IOCTL_PP_WRITE_CONTROL:
1410 b = *(uint8_t *)arg;
1411 if (ioctl(fd, PPISCTRL, &b) < 0)
1412 return -ENOTSUP;
1413 break;
1414 case CHR_IOCTL_PP_READ_STATUS:
1415 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1416 return -ENOTSUP;
1417 *(uint8_t *)arg = b;
1418 break;
1419 default:
1420 return -ENOTSUP;
1421 }
1422 return 0;
1423}
1424
Markus Armbruster1f514702012-02-07 15:09:08 +01001425static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001426{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001427 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001428 CharDriverState *chr;
1429 int fd;
1430
Markus Armbrusterb181e042012-02-07 15:09:09 +01001431 fd = qemu_open(filename, O_RDWR);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001432 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001433 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001434 }
blueswir16972f932008-11-22 20:49:12 +00001435
Anthony Liguori7267c092011-08-20 22:09:37 -05001436 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001437 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001438 chr->chr_write = null_chr_write;
1439 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001440 return chr;
blueswir16972f932008-11-22 20:49:12 +00001441}
1442#endif
1443
aliguori6f97dba2008-10-31 18:49:55 +00001444#else /* _WIN32 */
1445
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001446static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1447
aliguori6f97dba2008-10-31 18:49:55 +00001448typedef struct {
1449 int max_size;
1450 HANDLE hcom, hrecv, hsend;
1451 OVERLAPPED orecv, osend;
1452 BOOL fpipe;
1453 DWORD len;
1454} WinCharState;
1455
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001456typedef struct {
1457 HANDLE hStdIn;
1458 HANDLE hInputReadyEvent;
1459 HANDLE hInputDoneEvent;
1460 HANDLE hInputThread;
1461 uint8_t win_stdio_buf;
1462} WinStdioCharState;
1463
aliguori6f97dba2008-10-31 18:49:55 +00001464#define NSENDBUF 2048
1465#define NRECVBUF 2048
1466#define MAXCONNECT 1
1467#define NTIMEOUT 5000
1468
1469static int win_chr_poll(void *opaque);
1470static int win_chr_pipe_poll(void *opaque);
1471
1472static void win_chr_close(CharDriverState *chr)
1473{
1474 WinCharState *s = chr->opaque;
1475
1476 if (s->hsend) {
1477 CloseHandle(s->hsend);
1478 s->hsend = NULL;
1479 }
1480 if (s->hrecv) {
1481 CloseHandle(s->hrecv);
1482 s->hrecv = NULL;
1483 }
1484 if (s->hcom) {
1485 CloseHandle(s->hcom);
1486 s->hcom = NULL;
1487 }
1488 if (s->fpipe)
1489 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1490 else
1491 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301492
Hans de Goedea425d232011-11-19 10:22:43 +01001493 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001494}
1495
1496static int win_chr_init(CharDriverState *chr, const char *filename)
1497{
1498 WinCharState *s = chr->opaque;
1499 COMMCONFIG comcfg;
1500 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1501 COMSTAT comstat;
1502 DWORD size;
1503 DWORD err;
1504
1505 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1506 if (!s->hsend) {
1507 fprintf(stderr, "Failed CreateEvent\n");
1508 goto fail;
1509 }
1510 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1511 if (!s->hrecv) {
1512 fprintf(stderr, "Failed CreateEvent\n");
1513 goto fail;
1514 }
1515
1516 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1517 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1518 if (s->hcom == INVALID_HANDLE_VALUE) {
1519 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1520 s->hcom = NULL;
1521 goto fail;
1522 }
1523
1524 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1525 fprintf(stderr, "Failed SetupComm\n");
1526 goto fail;
1527 }
1528
1529 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1530 size = sizeof(COMMCONFIG);
1531 GetDefaultCommConfig(filename, &comcfg, &size);
1532 comcfg.dcb.DCBlength = sizeof(DCB);
1533 CommConfigDialog(filename, NULL, &comcfg);
1534
1535 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1536 fprintf(stderr, "Failed SetCommState\n");
1537 goto fail;
1538 }
1539
1540 if (!SetCommMask(s->hcom, EV_ERR)) {
1541 fprintf(stderr, "Failed SetCommMask\n");
1542 goto fail;
1543 }
1544
1545 cto.ReadIntervalTimeout = MAXDWORD;
1546 if (!SetCommTimeouts(s->hcom, &cto)) {
1547 fprintf(stderr, "Failed SetCommTimeouts\n");
1548 goto fail;
1549 }
1550
1551 if (!ClearCommError(s->hcom, &err, &comstat)) {
1552 fprintf(stderr, "Failed ClearCommError\n");
1553 goto fail;
1554 }
1555 qemu_add_polling_cb(win_chr_poll, chr);
1556 return 0;
1557
1558 fail:
1559 win_chr_close(chr);
1560 return -1;
1561}
1562
1563static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1564{
1565 WinCharState *s = chr->opaque;
1566 DWORD len, ret, size, err;
1567
1568 len = len1;
1569 ZeroMemory(&s->osend, sizeof(s->osend));
1570 s->osend.hEvent = s->hsend;
1571 while (len > 0) {
1572 if (s->hsend)
1573 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1574 else
1575 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1576 if (!ret) {
1577 err = GetLastError();
1578 if (err == ERROR_IO_PENDING) {
1579 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1580 if (ret) {
1581 buf += size;
1582 len -= size;
1583 } else {
1584 break;
1585 }
1586 } else {
1587 break;
1588 }
1589 } else {
1590 buf += size;
1591 len -= size;
1592 }
1593 }
1594 return len1 - len;
1595}
1596
1597static int win_chr_read_poll(CharDriverState *chr)
1598{
1599 WinCharState *s = chr->opaque;
1600
Anthony Liguori909cda12011-08-15 11:17:31 -05001601 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001602 return s->max_size;
1603}
1604
1605static void win_chr_readfile(CharDriverState *chr)
1606{
1607 WinCharState *s = chr->opaque;
1608 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301609 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001610 DWORD size;
1611
1612 ZeroMemory(&s->orecv, sizeof(s->orecv));
1613 s->orecv.hEvent = s->hrecv;
1614 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1615 if (!ret) {
1616 err = GetLastError();
1617 if (err == ERROR_IO_PENDING) {
1618 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1619 }
1620 }
1621
1622 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001623 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001624 }
1625}
1626
1627static void win_chr_read(CharDriverState *chr)
1628{
1629 WinCharState *s = chr->opaque;
1630
1631 if (s->len > s->max_size)
1632 s->len = s->max_size;
1633 if (s->len == 0)
1634 return;
1635
1636 win_chr_readfile(chr);
1637}
1638
1639static int win_chr_poll(void *opaque)
1640{
1641 CharDriverState *chr = opaque;
1642 WinCharState *s = chr->opaque;
1643 COMSTAT status;
1644 DWORD comerr;
1645
1646 ClearCommError(s->hcom, &comerr, &status);
1647 if (status.cbInQue > 0) {
1648 s->len = status.cbInQue;
1649 win_chr_read_poll(chr);
1650 win_chr_read(chr);
1651 return 1;
1652 }
1653 return 0;
1654}
1655
Markus Armbruster1f514702012-02-07 15:09:08 +01001656static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001657{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001658 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001659 CharDriverState *chr;
1660 WinCharState *s;
1661
Anthony Liguori7267c092011-08-20 22:09:37 -05001662 chr = g_malloc0(sizeof(CharDriverState));
1663 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001664 chr->opaque = s;
1665 chr->chr_write = win_chr_write;
1666 chr->chr_close = win_chr_close;
1667
1668 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001669 g_free(s);
1670 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001671 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001672 }
Amit Shah127338e2009-11-03 19:59:56 +05301673 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001674 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001675}
1676
1677static int win_chr_pipe_poll(void *opaque)
1678{
1679 CharDriverState *chr = opaque;
1680 WinCharState *s = chr->opaque;
1681 DWORD size;
1682
1683 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1684 if (size > 0) {
1685 s->len = size;
1686 win_chr_read_poll(chr);
1687 win_chr_read(chr);
1688 return 1;
1689 }
1690 return 0;
1691}
1692
1693static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1694{
1695 WinCharState *s = chr->opaque;
1696 OVERLAPPED ov;
1697 int ret;
1698 DWORD size;
1699 char openname[256];
1700
1701 s->fpipe = TRUE;
1702
1703 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1704 if (!s->hsend) {
1705 fprintf(stderr, "Failed CreateEvent\n");
1706 goto fail;
1707 }
1708 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1709 if (!s->hrecv) {
1710 fprintf(stderr, "Failed CreateEvent\n");
1711 goto fail;
1712 }
1713
1714 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1715 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1716 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1717 PIPE_WAIT,
1718 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1719 if (s->hcom == INVALID_HANDLE_VALUE) {
1720 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1721 s->hcom = NULL;
1722 goto fail;
1723 }
1724
1725 ZeroMemory(&ov, sizeof(ov));
1726 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1727 ret = ConnectNamedPipe(s->hcom, &ov);
1728 if (ret) {
1729 fprintf(stderr, "Failed ConnectNamedPipe\n");
1730 goto fail;
1731 }
1732
1733 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1734 if (!ret) {
1735 fprintf(stderr, "Failed GetOverlappedResult\n");
1736 if (ov.hEvent) {
1737 CloseHandle(ov.hEvent);
1738 ov.hEvent = NULL;
1739 }
1740 goto fail;
1741 }
1742
1743 if (ov.hEvent) {
1744 CloseHandle(ov.hEvent);
1745 ov.hEvent = NULL;
1746 }
1747 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1748 return 0;
1749
1750 fail:
1751 win_chr_close(chr);
1752 return -1;
1753}
1754
1755
Markus Armbruster1f514702012-02-07 15:09:08 +01001756static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001757{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001758 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001759 CharDriverState *chr;
1760 WinCharState *s;
1761
Anthony Liguori7267c092011-08-20 22:09:37 -05001762 chr = g_malloc0(sizeof(CharDriverState));
1763 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001764 chr->opaque = s;
1765 chr->chr_write = win_chr_write;
1766 chr->chr_close = win_chr_close;
1767
1768 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001769 g_free(s);
1770 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001771 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001772 }
Amit Shah127338e2009-11-03 19:59:56 +05301773 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001774 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001775}
1776
Markus Armbruster1f514702012-02-07 15:09:08 +01001777static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001778{
1779 CharDriverState *chr;
1780 WinCharState *s;
1781
Anthony Liguori7267c092011-08-20 22:09:37 -05001782 chr = g_malloc0(sizeof(CharDriverState));
1783 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001784 s->hcom = fd_out;
1785 chr->opaque = s;
1786 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301787 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001788 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001789}
1790
Markus Armbruster1f514702012-02-07 15:09:08 +01001791static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001792{
Markus Armbruster1f514702012-02-07 15:09:08 +01001793 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001794}
1795
Markus Armbruster1f514702012-02-07 15:09:08 +01001796static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001797{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001798 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001799 HANDLE fd_out;
1800
1801 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1802 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001803 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001804 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001805 }
aliguori6f97dba2008-10-31 18:49:55 +00001806
Markus Armbruster1f514702012-02-07 15:09:08 +01001807 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001808}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001809
1810static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1811{
1812 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1813 DWORD dwSize;
1814 int len1;
1815
1816 len1 = len;
1817
1818 while (len1 > 0) {
1819 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1820 break;
1821 }
1822 buf += dwSize;
1823 len1 -= dwSize;
1824 }
1825
1826 return len - len1;
1827}
1828
1829static void win_stdio_wait_func(void *opaque)
1830{
1831 CharDriverState *chr = opaque;
1832 WinStdioCharState *stdio = chr->opaque;
1833 INPUT_RECORD buf[4];
1834 int ret;
1835 DWORD dwSize;
1836 int i;
1837
1838 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1839 &dwSize);
1840
1841 if (!ret) {
1842 /* Avoid error storm */
1843 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1844 return;
1845 }
1846
1847 for (i = 0; i < dwSize; i++) {
1848 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1849
1850 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1851 int j;
1852 if (kev->uChar.AsciiChar != 0) {
1853 for (j = 0; j < kev->wRepeatCount; j++) {
1854 if (qemu_chr_be_can_write(chr)) {
1855 uint8_t c = kev->uChar.AsciiChar;
1856 qemu_chr_be_write(chr, &c, 1);
1857 }
1858 }
1859 }
1860 }
1861 }
1862}
1863
1864static DWORD WINAPI win_stdio_thread(LPVOID param)
1865{
1866 CharDriverState *chr = param;
1867 WinStdioCharState *stdio = chr->opaque;
1868 int ret;
1869 DWORD dwSize;
1870
1871 while (1) {
1872
1873 /* Wait for one byte */
1874 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1875
1876 /* Exit in case of error, continue if nothing read */
1877 if (!ret) {
1878 break;
1879 }
1880 if (!dwSize) {
1881 continue;
1882 }
1883
1884 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1885 if (stdio->win_stdio_buf == '\r') {
1886 continue;
1887 }
1888
1889 /* Signal the main thread and wait until the byte was eaten */
1890 if (!SetEvent(stdio->hInputReadyEvent)) {
1891 break;
1892 }
1893 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1894 != WAIT_OBJECT_0) {
1895 break;
1896 }
1897 }
1898
1899 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
1900 return 0;
1901}
1902
1903static void win_stdio_thread_wait_func(void *opaque)
1904{
1905 CharDriverState *chr = opaque;
1906 WinStdioCharState *stdio = chr->opaque;
1907
1908 if (qemu_chr_be_can_write(chr)) {
1909 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
1910 }
1911
1912 SetEvent(stdio->hInputDoneEvent);
1913}
1914
1915static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
1916{
1917 WinStdioCharState *stdio = chr->opaque;
1918 DWORD dwMode = 0;
1919
1920 GetConsoleMode(stdio->hStdIn, &dwMode);
1921
1922 if (echo) {
1923 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
1924 } else {
1925 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
1926 }
1927}
1928
1929static void win_stdio_close(CharDriverState *chr)
1930{
1931 WinStdioCharState *stdio = chr->opaque;
1932
1933 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
1934 CloseHandle(stdio->hInputReadyEvent);
1935 }
1936 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
1937 CloseHandle(stdio->hInputDoneEvent);
1938 }
1939 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
1940 TerminateThread(stdio->hInputThread, 0);
1941 }
1942
1943 g_free(chr->opaque);
1944 g_free(chr);
1945 stdio_nb_clients--;
1946}
1947
Markus Armbruster1f514702012-02-07 15:09:08 +01001948static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001949{
1950 CharDriverState *chr;
1951 WinStdioCharState *stdio;
1952 DWORD dwMode;
1953 int is_console = 0;
1954
1955 if (stdio_nb_clients >= STDIO_MAX_CLIENTS
1956 || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001957 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001958 }
1959
1960 chr = g_malloc0(sizeof(CharDriverState));
1961 stdio = g_malloc0(sizeof(WinStdioCharState));
1962
1963 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1964 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
1965 fprintf(stderr, "cannot open stdio: invalid handle\n");
1966 exit(1);
1967 }
1968
1969 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
1970
1971 chr->opaque = stdio;
1972 chr->chr_write = win_stdio_write;
1973 chr->chr_close = win_stdio_close;
1974
1975 if (stdio_nb_clients == 0) {
1976 if (is_console) {
1977 if (qemu_add_wait_object(stdio->hStdIn,
1978 win_stdio_wait_func, chr)) {
1979 fprintf(stderr, "qemu_add_wait_object: failed\n");
1980 }
1981 } else {
1982 DWORD dwId;
1983
1984 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1985 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1986 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
1987 chr, 0, &dwId);
1988
1989 if (stdio->hInputThread == INVALID_HANDLE_VALUE
1990 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
1991 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
1992 fprintf(stderr, "cannot create stdio thread or event\n");
1993 exit(1);
1994 }
1995 if (qemu_add_wait_object(stdio->hInputReadyEvent,
1996 win_stdio_thread_wait_func, chr)) {
1997 fprintf(stderr, "qemu_add_wait_object: failed\n");
1998 }
1999 }
2000 }
2001
2002 dwMode |= ENABLE_LINE_INPUT;
2003
2004 stdio_clients[stdio_nb_clients++] = chr;
2005 if (stdio_nb_clients == 1 && is_console) {
2006 /* set the terminal in raw mode */
2007 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2008 dwMode |= ENABLE_PROCESSED_INPUT;
2009 }
2010
2011 SetConsoleMode(stdio->hStdIn, dwMode);
2012
2013 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2014 qemu_chr_fe_set_echo(chr, false);
2015
Markus Armbruster1f514702012-02-07 15:09:08 +01002016 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002017}
aliguori6f97dba2008-10-31 18:49:55 +00002018#endif /* !_WIN32 */
2019
2020/***********************************************************/
2021/* UDP Net console */
2022
2023typedef struct {
2024 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05302025 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002026 int bufcnt;
2027 int bufptr;
2028 int max_size;
2029} NetCharDriver;
2030
2031static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2032{
2033 NetCharDriver *s = chr->opaque;
2034
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002035 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00002036}
2037
2038static int udp_chr_read_poll(void *opaque)
2039{
2040 CharDriverState *chr = opaque;
2041 NetCharDriver *s = chr->opaque;
2042
Anthony Liguori909cda12011-08-15 11:17:31 -05002043 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002044
2045 /* If there were any stray characters in the queue process them
2046 * first
2047 */
2048 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002049 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002050 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002051 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002052 }
2053 return s->max_size;
2054}
2055
2056static void udp_chr_read(void *opaque)
2057{
2058 CharDriverState *chr = opaque;
2059 NetCharDriver *s = chr->opaque;
2060
2061 if (s->max_size == 0)
2062 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00002063 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00002064 s->bufptr = s->bufcnt;
2065 if (s->bufcnt <= 0)
2066 return;
2067
2068 s->bufptr = 0;
2069 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002070 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002071 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002072 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002073 }
2074}
2075
2076static void udp_chr_update_read_handler(CharDriverState *chr)
2077{
2078 NetCharDriver *s = chr->opaque;
2079
2080 if (s->fd >= 0) {
2081 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2082 udp_chr_read, NULL, chr);
2083 }
2084}
2085
aliguori819f56b2009-03-28 17:58:14 +00002086static void udp_chr_close(CharDriverState *chr)
2087{
2088 NetCharDriver *s = chr->opaque;
2089 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002090 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00002091 closesocket(s->fd);
2092 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002093 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002094 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002095}
2096
Markus Armbruster1f514702012-02-07 15:09:08 +01002097static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002098{
2099 CharDriverState *chr = NULL;
2100 NetCharDriver *s = NULL;
2101 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002102
Anthony Liguori7267c092011-08-20 22:09:37 -05002103 chr = g_malloc0(sizeof(CharDriverState));
2104 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002105
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002106 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002107 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002108 fprintf(stderr, "inet_dgram_opts failed\n");
aliguori6f97dba2008-10-31 18:49:55 +00002109 goto return_err;
2110 }
2111
2112 s->fd = fd;
2113 s->bufcnt = 0;
2114 s->bufptr = 0;
2115 chr->opaque = s;
2116 chr->chr_write = udp_chr_write;
2117 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002118 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002119 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002120
2121return_err:
Anthony Liguori7267c092011-08-20 22:09:37 -05002122 g_free(chr);
2123 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002124 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002125 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002126 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002127 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002128}
2129
2130/***********************************************************/
2131/* TCP Net console */
2132
2133typedef struct {
2134 int fd, listen_fd;
2135 int connected;
2136 int max_size;
2137 int do_telnetopt;
2138 int do_nodelay;
2139 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002140 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002141} TCPCharDriver;
2142
2143static void tcp_chr_accept(void *opaque);
2144
2145static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2146{
2147 TCPCharDriver *s = chr->opaque;
2148 if (s->connected) {
2149 return send_all(s->fd, buf, len);
2150 } else {
2151 /* XXX: indicate an error ? */
2152 return len;
2153 }
2154}
2155
2156static int tcp_chr_read_poll(void *opaque)
2157{
2158 CharDriverState *chr = opaque;
2159 TCPCharDriver *s = chr->opaque;
2160 if (!s->connected)
2161 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002162 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002163 return s->max_size;
2164}
2165
2166#define IAC 255
2167#define IAC_BREAK 243
2168static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2169 TCPCharDriver *s,
2170 uint8_t *buf, int *size)
2171{
2172 /* Handle any telnet client's basic IAC options to satisfy char by
2173 * char mode with no echo. All IAC options will be removed from
2174 * the buf and the do_telnetopt variable will be used to track the
2175 * state of the width of the IAC information.
2176 *
2177 * IAC commands come in sets of 3 bytes with the exception of the
2178 * "IAC BREAK" command and the double IAC.
2179 */
2180
2181 int i;
2182 int j = 0;
2183
2184 for (i = 0; i < *size; i++) {
2185 if (s->do_telnetopt > 1) {
2186 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2187 /* Double IAC means send an IAC */
2188 if (j != i)
2189 buf[j] = buf[i];
2190 j++;
2191 s->do_telnetopt = 1;
2192 } else {
2193 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2194 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002195 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002196 s->do_telnetopt++;
2197 }
2198 s->do_telnetopt++;
2199 }
2200 if (s->do_telnetopt >= 4) {
2201 s->do_telnetopt = 1;
2202 }
2203 } else {
2204 if ((unsigned char)buf[i] == IAC) {
2205 s->do_telnetopt = 2;
2206 } else {
2207 if (j != i)
2208 buf[j] = buf[i];
2209 j++;
2210 }
2211 }
2212 }
2213 *size = j;
2214}
2215
Mark McLoughlin7d174052009-07-22 09:11:39 +01002216static int tcp_get_msgfd(CharDriverState *chr)
2217{
2218 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002219 int fd = s->msgfd;
2220 s->msgfd = -1;
2221 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002222}
2223
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002224#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002225static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2226{
2227 TCPCharDriver *s = chr->opaque;
2228 struct cmsghdr *cmsg;
2229
2230 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2231 int fd;
2232
2233 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2234 cmsg->cmsg_level != SOL_SOCKET ||
2235 cmsg->cmsg_type != SCM_RIGHTS)
2236 continue;
2237
2238 fd = *((int *)CMSG_DATA(cmsg));
2239 if (fd < 0)
2240 continue;
2241
2242 if (s->msgfd != -1)
2243 close(s->msgfd);
2244 s->msgfd = fd;
2245 }
2246}
2247
Mark McLoughlin9977c892009-07-22 09:11:38 +01002248static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2249{
2250 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002251 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002252 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002253 union {
2254 struct cmsghdr cmsg;
2255 char control[CMSG_SPACE(sizeof(int))];
2256 } msg_control;
2257 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002258
2259 iov[0].iov_base = buf;
2260 iov[0].iov_len = len;
2261
2262 msg.msg_iov = iov;
2263 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002264 msg.msg_control = &msg_control;
2265 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002266
Mark McLoughlin7d174052009-07-22 09:11:39 +01002267 ret = recvmsg(s->fd, &msg, 0);
2268 if (ret > 0 && s->is_unix)
2269 unix_process_msgfd(chr, &msg);
2270
2271 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002272}
2273#else
2274static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2275{
2276 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002277 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002278}
2279#endif
2280
aliguori6f97dba2008-10-31 18:49:55 +00002281static void tcp_chr_read(void *opaque)
2282{
2283 CharDriverState *chr = opaque;
2284 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302285 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002286 int len, size;
2287
2288 if (!s->connected || s->max_size <= 0)
2289 return;
2290 len = sizeof(buf);
2291 if (len > s->max_size)
2292 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002293 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002294 if (size == 0) {
2295 /* connection closed */
2296 s->connected = 0;
2297 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002298 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002299 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002300 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002301 closesocket(s->fd);
2302 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002303 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002304 } else if (size > 0) {
2305 if (s->do_telnetopt)
2306 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2307 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002308 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002309 }
2310}
2311
Blue Swirl68c18d12010-08-15 09:46:24 +00002312#ifndef _WIN32
2313CharDriverState *qemu_chr_open_eventfd(int eventfd)
2314{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002315 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002316}
Blue Swirl68c18d12010-08-15 09:46:24 +00002317#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002318
aliguori6f97dba2008-10-31 18:49:55 +00002319static void tcp_chr_connect(void *opaque)
2320{
2321 CharDriverState *chr = opaque;
2322 TCPCharDriver *s = chr->opaque;
2323
2324 s->connected = 1;
2325 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2326 tcp_chr_read, NULL, chr);
Amit Shah127338e2009-11-03 19:59:56 +05302327 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002328}
2329
2330#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2331static void tcp_chr_telnet_init(int fd)
2332{
2333 char buf[3];
2334 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2335 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2336 send(fd, (char *)buf, 3, 0);
2337 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2338 send(fd, (char *)buf, 3, 0);
2339 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2340 send(fd, (char *)buf, 3, 0);
2341 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2342 send(fd, (char *)buf, 3, 0);
2343}
2344
2345static void socket_set_nodelay(int fd)
2346{
2347 int val = 1;
2348 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2349}
2350
Daniel P. Berrange13661082011-06-23 13:31:42 +01002351static int tcp_chr_add_client(CharDriverState *chr, int fd)
2352{
2353 TCPCharDriver *s = chr->opaque;
2354 if (s->fd != -1)
2355 return -1;
2356
2357 socket_set_nonblock(fd);
2358 if (s->do_nodelay)
2359 socket_set_nodelay(fd);
2360 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002361 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002362 tcp_chr_connect(chr);
2363
2364 return 0;
2365}
2366
aliguori6f97dba2008-10-31 18:49:55 +00002367static void tcp_chr_accept(void *opaque)
2368{
2369 CharDriverState *chr = opaque;
2370 TCPCharDriver *s = chr->opaque;
2371 struct sockaddr_in saddr;
2372#ifndef _WIN32
2373 struct sockaddr_un uaddr;
2374#endif
2375 struct sockaddr *addr;
2376 socklen_t len;
2377 int fd;
2378
2379 for(;;) {
2380#ifndef _WIN32
2381 if (s->is_unix) {
2382 len = sizeof(uaddr);
2383 addr = (struct sockaddr *)&uaddr;
2384 } else
2385#endif
2386 {
2387 len = sizeof(saddr);
2388 addr = (struct sockaddr *)&saddr;
2389 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002390 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002391 if (fd < 0 && errno != EINTR) {
2392 return;
2393 } else if (fd >= 0) {
2394 if (s->do_telnetopt)
2395 tcp_chr_telnet_init(fd);
2396 break;
2397 }
2398 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002399 if (tcp_chr_add_client(chr, fd) < 0)
2400 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002401}
2402
2403static void tcp_chr_close(CharDriverState *chr)
2404{
2405 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002406 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002407 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002408 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002409 }
2410 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002411 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002412 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002413 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002414 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002415 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002416}
2417
Markus Armbruster1f514702012-02-07 15:09:08 +01002418static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002419{
2420 CharDriverState *chr = NULL;
2421 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002422 int fd = -1;
2423 int is_listen;
2424 int is_waitconnect;
2425 int do_nodelay;
2426 int is_unix;
2427 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002428
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002429 is_listen = qemu_opt_get_bool(opts, "server", 0);
2430 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2431 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2432 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2433 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002434 if (!is_listen)
2435 is_waitconnect = 0;
2436
Anthony Liguori7267c092011-08-20 22:09:37 -05002437 chr = g_malloc0(sizeof(CharDriverState));
2438 s = g_malloc0(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002439
aliguorif07b6002008-11-11 20:54:09 +00002440 if (is_unix) {
2441 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002442 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002443 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002444 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002445 }
2446 } else {
2447 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002448 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002449 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002450 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002451 }
2452 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002453 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002454 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002455 }
aliguori6f97dba2008-10-31 18:49:55 +00002456
2457 if (!is_waitconnect)
2458 socket_set_nonblock(fd);
2459
2460 s->connected = 0;
2461 s->fd = -1;
2462 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002463 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002464 s->is_unix = is_unix;
2465 s->do_nodelay = do_nodelay && !is_unix;
2466
2467 chr->opaque = s;
2468 chr->chr_write = tcp_chr_write;
2469 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002470 chr->get_msgfd = tcp_get_msgfd;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002471 chr->chr_add_client = tcp_chr_add_client;
aliguori6f97dba2008-10-31 18:49:55 +00002472
2473 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002474 s->listen_fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002475 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002476 if (is_telnet)
2477 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002478
aliguori6f97dba2008-10-31 18:49:55 +00002479 } else {
aliguorif07b6002008-11-11 20:54:09 +00002480 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002481 s->fd = fd;
2482 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002483 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002484 }
2485
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002486 /* for "info chardev" monitor command */
Anthony Liguori7267c092011-08-20 22:09:37 -05002487 chr->filename = g_malloc(256);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002488 if (is_unix) {
2489 snprintf(chr->filename, 256, "unix:%s%s",
2490 qemu_opt_get(opts, "path"),
2491 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2492 } else if (is_telnet) {
2493 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2494 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2495 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2496 } else {
2497 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2498 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2499 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2500 }
2501
aliguori6f97dba2008-10-31 18:49:55 +00002502 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002503 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002504 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002505 tcp_chr_accept(chr);
2506 socket_set_nonblock(s->listen_fd);
2507 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002508 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002509
aliguori6f97dba2008-10-31 18:49:55 +00002510 fail:
2511 if (fd >= 0)
2512 closesocket(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002513 g_free(s);
2514 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002515 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002516}
2517
Luiz Capitulino999bd672010-10-22 16:09:05 -02002518/***********************************************************/
2519/* Memory chardev */
2520typedef struct {
2521 size_t outbuf_size;
2522 size_t outbuf_capacity;
2523 uint8_t *outbuf;
2524} MemoryDriver;
2525
2526static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2527{
2528 MemoryDriver *d = chr->opaque;
2529
2530 /* TODO: the QString implementation has the same code, we should
2531 * introduce a generic way to do this in cutils.c */
2532 if (d->outbuf_capacity < d->outbuf_size + len) {
2533 /* grow outbuf */
2534 d->outbuf_capacity += len;
2535 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002536 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002537 }
2538
2539 memcpy(d->outbuf + d->outbuf_size, buf, len);
2540 d->outbuf_size += len;
2541
2542 return len;
2543}
2544
2545void qemu_chr_init_mem(CharDriverState *chr)
2546{
2547 MemoryDriver *d;
2548
Anthony Liguori7267c092011-08-20 22:09:37 -05002549 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002550 d->outbuf_size = 0;
2551 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002552 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002553
2554 memset(chr, 0, sizeof(*chr));
2555 chr->opaque = d;
2556 chr->chr_write = mem_chr_write;
2557}
2558
2559QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2560{
2561 MemoryDriver *d = chr->opaque;
2562 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2563}
2564
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002565/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002566void qemu_chr_close_mem(CharDriverState *chr)
2567{
2568 MemoryDriver *d = chr->opaque;
2569
Anthony Liguori7267c092011-08-20 22:09:37 -05002570 g_free(d->outbuf);
2571 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002572 chr->opaque = NULL;
2573 chr->chr_write = NULL;
2574}
2575
2576size_t qemu_chr_mem_osize(const CharDriverState *chr)
2577{
2578 const MemoryDriver *d = chr->opaque;
2579 return d->outbuf_size;
2580}
2581
Gerd Hoffmann33521632009-12-08 13:11:49 +01002582QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002583{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002584 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002585 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002586 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002587 QemuOpts *opts;
2588
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002589 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002590 if (NULL == opts)
2591 return NULL;
2592
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002593 if (strstart(filename, "mon:", &p)) {
2594 filename = p;
2595 qemu_opt_set(opts, "mux", "on");
2596 }
2597
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002598 if (strcmp(filename, "null") == 0 ||
2599 strcmp(filename, "pty") == 0 ||
2600 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002601 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002602 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002603 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002604 return opts;
2605 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002606 if (strstart(filename, "vc", &p)) {
2607 qemu_opt_set(opts, "backend", "vc");
2608 if (*p == ':') {
2609 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2610 /* pixels */
2611 qemu_opt_set(opts, "width", width);
2612 qemu_opt_set(opts, "height", height);
2613 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2614 /* chars */
2615 qemu_opt_set(opts, "cols", width);
2616 qemu_opt_set(opts, "rows", height);
2617 } else {
2618 goto fail;
2619 }
2620 }
2621 return opts;
2622 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002623 if (strcmp(filename, "con:") == 0) {
2624 qemu_opt_set(opts, "backend", "console");
2625 return opts;
2626 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002627 if (strstart(filename, "COM", NULL)) {
2628 qemu_opt_set(opts, "backend", "serial");
2629 qemu_opt_set(opts, "path", filename);
2630 return opts;
2631 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002632 if (strstart(filename, "file:", &p)) {
2633 qemu_opt_set(opts, "backend", "file");
2634 qemu_opt_set(opts, "path", p);
2635 return opts;
2636 }
2637 if (strstart(filename, "pipe:", &p)) {
2638 qemu_opt_set(opts, "backend", "pipe");
2639 qemu_opt_set(opts, "path", p);
2640 return opts;
2641 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002642 if (strstart(filename, "tcp:", &p) ||
2643 strstart(filename, "telnet:", &p)) {
2644 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2645 host[0] = 0;
2646 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2647 goto fail;
2648 }
2649 qemu_opt_set(opts, "backend", "socket");
2650 qemu_opt_set(opts, "host", host);
2651 qemu_opt_set(opts, "port", port);
2652 if (p[pos] == ',') {
2653 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2654 goto fail;
2655 }
2656 if (strstart(filename, "telnet:", &p))
2657 qemu_opt_set(opts, "telnet", "on");
2658 return opts;
2659 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002660 if (strstart(filename, "udp:", &p)) {
2661 qemu_opt_set(opts, "backend", "udp");
2662 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2663 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002664 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002665 goto fail;
2666 }
2667 }
2668 qemu_opt_set(opts, "host", host);
2669 qemu_opt_set(opts, "port", port);
2670 if (p[pos] == '@') {
2671 p += pos + 1;
2672 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2673 host[0] = 0;
2674 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002675 goto fail;
2676 }
2677 }
2678 qemu_opt_set(opts, "localaddr", host);
2679 qemu_opt_set(opts, "localport", port);
2680 }
2681 return opts;
2682 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002683 if (strstart(filename, "unix:", &p)) {
2684 qemu_opt_set(opts, "backend", "socket");
2685 if (qemu_opts_do_parse(opts, p, "path") != 0)
2686 goto fail;
2687 return opts;
2688 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002689 if (strstart(filename, "/dev/parport", NULL) ||
2690 strstart(filename, "/dev/ppi", NULL)) {
2691 qemu_opt_set(opts, "backend", "parport");
2692 qemu_opt_set(opts, "path", filename);
2693 return opts;
2694 }
2695 if (strstart(filename, "/dev/", NULL)) {
2696 qemu_opt_set(opts, "backend", "tty");
2697 qemu_opt_set(opts, "path", filename);
2698 return opts;
2699 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002700
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002701fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002702 qemu_opts_del(opts);
2703 return NULL;
2704}
2705
2706static const struct {
2707 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01002708 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002709} backend_table[] = {
2710 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002711 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002712 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002713 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002714 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002715#ifdef _WIN32
2716 { .name = "file", .open = qemu_chr_open_win_file_out },
2717 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002718 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002719 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002720 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002721#else
2722 { .name = "file", .open = qemu_chr_open_file_out },
2723 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002724 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002725 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002726#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002727#ifdef CONFIG_BRLAPI
2728 { .name = "braille", .open = chr_baum_init },
2729#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002730#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002731 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2732 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002733 { .name = "tty", .open = qemu_chr_open_tty },
2734#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002735#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2736 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002737 { .name = "parport", .open = qemu_chr_open_pp },
2738#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002739#ifdef CONFIG_SPICE
2740 { .name = "spicevmc", .open = qemu_chr_open_spice },
2741#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002742};
2743
Anthony Liguorif69554b2011-08-15 11:17:37 -05002744CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002745 void (*init)(struct CharDriverState *s))
2746{
2747 CharDriverState *chr;
2748 int i;
2749
2750 if (qemu_opts_id(opts) == NULL) {
2751 fprintf(stderr, "chardev: no id specified\n");
2752 return NULL;
2753 }
2754
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002755 if (qemu_opt_get(opts, "backend") == NULL) {
2756 fprintf(stderr, "chardev: \"%s\" missing backend\n",
2757 qemu_opts_id(opts));
2758 return NULL;
2759 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002760 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2761 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2762 break;
2763 }
2764 if (i == ARRAY_SIZE(backend_table)) {
2765 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2766 qemu_opt_get(opts, "backend"));
2767 return NULL;
2768 }
2769
Markus Armbruster1f514702012-02-07 15:09:08 +01002770 chr = backend_table[i].open(opts);
2771 if (!chr) {
2772 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2773 qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002774 return NULL;
2775 }
2776
2777 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05002778 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002779 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002780 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002781
2782 if (qemu_opt_get_bool(opts, "mux", 0)) {
2783 CharDriverState *base = chr;
2784 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05002785 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002786 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2787 chr = qemu_chr_open_mux(base);
2788 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002789 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002790 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002791 } else {
2792 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002793 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002794 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002795 return chr;
2796}
2797
Anthony Liguori27143a42011-08-15 11:17:36 -05002798CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002799{
2800 const char *p;
2801 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002802 QemuOpts *opts;
2803
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002804 if (strstart(filename, "chardev:", &p)) {
2805 return qemu_chr_find(p);
2806 }
2807
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002808 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002809 if (!opts)
2810 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002811
Anthony Liguorif69554b2011-08-15 11:17:37 -05002812 chr = qemu_chr_new_from_opts(opts, init);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002813 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2814 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002815 }
Paolo Bonzini363f8cb2010-12-23 13:42:54 +01002816 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002817 return chr;
2818}
2819
Anthony Liguori15f31512011-08-15 11:17:35 -05002820void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01002821{
2822 if (chr->chr_set_echo) {
2823 chr->chr_set_echo(chr, echo);
2824 }
2825}
2826
Anthony Liguoric9d830e2011-08-15 11:17:32 -05002827void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002828{
2829 if (chr->chr_guest_open) {
2830 chr->chr_guest_open(chr);
2831 }
2832}
2833
Anthony Liguori28178222011-08-15 11:17:33 -05002834void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002835{
2836 if (chr->chr_guest_close) {
2837 chr->chr_guest_close(chr);
2838 }
2839}
2840
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002841void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00002842{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002843 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002844 if (chr->chr_close)
2845 chr->chr_close(chr);
Anthony Liguori7267c092011-08-20 22:09:37 -05002846 g_free(chr->filename);
2847 g_free(chr->label);
2848 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002849}
2850
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002851ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002852{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002853 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002854 CharDriverState *chr;
2855
Blue Swirl72cf2d42009-09-12 07:36:22 +00002856 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002857 ChardevInfoList *info = g_malloc0(sizeof(*info));
2858 info->value = g_malloc0(sizeof(*info->value));
2859 info->value->label = g_strdup(chr->label);
2860 info->value->filename = g_strdup(chr->filename);
2861
2862 info->next = chr_list;
2863 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00002864 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002865
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002866 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002867}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002868
2869CharDriverState *qemu_chr_find(const char *name)
2870{
2871 CharDriverState *chr;
2872
Blue Swirl72cf2d42009-09-12 07:36:22 +00002873 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002874 if (strcmp(chr->label, name) != 0)
2875 continue;
2876 return chr;
2877 }
2878 return NULL;
2879}
Anthony Liguori0beb4942011-12-22 15:29:25 -06002880
2881/* Get a character (serial) device interface. */
2882CharDriverState *qemu_char_get_next_serial(void)
2883{
2884 static int next_serial;
2885
2886 /* FIXME: This function needs to go away: use chardev properties! */
2887 return serial_hds[next_serial++];
2888}
2889