blob: 16f3ad77de24bee635399d413de41417b7d296b1 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57#include <dev/ppbus/ppi.h>
58#include <dev/ppbus/ppbconf.h>
59#elif defined(__DragonFly__)
60#include <dev/misc/ppi/ppi.h>
61#include <bus/ppbus/ppbconf.h>
62#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010063#else
aliguori6f97dba2008-10-31 18:49:55 +000064#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000065#include <linux/ppdev.h>
66#include <linux/parport.h>
67#endif
68#ifdef __sun__
69#include <sys/stat.h>
70#include <sys/ethernet.h>
71#include <sys/sockio.h>
72#include <netinet/arp.h>
73#include <netinet/in.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_icmp.h> // must come after ip.h
77#include <netinet/udp.h>
78#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000079#endif
80#endif
81#endif
82
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010083#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020084#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000085
Amit Shah9bd78542009-11-03 19:59:54 +053086#define READ_BUF_LEN 4096
87
aliguori6f97dba2008-10-31 18:49:55 +000088/***********************************************************/
89/* character device */
90
Blue Swirl72cf2d42009-09-12 07:36:22 +000091static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000093
Hans de Goedea425d232011-11-19 10:22:43 +010094void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +000095{
Alexander Graf73cdf3f2010-04-01 18:42:39 +020096 /* Keep track if the char device is open */
97 switch (event) {
98 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +010099 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200100 break;
101 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100102 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200103 break;
104 }
105
aliguori6f97dba2008-10-31 18:49:55 +0000106 if (!s->chr_event)
107 return;
108 s->chr_event(s->handler_opaque, event);
109}
110
Hans de Goedefee204f2013-03-26 11:07:54 +0100111void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000112{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500113 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000114}
115
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500116int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000117{
118 return s->chr_write(s, buf, len);
119}
120
Anthony Liguoricd187202013-03-26 10:04:17 -0500121int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
122{
123 int offset = 0;
124 int res;
125
126 while (offset < len) {
127 do {
128 res = s->chr_write(s, buf + offset, len - offset);
129 if (res == -1 && errno == EAGAIN) {
130 g_usleep(100);
131 }
132 } while (res == -1 && errno == EAGAIN);
133
134 if (res == 0) {
135 break;
136 }
137
138 if (res < 0) {
139 return res;
140 }
141
142 offset += res;
143 }
144
145 return offset;
146}
147
Anthony Liguori41084f12011-08-15 11:17:34 -0500148int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000149{
150 if (!s->chr_ioctl)
151 return -ENOTSUP;
152 return s->chr_ioctl(s, cmd, arg);
153}
154
Anthony Liguori909cda12011-08-15 11:17:31 -0500155int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000156{
157 if (!s->chr_can_read)
158 return 0;
159 return s->chr_can_read(s->handler_opaque);
160}
161
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500162void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000163{
Stefan Weilac310732012-04-19 22:27:14 +0200164 if (s->chr_read) {
165 s->chr_read(s->handler_opaque, buf, len);
166 }
aliguori6f97dba2008-10-31 18:49:55 +0000167}
168
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500169int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100170{
171 return s->get_msgfd ? s->get_msgfd(s) : -1;
172}
173
Daniel P. Berrange13661082011-06-23 13:31:42 +0100174int qemu_chr_add_client(CharDriverState *s, int fd)
175{
176 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
177}
178
aliguori6f97dba2008-10-31 18:49:55 +0000179void qemu_chr_accept_input(CharDriverState *s)
180{
181 if (s->chr_accept_input)
182 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100183 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000184}
185
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500186void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000187{
Amit Shah9bd78542009-11-03 19:59:54 +0530188 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000189 va_list ap;
190 va_start(ap, fmt);
191 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500192 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000193 va_end(ap);
194}
195
aliguori6f97dba2008-10-31 18:49:55 +0000196void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100197 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000198 IOReadHandler *fd_read,
199 IOEventHandler *fd_event,
200 void *opaque)
201{
Hans de Goede19083222013-03-26 11:07:56 +0100202 int fe_open;
203
Amit Shahda7d9982011-04-25 15:18:22 +0530204 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100205 fe_open = 0;
206 } else {
207 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530208 }
aliguori6f97dba2008-10-31 18:49:55 +0000209 s->chr_can_read = fd_can_read;
210 s->chr_read = fd_read;
211 s->chr_event = fd_event;
212 s->handler_opaque = opaque;
213 if (s->chr_update_read_handler)
214 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200215
Hans de Goede19083222013-03-26 11:07:56 +0100216 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100217 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100218 }
219
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200220 /* We're connecting to an already opened device, so let's make sure we
221 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100222 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100223 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200224 }
aliguori6f97dba2008-10-31 18:49:55 +0000225}
226
227static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
228{
229 return len;
230}
231
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100232static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000233{
234 CharDriverState *chr;
235
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000237 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500238 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100239 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000240}
241
242/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000243#define MAX_MUX 4
244#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
245#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
246typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100247 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000248 IOReadHandler *chr_read[MAX_MUX];
249 IOEventHandler *chr_event[MAX_MUX];
250 void *ext_opaque[MAX_MUX];
251 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200252 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000253 int mux_cnt;
254 int term_got_escape;
255 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000256 /* Intermediate input buffer allows to catch escape sequences even if the
257 currently active device is not accepting any input - but only until it
258 is full as well. */
259 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
260 int prod[MAX_MUX];
261 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200262 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200263 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200264 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000265} MuxDriver;
266
267
268static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
269{
270 MuxDriver *d = chr->opaque;
271 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200272 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000273 ret = d->drv->chr_write(d->drv, buf, len);
274 } else {
275 int i;
276
277 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200278 for (i = 0; i < len; i++) {
279 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000280 char buf1[64];
281 int64_t ti;
282 int secs;
283
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100284 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200285 if (d->timestamps_start == -1)
286 d->timestamps_start = ti;
287 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000288 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000289 snprintf(buf1, sizeof(buf1),
290 "[%02d:%02d:%02d.%03d] ",
291 secs / 3600,
292 (secs / 60) % 60,
293 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000294 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000295 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200296 d->linestart = 0;
297 }
298 ret += d->drv->chr_write(d->drv, buf+i, 1);
299 if (buf[i] == '\n') {
300 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000301 }
302 }
303 }
304 return ret;
305}
306
307static const char * const mux_help[] = {
308 "% h print this help\n\r",
309 "% x exit emulator\n\r",
310 "% s save disk data back to file (if -snapshot)\n\r",
311 "% t toggle console timestamps\n\r"
312 "% b send break (magic sysrq)\n\r",
313 "% c switch between console and monitor\n\r",
314 "% % sends %\n\r",
315 NULL
316};
317
318int term_escape_char = 0x01; /* ctrl-a is used for escape */
319static void mux_print_help(CharDriverState *chr)
320{
321 int i, j;
322 char ebuf[15] = "Escape-Char";
323 char cbuf[50] = "\n\r";
324
325 if (term_escape_char > 0 && term_escape_char < 26) {
326 snprintf(cbuf, sizeof(cbuf), "\n\r");
327 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
328 } else {
329 snprintf(cbuf, sizeof(cbuf),
330 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
331 term_escape_char);
332 }
333 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
334 for (i = 0; mux_help[i] != NULL; i++) {
335 for (j=0; mux_help[i][j] != '\0'; j++) {
336 if (mux_help[i][j] == '%')
337 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
338 else
339 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
340 }
341 }
342}
343
aliguori2724b182009-03-05 23:01:47 +0000344static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
345{
346 if (d->chr_event[mux_nr])
347 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
348}
349
aliguori6f97dba2008-10-31 18:49:55 +0000350static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
351{
352 if (d->term_got_escape) {
353 d->term_got_escape = 0;
354 if (ch == term_escape_char)
355 goto send_char;
356 switch(ch) {
357 case '?':
358 case 'h':
359 mux_print_help(chr);
360 break;
361 case 'x':
362 {
363 const char *term = "QEMU: Terminated\n\r";
364 chr->chr_write(chr,(uint8_t *)term,strlen(term));
365 exit(0);
366 break;
367 }
368 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200369 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000370 break;
371 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100372 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000373 break;
374 case 'c':
375 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200376 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
377 d->focus++;
378 if (d->focus >= d->mux_cnt)
379 d->focus = 0;
380 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000381 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200382 case 't':
383 d->timestamps = !d->timestamps;
384 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200385 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200386 break;
aliguori6f97dba2008-10-31 18:49:55 +0000387 }
388 } else if (ch == term_escape_char) {
389 d->term_got_escape = 1;
390 } else {
391 send_char:
392 return 1;
393 }
394 return 0;
395}
396
397static void mux_chr_accept_input(CharDriverState *chr)
398{
aliguori6f97dba2008-10-31 18:49:55 +0000399 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200400 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000401
aliguoria80bf992009-03-05 23:00:02 +0000402 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000403 d->chr_can_read[m] &&
404 d->chr_can_read[m](d->ext_opaque[m])) {
405 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000406 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000407 }
408}
409
410static int mux_chr_can_read(void *opaque)
411{
412 CharDriverState *chr = opaque;
413 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200414 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000415
aliguoria80bf992009-03-05 23:00:02 +0000416 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000417 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000418 if (d->chr_can_read[m])
419 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000420 return 0;
421}
422
423static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
424{
425 CharDriverState *chr = opaque;
426 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200427 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000428 int i;
429
430 mux_chr_accept_input (opaque);
431
432 for(i = 0; i < size; i++)
433 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000434 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000435 d->chr_can_read[m] &&
436 d->chr_can_read[m](d->ext_opaque[m]))
437 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
438 else
aliguoria80bf992009-03-05 23:00:02 +0000439 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000440 }
441}
442
443static void mux_chr_event(void *opaque, int event)
444{
445 CharDriverState *chr = opaque;
446 MuxDriver *d = chr->opaque;
447 int i;
448
449 /* Send the event to all registered listeners */
450 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000451 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000452}
453
454static void mux_chr_update_read_handler(CharDriverState *chr)
455{
456 MuxDriver *d = chr->opaque;
457
458 if (d->mux_cnt >= MAX_MUX) {
459 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
460 return;
461 }
462 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
463 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
464 d->chr_read[d->mux_cnt] = chr->chr_read;
465 d->chr_event[d->mux_cnt] = chr->chr_event;
466 /* Fix up the real driver with mux routines */
467 if (d->mux_cnt == 0) {
468 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
469 mux_chr_event, chr);
470 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200471 if (d->focus != -1) {
472 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200473 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200474 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000475 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200476 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000477}
478
Michael Roth7b7ab182013-07-30 13:04:22 -0500479static bool muxes_realized;
480
481/**
482 * Called after processing of default and command-line-specified
483 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
484 * to a mux chardev. This is done here to ensure that
485 * output/prompts/banners are only displayed for the FE that has
486 * focus when initial command-line processing/machine init is
487 * completed.
488 *
489 * After this point, any new FE attached to any new or existing
490 * mux will receive CHR_EVENT_OPENED notifications for the BE
491 * immediately.
492 */
493static void muxes_realize_done(Notifier *notifier, void *unused)
494{
495 CharDriverState *chr;
496
497 QTAILQ_FOREACH(chr, &chardevs, next) {
498 if (chr->is_mux) {
499 MuxDriver *d = chr->opaque;
500 int i;
501
502 /* send OPENED to all already-attached FEs */
503 for (i = 0; i < d->mux_cnt; i++) {
504 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
505 }
506 /* mark mux as OPENED so any new FEs will immediately receive
507 * OPENED event
508 */
509 qemu_chr_be_generic_open(chr);
510 }
511 }
512 muxes_realized = true;
513}
514
515static Notifier muxes_realize_notify = {
516 .notify = muxes_realize_done,
517};
518
aliguori6f97dba2008-10-31 18:49:55 +0000519static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
520{
521 CharDriverState *chr;
522 MuxDriver *d;
523
Anthony Liguori7267c092011-08-20 22:09:37 -0500524 chr = g_malloc0(sizeof(CharDriverState));
525 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000526
527 chr->opaque = d;
528 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200529 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000530 chr->chr_write = mux_chr_write;
531 chr->chr_update_read_handler = mux_chr_update_read_handler;
532 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100533 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100534 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500535 /* only default to opened state if we've realized the initial
536 * set of muxes
537 */
538 chr->explicit_be_open = muxes_realized ? 0 : 1;
539 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200540
aliguori6f97dba2008-10-31 18:49:55 +0000541 return chr;
542}
543
544
545#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000546int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000547{
548 int ret, len;
549
550 len = len1;
551 while (len > 0) {
552 ret = send(fd, buf, len, 0);
553 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000554 errno = WSAGetLastError();
555 if (errno != WSAEWOULDBLOCK) {
556 return -1;
557 }
558 } else if (ret == 0) {
559 break;
560 } else {
561 buf += ret;
562 len -= ret;
563 }
564 }
565 return len1 - len;
566}
567
568#else
569
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100570int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000571{
572 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100573 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000574
575 len = len1;
576 while (len > 0) {
577 ret = write(fd, buf, len);
578 if (ret < 0) {
579 if (errno != EINTR && errno != EAGAIN)
580 return -1;
581 } else if (ret == 0) {
582 break;
583 } else {
584 buf += ret;
585 len -= ret;
586 }
587 }
588 return len1 - len;
589}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500590
591int recv_all(int fd, void *_buf, int len1, bool single_read)
592{
593 int ret, len;
594 uint8_t *buf = _buf;
595
596 len = len1;
597 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
598 if (ret < 0) {
599 if (errno != EINTR && errno != EAGAIN) {
600 return -1;
601 }
602 continue;
603 } else {
604 if (single_read) {
605 return ret;
606 }
607 buf += ret;
608 len -= ret;
609 }
610 }
611 return len1 - len;
612}
613
aliguori6f97dba2008-10-31 18:49:55 +0000614#endif /* !_WIN32 */
615
Anthony Liguori96c63842013-03-05 23:21:18 +0530616typedef struct IOWatchPoll
617{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200618 GSource parent;
619
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200620 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530621 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530622
623 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200624 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530625 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530626} IOWatchPoll;
627
Anthony Liguori96c63842013-03-05 23:21:18 +0530628static IOWatchPoll *io_watch_poll_from_source(GSource *source)
629{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200630 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530631}
632
633static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
634{
635 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200636 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200637 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200638 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530639 return FALSE;
640 }
641
Paolo Bonzinid185c092013-04-05 17:59:33 +0200642 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200643 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
644 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200645 g_source_attach(iwp->src, NULL);
646 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200647 g_source_destroy(iwp->src);
648 g_source_unref(iwp->src);
649 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200650 }
651 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530652}
653
654static gboolean io_watch_poll_check(GSource *source)
655{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200656 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530657}
658
659static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
660 gpointer user_data)
661{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200662 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530663}
664
665static void io_watch_poll_finalize(GSource *source)
666{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200667 /* Due to a glib bug, removing the last reference to a source
668 * inside a finalize callback causes recursive locking (and a
669 * deadlock). This is not a problem inside other callbacks,
670 * including dispatch callbacks, so we call io_remove_watch_poll
671 * to remove this source. At this point, iwp->src must
672 * be NULL, or we would leak it.
673 *
674 * This would be solved much more elegantly by child sources,
675 * but we support older glib versions that do not have them.
676 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530677 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200678 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530679}
680
681static GSourceFuncs io_watch_poll_funcs = {
682 .prepare = io_watch_poll_prepare,
683 .check = io_watch_poll_check,
684 .dispatch = io_watch_poll_dispatch,
685 .finalize = io_watch_poll_finalize,
686};
687
688/* Can only be used for read */
689static guint io_add_watch_poll(GIOChannel *channel,
690 IOCanReadHandler *fd_can_read,
691 GIOFunc fd_read,
692 gpointer user_data)
693{
694 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200695 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530696
Paolo Bonzinid185c092013-04-05 17:59:33 +0200697 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530698 iwp->fd_can_read = fd_can_read;
699 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200700 iwp->channel = channel;
701 iwp->fd_read = (GSourceFunc) fd_read;
702 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530703
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200704 tag = g_source_attach(&iwp->parent, NULL);
705 g_source_unref(&iwp->parent);
706 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530707}
708
Paolo Bonzini2b316772013-04-19 17:32:09 +0200709static void io_remove_watch_poll(guint tag)
710{
711 GSource *source;
712 IOWatchPoll *iwp;
713
714 g_return_if_fail (tag > 0);
715
716 source = g_main_context_find_source_by_id(NULL, tag);
717 g_return_if_fail (source != NULL);
718
719 iwp = io_watch_poll_from_source(source);
720 if (iwp->src) {
721 g_source_destroy(iwp->src);
722 g_source_unref(iwp->src);
723 iwp->src = NULL;
724 }
725 g_source_destroy(&iwp->parent);
726}
727
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000728#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530729static GIOChannel *io_channel_from_fd(int fd)
730{
731 GIOChannel *chan;
732
733 if (fd == -1) {
734 return NULL;
735 }
736
737 chan = g_io_channel_unix_new(fd);
738
739 g_io_channel_set_encoding(chan, NULL, NULL);
740 g_io_channel_set_buffered(chan, FALSE);
741
742 return chan;
743}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000744#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530745
Anthony Liguori76a96442013-03-05 23:21:21 +0530746static GIOChannel *io_channel_from_socket(int fd)
747{
748 GIOChannel *chan;
749
750 if (fd == -1) {
751 return NULL;
752 }
753
754#ifdef _WIN32
755 chan = g_io_channel_win32_new_socket(fd);
756#else
757 chan = g_io_channel_unix_new(fd);
758#endif
759
760 g_io_channel_set_encoding(chan, NULL, NULL);
761 g_io_channel_set_buffered(chan, FALSE);
762
763 return chan;
764}
765
Anthony Liguori684a0962013-03-29 11:39:50 -0500766static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530767{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200768 size_t offset = 0;
769 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530770
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200771 while (offset < len && status == G_IO_STATUS_NORMAL) {
772 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500773
774 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530775 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500776 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530777 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500778
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200779 if (offset > 0) {
780 return offset;
781 }
782 switch (status) {
783 case G_IO_STATUS_NORMAL:
784 g_assert(len == 0);
785 return 0;
786 case G_IO_STATUS_AGAIN:
787 errno = EAGAIN;
788 return -1;
789 default:
790 break;
791 }
792 errno = EINVAL;
793 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530794}
Anthony Liguori96c63842013-03-05 23:21:18 +0530795
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000796#ifndef _WIN32
797
Anthony Liguoria29753f2013-03-05 23:21:19 +0530798typedef struct FDCharDriver {
799 CharDriverState *chr;
800 GIOChannel *fd_in, *fd_out;
801 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000802 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530803 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000804} FDCharDriver;
805
aliguori6f97dba2008-10-31 18:49:55 +0000806static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
807{
808 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530809
Anthony Liguori684a0962013-03-29 11:39:50 -0500810 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530811}
812
813static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
814{
815 CharDriverState *chr = opaque;
816 FDCharDriver *s = chr->opaque;
817 int len;
818 uint8_t buf[READ_BUF_LEN];
819 GIOStatus status;
820 gsize bytes_read;
821
822 len = sizeof(buf);
823 if (len > s->max_size) {
824 len = s->max_size;
825 }
826 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200827 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530828 }
829
830 status = g_io_channel_read_chars(chan, (gchar *)buf,
831 len, &bytes_read, NULL);
832 if (status == G_IO_STATUS_EOF) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200833 if (s->fd_in_tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +0200834 io_remove_watch_poll(s->fd_in_tag);
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200835 s->fd_in_tag = 0;
836 }
Anthony Liguoria29753f2013-03-05 23:21:19 +0530837 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
838 return FALSE;
839 }
840 if (status == G_IO_STATUS_NORMAL) {
841 qemu_chr_be_write(chr, buf, bytes_read);
842 }
843
844 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000845}
846
847static int fd_chr_read_poll(void *opaque)
848{
849 CharDriverState *chr = opaque;
850 FDCharDriver *s = chr->opaque;
851
Anthony Liguori909cda12011-08-15 11:17:31 -0500852 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000853 return s->max_size;
854}
855
Anthony Liguori23673ca2013-03-05 23:21:23 +0530856static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
857{
858 FDCharDriver *s = chr->opaque;
859 return g_io_create_watch(s->fd_out, cond);
860}
861
aliguori6f97dba2008-10-31 18:49:55 +0000862static void fd_chr_update_read_handler(CharDriverState *chr)
863{
864 FDCharDriver *s = chr->opaque;
865
Anthony Liguoria29753f2013-03-05 23:21:19 +0530866 if (s->fd_in_tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +0200867 io_remove_watch_poll(s->fd_in_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +0200868 s->fd_in_tag = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530869 }
870
871 if (s->fd_in) {
872 s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000873 }
874}
875
876static void fd_chr_close(struct CharDriverState *chr)
877{
878 FDCharDriver *s = chr->opaque;
879
Anthony Liguoria29753f2013-03-05 23:21:19 +0530880 if (s->fd_in_tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +0200881 io_remove_watch_poll(s->fd_in_tag);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530882 s->fd_in_tag = 0;
883 }
884
885 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;
1015 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001016 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001017 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301018 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001019} PtyCharDriver;
1020
1021static void pty_chr_update_read_handler(CharDriverState *chr);
1022static void pty_chr_state(CharDriverState *chr, int connected);
1023
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301024static gboolean pty_chr_timer(gpointer opaque)
1025{
1026 struct CharDriverState *chr = opaque;
1027 PtyCharDriver *s = chr->opaque;
1028
1029 if (s->connected) {
1030 goto out;
1031 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301032
1033 /* Next poll ... */
1034 pty_chr_update_read_handler(chr);
1035
1036out:
Hans de Goede79f20072013-04-25 13:53:02 +02001037 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301038 return FALSE;
1039}
1040
1041static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1042{
1043 PtyCharDriver *s = chr->opaque;
1044
1045 if (s->timer_tag) {
1046 g_source_remove(s->timer_tag);
1047 s->timer_tag = 0;
1048 }
1049
1050 if (ms == 1000) {
1051 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1052 } else {
1053 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1054 }
1055}
1056
aliguori6f97dba2008-10-31 18:49:55 +00001057static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1058{
1059 PtyCharDriver *s = chr->opaque;
1060
1061 if (!s->connected) {
1062 /* guest sends data, check for (re-)connect */
1063 pty_chr_update_read_handler(chr);
1064 return 0;
1065 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001066 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001067}
1068
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301069static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1070{
1071 PtyCharDriver *s = chr->opaque;
1072 return g_io_create_watch(s->fd, cond);
1073}
1074
aliguori6f97dba2008-10-31 18:49:55 +00001075static int pty_chr_read_poll(void *opaque)
1076{
1077 CharDriverState *chr = opaque;
1078 PtyCharDriver *s = chr->opaque;
1079
Anthony Liguori909cda12011-08-15 11:17:31 -05001080 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001081 return s->read_bytes;
1082}
1083
Anthony Liguori093d3a22013-03-05 23:21:20 +05301084static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001085{
1086 CharDriverState *chr = opaque;
1087 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301088 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301089 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301090 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001091
1092 len = sizeof(buf);
1093 if (len > s->read_bytes)
1094 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001095 if (len == 0) {
1096 return TRUE;
1097 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301098 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1099 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001100 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301101 return FALSE;
1102 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001103 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001104 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001105 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301106 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001107}
1108
1109static void pty_chr_update_read_handler(CharDriverState *chr)
1110{
1111 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001112 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001113
Paolo Bonzini85a67692013-04-19 17:32:07 +02001114 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1115 pfd.events = G_IO_OUT;
1116 pfd.revents = 0;
1117 g_poll(&pfd, 1, 0);
1118 if (pfd.revents & G_IO_HUP) {
1119 pty_chr_state(chr, 0);
1120 } else {
1121 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301122 }
aliguori6f97dba2008-10-31 18:49:55 +00001123}
1124
1125static void pty_chr_state(CharDriverState *chr, int connected)
1126{
1127 PtyCharDriver *s = chr->opaque;
1128
1129 if (!connected) {
Paolo Bonzini910b6362013-04-19 17:32:06 +02001130 if (s->fd_tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02001131 io_remove_watch_poll(s->fd_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001132 s->fd_tag = 0;
1133 }
aliguori6f97dba2008-10-31 18:49:55 +00001134 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001135 /* (re-)connect poll interval for idle guests: once per second.
1136 * We check more frequently in case the guests sends data to
1137 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301138 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001139 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001140 if (s->timer_tag) {
1141 g_source_remove(s->timer_tag);
1142 s->timer_tag = 0;
1143 }
1144 if (!s->connected) {
Hans de Goedefee204f2013-03-26 11:07:54 +01001145 qemu_chr_be_generic_open(chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001146 s->connected = 1;
1147 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
1148 }
aliguori6f97dba2008-10-31 18:49:55 +00001149 }
1150}
1151
aliguori6f97dba2008-10-31 18:49:55 +00001152
1153static void pty_chr_close(struct CharDriverState *chr)
1154{
1155 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301156 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001157
Anthony Liguori093d3a22013-03-05 23:21:20 +05301158 if (s->fd_tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02001159 io_remove_watch_poll(s->fd_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001160 s->fd_tag = 0;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301161 }
1162 fd = g_io_channel_unix_get_fd(s->fd);
1163 g_io_channel_unref(s->fd);
1164 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301165 if (s->timer_tag) {
1166 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001167 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301168 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001169 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001170 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001171}
1172
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001173static CharDriverState *qemu_chr_open_pty(const char *id,
1174 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001175{
1176 CharDriverState *chr;
1177 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001178 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001179 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001180
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001181 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1182 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001183 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001184 }
1185
aliguori6f97dba2008-10-31 18:49:55 +00001186 close(slave_fd);
1187
Markus Armbrustera4e26042011-11-11 10:40:05 +01001188 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001189
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001190 chr->filename = g_strdup_printf("pty:%s", pty_name);
1191 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001192 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001193
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001194 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001195 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001196
1197 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001198 chr->opaque = s;
1199 chr->chr_write = pty_chr_write;
1200 chr->chr_update_read_handler = pty_chr_update_read_handler;
1201 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301202 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001203 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001204
Anthony Liguori093d3a22013-03-05 23:21:20 +05301205 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301206 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001207
Markus Armbruster1f514702012-02-07 15:09:08 +01001208 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001209}
1210
1211static void tty_serial_init(int fd, int speed,
1212 int parity, int data_bits, int stop_bits)
1213{
1214 struct termios tty;
1215 speed_t spd;
1216
1217#if 0
1218 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1219 speed, parity, data_bits, stop_bits);
1220#endif
1221 tcgetattr (fd, &tty);
1222
Stefan Weil45eea132009-10-26 16:10:10 +01001223#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1224 speed = speed * 10 / 11;
1225 do {
1226 check_speed(50);
1227 check_speed(75);
1228 check_speed(110);
1229 check_speed(134);
1230 check_speed(150);
1231 check_speed(200);
1232 check_speed(300);
1233 check_speed(600);
1234 check_speed(1200);
1235 check_speed(1800);
1236 check_speed(2400);
1237 check_speed(4800);
1238 check_speed(9600);
1239 check_speed(19200);
1240 check_speed(38400);
1241 /* Non-Posix values follow. They may be unsupported on some systems. */
1242 check_speed(57600);
1243 check_speed(115200);
1244#ifdef B230400
1245 check_speed(230400);
1246#endif
1247#ifdef B460800
1248 check_speed(460800);
1249#endif
1250#ifdef B500000
1251 check_speed(500000);
1252#endif
1253#ifdef B576000
1254 check_speed(576000);
1255#endif
1256#ifdef B921600
1257 check_speed(921600);
1258#endif
1259#ifdef B1000000
1260 check_speed(1000000);
1261#endif
1262#ifdef B1152000
1263 check_speed(1152000);
1264#endif
1265#ifdef B1500000
1266 check_speed(1500000);
1267#endif
1268#ifdef B2000000
1269 check_speed(2000000);
1270#endif
1271#ifdef B2500000
1272 check_speed(2500000);
1273#endif
1274#ifdef B3000000
1275 check_speed(3000000);
1276#endif
1277#ifdef B3500000
1278 check_speed(3500000);
1279#endif
1280#ifdef B4000000
1281 check_speed(4000000);
1282#endif
aliguori6f97dba2008-10-31 18:49:55 +00001283 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001284 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001285
1286 cfsetispeed(&tty, spd);
1287 cfsetospeed(&tty, spd);
1288
1289 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1290 |INLCR|IGNCR|ICRNL|IXON);
1291 tty.c_oflag |= OPOST;
1292 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1293 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1294 switch(data_bits) {
1295 default:
1296 case 8:
1297 tty.c_cflag |= CS8;
1298 break;
1299 case 7:
1300 tty.c_cflag |= CS7;
1301 break;
1302 case 6:
1303 tty.c_cflag |= CS6;
1304 break;
1305 case 5:
1306 tty.c_cflag |= CS5;
1307 break;
1308 }
1309 switch(parity) {
1310 default:
1311 case 'N':
1312 break;
1313 case 'E':
1314 tty.c_cflag |= PARENB;
1315 break;
1316 case 'O':
1317 tty.c_cflag |= PARENB | PARODD;
1318 break;
1319 }
1320 if (stop_bits == 2)
1321 tty.c_cflag |= CSTOPB;
1322
1323 tcsetattr (fd, TCSANOW, &tty);
1324}
1325
1326static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1327{
1328 FDCharDriver *s = chr->opaque;
1329
1330 switch(cmd) {
1331 case CHR_IOCTL_SERIAL_SET_PARAMS:
1332 {
1333 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301334 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1335 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001336 ssp->data_bits, ssp->stop_bits);
1337 }
1338 break;
1339 case CHR_IOCTL_SERIAL_SET_BREAK:
1340 {
1341 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301342 if (enable) {
1343 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1344 }
aliguori6f97dba2008-10-31 18:49:55 +00001345 }
1346 break;
1347 case CHR_IOCTL_SERIAL_GET_TIOCM:
1348 {
1349 int sarg = 0;
1350 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301351 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001352 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001353 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001354 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001355 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001356 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001357 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001358 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001359 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001360 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001361 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001362 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001363 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001364 *targ |= CHR_TIOCM_RTS;
1365 }
1366 break;
1367 case CHR_IOCTL_SERIAL_SET_TIOCM:
1368 {
1369 int sarg = *(int *)arg;
1370 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301371 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001372 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1373 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1374 if (sarg & CHR_TIOCM_CTS)
1375 targ |= TIOCM_CTS;
1376 if (sarg & CHR_TIOCM_CAR)
1377 targ |= TIOCM_CAR;
1378 if (sarg & CHR_TIOCM_DSR)
1379 targ |= TIOCM_DSR;
1380 if (sarg & CHR_TIOCM_RI)
1381 targ |= TIOCM_RI;
1382 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001383 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001384 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001385 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301386 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001387 }
1388 break;
1389 default:
1390 return -ENOTSUP;
1391 }
1392 return 0;
1393}
1394
David Ahern4266a132010-02-10 18:27:17 -07001395static void qemu_chr_close_tty(CharDriverState *chr)
1396{
1397 FDCharDriver *s = chr->opaque;
1398 int fd = -1;
1399
1400 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301401 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001402 }
1403
1404 fd_chr_close(chr);
1405
1406 if (fd >= 0) {
1407 close(fd);
1408 }
1409}
1410
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001411static CharDriverState *qemu_chr_open_tty_fd(int fd)
1412{
1413 CharDriverState *chr;
1414
1415 tty_serial_init(fd, 115200, 'N', 8, 1);
1416 chr = qemu_chr_open_fd(fd, fd);
1417 chr->chr_ioctl = tty_serial_ioctl;
1418 chr->chr_close = qemu_chr_close_tty;
1419 return chr;
1420}
aliguori6f97dba2008-10-31 18:49:55 +00001421#endif /* __linux__ || __sun__ */
1422
1423#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001424
1425#define HAVE_CHARDEV_PARPORT 1
1426
aliguori6f97dba2008-10-31 18:49:55 +00001427typedef struct {
1428 int fd;
1429 int mode;
1430} ParallelCharDriver;
1431
1432static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1433{
1434 if (s->mode != mode) {
1435 int m = mode;
1436 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1437 return 0;
1438 s->mode = mode;
1439 }
1440 return 1;
1441}
1442
1443static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1444{
1445 ParallelCharDriver *drv = chr->opaque;
1446 int fd = drv->fd;
1447 uint8_t b;
1448
1449 switch(cmd) {
1450 case CHR_IOCTL_PP_READ_DATA:
1451 if (ioctl(fd, PPRDATA, &b) < 0)
1452 return -ENOTSUP;
1453 *(uint8_t *)arg = b;
1454 break;
1455 case CHR_IOCTL_PP_WRITE_DATA:
1456 b = *(uint8_t *)arg;
1457 if (ioctl(fd, PPWDATA, &b) < 0)
1458 return -ENOTSUP;
1459 break;
1460 case CHR_IOCTL_PP_READ_CONTROL:
1461 if (ioctl(fd, PPRCONTROL, &b) < 0)
1462 return -ENOTSUP;
1463 /* Linux gives only the lowest bits, and no way to know data
1464 direction! For better compatibility set the fixed upper
1465 bits. */
1466 *(uint8_t *)arg = b | 0xc0;
1467 break;
1468 case CHR_IOCTL_PP_WRITE_CONTROL:
1469 b = *(uint8_t *)arg;
1470 if (ioctl(fd, PPWCONTROL, &b) < 0)
1471 return -ENOTSUP;
1472 break;
1473 case CHR_IOCTL_PP_READ_STATUS:
1474 if (ioctl(fd, PPRSTATUS, &b) < 0)
1475 return -ENOTSUP;
1476 *(uint8_t *)arg = b;
1477 break;
1478 case CHR_IOCTL_PP_DATA_DIR:
1479 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1480 return -ENOTSUP;
1481 break;
1482 case CHR_IOCTL_PP_EPP_READ_ADDR:
1483 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1484 struct ParallelIOArg *parg = arg;
1485 int n = read(fd, parg->buffer, parg->count);
1486 if (n != parg->count) {
1487 return -EIO;
1488 }
1489 }
1490 break;
1491 case CHR_IOCTL_PP_EPP_READ:
1492 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1493 struct ParallelIOArg *parg = arg;
1494 int n = read(fd, parg->buffer, parg->count);
1495 if (n != parg->count) {
1496 return -EIO;
1497 }
1498 }
1499 break;
1500 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1501 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1502 struct ParallelIOArg *parg = arg;
1503 int n = write(fd, parg->buffer, parg->count);
1504 if (n != parg->count) {
1505 return -EIO;
1506 }
1507 }
1508 break;
1509 case CHR_IOCTL_PP_EPP_WRITE:
1510 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1511 struct ParallelIOArg *parg = arg;
1512 int n = write(fd, parg->buffer, parg->count);
1513 if (n != parg->count) {
1514 return -EIO;
1515 }
1516 }
1517 break;
1518 default:
1519 return -ENOTSUP;
1520 }
1521 return 0;
1522}
1523
1524static void pp_close(CharDriverState *chr)
1525{
1526 ParallelCharDriver *drv = chr->opaque;
1527 int fd = drv->fd;
1528
1529 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1530 ioctl(fd, PPRELEASE);
1531 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001532 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001533 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001534}
1535
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001536static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001537{
1538 CharDriverState *chr;
1539 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001540
1541 if (ioctl(fd, PPCLAIM) < 0) {
1542 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001543 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001544 }
1545
Anthony Liguori7267c092011-08-20 22:09:37 -05001546 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001547 drv->fd = fd;
1548 drv->mode = IEEE1284_MODE_COMPAT;
1549
Anthony Liguori7267c092011-08-20 22:09:37 -05001550 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001551 chr->chr_write = null_chr_write;
1552 chr->chr_ioctl = pp_ioctl;
1553 chr->chr_close = pp_close;
1554 chr->opaque = drv;
1555
Markus Armbruster1f514702012-02-07 15:09:08 +01001556 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001557}
1558#endif /* __linux__ */
1559
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001560#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001561
1562#define HAVE_CHARDEV_PARPORT 1
1563
blueswir16972f932008-11-22 20:49:12 +00001564static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1565{
Stefan Weile0efb992011-02-23 19:09:16 +01001566 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001567 uint8_t b;
1568
1569 switch(cmd) {
1570 case CHR_IOCTL_PP_READ_DATA:
1571 if (ioctl(fd, PPIGDATA, &b) < 0)
1572 return -ENOTSUP;
1573 *(uint8_t *)arg = b;
1574 break;
1575 case CHR_IOCTL_PP_WRITE_DATA:
1576 b = *(uint8_t *)arg;
1577 if (ioctl(fd, PPISDATA, &b) < 0)
1578 return -ENOTSUP;
1579 break;
1580 case CHR_IOCTL_PP_READ_CONTROL:
1581 if (ioctl(fd, PPIGCTRL, &b) < 0)
1582 return -ENOTSUP;
1583 *(uint8_t *)arg = b;
1584 break;
1585 case CHR_IOCTL_PP_WRITE_CONTROL:
1586 b = *(uint8_t *)arg;
1587 if (ioctl(fd, PPISCTRL, &b) < 0)
1588 return -ENOTSUP;
1589 break;
1590 case CHR_IOCTL_PP_READ_STATUS:
1591 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1592 return -ENOTSUP;
1593 *(uint8_t *)arg = b;
1594 break;
1595 default:
1596 return -ENOTSUP;
1597 }
1598 return 0;
1599}
1600
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001601static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001602{
1603 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001604
Anthony Liguori7267c092011-08-20 22:09:37 -05001605 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001606 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001607 chr->chr_write = null_chr_write;
1608 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001609 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001610 return chr;
blueswir16972f932008-11-22 20:49:12 +00001611}
1612#endif
1613
aliguori6f97dba2008-10-31 18:49:55 +00001614#else /* _WIN32 */
1615
1616typedef struct {
1617 int max_size;
1618 HANDLE hcom, hrecv, hsend;
1619 OVERLAPPED orecv, osend;
1620 BOOL fpipe;
1621 DWORD len;
1622} WinCharState;
1623
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001624typedef struct {
1625 HANDLE hStdIn;
1626 HANDLE hInputReadyEvent;
1627 HANDLE hInputDoneEvent;
1628 HANDLE hInputThread;
1629 uint8_t win_stdio_buf;
1630} WinStdioCharState;
1631
aliguori6f97dba2008-10-31 18:49:55 +00001632#define NSENDBUF 2048
1633#define NRECVBUF 2048
1634#define MAXCONNECT 1
1635#define NTIMEOUT 5000
1636
1637static int win_chr_poll(void *opaque);
1638static int win_chr_pipe_poll(void *opaque);
1639
1640static void win_chr_close(CharDriverState *chr)
1641{
1642 WinCharState *s = chr->opaque;
1643
1644 if (s->hsend) {
1645 CloseHandle(s->hsend);
1646 s->hsend = NULL;
1647 }
1648 if (s->hrecv) {
1649 CloseHandle(s->hrecv);
1650 s->hrecv = NULL;
1651 }
1652 if (s->hcom) {
1653 CloseHandle(s->hcom);
1654 s->hcom = NULL;
1655 }
1656 if (s->fpipe)
1657 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1658 else
1659 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301660
Hans de Goedea425d232011-11-19 10:22:43 +01001661 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001662}
1663
1664static int win_chr_init(CharDriverState *chr, const char *filename)
1665{
1666 WinCharState *s = chr->opaque;
1667 COMMCONFIG comcfg;
1668 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1669 COMSTAT comstat;
1670 DWORD size;
1671 DWORD err;
1672
1673 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1674 if (!s->hsend) {
1675 fprintf(stderr, "Failed CreateEvent\n");
1676 goto fail;
1677 }
1678 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1679 if (!s->hrecv) {
1680 fprintf(stderr, "Failed CreateEvent\n");
1681 goto fail;
1682 }
1683
1684 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1685 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1686 if (s->hcom == INVALID_HANDLE_VALUE) {
1687 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1688 s->hcom = NULL;
1689 goto fail;
1690 }
1691
1692 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1693 fprintf(stderr, "Failed SetupComm\n");
1694 goto fail;
1695 }
1696
1697 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1698 size = sizeof(COMMCONFIG);
1699 GetDefaultCommConfig(filename, &comcfg, &size);
1700 comcfg.dcb.DCBlength = sizeof(DCB);
1701 CommConfigDialog(filename, NULL, &comcfg);
1702
1703 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1704 fprintf(stderr, "Failed SetCommState\n");
1705 goto fail;
1706 }
1707
1708 if (!SetCommMask(s->hcom, EV_ERR)) {
1709 fprintf(stderr, "Failed SetCommMask\n");
1710 goto fail;
1711 }
1712
1713 cto.ReadIntervalTimeout = MAXDWORD;
1714 if (!SetCommTimeouts(s->hcom, &cto)) {
1715 fprintf(stderr, "Failed SetCommTimeouts\n");
1716 goto fail;
1717 }
1718
1719 if (!ClearCommError(s->hcom, &err, &comstat)) {
1720 fprintf(stderr, "Failed ClearCommError\n");
1721 goto fail;
1722 }
1723 qemu_add_polling_cb(win_chr_poll, chr);
1724 return 0;
1725
1726 fail:
1727 win_chr_close(chr);
1728 return -1;
1729}
1730
1731static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1732{
1733 WinCharState *s = chr->opaque;
1734 DWORD len, ret, size, err;
1735
1736 len = len1;
1737 ZeroMemory(&s->osend, sizeof(s->osend));
1738 s->osend.hEvent = s->hsend;
1739 while (len > 0) {
1740 if (s->hsend)
1741 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1742 else
1743 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1744 if (!ret) {
1745 err = GetLastError();
1746 if (err == ERROR_IO_PENDING) {
1747 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1748 if (ret) {
1749 buf += size;
1750 len -= size;
1751 } else {
1752 break;
1753 }
1754 } else {
1755 break;
1756 }
1757 } else {
1758 buf += size;
1759 len -= size;
1760 }
1761 }
1762 return len1 - len;
1763}
1764
1765static int win_chr_read_poll(CharDriverState *chr)
1766{
1767 WinCharState *s = chr->opaque;
1768
Anthony Liguori909cda12011-08-15 11:17:31 -05001769 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001770 return s->max_size;
1771}
1772
1773static void win_chr_readfile(CharDriverState *chr)
1774{
1775 WinCharState *s = chr->opaque;
1776 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301777 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001778 DWORD size;
1779
1780 ZeroMemory(&s->orecv, sizeof(s->orecv));
1781 s->orecv.hEvent = s->hrecv;
1782 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1783 if (!ret) {
1784 err = GetLastError();
1785 if (err == ERROR_IO_PENDING) {
1786 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1787 }
1788 }
1789
1790 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001791 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001792 }
1793}
1794
1795static void win_chr_read(CharDriverState *chr)
1796{
1797 WinCharState *s = chr->opaque;
1798
1799 if (s->len > s->max_size)
1800 s->len = s->max_size;
1801 if (s->len == 0)
1802 return;
1803
1804 win_chr_readfile(chr);
1805}
1806
1807static int win_chr_poll(void *opaque)
1808{
1809 CharDriverState *chr = opaque;
1810 WinCharState *s = chr->opaque;
1811 COMSTAT status;
1812 DWORD comerr;
1813
1814 ClearCommError(s->hcom, &comerr, &status);
1815 if (status.cbInQue > 0) {
1816 s->len = status.cbInQue;
1817 win_chr_read_poll(chr);
1818 win_chr_read(chr);
1819 return 1;
1820 }
1821 return 0;
1822}
1823
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001824static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001825{
1826 CharDriverState *chr;
1827 WinCharState *s;
1828
Anthony Liguori7267c092011-08-20 22:09:37 -05001829 chr = g_malloc0(sizeof(CharDriverState));
1830 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001831 chr->opaque = s;
1832 chr->chr_write = win_chr_write;
1833 chr->chr_close = win_chr_close;
1834
1835 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001836 g_free(s);
1837 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001838 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001839 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001840 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001841}
1842
1843static int win_chr_pipe_poll(void *opaque)
1844{
1845 CharDriverState *chr = opaque;
1846 WinCharState *s = chr->opaque;
1847 DWORD size;
1848
1849 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1850 if (size > 0) {
1851 s->len = size;
1852 win_chr_read_poll(chr);
1853 win_chr_read(chr);
1854 return 1;
1855 }
1856 return 0;
1857}
1858
1859static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1860{
1861 WinCharState *s = chr->opaque;
1862 OVERLAPPED ov;
1863 int ret;
1864 DWORD size;
1865 char openname[256];
1866
1867 s->fpipe = TRUE;
1868
1869 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1870 if (!s->hsend) {
1871 fprintf(stderr, "Failed CreateEvent\n");
1872 goto fail;
1873 }
1874 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1875 if (!s->hrecv) {
1876 fprintf(stderr, "Failed CreateEvent\n");
1877 goto fail;
1878 }
1879
1880 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1881 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1882 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1883 PIPE_WAIT,
1884 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1885 if (s->hcom == INVALID_HANDLE_VALUE) {
1886 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1887 s->hcom = NULL;
1888 goto fail;
1889 }
1890
1891 ZeroMemory(&ov, sizeof(ov));
1892 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1893 ret = ConnectNamedPipe(s->hcom, &ov);
1894 if (ret) {
1895 fprintf(stderr, "Failed ConnectNamedPipe\n");
1896 goto fail;
1897 }
1898
1899 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1900 if (!ret) {
1901 fprintf(stderr, "Failed GetOverlappedResult\n");
1902 if (ov.hEvent) {
1903 CloseHandle(ov.hEvent);
1904 ov.hEvent = NULL;
1905 }
1906 goto fail;
1907 }
1908
1909 if (ov.hEvent) {
1910 CloseHandle(ov.hEvent);
1911 ov.hEvent = NULL;
1912 }
1913 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1914 return 0;
1915
1916 fail:
1917 win_chr_close(chr);
1918 return -1;
1919}
1920
1921
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001922static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001923{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001924 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001925 CharDriverState *chr;
1926 WinCharState *s;
1927
Anthony Liguori7267c092011-08-20 22:09:37 -05001928 chr = g_malloc0(sizeof(CharDriverState));
1929 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001930 chr->opaque = s;
1931 chr->chr_write = win_chr_write;
1932 chr->chr_close = win_chr_close;
1933
1934 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001935 g_free(s);
1936 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001937 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001938 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001939 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001940}
1941
Markus Armbruster1f514702012-02-07 15:09:08 +01001942static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001943{
1944 CharDriverState *chr;
1945 WinCharState *s;
1946
Anthony Liguori7267c092011-08-20 22:09:37 -05001947 chr = g_malloc0(sizeof(CharDriverState));
1948 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001949 s->hcom = fd_out;
1950 chr->opaque = s;
1951 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001952 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001953}
1954
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001955static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001956{
Markus Armbruster1f514702012-02-07 15:09:08 +01001957 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001958}
1959
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001960static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1961{
1962 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1963 DWORD dwSize;
1964 int len1;
1965
1966 len1 = len;
1967
1968 while (len1 > 0) {
1969 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1970 break;
1971 }
1972 buf += dwSize;
1973 len1 -= dwSize;
1974 }
1975
1976 return len - len1;
1977}
1978
1979static void win_stdio_wait_func(void *opaque)
1980{
1981 CharDriverState *chr = opaque;
1982 WinStdioCharState *stdio = chr->opaque;
1983 INPUT_RECORD buf[4];
1984 int ret;
1985 DWORD dwSize;
1986 int i;
1987
1988 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1989 &dwSize);
1990
1991 if (!ret) {
1992 /* Avoid error storm */
1993 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1994 return;
1995 }
1996
1997 for (i = 0; i < dwSize; i++) {
1998 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1999
2000 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2001 int j;
2002 if (kev->uChar.AsciiChar != 0) {
2003 for (j = 0; j < kev->wRepeatCount; j++) {
2004 if (qemu_chr_be_can_write(chr)) {
2005 uint8_t c = kev->uChar.AsciiChar;
2006 qemu_chr_be_write(chr, &c, 1);
2007 }
2008 }
2009 }
2010 }
2011 }
2012}
2013
2014static DWORD WINAPI win_stdio_thread(LPVOID param)
2015{
2016 CharDriverState *chr = param;
2017 WinStdioCharState *stdio = chr->opaque;
2018 int ret;
2019 DWORD dwSize;
2020
2021 while (1) {
2022
2023 /* Wait for one byte */
2024 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2025
2026 /* Exit in case of error, continue if nothing read */
2027 if (!ret) {
2028 break;
2029 }
2030 if (!dwSize) {
2031 continue;
2032 }
2033
2034 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2035 if (stdio->win_stdio_buf == '\r') {
2036 continue;
2037 }
2038
2039 /* Signal the main thread and wait until the byte was eaten */
2040 if (!SetEvent(stdio->hInputReadyEvent)) {
2041 break;
2042 }
2043 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2044 != WAIT_OBJECT_0) {
2045 break;
2046 }
2047 }
2048
2049 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2050 return 0;
2051}
2052
2053static void win_stdio_thread_wait_func(void *opaque)
2054{
2055 CharDriverState *chr = opaque;
2056 WinStdioCharState *stdio = chr->opaque;
2057
2058 if (qemu_chr_be_can_write(chr)) {
2059 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2060 }
2061
2062 SetEvent(stdio->hInputDoneEvent);
2063}
2064
2065static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2066{
2067 WinStdioCharState *stdio = chr->opaque;
2068 DWORD dwMode = 0;
2069
2070 GetConsoleMode(stdio->hStdIn, &dwMode);
2071
2072 if (echo) {
2073 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2074 } else {
2075 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2076 }
2077}
2078
2079static void win_stdio_close(CharDriverState *chr)
2080{
2081 WinStdioCharState *stdio = chr->opaque;
2082
2083 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2084 CloseHandle(stdio->hInputReadyEvent);
2085 }
2086 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2087 CloseHandle(stdio->hInputDoneEvent);
2088 }
2089 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2090 TerminateThread(stdio->hInputThread, 0);
2091 }
2092
2093 g_free(chr->opaque);
2094 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002095}
2096
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002097static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002098{
2099 CharDriverState *chr;
2100 WinStdioCharState *stdio;
2101 DWORD dwMode;
2102 int is_console = 0;
2103
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002104 chr = g_malloc0(sizeof(CharDriverState));
2105 stdio = g_malloc0(sizeof(WinStdioCharState));
2106
2107 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2108 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2109 fprintf(stderr, "cannot open stdio: invalid handle\n");
2110 exit(1);
2111 }
2112
2113 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2114
2115 chr->opaque = stdio;
2116 chr->chr_write = win_stdio_write;
2117 chr->chr_close = win_stdio_close;
2118
Anthony Liguoried7a1542013-03-05 23:21:17 +05302119 if (is_console) {
2120 if (qemu_add_wait_object(stdio->hStdIn,
2121 win_stdio_wait_func, chr)) {
2122 fprintf(stderr, "qemu_add_wait_object: failed\n");
2123 }
2124 } else {
2125 DWORD dwId;
2126
2127 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2128 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2129 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2130 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002131
Anthony Liguoried7a1542013-03-05 23:21:17 +05302132 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2133 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2134 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2135 fprintf(stderr, "cannot create stdio thread or event\n");
2136 exit(1);
2137 }
2138 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2139 win_stdio_thread_wait_func, chr)) {
2140 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002141 }
2142 }
2143
2144 dwMode |= ENABLE_LINE_INPUT;
2145
Anthony Liguoried7a1542013-03-05 23:21:17 +05302146 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002147 /* set the terminal in raw mode */
2148 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2149 dwMode |= ENABLE_PROCESSED_INPUT;
2150 }
2151
2152 SetConsoleMode(stdio->hStdIn, dwMode);
2153
2154 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2155 qemu_chr_fe_set_echo(chr, false);
2156
Markus Armbruster1f514702012-02-07 15:09:08 +01002157 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002158}
aliguori6f97dba2008-10-31 18:49:55 +00002159#endif /* !_WIN32 */
2160
Anthony Liguori5ab82112013-03-05 23:21:31 +05302161
aliguori6f97dba2008-10-31 18:49:55 +00002162/***********************************************************/
2163/* UDP Net console */
2164
2165typedef struct {
2166 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302167 GIOChannel *chan;
2168 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302169 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002170 int bufcnt;
2171 int bufptr;
2172 int max_size;
2173} NetCharDriver;
2174
2175static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2176{
2177 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302178 gsize bytes_written;
2179 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002180
Anthony Liguori76a96442013-03-05 23:21:21 +05302181 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2182 if (status == G_IO_STATUS_EOF) {
2183 return 0;
2184 } else if (status != G_IO_STATUS_NORMAL) {
2185 return -1;
2186 }
2187
2188 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002189}
2190
2191static int udp_chr_read_poll(void *opaque)
2192{
2193 CharDriverState *chr = opaque;
2194 NetCharDriver *s = chr->opaque;
2195
Anthony Liguori909cda12011-08-15 11:17:31 -05002196 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002197
2198 /* If there were any stray characters in the queue process them
2199 * first
2200 */
2201 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002202 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002203 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002204 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002205 }
2206 return s->max_size;
2207}
2208
Anthony Liguori76a96442013-03-05 23:21:21 +05302209static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002210{
2211 CharDriverState *chr = opaque;
2212 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302213 gsize bytes_read = 0;
2214 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002215
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002216 if (s->max_size == 0) {
2217 return TRUE;
2218 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302219 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2220 &bytes_read, NULL);
2221 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002222 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302223 if (status != G_IO_STATUS_NORMAL) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002224 if (s->tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02002225 io_remove_watch_poll(s->tag);
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002226 s->tag = 0;
2227 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302228 return FALSE;
2229 }
aliguori6f97dba2008-10-31 18:49:55 +00002230
2231 s->bufptr = 0;
2232 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002233 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002234 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002235 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002236 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302237
2238 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002239}
2240
2241static void udp_chr_update_read_handler(CharDriverState *chr)
2242{
2243 NetCharDriver *s = chr->opaque;
2244
Anthony Liguori76a96442013-03-05 23:21:21 +05302245 if (s->tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02002246 io_remove_watch_poll(s->tag);
Anthony Liguori76a96442013-03-05 23:21:21 +05302247 s->tag = 0;
2248 }
2249
2250 if (s->chan) {
2251 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002252 }
2253}
2254
aliguori819f56b2009-03-28 17:58:14 +00002255static void udp_chr_close(CharDriverState *chr)
2256{
2257 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302258 if (s->tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02002259 io_remove_watch_poll(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002260 s->tag = 0;
Anthony Liguori76a96442013-03-05 23:21:21 +05302261 }
2262 if (s->chan) {
2263 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002264 closesocket(s->fd);
2265 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002266 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002267 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002268}
2269
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002270static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002271{
2272 CharDriverState *chr = NULL;
2273 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002274
Anthony Liguori7267c092011-08-20 22:09:37 -05002275 chr = g_malloc0(sizeof(CharDriverState));
2276 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002277
aliguori6f97dba2008-10-31 18:49:55 +00002278 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302279 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002280 s->bufcnt = 0;
2281 s->bufptr = 0;
2282 chr->opaque = s;
2283 chr->chr_write = udp_chr_write;
2284 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002285 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002286 /* be isn't opened until we get a connection */
2287 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002288 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002289}
aliguori6f97dba2008-10-31 18:49:55 +00002290
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002291static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2292{
2293 Error *local_err = NULL;
2294 int fd = -1;
2295
2296 fd = inet_dgram_opts(opts, &local_err);
2297 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002298 qerror_report_err(local_err);
2299 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002300 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002301 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002302 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002303}
2304
2305/***********************************************************/
2306/* TCP Net console */
2307
2308typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302309
2310 GIOChannel *chan, *listen_chan;
2311 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002312 int fd, listen_fd;
2313 int connected;
2314 int max_size;
2315 int do_telnetopt;
2316 int do_nodelay;
2317 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002318 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002319} TCPCharDriver;
2320
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302321static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002322
2323static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2324{
2325 TCPCharDriver *s = chr->opaque;
2326 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002327 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002328 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002329 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002330 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002331 }
2332}
2333
2334static int tcp_chr_read_poll(void *opaque)
2335{
2336 CharDriverState *chr = opaque;
2337 TCPCharDriver *s = chr->opaque;
2338 if (!s->connected)
2339 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002340 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002341 return s->max_size;
2342}
2343
2344#define IAC 255
2345#define IAC_BREAK 243
2346static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2347 TCPCharDriver *s,
2348 uint8_t *buf, int *size)
2349{
2350 /* Handle any telnet client's basic IAC options to satisfy char by
2351 * char mode with no echo. All IAC options will be removed from
2352 * the buf and the do_telnetopt variable will be used to track the
2353 * state of the width of the IAC information.
2354 *
2355 * IAC commands come in sets of 3 bytes with the exception of the
2356 * "IAC BREAK" command and the double IAC.
2357 */
2358
2359 int i;
2360 int j = 0;
2361
2362 for (i = 0; i < *size; i++) {
2363 if (s->do_telnetopt > 1) {
2364 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2365 /* Double IAC means send an IAC */
2366 if (j != i)
2367 buf[j] = buf[i];
2368 j++;
2369 s->do_telnetopt = 1;
2370 } else {
2371 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2372 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002373 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002374 s->do_telnetopt++;
2375 }
2376 s->do_telnetopt++;
2377 }
2378 if (s->do_telnetopt >= 4) {
2379 s->do_telnetopt = 1;
2380 }
2381 } else {
2382 if ((unsigned char)buf[i] == IAC) {
2383 s->do_telnetopt = 2;
2384 } else {
2385 if (j != i)
2386 buf[j] = buf[i];
2387 j++;
2388 }
2389 }
2390 }
2391 *size = j;
2392}
2393
Mark McLoughlin7d174052009-07-22 09:11:39 +01002394static int tcp_get_msgfd(CharDriverState *chr)
2395{
2396 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002397 int fd = s->msgfd;
2398 s->msgfd = -1;
2399 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002400}
2401
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002402#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002403static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2404{
2405 TCPCharDriver *s = chr->opaque;
2406 struct cmsghdr *cmsg;
2407
2408 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2409 int fd;
2410
2411 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2412 cmsg->cmsg_level != SOL_SOCKET ||
2413 cmsg->cmsg_type != SCM_RIGHTS)
2414 continue;
2415
2416 fd = *((int *)CMSG_DATA(cmsg));
2417 if (fd < 0)
2418 continue;
2419
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002420 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2421 qemu_set_block(fd);
2422
Corey Bryant06138652012-08-14 16:43:42 -04002423#ifndef MSG_CMSG_CLOEXEC
2424 qemu_set_cloexec(fd);
2425#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002426 if (s->msgfd != -1)
2427 close(s->msgfd);
2428 s->msgfd = fd;
2429 }
2430}
2431
Mark McLoughlin9977c892009-07-22 09:11:38 +01002432static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2433{
2434 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002435 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002436 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002437 union {
2438 struct cmsghdr cmsg;
2439 char control[CMSG_SPACE(sizeof(int))];
2440 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002441 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002442 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002443
2444 iov[0].iov_base = buf;
2445 iov[0].iov_len = len;
2446
2447 msg.msg_iov = iov;
2448 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002449 msg.msg_control = &msg_control;
2450 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002451
Corey Bryant06138652012-08-14 16:43:42 -04002452#ifdef MSG_CMSG_CLOEXEC
2453 flags |= MSG_CMSG_CLOEXEC;
2454#endif
2455 ret = recvmsg(s->fd, &msg, flags);
2456 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002457 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002458 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002459
2460 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002461}
2462#else
2463static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2464{
2465 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002466 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002467}
2468#endif
2469
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302470static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2471{
2472 TCPCharDriver *s = chr->opaque;
2473 return g_io_create_watch(s->chan, cond);
2474}
2475
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302476static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002477{
2478 CharDriverState *chr = opaque;
2479 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302480 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002481 int len, size;
2482
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302483 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002484 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302485 }
aliguori6f97dba2008-10-31 18:49:55 +00002486 len = sizeof(buf);
2487 if (len > s->max_size)
2488 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002489 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002490 if (size == 0) {
2491 /* connection closed */
2492 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302493 if (s->listen_chan) {
2494 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002495 }
Paolo Bonzini910b6362013-04-19 17:32:06 +02002496 if (s->tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02002497 io_remove_watch_poll(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002498 s->tag = 0;
2499 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302500 g_io_channel_unref(s->chan);
2501 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002502 closesocket(s->fd);
2503 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002504 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002505 } else if (size > 0) {
2506 if (s->do_telnetopt)
2507 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2508 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002509 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002510 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302511
2512 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002513}
2514
Blue Swirl68c18d12010-08-15 09:46:24 +00002515#ifndef _WIN32
2516CharDriverState *qemu_chr_open_eventfd(int eventfd)
2517{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002518 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002519}
Blue Swirl68c18d12010-08-15 09:46:24 +00002520#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002521
aliguori6f97dba2008-10-31 18:49:55 +00002522static void tcp_chr_connect(void *opaque)
2523{
2524 CharDriverState *chr = opaque;
2525 TCPCharDriver *s = chr->opaque;
2526
2527 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302528 if (s->chan) {
2529 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002530 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002531 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002532}
2533
2534#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2535static void tcp_chr_telnet_init(int fd)
2536{
2537 char buf[3];
2538 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2539 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2540 send(fd, (char *)buf, 3, 0);
2541 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2542 send(fd, (char *)buf, 3, 0);
2543 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2544 send(fd, (char *)buf, 3, 0);
2545 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2546 send(fd, (char *)buf, 3, 0);
2547}
2548
Daniel P. Berrange13661082011-06-23 13:31:42 +01002549static int tcp_chr_add_client(CharDriverState *chr, int fd)
2550{
2551 TCPCharDriver *s = chr->opaque;
2552 if (s->fd != -1)
2553 return -1;
2554
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002555 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002556 if (s->do_nodelay)
2557 socket_set_nodelay(fd);
2558 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302559 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002560 if (s->listen_tag) {
2561 g_source_remove(s->listen_tag);
2562 s->listen_tag = 0;
2563 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002564 tcp_chr_connect(chr);
2565
2566 return 0;
2567}
2568
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302569static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002570{
2571 CharDriverState *chr = opaque;
2572 TCPCharDriver *s = chr->opaque;
2573 struct sockaddr_in saddr;
2574#ifndef _WIN32
2575 struct sockaddr_un uaddr;
2576#endif
2577 struct sockaddr *addr;
2578 socklen_t len;
2579 int fd;
2580
2581 for(;;) {
2582#ifndef _WIN32
2583 if (s->is_unix) {
2584 len = sizeof(uaddr);
2585 addr = (struct sockaddr *)&uaddr;
2586 } else
2587#endif
2588 {
2589 len = sizeof(saddr);
2590 addr = (struct sockaddr *)&saddr;
2591 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002592 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002593 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002594 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302595 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002596 } else if (fd >= 0) {
2597 if (s->do_telnetopt)
2598 tcp_chr_telnet_init(fd);
2599 break;
2600 }
2601 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002602 if (tcp_chr_add_client(chr, fd) < 0)
2603 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302604
2605 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002606}
2607
2608static void tcp_chr_close(CharDriverState *chr)
2609{
2610 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002611 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302612 if (s->tag) {
Paolo Bonzini2b316772013-04-19 17:32:09 +02002613 io_remove_watch_poll(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002614 s->tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302615 }
2616 if (s->chan) {
2617 g_io_channel_unref(s->chan);
2618 }
aliguori6f97dba2008-10-31 18:49:55 +00002619 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002620 }
2621 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302622 if (s->listen_tag) {
2623 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002624 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302625 }
2626 if (s->listen_chan) {
2627 g_io_channel_unref(s->listen_chan);
2628 }
aliguori6f97dba2008-10-31 18:49:55 +00002629 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002630 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002631 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002632 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002633}
2634
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002635static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2636 bool is_listen, bool is_telnet,
2637 bool is_waitconnect,
2638 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002639{
2640 CharDriverState *chr = NULL;
2641 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002642 char host[NI_MAXHOST], serv[NI_MAXSERV];
2643 const char *left = "", *right = "";
2644 struct sockaddr_storage ss;
2645 socklen_t ss_len = sizeof(ss);
2646
2647 memset(&ss, 0, ss_len);
2648 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002649 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002650 return NULL;
2651 }
2652
2653 chr = g_malloc0(sizeof(CharDriverState));
2654 s = g_malloc0(sizeof(TCPCharDriver));
2655
2656 s->connected = 0;
2657 s->fd = -1;
2658 s->listen_fd = -1;
2659 s->msgfd = -1;
2660
2661 chr->filename = g_malloc(256);
2662 switch (ss.ss_family) {
2663#ifndef _WIN32
2664 case AF_UNIX:
2665 s->is_unix = 1;
2666 snprintf(chr->filename, 256, "unix:%s%s",
2667 ((struct sockaddr_un *)(&ss))->sun_path,
2668 is_listen ? ",server" : "");
2669 break;
2670#endif
2671 case AF_INET6:
2672 left = "[";
2673 right = "]";
2674 /* fall through */
2675 case AF_INET:
2676 s->do_nodelay = do_nodelay;
2677 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2678 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002679 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002680 is_telnet ? "telnet" : "tcp",
2681 left, host, right, serv,
2682 is_listen ? ",server" : "");
2683 break;
2684 }
2685
2686 chr->opaque = s;
2687 chr->chr_write = tcp_chr_write;
2688 chr->chr_close = tcp_chr_close;
2689 chr->get_msgfd = tcp_get_msgfd;
2690 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302691 chr->chr_add_watch = tcp_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002692 /* be isn't opened until we get a connection */
2693 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002694
2695 if (is_listen) {
2696 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302697 s->listen_chan = io_channel_from_socket(s->listen_fd);
2698 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002699 if (is_telnet) {
2700 s->do_telnetopt = 1;
2701 }
2702 } else {
2703 s->connected = 1;
2704 s->fd = fd;
2705 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302706 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002707 tcp_chr_connect(chr);
2708 }
2709
2710 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002711 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2712 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302713 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002714 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002715 }
2716 return chr;
2717}
2718
2719static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2720{
2721 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002722 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002723 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002724
liguange990a392013-06-18 11:45:35 +08002725 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2726 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2727 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2728 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2729 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002730
aliguorif07b6002008-11-11 20:54:09 +00002731 if (is_unix) {
2732 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002733 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002734 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002735 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002736 }
2737 } else {
2738 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002739 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002740 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002741 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002742 }
2743 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002744 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002745 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002746 }
aliguori6f97dba2008-10-31 18:49:55 +00002747
2748 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002749 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002750
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002751 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2752 is_waitconnect, &local_err);
2753 if (error_is_set(&local_err)) {
2754 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002755 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002756 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002757
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002758
aliguori6f97dba2008-10-31 18:49:55 +00002759 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002760 if (local_err) {
2761 qerror_report_err(local_err);
2762 error_free(local_err);
2763 }
2764 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002765 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002766 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002767 if (chr) {
2768 g_free(chr->opaque);
2769 g_free(chr);
2770 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002771 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002772}
2773
Lei Li51767e72013-01-25 00:03:19 +08002774/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002775/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002776
2777typedef struct {
2778 size_t size;
2779 size_t prod;
2780 size_t cons;
2781 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002782} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002783
Markus Armbruster3949e592013-02-06 21:27:24 +01002784static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002785{
Markus Armbruster3949e592013-02-06 21:27:24 +01002786 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002787
Markus Armbruster5c230102013-02-06 21:27:23 +01002788 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002789}
2790
Markus Armbruster3949e592013-02-06 21:27:24 +01002791static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002792{
Markus Armbruster3949e592013-02-06 21:27:24 +01002793 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002794 int i;
2795
2796 if (!buf || (len < 0)) {
2797 return -1;
2798 }
2799
2800 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002801 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2802 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002803 d->cons = d->prod - d->size;
2804 }
2805 }
2806
2807 return 0;
2808}
2809
Markus Armbruster3949e592013-02-06 21:27:24 +01002810static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002811{
Markus Armbruster3949e592013-02-06 21:27:24 +01002812 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002813 int i;
2814
Markus Armbruster5c230102013-02-06 21:27:23 +01002815 for (i = 0; i < len && d->cons != d->prod; i++) {
2816 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002817 }
2818
2819 return i;
2820}
2821
Markus Armbruster3949e592013-02-06 21:27:24 +01002822static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002823{
Markus Armbruster3949e592013-02-06 21:27:24 +01002824 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002825
2826 g_free(d->cbuf);
2827 g_free(d);
2828 chr->opaque = NULL;
2829}
2830
Markus Armbruster4f573782013-07-26 16:44:32 +02002831static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2832 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002833{
2834 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002835 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002836
2837 chr = g_malloc0(sizeof(CharDriverState));
2838 d = g_malloc(sizeof(*d));
2839
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002840 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002841
2842 /* The size must be power of 2 */
2843 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02002844 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002845 goto fail;
2846 }
2847
2848 d->prod = 0;
2849 d->cons = 0;
2850 d->cbuf = g_malloc0(d->size);
2851
2852 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002853 chr->chr_write = ringbuf_chr_write;
2854 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002855
2856 return chr;
2857
2858fail:
2859 g_free(d);
2860 g_free(chr);
2861 return NULL;
2862}
2863
Markus Armbruster3949e592013-02-06 21:27:24 +01002864static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002865{
Markus Armbruster3949e592013-02-06 21:27:24 +01002866 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002867}
2868
Markus Armbruster3949e592013-02-06 21:27:24 +01002869void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002870 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002871 Error **errp)
2872{
2873 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002874 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002875 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10002876 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002877
2878 chr = qemu_chr_find(device);
2879 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002880 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002881 return;
2882 }
2883
Markus Armbruster3949e592013-02-06 21:27:24 +01002884 if (!chr_is_ringbuf(chr)) {
2885 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002886 return;
2887 }
2888
Lei Li1f590cf2013-01-25 00:03:20 +08002889 if (has_format && (format == DATA_FORMAT_BASE64)) {
2890 write_data = g_base64_decode(data, &write_count);
2891 } else {
2892 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002893 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002894 }
2895
Markus Armbruster3949e592013-02-06 21:27:24 +01002896 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002897
Markus Armbruster13289fb2013-02-06 21:27:18 +01002898 if (write_data != (uint8_t *)data) {
2899 g_free((void *)write_data);
2900 }
2901
Lei Li1f590cf2013-01-25 00:03:20 +08002902 if (ret < 0) {
2903 error_setg(errp, "Failed to write to device %s", device);
2904 return;
2905 }
2906}
2907
Markus Armbruster3949e592013-02-06 21:27:24 +01002908char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002909 bool has_format, enum DataFormat format,
2910 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002911{
2912 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002913 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002914 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002915 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002916
2917 chr = qemu_chr_find(device);
2918 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002919 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002920 return NULL;
2921 }
2922
Markus Armbruster3949e592013-02-06 21:27:24 +01002923 if (!chr_is_ringbuf(chr)) {
2924 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002925 return NULL;
2926 }
2927
2928 if (size <= 0) {
2929 error_setg(errp, "size must be greater than zero");
2930 return NULL;
2931 }
2932
Markus Armbruster3949e592013-02-06 21:27:24 +01002933 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002934 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002935 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002936
Markus Armbruster3949e592013-02-06 21:27:24 +01002937 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002938
2939 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002940 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002941 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002942 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002943 /*
2944 * FIXME should read only complete, valid UTF-8 characters up
2945 * to @size bytes. Invalid sequences should be replaced by a
2946 * suitable replacement character. Except when (and only
2947 * when) ring buffer lost characters since last read, initial
2948 * continuation characters should be dropped.
2949 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002950 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002951 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002952 }
2953
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002954 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002955}
2956
Gerd Hoffmann33521632009-12-08 13:11:49 +01002957QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002958{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002959 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002960 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002961 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002962 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002963 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002964
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002965 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2966 if (error_is_set(&local_err)) {
2967 qerror_report_err(local_err);
2968 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002969 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002970 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002971
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002972 if (strstart(filename, "mon:", &p)) {
2973 filename = p;
2974 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04002975 if (strcmp(filename, "stdio") == 0) {
2976 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2977 * but pass it to the guest. Handle this only for compat syntax,
2978 * for -chardev syntax we have special option for this.
2979 * This is what -nographic did, redirecting+muxing serial+monitor
2980 * to stdio causing Ctrl+C to be passed to guest. */
2981 qemu_opt_set(opts, "signal", "off");
2982 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002983 }
2984
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002985 if (strcmp(filename, "null") == 0 ||
2986 strcmp(filename, "pty") == 0 ||
2987 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002988 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002989 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002990 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002991 return opts;
2992 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002993 if (strstart(filename, "vc", &p)) {
2994 qemu_opt_set(opts, "backend", "vc");
2995 if (*p == ':') {
2996 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2997 /* pixels */
2998 qemu_opt_set(opts, "width", width);
2999 qemu_opt_set(opts, "height", height);
3000 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3001 /* chars */
3002 qemu_opt_set(opts, "cols", width);
3003 qemu_opt_set(opts, "rows", height);
3004 } else {
3005 goto fail;
3006 }
3007 }
3008 return opts;
3009 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003010 if (strcmp(filename, "con:") == 0) {
3011 qemu_opt_set(opts, "backend", "console");
3012 return opts;
3013 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003014 if (strstart(filename, "COM", NULL)) {
3015 qemu_opt_set(opts, "backend", "serial");
3016 qemu_opt_set(opts, "path", filename);
3017 return opts;
3018 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003019 if (strstart(filename, "file:", &p)) {
3020 qemu_opt_set(opts, "backend", "file");
3021 qemu_opt_set(opts, "path", p);
3022 return opts;
3023 }
3024 if (strstart(filename, "pipe:", &p)) {
3025 qemu_opt_set(opts, "backend", "pipe");
3026 qemu_opt_set(opts, "path", p);
3027 return opts;
3028 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003029 if (strstart(filename, "tcp:", &p) ||
3030 strstart(filename, "telnet:", &p)) {
3031 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3032 host[0] = 0;
3033 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3034 goto fail;
3035 }
3036 qemu_opt_set(opts, "backend", "socket");
3037 qemu_opt_set(opts, "host", host);
3038 qemu_opt_set(opts, "port", port);
3039 if (p[pos] == ',') {
3040 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3041 goto fail;
3042 }
3043 if (strstart(filename, "telnet:", &p))
3044 qemu_opt_set(opts, "telnet", "on");
3045 return opts;
3046 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003047 if (strstart(filename, "udp:", &p)) {
3048 qemu_opt_set(opts, "backend", "udp");
3049 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3050 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003051 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003052 goto fail;
3053 }
3054 }
3055 qemu_opt_set(opts, "host", host);
3056 qemu_opt_set(opts, "port", port);
3057 if (p[pos] == '@') {
3058 p += pos + 1;
3059 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3060 host[0] = 0;
3061 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003062 goto fail;
3063 }
3064 }
3065 qemu_opt_set(opts, "localaddr", host);
3066 qemu_opt_set(opts, "localport", port);
3067 }
3068 return opts;
3069 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003070 if (strstart(filename, "unix:", &p)) {
3071 qemu_opt_set(opts, "backend", "socket");
3072 if (qemu_opts_do_parse(opts, p, "path") != 0)
3073 goto fail;
3074 return opts;
3075 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003076 if (strstart(filename, "/dev/parport", NULL) ||
3077 strstart(filename, "/dev/ppi", NULL)) {
3078 qemu_opt_set(opts, "backend", "parport");
3079 qemu_opt_set(opts, "path", filename);
3080 return opts;
3081 }
3082 if (strstart(filename, "/dev/", NULL)) {
3083 qemu_opt_set(opts, "backend", "tty");
3084 qemu_opt_set(opts, "path", filename);
3085 return opts;
3086 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003087
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003088fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003089 qemu_opts_del(opts);
3090 return NULL;
3091}
3092
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003093static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3094 Error **errp)
3095{
3096 const char *path = qemu_opt_get(opts, "path");
3097
3098 if (path == NULL) {
3099 error_setg(errp, "chardev: file: no filename given");
3100 return;
3101 }
3102 backend->file = g_new0(ChardevFile, 1);
3103 backend->file->out = g_strdup(path);
3104}
3105
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003106static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3107 Error **errp)
3108{
3109 backend->stdio = g_new0(ChardevStdio, 1);
3110 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003111 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003112}
3113
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003114static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3115 Error **errp)
3116{
3117 const char *device = qemu_opt_get(opts, "path");
3118
3119 if (device == NULL) {
3120 error_setg(errp, "chardev: serial/tty: no device path given");
3121 return;
3122 }
3123 backend->serial = g_new0(ChardevHostdev, 1);
3124 backend->serial->device = g_strdup(device);
3125}
3126
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003127static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3128 Error **errp)
3129{
3130 const char *device = qemu_opt_get(opts, "path");
3131
3132 if (device == NULL) {
3133 error_setg(errp, "chardev: parallel: no device path given");
3134 return;
3135 }
3136 backend->parallel = g_new0(ChardevHostdev, 1);
3137 backend->parallel->device = g_strdup(device);
3138}
3139
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003140static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3141 Error **errp)
3142{
3143 const char *device = qemu_opt_get(opts, "path");
3144
3145 if (device == NULL) {
3146 error_setg(errp, "chardev: pipe: no device path given");
3147 return;
3148 }
3149 backend->pipe = g_new0(ChardevHostdev, 1);
3150 backend->pipe->device = g_strdup(device);
3151}
3152
Markus Armbruster4f573782013-07-26 16:44:32 +02003153static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3154 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003155{
3156 int val;
3157
Markus Armbruster3a1da422013-07-26 16:44:34 +02003158 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003159
Markus Armbruster0f953052013-06-27 16:22:07 +02003160 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003161 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003162 backend->ringbuf->has_size = true;
3163 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003164 }
3165}
3166
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003167static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3168 Error **errp)
3169{
3170 const char *chardev = qemu_opt_get(opts, "chardev");
3171
3172 if (chardev == NULL) {
3173 error_setg(errp, "chardev: mux: no chardev given");
3174 return;
3175 }
3176 backend->mux = g_new0(ChardevMux, 1);
3177 backend->mux->chardev = g_strdup(chardev);
3178}
3179
Anthony Liguorid654f342013-03-05 23:21:28 +05303180typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003181 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003182 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003183 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003184 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003185 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003186 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303187} CharDriver;
3188
3189static GSList *backends;
3190
3191void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3192{
3193 CharDriver *s;
3194
3195 s = g_malloc0(sizeof(*s));
3196 s->name = g_strdup(name);
3197 s->open = open;
3198
3199 backends = g_slist_append(backends, s);
3200}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003201
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003202void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003203 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3204{
3205 CharDriver *s;
3206
3207 s = g_malloc0(sizeof(*s));
3208 s->name = g_strdup(name);
3209 s->kind = kind;
3210 s->parse = parse;
3211
3212 backends = g_slist_append(backends, s);
3213}
3214
Anthony Liguorif69554b2011-08-15 11:17:37 -05003215CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003216 void (*init)(struct CharDriverState *s),
3217 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003218{
Anthony Liguorid654f342013-03-05 23:21:28 +05303219 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003220 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303221 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003222
3223 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003224 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003225 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003226 }
3227
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003228 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003229 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003230 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003231 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003232 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303233 for (i = backends; i; i = i->next) {
3234 cd = i->data;
3235
3236 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003237 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303238 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003239 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303240 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003241 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003242 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003243 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003244 }
3245
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003246 if (!cd->open) {
3247 /* using new, qapi init */
3248 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3249 ChardevReturn *ret = NULL;
3250 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003251 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003252
3253 if (qemu_opt_get_bool(opts, "mux", 0)) {
3254 bid = g_strdup_printf("%s-base", id);
3255 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003256
3257 chr = NULL;
3258 backend->kind = cd->kind;
3259 if (cd->parse) {
3260 cd->parse(opts, backend, errp);
3261 if (error_is_set(errp)) {
3262 goto qapi_out;
3263 }
3264 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003265 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003266 if (error_is_set(errp)) {
3267 goto qapi_out;
3268 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003269
3270 if (bid) {
3271 qapi_free_ChardevBackend(backend);
3272 qapi_free_ChardevReturn(ret);
3273 backend = g_new0(ChardevBackend, 1);
3274 backend->mux = g_new0(ChardevMux, 1);
3275 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3276 backend->mux->chardev = g_strdup(bid);
3277 ret = qmp_chardev_add(id, backend, errp);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003278 assert(!error_is_set(errp));
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003279 }
3280
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003281 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003282 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003283
3284 qapi_out:
3285 qapi_free_ChardevBackend(backend);
3286 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003287 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003288 return chr;
3289 }
3290
Anthony Liguorid654f342013-03-05 23:21:28 +05303291 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003292 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003293 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003294 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003295 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003296 }
3297
3298 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003299 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003300 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003301 /* if we didn't create the chardev via qmp_chardev_add, we
3302 * need to send the OPENED event here
3303 */
3304 if (!chr->explicit_be_open) {
3305 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3306 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003307 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003308
3309 if (qemu_opt_get_bool(opts, "mux", 0)) {
3310 CharDriverState *base = chr;
3311 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003312 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003313 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3314 chr = qemu_chr_open_mux(base);
3315 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003316 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003317 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003318 } else {
3319 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003320 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003321 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003322 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003323 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003324
3325err:
3326 qemu_opts_del(opts);
3327 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003328}
3329
Anthony Liguori27143a42011-08-15 11:17:36 -05003330CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003331{
3332 const char *p;
3333 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003334 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003335 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003336
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003337 if (strstart(filename, "chardev:", &p)) {
3338 return qemu_chr_find(p);
3339 }
3340
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003341 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003342 if (!opts)
3343 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003344
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003345 chr = qemu_chr_new_from_opts(opts, init, &err);
3346 if (error_is_set(&err)) {
3347 fprintf(stderr, "%s\n", error_get_pretty(err));
3348 error_free(err);
3349 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003350 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003351 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003352 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003353 }
3354 return chr;
3355}
3356
Anthony Liguori15f31512011-08-15 11:17:35 -05003357void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003358{
3359 if (chr->chr_set_echo) {
3360 chr->chr_set_echo(chr, echo);
3361 }
3362}
3363
Hans de Goede8e25daa2013-03-26 11:07:57 +01003364void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003365{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003366 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003367 return;
3368 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003369 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003370 if (chr->chr_set_fe_open) {
3371 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003372 }
3373}
3374
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003375int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3376 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303377{
3378 GSource *src;
3379 guint tag;
3380
3381 if (s->chr_add_watch == NULL) {
3382 return -ENOSYS;
3383 }
3384
3385 src = s->chr_add_watch(s, cond);
3386 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3387 tag = g_source_attach(src, NULL);
3388 g_source_unref(src);
3389
3390 return tag;
3391}
3392
Hans de Goede44c473d2013-03-27 20:29:39 +01003393int qemu_chr_fe_claim(CharDriverState *s)
3394{
3395 if (s->avail_connections < 1) {
3396 return -1;
3397 }
3398 s->avail_connections--;
3399 return 0;
3400}
3401
3402void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3403{
3404 if (qemu_chr_fe_claim(s) != 0) {
3405 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3406 __func__, s->label);
3407 exit(1);
3408 }
3409}
3410
3411void qemu_chr_fe_release(CharDriverState *s)
3412{
3413 s->avail_connections++;
3414}
3415
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003416void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003417{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003418 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003419 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003420 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003421 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003422 g_free(chr->filename);
3423 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003424 if (chr->opts) {
3425 qemu_opts_del(chr->opts);
3426 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003427 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003428}
3429
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003430ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003431{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003432 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003433 CharDriverState *chr;
3434
Blue Swirl72cf2d42009-09-12 07:36:22 +00003435 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003436 ChardevInfoList *info = g_malloc0(sizeof(*info));
3437 info->value = g_malloc0(sizeof(*info->value));
3438 info->value->label = g_strdup(chr->label);
3439 info->value->filename = g_strdup(chr->filename);
3440
3441 info->next = chr_list;
3442 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003443 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003444
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003445 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003446}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003447
3448CharDriverState *qemu_chr_find(const char *name)
3449{
3450 CharDriverState *chr;
3451
Blue Swirl72cf2d42009-09-12 07:36:22 +00003452 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003453 if (strcmp(chr->label, name) != 0)
3454 continue;
3455 return chr;
3456 }
3457 return NULL;
3458}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003459
3460/* Get a character (serial) device interface. */
3461CharDriverState *qemu_char_get_next_serial(void)
3462{
3463 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003464 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003465
3466 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003467
3468 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3469 chr = serial_hds[next_serial++];
3470 qemu_chr_fe_claim_no_fail(chr);
3471 return chr;
3472 }
3473 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003474}
3475
Paolo Bonzini4d454572012-11-26 16:03:42 +01003476QemuOptsList qemu_chardev_opts = {
3477 .name = "chardev",
3478 .implied_opt_name = "backend",
3479 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3480 .desc = {
3481 {
3482 .name = "backend",
3483 .type = QEMU_OPT_STRING,
3484 },{
3485 .name = "path",
3486 .type = QEMU_OPT_STRING,
3487 },{
3488 .name = "host",
3489 .type = QEMU_OPT_STRING,
3490 },{
3491 .name = "port",
3492 .type = QEMU_OPT_STRING,
3493 },{
3494 .name = "localaddr",
3495 .type = QEMU_OPT_STRING,
3496 },{
3497 .name = "localport",
3498 .type = QEMU_OPT_STRING,
3499 },{
3500 .name = "to",
3501 .type = QEMU_OPT_NUMBER,
3502 },{
3503 .name = "ipv4",
3504 .type = QEMU_OPT_BOOL,
3505 },{
3506 .name = "ipv6",
3507 .type = QEMU_OPT_BOOL,
3508 },{
3509 .name = "wait",
3510 .type = QEMU_OPT_BOOL,
3511 },{
3512 .name = "server",
3513 .type = QEMU_OPT_BOOL,
3514 },{
3515 .name = "delay",
3516 .type = QEMU_OPT_BOOL,
3517 },{
3518 .name = "telnet",
3519 .type = QEMU_OPT_BOOL,
3520 },{
3521 .name = "width",
3522 .type = QEMU_OPT_NUMBER,
3523 },{
3524 .name = "height",
3525 .type = QEMU_OPT_NUMBER,
3526 },{
3527 .name = "cols",
3528 .type = QEMU_OPT_NUMBER,
3529 },{
3530 .name = "rows",
3531 .type = QEMU_OPT_NUMBER,
3532 },{
3533 .name = "mux",
3534 .type = QEMU_OPT_BOOL,
3535 },{
3536 .name = "signal",
3537 .type = QEMU_OPT_BOOL,
3538 },{
3539 .name = "name",
3540 .type = QEMU_OPT_STRING,
3541 },{
3542 .name = "debug",
3543 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003544 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003545 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003546 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003547 },{
3548 .name = "chardev",
3549 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003550 },
3551 { /* end of list */ }
3552 },
3553};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003554
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003555#ifdef _WIN32
3556
3557static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3558{
3559 HANDLE out;
3560
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003561 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003562 error_setg(errp, "input file not supported");
3563 return NULL;
3564 }
3565
3566 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3567 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3568 if (out == INVALID_HANDLE_VALUE) {
3569 error_setg(errp, "open %s failed", file->out);
3570 return NULL;
3571 }
3572 return qemu_chr_open_win_file(out);
3573}
3574
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003575static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3576 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003577{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003578 return qemu_chr_open_win_path(serial->device);
3579}
3580
3581static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3582 Error **errp)
3583{
3584 error_setg(errp, "character device backend type 'parallel' not supported");
3585 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003586}
3587
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003588#else /* WIN32 */
3589
3590static int qmp_chardev_open_file_source(char *src, int flags,
3591 Error **errp)
3592{
3593 int fd = -1;
3594
3595 TFR(fd = qemu_open(src, flags, 0666));
3596 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003597 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003598 }
3599 return fd;
3600}
3601
3602static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3603{
3604 int flags, in = -1, out = -1;
3605
3606 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3607 out = qmp_chardev_open_file_source(file->out, flags, errp);
3608 if (error_is_set(errp)) {
3609 return NULL;
3610 }
3611
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003612 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003613 flags = O_RDONLY;
3614 in = qmp_chardev_open_file_source(file->in, flags, errp);
3615 if (error_is_set(errp)) {
3616 qemu_close(out);
3617 return NULL;
3618 }
3619 }
3620
3621 return qemu_chr_open_fd(in, out);
3622}
3623
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003624static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3625 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003626{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003627#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003628 int fd;
3629
3630 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3631 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003632 return NULL;
3633 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003634 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003635 return qemu_chr_open_tty_fd(fd);
3636#else
3637 error_setg(errp, "character device backend type 'serial' not supported");
3638 return NULL;
3639#endif
3640}
3641
3642static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3643 Error **errp)
3644{
3645#ifdef HAVE_CHARDEV_PARPORT
3646 int fd;
3647
3648 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3649 if (error_is_set(errp)) {
3650 return NULL;
3651 }
3652 return qemu_chr_open_pp_fd(fd);
3653#else
3654 error_setg(errp, "character device backend type 'parallel' not supported");
3655 return NULL;
3656#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003657}
3658
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003659#endif /* WIN32 */
3660
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003661static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3662 Error **errp)
3663{
3664 SocketAddress *addr = sock->addr;
3665 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3666 bool is_listen = sock->has_server ? sock->server : true;
3667 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3668 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3669 int fd;
3670
3671 if (is_listen) {
3672 fd = socket_listen(addr, errp);
3673 } else {
3674 fd = socket_connect(addr, errp, NULL, NULL);
3675 }
3676 if (error_is_set(errp)) {
3677 return NULL;
3678 }
3679 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3680 is_telnet, is_waitconnect, errp);
3681}
3682
Lei Li08d0ab32013-05-20 14:51:03 +08003683static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3684 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003685{
3686 int fd;
3687
Lei Li08d0ab32013-05-20 14:51:03 +08003688 fd = socket_dgram(udp->remote, udp->local, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003689 if (error_is_set(errp)) {
3690 return NULL;
3691 }
3692 return qemu_chr_open_udp_fd(fd);
3693}
3694
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003695ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3696 Error **errp)
3697{
3698 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003699 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003700
3701 chr = qemu_chr_find(id);
3702 if (chr) {
3703 error_setg(errp, "Chardev '%s' already exists", id);
3704 g_free(ret);
3705 return NULL;
3706 }
3707
3708 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003709 case CHARDEV_BACKEND_KIND_FILE:
3710 chr = qmp_chardev_open_file(backend->file, errp);
3711 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003712 case CHARDEV_BACKEND_KIND_SERIAL:
3713 chr = qmp_chardev_open_serial(backend->serial, errp);
3714 break;
3715 case CHARDEV_BACKEND_KIND_PARALLEL:
3716 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003717 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003718 case CHARDEV_BACKEND_KIND_PIPE:
3719 chr = qemu_chr_open_pipe(backend->pipe);
3720 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003721 case CHARDEV_BACKEND_KIND_SOCKET:
3722 chr = qmp_chardev_open_socket(backend->socket, errp);
3723 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003724 case CHARDEV_BACKEND_KIND_UDP:
3725 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003726 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003727#ifdef HAVE_CHARDEV_TTY
3728 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003729 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003730 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003731#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003732 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003733 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003734 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003735 case CHARDEV_BACKEND_KIND_MUX:
3736 base = qemu_chr_find(backend->mux->chardev);
3737 if (base == NULL) {
3738 error_setg(errp, "mux: base chardev %s not found",
3739 backend->mux->chardev);
3740 break;
3741 }
3742 chr = qemu_chr_open_mux(base);
3743 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003744 case CHARDEV_BACKEND_KIND_MSMOUSE:
3745 chr = qemu_chr_open_msmouse();
3746 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003747#ifdef CONFIG_BRLAPI
3748 case CHARDEV_BACKEND_KIND_BRAILLE:
3749 chr = chr_baum_init();
3750 break;
3751#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003752 case CHARDEV_BACKEND_KIND_STDIO:
3753 chr = qemu_chr_open_stdio(backend->stdio);
3754 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003755#ifdef _WIN32
3756 case CHARDEV_BACKEND_KIND_CONSOLE:
3757 chr = qemu_chr_open_win_con();
3758 break;
3759#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003760#ifdef CONFIG_SPICE
3761 case CHARDEV_BACKEND_KIND_SPICEVMC:
3762 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3763 break;
3764 case CHARDEV_BACKEND_KIND_SPICEPORT:
3765 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3766 break;
3767#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003768 case CHARDEV_BACKEND_KIND_VC:
3769 chr = vc_init(backend->vc);
3770 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003771 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003772 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003773 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003774 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003775 default:
3776 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3777 break;
3778 }
3779
3780 if (chr == NULL && !error_is_set(errp)) {
3781 error_setg(errp, "Failed to create chardev");
3782 }
3783 if (chr) {
3784 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003785 chr->avail_connections =
3786 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02003787 if (!chr->filename) {
3788 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
3789 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05003790 if (!chr->explicit_be_open) {
3791 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3792 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003793 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3794 return ret;
3795 } else {
3796 g_free(ret);
3797 return NULL;
3798 }
3799}
3800
3801void qmp_chardev_remove(const char *id, Error **errp)
3802{
3803 CharDriverState *chr;
3804
3805 chr = qemu_chr_find(id);
3806 if (NULL == chr) {
3807 error_setg(errp, "Chardev '%s' not found", id);
3808 return;
3809 }
3810 if (chr->chr_can_read || chr->chr_read ||
3811 chr->chr_event || chr->handler_opaque) {
3812 error_setg(errp, "Chardev '%s' is busy", id);
3813 return;
3814 }
3815 qemu_chr_delete(chr);
3816}
Anthony Liguorid654f342013-03-05 23:21:28 +05303817
3818static void register_types(void)
3819{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003820 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303821 register_char_driver("socket", qemu_chr_open_socket);
3822 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02003823 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02003824 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003825 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3826 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003827 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3828 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003829 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3830 qemu_chr_parse_serial);
3831 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3832 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003833 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3834 qemu_chr_parse_parallel);
3835 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3836 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003837 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003838 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003839 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3840 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003841 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
3842 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02003843 /* Bug-compatibility: */
3844 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3845 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05003846 /* this must be done after machine init, since we register FEs with muxes
3847 * as part of realize functions like serial_isa_realizefn when -nographic
3848 * is specified
3849 */
3850 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05303851}
3852
3853type_init(register_types);