blob: 70917438d3ef6509b4df3017ce5b423a18cca3bd [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
1011static int pty_chr_read_poll(void *opaque)
1012{
1013 CharDriverState *chr = opaque;
1014 PtyCharDriver *s = chr->opaque;
1015
Anthony Liguori909cda12011-08-15 11:17:31 -05001016 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001017 return s->read_bytes;
1018}
1019
Anthony Liguori093d3a22013-03-05 23:21:20 +05301020static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001021{
1022 CharDriverState *chr = opaque;
1023 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301024 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301025 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301026 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001027
1028 len = sizeof(buf);
1029 if (len > s->read_bytes)
1030 len = s->read_bytes;
1031 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301032 return FALSE;
1033 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1034 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001035 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301036 return FALSE;
1037 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001038 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001039 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001040 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301041 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001042}
1043
1044static void pty_chr_update_read_handler(CharDriverState *chr)
1045{
1046 PtyCharDriver *s = chr->opaque;
1047
Anthony Liguori093d3a22013-03-05 23:21:20 +05301048 if (s->fd_tag) {
1049 g_source_remove(s->fd_tag);
1050 }
1051
1052 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001053 s->polling = 1;
1054 /*
1055 * Short timeout here: just need wait long enougth that qemu makes
1056 * it through the poll loop once. When reconnected we want a
1057 * short timeout so we notice it almost instantly. Otherwise
1058 * read() gives us -EIO instantly, making pty_chr_state() reset the
1059 * timeout to the normal (much longer) poll interval before the
1060 * timer triggers.
1061 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001062 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +00001063}
1064
1065static void pty_chr_state(CharDriverState *chr, int connected)
1066{
1067 PtyCharDriver *s = chr->opaque;
1068
1069 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301070 g_source_remove(s->fd_tag);
1071 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001072 s->connected = 0;
1073 s->polling = 0;
1074 /* (re-)connect poll interval for idle guests: once per second.
1075 * We check more frequently in case the guests sends data to
1076 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001077 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001078 } else {
1079 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301080 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001081 s->connected = 1;
1082 }
1083}
1084
1085static void pty_chr_timer(void *opaque)
1086{
1087 struct CharDriverState *chr = opaque;
1088 PtyCharDriver *s = chr->opaque;
1089
1090 if (s->connected)
1091 return;
1092 if (s->polling) {
1093 /* If we arrive here without polling being cleared due
1094 * read returning -EIO, then we are (re-)connected */
1095 pty_chr_state(chr, 1);
1096 return;
1097 }
1098
1099 /* Next poll ... */
1100 pty_chr_update_read_handler(chr);
1101}
1102
1103static void pty_chr_close(struct CharDriverState *chr)
1104{
1105 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301106 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001107
Anthony Liguori093d3a22013-03-05 23:21:20 +05301108 if (s->fd_tag) {
1109 g_source_remove(s->fd_tag);
1110 }
1111 fd = g_io_channel_unix_get_fd(s->fd);
1112 g_io_channel_unref(s->fd);
1113 close(fd);
aliguori819f56b2009-03-28 17:58:14 +00001114 qemu_del_timer(s->timer);
1115 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -05001116 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001117 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001118}
1119
Markus Armbruster1f514702012-02-07 15:09:08 +01001120static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001121{
1122 CharDriverState *chr;
1123 PtyCharDriver *s;
1124 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001125 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001126 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001127#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001128 char pty_name[PATH_MAX];
1129#define q_ptsname(x) pty_name
1130#else
1131 char *pty_name = NULL;
1132#define q_ptsname(x) ptsname(x)
1133#endif
1134
Markus Armbrustera4e26042011-11-11 10:40:05 +01001135 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001136 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001137 }
1138
1139 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001140 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001141 cfmakeraw(&tty);
1142 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1143 close(slave_fd);
1144
Markus Armbrustera4e26042011-11-11 10:40:05 +01001145 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001146
Markus Armbrustera4e26042011-11-11 10:40:05 +01001147 len = strlen(q_ptsname(master_fd)) + 5;
1148 chr->filename = g_malloc(len);
1149 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1150 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001151
1152 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001153 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1154 q_ptsname(master_fd),
1155 label ? " (label " : "",
1156 label ? label : "",
1157 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001158
1159 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001160 chr->opaque = s;
1161 chr->chr_write = pty_chr_write;
1162 chr->chr_update_read_handler = pty_chr_update_read_handler;
1163 chr->chr_close = pty_chr_close;
1164
Anthony Liguori093d3a22013-03-05 23:21:20 +05301165 s->fd = io_channel_from_fd(master_fd);
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001166 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001167
Markus Armbruster1f514702012-02-07 15:09:08 +01001168 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001169}
1170
1171static void tty_serial_init(int fd, int speed,
1172 int parity, int data_bits, int stop_bits)
1173{
1174 struct termios tty;
1175 speed_t spd;
1176
1177#if 0
1178 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1179 speed, parity, data_bits, stop_bits);
1180#endif
1181 tcgetattr (fd, &tty);
1182
Stefan Weil45eea132009-10-26 16:10:10 +01001183#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1184 speed = speed * 10 / 11;
1185 do {
1186 check_speed(50);
1187 check_speed(75);
1188 check_speed(110);
1189 check_speed(134);
1190 check_speed(150);
1191 check_speed(200);
1192 check_speed(300);
1193 check_speed(600);
1194 check_speed(1200);
1195 check_speed(1800);
1196 check_speed(2400);
1197 check_speed(4800);
1198 check_speed(9600);
1199 check_speed(19200);
1200 check_speed(38400);
1201 /* Non-Posix values follow. They may be unsupported on some systems. */
1202 check_speed(57600);
1203 check_speed(115200);
1204#ifdef B230400
1205 check_speed(230400);
1206#endif
1207#ifdef B460800
1208 check_speed(460800);
1209#endif
1210#ifdef B500000
1211 check_speed(500000);
1212#endif
1213#ifdef B576000
1214 check_speed(576000);
1215#endif
1216#ifdef B921600
1217 check_speed(921600);
1218#endif
1219#ifdef B1000000
1220 check_speed(1000000);
1221#endif
1222#ifdef B1152000
1223 check_speed(1152000);
1224#endif
1225#ifdef B1500000
1226 check_speed(1500000);
1227#endif
1228#ifdef B2000000
1229 check_speed(2000000);
1230#endif
1231#ifdef B2500000
1232 check_speed(2500000);
1233#endif
1234#ifdef B3000000
1235 check_speed(3000000);
1236#endif
1237#ifdef B3500000
1238 check_speed(3500000);
1239#endif
1240#ifdef B4000000
1241 check_speed(4000000);
1242#endif
aliguori6f97dba2008-10-31 18:49:55 +00001243 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001244 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001245
1246 cfsetispeed(&tty, spd);
1247 cfsetospeed(&tty, spd);
1248
1249 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1250 |INLCR|IGNCR|ICRNL|IXON);
1251 tty.c_oflag |= OPOST;
1252 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1253 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1254 switch(data_bits) {
1255 default:
1256 case 8:
1257 tty.c_cflag |= CS8;
1258 break;
1259 case 7:
1260 tty.c_cflag |= CS7;
1261 break;
1262 case 6:
1263 tty.c_cflag |= CS6;
1264 break;
1265 case 5:
1266 tty.c_cflag |= CS5;
1267 break;
1268 }
1269 switch(parity) {
1270 default:
1271 case 'N':
1272 break;
1273 case 'E':
1274 tty.c_cflag |= PARENB;
1275 break;
1276 case 'O':
1277 tty.c_cflag |= PARENB | PARODD;
1278 break;
1279 }
1280 if (stop_bits == 2)
1281 tty.c_cflag |= CSTOPB;
1282
1283 tcsetattr (fd, TCSANOW, &tty);
1284}
1285
1286static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1287{
1288 FDCharDriver *s = chr->opaque;
1289
1290 switch(cmd) {
1291 case CHR_IOCTL_SERIAL_SET_PARAMS:
1292 {
1293 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301294 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1295 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001296 ssp->data_bits, ssp->stop_bits);
1297 }
1298 break;
1299 case CHR_IOCTL_SERIAL_SET_BREAK:
1300 {
1301 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301302 if (enable) {
1303 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1304 }
aliguori6f97dba2008-10-31 18:49:55 +00001305 }
1306 break;
1307 case CHR_IOCTL_SERIAL_GET_TIOCM:
1308 {
1309 int sarg = 0;
1310 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301311 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001312 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001313 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001314 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001315 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001316 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001317 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001318 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001319 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001320 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001321 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001322 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001323 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001324 *targ |= CHR_TIOCM_RTS;
1325 }
1326 break;
1327 case CHR_IOCTL_SERIAL_SET_TIOCM:
1328 {
1329 int sarg = *(int *)arg;
1330 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301331 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001332 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1333 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1334 if (sarg & CHR_TIOCM_CTS)
1335 targ |= TIOCM_CTS;
1336 if (sarg & CHR_TIOCM_CAR)
1337 targ |= TIOCM_CAR;
1338 if (sarg & CHR_TIOCM_DSR)
1339 targ |= TIOCM_DSR;
1340 if (sarg & CHR_TIOCM_RI)
1341 targ |= TIOCM_RI;
1342 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001343 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001344 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001345 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301346 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001347 }
1348 break;
1349 default:
1350 return -ENOTSUP;
1351 }
1352 return 0;
1353}
1354
David Ahern4266a132010-02-10 18:27:17 -07001355static void qemu_chr_close_tty(CharDriverState *chr)
1356{
1357 FDCharDriver *s = chr->opaque;
1358 int fd = -1;
1359
1360 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301361 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001362 }
1363
1364 fd_chr_close(chr);
1365
1366 if (fd >= 0) {
1367 close(fd);
1368 }
1369}
1370
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001371static CharDriverState *qemu_chr_open_tty_fd(int fd)
1372{
1373 CharDriverState *chr;
1374
1375 tty_serial_init(fd, 115200, 'N', 8, 1);
1376 chr = qemu_chr_open_fd(fd, fd);
1377 chr->chr_ioctl = tty_serial_ioctl;
1378 chr->chr_close = qemu_chr_close_tty;
1379 return chr;
1380}
1381
Markus Armbruster1f514702012-02-07 15:09:08 +01001382static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001383{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001384 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001385 int fd;
1386
Markus Armbrusterb181e042012-02-07 15:09:09 +01001387 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001388 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001389 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001390 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001391 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001392}
aliguori6f97dba2008-10-31 18:49:55 +00001393#endif /* __linux__ || __sun__ */
1394
1395#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001396
1397#define HAVE_CHARDEV_PARPORT 1
1398
aliguori6f97dba2008-10-31 18:49:55 +00001399typedef struct {
1400 int fd;
1401 int mode;
1402} ParallelCharDriver;
1403
1404static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1405{
1406 if (s->mode != mode) {
1407 int m = mode;
1408 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1409 return 0;
1410 s->mode = mode;
1411 }
1412 return 1;
1413}
1414
1415static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1416{
1417 ParallelCharDriver *drv = chr->opaque;
1418 int fd = drv->fd;
1419 uint8_t b;
1420
1421 switch(cmd) {
1422 case CHR_IOCTL_PP_READ_DATA:
1423 if (ioctl(fd, PPRDATA, &b) < 0)
1424 return -ENOTSUP;
1425 *(uint8_t *)arg = b;
1426 break;
1427 case CHR_IOCTL_PP_WRITE_DATA:
1428 b = *(uint8_t *)arg;
1429 if (ioctl(fd, PPWDATA, &b) < 0)
1430 return -ENOTSUP;
1431 break;
1432 case CHR_IOCTL_PP_READ_CONTROL:
1433 if (ioctl(fd, PPRCONTROL, &b) < 0)
1434 return -ENOTSUP;
1435 /* Linux gives only the lowest bits, and no way to know data
1436 direction! For better compatibility set the fixed upper
1437 bits. */
1438 *(uint8_t *)arg = b | 0xc0;
1439 break;
1440 case CHR_IOCTL_PP_WRITE_CONTROL:
1441 b = *(uint8_t *)arg;
1442 if (ioctl(fd, PPWCONTROL, &b) < 0)
1443 return -ENOTSUP;
1444 break;
1445 case CHR_IOCTL_PP_READ_STATUS:
1446 if (ioctl(fd, PPRSTATUS, &b) < 0)
1447 return -ENOTSUP;
1448 *(uint8_t *)arg = b;
1449 break;
1450 case CHR_IOCTL_PP_DATA_DIR:
1451 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1452 return -ENOTSUP;
1453 break;
1454 case CHR_IOCTL_PP_EPP_READ_ADDR:
1455 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1456 struct ParallelIOArg *parg = arg;
1457 int n = read(fd, parg->buffer, parg->count);
1458 if (n != parg->count) {
1459 return -EIO;
1460 }
1461 }
1462 break;
1463 case CHR_IOCTL_PP_EPP_READ:
1464 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1465 struct ParallelIOArg *parg = arg;
1466 int n = read(fd, parg->buffer, parg->count);
1467 if (n != parg->count) {
1468 return -EIO;
1469 }
1470 }
1471 break;
1472 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1473 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1474 struct ParallelIOArg *parg = arg;
1475 int n = write(fd, parg->buffer, parg->count);
1476 if (n != parg->count) {
1477 return -EIO;
1478 }
1479 }
1480 break;
1481 case CHR_IOCTL_PP_EPP_WRITE:
1482 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1483 struct ParallelIOArg *parg = arg;
1484 int n = write(fd, parg->buffer, parg->count);
1485 if (n != parg->count) {
1486 return -EIO;
1487 }
1488 }
1489 break;
1490 default:
1491 return -ENOTSUP;
1492 }
1493 return 0;
1494}
1495
1496static void pp_close(CharDriverState *chr)
1497{
1498 ParallelCharDriver *drv = chr->opaque;
1499 int fd = drv->fd;
1500
1501 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1502 ioctl(fd, PPRELEASE);
1503 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001504 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001505 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001506}
1507
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001508static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001509{
1510 CharDriverState *chr;
1511 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001512
1513 if (ioctl(fd, PPCLAIM) < 0) {
1514 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001515 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001516 }
1517
Anthony Liguori7267c092011-08-20 22:09:37 -05001518 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001519 drv->fd = fd;
1520 drv->mode = IEEE1284_MODE_COMPAT;
1521
Anthony Liguori7267c092011-08-20 22:09:37 -05001522 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001523 chr->chr_write = null_chr_write;
1524 chr->chr_ioctl = pp_ioctl;
1525 chr->chr_close = pp_close;
1526 chr->opaque = drv;
1527
Amit Shah127338e2009-11-03 19:59:56 +05301528 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001529
Markus Armbruster1f514702012-02-07 15:09:08 +01001530 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001531}
1532#endif /* __linux__ */
1533
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001534#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001535
1536#define HAVE_CHARDEV_PARPORT 1
1537
blueswir16972f932008-11-22 20:49:12 +00001538static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1539{
Stefan Weile0efb992011-02-23 19:09:16 +01001540 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001541 uint8_t b;
1542
1543 switch(cmd) {
1544 case CHR_IOCTL_PP_READ_DATA:
1545 if (ioctl(fd, PPIGDATA, &b) < 0)
1546 return -ENOTSUP;
1547 *(uint8_t *)arg = b;
1548 break;
1549 case CHR_IOCTL_PP_WRITE_DATA:
1550 b = *(uint8_t *)arg;
1551 if (ioctl(fd, PPISDATA, &b) < 0)
1552 return -ENOTSUP;
1553 break;
1554 case CHR_IOCTL_PP_READ_CONTROL:
1555 if (ioctl(fd, PPIGCTRL, &b) < 0)
1556 return -ENOTSUP;
1557 *(uint8_t *)arg = b;
1558 break;
1559 case CHR_IOCTL_PP_WRITE_CONTROL:
1560 b = *(uint8_t *)arg;
1561 if (ioctl(fd, PPISCTRL, &b) < 0)
1562 return -ENOTSUP;
1563 break;
1564 case CHR_IOCTL_PP_READ_STATUS:
1565 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1566 return -ENOTSUP;
1567 *(uint8_t *)arg = b;
1568 break;
1569 default:
1570 return -ENOTSUP;
1571 }
1572 return 0;
1573}
1574
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001575static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001576{
1577 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001578
Anthony Liguori7267c092011-08-20 22:09:37 -05001579 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001580 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001581 chr->chr_write = null_chr_write;
1582 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001583 return chr;
blueswir16972f932008-11-22 20:49:12 +00001584}
1585#endif
1586
aliguori6f97dba2008-10-31 18:49:55 +00001587#else /* _WIN32 */
1588
1589typedef struct {
1590 int max_size;
1591 HANDLE hcom, hrecv, hsend;
1592 OVERLAPPED orecv, osend;
1593 BOOL fpipe;
1594 DWORD len;
1595} WinCharState;
1596
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001597typedef struct {
1598 HANDLE hStdIn;
1599 HANDLE hInputReadyEvent;
1600 HANDLE hInputDoneEvent;
1601 HANDLE hInputThread;
1602 uint8_t win_stdio_buf;
1603} WinStdioCharState;
1604
aliguori6f97dba2008-10-31 18:49:55 +00001605#define NSENDBUF 2048
1606#define NRECVBUF 2048
1607#define MAXCONNECT 1
1608#define NTIMEOUT 5000
1609
1610static int win_chr_poll(void *opaque);
1611static int win_chr_pipe_poll(void *opaque);
1612
1613static void win_chr_close(CharDriverState *chr)
1614{
1615 WinCharState *s = chr->opaque;
1616
1617 if (s->hsend) {
1618 CloseHandle(s->hsend);
1619 s->hsend = NULL;
1620 }
1621 if (s->hrecv) {
1622 CloseHandle(s->hrecv);
1623 s->hrecv = NULL;
1624 }
1625 if (s->hcom) {
1626 CloseHandle(s->hcom);
1627 s->hcom = NULL;
1628 }
1629 if (s->fpipe)
1630 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1631 else
1632 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301633
Hans de Goedea425d232011-11-19 10:22:43 +01001634 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001635}
1636
1637static int win_chr_init(CharDriverState *chr, const char *filename)
1638{
1639 WinCharState *s = chr->opaque;
1640 COMMCONFIG comcfg;
1641 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1642 COMSTAT comstat;
1643 DWORD size;
1644 DWORD err;
1645
1646 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1647 if (!s->hsend) {
1648 fprintf(stderr, "Failed CreateEvent\n");
1649 goto fail;
1650 }
1651 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1652 if (!s->hrecv) {
1653 fprintf(stderr, "Failed CreateEvent\n");
1654 goto fail;
1655 }
1656
1657 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1658 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1659 if (s->hcom == INVALID_HANDLE_VALUE) {
1660 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1661 s->hcom = NULL;
1662 goto fail;
1663 }
1664
1665 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1666 fprintf(stderr, "Failed SetupComm\n");
1667 goto fail;
1668 }
1669
1670 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1671 size = sizeof(COMMCONFIG);
1672 GetDefaultCommConfig(filename, &comcfg, &size);
1673 comcfg.dcb.DCBlength = sizeof(DCB);
1674 CommConfigDialog(filename, NULL, &comcfg);
1675
1676 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1677 fprintf(stderr, "Failed SetCommState\n");
1678 goto fail;
1679 }
1680
1681 if (!SetCommMask(s->hcom, EV_ERR)) {
1682 fprintf(stderr, "Failed SetCommMask\n");
1683 goto fail;
1684 }
1685
1686 cto.ReadIntervalTimeout = MAXDWORD;
1687 if (!SetCommTimeouts(s->hcom, &cto)) {
1688 fprintf(stderr, "Failed SetCommTimeouts\n");
1689 goto fail;
1690 }
1691
1692 if (!ClearCommError(s->hcom, &err, &comstat)) {
1693 fprintf(stderr, "Failed ClearCommError\n");
1694 goto fail;
1695 }
1696 qemu_add_polling_cb(win_chr_poll, chr);
1697 return 0;
1698
1699 fail:
1700 win_chr_close(chr);
1701 return -1;
1702}
1703
1704static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1705{
1706 WinCharState *s = chr->opaque;
1707 DWORD len, ret, size, err;
1708
1709 len = len1;
1710 ZeroMemory(&s->osend, sizeof(s->osend));
1711 s->osend.hEvent = s->hsend;
1712 while (len > 0) {
1713 if (s->hsend)
1714 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1715 else
1716 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1717 if (!ret) {
1718 err = GetLastError();
1719 if (err == ERROR_IO_PENDING) {
1720 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1721 if (ret) {
1722 buf += size;
1723 len -= size;
1724 } else {
1725 break;
1726 }
1727 } else {
1728 break;
1729 }
1730 } else {
1731 buf += size;
1732 len -= size;
1733 }
1734 }
1735 return len1 - len;
1736}
1737
1738static int win_chr_read_poll(CharDriverState *chr)
1739{
1740 WinCharState *s = chr->opaque;
1741
Anthony Liguori909cda12011-08-15 11:17:31 -05001742 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001743 return s->max_size;
1744}
1745
1746static void win_chr_readfile(CharDriverState *chr)
1747{
1748 WinCharState *s = chr->opaque;
1749 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301750 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001751 DWORD size;
1752
1753 ZeroMemory(&s->orecv, sizeof(s->orecv));
1754 s->orecv.hEvent = s->hrecv;
1755 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1756 if (!ret) {
1757 err = GetLastError();
1758 if (err == ERROR_IO_PENDING) {
1759 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1760 }
1761 }
1762
1763 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001764 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001765 }
1766}
1767
1768static void win_chr_read(CharDriverState *chr)
1769{
1770 WinCharState *s = chr->opaque;
1771
1772 if (s->len > s->max_size)
1773 s->len = s->max_size;
1774 if (s->len == 0)
1775 return;
1776
1777 win_chr_readfile(chr);
1778}
1779
1780static int win_chr_poll(void *opaque)
1781{
1782 CharDriverState *chr = opaque;
1783 WinCharState *s = chr->opaque;
1784 COMSTAT status;
1785 DWORD comerr;
1786
1787 ClearCommError(s->hcom, &comerr, &status);
1788 if (status.cbInQue > 0) {
1789 s->len = status.cbInQue;
1790 win_chr_read_poll(chr);
1791 win_chr_read(chr);
1792 return 1;
1793 }
1794 return 0;
1795}
1796
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001797static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001798{
1799 CharDriverState *chr;
1800 WinCharState *s;
1801
Anthony Liguori7267c092011-08-20 22:09:37 -05001802 chr = g_malloc0(sizeof(CharDriverState));
1803 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001804 chr->opaque = s;
1805 chr->chr_write = win_chr_write;
1806 chr->chr_close = win_chr_close;
1807
1808 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001809 g_free(s);
1810 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001811 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001812 }
Amit Shah127338e2009-11-03 19:59:56 +05301813 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001814 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001815}
1816
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001817static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1818{
1819 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1820}
1821
aliguori6f97dba2008-10-31 18:49:55 +00001822static int win_chr_pipe_poll(void *opaque)
1823{
1824 CharDriverState *chr = opaque;
1825 WinCharState *s = chr->opaque;
1826 DWORD size;
1827
1828 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1829 if (size > 0) {
1830 s->len = size;
1831 win_chr_read_poll(chr);
1832 win_chr_read(chr);
1833 return 1;
1834 }
1835 return 0;
1836}
1837
1838static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1839{
1840 WinCharState *s = chr->opaque;
1841 OVERLAPPED ov;
1842 int ret;
1843 DWORD size;
1844 char openname[256];
1845
1846 s->fpipe = TRUE;
1847
1848 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1849 if (!s->hsend) {
1850 fprintf(stderr, "Failed CreateEvent\n");
1851 goto fail;
1852 }
1853 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1854 if (!s->hrecv) {
1855 fprintf(stderr, "Failed CreateEvent\n");
1856 goto fail;
1857 }
1858
1859 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1860 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1861 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1862 PIPE_WAIT,
1863 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1864 if (s->hcom == INVALID_HANDLE_VALUE) {
1865 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1866 s->hcom = NULL;
1867 goto fail;
1868 }
1869
1870 ZeroMemory(&ov, sizeof(ov));
1871 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1872 ret = ConnectNamedPipe(s->hcom, &ov);
1873 if (ret) {
1874 fprintf(stderr, "Failed ConnectNamedPipe\n");
1875 goto fail;
1876 }
1877
1878 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1879 if (!ret) {
1880 fprintf(stderr, "Failed GetOverlappedResult\n");
1881 if (ov.hEvent) {
1882 CloseHandle(ov.hEvent);
1883 ov.hEvent = NULL;
1884 }
1885 goto fail;
1886 }
1887
1888 if (ov.hEvent) {
1889 CloseHandle(ov.hEvent);
1890 ov.hEvent = NULL;
1891 }
1892 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1893 return 0;
1894
1895 fail:
1896 win_chr_close(chr);
1897 return -1;
1898}
1899
1900
Markus Armbruster1f514702012-02-07 15:09:08 +01001901static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001902{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001903 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001904 CharDriverState *chr;
1905 WinCharState *s;
1906
Anthony Liguori7267c092011-08-20 22:09:37 -05001907 chr = g_malloc0(sizeof(CharDriverState));
1908 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001909 chr->opaque = s;
1910 chr->chr_write = win_chr_write;
1911 chr->chr_close = win_chr_close;
1912
1913 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001914 g_free(s);
1915 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001916 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001917 }
Amit Shah127338e2009-11-03 19:59:56 +05301918 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001919 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001920}
1921
Markus Armbruster1f514702012-02-07 15:09:08 +01001922static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001923{
1924 CharDriverState *chr;
1925 WinCharState *s;
1926
Anthony Liguori7267c092011-08-20 22:09:37 -05001927 chr = g_malloc0(sizeof(CharDriverState));
1928 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001929 s->hcom = fd_out;
1930 chr->opaque = s;
1931 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301932 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001933 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001934}
1935
Markus Armbruster1f514702012-02-07 15:09:08 +01001936static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001937{
Markus Armbruster1f514702012-02-07 15:09:08 +01001938 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001939}
1940
Markus Armbruster1f514702012-02-07 15:09:08 +01001941static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001942{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001943 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001944 HANDLE fd_out;
1945
1946 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1947 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001948 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001949 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001950 }
aliguori6f97dba2008-10-31 18:49:55 +00001951
Markus Armbruster1f514702012-02-07 15:09:08 +01001952 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001953}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001954
1955static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1956{
1957 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1958 DWORD dwSize;
1959 int len1;
1960
1961 len1 = len;
1962
1963 while (len1 > 0) {
1964 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1965 break;
1966 }
1967 buf += dwSize;
1968 len1 -= dwSize;
1969 }
1970
1971 return len - len1;
1972}
1973
1974static void win_stdio_wait_func(void *opaque)
1975{
1976 CharDriverState *chr = opaque;
1977 WinStdioCharState *stdio = chr->opaque;
1978 INPUT_RECORD buf[4];
1979 int ret;
1980 DWORD dwSize;
1981 int i;
1982
1983 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1984 &dwSize);
1985
1986 if (!ret) {
1987 /* Avoid error storm */
1988 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1989 return;
1990 }
1991
1992 for (i = 0; i < dwSize; i++) {
1993 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1994
1995 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1996 int j;
1997 if (kev->uChar.AsciiChar != 0) {
1998 for (j = 0; j < kev->wRepeatCount; j++) {
1999 if (qemu_chr_be_can_write(chr)) {
2000 uint8_t c = kev->uChar.AsciiChar;
2001 qemu_chr_be_write(chr, &c, 1);
2002 }
2003 }
2004 }
2005 }
2006 }
2007}
2008
2009static DWORD WINAPI win_stdio_thread(LPVOID param)
2010{
2011 CharDriverState *chr = param;
2012 WinStdioCharState *stdio = chr->opaque;
2013 int ret;
2014 DWORD dwSize;
2015
2016 while (1) {
2017
2018 /* Wait for one byte */
2019 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2020
2021 /* Exit in case of error, continue if nothing read */
2022 if (!ret) {
2023 break;
2024 }
2025 if (!dwSize) {
2026 continue;
2027 }
2028
2029 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2030 if (stdio->win_stdio_buf == '\r') {
2031 continue;
2032 }
2033
2034 /* Signal the main thread and wait until the byte was eaten */
2035 if (!SetEvent(stdio->hInputReadyEvent)) {
2036 break;
2037 }
2038 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2039 != WAIT_OBJECT_0) {
2040 break;
2041 }
2042 }
2043
2044 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2045 return 0;
2046}
2047
2048static void win_stdio_thread_wait_func(void *opaque)
2049{
2050 CharDriverState *chr = opaque;
2051 WinStdioCharState *stdio = chr->opaque;
2052
2053 if (qemu_chr_be_can_write(chr)) {
2054 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2055 }
2056
2057 SetEvent(stdio->hInputDoneEvent);
2058}
2059
2060static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2061{
2062 WinStdioCharState *stdio = chr->opaque;
2063 DWORD dwMode = 0;
2064
2065 GetConsoleMode(stdio->hStdIn, &dwMode);
2066
2067 if (echo) {
2068 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2069 } else {
2070 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2071 }
2072}
2073
2074static void win_stdio_close(CharDriverState *chr)
2075{
2076 WinStdioCharState *stdio = chr->opaque;
2077
2078 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2079 CloseHandle(stdio->hInputReadyEvent);
2080 }
2081 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2082 CloseHandle(stdio->hInputDoneEvent);
2083 }
2084 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2085 TerminateThread(stdio->hInputThread, 0);
2086 }
2087
2088 g_free(chr->opaque);
2089 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002090}
2091
Markus Armbruster1f514702012-02-07 15:09:08 +01002092static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002093{
2094 CharDriverState *chr;
2095 WinStdioCharState *stdio;
2096 DWORD dwMode;
2097 int is_console = 0;
2098
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002099 chr = g_malloc0(sizeof(CharDriverState));
2100 stdio = g_malloc0(sizeof(WinStdioCharState));
2101
2102 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2103 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2104 fprintf(stderr, "cannot open stdio: invalid handle\n");
2105 exit(1);
2106 }
2107
2108 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2109
2110 chr->opaque = stdio;
2111 chr->chr_write = win_stdio_write;
2112 chr->chr_close = win_stdio_close;
2113
Anthony Liguoried7a1542013-03-05 23:21:17 +05302114 if (is_console) {
2115 if (qemu_add_wait_object(stdio->hStdIn,
2116 win_stdio_wait_func, chr)) {
2117 fprintf(stderr, "qemu_add_wait_object: failed\n");
2118 }
2119 } else {
2120 DWORD dwId;
2121
2122 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2123 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2124 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2125 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002126
Anthony Liguoried7a1542013-03-05 23:21:17 +05302127 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2128 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2129 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2130 fprintf(stderr, "cannot create stdio thread or event\n");
2131 exit(1);
2132 }
2133 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2134 win_stdio_thread_wait_func, chr)) {
2135 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002136 }
2137 }
2138
2139 dwMode |= ENABLE_LINE_INPUT;
2140
Anthony Liguoried7a1542013-03-05 23:21:17 +05302141 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002142 /* set the terminal in raw mode */
2143 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2144 dwMode |= ENABLE_PROCESSED_INPUT;
2145 }
2146
2147 SetConsoleMode(stdio->hStdIn, dwMode);
2148
2149 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2150 qemu_chr_fe_set_echo(chr, false);
2151
Markus Armbruster1f514702012-02-07 15:09:08 +01002152 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002153}
aliguori6f97dba2008-10-31 18:49:55 +00002154#endif /* !_WIN32 */
2155
2156/***********************************************************/
2157/* UDP Net console */
2158
2159typedef struct {
2160 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302161 GIOChannel *chan;
2162 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302163 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002164 int bufcnt;
2165 int bufptr;
2166 int max_size;
2167} NetCharDriver;
2168
2169static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2170{
2171 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302172 gsize bytes_written;
2173 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002174
Anthony Liguori76a96442013-03-05 23:21:21 +05302175 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2176 if (status == G_IO_STATUS_EOF) {
2177 return 0;
2178 } else if (status != G_IO_STATUS_NORMAL) {
2179 return -1;
2180 }
2181
2182 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002183}
2184
2185static int udp_chr_read_poll(void *opaque)
2186{
2187 CharDriverState *chr = opaque;
2188 NetCharDriver *s = chr->opaque;
2189
Anthony Liguori909cda12011-08-15 11:17:31 -05002190 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002191
2192 /* If there were any stray characters in the queue process them
2193 * first
2194 */
2195 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002196 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002197 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002198 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002199 }
2200 return s->max_size;
2201}
2202
Anthony Liguori76a96442013-03-05 23:21:21 +05302203static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002204{
2205 CharDriverState *chr = opaque;
2206 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302207 gsize bytes_read = 0;
2208 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002209
2210 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302211 return FALSE;
2212 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2213 &bytes_read, NULL);
2214 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002215 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302216 if (status != G_IO_STATUS_NORMAL) {
2217 return FALSE;
2218 }
aliguori6f97dba2008-10-31 18:49:55 +00002219
2220 s->bufptr = 0;
2221 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002222 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002223 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002224 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002225 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302226
2227 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002228}
2229
2230static void udp_chr_update_read_handler(CharDriverState *chr)
2231{
2232 NetCharDriver *s = chr->opaque;
2233
Anthony Liguori76a96442013-03-05 23:21:21 +05302234 if (s->tag) {
2235 g_source_remove(s->tag);
2236 s->tag = 0;
2237 }
2238
2239 if (s->chan) {
2240 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002241 }
2242}
2243
aliguori819f56b2009-03-28 17:58:14 +00002244static void udp_chr_close(CharDriverState *chr)
2245{
2246 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302247 if (s->tag) {
2248 g_source_remove(s->tag);
2249 }
2250 if (s->chan) {
2251 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002252 closesocket(s->fd);
2253 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002254 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002255 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002256}
2257
Markus Armbruster1f514702012-02-07 15:09:08 +01002258static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002259{
2260 CharDriverState *chr = NULL;
2261 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002262 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002263 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002264
Anthony Liguori7267c092011-08-20 22:09:37 -05002265 chr = g_malloc0(sizeof(CharDriverState));
2266 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002267
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002268 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002269 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002270 goto return_err;
2271 }
2272
2273 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302274 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002275 s->bufcnt = 0;
2276 s->bufptr = 0;
2277 chr->opaque = s;
2278 chr->chr_write = udp_chr_write;
2279 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002280 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002281 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002282
2283return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002284 if (local_err) {
2285 qerror_report_err(local_err);
2286 error_free(local_err);
2287 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002288 g_free(chr);
2289 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002290 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002291 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002292 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002293 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002294}
2295
2296/***********************************************************/
2297/* TCP Net console */
2298
2299typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302300
2301 GIOChannel *chan, *listen_chan;
2302 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002303 int fd, listen_fd;
2304 int connected;
2305 int max_size;
2306 int do_telnetopt;
2307 int do_nodelay;
2308 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002309 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002310} TCPCharDriver;
2311
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302312static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002313
2314static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2315{
2316 TCPCharDriver *s = chr->opaque;
2317 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302318 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002319 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002320 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002321 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002322 }
2323}
2324
2325static int tcp_chr_read_poll(void *opaque)
2326{
2327 CharDriverState *chr = opaque;
2328 TCPCharDriver *s = chr->opaque;
2329 if (!s->connected)
2330 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002331 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002332 return s->max_size;
2333}
2334
2335#define IAC 255
2336#define IAC_BREAK 243
2337static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2338 TCPCharDriver *s,
2339 uint8_t *buf, int *size)
2340{
2341 /* Handle any telnet client's basic IAC options to satisfy char by
2342 * char mode with no echo. All IAC options will be removed from
2343 * the buf and the do_telnetopt variable will be used to track the
2344 * state of the width of the IAC information.
2345 *
2346 * IAC commands come in sets of 3 bytes with the exception of the
2347 * "IAC BREAK" command and the double IAC.
2348 */
2349
2350 int i;
2351 int j = 0;
2352
2353 for (i = 0; i < *size; i++) {
2354 if (s->do_telnetopt > 1) {
2355 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2356 /* Double IAC means send an IAC */
2357 if (j != i)
2358 buf[j] = buf[i];
2359 j++;
2360 s->do_telnetopt = 1;
2361 } else {
2362 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2363 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002364 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002365 s->do_telnetopt++;
2366 }
2367 s->do_telnetopt++;
2368 }
2369 if (s->do_telnetopt >= 4) {
2370 s->do_telnetopt = 1;
2371 }
2372 } else {
2373 if ((unsigned char)buf[i] == IAC) {
2374 s->do_telnetopt = 2;
2375 } else {
2376 if (j != i)
2377 buf[j] = buf[i];
2378 j++;
2379 }
2380 }
2381 }
2382 *size = j;
2383}
2384
Mark McLoughlin7d174052009-07-22 09:11:39 +01002385static int tcp_get_msgfd(CharDriverState *chr)
2386{
2387 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002388 int fd = s->msgfd;
2389 s->msgfd = -1;
2390 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002391}
2392
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002393#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002394static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2395{
2396 TCPCharDriver *s = chr->opaque;
2397 struct cmsghdr *cmsg;
2398
2399 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2400 int fd;
2401
2402 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2403 cmsg->cmsg_level != SOL_SOCKET ||
2404 cmsg->cmsg_type != SCM_RIGHTS)
2405 continue;
2406
2407 fd = *((int *)CMSG_DATA(cmsg));
2408 if (fd < 0)
2409 continue;
2410
Corey Bryant06138652012-08-14 16:43:42 -04002411#ifndef MSG_CMSG_CLOEXEC
2412 qemu_set_cloexec(fd);
2413#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002414 if (s->msgfd != -1)
2415 close(s->msgfd);
2416 s->msgfd = fd;
2417 }
2418}
2419
Mark McLoughlin9977c892009-07-22 09:11:38 +01002420static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2421{
2422 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002423 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002424 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002425 union {
2426 struct cmsghdr cmsg;
2427 char control[CMSG_SPACE(sizeof(int))];
2428 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002429 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002430 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002431
2432 iov[0].iov_base = buf;
2433 iov[0].iov_len = len;
2434
2435 msg.msg_iov = iov;
2436 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002437 msg.msg_control = &msg_control;
2438 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002439
Corey Bryant06138652012-08-14 16:43:42 -04002440#ifdef MSG_CMSG_CLOEXEC
2441 flags |= MSG_CMSG_CLOEXEC;
2442#endif
2443 ret = recvmsg(s->fd, &msg, flags);
2444 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002445 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002446 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002447
2448 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002449}
2450#else
2451static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2452{
2453 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002454 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002455}
2456#endif
2457
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302458static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002459{
2460 CharDriverState *chr = opaque;
2461 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302462 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002463 int len, size;
2464
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302465 if (!s->connected || s->max_size <= 0) {
2466 return FALSE;
2467 }
aliguori6f97dba2008-10-31 18:49:55 +00002468 len = sizeof(buf);
2469 if (len > s->max_size)
2470 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002471 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002472 if (size == 0) {
2473 /* connection closed */
2474 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302475 if (s->listen_chan) {
2476 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002477 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302478 g_source_remove(s->tag);
2479 s->tag = 0;
2480 g_io_channel_unref(s->chan);
2481 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002482 closesocket(s->fd);
2483 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002484 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002485 } else if (size > 0) {
2486 if (s->do_telnetopt)
2487 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2488 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002489 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002490 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302491
2492 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002493}
2494
Blue Swirl68c18d12010-08-15 09:46:24 +00002495#ifndef _WIN32
2496CharDriverState *qemu_chr_open_eventfd(int eventfd)
2497{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002498 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002499}
Blue Swirl68c18d12010-08-15 09:46:24 +00002500#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002501
aliguori6f97dba2008-10-31 18:49:55 +00002502static void tcp_chr_connect(void *opaque)
2503{
2504 CharDriverState *chr = opaque;
2505 TCPCharDriver *s = chr->opaque;
2506
2507 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302508 if (s->chan) {
2509 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002510 }
Amit Shah127338e2009-11-03 19:59:56 +05302511 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002512}
2513
2514#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2515static void tcp_chr_telnet_init(int fd)
2516{
2517 char buf[3];
2518 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2519 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2520 send(fd, (char *)buf, 3, 0);
2521 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2522 send(fd, (char *)buf, 3, 0);
2523 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2524 send(fd, (char *)buf, 3, 0);
2525 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2526 send(fd, (char *)buf, 3, 0);
2527}
2528
Daniel P. Berrange13661082011-06-23 13:31:42 +01002529static int tcp_chr_add_client(CharDriverState *chr, int fd)
2530{
2531 TCPCharDriver *s = chr->opaque;
2532 if (s->fd != -1)
2533 return -1;
2534
2535 socket_set_nonblock(fd);
2536 if (s->do_nodelay)
2537 socket_set_nodelay(fd);
2538 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302539 s->chan = io_channel_from_socket(fd);
2540 g_source_remove(s->listen_tag);
2541 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002542 tcp_chr_connect(chr);
2543
2544 return 0;
2545}
2546
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302547static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002548{
2549 CharDriverState *chr = opaque;
2550 TCPCharDriver *s = chr->opaque;
2551 struct sockaddr_in saddr;
2552#ifndef _WIN32
2553 struct sockaddr_un uaddr;
2554#endif
2555 struct sockaddr *addr;
2556 socklen_t len;
2557 int fd;
2558
2559 for(;;) {
2560#ifndef _WIN32
2561 if (s->is_unix) {
2562 len = sizeof(uaddr);
2563 addr = (struct sockaddr *)&uaddr;
2564 } else
2565#endif
2566 {
2567 len = sizeof(saddr);
2568 addr = (struct sockaddr *)&saddr;
2569 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002570 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002571 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302572 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002573 } else if (fd >= 0) {
2574 if (s->do_telnetopt)
2575 tcp_chr_telnet_init(fd);
2576 break;
2577 }
2578 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002579 if (tcp_chr_add_client(chr, fd) < 0)
2580 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302581
2582 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002583}
2584
2585static void tcp_chr_close(CharDriverState *chr)
2586{
2587 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002588 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302589 if (s->tag) {
2590 g_source_remove(s->tag);
2591 }
2592 if (s->chan) {
2593 g_io_channel_unref(s->chan);
2594 }
aliguori6f97dba2008-10-31 18:49:55 +00002595 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002596 }
2597 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302598 if (s->listen_tag) {
2599 g_source_remove(s->listen_tag);
2600 }
2601 if (s->listen_chan) {
2602 g_io_channel_unref(s->listen_chan);
2603 }
aliguori6f97dba2008-10-31 18:49:55 +00002604 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002605 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002606 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002607 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002608}
2609
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002610static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2611 bool is_listen, bool is_telnet,
2612 bool is_waitconnect,
2613 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002614{
2615 CharDriverState *chr = NULL;
2616 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002617 char host[NI_MAXHOST], serv[NI_MAXSERV];
2618 const char *left = "", *right = "";
2619 struct sockaddr_storage ss;
2620 socklen_t ss_len = sizeof(ss);
2621
2622 memset(&ss, 0, ss_len);
2623 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2624 error_setg(errp, "getsockname: %s", strerror(errno));
2625 return NULL;
2626 }
2627
2628 chr = g_malloc0(sizeof(CharDriverState));
2629 s = g_malloc0(sizeof(TCPCharDriver));
2630
2631 s->connected = 0;
2632 s->fd = -1;
2633 s->listen_fd = -1;
2634 s->msgfd = -1;
2635
2636 chr->filename = g_malloc(256);
2637 switch (ss.ss_family) {
2638#ifndef _WIN32
2639 case AF_UNIX:
2640 s->is_unix = 1;
2641 snprintf(chr->filename, 256, "unix:%s%s",
2642 ((struct sockaddr_un *)(&ss))->sun_path,
2643 is_listen ? ",server" : "");
2644 break;
2645#endif
2646 case AF_INET6:
2647 left = "[";
2648 right = "]";
2649 /* fall through */
2650 case AF_INET:
2651 s->do_nodelay = do_nodelay;
2652 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2653 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2654 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2655 is_telnet ? "telnet" : "tcp",
2656 left, host, right, serv,
2657 is_listen ? ",server" : "");
2658 break;
2659 }
2660
2661 chr->opaque = s;
2662 chr->chr_write = tcp_chr_write;
2663 chr->chr_close = tcp_chr_close;
2664 chr->get_msgfd = tcp_get_msgfd;
2665 chr->chr_add_client = tcp_chr_add_client;
2666
2667 if (is_listen) {
2668 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302669 s->listen_chan = io_channel_from_socket(s->listen_fd);
2670 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002671 if (is_telnet) {
2672 s->do_telnetopt = 1;
2673 }
2674 } else {
2675 s->connected = 1;
2676 s->fd = fd;
2677 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302678 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002679 tcp_chr_connect(chr);
2680 }
2681
2682 if (is_listen && is_waitconnect) {
2683 printf("QEMU waiting for connection on: %s\n",
2684 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302685 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002686 socket_set_nonblock(s->listen_fd);
2687 }
2688 return chr;
2689}
2690
2691static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2692{
2693 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002694 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002695 int fd = -1;
2696 int is_listen;
2697 int is_waitconnect;
2698 int do_nodelay;
2699 int is_unix;
2700 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002701
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002702 is_listen = qemu_opt_get_bool(opts, "server", 0);
2703 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2704 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2705 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2706 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002707 if (!is_listen)
2708 is_waitconnect = 0;
2709
aliguorif07b6002008-11-11 20:54:09 +00002710 if (is_unix) {
2711 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002712 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002713 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002714 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002715 }
2716 } else {
2717 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002718 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002719 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002720 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002721 }
2722 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002723 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002724 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002725 }
aliguori6f97dba2008-10-31 18:49:55 +00002726
2727 if (!is_waitconnect)
2728 socket_set_nonblock(fd);
2729
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002730 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2731 is_waitconnect, &local_err);
2732 if (error_is_set(&local_err)) {
2733 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002734 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002735 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002736
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002737
aliguori6f97dba2008-10-31 18:49:55 +00002738 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002739 if (local_err) {
2740 qerror_report_err(local_err);
2741 error_free(local_err);
2742 }
2743 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002744 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002745 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002746 if (chr) {
2747 g_free(chr->opaque);
2748 g_free(chr);
2749 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002750 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002751}
2752
Luiz Capitulino999bd672010-10-22 16:09:05 -02002753/***********************************************************/
2754/* Memory chardev */
2755typedef struct {
2756 size_t outbuf_size;
2757 size_t outbuf_capacity;
2758 uint8_t *outbuf;
2759} MemoryDriver;
2760
2761static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2762{
2763 MemoryDriver *d = chr->opaque;
2764
2765 /* TODO: the QString implementation has the same code, we should
2766 * introduce a generic way to do this in cutils.c */
2767 if (d->outbuf_capacity < d->outbuf_size + len) {
2768 /* grow outbuf */
2769 d->outbuf_capacity += len;
2770 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002771 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002772 }
2773
2774 memcpy(d->outbuf + d->outbuf_size, buf, len);
2775 d->outbuf_size += len;
2776
2777 return len;
2778}
2779
2780void qemu_chr_init_mem(CharDriverState *chr)
2781{
2782 MemoryDriver *d;
2783
Anthony Liguori7267c092011-08-20 22:09:37 -05002784 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002785 d->outbuf_size = 0;
2786 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002787 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002788
2789 memset(chr, 0, sizeof(*chr));
2790 chr->opaque = d;
2791 chr->chr_write = mem_chr_write;
2792}
2793
2794QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2795{
2796 MemoryDriver *d = chr->opaque;
2797 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2798}
2799
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002800/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002801void qemu_chr_close_mem(CharDriverState *chr)
2802{
2803 MemoryDriver *d = chr->opaque;
2804
Anthony Liguori7267c092011-08-20 22:09:37 -05002805 g_free(d->outbuf);
2806 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002807 chr->opaque = NULL;
2808 chr->chr_write = NULL;
2809}
2810
2811size_t qemu_chr_mem_osize(const CharDriverState *chr)
2812{
2813 const MemoryDriver *d = chr->opaque;
2814 return d->outbuf_size;
2815}
2816
Lei Li51767e72013-01-25 00:03:19 +08002817/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002818/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002819
2820typedef struct {
2821 size_t size;
2822 size_t prod;
2823 size_t cons;
2824 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002825} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002826
Markus Armbruster3949e592013-02-06 21:27:24 +01002827static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002828{
Markus Armbruster3949e592013-02-06 21:27:24 +01002829 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002830
Markus Armbruster5c230102013-02-06 21:27:23 +01002831 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002832}
2833
Markus Armbruster3949e592013-02-06 21:27:24 +01002834static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002835{
Markus Armbruster3949e592013-02-06 21:27:24 +01002836 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002837 int i;
2838
2839 if (!buf || (len < 0)) {
2840 return -1;
2841 }
2842
2843 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002844 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2845 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002846 d->cons = d->prod - d->size;
2847 }
2848 }
2849
2850 return 0;
2851}
2852
Markus Armbruster3949e592013-02-06 21:27:24 +01002853static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002854{
Markus Armbruster3949e592013-02-06 21:27:24 +01002855 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002856 int i;
2857
Markus Armbruster5c230102013-02-06 21:27:23 +01002858 for (i = 0; i < len && d->cons != d->prod; i++) {
2859 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002860 }
2861
2862 return i;
2863}
2864
Markus Armbruster3949e592013-02-06 21:27:24 +01002865static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002866{
Markus Armbruster3949e592013-02-06 21:27:24 +01002867 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002868
2869 g_free(d->cbuf);
2870 g_free(d);
2871 chr->opaque = NULL;
2872}
2873
Markus Armbruster3949e592013-02-06 21:27:24 +01002874static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002875{
2876 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002877 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002878
2879 chr = g_malloc0(sizeof(CharDriverState));
2880 d = g_malloc(sizeof(*d));
2881
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002882 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002883 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002884 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002885 }
2886
2887 /* The size must be power of 2 */
2888 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002889 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002890 goto fail;
2891 }
2892
2893 d->prod = 0;
2894 d->cons = 0;
2895 d->cbuf = g_malloc0(d->size);
2896
2897 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002898 chr->chr_write = ringbuf_chr_write;
2899 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002900
2901 return chr;
2902
2903fail:
2904 g_free(d);
2905 g_free(chr);
2906 return NULL;
2907}
2908
Markus Armbruster3949e592013-02-06 21:27:24 +01002909static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002910{
Markus Armbruster3949e592013-02-06 21:27:24 +01002911 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002912}
2913
Markus Armbruster3949e592013-02-06 21:27:24 +01002914void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002915 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002916 Error **errp)
2917{
2918 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002919 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002920 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002921 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002922
2923 chr = qemu_chr_find(device);
2924 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002925 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002926 return;
2927 }
2928
Markus Armbruster3949e592013-02-06 21:27:24 +01002929 if (!chr_is_ringbuf(chr)) {
2930 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002931 return;
2932 }
2933
Lei Li1f590cf2013-01-25 00:03:20 +08002934 if (has_format && (format == DATA_FORMAT_BASE64)) {
2935 write_data = g_base64_decode(data, &write_count);
2936 } else {
2937 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002938 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002939 }
2940
Markus Armbruster3949e592013-02-06 21:27:24 +01002941 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002942
Markus Armbruster13289fb2013-02-06 21:27:18 +01002943 if (write_data != (uint8_t *)data) {
2944 g_free((void *)write_data);
2945 }
2946
Lei Li1f590cf2013-01-25 00:03:20 +08002947 if (ret < 0) {
2948 error_setg(errp, "Failed to write to device %s", device);
2949 return;
2950 }
2951}
2952
Markus Armbruster3949e592013-02-06 21:27:24 +01002953char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002954 bool has_format, enum DataFormat format,
2955 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002956{
2957 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002958 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002959 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002960 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002961
2962 chr = qemu_chr_find(device);
2963 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002964 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002965 return NULL;
2966 }
2967
Markus Armbruster3949e592013-02-06 21:27:24 +01002968 if (!chr_is_ringbuf(chr)) {
2969 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002970 return NULL;
2971 }
2972
2973 if (size <= 0) {
2974 error_setg(errp, "size must be greater than zero");
2975 return NULL;
2976 }
2977
Markus Armbruster3949e592013-02-06 21:27:24 +01002978 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002979 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002980 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002981
Markus Armbruster3949e592013-02-06 21:27:24 +01002982 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002983
2984 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002985 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002986 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002987 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002988 /*
2989 * FIXME should read only complete, valid UTF-8 characters up
2990 * to @size bytes. Invalid sequences should be replaced by a
2991 * suitable replacement character. Except when (and only
2992 * when) ring buffer lost characters since last read, initial
2993 * continuation characters should be dropped.
2994 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002995 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002996 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002997 }
2998
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002999 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003000}
3001
Gerd Hoffmann33521632009-12-08 13:11:49 +01003002QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003003{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003004 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003005 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003006 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003007 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003008 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003009
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003010 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3011 if (error_is_set(&local_err)) {
3012 qerror_report_err(local_err);
3013 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003014 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003015 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003016
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003017 if (strstart(filename, "mon:", &p)) {
3018 filename = p;
3019 qemu_opt_set(opts, "mux", "on");
3020 }
3021
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003022 if (strcmp(filename, "null") == 0 ||
3023 strcmp(filename, "pty") == 0 ||
3024 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003025 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003026 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003027 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003028 return opts;
3029 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003030 if (strstart(filename, "vc", &p)) {
3031 qemu_opt_set(opts, "backend", "vc");
3032 if (*p == ':') {
3033 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3034 /* pixels */
3035 qemu_opt_set(opts, "width", width);
3036 qemu_opt_set(opts, "height", height);
3037 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3038 /* chars */
3039 qemu_opt_set(opts, "cols", width);
3040 qemu_opt_set(opts, "rows", height);
3041 } else {
3042 goto fail;
3043 }
3044 }
3045 return opts;
3046 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003047 if (strcmp(filename, "con:") == 0) {
3048 qemu_opt_set(opts, "backend", "console");
3049 return opts;
3050 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003051 if (strstart(filename, "COM", NULL)) {
3052 qemu_opt_set(opts, "backend", "serial");
3053 qemu_opt_set(opts, "path", filename);
3054 return opts;
3055 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003056 if (strstart(filename, "file:", &p)) {
3057 qemu_opt_set(opts, "backend", "file");
3058 qemu_opt_set(opts, "path", p);
3059 return opts;
3060 }
3061 if (strstart(filename, "pipe:", &p)) {
3062 qemu_opt_set(opts, "backend", "pipe");
3063 qemu_opt_set(opts, "path", p);
3064 return opts;
3065 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003066 if (strstart(filename, "tcp:", &p) ||
3067 strstart(filename, "telnet:", &p)) {
3068 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3069 host[0] = 0;
3070 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3071 goto fail;
3072 }
3073 qemu_opt_set(opts, "backend", "socket");
3074 qemu_opt_set(opts, "host", host);
3075 qemu_opt_set(opts, "port", port);
3076 if (p[pos] == ',') {
3077 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3078 goto fail;
3079 }
3080 if (strstart(filename, "telnet:", &p))
3081 qemu_opt_set(opts, "telnet", "on");
3082 return opts;
3083 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003084 if (strstart(filename, "udp:", &p)) {
3085 qemu_opt_set(opts, "backend", "udp");
3086 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3087 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003088 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003089 goto fail;
3090 }
3091 }
3092 qemu_opt_set(opts, "host", host);
3093 qemu_opt_set(opts, "port", port);
3094 if (p[pos] == '@') {
3095 p += pos + 1;
3096 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3097 host[0] = 0;
3098 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003099 goto fail;
3100 }
3101 }
3102 qemu_opt_set(opts, "localaddr", host);
3103 qemu_opt_set(opts, "localport", port);
3104 }
3105 return opts;
3106 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003107 if (strstart(filename, "unix:", &p)) {
3108 qemu_opt_set(opts, "backend", "socket");
3109 if (qemu_opts_do_parse(opts, p, "path") != 0)
3110 goto fail;
3111 return opts;
3112 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003113 if (strstart(filename, "/dev/parport", NULL) ||
3114 strstart(filename, "/dev/ppi", NULL)) {
3115 qemu_opt_set(opts, "backend", "parport");
3116 qemu_opt_set(opts, "path", filename);
3117 return opts;
3118 }
3119 if (strstart(filename, "/dev/", NULL)) {
3120 qemu_opt_set(opts, "backend", "tty");
3121 qemu_opt_set(opts, "path", filename);
3122 return opts;
3123 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003124
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003125fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003126 qemu_opts_del(opts);
3127 return NULL;
3128}
3129
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003130#ifdef HAVE_CHARDEV_PARPORT
3131
3132static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3133{
3134 const char *filename = qemu_opt_get(opts, "path");
3135 int fd;
3136
3137 fd = qemu_open(filename, O_RDWR);
3138 if (fd < 0) {
3139 return NULL;
3140 }
3141 return qemu_chr_open_pp_fd(fd);
3142}
3143
3144#endif
3145
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003146static const struct {
3147 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01003148 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003149} backend_table[] = {
3150 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003151 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003152 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003153 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Anthony Liguorid82831d2013-02-20 07:43:19 -06003154 { .name = "vc", .open = vc_init },
Markus Armbruster3949e592013-02-06 21:27:24 +01003155 { .name = "memory", .open = qemu_chr_open_ringbuf },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003156#ifdef _WIN32
3157 { .name = "file", .open = qemu_chr_open_win_file_out },
3158 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003159 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003160 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02003161 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003162#else
3163 { .name = "file", .open = qemu_chr_open_file_out },
3164 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02003165 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003166#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003167#ifdef CONFIG_BRLAPI
3168 { .name = "braille", .open = chr_baum_init },
3169#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003170#ifdef HAVE_CHARDEV_TTY
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003171 { .name = "tty", .open = qemu_chr_open_tty },
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003172 { .name = "serial", .open = qemu_chr_open_tty },
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003173 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003174#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003175#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003176 { .name = "parallel", .open = qemu_chr_open_pp },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003177 { .name = "parport", .open = qemu_chr_open_pp },
3178#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003179#ifdef CONFIG_SPICE
3180 { .name = "spicevmc", .open = qemu_chr_open_spice },
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01003181#if SPICE_SERVER_VERSION >= 0x000c02
3182 { .name = "spiceport", .open = qemu_chr_open_spice_port },
3183#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003184#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003185};
3186
Anthony Liguorif69554b2011-08-15 11:17:37 -05003187CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003188 void (*init)(struct CharDriverState *s),
3189 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003190{
3191 CharDriverState *chr;
3192 int i;
3193
3194 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003195 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003196 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003197 }
3198
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003199 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003200 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003201 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003202 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003203 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003204 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
3205 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
3206 break;
3207 }
3208 if (i == ARRAY_SIZE(backend_table)) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003209 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003210 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003211 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003212 }
3213
Markus Armbruster1f514702012-02-07 15:09:08 +01003214 chr = backend_table[i].open(opts);
3215 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003216 error_setg(errp, "chardev: opening backend \"%s\" failed",
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
3221 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003222 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003223 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003224 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003225
3226 if (qemu_opt_get_bool(opts, "mux", 0)) {
3227 CharDriverState *base = chr;
3228 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003229 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003230 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3231 chr = qemu_chr_open_mux(base);
3232 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003233 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003234 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003235 } else {
3236 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003237 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003238 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003239 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003240 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003241
3242err:
3243 qemu_opts_del(opts);
3244 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003245}
3246
Anthony Liguori27143a42011-08-15 11:17:36 -05003247CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003248{
3249 const char *p;
3250 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003251 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003252 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003253
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003254 if (strstart(filename, "chardev:", &p)) {
3255 return qemu_chr_find(p);
3256 }
3257
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003258 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003259 if (!opts)
3260 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003261
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003262 chr = qemu_chr_new_from_opts(opts, init, &err);
3263 if (error_is_set(&err)) {
3264 fprintf(stderr, "%s\n", error_get_pretty(err));
3265 error_free(err);
3266 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003267 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3268 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003269 }
3270 return chr;
3271}
3272
Anthony Liguori15f31512011-08-15 11:17:35 -05003273void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003274{
3275 if (chr->chr_set_echo) {
3276 chr->chr_set_echo(chr, echo);
3277 }
3278}
3279
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003280void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003281{
3282 if (chr->chr_guest_open) {
3283 chr->chr_guest_open(chr);
3284 }
3285}
3286
Anthony Liguori28178222011-08-15 11:17:33 -05003287void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003288{
3289 if (chr->chr_guest_close) {
3290 chr->chr_guest_close(chr);
3291 }
3292}
3293
Anthony Liguori23673ca2013-03-05 23:21:23 +05303294guint qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3295 GIOFunc func, void *user_data)
3296{
3297 GSource *src;
3298 guint tag;
3299
3300 if (s->chr_add_watch == NULL) {
3301 return -ENOSYS;
3302 }
3303
3304 src = s->chr_add_watch(s, cond);
3305 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3306 tag = g_source_attach(src, NULL);
3307 g_source_unref(src);
3308
3309 return tag;
3310}
3311
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003312void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003313{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003314 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003315 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003316 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003317 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003318 g_free(chr->filename);
3319 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003320 if (chr->opts) {
3321 qemu_opts_del(chr->opts);
3322 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003323 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003324}
3325
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003326ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003327{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003328 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003329 CharDriverState *chr;
3330
Blue Swirl72cf2d42009-09-12 07:36:22 +00003331 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003332 ChardevInfoList *info = g_malloc0(sizeof(*info));
3333 info->value = g_malloc0(sizeof(*info->value));
3334 info->value->label = g_strdup(chr->label);
3335 info->value->filename = g_strdup(chr->filename);
3336
3337 info->next = chr_list;
3338 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003339 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003340
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003341 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003342}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003343
3344CharDriverState *qemu_chr_find(const char *name)
3345{
3346 CharDriverState *chr;
3347
Blue Swirl72cf2d42009-09-12 07:36:22 +00003348 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003349 if (strcmp(chr->label, name) != 0)
3350 continue;
3351 return chr;
3352 }
3353 return NULL;
3354}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003355
3356/* Get a character (serial) device interface. */
3357CharDriverState *qemu_char_get_next_serial(void)
3358{
3359 static int next_serial;
3360
3361 /* FIXME: This function needs to go away: use chardev properties! */
3362 return serial_hds[next_serial++];
3363}
3364
Paolo Bonzini4d454572012-11-26 16:03:42 +01003365QemuOptsList qemu_chardev_opts = {
3366 .name = "chardev",
3367 .implied_opt_name = "backend",
3368 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3369 .desc = {
3370 {
3371 .name = "backend",
3372 .type = QEMU_OPT_STRING,
3373 },{
3374 .name = "path",
3375 .type = QEMU_OPT_STRING,
3376 },{
3377 .name = "host",
3378 .type = QEMU_OPT_STRING,
3379 },{
3380 .name = "port",
3381 .type = QEMU_OPT_STRING,
3382 },{
3383 .name = "localaddr",
3384 .type = QEMU_OPT_STRING,
3385 },{
3386 .name = "localport",
3387 .type = QEMU_OPT_STRING,
3388 },{
3389 .name = "to",
3390 .type = QEMU_OPT_NUMBER,
3391 },{
3392 .name = "ipv4",
3393 .type = QEMU_OPT_BOOL,
3394 },{
3395 .name = "ipv6",
3396 .type = QEMU_OPT_BOOL,
3397 },{
3398 .name = "wait",
3399 .type = QEMU_OPT_BOOL,
3400 },{
3401 .name = "server",
3402 .type = QEMU_OPT_BOOL,
3403 },{
3404 .name = "delay",
3405 .type = QEMU_OPT_BOOL,
3406 },{
3407 .name = "telnet",
3408 .type = QEMU_OPT_BOOL,
3409 },{
3410 .name = "width",
3411 .type = QEMU_OPT_NUMBER,
3412 },{
3413 .name = "height",
3414 .type = QEMU_OPT_NUMBER,
3415 },{
3416 .name = "cols",
3417 .type = QEMU_OPT_NUMBER,
3418 },{
3419 .name = "rows",
3420 .type = QEMU_OPT_NUMBER,
3421 },{
3422 .name = "mux",
3423 .type = QEMU_OPT_BOOL,
3424 },{
3425 .name = "signal",
3426 .type = QEMU_OPT_BOOL,
3427 },{
3428 .name = "name",
3429 .type = QEMU_OPT_STRING,
3430 },{
3431 .name = "debug",
3432 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003433 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003434 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003435 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003436 },
3437 { /* end of list */ }
3438 },
3439};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003440
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003441#ifdef _WIN32
3442
3443static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3444{
3445 HANDLE out;
3446
3447 if (file->in) {
3448 error_setg(errp, "input file not supported");
3449 return NULL;
3450 }
3451
3452 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3453 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3454 if (out == INVALID_HANDLE_VALUE) {
3455 error_setg(errp, "open %s failed", file->out);
3456 return NULL;
3457 }
3458 return qemu_chr_open_win_file(out);
3459}
3460
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003461static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3462 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003463{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003464 return qemu_chr_open_win_path(serial->device);
3465}
3466
3467static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3468 Error **errp)
3469{
3470 error_setg(errp, "character device backend type 'parallel' not supported");
3471 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003472}
3473
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003474#else /* WIN32 */
3475
3476static int qmp_chardev_open_file_source(char *src, int flags,
3477 Error **errp)
3478{
3479 int fd = -1;
3480
3481 TFR(fd = qemu_open(src, flags, 0666));
3482 if (fd == -1) {
3483 error_setg(errp, "open %s: %s", src, strerror(errno));
3484 }
3485 return fd;
3486}
3487
3488static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3489{
3490 int flags, in = -1, out = -1;
3491
3492 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3493 out = qmp_chardev_open_file_source(file->out, flags, errp);
3494 if (error_is_set(errp)) {
3495 return NULL;
3496 }
3497
3498 if (file->in) {
3499 flags = O_RDONLY;
3500 in = qmp_chardev_open_file_source(file->in, flags, errp);
3501 if (error_is_set(errp)) {
3502 qemu_close(out);
3503 return NULL;
3504 }
3505 }
3506
3507 return qemu_chr_open_fd(in, out);
3508}
3509
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003510static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3511 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003512{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003513#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003514 int fd;
3515
3516 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3517 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003518 return NULL;
3519 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003520 socket_set_nonblock(fd);
3521 return qemu_chr_open_tty_fd(fd);
3522#else
3523 error_setg(errp, "character device backend type 'serial' not supported");
3524 return NULL;
3525#endif
3526}
3527
3528static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3529 Error **errp)
3530{
3531#ifdef HAVE_CHARDEV_PARPORT
3532 int fd;
3533
3534 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3535 if (error_is_set(errp)) {
3536 return NULL;
3537 }
3538 return qemu_chr_open_pp_fd(fd);
3539#else
3540 error_setg(errp, "character device backend type 'parallel' not supported");
3541 return NULL;
3542#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003543}
3544
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003545#endif /* WIN32 */
3546
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003547static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3548 Error **errp)
3549{
3550 SocketAddress *addr = sock->addr;
3551 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3552 bool is_listen = sock->has_server ? sock->server : true;
3553 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3554 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3555 int fd;
3556
3557 if (is_listen) {
3558 fd = socket_listen(addr, errp);
3559 } else {
3560 fd = socket_connect(addr, errp, NULL, NULL);
3561 }
3562 if (error_is_set(errp)) {
3563 return NULL;
3564 }
3565 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3566 is_telnet, is_waitconnect, errp);
3567}
3568
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003569ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3570 Error **errp)
3571{
3572 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3573 CharDriverState *chr = NULL;
3574
3575 chr = qemu_chr_find(id);
3576 if (chr) {
3577 error_setg(errp, "Chardev '%s' already exists", id);
3578 g_free(ret);
3579 return NULL;
3580 }
3581
3582 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003583 case CHARDEV_BACKEND_KIND_FILE:
3584 chr = qmp_chardev_open_file(backend->file, errp);
3585 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003586 case CHARDEV_BACKEND_KIND_SERIAL:
3587 chr = qmp_chardev_open_serial(backend->serial, errp);
3588 break;
3589 case CHARDEV_BACKEND_KIND_PARALLEL:
3590 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003591 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003592 case CHARDEV_BACKEND_KIND_SOCKET:
3593 chr = qmp_chardev_open_socket(backend->socket, errp);
3594 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003595#ifdef HAVE_CHARDEV_TTY
3596 case CHARDEV_BACKEND_KIND_PTY:
3597 {
3598 /* qemu_chr_open_pty sets "path" in opts */
3599 QemuOpts *opts;
3600 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3601 chr = qemu_chr_open_pty(opts);
3602 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3603 ret->has_pty = true;
3604 qemu_opts_del(opts);
3605 break;
3606 }
3607#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003608 case CHARDEV_BACKEND_KIND_NULL:
3609 chr = qemu_chr_open_null(NULL);
3610 break;
3611 default:
3612 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3613 break;
3614 }
3615
3616 if (chr == NULL && !error_is_set(errp)) {
3617 error_setg(errp, "Failed to create chardev");
3618 }
3619 if (chr) {
3620 chr->label = g_strdup(id);
3621 chr->avail_connections = 1;
3622 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3623 return ret;
3624 } else {
3625 g_free(ret);
3626 return NULL;
3627 }
3628}
3629
3630void qmp_chardev_remove(const char *id, Error **errp)
3631{
3632 CharDriverState *chr;
3633
3634 chr = qemu_chr_find(id);
3635 if (NULL == chr) {
3636 error_setg(errp, "Chardev '%s' not found", id);
3637 return;
3638 }
3639 if (chr->chr_can_read || chr->chr_read ||
3640 chr->chr_event || chr->handler_opaque) {
3641 error_setg(errp, "Chardev '%s' is busy", id);
3642 return;
3643 }
3644 qemu_chr_delete(chr);
3645}