blob: b3ff4700ff686bbbb0c86e9f601523881f681410 [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
Fabien Chouteaudb418a02011-10-06 16:37:51 +0200542#define STDIO_MAX_CLIENTS 1
543static int stdio_nb_clients;
544
aliguori6f97dba2008-10-31 18:49:55 +0000545#ifndef _WIN32
546
547typedef struct {
548 int fd_in, fd_out;
549 int max_size;
550} FDCharDriver;
551
aliguori6f97dba2008-10-31 18:49:55 +0000552
553static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
554{
555 FDCharDriver *s = chr->opaque;
556 return send_all(s->fd_out, buf, len);
557}
558
559static int fd_chr_read_poll(void *opaque)
560{
561 CharDriverState *chr = opaque;
562 FDCharDriver *s = chr->opaque;
563
Anthony Liguori909cda12011-08-15 11:17:31 -0500564 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000565 return s->max_size;
566}
567
568static void fd_chr_read(void *opaque)
569{
570 CharDriverState *chr = opaque;
571 FDCharDriver *s = chr->opaque;
572 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530573 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000574
575 len = sizeof(buf);
576 if (len > s->max_size)
577 len = s->max_size;
578 if (len == 0)
579 return;
580 size = read(s->fd_in, buf, len);
581 if (size == 0) {
582 /* FD has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100584 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000585 return;
586 }
587 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500588 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000589 }
590}
591
592static void fd_chr_update_read_handler(CharDriverState *chr)
593{
594 FDCharDriver *s = chr->opaque;
595
596 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500597 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000598 } else {
599 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
600 fd_chr_read, NULL, chr);
601 }
602 }
603}
604
605static void fd_chr_close(struct CharDriverState *chr)
606{
607 FDCharDriver *s = chr->opaque;
608
609 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500610 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000611 } else {
612 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
613 }
614 }
615
Anthony Liguori7267c092011-08-20 22:09:37 -0500616 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100617 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000618}
619
620/* open a character device to a unix fd */
621static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
622{
623 CharDriverState *chr;
624 FDCharDriver *s;
625
Anthony Liguori7267c092011-08-20 22:09:37 -0500626 chr = g_malloc0(sizeof(CharDriverState));
627 s = g_malloc0(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000628 s->fd_in = fd_in;
629 s->fd_out = fd_out;
630 chr->opaque = s;
631 chr->chr_write = fd_chr_write;
632 chr->chr_update_read_handler = fd_chr_update_read_handler;
633 chr->chr_close = fd_chr_close;
634
Amit Shah127338e2009-11-03 19:59:56 +0530635 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000636
637 return chr;
638}
639
Markus Armbruster1f514702012-02-07 15:09:08 +0100640static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000641{
642 int fd_out;
643
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100644 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200645 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100646 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100647 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100648 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100649 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000650}
651
Markus Armbruster1f514702012-02-07 15:09:08 +0100652static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000653{
654 int fd_in, fd_out;
655 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200656 const char *filename = qemu_opt_get(opts, "path");
657
658 if (filename == NULL) {
659 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100660 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200661 }
aliguori6f97dba2008-10-31 18:49:55 +0000662
663 snprintf(filename_in, 256, "%s.in", filename);
664 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100665 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
666 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000667 if (fd_in < 0 || fd_out < 0) {
668 if (fd_in >= 0)
669 close(fd_in);
670 if (fd_out >= 0)
671 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100672 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100673 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100674 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100675 }
aliguori6f97dba2008-10-31 18:49:55 +0000676 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100677 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000678}
679
680
681/* for STDIO, we handle the case where several clients use it
682 (nographic mode) */
683
684#define TERM_FIFO_MAX_SIZE 1
685
686static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
687static int term_fifo_size;
688
689static int stdio_read_poll(void *opaque)
690{
691 CharDriverState *chr = opaque;
692
693 /* try to flush the queue if needed */
Anthony Liguori909cda12011-08-15 11:17:31 -0500694 if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500695 qemu_chr_be_write(chr, term_fifo, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000696 term_fifo_size = 0;
697 }
698 /* see if we can absorb more chars */
699 if (term_fifo_size == 0)
700 return 1;
701 else
702 return 0;
703}
704
705static void stdio_read(void *opaque)
706{
707 int size;
708 uint8_t buf[1];
709 CharDriverState *chr = opaque;
710
711 size = read(0, buf, 1);
712 if (size == 0) {
713 /* stdin has been closed. Remove it from the active list. */
714 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100715 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000716 return;
717 }
718 if (size > 0) {
Anthony Liguori909cda12011-08-15 11:17:31 -0500719 if (qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500720 qemu_chr_be_write(chr, buf, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000721 } else if (term_fifo_size == 0) {
722 term_fifo[term_fifo_size++] = buf[0];
723 }
724 }
725}
726
727/* init terminal so that we can grab keys */
728static struct termios oldtty;
729static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100730static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000731
732static void term_exit(void)
733{
734 tcsetattr (0, TCSANOW, &oldtty);
735 fcntl(0, F_SETFL, old_fd0_flags);
736}
737
Paolo Bonzinibb002512010-12-23 13:42:50 +0100738static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000739{
740 struct termios tty;
741
Paolo Bonzini03693642010-12-23 13:42:49 +0100742 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100743 if (!echo) {
744 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000745 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100746 tty.c_oflag |= OPOST;
747 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
748 tty.c_cflag &= ~(CSIZE|PARENB);
749 tty.c_cflag |= CS8;
750 tty.c_cc[VMIN] = 1;
751 tty.c_cc[VTIME] = 0;
752 }
aliguori6f97dba2008-10-31 18:49:55 +0000753 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100754 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000755 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000756
757 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000758}
759
760static void qemu_chr_close_stdio(struct CharDriverState *chr)
761{
762 term_exit();
763 stdio_nb_clients--;
764 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
765 fd_chr_close(chr);
766}
767
Markus Armbruster1f514702012-02-07 15:09:08 +0100768static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000769{
770 CharDriverState *chr;
771
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100772 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100773 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100774 }
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400775 if (is_daemonized()) {
776 error_report("cannot use stdio with -daemonize");
777 return NULL;
778 }
Paolo Bonzini03693642010-12-23 13:42:49 +0100779 if (stdio_nb_clients == 0) {
780 old_fd0_flags = fcntl(0, F_GETFL);
781 tcgetattr (0, &oldtty);
782 fcntl(0, F_SETFL, O_NONBLOCK);
783 atexit(term_exit);
784 }
785
aliguori6f97dba2008-10-31 18:49:55 +0000786 chr = qemu_chr_open_fd(0, 1);
787 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100788 chr->chr_set_echo = qemu_chr_set_echo_stdio;
aliguori6f97dba2008-10-31 18:49:55 +0000789 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
790 stdio_nb_clients++;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100791 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
792 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500793 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000794
Markus Armbruster1f514702012-02-07 15:09:08 +0100795 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000796}
797
798#ifdef __sun__
799/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000800static int openpty(int *amaster, int *aslave, char *name,
801 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000802{
803 const char *slave;
804 int mfd = -1, sfd = -1;
805
806 *amaster = *aslave = -1;
807
808 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
809 if (mfd < 0)
810 goto err;
811
812 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
813 goto err;
814
815 if ((slave = ptsname(mfd)) == NULL)
816 goto err;
817
818 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
819 goto err;
820
821 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
822 (termp != NULL && tcgetattr(sfd, termp) < 0))
823 goto err;
824
825 if (amaster)
826 *amaster = mfd;
827 if (aslave)
828 *aslave = sfd;
829 if (winp)
830 ioctl(sfd, TIOCSWINSZ, winp);
831
832 return 0;
833
834err:
835 if (sfd != -1)
836 close(sfd);
837 close(mfd);
838 return -1;
839}
840
blueswir13f4cb3d2009-04-13 16:31:01 +0000841static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000842{
843 termios_p->c_iflag &=
844 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
845 termios_p->c_oflag &= ~OPOST;
846 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
847 termios_p->c_cflag &= ~(CSIZE|PARENB);
848 termios_p->c_cflag |= CS8;
849
850 termios_p->c_cc[VMIN] = 0;
851 termios_p->c_cc[VTIME] = 0;
852}
853#endif
854
855#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100856 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
857 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000858
859typedef struct {
860 int fd;
861 int connected;
862 int polling;
863 int read_bytes;
864 QEMUTimer *timer;
865} PtyCharDriver;
866
867static void pty_chr_update_read_handler(CharDriverState *chr);
868static void pty_chr_state(CharDriverState *chr, int connected);
869
870static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
871{
872 PtyCharDriver *s = chr->opaque;
873
874 if (!s->connected) {
875 /* guest sends data, check for (re-)connect */
876 pty_chr_update_read_handler(chr);
877 return 0;
878 }
879 return send_all(s->fd, buf, len);
880}
881
882static int pty_chr_read_poll(void *opaque)
883{
884 CharDriverState *chr = opaque;
885 PtyCharDriver *s = chr->opaque;
886
Anthony Liguori909cda12011-08-15 11:17:31 -0500887 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000888 return s->read_bytes;
889}
890
891static void pty_chr_read(void *opaque)
892{
893 CharDriverState *chr = opaque;
894 PtyCharDriver *s = chr->opaque;
895 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530896 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000897
898 len = sizeof(buf);
899 if (len > s->read_bytes)
900 len = s->read_bytes;
901 if (len == 0)
902 return;
903 size = read(s->fd, buf, len);
904 if ((size == -1 && errno == EIO) ||
905 (size == 0)) {
906 pty_chr_state(chr, 0);
907 return;
908 }
909 if (size > 0) {
910 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500911 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000912 }
913}
914
915static void pty_chr_update_read_handler(CharDriverState *chr)
916{
917 PtyCharDriver *s = chr->opaque;
918
919 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
920 pty_chr_read, NULL, chr);
921 s->polling = 1;
922 /*
923 * Short timeout here: just need wait long enougth that qemu makes
924 * it through the poll loop once. When reconnected we want a
925 * short timeout so we notice it almost instantly. Otherwise
926 * read() gives us -EIO instantly, making pty_chr_state() reset the
927 * timeout to the normal (much longer) poll interval before the
928 * timer triggers.
929 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100930 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +0000931}
932
933static void pty_chr_state(CharDriverState *chr, int connected)
934{
935 PtyCharDriver *s = chr->opaque;
936
937 if (!connected) {
938 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
939 s->connected = 0;
940 s->polling = 0;
941 /* (re-)connect poll interval for idle guests: once per second.
942 * We check more frequently in case the guests sends data to
943 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100944 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +0000945 } else {
946 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530947 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000948 s->connected = 1;
949 }
950}
951
952static void pty_chr_timer(void *opaque)
953{
954 struct CharDriverState *chr = opaque;
955 PtyCharDriver *s = chr->opaque;
956
957 if (s->connected)
958 return;
959 if (s->polling) {
960 /* If we arrive here without polling being cleared due
961 * read returning -EIO, then we are (re-)connected */
962 pty_chr_state(chr, 1);
963 return;
964 }
965
966 /* Next poll ... */
967 pty_chr_update_read_handler(chr);
968}
969
970static void pty_chr_close(struct CharDriverState *chr)
971{
972 PtyCharDriver *s = chr->opaque;
973
974 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
975 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000976 qemu_del_timer(s->timer);
977 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -0500978 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100979 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000980}
981
Markus Armbruster1f514702012-02-07 15:09:08 +0100982static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000983{
984 CharDriverState *chr;
985 PtyCharDriver *s;
986 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +0800987 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +0100988 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000989#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000990 char pty_name[PATH_MAX];
991#define q_ptsname(x) pty_name
992#else
993 char *pty_name = NULL;
994#define q_ptsname(x) ptsname(x)
995#endif
996
Markus Armbrustera4e26042011-11-11 10:40:05 +0100997 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100998 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000999 }
1000
1001 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001002 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001003 cfmakeraw(&tty);
1004 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1005 close(slave_fd);
1006
Markus Armbrustera4e26042011-11-11 10:40:05 +01001007 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001008
Markus Armbrustera4e26042011-11-11 10:40:05 +01001009 len = strlen(q_ptsname(master_fd)) + 5;
1010 chr->filename = g_malloc(len);
1011 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1012 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001013
1014 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001015 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1016 q_ptsname(master_fd),
1017 label ? " (label " : "",
1018 label ? label : "",
1019 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001020
1021 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001022 chr->opaque = s;
1023 chr->chr_write = pty_chr_write;
1024 chr->chr_update_read_handler = pty_chr_update_read_handler;
1025 chr->chr_close = pty_chr_close;
1026
Markus Armbrustera4e26042011-11-11 10:40:05 +01001027 s->fd = master_fd;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001028 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001029
Markus Armbruster1f514702012-02-07 15:09:08 +01001030 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001031}
1032
1033static void tty_serial_init(int fd, int speed,
1034 int parity, int data_bits, int stop_bits)
1035{
1036 struct termios tty;
1037 speed_t spd;
1038
1039#if 0
1040 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1041 speed, parity, data_bits, stop_bits);
1042#endif
1043 tcgetattr (fd, &tty);
1044
Stefan Weil45eea132009-10-26 16:10:10 +01001045#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1046 speed = speed * 10 / 11;
1047 do {
1048 check_speed(50);
1049 check_speed(75);
1050 check_speed(110);
1051 check_speed(134);
1052 check_speed(150);
1053 check_speed(200);
1054 check_speed(300);
1055 check_speed(600);
1056 check_speed(1200);
1057 check_speed(1800);
1058 check_speed(2400);
1059 check_speed(4800);
1060 check_speed(9600);
1061 check_speed(19200);
1062 check_speed(38400);
1063 /* Non-Posix values follow. They may be unsupported on some systems. */
1064 check_speed(57600);
1065 check_speed(115200);
1066#ifdef B230400
1067 check_speed(230400);
1068#endif
1069#ifdef B460800
1070 check_speed(460800);
1071#endif
1072#ifdef B500000
1073 check_speed(500000);
1074#endif
1075#ifdef B576000
1076 check_speed(576000);
1077#endif
1078#ifdef B921600
1079 check_speed(921600);
1080#endif
1081#ifdef B1000000
1082 check_speed(1000000);
1083#endif
1084#ifdef B1152000
1085 check_speed(1152000);
1086#endif
1087#ifdef B1500000
1088 check_speed(1500000);
1089#endif
1090#ifdef B2000000
1091 check_speed(2000000);
1092#endif
1093#ifdef B2500000
1094 check_speed(2500000);
1095#endif
1096#ifdef B3000000
1097 check_speed(3000000);
1098#endif
1099#ifdef B3500000
1100 check_speed(3500000);
1101#endif
1102#ifdef B4000000
1103 check_speed(4000000);
1104#endif
aliguori6f97dba2008-10-31 18:49:55 +00001105 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001106 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001107
1108 cfsetispeed(&tty, spd);
1109 cfsetospeed(&tty, spd);
1110
1111 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1112 |INLCR|IGNCR|ICRNL|IXON);
1113 tty.c_oflag |= OPOST;
1114 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1115 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1116 switch(data_bits) {
1117 default:
1118 case 8:
1119 tty.c_cflag |= CS8;
1120 break;
1121 case 7:
1122 tty.c_cflag |= CS7;
1123 break;
1124 case 6:
1125 tty.c_cflag |= CS6;
1126 break;
1127 case 5:
1128 tty.c_cflag |= CS5;
1129 break;
1130 }
1131 switch(parity) {
1132 default:
1133 case 'N':
1134 break;
1135 case 'E':
1136 tty.c_cflag |= PARENB;
1137 break;
1138 case 'O':
1139 tty.c_cflag |= PARENB | PARODD;
1140 break;
1141 }
1142 if (stop_bits == 2)
1143 tty.c_cflag |= CSTOPB;
1144
1145 tcsetattr (fd, TCSANOW, &tty);
1146}
1147
1148static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1149{
1150 FDCharDriver *s = chr->opaque;
1151
1152 switch(cmd) {
1153 case CHR_IOCTL_SERIAL_SET_PARAMS:
1154 {
1155 QEMUSerialSetParams *ssp = arg;
1156 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1157 ssp->data_bits, ssp->stop_bits);
1158 }
1159 break;
1160 case CHR_IOCTL_SERIAL_SET_BREAK:
1161 {
1162 int enable = *(int *)arg;
1163 if (enable)
1164 tcsendbreak(s->fd_in, 1);
1165 }
1166 break;
1167 case CHR_IOCTL_SERIAL_GET_TIOCM:
1168 {
1169 int sarg = 0;
1170 int *targ = (int *)arg;
1171 ioctl(s->fd_in, TIOCMGET, &sarg);
1172 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001173 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001174 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001175 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001176 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001177 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001178 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001179 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001180 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001181 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001182 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001183 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001184 *targ |= CHR_TIOCM_RTS;
1185 }
1186 break;
1187 case CHR_IOCTL_SERIAL_SET_TIOCM:
1188 {
1189 int sarg = *(int *)arg;
1190 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001191 ioctl(s->fd_in, TIOCMGET, &targ);
1192 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1193 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1194 if (sarg & CHR_TIOCM_CTS)
1195 targ |= TIOCM_CTS;
1196 if (sarg & CHR_TIOCM_CAR)
1197 targ |= TIOCM_CAR;
1198 if (sarg & CHR_TIOCM_DSR)
1199 targ |= TIOCM_DSR;
1200 if (sarg & CHR_TIOCM_RI)
1201 targ |= TIOCM_RI;
1202 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001203 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001204 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001205 targ |= TIOCM_RTS;
1206 ioctl(s->fd_in, TIOCMSET, &targ);
1207 }
1208 break;
1209 default:
1210 return -ENOTSUP;
1211 }
1212 return 0;
1213}
1214
David Ahern4266a132010-02-10 18:27:17 -07001215static void qemu_chr_close_tty(CharDriverState *chr)
1216{
1217 FDCharDriver *s = chr->opaque;
1218 int fd = -1;
1219
1220 if (s) {
1221 fd = s->fd_in;
1222 }
1223
1224 fd_chr_close(chr);
1225
1226 if (fd >= 0) {
1227 close(fd);
1228 }
1229}
1230
Markus Armbruster1f514702012-02-07 15:09:08 +01001231static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001232{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001233 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001234 CharDriverState *chr;
1235 int fd;
1236
Markus Armbrusterb181e042012-02-07 15:09:09 +01001237 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001238 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001239 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001240 }
aliguori6f97dba2008-10-31 18:49:55 +00001241 tty_serial_init(fd, 115200, 'N', 8, 1);
1242 chr = qemu_chr_open_fd(fd, fd);
aliguori6f97dba2008-10-31 18:49:55 +00001243 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001244 chr->chr_close = qemu_chr_close_tty;
Markus Armbruster1f514702012-02-07 15:09:08 +01001245 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001246}
1247#else /* ! __linux__ && ! __sun__ */
Markus Armbruster1f514702012-02-07 15:09:08 +01001248static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001249{
Markus Armbruster1f514702012-02-07 15:09:08 +01001250 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001251}
1252#endif /* __linux__ || __sun__ */
1253
1254#if defined(__linux__)
1255typedef struct {
1256 int fd;
1257 int mode;
1258} ParallelCharDriver;
1259
1260static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1261{
1262 if (s->mode != mode) {
1263 int m = mode;
1264 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1265 return 0;
1266 s->mode = mode;
1267 }
1268 return 1;
1269}
1270
1271static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1272{
1273 ParallelCharDriver *drv = chr->opaque;
1274 int fd = drv->fd;
1275 uint8_t b;
1276
1277 switch(cmd) {
1278 case CHR_IOCTL_PP_READ_DATA:
1279 if (ioctl(fd, PPRDATA, &b) < 0)
1280 return -ENOTSUP;
1281 *(uint8_t *)arg = b;
1282 break;
1283 case CHR_IOCTL_PP_WRITE_DATA:
1284 b = *(uint8_t *)arg;
1285 if (ioctl(fd, PPWDATA, &b) < 0)
1286 return -ENOTSUP;
1287 break;
1288 case CHR_IOCTL_PP_READ_CONTROL:
1289 if (ioctl(fd, PPRCONTROL, &b) < 0)
1290 return -ENOTSUP;
1291 /* Linux gives only the lowest bits, and no way to know data
1292 direction! For better compatibility set the fixed upper
1293 bits. */
1294 *(uint8_t *)arg = b | 0xc0;
1295 break;
1296 case CHR_IOCTL_PP_WRITE_CONTROL:
1297 b = *(uint8_t *)arg;
1298 if (ioctl(fd, PPWCONTROL, &b) < 0)
1299 return -ENOTSUP;
1300 break;
1301 case CHR_IOCTL_PP_READ_STATUS:
1302 if (ioctl(fd, PPRSTATUS, &b) < 0)
1303 return -ENOTSUP;
1304 *(uint8_t *)arg = b;
1305 break;
1306 case CHR_IOCTL_PP_DATA_DIR:
1307 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1308 return -ENOTSUP;
1309 break;
1310 case CHR_IOCTL_PP_EPP_READ_ADDR:
1311 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1312 struct ParallelIOArg *parg = arg;
1313 int n = read(fd, parg->buffer, parg->count);
1314 if (n != parg->count) {
1315 return -EIO;
1316 }
1317 }
1318 break;
1319 case CHR_IOCTL_PP_EPP_READ:
1320 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1321 struct ParallelIOArg *parg = arg;
1322 int n = read(fd, parg->buffer, parg->count);
1323 if (n != parg->count) {
1324 return -EIO;
1325 }
1326 }
1327 break;
1328 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1329 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1330 struct ParallelIOArg *parg = arg;
1331 int n = write(fd, parg->buffer, parg->count);
1332 if (n != parg->count) {
1333 return -EIO;
1334 }
1335 }
1336 break;
1337 case CHR_IOCTL_PP_EPP_WRITE:
1338 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1339 struct ParallelIOArg *parg = arg;
1340 int n = write(fd, parg->buffer, parg->count);
1341 if (n != parg->count) {
1342 return -EIO;
1343 }
1344 }
1345 break;
1346 default:
1347 return -ENOTSUP;
1348 }
1349 return 0;
1350}
1351
1352static void pp_close(CharDriverState *chr)
1353{
1354 ParallelCharDriver *drv = chr->opaque;
1355 int fd = drv->fd;
1356
1357 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1358 ioctl(fd, PPRELEASE);
1359 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001360 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001361 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001362}
1363
Markus Armbruster1f514702012-02-07 15:09:08 +01001364static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001365{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001366 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001367 CharDriverState *chr;
1368 ParallelCharDriver *drv;
1369 int fd;
1370
Markus Armbrusterb181e042012-02-07 15:09:09 +01001371 TFR(fd = qemu_open(filename, O_RDWR));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001372 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001373 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001374 }
aliguori6f97dba2008-10-31 18:49:55 +00001375
1376 if (ioctl(fd, PPCLAIM) < 0) {
1377 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001378 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001379 }
1380
Anthony Liguori7267c092011-08-20 22:09:37 -05001381 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001382 drv->fd = fd;
1383 drv->mode = IEEE1284_MODE_COMPAT;
1384
Anthony Liguori7267c092011-08-20 22:09:37 -05001385 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001386 chr->chr_write = null_chr_write;
1387 chr->chr_ioctl = pp_ioctl;
1388 chr->chr_close = pp_close;
1389 chr->opaque = drv;
1390
Amit Shah127338e2009-11-03 19:59:56 +05301391 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001392
Markus Armbruster1f514702012-02-07 15:09:08 +01001393 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001394}
1395#endif /* __linux__ */
1396
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001397#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001398static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1399{
Stefan Weile0efb992011-02-23 19:09:16 +01001400 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001401 uint8_t b;
1402
1403 switch(cmd) {
1404 case CHR_IOCTL_PP_READ_DATA:
1405 if (ioctl(fd, PPIGDATA, &b) < 0)
1406 return -ENOTSUP;
1407 *(uint8_t *)arg = b;
1408 break;
1409 case CHR_IOCTL_PP_WRITE_DATA:
1410 b = *(uint8_t *)arg;
1411 if (ioctl(fd, PPISDATA, &b) < 0)
1412 return -ENOTSUP;
1413 break;
1414 case CHR_IOCTL_PP_READ_CONTROL:
1415 if (ioctl(fd, PPIGCTRL, &b) < 0)
1416 return -ENOTSUP;
1417 *(uint8_t *)arg = b;
1418 break;
1419 case CHR_IOCTL_PP_WRITE_CONTROL:
1420 b = *(uint8_t *)arg;
1421 if (ioctl(fd, PPISCTRL, &b) < 0)
1422 return -ENOTSUP;
1423 break;
1424 case CHR_IOCTL_PP_READ_STATUS:
1425 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1426 return -ENOTSUP;
1427 *(uint8_t *)arg = b;
1428 break;
1429 default:
1430 return -ENOTSUP;
1431 }
1432 return 0;
1433}
1434
Markus Armbruster1f514702012-02-07 15:09:08 +01001435static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001436{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001437 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001438 CharDriverState *chr;
1439 int fd;
1440
Markus Armbrusterb181e042012-02-07 15:09:09 +01001441 fd = qemu_open(filename, O_RDWR);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001442 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001443 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001444 }
blueswir16972f932008-11-22 20:49:12 +00001445
Anthony Liguori7267c092011-08-20 22:09:37 -05001446 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001447 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001448 chr->chr_write = null_chr_write;
1449 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001450 return chr;
blueswir16972f932008-11-22 20:49:12 +00001451}
1452#endif
1453
aliguori6f97dba2008-10-31 18:49:55 +00001454#else /* _WIN32 */
1455
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001456static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1457
aliguori6f97dba2008-10-31 18:49:55 +00001458typedef struct {
1459 int max_size;
1460 HANDLE hcom, hrecv, hsend;
1461 OVERLAPPED orecv, osend;
1462 BOOL fpipe;
1463 DWORD len;
1464} WinCharState;
1465
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001466typedef struct {
1467 HANDLE hStdIn;
1468 HANDLE hInputReadyEvent;
1469 HANDLE hInputDoneEvent;
1470 HANDLE hInputThread;
1471 uint8_t win_stdio_buf;
1472} WinStdioCharState;
1473
aliguori6f97dba2008-10-31 18:49:55 +00001474#define NSENDBUF 2048
1475#define NRECVBUF 2048
1476#define MAXCONNECT 1
1477#define NTIMEOUT 5000
1478
1479static int win_chr_poll(void *opaque);
1480static int win_chr_pipe_poll(void *opaque);
1481
1482static void win_chr_close(CharDriverState *chr)
1483{
1484 WinCharState *s = chr->opaque;
1485
1486 if (s->hsend) {
1487 CloseHandle(s->hsend);
1488 s->hsend = NULL;
1489 }
1490 if (s->hrecv) {
1491 CloseHandle(s->hrecv);
1492 s->hrecv = NULL;
1493 }
1494 if (s->hcom) {
1495 CloseHandle(s->hcom);
1496 s->hcom = NULL;
1497 }
1498 if (s->fpipe)
1499 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1500 else
1501 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301502
Hans de Goedea425d232011-11-19 10:22:43 +01001503 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001504}
1505
1506static int win_chr_init(CharDriverState *chr, const char *filename)
1507{
1508 WinCharState *s = chr->opaque;
1509 COMMCONFIG comcfg;
1510 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1511 COMSTAT comstat;
1512 DWORD size;
1513 DWORD err;
1514
1515 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1516 if (!s->hsend) {
1517 fprintf(stderr, "Failed CreateEvent\n");
1518 goto fail;
1519 }
1520 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1521 if (!s->hrecv) {
1522 fprintf(stderr, "Failed CreateEvent\n");
1523 goto fail;
1524 }
1525
1526 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1527 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1528 if (s->hcom == INVALID_HANDLE_VALUE) {
1529 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1530 s->hcom = NULL;
1531 goto fail;
1532 }
1533
1534 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1535 fprintf(stderr, "Failed SetupComm\n");
1536 goto fail;
1537 }
1538
1539 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1540 size = sizeof(COMMCONFIG);
1541 GetDefaultCommConfig(filename, &comcfg, &size);
1542 comcfg.dcb.DCBlength = sizeof(DCB);
1543 CommConfigDialog(filename, NULL, &comcfg);
1544
1545 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1546 fprintf(stderr, "Failed SetCommState\n");
1547 goto fail;
1548 }
1549
1550 if (!SetCommMask(s->hcom, EV_ERR)) {
1551 fprintf(stderr, "Failed SetCommMask\n");
1552 goto fail;
1553 }
1554
1555 cto.ReadIntervalTimeout = MAXDWORD;
1556 if (!SetCommTimeouts(s->hcom, &cto)) {
1557 fprintf(stderr, "Failed SetCommTimeouts\n");
1558 goto fail;
1559 }
1560
1561 if (!ClearCommError(s->hcom, &err, &comstat)) {
1562 fprintf(stderr, "Failed ClearCommError\n");
1563 goto fail;
1564 }
1565 qemu_add_polling_cb(win_chr_poll, chr);
1566 return 0;
1567
1568 fail:
1569 win_chr_close(chr);
1570 return -1;
1571}
1572
1573static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1574{
1575 WinCharState *s = chr->opaque;
1576 DWORD len, ret, size, err;
1577
1578 len = len1;
1579 ZeroMemory(&s->osend, sizeof(s->osend));
1580 s->osend.hEvent = s->hsend;
1581 while (len > 0) {
1582 if (s->hsend)
1583 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1584 else
1585 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1586 if (!ret) {
1587 err = GetLastError();
1588 if (err == ERROR_IO_PENDING) {
1589 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1590 if (ret) {
1591 buf += size;
1592 len -= size;
1593 } else {
1594 break;
1595 }
1596 } else {
1597 break;
1598 }
1599 } else {
1600 buf += size;
1601 len -= size;
1602 }
1603 }
1604 return len1 - len;
1605}
1606
1607static int win_chr_read_poll(CharDriverState *chr)
1608{
1609 WinCharState *s = chr->opaque;
1610
Anthony Liguori909cda12011-08-15 11:17:31 -05001611 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001612 return s->max_size;
1613}
1614
1615static void win_chr_readfile(CharDriverState *chr)
1616{
1617 WinCharState *s = chr->opaque;
1618 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301619 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001620 DWORD size;
1621
1622 ZeroMemory(&s->orecv, sizeof(s->orecv));
1623 s->orecv.hEvent = s->hrecv;
1624 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1625 if (!ret) {
1626 err = GetLastError();
1627 if (err == ERROR_IO_PENDING) {
1628 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1629 }
1630 }
1631
1632 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001633 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001634 }
1635}
1636
1637static void win_chr_read(CharDriverState *chr)
1638{
1639 WinCharState *s = chr->opaque;
1640
1641 if (s->len > s->max_size)
1642 s->len = s->max_size;
1643 if (s->len == 0)
1644 return;
1645
1646 win_chr_readfile(chr);
1647}
1648
1649static int win_chr_poll(void *opaque)
1650{
1651 CharDriverState *chr = opaque;
1652 WinCharState *s = chr->opaque;
1653 COMSTAT status;
1654 DWORD comerr;
1655
1656 ClearCommError(s->hcom, &comerr, &status);
1657 if (status.cbInQue > 0) {
1658 s->len = status.cbInQue;
1659 win_chr_read_poll(chr);
1660 win_chr_read(chr);
1661 return 1;
1662 }
1663 return 0;
1664}
1665
Markus Armbruster1f514702012-02-07 15:09:08 +01001666static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001667{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001668 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001669 CharDriverState *chr;
1670 WinCharState *s;
1671
Anthony Liguori7267c092011-08-20 22:09:37 -05001672 chr = g_malloc0(sizeof(CharDriverState));
1673 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001674 chr->opaque = s;
1675 chr->chr_write = win_chr_write;
1676 chr->chr_close = win_chr_close;
1677
1678 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001679 g_free(s);
1680 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001681 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001682 }
Amit Shah127338e2009-11-03 19:59:56 +05301683 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001684 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001685}
1686
1687static int win_chr_pipe_poll(void *opaque)
1688{
1689 CharDriverState *chr = opaque;
1690 WinCharState *s = chr->opaque;
1691 DWORD size;
1692
1693 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1694 if (size > 0) {
1695 s->len = size;
1696 win_chr_read_poll(chr);
1697 win_chr_read(chr);
1698 return 1;
1699 }
1700 return 0;
1701}
1702
1703static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1704{
1705 WinCharState *s = chr->opaque;
1706 OVERLAPPED ov;
1707 int ret;
1708 DWORD size;
1709 char openname[256];
1710
1711 s->fpipe = TRUE;
1712
1713 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1714 if (!s->hsend) {
1715 fprintf(stderr, "Failed CreateEvent\n");
1716 goto fail;
1717 }
1718 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1719 if (!s->hrecv) {
1720 fprintf(stderr, "Failed CreateEvent\n");
1721 goto fail;
1722 }
1723
1724 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1725 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1726 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1727 PIPE_WAIT,
1728 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1729 if (s->hcom == INVALID_HANDLE_VALUE) {
1730 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1731 s->hcom = NULL;
1732 goto fail;
1733 }
1734
1735 ZeroMemory(&ov, sizeof(ov));
1736 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1737 ret = ConnectNamedPipe(s->hcom, &ov);
1738 if (ret) {
1739 fprintf(stderr, "Failed ConnectNamedPipe\n");
1740 goto fail;
1741 }
1742
1743 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1744 if (!ret) {
1745 fprintf(stderr, "Failed GetOverlappedResult\n");
1746 if (ov.hEvent) {
1747 CloseHandle(ov.hEvent);
1748 ov.hEvent = NULL;
1749 }
1750 goto fail;
1751 }
1752
1753 if (ov.hEvent) {
1754 CloseHandle(ov.hEvent);
1755 ov.hEvent = NULL;
1756 }
1757 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1758 return 0;
1759
1760 fail:
1761 win_chr_close(chr);
1762 return -1;
1763}
1764
1765
Markus Armbruster1f514702012-02-07 15:09:08 +01001766static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001767{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001768 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001769 CharDriverState *chr;
1770 WinCharState *s;
1771
Anthony Liguori7267c092011-08-20 22:09:37 -05001772 chr = g_malloc0(sizeof(CharDriverState));
1773 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001774 chr->opaque = s;
1775 chr->chr_write = win_chr_write;
1776 chr->chr_close = win_chr_close;
1777
1778 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001779 g_free(s);
1780 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001781 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001782 }
Amit Shah127338e2009-11-03 19:59:56 +05301783 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001784 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001785}
1786
Markus Armbruster1f514702012-02-07 15:09:08 +01001787static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001788{
1789 CharDriverState *chr;
1790 WinCharState *s;
1791
Anthony Liguori7267c092011-08-20 22:09:37 -05001792 chr = g_malloc0(sizeof(CharDriverState));
1793 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001794 s->hcom = fd_out;
1795 chr->opaque = s;
1796 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301797 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001798 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001799}
1800
Markus Armbruster1f514702012-02-07 15:09:08 +01001801static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001802{
Markus Armbruster1f514702012-02-07 15:09:08 +01001803 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001804}
1805
Markus Armbruster1f514702012-02-07 15:09:08 +01001806static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001807{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001808 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001809 HANDLE fd_out;
1810
1811 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1812 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001813 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001814 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001815 }
aliguori6f97dba2008-10-31 18:49:55 +00001816
Markus Armbruster1f514702012-02-07 15:09:08 +01001817 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001818}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001819
1820static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1821{
1822 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1823 DWORD dwSize;
1824 int len1;
1825
1826 len1 = len;
1827
1828 while (len1 > 0) {
1829 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1830 break;
1831 }
1832 buf += dwSize;
1833 len1 -= dwSize;
1834 }
1835
1836 return len - len1;
1837}
1838
1839static void win_stdio_wait_func(void *opaque)
1840{
1841 CharDriverState *chr = opaque;
1842 WinStdioCharState *stdio = chr->opaque;
1843 INPUT_RECORD buf[4];
1844 int ret;
1845 DWORD dwSize;
1846 int i;
1847
1848 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1849 &dwSize);
1850
1851 if (!ret) {
1852 /* Avoid error storm */
1853 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1854 return;
1855 }
1856
1857 for (i = 0; i < dwSize; i++) {
1858 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1859
1860 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1861 int j;
1862 if (kev->uChar.AsciiChar != 0) {
1863 for (j = 0; j < kev->wRepeatCount; j++) {
1864 if (qemu_chr_be_can_write(chr)) {
1865 uint8_t c = kev->uChar.AsciiChar;
1866 qemu_chr_be_write(chr, &c, 1);
1867 }
1868 }
1869 }
1870 }
1871 }
1872}
1873
1874static DWORD WINAPI win_stdio_thread(LPVOID param)
1875{
1876 CharDriverState *chr = param;
1877 WinStdioCharState *stdio = chr->opaque;
1878 int ret;
1879 DWORD dwSize;
1880
1881 while (1) {
1882
1883 /* Wait for one byte */
1884 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1885
1886 /* Exit in case of error, continue if nothing read */
1887 if (!ret) {
1888 break;
1889 }
1890 if (!dwSize) {
1891 continue;
1892 }
1893
1894 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1895 if (stdio->win_stdio_buf == '\r') {
1896 continue;
1897 }
1898
1899 /* Signal the main thread and wait until the byte was eaten */
1900 if (!SetEvent(stdio->hInputReadyEvent)) {
1901 break;
1902 }
1903 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1904 != WAIT_OBJECT_0) {
1905 break;
1906 }
1907 }
1908
1909 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
1910 return 0;
1911}
1912
1913static void win_stdio_thread_wait_func(void *opaque)
1914{
1915 CharDriverState *chr = opaque;
1916 WinStdioCharState *stdio = chr->opaque;
1917
1918 if (qemu_chr_be_can_write(chr)) {
1919 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
1920 }
1921
1922 SetEvent(stdio->hInputDoneEvent);
1923}
1924
1925static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
1926{
1927 WinStdioCharState *stdio = chr->opaque;
1928 DWORD dwMode = 0;
1929
1930 GetConsoleMode(stdio->hStdIn, &dwMode);
1931
1932 if (echo) {
1933 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
1934 } else {
1935 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
1936 }
1937}
1938
1939static void win_stdio_close(CharDriverState *chr)
1940{
1941 WinStdioCharState *stdio = chr->opaque;
1942
1943 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
1944 CloseHandle(stdio->hInputReadyEvent);
1945 }
1946 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
1947 CloseHandle(stdio->hInputDoneEvent);
1948 }
1949 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
1950 TerminateThread(stdio->hInputThread, 0);
1951 }
1952
1953 g_free(chr->opaque);
1954 g_free(chr);
1955 stdio_nb_clients--;
1956}
1957
Markus Armbruster1f514702012-02-07 15:09:08 +01001958static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001959{
1960 CharDriverState *chr;
1961 WinStdioCharState *stdio;
1962 DWORD dwMode;
1963 int is_console = 0;
1964
1965 if (stdio_nb_clients >= STDIO_MAX_CLIENTS
1966 || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001967 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001968 }
1969
1970 chr = g_malloc0(sizeof(CharDriverState));
1971 stdio = g_malloc0(sizeof(WinStdioCharState));
1972
1973 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1974 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
1975 fprintf(stderr, "cannot open stdio: invalid handle\n");
1976 exit(1);
1977 }
1978
1979 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
1980
1981 chr->opaque = stdio;
1982 chr->chr_write = win_stdio_write;
1983 chr->chr_close = win_stdio_close;
1984
1985 if (stdio_nb_clients == 0) {
1986 if (is_console) {
1987 if (qemu_add_wait_object(stdio->hStdIn,
1988 win_stdio_wait_func, chr)) {
1989 fprintf(stderr, "qemu_add_wait_object: failed\n");
1990 }
1991 } else {
1992 DWORD dwId;
1993
1994 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1995 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1996 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
1997 chr, 0, &dwId);
1998
1999 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2000 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2001 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2002 fprintf(stderr, "cannot create stdio thread or event\n");
2003 exit(1);
2004 }
2005 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2006 win_stdio_thread_wait_func, chr)) {
2007 fprintf(stderr, "qemu_add_wait_object: failed\n");
2008 }
2009 }
2010 }
2011
2012 dwMode |= ENABLE_LINE_INPUT;
2013
2014 stdio_clients[stdio_nb_clients++] = chr;
2015 if (stdio_nb_clients == 1 && is_console) {
2016 /* set the terminal in raw mode */
2017 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2018 dwMode |= ENABLE_PROCESSED_INPUT;
2019 }
2020
2021 SetConsoleMode(stdio->hStdIn, dwMode);
2022
2023 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2024 qemu_chr_fe_set_echo(chr, false);
2025
Markus Armbruster1f514702012-02-07 15:09:08 +01002026 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002027}
aliguori6f97dba2008-10-31 18:49:55 +00002028#endif /* !_WIN32 */
2029
2030/***********************************************************/
2031/* UDP Net console */
2032
2033typedef struct {
2034 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05302035 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002036 int bufcnt;
2037 int bufptr;
2038 int max_size;
2039} NetCharDriver;
2040
2041static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2042{
2043 NetCharDriver *s = chr->opaque;
2044
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002045 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00002046}
2047
2048static int udp_chr_read_poll(void *opaque)
2049{
2050 CharDriverState *chr = opaque;
2051 NetCharDriver *s = chr->opaque;
2052
Anthony Liguori909cda12011-08-15 11:17:31 -05002053 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002054
2055 /* If there were any stray characters in the queue process them
2056 * first
2057 */
2058 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002059 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002060 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002061 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002062 }
2063 return s->max_size;
2064}
2065
2066static void udp_chr_read(void *opaque)
2067{
2068 CharDriverState *chr = opaque;
2069 NetCharDriver *s = chr->opaque;
2070
2071 if (s->max_size == 0)
2072 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00002073 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00002074 s->bufptr = s->bufcnt;
2075 if (s->bufcnt <= 0)
2076 return;
2077
2078 s->bufptr = 0;
2079 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002080 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002081 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002082 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002083 }
2084}
2085
2086static void udp_chr_update_read_handler(CharDriverState *chr)
2087{
2088 NetCharDriver *s = chr->opaque;
2089
2090 if (s->fd >= 0) {
2091 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2092 udp_chr_read, NULL, chr);
2093 }
2094}
2095
aliguori819f56b2009-03-28 17:58:14 +00002096static void udp_chr_close(CharDriverState *chr)
2097{
2098 NetCharDriver *s = chr->opaque;
2099 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002100 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00002101 closesocket(s->fd);
2102 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002103 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002104 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002105}
2106
Markus Armbruster1f514702012-02-07 15:09:08 +01002107static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002108{
2109 CharDriverState *chr = NULL;
2110 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002111 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002112 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002113
Anthony Liguori7267c092011-08-20 22:09:37 -05002114 chr = g_malloc0(sizeof(CharDriverState));
2115 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002116
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002117 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002118 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002119 goto return_err;
2120 }
2121
2122 s->fd = fd;
2123 s->bufcnt = 0;
2124 s->bufptr = 0;
2125 chr->opaque = s;
2126 chr->chr_write = udp_chr_write;
2127 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002128 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002129 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002130
2131return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002132 if (local_err) {
2133 qerror_report_err(local_err);
2134 error_free(local_err);
2135 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002136 g_free(chr);
2137 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002138 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002139 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002140 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002141 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002142}
2143
2144/***********************************************************/
2145/* TCP Net console */
2146
2147typedef struct {
2148 int fd, listen_fd;
2149 int connected;
2150 int max_size;
2151 int do_telnetopt;
2152 int do_nodelay;
2153 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002154 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002155} TCPCharDriver;
2156
2157static void tcp_chr_accept(void *opaque);
2158
2159static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2160{
2161 TCPCharDriver *s = chr->opaque;
2162 if (s->connected) {
2163 return send_all(s->fd, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002164 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002165 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002166 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002167 }
2168}
2169
2170static int tcp_chr_read_poll(void *opaque)
2171{
2172 CharDriverState *chr = opaque;
2173 TCPCharDriver *s = chr->opaque;
2174 if (!s->connected)
2175 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002176 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002177 return s->max_size;
2178}
2179
2180#define IAC 255
2181#define IAC_BREAK 243
2182static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2183 TCPCharDriver *s,
2184 uint8_t *buf, int *size)
2185{
2186 /* Handle any telnet client's basic IAC options to satisfy char by
2187 * char mode with no echo. All IAC options will be removed from
2188 * the buf and the do_telnetopt variable will be used to track the
2189 * state of the width of the IAC information.
2190 *
2191 * IAC commands come in sets of 3 bytes with the exception of the
2192 * "IAC BREAK" command and the double IAC.
2193 */
2194
2195 int i;
2196 int j = 0;
2197
2198 for (i = 0; i < *size; i++) {
2199 if (s->do_telnetopt > 1) {
2200 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2201 /* Double IAC means send an IAC */
2202 if (j != i)
2203 buf[j] = buf[i];
2204 j++;
2205 s->do_telnetopt = 1;
2206 } else {
2207 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2208 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002209 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002210 s->do_telnetopt++;
2211 }
2212 s->do_telnetopt++;
2213 }
2214 if (s->do_telnetopt >= 4) {
2215 s->do_telnetopt = 1;
2216 }
2217 } else {
2218 if ((unsigned char)buf[i] == IAC) {
2219 s->do_telnetopt = 2;
2220 } else {
2221 if (j != i)
2222 buf[j] = buf[i];
2223 j++;
2224 }
2225 }
2226 }
2227 *size = j;
2228}
2229
Mark McLoughlin7d174052009-07-22 09:11:39 +01002230static int tcp_get_msgfd(CharDriverState *chr)
2231{
2232 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002233 int fd = s->msgfd;
2234 s->msgfd = -1;
2235 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002236}
2237
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002238#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002239static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2240{
2241 TCPCharDriver *s = chr->opaque;
2242 struct cmsghdr *cmsg;
2243
2244 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2245 int fd;
2246
2247 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2248 cmsg->cmsg_level != SOL_SOCKET ||
2249 cmsg->cmsg_type != SCM_RIGHTS)
2250 continue;
2251
2252 fd = *((int *)CMSG_DATA(cmsg));
2253 if (fd < 0)
2254 continue;
2255
Corey Bryant06138652012-08-14 16:43:42 -04002256#ifndef MSG_CMSG_CLOEXEC
2257 qemu_set_cloexec(fd);
2258#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002259 if (s->msgfd != -1)
2260 close(s->msgfd);
2261 s->msgfd = fd;
2262 }
2263}
2264
Mark McLoughlin9977c892009-07-22 09:11:38 +01002265static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2266{
2267 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002268 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002269 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002270 union {
2271 struct cmsghdr cmsg;
2272 char control[CMSG_SPACE(sizeof(int))];
2273 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002274 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002275 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002276
2277 iov[0].iov_base = buf;
2278 iov[0].iov_len = len;
2279
2280 msg.msg_iov = iov;
2281 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002282 msg.msg_control = &msg_control;
2283 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002284
Corey Bryant06138652012-08-14 16:43:42 -04002285#ifdef MSG_CMSG_CLOEXEC
2286 flags |= MSG_CMSG_CLOEXEC;
2287#endif
2288 ret = recvmsg(s->fd, &msg, flags);
2289 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002290 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002291 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002292
2293 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002294}
2295#else
2296static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2297{
2298 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002299 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002300}
2301#endif
2302
aliguori6f97dba2008-10-31 18:49:55 +00002303static void tcp_chr_read(void *opaque)
2304{
2305 CharDriverState *chr = opaque;
2306 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302307 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002308 int len, size;
2309
2310 if (!s->connected || s->max_size <= 0)
2311 return;
2312 len = sizeof(buf);
2313 if (len > s->max_size)
2314 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002315 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002316 if (size == 0) {
2317 /* connection closed */
2318 s->connected = 0;
2319 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002320 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002321 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002322 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002323 closesocket(s->fd);
2324 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002325 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002326 } else if (size > 0) {
2327 if (s->do_telnetopt)
2328 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2329 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002330 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002331 }
2332}
2333
Blue Swirl68c18d12010-08-15 09:46:24 +00002334#ifndef _WIN32
2335CharDriverState *qemu_chr_open_eventfd(int eventfd)
2336{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002337 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002338}
Blue Swirl68c18d12010-08-15 09:46:24 +00002339#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002340
aliguori6f97dba2008-10-31 18:49:55 +00002341static void tcp_chr_connect(void *opaque)
2342{
2343 CharDriverState *chr = opaque;
2344 TCPCharDriver *s = chr->opaque;
2345
2346 s->connected = 1;
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002347 if (s->fd >= 0) {
2348 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2349 tcp_chr_read, NULL, chr);
2350 }
Amit Shah127338e2009-11-03 19:59:56 +05302351 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002352}
2353
2354#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2355static void tcp_chr_telnet_init(int fd)
2356{
2357 char buf[3];
2358 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2359 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2360 send(fd, (char *)buf, 3, 0);
2361 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2362 send(fd, (char *)buf, 3, 0);
2363 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2364 send(fd, (char *)buf, 3, 0);
2365 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2366 send(fd, (char *)buf, 3, 0);
2367}
2368
2369static void socket_set_nodelay(int fd)
2370{
2371 int val = 1;
2372 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2373}
2374
Daniel P. Berrange13661082011-06-23 13:31:42 +01002375static int tcp_chr_add_client(CharDriverState *chr, int fd)
2376{
2377 TCPCharDriver *s = chr->opaque;
2378 if (s->fd != -1)
2379 return -1;
2380
2381 socket_set_nonblock(fd);
2382 if (s->do_nodelay)
2383 socket_set_nodelay(fd);
2384 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002385 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002386 tcp_chr_connect(chr);
2387
2388 return 0;
2389}
2390
aliguori6f97dba2008-10-31 18:49:55 +00002391static void tcp_chr_accept(void *opaque)
2392{
2393 CharDriverState *chr = opaque;
2394 TCPCharDriver *s = chr->opaque;
2395 struct sockaddr_in saddr;
2396#ifndef _WIN32
2397 struct sockaddr_un uaddr;
2398#endif
2399 struct sockaddr *addr;
2400 socklen_t len;
2401 int fd;
2402
2403 for(;;) {
2404#ifndef _WIN32
2405 if (s->is_unix) {
2406 len = sizeof(uaddr);
2407 addr = (struct sockaddr *)&uaddr;
2408 } else
2409#endif
2410 {
2411 len = sizeof(saddr);
2412 addr = (struct sockaddr *)&saddr;
2413 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002414 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002415 if (fd < 0 && errno != EINTR) {
2416 return;
2417 } else if (fd >= 0) {
2418 if (s->do_telnetopt)
2419 tcp_chr_telnet_init(fd);
2420 break;
2421 }
2422 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002423 if (tcp_chr_add_client(chr, fd) < 0)
2424 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002425}
2426
2427static void tcp_chr_close(CharDriverState *chr)
2428{
2429 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002430 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002431 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002432 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002433 }
2434 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002435 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002436 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002437 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002438 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002439 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002440}
2441
Markus Armbruster1f514702012-02-07 15:09:08 +01002442static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002443{
2444 CharDriverState *chr = NULL;
2445 TCPCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002446 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002447 int fd = -1;
2448 int is_listen;
2449 int is_waitconnect;
2450 int do_nodelay;
2451 int is_unix;
2452 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002453
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002454 is_listen = qemu_opt_get_bool(opts, "server", 0);
2455 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2456 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2457 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2458 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002459 if (!is_listen)
2460 is_waitconnect = 0;
2461
Anthony Liguori7267c092011-08-20 22:09:37 -05002462 chr = g_malloc0(sizeof(CharDriverState));
2463 s = g_malloc0(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002464
aliguorif07b6002008-11-11 20:54:09 +00002465 if (is_unix) {
2466 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002467 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002468 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002469 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002470 }
2471 } else {
2472 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002473 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002474 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002475 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002476 }
2477 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002478 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002479 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002480 }
aliguori6f97dba2008-10-31 18:49:55 +00002481
2482 if (!is_waitconnect)
2483 socket_set_nonblock(fd);
2484
2485 s->connected = 0;
2486 s->fd = -1;
2487 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002488 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002489 s->is_unix = is_unix;
2490 s->do_nodelay = do_nodelay && !is_unix;
2491
2492 chr->opaque = s;
2493 chr->chr_write = tcp_chr_write;
2494 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002495 chr->get_msgfd = tcp_get_msgfd;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002496 chr->chr_add_client = tcp_chr_add_client;
aliguori6f97dba2008-10-31 18:49:55 +00002497
2498 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002499 s->listen_fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002500 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002501 if (is_telnet)
2502 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002503
aliguori6f97dba2008-10-31 18:49:55 +00002504 } else {
aliguorif07b6002008-11-11 20:54:09 +00002505 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002506 s->fd = fd;
2507 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002508 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002509 }
2510
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002511 /* for "info chardev" monitor command */
Anthony Liguori7267c092011-08-20 22:09:37 -05002512 chr->filename = g_malloc(256);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002513 if (is_unix) {
2514 snprintf(chr->filename, 256, "unix:%s%s",
2515 qemu_opt_get(opts, "path"),
2516 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2517 } else if (is_telnet) {
2518 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2519 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2520 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2521 } else {
2522 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2523 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2524 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2525 }
2526
aliguori6f97dba2008-10-31 18:49:55 +00002527 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002528 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002529 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002530 tcp_chr_accept(chr);
2531 socket_set_nonblock(s->listen_fd);
2532 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002533 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002534
aliguori6f97dba2008-10-31 18:49:55 +00002535 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002536 if (local_err) {
2537 qerror_report_err(local_err);
2538 error_free(local_err);
2539 }
2540 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002541 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002542 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002543 g_free(s);
2544 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002545 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002546}
2547
Luiz Capitulino999bd672010-10-22 16:09:05 -02002548/***********************************************************/
2549/* Memory chardev */
2550typedef struct {
2551 size_t outbuf_size;
2552 size_t outbuf_capacity;
2553 uint8_t *outbuf;
2554} MemoryDriver;
2555
2556static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2557{
2558 MemoryDriver *d = chr->opaque;
2559
2560 /* TODO: the QString implementation has the same code, we should
2561 * introduce a generic way to do this in cutils.c */
2562 if (d->outbuf_capacity < d->outbuf_size + len) {
2563 /* grow outbuf */
2564 d->outbuf_capacity += len;
2565 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002566 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002567 }
2568
2569 memcpy(d->outbuf + d->outbuf_size, buf, len);
2570 d->outbuf_size += len;
2571
2572 return len;
2573}
2574
2575void qemu_chr_init_mem(CharDriverState *chr)
2576{
2577 MemoryDriver *d;
2578
Anthony Liguori7267c092011-08-20 22:09:37 -05002579 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002580 d->outbuf_size = 0;
2581 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002582 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002583
2584 memset(chr, 0, sizeof(*chr));
2585 chr->opaque = d;
2586 chr->chr_write = mem_chr_write;
2587}
2588
2589QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2590{
2591 MemoryDriver *d = chr->opaque;
2592 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2593}
2594
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002595/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002596void qemu_chr_close_mem(CharDriverState *chr)
2597{
2598 MemoryDriver *d = chr->opaque;
2599
Anthony Liguori7267c092011-08-20 22:09:37 -05002600 g_free(d->outbuf);
2601 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002602 chr->opaque = NULL;
2603 chr->chr_write = NULL;
2604}
2605
2606size_t qemu_chr_mem_osize(const CharDriverState *chr)
2607{
2608 const MemoryDriver *d = chr->opaque;
2609 return d->outbuf_size;
2610}
2611
Gerd Hoffmann33521632009-12-08 13:11:49 +01002612QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002613{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002614 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002615 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002616 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002617 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002618 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002619
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002620 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2621 if (error_is_set(&local_err)) {
2622 qerror_report_err(local_err);
2623 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002624 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002625 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002626
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002627 if (strstart(filename, "mon:", &p)) {
2628 filename = p;
2629 qemu_opt_set(opts, "mux", "on");
2630 }
2631
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002632 if (strcmp(filename, "null") == 0 ||
2633 strcmp(filename, "pty") == 0 ||
2634 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002635 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002636 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002637 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002638 return opts;
2639 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002640 if (strstart(filename, "vc", &p)) {
2641 qemu_opt_set(opts, "backend", "vc");
2642 if (*p == ':') {
2643 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2644 /* pixels */
2645 qemu_opt_set(opts, "width", width);
2646 qemu_opt_set(opts, "height", height);
2647 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2648 /* chars */
2649 qemu_opt_set(opts, "cols", width);
2650 qemu_opt_set(opts, "rows", height);
2651 } else {
2652 goto fail;
2653 }
2654 }
2655 return opts;
2656 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002657 if (strcmp(filename, "con:") == 0) {
2658 qemu_opt_set(opts, "backend", "console");
2659 return opts;
2660 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002661 if (strstart(filename, "COM", NULL)) {
2662 qemu_opt_set(opts, "backend", "serial");
2663 qemu_opt_set(opts, "path", filename);
2664 return opts;
2665 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002666 if (strstart(filename, "file:", &p)) {
2667 qemu_opt_set(opts, "backend", "file");
2668 qemu_opt_set(opts, "path", p);
2669 return opts;
2670 }
2671 if (strstart(filename, "pipe:", &p)) {
2672 qemu_opt_set(opts, "backend", "pipe");
2673 qemu_opt_set(opts, "path", p);
2674 return opts;
2675 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002676 if (strstart(filename, "tcp:", &p) ||
2677 strstart(filename, "telnet:", &p)) {
2678 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2679 host[0] = 0;
2680 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2681 goto fail;
2682 }
2683 qemu_opt_set(opts, "backend", "socket");
2684 qemu_opt_set(opts, "host", host);
2685 qemu_opt_set(opts, "port", port);
2686 if (p[pos] == ',') {
2687 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2688 goto fail;
2689 }
2690 if (strstart(filename, "telnet:", &p))
2691 qemu_opt_set(opts, "telnet", "on");
2692 return opts;
2693 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002694 if (strstart(filename, "udp:", &p)) {
2695 qemu_opt_set(opts, "backend", "udp");
2696 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2697 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002698 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002699 goto fail;
2700 }
2701 }
2702 qemu_opt_set(opts, "host", host);
2703 qemu_opt_set(opts, "port", port);
2704 if (p[pos] == '@') {
2705 p += pos + 1;
2706 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2707 host[0] = 0;
2708 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002709 goto fail;
2710 }
2711 }
2712 qemu_opt_set(opts, "localaddr", host);
2713 qemu_opt_set(opts, "localport", port);
2714 }
2715 return opts;
2716 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002717 if (strstart(filename, "unix:", &p)) {
2718 qemu_opt_set(opts, "backend", "socket");
2719 if (qemu_opts_do_parse(opts, p, "path") != 0)
2720 goto fail;
2721 return opts;
2722 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002723 if (strstart(filename, "/dev/parport", NULL) ||
2724 strstart(filename, "/dev/ppi", NULL)) {
2725 qemu_opt_set(opts, "backend", "parport");
2726 qemu_opt_set(opts, "path", filename);
2727 return opts;
2728 }
2729 if (strstart(filename, "/dev/", NULL)) {
2730 qemu_opt_set(opts, "backend", "tty");
2731 qemu_opt_set(opts, "path", filename);
2732 return opts;
2733 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002734
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002735fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002736 qemu_opts_del(opts);
2737 return NULL;
2738}
2739
2740static const struct {
2741 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01002742 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002743} backend_table[] = {
2744 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002745 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002746 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002747 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002748 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002749#ifdef _WIN32
2750 { .name = "file", .open = qemu_chr_open_win_file_out },
2751 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002752 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002753 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002754 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002755#else
2756 { .name = "file", .open = qemu_chr_open_file_out },
2757 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002758 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002759 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002760#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002761#ifdef CONFIG_BRLAPI
2762 { .name = "braille", .open = chr_baum_init },
2763#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002764#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002765 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2766 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002767 { .name = "tty", .open = qemu_chr_open_tty },
2768#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002769#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2770 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002771 { .name = "parport", .open = qemu_chr_open_pp },
2772#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002773#ifdef CONFIG_SPICE
2774 { .name = "spicevmc", .open = qemu_chr_open_spice },
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01002775#if SPICE_SERVER_VERSION >= 0x000c02
2776 { .name = "spiceport", .open = qemu_chr_open_spice_port },
2777#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002778#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002779};
2780
Anthony Liguorif69554b2011-08-15 11:17:37 -05002781CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002782 void (*init)(struct CharDriverState *s),
2783 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002784{
2785 CharDriverState *chr;
2786 int i;
2787
2788 if (qemu_opts_id(opts) == NULL) {
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002789 error_setg(errp, "chardev: no id specified\n");
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002790 return NULL;
2791 }
2792
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002793 if (qemu_opt_get(opts, "backend") == NULL) {
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002794 error_setg(errp, "chardev: \"%s\" missing backend\n",
2795 qemu_opts_id(opts));
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002796 return NULL;
2797 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002798 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2799 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2800 break;
2801 }
2802 if (i == ARRAY_SIZE(backend_table)) {
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002803 error_setg(errp, "chardev: backend \"%s\" not found\n",
2804 qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002805 return NULL;
2806 }
2807
Markus Armbruster1f514702012-02-07 15:09:08 +01002808 chr = backend_table[i].open(opts);
2809 if (!chr) {
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002810 error_setg(errp, "chardev: opening backend \"%s\" failed\n",
2811 qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002812 return NULL;
2813 }
2814
2815 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05002816 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002817 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002818 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002819
2820 if (qemu_opt_get_bool(opts, "mux", 0)) {
2821 CharDriverState *base = chr;
2822 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05002823 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002824 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2825 chr = qemu_chr_open_mux(base);
2826 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002827 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002828 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002829 } else {
2830 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002831 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002832 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002833 return chr;
2834}
2835
Anthony Liguori27143a42011-08-15 11:17:36 -05002836CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002837{
2838 const char *p;
2839 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002840 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002841 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002842
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002843 if (strstart(filename, "chardev:", &p)) {
2844 return qemu_chr_find(p);
2845 }
2846
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002847 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002848 if (!opts)
2849 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002850
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02002851 chr = qemu_chr_new_from_opts(opts, init, &err);
2852 if (error_is_set(&err)) {
2853 fprintf(stderr, "%s\n", error_get_pretty(err));
2854 error_free(err);
2855 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002856 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2857 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002858 }
Paolo Bonzini363f8cb2010-12-23 13:42:54 +01002859 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002860 return chr;
2861}
2862
Anthony Liguori15f31512011-08-15 11:17:35 -05002863void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01002864{
2865 if (chr->chr_set_echo) {
2866 chr->chr_set_echo(chr, echo);
2867 }
2868}
2869
Anthony Liguoric9d830e2011-08-15 11:17:32 -05002870void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002871{
2872 if (chr->chr_guest_open) {
2873 chr->chr_guest_open(chr);
2874 }
2875}
2876
Anthony Liguori28178222011-08-15 11:17:33 -05002877void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002878{
2879 if (chr->chr_guest_close) {
2880 chr->chr_guest_close(chr);
2881 }
2882}
2883
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002884void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00002885{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002886 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002887 if (chr->chr_close)
2888 chr->chr_close(chr);
Anthony Liguori7267c092011-08-20 22:09:37 -05002889 g_free(chr->filename);
2890 g_free(chr->label);
2891 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002892}
2893
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002894ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002895{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002896 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002897 CharDriverState *chr;
2898
Blue Swirl72cf2d42009-09-12 07:36:22 +00002899 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002900 ChardevInfoList *info = g_malloc0(sizeof(*info));
2901 info->value = g_malloc0(sizeof(*info->value));
2902 info->value->label = g_strdup(chr->label);
2903 info->value->filename = g_strdup(chr->filename);
2904
2905 info->next = chr_list;
2906 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00002907 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002908
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002909 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002910}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002911
2912CharDriverState *qemu_chr_find(const char *name)
2913{
2914 CharDriverState *chr;
2915
Blue Swirl72cf2d42009-09-12 07:36:22 +00002916 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002917 if (strcmp(chr->label, name) != 0)
2918 continue;
2919 return chr;
2920 }
2921 return NULL;
2922}
Anthony Liguori0beb4942011-12-22 15:29:25 -06002923
2924/* Get a character (serial) device interface. */
2925CharDriverState *qemu_char_get_next_serial(void)
2926{
2927 static int next_serial;
2928
2929 /* FIXME: This function needs to go away: use chardev properties! */
2930 return serial_hds[next_serial++];
2931}
2932
Paolo Bonzini4d454572012-11-26 16:03:42 +01002933QemuOptsList qemu_chardev_opts = {
2934 .name = "chardev",
2935 .implied_opt_name = "backend",
2936 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
2937 .desc = {
2938 {
2939 .name = "backend",
2940 .type = QEMU_OPT_STRING,
2941 },{
2942 .name = "path",
2943 .type = QEMU_OPT_STRING,
2944 },{
2945 .name = "host",
2946 .type = QEMU_OPT_STRING,
2947 },{
2948 .name = "port",
2949 .type = QEMU_OPT_STRING,
2950 },{
2951 .name = "localaddr",
2952 .type = QEMU_OPT_STRING,
2953 },{
2954 .name = "localport",
2955 .type = QEMU_OPT_STRING,
2956 },{
2957 .name = "to",
2958 .type = QEMU_OPT_NUMBER,
2959 },{
2960 .name = "ipv4",
2961 .type = QEMU_OPT_BOOL,
2962 },{
2963 .name = "ipv6",
2964 .type = QEMU_OPT_BOOL,
2965 },{
2966 .name = "wait",
2967 .type = QEMU_OPT_BOOL,
2968 },{
2969 .name = "server",
2970 .type = QEMU_OPT_BOOL,
2971 },{
2972 .name = "delay",
2973 .type = QEMU_OPT_BOOL,
2974 },{
2975 .name = "telnet",
2976 .type = QEMU_OPT_BOOL,
2977 },{
2978 .name = "width",
2979 .type = QEMU_OPT_NUMBER,
2980 },{
2981 .name = "height",
2982 .type = QEMU_OPT_NUMBER,
2983 },{
2984 .name = "cols",
2985 .type = QEMU_OPT_NUMBER,
2986 },{
2987 .name = "rows",
2988 .type = QEMU_OPT_NUMBER,
2989 },{
2990 .name = "mux",
2991 .type = QEMU_OPT_BOOL,
2992 },{
2993 .name = "signal",
2994 .type = QEMU_OPT_BOOL,
2995 },{
2996 .name = "name",
2997 .type = QEMU_OPT_STRING,
2998 },{
2999 .name = "debug",
3000 .type = QEMU_OPT_NUMBER,
3001 },
3002 { /* end of list */ }
3003 },
3004};