blob: 4c63ccbd78027fb3e45c9c4e6b7c9e65f301dff6 [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 +0530544typedef struct IOWatchPoll
545{
546 GSource *src;
547 int max_size;
548
549 IOCanReadHandler *fd_can_read;
550 void *opaque;
551
552 QTAILQ_ENTRY(IOWatchPoll) node;
553} IOWatchPoll;
554
555static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
556 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);
557
558static IOWatchPoll *io_watch_poll_from_source(GSource *source)
559{
560 IOWatchPoll *i;
561
562 QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
563 if (i->src == source) {
564 return i;
565 }
566 }
567
568 return NULL;
569}
570
571static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
572{
573 IOWatchPoll *iwp = io_watch_poll_from_source(source);
574
575 iwp->max_size = iwp->fd_can_read(iwp->opaque);
576 if (iwp->max_size == 0) {
577 return FALSE;
578 }
579
580 return g_io_watch_funcs.prepare(source, timeout_);
581}
582
583static gboolean io_watch_poll_check(GSource *source)
584{
585 IOWatchPoll *iwp = io_watch_poll_from_source(source);
586
587 if (iwp->max_size == 0) {
588 return FALSE;
589 }
590
591 return g_io_watch_funcs.check(source);
592}
593
594static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
595 gpointer user_data)
596{
597 return g_io_watch_funcs.dispatch(source, callback, user_data);
598}
599
600static void io_watch_poll_finalize(GSource *source)
601{
602 IOWatchPoll *iwp = io_watch_poll_from_source(source);
603 QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
604 g_io_watch_funcs.finalize(source);
605}
606
607static GSourceFuncs io_watch_poll_funcs = {
608 .prepare = io_watch_poll_prepare,
609 .check = io_watch_poll_check,
610 .dispatch = io_watch_poll_dispatch,
611 .finalize = io_watch_poll_finalize,
612};
613
614/* Can only be used for read */
615static guint io_add_watch_poll(GIOChannel *channel,
616 IOCanReadHandler *fd_can_read,
617 GIOFunc fd_read,
618 gpointer user_data)
619{
620 IOWatchPoll *iwp;
621 GSource *src;
622 guint tag;
623
624 src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
625 g_source_set_funcs(src, &io_watch_poll_funcs);
626 g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
627 tag = g_source_attach(src, NULL);
628 g_source_unref(src);
629
630 iwp = g_malloc0(sizeof(*iwp));
631 iwp->src = src;
632 iwp->max_size = 0;
633 iwp->fd_can_read = fd_can_read;
634 iwp->opaque = user_data;
635
636 QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);
637
638 return tag;
639}
640
641static GIOChannel *io_channel_from_fd(int fd)
642{
643 GIOChannel *chan;
644
645 if (fd == -1) {
646 return NULL;
647 }
648
649 chan = g_io_channel_unix_new(fd);
650
651 g_io_channel_set_encoding(chan, NULL, NULL);
652 g_io_channel_set_buffered(chan, FALSE);
653
654 return chan;
655}
656
657static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
658{
659 GIOStatus status;
660 gsize bytes_written;
661 int len;
662 const uint8_t *buf = _buf;
663
664 len = len1;
665 while (len > 0) {
666 status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
667 &bytes_written, NULL);
668 if (status != G_IO_STATUS_NORMAL) {
669 if (status != G_IO_STATUS_AGAIN) {
670 return -1;
671 }
672 } else if (status == G_IO_STATUS_EOF) {
673 break;
674 } else {
675 buf += bytes_written;
676 len -= bytes_written;
677 }
678 }
679 return len1 - len;
680}
Anthony Liguori96c63842013-03-05 23:21:18 +0530681
Anthony Liguoria29753f2013-03-05 23:21:19 +0530682typedef struct FDCharDriver {
683 CharDriverState *chr;
684 GIOChannel *fd_in, *fd_out;
685 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000686 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530687 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000688} FDCharDriver;
689
aliguori6f97dba2008-10-31 18:49:55 +0000690static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
691{
692 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530693
694 return io_channel_send_all(s->fd_out, buf, len);
695}
696
697static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
698{
699 CharDriverState *chr = opaque;
700 FDCharDriver *s = chr->opaque;
701 int len;
702 uint8_t buf[READ_BUF_LEN];
703 GIOStatus status;
704 gsize bytes_read;
705
706 len = sizeof(buf);
707 if (len > s->max_size) {
708 len = s->max_size;
709 }
710 if (len == 0) {
711 return FALSE;
712 }
713
714 status = g_io_channel_read_chars(chan, (gchar *)buf,
715 len, &bytes_read, NULL);
716 if (status == G_IO_STATUS_EOF) {
717 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
718 return FALSE;
719 }
720 if (status == G_IO_STATUS_NORMAL) {
721 qemu_chr_be_write(chr, buf, bytes_read);
722 }
723
724 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000725}
726
727static int fd_chr_read_poll(void *opaque)
728{
729 CharDriverState *chr = opaque;
730 FDCharDriver *s = chr->opaque;
731
Anthony Liguori909cda12011-08-15 11:17:31 -0500732 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000733 return s->max_size;
734}
735
aliguori6f97dba2008-10-31 18:49:55 +0000736static void fd_chr_update_read_handler(CharDriverState *chr)
737{
738 FDCharDriver *s = chr->opaque;
739
Anthony Liguoria29753f2013-03-05 23:21:19 +0530740 if (s->fd_in_tag) {
741 g_source_remove(s->fd_in_tag);
742 }
743
744 if (s->fd_in) {
745 s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000746 }
747}
748
749static void fd_chr_close(struct CharDriverState *chr)
750{
751 FDCharDriver *s = chr->opaque;
752
Anthony Liguoria29753f2013-03-05 23:21:19 +0530753 if (s->fd_in_tag) {
754 g_source_remove(s->fd_in_tag);
755 s->fd_in_tag = 0;
756 }
757
758 if (s->fd_in) {
759 g_io_channel_unref(s->fd_in);
760 }
761 if (s->fd_out) {
762 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000763 }
764
Anthony Liguori7267c092011-08-20 22:09:37 -0500765 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100766 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000767}
768
769/* open a character device to a unix fd */
770static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
771{
772 CharDriverState *chr;
773 FDCharDriver *s;
774
Anthony Liguori7267c092011-08-20 22:09:37 -0500775 chr = g_malloc0(sizeof(CharDriverState));
776 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530777 s->fd_in = io_channel_from_fd(fd_in);
778 s->fd_out = io_channel_from_fd(fd_out);
779 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000780 chr->opaque = s;
781 chr->chr_write = fd_chr_write;
782 chr->chr_update_read_handler = fd_chr_update_read_handler;
783 chr->chr_close = fd_chr_close;
784
Amit Shah127338e2009-11-03 19:59:56 +0530785 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000786
787 return chr;
788}
789
Markus Armbruster1f514702012-02-07 15:09:08 +0100790static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000791{
792 int fd_out;
793
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100794 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200795 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100796 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100797 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100798 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100799 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000800}
801
Markus Armbruster1f514702012-02-07 15:09:08 +0100802static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000803{
804 int fd_in, fd_out;
805 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200806 const char *filename = qemu_opt_get(opts, "path");
807
808 if (filename == NULL) {
809 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100810 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200811 }
aliguori6f97dba2008-10-31 18:49:55 +0000812
813 snprintf(filename_in, 256, "%s.in", filename);
814 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100815 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
816 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000817 if (fd_in < 0 || fd_out < 0) {
818 if (fd_in >= 0)
819 close(fd_in);
820 if (fd_out >= 0)
821 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100822 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100823 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100824 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100825 }
aliguori6f97dba2008-10-31 18:49:55 +0000826 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100827 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000828}
829
aliguori6f97dba2008-10-31 18:49:55 +0000830/* init terminal so that we can grab keys */
831static struct termios oldtty;
832static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100833static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000834
835static void term_exit(void)
836{
837 tcsetattr (0, TCSANOW, &oldtty);
838 fcntl(0, F_SETFL, old_fd0_flags);
839}
840
Paolo Bonzinibb002512010-12-23 13:42:50 +0100841static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000842{
843 struct termios tty;
844
Paolo Bonzini03693642010-12-23 13:42:49 +0100845 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100846 if (!echo) {
847 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000848 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100849 tty.c_oflag |= OPOST;
850 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
851 tty.c_cflag &= ~(CSIZE|PARENB);
852 tty.c_cflag |= CS8;
853 tty.c_cc[VMIN] = 1;
854 tty.c_cc[VTIME] = 0;
855 }
aliguori6f97dba2008-10-31 18:49:55 +0000856 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100857 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000858 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000859
860 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000861}
862
863static void qemu_chr_close_stdio(struct CharDriverState *chr)
864{
865 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000866 fd_chr_close(chr);
867}
868
Markus Armbruster1f514702012-02-07 15:09:08 +0100869static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000870{
871 CharDriverState *chr;
872
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400873 if (is_daemonized()) {
874 error_report("cannot use stdio with -daemonize");
875 return NULL;
876 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530877 old_fd0_flags = fcntl(0, F_GETFL);
878 tcgetattr (0, &oldtty);
879 fcntl(0, F_SETFL, O_NONBLOCK);
880 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100881
aliguori6f97dba2008-10-31 18:49:55 +0000882 chr = qemu_chr_open_fd(0, 1);
883 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100884 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100885 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
886 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500887 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000888
Markus Armbruster1f514702012-02-07 15:09:08 +0100889 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000890}
891
892#ifdef __sun__
893/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000894static int openpty(int *amaster, int *aslave, char *name,
895 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000896{
897 const char *slave;
898 int mfd = -1, sfd = -1;
899
900 *amaster = *aslave = -1;
901
902 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
903 if (mfd < 0)
904 goto err;
905
906 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
907 goto err;
908
909 if ((slave = ptsname(mfd)) == NULL)
910 goto err;
911
912 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
913 goto err;
914
915 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
916 (termp != NULL && tcgetattr(sfd, termp) < 0))
917 goto err;
918
919 if (amaster)
920 *amaster = mfd;
921 if (aslave)
922 *aslave = sfd;
923 if (winp)
924 ioctl(sfd, TIOCSWINSZ, winp);
925
926 return 0;
927
928err:
929 if (sfd != -1)
930 close(sfd);
931 close(mfd);
932 return -1;
933}
934
blueswir13f4cb3d2009-04-13 16:31:01 +0000935static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000936{
937 termios_p->c_iflag &=
938 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
939 termios_p->c_oflag &= ~OPOST;
940 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
941 termios_p->c_cflag &= ~(CSIZE|PARENB);
942 termios_p->c_cflag |= CS8;
943
944 termios_p->c_cc[VMIN] = 0;
945 termios_p->c_cc[VTIME] = 0;
946}
947#endif
948
949#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100950 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
951 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000952
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100953#define HAVE_CHARDEV_TTY 1
954
aliguori6f97dba2008-10-31 18:49:55 +0000955typedef struct {
956 int fd;
957 int connected;
958 int polling;
959 int read_bytes;
960 QEMUTimer *timer;
961} PtyCharDriver;
962
963static void pty_chr_update_read_handler(CharDriverState *chr);
964static void pty_chr_state(CharDriverState *chr, int connected);
965
966static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
967{
968 PtyCharDriver *s = chr->opaque;
969
970 if (!s->connected) {
971 /* guest sends data, check for (re-)connect */
972 pty_chr_update_read_handler(chr);
973 return 0;
974 }
975 return send_all(s->fd, buf, len);
976}
977
978static int pty_chr_read_poll(void *opaque)
979{
980 CharDriverState *chr = opaque;
981 PtyCharDriver *s = chr->opaque;
982
Anthony Liguori909cda12011-08-15 11:17:31 -0500983 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000984 return s->read_bytes;
985}
986
987static void pty_chr_read(void *opaque)
988{
989 CharDriverState *chr = opaque;
990 PtyCharDriver *s = chr->opaque;
991 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530992 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000993
994 len = sizeof(buf);
995 if (len > s->read_bytes)
996 len = s->read_bytes;
997 if (len == 0)
998 return;
999 size = read(s->fd, buf, len);
1000 if ((size == -1 && errno == EIO) ||
1001 (size == 0)) {
1002 pty_chr_state(chr, 0);
1003 return;
1004 }
1005 if (size > 0) {
1006 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001007 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001008 }
1009}
1010
1011static void pty_chr_update_read_handler(CharDriverState *chr)
1012{
1013 PtyCharDriver *s = chr->opaque;
1014
1015 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
1016 pty_chr_read, NULL, chr);
1017 s->polling = 1;
1018 /*
1019 * Short timeout here: just need wait long enougth that qemu makes
1020 * it through the poll loop once. When reconnected we want a
1021 * short timeout so we notice it almost instantly. Otherwise
1022 * read() gives us -EIO instantly, making pty_chr_state() reset the
1023 * timeout to the normal (much longer) poll interval before the
1024 * timer triggers.
1025 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001026 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +00001027}
1028
1029static void pty_chr_state(CharDriverState *chr, int connected)
1030{
1031 PtyCharDriver *s = chr->opaque;
1032
1033 if (!connected) {
1034 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
1035 s->connected = 0;
1036 s->polling = 0;
1037 /* (re-)connect poll interval for idle guests: once per second.
1038 * We check more frequently in case the guests sends data to
1039 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001040 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001041 } else {
1042 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301043 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001044 s->connected = 1;
1045 }
1046}
1047
1048static void pty_chr_timer(void *opaque)
1049{
1050 struct CharDriverState *chr = opaque;
1051 PtyCharDriver *s = chr->opaque;
1052
1053 if (s->connected)
1054 return;
1055 if (s->polling) {
1056 /* If we arrive here without polling being cleared due
1057 * read returning -EIO, then we are (re-)connected */
1058 pty_chr_state(chr, 1);
1059 return;
1060 }
1061
1062 /* Next poll ... */
1063 pty_chr_update_read_handler(chr);
1064}
1065
1066static void pty_chr_close(struct CharDriverState *chr)
1067{
1068 PtyCharDriver *s = chr->opaque;
1069
1070 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
1071 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00001072 qemu_del_timer(s->timer);
1073 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -05001074 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001075 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001076}
1077
Markus Armbruster1f514702012-02-07 15:09:08 +01001078static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001079{
1080 CharDriverState *chr;
1081 PtyCharDriver *s;
1082 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001083 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001084 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001085#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001086 char pty_name[PATH_MAX];
1087#define q_ptsname(x) pty_name
1088#else
1089 char *pty_name = NULL;
1090#define q_ptsname(x) ptsname(x)
1091#endif
1092
Markus Armbrustera4e26042011-11-11 10:40:05 +01001093 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001094 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001095 }
1096
1097 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001098 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001099 cfmakeraw(&tty);
1100 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1101 close(slave_fd);
1102
Markus Armbrustera4e26042011-11-11 10:40:05 +01001103 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001104
Markus Armbrustera4e26042011-11-11 10:40:05 +01001105 len = strlen(q_ptsname(master_fd)) + 5;
1106 chr->filename = g_malloc(len);
1107 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1108 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001109
1110 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001111 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1112 q_ptsname(master_fd),
1113 label ? " (label " : "",
1114 label ? label : "",
1115 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001116
1117 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001118 chr->opaque = s;
1119 chr->chr_write = pty_chr_write;
1120 chr->chr_update_read_handler = pty_chr_update_read_handler;
1121 chr->chr_close = pty_chr_close;
1122
Markus Armbrustera4e26042011-11-11 10:40:05 +01001123 s->fd = master_fd;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001124 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001125
Markus Armbruster1f514702012-02-07 15:09:08 +01001126 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001127}
1128
1129static void tty_serial_init(int fd, int speed,
1130 int parity, int data_bits, int stop_bits)
1131{
1132 struct termios tty;
1133 speed_t spd;
1134
1135#if 0
1136 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1137 speed, parity, data_bits, stop_bits);
1138#endif
1139 tcgetattr (fd, &tty);
1140
Stefan Weil45eea132009-10-26 16:10:10 +01001141#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1142 speed = speed * 10 / 11;
1143 do {
1144 check_speed(50);
1145 check_speed(75);
1146 check_speed(110);
1147 check_speed(134);
1148 check_speed(150);
1149 check_speed(200);
1150 check_speed(300);
1151 check_speed(600);
1152 check_speed(1200);
1153 check_speed(1800);
1154 check_speed(2400);
1155 check_speed(4800);
1156 check_speed(9600);
1157 check_speed(19200);
1158 check_speed(38400);
1159 /* Non-Posix values follow. They may be unsupported on some systems. */
1160 check_speed(57600);
1161 check_speed(115200);
1162#ifdef B230400
1163 check_speed(230400);
1164#endif
1165#ifdef B460800
1166 check_speed(460800);
1167#endif
1168#ifdef B500000
1169 check_speed(500000);
1170#endif
1171#ifdef B576000
1172 check_speed(576000);
1173#endif
1174#ifdef B921600
1175 check_speed(921600);
1176#endif
1177#ifdef B1000000
1178 check_speed(1000000);
1179#endif
1180#ifdef B1152000
1181 check_speed(1152000);
1182#endif
1183#ifdef B1500000
1184 check_speed(1500000);
1185#endif
1186#ifdef B2000000
1187 check_speed(2000000);
1188#endif
1189#ifdef B2500000
1190 check_speed(2500000);
1191#endif
1192#ifdef B3000000
1193 check_speed(3000000);
1194#endif
1195#ifdef B3500000
1196 check_speed(3500000);
1197#endif
1198#ifdef B4000000
1199 check_speed(4000000);
1200#endif
aliguori6f97dba2008-10-31 18:49:55 +00001201 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001202 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001203
1204 cfsetispeed(&tty, spd);
1205 cfsetospeed(&tty, spd);
1206
1207 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1208 |INLCR|IGNCR|ICRNL|IXON);
1209 tty.c_oflag |= OPOST;
1210 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1211 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1212 switch(data_bits) {
1213 default:
1214 case 8:
1215 tty.c_cflag |= CS8;
1216 break;
1217 case 7:
1218 tty.c_cflag |= CS7;
1219 break;
1220 case 6:
1221 tty.c_cflag |= CS6;
1222 break;
1223 case 5:
1224 tty.c_cflag |= CS5;
1225 break;
1226 }
1227 switch(parity) {
1228 default:
1229 case 'N':
1230 break;
1231 case 'E':
1232 tty.c_cflag |= PARENB;
1233 break;
1234 case 'O':
1235 tty.c_cflag |= PARENB | PARODD;
1236 break;
1237 }
1238 if (stop_bits == 2)
1239 tty.c_cflag |= CSTOPB;
1240
1241 tcsetattr (fd, TCSANOW, &tty);
1242}
1243
1244static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1245{
1246 FDCharDriver *s = chr->opaque;
1247
1248 switch(cmd) {
1249 case CHR_IOCTL_SERIAL_SET_PARAMS:
1250 {
1251 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301252 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1253 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001254 ssp->data_bits, ssp->stop_bits);
1255 }
1256 break;
1257 case CHR_IOCTL_SERIAL_SET_BREAK:
1258 {
1259 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301260 if (enable) {
1261 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1262 }
aliguori6f97dba2008-10-31 18:49:55 +00001263 }
1264 break;
1265 case CHR_IOCTL_SERIAL_GET_TIOCM:
1266 {
1267 int sarg = 0;
1268 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301269 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001270 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001271 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001272 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001273 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001274 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001275 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001276 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001277 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001278 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001279 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001280 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001281 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001282 *targ |= CHR_TIOCM_RTS;
1283 }
1284 break;
1285 case CHR_IOCTL_SERIAL_SET_TIOCM:
1286 {
1287 int sarg = *(int *)arg;
1288 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301289 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001290 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1291 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1292 if (sarg & CHR_TIOCM_CTS)
1293 targ |= TIOCM_CTS;
1294 if (sarg & CHR_TIOCM_CAR)
1295 targ |= TIOCM_CAR;
1296 if (sarg & CHR_TIOCM_DSR)
1297 targ |= TIOCM_DSR;
1298 if (sarg & CHR_TIOCM_RI)
1299 targ |= TIOCM_RI;
1300 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001301 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001302 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001303 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301304 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001305 }
1306 break;
1307 default:
1308 return -ENOTSUP;
1309 }
1310 return 0;
1311}
1312
David Ahern4266a132010-02-10 18:27:17 -07001313static void qemu_chr_close_tty(CharDriverState *chr)
1314{
1315 FDCharDriver *s = chr->opaque;
1316 int fd = -1;
1317
1318 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301319 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001320 }
1321
1322 fd_chr_close(chr);
1323
1324 if (fd >= 0) {
1325 close(fd);
1326 }
1327}
1328
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001329static CharDriverState *qemu_chr_open_tty_fd(int fd)
1330{
1331 CharDriverState *chr;
1332
1333 tty_serial_init(fd, 115200, 'N', 8, 1);
1334 chr = qemu_chr_open_fd(fd, fd);
1335 chr->chr_ioctl = tty_serial_ioctl;
1336 chr->chr_close = qemu_chr_close_tty;
1337 return chr;
1338}
1339
Markus Armbruster1f514702012-02-07 15:09:08 +01001340static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001341{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001342 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001343 int fd;
1344
Markus Armbrusterb181e042012-02-07 15:09:09 +01001345 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001346 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001347 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001348 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001349 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001350}
aliguori6f97dba2008-10-31 18:49:55 +00001351#endif /* __linux__ || __sun__ */
1352
1353#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001354
1355#define HAVE_CHARDEV_PARPORT 1
1356
aliguori6f97dba2008-10-31 18:49:55 +00001357typedef struct {
1358 int fd;
1359 int mode;
1360} ParallelCharDriver;
1361
1362static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1363{
1364 if (s->mode != mode) {
1365 int m = mode;
1366 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1367 return 0;
1368 s->mode = mode;
1369 }
1370 return 1;
1371}
1372
1373static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1374{
1375 ParallelCharDriver *drv = chr->opaque;
1376 int fd = drv->fd;
1377 uint8_t b;
1378
1379 switch(cmd) {
1380 case CHR_IOCTL_PP_READ_DATA:
1381 if (ioctl(fd, PPRDATA, &b) < 0)
1382 return -ENOTSUP;
1383 *(uint8_t *)arg = b;
1384 break;
1385 case CHR_IOCTL_PP_WRITE_DATA:
1386 b = *(uint8_t *)arg;
1387 if (ioctl(fd, PPWDATA, &b) < 0)
1388 return -ENOTSUP;
1389 break;
1390 case CHR_IOCTL_PP_READ_CONTROL:
1391 if (ioctl(fd, PPRCONTROL, &b) < 0)
1392 return -ENOTSUP;
1393 /* Linux gives only the lowest bits, and no way to know data
1394 direction! For better compatibility set the fixed upper
1395 bits. */
1396 *(uint8_t *)arg = b | 0xc0;
1397 break;
1398 case CHR_IOCTL_PP_WRITE_CONTROL:
1399 b = *(uint8_t *)arg;
1400 if (ioctl(fd, PPWCONTROL, &b) < 0)
1401 return -ENOTSUP;
1402 break;
1403 case CHR_IOCTL_PP_READ_STATUS:
1404 if (ioctl(fd, PPRSTATUS, &b) < 0)
1405 return -ENOTSUP;
1406 *(uint8_t *)arg = b;
1407 break;
1408 case CHR_IOCTL_PP_DATA_DIR:
1409 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1410 return -ENOTSUP;
1411 break;
1412 case CHR_IOCTL_PP_EPP_READ_ADDR:
1413 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1414 struct ParallelIOArg *parg = arg;
1415 int n = read(fd, parg->buffer, parg->count);
1416 if (n != parg->count) {
1417 return -EIO;
1418 }
1419 }
1420 break;
1421 case CHR_IOCTL_PP_EPP_READ:
1422 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1423 struct ParallelIOArg *parg = arg;
1424 int n = read(fd, parg->buffer, parg->count);
1425 if (n != parg->count) {
1426 return -EIO;
1427 }
1428 }
1429 break;
1430 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1431 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1432 struct ParallelIOArg *parg = arg;
1433 int n = write(fd, parg->buffer, parg->count);
1434 if (n != parg->count) {
1435 return -EIO;
1436 }
1437 }
1438 break;
1439 case CHR_IOCTL_PP_EPP_WRITE:
1440 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1441 struct ParallelIOArg *parg = arg;
1442 int n = write(fd, parg->buffer, parg->count);
1443 if (n != parg->count) {
1444 return -EIO;
1445 }
1446 }
1447 break;
1448 default:
1449 return -ENOTSUP;
1450 }
1451 return 0;
1452}
1453
1454static void pp_close(CharDriverState *chr)
1455{
1456 ParallelCharDriver *drv = chr->opaque;
1457 int fd = drv->fd;
1458
1459 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1460 ioctl(fd, PPRELEASE);
1461 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001462 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001463 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001464}
1465
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001466static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001467{
1468 CharDriverState *chr;
1469 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001470
1471 if (ioctl(fd, PPCLAIM) < 0) {
1472 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001473 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001474 }
1475
Anthony Liguori7267c092011-08-20 22:09:37 -05001476 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001477 drv->fd = fd;
1478 drv->mode = IEEE1284_MODE_COMPAT;
1479
Anthony Liguori7267c092011-08-20 22:09:37 -05001480 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001481 chr->chr_write = null_chr_write;
1482 chr->chr_ioctl = pp_ioctl;
1483 chr->chr_close = pp_close;
1484 chr->opaque = drv;
1485
Amit Shah127338e2009-11-03 19:59:56 +05301486 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001487
Markus Armbruster1f514702012-02-07 15:09:08 +01001488 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001489}
1490#endif /* __linux__ */
1491
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001492#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001493
1494#define HAVE_CHARDEV_PARPORT 1
1495
blueswir16972f932008-11-22 20:49:12 +00001496static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1497{
Stefan Weile0efb992011-02-23 19:09:16 +01001498 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001499 uint8_t b;
1500
1501 switch(cmd) {
1502 case CHR_IOCTL_PP_READ_DATA:
1503 if (ioctl(fd, PPIGDATA, &b) < 0)
1504 return -ENOTSUP;
1505 *(uint8_t *)arg = b;
1506 break;
1507 case CHR_IOCTL_PP_WRITE_DATA:
1508 b = *(uint8_t *)arg;
1509 if (ioctl(fd, PPISDATA, &b) < 0)
1510 return -ENOTSUP;
1511 break;
1512 case CHR_IOCTL_PP_READ_CONTROL:
1513 if (ioctl(fd, PPIGCTRL, &b) < 0)
1514 return -ENOTSUP;
1515 *(uint8_t *)arg = b;
1516 break;
1517 case CHR_IOCTL_PP_WRITE_CONTROL:
1518 b = *(uint8_t *)arg;
1519 if (ioctl(fd, PPISCTRL, &b) < 0)
1520 return -ENOTSUP;
1521 break;
1522 case CHR_IOCTL_PP_READ_STATUS:
1523 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1524 return -ENOTSUP;
1525 *(uint8_t *)arg = b;
1526 break;
1527 default:
1528 return -ENOTSUP;
1529 }
1530 return 0;
1531}
1532
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001533static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001534{
1535 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001536
Anthony Liguori7267c092011-08-20 22:09:37 -05001537 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001538 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001539 chr->chr_write = null_chr_write;
1540 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001541 return chr;
blueswir16972f932008-11-22 20:49:12 +00001542}
1543#endif
1544
aliguori6f97dba2008-10-31 18:49:55 +00001545#else /* _WIN32 */
1546
1547typedef struct {
1548 int max_size;
1549 HANDLE hcom, hrecv, hsend;
1550 OVERLAPPED orecv, osend;
1551 BOOL fpipe;
1552 DWORD len;
1553} WinCharState;
1554
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001555typedef struct {
1556 HANDLE hStdIn;
1557 HANDLE hInputReadyEvent;
1558 HANDLE hInputDoneEvent;
1559 HANDLE hInputThread;
1560 uint8_t win_stdio_buf;
1561} WinStdioCharState;
1562
aliguori6f97dba2008-10-31 18:49:55 +00001563#define NSENDBUF 2048
1564#define NRECVBUF 2048
1565#define MAXCONNECT 1
1566#define NTIMEOUT 5000
1567
1568static int win_chr_poll(void *opaque);
1569static int win_chr_pipe_poll(void *opaque);
1570
1571static void win_chr_close(CharDriverState *chr)
1572{
1573 WinCharState *s = chr->opaque;
1574
1575 if (s->hsend) {
1576 CloseHandle(s->hsend);
1577 s->hsend = NULL;
1578 }
1579 if (s->hrecv) {
1580 CloseHandle(s->hrecv);
1581 s->hrecv = NULL;
1582 }
1583 if (s->hcom) {
1584 CloseHandle(s->hcom);
1585 s->hcom = NULL;
1586 }
1587 if (s->fpipe)
1588 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1589 else
1590 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301591
Hans de Goedea425d232011-11-19 10:22:43 +01001592 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001593}
1594
1595static int win_chr_init(CharDriverState *chr, const char *filename)
1596{
1597 WinCharState *s = chr->opaque;
1598 COMMCONFIG comcfg;
1599 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1600 COMSTAT comstat;
1601 DWORD size;
1602 DWORD err;
1603
1604 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1605 if (!s->hsend) {
1606 fprintf(stderr, "Failed CreateEvent\n");
1607 goto fail;
1608 }
1609 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1610 if (!s->hrecv) {
1611 fprintf(stderr, "Failed CreateEvent\n");
1612 goto fail;
1613 }
1614
1615 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1616 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1617 if (s->hcom == INVALID_HANDLE_VALUE) {
1618 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1619 s->hcom = NULL;
1620 goto fail;
1621 }
1622
1623 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1624 fprintf(stderr, "Failed SetupComm\n");
1625 goto fail;
1626 }
1627
1628 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1629 size = sizeof(COMMCONFIG);
1630 GetDefaultCommConfig(filename, &comcfg, &size);
1631 comcfg.dcb.DCBlength = sizeof(DCB);
1632 CommConfigDialog(filename, NULL, &comcfg);
1633
1634 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1635 fprintf(stderr, "Failed SetCommState\n");
1636 goto fail;
1637 }
1638
1639 if (!SetCommMask(s->hcom, EV_ERR)) {
1640 fprintf(stderr, "Failed SetCommMask\n");
1641 goto fail;
1642 }
1643
1644 cto.ReadIntervalTimeout = MAXDWORD;
1645 if (!SetCommTimeouts(s->hcom, &cto)) {
1646 fprintf(stderr, "Failed SetCommTimeouts\n");
1647 goto fail;
1648 }
1649
1650 if (!ClearCommError(s->hcom, &err, &comstat)) {
1651 fprintf(stderr, "Failed ClearCommError\n");
1652 goto fail;
1653 }
1654 qemu_add_polling_cb(win_chr_poll, chr);
1655 return 0;
1656
1657 fail:
1658 win_chr_close(chr);
1659 return -1;
1660}
1661
1662static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1663{
1664 WinCharState *s = chr->opaque;
1665 DWORD len, ret, size, err;
1666
1667 len = len1;
1668 ZeroMemory(&s->osend, sizeof(s->osend));
1669 s->osend.hEvent = s->hsend;
1670 while (len > 0) {
1671 if (s->hsend)
1672 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1673 else
1674 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1675 if (!ret) {
1676 err = GetLastError();
1677 if (err == ERROR_IO_PENDING) {
1678 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1679 if (ret) {
1680 buf += size;
1681 len -= size;
1682 } else {
1683 break;
1684 }
1685 } else {
1686 break;
1687 }
1688 } else {
1689 buf += size;
1690 len -= size;
1691 }
1692 }
1693 return len1 - len;
1694}
1695
1696static int win_chr_read_poll(CharDriverState *chr)
1697{
1698 WinCharState *s = chr->opaque;
1699
Anthony Liguori909cda12011-08-15 11:17:31 -05001700 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001701 return s->max_size;
1702}
1703
1704static void win_chr_readfile(CharDriverState *chr)
1705{
1706 WinCharState *s = chr->opaque;
1707 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301708 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001709 DWORD size;
1710
1711 ZeroMemory(&s->orecv, sizeof(s->orecv));
1712 s->orecv.hEvent = s->hrecv;
1713 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1714 if (!ret) {
1715 err = GetLastError();
1716 if (err == ERROR_IO_PENDING) {
1717 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1718 }
1719 }
1720
1721 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001722 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001723 }
1724}
1725
1726static void win_chr_read(CharDriverState *chr)
1727{
1728 WinCharState *s = chr->opaque;
1729
1730 if (s->len > s->max_size)
1731 s->len = s->max_size;
1732 if (s->len == 0)
1733 return;
1734
1735 win_chr_readfile(chr);
1736}
1737
1738static int win_chr_poll(void *opaque)
1739{
1740 CharDriverState *chr = opaque;
1741 WinCharState *s = chr->opaque;
1742 COMSTAT status;
1743 DWORD comerr;
1744
1745 ClearCommError(s->hcom, &comerr, &status);
1746 if (status.cbInQue > 0) {
1747 s->len = status.cbInQue;
1748 win_chr_read_poll(chr);
1749 win_chr_read(chr);
1750 return 1;
1751 }
1752 return 0;
1753}
1754
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001755static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001756{
1757 CharDriverState *chr;
1758 WinCharState *s;
1759
Anthony Liguori7267c092011-08-20 22:09:37 -05001760 chr = g_malloc0(sizeof(CharDriverState));
1761 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001762 chr->opaque = s;
1763 chr->chr_write = win_chr_write;
1764 chr->chr_close = win_chr_close;
1765
1766 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001767 g_free(s);
1768 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001769 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001770 }
Amit Shah127338e2009-11-03 19:59:56 +05301771 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001772 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001773}
1774
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001775static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1776{
1777 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1778}
1779
aliguori6f97dba2008-10-31 18:49:55 +00001780static int win_chr_pipe_poll(void *opaque)
1781{
1782 CharDriverState *chr = opaque;
1783 WinCharState *s = chr->opaque;
1784 DWORD size;
1785
1786 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1787 if (size > 0) {
1788 s->len = size;
1789 win_chr_read_poll(chr);
1790 win_chr_read(chr);
1791 return 1;
1792 }
1793 return 0;
1794}
1795
1796static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1797{
1798 WinCharState *s = chr->opaque;
1799 OVERLAPPED ov;
1800 int ret;
1801 DWORD size;
1802 char openname[256];
1803
1804 s->fpipe = TRUE;
1805
1806 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1807 if (!s->hsend) {
1808 fprintf(stderr, "Failed CreateEvent\n");
1809 goto fail;
1810 }
1811 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1812 if (!s->hrecv) {
1813 fprintf(stderr, "Failed CreateEvent\n");
1814 goto fail;
1815 }
1816
1817 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1818 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1819 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1820 PIPE_WAIT,
1821 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1822 if (s->hcom == INVALID_HANDLE_VALUE) {
1823 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1824 s->hcom = NULL;
1825 goto fail;
1826 }
1827
1828 ZeroMemory(&ov, sizeof(ov));
1829 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1830 ret = ConnectNamedPipe(s->hcom, &ov);
1831 if (ret) {
1832 fprintf(stderr, "Failed ConnectNamedPipe\n");
1833 goto fail;
1834 }
1835
1836 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1837 if (!ret) {
1838 fprintf(stderr, "Failed GetOverlappedResult\n");
1839 if (ov.hEvent) {
1840 CloseHandle(ov.hEvent);
1841 ov.hEvent = NULL;
1842 }
1843 goto fail;
1844 }
1845
1846 if (ov.hEvent) {
1847 CloseHandle(ov.hEvent);
1848 ov.hEvent = NULL;
1849 }
1850 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1851 return 0;
1852
1853 fail:
1854 win_chr_close(chr);
1855 return -1;
1856}
1857
1858
Markus Armbruster1f514702012-02-07 15:09:08 +01001859static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001860{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001861 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001862 CharDriverState *chr;
1863 WinCharState *s;
1864
Anthony Liguori7267c092011-08-20 22:09:37 -05001865 chr = g_malloc0(sizeof(CharDriverState));
1866 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001867 chr->opaque = s;
1868 chr->chr_write = win_chr_write;
1869 chr->chr_close = win_chr_close;
1870
1871 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001872 g_free(s);
1873 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001874 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001875 }
Amit Shah127338e2009-11-03 19:59:56 +05301876 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001877 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001878}
1879
Markus Armbruster1f514702012-02-07 15:09:08 +01001880static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001881{
1882 CharDriverState *chr;
1883 WinCharState *s;
1884
Anthony Liguori7267c092011-08-20 22:09:37 -05001885 chr = g_malloc0(sizeof(CharDriverState));
1886 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001887 s->hcom = fd_out;
1888 chr->opaque = s;
1889 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301890 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001891 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001892}
1893
Markus Armbruster1f514702012-02-07 15:09:08 +01001894static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001895{
Markus Armbruster1f514702012-02-07 15:09:08 +01001896 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001897}
1898
Markus Armbruster1f514702012-02-07 15:09:08 +01001899static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001900{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001901 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001902 HANDLE fd_out;
1903
1904 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1905 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001906 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001907 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001908 }
aliguori6f97dba2008-10-31 18:49:55 +00001909
Markus Armbruster1f514702012-02-07 15:09:08 +01001910 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001911}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001912
1913static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1914{
1915 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1916 DWORD dwSize;
1917 int len1;
1918
1919 len1 = len;
1920
1921 while (len1 > 0) {
1922 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1923 break;
1924 }
1925 buf += dwSize;
1926 len1 -= dwSize;
1927 }
1928
1929 return len - len1;
1930}
1931
1932static void win_stdio_wait_func(void *opaque)
1933{
1934 CharDriverState *chr = opaque;
1935 WinStdioCharState *stdio = chr->opaque;
1936 INPUT_RECORD buf[4];
1937 int ret;
1938 DWORD dwSize;
1939 int i;
1940
1941 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1942 &dwSize);
1943
1944 if (!ret) {
1945 /* Avoid error storm */
1946 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1947 return;
1948 }
1949
1950 for (i = 0; i < dwSize; i++) {
1951 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1952
1953 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1954 int j;
1955 if (kev->uChar.AsciiChar != 0) {
1956 for (j = 0; j < kev->wRepeatCount; j++) {
1957 if (qemu_chr_be_can_write(chr)) {
1958 uint8_t c = kev->uChar.AsciiChar;
1959 qemu_chr_be_write(chr, &c, 1);
1960 }
1961 }
1962 }
1963 }
1964 }
1965}
1966
1967static DWORD WINAPI win_stdio_thread(LPVOID param)
1968{
1969 CharDriverState *chr = param;
1970 WinStdioCharState *stdio = chr->opaque;
1971 int ret;
1972 DWORD dwSize;
1973
1974 while (1) {
1975
1976 /* Wait for one byte */
1977 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1978
1979 /* Exit in case of error, continue if nothing read */
1980 if (!ret) {
1981 break;
1982 }
1983 if (!dwSize) {
1984 continue;
1985 }
1986
1987 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1988 if (stdio->win_stdio_buf == '\r') {
1989 continue;
1990 }
1991
1992 /* Signal the main thread and wait until the byte was eaten */
1993 if (!SetEvent(stdio->hInputReadyEvent)) {
1994 break;
1995 }
1996 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1997 != WAIT_OBJECT_0) {
1998 break;
1999 }
2000 }
2001
2002 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2003 return 0;
2004}
2005
2006static void win_stdio_thread_wait_func(void *opaque)
2007{
2008 CharDriverState *chr = opaque;
2009 WinStdioCharState *stdio = chr->opaque;
2010
2011 if (qemu_chr_be_can_write(chr)) {
2012 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2013 }
2014
2015 SetEvent(stdio->hInputDoneEvent);
2016}
2017
2018static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2019{
2020 WinStdioCharState *stdio = chr->opaque;
2021 DWORD dwMode = 0;
2022
2023 GetConsoleMode(stdio->hStdIn, &dwMode);
2024
2025 if (echo) {
2026 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2027 } else {
2028 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2029 }
2030}
2031
2032static void win_stdio_close(CharDriverState *chr)
2033{
2034 WinStdioCharState *stdio = chr->opaque;
2035
2036 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2037 CloseHandle(stdio->hInputReadyEvent);
2038 }
2039 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2040 CloseHandle(stdio->hInputDoneEvent);
2041 }
2042 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2043 TerminateThread(stdio->hInputThread, 0);
2044 }
2045
2046 g_free(chr->opaque);
2047 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002048}
2049
Markus Armbruster1f514702012-02-07 15:09:08 +01002050static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002051{
2052 CharDriverState *chr;
2053 WinStdioCharState *stdio;
2054 DWORD dwMode;
2055 int is_console = 0;
2056
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002057 chr = g_malloc0(sizeof(CharDriverState));
2058 stdio = g_malloc0(sizeof(WinStdioCharState));
2059
2060 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2061 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2062 fprintf(stderr, "cannot open stdio: invalid handle\n");
2063 exit(1);
2064 }
2065
2066 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2067
2068 chr->opaque = stdio;
2069 chr->chr_write = win_stdio_write;
2070 chr->chr_close = win_stdio_close;
2071
Anthony Liguoried7a1542013-03-05 23:21:17 +05302072 if (is_console) {
2073 if (qemu_add_wait_object(stdio->hStdIn,
2074 win_stdio_wait_func, chr)) {
2075 fprintf(stderr, "qemu_add_wait_object: failed\n");
2076 }
2077 } else {
2078 DWORD dwId;
2079
2080 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2081 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2082 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2083 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002084
Anthony Liguoried7a1542013-03-05 23:21:17 +05302085 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2086 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2087 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2088 fprintf(stderr, "cannot create stdio thread or event\n");
2089 exit(1);
2090 }
2091 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2092 win_stdio_thread_wait_func, chr)) {
2093 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002094 }
2095 }
2096
2097 dwMode |= ENABLE_LINE_INPUT;
2098
Anthony Liguoried7a1542013-03-05 23:21:17 +05302099 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002100 /* set the terminal in raw mode */
2101 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2102 dwMode |= ENABLE_PROCESSED_INPUT;
2103 }
2104
2105 SetConsoleMode(stdio->hStdIn, dwMode);
2106
2107 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2108 qemu_chr_fe_set_echo(chr, false);
2109
Markus Armbruster1f514702012-02-07 15:09:08 +01002110 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002111}
aliguori6f97dba2008-10-31 18:49:55 +00002112#endif /* !_WIN32 */
2113
2114/***********************************************************/
2115/* UDP Net console */
2116
2117typedef struct {
2118 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05302119 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002120 int bufcnt;
2121 int bufptr;
2122 int max_size;
2123} NetCharDriver;
2124
2125static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2126{
2127 NetCharDriver *s = chr->opaque;
2128
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002129 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00002130}
2131
2132static int udp_chr_read_poll(void *opaque)
2133{
2134 CharDriverState *chr = opaque;
2135 NetCharDriver *s = chr->opaque;
2136
Anthony Liguori909cda12011-08-15 11:17:31 -05002137 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002138
2139 /* If there were any stray characters in the queue process them
2140 * first
2141 */
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 return s->max_size;
2148}
2149
2150static void udp_chr_read(void *opaque)
2151{
2152 CharDriverState *chr = opaque;
2153 NetCharDriver *s = chr->opaque;
2154
2155 if (s->max_size == 0)
2156 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00002157 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00002158 s->bufptr = s->bufcnt;
2159 if (s->bufcnt <= 0)
2160 return;
2161
2162 s->bufptr = 0;
2163 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002164 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002165 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002166 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002167 }
2168}
2169
2170static void udp_chr_update_read_handler(CharDriverState *chr)
2171{
2172 NetCharDriver *s = chr->opaque;
2173
2174 if (s->fd >= 0) {
2175 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2176 udp_chr_read, NULL, chr);
2177 }
2178}
2179
aliguori819f56b2009-03-28 17:58:14 +00002180static void udp_chr_close(CharDriverState *chr)
2181{
2182 NetCharDriver *s = chr->opaque;
2183 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002184 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00002185 closesocket(s->fd);
2186 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002187 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002188 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002189}
2190
Markus Armbruster1f514702012-02-07 15:09:08 +01002191static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002192{
2193 CharDriverState *chr = NULL;
2194 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002195 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002196 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002197
Anthony Liguori7267c092011-08-20 22:09:37 -05002198 chr = g_malloc0(sizeof(CharDriverState));
2199 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002200
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002201 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002202 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002203 goto return_err;
2204 }
2205
2206 s->fd = fd;
2207 s->bufcnt = 0;
2208 s->bufptr = 0;
2209 chr->opaque = s;
2210 chr->chr_write = udp_chr_write;
2211 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002212 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002213 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002214
2215return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002216 if (local_err) {
2217 qerror_report_err(local_err);
2218 error_free(local_err);
2219 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002220 g_free(chr);
2221 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002222 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002223 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002224 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002225 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002226}
2227
2228/***********************************************************/
2229/* TCP Net console */
2230
2231typedef struct {
2232 int fd, listen_fd;
2233 int connected;
2234 int max_size;
2235 int do_telnetopt;
2236 int do_nodelay;
2237 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002238 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002239} TCPCharDriver;
2240
2241static void tcp_chr_accept(void *opaque);
2242
2243static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2244{
2245 TCPCharDriver *s = chr->opaque;
2246 if (s->connected) {
2247 return send_all(s->fd, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002248 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002249 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002250 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002251 }
2252}
2253
2254static int tcp_chr_read_poll(void *opaque)
2255{
2256 CharDriverState *chr = opaque;
2257 TCPCharDriver *s = chr->opaque;
2258 if (!s->connected)
2259 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002260 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002261 return s->max_size;
2262}
2263
2264#define IAC 255
2265#define IAC_BREAK 243
2266static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2267 TCPCharDriver *s,
2268 uint8_t *buf, int *size)
2269{
2270 /* Handle any telnet client's basic IAC options to satisfy char by
2271 * char mode with no echo. All IAC options will be removed from
2272 * the buf and the do_telnetopt variable will be used to track the
2273 * state of the width of the IAC information.
2274 *
2275 * IAC commands come in sets of 3 bytes with the exception of the
2276 * "IAC BREAK" command and the double IAC.
2277 */
2278
2279 int i;
2280 int j = 0;
2281
2282 for (i = 0; i < *size; i++) {
2283 if (s->do_telnetopt > 1) {
2284 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2285 /* Double IAC means send an IAC */
2286 if (j != i)
2287 buf[j] = buf[i];
2288 j++;
2289 s->do_telnetopt = 1;
2290 } else {
2291 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2292 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002293 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002294 s->do_telnetopt++;
2295 }
2296 s->do_telnetopt++;
2297 }
2298 if (s->do_telnetopt >= 4) {
2299 s->do_telnetopt = 1;
2300 }
2301 } else {
2302 if ((unsigned char)buf[i] == IAC) {
2303 s->do_telnetopt = 2;
2304 } else {
2305 if (j != i)
2306 buf[j] = buf[i];
2307 j++;
2308 }
2309 }
2310 }
2311 *size = j;
2312}
2313
Mark McLoughlin7d174052009-07-22 09:11:39 +01002314static int tcp_get_msgfd(CharDriverState *chr)
2315{
2316 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002317 int fd = s->msgfd;
2318 s->msgfd = -1;
2319 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002320}
2321
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002322#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002323static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2324{
2325 TCPCharDriver *s = chr->opaque;
2326 struct cmsghdr *cmsg;
2327
2328 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2329 int fd;
2330
2331 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2332 cmsg->cmsg_level != SOL_SOCKET ||
2333 cmsg->cmsg_type != SCM_RIGHTS)
2334 continue;
2335
2336 fd = *((int *)CMSG_DATA(cmsg));
2337 if (fd < 0)
2338 continue;
2339
Corey Bryant06138652012-08-14 16:43:42 -04002340#ifndef MSG_CMSG_CLOEXEC
2341 qemu_set_cloexec(fd);
2342#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002343 if (s->msgfd != -1)
2344 close(s->msgfd);
2345 s->msgfd = fd;
2346 }
2347}
2348
Mark McLoughlin9977c892009-07-22 09:11:38 +01002349static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2350{
2351 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002352 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002353 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002354 union {
2355 struct cmsghdr cmsg;
2356 char control[CMSG_SPACE(sizeof(int))];
2357 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002358 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002359 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002360
2361 iov[0].iov_base = buf;
2362 iov[0].iov_len = len;
2363
2364 msg.msg_iov = iov;
2365 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002366 msg.msg_control = &msg_control;
2367 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002368
Corey Bryant06138652012-08-14 16:43:42 -04002369#ifdef MSG_CMSG_CLOEXEC
2370 flags |= MSG_CMSG_CLOEXEC;
2371#endif
2372 ret = recvmsg(s->fd, &msg, flags);
2373 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002374 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002375 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002376
2377 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002378}
2379#else
2380static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2381{
2382 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002383 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002384}
2385#endif
2386
aliguori6f97dba2008-10-31 18:49:55 +00002387static void tcp_chr_read(void *opaque)
2388{
2389 CharDriverState *chr = opaque;
2390 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302391 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002392 int len, size;
2393
2394 if (!s->connected || s->max_size <= 0)
2395 return;
2396 len = sizeof(buf);
2397 if (len > s->max_size)
2398 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002399 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002400 if (size == 0) {
2401 /* connection closed */
2402 s->connected = 0;
2403 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002404 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002405 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002406 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002407 closesocket(s->fd);
2408 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002409 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002410 } else if (size > 0) {
2411 if (s->do_telnetopt)
2412 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2413 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002414 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002415 }
2416}
2417
Blue Swirl68c18d12010-08-15 09:46:24 +00002418#ifndef _WIN32
2419CharDriverState *qemu_chr_open_eventfd(int eventfd)
2420{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002421 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002422}
Blue Swirl68c18d12010-08-15 09:46:24 +00002423#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002424
aliguori6f97dba2008-10-31 18:49:55 +00002425static void tcp_chr_connect(void *opaque)
2426{
2427 CharDriverState *chr = opaque;
2428 TCPCharDriver *s = chr->opaque;
2429
2430 s->connected = 1;
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002431 if (s->fd >= 0) {
2432 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2433 tcp_chr_read, NULL, chr);
2434 }
Amit Shah127338e2009-11-03 19:59:56 +05302435 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002436}
2437
2438#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2439static void tcp_chr_telnet_init(int fd)
2440{
2441 char buf[3];
2442 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2443 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2444 send(fd, (char *)buf, 3, 0);
2445 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2446 send(fd, (char *)buf, 3, 0);
2447 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2448 send(fd, (char *)buf, 3, 0);
2449 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2450 send(fd, (char *)buf, 3, 0);
2451}
2452
Daniel P. Berrange13661082011-06-23 13:31:42 +01002453static int tcp_chr_add_client(CharDriverState *chr, int fd)
2454{
2455 TCPCharDriver *s = chr->opaque;
2456 if (s->fd != -1)
2457 return -1;
2458
2459 socket_set_nonblock(fd);
2460 if (s->do_nodelay)
2461 socket_set_nodelay(fd);
2462 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002463 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002464 tcp_chr_connect(chr);
2465
2466 return 0;
2467}
2468
aliguori6f97dba2008-10-31 18:49:55 +00002469static void tcp_chr_accept(void *opaque)
2470{
2471 CharDriverState *chr = opaque;
2472 TCPCharDriver *s = chr->opaque;
2473 struct sockaddr_in saddr;
2474#ifndef _WIN32
2475 struct sockaddr_un uaddr;
2476#endif
2477 struct sockaddr *addr;
2478 socklen_t len;
2479 int fd;
2480
2481 for(;;) {
2482#ifndef _WIN32
2483 if (s->is_unix) {
2484 len = sizeof(uaddr);
2485 addr = (struct sockaddr *)&uaddr;
2486 } else
2487#endif
2488 {
2489 len = sizeof(saddr);
2490 addr = (struct sockaddr *)&saddr;
2491 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002492 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002493 if (fd < 0 && errno != EINTR) {
2494 return;
2495 } else if (fd >= 0) {
2496 if (s->do_telnetopt)
2497 tcp_chr_telnet_init(fd);
2498 break;
2499 }
2500 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002501 if (tcp_chr_add_client(chr, fd) < 0)
2502 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002503}
2504
2505static void tcp_chr_close(CharDriverState *chr)
2506{
2507 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002508 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002509 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002510 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002511 }
2512 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002513 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002514 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002515 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002516 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002517 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002518}
2519
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002520static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2521 bool is_listen, bool is_telnet,
2522 bool is_waitconnect,
2523 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002524{
2525 CharDriverState *chr = NULL;
2526 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002527 char host[NI_MAXHOST], serv[NI_MAXSERV];
2528 const char *left = "", *right = "";
2529 struct sockaddr_storage ss;
2530 socklen_t ss_len = sizeof(ss);
2531
2532 memset(&ss, 0, ss_len);
2533 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2534 error_setg(errp, "getsockname: %s", strerror(errno));
2535 return NULL;
2536 }
2537
2538 chr = g_malloc0(sizeof(CharDriverState));
2539 s = g_malloc0(sizeof(TCPCharDriver));
2540
2541 s->connected = 0;
2542 s->fd = -1;
2543 s->listen_fd = -1;
2544 s->msgfd = -1;
2545
2546 chr->filename = g_malloc(256);
2547 switch (ss.ss_family) {
2548#ifndef _WIN32
2549 case AF_UNIX:
2550 s->is_unix = 1;
2551 snprintf(chr->filename, 256, "unix:%s%s",
2552 ((struct sockaddr_un *)(&ss))->sun_path,
2553 is_listen ? ",server" : "");
2554 break;
2555#endif
2556 case AF_INET6:
2557 left = "[";
2558 right = "]";
2559 /* fall through */
2560 case AF_INET:
2561 s->do_nodelay = do_nodelay;
2562 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2563 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2564 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2565 is_telnet ? "telnet" : "tcp",
2566 left, host, right, serv,
2567 is_listen ? ",server" : "");
2568 break;
2569 }
2570
2571 chr->opaque = s;
2572 chr->chr_write = tcp_chr_write;
2573 chr->chr_close = tcp_chr_close;
2574 chr->get_msgfd = tcp_get_msgfd;
2575 chr->chr_add_client = tcp_chr_add_client;
2576
2577 if (is_listen) {
2578 s->listen_fd = fd;
2579 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
2580 if (is_telnet) {
2581 s->do_telnetopt = 1;
2582 }
2583 } else {
2584 s->connected = 1;
2585 s->fd = fd;
2586 socket_set_nodelay(fd);
2587 tcp_chr_connect(chr);
2588 }
2589
2590 if (is_listen && is_waitconnect) {
2591 printf("QEMU waiting for connection on: %s\n",
2592 chr->filename);
2593 tcp_chr_accept(chr);
2594 socket_set_nonblock(s->listen_fd);
2595 }
2596 return chr;
2597}
2598
2599static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2600{
2601 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002602 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002603 int fd = -1;
2604 int is_listen;
2605 int is_waitconnect;
2606 int do_nodelay;
2607 int is_unix;
2608 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002609
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002610 is_listen = qemu_opt_get_bool(opts, "server", 0);
2611 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2612 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2613 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2614 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002615 if (!is_listen)
2616 is_waitconnect = 0;
2617
aliguorif07b6002008-11-11 20:54:09 +00002618 if (is_unix) {
2619 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002620 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002621 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002622 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002623 }
2624 } else {
2625 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002626 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002627 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002628 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002629 }
2630 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002631 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002632 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002633 }
aliguori6f97dba2008-10-31 18:49:55 +00002634
2635 if (!is_waitconnect)
2636 socket_set_nonblock(fd);
2637
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002638 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2639 is_waitconnect, &local_err);
2640 if (error_is_set(&local_err)) {
2641 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002642 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002643 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002644
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002645
aliguori6f97dba2008-10-31 18:49:55 +00002646 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002647 if (local_err) {
2648 qerror_report_err(local_err);
2649 error_free(local_err);
2650 }
2651 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002652 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002653 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002654 if (chr) {
2655 g_free(chr->opaque);
2656 g_free(chr);
2657 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002658 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002659}
2660
Luiz Capitulino999bd672010-10-22 16:09:05 -02002661/***********************************************************/
2662/* Memory chardev */
2663typedef struct {
2664 size_t outbuf_size;
2665 size_t outbuf_capacity;
2666 uint8_t *outbuf;
2667} MemoryDriver;
2668
2669static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2670{
2671 MemoryDriver *d = chr->opaque;
2672
2673 /* TODO: the QString implementation has the same code, we should
2674 * introduce a generic way to do this in cutils.c */
2675 if (d->outbuf_capacity < d->outbuf_size + len) {
2676 /* grow outbuf */
2677 d->outbuf_capacity += len;
2678 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002679 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002680 }
2681
2682 memcpy(d->outbuf + d->outbuf_size, buf, len);
2683 d->outbuf_size += len;
2684
2685 return len;
2686}
2687
2688void qemu_chr_init_mem(CharDriverState *chr)
2689{
2690 MemoryDriver *d;
2691
Anthony Liguori7267c092011-08-20 22:09:37 -05002692 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002693 d->outbuf_size = 0;
2694 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002695 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002696
2697 memset(chr, 0, sizeof(*chr));
2698 chr->opaque = d;
2699 chr->chr_write = mem_chr_write;
2700}
2701
2702QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2703{
2704 MemoryDriver *d = chr->opaque;
2705 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2706}
2707
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002708/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002709void qemu_chr_close_mem(CharDriverState *chr)
2710{
2711 MemoryDriver *d = chr->opaque;
2712
Anthony Liguori7267c092011-08-20 22:09:37 -05002713 g_free(d->outbuf);
2714 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002715 chr->opaque = NULL;
2716 chr->chr_write = NULL;
2717}
2718
2719size_t qemu_chr_mem_osize(const CharDriverState *chr)
2720{
2721 const MemoryDriver *d = chr->opaque;
2722 return d->outbuf_size;
2723}
2724
Lei Li51767e72013-01-25 00:03:19 +08002725/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002726/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002727
2728typedef struct {
2729 size_t size;
2730 size_t prod;
2731 size_t cons;
2732 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002733} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002734
Markus Armbruster3949e592013-02-06 21:27:24 +01002735static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002736{
Markus Armbruster3949e592013-02-06 21:27:24 +01002737 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002738
Markus Armbruster5c230102013-02-06 21:27:23 +01002739 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002740}
2741
Markus Armbruster3949e592013-02-06 21:27:24 +01002742static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002743{
Markus Armbruster3949e592013-02-06 21:27:24 +01002744 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002745 int i;
2746
2747 if (!buf || (len < 0)) {
2748 return -1;
2749 }
2750
2751 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002752 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2753 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002754 d->cons = d->prod - d->size;
2755 }
2756 }
2757
2758 return 0;
2759}
2760
Markus Armbruster3949e592013-02-06 21:27:24 +01002761static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002762{
Markus Armbruster3949e592013-02-06 21:27:24 +01002763 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002764 int i;
2765
Markus Armbruster5c230102013-02-06 21:27:23 +01002766 for (i = 0; i < len && d->cons != d->prod; i++) {
2767 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002768 }
2769
2770 return i;
2771}
2772
Markus Armbruster3949e592013-02-06 21:27:24 +01002773static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002774{
Markus Armbruster3949e592013-02-06 21:27:24 +01002775 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002776
2777 g_free(d->cbuf);
2778 g_free(d);
2779 chr->opaque = NULL;
2780}
2781
Markus Armbruster3949e592013-02-06 21:27:24 +01002782static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002783{
2784 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002785 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002786
2787 chr = g_malloc0(sizeof(CharDriverState));
2788 d = g_malloc(sizeof(*d));
2789
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002790 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002791 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002792 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002793 }
2794
2795 /* The size must be power of 2 */
2796 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002797 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002798 goto fail;
2799 }
2800
2801 d->prod = 0;
2802 d->cons = 0;
2803 d->cbuf = g_malloc0(d->size);
2804
2805 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002806 chr->chr_write = ringbuf_chr_write;
2807 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002808
2809 return chr;
2810
2811fail:
2812 g_free(d);
2813 g_free(chr);
2814 return NULL;
2815}
2816
Markus Armbruster3949e592013-02-06 21:27:24 +01002817static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002818{
Markus Armbruster3949e592013-02-06 21:27:24 +01002819 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002820}
2821
Markus Armbruster3949e592013-02-06 21:27:24 +01002822void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002823 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002824 Error **errp)
2825{
2826 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002827 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002828 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002829 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002830
2831 chr = qemu_chr_find(device);
2832 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002833 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002834 return;
2835 }
2836
Markus Armbruster3949e592013-02-06 21:27:24 +01002837 if (!chr_is_ringbuf(chr)) {
2838 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002839 return;
2840 }
2841
Lei Li1f590cf2013-01-25 00:03:20 +08002842 if (has_format && (format == DATA_FORMAT_BASE64)) {
2843 write_data = g_base64_decode(data, &write_count);
2844 } else {
2845 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002846 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002847 }
2848
Markus Armbruster3949e592013-02-06 21:27:24 +01002849 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002850
Markus Armbruster13289fb2013-02-06 21:27:18 +01002851 if (write_data != (uint8_t *)data) {
2852 g_free((void *)write_data);
2853 }
2854
Lei Li1f590cf2013-01-25 00:03:20 +08002855 if (ret < 0) {
2856 error_setg(errp, "Failed to write to device %s", device);
2857 return;
2858 }
2859}
2860
Markus Armbruster3949e592013-02-06 21:27:24 +01002861char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002862 bool has_format, enum DataFormat format,
2863 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002864{
2865 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002866 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002867 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002868 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002869
2870 chr = qemu_chr_find(device);
2871 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002872 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002873 return NULL;
2874 }
2875
Markus Armbruster3949e592013-02-06 21:27:24 +01002876 if (!chr_is_ringbuf(chr)) {
2877 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002878 return NULL;
2879 }
2880
2881 if (size <= 0) {
2882 error_setg(errp, "size must be greater than zero");
2883 return NULL;
2884 }
2885
Markus Armbruster3949e592013-02-06 21:27:24 +01002886 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002887 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002888 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002889
Markus Armbruster3949e592013-02-06 21:27:24 +01002890 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002891
2892 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002893 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002894 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002895 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002896 /*
2897 * FIXME should read only complete, valid UTF-8 characters up
2898 * to @size bytes. Invalid sequences should be replaced by a
2899 * suitable replacement character. Except when (and only
2900 * when) ring buffer lost characters since last read, initial
2901 * continuation characters should be dropped.
2902 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002903 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002904 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002905 }
2906
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002907 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002908}
2909
Gerd Hoffmann33521632009-12-08 13:11:49 +01002910QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002911{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002912 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002913 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002914 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002915 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002916 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002917
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002918 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2919 if (error_is_set(&local_err)) {
2920 qerror_report_err(local_err);
2921 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002922 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002923 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002924
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002925 if (strstart(filename, "mon:", &p)) {
2926 filename = p;
2927 qemu_opt_set(opts, "mux", "on");
2928 }
2929
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002930 if (strcmp(filename, "null") == 0 ||
2931 strcmp(filename, "pty") == 0 ||
2932 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002933 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002934 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002935 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002936 return opts;
2937 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002938 if (strstart(filename, "vc", &p)) {
2939 qemu_opt_set(opts, "backend", "vc");
2940 if (*p == ':') {
2941 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2942 /* pixels */
2943 qemu_opt_set(opts, "width", width);
2944 qemu_opt_set(opts, "height", height);
2945 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2946 /* chars */
2947 qemu_opt_set(opts, "cols", width);
2948 qemu_opt_set(opts, "rows", height);
2949 } else {
2950 goto fail;
2951 }
2952 }
2953 return opts;
2954 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002955 if (strcmp(filename, "con:") == 0) {
2956 qemu_opt_set(opts, "backend", "console");
2957 return opts;
2958 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002959 if (strstart(filename, "COM", NULL)) {
2960 qemu_opt_set(opts, "backend", "serial");
2961 qemu_opt_set(opts, "path", filename);
2962 return opts;
2963 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002964 if (strstart(filename, "file:", &p)) {
2965 qemu_opt_set(opts, "backend", "file");
2966 qemu_opt_set(opts, "path", p);
2967 return opts;
2968 }
2969 if (strstart(filename, "pipe:", &p)) {
2970 qemu_opt_set(opts, "backend", "pipe");
2971 qemu_opt_set(opts, "path", p);
2972 return opts;
2973 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002974 if (strstart(filename, "tcp:", &p) ||
2975 strstart(filename, "telnet:", &p)) {
2976 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2977 host[0] = 0;
2978 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2979 goto fail;
2980 }
2981 qemu_opt_set(opts, "backend", "socket");
2982 qemu_opt_set(opts, "host", host);
2983 qemu_opt_set(opts, "port", port);
2984 if (p[pos] == ',') {
2985 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2986 goto fail;
2987 }
2988 if (strstart(filename, "telnet:", &p))
2989 qemu_opt_set(opts, "telnet", "on");
2990 return opts;
2991 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002992 if (strstart(filename, "udp:", &p)) {
2993 qemu_opt_set(opts, "backend", "udp");
2994 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2995 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002996 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002997 goto fail;
2998 }
2999 }
3000 qemu_opt_set(opts, "host", host);
3001 qemu_opt_set(opts, "port", port);
3002 if (p[pos] == '@') {
3003 p += pos + 1;
3004 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3005 host[0] = 0;
3006 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003007 goto fail;
3008 }
3009 }
3010 qemu_opt_set(opts, "localaddr", host);
3011 qemu_opt_set(opts, "localport", port);
3012 }
3013 return opts;
3014 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003015 if (strstart(filename, "unix:", &p)) {
3016 qemu_opt_set(opts, "backend", "socket");
3017 if (qemu_opts_do_parse(opts, p, "path") != 0)
3018 goto fail;
3019 return opts;
3020 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003021 if (strstart(filename, "/dev/parport", NULL) ||
3022 strstart(filename, "/dev/ppi", NULL)) {
3023 qemu_opt_set(opts, "backend", "parport");
3024 qemu_opt_set(opts, "path", filename);
3025 return opts;
3026 }
3027 if (strstart(filename, "/dev/", NULL)) {
3028 qemu_opt_set(opts, "backend", "tty");
3029 qemu_opt_set(opts, "path", filename);
3030 return opts;
3031 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003032
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003033fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003034 qemu_opts_del(opts);
3035 return NULL;
3036}
3037
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003038#ifdef HAVE_CHARDEV_PARPORT
3039
3040static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3041{
3042 const char *filename = qemu_opt_get(opts, "path");
3043 int fd;
3044
3045 fd = qemu_open(filename, O_RDWR);
3046 if (fd < 0) {
3047 return NULL;
3048 }
3049 return qemu_chr_open_pp_fd(fd);
3050}
3051
3052#endif
3053
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003054static const struct {
3055 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01003056 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003057} backend_table[] = {
3058 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003059 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003060 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003061 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Anthony Liguorid82831d2013-02-20 07:43:19 -06003062 { .name = "vc", .open = vc_init },
Markus Armbruster3949e592013-02-06 21:27:24 +01003063 { .name = "memory", .open = qemu_chr_open_ringbuf },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003064#ifdef _WIN32
3065 { .name = "file", .open = qemu_chr_open_win_file_out },
3066 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003067 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003068 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02003069 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003070#else
3071 { .name = "file", .open = qemu_chr_open_file_out },
3072 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02003073 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003074#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003075#ifdef CONFIG_BRLAPI
3076 { .name = "braille", .open = chr_baum_init },
3077#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003078#ifdef HAVE_CHARDEV_TTY
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003079 { .name = "tty", .open = qemu_chr_open_tty },
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003080 { .name = "serial", .open = qemu_chr_open_tty },
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003081 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003082#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003083#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003084 { .name = "parallel", .open = qemu_chr_open_pp },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003085 { .name = "parport", .open = qemu_chr_open_pp },
3086#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003087#ifdef CONFIG_SPICE
3088 { .name = "spicevmc", .open = qemu_chr_open_spice },
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01003089#if SPICE_SERVER_VERSION >= 0x000c02
3090 { .name = "spiceport", .open = qemu_chr_open_spice_port },
3091#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003092#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003093};
3094
Anthony Liguorif69554b2011-08-15 11:17:37 -05003095CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003096 void (*init)(struct CharDriverState *s),
3097 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003098{
3099 CharDriverState *chr;
3100 int i;
3101
3102 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003103 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003104 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003105 }
3106
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003107 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003108 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003109 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003110 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003111 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003112 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
3113 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
3114 break;
3115 }
3116 if (i == ARRAY_SIZE(backend_table)) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003117 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003118 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003119 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003120 }
3121
Markus Armbruster1f514702012-02-07 15:09:08 +01003122 chr = backend_table[i].open(opts);
3123 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003124 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003125 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003126 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003127 }
3128
3129 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003130 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003131 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003132 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003133
3134 if (qemu_opt_get_bool(opts, "mux", 0)) {
3135 CharDriverState *base = chr;
3136 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003137 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003138 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3139 chr = qemu_chr_open_mux(base);
3140 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003141 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003142 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003143 } else {
3144 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003145 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003146 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003147 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003148 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003149
3150err:
3151 qemu_opts_del(opts);
3152 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003153}
3154
Anthony Liguori27143a42011-08-15 11:17:36 -05003155CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003156{
3157 const char *p;
3158 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003159 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003160 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003161
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003162 if (strstart(filename, "chardev:", &p)) {
3163 return qemu_chr_find(p);
3164 }
3165
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003166 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003167 if (!opts)
3168 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003169
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003170 chr = qemu_chr_new_from_opts(opts, init, &err);
3171 if (error_is_set(&err)) {
3172 fprintf(stderr, "%s\n", error_get_pretty(err));
3173 error_free(err);
3174 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003175 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3176 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003177 }
3178 return chr;
3179}
3180
Anthony Liguori15f31512011-08-15 11:17:35 -05003181void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003182{
3183 if (chr->chr_set_echo) {
3184 chr->chr_set_echo(chr, echo);
3185 }
3186}
3187
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003188void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003189{
3190 if (chr->chr_guest_open) {
3191 chr->chr_guest_open(chr);
3192 }
3193}
3194
Anthony Liguori28178222011-08-15 11:17:33 -05003195void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003196{
3197 if (chr->chr_guest_close) {
3198 chr->chr_guest_close(chr);
3199 }
3200}
3201
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003202void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003203{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003204 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003205 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003206 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003207 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003208 g_free(chr->filename);
3209 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003210 if (chr->opts) {
3211 qemu_opts_del(chr->opts);
3212 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003213 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003214}
3215
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003216ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003217{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003218 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003219 CharDriverState *chr;
3220
Blue Swirl72cf2d42009-09-12 07:36:22 +00003221 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003222 ChardevInfoList *info = g_malloc0(sizeof(*info));
3223 info->value = g_malloc0(sizeof(*info->value));
3224 info->value->label = g_strdup(chr->label);
3225 info->value->filename = g_strdup(chr->filename);
3226
3227 info->next = chr_list;
3228 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003229 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003230
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003231 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003232}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003233
3234CharDriverState *qemu_chr_find(const char *name)
3235{
3236 CharDriverState *chr;
3237
Blue Swirl72cf2d42009-09-12 07:36:22 +00003238 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003239 if (strcmp(chr->label, name) != 0)
3240 continue;
3241 return chr;
3242 }
3243 return NULL;
3244}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003245
3246/* Get a character (serial) device interface. */
3247CharDriverState *qemu_char_get_next_serial(void)
3248{
3249 static int next_serial;
3250
3251 /* FIXME: This function needs to go away: use chardev properties! */
3252 return serial_hds[next_serial++];
3253}
3254
Paolo Bonzini4d454572012-11-26 16:03:42 +01003255QemuOptsList qemu_chardev_opts = {
3256 .name = "chardev",
3257 .implied_opt_name = "backend",
3258 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3259 .desc = {
3260 {
3261 .name = "backend",
3262 .type = QEMU_OPT_STRING,
3263 },{
3264 .name = "path",
3265 .type = QEMU_OPT_STRING,
3266 },{
3267 .name = "host",
3268 .type = QEMU_OPT_STRING,
3269 },{
3270 .name = "port",
3271 .type = QEMU_OPT_STRING,
3272 },{
3273 .name = "localaddr",
3274 .type = QEMU_OPT_STRING,
3275 },{
3276 .name = "localport",
3277 .type = QEMU_OPT_STRING,
3278 },{
3279 .name = "to",
3280 .type = QEMU_OPT_NUMBER,
3281 },{
3282 .name = "ipv4",
3283 .type = QEMU_OPT_BOOL,
3284 },{
3285 .name = "ipv6",
3286 .type = QEMU_OPT_BOOL,
3287 },{
3288 .name = "wait",
3289 .type = QEMU_OPT_BOOL,
3290 },{
3291 .name = "server",
3292 .type = QEMU_OPT_BOOL,
3293 },{
3294 .name = "delay",
3295 .type = QEMU_OPT_BOOL,
3296 },{
3297 .name = "telnet",
3298 .type = QEMU_OPT_BOOL,
3299 },{
3300 .name = "width",
3301 .type = QEMU_OPT_NUMBER,
3302 },{
3303 .name = "height",
3304 .type = QEMU_OPT_NUMBER,
3305 },{
3306 .name = "cols",
3307 .type = QEMU_OPT_NUMBER,
3308 },{
3309 .name = "rows",
3310 .type = QEMU_OPT_NUMBER,
3311 },{
3312 .name = "mux",
3313 .type = QEMU_OPT_BOOL,
3314 },{
3315 .name = "signal",
3316 .type = QEMU_OPT_BOOL,
3317 },{
3318 .name = "name",
3319 .type = QEMU_OPT_STRING,
3320 },{
3321 .name = "debug",
3322 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003323 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003324 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003325 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003326 },
3327 { /* end of list */ }
3328 },
3329};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003330
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003331#ifdef _WIN32
3332
3333static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3334{
3335 HANDLE out;
3336
3337 if (file->in) {
3338 error_setg(errp, "input file not supported");
3339 return NULL;
3340 }
3341
3342 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3343 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3344 if (out == INVALID_HANDLE_VALUE) {
3345 error_setg(errp, "open %s failed", file->out);
3346 return NULL;
3347 }
3348 return qemu_chr_open_win_file(out);
3349}
3350
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003351static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3352 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003353{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003354 return qemu_chr_open_win_path(serial->device);
3355}
3356
3357static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3358 Error **errp)
3359{
3360 error_setg(errp, "character device backend type 'parallel' not supported");
3361 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003362}
3363
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003364#else /* WIN32 */
3365
3366static int qmp_chardev_open_file_source(char *src, int flags,
3367 Error **errp)
3368{
3369 int fd = -1;
3370
3371 TFR(fd = qemu_open(src, flags, 0666));
3372 if (fd == -1) {
3373 error_setg(errp, "open %s: %s", src, strerror(errno));
3374 }
3375 return fd;
3376}
3377
3378static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3379{
3380 int flags, in = -1, out = -1;
3381
3382 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3383 out = qmp_chardev_open_file_source(file->out, flags, errp);
3384 if (error_is_set(errp)) {
3385 return NULL;
3386 }
3387
3388 if (file->in) {
3389 flags = O_RDONLY;
3390 in = qmp_chardev_open_file_source(file->in, flags, errp);
3391 if (error_is_set(errp)) {
3392 qemu_close(out);
3393 return NULL;
3394 }
3395 }
3396
3397 return qemu_chr_open_fd(in, out);
3398}
3399
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003400static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3401 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003402{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003403#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003404 int fd;
3405
3406 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3407 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003408 return NULL;
3409 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003410 socket_set_nonblock(fd);
3411 return qemu_chr_open_tty_fd(fd);
3412#else
3413 error_setg(errp, "character device backend type 'serial' not supported");
3414 return NULL;
3415#endif
3416}
3417
3418static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3419 Error **errp)
3420{
3421#ifdef HAVE_CHARDEV_PARPORT
3422 int fd;
3423
3424 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3425 if (error_is_set(errp)) {
3426 return NULL;
3427 }
3428 return qemu_chr_open_pp_fd(fd);
3429#else
3430 error_setg(errp, "character device backend type 'parallel' not supported");
3431 return NULL;
3432#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003433}
3434
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003435#endif /* WIN32 */
3436
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003437static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3438 Error **errp)
3439{
3440 SocketAddress *addr = sock->addr;
3441 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3442 bool is_listen = sock->has_server ? sock->server : true;
3443 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3444 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3445 int fd;
3446
3447 if (is_listen) {
3448 fd = socket_listen(addr, errp);
3449 } else {
3450 fd = socket_connect(addr, errp, NULL, NULL);
3451 }
3452 if (error_is_set(errp)) {
3453 return NULL;
3454 }
3455 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3456 is_telnet, is_waitconnect, errp);
3457}
3458
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003459ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3460 Error **errp)
3461{
3462 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3463 CharDriverState *chr = NULL;
3464
3465 chr = qemu_chr_find(id);
3466 if (chr) {
3467 error_setg(errp, "Chardev '%s' already exists", id);
3468 g_free(ret);
3469 return NULL;
3470 }
3471
3472 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003473 case CHARDEV_BACKEND_KIND_FILE:
3474 chr = qmp_chardev_open_file(backend->file, errp);
3475 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003476 case CHARDEV_BACKEND_KIND_SERIAL:
3477 chr = qmp_chardev_open_serial(backend->serial, errp);
3478 break;
3479 case CHARDEV_BACKEND_KIND_PARALLEL:
3480 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003481 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003482 case CHARDEV_BACKEND_KIND_SOCKET:
3483 chr = qmp_chardev_open_socket(backend->socket, errp);
3484 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003485#ifdef HAVE_CHARDEV_TTY
3486 case CHARDEV_BACKEND_KIND_PTY:
3487 {
3488 /* qemu_chr_open_pty sets "path" in opts */
3489 QemuOpts *opts;
3490 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3491 chr = qemu_chr_open_pty(opts);
3492 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3493 ret->has_pty = true;
3494 qemu_opts_del(opts);
3495 break;
3496 }
3497#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003498 case CHARDEV_BACKEND_KIND_NULL:
3499 chr = qemu_chr_open_null(NULL);
3500 break;
3501 default:
3502 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3503 break;
3504 }
3505
3506 if (chr == NULL && !error_is_set(errp)) {
3507 error_setg(errp, "Failed to create chardev");
3508 }
3509 if (chr) {
3510 chr->label = g_strdup(id);
3511 chr->avail_connections = 1;
3512 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3513 return ret;
3514 } else {
3515 g_free(ret);
3516 return NULL;
3517 }
3518}
3519
3520void qmp_chardev_remove(const char *id, Error **errp)
3521{
3522 CharDriverState *chr;
3523
3524 chr = qemu_chr_find(id);
3525 if (NULL == chr) {
3526 error_setg(errp, "Chardev '%s' not found", id);
3527 return;
3528 }
3529 if (chr->chr_can_read || chr->chr_read ||
3530 chr->chr_event || chr->handler_opaque) {
3531 error_setg(errp, "Chardev '%s' is busy", id);
3532 return;
3533 }
3534 qemu_chr_delete(chr);
3535}