blob: f1d089ffdd25291a858e348b327c6bd80cf976af [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzini927d4872012-12-17 18:20:05 +010029#include "char/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
31#include "hw/baum.h"
aurel32aa71cf82009-02-08 15:53:20 +000032#include "hw/msmouse.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030033#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000034
35#include <unistd.h>
36#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000037#include <time.h>
38#include <errno.h>
39#include <sys/time.h>
40#include <zlib.h>
41
42#ifndef _WIN32
43#include <sys/times.h>
44#include <sys/wait.h>
45#include <termios.h>
46#include <sys/mman.h>
47#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000049#include <sys/socket.h>
50#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000051#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000053#include <dirent.h>
54#include <netdb.h>
55#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020056#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000057#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010058#if defined(__GLIBC__)
59#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040060#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000061#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000062#else
63#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000064#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040065#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66#include <dev/ppbus/ppi.h>
67#include <dev/ppbus/ppbconf.h>
68#elif defined(__DragonFly__)
69#include <dev/misc/ppi/ppi.h>
70#include <bus/ppbus/ppbconf.h>
71#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010072#else
aliguori6f97dba2008-10-31 18:49:55 +000073#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000074#include <pty.h>
75
76#include <linux/ppdev.h>
77#include <linux/parport.h>
78#endif
79#ifdef __sun__
80#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
83#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
93#endif
94#endif
95#endif
96
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010097#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020098#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000099
Amit Shah9bd78542009-11-03 19:59:54 +0530100#define READ_BUF_LEN 4096
101
aliguori6f97dba2008-10-31 18:49:55 +0000102/***********************************************************/
103/* character device */
104
Blue Swirl72cf2d42009-09-12 07:36:22 +0000105static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000107
Hans de Goedea425d232011-11-19 10:22:43 +0100108void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000109{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200110 /* Keep track if the char device is open */
111 switch (event) {
112 case CHR_EVENT_OPENED:
113 s->opened = 1;
114 break;
115 case CHR_EVENT_CLOSED:
116 s->opened = 0;
117 break;
118 }
119
aliguori6f97dba2008-10-31 18:49:55 +0000120 if (!s->chr_event)
121 return;
122 s->chr_event(s->handler_opaque, event);
123}
124
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200125static void qemu_chr_fire_open_event(void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000126{
127 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100128 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200129 qemu_free_timer(s->open_timer);
130 s->open_timer = NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000131}
132
Amit Shah127338e2009-11-03 19:59:56 +0530133void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000134{
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200135 if (s->open_timer == NULL) {
Jan Kiszka06dec082012-11-22 18:30:02 +0100136 s->open_timer = qemu_new_timer_ms(rt_clock,
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200137 qemu_chr_fire_open_event, s);
Jan Kiszka06dec082012-11-22 18:30:02 +0100138 qemu_mod_timer(s->open_timer, qemu_get_clock_ms(rt_clock) - 1);
aliguori6f97dba2008-10-31 18:49:55 +0000139 }
140}
141
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500142int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000143{
144 return s->chr_write(s, buf, len);
145}
146
Anthony Liguori41084f12011-08-15 11:17:34 -0500147int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000148{
149 if (!s->chr_ioctl)
150 return -ENOTSUP;
151 return s->chr_ioctl(s, cmd, arg);
152}
153
Anthony Liguori909cda12011-08-15 11:17:31 -0500154int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000155{
156 if (!s->chr_can_read)
157 return 0;
158 return s->chr_can_read(s->handler_opaque);
159}
160
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500161void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000162{
Stefan Weilac310732012-04-19 22:27:14 +0200163 if (s->chr_read) {
164 s->chr_read(s->handler_opaque, buf, len);
165 }
aliguori6f97dba2008-10-31 18:49:55 +0000166}
167
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500168int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100169{
170 return s->get_msgfd ? s->get_msgfd(s) : -1;
171}
172
Daniel P. Berrange13661082011-06-23 13:31:42 +0100173int qemu_chr_add_client(CharDriverState *s, int fd)
174{
175 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
176}
177
aliguori6f97dba2008-10-31 18:49:55 +0000178void qemu_chr_accept_input(CharDriverState *s)
179{
180 if (s->chr_accept_input)
181 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100182 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000183}
184
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500185void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000186{
Amit Shah9bd78542009-11-03 19:59:54 +0530187 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000188 va_list ap;
189 va_start(ap, fmt);
190 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500191 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000192 va_end(ap);
193}
194
aliguori6f97dba2008-10-31 18:49:55 +0000195void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100196 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000197 IOReadHandler *fd_read,
198 IOEventHandler *fd_event,
199 void *opaque)
200{
Amit Shahda7d9982011-04-25 15:18:22 +0530201 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530202 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900203 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530204 }
aliguori6f97dba2008-10-31 18:49:55 +0000205 s->chr_can_read = fd_can_read;
206 s->chr_read = fd_read;
207 s->chr_event = fd_event;
208 s->handler_opaque = opaque;
209 if (s->chr_update_read_handler)
210 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200211
212 /* We're connecting to an already opened device, so let's make sure we
213 also get the open event */
214 if (s->opened) {
215 qemu_chr_generic_open(s);
216 }
aliguori6f97dba2008-10-31 18:49:55 +0000217}
218
219static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
220{
221 return len;
222}
223
Markus Armbruster1f514702012-02-07 15:09:08 +0100224static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000225{
226 CharDriverState *chr;
227
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000229 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100230 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000231}
232
233/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000234#define MAX_MUX 4
235#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
236#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
237typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100238 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000239 IOReadHandler *chr_read[MAX_MUX];
240 IOEventHandler *chr_event[MAX_MUX];
241 void *ext_opaque[MAX_MUX];
242 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200243 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000244 int mux_cnt;
245 int term_got_escape;
246 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000247 /* Intermediate input buffer allows to catch escape sequences even if the
248 currently active device is not accepting any input - but only until it
249 is full as well. */
250 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
251 int prod[MAX_MUX];
252 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200253 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200254 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200255 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000256} MuxDriver;
257
258
259static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
260{
261 MuxDriver *d = chr->opaque;
262 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200263 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000264 ret = d->drv->chr_write(d->drv, buf, len);
265 } else {
266 int i;
267
268 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200269 for (i = 0; i < len; i++) {
270 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000271 char buf1[64];
272 int64_t ti;
273 int secs;
274
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100275 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200276 if (d->timestamps_start == -1)
277 d->timestamps_start = ti;
278 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000279 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000280 snprintf(buf1, sizeof(buf1),
281 "[%02d:%02d:%02d.%03d] ",
282 secs / 3600,
283 (secs / 60) % 60,
284 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000285 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000286 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200287 d->linestart = 0;
288 }
289 ret += d->drv->chr_write(d->drv, buf+i, 1);
290 if (buf[i] == '\n') {
291 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000292 }
293 }
294 }
295 return ret;
296}
297
298static const char * const mux_help[] = {
299 "% h print this help\n\r",
300 "% x exit emulator\n\r",
301 "% s save disk data back to file (if -snapshot)\n\r",
302 "% t toggle console timestamps\n\r"
303 "% b send break (magic sysrq)\n\r",
304 "% c switch between console and monitor\n\r",
305 "% % sends %\n\r",
306 NULL
307};
308
309int term_escape_char = 0x01; /* ctrl-a is used for escape */
310static void mux_print_help(CharDriverState *chr)
311{
312 int i, j;
313 char ebuf[15] = "Escape-Char";
314 char cbuf[50] = "\n\r";
315
316 if (term_escape_char > 0 && term_escape_char < 26) {
317 snprintf(cbuf, sizeof(cbuf), "\n\r");
318 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
319 } else {
320 snprintf(cbuf, sizeof(cbuf),
321 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
322 term_escape_char);
323 }
324 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
325 for (i = 0; mux_help[i] != NULL; i++) {
326 for (j=0; mux_help[i][j] != '\0'; j++) {
327 if (mux_help[i][j] == '%')
328 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
329 else
330 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
331 }
332 }
333}
334
aliguori2724b182009-03-05 23:01:47 +0000335static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
336{
337 if (d->chr_event[mux_nr])
338 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
339}
340
aliguori6f97dba2008-10-31 18:49:55 +0000341static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
342{
343 if (d->term_got_escape) {
344 d->term_got_escape = 0;
345 if (ch == term_escape_char)
346 goto send_char;
347 switch(ch) {
348 case '?':
349 case 'h':
350 mux_print_help(chr);
351 break;
352 case 'x':
353 {
354 const char *term = "QEMU: Terminated\n\r";
355 chr->chr_write(chr,(uint8_t *)term,strlen(term));
356 exit(0);
357 break;
358 }
359 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200360 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000361 break;
362 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100363 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000364 break;
365 case 'c':
366 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
368 d->focus++;
369 if (d->focus >= d->mux_cnt)
370 d->focus = 0;
371 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000372 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200373 case 't':
374 d->timestamps = !d->timestamps;
375 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200376 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200377 break;
aliguori6f97dba2008-10-31 18:49:55 +0000378 }
379 } else if (ch == term_escape_char) {
380 d->term_got_escape = 1;
381 } else {
382 send_char:
383 return 1;
384 }
385 return 0;
386}
387
388static void mux_chr_accept_input(CharDriverState *chr)
389{
aliguori6f97dba2008-10-31 18:49:55 +0000390 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200391 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000392
aliguoria80bf992009-03-05 23:00:02 +0000393 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000394 d->chr_can_read[m] &&
395 d->chr_can_read[m](d->ext_opaque[m])) {
396 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000397 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000398 }
399}
400
401static int mux_chr_can_read(void *opaque)
402{
403 CharDriverState *chr = opaque;
404 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200405 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000406
aliguoria80bf992009-03-05 23:00:02 +0000407 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000408 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000409 if (d->chr_can_read[m])
410 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000411 return 0;
412}
413
414static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
415{
416 CharDriverState *chr = opaque;
417 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200418 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000419 int i;
420
421 mux_chr_accept_input (opaque);
422
423 for(i = 0; i < size; i++)
424 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000425 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000426 d->chr_can_read[m] &&
427 d->chr_can_read[m](d->ext_opaque[m]))
428 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
429 else
aliguoria80bf992009-03-05 23:00:02 +0000430 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000431 }
432}
433
434static void mux_chr_event(void *opaque, int event)
435{
436 CharDriverState *chr = opaque;
437 MuxDriver *d = chr->opaque;
438 int i;
439
440 /* Send the event to all registered listeners */
441 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000442 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000443}
444
445static void mux_chr_update_read_handler(CharDriverState *chr)
446{
447 MuxDriver *d = chr->opaque;
448
449 if (d->mux_cnt >= MAX_MUX) {
450 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
451 return;
452 }
453 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
454 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
455 d->chr_read[d->mux_cnt] = chr->chr_read;
456 d->chr_event[d->mux_cnt] = chr->chr_event;
457 /* Fix up the real driver with mux routines */
458 if (d->mux_cnt == 0) {
459 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
460 mux_chr_event, chr);
461 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200462 if (d->focus != -1) {
463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200464 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200465 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000466 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200467 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000468}
469
470static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
471{
472 CharDriverState *chr;
473 MuxDriver *d;
474
Anthony Liguori7267c092011-08-20 22:09:37 -0500475 chr = g_malloc0(sizeof(CharDriverState));
476 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000477
478 chr->opaque = d;
479 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200480 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000481 chr->chr_write = mux_chr_write;
482 chr->chr_update_read_handler = mux_chr_update_read_handler;
483 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100484 /* Frontend guest-open / -close notification is not support with muxes */
485 chr->chr_guest_open = NULL;
486 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200487
488 /* Muxes are always open on creation */
489 qemu_chr_generic_open(chr);
490
aliguori6f97dba2008-10-31 18:49:55 +0000491 return chr;
492}
493
494
495#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000496int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000497{
498 int ret, len;
499
500 len = len1;
501 while (len > 0) {
502 ret = send(fd, buf, len, 0);
503 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000504 errno = WSAGetLastError();
505 if (errno != WSAEWOULDBLOCK) {
506 return -1;
507 }
508 } else if (ret == 0) {
509 break;
510 } else {
511 buf += ret;
512 len -= ret;
513 }
514 }
515 return len1 - len;
516}
517
518#else
519
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100520int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000521{
522 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100523 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000524
525 len = len1;
526 while (len > 0) {
527 ret = write(fd, buf, len);
528 if (ret < 0) {
529 if (errno != EINTR && errno != EAGAIN)
530 return -1;
531 } else if (ret == 0) {
532 break;
533 } else {
534 buf += ret;
535 len -= ret;
536 }
537 }
538 return len1 - len;
539}
aliguori6f97dba2008-10-31 18:49:55 +0000540#endif /* !_WIN32 */
541
542#ifndef _WIN32
543
Anthony Liguori96c63842013-03-05 23:21:18 +0530544typedef struct IOWatchPoll
545{
546 GSource *src;
547 int max_size;
548
549 IOCanReadHandler *fd_can_read;
550 void *opaque;
551
552 QTAILQ_ENTRY(IOWatchPoll) node;
553} IOWatchPoll;
554
555static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
556 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);
557
558static IOWatchPoll *io_watch_poll_from_source(GSource *source)
559{
560 IOWatchPoll *i;
561
562 QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
563 if (i->src == source) {
564 return i;
565 }
566 }
567
568 return NULL;
569}
570
571static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
572{
573 IOWatchPoll *iwp = io_watch_poll_from_source(source);
574
575 iwp->max_size = iwp->fd_can_read(iwp->opaque);
576 if (iwp->max_size == 0) {
577 return FALSE;
578 }
579
580 return g_io_watch_funcs.prepare(source, timeout_);
581}
582
583static gboolean io_watch_poll_check(GSource *source)
584{
585 IOWatchPoll *iwp = io_watch_poll_from_source(source);
586
587 if (iwp->max_size == 0) {
588 return FALSE;
589 }
590
591 return g_io_watch_funcs.check(source);
592}
593
594static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
595 gpointer user_data)
596{
597 return g_io_watch_funcs.dispatch(source, callback, user_data);
598}
599
600static void io_watch_poll_finalize(GSource *source)
601{
602 IOWatchPoll *iwp = io_watch_poll_from_source(source);
603 QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
604 g_io_watch_funcs.finalize(source);
605}
606
607static GSourceFuncs io_watch_poll_funcs = {
608 .prepare = io_watch_poll_prepare,
609 .check = io_watch_poll_check,
610 .dispatch = io_watch_poll_dispatch,
611 .finalize = io_watch_poll_finalize,
612};
613
614/* Can only be used for read */
615static guint io_add_watch_poll(GIOChannel *channel,
616 IOCanReadHandler *fd_can_read,
617 GIOFunc fd_read,
618 gpointer user_data)
619{
620 IOWatchPoll *iwp;
621 GSource *src;
622 guint tag;
623
624 src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
625 g_source_set_funcs(src, &io_watch_poll_funcs);
626 g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
627 tag = g_source_attach(src, NULL);
628 g_source_unref(src);
629
630 iwp = g_malloc0(sizeof(*iwp));
631 iwp->src = src;
632 iwp->max_size = 0;
633 iwp->fd_can_read = fd_can_read;
634 iwp->opaque = user_data;
635
636 QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);
637
638 return tag;
639}
640
641static GIOChannel *io_channel_from_fd(int fd)
642{
643 GIOChannel *chan;
644
645 if (fd == -1) {
646 return NULL;
647 }
648
649 chan = g_io_channel_unix_new(fd);
650
651 g_io_channel_set_encoding(chan, NULL, NULL);
652 g_io_channel_set_buffered(chan, FALSE);
653
654 return chan;
655}
656
Anthony Liguori76a96442013-03-05 23:21:21 +0530657static GIOChannel *io_channel_from_socket(int fd)
658{
659 GIOChannel *chan;
660
661 if (fd == -1) {
662 return NULL;
663 }
664
665#ifdef _WIN32
666 chan = g_io_channel_win32_new_socket(fd);
667#else
668 chan = g_io_channel_unix_new(fd);
669#endif
670
671 g_io_channel_set_encoding(chan, NULL, NULL);
672 g_io_channel_set_buffered(chan, FALSE);
673
674 return chan;
675}
676
Anthony Liguori96c63842013-03-05 23:21:18 +0530677static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
678{
679 GIOStatus status;
680 gsize bytes_written;
681 int len;
682 const uint8_t *buf = _buf;
683
684 len = len1;
685 while (len > 0) {
686 status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
687 &bytes_written, NULL);
688 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530689 if (status == G_IO_STATUS_AGAIN) {
690 errno = EAGAIN;
691 return -1;
692 } else {
693 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530694 return -1;
695 }
696 } else if (status == G_IO_STATUS_EOF) {
697 break;
698 } else {
699 buf += bytes_written;
700 len -= bytes_written;
701 }
702 }
703 return len1 - len;
704}
Anthony Liguori96c63842013-03-05 23:21:18 +0530705
Anthony Liguoria29753f2013-03-05 23:21:19 +0530706typedef struct FDCharDriver {
707 CharDriverState *chr;
708 GIOChannel *fd_in, *fd_out;
709 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000710 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530711 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000712} FDCharDriver;
713
aliguori6f97dba2008-10-31 18:49:55 +0000714static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
715{
716 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530717
718 return io_channel_send_all(s->fd_out, buf, len);
719}
720
721static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
722{
723 CharDriverState *chr = opaque;
724 FDCharDriver *s = chr->opaque;
725 int len;
726 uint8_t buf[READ_BUF_LEN];
727 GIOStatus status;
728 gsize bytes_read;
729
730 len = sizeof(buf);
731 if (len > s->max_size) {
732 len = s->max_size;
733 }
734 if (len == 0) {
735 return FALSE;
736 }
737
738 status = g_io_channel_read_chars(chan, (gchar *)buf,
739 len, &bytes_read, NULL);
740 if (status == G_IO_STATUS_EOF) {
741 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
742 return FALSE;
743 }
744 if (status == G_IO_STATUS_NORMAL) {
745 qemu_chr_be_write(chr, buf, bytes_read);
746 }
747
748 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000749}
750
751static int fd_chr_read_poll(void *opaque)
752{
753 CharDriverState *chr = opaque;
754 FDCharDriver *s = chr->opaque;
755
Anthony Liguori909cda12011-08-15 11:17:31 -0500756 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000757 return s->max_size;
758}
759
Anthony Liguori23673ca2013-03-05 23:21:23 +0530760static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
761{
762 FDCharDriver *s = chr->opaque;
763 return g_io_create_watch(s->fd_out, cond);
764}
765
aliguori6f97dba2008-10-31 18:49:55 +0000766static void fd_chr_update_read_handler(CharDriverState *chr)
767{
768 FDCharDriver *s = chr->opaque;
769
Anthony Liguoria29753f2013-03-05 23:21:19 +0530770 if (s->fd_in_tag) {
771 g_source_remove(s->fd_in_tag);
772 }
773
774 if (s->fd_in) {
775 s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000776 }
777}
778
779static void fd_chr_close(struct CharDriverState *chr)
780{
781 FDCharDriver *s = chr->opaque;
782
Anthony Liguoria29753f2013-03-05 23:21:19 +0530783 if (s->fd_in_tag) {
784 g_source_remove(s->fd_in_tag);
785 s->fd_in_tag = 0;
786 }
787
788 if (s->fd_in) {
789 g_io_channel_unref(s->fd_in);
790 }
791 if (s->fd_out) {
792 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000793 }
794
Anthony Liguori7267c092011-08-20 22:09:37 -0500795 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100796 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000797}
798
799/* open a character device to a unix fd */
800static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
801{
802 CharDriverState *chr;
803 FDCharDriver *s;
804
Anthony Liguori7267c092011-08-20 22:09:37 -0500805 chr = g_malloc0(sizeof(CharDriverState));
806 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530807 s->fd_in = io_channel_from_fd(fd_in);
808 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530809 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530810 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000811 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530812 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000813 chr->chr_write = fd_chr_write;
814 chr->chr_update_read_handler = fd_chr_update_read_handler;
815 chr->chr_close = fd_chr_close;
816
Amit Shah127338e2009-11-03 19:59:56 +0530817 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000818
819 return chr;
820}
821
Markus Armbruster1f514702012-02-07 15:09:08 +0100822static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000823{
824 int fd_out;
825
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100826 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200827 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100828 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100829 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100830 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100831 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000832}
833
Markus Armbruster1f514702012-02-07 15:09:08 +0100834static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000835{
836 int fd_in, fd_out;
837 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200838 const char *filename = qemu_opt_get(opts, "path");
839
840 if (filename == NULL) {
841 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100842 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200843 }
aliguori6f97dba2008-10-31 18:49:55 +0000844
845 snprintf(filename_in, 256, "%s.in", filename);
846 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100847 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
848 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000849 if (fd_in < 0 || fd_out < 0) {
850 if (fd_in >= 0)
851 close(fd_in);
852 if (fd_out >= 0)
853 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100854 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100855 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100856 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100857 }
aliguori6f97dba2008-10-31 18:49:55 +0000858 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100859 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000860}
861
aliguori6f97dba2008-10-31 18:49:55 +0000862/* init terminal so that we can grab keys */
863static struct termios oldtty;
864static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100865static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000866
867static void term_exit(void)
868{
869 tcsetattr (0, TCSANOW, &oldtty);
870 fcntl(0, F_SETFL, old_fd0_flags);
871}
872
Paolo Bonzinibb002512010-12-23 13:42:50 +0100873static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000874{
875 struct termios tty;
876
Paolo Bonzini03693642010-12-23 13:42:49 +0100877 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100878 if (!echo) {
879 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000880 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100881 tty.c_oflag |= OPOST;
882 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
883 tty.c_cflag &= ~(CSIZE|PARENB);
884 tty.c_cflag |= CS8;
885 tty.c_cc[VMIN] = 1;
886 tty.c_cc[VTIME] = 0;
887 }
aliguori6f97dba2008-10-31 18:49:55 +0000888 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100889 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000890 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000891
892 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000893}
894
895static void qemu_chr_close_stdio(struct CharDriverState *chr)
896{
897 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000898 fd_chr_close(chr);
899}
900
Markus Armbruster1f514702012-02-07 15:09:08 +0100901static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000902{
903 CharDriverState *chr;
904
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400905 if (is_daemonized()) {
906 error_report("cannot use stdio with -daemonize");
907 return NULL;
908 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530909 old_fd0_flags = fcntl(0, F_GETFL);
910 tcgetattr (0, &oldtty);
911 fcntl(0, F_SETFL, O_NONBLOCK);
912 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100913
aliguori6f97dba2008-10-31 18:49:55 +0000914 chr = qemu_chr_open_fd(0, 1);
915 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100916 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100917 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
918 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500919 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000920
Markus Armbruster1f514702012-02-07 15:09:08 +0100921 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000922}
923
924#ifdef __sun__
925/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000926static int openpty(int *amaster, int *aslave, char *name,
927 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000928{
929 const char *slave;
930 int mfd = -1, sfd = -1;
931
932 *amaster = *aslave = -1;
933
934 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
935 if (mfd < 0)
936 goto err;
937
938 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
939 goto err;
940
941 if ((slave = ptsname(mfd)) == NULL)
942 goto err;
943
944 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
945 goto err;
946
947 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
948 (termp != NULL && tcgetattr(sfd, termp) < 0))
949 goto err;
950
951 if (amaster)
952 *amaster = mfd;
953 if (aslave)
954 *aslave = sfd;
955 if (winp)
956 ioctl(sfd, TIOCSWINSZ, winp);
957
958 return 0;
959
960err:
961 if (sfd != -1)
962 close(sfd);
963 close(mfd);
964 return -1;
965}
966
blueswir13f4cb3d2009-04-13 16:31:01 +0000967static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000968{
969 termios_p->c_iflag &=
970 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
971 termios_p->c_oflag &= ~OPOST;
972 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
973 termios_p->c_cflag &= ~(CSIZE|PARENB);
974 termios_p->c_cflag |= CS8;
975
976 termios_p->c_cc[VMIN] = 0;
977 termios_p->c_cc[VTIME] = 0;
978}
979#endif
980
981#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100982 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
983 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000984
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100985#define HAVE_CHARDEV_TTY 1
986
aliguori6f97dba2008-10-31 18:49:55 +0000987typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +0530988 GIOChannel *fd;
989 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000990 int connected;
991 int polling;
992 int read_bytes;
993 QEMUTimer *timer;
994} PtyCharDriver;
995
996static void pty_chr_update_read_handler(CharDriverState *chr);
997static void pty_chr_state(CharDriverState *chr, int connected);
998
999static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1000{
1001 PtyCharDriver *s = chr->opaque;
1002
1003 if (!s->connected) {
1004 /* guest sends data, check for (re-)connect */
1005 pty_chr_update_read_handler(chr);
1006 return 0;
1007 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301008 return io_channel_send_all(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001009}
1010
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301011static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1012{
1013 PtyCharDriver *s = chr->opaque;
1014 return g_io_create_watch(s->fd, cond);
1015}
1016
aliguori6f97dba2008-10-31 18:49:55 +00001017static int pty_chr_read_poll(void *opaque)
1018{
1019 CharDriverState *chr = opaque;
1020 PtyCharDriver *s = chr->opaque;
1021
Anthony Liguori909cda12011-08-15 11:17:31 -05001022 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001023 return s->read_bytes;
1024}
1025
Anthony Liguori093d3a22013-03-05 23:21:20 +05301026static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001027{
1028 CharDriverState *chr = opaque;
1029 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301030 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301031 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301032 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001033
1034 len = sizeof(buf);
1035 if (len > s->read_bytes)
1036 len = s->read_bytes;
1037 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301038 return FALSE;
1039 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1040 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001041 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301042 return FALSE;
1043 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001044 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001045 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001046 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301047 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001048}
1049
1050static void pty_chr_update_read_handler(CharDriverState *chr)
1051{
1052 PtyCharDriver *s = chr->opaque;
1053
Anthony Liguori093d3a22013-03-05 23:21:20 +05301054 if (s->fd_tag) {
1055 g_source_remove(s->fd_tag);
1056 }
1057
1058 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001059 s->polling = 1;
1060 /*
1061 * Short timeout here: just need wait long enougth that qemu makes
1062 * it through the poll loop once. When reconnected we want a
1063 * short timeout so we notice it almost instantly. Otherwise
1064 * read() gives us -EIO instantly, making pty_chr_state() reset the
1065 * timeout to the normal (much longer) poll interval before the
1066 * timer triggers.
1067 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001068 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +00001069}
1070
1071static void pty_chr_state(CharDriverState *chr, int connected)
1072{
1073 PtyCharDriver *s = chr->opaque;
1074
1075 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301076 g_source_remove(s->fd_tag);
1077 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001078 s->connected = 0;
1079 s->polling = 0;
1080 /* (re-)connect poll interval for idle guests: once per second.
1081 * We check more frequently in case the guests sends data to
1082 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001083 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001084 } else {
1085 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301086 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001087 s->connected = 1;
1088 }
1089}
1090
1091static void pty_chr_timer(void *opaque)
1092{
1093 struct CharDriverState *chr = opaque;
1094 PtyCharDriver *s = chr->opaque;
1095
1096 if (s->connected)
1097 return;
1098 if (s->polling) {
1099 /* If we arrive here without polling being cleared due
1100 * read returning -EIO, then we are (re-)connected */
1101 pty_chr_state(chr, 1);
1102 return;
1103 }
1104
1105 /* Next poll ... */
1106 pty_chr_update_read_handler(chr);
1107}
1108
1109static void pty_chr_close(struct CharDriverState *chr)
1110{
1111 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301112 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001113
Anthony Liguori093d3a22013-03-05 23:21:20 +05301114 if (s->fd_tag) {
1115 g_source_remove(s->fd_tag);
1116 }
1117 fd = g_io_channel_unix_get_fd(s->fd);
1118 g_io_channel_unref(s->fd);
1119 close(fd);
aliguori819f56b2009-03-28 17:58:14 +00001120 qemu_del_timer(s->timer);
1121 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -05001122 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001123 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001124}
1125
Markus Armbruster1f514702012-02-07 15:09:08 +01001126static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001127{
1128 CharDriverState *chr;
1129 PtyCharDriver *s;
1130 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001131 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001132 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001133#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001134 char pty_name[PATH_MAX];
1135#define q_ptsname(x) pty_name
1136#else
1137 char *pty_name = NULL;
1138#define q_ptsname(x) ptsname(x)
1139#endif
1140
Markus Armbrustera4e26042011-11-11 10:40:05 +01001141 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001142 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001143 }
1144
1145 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001146 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001147 cfmakeraw(&tty);
1148 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1149 close(slave_fd);
1150
Markus Armbrustera4e26042011-11-11 10:40:05 +01001151 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001152
Markus Armbrustera4e26042011-11-11 10:40:05 +01001153 len = strlen(q_ptsname(master_fd)) + 5;
1154 chr->filename = g_malloc(len);
1155 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1156 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001157
1158 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001159 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1160 q_ptsname(master_fd),
1161 label ? " (label " : "",
1162 label ? label : "",
1163 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001164
1165 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001166 chr->opaque = s;
1167 chr->chr_write = pty_chr_write;
1168 chr->chr_update_read_handler = pty_chr_update_read_handler;
1169 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301170 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001171
Anthony Liguori093d3a22013-03-05 23:21:20 +05301172 s->fd = io_channel_from_fd(master_fd);
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001173 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001174
Markus Armbruster1f514702012-02-07 15:09:08 +01001175 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001176}
1177
1178static void tty_serial_init(int fd, int speed,
1179 int parity, int data_bits, int stop_bits)
1180{
1181 struct termios tty;
1182 speed_t spd;
1183
1184#if 0
1185 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1186 speed, parity, data_bits, stop_bits);
1187#endif
1188 tcgetattr (fd, &tty);
1189
Stefan Weil45eea132009-10-26 16:10:10 +01001190#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1191 speed = speed * 10 / 11;
1192 do {
1193 check_speed(50);
1194 check_speed(75);
1195 check_speed(110);
1196 check_speed(134);
1197 check_speed(150);
1198 check_speed(200);
1199 check_speed(300);
1200 check_speed(600);
1201 check_speed(1200);
1202 check_speed(1800);
1203 check_speed(2400);
1204 check_speed(4800);
1205 check_speed(9600);
1206 check_speed(19200);
1207 check_speed(38400);
1208 /* Non-Posix values follow. They may be unsupported on some systems. */
1209 check_speed(57600);
1210 check_speed(115200);
1211#ifdef B230400
1212 check_speed(230400);
1213#endif
1214#ifdef B460800
1215 check_speed(460800);
1216#endif
1217#ifdef B500000
1218 check_speed(500000);
1219#endif
1220#ifdef B576000
1221 check_speed(576000);
1222#endif
1223#ifdef B921600
1224 check_speed(921600);
1225#endif
1226#ifdef B1000000
1227 check_speed(1000000);
1228#endif
1229#ifdef B1152000
1230 check_speed(1152000);
1231#endif
1232#ifdef B1500000
1233 check_speed(1500000);
1234#endif
1235#ifdef B2000000
1236 check_speed(2000000);
1237#endif
1238#ifdef B2500000
1239 check_speed(2500000);
1240#endif
1241#ifdef B3000000
1242 check_speed(3000000);
1243#endif
1244#ifdef B3500000
1245 check_speed(3500000);
1246#endif
1247#ifdef B4000000
1248 check_speed(4000000);
1249#endif
aliguori6f97dba2008-10-31 18:49:55 +00001250 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001251 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001252
1253 cfsetispeed(&tty, spd);
1254 cfsetospeed(&tty, spd);
1255
1256 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1257 |INLCR|IGNCR|ICRNL|IXON);
1258 tty.c_oflag |= OPOST;
1259 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1260 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1261 switch(data_bits) {
1262 default:
1263 case 8:
1264 tty.c_cflag |= CS8;
1265 break;
1266 case 7:
1267 tty.c_cflag |= CS7;
1268 break;
1269 case 6:
1270 tty.c_cflag |= CS6;
1271 break;
1272 case 5:
1273 tty.c_cflag |= CS5;
1274 break;
1275 }
1276 switch(parity) {
1277 default:
1278 case 'N':
1279 break;
1280 case 'E':
1281 tty.c_cflag |= PARENB;
1282 break;
1283 case 'O':
1284 tty.c_cflag |= PARENB | PARODD;
1285 break;
1286 }
1287 if (stop_bits == 2)
1288 tty.c_cflag |= CSTOPB;
1289
1290 tcsetattr (fd, TCSANOW, &tty);
1291}
1292
1293static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1294{
1295 FDCharDriver *s = chr->opaque;
1296
1297 switch(cmd) {
1298 case CHR_IOCTL_SERIAL_SET_PARAMS:
1299 {
1300 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301301 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1302 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001303 ssp->data_bits, ssp->stop_bits);
1304 }
1305 break;
1306 case CHR_IOCTL_SERIAL_SET_BREAK:
1307 {
1308 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301309 if (enable) {
1310 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1311 }
aliguori6f97dba2008-10-31 18:49:55 +00001312 }
1313 break;
1314 case CHR_IOCTL_SERIAL_GET_TIOCM:
1315 {
1316 int sarg = 0;
1317 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301318 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001319 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001320 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001321 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001322 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001323 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001324 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001325 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001326 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001327 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001328 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001329 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001330 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001331 *targ |= CHR_TIOCM_RTS;
1332 }
1333 break;
1334 case CHR_IOCTL_SERIAL_SET_TIOCM:
1335 {
1336 int sarg = *(int *)arg;
1337 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301338 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001339 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1340 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1341 if (sarg & CHR_TIOCM_CTS)
1342 targ |= TIOCM_CTS;
1343 if (sarg & CHR_TIOCM_CAR)
1344 targ |= TIOCM_CAR;
1345 if (sarg & CHR_TIOCM_DSR)
1346 targ |= TIOCM_DSR;
1347 if (sarg & CHR_TIOCM_RI)
1348 targ |= TIOCM_RI;
1349 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001350 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001351 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001352 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301353 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001354 }
1355 break;
1356 default:
1357 return -ENOTSUP;
1358 }
1359 return 0;
1360}
1361
David Ahern4266a132010-02-10 18:27:17 -07001362static void qemu_chr_close_tty(CharDriverState *chr)
1363{
1364 FDCharDriver *s = chr->opaque;
1365 int fd = -1;
1366
1367 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301368 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001369 }
1370
1371 fd_chr_close(chr);
1372
1373 if (fd >= 0) {
1374 close(fd);
1375 }
1376}
1377
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001378static CharDriverState *qemu_chr_open_tty_fd(int fd)
1379{
1380 CharDriverState *chr;
1381
1382 tty_serial_init(fd, 115200, 'N', 8, 1);
1383 chr = qemu_chr_open_fd(fd, fd);
1384 chr->chr_ioctl = tty_serial_ioctl;
1385 chr->chr_close = qemu_chr_close_tty;
1386 return chr;
1387}
1388
Markus Armbruster1f514702012-02-07 15:09:08 +01001389static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001390{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001391 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001392 int fd;
1393
Markus Armbrusterb181e042012-02-07 15:09:09 +01001394 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001395 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001396 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001397 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001398 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001399}
aliguori6f97dba2008-10-31 18:49:55 +00001400#endif /* __linux__ || __sun__ */
1401
1402#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001403
1404#define HAVE_CHARDEV_PARPORT 1
1405
aliguori6f97dba2008-10-31 18:49:55 +00001406typedef struct {
1407 int fd;
1408 int mode;
1409} ParallelCharDriver;
1410
1411static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1412{
1413 if (s->mode != mode) {
1414 int m = mode;
1415 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1416 return 0;
1417 s->mode = mode;
1418 }
1419 return 1;
1420}
1421
1422static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1423{
1424 ParallelCharDriver *drv = chr->opaque;
1425 int fd = drv->fd;
1426 uint8_t b;
1427
1428 switch(cmd) {
1429 case CHR_IOCTL_PP_READ_DATA:
1430 if (ioctl(fd, PPRDATA, &b) < 0)
1431 return -ENOTSUP;
1432 *(uint8_t *)arg = b;
1433 break;
1434 case CHR_IOCTL_PP_WRITE_DATA:
1435 b = *(uint8_t *)arg;
1436 if (ioctl(fd, PPWDATA, &b) < 0)
1437 return -ENOTSUP;
1438 break;
1439 case CHR_IOCTL_PP_READ_CONTROL:
1440 if (ioctl(fd, PPRCONTROL, &b) < 0)
1441 return -ENOTSUP;
1442 /* Linux gives only the lowest bits, and no way to know data
1443 direction! For better compatibility set the fixed upper
1444 bits. */
1445 *(uint8_t *)arg = b | 0xc0;
1446 break;
1447 case CHR_IOCTL_PP_WRITE_CONTROL:
1448 b = *(uint8_t *)arg;
1449 if (ioctl(fd, PPWCONTROL, &b) < 0)
1450 return -ENOTSUP;
1451 break;
1452 case CHR_IOCTL_PP_READ_STATUS:
1453 if (ioctl(fd, PPRSTATUS, &b) < 0)
1454 return -ENOTSUP;
1455 *(uint8_t *)arg = b;
1456 break;
1457 case CHR_IOCTL_PP_DATA_DIR:
1458 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1459 return -ENOTSUP;
1460 break;
1461 case CHR_IOCTL_PP_EPP_READ_ADDR:
1462 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1463 struct ParallelIOArg *parg = arg;
1464 int n = read(fd, parg->buffer, parg->count);
1465 if (n != parg->count) {
1466 return -EIO;
1467 }
1468 }
1469 break;
1470 case CHR_IOCTL_PP_EPP_READ:
1471 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1472 struct ParallelIOArg *parg = arg;
1473 int n = read(fd, parg->buffer, parg->count);
1474 if (n != parg->count) {
1475 return -EIO;
1476 }
1477 }
1478 break;
1479 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1480 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1481 struct ParallelIOArg *parg = arg;
1482 int n = write(fd, parg->buffer, parg->count);
1483 if (n != parg->count) {
1484 return -EIO;
1485 }
1486 }
1487 break;
1488 case CHR_IOCTL_PP_EPP_WRITE:
1489 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1490 struct ParallelIOArg *parg = arg;
1491 int n = write(fd, parg->buffer, parg->count);
1492 if (n != parg->count) {
1493 return -EIO;
1494 }
1495 }
1496 break;
1497 default:
1498 return -ENOTSUP;
1499 }
1500 return 0;
1501}
1502
1503static void pp_close(CharDriverState *chr)
1504{
1505 ParallelCharDriver *drv = chr->opaque;
1506 int fd = drv->fd;
1507
1508 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1509 ioctl(fd, PPRELEASE);
1510 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001511 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001512 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001513}
1514
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001515static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001516{
1517 CharDriverState *chr;
1518 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001519
1520 if (ioctl(fd, PPCLAIM) < 0) {
1521 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001522 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001523 }
1524
Anthony Liguori7267c092011-08-20 22:09:37 -05001525 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001526 drv->fd = fd;
1527 drv->mode = IEEE1284_MODE_COMPAT;
1528
Anthony Liguori7267c092011-08-20 22:09:37 -05001529 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001530 chr->chr_write = null_chr_write;
1531 chr->chr_ioctl = pp_ioctl;
1532 chr->chr_close = pp_close;
1533 chr->opaque = drv;
1534
Amit Shah127338e2009-11-03 19:59:56 +05301535 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001536
Markus Armbruster1f514702012-02-07 15:09:08 +01001537 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001538}
1539#endif /* __linux__ */
1540
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001541#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001542
1543#define HAVE_CHARDEV_PARPORT 1
1544
blueswir16972f932008-11-22 20:49:12 +00001545static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1546{
Stefan Weile0efb992011-02-23 19:09:16 +01001547 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001548 uint8_t b;
1549
1550 switch(cmd) {
1551 case CHR_IOCTL_PP_READ_DATA:
1552 if (ioctl(fd, PPIGDATA, &b) < 0)
1553 return -ENOTSUP;
1554 *(uint8_t *)arg = b;
1555 break;
1556 case CHR_IOCTL_PP_WRITE_DATA:
1557 b = *(uint8_t *)arg;
1558 if (ioctl(fd, PPISDATA, &b) < 0)
1559 return -ENOTSUP;
1560 break;
1561 case CHR_IOCTL_PP_READ_CONTROL:
1562 if (ioctl(fd, PPIGCTRL, &b) < 0)
1563 return -ENOTSUP;
1564 *(uint8_t *)arg = b;
1565 break;
1566 case CHR_IOCTL_PP_WRITE_CONTROL:
1567 b = *(uint8_t *)arg;
1568 if (ioctl(fd, PPISCTRL, &b) < 0)
1569 return -ENOTSUP;
1570 break;
1571 case CHR_IOCTL_PP_READ_STATUS:
1572 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1573 return -ENOTSUP;
1574 *(uint8_t *)arg = b;
1575 break;
1576 default:
1577 return -ENOTSUP;
1578 }
1579 return 0;
1580}
1581
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001582static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001583{
1584 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001585
Anthony Liguori7267c092011-08-20 22:09:37 -05001586 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001587 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001588 chr->chr_write = null_chr_write;
1589 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001590 return chr;
blueswir16972f932008-11-22 20:49:12 +00001591}
1592#endif
1593
aliguori6f97dba2008-10-31 18:49:55 +00001594#else /* _WIN32 */
1595
1596typedef struct {
1597 int max_size;
1598 HANDLE hcom, hrecv, hsend;
1599 OVERLAPPED orecv, osend;
1600 BOOL fpipe;
1601 DWORD len;
1602} WinCharState;
1603
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001604typedef struct {
1605 HANDLE hStdIn;
1606 HANDLE hInputReadyEvent;
1607 HANDLE hInputDoneEvent;
1608 HANDLE hInputThread;
1609 uint8_t win_stdio_buf;
1610} WinStdioCharState;
1611
aliguori6f97dba2008-10-31 18:49:55 +00001612#define NSENDBUF 2048
1613#define NRECVBUF 2048
1614#define MAXCONNECT 1
1615#define NTIMEOUT 5000
1616
1617static int win_chr_poll(void *opaque);
1618static int win_chr_pipe_poll(void *opaque);
1619
1620static void win_chr_close(CharDriverState *chr)
1621{
1622 WinCharState *s = chr->opaque;
1623
1624 if (s->hsend) {
1625 CloseHandle(s->hsend);
1626 s->hsend = NULL;
1627 }
1628 if (s->hrecv) {
1629 CloseHandle(s->hrecv);
1630 s->hrecv = NULL;
1631 }
1632 if (s->hcom) {
1633 CloseHandle(s->hcom);
1634 s->hcom = NULL;
1635 }
1636 if (s->fpipe)
1637 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1638 else
1639 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301640
Hans de Goedea425d232011-11-19 10:22:43 +01001641 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001642}
1643
1644static int win_chr_init(CharDriverState *chr, const char *filename)
1645{
1646 WinCharState *s = chr->opaque;
1647 COMMCONFIG comcfg;
1648 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1649 COMSTAT comstat;
1650 DWORD size;
1651 DWORD err;
1652
1653 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1654 if (!s->hsend) {
1655 fprintf(stderr, "Failed CreateEvent\n");
1656 goto fail;
1657 }
1658 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1659 if (!s->hrecv) {
1660 fprintf(stderr, "Failed CreateEvent\n");
1661 goto fail;
1662 }
1663
1664 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1665 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1666 if (s->hcom == INVALID_HANDLE_VALUE) {
1667 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1668 s->hcom = NULL;
1669 goto fail;
1670 }
1671
1672 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1673 fprintf(stderr, "Failed SetupComm\n");
1674 goto fail;
1675 }
1676
1677 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1678 size = sizeof(COMMCONFIG);
1679 GetDefaultCommConfig(filename, &comcfg, &size);
1680 comcfg.dcb.DCBlength = sizeof(DCB);
1681 CommConfigDialog(filename, NULL, &comcfg);
1682
1683 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1684 fprintf(stderr, "Failed SetCommState\n");
1685 goto fail;
1686 }
1687
1688 if (!SetCommMask(s->hcom, EV_ERR)) {
1689 fprintf(stderr, "Failed SetCommMask\n");
1690 goto fail;
1691 }
1692
1693 cto.ReadIntervalTimeout = MAXDWORD;
1694 if (!SetCommTimeouts(s->hcom, &cto)) {
1695 fprintf(stderr, "Failed SetCommTimeouts\n");
1696 goto fail;
1697 }
1698
1699 if (!ClearCommError(s->hcom, &err, &comstat)) {
1700 fprintf(stderr, "Failed ClearCommError\n");
1701 goto fail;
1702 }
1703 qemu_add_polling_cb(win_chr_poll, chr);
1704 return 0;
1705
1706 fail:
1707 win_chr_close(chr);
1708 return -1;
1709}
1710
1711static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1712{
1713 WinCharState *s = chr->opaque;
1714 DWORD len, ret, size, err;
1715
1716 len = len1;
1717 ZeroMemory(&s->osend, sizeof(s->osend));
1718 s->osend.hEvent = s->hsend;
1719 while (len > 0) {
1720 if (s->hsend)
1721 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1722 else
1723 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1724 if (!ret) {
1725 err = GetLastError();
1726 if (err == ERROR_IO_PENDING) {
1727 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1728 if (ret) {
1729 buf += size;
1730 len -= size;
1731 } else {
1732 break;
1733 }
1734 } else {
1735 break;
1736 }
1737 } else {
1738 buf += size;
1739 len -= size;
1740 }
1741 }
1742 return len1 - len;
1743}
1744
1745static int win_chr_read_poll(CharDriverState *chr)
1746{
1747 WinCharState *s = chr->opaque;
1748
Anthony Liguori909cda12011-08-15 11:17:31 -05001749 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001750 return s->max_size;
1751}
1752
1753static void win_chr_readfile(CharDriverState *chr)
1754{
1755 WinCharState *s = chr->opaque;
1756 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301757 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001758 DWORD size;
1759
1760 ZeroMemory(&s->orecv, sizeof(s->orecv));
1761 s->orecv.hEvent = s->hrecv;
1762 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1763 if (!ret) {
1764 err = GetLastError();
1765 if (err == ERROR_IO_PENDING) {
1766 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1767 }
1768 }
1769
1770 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001771 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001772 }
1773}
1774
1775static void win_chr_read(CharDriverState *chr)
1776{
1777 WinCharState *s = chr->opaque;
1778
1779 if (s->len > s->max_size)
1780 s->len = s->max_size;
1781 if (s->len == 0)
1782 return;
1783
1784 win_chr_readfile(chr);
1785}
1786
1787static int win_chr_poll(void *opaque)
1788{
1789 CharDriverState *chr = opaque;
1790 WinCharState *s = chr->opaque;
1791 COMSTAT status;
1792 DWORD comerr;
1793
1794 ClearCommError(s->hcom, &comerr, &status);
1795 if (status.cbInQue > 0) {
1796 s->len = status.cbInQue;
1797 win_chr_read_poll(chr);
1798 win_chr_read(chr);
1799 return 1;
1800 }
1801 return 0;
1802}
1803
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001804static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001805{
1806 CharDriverState *chr;
1807 WinCharState *s;
1808
Anthony Liguori7267c092011-08-20 22:09:37 -05001809 chr = g_malloc0(sizeof(CharDriverState));
1810 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001811 chr->opaque = s;
1812 chr->chr_write = win_chr_write;
1813 chr->chr_close = win_chr_close;
1814
1815 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001816 g_free(s);
1817 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001818 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001819 }
Amit Shah127338e2009-11-03 19:59:56 +05301820 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001821 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001822}
1823
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001824static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1825{
1826 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1827}
1828
aliguori6f97dba2008-10-31 18:49:55 +00001829static int win_chr_pipe_poll(void *opaque)
1830{
1831 CharDriverState *chr = opaque;
1832 WinCharState *s = chr->opaque;
1833 DWORD size;
1834
1835 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1836 if (size > 0) {
1837 s->len = size;
1838 win_chr_read_poll(chr);
1839 win_chr_read(chr);
1840 return 1;
1841 }
1842 return 0;
1843}
1844
1845static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1846{
1847 WinCharState *s = chr->opaque;
1848 OVERLAPPED ov;
1849 int ret;
1850 DWORD size;
1851 char openname[256];
1852
1853 s->fpipe = TRUE;
1854
1855 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1856 if (!s->hsend) {
1857 fprintf(stderr, "Failed CreateEvent\n");
1858 goto fail;
1859 }
1860 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1861 if (!s->hrecv) {
1862 fprintf(stderr, "Failed CreateEvent\n");
1863 goto fail;
1864 }
1865
1866 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1867 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1868 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1869 PIPE_WAIT,
1870 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1871 if (s->hcom == INVALID_HANDLE_VALUE) {
1872 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1873 s->hcom = NULL;
1874 goto fail;
1875 }
1876
1877 ZeroMemory(&ov, sizeof(ov));
1878 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1879 ret = ConnectNamedPipe(s->hcom, &ov);
1880 if (ret) {
1881 fprintf(stderr, "Failed ConnectNamedPipe\n");
1882 goto fail;
1883 }
1884
1885 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1886 if (!ret) {
1887 fprintf(stderr, "Failed GetOverlappedResult\n");
1888 if (ov.hEvent) {
1889 CloseHandle(ov.hEvent);
1890 ov.hEvent = NULL;
1891 }
1892 goto fail;
1893 }
1894
1895 if (ov.hEvent) {
1896 CloseHandle(ov.hEvent);
1897 ov.hEvent = NULL;
1898 }
1899 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1900 return 0;
1901
1902 fail:
1903 win_chr_close(chr);
1904 return -1;
1905}
1906
1907
Markus Armbruster1f514702012-02-07 15:09:08 +01001908static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001909{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001910 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001911 CharDriverState *chr;
1912 WinCharState *s;
1913
Anthony Liguori7267c092011-08-20 22:09:37 -05001914 chr = g_malloc0(sizeof(CharDriverState));
1915 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001916 chr->opaque = s;
1917 chr->chr_write = win_chr_write;
1918 chr->chr_close = win_chr_close;
1919
1920 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001921 g_free(s);
1922 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001923 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001924 }
Amit Shah127338e2009-11-03 19:59:56 +05301925 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001926 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001927}
1928
Markus Armbruster1f514702012-02-07 15:09:08 +01001929static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001930{
1931 CharDriverState *chr;
1932 WinCharState *s;
1933
Anthony Liguori7267c092011-08-20 22:09:37 -05001934 chr = g_malloc0(sizeof(CharDriverState));
1935 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001936 s->hcom = fd_out;
1937 chr->opaque = s;
1938 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301939 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001940 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001941}
1942
Markus Armbruster1f514702012-02-07 15:09:08 +01001943static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001944{
Markus Armbruster1f514702012-02-07 15:09:08 +01001945 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001946}
1947
Markus Armbruster1f514702012-02-07 15:09:08 +01001948static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001949{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001950 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001951 HANDLE fd_out;
1952
1953 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1954 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001955 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001956 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001957 }
aliguori6f97dba2008-10-31 18:49:55 +00001958
Markus Armbruster1f514702012-02-07 15:09:08 +01001959 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001960}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001961
1962static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1963{
1964 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1965 DWORD dwSize;
1966 int len1;
1967
1968 len1 = len;
1969
1970 while (len1 > 0) {
1971 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1972 break;
1973 }
1974 buf += dwSize;
1975 len1 -= dwSize;
1976 }
1977
1978 return len - len1;
1979}
1980
1981static void win_stdio_wait_func(void *opaque)
1982{
1983 CharDriverState *chr = opaque;
1984 WinStdioCharState *stdio = chr->opaque;
1985 INPUT_RECORD buf[4];
1986 int ret;
1987 DWORD dwSize;
1988 int i;
1989
1990 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1991 &dwSize);
1992
1993 if (!ret) {
1994 /* Avoid error storm */
1995 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1996 return;
1997 }
1998
1999 for (i = 0; i < dwSize; i++) {
2000 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2001
2002 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2003 int j;
2004 if (kev->uChar.AsciiChar != 0) {
2005 for (j = 0; j < kev->wRepeatCount; j++) {
2006 if (qemu_chr_be_can_write(chr)) {
2007 uint8_t c = kev->uChar.AsciiChar;
2008 qemu_chr_be_write(chr, &c, 1);
2009 }
2010 }
2011 }
2012 }
2013 }
2014}
2015
2016static DWORD WINAPI win_stdio_thread(LPVOID param)
2017{
2018 CharDriverState *chr = param;
2019 WinStdioCharState *stdio = chr->opaque;
2020 int ret;
2021 DWORD dwSize;
2022
2023 while (1) {
2024
2025 /* Wait for one byte */
2026 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2027
2028 /* Exit in case of error, continue if nothing read */
2029 if (!ret) {
2030 break;
2031 }
2032 if (!dwSize) {
2033 continue;
2034 }
2035
2036 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2037 if (stdio->win_stdio_buf == '\r') {
2038 continue;
2039 }
2040
2041 /* Signal the main thread and wait until the byte was eaten */
2042 if (!SetEvent(stdio->hInputReadyEvent)) {
2043 break;
2044 }
2045 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2046 != WAIT_OBJECT_0) {
2047 break;
2048 }
2049 }
2050
2051 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2052 return 0;
2053}
2054
2055static void win_stdio_thread_wait_func(void *opaque)
2056{
2057 CharDriverState *chr = opaque;
2058 WinStdioCharState *stdio = chr->opaque;
2059
2060 if (qemu_chr_be_can_write(chr)) {
2061 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2062 }
2063
2064 SetEvent(stdio->hInputDoneEvent);
2065}
2066
2067static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2068{
2069 WinStdioCharState *stdio = chr->opaque;
2070 DWORD dwMode = 0;
2071
2072 GetConsoleMode(stdio->hStdIn, &dwMode);
2073
2074 if (echo) {
2075 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2076 } else {
2077 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2078 }
2079}
2080
2081static void win_stdio_close(CharDriverState *chr)
2082{
2083 WinStdioCharState *stdio = chr->opaque;
2084
2085 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2086 CloseHandle(stdio->hInputReadyEvent);
2087 }
2088 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2089 CloseHandle(stdio->hInputDoneEvent);
2090 }
2091 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2092 TerminateThread(stdio->hInputThread, 0);
2093 }
2094
2095 g_free(chr->opaque);
2096 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002097}
2098
Markus Armbruster1f514702012-02-07 15:09:08 +01002099static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002100{
2101 CharDriverState *chr;
2102 WinStdioCharState *stdio;
2103 DWORD dwMode;
2104 int is_console = 0;
2105
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002106 chr = g_malloc0(sizeof(CharDriverState));
2107 stdio = g_malloc0(sizeof(WinStdioCharState));
2108
2109 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2110 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2111 fprintf(stderr, "cannot open stdio: invalid handle\n");
2112 exit(1);
2113 }
2114
2115 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2116
2117 chr->opaque = stdio;
2118 chr->chr_write = win_stdio_write;
2119 chr->chr_close = win_stdio_close;
2120
Anthony Liguoried7a1542013-03-05 23:21:17 +05302121 if (is_console) {
2122 if (qemu_add_wait_object(stdio->hStdIn,
2123 win_stdio_wait_func, chr)) {
2124 fprintf(stderr, "qemu_add_wait_object: failed\n");
2125 }
2126 } else {
2127 DWORD dwId;
2128
2129 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2130 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2131 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2132 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002133
Anthony Liguoried7a1542013-03-05 23:21:17 +05302134 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2135 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2136 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2137 fprintf(stderr, "cannot create stdio thread or event\n");
2138 exit(1);
2139 }
2140 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2141 win_stdio_thread_wait_func, chr)) {
2142 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002143 }
2144 }
2145
2146 dwMode |= ENABLE_LINE_INPUT;
2147
Anthony Liguoried7a1542013-03-05 23:21:17 +05302148 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002149 /* set the terminal in raw mode */
2150 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2151 dwMode |= ENABLE_PROCESSED_INPUT;
2152 }
2153
2154 SetConsoleMode(stdio->hStdIn, dwMode);
2155
2156 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2157 qemu_chr_fe_set_echo(chr, false);
2158
Markus Armbruster1f514702012-02-07 15:09:08 +01002159 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002160}
aliguori6f97dba2008-10-31 18:49:55 +00002161#endif /* !_WIN32 */
2162
2163/***********************************************************/
2164/* UDP Net console */
2165
2166typedef struct {
2167 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302168 GIOChannel *chan;
2169 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302170 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002171 int bufcnt;
2172 int bufptr;
2173 int max_size;
2174} NetCharDriver;
2175
2176static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2177{
2178 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302179 gsize bytes_written;
2180 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002181
Anthony Liguori76a96442013-03-05 23:21:21 +05302182 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2183 if (status == G_IO_STATUS_EOF) {
2184 return 0;
2185 } else if (status != G_IO_STATUS_NORMAL) {
2186 return -1;
2187 }
2188
2189 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002190}
2191
2192static int udp_chr_read_poll(void *opaque)
2193{
2194 CharDriverState *chr = opaque;
2195 NetCharDriver *s = chr->opaque;
2196
Anthony Liguori909cda12011-08-15 11:17:31 -05002197 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002198
2199 /* If there were any stray characters in the queue process them
2200 * first
2201 */
2202 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002203 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002204 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002205 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002206 }
2207 return s->max_size;
2208}
2209
Anthony Liguori76a96442013-03-05 23:21:21 +05302210static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002211{
2212 CharDriverState *chr = opaque;
2213 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302214 gsize bytes_read = 0;
2215 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002216
2217 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302218 return FALSE;
2219 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2220 &bytes_read, NULL);
2221 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002222 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302223 if (status != G_IO_STATUS_NORMAL) {
2224 return FALSE;
2225 }
aliguori6f97dba2008-10-31 18:49:55 +00002226
2227 s->bufptr = 0;
2228 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002229 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002230 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002231 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002232 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302233
2234 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002235}
2236
2237static void udp_chr_update_read_handler(CharDriverState *chr)
2238{
2239 NetCharDriver *s = chr->opaque;
2240
Anthony Liguori76a96442013-03-05 23:21:21 +05302241 if (s->tag) {
2242 g_source_remove(s->tag);
2243 s->tag = 0;
2244 }
2245
2246 if (s->chan) {
2247 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002248 }
2249}
2250
aliguori819f56b2009-03-28 17:58:14 +00002251static void udp_chr_close(CharDriverState *chr)
2252{
2253 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302254 if (s->tag) {
2255 g_source_remove(s->tag);
2256 }
2257 if (s->chan) {
2258 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002259 closesocket(s->fd);
2260 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002261 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002262 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002263}
2264
Markus Armbruster1f514702012-02-07 15:09:08 +01002265static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002266{
2267 CharDriverState *chr = NULL;
2268 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002269 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002270 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002271
Anthony Liguori7267c092011-08-20 22:09:37 -05002272 chr = g_malloc0(sizeof(CharDriverState));
2273 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002274
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002275 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002276 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002277 goto return_err;
2278 }
2279
2280 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302281 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002282 s->bufcnt = 0;
2283 s->bufptr = 0;
2284 chr->opaque = s;
2285 chr->chr_write = udp_chr_write;
2286 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002287 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002288 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002289
2290return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002291 if (local_err) {
2292 qerror_report_err(local_err);
2293 error_free(local_err);
2294 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002295 g_free(chr);
2296 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002297 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002298 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002299 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002300 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002301}
2302
2303/***********************************************************/
2304/* TCP Net console */
2305
2306typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302307
2308 GIOChannel *chan, *listen_chan;
2309 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002310 int fd, listen_fd;
2311 int connected;
2312 int max_size;
2313 int do_telnetopt;
2314 int do_nodelay;
2315 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002316 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002317} TCPCharDriver;
2318
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302319static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002320
2321static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2322{
2323 TCPCharDriver *s = chr->opaque;
2324 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302325 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002326 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002327 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002328 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002329 }
2330}
2331
2332static int tcp_chr_read_poll(void *opaque)
2333{
2334 CharDriverState *chr = opaque;
2335 TCPCharDriver *s = chr->opaque;
2336 if (!s->connected)
2337 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002338 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002339 return s->max_size;
2340}
2341
2342#define IAC 255
2343#define IAC_BREAK 243
2344static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2345 TCPCharDriver *s,
2346 uint8_t *buf, int *size)
2347{
2348 /* Handle any telnet client's basic IAC options to satisfy char by
2349 * char mode with no echo. All IAC options will be removed from
2350 * the buf and the do_telnetopt variable will be used to track the
2351 * state of the width of the IAC information.
2352 *
2353 * IAC commands come in sets of 3 bytes with the exception of the
2354 * "IAC BREAK" command and the double IAC.
2355 */
2356
2357 int i;
2358 int j = 0;
2359
2360 for (i = 0; i < *size; i++) {
2361 if (s->do_telnetopt > 1) {
2362 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2363 /* Double IAC means send an IAC */
2364 if (j != i)
2365 buf[j] = buf[i];
2366 j++;
2367 s->do_telnetopt = 1;
2368 } else {
2369 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2370 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002371 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002372 s->do_telnetopt++;
2373 }
2374 s->do_telnetopt++;
2375 }
2376 if (s->do_telnetopt >= 4) {
2377 s->do_telnetopt = 1;
2378 }
2379 } else {
2380 if ((unsigned char)buf[i] == IAC) {
2381 s->do_telnetopt = 2;
2382 } else {
2383 if (j != i)
2384 buf[j] = buf[i];
2385 j++;
2386 }
2387 }
2388 }
2389 *size = j;
2390}
2391
Mark McLoughlin7d174052009-07-22 09:11:39 +01002392static int tcp_get_msgfd(CharDriverState *chr)
2393{
2394 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002395 int fd = s->msgfd;
2396 s->msgfd = -1;
2397 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002398}
2399
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002400#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002401static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2402{
2403 TCPCharDriver *s = chr->opaque;
2404 struct cmsghdr *cmsg;
2405
2406 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2407 int fd;
2408
2409 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2410 cmsg->cmsg_level != SOL_SOCKET ||
2411 cmsg->cmsg_type != SCM_RIGHTS)
2412 continue;
2413
2414 fd = *((int *)CMSG_DATA(cmsg));
2415 if (fd < 0)
2416 continue;
2417
Corey Bryant06138652012-08-14 16:43:42 -04002418#ifndef MSG_CMSG_CLOEXEC
2419 qemu_set_cloexec(fd);
2420#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002421 if (s->msgfd != -1)
2422 close(s->msgfd);
2423 s->msgfd = fd;
2424 }
2425}
2426
Mark McLoughlin9977c892009-07-22 09:11:38 +01002427static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2428{
2429 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002430 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002431 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002432 union {
2433 struct cmsghdr cmsg;
2434 char control[CMSG_SPACE(sizeof(int))];
2435 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002436 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002437 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002438
2439 iov[0].iov_base = buf;
2440 iov[0].iov_len = len;
2441
2442 msg.msg_iov = iov;
2443 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002444 msg.msg_control = &msg_control;
2445 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002446
Corey Bryant06138652012-08-14 16:43:42 -04002447#ifdef MSG_CMSG_CLOEXEC
2448 flags |= MSG_CMSG_CLOEXEC;
2449#endif
2450 ret = recvmsg(s->fd, &msg, flags);
2451 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002452 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002453 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002454
2455 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002456}
2457#else
2458static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2459{
2460 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002461 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002462}
2463#endif
2464
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302465static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002466{
2467 CharDriverState *chr = opaque;
2468 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302469 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002470 int len, size;
2471
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302472 if (!s->connected || s->max_size <= 0) {
2473 return FALSE;
2474 }
aliguori6f97dba2008-10-31 18:49:55 +00002475 len = sizeof(buf);
2476 if (len > s->max_size)
2477 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002478 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002479 if (size == 0) {
2480 /* connection closed */
2481 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302482 if (s->listen_chan) {
2483 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002484 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302485 g_source_remove(s->tag);
2486 s->tag = 0;
2487 g_io_channel_unref(s->chan);
2488 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002489 closesocket(s->fd);
2490 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002491 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002492 } else if (size > 0) {
2493 if (s->do_telnetopt)
2494 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2495 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002496 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002497 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302498
2499 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002500}
2501
Blue Swirl68c18d12010-08-15 09:46:24 +00002502#ifndef _WIN32
2503CharDriverState *qemu_chr_open_eventfd(int eventfd)
2504{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002505 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002506}
Blue Swirl68c18d12010-08-15 09:46:24 +00002507#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002508
aliguori6f97dba2008-10-31 18:49:55 +00002509static void tcp_chr_connect(void *opaque)
2510{
2511 CharDriverState *chr = opaque;
2512 TCPCharDriver *s = chr->opaque;
2513
2514 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302515 if (s->chan) {
2516 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002517 }
Amit Shah127338e2009-11-03 19:59:56 +05302518 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002519}
2520
2521#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2522static void tcp_chr_telnet_init(int fd)
2523{
2524 char buf[3];
2525 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2526 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2527 send(fd, (char *)buf, 3, 0);
2528 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2529 send(fd, (char *)buf, 3, 0);
2530 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2531 send(fd, (char *)buf, 3, 0);
2532 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2533 send(fd, (char *)buf, 3, 0);
2534}
2535
Daniel P. Berrange13661082011-06-23 13:31:42 +01002536static int tcp_chr_add_client(CharDriverState *chr, int fd)
2537{
2538 TCPCharDriver *s = chr->opaque;
2539 if (s->fd != -1)
2540 return -1;
2541
2542 socket_set_nonblock(fd);
2543 if (s->do_nodelay)
2544 socket_set_nodelay(fd);
2545 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302546 s->chan = io_channel_from_socket(fd);
2547 g_source_remove(s->listen_tag);
2548 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002549 tcp_chr_connect(chr);
2550
2551 return 0;
2552}
2553
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302554static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002555{
2556 CharDriverState *chr = opaque;
2557 TCPCharDriver *s = chr->opaque;
2558 struct sockaddr_in saddr;
2559#ifndef _WIN32
2560 struct sockaddr_un uaddr;
2561#endif
2562 struct sockaddr *addr;
2563 socklen_t len;
2564 int fd;
2565
2566 for(;;) {
2567#ifndef _WIN32
2568 if (s->is_unix) {
2569 len = sizeof(uaddr);
2570 addr = (struct sockaddr *)&uaddr;
2571 } else
2572#endif
2573 {
2574 len = sizeof(saddr);
2575 addr = (struct sockaddr *)&saddr;
2576 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002577 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002578 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302579 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002580 } else if (fd >= 0) {
2581 if (s->do_telnetopt)
2582 tcp_chr_telnet_init(fd);
2583 break;
2584 }
2585 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002586 if (tcp_chr_add_client(chr, fd) < 0)
2587 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302588
2589 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002590}
2591
2592static void tcp_chr_close(CharDriverState *chr)
2593{
2594 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002595 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302596 if (s->tag) {
2597 g_source_remove(s->tag);
2598 }
2599 if (s->chan) {
2600 g_io_channel_unref(s->chan);
2601 }
aliguori6f97dba2008-10-31 18:49:55 +00002602 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002603 }
2604 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302605 if (s->listen_tag) {
2606 g_source_remove(s->listen_tag);
2607 }
2608 if (s->listen_chan) {
2609 g_io_channel_unref(s->listen_chan);
2610 }
aliguori6f97dba2008-10-31 18:49:55 +00002611 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002612 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002613 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002614 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002615}
2616
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002617static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2618 bool is_listen, bool is_telnet,
2619 bool is_waitconnect,
2620 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002621{
2622 CharDriverState *chr = NULL;
2623 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002624 char host[NI_MAXHOST], serv[NI_MAXSERV];
2625 const char *left = "", *right = "";
2626 struct sockaddr_storage ss;
2627 socklen_t ss_len = sizeof(ss);
2628
2629 memset(&ss, 0, ss_len);
2630 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2631 error_setg(errp, "getsockname: %s", strerror(errno));
2632 return NULL;
2633 }
2634
2635 chr = g_malloc0(sizeof(CharDriverState));
2636 s = g_malloc0(sizeof(TCPCharDriver));
2637
2638 s->connected = 0;
2639 s->fd = -1;
2640 s->listen_fd = -1;
2641 s->msgfd = -1;
2642
2643 chr->filename = g_malloc(256);
2644 switch (ss.ss_family) {
2645#ifndef _WIN32
2646 case AF_UNIX:
2647 s->is_unix = 1;
2648 snprintf(chr->filename, 256, "unix:%s%s",
2649 ((struct sockaddr_un *)(&ss))->sun_path,
2650 is_listen ? ",server" : "");
2651 break;
2652#endif
2653 case AF_INET6:
2654 left = "[";
2655 right = "]";
2656 /* fall through */
2657 case AF_INET:
2658 s->do_nodelay = do_nodelay;
2659 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2660 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2661 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2662 is_telnet ? "telnet" : "tcp",
2663 left, host, right, serv,
2664 is_listen ? ",server" : "");
2665 break;
2666 }
2667
2668 chr->opaque = s;
2669 chr->chr_write = tcp_chr_write;
2670 chr->chr_close = tcp_chr_close;
2671 chr->get_msgfd = tcp_get_msgfd;
2672 chr->chr_add_client = tcp_chr_add_client;
2673
2674 if (is_listen) {
2675 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302676 s->listen_chan = io_channel_from_socket(s->listen_fd);
2677 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002678 if (is_telnet) {
2679 s->do_telnetopt = 1;
2680 }
2681 } else {
2682 s->connected = 1;
2683 s->fd = fd;
2684 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302685 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002686 tcp_chr_connect(chr);
2687 }
2688
2689 if (is_listen && is_waitconnect) {
2690 printf("QEMU waiting for connection on: %s\n",
2691 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302692 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002693 socket_set_nonblock(s->listen_fd);
2694 }
2695 return chr;
2696}
2697
2698static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2699{
2700 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002701 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002702 int fd = -1;
2703 int is_listen;
2704 int is_waitconnect;
2705 int do_nodelay;
2706 int is_unix;
2707 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002708
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002709 is_listen = qemu_opt_get_bool(opts, "server", 0);
2710 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2711 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2712 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2713 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002714 if (!is_listen)
2715 is_waitconnect = 0;
2716
aliguorif07b6002008-11-11 20:54:09 +00002717 if (is_unix) {
2718 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002719 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002720 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002721 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002722 }
2723 } else {
2724 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002725 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002726 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002727 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002728 }
2729 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002730 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002731 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002732 }
aliguori6f97dba2008-10-31 18:49:55 +00002733
2734 if (!is_waitconnect)
2735 socket_set_nonblock(fd);
2736
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002737 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2738 is_waitconnect, &local_err);
2739 if (error_is_set(&local_err)) {
2740 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002741 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002742 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002743
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002744
aliguori6f97dba2008-10-31 18:49:55 +00002745 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002746 if (local_err) {
2747 qerror_report_err(local_err);
2748 error_free(local_err);
2749 }
2750 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002751 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002752 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002753 if (chr) {
2754 g_free(chr->opaque);
2755 g_free(chr);
2756 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002757 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002758}
2759
Luiz Capitulino999bd672010-10-22 16:09:05 -02002760/***********************************************************/
2761/* Memory chardev */
2762typedef struct {
2763 size_t outbuf_size;
2764 size_t outbuf_capacity;
2765 uint8_t *outbuf;
2766} MemoryDriver;
2767
2768static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2769{
2770 MemoryDriver *d = chr->opaque;
2771
2772 /* TODO: the QString implementation has the same code, we should
2773 * introduce a generic way to do this in cutils.c */
2774 if (d->outbuf_capacity < d->outbuf_size + len) {
2775 /* grow outbuf */
2776 d->outbuf_capacity += len;
2777 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002778 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002779 }
2780
2781 memcpy(d->outbuf + d->outbuf_size, buf, len);
2782 d->outbuf_size += len;
2783
2784 return len;
2785}
2786
2787void qemu_chr_init_mem(CharDriverState *chr)
2788{
2789 MemoryDriver *d;
2790
Anthony Liguori7267c092011-08-20 22:09:37 -05002791 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002792 d->outbuf_size = 0;
2793 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002794 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002795
2796 memset(chr, 0, sizeof(*chr));
2797 chr->opaque = d;
2798 chr->chr_write = mem_chr_write;
2799}
2800
2801QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2802{
2803 MemoryDriver *d = chr->opaque;
2804 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2805}
2806
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002807/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002808void qemu_chr_close_mem(CharDriverState *chr)
2809{
2810 MemoryDriver *d = chr->opaque;
2811
Anthony Liguori7267c092011-08-20 22:09:37 -05002812 g_free(d->outbuf);
2813 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002814 chr->opaque = NULL;
2815 chr->chr_write = NULL;
2816}
2817
2818size_t qemu_chr_mem_osize(const CharDriverState *chr)
2819{
2820 const MemoryDriver *d = chr->opaque;
2821 return d->outbuf_size;
2822}
2823
Lei Li51767e72013-01-25 00:03:19 +08002824/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002825/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002826
2827typedef struct {
2828 size_t size;
2829 size_t prod;
2830 size_t cons;
2831 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002832} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002833
Markus Armbruster3949e592013-02-06 21:27:24 +01002834static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002835{
Markus Armbruster3949e592013-02-06 21:27:24 +01002836 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002837
Markus Armbruster5c230102013-02-06 21:27:23 +01002838 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002839}
2840
Markus Armbruster3949e592013-02-06 21:27:24 +01002841static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002842{
Markus Armbruster3949e592013-02-06 21:27:24 +01002843 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002844 int i;
2845
2846 if (!buf || (len < 0)) {
2847 return -1;
2848 }
2849
2850 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002851 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2852 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002853 d->cons = d->prod - d->size;
2854 }
2855 }
2856
2857 return 0;
2858}
2859
Markus Armbruster3949e592013-02-06 21:27:24 +01002860static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002861{
Markus Armbruster3949e592013-02-06 21:27:24 +01002862 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002863 int i;
2864
Markus Armbruster5c230102013-02-06 21:27:23 +01002865 for (i = 0; i < len && d->cons != d->prod; i++) {
2866 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002867 }
2868
2869 return i;
2870}
2871
Markus Armbruster3949e592013-02-06 21:27:24 +01002872static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002873{
Markus Armbruster3949e592013-02-06 21:27:24 +01002874 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002875
2876 g_free(d->cbuf);
2877 g_free(d);
2878 chr->opaque = NULL;
2879}
2880
Markus Armbruster3949e592013-02-06 21:27:24 +01002881static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002882{
2883 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002884 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002885
2886 chr = g_malloc0(sizeof(CharDriverState));
2887 d = g_malloc(sizeof(*d));
2888
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002889 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002890 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002891 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002892 }
2893
2894 /* The size must be power of 2 */
2895 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002896 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002897 goto fail;
2898 }
2899
2900 d->prod = 0;
2901 d->cons = 0;
2902 d->cbuf = g_malloc0(d->size);
2903
2904 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002905 chr->chr_write = ringbuf_chr_write;
2906 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002907
2908 return chr;
2909
2910fail:
2911 g_free(d);
2912 g_free(chr);
2913 return NULL;
2914}
2915
Markus Armbruster3949e592013-02-06 21:27:24 +01002916static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002917{
Markus Armbruster3949e592013-02-06 21:27:24 +01002918 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002919}
2920
Markus Armbruster3949e592013-02-06 21:27:24 +01002921void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002922 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002923 Error **errp)
2924{
2925 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002926 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002927 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002928 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002929
2930 chr = qemu_chr_find(device);
2931 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002932 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002933 return;
2934 }
2935
Markus Armbruster3949e592013-02-06 21:27:24 +01002936 if (!chr_is_ringbuf(chr)) {
2937 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002938 return;
2939 }
2940
Lei Li1f590cf2013-01-25 00:03:20 +08002941 if (has_format && (format == DATA_FORMAT_BASE64)) {
2942 write_data = g_base64_decode(data, &write_count);
2943 } else {
2944 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002945 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002946 }
2947
Markus Armbruster3949e592013-02-06 21:27:24 +01002948 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002949
Markus Armbruster13289fb2013-02-06 21:27:18 +01002950 if (write_data != (uint8_t *)data) {
2951 g_free((void *)write_data);
2952 }
2953
Lei Li1f590cf2013-01-25 00:03:20 +08002954 if (ret < 0) {
2955 error_setg(errp, "Failed to write to device %s", device);
2956 return;
2957 }
2958}
2959
Markus Armbruster3949e592013-02-06 21:27:24 +01002960char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002961 bool has_format, enum DataFormat format,
2962 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002963{
2964 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002965 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002966 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002967 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002968
2969 chr = qemu_chr_find(device);
2970 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002971 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002972 return NULL;
2973 }
2974
Markus Armbruster3949e592013-02-06 21:27:24 +01002975 if (!chr_is_ringbuf(chr)) {
2976 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002977 return NULL;
2978 }
2979
2980 if (size <= 0) {
2981 error_setg(errp, "size must be greater than zero");
2982 return NULL;
2983 }
2984
Markus Armbruster3949e592013-02-06 21:27:24 +01002985 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002986 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002987 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002988
Markus Armbruster3949e592013-02-06 21:27:24 +01002989 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002990
2991 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002992 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002993 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002994 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002995 /*
2996 * FIXME should read only complete, valid UTF-8 characters up
2997 * to @size bytes. Invalid sequences should be replaced by a
2998 * suitable replacement character. Except when (and only
2999 * when) ring buffer lost characters since last read, initial
3000 * continuation characters should be dropped.
3001 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003002 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003003 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003004 }
3005
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003006 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003007}
3008
Gerd Hoffmann33521632009-12-08 13:11:49 +01003009QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003010{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003011 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003012 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003013 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003014 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003015 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003016
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003017 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3018 if (error_is_set(&local_err)) {
3019 qerror_report_err(local_err);
3020 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003021 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003022 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003023
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003024 if (strstart(filename, "mon:", &p)) {
3025 filename = p;
3026 qemu_opt_set(opts, "mux", "on");
3027 }
3028
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003029 if (strcmp(filename, "null") == 0 ||
3030 strcmp(filename, "pty") == 0 ||
3031 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003032 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003033 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003034 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003035 return opts;
3036 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003037 if (strstart(filename, "vc", &p)) {
3038 qemu_opt_set(opts, "backend", "vc");
3039 if (*p == ':') {
3040 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3041 /* pixels */
3042 qemu_opt_set(opts, "width", width);
3043 qemu_opt_set(opts, "height", height);
3044 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3045 /* chars */
3046 qemu_opt_set(opts, "cols", width);
3047 qemu_opt_set(opts, "rows", height);
3048 } else {
3049 goto fail;
3050 }
3051 }
3052 return opts;
3053 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003054 if (strcmp(filename, "con:") == 0) {
3055 qemu_opt_set(opts, "backend", "console");
3056 return opts;
3057 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003058 if (strstart(filename, "COM", NULL)) {
3059 qemu_opt_set(opts, "backend", "serial");
3060 qemu_opt_set(opts, "path", filename);
3061 return opts;
3062 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003063 if (strstart(filename, "file:", &p)) {
3064 qemu_opt_set(opts, "backend", "file");
3065 qemu_opt_set(opts, "path", p);
3066 return opts;
3067 }
3068 if (strstart(filename, "pipe:", &p)) {
3069 qemu_opt_set(opts, "backend", "pipe");
3070 qemu_opt_set(opts, "path", p);
3071 return opts;
3072 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003073 if (strstart(filename, "tcp:", &p) ||
3074 strstart(filename, "telnet:", &p)) {
3075 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3076 host[0] = 0;
3077 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3078 goto fail;
3079 }
3080 qemu_opt_set(opts, "backend", "socket");
3081 qemu_opt_set(opts, "host", host);
3082 qemu_opt_set(opts, "port", port);
3083 if (p[pos] == ',') {
3084 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3085 goto fail;
3086 }
3087 if (strstart(filename, "telnet:", &p))
3088 qemu_opt_set(opts, "telnet", "on");
3089 return opts;
3090 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003091 if (strstart(filename, "udp:", &p)) {
3092 qemu_opt_set(opts, "backend", "udp");
3093 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3094 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003095 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003096 goto fail;
3097 }
3098 }
3099 qemu_opt_set(opts, "host", host);
3100 qemu_opt_set(opts, "port", port);
3101 if (p[pos] == '@') {
3102 p += pos + 1;
3103 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3104 host[0] = 0;
3105 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003106 goto fail;
3107 }
3108 }
3109 qemu_opt_set(opts, "localaddr", host);
3110 qemu_opt_set(opts, "localport", port);
3111 }
3112 return opts;
3113 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003114 if (strstart(filename, "unix:", &p)) {
3115 qemu_opt_set(opts, "backend", "socket");
3116 if (qemu_opts_do_parse(opts, p, "path") != 0)
3117 goto fail;
3118 return opts;
3119 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003120 if (strstart(filename, "/dev/parport", NULL) ||
3121 strstart(filename, "/dev/ppi", NULL)) {
3122 qemu_opt_set(opts, "backend", "parport");
3123 qemu_opt_set(opts, "path", filename);
3124 return opts;
3125 }
3126 if (strstart(filename, "/dev/", NULL)) {
3127 qemu_opt_set(opts, "backend", "tty");
3128 qemu_opt_set(opts, "path", filename);
3129 return opts;
3130 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003131
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003132fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003133 qemu_opts_del(opts);
3134 return NULL;
3135}
3136
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003137#ifdef HAVE_CHARDEV_PARPORT
3138
3139static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3140{
3141 const char *filename = qemu_opt_get(opts, "path");
3142 int fd;
3143
3144 fd = qemu_open(filename, O_RDWR);
3145 if (fd < 0) {
3146 return NULL;
3147 }
3148 return qemu_chr_open_pp_fd(fd);
3149}
3150
3151#endif
3152
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003153static const struct {
3154 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01003155 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003156} backend_table[] = {
3157 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003158 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003159 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003160 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Anthony Liguorid82831d2013-02-20 07:43:19 -06003161 { .name = "vc", .open = vc_init },
Markus Armbruster3949e592013-02-06 21:27:24 +01003162 { .name = "memory", .open = qemu_chr_open_ringbuf },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003163#ifdef _WIN32
3164 { .name = "file", .open = qemu_chr_open_win_file_out },
3165 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003166 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003167 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02003168 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003169#else
3170 { .name = "file", .open = qemu_chr_open_file_out },
3171 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02003172 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003173#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003174#ifdef CONFIG_BRLAPI
3175 { .name = "braille", .open = chr_baum_init },
3176#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003177#ifdef HAVE_CHARDEV_TTY
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003178 { .name = "tty", .open = qemu_chr_open_tty },
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003179 { .name = "serial", .open = qemu_chr_open_tty },
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003180 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003181#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003182#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003183 { .name = "parallel", .open = qemu_chr_open_pp },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003184 { .name = "parport", .open = qemu_chr_open_pp },
3185#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003186#ifdef CONFIG_SPICE
3187 { .name = "spicevmc", .open = qemu_chr_open_spice },
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01003188#if SPICE_SERVER_VERSION >= 0x000c02
3189 { .name = "spiceport", .open = qemu_chr_open_spice_port },
3190#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003191#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003192};
3193
Anthony Liguorif69554b2011-08-15 11:17:37 -05003194CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003195 void (*init)(struct CharDriverState *s),
3196 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003197{
3198 CharDriverState *chr;
3199 int i;
3200
3201 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003202 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003203 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003204 }
3205
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003206 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003207 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003208 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003209 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003210 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003211 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
3212 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
3213 break;
3214 }
3215 if (i == ARRAY_SIZE(backend_table)) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003216 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003217 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003218 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003219 }
3220
Markus Armbruster1f514702012-02-07 15:09:08 +01003221 chr = backend_table[i].open(opts);
3222 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003223 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003224 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003225 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003226 }
3227
3228 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003229 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003231 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003232
3233 if (qemu_opt_get_bool(opts, "mux", 0)) {
3234 CharDriverState *base = chr;
3235 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003236 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003237 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3238 chr = qemu_chr_open_mux(base);
3239 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003240 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003241 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003242 } else {
3243 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003244 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003245 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003246 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003247 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003248
3249err:
3250 qemu_opts_del(opts);
3251 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003252}
3253
Anthony Liguori27143a42011-08-15 11:17:36 -05003254CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003255{
3256 const char *p;
3257 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003258 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003259 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003260
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003261 if (strstart(filename, "chardev:", &p)) {
3262 return qemu_chr_find(p);
3263 }
3264
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003265 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003266 if (!opts)
3267 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003268
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003269 chr = qemu_chr_new_from_opts(opts, init, &err);
3270 if (error_is_set(&err)) {
3271 fprintf(stderr, "%s\n", error_get_pretty(err));
3272 error_free(err);
3273 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003274 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3275 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003276 }
3277 return chr;
3278}
3279
Anthony Liguori15f31512011-08-15 11:17:35 -05003280void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003281{
3282 if (chr->chr_set_echo) {
3283 chr->chr_set_echo(chr, echo);
3284 }
3285}
3286
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003287void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003288{
3289 if (chr->chr_guest_open) {
3290 chr->chr_guest_open(chr);
3291 }
3292}
3293
Anthony Liguori28178222011-08-15 11:17:33 -05003294void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003295{
3296 if (chr->chr_guest_close) {
3297 chr->chr_guest_close(chr);
3298 }
3299}
3300
Anthony Liguori23673ca2013-03-05 23:21:23 +05303301guint qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3302 GIOFunc func, void *user_data)
3303{
3304 GSource *src;
3305 guint tag;
3306
3307 if (s->chr_add_watch == NULL) {
3308 return -ENOSYS;
3309 }
3310
3311 src = s->chr_add_watch(s, cond);
3312 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3313 tag = g_source_attach(src, NULL);
3314 g_source_unref(src);
3315
3316 return tag;
3317}
3318
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003319void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003320{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003321 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003322 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003323 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003324 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003325 g_free(chr->filename);
3326 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003327 if (chr->opts) {
3328 qemu_opts_del(chr->opts);
3329 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003330 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003331}
3332
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003333ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003334{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003335 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003336 CharDriverState *chr;
3337
Blue Swirl72cf2d42009-09-12 07:36:22 +00003338 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003339 ChardevInfoList *info = g_malloc0(sizeof(*info));
3340 info->value = g_malloc0(sizeof(*info->value));
3341 info->value->label = g_strdup(chr->label);
3342 info->value->filename = g_strdup(chr->filename);
3343
3344 info->next = chr_list;
3345 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003346 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003347
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003348 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003349}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003350
3351CharDriverState *qemu_chr_find(const char *name)
3352{
3353 CharDriverState *chr;
3354
Blue Swirl72cf2d42009-09-12 07:36:22 +00003355 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003356 if (strcmp(chr->label, name) != 0)
3357 continue;
3358 return chr;
3359 }
3360 return NULL;
3361}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003362
3363/* Get a character (serial) device interface. */
3364CharDriverState *qemu_char_get_next_serial(void)
3365{
3366 static int next_serial;
3367
3368 /* FIXME: This function needs to go away: use chardev properties! */
3369 return serial_hds[next_serial++];
3370}
3371
Paolo Bonzini4d454572012-11-26 16:03:42 +01003372QemuOptsList qemu_chardev_opts = {
3373 .name = "chardev",
3374 .implied_opt_name = "backend",
3375 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3376 .desc = {
3377 {
3378 .name = "backend",
3379 .type = QEMU_OPT_STRING,
3380 },{
3381 .name = "path",
3382 .type = QEMU_OPT_STRING,
3383 },{
3384 .name = "host",
3385 .type = QEMU_OPT_STRING,
3386 },{
3387 .name = "port",
3388 .type = QEMU_OPT_STRING,
3389 },{
3390 .name = "localaddr",
3391 .type = QEMU_OPT_STRING,
3392 },{
3393 .name = "localport",
3394 .type = QEMU_OPT_STRING,
3395 },{
3396 .name = "to",
3397 .type = QEMU_OPT_NUMBER,
3398 },{
3399 .name = "ipv4",
3400 .type = QEMU_OPT_BOOL,
3401 },{
3402 .name = "ipv6",
3403 .type = QEMU_OPT_BOOL,
3404 },{
3405 .name = "wait",
3406 .type = QEMU_OPT_BOOL,
3407 },{
3408 .name = "server",
3409 .type = QEMU_OPT_BOOL,
3410 },{
3411 .name = "delay",
3412 .type = QEMU_OPT_BOOL,
3413 },{
3414 .name = "telnet",
3415 .type = QEMU_OPT_BOOL,
3416 },{
3417 .name = "width",
3418 .type = QEMU_OPT_NUMBER,
3419 },{
3420 .name = "height",
3421 .type = QEMU_OPT_NUMBER,
3422 },{
3423 .name = "cols",
3424 .type = QEMU_OPT_NUMBER,
3425 },{
3426 .name = "rows",
3427 .type = QEMU_OPT_NUMBER,
3428 },{
3429 .name = "mux",
3430 .type = QEMU_OPT_BOOL,
3431 },{
3432 .name = "signal",
3433 .type = QEMU_OPT_BOOL,
3434 },{
3435 .name = "name",
3436 .type = QEMU_OPT_STRING,
3437 },{
3438 .name = "debug",
3439 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003440 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003441 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003442 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003443 },
3444 { /* end of list */ }
3445 },
3446};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003447
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003448#ifdef _WIN32
3449
3450static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3451{
3452 HANDLE out;
3453
3454 if (file->in) {
3455 error_setg(errp, "input file not supported");
3456 return NULL;
3457 }
3458
3459 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3460 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3461 if (out == INVALID_HANDLE_VALUE) {
3462 error_setg(errp, "open %s failed", file->out);
3463 return NULL;
3464 }
3465 return qemu_chr_open_win_file(out);
3466}
3467
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003468static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3469 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003470{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003471 return qemu_chr_open_win_path(serial->device);
3472}
3473
3474static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3475 Error **errp)
3476{
3477 error_setg(errp, "character device backend type 'parallel' not supported");
3478 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003479}
3480
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003481#else /* WIN32 */
3482
3483static int qmp_chardev_open_file_source(char *src, int flags,
3484 Error **errp)
3485{
3486 int fd = -1;
3487
3488 TFR(fd = qemu_open(src, flags, 0666));
3489 if (fd == -1) {
3490 error_setg(errp, "open %s: %s", src, strerror(errno));
3491 }
3492 return fd;
3493}
3494
3495static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3496{
3497 int flags, in = -1, out = -1;
3498
3499 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3500 out = qmp_chardev_open_file_source(file->out, flags, errp);
3501 if (error_is_set(errp)) {
3502 return NULL;
3503 }
3504
3505 if (file->in) {
3506 flags = O_RDONLY;
3507 in = qmp_chardev_open_file_source(file->in, flags, errp);
3508 if (error_is_set(errp)) {
3509 qemu_close(out);
3510 return NULL;
3511 }
3512 }
3513
3514 return qemu_chr_open_fd(in, out);
3515}
3516
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003517static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3518 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003519{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003520#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003521 int fd;
3522
3523 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3524 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003525 return NULL;
3526 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003527 socket_set_nonblock(fd);
3528 return qemu_chr_open_tty_fd(fd);
3529#else
3530 error_setg(errp, "character device backend type 'serial' not supported");
3531 return NULL;
3532#endif
3533}
3534
3535static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3536 Error **errp)
3537{
3538#ifdef HAVE_CHARDEV_PARPORT
3539 int fd;
3540
3541 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3542 if (error_is_set(errp)) {
3543 return NULL;
3544 }
3545 return qemu_chr_open_pp_fd(fd);
3546#else
3547 error_setg(errp, "character device backend type 'parallel' not supported");
3548 return NULL;
3549#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003550}
3551
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003552#endif /* WIN32 */
3553
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003554static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3555 Error **errp)
3556{
3557 SocketAddress *addr = sock->addr;
3558 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3559 bool is_listen = sock->has_server ? sock->server : true;
3560 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3561 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3562 int fd;
3563
3564 if (is_listen) {
3565 fd = socket_listen(addr, errp);
3566 } else {
3567 fd = socket_connect(addr, errp, NULL, NULL);
3568 }
3569 if (error_is_set(errp)) {
3570 return NULL;
3571 }
3572 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3573 is_telnet, is_waitconnect, errp);
3574}
3575
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003576ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3577 Error **errp)
3578{
3579 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3580 CharDriverState *chr = NULL;
3581
3582 chr = qemu_chr_find(id);
3583 if (chr) {
3584 error_setg(errp, "Chardev '%s' already exists", id);
3585 g_free(ret);
3586 return NULL;
3587 }
3588
3589 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003590 case CHARDEV_BACKEND_KIND_FILE:
3591 chr = qmp_chardev_open_file(backend->file, errp);
3592 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003593 case CHARDEV_BACKEND_KIND_SERIAL:
3594 chr = qmp_chardev_open_serial(backend->serial, errp);
3595 break;
3596 case CHARDEV_BACKEND_KIND_PARALLEL:
3597 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003598 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003599 case CHARDEV_BACKEND_KIND_SOCKET:
3600 chr = qmp_chardev_open_socket(backend->socket, errp);
3601 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003602#ifdef HAVE_CHARDEV_TTY
3603 case CHARDEV_BACKEND_KIND_PTY:
3604 {
3605 /* qemu_chr_open_pty sets "path" in opts */
3606 QemuOpts *opts;
3607 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3608 chr = qemu_chr_open_pty(opts);
3609 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3610 ret->has_pty = true;
3611 qemu_opts_del(opts);
3612 break;
3613 }
3614#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003615 case CHARDEV_BACKEND_KIND_NULL:
3616 chr = qemu_chr_open_null(NULL);
3617 break;
3618 default:
3619 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3620 break;
3621 }
3622
3623 if (chr == NULL && !error_is_set(errp)) {
3624 error_setg(errp, "Failed to create chardev");
3625 }
3626 if (chr) {
3627 chr->label = g_strdup(id);
3628 chr->avail_connections = 1;
3629 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3630 return ret;
3631 } else {
3632 g_free(ret);
3633 return NULL;
3634 }
3635}
3636
3637void qmp_chardev_remove(const char *id, Error **errp)
3638{
3639 CharDriverState *chr;
3640
3641 chr = qemu_chr_find(id);
3642 if (NULL == chr) {
3643 error_setg(errp, "Chardev '%s' not found", id);
3644 return;
3645 }
3646 if (chr->chr_can_read || chr->chr_read ||
3647 chr->chr_event || chr->handler_opaque) {
3648 error_setg(errp, "Chardev '%s' is busy", id);
3649 return;
3650 }
3651 qemu_chr_delete(chr);
3652}