blob: 6f111abfbe6522a99e92a978254e86a50aabad65 [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
Amit Shah386a5a12013-08-28 15:24:05 +0530196static void remove_fd_in_watch(CharDriverState *chr);
197
aliguori6f97dba2008-10-31 18:49:55 +0000198void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100199 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000200 IOReadHandler *fd_read,
201 IOEventHandler *fd_event,
202 void *opaque)
203{
Hans de Goede19083222013-03-26 11:07:56 +0100204 int fe_open;
205
Amit Shahda7d9982011-04-25 15:18:22 +0530206 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100207 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530208 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100209 } else {
210 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530211 }
aliguori6f97dba2008-10-31 18:49:55 +0000212 s->chr_can_read = fd_can_read;
213 s->chr_read = fd_read;
214 s->chr_event = fd_event;
215 s->handler_opaque = opaque;
216 if (s->chr_update_read_handler)
217 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200218
Hans de Goede19083222013-03-26 11:07:56 +0100219 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100220 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100221 }
222
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200223 /* We're connecting to an already opened device, so let's make sure we
224 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100225 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100226 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200227 }
aliguori6f97dba2008-10-31 18:49:55 +0000228}
229
230static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
231{
232 return len;
233}
234
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100235static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000236{
237 CharDriverState *chr;
238
Anthony Liguori7267c092011-08-20 22:09:37 -0500239 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000240 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500241 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100242 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000243}
244
245/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000246#define MAX_MUX 4
247#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
248#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
249typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100250 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000251 IOReadHandler *chr_read[MAX_MUX];
252 IOEventHandler *chr_event[MAX_MUX];
253 void *ext_opaque[MAX_MUX];
254 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200255 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000256 int mux_cnt;
257 int term_got_escape;
258 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000259 /* Intermediate input buffer allows to catch escape sequences even if the
260 currently active device is not accepting any input - but only until it
261 is full as well. */
262 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
263 int prod[MAX_MUX];
264 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200265 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200266 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200267 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000268} MuxDriver;
269
270
271static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
272{
273 MuxDriver *d = chr->opaque;
274 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200275 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000276 ret = d->drv->chr_write(d->drv, buf, len);
277 } else {
278 int i;
279
280 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200281 for (i = 0; i < len; i++) {
282 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000283 char buf1[64];
284 int64_t ti;
285 int secs;
286
Alex Blighbc72ad62013-08-21 16:03:08 +0100287 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200288 if (d->timestamps_start == -1)
289 d->timestamps_start = ti;
290 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000291 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000292 snprintf(buf1, sizeof(buf1),
293 "[%02d:%02d:%02d.%03d] ",
294 secs / 3600,
295 (secs / 60) % 60,
296 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000297 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000298 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200299 d->linestart = 0;
300 }
301 ret += d->drv->chr_write(d->drv, buf+i, 1);
302 if (buf[i] == '\n') {
303 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000304 }
305 }
306 }
307 return ret;
308}
309
310static const char * const mux_help[] = {
311 "% h print this help\n\r",
312 "% x exit emulator\n\r",
313 "% s save disk data back to file (if -snapshot)\n\r",
314 "% t toggle console timestamps\n\r"
315 "% b send break (magic sysrq)\n\r",
316 "% c switch between console and monitor\n\r",
317 "% % sends %\n\r",
318 NULL
319};
320
321int term_escape_char = 0x01; /* ctrl-a is used for escape */
322static void mux_print_help(CharDriverState *chr)
323{
324 int i, j;
325 char ebuf[15] = "Escape-Char";
326 char cbuf[50] = "\n\r";
327
328 if (term_escape_char > 0 && term_escape_char < 26) {
329 snprintf(cbuf, sizeof(cbuf), "\n\r");
330 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
331 } else {
332 snprintf(cbuf, sizeof(cbuf),
333 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
334 term_escape_char);
335 }
336 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
337 for (i = 0; mux_help[i] != NULL; i++) {
338 for (j=0; mux_help[i][j] != '\0'; j++) {
339 if (mux_help[i][j] == '%')
340 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
341 else
342 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
343 }
344 }
345}
346
aliguori2724b182009-03-05 23:01:47 +0000347static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
348{
349 if (d->chr_event[mux_nr])
350 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
351}
352
aliguori6f97dba2008-10-31 18:49:55 +0000353static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
354{
355 if (d->term_got_escape) {
356 d->term_got_escape = 0;
357 if (ch == term_escape_char)
358 goto send_char;
359 switch(ch) {
360 case '?':
361 case 'h':
362 mux_print_help(chr);
363 break;
364 case 'x':
365 {
366 const char *term = "QEMU: Terminated\n\r";
367 chr->chr_write(chr,(uint8_t *)term,strlen(term));
368 exit(0);
369 break;
370 }
371 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200372 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000373 break;
374 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100375 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000376 break;
377 case 'c':
378 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200379 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
380 d->focus++;
381 if (d->focus >= d->mux_cnt)
382 d->focus = 0;
383 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000384 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200385 case 't':
386 d->timestamps = !d->timestamps;
387 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200388 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200389 break;
aliguori6f97dba2008-10-31 18:49:55 +0000390 }
391 } else if (ch == term_escape_char) {
392 d->term_got_escape = 1;
393 } else {
394 send_char:
395 return 1;
396 }
397 return 0;
398}
399
400static void mux_chr_accept_input(CharDriverState *chr)
401{
aliguori6f97dba2008-10-31 18:49:55 +0000402 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200403 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000404
aliguoria80bf992009-03-05 23:00:02 +0000405 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000406 d->chr_can_read[m] &&
407 d->chr_can_read[m](d->ext_opaque[m])) {
408 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000409 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000410 }
411}
412
413static int mux_chr_can_read(void *opaque)
414{
415 CharDriverState *chr = opaque;
416 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200417 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000418
aliguoria80bf992009-03-05 23:00:02 +0000419 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000420 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000421 if (d->chr_can_read[m])
422 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000423 return 0;
424}
425
426static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
427{
428 CharDriverState *chr = opaque;
429 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200430 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000431 int i;
432
433 mux_chr_accept_input (opaque);
434
435 for(i = 0; i < size; i++)
436 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000437 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000438 d->chr_can_read[m] &&
439 d->chr_can_read[m](d->ext_opaque[m]))
440 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
441 else
aliguoria80bf992009-03-05 23:00:02 +0000442 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000443 }
444}
445
446static void mux_chr_event(void *opaque, int event)
447{
448 CharDriverState *chr = opaque;
449 MuxDriver *d = chr->opaque;
450 int i;
451
452 /* Send the event to all registered listeners */
453 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000454 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000455}
456
457static void mux_chr_update_read_handler(CharDriverState *chr)
458{
459 MuxDriver *d = chr->opaque;
460
461 if (d->mux_cnt >= MAX_MUX) {
462 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
463 return;
464 }
465 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
466 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
467 d->chr_read[d->mux_cnt] = chr->chr_read;
468 d->chr_event[d->mux_cnt] = chr->chr_event;
469 /* Fix up the real driver with mux routines */
470 if (d->mux_cnt == 0) {
471 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
472 mux_chr_event, chr);
473 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200474 if (d->focus != -1) {
475 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200476 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200477 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000478 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200479 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000480}
481
Michael Roth7b7ab182013-07-30 13:04:22 -0500482static bool muxes_realized;
483
484/**
485 * Called after processing of default and command-line-specified
486 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
487 * to a mux chardev. This is done here to ensure that
488 * output/prompts/banners are only displayed for the FE that has
489 * focus when initial command-line processing/machine init is
490 * completed.
491 *
492 * After this point, any new FE attached to any new or existing
493 * mux will receive CHR_EVENT_OPENED notifications for the BE
494 * immediately.
495 */
496static void muxes_realize_done(Notifier *notifier, void *unused)
497{
498 CharDriverState *chr;
499
500 QTAILQ_FOREACH(chr, &chardevs, next) {
501 if (chr->is_mux) {
502 MuxDriver *d = chr->opaque;
503 int i;
504
505 /* send OPENED to all already-attached FEs */
506 for (i = 0; i < d->mux_cnt; i++) {
507 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
508 }
509 /* mark mux as OPENED so any new FEs will immediately receive
510 * OPENED event
511 */
512 qemu_chr_be_generic_open(chr);
513 }
514 }
515 muxes_realized = true;
516}
517
518static Notifier muxes_realize_notify = {
519 .notify = muxes_realize_done,
520};
521
aliguori6f97dba2008-10-31 18:49:55 +0000522static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
523{
524 CharDriverState *chr;
525 MuxDriver *d;
526
Anthony Liguori7267c092011-08-20 22:09:37 -0500527 chr = g_malloc0(sizeof(CharDriverState));
528 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000529
530 chr->opaque = d;
531 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200532 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000533 chr->chr_write = mux_chr_write;
534 chr->chr_update_read_handler = mux_chr_update_read_handler;
535 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100536 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100537 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500538 /* only default to opened state if we've realized the initial
539 * set of muxes
540 */
541 chr->explicit_be_open = muxes_realized ? 0 : 1;
542 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200543
aliguori6f97dba2008-10-31 18:49:55 +0000544 return chr;
545}
546
547
548#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000549int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000550{
551 int ret, len;
552
553 len = len1;
554 while (len > 0) {
555 ret = send(fd, buf, len, 0);
556 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000557 errno = WSAGetLastError();
558 if (errno != WSAEWOULDBLOCK) {
559 return -1;
560 }
561 } else if (ret == 0) {
562 break;
563 } else {
564 buf += ret;
565 len -= ret;
566 }
567 }
568 return len1 - len;
569}
570
571#else
572
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100573int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000574{
575 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100576 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000577
578 len = len1;
579 while (len > 0) {
580 ret = write(fd, buf, len);
581 if (ret < 0) {
582 if (errno != EINTR && errno != EAGAIN)
583 return -1;
584 } else if (ret == 0) {
585 break;
586 } else {
587 buf += ret;
588 len -= ret;
589 }
590 }
591 return len1 - len;
592}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500593
594int recv_all(int fd, void *_buf, int len1, bool single_read)
595{
596 int ret, len;
597 uint8_t *buf = _buf;
598
599 len = len1;
600 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
601 if (ret < 0) {
602 if (errno != EINTR && errno != EAGAIN) {
603 return -1;
604 }
605 continue;
606 } else {
607 if (single_read) {
608 return ret;
609 }
610 buf += ret;
611 len -= ret;
612 }
613 }
614 return len1 - len;
615}
616
aliguori6f97dba2008-10-31 18:49:55 +0000617#endif /* !_WIN32 */
618
Anthony Liguori96c63842013-03-05 23:21:18 +0530619typedef struct IOWatchPoll
620{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200621 GSource parent;
622
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200623 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530624 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530625
626 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200627 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530628 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530629} IOWatchPoll;
630
Anthony Liguori96c63842013-03-05 23:21:18 +0530631static IOWatchPoll *io_watch_poll_from_source(GSource *source)
632{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200633 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530634}
635
636static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
637{
638 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200639 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200640 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200641 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530642 return FALSE;
643 }
644
Paolo Bonzinid185c092013-04-05 17:59:33 +0200645 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200646 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
647 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200648 g_source_attach(iwp->src, NULL);
649 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200650 g_source_destroy(iwp->src);
651 g_source_unref(iwp->src);
652 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200653 }
654 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530655}
656
657static gboolean io_watch_poll_check(GSource *source)
658{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200659 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530660}
661
662static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
663 gpointer user_data)
664{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200665 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530666}
667
668static void io_watch_poll_finalize(GSource *source)
669{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200670 /* Due to a glib bug, removing the last reference to a source
671 * inside a finalize callback causes recursive locking (and a
672 * deadlock). This is not a problem inside other callbacks,
673 * including dispatch callbacks, so we call io_remove_watch_poll
674 * to remove this source. At this point, iwp->src must
675 * be NULL, or we would leak it.
676 *
677 * This would be solved much more elegantly by child sources,
678 * but we support older glib versions that do not have them.
679 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530680 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200681 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530682}
683
684static GSourceFuncs io_watch_poll_funcs = {
685 .prepare = io_watch_poll_prepare,
686 .check = io_watch_poll_check,
687 .dispatch = io_watch_poll_dispatch,
688 .finalize = io_watch_poll_finalize,
689};
690
691/* Can only be used for read */
692static guint io_add_watch_poll(GIOChannel *channel,
693 IOCanReadHandler *fd_can_read,
694 GIOFunc fd_read,
695 gpointer user_data)
696{
697 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200698 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530699
Paolo Bonzinid185c092013-04-05 17:59:33 +0200700 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530701 iwp->fd_can_read = fd_can_read;
702 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200703 iwp->channel = channel;
704 iwp->fd_read = (GSourceFunc) fd_read;
705 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530706
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200707 tag = g_source_attach(&iwp->parent, NULL);
708 g_source_unref(&iwp->parent);
709 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530710}
711
Paolo Bonzini2b316772013-04-19 17:32:09 +0200712static void io_remove_watch_poll(guint tag)
713{
714 GSource *source;
715 IOWatchPoll *iwp;
716
717 g_return_if_fail (tag > 0);
718
719 source = g_main_context_find_source_by_id(NULL, tag);
720 g_return_if_fail (source != NULL);
721
722 iwp = io_watch_poll_from_source(source);
723 if (iwp->src) {
724 g_source_destroy(iwp->src);
725 g_source_unref(iwp->src);
726 iwp->src = NULL;
727 }
728 g_source_destroy(&iwp->parent);
729}
730
Amit Shah26da70c2013-08-28 15:23:37 +0530731static void remove_fd_in_watch(CharDriverState *chr)
732{
733 if (chr->fd_in_tag) {
734 io_remove_watch_poll(chr->fd_in_tag);
735 chr->fd_in_tag = 0;
736 }
737}
738
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000739#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530740static GIOChannel *io_channel_from_fd(int fd)
741{
742 GIOChannel *chan;
743
744 if (fd == -1) {
745 return NULL;
746 }
747
748 chan = g_io_channel_unix_new(fd);
749
750 g_io_channel_set_encoding(chan, NULL, NULL);
751 g_io_channel_set_buffered(chan, FALSE);
752
753 return chan;
754}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000755#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530756
Anthony Liguori76a96442013-03-05 23:21:21 +0530757static GIOChannel *io_channel_from_socket(int fd)
758{
759 GIOChannel *chan;
760
761 if (fd == -1) {
762 return NULL;
763 }
764
765#ifdef _WIN32
766 chan = g_io_channel_win32_new_socket(fd);
767#else
768 chan = g_io_channel_unix_new(fd);
769#endif
770
771 g_io_channel_set_encoding(chan, NULL, NULL);
772 g_io_channel_set_buffered(chan, FALSE);
773
774 return chan;
775}
776
Anthony Liguori684a0962013-03-29 11:39:50 -0500777static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530778{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200779 size_t offset = 0;
780 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530781
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200782 while (offset < len && status == G_IO_STATUS_NORMAL) {
783 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500784
785 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530786 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500787 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530788 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500789
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200790 if (offset > 0) {
791 return offset;
792 }
793 switch (status) {
794 case G_IO_STATUS_NORMAL:
795 g_assert(len == 0);
796 return 0;
797 case G_IO_STATUS_AGAIN:
798 errno = EAGAIN;
799 return -1;
800 default:
801 break;
802 }
803 errno = EINVAL;
804 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530805}
Anthony Liguori96c63842013-03-05 23:21:18 +0530806
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000807#ifndef _WIN32
808
Anthony Liguoria29753f2013-03-05 23:21:19 +0530809typedef struct FDCharDriver {
810 CharDriverState *chr;
811 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000812 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530813 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000814} FDCharDriver;
815
aliguori6f97dba2008-10-31 18:49:55 +0000816static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
817{
818 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530819
Anthony Liguori684a0962013-03-29 11:39:50 -0500820 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530821}
822
823static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
824{
825 CharDriverState *chr = opaque;
826 FDCharDriver *s = chr->opaque;
827 int len;
828 uint8_t buf[READ_BUF_LEN];
829 GIOStatus status;
830 gsize bytes_read;
831
832 len = sizeof(buf);
833 if (len > s->max_size) {
834 len = s->max_size;
835 }
836 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200837 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530838 }
839
840 status = g_io_channel_read_chars(chan, (gchar *)buf,
841 len, &bytes_read, NULL);
842 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530843 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530844 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
845 return FALSE;
846 }
847 if (status == G_IO_STATUS_NORMAL) {
848 qemu_chr_be_write(chr, buf, bytes_read);
849 }
850
851 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000852}
853
854static int fd_chr_read_poll(void *opaque)
855{
856 CharDriverState *chr = opaque;
857 FDCharDriver *s = chr->opaque;
858
Anthony Liguori909cda12011-08-15 11:17:31 -0500859 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000860 return s->max_size;
861}
862
Anthony Liguori23673ca2013-03-05 23:21:23 +0530863static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
864{
865 FDCharDriver *s = chr->opaque;
866 return g_io_create_watch(s->fd_out, cond);
867}
868
aliguori6f97dba2008-10-31 18:49:55 +0000869static void fd_chr_update_read_handler(CharDriverState *chr)
870{
871 FDCharDriver *s = chr->opaque;
872
Amit Shah26da70c2013-08-28 15:23:37 +0530873 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530874 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530875 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
876 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000877 }
878}
879
880static void fd_chr_close(struct CharDriverState *chr)
881{
882 FDCharDriver *s = chr->opaque;
883
Amit Shah26da70c2013-08-28 15:23:37 +0530884 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530885 if (s->fd_in) {
886 g_io_channel_unref(s->fd_in);
887 }
888 if (s->fd_out) {
889 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000890 }
891
Anthony Liguori7267c092011-08-20 22:09:37 -0500892 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100893 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000894}
895
896/* open a character device to a unix fd */
897static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
898{
899 CharDriverState *chr;
900 FDCharDriver *s;
901
Anthony Liguori7267c092011-08-20 22:09:37 -0500902 chr = g_malloc0(sizeof(CharDriverState));
903 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530904 s->fd_in = io_channel_from_fd(fd_in);
905 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530906 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530907 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000908 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530909 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000910 chr->chr_write = fd_chr_write;
911 chr->chr_update_read_handler = fd_chr_update_read_handler;
912 chr->chr_close = fd_chr_close;
913
aliguori6f97dba2008-10-31 18:49:55 +0000914 return chr;
915}
916
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100917static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000918{
919 int fd_in, fd_out;
920 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100921 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200922
923 if (filename == NULL) {
924 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100925 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200926 }
aliguori6f97dba2008-10-31 18:49:55 +0000927
928 snprintf(filename_in, 256, "%s.in", filename);
929 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100930 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
931 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000932 if (fd_in < 0 || fd_out < 0) {
933 if (fd_in >= 0)
934 close(fd_in);
935 if (fd_out >= 0)
936 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100937 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100938 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100939 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100940 }
aliguori6f97dba2008-10-31 18:49:55 +0000941 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100942 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000943}
944
aliguori6f97dba2008-10-31 18:49:55 +0000945/* init terminal so that we can grab keys */
946static struct termios oldtty;
947static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100948static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000949
950static void term_exit(void)
951{
952 tcsetattr (0, TCSANOW, &oldtty);
953 fcntl(0, F_SETFL, old_fd0_flags);
954}
955
Paolo Bonzinibb002512010-12-23 13:42:50 +0100956static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000957{
958 struct termios tty;
959
Paolo Bonzini03693642010-12-23 13:42:49 +0100960 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100961 if (!echo) {
962 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000963 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100964 tty.c_oflag |= OPOST;
965 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
966 tty.c_cflag &= ~(CSIZE|PARENB);
967 tty.c_cflag |= CS8;
968 tty.c_cc[VMIN] = 1;
969 tty.c_cc[VTIME] = 0;
970 }
Paolo Bonzinibb002512010-12-23 13:42:50 +0100971 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000972 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000973
974 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000975}
976
977static void qemu_chr_close_stdio(struct CharDriverState *chr)
978{
979 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000980 fd_chr_close(chr);
981}
982
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100983static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000984{
985 CharDriverState *chr;
986
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400987 if (is_daemonized()) {
988 error_report("cannot use stdio with -daemonize");
989 return NULL;
990 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530991 old_fd0_flags = fcntl(0, F_GETFL);
992 tcgetattr (0, &oldtty);
993 fcntl(0, F_SETFL, O_NONBLOCK);
994 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100995
aliguori6f97dba2008-10-31 18:49:55 +0000996 chr = qemu_chr_open_fd(0, 1);
997 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100998 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100999 if (opts->has_signal) {
1000 stdio_allow_signal = opts->signal;
1001 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001002 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001003
Markus Armbruster1f514702012-02-07 15:09:08 +01001004 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001005}
1006
aliguori6f97dba2008-10-31 18:49:55 +00001007#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001008 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1009 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001010
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001011#define HAVE_CHARDEV_TTY 1
1012
aliguori6f97dba2008-10-31 18:49:55 +00001013typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301014 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001015 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001016 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301017 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001018} PtyCharDriver;
1019
1020static void pty_chr_update_read_handler(CharDriverState *chr);
1021static void pty_chr_state(CharDriverState *chr, int connected);
1022
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301023static gboolean pty_chr_timer(gpointer opaque)
1024{
1025 struct CharDriverState *chr = opaque;
1026 PtyCharDriver *s = chr->opaque;
1027
1028 if (s->connected) {
1029 goto out;
1030 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301031
1032 /* Next poll ... */
1033 pty_chr_update_read_handler(chr);
1034
1035out:
Hans de Goede79f20072013-04-25 13:53:02 +02001036 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301037 return FALSE;
1038}
1039
1040static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1041{
1042 PtyCharDriver *s = chr->opaque;
1043
1044 if (s->timer_tag) {
1045 g_source_remove(s->timer_tag);
1046 s->timer_tag = 0;
1047 }
1048
1049 if (ms == 1000) {
1050 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1051 } else {
1052 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1053 }
1054}
1055
aliguori6f97dba2008-10-31 18:49:55 +00001056static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1057{
1058 PtyCharDriver *s = chr->opaque;
1059
1060 if (!s->connected) {
1061 /* guest sends data, check for (re-)connect */
1062 pty_chr_update_read_handler(chr);
1063 return 0;
1064 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001065 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001066}
1067
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301068static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1069{
1070 PtyCharDriver *s = chr->opaque;
1071 return g_io_create_watch(s->fd, cond);
1072}
1073
aliguori6f97dba2008-10-31 18:49:55 +00001074static int pty_chr_read_poll(void *opaque)
1075{
1076 CharDriverState *chr = opaque;
1077 PtyCharDriver *s = chr->opaque;
1078
Anthony Liguori909cda12011-08-15 11:17:31 -05001079 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001080 return s->read_bytes;
1081}
1082
Anthony Liguori093d3a22013-03-05 23:21:20 +05301083static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001084{
1085 CharDriverState *chr = opaque;
1086 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301087 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301088 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301089 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001090
1091 len = sizeof(buf);
1092 if (len > s->read_bytes)
1093 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001094 if (len == 0) {
1095 return TRUE;
1096 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301097 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1098 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001099 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301100 return FALSE;
1101 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001102 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001103 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001104 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301105 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001106}
1107
1108static void pty_chr_update_read_handler(CharDriverState *chr)
1109{
1110 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001111 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001112
Paolo Bonzini85a67692013-04-19 17:32:07 +02001113 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1114 pfd.events = G_IO_OUT;
1115 pfd.revents = 0;
1116 g_poll(&pfd, 1, 0);
1117 if (pfd.revents & G_IO_HUP) {
1118 pty_chr_state(chr, 0);
1119 } else {
1120 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301121 }
aliguori6f97dba2008-10-31 18:49:55 +00001122}
1123
1124static void pty_chr_state(CharDriverState *chr, int connected)
1125{
1126 PtyCharDriver *s = chr->opaque;
1127
1128 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301129 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001130 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001131 /* (re-)connect poll interval for idle guests: once per second.
1132 * We check more frequently in case the guests sends data to
1133 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301134 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001135 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001136 if (s->timer_tag) {
1137 g_source_remove(s->timer_tag);
1138 s->timer_tag = 0;
1139 }
1140 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001141 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001142 qemu_chr_be_generic_open(chr);
Amit Shah7ba9add2013-08-28 15:18:29 +05301143 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1144 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001145 }
aliguori6f97dba2008-10-31 18:49:55 +00001146 }
1147}
1148
aliguori6f97dba2008-10-31 18:49:55 +00001149
1150static void pty_chr_close(struct CharDriverState *chr)
1151{
1152 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301153 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001154
Amit Shah26da70c2013-08-28 15:23:37 +05301155 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301156 fd = g_io_channel_unix_get_fd(s->fd);
1157 g_io_channel_unref(s->fd);
1158 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301159 if (s->timer_tag) {
1160 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001161 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301162 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001163 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001164 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001165}
1166
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001167static CharDriverState *qemu_chr_open_pty(const char *id,
1168 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001169{
1170 CharDriverState *chr;
1171 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001172 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001173 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001174
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001175 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1176 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001177 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001178 }
1179
aliguori6f97dba2008-10-31 18:49:55 +00001180 close(slave_fd);
1181
Markus Armbrustera4e26042011-11-11 10:40:05 +01001182 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001183
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001184 chr->filename = g_strdup_printf("pty:%s", pty_name);
1185 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001186 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001187
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001188 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001189 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001190
1191 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001192 chr->opaque = s;
1193 chr->chr_write = pty_chr_write;
1194 chr->chr_update_read_handler = pty_chr_update_read_handler;
1195 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301196 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001197 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001198
Anthony Liguori093d3a22013-03-05 23:21:20 +05301199 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301200 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001201
Markus Armbruster1f514702012-02-07 15:09:08 +01001202 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001203}
1204
1205static void tty_serial_init(int fd, int speed,
1206 int parity, int data_bits, int stop_bits)
1207{
1208 struct termios tty;
1209 speed_t spd;
1210
1211#if 0
1212 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1213 speed, parity, data_bits, stop_bits);
1214#endif
1215 tcgetattr (fd, &tty);
1216
Stefan Weil45eea132009-10-26 16:10:10 +01001217#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1218 speed = speed * 10 / 11;
1219 do {
1220 check_speed(50);
1221 check_speed(75);
1222 check_speed(110);
1223 check_speed(134);
1224 check_speed(150);
1225 check_speed(200);
1226 check_speed(300);
1227 check_speed(600);
1228 check_speed(1200);
1229 check_speed(1800);
1230 check_speed(2400);
1231 check_speed(4800);
1232 check_speed(9600);
1233 check_speed(19200);
1234 check_speed(38400);
1235 /* Non-Posix values follow. They may be unsupported on some systems. */
1236 check_speed(57600);
1237 check_speed(115200);
1238#ifdef B230400
1239 check_speed(230400);
1240#endif
1241#ifdef B460800
1242 check_speed(460800);
1243#endif
1244#ifdef B500000
1245 check_speed(500000);
1246#endif
1247#ifdef B576000
1248 check_speed(576000);
1249#endif
1250#ifdef B921600
1251 check_speed(921600);
1252#endif
1253#ifdef B1000000
1254 check_speed(1000000);
1255#endif
1256#ifdef B1152000
1257 check_speed(1152000);
1258#endif
1259#ifdef B1500000
1260 check_speed(1500000);
1261#endif
1262#ifdef B2000000
1263 check_speed(2000000);
1264#endif
1265#ifdef B2500000
1266 check_speed(2500000);
1267#endif
1268#ifdef B3000000
1269 check_speed(3000000);
1270#endif
1271#ifdef B3500000
1272 check_speed(3500000);
1273#endif
1274#ifdef B4000000
1275 check_speed(4000000);
1276#endif
aliguori6f97dba2008-10-31 18:49:55 +00001277 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001278 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001279
1280 cfsetispeed(&tty, spd);
1281 cfsetospeed(&tty, spd);
1282
1283 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1284 |INLCR|IGNCR|ICRNL|IXON);
1285 tty.c_oflag |= OPOST;
1286 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1287 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1288 switch(data_bits) {
1289 default:
1290 case 8:
1291 tty.c_cflag |= CS8;
1292 break;
1293 case 7:
1294 tty.c_cflag |= CS7;
1295 break;
1296 case 6:
1297 tty.c_cflag |= CS6;
1298 break;
1299 case 5:
1300 tty.c_cflag |= CS5;
1301 break;
1302 }
1303 switch(parity) {
1304 default:
1305 case 'N':
1306 break;
1307 case 'E':
1308 tty.c_cflag |= PARENB;
1309 break;
1310 case 'O':
1311 tty.c_cflag |= PARENB | PARODD;
1312 break;
1313 }
1314 if (stop_bits == 2)
1315 tty.c_cflag |= CSTOPB;
1316
1317 tcsetattr (fd, TCSANOW, &tty);
1318}
1319
1320static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1321{
1322 FDCharDriver *s = chr->opaque;
1323
1324 switch(cmd) {
1325 case CHR_IOCTL_SERIAL_SET_PARAMS:
1326 {
1327 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301328 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1329 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001330 ssp->data_bits, ssp->stop_bits);
1331 }
1332 break;
1333 case CHR_IOCTL_SERIAL_SET_BREAK:
1334 {
1335 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301336 if (enable) {
1337 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1338 }
aliguori6f97dba2008-10-31 18:49:55 +00001339 }
1340 break;
1341 case CHR_IOCTL_SERIAL_GET_TIOCM:
1342 {
1343 int sarg = 0;
1344 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301345 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001346 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001347 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001348 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001349 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001350 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001351 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001352 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001353 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001354 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001355 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001356 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001357 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001358 *targ |= CHR_TIOCM_RTS;
1359 }
1360 break;
1361 case CHR_IOCTL_SERIAL_SET_TIOCM:
1362 {
1363 int sarg = *(int *)arg;
1364 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301365 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001366 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1367 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1368 if (sarg & CHR_TIOCM_CTS)
1369 targ |= TIOCM_CTS;
1370 if (sarg & CHR_TIOCM_CAR)
1371 targ |= TIOCM_CAR;
1372 if (sarg & CHR_TIOCM_DSR)
1373 targ |= TIOCM_DSR;
1374 if (sarg & CHR_TIOCM_RI)
1375 targ |= TIOCM_RI;
1376 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001377 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001378 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001379 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301380 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001381 }
1382 break;
1383 default:
1384 return -ENOTSUP;
1385 }
1386 return 0;
1387}
1388
David Ahern4266a132010-02-10 18:27:17 -07001389static void qemu_chr_close_tty(CharDriverState *chr)
1390{
1391 FDCharDriver *s = chr->opaque;
1392 int fd = -1;
1393
1394 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301395 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001396 }
1397
1398 fd_chr_close(chr);
1399
1400 if (fd >= 0) {
1401 close(fd);
1402 }
1403}
1404
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001405static CharDriverState *qemu_chr_open_tty_fd(int fd)
1406{
1407 CharDriverState *chr;
1408
1409 tty_serial_init(fd, 115200, 'N', 8, 1);
1410 chr = qemu_chr_open_fd(fd, fd);
1411 chr->chr_ioctl = tty_serial_ioctl;
1412 chr->chr_close = qemu_chr_close_tty;
1413 return chr;
1414}
aliguori6f97dba2008-10-31 18:49:55 +00001415#endif /* __linux__ || __sun__ */
1416
1417#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001418
1419#define HAVE_CHARDEV_PARPORT 1
1420
aliguori6f97dba2008-10-31 18:49:55 +00001421typedef struct {
1422 int fd;
1423 int mode;
1424} ParallelCharDriver;
1425
1426static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1427{
1428 if (s->mode != mode) {
1429 int m = mode;
1430 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1431 return 0;
1432 s->mode = mode;
1433 }
1434 return 1;
1435}
1436
1437static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1438{
1439 ParallelCharDriver *drv = chr->opaque;
1440 int fd = drv->fd;
1441 uint8_t b;
1442
1443 switch(cmd) {
1444 case CHR_IOCTL_PP_READ_DATA:
1445 if (ioctl(fd, PPRDATA, &b) < 0)
1446 return -ENOTSUP;
1447 *(uint8_t *)arg = b;
1448 break;
1449 case CHR_IOCTL_PP_WRITE_DATA:
1450 b = *(uint8_t *)arg;
1451 if (ioctl(fd, PPWDATA, &b) < 0)
1452 return -ENOTSUP;
1453 break;
1454 case CHR_IOCTL_PP_READ_CONTROL:
1455 if (ioctl(fd, PPRCONTROL, &b) < 0)
1456 return -ENOTSUP;
1457 /* Linux gives only the lowest bits, and no way to know data
1458 direction! For better compatibility set the fixed upper
1459 bits. */
1460 *(uint8_t *)arg = b | 0xc0;
1461 break;
1462 case CHR_IOCTL_PP_WRITE_CONTROL:
1463 b = *(uint8_t *)arg;
1464 if (ioctl(fd, PPWCONTROL, &b) < 0)
1465 return -ENOTSUP;
1466 break;
1467 case CHR_IOCTL_PP_READ_STATUS:
1468 if (ioctl(fd, PPRSTATUS, &b) < 0)
1469 return -ENOTSUP;
1470 *(uint8_t *)arg = b;
1471 break;
1472 case CHR_IOCTL_PP_DATA_DIR:
1473 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1474 return -ENOTSUP;
1475 break;
1476 case CHR_IOCTL_PP_EPP_READ_ADDR:
1477 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1478 struct ParallelIOArg *parg = arg;
1479 int n = read(fd, parg->buffer, parg->count);
1480 if (n != parg->count) {
1481 return -EIO;
1482 }
1483 }
1484 break;
1485 case CHR_IOCTL_PP_EPP_READ:
1486 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1487 struct ParallelIOArg *parg = arg;
1488 int n = read(fd, parg->buffer, parg->count);
1489 if (n != parg->count) {
1490 return -EIO;
1491 }
1492 }
1493 break;
1494 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1495 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1496 struct ParallelIOArg *parg = arg;
1497 int n = write(fd, parg->buffer, parg->count);
1498 if (n != parg->count) {
1499 return -EIO;
1500 }
1501 }
1502 break;
1503 case CHR_IOCTL_PP_EPP_WRITE:
1504 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1505 struct ParallelIOArg *parg = arg;
1506 int n = write(fd, parg->buffer, parg->count);
1507 if (n != parg->count) {
1508 return -EIO;
1509 }
1510 }
1511 break;
1512 default:
1513 return -ENOTSUP;
1514 }
1515 return 0;
1516}
1517
1518static void pp_close(CharDriverState *chr)
1519{
1520 ParallelCharDriver *drv = chr->opaque;
1521 int fd = drv->fd;
1522
1523 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1524 ioctl(fd, PPRELEASE);
1525 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001526 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001527 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001528}
1529
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001530static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001531{
1532 CharDriverState *chr;
1533 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001534
1535 if (ioctl(fd, PPCLAIM) < 0) {
1536 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001537 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001538 }
1539
Anthony Liguori7267c092011-08-20 22:09:37 -05001540 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001541 drv->fd = fd;
1542 drv->mode = IEEE1284_MODE_COMPAT;
1543
Anthony Liguori7267c092011-08-20 22:09:37 -05001544 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001545 chr->chr_write = null_chr_write;
1546 chr->chr_ioctl = pp_ioctl;
1547 chr->chr_close = pp_close;
1548 chr->opaque = drv;
1549
Markus Armbruster1f514702012-02-07 15:09:08 +01001550 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001551}
1552#endif /* __linux__ */
1553
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001554#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001555
1556#define HAVE_CHARDEV_PARPORT 1
1557
blueswir16972f932008-11-22 20:49:12 +00001558static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1559{
Stefan Weile0efb992011-02-23 19:09:16 +01001560 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001561 uint8_t b;
1562
1563 switch(cmd) {
1564 case CHR_IOCTL_PP_READ_DATA:
1565 if (ioctl(fd, PPIGDATA, &b) < 0)
1566 return -ENOTSUP;
1567 *(uint8_t *)arg = b;
1568 break;
1569 case CHR_IOCTL_PP_WRITE_DATA:
1570 b = *(uint8_t *)arg;
1571 if (ioctl(fd, PPISDATA, &b) < 0)
1572 return -ENOTSUP;
1573 break;
1574 case CHR_IOCTL_PP_READ_CONTROL:
1575 if (ioctl(fd, PPIGCTRL, &b) < 0)
1576 return -ENOTSUP;
1577 *(uint8_t *)arg = b;
1578 break;
1579 case CHR_IOCTL_PP_WRITE_CONTROL:
1580 b = *(uint8_t *)arg;
1581 if (ioctl(fd, PPISCTRL, &b) < 0)
1582 return -ENOTSUP;
1583 break;
1584 case CHR_IOCTL_PP_READ_STATUS:
1585 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1586 return -ENOTSUP;
1587 *(uint8_t *)arg = b;
1588 break;
1589 default:
1590 return -ENOTSUP;
1591 }
1592 return 0;
1593}
1594
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001595static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001596{
1597 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001598
Anthony Liguori7267c092011-08-20 22:09:37 -05001599 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001600 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001601 chr->chr_write = null_chr_write;
1602 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001603 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001604 return chr;
blueswir16972f932008-11-22 20:49:12 +00001605}
1606#endif
1607
aliguori6f97dba2008-10-31 18:49:55 +00001608#else /* _WIN32 */
1609
1610typedef struct {
1611 int max_size;
1612 HANDLE hcom, hrecv, hsend;
1613 OVERLAPPED orecv, osend;
1614 BOOL fpipe;
1615 DWORD len;
1616} WinCharState;
1617
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001618typedef struct {
1619 HANDLE hStdIn;
1620 HANDLE hInputReadyEvent;
1621 HANDLE hInputDoneEvent;
1622 HANDLE hInputThread;
1623 uint8_t win_stdio_buf;
1624} WinStdioCharState;
1625
aliguori6f97dba2008-10-31 18:49:55 +00001626#define NSENDBUF 2048
1627#define NRECVBUF 2048
1628#define MAXCONNECT 1
1629#define NTIMEOUT 5000
1630
1631static int win_chr_poll(void *opaque);
1632static int win_chr_pipe_poll(void *opaque);
1633
1634static void win_chr_close(CharDriverState *chr)
1635{
1636 WinCharState *s = chr->opaque;
1637
1638 if (s->hsend) {
1639 CloseHandle(s->hsend);
1640 s->hsend = NULL;
1641 }
1642 if (s->hrecv) {
1643 CloseHandle(s->hrecv);
1644 s->hrecv = NULL;
1645 }
1646 if (s->hcom) {
1647 CloseHandle(s->hcom);
1648 s->hcom = NULL;
1649 }
1650 if (s->fpipe)
1651 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1652 else
1653 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301654
Hans de Goedea425d232011-11-19 10:22:43 +01001655 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001656}
1657
1658static int win_chr_init(CharDriverState *chr, const char *filename)
1659{
1660 WinCharState *s = chr->opaque;
1661 COMMCONFIG comcfg;
1662 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1663 COMSTAT comstat;
1664 DWORD size;
1665 DWORD err;
1666
1667 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1668 if (!s->hsend) {
1669 fprintf(stderr, "Failed CreateEvent\n");
1670 goto fail;
1671 }
1672 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1673 if (!s->hrecv) {
1674 fprintf(stderr, "Failed CreateEvent\n");
1675 goto fail;
1676 }
1677
1678 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1679 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1680 if (s->hcom == INVALID_HANDLE_VALUE) {
1681 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1682 s->hcom = NULL;
1683 goto fail;
1684 }
1685
1686 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1687 fprintf(stderr, "Failed SetupComm\n");
1688 goto fail;
1689 }
1690
1691 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1692 size = sizeof(COMMCONFIG);
1693 GetDefaultCommConfig(filename, &comcfg, &size);
1694 comcfg.dcb.DCBlength = sizeof(DCB);
1695 CommConfigDialog(filename, NULL, &comcfg);
1696
1697 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1698 fprintf(stderr, "Failed SetCommState\n");
1699 goto fail;
1700 }
1701
1702 if (!SetCommMask(s->hcom, EV_ERR)) {
1703 fprintf(stderr, "Failed SetCommMask\n");
1704 goto fail;
1705 }
1706
1707 cto.ReadIntervalTimeout = MAXDWORD;
1708 if (!SetCommTimeouts(s->hcom, &cto)) {
1709 fprintf(stderr, "Failed SetCommTimeouts\n");
1710 goto fail;
1711 }
1712
1713 if (!ClearCommError(s->hcom, &err, &comstat)) {
1714 fprintf(stderr, "Failed ClearCommError\n");
1715 goto fail;
1716 }
1717 qemu_add_polling_cb(win_chr_poll, chr);
1718 return 0;
1719
1720 fail:
1721 win_chr_close(chr);
1722 return -1;
1723}
1724
1725static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1726{
1727 WinCharState *s = chr->opaque;
1728 DWORD len, ret, size, err;
1729
1730 len = len1;
1731 ZeroMemory(&s->osend, sizeof(s->osend));
1732 s->osend.hEvent = s->hsend;
1733 while (len > 0) {
1734 if (s->hsend)
1735 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1736 else
1737 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1738 if (!ret) {
1739 err = GetLastError();
1740 if (err == ERROR_IO_PENDING) {
1741 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1742 if (ret) {
1743 buf += size;
1744 len -= size;
1745 } else {
1746 break;
1747 }
1748 } else {
1749 break;
1750 }
1751 } else {
1752 buf += size;
1753 len -= size;
1754 }
1755 }
1756 return len1 - len;
1757}
1758
1759static int win_chr_read_poll(CharDriverState *chr)
1760{
1761 WinCharState *s = chr->opaque;
1762
Anthony Liguori909cda12011-08-15 11:17:31 -05001763 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001764 return s->max_size;
1765}
1766
1767static void win_chr_readfile(CharDriverState *chr)
1768{
1769 WinCharState *s = chr->opaque;
1770 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301771 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001772 DWORD size;
1773
1774 ZeroMemory(&s->orecv, sizeof(s->orecv));
1775 s->orecv.hEvent = s->hrecv;
1776 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1777 if (!ret) {
1778 err = GetLastError();
1779 if (err == ERROR_IO_PENDING) {
1780 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1781 }
1782 }
1783
1784 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001785 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001786 }
1787}
1788
1789static void win_chr_read(CharDriverState *chr)
1790{
1791 WinCharState *s = chr->opaque;
1792
1793 if (s->len > s->max_size)
1794 s->len = s->max_size;
1795 if (s->len == 0)
1796 return;
1797
1798 win_chr_readfile(chr);
1799}
1800
1801static int win_chr_poll(void *opaque)
1802{
1803 CharDriverState *chr = opaque;
1804 WinCharState *s = chr->opaque;
1805 COMSTAT status;
1806 DWORD comerr;
1807
1808 ClearCommError(s->hcom, &comerr, &status);
1809 if (status.cbInQue > 0) {
1810 s->len = status.cbInQue;
1811 win_chr_read_poll(chr);
1812 win_chr_read(chr);
1813 return 1;
1814 }
1815 return 0;
1816}
1817
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001818static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001819{
1820 CharDriverState *chr;
1821 WinCharState *s;
1822
Anthony Liguori7267c092011-08-20 22:09:37 -05001823 chr = g_malloc0(sizeof(CharDriverState));
1824 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001825 chr->opaque = s;
1826 chr->chr_write = win_chr_write;
1827 chr->chr_close = win_chr_close;
1828
1829 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001830 g_free(s);
1831 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001832 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001833 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001834 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001835}
1836
1837static int win_chr_pipe_poll(void *opaque)
1838{
1839 CharDriverState *chr = opaque;
1840 WinCharState *s = chr->opaque;
1841 DWORD size;
1842
1843 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1844 if (size > 0) {
1845 s->len = size;
1846 win_chr_read_poll(chr);
1847 win_chr_read(chr);
1848 return 1;
1849 }
1850 return 0;
1851}
1852
1853static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1854{
1855 WinCharState *s = chr->opaque;
1856 OVERLAPPED ov;
1857 int ret;
1858 DWORD size;
1859 char openname[256];
1860
1861 s->fpipe = TRUE;
1862
1863 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1864 if (!s->hsend) {
1865 fprintf(stderr, "Failed CreateEvent\n");
1866 goto fail;
1867 }
1868 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1869 if (!s->hrecv) {
1870 fprintf(stderr, "Failed CreateEvent\n");
1871 goto fail;
1872 }
1873
1874 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1875 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1876 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1877 PIPE_WAIT,
1878 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1879 if (s->hcom == INVALID_HANDLE_VALUE) {
1880 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1881 s->hcom = NULL;
1882 goto fail;
1883 }
1884
1885 ZeroMemory(&ov, sizeof(ov));
1886 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1887 ret = ConnectNamedPipe(s->hcom, &ov);
1888 if (ret) {
1889 fprintf(stderr, "Failed ConnectNamedPipe\n");
1890 goto fail;
1891 }
1892
1893 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1894 if (!ret) {
1895 fprintf(stderr, "Failed GetOverlappedResult\n");
1896 if (ov.hEvent) {
1897 CloseHandle(ov.hEvent);
1898 ov.hEvent = NULL;
1899 }
1900 goto fail;
1901 }
1902
1903 if (ov.hEvent) {
1904 CloseHandle(ov.hEvent);
1905 ov.hEvent = NULL;
1906 }
1907 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1908 return 0;
1909
1910 fail:
1911 win_chr_close(chr);
1912 return -1;
1913}
1914
1915
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001916static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001917{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001918 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001919 CharDriverState *chr;
1920 WinCharState *s;
1921
Anthony Liguori7267c092011-08-20 22:09:37 -05001922 chr = g_malloc0(sizeof(CharDriverState));
1923 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001924 chr->opaque = s;
1925 chr->chr_write = win_chr_write;
1926 chr->chr_close = win_chr_close;
1927
1928 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001929 g_free(s);
1930 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001931 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001932 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001933 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001934}
1935
Markus Armbruster1f514702012-02-07 15:09:08 +01001936static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001937{
1938 CharDriverState *chr;
1939 WinCharState *s;
1940
Anthony Liguori7267c092011-08-20 22:09:37 -05001941 chr = g_malloc0(sizeof(CharDriverState));
1942 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001943 s->hcom = fd_out;
1944 chr->opaque = s;
1945 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001946 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001947}
1948
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001949static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001950{
Markus Armbruster1f514702012-02-07 15:09:08 +01001951 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001952}
1953
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001954static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1955{
1956 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1957 DWORD dwSize;
1958 int len1;
1959
1960 len1 = len;
1961
1962 while (len1 > 0) {
1963 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1964 break;
1965 }
1966 buf += dwSize;
1967 len1 -= dwSize;
1968 }
1969
1970 return len - len1;
1971}
1972
1973static void win_stdio_wait_func(void *opaque)
1974{
1975 CharDriverState *chr = opaque;
1976 WinStdioCharState *stdio = chr->opaque;
1977 INPUT_RECORD buf[4];
1978 int ret;
1979 DWORD dwSize;
1980 int i;
1981
1982 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1983 &dwSize);
1984
1985 if (!ret) {
1986 /* Avoid error storm */
1987 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1988 return;
1989 }
1990
1991 for (i = 0; i < dwSize; i++) {
1992 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1993
1994 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1995 int j;
1996 if (kev->uChar.AsciiChar != 0) {
1997 for (j = 0; j < kev->wRepeatCount; j++) {
1998 if (qemu_chr_be_can_write(chr)) {
1999 uint8_t c = kev->uChar.AsciiChar;
2000 qemu_chr_be_write(chr, &c, 1);
2001 }
2002 }
2003 }
2004 }
2005 }
2006}
2007
2008static DWORD WINAPI win_stdio_thread(LPVOID param)
2009{
2010 CharDriverState *chr = param;
2011 WinStdioCharState *stdio = chr->opaque;
2012 int ret;
2013 DWORD dwSize;
2014
2015 while (1) {
2016
2017 /* Wait for one byte */
2018 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2019
2020 /* Exit in case of error, continue if nothing read */
2021 if (!ret) {
2022 break;
2023 }
2024 if (!dwSize) {
2025 continue;
2026 }
2027
2028 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2029 if (stdio->win_stdio_buf == '\r') {
2030 continue;
2031 }
2032
2033 /* Signal the main thread and wait until the byte was eaten */
2034 if (!SetEvent(stdio->hInputReadyEvent)) {
2035 break;
2036 }
2037 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2038 != WAIT_OBJECT_0) {
2039 break;
2040 }
2041 }
2042
2043 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2044 return 0;
2045}
2046
2047static void win_stdio_thread_wait_func(void *opaque)
2048{
2049 CharDriverState *chr = opaque;
2050 WinStdioCharState *stdio = chr->opaque;
2051
2052 if (qemu_chr_be_can_write(chr)) {
2053 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2054 }
2055
2056 SetEvent(stdio->hInputDoneEvent);
2057}
2058
2059static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2060{
2061 WinStdioCharState *stdio = chr->opaque;
2062 DWORD dwMode = 0;
2063
2064 GetConsoleMode(stdio->hStdIn, &dwMode);
2065
2066 if (echo) {
2067 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2068 } else {
2069 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2070 }
2071}
2072
2073static void win_stdio_close(CharDriverState *chr)
2074{
2075 WinStdioCharState *stdio = chr->opaque;
2076
2077 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2078 CloseHandle(stdio->hInputReadyEvent);
2079 }
2080 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2081 CloseHandle(stdio->hInputDoneEvent);
2082 }
2083 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2084 TerminateThread(stdio->hInputThread, 0);
2085 }
2086
2087 g_free(chr->opaque);
2088 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002089}
2090
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002091static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002092{
2093 CharDriverState *chr;
2094 WinStdioCharState *stdio;
2095 DWORD dwMode;
2096 int is_console = 0;
2097
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002098 chr = g_malloc0(sizeof(CharDriverState));
2099 stdio = g_malloc0(sizeof(WinStdioCharState));
2100
2101 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2102 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2103 fprintf(stderr, "cannot open stdio: invalid handle\n");
2104 exit(1);
2105 }
2106
2107 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2108
2109 chr->opaque = stdio;
2110 chr->chr_write = win_stdio_write;
2111 chr->chr_close = win_stdio_close;
2112
Anthony Liguoried7a1542013-03-05 23:21:17 +05302113 if (is_console) {
2114 if (qemu_add_wait_object(stdio->hStdIn,
2115 win_stdio_wait_func, chr)) {
2116 fprintf(stderr, "qemu_add_wait_object: failed\n");
2117 }
2118 } else {
2119 DWORD dwId;
2120
2121 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2122 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2123 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2124 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002125
Anthony Liguoried7a1542013-03-05 23:21:17 +05302126 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2127 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2128 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2129 fprintf(stderr, "cannot create stdio thread or event\n");
2130 exit(1);
2131 }
2132 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2133 win_stdio_thread_wait_func, chr)) {
2134 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002135 }
2136 }
2137
2138 dwMode |= ENABLE_LINE_INPUT;
2139
Anthony Liguoried7a1542013-03-05 23:21:17 +05302140 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002141 /* set the terminal in raw mode */
2142 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2143 dwMode |= ENABLE_PROCESSED_INPUT;
2144 }
2145
2146 SetConsoleMode(stdio->hStdIn, dwMode);
2147
2148 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2149 qemu_chr_fe_set_echo(chr, false);
2150
Markus Armbruster1f514702012-02-07 15:09:08 +01002151 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002152}
aliguori6f97dba2008-10-31 18:49:55 +00002153#endif /* !_WIN32 */
2154
Anthony Liguori5ab82112013-03-05 23:21:31 +05302155
aliguori6f97dba2008-10-31 18:49:55 +00002156/***********************************************************/
2157/* UDP Net console */
2158
2159typedef struct {
2160 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302161 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302162 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002163 int bufcnt;
2164 int bufptr;
2165 int max_size;
2166} NetCharDriver;
2167
2168static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2169{
2170 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302171 gsize bytes_written;
2172 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002173
Anthony Liguori76a96442013-03-05 23:21:21 +05302174 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2175 if (status == G_IO_STATUS_EOF) {
2176 return 0;
2177 } else if (status != G_IO_STATUS_NORMAL) {
2178 return -1;
2179 }
2180
2181 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002182}
2183
2184static int udp_chr_read_poll(void *opaque)
2185{
2186 CharDriverState *chr = opaque;
2187 NetCharDriver *s = chr->opaque;
2188
Anthony Liguori909cda12011-08-15 11:17:31 -05002189 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002190
2191 /* If there were any stray characters in the queue process them
2192 * first
2193 */
2194 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002195 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002196 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002197 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002198 }
2199 return s->max_size;
2200}
2201
Anthony Liguori76a96442013-03-05 23:21:21 +05302202static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002203{
2204 CharDriverState *chr = opaque;
2205 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302206 gsize bytes_read = 0;
2207 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002208
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002209 if (s->max_size == 0) {
2210 return TRUE;
2211 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302212 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2213 &bytes_read, NULL);
2214 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002215 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302216 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302217 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302218 return FALSE;
2219 }
aliguori6f97dba2008-10-31 18:49:55 +00002220
2221 s->bufptr = 0;
2222 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002223 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002224 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002225 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002226 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302227
2228 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002229}
2230
2231static void udp_chr_update_read_handler(CharDriverState *chr)
2232{
2233 NetCharDriver *s = chr->opaque;
2234
Amit Shah26da70c2013-08-28 15:23:37 +05302235 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302236 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302237 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2238 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002239 }
2240}
2241
aliguori819f56b2009-03-28 17:58:14 +00002242static void udp_chr_close(CharDriverState *chr)
2243{
2244 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302245
2246 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302247 if (s->chan) {
2248 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002249 closesocket(s->fd);
2250 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002251 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002252 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002253}
2254
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002255static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002256{
2257 CharDriverState *chr = NULL;
2258 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002259
Anthony Liguori7267c092011-08-20 22:09:37 -05002260 chr = g_malloc0(sizeof(CharDriverState));
2261 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002262
aliguori6f97dba2008-10-31 18:49:55 +00002263 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302264 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002265 s->bufcnt = 0;
2266 s->bufptr = 0;
2267 chr->opaque = s;
2268 chr->chr_write = udp_chr_write;
2269 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002270 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002271 /* be isn't opened until we get a connection */
2272 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002273 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002274}
aliguori6f97dba2008-10-31 18:49:55 +00002275
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002276static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2277{
2278 Error *local_err = NULL;
2279 int fd = -1;
2280
2281 fd = inet_dgram_opts(opts, &local_err);
2282 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002283 qerror_report_err(local_err);
2284 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002285 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002286 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002287 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002288}
2289
2290/***********************************************************/
2291/* TCP Net console */
2292
2293typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302294
2295 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302296 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002297 int fd, listen_fd;
2298 int connected;
2299 int max_size;
2300 int do_telnetopt;
2301 int do_nodelay;
2302 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002303 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002304} TCPCharDriver;
2305
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302306static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002307
2308static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2309{
2310 TCPCharDriver *s = chr->opaque;
2311 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002312 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002313 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002314 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002315 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002316 }
2317}
2318
2319static int tcp_chr_read_poll(void *opaque)
2320{
2321 CharDriverState *chr = opaque;
2322 TCPCharDriver *s = chr->opaque;
2323 if (!s->connected)
2324 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002325 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002326 return s->max_size;
2327}
2328
2329#define IAC 255
2330#define IAC_BREAK 243
2331static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2332 TCPCharDriver *s,
2333 uint8_t *buf, int *size)
2334{
2335 /* Handle any telnet client's basic IAC options to satisfy char by
2336 * char mode with no echo. All IAC options will be removed from
2337 * the buf and the do_telnetopt variable will be used to track the
2338 * state of the width of the IAC information.
2339 *
2340 * IAC commands come in sets of 3 bytes with the exception of the
2341 * "IAC BREAK" command and the double IAC.
2342 */
2343
2344 int i;
2345 int j = 0;
2346
2347 for (i = 0; i < *size; i++) {
2348 if (s->do_telnetopt > 1) {
2349 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2350 /* Double IAC means send an IAC */
2351 if (j != i)
2352 buf[j] = buf[i];
2353 j++;
2354 s->do_telnetopt = 1;
2355 } else {
2356 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2357 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002358 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002359 s->do_telnetopt++;
2360 }
2361 s->do_telnetopt++;
2362 }
2363 if (s->do_telnetopt >= 4) {
2364 s->do_telnetopt = 1;
2365 }
2366 } else {
2367 if ((unsigned char)buf[i] == IAC) {
2368 s->do_telnetopt = 2;
2369 } else {
2370 if (j != i)
2371 buf[j] = buf[i];
2372 j++;
2373 }
2374 }
2375 }
2376 *size = j;
2377}
2378
Mark McLoughlin7d174052009-07-22 09:11:39 +01002379static int tcp_get_msgfd(CharDriverState *chr)
2380{
2381 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002382 int fd = s->msgfd;
2383 s->msgfd = -1;
2384 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002385}
2386
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002387#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002388static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2389{
2390 TCPCharDriver *s = chr->opaque;
2391 struct cmsghdr *cmsg;
2392
2393 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2394 int fd;
2395
2396 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2397 cmsg->cmsg_level != SOL_SOCKET ||
2398 cmsg->cmsg_type != SCM_RIGHTS)
2399 continue;
2400
2401 fd = *((int *)CMSG_DATA(cmsg));
2402 if (fd < 0)
2403 continue;
2404
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002405 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2406 qemu_set_block(fd);
2407
Corey Bryant06138652012-08-14 16:43:42 -04002408#ifndef MSG_CMSG_CLOEXEC
2409 qemu_set_cloexec(fd);
2410#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002411 if (s->msgfd != -1)
2412 close(s->msgfd);
2413 s->msgfd = fd;
2414 }
2415}
2416
Mark McLoughlin9977c892009-07-22 09:11:38 +01002417static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2418{
2419 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002420 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002421 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002422 union {
2423 struct cmsghdr cmsg;
2424 char control[CMSG_SPACE(sizeof(int))];
2425 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002426 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002427 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002428
2429 iov[0].iov_base = buf;
2430 iov[0].iov_len = len;
2431
2432 msg.msg_iov = iov;
2433 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002434 msg.msg_control = &msg_control;
2435 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002436
Corey Bryant06138652012-08-14 16:43:42 -04002437#ifdef MSG_CMSG_CLOEXEC
2438 flags |= MSG_CMSG_CLOEXEC;
2439#endif
2440 ret = recvmsg(s->fd, &msg, flags);
2441 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002442 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002443 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002444
2445 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002446}
2447#else
2448static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2449{
2450 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002451 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002452}
2453#endif
2454
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302455static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2456{
2457 TCPCharDriver *s = chr->opaque;
2458 return g_io_create_watch(s->chan, cond);
2459}
2460
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302461static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002462{
2463 CharDriverState *chr = opaque;
2464 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302465 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002466 int len, size;
2467
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302468 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002469 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302470 }
aliguori6f97dba2008-10-31 18:49:55 +00002471 len = sizeof(buf);
2472 if (len > s->max_size)
2473 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002474 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002475 if (size == 0) {
2476 /* connection closed */
2477 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302478 if (s->listen_chan) {
2479 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002480 }
Amit Shah26da70c2013-08-28 15:23:37 +05302481 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302482 g_io_channel_unref(s->chan);
2483 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002484 closesocket(s->fd);
2485 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002486 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002487 } else if (size > 0) {
2488 if (s->do_telnetopt)
2489 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2490 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002491 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002492 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302493
2494 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002495}
2496
Blue Swirl68c18d12010-08-15 09:46:24 +00002497#ifndef _WIN32
2498CharDriverState *qemu_chr_open_eventfd(int eventfd)
2499{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002500 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002501}
Blue Swirl68c18d12010-08-15 09:46:24 +00002502#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002503
aliguori6f97dba2008-10-31 18:49:55 +00002504static void tcp_chr_connect(void *opaque)
2505{
2506 CharDriverState *chr = opaque;
2507 TCPCharDriver *s = chr->opaque;
2508
2509 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302510 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302511 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2512 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002513 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002514 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002515}
2516
2517#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2518static void tcp_chr_telnet_init(int fd)
2519{
2520 char buf[3];
2521 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2522 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2523 send(fd, (char *)buf, 3, 0);
2524 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2525 send(fd, (char *)buf, 3, 0);
2526 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2527 send(fd, (char *)buf, 3, 0);
2528 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2529 send(fd, (char *)buf, 3, 0);
2530}
2531
Daniel P. Berrange13661082011-06-23 13:31:42 +01002532static int tcp_chr_add_client(CharDriverState *chr, int fd)
2533{
2534 TCPCharDriver *s = chr->opaque;
2535 if (s->fd != -1)
2536 return -1;
2537
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002538 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002539 if (s->do_nodelay)
2540 socket_set_nodelay(fd);
2541 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302542 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002543 if (s->listen_tag) {
2544 g_source_remove(s->listen_tag);
2545 s->listen_tag = 0;
2546 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002547 tcp_chr_connect(chr);
2548
2549 return 0;
2550}
2551
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302552static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002553{
2554 CharDriverState *chr = opaque;
2555 TCPCharDriver *s = chr->opaque;
2556 struct sockaddr_in saddr;
2557#ifndef _WIN32
2558 struct sockaddr_un uaddr;
2559#endif
2560 struct sockaddr *addr;
2561 socklen_t len;
2562 int fd;
2563
2564 for(;;) {
2565#ifndef _WIN32
2566 if (s->is_unix) {
2567 len = sizeof(uaddr);
2568 addr = (struct sockaddr *)&uaddr;
2569 } else
2570#endif
2571 {
2572 len = sizeof(saddr);
2573 addr = (struct sockaddr *)&saddr;
2574 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002575 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002576 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002577 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302578 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002579 } else if (fd >= 0) {
2580 if (s->do_telnetopt)
2581 tcp_chr_telnet_init(fd);
2582 break;
2583 }
2584 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002585 if (tcp_chr_add_client(chr, fd) < 0)
2586 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302587
2588 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002589}
2590
2591static void tcp_chr_close(CharDriverState *chr)
2592{
2593 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002594 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302595 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302596 if (s->chan) {
2597 g_io_channel_unref(s->chan);
2598 }
aliguori6f97dba2008-10-31 18:49:55 +00002599 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002600 }
2601 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302602 if (s->listen_tag) {
2603 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002604 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302605 }
2606 if (s->listen_chan) {
2607 g_io_channel_unref(s->listen_chan);
2608 }
aliguori6f97dba2008-10-31 18:49:55 +00002609 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002610 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002611 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002612 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002613}
2614
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002615static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2616 bool is_listen, bool is_telnet,
2617 bool is_waitconnect,
2618 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002619{
2620 CharDriverState *chr = NULL;
2621 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002622 char host[NI_MAXHOST], serv[NI_MAXSERV];
2623 const char *left = "", *right = "";
2624 struct sockaddr_storage ss;
2625 socklen_t ss_len = sizeof(ss);
2626
2627 memset(&ss, 0, ss_len);
2628 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002629 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002630 return NULL;
2631 }
2632
2633 chr = g_malloc0(sizeof(CharDriverState));
2634 s = g_malloc0(sizeof(TCPCharDriver));
2635
2636 s->connected = 0;
2637 s->fd = -1;
2638 s->listen_fd = -1;
2639 s->msgfd = -1;
2640
2641 chr->filename = g_malloc(256);
2642 switch (ss.ss_family) {
2643#ifndef _WIN32
2644 case AF_UNIX:
2645 s->is_unix = 1;
2646 snprintf(chr->filename, 256, "unix:%s%s",
2647 ((struct sockaddr_un *)(&ss))->sun_path,
2648 is_listen ? ",server" : "");
2649 break;
2650#endif
2651 case AF_INET6:
2652 left = "[";
2653 right = "]";
2654 /* fall through */
2655 case AF_INET:
2656 s->do_nodelay = do_nodelay;
2657 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2658 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002659 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002660 is_telnet ? "telnet" : "tcp",
2661 left, host, right, serv,
2662 is_listen ? ",server" : "");
2663 break;
2664 }
2665
2666 chr->opaque = s;
2667 chr->chr_write = tcp_chr_write;
2668 chr->chr_close = tcp_chr_close;
2669 chr->get_msgfd = tcp_get_msgfd;
2670 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302671 chr->chr_add_watch = tcp_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002672 /* be isn't opened until we get a connection */
2673 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002674
2675 if (is_listen) {
2676 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302677 s->listen_chan = io_channel_from_socket(s->listen_fd);
2678 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002679 if (is_telnet) {
2680 s->do_telnetopt = 1;
2681 }
2682 } else {
2683 s->connected = 1;
2684 s->fd = fd;
2685 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302686 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002687 tcp_chr_connect(chr);
2688 }
2689
2690 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002691 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2692 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302693 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002694 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002695 }
2696 return chr;
2697}
2698
2699static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2700{
2701 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002702 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002703 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002704
liguange990a392013-06-18 11:45:35 +08002705 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2706 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2707 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2708 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2709 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002710
aliguorif07b6002008-11-11 20:54:09 +00002711 if (is_unix) {
2712 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002713 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002714 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002715 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002716 }
2717 } else {
2718 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002719 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002720 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002721 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002722 }
2723 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002724 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002725 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002726 }
aliguori6f97dba2008-10-31 18:49:55 +00002727
2728 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002729 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002730
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002731 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2732 is_waitconnect, &local_err);
2733 if (error_is_set(&local_err)) {
2734 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002735 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002736 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002737
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002738
aliguori6f97dba2008-10-31 18:49:55 +00002739 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002740 if (local_err) {
2741 qerror_report_err(local_err);
2742 error_free(local_err);
2743 }
2744 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002745 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002746 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002747 if (chr) {
2748 g_free(chr->opaque);
2749 g_free(chr);
2750 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002751 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002752}
2753
Lei Li51767e72013-01-25 00:03:19 +08002754/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002755/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002756
2757typedef struct {
2758 size_t size;
2759 size_t prod;
2760 size_t cons;
2761 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002762} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002763
Markus Armbruster3949e592013-02-06 21:27:24 +01002764static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002765{
Markus Armbruster3949e592013-02-06 21:27:24 +01002766 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002767
Markus Armbruster5c230102013-02-06 21:27:23 +01002768 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002769}
2770
Markus Armbruster3949e592013-02-06 21:27:24 +01002771static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002772{
Markus Armbruster3949e592013-02-06 21:27:24 +01002773 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002774 int i;
2775
2776 if (!buf || (len < 0)) {
2777 return -1;
2778 }
2779
2780 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002781 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2782 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002783 d->cons = d->prod - d->size;
2784 }
2785 }
2786
2787 return 0;
2788}
2789
Markus Armbruster3949e592013-02-06 21:27:24 +01002790static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002791{
Markus Armbruster3949e592013-02-06 21:27:24 +01002792 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002793 int i;
2794
Markus Armbruster5c230102013-02-06 21:27:23 +01002795 for (i = 0; i < len && d->cons != d->prod; i++) {
2796 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002797 }
2798
2799 return i;
2800}
2801
Markus Armbruster3949e592013-02-06 21:27:24 +01002802static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002803{
Markus Armbruster3949e592013-02-06 21:27:24 +01002804 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002805
2806 g_free(d->cbuf);
2807 g_free(d);
2808 chr->opaque = NULL;
2809}
2810
Markus Armbruster4f573782013-07-26 16:44:32 +02002811static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2812 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002813{
2814 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002815 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002816
2817 chr = g_malloc0(sizeof(CharDriverState));
2818 d = g_malloc(sizeof(*d));
2819
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002820 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002821
2822 /* The size must be power of 2 */
2823 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02002824 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002825 goto fail;
2826 }
2827
2828 d->prod = 0;
2829 d->cons = 0;
2830 d->cbuf = g_malloc0(d->size);
2831
2832 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002833 chr->chr_write = ringbuf_chr_write;
2834 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002835
2836 return chr;
2837
2838fail:
2839 g_free(d);
2840 g_free(chr);
2841 return NULL;
2842}
2843
Markus Armbruster3949e592013-02-06 21:27:24 +01002844static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002845{
Markus Armbruster3949e592013-02-06 21:27:24 +01002846 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002847}
2848
Markus Armbruster3949e592013-02-06 21:27:24 +01002849void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002850 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002851 Error **errp)
2852{
2853 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002854 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002855 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10002856 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002857
2858 chr = qemu_chr_find(device);
2859 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002860 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002861 return;
2862 }
2863
Markus Armbruster3949e592013-02-06 21:27:24 +01002864 if (!chr_is_ringbuf(chr)) {
2865 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002866 return;
2867 }
2868
Lei Li1f590cf2013-01-25 00:03:20 +08002869 if (has_format && (format == DATA_FORMAT_BASE64)) {
2870 write_data = g_base64_decode(data, &write_count);
2871 } else {
2872 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002873 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002874 }
2875
Markus Armbruster3949e592013-02-06 21:27:24 +01002876 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002877
Markus Armbruster13289fb2013-02-06 21:27:18 +01002878 if (write_data != (uint8_t *)data) {
2879 g_free((void *)write_data);
2880 }
2881
Lei Li1f590cf2013-01-25 00:03:20 +08002882 if (ret < 0) {
2883 error_setg(errp, "Failed to write to device %s", device);
2884 return;
2885 }
2886}
2887
Markus Armbruster3949e592013-02-06 21:27:24 +01002888char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002889 bool has_format, enum DataFormat format,
2890 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002891{
2892 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002893 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002894 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002895 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002896
2897 chr = qemu_chr_find(device);
2898 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002899 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002900 return NULL;
2901 }
2902
Markus Armbruster3949e592013-02-06 21:27:24 +01002903 if (!chr_is_ringbuf(chr)) {
2904 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002905 return NULL;
2906 }
2907
2908 if (size <= 0) {
2909 error_setg(errp, "size must be greater than zero");
2910 return NULL;
2911 }
2912
Markus Armbruster3949e592013-02-06 21:27:24 +01002913 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002914 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002915 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002916
Markus Armbruster3949e592013-02-06 21:27:24 +01002917 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002918
2919 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002920 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002921 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002922 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002923 /*
2924 * FIXME should read only complete, valid UTF-8 characters up
2925 * to @size bytes. Invalid sequences should be replaced by a
2926 * suitable replacement character. Except when (and only
2927 * when) ring buffer lost characters since last read, initial
2928 * continuation characters should be dropped.
2929 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002930 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002931 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002932 }
2933
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002934 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002935}
2936
Gerd Hoffmann33521632009-12-08 13:11:49 +01002937QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002938{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002939 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002940 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002941 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002942 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002943 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002944
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002945 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2946 if (error_is_set(&local_err)) {
2947 qerror_report_err(local_err);
2948 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002949 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002950 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002951
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002952 if (strstart(filename, "mon:", &p)) {
2953 filename = p;
2954 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04002955 if (strcmp(filename, "stdio") == 0) {
2956 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2957 * but pass it to the guest. Handle this only for compat syntax,
2958 * for -chardev syntax we have special option for this.
2959 * This is what -nographic did, redirecting+muxing serial+monitor
2960 * to stdio causing Ctrl+C to be passed to guest. */
2961 qemu_opt_set(opts, "signal", "off");
2962 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002963 }
2964
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002965 if (strcmp(filename, "null") == 0 ||
2966 strcmp(filename, "pty") == 0 ||
2967 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002968 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002969 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002970 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002971 return opts;
2972 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002973 if (strstart(filename, "vc", &p)) {
2974 qemu_opt_set(opts, "backend", "vc");
2975 if (*p == ':') {
2976 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2977 /* pixels */
2978 qemu_opt_set(opts, "width", width);
2979 qemu_opt_set(opts, "height", height);
2980 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2981 /* chars */
2982 qemu_opt_set(opts, "cols", width);
2983 qemu_opt_set(opts, "rows", height);
2984 } else {
2985 goto fail;
2986 }
2987 }
2988 return opts;
2989 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002990 if (strcmp(filename, "con:") == 0) {
2991 qemu_opt_set(opts, "backend", "console");
2992 return opts;
2993 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002994 if (strstart(filename, "COM", NULL)) {
2995 qemu_opt_set(opts, "backend", "serial");
2996 qemu_opt_set(opts, "path", filename);
2997 return opts;
2998 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002999 if (strstart(filename, "file:", &p)) {
3000 qemu_opt_set(opts, "backend", "file");
3001 qemu_opt_set(opts, "path", p);
3002 return opts;
3003 }
3004 if (strstart(filename, "pipe:", &p)) {
3005 qemu_opt_set(opts, "backend", "pipe");
3006 qemu_opt_set(opts, "path", p);
3007 return opts;
3008 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003009 if (strstart(filename, "tcp:", &p) ||
3010 strstart(filename, "telnet:", &p)) {
3011 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3012 host[0] = 0;
3013 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3014 goto fail;
3015 }
3016 qemu_opt_set(opts, "backend", "socket");
3017 qemu_opt_set(opts, "host", host);
3018 qemu_opt_set(opts, "port", port);
3019 if (p[pos] == ',') {
3020 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3021 goto fail;
3022 }
3023 if (strstart(filename, "telnet:", &p))
3024 qemu_opt_set(opts, "telnet", "on");
3025 return opts;
3026 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003027 if (strstart(filename, "udp:", &p)) {
3028 qemu_opt_set(opts, "backend", "udp");
3029 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3030 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003031 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003032 goto fail;
3033 }
3034 }
3035 qemu_opt_set(opts, "host", host);
3036 qemu_opt_set(opts, "port", port);
3037 if (p[pos] == '@') {
3038 p += pos + 1;
3039 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3040 host[0] = 0;
3041 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003042 goto fail;
3043 }
3044 }
3045 qemu_opt_set(opts, "localaddr", host);
3046 qemu_opt_set(opts, "localport", port);
3047 }
3048 return opts;
3049 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003050 if (strstart(filename, "unix:", &p)) {
3051 qemu_opt_set(opts, "backend", "socket");
3052 if (qemu_opts_do_parse(opts, p, "path") != 0)
3053 goto fail;
3054 return opts;
3055 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003056 if (strstart(filename, "/dev/parport", NULL) ||
3057 strstart(filename, "/dev/ppi", NULL)) {
3058 qemu_opt_set(opts, "backend", "parport");
3059 qemu_opt_set(opts, "path", filename);
3060 return opts;
3061 }
3062 if (strstart(filename, "/dev/", NULL)) {
3063 qemu_opt_set(opts, "backend", "tty");
3064 qemu_opt_set(opts, "path", filename);
3065 return opts;
3066 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003067
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003068fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003069 qemu_opts_del(opts);
3070 return NULL;
3071}
3072
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003073static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3074 Error **errp)
3075{
3076 const char *path = qemu_opt_get(opts, "path");
3077
3078 if (path == NULL) {
3079 error_setg(errp, "chardev: file: no filename given");
3080 return;
3081 }
3082 backend->file = g_new0(ChardevFile, 1);
3083 backend->file->out = g_strdup(path);
3084}
3085
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003086static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3087 Error **errp)
3088{
3089 backend->stdio = g_new0(ChardevStdio, 1);
3090 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003091 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003092}
3093
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003094static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3095 Error **errp)
3096{
3097 const char *device = qemu_opt_get(opts, "path");
3098
3099 if (device == NULL) {
3100 error_setg(errp, "chardev: serial/tty: no device path given");
3101 return;
3102 }
3103 backend->serial = g_new0(ChardevHostdev, 1);
3104 backend->serial->device = g_strdup(device);
3105}
3106
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003107static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3108 Error **errp)
3109{
3110 const char *device = qemu_opt_get(opts, "path");
3111
3112 if (device == NULL) {
3113 error_setg(errp, "chardev: parallel: no device path given");
3114 return;
3115 }
3116 backend->parallel = g_new0(ChardevHostdev, 1);
3117 backend->parallel->device = g_strdup(device);
3118}
3119
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003120static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3121 Error **errp)
3122{
3123 const char *device = qemu_opt_get(opts, "path");
3124
3125 if (device == NULL) {
3126 error_setg(errp, "chardev: pipe: no device path given");
3127 return;
3128 }
3129 backend->pipe = g_new0(ChardevHostdev, 1);
3130 backend->pipe->device = g_strdup(device);
3131}
3132
Markus Armbruster4f573782013-07-26 16:44:32 +02003133static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3134 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003135{
3136 int val;
3137
Markus Armbruster3a1da422013-07-26 16:44:34 +02003138 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003139
Markus Armbruster0f953052013-06-27 16:22:07 +02003140 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003141 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003142 backend->ringbuf->has_size = true;
3143 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003144 }
3145}
3146
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003147static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3148 Error **errp)
3149{
3150 const char *chardev = qemu_opt_get(opts, "chardev");
3151
3152 if (chardev == NULL) {
3153 error_setg(errp, "chardev: mux: no chardev given");
3154 return;
3155 }
3156 backend->mux = g_new0(ChardevMux, 1);
3157 backend->mux->chardev = g_strdup(chardev);
3158}
3159
Anthony Liguorid654f342013-03-05 23:21:28 +05303160typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003161 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003162 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003163 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003164 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003165 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003166 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303167} CharDriver;
3168
3169static GSList *backends;
3170
3171void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3172{
3173 CharDriver *s;
3174
3175 s = g_malloc0(sizeof(*s));
3176 s->name = g_strdup(name);
3177 s->open = open;
3178
3179 backends = g_slist_append(backends, s);
3180}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003181
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003182void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003183 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3184{
3185 CharDriver *s;
3186
3187 s = g_malloc0(sizeof(*s));
3188 s->name = g_strdup(name);
3189 s->kind = kind;
3190 s->parse = parse;
3191
3192 backends = g_slist_append(backends, s);
3193}
3194
Anthony Liguorif69554b2011-08-15 11:17:37 -05003195CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003196 void (*init)(struct CharDriverState *s),
3197 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003198{
Anthony Liguorid654f342013-03-05 23:21:28 +05303199 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003200 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303201 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003202
3203 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003204 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003205 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003206 }
3207
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003208 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003209 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003210 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003211 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003212 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303213 for (i = backends; i; i = i->next) {
3214 cd = i->data;
3215
3216 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003217 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303218 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003219 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303220 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003221 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003222 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003223 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003224 }
3225
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003226 if (!cd->open) {
3227 /* using new, qapi init */
3228 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3229 ChardevReturn *ret = NULL;
3230 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003231 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003232
3233 if (qemu_opt_get_bool(opts, "mux", 0)) {
3234 bid = g_strdup_printf("%s-base", id);
3235 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003236
3237 chr = NULL;
3238 backend->kind = cd->kind;
3239 if (cd->parse) {
3240 cd->parse(opts, backend, errp);
3241 if (error_is_set(errp)) {
3242 goto qapi_out;
3243 }
3244 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003245 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003246 if (error_is_set(errp)) {
3247 goto qapi_out;
3248 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003249
3250 if (bid) {
3251 qapi_free_ChardevBackend(backend);
3252 qapi_free_ChardevReturn(ret);
3253 backend = g_new0(ChardevBackend, 1);
3254 backend->mux = g_new0(ChardevMux, 1);
3255 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3256 backend->mux->chardev = g_strdup(bid);
3257 ret = qmp_chardev_add(id, backend, errp);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003258 assert(!error_is_set(errp));
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003259 }
3260
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003261 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003262 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003263
3264 qapi_out:
3265 qapi_free_ChardevBackend(backend);
3266 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003267 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003268 return chr;
3269 }
3270
Anthony Liguorid654f342013-03-05 23:21:28 +05303271 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003272 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003273 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003274 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003275 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003276 }
3277
3278 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003279 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003280 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003281 /* if we didn't create the chardev via qmp_chardev_add, we
3282 * need to send the OPENED event here
3283 */
3284 if (!chr->explicit_be_open) {
3285 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3286 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003287 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003288
3289 if (qemu_opt_get_bool(opts, "mux", 0)) {
3290 CharDriverState *base = chr;
3291 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003292 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003293 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3294 chr = qemu_chr_open_mux(base);
3295 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003296 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003297 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003298 } else {
3299 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003300 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003301 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003302 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003303 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003304
3305err:
3306 qemu_opts_del(opts);
3307 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003308}
3309
Anthony Liguori27143a42011-08-15 11:17:36 -05003310CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003311{
3312 const char *p;
3313 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003314 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003315 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003316
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003317 if (strstart(filename, "chardev:", &p)) {
3318 return qemu_chr_find(p);
3319 }
3320
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003321 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003322 if (!opts)
3323 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003324
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003325 chr = qemu_chr_new_from_opts(opts, init, &err);
3326 if (error_is_set(&err)) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003327 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003328 error_free(err);
3329 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003330 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003331 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003332 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003333 }
3334 return chr;
3335}
3336
Anthony Liguori15f31512011-08-15 11:17:35 -05003337void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003338{
3339 if (chr->chr_set_echo) {
3340 chr->chr_set_echo(chr, echo);
3341 }
3342}
3343
Hans de Goede8e25daa2013-03-26 11:07:57 +01003344void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003345{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003346 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003347 return;
3348 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003349 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003350 if (chr->chr_set_fe_open) {
3351 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003352 }
3353}
3354
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003355int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3356 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303357{
3358 GSource *src;
3359 guint tag;
3360
3361 if (s->chr_add_watch == NULL) {
3362 return -ENOSYS;
3363 }
3364
3365 src = s->chr_add_watch(s, cond);
3366 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3367 tag = g_source_attach(src, NULL);
3368 g_source_unref(src);
3369
3370 return tag;
3371}
3372
Hans de Goede44c473d2013-03-27 20:29:39 +01003373int qemu_chr_fe_claim(CharDriverState *s)
3374{
3375 if (s->avail_connections < 1) {
3376 return -1;
3377 }
3378 s->avail_connections--;
3379 return 0;
3380}
3381
3382void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3383{
3384 if (qemu_chr_fe_claim(s) != 0) {
3385 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3386 __func__, s->label);
3387 exit(1);
3388 }
3389}
3390
3391void qemu_chr_fe_release(CharDriverState *s)
3392{
3393 s->avail_connections++;
3394}
3395
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003396void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003397{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003398 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003399 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003400 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003401 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003402 g_free(chr->filename);
3403 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003404 if (chr->opts) {
3405 qemu_opts_del(chr->opts);
3406 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003407 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003408}
3409
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003410ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003411{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003412 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003413 CharDriverState *chr;
3414
Blue Swirl72cf2d42009-09-12 07:36:22 +00003415 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003416 ChardevInfoList *info = g_malloc0(sizeof(*info));
3417 info->value = g_malloc0(sizeof(*info->value));
3418 info->value->label = g_strdup(chr->label);
3419 info->value->filename = g_strdup(chr->filename);
3420
3421 info->next = chr_list;
3422 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003423 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003424
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003425 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003426}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003427
3428CharDriverState *qemu_chr_find(const char *name)
3429{
3430 CharDriverState *chr;
3431
Blue Swirl72cf2d42009-09-12 07:36:22 +00003432 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003433 if (strcmp(chr->label, name) != 0)
3434 continue;
3435 return chr;
3436 }
3437 return NULL;
3438}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003439
3440/* Get a character (serial) device interface. */
3441CharDriverState *qemu_char_get_next_serial(void)
3442{
3443 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003444 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003445
3446 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003447
3448 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3449 chr = serial_hds[next_serial++];
3450 qemu_chr_fe_claim_no_fail(chr);
3451 return chr;
3452 }
3453 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003454}
3455
Paolo Bonzini4d454572012-11-26 16:03:42 +01003456QemuOptsList qemu_chardev_opts = {
3457 .name = "chardev",
3458 .implied_opt_name = "backend",
3459 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3460 .desc = {
3461 {
3462 .name = "backend",
3463 .type = QEMU_OPT_STRING,
3464 },{
3465 .name = "path",
3466 .type = QEMU_OPT_STRING,
3467 },{
3468 .name = "host",
3469 .type = QEMU_OPT_STRING,
3470 },{
3471 .name = "port",
3472 .type = QEMU_OPT_STRING,
3473 },{
3474 .name = "localaddr",
3475 .type = QEMU_OPT_STRING,
3476 },{
3477 .name = "localport",
3478 .type = QEMU_OPT_STRING,
3479 },{
3480 .name = "to",
3481 .type = QEMU_OPT_NUMBER,
3482 },{
3483 .name = "ipv4",
3484 .type = QEMU_OPT_BOOL,
3485 },{
3486 .name = "ipv6",
3487 .type = QEMU_OPT_BOOL,
3488 },{
3489 .name = "wait",
3490 .type = QEMU_OPT_BOOL,
3491 },{
3492 .name = "server",
3493 .type = QEMU_OPT_BOOL,
3494 },{
3495 .name = "delay",
3496 .type = QEMU_OPT_BOOL,
3497 },{
3498 .name = "telnet",
3499 .type = QEMU_OPT_BOOL,
3500 },{
3501 .name = "width",
3502 .type = QEMU_OPT_NUMBER,
3503 },{
3504 .name = "height",
3505 .type = QEMU_OPT_NUMBER,
3506 },{
3507 .name = "cols",
3508 .type = QEMU_OPT_NUMBER,
3509 },{
3510 .name = "rows",
3511 .type = QEMU_OPT_NUMBER,
3512 },{
3513 .name = "mux",
3514 .type = QEMU_OPT_BOOL,
3515 },{
3516 .name = "signal",
3517 .type = QEMU_OPT_BOOL,
3518 },{
3519 .name = "name",
3520 .type = QEMU_OPT_STRING,
3521 },{
3522 .name = "debug",
3523 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003524 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003525 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003526 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003527 },{
3528 .name = "chardev",
3529 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003530 },
3531 { /* end of list */ }
3532 },
3533};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003534
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003535#ifdef _WIN32
3536
3537static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3538{
3539 HANDLE out;
3540
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003541 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003542 error_setg(errp, "input file not supported");
3543 return NULL;
3544 }
3545
3546 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3547 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3548 if (out == INVALID_HANDLE_VALUE) {
3549 error_setg(errp, "open %s failed", file->out);
3550 return NULL;
3551 }
3552 return qemu_chr_open_win_file(out);
3553}
3554
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003555static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3556 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003557{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003558 return qemu_chr_open_win_path(serial->device);
3559}
3560
3561static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3562 Error **errp)
3563{
3564 error_setg(errp, "character device backend type 'parallel' not supported");
3565 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003566}
3567
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003568#else /* WIN32 */
3569
3570static int qmp_chardev_open_file_source(char *src, int flags,
3571 Error **errp)
3572{
3573 int fd = -1;
3574
3575 TFR(fd = qemu_open(src, flags, 0666));
3576 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003577 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003578 }
3579 return fd;
3580}
3581
3582static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3583{
3584 int flags, in = -1, out = -1;
3585
3586 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3587 out = qmp_chardev_open_file_source(file->out, flags, errp);
3588 if (error_is_set(errp)) {
3589 return NULL;
3590 }
3591
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003592 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003593 flags = O_RDONLY;
3594 in = qmp_chardev_open_file_source(file->in, flags, errp);
3595 if (error_is_set(errp)) {
3596 qemu_close(out);
3597 return NULL;
3598 }
3599 }
3600
3601 return qemu_chr_open_fd(in, out);
3602}
3603
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003604static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3605 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003606{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003607#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003608 int fd;
3609
3610 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3611 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003612 return NULL;
3613 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003614 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003615 return qemu_chr_open_tty_fd(fd);
3616#else
3617 error_setg(errp, "character device backend type 'serial' not supported");
3618 return NULL;
3619#endif
3620}
3621
3622static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3623 Error **errp)
3624{
3625#ifdef HAVE_CHARDEV_PARPORT
3626 int fd;
3627
3628 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3629 if (error_is_set(errp)) {
3630 return NULL;
3631 }
3632 return qemu_chr_open_pp_fd(fd);
3633#else
3634 error_setg(errp, "character device backend type 'parallel' not supported");
3635 return NULL;
3636#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003637}
3638
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003639#endif /* WIN32 */
3640
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003641static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3642 Error **errp)
3643{
3644 SocketAddress *addr = sock->addr;
3645 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3646 bool is_listen = sock->has_server ? sock->server : true;
3647 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3648 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3649 int fd;
3650
3651 if (is_listen) {
3652 fd = socket_listen(addr, errp);
3653 } else {
3654 fd = socket_connect(addr, errp, NULL, NULL);
3655 }
3656 if (error_is_set(errp)) {
3657 return NULL;
3658 }
3659 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3660 is_telnet, is_waitconnect, errp);
3661}
3662
Lei Li08d0ab32013-05-20 14:51:03 +08003663static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3664 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003665{
3666 int fd;
3667
Lei Li08d0ab32013-05-20 14:51:03 +08003668 fd = socket_dgram(udp->remote, udp->local, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003669 if (error_is_set(errp)) {
3670 return NULL;
3671 }
3672 return qemu_chr_open_udp_fd(fd);
3673}
3674
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003675ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3676 Error **errp)
3677{
3678 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003679 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003680
3681 chr = qemu_chr_find(id);
3682 if (chr) {
3683 error_setg(errp, "Chardev '%s' already exists", id);
3684 g_free(ret);
3685 return NULL;
3686 }
3687
3688 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003689 case CHARDEV_BACKEND_KIND_FILE:
3690 chr = qmp_chardev_open_file(backend->file, errp);
3691 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003692 case CHARDEV_BACKEND_KIND_SERIAL:
3693 chr = qmp_chardev_open_serial(backend->serial, errp);
3694 break;
3695 case CHARDEV_BACKEND_KIND_PARALLEL:
3696 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003697 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003698 case CHARDEV_BACKEND_KIND_PIPE:
3699 chr = qemu_chr_open_pipe(backend->pipe);
3700 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003701 case CHARDEV_BACKEND_KIND_SOCKET:
3702 chr = qmp_chardev_open_socket(backend->socket, errp);
3703 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003704 case CHARDEV_BACKEND_KIND_UDP:
3705 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003706 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003707#ifdef HAVE_CHARDEV_TTY
3708 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003709 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003710 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003711#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003712 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003713 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003714 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003715 case CHARDEV_BACKEND_KIND_MUX:
3716 base = qemu_chr_find(backend->mux->chardev);
3717 if (base == NULL) {
3718 error_setg(errp, "mux: base chardev %s not found",
3719 backend->mux->chardev);
3720 break;
3721 }
3722 chr = qemu_chr_open_mux(base);
3723 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003724 case CHARDEV_BACKEND_KIND_MSMOUSE:
3725 chr = qemu_chr_open_msmouse();
3726 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003727#ifdef CONFIG_BRLAPI
3728 case CHARDEV_BACKEND_KIND_BRAILLE:
3729 chr = chr_baum_init();
3730 break;
3731#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003732 case CHARDEV_BACKEND_KIND_STDIO:
3733 chr = qemu_chr_open_stdio(backend->stdio);
3734 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003735#ifdef _WIN32
3736 case CHARDEV_BACKEND_KIND_CONSOLE:
3737 chr = qemu_chr_open_win_con();
3738 break;
3739#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003740#ifdef CONFIG_SPICE
3741 case CHARDEV_BACKEND_KIND_SPICEVMC:
3742 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3743 break;
3744 case CHARDEV_BACKEND_KIND_SPICEPORT:
3745 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3746 break;
3747#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003748 case CHARDEV_BACKEND_KIND_VC:
3749 chr = vc_init(backend->vc);
3750 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003751 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003752 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003753 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003754 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003755 default:
3756 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3757 break;
3758 }
3759
3760 if (chr == NULL && !error_is_set(errp)) {
3761 error_setg(errp, "Failed to create chardev");
3762 }
3763 if (chr) {
3764 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003765 chr->avail_connections =
3766 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02003767 if (!chr->filename) {
3768 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
3769 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05003770 if (!chr->explicit_be_open) {
3771 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3772 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003773 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3774 return ret;
3775 } else {
3776 g_free(ret);
3777 return NULL;
3778 }
3779}
3780
3781void qmp_chardev_remove(const char *id, Error **errp)
3782{
3783 CharDriverState *chr;
3784
3785 chr = qemu_chr_find(id);
3786 if (NULL == chr) {
3787 error_setg(errp, "Chardev '%s' not found", id);
3788 return;
3789 }
3790 if (chr->chr_can_read || chr->chr_read ||
3791 chr->chr_event || chr->handler_opaque) {
3792 error_setg(errp, "Chardev '%s' is busy", id);
3793 return;
3794 }
3795 qemu_chr_delete(chr);
3796}
Anthony Liguorid654f342013-03-05 23:21:28 +05303797
3798static void register_types(void)
3799{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003800 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303801 register_char_driver("socket", qemu_chr_open_socket);
3802 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02003803 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02003804 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003805 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3806 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003807 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3808 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003809 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3810 qemu_chr_parse_serial);
3811 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3812 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003813 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3814 qemu_chr_parse_parallel);
3815 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3816 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003817 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003818 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003819 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3820 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003821 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
3822 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02003823 /* Bug-compatibility: */
3824 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3825 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05003826 /* this must be done after machine init, since we register FEs with muxes
3827 * as part of realize functions like serial_isa_realizefn when -nographic
3828 * is specified
3829 */
3830 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05303831}
3832
3833type_init(register_types);