blob: b6430c7428051084e4fad75ee81912740c927360 [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
Peter Maydell9355ad32014-06-16 18:36:39 +0100179int qemu_chr_del_client(CharDriverState *s)
180{
181 return s->chr_del_client ? s->chr_del_client(s) : -1;
182}
183
aliguori6f97dba2008-10-31 18:49:55 +0000184void qemu_chr_accept_input(CharDriverState *s)
185{
186 if (s->chr_accept_input)
187 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100188 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000189}
190
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500191void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000192{
Amit Shah9bd78542009-11-03 19:59:54 +0530193 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000194 va_list ap;
195 va_start(ap, fmt);
196 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500197 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000198 va_end(ap);
199}
200
Amit Shah386a5a12013-08-28 15:24:05 +0530201static void remove_fd_in_watch(CharDriverState *chr);
202
aliguori6f97dba2008-10-31 18:49:55 +0000203void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100204 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000205 IOReadHandler *fd_read,
206 IOEventHandler *fd_event,
207 void *opaque)
208{
Hans de Goede19083222013-03-26 11:07:56 +0100209 int fe_open;
210
Amit Shahda7d9982011-04-25 15:18:22 +0530211 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100212 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530213 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100214 } else {
215 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530216 }
aliguori6f97dba2008-10-31 18:49:55 +0000217 s->chr_can_read = fd_can_read;
218 s->chr_read = fd_read;
219 s->chr_event = fd_event;
220 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200221 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000222 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200223
Hans de Goede19083222013-03-26 11:07:56 +0100224 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100225 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100226 }
227
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200228 /* We're connecting to an already opened device, so let's make sure we
229 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100230 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100231 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200232 }
aliguori6f97dba2008-10-31 18:49:55 +0000233}
234
235static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
236{
237 return len;
238}
239
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100240static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000241{
242 CharDriverState *chr;
243
Anthony Liguori7267c092011-08-20 22:09:37 -0500244 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000245 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500246 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100247 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000248}
249
250/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000251#define MAX_MUX 4
252#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
253#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
254typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100255 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000256 IOReadHandler *chr_read[MAX_MUX];
257 IOEventHandler *chr_event[MAX_MUX];
258 void *ext_opaque[MAX_MUX];
259 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200260 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000261 int mux_cnt;
262 int term_got_escape;
263 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000264 /* Intermediate input buffer allows to catch escape sequences even if the
265 currently active device is not accepting any input - but only until it
266 is full as well. */
267 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
268 int prod[MAX_MUX];
269 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200270 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200271 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200272 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000273} MuxDriver;
274
275
276static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
277{
278 MuxDriver *d = chr->opaque;
279 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200280 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000281 ret = d->drv->chr_write(d->drv, buf, len);
282 } else {
283 int i;
284
285 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200286 for (i = 0; i < len; i++) {
287 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000288 char buf1[64];
289 int64_t ti;
290 int secs;
291
Alex Blighbc72ad62013-08-21 16:03:08 +0100292 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200293 if (d->timestamps_start == -1)
294 d->timestamps_start = ti;
295 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000296 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000297 snprintf(buf1, sizeof(buf1),
298 "[%02d:%02d:%02d.%03d] ",
299 secs / 3600,
300 (secs / 60) % 60,
301 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000302 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000303 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200304 d->linestart = 0;
305 }
306 ret += d->drv->chr_write(d->drv, buf+i, 1);
307 if (buf[i] == '\n') {
308 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000309 }
310 }
311 }
312 return ret;
313}
314
315static const char * const mux_help[] = {
316 "% h print this help\n\r",
317 "% x exit emulator\n\r",
318 "% s save disk data back to file (if -snapshot)\n\r",
319 "% t toggle console timestamps\n\r"
320 "% b send break (magic sysrq)\n\r",
321 "% c switch between console and monitor\n\r",
322 "% % sends %\n\r",
323 NULL
324};
325
326int term_escape_char = 0x01; /* ctrl-a is used for escape */
327static void mux_print_help(CharDriverState *chr)
328{
329 int i, j;
330 char ebuf[15] = "Escape-Char";
331 char cbuf[50] = "\n\r";
332
333 if (term_escape_char > 0 && term_escape_char < 26) {
334 snprintf(cbuf, sizeof(cbuf), "\n\r");
335 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
336 } else {
337 snprintf(cbuf, sizeof(cbuf),
338 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
339 term_escape_char);
340 }
341 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
342 for (i = 0; mux_help[i] != NULL; i++) {
343 for (j=0; mux_help[i][j] != '\0'; j++) {
344 if (mux_help[i][j] == '%')
345 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
346 else
347 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
348 }
349 }
350}
351
aliguori2724b182009-03-05 23:01:47 +0000352static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
353{
354 if (d->chr_event[mux_nr])
355 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
356}
357
aliguori6f97dba2008-10-31 18:49:55 +0000358static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
359{
360 if (d->term_got_escape) {
361 d->term_got_escape = 0;
362 if (ch == term_escape_char)
363 goto send_char;
364 switch(ch) {
365 case '?':
366 case 'h':
367 mux_print_help(chr);
368 break;
369 case 'x':
370 {
371 const char *term = "QEMU: Terminated\n\r";
372 chr->chr_write(chr,(uint8_t *)term,strlen(term));
373 exit(0);
374 break;
375 }
376 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200377 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000378 break;
379 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100380 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000381 break;
382 case 'c':
383 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200384 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
385 d->focus++;
386 if (d->focus >= d->mux_cnt)
387 d->focus = 0;
388 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000389 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200390 case 't':
391 d->timestamps = !d->timestamps;
392 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200393 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200394 break;
aliguori6f97dba2008-10-31 18:49:55 +0000395 }
396 } else if (ch == term_escape_char) {
397 d->term_got_escape = 1;
398 } else {
399 send_char:
400 return 1;
401 }
402 return 0;
403}
404
405static void mux_chr_accept_input(CharDriverState *chr)
406{
aliguori6f97dba2008-10-31 18:49:55 +0000407 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200408 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000409
aliguoria80bf992009-03-05 23:00:02 +0000410 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000411 d->chr_can_read[m] &&
412 d->chr_can_read[m](d->ext_opaque[m])) {
413 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000414 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000415 }
416}
417
418static int mux_chr_can_read(void *opaque)
419{
420 CharDriverState *chr = opaque;
421 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200422 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000423
aliguoria80bf992009-03-05 23:00:02 +0000424 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000425 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000426 if (d->chr_can_read[m])
427 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000428 return 0;
429}
430
431static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
432{
433 CharDriverState *chr = opaque;
434 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200435 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000436 int i;
437
438 mux_chr_accept_input (opaque);
439
440 for(i = 0; i < size; i++)
441 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000442 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000443 d->chr_can_read[m] &&
444 d->chr_can_read[m](d->ext_opaque[m]))
445 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
446 else
aliguoria80bf992009-03-05 23:00:02 +0000447 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000448 }
449}
450
451static void mux_chr_event(void *opaque, int event)
452{
453 CharDriverState *chr = opaque;
454 MuxDriver *d = chr->opaque;
455 int i;
456
457 /* Send the event to all registered listeners */
458 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000459 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000460}
461
462static void mux_chr_update_read_handler(CharDriverState *chr)
463{
464 MuxDriver *d = chr->opaque;
465
466 if (d->mux_cnt >= MAX_MUX) {
467 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
468 return;
469 }
470 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
471 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
472 d->chr_read[d->mux_cnt] = chr->chr_read;
473 d->chr_event[d->mux_cnt] = chr->chr_event;
474 /* Fix up the real driver with mux routines */
475 if (d->mux_cnt == 0) {
476 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
477 mux_chr_event, chr);
478 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200479 if (d->focus != -1) {
480 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200481 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200482 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000483 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200484 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000485}
486
Michael Roth7b7ab182013-07-30 13:04:22 -0500487static bool muxes_realized;
488
489/**
490 * Called after processing of default and command-line-specified
491 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
492 * to a mux chardev. This is done here to ensure that
493 * output/prompts/banners are only displayed for the FE that has
494 * focus when initial command-line processing/machine init is
495 * completed.
496 *
497 * After this point, any new FE attached to any new or existing
498 * mux will receive CHR_EVENT_OPENED notifications for the BE
499 * immediately.
500 */
501static void muxes_realize_done(Notifier *notifier, void *unused)
502{
503 CharDriverState *chr;
504
505 QTAILQ_FOREACH(chr, &chardevs, next) {
506 if (chr->is_mux) {
507 MuxDriver *d = chr->opaque;
508 int i;
509
510 /* send OPENED to all already-attached FEs */
511 for (i = 0; i < d->mux_cnt; i++) {
512 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
513 }
514 /* mark mux as OPENED so any new FEs will immediately receive
515 * OPENED event
516 */
517 qemu_chr_be_generic_open(chr);
518 }
519 }
520 muxes_realized = true;
521}
522
523static Notifier muxes_realize_notify = {
524 .notify = muxes_realize_done,
525};
526
aliguori6f97dba2008-10-31 18:49:55 +0000527static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
528{
529 CharDriverState *chr;
530 MuxDriver *d;
531
Anthony Liguori7267c092011-08-20 22:09:37 -0500532 chr = g_malloc0(sizeof(CharDriverState));
533 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000534
535 chr->opaque = d;
536 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200537 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000538 chr->chr_write = mux_chr_write;
539 chr->chr_update_read_handler = mux_chr_update_read_handler;
540 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100541 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100542 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500543 /* only default to opened state if we've realized the initial
544 * set of muxes
545 */
546 chr->explicit_be_open = muxes_realized ? 0 : 1;
547 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200548
aliguori6f97dba2008-10-31 18:49:55 +0000549 return chr;
550}
551
552
553#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000554int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000555{
556 int ret, len;
557
558 len = len1;
559 while (len > 0) {
560 ret = send(fd, buf, len, 0);
561 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000562 errno = WSAGetLastError();
563 if (errno != WSAEWOULDBLOCK) {
564 return -1;
565 }
566 } else if (ret == 0) {
567 break;
568 } else {
569 buf += ret;
570 len -= ret;
571 }
572 }
573 return len1 - len;
574}
575
576#else
577
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100578int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000579{
580 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100581 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000582
583 len = len1;
584 while (len > 0) {
585 ret = write(fd, buf, len);
586 if (ret < 0) {
587 if (errno != EINTR && errno != EAGAIN)
588 return -1;
589 } else if (ret == 0) {
590 break;
591 } else {
592 buf += ret;
593 len -= ret;
594 }
595 }
596 return len1 - len;
597}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500598
599int recv_all(int fd, void *_buf, int len1, bool single_read)
600{
601 int ret, len;
602 uint8_t *buf = _buf;
603
604 len = len1;
605 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
606 if (ret < 0) {
607 if (errno != EINTR && errno != EAGAIN) {
608 return -1;
609 }
610 continue;
611 } else {
612 if (single_read) {
613 return ret;
614 }
615 buf += ret;
616 len -= ret;
617 }
618 }
619 return len1 - len;
620}
621
aliguori6f97dba2008-10-31 18:49:55 +0000622#endif /* !_WIN32 */
623
Anthony Liguori96c63842013-03-05 23:21:18 +0530624typedef struct IOWatchPoll
625{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200626 GSource parent;
627
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200628 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530629 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530630
631 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200632 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530633 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530634} IOWatchPoll;
635
Anthony Liguori96c63842013-03-05 23:21:18 +0530636static IOWatchPoll *io_watch_poll_from_source(GSource *source)
637{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200638 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530639}
640
641static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
642{
643 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200644 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200645 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200646 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530647 return FALSE;
648 }
649
Paolo Bonzinid185c092013-04-05 17:59:33 +0200650 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200651 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
652 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200653 g_source_attach(iwp->src, NULL);
654 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200655 g_source_destroy(iwp->src);
656 g_source_unref(iwp->src);
657 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200658 }
659 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530660}
661
662static gboolean io_watch_poll_check(GSource *source)
663{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200664 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530665}
666
667static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
668 gpointer user_data)
669{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200670 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530671}
672
673static void io_watch_poll_finalize(GSource *source)
674{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200675 /* Due to a glib bug, removing the last reference to a source
676 * inside a finalize callback causes recursive locking (and a
677 * deadlock). This is not a problem inside other callbacks,
678 * including dispatch callbacks, so we call io_remove_watch_poll
679 * to remove this source. At this point, iwp->src must
680 * be NULL, or we would leak it.
681 *
682 * This would be solved much more elegantly by child sources,
683 * but we support older glib versions that do not have them.
684 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530685 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200686 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530687}
688
689static GSourceFuncs io_watch_poll_funcs = {
690 .prepare = io_watch_poll_prepare,
691 .check = io_watch_poll_check,
692 .dispatch = io_watch_poll_dispatch,
693 .finalize = io_watch_poll_finalize,
694};
695
696/* Can only be used for read */
697static guint io_add_watch_poll(GIOChannel *channel,
698 IOCanReadHandler *fd_can_read,
699 GIOFunc fd_read,
700 gpointer user_data)
701{
702 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200703 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530704
Paolo Bonzinid185c092013-04-05 17:59:33 +0200705 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530706 iwp->fd_can_read = fd_can_read;
707 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200708 iwp->channel = channel;
709 iwp->fd_read = (GSourceFunc) fd_read;
710 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530711
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200712 tag = g_source_attach(&iwp->parent, NULL);
713 g_source_unref(&iwp->parent);
714 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530715}
716
Paolo Bonzini2b316772013-04-19 17:32:09 +0200717static void io_remove_watch_poll(guint tag)
718{
719 GSource *source;
720 IOWatchPoll *iwp;
721
722 g_return_if_fail (tag > 0);
723
724 source = g_main_context_find_source_by_id(NULL, tag);
725 g_return_if_fail (source != NULL);
726
727 iwp = io_watch_poll_from_source(source);
728 if (iwp->src) {
729 g_source_destroy(iwp->src);
730 g_source_unref(iwp->src);
731 iwp->src = NULL;
732 }
733 g_source_destroy(&iwp->parent);
734}
735
Amit Shah26da70c2013-08-28 15:23:37 +0530736static void remove_fd_in_watch(CharDriverState *chr)
737{
738 if (chr->fd_in_tag) {
739 io_remove_watch_poll(chr->fd_in_tag);
740 chr->fd_in_tag = 0;
741 }
742}
743
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000744#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530745static GIOChannel *io_channel_from_fd(int fd)
746{
747 GIOChannel *chan;
748
749 if (fd == -1) {
750 return NULL;
751 }
752
753 chan = g_io_channel_unix_new(fd);
754
755 g_io_channel_set_encoding(chan, NULL, NULL);
756 g_io_channel_set_buffered(chan, FALSE);
757
758 return chan;
759}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000760#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530761
Anthony Liguori76a96442013-03-05 23:21:21 +0530762static GIOChannel *io_channel_from_socket(int fd)
763{
764 GIOChannel *chan;
765
766 if (fd == -1) {
767 return NULL;
768 }
769
770#ifdef _WIN32
771 chan = g_io_channel_win32_new_socket(fd);
772#else
773 chan = g_io_channel_unix_new(fd);
774#endif
775
776 g_io_channel_set_encoding(chan, NULL, NULL);
777 g_io_channel_set_buffered(chan, FALSE);
778
779 return chan;
780}
781
Anthony Liguori684a0962013-03-29 11:39:50 -0500782static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530783{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200784 size_t offset = 0;
785 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530786
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200787 while (offset < len && status == G_IO_STATUS_NORMAL) {
788 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500789
790 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530791 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500792 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530793 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500794
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200795 if (offset > 0) {
796 return offset;
797 }
798 switch (status) {
799 case G_IO_STATUS_NORMAL:
800 g_assert(len == 0);
801 return 0;
802 case G_IO_STATUS_AGAIN:
803 errno = EAGAIN;
804 return -1;
805 default:
806 break;
807 }
808 errno = EINVAL;
809 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530810}
Anthony Liguori96c63842013-03-05 23:21:18 +0530811
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000812#ifndef _WIN32
813
Anthony Liguoria29753f2013-03-05 23:21:19 +0530814typedef struct FDCharDriver {
815 CharDriverState *chr;
816 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000817 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530818 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000819} FDCharDriver;
820
aliguori6f97dba2008-10-31 18:49:55 +0000821static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
822{
823 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530824
Anthony Liguori684a0962013-03-29 11:39:50 -0500825 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530826}
827
828static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
829{
830 CharDriverState *chr = opaque;
831 FDCharDriver *s = chr->opaque;
832 int len;
833 uint8_t buf[READ_BUF_LEN];
834 GIOStatus status;
835 gsize bytes_read;
836
837 len = sizeof(buf);
838 if (len > s->max_size) {
839 len = s->max_size;
840 }
841 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200842 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530843 }
844
845 status = g_io_channel_read_chars(chan, (gchar *)buf,
846 len, &bytes_read, NULL);
847 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530848 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530849 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
850 return FALSE;
851 }
852 if (status == G_IO_STATUS_NORMAL) {
853 qemu_chr_be_write(chr, buf, bytes_read);
854 }
855
856 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000857}
858
859static int fd_chr_read_poll(void *opaque)
860{
861 CharDriverState *chr = opaque;
862 FDCharDriver *s = chr->opaque;
863
Anthony Liguori909cda12011-08-15 11:17:31 -0500864 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000865 return s->max_size;
866}
867
Anthony Liguori23673ca2013-03-05 23:21:23 +0530868static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
869{
870 FDCharDriver *s = chr->opaque;
871 return g_io_create_watch(s->fd_out, cond);
872}
873
aliguori6f97dba2008-10-31 18:49:55 +0000874static void fd_chr_update_read_handler(CharDriverState *chr)
875{
876 FDCharDriver *s = chr->opaque;
877
Amit Shah26da70c2013-08-28 15:23:37 +0530878 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530879 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530880 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
881 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000882 }
883}
884
885static void fd_chr_close(struct CharDriverState *chr)
886{
887 FDCharDriver *s = chr->opaque;
888
Amit Shah26da70c2013-08-28 15:23:37 +0530889 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530890 if (s->fd_in) {
891 g_io_channel_unref(s->fd_in);
892 }
893 if (s->fd_out) {
894 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000895 }
896
Anthony Liguori7267c092011-08-20 22:09:37 -0500897 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100898 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000899}
900
901/* open a character device to a unix fd */
902static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
903{
904 CharDriverState *chr;
905 FDCharDriver *s;
906
Anthony Liguori7267c092011-08-20 22:09:37 -0500907 chr = g_malloc0(sizeof(CharDriverState));
908 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530909 s->fd_in = io_channel_from_fd(fd_in);
910 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530911 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530912 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000913 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530914 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000915 chr->chr_write = fd_chr_write;
916 chr->chr_update_read_handler = fd_chr_update_read_handler;
917 chr->chr_close = fd_chr_close;
918
aliguori6f97dba2008-10-31 18:49:55 +0000919 return chr;
920}
921
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100922static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000923{
924 int fd_in, fd_out;
925 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100926 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200927
928 if (filename == NULL) {
929 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100930 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200931 }
aliguori6f97dba2008-10-31 18:49:55 +0000932
933 snprintf(filename_in, 256, "%s.in", filename);
934 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100935 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
936 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000937 if (fd_in < 0 || fd_out < 0) {
938 if (fd_in >= 0)
939 close(fd_in);
940 if (fd_out >= 0)
941 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100942 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100943 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100944 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100945 }
aliguori6f97dba2008-10-31 18:49:55 +0000946 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100947 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000948}
949
aliguori6f97dba2008-10-31 18:49:55 +0000950/* init terminal so that we can grab keys */
951static struct termios oldtty;
952static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100953static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000954
955static void term_exit(void)
956{
957 tcsetattr (0, TCSANOW, &oldtty);
958 fcntl(0, F_SETFL, old_fd0_flags);
959}
960
Paolo Bonzinibb002512010-12-23 13:42:50 +0100961static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000962{
963 struct termios tty;
964
Paolo Bonzini03693642010-12-23 13:42:49 +0100965 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100966 if (!echo) {
967 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000968 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100969 tty.c_oflag |= OPOST;
970 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
971 tty.c_cflag &= ~(CSIZE|PARENB);
972 tty.c_cflag |= CS8;
973 tty.c_cc[VMIN] = 1;
974 tty.c_cc[VTIME] = 0;
975 }
Paolo Bonzinibb002512010-12-23 13:42:50 +0100976 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000977 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000978
979 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000980}
981
982static void qemu_chr_close_stdio(struct CharDriverState *chr)
983{
984 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000985 fd_chr_close(chr);
986}
987
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100988static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000989{
990 CharDriverState *chr;
991
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400992 if (is_daemonized()) {
993 error_report("cannot use stdio with -daemonize");
994 return NULL;
995 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530996 old_fd0_flags = fcntl(0, F_GETFL);
997 tcgetattr (0, &oldtty);
998 fcntl(0, F_SETFL, O_NONBLOCK);
999 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001000
aliguori6f97dba2008-10-31 18:49:55 +00001001 chr = qemu_chr_open_fd(0, 1);
1002 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001003 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001004 if (opts->has_signal) {
1005 stdio_allow_signal = opts->signal;
1006 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001007 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001008
Markus Armbruster1f514702012-02-07 15:09:08 +01001009 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001010}
1011
aliguori6f97dba2008-10-31 18:49:55 +00001012#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001013 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1014 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001015
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001016#define HAVE_CHARDEV_TTY 1
1017
aliguori6f97dba2008-10-31 18:49:55 +00001018typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301019 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001020 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001021 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301022 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001023} PtyCharDriver;
1024
1025static void pty_chr_update_read_handler(CharDriverState *chr);
1026static void pty_chr_state(CharDriverState *chr, int connected);
1027
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301028static gboolean pty_chr_timer(gpointer opaque)
1029{
1030 struct CharDriverState *chr = opaque;
1031 PtyCharDriver *s = chr->opaque;
1032
Hans de Goede79f20072013-04-25 13:53:02 +02001033 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001034 if (!s->connected) {
1035 /* Next poll ... */
1036 pty_chr_update_read_handler(chr);
1037 }
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) {
Amit Shah26da70c2013-08-28 15:23:37 +05301130 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001131 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001132 /* (re-)connect poll interval for idle guests: once per second.
1133 * We check more frequently in case the guests sends data to
1134 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301135 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001136 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001137 if (s->timer_tag) {
1138 g_source_remove(s->timer_tag);
1139 s->timer_tag = 0;
1140 }
1141 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001142 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001143 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001144 }
1145 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301146 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1147 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001148 }
aliguori6f97dba2008-10-31 18:49:55 +00001149 }
1150}
1151
aliguori6f97dba2008-10-31 18:49:55 +00001152static void pty_chr_close(struct CharDriverState *chr)
1153{
1154 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301155 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001156
Amit Shah26da70c2013-08-28 15:23:37 +05301157 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301158 fd = g_io_channel_unix_get_fd(s->fd);
1159 g_io_channel_unref(s->fd);
1160 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301161 if (s->timer_tag) {
1162 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001163 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301164 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001165 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001166 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001167}
1168
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001169static CharDriverState *qemu_chr_open_pty(const char *id,
1170 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001171{
1172 CharDriverState *chr;
1173 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001174 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001175 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001176
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001177 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1178 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001179 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001180 }
1181
aliguori6f97dba2008-10-31 18:49:55 +00001182 close(slave_fd);
1183
Markus Armbrustera4e26042011-11-11 10:40:05 +01001184 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001185
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001186 chr->filename = g_strdup_printf("pty:%s", pty_name);
1187 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001188 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001189
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001190 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001191 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001192
1193 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001194 chr->opaque = s;
1195 chr->chr_write = pty_chr_write;
1196 chr->chr_update_read_handler = pty_chr_update_read_handler;
1197 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301198 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001199 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001200
Anthony Liguori093d3a22013-03-05 23:21:20 +05301201 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301202 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001203
Markus Armbruster1f514702012-02-07 15:09:08 +01001204 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001205}
1206
1207static void tty_serial_init(int fd, int speed,
1208 int parity, int data_bits, int stop_bits)
1209{
1210 struct termios tty;
1211 speed_t spd;
1212
1213#if 0
1214 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1215 speed, parity, data_bits, stop_bits);
1216#endif
1217 tcgetattr (fd, &tty);
1218
Stefan Weil45eea132009-10-26 16:10:10 +01001219#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1220 speed = speed * 10 / 11;
1221 do {
1222 check_speed(50);
1223 check_speed(75);
1224 check_speed(110);
1225 check_speed(134);
1226 check_speed(150);
1227 check_speed(200);
1228 check_speed(300);
1229 check_speed(600);
1230 check_speed(1200);
1231 check_speed(1800);
1232 check_speed(2400);
1233 check_speed(4800);
1234 check_speed(9600);
1235 check_speed(19200);
1236 check_speed(38400);
1237 /* Non-Posix values follow. They may be unsupported on some systems. */
1238 check_speed(57600);
1239 check_speed(115200);
1240#ifdef B230400
1241 check_speed(230400);
1242#endif
1243#ifdef B460800
1244 check_speed(460800);
1245#endif
1246#ifdef B500000
1247 check_speed(500000);
1248#endif
1249#ifdef B576000
1250 check_speed(576000);
1251#endif
1252#ifdef B921600
1253 check_speed(921600);
1254#endif
1255#ifdef B1000000
1256 check_speed(1000000);
1257#endif
1258#ifdef B1152000
1259 check_speed(1152000);
1260#endif
1261#ifdef B1500000
1262 check_speed(1500000);
1263#endif
1264#ifdef B2000000
1265 check_speed(2000000);
1266#endif
1267#ifdef B2500000
1268 check_speed(2500000);
1269#endif
1270#ifdef B3000000
1271 check_speed(3000000);
1272#endif
1273#ifdef B3500000
1274 check_speed(3500000);
1275#endif
1276#ifdef B4000000
1277 check_speed(4000000);
1278#endif
aliguori6f97dba2008-10-31 18:49:55 +00001279 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001280 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001281
1282 cfsetispeed(&tty, spd);
1283 cfsetospeed(&tty, spd);
1284
1285 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1286 |INLCR|IGNCR|ICRNL|IXON);
1287 tty.c_oflag |= OPOST;
1288 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1289 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1290 switch(data_bits) {
1291 default:
1292 case 8:
1293 tty.c_cflag |= CS8;
1294 break;
1295 case 7:
1296 tty.c_cflag |= CS7;
1297 break;
1298 case 6:
1299 tty.c_cflag |= CS6;
1300 break;
1301 case 5:
1302 tty.c_cflag |= CS5;
1303 break;
1304 }
1305 switch(parity) {
1306 default:
1307 case 'N':
1308 break;
1309 case 'E':
1310 tty.c_cflag |= PARENB;
1311 break;
1312 case 'O':
1313 tty.c_cflag |= PARENB | PARODD;
1314 break;
1315 }
1316 if (stop_bits == 2)
1317 tty.c_cflag |= CSTOPB;
1318
1319 tcsetattr (fd, TCSANOW, &tty);
1320}
1321
1322static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1323{
1324 FDCharDriver *s = chr->opaque;
1325
1326 switch(cmd) {
1327 case CHR_IOCTL_SERIAL_SET_PARAMS:
1328 {
1329 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301330 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1331 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001332 ssp->data_bits, ssp->stop_bits);
1333 }
1334 break;
1335 case CHR_IOCTL_SERIAL_SET_BREAK:
1336 {
1337 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301338 if (enable) {
1339 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1340 }
aliguori6f97dba2008-10-31 18:49:55 +00001341 }
1342 break;
1343 case CHR_IOCTL_SERIAL_GET_TIOCM:
1344 {
1345 int sarg = 0;
1346 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301347 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001348 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001349 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001350 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001351 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001352 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001353 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001354 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001355 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001356 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001357 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001358 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001359 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001360 *targ |= CHR_TIOCM_RTS;
1361 }
1362 break;
1363 case CHR_IOCTL_SERIAL_SET_TIOCM:
1364 {
1365 int sarg = *(int *)arg;
1366 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301367 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001368 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1369 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1370 if (sarg & CHR_TIOCM_CTS)
1371 targ |= TIOCM_CTS;
1372 if (sarg & CHR_TIOCM_CAR)
1373 targ |= TIOCM_CAR;
1374 if (sarg & CHR_TIOCM_DSR)
1375 targ |= TIOCM_DSR;
1376 if (sarg & CHR_TIOCM_RI)
1377 targ |= TIOCM_RI;
1378 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001379 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001380 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001381 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301382 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001383 }
1384 break;
1385 default:
1386 return -ENOTSUP;
1387 }
1388 return 0;
1389}
1390
David Ahern4266a132010-02-10 18:27:17 -07001391static void qemu_chr_close_tty(CharDriverState *chr)
1392{
1393 FDCharDriver *s = chr->opaque;
1394 int fd = -1;
1395
1396 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301397 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001398 }
1399
1400 fd_chr_close(chr);
1401
1402 if (fd >= 0) {
1403 close(fd);
1404 }
1405}
1406
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001407static CharDriverState *qemu_chr_open_tty_fd(int fd)
1408{
1409 CharDriverState *chr;
1410
1411 tty_serial_init(fd, 115200, 'N', 8, 1);
1412 chr = qemu_chr_open_fd(fd, fd);
1413 chr->chr_ioctl = tty_serial_ioctl;
1414 chr->chr_close = qemu_chr_close_tty;
1415 return chr;
1416}
aliguori6f97dba2008-10-31 18:49:55 +00001417#endif /* __linux__ || __sun__ */
1418
1419#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001420
1421#define HAVE_CHARDEV_PARPORT 1
1422
aliguori6f97dba2008-10-31 18:49:55 +00001423typedef struct {
1424 int fd;
1425 int mode;
1426} ParallelCharDriver;
1427
1428static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1429{
1430 if (s->mode != mode) {
1431 int m = mode;
1432 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1433 return 0;
1434 s->mode = mode;
1435 }
1436 return 1;
1437}
1438
1439static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1440{
1441 ParallelCharDriver *drv = chr->opaque;
1442 int fd = drv->fd;
1443 uint8_t b;
1444
1445 switch(cmd) {
1446 case CHR_IOCTL_PP_READ_DATA:
1447 if (ioctl(fd, PPRDATA, &b) < 0)
1448 return -ENOTSUP;
1449 *(uint8_t *)arg = b;
1450 break;
1451 case CHR_IOCTL_PP_WRITE_DATA:
1452 b = *(uint8_t *)arg;
1453 if (ioctl(fd, PPWDATA, &b) < 0)
1454 return -ENOTSUP;
1455 break;
1456 case CHR_IOCTL_PP_READ_CONTROL:
1457 if (ioctl(fd, PPRCONTROL, &b) < 0)
1458 return -ENOTSUP;
1459 /* Linux gives only the lowest bits, and no way to know data
1460 direction! For better compatibility set the fixed upper
1461 bits. */
1462 *(uint8_t *)arg = b | 0xc0;
1463 break;
1464 case CHR_IOCTL_PP_WRITE_CONTROL:
1465 b = *(uint8_t *)arg;
1466 if (ioctl(fd, PPWCONTROL, &b) < 0)
1467 return -ENOTSUP;
1468 break;
1469 case CHR_IOCTL_PP_READ_STATUS:
1470 if (ioctl(fd, PPRSTATUS, &b) < 0)
1471 return -ENOTSUP;
1472 *(uint8_t *)arg = b;
1473 break;
1474 case CHR_IOCTL_PP_DATA_DIR:
1475 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1476 return -ENOTSUP;
1477 break;
1478 case CHR_IOCTL_PP_EPP_READ_ADDR:
1479 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1480 struct ParallelIOArg *parg = arg;
1481 int n = read(fd, parg->buffer, parg->count);
1482 if (n != parg->count) {
1483 return -EIO;
1484 }
1485 }
1486 break;
1487 case CHR_IOCTL_PP_EPP_READ:
1488 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1489 struct ParallelIOArg *parg = arg;
1490 int n = read(fd, parg->buffer, parg->count);
1491 if (n != parg->count) {
1492 return -EIO;
1493 }
1494 }
1495 break;
1496 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1497 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1498 struct ParallelIOArg *parg = arg;
1499 int n = write(fd, parg->buffer, parg->count);
1500 if (n != parg->count) {
1501 return -EIO;
1502 }
1503 }
1504 break;
1505 case CHR_IOCTL_PP_EPP_WRITE:
1506 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1507 struct ParallelIOArg *parg = arg;
1508 int n = write(fd, parg->buffer, parg->count);
1509 if (n != parg->count) {
1510 return -EIO;
1511 }
1512 }
1513 break;
1514 default:
1515 return -ENOTSUP;
1516 }
1517 return 0;
1518}
1519
1520static void pp_close(CharDriverState *chr)
1521{
1522 ParallelCharDriver *drv = chr->opaque;
1523 int fd = drv->fd;
1524
1525 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1526 ioctl(fd, PPRELEASE);
1527 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001528 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001529 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001530}
1531
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001532static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001533{
1534 CharDriverState *chr;
1535 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001536
1537 if (ioctl(fd, PPCLAIM) < 0) {
1538 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001539 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001540 }
1541
Anthony Liguori7267c092011-08-20 22:09:37 -05001542 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001543 drv->fd = fd;
1544 drv->mode = IEEE1284_MODE_COMPAT;
1545
Anthony Liguori7267c092011-08-20 22:09:37 -05001546 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001547 chr->chr_write = null_chr_write;
1548 chr->chr_ioctl = pp_ioctl;
1549 chr->chr_close = pp_close;
1550 chr->opaque = drv;
1551
Markus Armbruster1f514702012-02-07 15:09:08 +01001552 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001553}
1554#endif /* __linux__ */
1555
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001556#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001557
1558#define HAVE_CHARDEV_PARPORT 1
1559
blueswir16972f932008-11-22 20:49:12 +00001560static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1561{
Stefan Weile0efb992011-02-23 19:09:16 +01001562 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001563 uint8_t b;
1564
1565 switch(cmd) {
1566 case CHR_IOCTL_PP_READ_DATA:
1567 if (ioctl(fd, PPIGDATA, &b) < 0)
1568 return -ENOTSUP;
1569 *(uint8_t *)arg = b;
1570 break;
1571 case CHR_IOCTL_PP_WRITE_DATA:
1572 b = *(uint8_t *)arg;
1573 if (ioctl(fd, PPISDATA, &b) < 0)
1574 return -ENOTSUP;
1575 break;
1576 case CHR_IOCTL_PP_READ_CONTROL:
1577 if (ioctl(fd, PPIGCTRL, &b) < 0)
1578 return -ENOTSUP;
1579 *(uint8_t *)arg = b;
1580 break;
1581 case CHR_IOCTL_PP_WRITE_CONTROL:
1582 b = *(uint8_t *)arg;
1583 if (ioctl(fd, PPISCTRL, &b) < 0)
1584 return -ENOTSUP;
1585 break;
1586 case CHR_IOCTL_PP_READ_STATUS:
1587 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1588 return -ENOTSUP;
1589 *(uint8_t *)arg = b;
1590 break;
1591 default:
1592 return -ENOTSUP;
1593 }
1594 return 0;
1595}
1596
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001597static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001598{
1599 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001600
Anthony Liguori7267c092011-08-20 22:09:37 -05001601 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001602 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001603 chr->chr_write = null_chr_write;
1604 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001605 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001606 return chr;
blueswir16972f932008-11-22 20:49:12 +00001607}
1608#endif
1609
aliguori6f97dba2008-10-31 18:49:55 +00001610#else /* _WIN32 */
1611
1612typedef struct {
1613 int max_size;
1614 HANDLE hcom, hrecv, hsend;
1615 OVERLAPPED orecv, osend;
1616 BOOL fpipe;
1617 DWORD len;
1618} WinCharState;
1619
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001620typedef struct {
1621 HANDLE hStdIn;
1622 HANDLE hInputReadyEvent;
1623 HANDLE hInputDoneEvent;
1624 HANDLE hInputThread;
1625 uint8_t win_stdio_buf;
1626} WinStdioCharState;
1627
aliguori6f97dba2008-10-31 18:49:55 +00001628#define NSENDBUF 2048
1629#define NRECVBUF 2048
1630#define MAXCONNECT 1
1631#define NTIMEOUT 5000
1632
1633static int win_chr_poll(void *opaque);
1634static int win_chr_pipe_poll(void *opaque);
1635
1636static void win_chr_close(CharDriverState *chr)
1637{
1638 WinCharState *s = chr->opaque;
1639
1640 if (s->hsend) {
1641 CloseHandle(s->hsend);
1642 s->hsend = NULL;
1643 }
1644 if (s->hrecv) {
1645 CloseHandle(s->hrecv);
1646 s->hrecv = NULL;
1647 }
1648 if (s->hcom) {
1649 CloseHandle(s->hcom);
1650 s->hcom = NULL;
1651 }
1652 if (s->fpipe)
1653 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1654 else
1655 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301656
Hans de Goedea425d232011-11-19 10:22:43 +01001657 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001658}
1659
1660static int win_chr_init(CharDriverState *chr, const char *filename)
1661{
1662 WinCharState *s = chr->opaque;
1663 COMMCONFIG comcfg;
1664 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1665 COMSTAT comstat;
1666 DWORD size;
1667 DWORD err;
1668
1669 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1670 if (!s->hsend) {
1671 fprintf(stderr, "Failed CreateEvent\n");
1672 goto fail;
1673 }
1674 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1675 if (!s->hrecv) {
1676 fprintf(stderr, "Failed CreateEvent\n");
1677 goto fail;
1678 }
1679
1680 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1681 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1682 if (s->hcom == INVALID_HANDLE_VALUE) {
1683 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1684 s->hcom = NULL;
1685 goto fail;
1686 }
1687
1688 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1689 fprintf(stderr, "Failed SetupComm\n");
1690 goto fail;
1691 }
1692
1693 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1694 size = sizeof(COMMCONFIG);
1695 GetDefaultCommConfig(filename, &comcfg, &size);
1696 comcfg.dcb.DCBlength = sizeof(DCB);
1697 CommConfigDialog(filename, NULL, &comcfg);
1698
1699 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1700 fprintf(stderr, "Failed SetCommState\n");
1701 goto fail;
1702 }
1703
1704 if (!SetCommMask(s->hcom, EV_ERR)) {
1705 fprintf(stderr, "Failed SetCommMask\n");
1706 goto fail;
1707 }
1708
1709 cto.ReadIntervalTimeout = MAXDWORD;
1710 if (!SetCommTimeouts(s->hcom, &cto)) {
1711 fprintf(stderr, "Failed SetCommTimeouts\n");
1712 goto fail;
1713 }
1714
1715 if (!ClearCommError(s->hcom, &err, &comstat)) {
1716 fprintf(stderr, "Failed ClearCommError\n");
1717 goto fail;
1718 }
1719 qemu_add_polling_cb(win_chr_poll, chr);
1720 return 0;
1721
1722 fail:
1723 win_chr_close(chr);
1724 return -1;
1725}
1726
1727static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1728{
1729 WinCharState *s = chr->opaque;
1730 DWORD len, ret, size, err;
1731
1732 len = len1;
1733 ZeroMemory(&s->osend, sizeof(s->osend));
1734 s->osend.hEvent = s->hsend;
1735 while (len > 0) {
1736 if (s->hsend)
1737 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1738 else
1739 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1740 if (!ret) {
1741 err = GetLastError();
1742 if (err == ERROR_IO_PENDING) {
1743 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1744 if (ret) {
1745 buf += size;
1746 len -= size;
1747 } else {
1748 break;
1749 }
1750 } else {
1751 break;
1752 }
1753 } else {
1754 buf += size;
1755 len -= size;
1756 }
1757 }
1758 return len1 - len;
1759}
1760
1761static int win_chr_read_poll(CharDriverState *chr)
1762{
1763 WinCharState *s = chr->opaque;
1764
Anthony Liguori909cda12011-08-15 11:17:31 -05001765 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001766 return s->max_size;
1767}
1768
1769static void win_chr_readfile(CharDriverState *chr)
1770{
1771 WinCharState *s = chr->opaque;
1772 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301773 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001774 DWORD size;
1775
1776 ZeroMemory(&s->orecv, sizeof(s->orecv));
1777 s->orecv.hEvent = s->hrecv;
1778 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1779 if (!ret) {
1780 err = GetLastError();
1781 if (err == ERROR_IO_PENDING) {
1782 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1783 }
1784 }
1785
1786 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001787 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001788 }
1789}
1790
1791static void win_chr_read(CharDriverState *chr)
1792{
1793 WinCharState *s = chr->opaque;
1794
1795 if (s->len > s->max_size)
1796 s->len = s->max_size;
1797 if (s->len == 0)
1798 return;
1799
1800 win_chr_readfile(chr);
1801}
1802
1803static int win_chr_poll(void *opaque)
1804{
1805 CharDriverState *chr = opaque;
1806 WinCharState *s = chr->opaque;
1807 COMSTAT status;
1808 DWORD comerr;
1809
1810 ClearCommError(s->hcom, &comerr, &status);
1811 if (status.cbInQue > 0) {
1812 s->len = status.cbInQue;
1813 win_chr_read_poll(chr);
1814 win_chr_read(chr);
1815 return 1;
1816 }
1817 return 0;
1818}
1819
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001820static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001821{
1822 CharDriverState *chr;
1823 WinCharState *s;
1824
Anthony Liguori7267c092011-08-20 22:09:37 -05001825 chr = g_malloc0(sizeof(CharDriverState));
1826 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001827 chr->opaque = s;
1828 chr->chr_write = win_chr_write;
1829 chr->chr_close = win_chr_close;
1830
1831 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001832 g_free(s);
1833 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001834 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001835 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001836 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001837}
1838
1839static int win_chr_pipe_poll(void *opaque)
1840{
1841 CharDriverState *chr = opaque;
1842 WinCharState *s = chr->opaque;
1843 DWORD size;
1844
1845 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1846 if (size > 0) {
1847 s->len = size;
1848 win_chr_read_poll(chr);
1849 win_chr_read(chr);
1850 return 1;
1851 }
1852 return 0;
1853}
1854
1855static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1856{
1857 WinCharState *s = chr->opaque;
1858 OVERLAPPED ov;
1859 int ret;
1860 DWORD size;
1861 char openname[256];
1862
1863 s->fpipe = TRUE;
1864
1865 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1866 if (!s->hsend) {
1867 fprintf(stderr, "Failed CreateEvent\n");
1868 goto fail;
1869 }
1870 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1871 if (!s->hrecv) {
1872 fprintf(stderr, "Failed CreateEvent\n");
1873 goto fail;
1874 }
1875
1876 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1877 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1878 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1879 PIPE_WAIT,
1880 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1881 if (s->hcom == INVALID_HANDLE_VALUE) {
1882 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1883 s->hcom = NULL;
1884 goto fail;
1885 }
1886
1887 ZeroMemory(&ov, sizeof(ov));
1888 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1889 ret = ConnectNamedPipe(s->hcom, &ov);
1890 if (ret) {
1891 fprintf(stderr, "Failed ConnectNamedPipe\n");
1892 goto fail;
1893 }
1894
1895 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1896 if (!ret) {
1897 fprintf(stderr, "Failed GetOverlappedResult\n");
1898 if (ov.hEvent) {
1899 CloseHandle(ov.hEvent);
1900 ov.hEvent = NULL;
1901 }
1902 goto fail;
1903 }
1904
1905 if (ov.hEvent) {
1906 CloseHandle(ov.hEvent);
1907 ov.hEvent = NULL;
1908 }
1909 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1910 return 0;
1911
1912 fail:
1913 win_chr_close(chr);
1914 return -1;
1915}
1916
1917
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001918static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001919{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001920 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001921 CharDriverState *chr;
1922 WinCharState *s;
1923
Anthony Liguori7267c092011-08-20 22:09:37 -05001924 chr = g_malloc0(sizeof(CharDriverState));
1925 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001926 chr->opaque = s;
1927 chr->chr_write = win_chr_write;
1928 chr->chr_close = win_chr_close;
1929
1930 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001931 g_free(s);
1932 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001933 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001934 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001935 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001936}
1937
Markus Armbruster1f514702012-02-07 15:09:08 +01001938static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001939{
1940 CharDriverState *chr;
1941 WinCharState *s;
1942
Anthony Liguori7267c092011-08-20 22:09:37 -05001943 chr = g_malloc0(sizeof(CharDriverState));
1944 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001945 s->hcom = fd_out;
1946 chr->opaque = s;
1947 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001948 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001949}
1950
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001951static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001952{
Markus Armbruster1f514702012-02-07 15:09:08 +01001953 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001954}
1955
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001956static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1957{
1958 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1959 DWORD dwSize;
1960 int len1;
1961
1962 len1 = len;
1963
1964 while (len1 > 0) {
1965 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1966 break;
1967 }
1968 buf += dwSize;
1969 len1 -= dwSize;
1970 }
1971
1972 return len - len1;
1973}
1974
1975static void win_stdio_wait_func(void *opaque)
1976{
1977 CharDriverState *chr = opaque;
1978 WinStdioCharState *stdio = chr->opaque;
1979 INPUT_RECORD buf[4];
1980 int ret;
1981 DWORD dwSize;
1982 int i;
1983
Stefan Weildff74242013-12-07 14:48:04 +01001984 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001985
1986 if (!ret) {
1987 /* Avoid error storm */
1988 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1989 return;
1990 }
1991
1992 for (i = 0; i < dwSize; i++) {
1993 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1994
1995 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1996 int j;
1997 if (kev->uChar.AsciiChar != 0) {
1998 for (j = 0; j < kev->wRepeatCount; j++) {
1999 if (qemu_chr_be_can_write(chr)) {
2000 uint8_t c = kev->uChar.AsciiChar;
2001 qemu_chr_be_write(chr, &c, 1);
2002 }
2003 }
2004 }
2005 }
2006 }
2007}
2008
2009static DWORD WINAPI win_stdio_thread(LPVOID param)
2010{
2011 CharDriverState *chr = param;
2012 WinStdioCharState *stdio = chr->opaque;
2013 int ret;
2014 DWORD dwSize;
2015
2016 while (1) {
2017
2018 /* Wait for one byte */
2019 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2020
2021 /* Exit in case of error, continue if nothing read */
2022 if (!ret) {
2023 break;
2024 }
2025 if (!dwSize) {
2026 continue;
2027 }
2028
2029 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2030 if (stdio->win_stdio_buf == '\r') {
2031 continue;
2032 }
2033
2034 /* Signal the main thread and wait until the byte was eaten */
2035 if (!SetEvent(stdio->hInputReadyEvent)) {
2036 break;
2037 }
2038 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2039 != WAIT_OBJECT_0) {
2040 break;
2041 }
2042 }
2043
2044 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2045 return 0;
2046}
2047
2048static void win_stdio_thread_wait_func(void *opaque)
2049{
2050 CharDriverState *chr = opaque;
2051 WinStdioCharState *stdio = chr->opaque;
2052
2053 if (qemu_chr_be_can_write(chr)) {
2054 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2055 }
2056
2057 SetEvent(stdio->hInputDoneEvent);
2058}
2059
2060static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2061{
2062 WinStdioCharState *stdio = chr->opaque;
2063 DWORD dwMode = 0;
2064
2065 GetConsoleMode(stdio->hStdIn, &dwMode);
2066
2067 if (echo) {
2068 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2069 } else {
2070 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2071 }
2072}
2073
2074static void win_stdio_close(CharDriverState *chr)
2075{
2076 WinStdioCharState *stdio = chr->opaque;
2077
2078 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2079 CloseHandle(stdio->hInputReadyEvent);
2080 }
2081 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2082 CloseHandle(stdio->hInputDoneEvent);
2083 }
2084 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2085 TerminateThread(stdio->hInputThread, 0);
2086 }
2087
2088 g_free(chr->opaque);
2089 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002090}
2091
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002092static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002093{
2094 CharDriverState *chr;
2095 WinStdioCharState *stdio;
2096 DWORD dwMode;
2097 int is_console = 0;
2098
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002099 chr = g_malloc0(sizeof(CharDriverState));
2100 stdio = g_malloc0(sizeof(WinStdioCharState));
2101
2102 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2103 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2104 fprintf(stderr, "cannot open stdio: invalid handle\n");
2105 exit(1);
2106 }
2107
2108 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2109
2110 chr->opaque = stdio;
2111 chr->chr_write = win_stdio_write;
2112 chr->chr_close = win_stdio_close;
2113
Anthony Liguoried7a1542013-03-05 23:21:17 +05302114 if (is_console) {
2115 if (qemu_add_wait_object(stdio->hStdIn,
2116 win_stdio_wait_func, chr)) {
2117 fprintf(stderr, "qemu_add_wait_object: failed\n");
2118 }
2119 } else {
2120 DWORD dwId;
2121
2122 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2123 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2124 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2125 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002126
Anthony Liguoried7a1542013-03-05 23:21:17 +05302127 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2128 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2129 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2130 fprintf(stderr, "cannot create stdio thread or event\n");
2131 exit(1);
2132 }
2133 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2134 win_stdio_thread_wait_func, chr)) {
2135 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002136 }
2137 }
2138
2139 dwMode |= ENABLE_LINE_INPUT;
2140
Anthony Liguoried7a1542013-03-05 23:21:17 +05302141 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002142 /* set the terminal in raw mode */
2143 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2144 dwMode |= ENABLE_PROCESSED_INPUT;
2145 }
2146
2147 SetConsoleMode(stdio->hStdIn, dwMode);
2148
2149 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2150 qemu_chr_fe_set_echo(chr, false);
2151
Markus Armbruster1f514702012-02-07 15:09:08 +01002152 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002153}
aliguori6f97dba2008-10-31 18:49:55 +00002154#endif /* !_WIN32 */
2155
Anthony Liguori5ab82112013-03-05 23:21:31 +05302156
aliguori6f97dba2008-10-31 18:49:55 +00002157/***********************************************************/
2158/* UDP Net console */
2159
2160typedef struct {
2161 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302162 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302163 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002164 int bufcnt;
2165 int bufptr;
2166 int max_size;
2167} NetCharDriver;
2168
2169static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2170{
2171 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302172 gsize bytes_written;
2173 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002174
Anthony Liguori76a96442013-03-05 23:21:21 +05302175 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2176 if (status == G_IO_STATUS_EOF) {
2177 return 0;
2178 } else if (status != G_IO_STATUS_NORMAL) {
2179 return -1;
2180 }
2181
2182 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002183}
2184
2185static int udp_chr_read_poll(void *opaque)
2186{
2187 CharDriverState *chr = opaque;
2188 NetCharDriver *s = chr->opaque;
2189
Anthony Liguori909cda12011-08-15 11:17:31 -05002190 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002191
2192 /* If there were any stray characters in the queue process them
2193 * first
2194 */
2195 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002196 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002197 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002198 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002199 }
2200 return s->max_size;
2201}
2202
Anthony Liguori76a96442013-03-05 23:21:21 +05302203static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002204{
2205 CharDriverState *chr = opaque;
2206 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302207 gsize bytes_read = 0;
2208 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002209
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002210 if (s->max_size == 0) {
2211 return TRUE;
2212 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302213 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2214 &bytes_read, NULL);
2215 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002216 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302217 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302218 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302219 return FALSE;
2220 }
aliguori6f97dba2008-10-31 18:49:55 +00002221
2222 s->bufptr = 0;
2223 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002224 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002225 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002226 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002227 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302228
2229 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002230}
2231
2232static void udp_chr_update_read_handler(CharDriverState *chr)
2233{
2234 NetCharDriver *s = chr->opaque;
2235
Amit Shah26da70c2013-08-28 15:23:37 +05302236 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302237 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302238 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2239 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002240 }
2241}
2242
aliguori819f56b2009-03-28 17:58:14 +00002243static void udp_chr_close(CharDriverState *chr)
2244{
2245 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302246
2247 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302248 if (s->chan) {
2249 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002250 closesocket(s->fd);
2251 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002252 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002253 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002254}
2255
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002256static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002257{
2258 CharDriverState *chr = NULL;
2259 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002260
Anthony Liguori7267c092011-08-20 22:09:37 -05002261 chr = g_malloc0(sizeof(CharDriverState));
2262 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002263
aliguori6f97dba2008-10-31 18:49:55 +00002264 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302265 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002266 s->bufcnt = 0;
2267 s->bufptr = 0;
2268 chr->opaque = s;
2269 chr->chr_write = udp_chr_write;
2270 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002271 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002272 /* be isn't opened until we get a connection */
2273 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002274 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002275}
aliguori6f97dba2008-10-31 18:49:55 +00002276
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002277static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2278{
2279 Error *local_err = NULL;
2280 int fd = -1;
2281
2282 fd = inet_dgram_opts(opts, &local_err);
2283 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002284 qerror_report_err(local_err);
2285 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002286 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002287 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002288 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002289}
2290
2291/***********************************************************/
2292/* TCP Net console */
2293
2294typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302295
2296 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302297 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002298 int fd, listen_fd;
2299 int connected;
2300 int max_size;
2301 int do_telnetopt;
2302 int do_nodelay;
2303 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002304 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002305} TCPCharDriver;
2306
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302307static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002308
2309static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2310{
2311 TCPCharDriver *s = chr->opaque;
2312 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002313 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002314 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002315 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002316 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002317 }
2318}
2319
2320static int tcp_chr_read_poll(void *opaque)
2321{
2322 CharDriverState *chr = opaque;
2323 TCPCharDriver *s = chr->opaque;
2324 if (!s->connected)
2325 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002326 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002327 return s->max_size;
2328}
2329
2330#define IAC 255
2331#define IAC_BREAK 243
2332static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2333 TCPCharDriver *s,
2334 uint8_t *buf, int *size)
2335{
2336 /* Handle any telnet client's basic IAC options to satisfy char by
2337 * char mode with no echo. All IAC options will be removed from
2338 * the buf and the do_telnetopt variable will be used to track the
2339 * state of the width of the IAC information.
2340 *
2341 * IAC commands come in sets of 3 bytes with the exception of the
2342 * "IAC BREAK" command and the double IAC.
2343 */
2344
2345 int i;
2346 int j = 0;
2347
2348 for (i = 0; i < *size; i++) {
2349 if (s->do_telnetopt > 1) {
2350 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2351 /* Double IAC means send an IAC */
2352 if (j != i)
2353 buf[j] = buf[i];
2354 j++;
2355 s->do_telnetopt = 1;
2356 } else {
2357 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2358 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002359 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002360 s->do_telnetopt++;
2361 }
2362 s->do_telnetopt++;
2363 }
2364 if (s->do_telnetopt >= 4) {
2365 s->do_telnetopt = 1;
2366 }
2367 } else {
2368 if ((unsigned char)buf[i] == IAC) {
2369 s->do_telnetopt = 2;
2370 } else {
2371 if (j != i)
2372 buf[j] = buf[i];
2373 j++;
2374 }
2375 }
2376 }
2377 *size = j;
2378}
2379
Mark McLoughlin7d174052009-07-22 09:11:39 +01002380static int tcp_get_msgfd(CharDriverState *chr)
2381{
2382 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002383 int fd = s->msgfd;
2384 s->msgfd = -1;
2385 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002386}
2387
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002388#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002389static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2390{
2391 TCPCharDriver *s = chr->opaque;
2392 struct cmsghdr *cmsg;
2393
2394 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2395 int fd;
2396
2397 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2398 cmsg->cmsg_level != SOL_SOCKET ||
2399 cmsg->cmsg_type != SCM_RIGHTS)
2400 continue;
2401
2402 fd = *((int *)CMSG_DATA(cmsg));
2403 if (fd < 0)
2404 continue;
2405
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002406 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2407 qemu_set_block(fd);
2408
Corey Bryant06138652012-08-14 16:43:42 -04002409#ifndef MSG_CMSG_CLOEXEC
2410 qemu_set_cloexec(fd);
2411#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002412 if (s->msgfd != -1)
2413 close(s->msgfd);
2414 s->msgfd = fd;
2415 }
2416}
2417
Mark McLoughlin9977c892009-07-22 09:11:38 +01002418static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2419{
2420 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002421 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002422 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002423 union {
2424 struct cmsghdr cmsg;
2425 char control[CMSG_SPACE(sizeof(int))];
2426 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002427 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002428 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002429
2430 iov[0].iov_base = buf;
2431 iov[0].iov_len = len;
2432
2433 msg.msg_iov = iov;
2434 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002435 msg.msg_control = &msg_control;
2436 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002437
Corey Bryant06138652012-08-14 16:43:42 -04002438#ifdef MSG_CMSG_CLOEXEC
2439 flags |= MSG_CMSG_CLOEXEC;
2440#endif
2441 ret = recvmsg(s->fd, &msg, flags);
2442 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002443 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002444 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002445
2446 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002447}
2448#else
2449static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2450{
2451 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002452 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002453}
2454#endif
2455
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302456static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2457{
2458 TCPCharDriver *s = chr->opaque;
2459 return g_io_create_watch(s->chan, cond);
2460}
2461
Peter Maydell9355ad32014-06-16 18:36:39 +01002462static int tcp_chr_del_client(CharDriverState *chr)
2463{
2464 TCPCharDriver *s = chr->opaque;
2465
2466 if (!s->connected) {
2467 return -1;
2468 }
2469
2470 s->connected = 0;
2471 if (s->listen_chan) {
2472 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2473 tcp_chr_accept, chr);
2474 }
2475 remove_fd_in_watch(chr);
2476 g_io_channel_unref(s->chan);
2477 s->chan = NULL;
2478 closesocket(s->fd);
2479 s->fd = -1;
2480 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2481 return 0;
2482}
2483
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302484static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002485{
2486 CharDriverState *chr = opaque;
2487 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302488 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002489 int len, size;
2490
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302491 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002492 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302493 }
aliguori6f97dba2008-10-31 18:49:55 +00002494 len = sizeof(buf);
2495 if (len > s->max_size)
2496 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002497 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002498 if (size == 0) {
2499 /* connection closed */
Peter Maydell9355ad32014-06-16 18:36:39 +01002500 tcp_chr_del_client(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002501 } else if (size > 0) {
2502 if (s->do_telnetopt)
2503 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2504 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002505 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002506 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302507
2508 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002509}
2510
Blue Swirl68c18d12010-08-15 09:46:24 +00002511#ifndef _WIN32
2512CharDriverState *qemu_chr_open_eventfd(int eventfd)
2513{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002514 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002515}
Blue Swirl68c18d12010-08-15 09:46:24 +00002516#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002517
aliguori6f97dba2008-10-31 18:49:55 +00002518static void tcp_chr_connect(void *opaque)
2519{
2520 CharDriverState *chr = opaque;
2521 TCPCharDriver *s = chr->opaque;
2522
2523 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302524 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302525 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2526 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002527 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002528 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002529}
2530
Gal Hammerac1b84d2014-02-25 12:12:35 +02002531static void tcp_chr_update_read_handler(CharDriverState *chr)
2532{
2533 TCPCharDriver *s = chr->opaque;
2534
2535 remove_fd_in_watch(chr);
2536 if (s->chan) {
2537 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2538 tcp_chr_read, chr);
2539 }
2540}
2541
aliguori6f97dba2008-10-31 18:49:55 +00002542#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2543static void tcp_chr_telnet_init(int fd)
2544{
2545 char buf[3];
2546 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2547 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2548 send(fd, (char *)buf, 3, 0);
2549 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2550 send(fd, (char *)buf, 3, 0);
2551 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2552 send(fd, (char *)buf, 3, 0);
2553 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2554 send(fd, (char *)buf, 3, 0);
2555}
2556
Daniel P. Berrange13661082011-06-23 13:31:42 +01002557static int tcp_chr_add_client(CharDriverState *chr, int fd)
2558{
2559 TCPCharDriver *s = chr->opaque;
2560 if (s->fd != -1)
2561 return -1;
2562
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002563 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002564 if (s->do_nodelay)
2565 socket_set_nodelay(fd);
2566 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302567 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002568 if (s->listen_tag) {
2569 g_source_remove(s->listen_tag);
2570 s->listen_tag = 0;
2571 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002572 tcp_chr_connect(chr);
2573
2574 return 0;
2575}
2576
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302577static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002578{
2579 CharDriverState *chr = opaque;
2580 TCPCharDriver *s = chr->opaque;
2581 struct sockaddr_in saddr;
2582#ifndef _WIN32
2583 struct sockaddr_un uaddr;
2584#endif
2585 struct sockaddr *addr;
2586 socklen_t len;
2587 int fd;
2588
2589 for(;;) {
2590#ifndef _WIN32
2591 if (s->is_unix) {
2592 len = sizeof(uaddr);
2593 addr = (struct sockaddr *)&uaddr;
2594 } else
2595#endif
2596 {
2597 len = sizeof(saddr);
2598 addr = (struct sockaddr *)&saddr;
2599 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002600 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002601 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002602 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302603 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002604 } else if (fd >= 0) {
2605 if (s->do_telnetopt)
2606 tcp_chr_telnet_init(fd);
2607 break;
2608 }
2609 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002610 if (tcp_chr_add_client(chr, fd) < 0)
2611 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302612
2613 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002614}
2615
2616static void tcp_chr_close(CharDriverState *chr)
2617{
2618 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002619 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302620 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302621 if (s->chan) {
2622 g_io_channel_unref(s->chan);
2623 }
aliguori6f97dba2008-10-31 18:49:55 +00002624 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002625 }
2626 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302627 if (s->listen_tag) {
2628 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002629 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302630 }
2631 if (s->listen_chan) {
2632 g_io_channel_unref(s->listen_chan);
2633 }
aliguori6f97dba2008-10-31 18:49:55 +00002634 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002635 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002636 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002637 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002638}
2639
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002640static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2641 bool is_listen, bool is_telnet,
2642 bool is_waitconnect,
2643 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002644{
2645 CharDriverState *chr = NULL;
2646 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002647 char host[NI_MAXHOST], serv[NI_MAXSERV];
2648 const char *left = "", *right = "";
2649 struct sockaddr_storage ss;
2650 socklen_t ss_len = sizeof(ss);
2651
2652 memset(&ss, 0, ss_len);
2653 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002654 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002655 return NULL;
2656 }
2657
2658 chr = g_malloc0(sizeof(CharDriverState));
2659 s = g_malloc0(sizeof(TCPCharDriver));
2660
2661 s->connected = 0;
2662 s->fd = -1;
2663 s->listen_fd = -1;
2664 s->msgfd = -1;
2665
2666 chr->filename = g_malloc(256);
2667 switch (ss.ss_family) {
2668#ifndef _WIN32
2669 case AF_UNIX:
2670 s->is_unix = 1;
2671 snprintf(chr->filename, 256, "unix:%s%s",
2672 ((struct sockaddr_un *)(&ss))->sun_path,
2673 is_listen ? ",server" : "");
2674 break;
2675#endif
2676 case AF_INET6:
2677 left = "[";
2678 right = "]";
2679 /* fall through */
2680 case AF_INET:
2681 s->do_nodelay = do_nodelay;
2682 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2683 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002684 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002685 is_telnet ? "telnet" : "tcp",
2686 left, host, right, serv,
2687 is_listen ? ",server" : "");
2688 break;
2689 }
2690
2691 chr->opaque = s;
2692 chr->chr_write = tcp_chr_write;
2693 chr->chr_close = tcp_chr_close;
2694 chr->get_msgfd = tcp_get_msgfd;
2695 chr->chr_add_client = tcp_chr_add_client;
Peter Maydell9355ad32014-06-16 18:36:39 +01002696 chr->chr_del_client = tcp_chr_del_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302697 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002698 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002699 /* be isn't opened until we get a connection */
2700 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002701
2702 if (is_listen) {
2703 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302704 s->listen_chan = io_channel_from_socket(s->listen_fd);
2705 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002706 if (is_telnet) {
2707 s->do_telnetopt = 1;
2708 }
2709 } else {
2710 s->connected = 1;
2711 s->fd = fd;
2712 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302713 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002714 tcp_chr_connect(chr);
2715 }
2716
2717 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002718 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2719 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302720 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002721 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002722 }
2723 return chr;
2724}
2725
2726static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2727{
2728 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002729 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002730 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002731
liguange990a392013-06-18 11:45:35 +08002732 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2733 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2734 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2735 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2736 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002737
aliguorif07b6002008-11-11 20:54:09 +00002738 if (is_unix) {
2739 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002740 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002741 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002742 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002743 }
2744 } else {
2745 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002746 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002747 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002748 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002749 }
2750 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002751 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002752 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002753 }
aliguori6f97dba2008-10-31 18:49:55 +00002754
2755 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002756 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002757
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002758 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2759 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002760 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002761 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002762 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002763 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002764
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002765
aliguori6f97dba2008-10-31 18:49:55 +00002766 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002767 if (local_err) {
2768 qerror_report_err(local_err);
2769 error_free(local_err);
2770 }
2771 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002772 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002773 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002774 if (chr) {
2775 g_free(chr->opaque);
2776 g_free(chr);
2777 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002778 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002779}
2780
Lei Li51767e72013-01-25 00:03:19 +08002781/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002782/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002783
2784typedef struct {
2785 size_t size;
2786 size_t prod;
2787 size_t cons;
2788 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002789} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002790
Markus Armbruster3949e592013-02-06 21:27:24 +01002791static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002792{
Markus Armbruster3949e592013-02-06 21:27:24 +01002793 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002794
Markus Armbruster5c230102013-02-06 21:27:23 +01002795 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002796}
2797
Markus Armbruster3949e592013-02-06 21:27:24 +01002798static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002799{
Markus Armbruster3949e592013-02-06 21:27:24 +01002800 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002801 int i;
2802
2803 if (!buf || (len < 0)) {
2804 return -1;
2805 }
2806
2807 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002808 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2809 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002810 d->cons = d->prod - d->size;
2811 }
2812 }
2813
2814 return 0;
2815}
2816
Markus Armbruster3949e592013-02-06 21:27:24 +01002817static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002818{
Markus Armbruster3949e592013-02-06 21:27:24 +01002819 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002820 int i;
2821
Markus Armbruster5c230102013-02-06 21:27:23 +01002822 for (i = 0; i < len && d->cons != d->prod; i++) {
2823 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002824 }
2825
2826 return i;
2827}
2828
Markus Armbruster3949e592013-02-06 21:27:24 +01002829static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002830{
Markus Armbruster3949e592013-02-06 21:27:24 +01002831 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002832
2833 g_free(d->cbuf);
2834 g_free(d);
2835 chr->opaque = NULL;
2836}
2837
Markus Armbruster4f573782013-07-26 16:44:32 +02002838static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2839 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002840{
2841 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002842 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002843
2844 chr = g_malloc0(sizeof(CharDriverState));
2845 d = g_malloc(sizeof(*d));
2846
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002847 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002848
2849 /* The size must be power of 2 */
2850 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02002851 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002852 goto fail;
2853 }
2854
2855 d->prod = 0;
2856 d->cons = 0;
2857 d->cbuf = g_malloc0(d->size);
2858
2859 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002860 chr->chr_write = ringbuf_chr_write;
2861 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002862
2863 return chr;
2864
2865fail:
2866 g_free(d);
2867 g_free(chr);
2868 return NULL;
2869}
2870
Markus Armbruster3949e592013-02-06 21:27:24 +01002871static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002872{
Markus Armbruster3949e592013-02-06 21:27:24 +01002873 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002874}
2875
Markus Armbruster3949e592013-02-06 21:27:24 +01002876void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002877 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002878 Error **errp)
2879{
2880 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002881 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002882 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10002883 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002884
2885 chr = qemu_chr_find(device);
2886 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002887 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002888 return;
2889 }
2890
Markus Armbruster3949e592013-02-06 21:27:24 +01002891 if (!chr_is_ringbuf(chr)) {
2892 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002893 return;
2894 }
2895
Lei Li1f590cf2013-01-25 00:03:20 +08002896 if (has_format && (format == DATA_FORMAT_BASE64)) {
2897 write_data = g_base64_decode(data, &write_count);
2898 } else {
2899 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002900 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002901 }
2902
Markus Armbruster3949e592013-02-06 21:27:24 +01002903 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002904
Markus Armbruster13289fb2013-02-06 21:27:18 +01002905 if (write_data != (uint8_t *)data) {
2906 g_free((void *)write_data);
2907 }
2908
Lei Li1f590cf2013-01-25 00:03:20 +08002909 if (ret < 0) {
2910 error_setg(errp, "Failed to write to device %s", device);
2911 return;
2912 }
2913}
2914
Markus Armbruster3949e592013-02-06 21:27:24 +01002915char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002916 bool has_format, enum DataFormat format,
2917 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002918{
2919 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002920 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002921 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002922 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002923
2924 chr = qemu_chr_find(device);
2925 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002926 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002927 return NULL;
2928 }
2929
Markus Armbruster3949e592013-02-06 21:27:24 +01002930 if (!chr_is_ringbuf(chr)) {
2931 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002932 return NULL;
2933 }
2934
2935 if (size <= 0) {
2936 error_setg(errp, "size must be greater than zero");
2937 return NULL;
2938 }
2939
Markus Armbruster3949e592013-02-06 21:27:24 +01002940 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002941 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002942 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002943
Markus Armbruster3949e592013-02-06 21:27:24 +01002944 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002945
2946 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002947 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002948 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002949 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002950 /*
2951 * FIXME should read only complete, valid UTF-8 characters up
2952 * to @size bytes. Invalid sequences should be replaced by a
2953 * suitable replacement character. Except when (and only
2954 * when) ring buffer lost characters since last read, initial
2955 * continuation characters should be dropped.
2956 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002957 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002958 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002959 }
2960
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002961 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002962}
2963
Gerd Hoffmann33521632009-12-08 13:11:49 +01002964QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002965{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002966 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002967 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002968 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002969 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002970 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002971
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002972 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002973 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002974 qerror_report_err(local_err);
2975 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002976 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002977 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002978
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002979 if (strstart(filename, "mon:", &p)) {
2980 filename = p;
2981 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04002982 if (strcmp(filename, "stdio") == 0) {
2983 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2984 * but pass it to the guest. Handle this only for compat syntax,
2985 * for -chardev syntax we have special option for this.
2986 * This is what -nographic did, redirecting+muxing serial+monitor
2987 * to stdio causing Ctrl+C to be passed to guest. */
2988 qemu_opt_set(opts, "signal", "off");
2989 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002990 }
2991
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002992 if (strcmp(filename, "null") == 0 ||
2993 strcmp(filename, "pty") == 0 ||
2994 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002995 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002996 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002997 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002998 return opts;
2999 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003000 if (strstart(filename, "vc", &p)) {
3001 qemu_opt_set(opts, "backend", "vc");
3002 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003003 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003004 /* pixels */
3005 qemu_opt_set(opts, "width", width);
3006 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003007 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003008 /* chars */
3009 qemu_opt_set(opts, "cols", width);
3010 qemu_opt_set(opts, "rows", height);
3011 } else {
3012 goto fail;
3013 }
3014 }
3015 return opts;
3016 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003017 if (strcmp(filename, "con:") == 0) {
3018 qemu_opt_set(opts, "backend", "console");
3019 return opts;
3020 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003021 if (strstart(filename, "COM", NULL)) {
3022 qemu_opt_set(opts, "backend", "serial");
3023 qemu_opt_set(opts, "path", filename);
3024 return opts;
3025 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003026 if (strstart(filename, "file:", &p)) {
3027 qemu_opt_set(opts, "backend", "file");
3028 qemu_opt_set(opts, "path", p);
3029 return opts;
3030 }
3031 if (strstart(filename, "pipe:", &p)) {
3032 qemu_opt_set(opts, "backend", "pipe");
3033 qemu_opt_set(opts, "path", p);
3034 return opts;
3035 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003036 if (strstart(filename, "tcp:", &p) ||
3037 strstart(filename, "telnet:", &p)) {
3038 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3039 host[0] = 0;
3040 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3041 goto fail;
3042 }
3043 qemu_opt_set(opts, "backend", "socket");
3044 qemu_opt_set(opts, "host", host);
3045 qemu_opt_set(opts, "port", port);
3046 if (p[pos] == ',') {
3047 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3048 goto fail;
3049 }
3050 if (strstart(filename, "telnet:", &p))
3051 qemu_opt_set(opts, "telnet", "on");
3052 return opts;
3053 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003054 if (strstart(filename, "udp:", &p)) {
3055 qemu_opt_set(opts, "backend", "udp");
3056 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3057 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003058 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003059 goto fail;
3060 }
3061 }
3062 qemu_opt_set(opts, "host", host);
3063 qemu_opt_set(opts, "port", port);
3064 if (p[pos] == '@') {
3065 p += pos + 1;
3066 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3067 host[0] = 0;
3068 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003069 goto fail;
3070 }
3071 }
3072 qemu_opt_set(opts, "localaddr", host);
3073 qemu_opt_set(opts, "localport", port);
3074 }
3075 return opts;
3076 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003077 if (strstart(filename, "unix:", &p)) {
3078 qemu_opt_set(opts, "backend", "socket");
3079 if (qemu_opts_do_parse(opts, p, "path") != 0)
3080 goto fail;
3081 return opts;
3082 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003083 if (strstart(filename, "/dev/parport", NULL) ||
3084 strstart(filename, "/dev/ppi", NULL)) {
3085 qemu_opt_set(opts, "backend", "parport");
3086 qemu_opt_set(opts, "path", filename);
3087 return opts;
3088 }
3089 if (strstart(filename, "/dev/", NULL)) {
3090 qemu_opt_set(opts, "backend", "tty");
3091 qemu_opt_set(opts, "path", filename);
3092 return opts;
3093 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003094
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003095fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003096 qemu_opts_del(opts);
3097 return NULL;
3098}
3099
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003100static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3101 Error **errp)
3102{
3103 const char *path = qemu_opt_get(opts, "path");
3104
3105 if (path == NULL) {
3106 error_setg(errp, "chardev: file: no filename given");
3107 return;
3108 }
3109 backend->file = g_new0(ChardevFile, 1);
3110 backend->file->out = g_strdup(path);
3111}
3112
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003113static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3114 Error **errp)
3115{
3116 backend->stdio = g_new0(ChardevStdio, 1);
3117 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003118 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003119}
3120
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003121static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3122 Error **errp)
3123{
3124 const char *device = qemu_opt_get(opts, "path");
3125
3126 if (device == NULL) {
3127 error_setg(errp, "chardev: serial/tty: no device path given");
3128 return;
3129 }
3130 backend->serial = g_new0(ChardevHostdev, 1);
3131 backend->serial->device = g_strdup(device);
3132}
3133
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003134static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3135 Error **errp)
3136{
3137 const char *device = qemu_opt_get(opts, "path");
3138
3139 if (device == NULL) {
3140 error_setg(errp, "chardev: parallel: no device path given");
3141 return;
3142 }
3143 backend->parallel = g_new0(ChardevHostdev, 1);
3144 backend->parallel->device = g_strdup(device);
3145}
3146
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003147static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3148 Error **errp)
3149{
3150 const char *device = qemu_opt_get(opts, "path");
3151
3152 if (device == NULL) {
3153 error_setg(errp, "chardev: pipe: no device path given");
3154 return;
3155 }
3156 backend->pipe = g_new0(ChardevHostdev, 1);
3157 backend->pipe->device = g_strdup(device);
3158}
3159
Markus Armbruster4f573782013-07-26 16:44:32 +02003160static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3161 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003162{
3163 int val;
3164
Markus Armbruster3a1da422013-07-26 16:44:34 +02003165 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003166
Markus Armbruster0f953052013-06-27 16:22:07 +02003167 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003168 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003169 backend->ringbuf->has_size = true;
3170 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003171 }
3172}
3173
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003174static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3175 Error **errp)
3176{
3177 const char *chardev = qemu_opt_get(opts, "chardev");
3178
3179 if (chardev == NULL) {
3180 error_setg(errp, "chardev: mux: no chardev given");
3181 return;
3182 }
3183 backend->mux = g_new0(ChardevMux, 1);
3184 backend->mux->chardev = g_strdup(chardev);
3185}
3186
Anthony Liguorid654f342013-03-05 23:21:28 +05303187typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003188 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003189 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003190 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003191 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003192 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003193 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303194} CharDriver;
3195
3196static GSList *backends;
3197
3198void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3199{
3200 CharDriver *s;
3201
3202 s = g_malloc0(sizeof(*s));
3203 s->name = g_strdup(name);
3204 s->open = open;
3205
3206 backends = g_slist_append(backends, s);
3207}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003208
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003209void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003210 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3211{
3212 CharDriver *s;
3213
3214 s = g_malloc0(sizeof(*s));
3215 s->name = g_strdup(name);
3216 s->kind = kind;
3217 s->parse = parse;
3218
3219 backends = g_slist_append(backends, s);
3220}
3221
Anthony Liguorif69554b2011-08-15 11:17:37 -05003222CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003223 void (*init)(struct CharDriverState *s),
3224 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003225{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003226 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303227 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003228 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303229 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230
3231 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003232 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003233 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003234 }
3235
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003236 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003237 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003238 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003239 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003240 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303241 for (i = backends; i; i = i->next) {
3242 cd = i->data;
3243
3244 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003245 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303246 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003247 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303248 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003249 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003250 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003251 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003252 }
3253
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003254 if (!cd->open) {
3255 /* using new, qapi init */
3256 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3257 ChardevReturn *ret = NULL;
3258 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003259 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003260
3261 if (qemu_opt_get_bool(opts, "mux", 0)) {
3262 bid = g_strdup_printf("%s-base", id);
3263 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003264
3265 chr = NULL;
3266 backend->kind = cd->kind;
3267 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003268 cd->parse(opts, backend, &local_err);
3269 if (local_err) {
3270 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003271 goto qapi_out;
3272 }
3273 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003274 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003275 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003276 goto qapi_out;
3277 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003278
3279 if (bid) {
3280 qapi_free_ChardevBackend(backend);
3281 qapi_free_ChardevReturn(ret);
3282 backend = g_new0(ChardevBackend, 1);
3283 backend->mux = g_new0(ChardevMux, 1);
3284 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3285 backend->mux->chardev = g_strdup(bid);
3286 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003287 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003288 chr = qemu_chr_find(bid);
3289 qemu_chr_delete(chr);
3290 chr = NULL;
3291 goto qapi_out;
3292 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003293 }
3294
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003295 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003296 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003297
3298 qapi_out:
3299 qapi_free_ChardevBackend(backend);
3300 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003301 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003302 return chr;
3303 }
3304
Anthony Liguorid654f342013-03-05 23:21:28 +05303305 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003306 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003307 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003308 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003309 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003310 }
3311
3312 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003313 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003314 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003315 /* if we didn't create the chardev via qmp_chardev_add, we
3316 * need to send the OPENED event here
3317 */
3318 if (!chr->explicit_be_open) {
3319 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3320 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003321 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003322
3323 if (qemu_opt_get_bool(opts, "mux", 0)) {
3324 CharDriverState *base = chr;
3325 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003326 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003327 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3328 chr = qemu_chr_open_mux(base);
3329 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003330 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003331 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003332 } else {
3333 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003334 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003335 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003336 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003337 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003338
3339err:
3340 qemu_opts_del(opts);
3341 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003342}
3343
Anthony Liguori27143a42011-08-15 11:17:36 -05003344CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003345{
3346 const char *p;
3347 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003348 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003349 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003350
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003351 if (strstart(filename, "chardev:", &p)) {
3352 return qemu_chr_find(p);
3353 }
3354
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003355 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003356 if (!opts)
3357 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003358
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003359 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003360 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003361 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003362 error_free(err);
3363 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003364 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003365 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003366 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003367 }
3368 return chr;
3369}
3370
Anthony Liguori15f31512011-08-15 11:17:35 -05003371void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003372{
3373 if (chr->chr_set_echo) {
3374 chr->chr_set_echo(chr, echo);
3375 }
3376}
3377
Hans de Goede8e25daa2013-03-26 11:07:57 +01003378void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003379{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003380 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003381 return;
3382 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003383 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003384 if (chr->chr_set_fe_open) {
3385 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003386 }
3387}
3388
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003389void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3390{
3391 if (chr->chr_fe_event) {
3392 chr->chr_fe_event(chr, event);
3393 }
3394}
3395
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003396int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3397 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303398{
3399 GSource *src;
3400 guint tag;
3401
3402 if (s->chr_add_watch == NULL) {
3403 return -ENOSYS;
3404 }
3405
3406 src = s->chr_add_watch(s, cond);
3407 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3408 tag = g_source_attach(src, NULL);
3409 g_source_unref(src);
3410
3411 return tag;
3412}
3413
Hans de Goede44c473d2013-03-27 20:29:39 +01003414int qemu_chr_fe_claim(CharDriverState *s)
3415{
3416 if (s->avail_connections < 1) {
3417 return -1;
3418 }
3419 s->avail_connections--;
3420 return 0;
3421}
3422
3423void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3424{
3425 if (qemu_chr_fe_claim(s) != 0) {
3426 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3427 __func__, s->label);
3428 exit(1);
3429 }
3430}
3431
3432void qemu_chr_fe_release(CharDriverState *s)
3433{
3434 s->avail_connections++;
3435}
3436
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003437void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003438{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003439 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003440 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003441 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003442 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003443 g_free(chr->filename);
3444 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003445 if (chr->opts) {
3446 qemu_opts_del(chr->opts);
3447 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003448 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003449}
3450
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003451ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003452{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003453 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003454 CharDriverState *chr;
3455
Blue Swirl72cf2d42009-09-12 07:36:22 +00003456 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003457 ChardevInfoList *info = g_malloc0(sizeof(*info));
3458 info->value = g_malloc0(sizeof(*info->value));
3459 info->value->label = g_strdup(chr->label);
3460 info->value->filename = g_strdup(chr->filename);
3461
3462 info->next = chr_list;
3463 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003464 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003465
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003466 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003467}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003468
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003469ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3470{
3471 ChardevBackendInfoList *backend_list = NULL;
3472 CharDriver *c = NULL;
3473 GSList *i = NULL;
3474
3475 for (i = backends; i; i = i->next) {
3476 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3477 c = i->data;
3478 info->value = g_malloc0(sizeof(*info->value));
3479 info->value->name = g_strdup(c->name);
3480
3481 info->next = backend_list;
3482 backend_list = info;
3483 }
3484
3485 return backend_list;
3486}
3487
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003488CharDriverState *qemu_chr_find(const char *name)
3489{
3490 CharDriverState *chr;
3491
Blue Swirl72cf2d42009-09-12 07:36:22 +00003492 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003493 if (strcmp(chr->label, name) != 0)
3494 continue;
3495 return chr;
3496 }
3497 return NULL;
3498}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003499
3500/* Get a character (serial) device interface. */
3501CharDriverState *qemu_char_get_next_serial(void)
3502{
3503 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003504 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003505
3506 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003507
3508 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3509 chr = serial_hds[next_serial++];
3510 qemu_chr_fe_claim_no_fail(chr);
3511 return chr;
3512 }
3513 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003514}
3515
Paolo Bonzini4d454572012-11-26 16:03:42 +01003516QemuOptsList qemu_chardev_opts = {
3517 .name = "chardev",
3518 .implied_opt_name = "backend",
3519 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3520 .desc = {
3521 {
3522 .name = "backend",
3523 .type = QEMU_OPT_STRING,
3524 },{
3525 .name = "path",
3526 .type = QEMU_OPT_STRING,
3527 },{
3528 .name = "host",
3529 .type = QEMU_OPT_STRING,
3530 },{
3531 .name = "port",
3532 .type = QEMU_OPT_STRING,
3533 },{
3534 .name = "localaddr",
3535 .type = QEMU_OPT_STRING,
3536 },{
3537 .name = "localport",
3538 .type = QEMU_OPT_STRING,
3539 },{
3540 .name = "to",
3541 .type = QEMU_OPT_NUMBER,
3542 },{
3543 .name = "ipv4",
3544 .type = QEMU_OPT_BOOL,
3545 },{
3546 .name = "ipv6",
3547 .type = QEMU_OPT_BOOL,
3548 },{
3549 .name = "wait",
3550 .type = QEMU_OPT_BOOL,
3551 },{
3552 .name = "server",
3553 .type = QEMU_OPT_BOOL,
3554 },{
3555 .name = "delay",
3556 .type = QEMU_OPT_BOOL,
3557 },{
3558 .name = "telnet",
3559 .type = QEMU_OPT_BOOL,
3560 },{
3561 .name = "width",
3562 .type = QEMU_OPT_NUMBER,
3563 },{
3564 .name = "height",
3565 .type = QEMU_OPT_NUMBER,
3566 },{
3567 .name = "cols",
3568 .type = QEMU_OPT_NUMBER,
3569 },{
3570 .name = "rows",
3571 .type = QEMU_OPT_NUMBER,
3572 },{
3573 .name = "mux",
3574 .type = QEMU_OPT_BOOL,
3575 },{
3576 .name = "signal",
3577 .type = QEMU_OPT_BOOL,
3578 },{
3579 .name = "name",
3580 .type = QEMU_OPT_STRING,
3581 },{
3582 .name = "debug",
3583 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003584 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003585 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003586 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003587 },{
3588 .name = "chardev",
3589 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003590 },
3591 { /* end of list */ }
3592 },
3593};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003594
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003595#ifdef _WIN32
3596
3597static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3598{
3599 HANDLE out;
3600
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003601 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003602 error_setg(errp, "input file not supported");
3603 return NULL;
3604 }
3605
3606 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3607 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3608 if (out == INVALID_HANDLE_VALUE) {
3609 error_setg(errp, "open %s failed", file->out);
3610 return NULL;
3611 }
3612 return qemu_chr_open_win_file(out);
3613}
3614
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003615static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3616 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003617{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003618 return qemu_chr_open_win_path(serial->device);
3619}
3620
3621static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3622 Error **errp)
3623{
3624 error_setg(errp, "character device backend type 'parallel' not supported");
3625 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003626}
3627
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003628#else /* WIN32 */
3629
3630static int qmp_chardev_open_file_source(char *src, int flags,
3631 Error **errp)
3632{
3633 int fd = -1;
3634
3635 TFR(fd = qemu_open(src, flags, 0666));
3636 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003637 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003638 }
3639 return fd;
3640}
3641
3642static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3643{
Markus Armbruster5f758362014-05-19 18:57:34 +02003644 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003645
3646 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3647 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003648 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003649 return NULL;
3650 }
3651
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003652 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003653 flags = O_RDONLY;
3654 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003655 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003656 qemu_close(out);
3657 return NULL;
3658 }
3659 }
3660
3661 return qemu_chr_open_fd(in, out);
3662}
3663
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003664static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3665 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003666{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003667#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003668 int fd;
3669
3670 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003671 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003672 return NULL;
3673 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003674 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003675 return qemu_chr_open_tty_fd(fd);
3676#else
3677 error_setg(errp, "character device backend type 'serial' not supported");
3678 return NULL;
3679#endif
3680}
3681
3682static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3683 Error **errp)
3684{
3685#ifdef HAVE_CHARDEV_PARPORT
3686 int fd;
3687
3688 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003689 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003690 return NULL;
3691 }
3692 return qemu_chr_open_pp_fd(fd);
3693#else
3694 error_setg(errp, "character device backend type 'parallel' not supported");
3695 return NULL;
3696#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003697}
3698
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003699#endif /* WIN32 */
3700
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003701static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3702 Error **errp)
3703{
3704 SocketAddress *addr = sock->addr;
3705 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3706 bool is_listen = sock->has_server ? sock->server : true;
3707 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3708 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3709 int fd;
3710
3711 if (is_listen) {
3712 fd = socket_listen(addr, errp);
3713 } else {
3714 fd = socket_connect(addr, errp, NULL, NULL);
3715 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003716 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003717 return NULL;
3718 }
3719 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3720 is_telnet, is_waitconnect, errp);
3721}
3722
Lei Li08d0ab32013-05-20 14:51:03 +08003723static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3724 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003725{
3726 int fd;
3727
Lei Li08d0ab32013-05-20 14:51:03 +08003728 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003729 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003730 return NULL;
3731 }
3732 return qemu_chr_open_udp_fd(fd);
3733}
3734
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003735ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3736 Error **errp)
3737{
3738 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003739 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003740
3741 chr = qemu_chr_find(id);
3742 if (chr) {
3743 error_setg(errp, "Chardev '%s' already exists", id);
3744 g_free(ret);
3745 return NULL;
3746 }
3747
3748 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003749 case CHARDEV_BACKEND_KIND_FILE:
3750 chr = qmp_chardev_open_file(backend->file, errp);
3751 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003752 case CHARDEV_BACKEND_KIND_SERIAL:
3753 chr = qmp_chardev_open_serial(backend->serial, errp);
3754 break;
3755 case CHARDEV_BACKEND_KIND_PARALLEL:
3756 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003757 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003758 case CHARDEV_BACKEND_KIND_PIPE:
3759 chr = qemu_chr_open_pipe(backend->pipe);
3760 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003761 case CHARDEV_BACKEND_KIND_SOCKET:
3762 chr = qmp_chardev_open_socket(backend->socket, errp);
3763 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003764 case CHARDEV_BACKEND_KIND_UDP:
3765 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003766 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003767#ifdef HAVE_CHARDEV_TTY
3768 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003769 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003770 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003771#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003772 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003773 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003774 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003775 case CHARDEV_BACKEND_KIND_MUX:
3776 base = qemu_chr_find(backend->mux->chardev);
3777 if (base == NULL) {
3778 error_setg(errp, "mux: base chardev %s not found",
3779 backend->mux->chardev);
3780 break;
3781 }
3782 chr = qemu_chr_open_mux(base);
3783 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003784 case CHARDEV_BACKEND_KIND_MSMOUSE:
3785 chr = qemu_chr_open_msmouse();
3786 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003787#ifdef CONFIG_BRLAPI
3788 case CHARDEV_BACKEND_KIND_BRAILLE:
3789 chr = chr_baum_init();
3790 break;
3791#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003792 case CHARDEV_BACKEND_KIND_STDIO:
3793 chr = qemu_chr_open_stdio(backend->stdio);
3794 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003795#ifdef _WIN32
3796 case CHARDEV_BACKEND_KIND_CONSOLE:
3797 chr = qemu_chr_open_win_con();
3798 break;
3799#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003800#ifdef CONFIG_SPICE
3801 case CHARDEV_BACKEND_KIND_SPICEVMC:
3802 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3803 break;
3804 case CHARDEV_BACKEND_KIND_SPICEPORT:
3805 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3806 break;
3807#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003808 case CHARDEV_BACKEND_KIND_VC:
3809 chr = vc_init(backend->vc);
3810 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003811 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003812 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003813 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003814 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003815 default:
3816 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3817 break;
3818 }
3819
Markus Armbruster3894c782014-05-19 18:57:36 +02003820 /*
3821 * Character backend open hasn't been fully converted to the Error
3822 * API. Some opens fail without setting an error. Set a generic
3823 * error then.
3824 * TODO full conversion to Error API
3825 */
3826 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003827 error_setg(errp, "Failed to create chardev");
3828 }
3829 if (chr) {
3830 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003831 chr->avail_connections =
3832 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02003833 if (!chr->filename) {
3834 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
3835 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05003836 if (!chr->explicit_be_open) {
3837 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3838 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003839 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3840 return ret;
3841 } else {
3842 g_free(ret);
3843 return NULL;
3844 }
3845}
3846
3847void qmp_chardev_remove(const char *id, Error **errp)
3848{
3849 CharDriverState *chr;
3850
3851 chr = qemu_chr_find(id);
3852 if (NULL == chr) {
3853 error_setg(errp, "Chardev '%s' not found", id);
3854 return;
3855 }
3856 if (chr->chr_can_read || chr->chr_read ||
3857 chr->chr_event || chr->handler_opaque) {
3858 error_setg(errp, "Chardev '%s' is busy", id);
3859 return;
3860 }
3861 qemu_chr_delete(chr);
3862}
Anthony Liguorid654f342013-03-05 23:21:28 +05303863
3864static void register_types(void)
3865{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003866 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303867 register_char_driver("socket", qemu_chr_open_socket);
3868 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02003869 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02003870 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003871 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3872 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003873 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3874 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003875 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3876 qemu_chr_parse_serial);
3877 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3878 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003879 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3880 qemu_chr_parse_parallel);
3881 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3882 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003883 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003884 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003885 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3886 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003887 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
3888 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02003889 /* Bug-compatibility: */
3890 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3891 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05003892 /* this must be done after machine init, since we register FEs with muxes
3893 * as part of realize functions like serial_isa_realizefn when -nographic
3894 * is specified
3895 */
3896 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05303897}
3898
3899type_init(register_types);