blob: 0a0833f77b8c96b75c2d03d52782a31127a0ff24 [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"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57#include <dev/ppbus/ppi.h>
58#include <dev/ppbus/ppbconf.h>
59#elif defined(__DragonFly__)
60#include <dev/misc/ppi/ppi.h>
61#include <bus/ppbus/ppbconf.h>
62#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010063#else
aliguori6f97dba2008-10-31 18:49:55 +000064#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000065#include <linux/ppdev.h>
66#include <linux/parport.h>
67#endif
68#ifdef __sun__
69#include <sys/stat.h>
70#include <sys/ethernet.h>
71#include <sys/sockio.h>
72#include <netinet/arp.h>
73#include <netinet/in.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_icmp.h> // must come after ip.h
77#include <netinet/udp.h>
78#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000079#endif
80#endif
81#endif
82
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010083#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020084#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000085
Amit Shah9bd78542009-11-03 19:59:54 +053086#define READ_BUF_LEN 4096
87
aliguori6f97dba2008-10-31 18:49:55 +000088/***********************************************************/
89/* character device */
90
Blue Swirl72cf2d42009-09-12 07:36:22 +000091static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000093
Hans de Goedea425d232011-11-19 10:22:43 +010094void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +000095{
Alexander Graf73cdf3f2010-04-01 18:42:39 +020096 /* Keep track if the char device is open */
97 switch (event) {
98 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +010099 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200100 break;
101 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100102 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200103 break;
104 }
105
aliguori6f97dba2008-10-31 18:49:55 +0000106 if (!s->chr_event)
107 return;
108 s->chr_event(s->handler_opaque, event);
109}
110
Hans de Goedefee204f2013-03-26 11:07:54 +0100111void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000112{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500113 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000114}
115
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500116int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000117{
118 return s->chr_write(s, buf, len);
119}
120
Anthony Liguoricd187202013-03-26 10:04:17 -0500121int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
122{
123 int offset = 0;
124 int res;
125
126 while (offset < len) {
127 do {
128 res = s->chr_write(s, buf + offset, len - offset);
129 if (res == -1 && errno == EAGAIN) {
130 g_usleep(100);
131 }
132 } while (res == -1 && errno == EAGAIN);
133
134 if (res == 0) {
135 break;
136 }
137
138 if (res < 0) {
139 return res;
140 }
141
142 offset += res;
143 }
144
145 return offset;
146}
147
Anthony Liguori41084f12011-08-15 11:17:34 -0500148int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000149{
150 if (!s->chr_ioctl)
151 return -ENOTSUP;
152 return s->chr_ioctl(s, cmd, arg);
153}
154
Anthony Liguori909cda12011-08-15 11:17:31 -0500155int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000156{
157 if (!s->chr_can_read)
158 return 0;
159 return s->chr_can_read(s->handler_opaque);
160}
161
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500162void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000163{
Stefan Weilac310732012-04-19 22:27:14 +0200164 if (s->chr_read) {
165 s->chr_read(s->handler_opaque, buf, len);
166 }
aliguori6f97dba2008-10-31 18:49:55 +0000167}
168
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500169int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100170{
171 return s->get_msgfd ? s->get_msgfd(s) : -1;
172}
173
Daniel P. Berrange13661082011-06-23 13:31:42 +0100174int qemu_chr_add_client(CharDriverState *s, int fd)
175{
176 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
177}
178
aliguori6f97dba2008-10-31 18:49:55 +0000179void qemu_chr_accept_input(CharDriverState *s)
180{
181 if (s->chr_accept_input)
182 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100183 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000184}
185
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500186void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000187{
Amit Shah9bd78542009-11-03 19:59:54 +0530188 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000189 va_list ap;
190 va_start(ap, fmt);
191 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500192 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000193 va_end(ap);
194}
195
aliguori6f97dba2008-10-31 18:49:55 +0000196void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100197 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000198 IOReadHandler *fd_read,
199 IOEventHandler *fd_event,
200 void *opaque)
201{
Hans de Goede19083222013-03-26 11:07:56 +0100202 int fe_open;
203
Amit Shahda7d9982011-04-25 15:18:22 +0530204 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100205 fe_open = 0;
206 } else {
207 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530208 }
aliguori6f97dba2008-10-31 18:49:55 +0000209 s->chr_can_read = fd_can_read;
210 s->chr_read = fd_read;
211 s->chr_event = fd_event;
212 s->handler_opaque = opaque;
213 if (s->chr_update_read_handler)
214 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200215
Hans de Goede19083222013-03-26 11:07:56 +0100216 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100217 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100218 }
219
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200220 /* We're connecting to an already opened device, so let's make sure we
221 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100222 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100223 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200224 }
aliguori6f97dba2008-10-31 18:49:55 +0000225}
226
227static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
228{
229 return len;
230}
231
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100232static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000233{
234 CharDriverState *chr;
235
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000237 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500238 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100239 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000240}
241
242/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000243#define MAX_MUX 4
244#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
245#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
246typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100247 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000248 IOReadHandler *chr_read[MAX_MUX];
249 IOEventHandler *chr_event[MAX_MUX];
250 void *ext_opaque[MAX_MUX];
251 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200252 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000253 int mux_cnt;
254 int term_got_escape;
255 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000256 /* Intermediate input buffer allows to catch escape sequences even if the
257 currently active device is not accepting any input - but only until it
258 is full as well. */
259 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
260 int prod[MAX_MUX];
261 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200262 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200263 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200264 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000265} MuxDriver;
266
267
268static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
269{
270 MuxDriver *d = chr->opaque;
271 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200272 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000273 ret = d->drv->chr_write(d->drv, buf, len);
274 } else {
275 int i;
276
277 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200278 for (i = 0; i < len; i++) {
279 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000280 char buf1[64];
281 int64_t ti;
282 int secs;
283
Alex Blighbc72ad62013-08-21 16:03:08 +0100284 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200285 if (d->timestamps_start == -1)
286 d->timestamps_start = ti;
287 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000288 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000289 snprintf(buf1, sizeof(buf1),
290 "[%02d:%02d:%02d.%03d] ",
291 secs / 3600,
292 (secs / 60) % 60,
293 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000294 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000295 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200296 d->linestart = 0;
297 }
298 ret += d->drv->chr_write(d->drv, buf+i, 1);
299 if (buf[i] == '\n') {
300 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000301 }
302 }
303 }
304 return ret;
305}
306
307static const char * const mux_help[] = {
308 "% h print this help\n\r",
309 "% x exit emulator\n\r",
310 "% s save disk data back to file (if -snapshot)\n\r",
311 "% t toggle console timestamps\n\r"
312 "% b send break (magic sysrq)\n\r",
313 "% c switch between console and monitor\n\r",
314 "% % sends %\n\r",
315 NULL
316};
317
318int term_escape_char = 0x01; /* ctrl-a is used for escape */
319static void mux_print_help(CharDriverState *chr)
320{
321 int i, j;
322 char ebuf[15] = "Escape-Char";
323 char cbuf[50] = "\n\r";
324
325 if (term_escape_char > 0 && term_escape_char < 26) {
326 snprintf(cbuf, sizeof(cbuf), "\n\r");
327 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
328 } else {
329 snprintf(cbuf, sizeof(cbuf),
330 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
331 term_escape_char);
332 }
333 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
334 for (i = 0; mux_help[i] != NULL; i++) {
335 for (j=0; mux_help[i][j] != '\0'; j++) {
336 if (mux_help[i][j] == '%')
337 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
338 else
339 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
340 }
341 }
342}
343
aliguori2724b182009-03-05 23:01:47 +0000344static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
345{
346 if (d->chr_event[mux_nr])
347 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
348}
349
aliguori6f97dba2008-10-31 18:49:55 +0000350static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
351{
352 if (d->term_got_escape) {
353 d->term_got_escape = 0;
354 if (ch == term_escape_char)
355 goto send_char;
356 switch(ch) {
357 case '?':
358 case 'h':
359 mux_print_help(chr);
360 break;
361 case 'x':
362 {
363 const char *term = "QEMU: Terminated\n\r";
364 chr->chr_write(chr,(uint8_t *)term,strlen(term));
365 exit(0);
366 break;
367 }
368 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200369 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000370 break;
371 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100372 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000373 break;
374 case 'c':
375 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200376 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
377 d->focus++;
378 if (d->focus >= d->mux_cnt)
379 d->focus = 0;
380 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000381 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200382 case 't':
383 d->timestamps = !d->timestamps;
384 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200385 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200386 break;
aliguori6f97dba2008-10-31 18:49:55 +0000387 }
388 } else if (ch == term_escape_char) {
389 d->term_got_escape = 1;
390 } else {
391 send_char:
392 return 1;
393 }
394 return 0;
395}
396
397static void mux_chr_accept_input(CharDriverState *chr)
398{
aliguori6f97dba2008-10-31 18:49:55 +0000399 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200400 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000401
aliguoria80bf992009-03-05 23:00:02 +0000402 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000403 d->chr_can_read[m] &&
404 d->chr_can_read[m](d->ext_opaque[m])) {
405 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000406 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000407 }
408}
409
410static int mux_chr_can_read(void *opaque)
411{
412 CharDriverState *chr = opaque;
413 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200414 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000415
aliguoria80bf992009-03-05 23:00:02 +0000416 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000417 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000418 if (d->chr_can_read[m])
419 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000420 return 0;
421}
422
423static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
424{
425 CharDriverState *chr = opaque;
426 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200427 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000428 int i;
429
430 mux_chr_accept_input (opaque);
431
432 for(i = 0; i < size; i++)
433 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000434 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000435 d->chr_can_read[m] &&
436 d->chr_can_read[m](d->ext_opaque[m]))
437 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
438 else
aliguoria80bf992009-03-05 23:00:02 +0000439 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000440 }
441}
442
443static void mux_chr_event(void *opaque, int event)
444{
445 CharDriverState *chr = opaque;
446 MuxDriver *d = chr->opaque;
447 int i;
448
449 /* Send the event to all registered listeners */
450 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000451 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000452}
453
454static void mux_chr_update_read_handler(CharDriverState *chr)
455{
456 MuxDriver *d = chr->opaque;
457
458 if (d->mux_cnt >= MAX_MUX) {
459 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
460 return;
461 }
462 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
463 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
464 d->chr_read[d->mux_cnt] = chr->chr_read;
465 d->chr_event[d->mux_cnt] = chr->chr_event;
466 /* Fix up the real driver with mux routines */
467 if (d->mux_cnt == 0) {
468 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
469 mux_chr_event, chr);
470 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200471 if (d->focus != -1) {
472 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200473 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200474 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000475 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200476 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000477}
478
Michael Roth7b7ab182013-07-30 13:04:22 -0500479static bool muxes_realized;
480
481/**
482 * Called after processing of default and command-line-specified
483 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
484 * to a mux chardev. This is done here to ensure that
485 * output/prompts/banners are only displayed for the FE that has
486 * focus when initial command-line processing/machine init is
487 * completed.
488 *
489 * After this point, any new FE attached to any new or existing
490 * mux will receive CHR_EVENT_OPENED notifications for the BE
491 * immediately.
492 */
493static void muxes_realize_done(Notifier *notifier, void *unused)
494{
495 CharDriverState *chr;
496
497 QTAILQ_FOREACH(chr, &chardevs, next) {
498 if (chr->is_mux) {
499 MuxDriver *d = chr->opaque;
500 int i;
501
502 /* send OPENED to all already-attached FEs */
503 for (i = 0; i < d->mux_cnt; i++) {
504 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
505 }
506 /* mark mux as OPENED so any new FEs will immediately receive
507 * OPENED event
508 */
509 qemu_chr_be_generic_open(chr);
510 }
511 }
512 muxes_realized = true;
513}
514
515static Notifier muxes_realize_notify = {
516 .notify = muxes_realize_done,
517};
518
aliguori6f97dba2008-10-31 18:49:55 +0000519static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
520{
521 CharDriverState *chr;
522 MuxDriver *d;
523
Anthony Liguori7267c092011-08-20 22:09:37 -0500524 chr = g_malloc0(sizeof(CharDriverState));
525 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000526
527 chr->opaque = d;
528 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200529 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000530 chr->chr_write = mux_chr_write;
531 chr->chr_update_read_handler = mux_chr_update_read_handler;
532 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100533 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100534 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500535 /* only default to opened state if we've realized the initial
536 * set of muxes
537 */
538 chr->explicit_be_open = muxes_realized ? 0 : 1;
539 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200540
aliguori6f97dba2008-10-31 18:49:55 +0000541 return chr;
542}
543
544
545#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000546int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000547{
548 int ret, len;
549
550 len = len1;
551 while (len > 0) {
552 ret = send(fd, buf, len, 0);
553 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000554 errno = WSAGetLastError();
555 if (errno != WSAEWOULDBLOCK) {
556 return -1;
557 }
558 } else if (ret == 0) {
559 break;
560 } else {
561 buf += ret;
562 len -= ret;
563 }
564 }
565 return len1 - len;
566}
567
568#else
569
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100570int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000571{
572 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100573 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000574
575 len = len1;
576 while (len > 0) {
577 ret = write(fd, buf, len);
578 if (ret < 0) {
579 if (errno != EINTR && errno != EAGAIN)
580 return -1;
581 } else if (ret == 0) {
582 break;
583 } else {
584 buf += ret;
585 len -= ret;
586 }
587 }
588 return len1 - len;
589}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500590
591int recv_all(int fd, void *_buf, int len1, bool single_read)
592{
593 int ret, len;
594 uint8_t *buf = _buf;
595
596 len = len1;
597 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
598 if (ret < 0) {
599 if (errno != EINTR && errno != EAGAIN) {
600 return -1;
601 }
602 continue;
603 } else {
604 if (single_read) {
605 return ret;
606 }
607 buf += ret;
608 len -= ret;
609 }
610 }
611 return len1 - len;
612}
613
aliguori6f97dba2008-10-31 18:49:55 +0000614#endif /* !_WIN32 */
615
Anthony Liguori96c63842013-03-05 23:21:18 +0530616typedef struct IOWatchPoll
617{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200618 GSource parent;
619
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200620 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530621 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530622
623 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200624 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530625 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530626} IOWatchPoll;
627
Anthony Liguori96c63842013-03-05 23:21:18 +0530628static IOWatchPoll *io_watch_poll_from_source(GSource *source)
629{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200630 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530631}
632
633static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
634{
635 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200636 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200637 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200638 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530639 return FALSE;
640 }
641
Paolo Bonzinid185c092013-04-05 17:59:33 +0200642 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200643 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
644 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200645 g_source_attach(iwp->src, NULL);
646 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200647 g_source_destroy(iwp->src);
648 g_source_unref(iwp->src);
649 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200650 }
651 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530652}
653
654static gboolean io_watch_poll_check(GSource *source)
655{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200656 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530657}
658
659static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
660 gpointer user_data)
661{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200662 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530663}
664
665static void io_watch_poll_finalize(GSource *source)
666{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200667 /* Due to a glib bug, removing the last reference to a source
668 * inside a finalize callback causes recursive locking (and a
669 * deadlock). This is not a problem inside other callbacks,
670 * including dispatch callbacks, so we call io_remove_watch_poll
671 * to remove this source. At this point, iwp->src must
672 * be NULL, or we would leak it.
673 *
674 * This would be solved much more elegantly by child sources,
675 * but we support older glib versions that do not have them.
676 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530677 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200678 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530679}
680
681static GSourceFuncs io_watch_poll_funcs = {
682 .prepare = io_watch_poll_prepare,
683 .check = io_watch_poll_check,
684 .dispatch = io_watch_poll_dispatch,
685 .finalize = io_watch_poll_finalize,
686};
687
688/* Can only be used for read */
689static guint io_add_watch_poll(GIOChannel *channel,
690 IOCanReadHandler *fd_can_read,
691 GIOFunc fd_read,
692 gpointer user_data)
693{
694 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200695 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530696
Paolo Bonzinid185c092013-04-05 17:59:33 +0200697 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530698 iwp->fd_can_read = fd_can_read;
699 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200700 iwp->channel = channel;
701 iwp->fd_read = (GSourceFunc) fd_read;
702 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530703
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200704 tag = g_source_attach(&iwp->parent, NULL);
705 g_source_unref(&iwp->parent);
706 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530707}
708
Paolo Bonzini2b316772013-04-19 17:32:09 +0200709static void io_remove_watch_poll(guint tag)
710{
711 GSource *source;
712 IOWatchPoll *iwp;
713
714 g_return_if_fail (tag > 0);
715
716 source = g_main_context_find_source_by_id(NULL, tag);
717 g_return_if_fail (source != NULL);
718
719 iwp = io_watch_poll_from_source(source);
720 if (iwp->src) {
721 g_source_destroy(iwp->src);
722 g_source_unref(iwp->src);
723 iwp->src = NULL;
724 }
725 g_source_destroy(&iwp->parent);
726}
727
Amit Shah26da70c2013-08-28 15:23:37 +0530728static void remove_fd_in_watch(CharDriverState *chr)
729{
730 if (chr->fd_in_tag) {
731 io_remove_watch_poll(chr->fd_in_tag);
732 chr->fd_in_tag = 0;
733 }
734}
735
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000736#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530737static GIOChannel *io_channel_from_fd(int fd)
738{
739 GIOChannel *chan;
740
741 if (fd == -1) {
742 return NULL;
743 }
744
745 chan = g_io_channel_unix_new(fd);
746
747 g_io_channel_set_encoding(chan, NULL, NULL);
748 g_io_channel_set_buffered(chan, FALSE);
749
750 return chan;
751}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000752#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530753
Anthony Liguori76a96442013-03-05 23:21:21 +0530754static GIOChannel *io_channel_from_socket(int fd)
755{
756 GIOChannel *chan;
757
758 if (fd == -1) {
759 return NULL;
760 }
761
762#ifdef _WIN32
763 chan = g_io_channel_win32_new_socket(fd);
764#else
765 chan = g_io_channel_unix_new(fd);
766#endif
767
768 g_io_channel_set_encoding(chan, NULL, NULL);
769 g_io_channel_set_buffered(chan, FALSE);
770
771 return chan;
772}
773
Anthony Liguori684a0962013-03-29 11:39:50 -0500774static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530775{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200776 size_t offset = 0;
777 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530778
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200779 while (offset < len && status == G_IO_STATUS_NORMAL) {
780 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500781
782 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530783 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500784 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530785 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500786
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200787 if (offset > 0) {
788 return offset;
789 }
790 switch (status) {
791 case G_IO_STATUS_NORMAL:
792 g_assert(len == 0);
793 return 0;
794 case G_IO_STATUS_AGAIN:
795 errno = EAGAIN;
796 return -1;
797 default:
798 break;
799 }
800 errno = EINVAL;
801 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530802}
Anthony Liguori96c63842013-03-05 23:21:18 +0530803
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000804#ifndef _WIN32
805
Anthony Liguoria29753f2013-03-05 23:21:19 +0530806typedef struct FDCharDriver {
807 CharDriverState *chr;
808 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000809 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530810 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000811} FDCharDriver;
812
aliguori6f97dba2008-10-31 18:49:55 +0000813static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
814{
815 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530816
Anthony Liguori684a0962013-03-29 11:39:50 -0500817 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530818}
819
820static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
821{
822 CharDriverState *chr = opaque;
823 FDCharDriver *s = chr->opaque;
824 int len;
825 uint8_t buf[READ_BUF_LEN];
826 GIOStatus status;
827 gsize bytes_read;
828
829 len = sizeof(buf);
830 if (len > s->max_size) {
831 len = s->max_size;
832 }
833 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200834 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530835 }
836
837 status = g_io_channel_read_chars(chan, (gchar *)buf,
838 len, &bytes_read, NULL);
839 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530840 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530841 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
842 return FALSE;
843 }
844 if (status == G_IO_STATUS_NORMAL) {
845 qemu_chr_be_write(chr, buf, bytes_read);
846 }
847
848 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000849}
850
851static int fd_chr_read_poll(void *opaque)
852{
853 CharDriverState *chr = opaque;
854 FDCharDriver *s = chr->opaque;
855
Anthony Liguori909cda12011-08-15 11:17:31 -0500856 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000857 return s->max_size;
858}
859
Anthony Liguori23673ca2013-03-05 23:21:23 +0530860static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
861{
862 FDCharDriver *s = chr->opaque;
863 return g_io_create_watch(s->fd_out, cond);
864}
865
aliguori6f97dba2008-10-31 18:49:55 +0000866static void fd_chr_update_read_handler(CharDriverState *chr)
867{
868 FDCharDriver *s = chr->opaque;
869
Amit Shah26da70c2013-08-28 15:23:37 +0530870 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530871 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530872 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
873 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000874 }
875}
876
877static void fd_chr_close(struct CharDriverState *chr)
878{
879 FDCharDriver *s = chr->opaque;
880
Amit Shah26da70c2013-08-28 15:23:37 +0530881 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530882 if (s->fd_in) {
883 g_io_channel_unref(s->fd_in);
884 }
885 if (s->fd_out) {
886 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000887 }
888
Anthony Liguori7267c092011-08-20 22:09:37 -0500889 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100890 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000891}
892
893/* open a character device to a unix fd */
894static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
895{
896 CharDriverState *chr;
897 FDCharDriver *s;
898
Anthony Liguori7267c092011-08-20 22:09:37 -0500899 chr = g_malloc0(sizeof(CharDriverState));
900 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530901 s->fd_in = io_channel_from_fd(fd_in);
902 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530903 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530904 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000905 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530906 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000907 chr->chr_write = fd_chr_write;
908 chr->chr_update_read_handler = fd_chr_update_read_handler;
909 chr->chr_close = fd_chr_close;
910
aliguori6f97dba2008-10-31 18:49:55 +0000911 return chr;
912}
913
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100914static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000915{
916 int fd_in, fd_out;
917 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100918 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200919
920 if (filename == NULL) {
921 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100922 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200923 }
aliguori6f97dba2008-10-31 18:49:55 +0000924
925 snprintf(filename_in, 256, "%s.in", filename);
926 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100927 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
928 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000929 if (fd_in < 0 || fd_out < 0) {
930 if (fd_in >= 0)
931 close(fd_in);
932 if (fd_out >= 0)
933 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100934 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100935 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100936 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100937 }
aliguori6f97dba2008-10-31 18:49:55 +0000938 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100939 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000940}
941
aliguori6f97dba2008-10-31 18:49:55 +0000942/* init terminal so that we can grab keys */
943static struct termios oldtty;
944static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100945static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000946
947static void term_exit(void)
948{
949 tcsetattr (0, TCSANOW, &oldtty);
950 fcntl(0, F_SETFL, old_fd0_flags);
951}
952
Paolo Bonzinibb002512010-12-23 13:42:50 +0100953static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000954{
955 struct termios tty;
956
Paolo Bonzini03693642010-12-23 13:42:49 +0100957 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100958 if (!echo) {
959 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000960 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100961 tty.c_oflag |= OPOST;
962 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
963 tty.c_cflag &= ~(CSIZE|PARENB);
964 tty.c_cflag |= CS8;
965 tty.c_cc[VMIN] = 1;
966 tty.c_cc[VTIME] = 0;
967 }
Paolo Bonzinibb002512010-12-23 13:42:50 +0100968 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000969 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000970
971 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000972}
973
974static void qemu_chr_close_stdio(struct CharDriverState *chr)
975{
976 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000977 fd_chr_close(chr);
978}
979
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100980static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000981{
982 CharDriverState *chr;
983
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400984 if (is_daemonized()) {
985 error_report("cannot use stdio with -daemonize");
986 return NULL;
987 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530988 old_fd0_flags = fcntl(0, F_GETFL);
989 tcgetattr (0, &oldtty);
990 fcntl(0, F_SETFL, O_NONBLOCK);
991 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100992
aliguori6f97dba2008-10-31 18:49:55 +0000993 chr = qemu_chr_open_fd(0, 1);
994 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100995 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100996 if (opts->has_signal) {
997 stdio_allow_signal = opts->signal;
998 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500999 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001000
Markus Armbruster1f514702012-02-07 15:09:08 +01001001 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001002}
1003
aliguori6f97dba2008-10-31 18:49:55 +00001004#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001005 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1006 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001007
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001008#define HAVE_CHARDEV_TTY 1
1009
aliguori6f97dba2008-10-31 18:49:55 +00001010typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301011 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001012 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001013 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301014 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001015} PtyCharDriver;
1016
1017static void pty_chr_update_read_handler(CharDriverState *chr);
1018static void pty_chr_state(CharDriverState *chr, int connected);
1019
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301020static gboolean pty_chr_timer(gpointer opaque)
1021{
1022 struct CharDriverState *chr = opaque;
1023 PtyCharDriver *s = chr->opaque;
1024
1025 if (s->connected) {
1026 goto out;
1027 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301028
1029 /* Next poll ... */
1030 pty_chr_update_read_handler(chr);
1031
1032out:
Hans de Goede79f20072013-04-25 13:53:02 +02001033 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301034 return FALSE;
1035}
1036
1037static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1038{
1039 PtyCharDriver *s = chr->opaque;
1040
1041 if (s->timer_tag) {
1042 g_source_remove(s->timer_tag);
1043 s->timer_tag = 0;
1044 }
1045
1046 if (ms == 1000) {
1047 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1048 } else {
1049 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1050 }
1051}
1052
aliguori6f97dba2008-10-31 18:49:55 +00001053static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1054{
1055 PtyCharDriver *s = chr->opaque;
1056
1057 if (!s->connected) {
1058 /* guest sends data, check for (re-)connect */
1059 pty_chr_update_read_handler(chr);
1060 return 0;
1061 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001062 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001063}
1064
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301065static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1066{
1067 PtyCharDriver *s = chr->opaque;
1068 return g_io_create_watch(s->fd, cond);
1069}
1070
aliguori6f97dba2008-10-31 18:49:55 +00001071static int pty_chr_read_poll(void *opaque)
1072{
1073 CharDriverState *chr = opaque;
1074 PtyCharDriver *s = chr->opaque;
1075
Anthony Liguori909cda12011-08-15 11:17:31 -05001076 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001077 return s->read_bytes;
1078}
1079
Anthony Liguori093d3a22013-03-05 23:21:20 +05301080static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001081{
1082 CharDriverState *chr = opaque;
1083 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301084 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301085 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301086 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001087
1088 len = sizeof(buf);
1089 if (len > s->read_bytes)
1090 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001091 if (len == 0) {
1092 return TRUE;
1093 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301094 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1095 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001096 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301097 return FALSE;
1098 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001099 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001100 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001101 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301102 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001103}
1104
1105static void pty_chr_update_read_handler(CharDriverState *chr)
1106{
1107 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001108 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001109
Paolo Bonzini85a67692013-04-19 17:32:07 +02001110 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1111 pfd.events = G_IO_OUT;
1112 pfd.revents = 0;
1113 g_poll(&pfd, 1, 0);
1114 if (pfd.revents & G_IO_HUP) {
1115 pty_chr_state(chr, 0);
1116 } else {
1117 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301118 }
aliguori6f97dba2008-10-31 18:49:55 +00001119}
1120
1121static void pty_chr_state(CharDriverState *chr, int connected)
1122{
1123 PtyCharDriver *s = chr->opaque;
1124
1125 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301126 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001127 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001128 /* (re-)connect poll interval for idle guests: once per second.
1129 * We check more frequently in case the guests sends data to
1130 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301131 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001132 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001133 if (s->timer_tag) {
1134 g_source_remove(s->timer_tag);
1135 s->timer_tag = 0;
1136 }
1137 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001138 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001139 qemu_chr_be_generic_open(chr);
Amit Shah7ba9add2013-08-28 15:18:29 +05301140 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1141 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001142 }
aliguori6f97dba2008-10-31 18:49:55 +00001143 }
1144}
1145
aliguori6f97dba2008-10-31 18:49:55 +00001146
1147static void pty_chr_close(struct CharDriverState *chr)
1148{
1149 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301150 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001151
Amit Shah26da70c2013-08-28 15:23:37 +05301152 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301153 fd = g_io_channel_unix_get_fd(s->fd);
1154 g_io_channel_unref(s->fd);
1155 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301156 if (s->timer_tag) {
1157 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001158 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301159 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001160 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001161 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001162}
1163
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001164static CharDriverState *qemu_chr_open_pty(const char *id,
1165 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001166{
1167 CharDriverState *chr;
1168 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001169 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001170 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001171
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001172 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1173 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001174 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001175 }
1176
aliguori6f97dba2008-10-31 18:49:55 +00001177 close(slave_fd);
1178
Markus Armbrustera4e26042011-11-11 10:40:05 +01001179 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001180
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001181 chr->filename = g_strdup_printf("pty:%s", pty_name);
1182 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001183 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001184
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001185 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001186 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001187
1188 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001189 chr->opaque = s;
1190 chr->chr_write = pty_chr_write;
1191 chr->chr_update_read_handler = pty_chr_update_read_handler;
1192 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301193 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001194 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001195
Anthony Liguori093d3a22013-03-05 23:21:20 +05301196 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301197 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001198
Markus Armbruster1f514702012-02-07 15:09:08 +01001199 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001200}
1201
1202static void tty_serial_init(int fd, int speed,
1203 int parity, int data_bits, int stop_bits)
1204{
1205 struct termios tty;
1206 speed_t spd;
1207
1208#if 0
1209 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1210 speed, parity, data_bits, stop_bits);
1211#endif
1212 tcgetattr (fd, &tty);
1213
Stefan Weil45eea132009-10-26 16:10:10 +01001214#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1215 speed = speed * 10 / 11;
1216 do {
1217 check_speed(50);
1218 check_speed(75);
1219 check_speed(110);
1220 check_speed(134);
1221 check_speed(150);
1222 check_speed(200);
1223 check_speed(300);
1224 check_speed(600);
1225 check_speed(1200);
1226 check_speed(1800);
1227 check_speed(2400);
1228 check_speed(4800);
1229 check_speed(9600);
1230 check_speed(19200);
1231 check_speed(38400);
1232 /* Non-Posix values follow. They may be unsupported on some systems. */
1233 check_speed(57600);
1234 check_speed(115200);
1235#ifdef B230400
1236 check_speed(230400);
1237#endif
1238#ifdef B460800
1239 check_speed(460800);
1240#endif
1241#ifdef B500000
1242 check_speed(500000);
1243#endif
1244#ifdef B576000
1245 check_speed(576000);
1246#endif
1247#ifdef B921600
1248 check_speed(921600);
1249#endif
1250#ifdef B1000000
1251 check_speed(1000000);
1252#endif
1253#ifdef B1152000
1254 check_speed(1152000);
1255#endif
1256#ifdef B1500000
1257 check_speed(1500000);
1258#endif
1259#ifdef B2000000
1260 check_speed(2000000);
1261#endif
1262#ifdef B2500000
1263 check_speed(2500000);
1264#endif
1265#ifdef B3000000
1266 check_speed(3000000);
1267#endif
1268#ifdef B3500000
1269 check_speed(3500000);
1270#endif
1271#ifdef B4000000
1272 check_speed(4000000);
1273#endif
aliguori6f97dba2008-10-31 18:49:55 +00001274 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001275 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001276
1277 cfsetispeed(&tty, spd);
1278 cfsetospeed(&tty, spd);
1279
1280 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1281 |INLCR|IGNCR|ICRNL|IXON);
1282 tty.c_oflag |= OPOST;
1283 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1284 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1285 switch(data_bits) {
1286 default:
1287 case 8:
1288 tty.c_cflag |= CS8;
1289 break;
1290 case 7:
1291 tty.c_cflag |= CS7;
1292 break;
1293 case 6:
1294 tty.c_cflag |= CS6;
1295 break;
1296 case 5:
1297 tty.c_cflag |= CS5;
1298 break;
1299 }
1300 switch(parity) {
1301 default:
1302 case 'N':
1303 break;
1304 case 'E':
1305 tty.c_cflag |= PARENB;
1306 break;
1307 case 'O':
1308 tty.c_cflag |= PARENB | PARODD;
1309 break;
1310 }
1311 if (stop_bits == 2)
1312 tty.c_cflag |= CSTOPB;
1313
1314 tcsetattr (fd, TCSANOW, &tty);
1315}
1316
1317static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1318{
1319 FDCharDriver *s = chr->opaque;
1320
1321 switch(cmd) {
1322 case CHR_IOCTL_SERIAL_SET_PARAMS:
1323 {
1324 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301325 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1326 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001327 ssp->data_bits, ssp->stop_bits);
1328 }
1329 break;
1330 case CHR_IOCTL_SERIAL_SET_BREAK:
1331 {
1332 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301333 if (enable) {
1334 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1335 }
aliguori6f97dba2008-10-31 18:49:55 +00001336 }
1337 break;
1338 case CHR_IOCTL_SERIAL_GET_TIOCM:
1339 {
1340 int sarg = 0;
1341 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301342 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001343 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001344 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001345 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001346 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001347 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001348 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001349 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001350 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001351 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001352 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001353 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001354 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001355 *targ |= CHR_TIOCM_RTS;
1356 }
1357 break;
1358 case CHR_IOCTL_SERIAL_SET_TIOCM:
1359 {
1360 int sarg = *(int *)arg;
1361 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301362 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001363 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1364 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1365 if (sarg & CHR_TIOCM_CTS)
1366 targ |= TIOCM_CTS;
1367 if (sarg & CHR_TIOCM_CAR)
1368 targ |= TIOCM_CAR;
1369 if (sarg & CHR_TIOCM_DSR)
1370 targ |= TIOCM_DSR;
1371 if (sarg & CHR_TIOCM_RI)
1372 targ |= TIOCM_RI;
1373 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001374 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001375 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001376 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301377 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001378 }
1379 break;
1380 default:
1381 return -ENOTSUP;
1382 }
1383 return 0;
1384}
1385
David Ahern4266a132010-02-10 18:27:17 -07001386static void qemu_chr_close_tty(CharDriverState *chr)
1387{
1388 FDCharDriver *s = chr->opaque;
1389 int fd = -1;
1390
1391 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301392 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001393 }
1394
1395 fd_chr_close(chr);
1396
1397 if (fd >= 0) {
1398 close(fd);
1399 }
1400}
1401
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001402static CharDriverState *qemu_chr_open_tty_fd(int fd)
1403{
1404 CharDriverState *chr;
1405
1406 tty_serial_init(fd, 115200, 'N', 8, 1);
1407 chr = qemu_chr_open_fd(fd, fd);
1408 chr->chr_ioctl = tty_serial_ioctl;
1409 chr->chr_close = qemu_chr_close_tty;
1410 return chr;
1411}
aliguori6f97dba2008-10-31 18:49:55 +00001412#endif /* __linux__ || __sun__ */
1413
1414#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001415
1416#define HAVE_CHARDEV_PARPORT 1
1417
aliguori6f97dba2008-10-31 18:49:55 +00001418typedef struct {
1419 int fd;
1420 int mode;
1421} ParallelCharDriver;
1422
1423static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1424{
1425 if (s->mode != mode) {
1426 int m = mode;
1427 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1428 return 0;
1429 s->mode = mode;
1430 }
1431 return 1;
1432}
1433
1434static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1435{
1436 ParallelCharDriver *drv = chr->opaque;
1437 int fd = drv->fd;
1438 uint8_t b;
1439
1440 switch(cmd) {
1441 case CHR_IOCTL_PP_READ_DATA:
1442 if (ioctl(fd, PPRDATA, &b) < 0)
1443 return -ENOTSUP;
1444 *(uint8_t *)arg = b;
1445 break;
1446 case CHR_IOCTL_PP_WRITE_DATA:
1447 b = *(uint8_t *)arg;
1448 if (ioctl(fd, PPWDATA, &b) < 0)
1449 return -ENOTSUP;
1450 break;
1451 case CHR_IOCTL_PP_READ_CONTROL:
1452 if (ioctl(fd, PPRCONTROL, &b) < 0)
1453 return -ENOTSUP;
1454 /* Linux gives only the lowest bits, and no way to know data
1455 direction! For better compatibility set the fixed upper
1456 bits. */
1457 *(uint8_t *)arg = b | 0xc0;
1458 break;
1459 case CHR_IOCTL_PP_WRITE_CONTROL:
1460 b = *(uint8_t *)arg;
1461 if (ioctl(fd, PPWCONTROL, &b) < 0)
1462 return -ENOTSUP;
1463 break;
1464 case CHR_IOCTL_PP_READ_STATUS:
1465 if (ioctl(fd, PPRSTATUS, &b) < 0)
1466 return -ENOTSUP;
1467 *(uint8_t *)arg = b;
1468 break;
1469 case CHR_IOCTL_PP_DATA_DIR:
1470 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1471 return -ENOTSUP;
1472 break;
1473 case CHR_IOCTL_PP_EPP_READ_ADDR:
1474 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1475 struct ParallelIOArg *parg = arg;
1476 int n = read(fd, parg->buffer, parg->count);
1477 if (n != parg->count) {
1478 return -EIO;
1479 }
1480 }
1481 break;
1482 case CHR_IOCTL_PP_EPP_READ:
1483 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1484 struct ParallelIOArg *parg = arg;
1485 int n = read(fd, parg->buffer, parg->count);
1486 if (n != parg->count) {
1487 return -EIO;
1488 }
1489 }
1490 break;
1491 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1492 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1493 struct ParallelIOArg *parg = arg;
1494 int n = write(fd, parg->buffer, parg->count);
1495 if (n != parg->count) {
1496 return -EIO;
1497 }
1498 }
1499 break;
1500 case CHR_IOCTL_PP_EPP_WRITE:
1501 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1502 struct ParallelIOArg *parg = arg;
1503 int n = write(fd, parg->buffer, parg->count);
1504 if (n != parg->count) {
1505 return -EIO;
1506 }
1507 }
1508 break;
1509 default:
1510 return -ENOTSUP;
1511 }
1512 return 0;
1513}
1514
1515static void pp_close(CharDriverState *chr)
1516{
1517 ParallelCharDriver *drv = chr->opaque;
1518 int fd = drv->fd;
1519
1520 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1521 ioctl(fd, PPRELEASE);
1522 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001523 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001524 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001525}
1526
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001527static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001528{
1529 CharDriverState *chr;
1530 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001531
1532 if (ioctl(fd, PPCLAIM) < 0) {
1533 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001534 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001535 }
1536
Anthony Liguori7267c092011-08-20 22:09:37 -05001537 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001538 drv->fd = fd;
1539 drv->mode = IEEE1284_MODE_COMPAT;
1540
Anthony Liguori7267c092011-08-20 22:09:37 -05001541 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001542 chr->chr_write = null_chr_write;
1543 chr->chr_ioctl = pp_ioctl;
1544 chr->chr_close = pp_close;
1545 chr->opaque = drv;
1546
Markus Armbruster1f514702012-02-07 15:09:08 +01001547 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001548}
1549#endif /* __linux__ */
1550
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001551#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001552
1553#define HAVE_CHARDEV_PARPORT 1
1554
blueswir16972f932008-11-22 20:49:12 +00001555static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1556{
Stefan Weile0efb992011-02-23 19:09:16 +01001557 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001558 uint8_t b;
1559
1560 switch(cmd) {
1561 case CHR_IOCTL_PP_READ_DATA:
1562 if (ioctl(fd, PPIGDATA, &b) < 0)
1563 return -ENOTSUP;
1564 *(uint8_t *)arg = b;
1565 break;
1566 case CHR_IOCTL_PP_WRITE_DATA:
1567 b = *(uint8_t *)arg;
1568 if (ioctl(fd, PPISDATA, &b) < 0)
1569 return -ENOTSUP;
1570 break;
1571 case CHR_IOCTL_PP_READ_CONTROL:
1572 if (ioctl(fd, PPIGCTRL, &b) < 0)
1573 return -ENOTSUP;
1574 *(uint8_t *)arg = b;
1575 break;
1576 case CHR_IOCTL_PP_WRITE_CONTROL:
1577 b = *(uint8_t *)arg;
1578 if (ioctl(fd, PPISCTRL, &b) < 0)
1579 return -ENOTSUP;
1580 break;
1581 case CHR_IOCTL_PP_READ_STATUS:
1582 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1583 return -ENOTSUP;
1584 *(uint8_t *)arg = b;
1585 break;
1586 default:
1587 return -ENOTSUP;
1588 }
1589 return 0;
1590}
1591
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001592static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001593{
1594 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001595
Anthony Liguori7267c092011-08-20 22:09:37 -05001596 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001597 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001598 chr->chr_write = null_chr_write;
1599 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001600 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001601 return chr;
blueswir16972f932008-11-22 20:49:12 +00001602}
1603#endif
1604
aliguori6f97dba2008-10-31 18:49:55 +00001605#else /* _WIN32 */
1606
1607typedef struct {
1608 int max_size;
1609 HANDLE hcom, hrecv, hsend;
1610 OVERLAPPED orecv, osend;
1611 BOOL fpipe;
1612 DWORD len;
1613} WinCharState;
1614
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001615typedef struct {
1616 HANDLE hStdIn;
1617 HANDLE hInputReadyEvent;
1618 HANDLE hInputDoneEvent;
1619 HANDLE hInputThread;
1620 uint8_t win_stdio_buf;
1621} WinStdioCharState;
1622
aliguori6f97dba2008-10-31 18:49:55 +00001623#define NSENDBUF 2048
1624#define NRECVBUF 2048
1625#define MAXCONNECT 1
1626#define NTIMEOUT 5000
1627
1628static int win_chr_poll(void *opaque);
1629static int win_chr_pipe_poll(void *opaque);
1630
1631static void win_chr_close(CharDriverState *chr)
1632{
1633 WinCharState *s = chr->opaque;
1634
1635 if (s->hsend) {
1636 CloseHandle(s->hsend);
1637 s->hsend = NULL;
1638 }
1639 if (s->hrecv) {
1640 CloseHandle(s->hrecv);
1641 s->hrecv = NULL;
1642 }
1643 if (s->hcom) {
1644 CloseHandle(s->hcom);
1645 s->hcom = NULL;
1646 }
1647 if (s->fpipe)
1648 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1649 else
1650 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301651
Hans de Goedea425d232011-11-19 10:22:43 +01001652 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001653}
1654
1655static int win_chr_init(CharDriverState *chr, const char *filename)
1656{
1657 WinCharState *s = chr->opaque;
1658 COMMCONFIG comcfg;
1659 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1660 COMSTAT comstat;
1661 DWORD size;
1662 DWORD err;
1663
1664 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1665 if (!s->hsend) {
1666 fprintf(stderr, "Failed CreateEvent\n");
1667 goto fail;
1668 }
1669 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1670 if (!s->hrecv) {
1671 fprintf(stderr, "Failed CreateEvent\n");
1672 goto fail;
1673 }
1674
1675 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1676 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1677 if (s->hcom == INVALID_HANDLE_VALUE) {
1678 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1679 s->hcom = NULL;
1680 goto fail;
1681 }
1682
1683 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1684 fprintf(stderr, "Failed SetupComm\n");
1685 goto fail;
1686 }
1687
1688 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1689 size = sizeof(COMMCONFIG);
1690 GetDefaultCommConfig(filename, &comcfg, &size);
1691 comcfg.dcb.DCBlength = sizeof(DCB);
1692 CommConfigDialog(filename, NULL, &comcfg);
1693
1694 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1695 fprintf(stderr, "Failed SetCommState\n");
1696 goto fail;
1697 }
1698
1699 if (!SetCommMask(s->hcom, EV_ERR)) {
1700 fprintf(stderr, "Failed SetCommMask\n");
1701 goto fail;
1702 }
1703
1704 cto.ReadIntervalTimeout = MAXDWORD;
1705 if (!SetCommTimeouts(s->hcom, &cto)) {
1706 fprintf(stderr, "Failed SetCommTimeouts\n");
1707 goto fail;
1708 }
1709
1710 if (!ClearCommError(s->hcom, &err, &comstat)) {
1711 fprintf(stderr, "Failed ClearCommError\n");
1712 goto fail;
1713 }
1714 qemu_add_polling_cb(win_chr_poll, chr);
1715 return 0;
1716
1717 fail:
1718 win_chr_close(chr);
1719 return -1;
1720}
1721
1722static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1723{
1724 WinCharState *s = chr->opaque;
1725 DWORD len, ret, size, err;
1726
1727 len = len1;
1728 ZeroMemory(&s->osend, sizeof(s->osend));
1729 s->osend.hEvent = s->hsend;
1730 while (len > 0) {
1731 if (s->hsend)
1732 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1733 else
1734 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1735 if (!ret) {
1736 err = GetLastError();
1737 if (err == ERROR_IO_PENDING) {
1738 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1739 if (ret) {
1740 buf += size;
1741 len -= size;
1742 } else {
1743 break;
1744 }
1745 } else {
1746 break;
1747 }
1748 } else {
1749 buf += size;
1750 len -= size;
1751 }
1752 }
1753 return len1 - len;
1754}
1755
1756static int win_chr_read_poll(CharDriverState *chr)
1757{
1758 WinCharState *s = chr->opaque;
1759
Anthony Liguori909cda12011-08-15 11:17:31 -05001760 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001761 return s->max_size;
1762}
1763
1764static void win_chr_readfile(CharDriverState *chr)
1765{
1766 WinCharState *s = chr->opaque;
1767 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301768 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001769 DWORD size;
1770
1771 ZeroMemory(&s->orecv, sizeof(s->orecv));
1772 s->orecv.hEvent = s->hrecv;
1773 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1774 if (!ret) {
1775 err = GetLastError();
1776 if (err == ERROR_IO_PENDING) {
1777 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1778 }
1779 }
1780
1781 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001782 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001783 }
1784}
1785
1786static void win_chr_read(CharDriverState *chr)
1787{
1788 WinCharState *s = chr->opaque;
1789
1790 if (s->len > s->max_size)
1791 s->len = s->max_size;
1792 if (s->len == 0)
1793 return;
1794
1795 win_chr_readfile(chr);
1796}
1797
1798static int win_chr_poll(void *opaque)
1799{
1800 CharDriverState *chr = opaque;
1801 WinCharState *s = chr->opaque;
1802 COMSTAT status;
1803 DWORD comerr;
1804
1805 ClearCommError(s->hcom, &comerr, &status);
1806 if (status.cbInQue > 0) {
1807 s->len = status.cbInQue;
1808 win_chr_read_poll(chr);
1809 win_chr_read(chr);
1810 return 1;
1811 }
1812 return 0;
1813}
1814
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001815static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001816{
1817 CharDriverState *chr;
1818 WinCharState *s;
1819
Anthony Liguori7267c092011-08-20 22:09:37 -05001820 chr = g_malloc0(sizeof(CharDriverState));
1821 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001822 chr->opaque = s;
1823 chr->chr_write = win_chr_write;
1824 chr->chr_close = win_chr_close;
1825
1826 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001827 g_free(s);
1828 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001829 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001830 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001831 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001832}
1833
1834static int win_chr_pipe_poll(void *opaque)
1835{
1836 CharDriverState *chr = opaque;
1837 WinCharState *s = chr->opaque;
1838 DWORD size;
1839
1840 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1841 if (size > 0) {
1842 s->len = size;
1843 win_chr_read_poll(chr);
1844 win_chr_read(chr);
1845 return 1;
1846 }
1847 return 0;
1848}
1849
1850static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1851{
1852 WinCharState *s = chr->opaque;
1853 OVERLAPPED ov;
1854 int ret;
1855 DWORD size;
1856 char openname[256];
1857
1858 s->fpipe = TRUE;
1859
1860 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1861 if (!s->hsend) {
1862 fprintf(stderr, "Failed CreateEvent\n");
1863 goto fail;
1864 }
1865 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1866 if (!s->hrecv) {
1867 fprintf(stderr, "Failed CreateEvent\n");
1868 goto fail;
1869 }
1870
1871 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1872 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1873 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1874 PIPE_WAIT,
1875 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1876 if (s->hcom == INVALID_HANDLE_VALUE) {
1877 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1878 s->hcom = NULL;
1879 goto fail;
1880 }
1881
1882 ZeroMemory(&ov, sizeof(ov));
1883 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1884 ret = ConnectNamedPipe(s->hcom, &ov);
1885 if (ret) {
1886 fprintf(stderr, "Failed ConnectNamedPipe\n");
1887 goto fail;
1888 }
1889
1890 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1891 if (!ret) {
1892 fprintf(stderr, "Failed GetOverlappedResult\n");
1893 if (ov.hEvent) {
1894 CloseHandle(ov.hEvent);
1895 ov.hEvent = NULL;
1896 }
1897 goto fail;
1898 }
1899
1900 if (ov.hEvent) {
1901 CloseHandle(ov.hEvent);
1902 ov.hEvent = NULL;
1903 }
1904 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1905 return 0;
1906
1907 fail:
1908 win_chr_close(chr);
1909 return -1;
1910}
1911
1912
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001913static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001914{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001915 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001916 CharDriverState *chr;
1917 WinCharState *s;
1918
Anthony Liguori7267c092011-08-20 22:09:37 -05001919 chr = g_malloc0(sizeof(CharDriverState));
1920 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001921 chr->opaque = s;
1922 chr->chr_write = win_chr_write;
1923 chr->chr_close = win_chr_close;
1924
1925 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001926 g_free(s);
1927 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001928 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001929 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001930 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001931}
1932
Markus Armbruster1f514702012-02-07 15:09:08 +01001933static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001934{
1935 CharDriverState *chr;
1936 WinCharState *s;
1937
Anthony Liguori7267c092011-08-20 22:09:37 -05001938 chr = g_malloc0(sizeof(CharDriverState));
1939 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001940 s->hcom = fd_out;
1941 chr->opaque = s;
1942 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001943 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001944}
1945
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001946static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001947{
Markus Armbruster1f514702012-02-07 15:09:08 +01001948 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001949}
1950
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001951static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1952{
1953 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1954 DWORD dwSize;
1955 int len1;
1956
1957 len1 = len;
1958
1959 while (len1 > 0) {
1960 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1961 break;
1962 }
1963 buf += dwSize;
1964 len1 -= dwSize;
1965 }
1966
1967 return len - len1;
1968}
1969
1970static void win_stdio_wait_func(void *opaque)
1971{
1972 CharDriverState *chr = opaque;
1973 WinStdioCharState *stdio = chr->opaque;
1974 INPUT_RECORD buf[4];
1975 int ret;
1976 DWORD dwSize;
1977 int i;
1978
1979 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1980 &dwSize);
1981
1982 if (!ret) {
1983 /* Avoid error storm */
1984 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1985 return;
1986 }
1987
1988 for (i = 0; i < dwSize; i++) {
1989 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1990
1991 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1992 int j;
1993 if (kev->uChar.AsciiChar != 0) {
1994 for (j = 0; j < kev->wRepeatCount; j++) {
1995 if (qemu_chr_be_can_write(chr)) {
1996 uint8_t c = kev->uChar.AsciiChar;
1997 qemu_chr_be_write(chr, &c, 1);
1998 }
1999 }
2000 }
2001 }
2002 }
2003}
2004
2005static DWORD WINAPI win_stdio_thread(LPVOID param)
2006{
2007 CharDriverState *chr = param;
2008 WinStdioCharState *stdio = chr->opaque;
2009 int ret;
2010 DWORD dwSize;
2011
2012 while (1) {
2013
2014 /* Wait for one byte */
2015 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2016
2017 /* Exit in case of error, continue if nothing read */
2018 if (!ret) {
2019 break;
2020 }
2021 if (!dwSize) {
2022 continue;
2023 }
2024
2025 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2026 if (stdio->win_stdio_buf == '\r') {
2027 continue;
2028 }
2029
2030 /* Signal the main thread and wait until the byte was eaten */
2031 if (!SetEvent(stdio->hInputReadyEvent)) {
2032 break;
2033 }
2034 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2035 != WAIT_OBJECT_0) {
2036 break;
2037 }
2038 }
2039
2040 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2041 return 0;
2042}
2043
2044static void win_stdio_thread_wait_func(void *opaque)
2045{
2046 CharDriverState *chr = opaque;
2047 WinStdioCharState *stdio = chr->opaque;
2048
2049 if (qemu_chr_be_can_write(chr)) {
2050 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2051 }
2052
2053 SetEvent(stdio->hInputDoneEvent);
2054}
2055
2056static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2057{
2058 WinStdioCharState *stdio = chr->opaque;
2059 DWORD dwMode = 0;
2060
2061 GetConsoleMode(stdio->hStdIn, &dwMode);
2062
2063 if (echo) {
2064 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2065 } else {
2066 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2067 }
2068}
2069
2070static void win_stdio_close(CharDriverState *chr)
2071{
2072 WinStdioCharState *stdio = chr->opaque;
2073
2074 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2075 CloseHandle(stdio->hInputReadyEvent);
2076 }
2077 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2078 CloseHandle(stdio->hInputDoneEvent);
2079 }
2080 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2081 TerminateThread(stdio->hInputThread, 0);
2082 }
2083
2084 g_free(chr->opaque);
2085 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002086}
2087
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002088static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002089{
2090 CharDriverState *chr;
2091 WinStdioCharState *stdio;
2092 DWORD dwMode;
2093 int is_console = 0;
2094
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002095 chr = g_malloc0(sizeof(CharDriverState));
2096 stdio = g_malloc0(sizeof(WinStdioCharState));
2097
2098 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2099 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2100 fprintf(stderr, "cannot open stdio: invalid handle\n");
2101 exit(1);
2102 }
2103
2104 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2105
2106 chr->opaque = stdio;
2107 chr->chr_write = win_stdio_write;
2108 chr->chr_close = win_stdio_close;
2109
Anthony Liguoried7a1542013-03-05 23:21:17 +05302110 if (is_console) {
2111 if (qemu_add_wait_object(stdio->hStdIn,
2112 win_stdio_wait_func, chr)) {
2113 fprintf(stderr, "qemu_add_wait_object: failed\n");
2114 }
2115 } else {
2116 DWORD dwId;
2117
2118 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2119 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2120 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2121 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002122
Anthony Liguoried7a1542013-03-05 23:21:17 +05302123 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2124 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2125 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2126 fprintf(stderr, "cannot create stdio thread or event\n");
2127 exit(1);
2128 }
2129 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2130 win_stdio_thread_wait_func, chr)) {
2131 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002132 }
2133 }
2134
2135 dwMode |= ENABLE_LINE_INPUT;
2136
Anthony Liguoried7a1542013-03-05 23:21:17 +05302137 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002138 /* set the terminal in raw mode */
2139 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2140 dwMode |= ENABLE_PROCESSED_INPUT;
2141 }
2142
2143 SetConsoleMode(stdio->hStdIn, dwMode);
2144
2145 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2146 qemu_chr_fe_set_echo(chr, false);
2147
Markus Armbruster1f514702012-02-07 15:09:08 +01002148 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002149}
aliguori6f97dba2008-10-31 18:49:55 +00002150#endif /* !_WIN32 */
2151
Anthony Liguori5ab82112013-03-05 23:21:31 +05302152
aliguori6f97dba2008-10-31 18:49:55 +00002153/***********************************************************/
2154/* UDP Net console */
2155
2156typedef struct {
2157 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302158 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302159 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002160 int bufcnt;
2161 int bufptr;
2162 int max_size;
2163} NetCharDriver;
2164
2165static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2166{
2167 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302168 gsize bytes_written;
2169 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002170
Anthony Liguori76a96442013-03-05 23:21:21 +05302171 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2172 if (status == G_IO_STATUS_EOF) {
2173 return 0;
2174 } else if (status != G_IO_STATUS_NORMAL) {
2175 return -1;
2176 }
2177
2178 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002179}
2180
2181static int udp_chr_read_poll(void *opaque)
2182{
2183 CharDriverState *chr = opaque;
2184 NetCharDriver *s = chr->opaque;
2185
Anthony Liguori909cda12011-08-15 11:17:31 -05002186 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002187
2188 /* If there were any stray characters in the queue process them
2189 * first
2190 */
2191 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002192 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002193 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002194 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002195 }
2196 return s->max_size;
2197}
2198
Anthony Liguori76a96442013-03-05 23:21:21 +05302199static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002200{
2201 CharDriverState *chr = opaque;
2202 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302203 gsize bytes_read = 0;
2204 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002205
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002206 if (s->max_size == 0) {
2207 return TRUE;
2208 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302209 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2210 &bytes_read, NULL);
2211 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002212 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302213 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302214 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302215 return FALSE;
2216 }
aliguori6f97dba2008-10-31 18:49:55 +00002217
2218 s->bufptr = 0;
2219 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002220 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002221 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002222 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002223 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302224
2225 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002226}
2227
2228static void udp_chr_update_read_handler(CharDriverState *chr)
2229{
2230 NetCharDriver *s = chr->opaque;
2231
Amit Shah26da70c2013-08-28 15:23:37 +05302232 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302233 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302234 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2235 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002236 }
2237}
2238
aliguori819f56b2009-03-28 17:58:14 +00002239static void udp_chr_close(CharDriverState *chr)
2240{
2241 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302242
2243 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302244 if (s->chan) {
2245 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002246 closesocket(s->fd);
2247 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002248 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002249 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002250}
2251
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002252static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002253{
2254 CharDriverState *chr = NULL;
2255 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002256
Anthony Liguori7267c092011-08-20 22:09:37 -05002257 chr = g_malloc0(sizeof(CharDriverState));
2258 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002259
aliguori6f97dba2008-10-31 18:49:55 +00002260 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302261 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002262 s->bufcnt = 0;
2263 s->bufptr = 0;
2264 chr->opaque = s;
2265 chr->chr_write = udp_chr_write;
2266 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002267 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002268 /* be isn't opened until we get a connection */
2269 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002270 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002271}
aliguori6f97dba2008-10-31 18:49:55 +00002272
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002273static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2274{
2275 Error *local_err = NULL;
2276 int fd = -1;
2277
2278 fd = inet_dgram_opts(opts, &local_err);
2279 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002280 qerror_report_err(local_err);
2281 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002282 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002283 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002284 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002285}
2286
2287/***********************************************************/
2288/* TCP Net console */
2289
2290typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302291
2292 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302293 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002294 int fd, listen_fd;
2295 int connected;
2296 int max_size;
2297 int do_telnetopt;
2298 int do_nodelay;
2299 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002300 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002301} TCPCharDriver;
2302
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302303static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002304
2305static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2306{
2307 TCPCharDriver *s = chr->opaque;
2308 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002309 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002310 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002311 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002312 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002313 }
2314}
2315
2316static int tcp_chr_read_poll(void *opaque)
2317{
2318 CharDriverState *chr = opaque;
2319 TCPCharDriver *s = chr->opaque;
2320 if (!s->connected)
2321 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002322 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002323 return s->max_size;
2324}
2325
2326#define IAC 255
2327#define IAC_BREAK 243
2328static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2329 TCPCharDriver *s,
2330 uint8_t *buf, int *size)
2331{
2332 /* Handle any telnet client's basic IAC options to satisfy char by
2333 * char mode with no echo. All IAC options will be removed from
2334 * the buf and the do_telnetopt variable will be used to track the
2335 * state of the width of the IAC information.
2336 *
2337 * IAC commands come in sets of 3 bytes with the exception of the
2338 * "IAC BREAK" command and the double IAC.
2339 */
2340
2341 int i;
2342 int j = 0;
2343
2344 for (i = 0; i < *size; i++) {
2345 if (s->do_telnetopt > 1) {
2346 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2347 /* Double IAC means send an IAC */
2348 if (j != i)
2349 buf[j] = buf[i];
2350 j++;
2351 s->do_telnetopt = 1;
2352 } else {
2353 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2354 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002355 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002356 s->do_telnetopt++;
2357 }
2358 s->do_telnetopt++;
2359 }
2360 if (s->do_telnetopt >= 4) {
2361 s->do_telnetopt = 1;
2362 }
2363 } else {
2364 if ((unsigned char)buf[i] == IAC) {
2365 s->do_telnetopt = 2;
2366 } else {
2367 if (j != i)
2368 buf[j] = buf[i];
2369 j++;
2370 }
2371 }
2372 }
2373 *size = j;
2374}
2375
Mark McLoughlin7d174052009-07-22 09:11:39 +01002376static int tcp_get_msgfd(CharDriverState *chr)
2377{
2378 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002379 int fd = s->msgfd;
2380 s->msgfd = -1;
2381 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002382}
2383
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002384#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002385static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2386{
2387 TCPCharDriver *s = chr->opaque;
2388 struct cmsghdr *cmsg;
2389
2390 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2391 int fd;
2392
2393 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2394 cmsg->cmsg_level != SOL_SOCKET ||
2395 cmsg->cmsg_type != SCM_RIGHTS)
2396 continue;
2397
2398 fd = *((int *)CMSG_DATA(cmsg));
2399 if (fd < 0)
2400 continue;
2401
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002402 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2403 qemu_set_block(fd);
2404
Corey Bryant06138652012-08-14 16:43:42 -04002405#ifndef MSG_CMSG_CLOEXEC
2406 qemu_set_cloexec(fd);
2407#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002408 if (s->msgfd != -1)
2409 close(s->msgfd);
2410 s->msgfd = fd;
2411 }
2412}
2413
Mark McLoughlin9977c892009-07-22 09:11:38 +01002414static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2415{
2416 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002417 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002418 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002419 union {
2420 struct cmsghdr cmsg;
2421 char control[CMSG_SPACE(sizeof(int))];
2422 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002423 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002424 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002425
2426 iov[0].iov_base = buf;
2427 iov[0].iov_len = len;
2428
2429 msg.msg_iov = iov;
2430 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002431 msg.msg_control = &msg_control;
2432 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002433
Corey Bryant06138652012-08-14 16:43:42 -04002434#ifdef MSG_CMSG_CLOEXEC
2435 flags |= MSG_CMSG_CLOEXEC;
2436#endif
2437 ret = recvmsg(s->fd, &msg, flags);
2438 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002439 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002440 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002441
2442 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002443}
2444#else
2445static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2446{
2447 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002448 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002449}
2450#endif
2451
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302452static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2453{
2454 TCPCharDriver *s = chr->opaque;
2455 return g_io_create_watch(s->chan, cond);
2456}
2457
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302458static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002459{
2460 CharDriverState *chr = opaque;
2461 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302462 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002463 int len, size;
2464
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302465 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002466 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302467 }
aliguori6f97dba2008-10-31 18:49:55 +00002468 len = sizeof(buf);
2469 if (len > s->max_size)
2470 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002471 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002472 if (size == 0) {
2473 /* connection closed */
2474 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302475 if (s->listen_chan) {
2476 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002477 }
Amit Shah26da70c2013-08-28 15:23:37 +05302478 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302479 g_io_channel_unref(s->chan);
2480 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002481 closesocket(s->fd);
2482 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002483 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002484 } else if (size > 0) {
2485 if (s->do_telnetopt)
2486 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2487 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002488 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002489 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302490
2491 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002492}
2493
Blue Swirl68c18d12010-08-15 09:46:24 +00002494#ifndef _WIN32
2495CharDriverState *qemu_chr_open_eventfd(int eventfd)
2496{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002497 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002498}
Blue Swirl68c18d12010-08-15 09:46:24 +00002499#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002500
aliguori6f97dba2008-10-31 18:49:55 +00002501static void tcp_chr_connect(void *opaque)
2502{
2503 CharDriverState *chr = opaque;
2504 TCPCharDriver *s = chr->opaque;
2505
2506 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302507 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302508 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2509 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002510 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002511 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002512}
2513
2514#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2515static void tcp_chr_telnet_init(int fd)
2516{
2517 char buf[3];
2518 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2519 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2520 send(fd, (char *)buf, 3, 0);
2521 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2522 send(fd, (char *)buf, 3, 0);
2523 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2524 send(fd, (char *)buf, 3, 0);
2525 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2526 send(fd, (char *)buf, 3, 0);
2527}
2528
Daniel P. Berrange13661082011-06-23 13:31:42 +01002529static int tcp_chr_add_client(CharDriverState *chr, int fd)
2530{
2531 TCPCharDriver *s = chr->opaque;
2532 if (s->fd != -1)
2533 return -1;
2534
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002535 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002536 if (s->do_nodelay)
2537 socket_set_nodelay(fd);
2538 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302539 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002540 if (s->listen_tag) {
2541 g_source_remove(s->listen_tag);
2542 s->listen_tag = 0;
2543 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002544 tcp_chr_connect(chr);
2545
2546 return 0;
2547}
2548
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302549static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002550{
2551 CharDriverState *chr = opaque;
2552 TCPCharDriver *s = chr->opaque;
2553 struct sockaddr_in saddr;
2554#ifndef _WIN32
2555 struct sockaddr_un uaddr;
2556#endif
2557 struct sockaddr *addr;
2558 socklen_t len;
2559 int fd;
2560
2561 for(;;) {
2562#ifndef _WIN32
2563 if (s->is_unix) {
2564 len = sizeof(uaddr);
2565 addr = (struct sockaddr *)&uaddr;
2566 } else
2567#endif
2568 {
2569 len = sizeof(saddr);
2570 addr = (struct sockaddr *)&saddr;
2571 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002572 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002573 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002574 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302575 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002576 } else if (fd >= 0) {
2577 if (s->do_telnetopt)
2578 tcp_chr_telnet_init(fd);
2579 break;
2580 }
2581 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002582 if (tcp_chr_add_client(chr, fd) < 0)
2583 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302584
2585 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002586}
2587
2588static void tcp_chr_close(CharDriverState *chr)
2589{
2590 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002591 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302592 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302593 if (s->chan) {
2594 g_io_channel_unref(s->chan);
2595 }
aliguori6f97dba2008-10-31 18:49:55 +00002596 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002597 }
2598 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302599 if (s->listen_tag) {
2600 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002601 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302602 }
2603 if (s->listen_chan) {
2604 g_io_channel_unref(s->listen_chan);
2605 }
aliguori6f97dba2008-10-31 18:49:55 +00002606 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002607 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002608 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002609 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002610}
2611
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002612static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2613 bool is_listen, bool is_telnet,
2614 bool is_waitconnect,
2615 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002616{
2617 CharDriverState *chr = NULL;
2618 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002619 char host[NI_MAXHOST], serv[NI_MAXSERV];
2620 const char *left = "", *right = "";
2621 struct sockaddr_storage ss;
2622 socklen_t ss_len = sizeof(ss);
2623
2624 memset(&ss, 0, ss_len);
2625 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002626 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002627 return NULL;
2628 }
2629
2630 chr = g_malloc0(sizeof(CharDriverState));
2631 s = g_malloc0(sizeof(TCPCharDriver));
2632
2633 s->connected = 0;
2634 s->fd = -1;
2635 s->listen_fd = -1;
2636 s->msgfd = -1;
2637
2638 chr->filename = g_malloc(256);
2639 switch (ss.ss_family) {
2640#ifndef _WIN32
2641 case AF_UNIX:
2642 s->is_unix = 1;
2643 snprintf(chr->filename, 256, "unix:%s%s",
2644 ((struct sockaddr_un *)(&ss))->sun_path,
2645 is_listen ? ",server" : "");
2646 break;
2647#endif
2648 case AF_INET6:
2649 left = "[";
2650 right = "]";
2651 /* fall through */
2652 case AF_INET:
2653 s->do_nodelay = do_nodelay;
2654 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2655 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002656 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002657 is_telnet ? "telnet" : "tcp",
2658 left, host, right, serv,
2659 is_listen ? ",server" : "");
2660 break;
2661 }
2662
2663 chr->opaque = s;
2664 chr->chr_write = tcp_chr_write;
2665 chr->chr_close = tcp_chr_close;
2666 chr->get_msgfd = tcp_get_msgfd;
2667 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302668 chr->chr_add_watch = tcp_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002669 /* be isn't opened until we get a connection */
2670 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002671
2672 if (is_listen) {
2673 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302674 s->listen_chan = io_channel_from_socket(s->listen_fd);
2675 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002676 if (is_telnet) {
2677 s->do_telnetopt = 1;
2678 }
2679 } else {
2680 s->connected = 1;
2681 s->fd = fd;
2682 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302683 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002684 tcp_chr_connect(chr);
2685 }
2686
2687 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002688 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2689 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302690 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002691 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002692 }
2693 return chr;
2694}
2695
2696static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2697{
2698 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002699 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002700 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002701
liguange990a392013-06-18 11:45:35 +08002702 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2703 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2704 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2705 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2706 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002707
aliguorif07b6002008-11-11 20:54:09 +00002708 if (is_unix) {
2709 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002710 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002711 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002712 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002713 }
2714 } else {
2715 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002716 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002717 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002718 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002719 }
2720 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002721 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002722 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002723 }
aliguori6f97dba2008-10-31 18:49:55 +00002724
2725 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002726 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002727
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002728 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2729 is_waitconnect, &local_err);
2730 if (error_is_set(&local_err)) {
2731 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002732 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002733 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002734
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002735
aliguori6f97dba2008-10-31 18:49:55 +00002736 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002737 if (local_err) {
2738 qerror_report_err(local_err);
2739 error_free(local_err);
2740 }
2741 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002742 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002743 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002744 if (chr) {
2745 g_free(chr->opaque);
2746 g_free(chr);
2747 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002748 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002749}
2750
Lei Li51767e72013-01-25 00:03:19 +08002751/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002752/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002753
2754typedef struct {
2755 size_t size;
2756 size_t prod;
2757 size_t cons;
2758 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002759} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002760
Markus Armbruster3949e592013-02-06 21:27:24 +01002761static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002762{
Markus Armbruster3949e592013-02-06 21:27:24 +01002763 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002764
Markus Armbruster5c230102013-02-06 21:27:23 +01002765 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002766}
2767
Markus Armbruster3949e592013-02-06 21:27:24 +01002768static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002769{
Markus Armbruster3949e592013-02-06 21:27:24 +01002770 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002771 int i;
2772
2773 if (!buf || (len < 0)) {
2774 return -1;
2775 }
2776
2777 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002778 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2779 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002780 d->cons = d->prod - d->size;
2781 }
2782 }
2783
2784 return 0;
2785}
2786
Markus Armbruster3949e592013-02-06 21:27:24 +01002787static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002788{
Markus Armbruster3949e592013-02-06 21:27:24 +01002789 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002790 int i;
2791
Markus Armbruster5c230102013-02-06 21:27:23 +01002792 for (i = 0; i < len && d->cons != d->prod; i++) {
2793 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002794 }
2795
2796 return i;
2797}
2798
Markus Armbruster3949e592013-02-06 21:27:24 +01002799static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002800{
Markus Armbruster3949e592013-02-06 21:27:24 +01002801 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002802
2803 g_free(d->cbuf);
2804 g_free(d);
2805 chr->opaque = NULL;
2806}
2807
Markus Armbruster4f573782013-07-26 16:44:32 +02002808static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2809 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002810{
2811 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002812 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002813
2814 chr = g_malloc0(sizeof(CharDriverState));
2815 d = g_malloc(sizeof(*d));
2816
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002817 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002818
2819 /* The size must be power of 2 */
2820 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02002821 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002822 goto fail;
2823 }
2824
2825 d->prod = 0;
2826 d->cons = 0;
2827 d->cbuf = g_malloc0(d->size);
2828
2829 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002830 chr->chr_write = ringbuf_chr_write;
2831 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002832
2833 return chr;
2834
2835fail:
2836 g_free(d);
2837 g_free(chr);
2838 return NULL;
2839}
2840
Markus Armbruster3949e592013-02-06 21:27:24 +01002841static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002842{
Markus Armbruster3949e592013-02-06 21:27:24 +01002843 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002844}
2845
Markus Armbruster3949e592013-02-06 21:27:24 +01002846void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002847 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002848 Error **errp)
2849{
2850 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002851 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002852 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10002853 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002854
2855 chr = qemu_chr_find(device);
2856 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002857 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002858 return;
2859 }
2860
Markus Armbruster3949e592013-02-06 21:27:24 +01002861 if (!chr_is_ringbuf(chr)) {
2862 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002863 return;
2864 }
2865
Lei Li1f590cf2013-01-25 00:03:20 +08002866 if (has_format && (format == DATA_FORMAT_BASE64)) {
2867 write_data = g_base64_decode(data, &write_count);
2868 } else {
2869 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002870 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002871 }
2872
Markus Armbruster3949e592013-02-06 21:27:24 +01002873 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002874
Markus Armbruster13289fb2013-02-06 21:27:18 +01002875 if (write_data != (uint8_t *)data) {
2876 g_free((void *)write_data);
2877 }
2878
Lei Li1f590cf2013-01-25 00:03:20 +08002879 if (ret < 0) {
2880 error_setg(errp, "Failed to write to device %s", device);
2881 return;
2882 }
2883}
2884
Markus Armbruster3949e592013-02-06 21:27:24 +01002885char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002886 bool has_format, enum DataFormat format,
2887 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002888{
2889 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002890 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002891 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002892 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002893
2894 chr = qemu_chr_find(device);
2895 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002896 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002897 return NULL;
2898 }
2899
Markus Armbruster3949e592013-02-06 21:27:24 +01002900 if (!chr_is_ringbuf(chr)) {
2901 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002902 return NULL;
2903 }
2904
2905 if (size <= 0) {
2906 error_setg(errp, "size must be greater than zero");
2907 return NULL;
2908 }
2909
Markus Armbruster3949e592013-02-06 21:27:24 +01002910 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002911 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002912 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002913
Markus Armbruster3949e592013-02-06 21:27:24 +01002914 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002915
2916 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002917 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002918 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002919 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002920 /*
2921 * FIXME should read only complete, valid UTF-8 characters up
2922 * to @size bytes. Invalid sequences should be replaced by a
2923 * suitable replacement character. Except when (and only
2924 * when) ring buffer lost characters since last read, initial
2925 * continuation characters should be dropped.
2926 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002927 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002928 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002929 }
2930
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002931 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002932}
2933
Gerd Hoffmann33521632009-12-08 13:11:49 +01002934QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002935{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002936 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002937 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002938 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002939 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002940 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002941
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002942 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2943 if (error_is_set(&local_err)) {
2944 qerror_report_err(local_err);
2945 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002946 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002947 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002948
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002949 if (strstart(filename, "mon:", &p)) {
2950 filename = p;
2951 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04002952 if (strcmp(filename, "stdio") == 0) {
2953 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2954 * but pass it to the guest. Handle this only for compat syntax,
2955 * for -chardev syntax we have special option for this.
2956 * This is what -nographic did, redirecting+muxing serial+monitor
2957 * to stdio causing Ctrl+C to be passed to guest. */
2958 qemu_opt_set(opts, "signal", "off");
2959 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002960 }
2961
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002962 if (strcmp(filename, "null") == 0 ||
2963 strcmp(filename, "pty") == 0 ||
2964 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002965 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002966 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002967 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002968 return opts;
2969 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002970 if (strstart(filename, "vc", &p)) {
2971 qemu_opt_set(opts, "backend", "vc");
2972 if (*p == ':') {
2973 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2974 /* pixels */
2975 qemu_opt_set(opts, "width", width);
2976 qemu_opt_set(opts, "height", height);
2977 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2978 /* chars */
2979 qemu_opt_set(opts, "cols", width);
2980 qemu_opt_set(opts, "rows", height);
2981 } else {
2982 goto fail;
2983 }
2984 }
2985 return opts;
2986 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002987 if (strcmp(filename, "con:") == 0) {
2988 qemu_opt_set(opts, "backend", "console");
2989 return opts;
2990 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002991 if (strstart(filename, "COM", NULL)) {
2992 qemu_opt_set(opts, "backend", "serial");
2993 qemu_opt_set(opts, "path", filename);
2994 return opts;
2995 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002996 if (strstart(filename, "file:", &p)) {
2997 qemu_opt_set(opts, "backend", "file");
2998 qemu_opt_set(opts, "path", p);
2999 return opts;
3000 }
3001 if (strstart(filename, "pipe:", &p)) {
3002 qemu_opt_set(opts, "backend", "pipe");
3003 qemu_opt_set(opts, "path", p);
3004 return opts;
3005 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003006 if (strstart(filename, "tcp:", &p) ||
3007 strstart(filename, "telnet:", &p)) {
3008 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3009 host[0] = 0;
3010 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3011 goto fail;
3012 }
3013 qemu_opt_set(opts, "backend", "socket");
3014 qemu_opt_set(opts, "host", host);
3015 qemu_opt_set(opts, "port", port);
3016 if (p[pos] == ',') {
3017 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3018 goto fail;
3019 }
3020 if (strstart(filename, "telnet:", &p))
3021 qemu_opt_set(opts, "telnet", "on");
3022 return opts;
3023 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003024 if (strstart(filename, "udp:", &p)) {
3025 qemu_opt_set(opts, "backend", "udp");
3026 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3027 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003028 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003029 goto fail;
3030 }
3031 }
3032 qemu_opt_set(opts, "host", host);
3033 qemu_opt_set(opts, "port", port);
3034 if (p[pos] == '@') {
3035 p += pos + 1;
3036 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3037 host[0] = 0;
3038 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003039 goto fail;
3040 }
3041 }
3042 qemu_opt_set(opts, "localaddr", host);
3043 qemu_opt_set(opts, "localport", port);
3044 }
3045 return opts;
3046 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003047 if (strstart(filename, "unix:", &p)) {
3048 qemu_opt_set(opts, "backend", "socket");
3049 if (qemu_opts_do_parse(opts, p, "path") != 0)
3050 goto fail;
3051 return opts;
3052 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003053 if (strstart(filename, "/dev/parport", NULL) ||
3054 strstart(filename, "/dev/ppi", NULL)) {
3055 qemu_opt_set(opts, "backend", "parport");
3056 qemu_opt_set(opts, "path", filename);
3057 return opts;
3058 }
3059 if (strstart(filename, "/dev/", NULL)) {
3060 qemu_opt_set(opts, "backend", "tty");
3061 qemu_opt_set(opts, "path", filename);
3062 return opts;
3063 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003064
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003065fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003066 qemu_opts_del(opts);
3067 return NULL;
3068}
3069
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003070static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3071 Error **errp)
3072{
3073 const char *path = qemu_opt_get(opts, "path");
3074
3075 if (path == NULL) {
3076 error_setg(errp, "chardev: file: no filename given");
3077 return;
3078 }
3079 backend->file = g_new0(ChardevFile, 1);
3080 backend->file->out = g_strdup(path);
3081}
3082
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003083static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3084 Error **errp)
3085{
3086 backend->stdio = g_new0(ChardevStdio, 1);
3087 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003088 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003089}
3090
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003091static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3092 Error **errp)
3093{
3094 const char *device = qemu_opt_get(opts, "path");
3095
3096 if (device == NULL) {
3097 error_setg(errp, "chardev: serial/tty: no device path given");
3098 return;
3099 }
3100 backend->serial = g_new0(ChardevHostdev, 1);
3101 backend->serial->device = g_strdup(device);
3102}
3103
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003104static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3105 Error **errp)
3106{
3107 const char *device = qemu_opt_get(opts, "path");
3108
3109 if (device == NULL) {
3110 error_setg(errp, "chardev: parallel: no device path given");
3111 return;
3112 }
3113 backend->parallel = g_new0(ChardevHostdev, 1);
3114 backend->parallel->device = g_strdup(device);
3115}
3116
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003117static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3118 Error **errp)
3119{
3120 const char *device = qemu_opt_get(opts, "path");
3121
3122 if (device == NULL) {
3123 error_setg(errp, "chardev: pipe: no device path given");
3124 return;
3125 }
3126 backend->pipe = g_new0(ChardevHostdev, 1);
3127 backend->pipe->device = g_strdup(device);
3128}
3129
Markus Armbruster4f573782013-07-26 16:44:32 +02003130static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3131 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003132{
3133 int val;
3134
Markus Armbruster3a1da422013-07-26 16:44:34 +02003135 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003136
Markus Armbruster0f953052013-06-27 16:22:07 +02003137 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003138 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003139 backend->ringbuf->has_size = true;
3140 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003141 }
3142}
3143
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003144static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3145 Error **errp)
3146{
3147 const char *chardev = qemu_opt_get(opts, "chardev");
3148
3149 if (chardev == NULL) {
3150 error_setg(errp, "chardev: mux: no chardev given");
3151 return;
3152 }
3153 backend->mux = g_new0(ChardevMux, 1);
3154 backend->mux->chardev = g_strdup(chardev);
3155}
3156
Anthony Liguorid654f342013-03-05 23:21:28 +05303157typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003158 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003159 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003160 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003161 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003162 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003163 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303164} CharDriver;
3165
3166static GSList *backends;
3167
3168void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3169{
3170 CharDriver *s;
3171
3172 s = g_malloc0(sizeof(*s));
3173 s->name = g_strdup(name);
3174 s->open = open;
3175
3176 backends = g_slist_append(backends, s);
3177}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003178
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003179void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003180 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3181{
3182 CharDriver *s;
3183
3184 s = g_malloc0(sizeof(*s));
3185 s->name = g_strdup(name);
3186 s->kind = kind;
3187 s->parse = parse;
3188
3189 backends = g_slist_append(backends, s);
3190}
3191
Anthony Liguorif69554b2011-08-15 11:17:37 -05003192CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003193 void (*init)(struct CharDriverState *s),
3194 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003195{
Anthony Liguorid654f342013-03-05 23:21:28 +05303196 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003197 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303198 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003199
3200 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003201 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003202 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003203 }
3204
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003205 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003206 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003207 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003208 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003209 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303210 for (i = backends; i; i = i->next) {
3211 cd = i->data;
3212
3213 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003214 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303215 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003216 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303217 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003218 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003219 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003220 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003221 }
3222
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003223 if (!cd->open) {
3224 /* using new, qapi init */
3225 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3226 ChardevReturn *ret = NULL;
3227 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003228 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003229
3230 if (qemu_opt_get_bool(opts, "mux", 0)) {
3231 bid = g_strdup_printf("%s-base", id);
3232 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003233
3234 chr = NULL;
3235 backend->kind = cd->kind;
3236 if (cd->parse) {
3237 cd->parse(opts, backend, errp);
3238 if (error_is_set(errp)) {
3239 goto qapi_out;
3240 }
3241 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003242 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003243 if (error_is_set(errp)) {
3244 goto qapi_out;
3245 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003246
3247 if (bid) {
3248 qapi_free_ChardevBackend(backend);
3249 qapi_free_ChardevReturn(ret);
3250 backend = g_new0(ChardevBackend, 1);
3251 backend->mux = g_new0(ChardevMux, 1);
3252 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3253 backend->mux->chardev = g_strdup(bid);
3254 ret = qmp_chardev_add(id, backend, errp);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003255 assert(!error_is_set(errp));
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003256 }
3257
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003258 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003259 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003260
3261 qapi_out:
3262 qapi_free_ChardevBackend(backend);
3263 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003264 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003265 return chr;
3266 }
3267
Anthony Liguorid654f342013-03-05 23:21:28 +05303268 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003269 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003270 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003271 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003272 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003273 }
3274
3275 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003276 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003277 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003278 /* if we didn't create the chardev via qmp_chardev_add, we
3279 * need to send the OPENED event here
3280 */
3281 if (!chr->explicit_be_open) {
3282 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3283 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003284 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003285
3286 if (qemu_opt_get_bool(opts, "mux", 0)) {
3287 CharDriverState *base = chr;
3288 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003289 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003290 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3291 chr = qemu_chr_open_mux(base);
3292 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003293 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003294 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003295 } else {
3296 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003297 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003298 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003299 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003300 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003301
3302err:
3303 qemu_opts_del(opts);
3304 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003305}
3306
Anthony Liguori27143a42011-08-15 11:17:36 -05003307CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003308{
3309 const char *p;
3310 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003311 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003312 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003313
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003314 if (strstart(filename, "chardev:", &p)) {
3315 return qemu_chr_find(p);
3316 }
3317
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003318 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003319 if (!opts)
3320 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003321
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003322 chr = qemu_chr_new_from_opts(opts, init, &err);
3323 if (error_is_set(&err)) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003324 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003325 error_free(err);
3326 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003327 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003328 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003329 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003330 }
3331 return chr;
3332}
3333
Anthony Liguori15f31512011-08-15 11:17:35 -05003334void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003335{
3336 if (chr->chr_set_echo) {
3337 chr->chr_set_echo(chr, echo);
3338 }
3339}
3340
Hans de Goede8e25daa2013-03-26 11:07:57 +01003341void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003342{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003343 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003344 return;
3345 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003346 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003347 if (chr->chr_set_fe_open) {
3348 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003349 }
3350}
3351
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003352int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3353 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303354{
3355 GSource *src;
3356 guint tag;
3357
3358 if (s->chr_add_watch == NULL) {
3359 return -ENOSYS;
3360 }
3361
3362 src = s->chr_add_watch(s, cond);
3363 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3364 tag = g_source_attach(src, NULL);
3365 g_source_unref(src);
3366
3367 return tag;
3368}
3369
Hans de Goede44c473d2013-03-27 20:29:39 +01003370int qemu_chr_fe_claim(CharDriverState *s)
3371{
3372 if (s->avail_connections < 1) {
3373 return -1;
3374 }
3375 s->avail_connections--;
3376 return 0;
3377}
3378
3379void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3380{
3381 if (qemu_chr_fe_claim(s) != 0) {
3382 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3383 __func__, s->label);
3384 exit(1);
3385 }
3386}
3387
3388void qemu_chr_fe_release(CharDriverState *s)
3389{
3390 s->avail_connections++;
3391}
3392
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003393void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003394{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003395 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003396 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003397 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003398 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003399 g_free(chr->filename);
3400 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003401 if (chr->opts) {
3402 qemu_opts_del(chr->opts);
3403 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003404 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003405}
3406
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003407ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003408{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003409 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003410 CharDriverState *chr;
3411
Blue Swirl72cf2d42009-09-12 07:36:22 +00003412 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003413 ChardevInfoList *info = g_malloc0(sizeof(*info));
3414 info->value = g_malloc0(sizeof(*info->value));
3415 info->value->label = g_strdup(chr->label);
3416 info->value->filename = g_strdup(chr->filename);
3417
3418 info->next = chr_list;
3419 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003420 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003421
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003422 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003423}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003424
3425CharDriverState *qemu_chr_find(const char *name)
3426{
3427 CharDriverState *chr;
3428
Blue Swirl72cf2d42009-09-12 07:36:22 +00003429 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003430 if (strcmp(chr->label, name) != 0)
3431 continue;
3432 return chr;
3433 }
3434 return NULL;
3435}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003436
3437/* Get a character (serial) device interface. */
3438CharDriverState *qemu_char_get_next_serial(void)
3439{
3440 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003441 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003442
3443 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003444
3445 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3446 chr = serial_hds[next_serial++];
3447 qemu_chr_fe_claim_no_fail(chr);
3448 return chr;
3449 }
3450 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003451}
3452
Paolo Bonzini4d454572012-11-26 16:03:42 +01003453QemuOptsList qemu_chardev_opts = {
3454 .name = "chardev",
3455 .implied_opt_name = "backend",
3456 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3457 .desc = {
3458 {
3459 .name = "backend",
3460 .type = QEMU_OPT_STRING,
3461 },{
3462 .name = "path",
3463 .type = QEMU_OPT_STRING,
3464 },{
3465 .name = "host",
3466 .type = QEMU_OPT_STRING,
3467 },{
3468 .name = "port",
3469 .type = QEMU_OPT_STRING,
3470 },{
3471 .name = "localaddr",
3472 .type = QEMU_OPT_STRING,
3473 },{
3474 .name = "localport",
3475 .type = QEMU_OPT_STRING,
3476 },{
3477 .name = "to",
3478 .type = QEMU_OPT_NUMBER,
3479 },{
3480 .name = "ipv4",
3481 .type = QEMU_OPT_BOOL,
3482 },{
3483 .name = "ipv6",
3484 .type = QEMU_OPT_BOOL,
3485 },{
3486 .name = "wait",
3487 .type = QEMU_OPT_BOOL,
3488 },{
3489 .name = "server",
3490 .type = QEMU_OPT_BOOL,
3491 },{
3492 .name = "delay",
3493 .type = QEMU_OPT_BOOL,
3494 },{
3495 .name = "telnet",
3496 .type = QEMU_OPT_BOOL,
3497 },{
3498 .name = "width",
3499 .type = QEMU_OPT_NUMBER,
3500 },{
3501 .name = "height",
3502 .type = QEMU_OPT_NUMBER,
3503 },{
3504 .name = "cols",
3505 .type = QEMU_OPT_NUMBER,
3506 },{
3507 .name = "rows",
3508 .type = QEMU_OPT_NUMBER,
3509 },{
3510 .name = "mux",
3511 .type = QEMU_OPT_BOOL,
3512 },{
3513 .name = "signal",
3514 .type = QEMU_OPT_BOOL,
3515 },{
3516 .name = "name",
3517 .type = QEMU_OPT_STRING,
3518 },{
3519 .name = "debug",
3520 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003521 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003522 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003523 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003524 },{
3525 .name = "chardev",
3526 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003527 },
3528 { /* end of list */ }
3529 },
3530};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003531
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003532#ifdef _WIN32
3533
3534static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3535{
3536 HANDLE out;
3537
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003538 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003539 error_setg(errp, "input file not supported");
3540 return NULL;
3541 }
3542
3543 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3544 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3545 if (out == INVALID_HANDLE_VALUE) {
3546 error_setg(errp, "open %s failed", file->out);
3547 return NULL;
3548 }
3549 return qemu_chr_open_win_file(out);
3550}
3551
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003552static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3553 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003554{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003555 return qemu_chr_open_win_path(serial->device);
3556}
3557
3558static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3559 Error **errp)
3560{
3561 error_setg(errp, "character device backend type 'parallel' not supported");
3562 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003563}
3564
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003565#else /* WIN32 */
3566
3567static int qmp_chardev_open_file_source(char *src, int flags,
3568 Error **errp)
3569{
3570 int fd = -1;
3571
3572 TFR(fd = qemu_open(src, flags, 0666));
3573 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003574 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003575 }
3576 return fd;
3577}
3578
3579static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3580{
3581 int flags, in = -1, out = -1;
3582
3583 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3584 out = qmp_chardev_open_file_source(file->out, flags, errp);
3585 if (error_is_set(errp)) {
3586 return NULL;
3587 }
3588
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003589 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003590 flags = O_RDONLY;
3591 in = qmp_chardev_open_file_source(file->in, flags, errp);
3592 if (error_is_set(errp)) {
3593 qemu_close(out);
3594 return NULL;
3595 }
3596 }
3597
3598 return qemu_chr_open_fd(in, out);
3599}
3600
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003601static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3602 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003603{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003604#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003605 int fd;
3606
3607 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3608 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003609 return NULL;
3610 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003611 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003612 return qemu_chr_open_tty_fd(fd);
3613#else
3614 error_setg(errp, "character device backend type 'serial' not supported");
3615 return NULL;
3616#endif
3617}
3618
3619static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3620 Error **errp)
3621{
3622#ifdef HAVE_CHARDEV_PARPORT
3623 int fd;
3624
3625 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3626 if (error_is_set(errp)) {
3627 return NULL;
3628 }
3629 return qemu_chr_open_pp_fd(fd);
3630#else
3631 error_setg(errp, "character device backend type 'parallel' not supported");
3632 return NULL;
3633#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003634}
3635
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003636#endif /* WIN32 */
3637
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003638static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3639 Error **errp)
3640{
3641 SocketAddress *addr = sock->addr;
3642 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3643 bool is_listen = sock->has_server ? sock->server : true;
3644 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3645 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3646 int fd;
3647
3648 if (is_listen) {
3649 fd = socket_listen(addr, errp);
3650 } else {
3651 fd = socket_connect(addr, errp, NULL, NULL);
3652 }
3653 if (error_is_set(errp)) {
3654 return NULL;
3655 }
3656 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3657 is_telnet, is_waitconnect, errp);
3658}
3659
Lei Li08d0ab32013-05-20 14:51:03 +08003660static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3661 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003662{
3663 int fd;
3664
Lei Li08d0ab32013-05-20 14:51:03 +08003665 fd = socket_dgram(udp->remote, udp->local, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003666 if (error_is_set(errp)) {
3667 return NULL;
3668 }
3669 return qemu_chr_open_udp_fd(fd);
3670}
3671
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003672ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3673 Error **errp)
3674{
3675 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003676 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003677
3678 chr = qemu_chr_find(id);
3679 if (chr) {
3680 error_setg(errp, "Chardev '%s' already exists", id);
3681 g_free(ret);
3682 return NULL;
3683 }
3684
3685 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003686 case CHARDEV_BACKEND_KIND_FILE:
3687 chr = qmp_chardev_open_file(backend->file, errp);
3688 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003689 case CHARDEV_BACKEND_KIND_SERIAL:
3690 chr = qmp_chardev_open_serial(backend->serial, errp);
3691 break;
3692 case CHARDEV_BACKEND_KIND_PARALLEL:
3693 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003694 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003695 case CHARDEV_BACKEND_KIND_PIPE:
3696 chr = qemu_chr_open_pipe(backend->pipe);
3697 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003698 case CHARDEV_BACKEND_KIND_SOCKET:
3699 chr = qmp_chardev_open_socket(backend->socket, errp);
3700 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003701 case CHARDEV_BACKEND_KIND_UDP:
3702 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003703 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003704#ifdef HAVE_CHARDEV_TTY
3705 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003706 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003707 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003708#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003709 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003710 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003711 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003712 case CHARDEV_BACKEND_KIND_MUX:
3713 base = qemu_chr_find(backend->mux->chardev);
3714 if (base == NULL) {
3715 error_setg(errp, "mux: base chardev %s not found",
3716 backend->mux->chardev);
3717 break;
3718 }
3719 chr = qemu_chr_open_mux(base);
3720 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003721 case CHARDEV_BACKEND_KIND_MSMOUSE:
3722 chr = qemu_chr_open_msmouse();
3723 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003724#ifdef CONFIG_BRLAPI
3725 case CHARDEV_BACKEND_KIND_BRAILLE:
3726 chr = chr_baum_init();
3727 break;
3728#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003729 case CHARDEV_BACKEND_KIND_STDIO:
3730 chr = qemu_chr_open_stdio(backend->stdio);
3731 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003732#ifdef _WIN32
3733 case CHARDEV_BACKEND_KIND_CONSOLE:
3734 chr = qemu_chr_open_win_con();
3735 break;
3736#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003737#ifdef CONFIG_SPICE
3738 case CHARDEV_BACKEND_KIND_SPICEVMC:
3739 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3740 break;
3741 case CHARDEV_BACKEND_KIND_SPICEPORT:
3742 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3743 break;
3744#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003745 case CHARDEV_BACKEND_KIND_VC:
3746 chr = vc_init(backend->vc);
3747 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003748 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003749 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003750 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003751 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003752 default:
3753 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3754 break;
3755 }
3756
3757 if (chr == NULL && !error_is_set(errp)) {
3758 error_setg(errp, "Failed to create chardev");
3759 }
3760 if (chr) {
3761 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003762 chr->avail_connections =
3763 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02003764 if (!chr->filename) {
3765 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
3766 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05003767 if (!chr->explicit_be_open) {
3768 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3769 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003770 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3771 return ret;
3772 } else {
3773 g_free(ret);
3774 return NULL;
3775 }
3776}
3777
3778void qmp_chardev_remove(const char *id, Error **errp)
3779{
3780 CharDriverState *chr;
3781
3782 chr = qemu_chr_find(id);
3783 if (NULL == chr) {
3784 error_setg(errp, "Chardev '%s' not found", id);
3785 return;
3786 }
3787 if (chr->chr_can_read || chr->chr_read ||
3788 chr->chr_event || chr->handler_opaque) {
3789 error_setg(errp, "Chardev '%s' is busy", id);
3790 return;
3791 }
3792 qemu_chr_delete(chr);
3793}
Anthony Liguorid654f342013-03-05 23:21:28 +05303794
3795static void register_types(void)
3796{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003797 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303798 register_char_driver("socket", qemu_chr_open_socket);
3799 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02003800 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02003801 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003802 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3803 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003804 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3805 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003806 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3807 qemu_chr_parse_serial);
3808 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3809 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003810 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3811 qemu_chr_parse_parallel);
3812 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3813 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003814 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003815 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003816 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3817 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003818 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
3819 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02003820 /* Bug-compatibility: */
3821 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3822 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05003823 /* this must be done after machine init, since we register FEs with muxes
3824 * as part of realize functions like serial_isa_realizefn when -nographic
3825 * is specified
3826 */
3827 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05303828}
3829
3830type_init(register_types);