blob: 8aa0b4195404debb531b8559e289adbd80628578 [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 Bonzini927d4872012-12-17 18:20:05 +010029#include "char/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
31#include "hw/baum.h"
aurel32aa71cf82009-02-08 15:53:20 +000032#include "hw/msmouse.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030033#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000034
35#include <unistd.h>
36#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000037#include <time.h>
38#include <errno.h>
39#include <sys/time.h>
40#include <zlib.h>
41
42#ifndef _WIN32
43#include <sys/times.h>
44#include <sys/wait.h>
45#include <termios.h>
46#include <sys/mman.h>
47#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000049#include <sys/socket.h>
50#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000051#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000053#include <dirent.h>
54#include <netdb.h>
55#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020056#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000057#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010058#if defined(__GLIBC__)
59#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040060#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000061#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000062#else
63#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000064#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040065#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66#include <dev/ppbus/ppi.h>
67#include <dev/ppbus/ppbconf.h>
68#elif defined(__DragonFly__)
69#include <dev/misc/ppi/ppi.h>
70#include <bus/ppbus/ppbconf.h>
71#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010072#else
aliguori6f97dba2008-10-31 18:49:55 +000073#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000074#include <pty.h>
75
76#include <linux/ppdev.h>
77#include <linux/parport.h>
78#endif
79#ifdef __sun__
80#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
83#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
93#endif
94#endif
95#endif
96
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010097#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020098#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000099
Amit Shah9bd78542009-11-03 19:59:54 +0530100#define READ_BUF_LEN 4096
101
aliguori6f97dba2008-10-31 18:49:55 +0000102/***********************************************************/
103/* character device */
104
Blue Swirl72cf2d42009-09-12 07:36:22 +0000105static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000107
Hans de Goedea425d232011-11-19 10:22:43 +0100108void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000109{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200110 /* Keep track if the char device is open */
111 switch (event) {
112 case CHR_EVENT_OPENED:
113 s->opened = 1;
114 break;
115 case CHR_EVENT_CLOSED:
116 s->opened = 0;
117 break;
118 }
119
aliguori6f97dba2008-10-31 18:49:55 +0000120 if (!s->chr_event)
121 return;
122 s->chr_event(s->handler_opaque, event);
123}
124
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200125static void qemu_chr_fire_open_event(void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000126{
127 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100128 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200129 qemu_free_timer(s->open_timer);
130 s->open_timer = NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000131}
132
Amit Shah127338e2009-11-03 19:59:56 +0530133void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000134{
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200135 if (s->open_timer == NULL) {
Jan Kiszka06dec082012-11-22 18:30:02 +0100136 s->open_timer = qemu_new_timer_ms(rt_clock,
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200137 qemu_chr_fire_open_event, s);
Jan Kiszka06dec082012-11-22 18:30:02 +0100138 qemu_mod_timer(s->open_timer, qemu_get_clock_ms(rt_clock) - 1);
aliguori6f97dba2008-10-31 18:49:55 +0000139 }
140}
141
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500142int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000143{
144 return s->chr_write(s, buf, len);
145}
146
Anthony Liguori41084f12011-08-15 11:17:34 -0500147int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000148{
149 if (!s->chr_ioctl)
150 return -ENOTSUP;
151 return s->chr_ioctl(s, cmd, arg);
152}
153
Anthony Liguori909cda12011-08-15 11:17:31 -0500154int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000155{
156 if (!s->chr_can_read)
157 return 0;
158 return s->chr_can_read(s->handler_opaque);
159}
160
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500161void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000162{
Stefan Weilac310732012-04-19 22:27:14 +0200163 if (s->chr_read) {
164 s->chr_read(s->handler_opaque, buf, len);
165 }
aliguori6f97dba2008-10-31 18:49:55 +0000166}
167
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500168int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100169{
170 return s->get_msgfd ? s->get_msgfd(s) : -1;
171}
172
Daniel P. Berrange13661082011-06-23 13:31:42 +0100173int qemu_chr_add_client(CharDriverState *s, int fd)
174{
175 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
176}
177
aliguori6f97dba2008-10-31 18:49:55 +0000178void qemu_chr_accept_input(CharDriverState *s)
179{
180 if (s->chr_accept_input)
181 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100182 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000183}
184
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500185void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000186{
Amit Shah9bd78542009-11-03 19:59:54 +0530187 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000188 va_list ap;
189 va_start(ap, fmt);
190 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500191 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000192 va_end(ap);
193}
194
aliguori6f97dba2008-10-31 18:49:55 +0000195void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100196 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000197 IOReadHandler *fd_read,
198 IOEventHandler *fd_event,
199 void *opaque)
200{
Amit Shahda7d9982011-04-25 15:18:22 +0530201 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530202 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900203 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530204 }
aliguori6f97dba2008-10-31 18:49:55 +0000205 s->chr_can_read = fd_can_read;
206 s->chr_read = fd_read;
207 s->chr_event = fd_event;
208 s->handler_opaque = opaque;
209 if (s->chr_update_read_handler)
210 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200211
212 /* We're connecting to an already opened device, so let's make sure we
213 also get the open event */
214 if (s->opened) {
215 qemu_chr_generic_open(s);
216 }
aliguori6f97dba2008-10-31 18:49:55 +0000217}
218
219static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
220{
221 return len;
222}
223
Markus Armbruster1f514702012-02-07 15:09:08 +0100224static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000225{
226 CharDriverState *chr;
227
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000229 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100230 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000231}
232
233/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000234#define MAX_MUX 4
235#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
236#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
237typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100238 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000239 IOReadHandler *chr_read[MAX_MUX];
240 IOEventHandler *chr_event[MAX_MUX];
241 void *ext_opaque[MAX_MUX];
242 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200243 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000244 int mux_cnt;
245 int term_got_escape;
246 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000247 /* Intermediate input buffer allows to catch escape sequences even if the
248 currently active device is not accepting any input - but only until it
249 is full as well. */
250 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
251 int prod[MAX_MUX];
252 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200253 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200254 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200255 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000256} MuxDriver;
257
258
259static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
260{
261 MuxDriver *d = chr->opaque;
262 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200263 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000264 ret = d->drv->chr_write(d->drv, buf, len);
265 } else {
266 int i;
267
268 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200269 for (i = 0; i < len; i++) {
270 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000271 char buf1[64];
272 int64_t ti;
273 int secs;
274
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100275 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200276 if (d->timestamps_start == -1)
277 d->timestamps_start = ti;
278 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000279 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000280 snprintf(buf1, sizeof(buf1),
281 "[%02d:%02d:%02d.%03d] ",
282 secs / 3600,
283 (secs / 60) % 60,
284 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000285 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000286 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200287 d->linestart = 0;
288 }
289 ret += d->drv->chr_write(d->drv, buf+i, 1);
290 if (buf[i] == '\n') {
291 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000292 }
293 }
294 }
295 return ret;
296}
297
298static const char * const mux_help[] = {
299 "% h print this help\n\r",
300 "% x exit emulator\n\r",
301 "% s save disk data back to file (if -snapshot)\n\r",
302 "% t toggle console timestamps\n\r"
303 "% b send break (magic sysrq)\n\r",
304 "% c switch between console and monitor\n\r",
305 "% % sends %\n\r",
306 NULL
307};
308
309int term_escape_char = 0x01; /* ctrl-a is used for escape */
310static void mux_print_help(CharDriverState *chr)
311{
312 int i, j;
313 char ebuf[15] = "Escape-Char";
314 char cbuf[50] = "\n\r";
315
316 if (term_escape_char > 0 && term_escape_char < 26) {
317 snprintf(cbuf, sizeof(cbuf), "\n\r");
318 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
319 } else {
320 snprintf(cbuf, sizeof(cbuf),
321 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
322 term_escape_char);
323 }
324 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
325 for (i = 0; mux_help[i] != NULL; i++) {
326 for (j=0; mux_help[i][j] != '\0'; j++) {
327 if (mux_help[i][j] == '%')
328 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
329 else
330 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
331 }
332 }
333}
334
aliguori2724b182009-03-05 23:01:47 +0000335static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
336{
337 if (d->chr_event[mux_nr])
338 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
339}
340
aliguori6f97dba2008-10-31 18:49:55 +0000341static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
342{
343 if (d->term_got_escape) {
344 d->term_got_escape = 0;
345 if (ch == term_escape_char)
346 goto send_char;
347 switch(ch) {
348 case '?':
349 case 'h':
350 mux_print_help(chr);
351 break;
352 case 'x':
353 {
354 const char *term = "QEMU: Terminated\n\r";
355 chr->chr_write(chr,(uint8_t *)term,strlen(term));
356 exit(0);
357 break;
358 }
359 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200360 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000361 break;
362 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100363 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000364 break;
365 case 'c':
366 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
368 d->focus++;
369 if (d->focus >= d->mux_cnt)
370 d->focus = 0;
371 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000372 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200373 case 't':
374 d->timestamps = !d->timestamps;
375 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200376 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200377 break;
aliguori6f97dba2008-10-31 18:49:55 +0000378 }
379 } else if (ch == term_escape_char) {
380 d->term_got_escape = 1;
381 } else {
382 send_char:
383 return 1;
384 }
385 return 0;
386}
387
388static void mux_chr_accept_input(CharDriverState *chr)
389{
aliguori6f97dba2008-10-31 18:49:55 +0000390 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200391 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000392
aliguoria80bf992009-03-05 23:00:02 +0000393 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000394 d->chr_can_read[m] &&
395 d->chr_can_read[m](d->ext_opaque[m])) {
396 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000397 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000398 }
399}
400
401static int mux_chr_can_read(void *opaque)
402{
403 CharDriverState *chr = opaque;
404 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200405 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000406
aliguoria80bf992009-03-05 23:00:02 +0000407 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000408 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000409 if (d->chr_can_read[m])
410 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000411 return 0;
412}
413
414static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
415{
416 CharDriverState *chr = opaque;
417 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200418 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000419 int i;
420
421 mux_chr_accept_input (opaque);
422
423 for(i = 0; i < size; i++)
424 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000425 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000426 d->chr_can_read[m] &&
427 d->chr_can_read[m](d->ext_opaque[m]))
428 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
429 else
aliguoria80bf992009-03-05 23:00:02 +0000430 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000431 }
432}
433
434static void mux_chr_event(void *opaque, int event)
435{
436 CharDriverState *chr = opaque;
437 MuxDriver *d = chr->opaque;
438 int i;
439
440 /* Send the event to all registered listeners */
441 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000442 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000443}
444
445static void mux_chr_update_read_handler(CharDriverState *chr)
446{
447 MuxDriver *d = chr->opaque;
448
449 if (d->mux_cnt >= MAX_MUX) {
450 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
451 return;
452 }
453 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
454 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
455 d->chr_read[d->mux_cnt] = chr->chr_read;
456 d->chr_event[d->mux_cnt] = chr->chr_event;
457 /* Fix up the real driver with mux routines */
458 if (d->mux_cnt == 0) {
459 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
460 mux_chr_event, chr);
461 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200462 if (d->focus != -1) {
463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200464 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200465 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000466 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200467 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000468}
469
470static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
471{
472 CharDriverState *chr;
473 MuxDriver *d;
474
Anthony Liguori7267c092011-08-20 22:09:37 -0500475 chr = g_malloc0(sizeof(CharDriverState));
476 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000477
478 chr->opaque = d;
479 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200480 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000481 chr->chr_write = mux_chr_write;
482 chr->chr_update_read_handler = mux_chr_update_read_handler;
483 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100484 /* Frontend guest-open / -close notification is not support with muxes */
485 chr->chr_guest_open = NULL;
486 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200487
488 /* Muxes are always open on creation */
489 qemu_chr_generic_open(chr);
490
aliguori6f97dba2008-10-31 18:49:55 +0000491 return chr;
492}
493
494
495#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000496int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000497{
498 int ret, len;
499
500 len = len1;
501 while (len > 0) {
502 ret = send(fd, buf, len, 0);
503 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000504 errno = WSAGetLastError();
505 if (errno != WSAEWOULDBLOCK) {
506 return -1;
507 }
508 } else if (ret == 0) {
509 break;
510 } else {
511 buf += ret;
512 len -= ret;
513 }
514 }
515 return len1 - len;
516}
517
518#else
519
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100520int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000521{
522 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100523 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000524
525 len = len1;
526 while (len > 0) {
527 ret = write(fd, buf, len);
528 if (ret < 0) {
529 if (errno != EINTR && errno != EAGAIN)
530 return -1;
531 } else if (ret == 0) {
532 break;
533 } else {
534 buf += ret;
535 len -= ret;
536 }
537 }
538 return len1 - len;
539}
aliguori6f97dba2008-10-31 18:49:55 +0000540#endif /* !_WIN32 */
541
542#ifndef _WIN32
543
Anthony Liguori96c63842013-03-05 23:21:18 +0530544#if 0
545typedef struct IOWatchPoll
546{
547 GSource *src;
548 int max_size;
549
550 IOCanReadHandler *fd_can_read;
551 void *opaque;
552
553 QTAILQ_ENTRY(IOWatchPoll) node;
554} IOWatchPoll;
555
556static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
557 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);
558
559static IOWatchPoll *io_watch_poll_from_source(GSource *source)
560{
561 IOWatchPoll *i;
562
563 QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
564 if (i->src == source) {
565 return i;
566 }
567 }
568
569 return NULL;
570}
571
572static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
573{
574 IOWatchPoll *iwp = io_watch_poll_from_source(source);
575
576 iwp->max_size = iwp->fd_can_read(iwp->opaque);
577 if (iwp->max_size == 0) {
578 return FALSE;
579 }
580
581 return g_io_watch_funcs.prepare(source, timeout_);
582}
583
584static gboolean io_watch_poll_check(GSource *source)
585{
586 IOWatchPoll *iwp = io_watch_poll_from_source(source);
587
588 if (iwp->max_size == 0) {
589 return FALSE;
590 }
591
592 return g_io_watch_funcs.check(source);
593}
594
595static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
596 gpointer user_data)
597{
598 return g_io_watch_funcs.dispatch(source, callback, user_data);
599}
600
601static void io_watch_poll_finalize(GSource *source)
602{
603 IOWatchPoll *iwp = io_watch_poll_from_source(source);
604 QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
605 g_io_watch_funcs.finalize(source);
606}
607
608static GSourceFuncs io_watch_poll_funcs = {
609 .prepare = io_watch_poll_prepare,
610 .check = io_watch_poll_check,
611 .dispatch = io_watch_poll_dispatch,
612 .finalize = io_watch_poll_finalize,
613};
614
615/* Can only be used for read */
616static guint io_add_watch_poll(GIOChannel *channel,
617 IOCanReadHandler *fd_can_read,
618 GIOFunc fd_read,
619 gpointer user_data)
620{
621 IOWatchPoll *iwp;
622 GSource *src;
623 guint tag;
624
625 src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
626 g_source_set_funcs(src, &io_watch_poll_funcs);
627 g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
628 tag = g_source_attach(src, NULL);
629 g_source_unref(src);
630
631 iwp = g_malloc0(sizeof(*iwp));
632 iwp->src = src;
633 iwp->max_size = 0;
634 iwp->fd_can_read = fd_can_read;
635 iwp->opaque = user_data;
636
637 QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);
638
639 return tag;
640}
641
642static GIOChannel *io_channel_from_fd(int fd)
643{
644 GIOChannel *chan;
645
646 if (fd == -1) {
647 return NULL;
648 }
649
650 chan = g_io_channel_unix_new(fd);
651
652 g_io_channel_set_encoding(chan, NULL, NULL);
653 g_io_channel_set_buffered(chan, FALSE);
654
655 return chan;
656}
657
658static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
659{
660 GIOStatus status;
661 gsize bytes_written;
662 int len;
663 const uint8_t *buf = _buf;
664
665 len = len1;
666 while (len > 0) {
667 status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
668 &bytes_written, NULL);
669 if (status != G_IO_STATUS_NORMAL) {
670 if (status != G_IO_STATUS_AGAIN) {
671 return -1;
672 }
673 } else if (status == G_IO_STATUS_EOF) {
674 break;
675 } else {
676 buf += bytes_written;
677 len -= bytes_written;
678 }
679 }
680 return len1 - len;
681}
682#endif
683
aliguori6f97dba2008-10-31 18:49:55 +0000684typedef struct {
685 int fd_in, fd_out;
686 int max_size;
687} FDCharDriver;
688
aliguori6f97dba2008-10-31 18:49:55 +0000689
690static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
691{
692 FDCharDriver *s = chr->opaque;
693 return send_all(s->fd_out, buf, len);
694}
695
696static int fd_chr_read_poll(void *opaque)
697{
698 CharDriverState *chr = opaque;
699 FDCharDriver *s = chr->opaque;
700
Anthony Liguori909cda12011-08-15 11:17:31 -0500701 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000702 return s->max_size;
703}
704
705static void fd_chr_read(void *opaque)
706{
707 CharDriverState *chr = opaque;
708 FDCharDriver *s = chr->opaque;
709 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530710 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000711
712 len = sizeof(buf);
713 if (len > s->max_size)
714 len = s->max_size;
715 if (len == 0)
716 return;
717 size = read(s->fd_in, buf, len);
718 if (size == 0) {
719 /* FD has been closed. Remove it from the active list. */
720 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100721 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000722 return;
723 }
724 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500725 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000726 }
727}
728
729static void fd_chr_update_read_handler(CharDriverState *chr)
730{
731 FDCharDriver *s = chr->opaque;
732
733 if (s->fd_in >= 0) {
Anthony Liguoried7a1542013-03-05 23:21:17 +0530734 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
735 fd_chr_read, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000736 }
737}
738
739static void fd_chr_close(struct CharDriverState *chr)
740{
741 FDCharDriver *s = chr->opaque;
742
743 if (s->fd_in >= 0) {
Anthony Liguoried7a1542013-03-05 23:21:17 +0530744 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +0000745 }
746
Anthony Liguori7267c092011-08-20 22:09:37 -0500747 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100748 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000749}
750
751/* open a character device to a unix fd */
752static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
753{
754 CharDriverState *chr;
755 FDCharDriver *s;
756
Anthony Liguori7267c092011-08-20 22:09:37 -0500757 chr = g_malloc0(sizeof(CharDriverState));
758 s = g_malloc0(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000759 s->fd_in = fd_in;
760 s->fd_out = fd_out;
761 chr->opaque = s;
762 chr->chr_write = fd_chr_write;
763 chr->chr_update_read_handler = fd_chr_update_read_handler;
764 chr->chr_close = fd_chr_close;
765
Amit Shah127338e2009-11-03 19:59:56 +0530766 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000767
768 return chr;
769}
770
Markus Armbruster1f514702012-02-07 15:09:08 +0100771static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000772{
773 int fd_out;
774
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100775 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200776 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100777 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100778 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100779 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100780 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000781}
782
Markus Armbruster1f514702012-02-07 15:09:08 +0100783static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000784{
785 int fd_in, fd_out;
786 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200787 const char *filename = qemu_opt_get(opts, "path");
788
789 if (filename == NULL) {
790 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100791 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200792 }
aliguori6f97dba2008-10-31 18:49:55 +0000793
794 snprintf(filename_in, 256, "%s.in", filename);
795 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100796 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
797 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000798 if (fd_in < 0 || fd_out < 0) {
799 if (fd_in >= 0)
800 close(fd_in);
801 if (fd_out >= 0)
802 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100803 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100804 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100805 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100806 }
aliguori6f97dba2008-10-31 18:49:55 +0000807 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100808 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000809}
810
aliguori6f97dba2008-10-31 18:49:55 +0000811/* init terminal so that we can grab keys */
812static struct termios oldtty;
813static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100814static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000815
816static void term_exit(void)
817{
818 tcsetattr (0, TCSANOW, &oldtty);
819 fcntl(0, F_SETFL, old_fd0_flags);
820}
821
Paolo Bonzinibb002512010-12-23 13:42:50 +0100822static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000823{
824 struct termios tty;
825
Paolo Bonzini03693642010-12-23 13:42:49 +0100826 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100827 if (!echo) {
828 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000829 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100830 tty.c_oflag |= OPOST;
831 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
832 tty.c_cflag &= ~(CSIZE|PARENB);
833 tty.c_cflag |= CS8;
834 tty.c_cc[VMIN] = 1;
835 tty.c_cc[VTIME] = 0;
836 }
aliguori6f97dba2008-10-31 18:49:55 +0000837 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100838 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000839 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000840
841 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000842}
843
844static void qemu_chr_close_stdio(struct CharDriverState *chr)
845{
846 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000847 fd_chr_close(chr);
848}
849
Markus Armbruster1f514702012-02-07 15:09:08 +0100850static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000851{
852 CharDriverState *chr;
853
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400854 if (is_daemonized()) {
855 error_report("cannot use stdio with -daemonize");
856 return NULL;
857 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530858 old_fd0_flags = fcntl(0, F_GETFL);
859 tcgetattr (0, &oldtty);
860 fcntl(0, F_SETFL, O_NONBLOCK);
861 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100862
aliguori6f97dba2008-10-31 18:49:55 +0000863 chr = qemu_chr_open_fd(0, 1);
864 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100865 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100866 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
867 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500868 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000869
Markus Armbruster1f514702012-02-07 15:09:08 +0100870 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000871}
872
873#ifdef __sun__
874/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000875static int openpty(int *amaster, int *aslave, char *name,
876 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000877{
878 const char *slave;
879 int mfd = -1, sfd = -1;
880
881 *amaster = *aslave = -1;
882
883 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
884 if (mfd < 0)
885 goto err;
886
887 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
888 goto err;
889
890 if ((slave = ptsname(mfd)) == NULL)
891 goto err;
892
893 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
894 goto err;
895
896 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
897 (termp != NULL && tcgetattr(sfd, termp) < 0))
898 goto err;
899
900 if (amaster)
901 *amaster = mfd;
902 if (aslave)
903 *aslave = sfd;
904 if (winp)
905 ioctl(sfd, TIOCSWINSZ, winp);
906
907 return 0;
908
909err:
910 if (sfd != -1)
911 close(sfd);
912 close(mfd);
913 return -1;
914}
915
blueswir13f4cb3d2009-04-13 16:31:01 +0000916static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000917{
918 termios_p->c_iflag &=
919 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
920 termios_p->c_oflag &= ~OPOST;
921 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
922 termios_p->c_cflag &= ~(CSIZE|PARENB);
923 termios_p->c_cflag |= CS8;
924
925 termios_p->c_cc[VMIN] = 0;
926 termios_p->c_cc[VTIME] = 0;
927}
928#endif
929
930#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100931 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
932 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000933
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100934#define HAVE_CHARDEV_TTY 1
935
aliguori6f97dba2008-10-31 18:49:55 +0000936typedef struct {
937 int fd;
938 int connected;
939 int polling;
940 int read_bytes;
941 QEMUTimer *timer;
942} PtyCharDriver;
943
944static void pty_chr_update_read_handler(CharDriverState *chr);
945static void pty_chr_state(CharDriverState *chr, int connected);
946
947static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
948{
949 PtyCharDriver *s = chr->opaque;
950
951 if (!s->connected) {
952 /* guest sends data, check for (re-)connect */
953 pty_chr_update_read_handler(chr);
954 return 0;
955 }
956 return send_all(s->fd, buf, len);
957}
958
959static int pty_chr_read_poll(void *opaque)
960{
961 CharDriverState *chr = opaque;
962 PtyCharDriver *s = chr->opaque;
963
Anthony Liguori909cda12011-08-15 11:17:31 -0500964 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000965 return s->read_bytes;
966}
967
968static void pty_chr_read(void *opaque)
969{
970 CharDriverState *chr = opaque;
971 PtyCharDriver *s = chr->opaque;
972 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530973 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000974
975 len = sizeof(buf);
976 if (len > s->read_bytes)
977 len = s->read_bytes;
978 if (len == 0)
979 return;
980 size = read(s->fd, buf, len);
981 if ((size == -1 && errno == EIO) ||
982 (size == 0)) {
983 pty_chr_state(chr, 0);
984 return;
985 }
986 if (size > 0) {
987 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500988 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000989 }
990}
991
992static void pty_chr_update_read_handler(CharDriverState *chr)
993{
994 PtyCharDriver *s = chr->opaque;
995
996 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
997 pty_chr_read, NULL, chr);
998 s->polling = 1;
999 /*
1000 * Short timeout here: just need wait long enougth that qemu makes
1001 * it through the poll loop once. When reconnected we want a
1002 * short timeout so we notice it almost instantly. Otherwise
1003 * read() gives us -EIO instantly, making pty_chr_state() reset the
1004 * timeout to the normal (much longer) poll interval before the
1005 * timer triggers.
1006 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001007 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +00001008}
1009
1010static void pty_chr_state(CharDriverState *chr, int connected)
1011{
1012 PtyCharDriver *s = chr->opaque;
1013
1014 if (!connected) {
1015 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
1016 s->connected = 0;
1017 s->polling = 0;
1018 /* (re-)connect poll interval for idle guests: once per second.
1019 * We check more frequently in case the guests sends data to
1020 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001021 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001022 } else {
1023 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301024 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001025 s->connected = 1;
1026 }
1027}
1028
1029static void pty_chr_timer(void *opaque)
1030{
1031 struct CharDriverState *chr = opaque;
1032 PtyCharDriver *s = chr->opaque;
1033
1034 if (s->connected)
1035 return;
1036 if (s->polling) {
1037 /* If we arrive here without polling being cleared due
1038 * read returning -EIO, then we are (re-)connected */
1039 pty_chr_state(chr, 1);
1040 return;
1041 }
1042
1043 /* Next poll ... */
1044 pty_chr_update_read_handler(chr);
1045}
1046
1047static void pty_chr_close(struct CharDriverState *chr)
1048{
1049 PtyCharDriver *s = chr->opaque;
1050
1051 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
1052 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00001053 qemu_del_timer(s->timer);
1054 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -05001055 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001056 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001057}
1058
Markus Armbruster1f514702012-02-07 15:09:08 +01001059static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001060{
1061 CharDriverState *chr;
1062 PtyCharDriver *s;
1063 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001064 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001065 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001066#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001067 char pty_name[PATH_MAX];
1068#define q_ptsname(x) pty_name
1069#else
1070 char *pty_name = NULL;
1071#define q_ptsname(x) ptsname(x)
1072#endif
1073
Markus Armbrustera4e26042011-11-11 10:40:05 +01001074 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001075 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001076 }
1077
1078 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001079 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001080 cfmakeraw(&tty);
1081 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1082 close(slave_fd);
1083
Markus Armbrustera4e26042011-11-11 10:40:05 +01001084 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001085
Markus Armbrustera4e26042011-11-11 10:40:05 +01001086 len = strlen(q_ptsname(master_fd)) + 5;
1087 chr->filename = g_malloc(len);
1088 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1089 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001090
1091 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001092 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1093 q_ptsname(master_fd),
1094 label ? " (label " : "",
1095 label ? label : "",
1096 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001097
1098 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001099 chr->opaque = s;
1100 chr->chr_write = pty_chr_write;
1101 chr->chr_update_read_handler = pty_chr_update_read_handler;
1102 chr->chr_close = pty_chr_close;
1103
Markus Armbrustera4e26042011-11-11 10:40:05 +01001104 s->fd = master_fd;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001105 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001106
Markus Armbruster1f514702012-02-07 15:09:08 +01001107 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001108}
1109
1110static void tty_serial_init(int fd, int speed,
1111 int parity, int data_bits, int stop_bits)
1112{
1113 struct termios tty;
1114 speed_t spd;
1115
1116#if 0
1117 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1118 speed, parity, data_bits, stop_bits);
1119#endif
1120 tcgetattr (fd, &tty);
1121
Stefan Weil45eea132009-10-26 16:10:10 +01001122#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1123 speed = speed * 10 / 11;
1124 do {
1125 check_speed(50);
1126 check_speed(75);
1127 check_speed(110);
1128 check_speed(134);
1129 check_speed(150);
1130 check_speed(200);
1131 check_speed(300);
1132 check_speed(600);
1133 check_speed(1200);
1134 check_speed(1800);
1135 check_speed(2400);
1136 check_speed(4800);
1137 check_speed(9600);
1138 check_speed(19200);
1139 check_speed(38400);
1140 /* Non-Posix values follow. They may be unsupported on some systems. */
1141 check_speed(57600);
1142 check_speed(115200);
1143#ifdef B230400
1144 check_speed(230400);
1145#endif
1146#ifdef B460800
1147 check_speed(460800);
1148#endif
1149#ifdef B500000
1150 check_speed(500000);
1151#endif
1152#ifdef B576000
1153 check_speed(576000);
1154#endif
1155#ifdef B921600
1156 check_speed(921600);
1157#endif
1158#ifdef B1000000
1159 check_speed(1000000);
1160#endif
1161#ifdef B1152000
1162 check_speed(1152000);
1163#endif
1164#ifdef B1500000
1165 check_speed(1500000);
1166#endif
1167#ifdef B2000000
1168 check_speed(2000000);
1169#endif
1170#ifdef B2500000
1171 check_speed(2500000);
1172#endif
1173#ifdef B3000000
1174 check_speed(3000000);
1175#endif
1176#ifdef B3500000
1177 check_speed(3500000);
1178#endif
1179#ifdef B4000000
1180 check_speed(4000000);
1181#endif
aliguori6f97dba2008-10-31 18:49:55 +00001182 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001183 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001184
1185 cfsetispeed(&tty, spd);
1186 cfsetospeed(&tty, spd);
1187
1188 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1189 |INLCR|IGNCR|ICRNL|IXON);
1190 tty.c_oflag |= OPOST;
1191 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1192 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1193 switch(data_bits) {
1194 default:
1195 case 8:
1196 tty.c_cflag |= CS8;
1197 break;
1198 case 7:
1199 tty.c_cflag |= CS7;
1200 break;
1201 case 6:
1202 tty.c_cflag |= CS6;
1203 break;
1204 case 5:
1205 tty.c_cflag |= CS5;
1206 break;
1207 }
1208 switch(parity) {
1209 default:
1210 case 'N':
1211 break;
1212 case 'E':
1213 tty.c_cflag |= PARENB;
1214 break;
1215 case 'O':
1216 tty.c_cflag |= PARENB | PARODD;
1217 break;
1218 }
1219 if (stop_bits == 2)
1220 tty.c_cflag |= CSTOPB;
1221
1222 tcsetattr (fd, TCSANOW, &tty);
1223}
1224
1225static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1226{
1227 FDCharDriver *s = chr->opaque;
1228
1229 switch(cmd) {
1230 case CHR_IOCTL_SERIAL_SET_PARAMS:
1231 {
1232 QEMUSerialSetParams *ssp = arg;
1233 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1234 ssp->data_bits, ssp->stop_bits);
1235 }
1236 break;
1237 case CHR_IOCTL_SERIAL_SET_BREAK:
1238 {
1239 int enable = *(int *)arg;
1240 if (enable)
1241 tcsendbreak(s->fd_in, 1);
1242 }
1243 break;
1244 case CHR_IOCTL_SERIAL_GET_TIOCM:
1245 {
1246 int sarg = 0;
1247 int *targ = (int *)arg;
1248 ioctl(s->fd_in, TIOCMGET, &sarg);
1249 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001250 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001251 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001252 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001253 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001254 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001255 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001256 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001257 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001258 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001259 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001260 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001261 *targ |= CHR_TIOCM_RTS;
1262 }
1263 break;
1264 case CHR_IOCTL_SERIAL_SET_TIOCM:
1265 {
1266 int sarg = *(int *)arg;
1267 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001268 ioctl(s->fd_in, TIOCMGET, &targ);
1269 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1270 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1271 if (sarg & CHR_TIOCM_CTS)
1272 targ |= TIOCM_CTS;
1273 if (sarg & CHR_TIOCM_CAR)
1274 targ |= TIOCM_CAR;
1275 if (sarg & CHR_TIOCM_DSR)
1276 targ |= TIOCM_DSR;
1277 if (sarg & CHR_TIOCM_RI)
1278 targ |= TIOCM_RI;
1279 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001280 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001281 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001282 targ |= TIOCM_RTS;
1283 ioctl(s->fd_in, TIOCMSET, &targ);
1284 }
1285 break;
1286 default:
1287 return -ENOTSUP;
1288 }
1289 return 0;
1290}
1291
David Ahern4266a132010-02-10 18:27:17 -07001292static void qemu_chr_close_tty(CharDriverState *chr)
1293{
1294 FDCharDriver *s = chr->opaque;
1295 int fd = -1;
1296
1297 if (s) {
1298 fd = s->fd_in;
1299 }
1300
1301 fd_chr_close(chr);
1302
1303 if (fd >= 0) {
1304 close(fd);
1305 }
1306}
1307
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001308static CharDriverState *qemu_chr_open_tty_fd(int fd)
1309{
1310 CharDriverState *chr;
1311
1312 tty_serial_init(fd, 115200, 'N', 8, 1);
1313 chr = qemu_chr_open_fd(fd, fd);
1314 chr->chr_ioctl = tty_serial_ioctl;
1315 chr->chr_close = qemu_chr_close_tty;
1316 return chr;
1317}
1318
Markus Armbruster1f514702012-02-07 15:09:08 +01001319static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001320{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001321 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001322 int fd;
1323
Markus Armbrusterb181e042012-02-07 15:09:09 +01001324 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001325 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001326 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001327 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001328 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001329}
aliguori6f97dba2008-10-31 18:49:55 +00001330#endif /* __linux__ || __sun__ */
1331
1332#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001333
1334#define HAVE_CHARDEV_PARPORT 1
1335
aliguori6f97dba2008-10-31 18:49:55 +00001336typedef struct {
1337 int fd;
1338 int mode;
1339} ParallelCharDriver;
1340
1341static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1342{
1343 if (s->mode != mode) {
1344 int m = mode;
1345 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1346 return 0;
1347 s->mode = mode;
1348 }
1349 return 1;
1350}
1351
1352static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1353{
1354 ParallelCharDriver *drv = chr->opaque;
1355 int fd = drv->fd;
1356 uint8_t b;
1357
1358 switch(cmd) {
1359 case CHR_IOCTL_PP_READ_DATA:
1360 if (ioctl(fd, PPRDATA, &b) < 0)
1361 return -ENOTSUP;
1362 *(uint8_t *)arg = b;
1363 break;
1364 case CHR_IOCTL_PP_WRITE_DATA:
1365 b = *(uint8_t *)arg;
1366 if (ioctl(fd, PPWDATA, &b) < 0)
1367 return -ENOTSUP;
1368 break;
1369 case CHR_IOCTL_PP_READ_CONTROL:
1370 if (ioctl(fd, PPRCONTROL, &b) < 0)
1371 return -ENOTSUP;
1372 /* Linux gives only the lowest bits, and no way to know data
1373 direction! For better compatibility set the fixed upper
1374 bits. */
1375 *(uint8_t *)arg = b | 0xc0;
1376 break;
1377 case CHR_IOCTL_PP_WRITE_CONTROL:
1378 b = *(uint8_t *)arg;
1379 if (ioctl(fd, PPWCONTROL, &b) < 0)
1380 return -ENOTSUP;
1381 break;
1382 case CHR_IOCTL_PP_READ_STATUS:
1383 if (ioctl(fd, PPRSTATUS, &b) < 0)
1384 return -ENOTSUP;
1385 *(uint8_t *)arg = b;
1386 break;
1387 case CHR_IOCTL_PP_DATA_DIR:
1388 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1389 return -ENOTSUP;
1390 break;
1391 case CHR_IOCTL_PP_EPP_READ_ADDR:
1392 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1393 struct ParallelIOArg *parg = arg;
1394 int n = read(fd, parg->buffer, parg->count);
1395 if (n != parg->count) {
1396 return -EIO;
1397 }
1398 }
1399 break;
1400 case CHR_IOCTL_PP_EPP_READ:
1401 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1402 struct ParallelIOArg *parg = arg;
1403 int n = read(fd, parg->buffer, parg->count);
1404 if (n != parg->count) {
1405 return -EIO;
1406 }
1407 }
1408 break;
1409 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1410 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1411 struct ParallelIOArg *parg = arg;
1412 int n = write(fd, parg->buffer, parg->count);
1413 if (n != parg->count) {
1414 return -EIO;
1415 }
1416 }
1417 break;
1418 case CHR_IOCTL_PP_EPP_WRITE:
1419 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1420 struct ParallelIOArg *parg = arg;
1421 int n = write(fd, parg->buffer, parg->count);
1422 if (n != parg->count) {
1423 return -EIO;
1424 }
1425 }
1426 break;
1427 default:
1428 return -ENOTSUP;
1429 }
1430 return 0;
1431}
1432
1433static void pp_close(CharDriverState *chr)
1434{
1435 ParallelCharDriver *drv = chr->opaque;
1436 int fd = drv->fd;
1437
1438 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1439 ioctl(fd, PPRELEASE);
1440 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001441 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001442 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001443}
1444
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001445static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001446{
1447 CharDriverState *chr;
1448 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001449
1450 if (ioctl(fd, PPCLAIM) < 0) {
1451 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001452 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001453 }
1454
Anthony Liguori7267c092011-08-20 22:09:37 -05001455 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001456 drv->fd = fd;
1457 drv->mode = IEEE1284_MODE_COMPAT;
1458
Anthony Liguori7267c092011-08-20 22:09:37 -05001459 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001460 chr->chr_write = null_chr_write;
1461 chr->chr_ioctl = pp_ioctl;
1462 chr->chr_close = pp_close;
1463 chr->opaque = drv;
1464
Amit Shah127338e2009-11-03 19:59:56 +05301465 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001466
Markus Armbruster1f514702012-02-07 15:09:08 +01001467 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001468}
1469#endif /* __linux__ */
1470
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001471#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001472
1473#define HAVE_CHARDEV_PARPORT 1
1474
blueswir16972f932008-11-22 20:49:12 +00001475static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1476{
Stefan Weile0efb992011-02-23 19:09:16 +01001477 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001478 uint8_t b;
1479
1480 switch(cmd) {
1481 case CHR_IOCTL_PP_READ_DATA:
1482 if (ioctl(fd, PPIGDATA, &b) < 0)
1483 return -ENOTSUP;
1484 *(uint8_t *)arg = b;
1485 break;
1486 case CHR_IOCTL_PP_WRITE_DATA:
1487 b = *(uint8_t *)arg;
1488 if (ioctl(fd, PPISDATA, &b) < 0)
1489 return -ENOTSUP;
1490 break;
1491 case CHR_IOCTL_PP_READ_CONTROL:
1492 if (ioctl(fd, PPIGCTRL, &b) < 0)
1493 return -ENOTSUP;
1494 *(uint8_t *)arg = b;
1495 break;
1496 case CHR_IOCTL_PP_WRITE_CONTROL:
1497 b = *(uint8_t *)arg;
1498 if (ioctl(fd, PPISCTRL, &b) < 0)
1499 return -ENOTSUP;
1500 break;
1501 case CHR_IOCTL_PP_READ_STATUS:
1502 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1503 return -ENOTSUP;
1504 *(uint8_t *)arg = b;
1505 break;
1506 default:
1507 return -ENOTSUP;
1508 }
1509 return 0;
1510}
1511
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001512static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001513{
1514 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001515
Anthony Liguori7267c092011-08-20 22:09:37 -05001516 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001517 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001518 chr->chr_write = null_chr_write;
1519 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001520 return chr;
blueswir16972f932008-11-22 20:49:12 +00001521}
1522#endif
1523
aliguori6f97dba2008-10-31 18:49:55 +00001524#else /* _WIN32 */
1525
1526typedef struct {
1527 int max_size;
1528 HANDLE hcom, hrecv, hsend;
1529 OVERLAPPED orecv, osend;
1530 BOOL fpipe;
1531 DWORD len;
1532} WinCharState;
1533
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001534typedef struct {
1535 HANDLE hStdIn;
1536 HANDLE hInputReadyEvent;
1537 HANDLE hInputDoneEvent;
1538 HANDLE hInputThread;
1539 uint8_t win_stdio_buf;
1540} WinStdioCharState;
1541
aliguori6f97dba2008-10-31 18:49:55 +00001542#define NSENDBUF 2048
1543#define NRECVBUF 2048
1544#define MAXCONNECT 1
1545#define NTIMEOUT 5000
1546
1547static int win_chr_poll(void *opaque);
1548static int win_chr_pipe_poll(void *opaque);
1549
1550static void win_chr_close(CharDriverState *chr)
1551{
1552 WinCharState *s = chr->opaque;
1553
1554 if (s->hsend) {
1555 CloseHandle(s->hsend);
1556 s->hsend = NULL;
1557 }
1558 if (s->hrecv) {
1559 CloseHandle(s->hrecv);
1560 s->hrecv = NULL;
1561 }
1562 if (s->hcom) {
1563 CloseHandle(s->hcom);
1564 s->hcom = NULL;
1565 }
1566 if (s->fpipe)
1567 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1568 else
1569 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301570
Hans de Goedea425d232011-11-19 10:22:43 +01001571 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001572}
1573
1574static int win_chr_init(CharDriverState *chr, const char *filename)
1575{
1576 WinCharState *s = chr->opaque;
1577 COMMCONFIG comcfg;
1578 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1579 COMSTAT comstat;
1580 DWORD size;
1581 DWORD err;
1582
1583 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1584 if (!s->hsend) {
1585 fprintf(stderr, "Failed CreateEvent\n");
1586 goto fail;
1587 }
1588 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1589 if (!s->hrecv) {
1590 fprintf(stderr, "Failed CreateEvent\n");
1591 goto fail;
1592 }
1593
1594 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1595 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1596 if (s->hcom == INVALID_HANDLE_VALUE) {
1597 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1598 s->hcom = NULL;
1599 goto fail;
1600 }
1601
1602 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1603 fprintf(stderr, "Failed SetupComm\n");
1604 goto fail;
1605 }
1606
1607 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1608 size = sizeof(COMMCONFIG);
1609 GetDefaultCommConfig(filename, &comcfg, &size);
1610 comcfg.dcb.DCBlength = sizeof(DCB);
1611 CommConfigDialog(filename, NULL, &comcfg);
1612
1613 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1614 fprintf(stderr, "Failed SetCommState\n");
1615 goto fail;
1616 }
1617
1618 if (!SetCommMask(s->hcom, EV_ERR)) {
1619 fprintf(stderr, "Failed SetCommMask\n");
1620 goto fail;
1621 }
1622
1623 cto.ReadIntervalTimeout = MAXDWORD;
1624 if (!SetCommTimeouts(s->hcom, &cto)) {
1625 fprintf(stderr, "Failed SetCommTimeouts\n");
1626 goto fail;
1627 }
1628
1629 if (!ClearCommError(s->hcom, &err, &comstat)) {
1630 fprintf(stderr, "Failed ClearCommError\n");
1631 goto fail;
1632 }
1633 qemu_add_polling_cb(win_chr_poll, chr);
1634 return 0;
1635
1636 fail:
1637 win_chr_close(chr);
1638 return -1;
1639}
1640
1641static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1642{
1643 WinCharState *s = chr->opaque;
1644 DWORD len, ret, size, err;
1645
1646 len = len1;
1647 ZeroMemory(&s->osend, sizeof(s->osend));
1648 s->osend.hEvent = s->hsend;
1649 while (len > 0) {
1650 if (s->hsend)
1651 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1652 else
1653 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1654 if (!ret) {
1655 err = GetLastError();
1656 if (err == ERROR_IO_PENDING) {
1657 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1658 if (ret) {
1659 buf += size;
1660 len -= size;
1661 } else {
1662 break;
1663 }
1664 } else {
1665 break;
1666 }
1667 } else {
1668 buf += size;
1669 len -= size;
1670 }
1671 }
1672 return len1 - len;
1673}
1674
1675static int win_chr_read_poll(CharDriverState *chr)
1676{
1677 WinCharState *s = chr->opaque;
1678
Anthony Liguori909cda12011-08-15 11:17:31 -05001679 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001680 return s->max_size;
1681}
1682
1683static void win_chr_readfile(CharDriverState *chr)
1684{
1685 WinCharState *s = chr->opaque;
1686 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301687 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001688 DWORD size;
1689
1690 ZeroMemory(&s->orecv, sizeof(s->orecv));
1691 s->orecv.hEvent = s->hrecv;
1692 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1693 if (!ret) {
1694 err = GetLastError();
1695 if (err == ERROR_IO_PENDING) {
1696 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1697 }
1698 }
1699
1700 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001701 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001702 }
1703}
1704
1705static void win_chr_read(CharDriverState *chr)
1706{
1707 WinCharState *s = chr->opaque;
1708
1709 if (s->len > s->max_size)
1710 s->len = s->max_size;
1711 if (s->len == 0)
1712 return;
1713
1714 win_chr_readfile(chr);
1715}
1716
1717static int win_chr_poll(void *opaque)
1718{
1719 CharDriverState *chr = opaque;
1720 WinCharState *s = chr->opaque;
1721 COMSTAT status;
1722 DWORD comerr;
1723
1724 ClearCommError(s->hcom, &comerr, &status);
1725 if (status.cbInQue > 0) {
1726 s->len = status.cbInQue;
1727 win_chr_read_poll(chr);
1728 win_chr_read(chr);
1729 return 1;
1730 }
1731 return 0;
1732}
1733
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001734static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001735{
1736 CharDriverState *chr;
1737 WinCharState *s;
1738
Anthony Liguori7267c092011-08-20 22:09:37 -05001739 chr = g_malloc0(sizeof(CharDriverState));
1740 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001741 chr->opaque = s;
1742 chr->chr_write = win_chr_write;
1743 chr->chr_close = win_chr_close;
1744
1745 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001746 g_free(s);
1747 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001748 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001749 }
Amit Shah127338e2009-11-03 19:59:56 +05301750 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001751 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001752}
1753
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001754static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1755{
1756 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1757}
1758
aliguori6f97dba2008-10-31 18:49:55 +00001759static int win_chr_pipe_poll(void *opaque)
1760{
1761 CharDriverState *chr = opaque;
1762 WinCharState *s = chr->opaque;
1763 DWORD size;
1764
1765 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1766 if (size > 0) {
1767 s->len = size;
1768 win_chr_read_poll(chr);
1769 win_chr_read(chr);
1770 return 1;
1771 }
1772 return 0;
1773}
1774
1775static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1776{
1777 WinCharState *s = chr->opaque;
1778 OVERLAPPED ov;
1779 int ret;
1780 DWORD size;
1781 char openname[256];
1782
1783 s->fpipe = TRUE;
1784
1785 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1786 if (!s->hsend) {
1787 fprintf(stderr, "Failed CreateEvent\n");
1788 goto fail;
1789 }
1790 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1791 if (!s->hrecv) {
1792 fprintf(stderr, "Failed CreateEvent\n");
1793 goto fail;
1794 }
1795
1796 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1797 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1798 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1799 PIPE_WAIT,
1800 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1801 if (s->hcom == INVALID_HANDLE_VALUE) {
1802 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1803 s->hcom = NULL;
1804 goto fail;
1805 }
1806
1807 ZeroMemory(&ov, sizeof(ov));
1808 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1809 ret = ConnectNamedPipe(s->hcom, &ov);
1810 if (ret) {
1811 fprintf(stderr, "Failed ConnectNamedPipe\n");
1812 goto fail;
1813 }
1814
1815 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1816 if (!ret) {
1817 fprintf(stderr, "Failed GetOverlappedResult\n");
1818 if (ov.hEvent) {
1819 CloseHandle(ov.hEvent);
1820 ov.hEvent = NULL;
1821 }
1822 goto fail;
1823 }
1824
1825 if (ov.hEvent) {
1826 CloseHandle(ov.hEvent);
1827 ov.hEvent = NULL;
1828 }
1829 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1830 return 0;
1831
1832 fail:
1833 win_chr_close(chr);
1834 return -1;
1835}
1836
1837
Markus Armbruster1f514702012-02-07 15:09:08 +01001838static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001839{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001840 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001841 CharDriverState *chr;
1842 WinCharState *s;
1843
Anthony Liguori7267c092011-08-20 22:09:37 -05001844 chr = g_malloc0(sizeof(CharDriverState));
1845 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001846 chr->opaque = s;
1847 chr->chr_write = win_chr_write;
1848 chr->chr_close = win_chr_close;
1849
1850 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001851 g_free(s);
1852 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001853 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001854 }
Amit Shah127338e2009-11-03 19:59:56 +05301855 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001856 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001857}
1858
Markus Armbruster1f514702012-02-07 15:09:08 +01001859static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001860{
1861 CharDriverState *chr;
1862 WinCharState *s;
1863
Anthony Liguori7267c092011-08-20 22:09:37 -05001864 chr = g_malloc0(sizeof(CharDriverState));
1865 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001866 s->hcom = fd_out;
1867 chr->opaque = s;
1868 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301869 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001870 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001871}
1872
Markus Armbruster1f514702012-02-07 15:09:08 +01001873static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001874{
Markus Armbruster1f514702012-02-07 15:09:08 +01001875 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001876}
1877
Markus Armbruster1f514702012-02-07 15:09:08 +01001878static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001879{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001880 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001881 HANDLE fd_out;
1882
1883 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1884 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001885 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001886 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001887 }
aliguori6f97dba2008-10-31 18:49:55 +00001888
Markus Armbruster1f514702012-02-07 15:09:08 +01001889 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001890}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001891
1892static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1893{
1894 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1895 DWORD dwSize;
1896 int len1;
1897
1898 len1 = len;
1899
1900 while (len1 > 0) {
1901 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1902 break;
1903 }
1904 buf += dwSize;
1905 len1 -= dwSize;
1906 }
1907
1908 return len - len1;
1909}
1910
1911static void win_stdio_wait_func(void *opaque)
1912{
1913 CharDriverState *chr = opaque;
1914 WinStdioCharState *stdio = chr->opaque;
1915 INPUT_RECORD buf[4];
1916 int ret;
1917 DWORD dwSize;
1918 int i;
1919
1920 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1921 &dwSize);
1922
1923 if (!ret) {
1924 /* Avoid error storm */
1925 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1926 return;
1927 }
1928
1929 for (i = 0; i < dwSize; i++) {
1930 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1931
1932 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1933 int j;
1934 if (kev->uChar.AsciiChar != 0) {
1935 for (j = 0; j < kev->wRepeatCount; j++) {
1936 if (qemu_chr_be_can_write(chr)) {
1937 uint8_t c = kev->uChar.AsciiChar;
1938 qemu_chr_be_write(chr, &c, 1);
1939 }
1940 }
1941 }
1942 }
1943 }
1944}
1945
1946static DWORD WINAPI win_stdio_thread(LPVOID param)
1947{
1948 CharDriverState *chr = param;
1949 WinStdioCharState *stdio = chr->opaque;
1950 int ret;
1951 DWORD dwSize;
1952
1953 while (1) {
1954
1955 /* Wait for one byte */
1956 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1957
1958 /* Exit in case of error, continue if nothing read */
1959 if (!ret) {
1960 break;
1961 }
1962 if (!dwSize) {
1963 continue;
1964 }
1965
1966 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1967 if (stdio->win_stdio_buf == '\r') {
1968 continue;
1969 }
1970
1971 /* Signal the main thread and wait until the byte was eaten */
1972 if (!SetEvent(stdio->hInputReadyEvent)) {
1973 break;
1974 }
1975 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1976 != WAIT_OBJECT_0) {
1977 break;
1978 }
1979 }
1980
1981 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
1982 return 0;
1983}
1984
1985static void win_stdio_thread_wait_func(void *opaque)
1986{
1987 CharDriverState *chr = opaque;
1988 WinStdioCharState *stdio = chr->opaque;
1989
1990 if (qemu_chr_be_can_write(chr)) {
1991 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
1992 }
1993
1994 SetEvent(stdio->hInputDoneEvent);
1995}
1996
1997static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
1998{
1999 WinStdioCharState *stdio = chr->opaque;
2000 DWORD dwMode = 0;
2001
2002 GetConsoleMode(stdio->hStdIn, &dwMode);
2003
2004 if (echo) {
2005 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2006 } else {
2007 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2008 }
2009}
2010
2011static void win_stdio_close(CharDriverState *chr)
2012{
2013 WinStdioCharState *stdio = chr->opaque;
2014
2015 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2016 CloseHandle(stdio->hInputReadyEvent);
2017 }
2018 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2019 CloseHandle(stdio->hInputDoneEvent);
2020 }
2021 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2022 TerminateThread(stdio->hInputThread, 0);
2023 }
2024
2025 g_free(chr->opaque);
2026 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002027}
2028
Markus Armbruster1f514702012-02-07 15:09:08 +01002029static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002030{
2031 CharDriverState *chr;
2032 WinStdioCharState *stdio;
2033 DWORD dwMode;
2034 int is_console = 0;
2035
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002036 chr = g_malloc0(sizeof(CharDriverState));
2037 stdio = g_malloc0(sizeof(WinStdioCharState));
2038
2039 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2040 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2041 fprintf(stderr, "cannot open stdio: invalid handle\n");
2042 exit(1);
2043 }
2044
2045 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2046
2047 chr->opaque = stdio;
2048 chr->chr_write = win_stdio_write;
2049 chr->chr_close = win_stdio_close;
2050
Anthony Liguoried7a1542013-03-05 23:21:17 +05302051 if (is_console) {
2052 if (qemu_add_wait_object(stdio->hStdIn,
2053 win_stdio_wait_func, chr)) {
2054 fprintf(stderr, "qemu_add_wait_object: failed\n");
2055 }
2056 } else {
2057 DWORD dwId;
2058
2059 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2060 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2061 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2062 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002063
Anthony Liguoried7a1542013-03-05 23:21:17 +05302064 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2065 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2066 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2067 fprintf(stderr, "cannot create stdio thread or event\n");
2068 exit(1);
2069 }
2070 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2071 win_stdio_thread_wait_func, chr)) {
2072 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002073 }
2074 }
2075
2076 dwMode |= ENABLE_LINE_INPUT;
2077
Anthony Liguoried7a1542013-03-05 23:21:17 +05302078 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002079 /* set the terminal in raw mode */
2080 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2081 dwMode |= ENABLE_PROCESSED_INPUT;
2082 }
2083
2084 SetConsoleMode(stdio->hStdIn, dwMode);
2085
2086 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2087 qemu_chr_fe_set_echo(chr, false);
2088
Markus Armbruster1f514702012-02-07 15:09:08 +01002089 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002090}
aliguori6f97dba2008-10-31 18:49:55 +00002091#endif /* !_WIN32 */
2092
2093/***********************************************************/
2094/* UDP Net console */
2095
2096typedef struct {
2097 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05302098 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002099 int bufcnt;
2100 int bufptr;
2101 int max_size;
2102} NetCharDriver;
2103
2104static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2105{
2106 NetCharDriver *s = chr->opaque;
2107
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002108 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00002109}
2110
2111static int udp_chr_read_poll(void *opaque)
2112{
2113 CharDriverState *chr = opaque;
2114 NetCharDriver *s = chr->opaque;
2115
Anthony Liguori909cda12011-08-15 11:17:31 -05002116 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002117
2118 /* If there were any stray characters in the queue process them
2119 * first
2120 */
2121 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002122 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002123 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002124 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002125 }
2126 return s->max_size;
2127}
2128
2129static void udp_chr_read(void *opaque)
2130{
2131 CharDriverState *chr = opaque;
2132 NetCharDriver *s = chr->opaque;
2133
2134 if (s->max_size == 0)
2135 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00002136 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00002137 s->bufptr = s->bufcnt;
2138 if (s->bufcnt <= 0)
2139 return;
2140
2141 s->bufptr = 0;
2142 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002143 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002144 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002145 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002146 }
2147}
2148
2149static void udp_chr_update_read_handler(CharDriverState *chr)
2150{
2151 NetCharDriver *s = chr->opaque;
2152
2153 if (s->fd >= 0) {
2154 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2155 udp_chr_read, NULL, chr);
2156 }
2157}
2158
aliguori819f56b2009-03-28 17:58:14 +00002159static void udp_chr_close(CharDriverState *chr)
2160{
2161 NetCharDriver *s = chr->opaque;
2162 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002163 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00002164 closesocket(s->fd);
2165 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002166 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002167 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002168}
2169
Markus Armbruster1f514702012-02-07 15:09:08 +01002170static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002171{
2172 CharDriverState *chr = NULL;
2173 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002174 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002175 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002176
Anthony Liguori7267c092011-08-20 22:09:37 -05002177 chr = g_malloc0(sizeof(CharDriverState));
2178 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002179
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002180 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002181 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002182 goto return_err;
2183 }
2184
2185 s->fd = fd;
2186 s->bufcnt = 0;
2187 s->bufptr = 0;
2188 chr->opaque = s;
2189 chr->chr_write = udp_chr_write;
2190 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002191 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002192 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002193
2194return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002195 if (local_err) {
2196 qerror_report_err(local_err);
2197 error_free(local_err);
2198 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002199 g_free(chr);
2200 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002201 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002202 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002203 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002204 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002205}
2206
2207/***********************************************************/
2208/* TCP Net console */
2209
2210typedef struct {
2211 int fd, listen_fd;
2212 int connected;
2213 int max_size;
2214 int do_telnetopt;
2215 int do_nodelay;
2216 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002217 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002218} TCPCharDriver;
2219
2220static void tcp_chr_accept(void *opaque);
2221
2222static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2223{
2224 TCPCharDriver *s = chr->opaque;
2225 if (s->connected) {
2226 return send_all(s->fd, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002227 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002228 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002229 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002230 }
2231}
2232
2233static int tcp_chr_read_poll(void *opaque)
2234{
2235 CharDriverState *chr = opaque;
2236 TCPCharDriver *s = chr->opaque;
2237 if (!s->connected)
2238 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002239 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002240 return s->max_size;
2241}
2242
2243#define IAC 255
2244#define IAC_BREAK 243
2245static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2246 TCPCharDriver *s,
2247 uint8_t *buf, int *size)
2248{
2249 /* Handle any telnet client's basic IAC options to satisfy char by
2250 * char mode with no echo. All IAC options will be removed from
2251 * the buf and the do_telnetopt variable will be used to track the
2252 * state of the width of the IAC information.
2253 *
2254 * IAC commands come in sets of 3 bytes with the exception of the
2255 * "IAC BREAK" command and the double IAC.
2256 */
2257
2258 int i;
2259 int j = 0;
2260
2261 for (i = 0; i < *size; i++) {
2262 if (s->do_telnetopt > 1) {
2263 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2264 /* Double IAC means send an IAC */
2265 if (j != i)
2266 buf[j] = buf[i];
2267 j++;
2268 s->do_telnetopt = 1;
2269 } else {
2270 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2271 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002272 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002273 s->do_telnetopt++;
2274 }
2275 s->do_telnetopt++;
2276 }
2277 if (s->do_telnetopt >= 4) {
2278 s->do_telnetopt = 1;
2279 }
2280 } else {
2281 if ((unsigned char)buf[i] == IAC) {
2282 s->do_telnetopt = 2;
2283 } else {
2284 if (j != i)
2285 buf[j] = buf[i];
2286 j++;
2287 }
2288 }
2289 }
2290 *size = j;
2291}
2292
Mark McLoughlin7d174052009-07-22 09:11:39 +01002293static int tcp_get_msgfd(CharDriverState *chr)
2294{
2295 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002296 int fd = s->msgfd;
2297 s->msgfd = -1;
2298 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002299}
2300
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002301#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002302static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2303{
2304 TCPCharDriver *s = chr->opaque;
2305 struct cmsghdr *cmsg;
2306
2307 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2308 int fd;
2309
2310 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2311 cmsg->cmsg_level != SOL_SOCKET ||
2312 cmsg->cmsg_type != SCM_RIGHTS)
2313 continue;
2314
2315 fd = *((int *)CMSG_DATA(cmsg));
2316 if (fd < 0)
2317 continue;
2318
Corey Bryant06138652012-08-14 16:43:42 -04002319#ifndef MSG_CMSG_CLOEXEC
2320 qemu_set_cloexec(fd);
2321#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002322 if (s->msgfd != -1)
2323 close(s->msgfd);
2324 s->msgfd = fd;
2325 }
2326}
2327
Mark McLoughlin9977c892009-07-22 09:11:38 +01002328static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2329{
2330 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002331 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002332 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002333 union {
2334 struct cmsghdr cmsg;
2335 char control[CMSG_SPACE(sizeof(int))];
2336 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002337 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002338 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002339
2340 iov[0].iov_base = buf;
2341 iov[0].iov_len = len;
2342
2343 msg.msg_iov = iov;
2344 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002345 msg.msg_control = &msg_control;
2346 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002347
Corey Bryant06138652012-08-14 16:43:42 -04002348#ifdef MSG_CMSG_CLOEXEC
2349 flags |= MSG_CMSG_CLOEXEC;
2350#endif
2351 ret = recvmsg(s->fd, &msg, flags);
2352 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002353 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002354 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002355
2356 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002357}
2358#else
2359static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2360{
2361 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002362 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002363}
2364#endif
2365
aliguori6f97dba2008-10-31 18:49:55 +00002366static void tcp_chr_read(void *opaque)
2367{
2368 CharDriverState *chr = opaque;
2369 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302370 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002371 int len, size;
2372
2373 if (!s->connected || s->max_size <= 0)
2374 return;
2375 len = sizeof(buf);
2376 if (len > s->max_size)
2377 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002378 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002379 if (size == 0) {
2380 /* connection closed */
2381 s->connected = 0;
2382 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002383 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002384 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002385 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002386 closesocket(s->fd);
2387 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002388 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002389 } else if (size > 0) {
2390 if (s->do_telnetopt)
2391 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2392 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002393 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002394 }
2395}
2396
Blue Swirl68c18d12010-08-15 09:46:24 +00002397#ifndef _WIN32
2398CharDriverState *qemu_chr_open_eventfd(int eventfd)
2399{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002400 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002401}
Blue Swirl68c18d12010-08-15 09:46:24 +00002402#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002403
aliguori6f97dba2008-10-31 18:49:55 +00002404static void tcp_chr_connect(void *opaque)
2405{
2406 CharDriverState *chr = opaque;
2407 TCPCharDriver *s = chr->opaque;
2408
2409 s->connected = 1;
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002410 if (s->fd >= 0) {
2411 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2412 tcp_chr_read, NULL, chr);
2413 }
Amit Shah127338e2009-11-03 19:59:56 +05302414 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002415}
2416
2417#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2418static void tcp_chr_telnet_init(int fd)
2419{
2420 char buf[3];
2421 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2422 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2423 send(fd, (char *)buf, 3, 0);
2424 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2425 send(fd, (char *)buf, 3, 0);
2426 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2427 send(fd, (char *)buf, 3, 0);
2428 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2429 send(fd, (char *)buf, 3, 0);
2430}
2431
Daniel P. Berrange13661082011-06-23 13:31:42 +01002432static int tcp_chr_add_client(CharDriverState *chr, int fd)
2433{
2434 TCPCharDriver *s = chr->opaque;
2435 if (s->fd != -1)
2436 return -1;
2437
2438 socket_set_nonblock(fd);
2439 if (s->do_nodelay)
2440 socket_set_nodelay(fd);
2441 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002442 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002443 tcp_chr_connect(chr);
2444
2445 return 0;
2446}
2447
aliguori6f97dba2008-10-31 18:49:55 +00002448static void tcp_chr_accept(void *opaque)
2449{
2450 CharDriverState *chr = opaque;
2451 TCPCharDriver *s = chr->opaque;
2452 struct sockaddr_in saddr;
2453#ifndef _WIN32
2454 struct sockaddr_un uaddr;
2455#endif
2456 struct sockaddr *addr;
2457 socklen_t len;
2458 int fd;
2459
2460 for(;;) {
2461#ifndef _WIN32
2462 if (s->is_unix) {
2463 len = sizeof(uaddr);
2464 addr = (struct sockaddr *)&uaddr;
2465 } else
2466#endif
2467 {
2468 len = sizeof(saddr);
2469 addr = (struct sockaddr *)&saddr;
2470 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002471 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002472 if (fd < 0 && errno != EINTR) {
2473 return;
2474 } else if (fd >= 0) {
2475 if (s->do_telnetopt)
2476 tcp_chr_telnet_init(fd);
2477 break;
2478 }
2479 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002480 if (tcp_chr_add_client(chr, fd) < 0)
2481 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002482}
2483
2484static void tcp_chr_close(CharDriverState *chr)
2485{
2486 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002487 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002488 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002489 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002490 }
2491 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002492 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002493 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002494 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002495 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002496 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002497}
2498
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002499static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2500 bool is_listen, bool is_telnet,
2501 bool is_waitconnect,
2502 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002503{
2504 CharDriverState *chr = NULL;
2505 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002506 char host[NI_MAXHOST], serv[NI_MAXSERV];
2507 const char *left = "", *right = "";
2508 struct sockaddr_storage ss;
2509 socklen_t ss_len = sizeof(ss);
2510
2511 memset(&ss, 0, ss_len);
2512 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2513 error_setg(errp, "getsockname: %s", strerror(errno));
2514 return NULL;
2515 }
2516
2517 chr = g_malloc0(sizeof(CharDriverState));
2518 s = g_malloc0(sizeof(TCPCharDriver));
2519
2520 s->connected = 0;
2521 s->fd = -1;
2522 s->listen_fd = -1;
2523 s->msgfd = -1;
2524
2525 chr->filename = g_malloc(256);
2526 switch (ss.ss_family) {
2527#ifndef _WIN32
2528 case AF_UNIX:
2529 s->is_unix = 1;
2530 snprintf(chr->filename, 256, "unix:%s%s",
2531 ((struct sockaddr_un *)(&ss))->sun_path,
2532 is_listen ? ",server" : "");
2533 break;
2534#endif
2535 case AF_INET6:
2536 left = "[";
2537 right = "]";
2538 /* fall through */
2539 case AF_INET:
2540 s->do_nodelay = do_nodelay;
2541 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2542 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2543 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2544 is_telnet ? "telnet" : "tcp",
2545 left, host, right, serv,
2546 is_listen ? ",server" : "");
2547 break;
2548 }
2549
2550 chr->opaque = s;
2551 chr->chr_write = tcp_chr_write;
2552 chr->chr_close = tcp_chr_close;
2553 chr->get_msgfd = tcp_get_msgfd;
2554 chr->chr_add_client = tcp_chr_add_client;
2555
2556 if (is_listen) {
2557 s->listen_fd = fd;
2558 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
2559 if (is_telnet) {
2560 s->do_telnetopt = 1;
2561 }
2562 } else {
2563 s->connected = 1;
2564 s->fd = fd;
2565 socket_set_nodelay(fd);
2566 tcp_chr_connect(chr);
2567 }
2568
2569 if (is_listen && is_waitconnect) {
2570 printf("QEMU waiting for connection on: %s\n",
2571 chr->filename);
2572 tcp_chr_accept(chr);
2573 socket_set_nonblock(s->listen_fd);
2574 }
2575 return chr;
2576}
2577
2578static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2579{
2580 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002581 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002582 int fd = -1;
2583 int is_listen;
2584 int is_waitconnect;
2585 int do_nodelay;
2586 int is_unix;
2587 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002588
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002589 is_listen = qemu_opt_get_bool(opts, "server", 0);
2590 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2591 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2592 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2593 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002594 if (!is_listen)
2595 is_waitconnect = 0;
2596
aliguorif07b6002008-11-11 20:54:09 +00002597 if (is_unix) {
2598 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002599 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002600 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002601 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002602 }
2603 } else {
2604 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002605 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002606 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002607 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002608 }
2609 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002610 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002611 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002612 }
aliguori6f97dba2008-10-31 18:49:55 +00002613
2614 if (!is_waitconnect)
2615 socket_set_nonblock(fd);
2616
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002617 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2618 is_waitconnect, &local_err);
2619 if (error_is_set(&local_err)) {
2620 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002621 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002622 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002623
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002624
aliguori6f97dba2008-10-31 18:49:55 +00002625 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002626 if (local_err) {
2627 qerror_report_err(local_err);
2628 error_free(local_err);
2629 }
2630 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002631 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002632 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002633 if (chr) {
2634 g_free(chr->opaque);
2635 g_free(chr);
2636 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002637 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002638}
2639
Luiz Capitulino999bd672010-10-22 16:09:05 -02002640/***********************************************************/
2641/* Memory chardev */
2642typedef struct {
2643 size_t outbuf_size;
2644 size_t outbuf_capacity;
2645 uint8_t *outbuf;
2646} MemoryDriver;
2647
2648static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2649{
2650 MemoryDriver *d = chr->opaque;
2651
2652 /* TODO: the QString implementation has the same code, we should
2653 * introduce a generic way to do this in cutils.c */
2654 if (d->outbuf_capacity < d->outbuf_size + len) {
2655 /* grow outbuf */
2656 d->outbuf_capacity += len;
2657 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002658 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002659 }
2660
2661 memcpy(d->outbuf + d->outbuf_size, buf, len);
2662 d->outbuf_size += len;
2663
2664 return len;
2665}
2666
2667void qemu_chr_init_mem(CharDriverState *chr)
2668{
2669 MemoryDriver *d;
2670
Anthony Liguori7267c092011-08-20 22:09:37 -05002671 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002672 d->outbuf_size = 0;
2673 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002674 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002675
2676 memset(chr, 0, sizeof(*chr));
2677 chr->opaque = d;
2678 chr->chr_write = mem_chr_write;
2679}
2680
2681QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2682{
2683 MemoryDriver *d = chr->opaque;
2684 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2685}
2686
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002687/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002688void qemu_chr_close_mem(CharDriverState *chr)
2689{
2690 MemoryDriver *d = chr->opaque;
2691
Anthony Liguori7267c092011-08-20 22:09:37 -05002692 g_free(d->outbuf);
2693 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002694 chr->opaque = NULL;
2695 chr->chr_write = NULL;
2696}
2697
2698size_t qemu_chr_mem_osize(const CharDriverState *chr)
2699{
2700 const MemoryDriver *d = chr->opaque;
2701 return d->outbuf_size;
2702}
2703
Lei Li51767e72013-01-25 00:03:19 +08002704/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002705/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002706
2707typedef struct {
2708 size_t size;
2709 size_t prod;
2710 size_t cons;
2711 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002712} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002713
Markus Armbruster3949e592013-02-06 21:27:24 +01002714static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002715{
Markus Armbruster3949e592013-02-06 21:27:24 +01002716 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002717
Markus Armbruster5c230102013-02-06 21:27:23 +01002718 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002719}
2720
Markus Armbruster3949e592013-02-06 21:27:24 +01002721static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002722{
Markus Armbruster3949e592013-02-06 21:27:24 +01002723 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002724 int i;
2725
2726 if (!buf || (len < 0)) {
2727 return -1;
2728 }
2729
2730 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002731 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2732 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002733 d->cons = d->prod - d->size;
2734 }
2735 }
2736
2737 return 0;
2738}
2739
Markus Armbruster3949e592013-02-06 21:27:24 +01002740static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002741{
Markus Armbruster3949e592013-02-06 21:27:24 +01002742 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002743 int i;
2744
Markus Armbruster5c230102013-02-06 21:27:23 +01002745 for (i = 0; i < len && d->cons != d->prod; i++) {
2746 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002747 }
2748
2749 return i;
2750}
2751
Markus Armbruster3949e592013-02-06 21:27:24 +01002752static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002753{
Markus Armbruster3949e592013-02-06 21:27:24 +01002754 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002755
2756 g_free(d->cbuf);
2757 g_free(d);
2758 chr->opaque = NULL;
2759}
2760
Markus Armbruster3949e592013-02-06 21:27:24 +01002761static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002762{
2763 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002764 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002765
2766 chr = g_malloc0(sizeof(CharDriverState));
2767 d = g_malloc(sizeof(*d));
2768
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002769 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002770 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002771 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002772 }
2773
2774 /* The size must be power of 2 */
2775 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002776 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002777 goto fail;
2778 }
2779
2780 d->prod = 0;
2781 d->cons = 0;
2782 d->cbuf = g_malloc0(d->size);
2783
2784 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002785 chr->chr_write = ringbuf_chr_write;
2786 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002787
2788 return chr;
2789
2790fail:
2791 g_free(d);
2792 g_free(chr);
2793 return NULL;
2794}
2795
Markus Armbruster3949e592013-02-06 21:27:24 +01002796static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002797{
Markus Armbruster3949e592013-02-06 21:27:24 +01002798 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002799}
2800
Markus Armbruster3949e592013-02-06 21:27:24 +01002801void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002802 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002803 Error **errp)
2804{
2805 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002806 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002807 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002808 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002809
2810 chr = qemu_chr_find(device);
2811 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002812 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002813 return;
2814 }
2815
Markus Armbruster3949e592013-02-06 21:27:24 +01002816 if (!chr_is_ringbuf(chr)) {
2817 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002818 return;
2819 }
2820
Lei Li1f590cf2013-01-25 00:03:20 +08002821 if (has_format && (format == DATA_FORMAT_BASE64)) {
2822 write_data = g_base64_decode(data, &write_count);
2823 } else {
2824 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002825 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002826 }
2827
Markus Armbruster3949e592013-02-06 21:27:24 +01002828 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002829
Markus Armbruster13289fb2013-02-06 21:27:18 +01002830 if (write_data != (uint8_t *)data) {
2831 g_free((void *)write_data);
2832 }
2833
Lei Li1f590cf2013-01-25 00:03:20 +08002834 if (ret < 0) {
2835 error_setg(errp, "Failed to write to device %s", device);
2836 return;
2837 }
2838}
2839
Markus Armbruster3949e592013-02-06 21:27:24 +01002840char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002841 bool has_format, enum DataFormat format,
2842 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002843{
2844 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002845 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002846 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002847 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002848
2849 chr = qemu_chr_find(device);
2850 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002851 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002852 return NULL;
2853 }
2854
Markus Armbruster3949e592013-02-06 21:27:24 +01002855 if (!chr_is_ringbuf(chr)) {
2856 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002857 return NULL;
2858 }
2859
2860 if (size <= 0) {
2861 error_setg(errp, "size must be greater than zero");
2862 return NULL;
2863 }
2864
Markus Armbruster3949e592013-02-06 21:27:24 +01002865 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002866 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002867 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002868
Markus Armbruster3949e592013-02-06 21:27:24 +01002869 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002870
2871 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002872 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002873 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002874 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002875 /*
2876 * FIXME should read only complete, valid UTF-8 characters up
2877 * to @size bytes. Invalid sequences should be replaced by a
2878 * suitable replacement character. Except when (and only
2879 * when) ring buffer lost characters since last read, initial
2880 * continuation characters should be dropped.
2881 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002882 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002883 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002884 }
2885
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002886 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002887}
2888
Gerd Hoffmann33521632009-12-08 13:11:49 +01002889QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002890{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002891 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002892 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002893 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002894 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002895 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002896
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002897 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2898 if (error_is_set(&local_err)) {
2899 qerror_report_err(local_err);
2900 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002901 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002902 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002903
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002904 if (strstart(filename, "mon:", &p)) {
2905 filename = p;
2906 qemu_opt_set(opts, "mux", "on");
2907 }
2908
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002909 if (strcmp(filename, "null") == 0 ||
2910 strcmp(filename, "pty") == 0 ||
2911 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002912 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002913 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002914 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002915 return opts;
2916 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002917 if (strstart(filename, "vc", &p)) {
2918 qemu_opt_set(opts, "backend", "vc");
2919 if (*p == ':') {
2920 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2921 /* pixels */
2922 qemu_opt_set(opts, "width", width);
2923 qemu_opt_set(opts, "height", height);
2924 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2925 /* chars */
2926 qemu_opt_set(opts, "cols", width);
2927 qemu_opt_set(opts, "rows", height);
2928 } else {
2929 goto fail;
2930 }
2931 }
2932 return opts;
2933 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002934 if (strcmp(filename, "con:") == 0) {
2935 qemu_opt_set(opts, "backend", "console");
2936 return opts;
2937 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002938 if (strstart(filename, "COM", NULL)) {
2939 qemu_opt_set(opts, "backend", "serial");
2940 qemu_opt_set(opts, "path", filename);
2941 return opts;
2942 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002943 if (strstart(filename, "file:", &p)) {
2944 qemu_opt_set(opts, "backend", "file");
2945 qemu_opt_set(opts, "path", p);
2946 return opts;
2947 }
2948 if (strstart(filename, "pipe:", &p)) {
2949 qemu_opt_set(opts, "backend", "pipe");
2950 qemu_opt_set(opts, "path", p);
2951 return opts;
2952 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002953 if (strstart(filename, "tcp:", &p) ||
2954 strstart(filename, "telnet:", &p)) {
2955 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2956 host[0] = 0;
2957 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2958 goto fail;
2959 }
2960 qemu_opt_set(opts, "backend", "socket");
2961 qemu_opt_set(opts, "host", host);
2962 qemu_opt_set(opts, "port", port);
2963 if (p[pos] == ',') {
2964 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2965 goto fail;
2966 }
2967 if (strstart(filename, "telnet:", &p))
2968 qemu_opt_set(opts, "telnet", "on");
2969 return opts;
2970 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002971 if (strstart(filename, "udp:", &p)) {
2972 qemu_opt_set(opts, "backend", "udp");
2973 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2974 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002975 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002976 goto fail;
2977 }
2978 }
2979 qemu_opt_set(opts, "host", host);
2980 qemu_opt_set(opts, "port", port);
2981 if (p[pos] == '@') {
2982 p += pos + 1;
2983 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2984 host[0] = 0;
2985 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002986 goto fail;
2987 }
2988 }
2989 qemu_opt_set(opts, "localaddr", host);
2990 qemu_opt_set(opts, "localport", port);
2991 }
2992 return opts;
2993 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002994 if (strstart(filename, "unix:", &p)) {
2995 qemu_opt_set(opts, "backend", "socket");
2996 if (qemu_opts_do_parse(opts, p, "path") != 0)
2997 goto fail;
2998 return opts;
2999 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003000 if (strstart(filename, "/dev/parport", NULL) ||
3001 strstart(filename, "/dev/ppi", NULL)) {
3002 qemu_opt_set(opts, "backend", "parport");
3003 qemu_opt_set(opts, "path", filename);
3004 return opts;
3005 }
3006 if (strstart(filename, "/dev/", NULL)) {
3007 qemu_opt_set(opts, "backend", "tty");
3008 qemu_opt_set(opts, "path", filename);
3009 return opts;
3010 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003011
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003012fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003013 qemu_opts_del(opts);
3014 return NULL;
3015}
3016
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003017#ifdef HAVE_CHARDEV_PARPORT
3018
3019static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3020{
3021 const char *filename = qemu_opt_get(opts, "path");
3022 int fd;
3023
3024 fd = qemu_open(filename, O_RDWR);
3025 if (fd < 0) {
3026 return NULL;
3027 }
3028 return qemu_chr_open_pp_fd(fd);
3029}
3030
3031#endif
3032
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003033static const struct {
3034 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01003035 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003036} backend_table[] = {
3037 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003038 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003039 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003040 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Anthony Liguorid82831d2013-02-20 07:43:19 -06003041 { .name = "vc", .open = vc_init },
Markus Armbruster3949e592013-02-06 21:27:24 +01003042 { .name = "memory", .open = qemu_chr_open_ringbuf },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003043#ifdef _WIN32
3044 { .name = "file", .open = qemu_chr_open_win_file_out },
3045 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003046 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003047 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02003048 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003049#else
3050 { .name = "file", .open = qemu_chr_open_file_out },
3051 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02003052 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003053#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003054#ifdef CONFIG_BRLAPI
3055 { .name = "braille", .open = chr_baum_init },
3056#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003057#ifdef HAVE_CHARDEV_TTY
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003058 { .name = "tty", .open = qemu_chr_open_tty },
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003059 { .name = "serial", .open = qemu_chr_open_tty },
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003060 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003061#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003062#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003063 { .name = "parallel", .open = qemu_chr_open_pp },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003064 { .name = "parport", .open = qemu_chr_open_pp },
3065#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003066#ifdef CONFIG_SPICE
3067 { .name = "spicevmc", .open = qemu_chr_open_spice },
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01003068#if SPICE_SERVER_VERSION >= 0x000c02
3069 { .name = "spiceport", .open = qemu_chr_open_spice_port },
3070#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003071#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003072};
3073
Anthony Liguorif69554b2011-08-15 11:17:37 -05003074CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003075 void (*init)(struct CharDriverState *s),
3076 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003077{
3078 CharDriverState *chr;
3079 int i;
3080
3081 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003082 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003083 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003084 }
3085
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003086 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003087 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003088 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003089 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003090 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003091 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
3092 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
3093 break;
3094 }
3095 if (i == ARRAY_SIZE(backend_table)) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003096 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003097 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003098 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003099 }
3100
Markus Armbruster1f514702012-02-07 15:09:08 +01003101 chr = backend_table[i].open(opts);
3102 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003103 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003104 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003105 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003106 }
3107
3108 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003109 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003110 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003111 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003112
3113 if (qemu_opt_get_bool(opts, "mux", 0)) {
3114 CharDriverState *base = chr;
3115 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003116 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003117 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3118 chr = qemu_chr_open_mux(base);
3119 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003120 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003121 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003122 } else {
3123 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003124 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003125 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003126 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003127 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003128
3129err:
3130 qemu_opts_del(opts);
3131 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003132}
3133
Anthony Liguori27143a42011-08-15 11:17:36 -05003134CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003135{
3136 const char *p;
3137 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003138 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003139 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003140
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003141 if (strstart(filename, "chardev:", &p)) {
3142 return qemu_chr_find(p);
3143 }
3144
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003145 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003146 if (!opts)
3147 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003148
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003149 chr = qemu_chr_new_from_opts(opts, init, &err);
3150 if (error_is_set(&err)) {
3151 fprintf(stderr, "%s\n", error_get_pretty(err));
3152 error_free(err);
3153 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003154 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3155 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003156 }
3157 return chr;
3158}
3159
Anthony Liguori15f31512011-08-15 11:17:35 -05003160void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003161{
3162 if (chr->chr_set_echo) {
3163 chr->chr_set_echo(chr, echo);
3164 }
3165}
3166
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003167void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003168{
3169 if (chr->chr_guest_open) {
3170 chr->chr_guest_open(chr);
3171 }
3172}
3173
Anthony Liguori28178222011-08-15 11:17:33 -05003174void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003175{
3176 if (chr->chr_guest_close) {
3177 chr->chr_guest_close(chr);
3178 }
3179}
3180
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003181void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003182{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003183 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003184 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003185 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003186 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003187 g_free(chr->filename);
3188 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003189 if (chr->opts) {
3190 qemu_opts_del(chr->opts);
3191 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003192 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003193}
3194
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003195ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003196{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003197 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003198 CharDriverState *chr;
3199
Blue Swirl72cf2d42009-09-12 07:36:22 +00003200 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003201 ChardevInfoList *info = g_malloc0(sizeof(*info));
3202 info->value = g_malloc0(sizeof(*info->value));
3203 info->value->label = g_strdup(chr->label);
3204 info->value->filename = g_strdup(chr->filename);
3205
3206 info->next = chr_list;
3207 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003208 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003209
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003210 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003211}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003212
3213CharDriverState *qemu_chr_find(const char *name)
3214{
3215 CharDriverState *chr;
3216
Blue Swirl72cf2d42009-09-12 07:36:22 +00003217 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003218 if (strcmp(chr->label, name) != 0)
3219 continue;
3220 return chr;
3221 }
3222 return NULL;
3223}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003224
3225/* Get a character (serial) device interface. */
3226CharDriverState *qemu_char_get_next_serial(void)
3227{
3228 static int next_serial;
3229
3230 /* FIXME: This function needs to go away: use chardev properties! */
3231 return serial_hds[next_serial++];
3232}
3233
Paolo Bonzini4d454572012-11-26 16:03:42 +01003234QemuOptsList qemu_chardev_opts = {
3235 .name = "chardev",
3236 .implied_opt_name = "backend",
3237 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3238 .desc = {
3239 {
3240 .name = "backend",
3241 .type = QEMU_OPT_STRING,
3242 },{
3243 .name = "path",
3244 .type = QEMU_OPT_STRING,
3245 },{
3246 .name = "host",
3247 .type = QEMU_OPT_STRING,
3248 },{
3249 .name = "port",
3250 .type = QEMU_OPT_STRING,
3251 },{
3252 .name = "localaddr",
3253 .type = QEMU_OPT_STRING,
3254 },{
3255 .name = "localport",
3256 .type = QEMU_OPT_STRING,
3257 },{
3258 .name = "to",
3259 .type = QEMU_OPT_NUMBER,
3260 },{
3261 .name = "ipv4",
3262 .type = QEMU_OPT_BOOL,
3263 },{
3264 .name = "ipv6",
3265 .type = QEMU_OPT_BOOL,
3266 },{
3267 .name = "wait",
3268 .type = QEMU_OPT_BOOL,
3269 },{
3270 .name = "server",
3271 .type = QEMU_OPT_BOOL,
3272 },{
3273 .name = "delay",
3274 .type = QEMU_OPT_BOOL,
3275 },{
3276 .name = "telnet",
3277 .type = QEMU_OPT_BOOL,
3278 },{
3279 .name = "width",
3280 .type = QEMU_OPT_NUMBER,
3281 },{
3282 .name = "height",
3283 .type = QEMU_OPT_NUMBER,
3284 },{
3285 .name = "cols",
3286 .type = QEMU_OPT_NUMBER,
3287 },{
3288 .name = "rows",
3289 .type = QEMU_OPT_NUMBER,
3290 },{
3291 .name = "mux",
3292 .type = QEMU_OPT_BOOL,
3293 },{
3294 .name = "signal",
3295 .type = QEMU_OPT_BOOL,
3296 },{
3297 .name = "name",
3298 .type = QEMU_OPT_STRING,
3299 },{
3300 .name = "debug",
3301 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003302 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003303 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003304 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003305 },
3306 { /* end of list */ }
3307 },
3308};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003309
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003310#ifdef _WIN32
3311
3312static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3313{
3314 HANDLE out;
3315
3316 if (file->in) {
3317 error_setg(errp, "input file not supported");
3318 return NULL;
3319 }
3320
3321 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3322 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3323 if (out == INVALID_HANDLE_VALUE) {
3324 error_setg(errp, "open %s failed", file->out);
3325 return NULL;
3326 }
3327 return qemu_chr_open_win_file(out);
3328}
3329
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003330static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3331 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003332{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003333 return qemu_chr_open_win_path(serial->device);
3334}
3335
3336static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3337 Error **errp)
3338{
3339 error_setg(errp, "character device backend type 'parallel' not supported");
3340 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003341}
3342
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003343#else /* WIN32 */
3344
3345static int qmp_chardev_open_file_source(char *src, int flags,
3346 Error **errp)
3347{
3348 int fd = -1;
3349
3350 TFR(fd = qemu_open(src, flags, 0666));
3351 if (fd == -1) {
3352 error_setg(errp, "open %s: %s", src, strerror(errno));
3353 }
3354 return fd;
3355}
3356
3357static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3358{
3359 int flags, in = -1, out = -1;
3360
3361 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3362 out = qmp_chardev_open_file_source(file->out, flags, errp);
3363 if (error_is_set(errp)) {
3364 return NULL;
3365 }
3366
3367 if (file->in) {
3368 flags = O_RDONLY;
3369 in = qmp_chardev_open_file_source(file->in, flags, errp);
3370 if (error_is_set(errp)) {
3371 qemu_close(out);
3372 return NULL;
3373 }
3374 }
3375
3376 return qemu_chr_open_fd(in, out);
3377}
3378
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003379static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3380 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003381{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003382#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003383 int fd;
3384
3385 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3386 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003387 return NULL;
3388 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003389 socket_set_nonblock(fd);
3390 return qemu_chr_open_tty_fd(fd);
3391#else
3392 error_setg(errp, "character device backend type 'serial' not supported");
3393 return NULL;
3394#endif
3395}
3396
3397static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3398 Error **errp)
3399{
3400#ifdef HAVE_CHARDEV_PARPORT
3401 int fd;
3402
3403 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3404 if (error_is_set(errp)) {
3405 return NULL;
3406 }
3407 return qemu_chr_open_pp_fd(fd);
3408#else
3409 error_setg(errp, "character device backend type 'parallel' not supported");
3410 return NULL;
3411#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003412}
3413
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003414#endif /* WIN32 */
3415
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003416static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3417 Error **errp)
3418{
3419 SocketAddress *addr = sock->addr;
3420 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3421 bool is_listen = sock->has_server ? sock->server : true;
3422 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3423 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3424 int fd;
3425
3426 if (is_listen) {
3427 fd = socket_listen(addr, errp);
3428 } else {
3429 fd = socket_connect(addr, errp, NULL, NULL);
3430 }
3431 if (error_is_set(errp)) {
3432 return NULL;
3433 }
3434 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3435 is_telnet, is_waitconnect, errp);
3436}
3437
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003438ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3439 Error **errp)
3440{
3441 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3442 CharDriverState *chr = NULL;
3443
3444 chr = qemu_chr_find(id);
3445 if (chr) {
3446 error_setg(errp, "Chardev '%s' already exists", id);
3447 g_free(ret);
3448 return NULL;
3449 }
3450
3451 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003452 case CHARDEV_BACKEND_KIND_FILE:
3453 chr = qmp_chardev_open_file(backend->file, errp);
3454 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003455 case CHARDEV_BACKEND_KIND_SERIAL:
3456 chr = qmp_chardev_open_serial(backend->serial, errp);
3457 break;
3458 case CHARDEV_BACKEND_KIND_PARALLEL:
3459 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003460 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003461 case CHARDEV_BACKEND_KIND_SOCKET:
3462 chr = qmp_chardev_open_socket(backend->socket, errp);
3463 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003464#ifdef HAVE_CHARDEV_TTY
3465 case CHARDEV_BACKEND_KIND_PTY:
3466 {
3467 /* qemu_chr_open_pty sets "path" in opts */
3468 QemuOpts *opts;
3469 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3470 chr = qemu_chr_open_pty(opts);
3471 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3472 ret->has_pty = true;
3473 qemu_opts_del(opts);
3474 break;
3475 }
3476#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003477 case CHARDEV_BACKEND_KIND_NULL:
3478 chr = qemu_chr_open_null(NULL);
3479 break;
3480 default:
3481 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3482 break;
3483 }
3484
3485 if (chr == NULL && !error_is_set(errp)) {
3486 error_setg(errp, "Failed to create chardev");
3487 }
3488 if (chr) {
3489 chr->label = g_strdup(id);
3490 chr->avail_connections = 1;
3491 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3492 return ret;
3493 } else {
3494 g_free(ret);
3495 return NULL;
3496 }
3497}
3498
3499void qmp_chardev_remove(const char *id, Error **errp)
3500{
3501 CharDriverState *chr;
3502
3503 chr = qemu_chr_find(id);
3504 if (NULL == chr) {
3505 error_setg(errp, "Chardev '%s' not found", id);
3506 return;
3507 }
3508 if (chr->chr_can_read || chr->chr_read ||
3509 chr->chr_event || chr->handler_opaque) {
3510 error_setg(errp, "Chardev '%s' is busy", id);
3511 return;
3512 }
3513 qemu_chr_delete(chr);
3514}