blob: 5a5349111c58a1e895a5199ca3fbe91dc9d35dcf [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) {
689 if (status != G_IO_STATUS_AGAIN) {
690 return -1;
691 }
692 } else if (status == G_IO_STATUS_EOF) {
693 break;
694 } else {
695 buf += bytes_written;
696 len -= bytes_written;
697 }
698 }
699 return len1 - len;
700}
Anthony Liguori96c63842013-03-05 23:21:18 +0530701
Anthony Liguoria29753f2013-03-05 23:21:19 +0530702typedef struct FDCharDriver {
703 CharDriverState *chr;
704 GIOChannel *fd_in, *fd_out;
705 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000706 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530707 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000708} FDCharDriver;
709
aliguori6f97dba2008-10-31 18:49:55 +0000710static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
711{
712 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530713
714 return io_channel_send_all(s->fd_out, buf, len);
715}
716
717static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
718{
719 CharDriverState *chr = opaque;
720 FDCharDriver *s = chr->opaque;
721 int len;
722 uint8_t buf[READ_BUF_LEN];
723 GIOStatus status;
724 gsize bytes_read;
725
726 len = sizeof(buf);
727 if (len > s->max_size) {
728 len = s->max_size;
729 }
730 if (len == 0) {
731 return FALSE;
732 }
733
734 status = g_io_channel_read_chars(chan, (gchar *)buf,
735 len, &bytes_read, NULL);
736 if (status == G_IO_STATUS_EOF) {
737 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
738 return FALSE;
739 }
740 if (status == G_IO_STATUS_NORMAL) {
741 qemu_chr_be_write(chr, buf, bytes_read);
742 }
743
744 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000745}
746
747static int fd_chr_read_poll(void *opaque)
748{
749 CharDriverState *chr = opaque;
750 FDCharDriver *s = chr->opaque;
751
Anthony Liguori909cda12011-08-15 11:17:31 -0500752 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000753 return s->max_size;
754}
755
aliguori6f97dba2008-10-31 18:49:55 +0000756static void fd_chr_update_read_handler(CharDriverState *chr)
757{
758 FDCharDriver *s = chr->opaque;
759
Anthony Liguoria29753f2013-03-05 23:21:19 +0530760 if (s->fd_in_tag) {
761 g_source_remove(s->fd_in_tag);
762 }
763
764 if (s->fd_in) {
765 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 +0000766 }
767}
768
769static void fd_chr_close(struct CharDriverState *chr)
770{
771 FDCharDriver *s = chr->opaque;
772
Anthony Liguoria29753f2013-03-05 23:21:19 +0530773 if (s->fd_in_tag) {
774 g_source_remove(s->fd_in_tag);
775 s->fd_in_tag = 0;
776 }
777
778 if (s->fd_in) {
779 g_io_channel_unref(s->fd_in);
780 }
781 if (s->fd_out) {
782 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000783 }
784
Anthony Liguori7267c092011-08-20 22:09:37 -0500785 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100786 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000787}
788
789/* open a character device to a unix fd */
790static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
791{
792 CharDriverState *chr;
793 FDCharDriver *s;
794
Anthony Liguori7267c092011-08-20 22:09:37 -0500795 chr = g_malloc0(sizeof(CharDriverState));
796 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530797 s->fd_in = io_channel_from_fd(fd_in);
798 s->fd_out = io_channel_from_fd(fd_out);
799 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000800 chr->opaque = s;
801 chr->chr_write = fd_chr_write;
802 chr->chr_update_read_handler = fd_chr_update_read_handler;
803 chr->chr_close = fd_chr_close;
804
Amit Shah127338e2009-11-03 19:59:56 +0530805 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000806
807 return chr;
808}
809
Markus Armbruster1f514702012-02-07 15:09:08 +0100810static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000811{
812 int fd_out;
813
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100814 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200815 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100816 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100817 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100818 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100819 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000820}
821
Markus Armbruster1f514702012-02-07 15:09:08 +0100822static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000823{
824 int fd_in, fd_out;
825 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200826 const char *filename = qemu_opt_get(opts, "path");
827
828 if (filename == NULL) {
829 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100830 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200831 }
aliguori6f97dba2008-10-31 18:49:55 +0000832
833 snprintf(filename_in, 256, "%s.in", filename);
834 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100835 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
836 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000837 if (fd_in < 0 || fd_out < 0) {
838 if (fd_in >= 0)
839 close(fd_in);
840 if (fd_out >= 0)
841 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100842 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100843 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100844 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100845 }
aliguori6f97dba2008-10-31 18:49:55 +0000846 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100847 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000848}
849
aliguori6f97dba2008-10-31 18:49:55 +0000850/* init terminal so that we can grab keys */
851static struct termios oldtty;
852static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100853static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000854
855static void term_exit(void)
856{
857 tcsetattr (0, TCSANOW, &oldtty);
858 fcntl(0, F_SETFL, old_fd0_flags);
859}
860
Paolo Bonzinibb002512010-12-23 13:42:50 +0100861static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000862{
863 struct termios tty;
864
Paolo Bonzini03693642010-12-23 13:42:49 +0100865 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100866 if (!echo) {
867 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000868 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100869 tty.c_oflag |= OPOST;
870 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
871 tty.c_cflag &= ~(CSIZE|PARENB);
872 tty.c_cflag |= CS8;
873 tty.c_cc[VMIN] = 1;
874 tty.c_cc[VTIME] = 0;
875 }
aliguori6f97dba2008-10-31 18:49:55 +0000876 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100877 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000878 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000879
880 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000881}
882
883static void qemu_chr_close_stdio(struct CharDriverState *chr)
884{
885 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000886 fd_chr_close(chr);
887}
888
Markus Armbruster1f514702012-02-07 15:09:08 +0100889static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000890{
891 CharDriverState *chr;
892
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400893 if (is_daemonized()) {
894 error_report("cannot use stdio with -daemonize");
895 return NULL;
896 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530897 old_fd0_flags = fcntl(0, F_GETFL);
898 tcgetattr (0, &oldtty);
899 fcntl(0, F_SETFL, O_NONBLOCK);
900 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100901
aliguori6f97dba2008-10-31 18:49:55 +0000902 chr = qemu_chr_open_fd(0, 1);
903 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100904 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100905 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
906 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500907 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000908
Markus Armbruster1f514702012-02-07 15:09:08 +0100909 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000910}
911
912#ifdef __sun__
913/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000914static int openpty(int *amaster, int *aslave, char *name,
915 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000916{
917 const char *slave;
918 int mfd = -1, sfd = -1;
919
920 *amaster = *aslave = -1;
921
922 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
923 if (mfd < 0)
924 goto err;
925
926 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
927 goto err;
928
929 if ((slave = ptsname(mfd)) == NULL)
930 goto err;
931
932 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
933 goto err;
934
935 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
936 (termp != NULL && tcgetattr(sfd, termp) < 0))
937 goto err;
938
939 if (amaster)
940 *amaster = mfd;
941 if (aslave)
942 *aslave = sfd;
943 if (winp)
944 ioctl(sfd, TIOCSWINSZ, winp);
945
946 return 0;
947
948err:
949 if (sfd != -1)
950 close(sfd);
951 close(mfd);
952 return -1;
953}
954
blueswir13f4cb3d2009-04-13 16:31:01 +0000955static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000956{
957 termios_p->c_iflag &=
958 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
959 termios_p->c_oflag &= ~OPOST;
960 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
961 termios_p->c_cflag &= ~(CSIZE|PARENB);
962 termios_p->c_cflag |= CS8;
963
964 termios_p->c_cc[VMIN] = 0;
965 termios_p->c_cc[VTIME] = 0;
966}
967#endif
968
969#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100970 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
971 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000972
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100973#define HAVE_CHARDEV_TTY 1
974
aliguori6f97dba2008-10-31 18:49:55 +0000975typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +0530976 GIOChannel *fd;
977 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000978 int connected;
979 int polling;
980 int read_bytes;
981 QEMUTimer *timer;
982} PtyCharDriver;
983
984static void pty_chr_update_read_handler(CharDriverState *chr);
985static void pty_chr_state(CharDriverState *chr, int connected);
986
987static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
988{
989 PtyCharDriver *s = chr->opaque;
990
991 if (!s->connected) {
992 /* guest sends data, check for (re-)connect */
993 pty_chr_update_read_handler(chr);
994 return 0;
995 }
Anthony Liguori093d3a22013-03-05 23:21:20 +0530996 return io_channel_send_all(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000997}
998
999static int pty_chr_read_poll(void *opaque)
1000{
1001 CharDriverState *chr = opaque;
1002 PtyCharDriver *s = chr->opaque;
1003
Anthony Liguori909cda12011-08-15 11:17:31 -05001004 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001005 return s->read_bytes;
1006}
1007
Anthony Liguori093d3a22013-03-05 23:21:20 +05301008static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001009{
1010 CharDriverState *chr = opaque;
1011 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301012 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301013 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301014 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001015
1016 len = sizeof(buf);
1017 if (len > s->read_bytes)
1018 len = s->read_bytes;
1019 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301020 return FALSE;
1021 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1022 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001023 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301024 return FALSE;
1025 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001026 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001027 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001028 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301029 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001030}
1031
1032static void pty_chr_update_read_handler(CharDriverState *chr)
1033{
1034 PtyCharDriver *s = chr->opaque;
1035
Anthony Liguori093d3a22013-03-05 23:21:20 +05301036 if (s->fd_tag) {
1037 g_source_remove(s->fd_tag);
1038 }
1039
1040 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001041 s->polling = 1;
1042 /*
1043 * Short timeout here: just need wait long enougth that qemu makes
1044 * it through the poll loop once. When reconnected we want a
1045 * short timeout so we notice it almost instantly. Otherwise
1046 * read() gives us -EIO instantly, making pty_chr_state() reset the
1047 * timeout to the normal (much longer) poll interval before the
1048 * timer triggers.
1049 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001050 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +00001051}
1052
1053static void pty_chr_state(CharDriverState *chr, int connected)
1054{
1055 PtyCharDriver *s = chr->opaque;
1056
1057 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301058 g_source_remove(s->fd_tag);
1059 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001060 s->connected = 0;
1061 s->polling = 0;
1062 /* (re-)connect poll interval for idle guests: once per second.
1063 * We check more frequently in case the guests sends data to
1064 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001065 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001066 } else {
1067 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301068 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001069 s->connected = 1;
1070 }
1071}
1072
1073static void pty_chr_timer(void *opaque)
1074{
1075 struct CharDriverState *chr = opaque;
1076 PtyCharDriver *s = chr->opaque;
1077
1078 if (s->connected)
1079 return;
1080 if (s->polling) {
1081 /* If we arrive here without polling being cleared due
1082 * read returning -EIO, then we are (re-)connected */
1083 pty_chr_state(chr, 1);
1084 return;
1085 }
1086
1087 /* Next poll ... */
1088 pty_chr_update_read_handler(chr);
1089}
1090
1091static void pty_chr_close(struct CharDriverState *chr)
1092{
1093 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301094 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001095
Anthony Liguori093d3a22013-03-05 23:21:20 +05301096 if (s->fd_tag) {
1097 g_source_remove(s->fd_tag);
1098 }
1099 fd = g_io_channel_unix_get_fd(s->fd);
1100 g_io_channel_unref(s->fd);
1101 close(fd);
aliguori819f56b2009-03-28 17:58:14 +00001102 qemu_del_timer(s->timer);
1103 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -05001104 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001105 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001106}
1107
Markus Armbruster1f514702012-02-07 15:09:08 +01001108static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001109{
1110 CharDriverState *chr;
1111 PtyCharDriver *s;
1112 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001113 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001114 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001115#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001116 char pty_name[PATH_MAX];
1117#define q_ptsname(x) pty_name
1118#else
1119 char *pty_name = NULL;
1120#define q_ptsname(x) ptsname(x)
1121#endif
1122
Markus Armbrustera4e26042011-11-11 10:40:05 +01001123 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001124 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001125 }
1126
1127 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001128 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001129 cfmakeraw(&tty);
1130 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1131 close(slave_fd);
1132
Markus Armbrustera4e26042011-11-11 10:40:05 +01001133 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001134
Markus Armbrustera4e26042011-11-11 10:40:05 +01001135 len = strlen(q_ptsname(master_fd)) + 5;
1136 chr->filename = g_malloc(len);
1137 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1138 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001139
1140 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001141 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1142 q_ptsname(master_fd),
1143 label ? " (label " : "",
1144 label ? label : "",
1145 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001146
1147 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001148 chr->opaque = s;
1149 chr->chr_write = pty_chr_write;
1150 chr->chr_update_read_handler = pty_chr_update_read_handler;
1151 chr->chr_close = pty_chr_close;
1152
Anthony Liguori093d3a22013-03-05 23:21:20 +05301153 s->fd = io_channel_from_fd(master_fd);
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001154 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001155
Markus Armbruster1f514702012-02-07 15:09:08 +01001156 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001157}
1158
1159static void tty_serial_init(int fd, int speed,
1160 int parity, int data_bits, int stop_bits)
1161{
1162 struct termios tty;
1163 speed_t spd;
1164
1165#if 0
1166 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1167 speed, parity, data_bits, stop_bits);
1168#endif
1169 tcgetattr (fd, &tty);
1170
Stefan Weil45eea132009-10-26 16:10:10 +01001171#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1172 speed = speed * 10 / 11;
1173 do {
1174 check_speed(50);
1175 check_speed(75);
1176 check_speed(110);
1177 check_speed(134);
1178 check_speed(150);
1179 check_speed(200);
1180 check_speed(300);
1181 check_speed(600);
1182 check_speed(1200);
1183 check_speed(1800);
1184 check_speed(2400);
1185 check_speed(4800);
1186 check_speed(9600);
1187 check_speed(19200);
1188 check_speed(38400);
1189 /* Non-Posix values follow. They may be unsupported on some systems. */
1190 check_speed(57600);
1191 check_speed(115200);
1192#ifdef B230400
1193 check_speed(230400);
1194#endif
1195#ifdef B460800
1196 check_speed(460800);
1197#endif
1198#ifdef B500000
1199 check_speed(500000);
1200#endif
1201#ifdef B576000
1202 check_speed(576000);
1203#endif
1204#ifdef B921600
1205 check_speed(921600);
1206#endif
1207#ifdef B1000000
1208 check_speed(1000000);
1209#endif
1210#ifdef B1152000
1211 check_speed(1152000);
1212#endif
1213#ifdef B1500000
1214 check_speed(1500000);
1215#endif
1216#ifdef B2000000
1217 check_speed(2000000);
1218#endif
1219#ifdef B2500000
1220 check_speed(2500000);
1221#endif
1222#ifdef B3000000
1223 check_speed(3000000);
1224#endif
1225#ifdef B3500000
1226 check_speed(3500000);
1227#endif
1228#ifdef B4000000
1229 check_speed(4000000);
1230#endif
aliguori6f97dba2008-10-31 18:49:55 +00001231 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001232 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001233
1234 cfsetispeed(&tty, spd);
1235 cfsetospeed(&tty, spd);
1236
1237 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1238 |INLCR|IGNCR|ICRNL|IXON);
1239 tty.c_oflag |= OPOST;
1240 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1241 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1242 switch(data_bits) {
1243 default:
1244 case 8:
1245 tty.c_cflag |= CS8;
1246 break;
1247 case 7:
1248 tty.c_cflag |= CS7;
1249 break;
1250 case 6:
1251 tty.c_cflag |= CS6;
1252 break;
1253 case 5:
1254 tty.c_cflag |= CS5;
1255 break;
1256 }
1257 switch(parity) {
1258 default:
1259 case 'N':
1260 break;
1261 case 'E':
1262 tty.c_cflag |= PARENB;
1263 break;
1264 case 'O':
1265 tty.c_cflag |= PARENB | PARODD;
1266 break;
1267 }
1268 if (stop_bits == 2)
1269 tty.c_cflag |= CSTOPB;
1270
1271 tcsetattr (fd, TCSANOW, &tty);
1272}
1273
1274static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1275{
1276 FDCharDriver *s = chr->opaque;
1277
1278 switch(cmd) {
1279 case CHR_IOCTL_SERIAL_SET_PARAMS:
1280 {
1281 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301282 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1283 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001284 ssp->data_bits, ssp->stop_bits);
1285 }
1286 break;
1287 case CHR_IOCTL_SERIAL_SET_BREAK:
1288 {
1289 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301290 if (enable) {
1291 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1292 }
aliguori6f97dba2008-10-31 18:49:55 +00001293 }
1294 break;
1295 case CHR_IOCTL_SERIAL_GET_TIOCM:
1296 {
1297 int sarg = 0;
1298 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301299 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001300 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001301 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001302 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001303 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001304 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001305 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001306 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001307 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001308 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001309 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001310 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001311 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001312 *targ |= CHR_TIOCM_RTS;
1313 }
1314 break;
1315 case CHR_IOCTL_SERIAL_SET_TIOCM:
1316 {
1317 int sarg = *(int *)arg;
1318 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301319 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001320 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1321 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1322 if (sarg & CHR_TIOCM_CTS)
1323 targ |= TIOCM_CTS;
1324 if (sarg & CHR_TIOCM_CAR)
1325 targ |= TIOCM_CAR;
1326 if (sarg & CHR_TIOCM_DSR)
1327 targ |= TIOCM_DSR;
1328 if (sarg & CHR_TIOCM_RI)
1329 targ |= TIOCM_RI;
1330 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001331 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001332 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001333 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301334 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001335 }
1336 break;
1337 default:
1338 return -ENOTSUP;
1339 }
1340 return 0;
1341}
1342
David Ahern4266a132010-02-10 18:27:17 -07001343static void qemu_chr_close_tty(CharDriverState *chr)
1344{
1345 FDCharDriver *s = chr->opaque;
1346 int fd = -1;
1347
1348 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301349 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001350 }
1351
1352 fd_chr_close(chr);
1353
1354 if (fd >= 0) {
1355 close(fd);
1356 }
1357}
1358
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001359static CharDriverState *qemu_chr_open_tty_fd(int fd)
1360{
1361 CharDriverState *chr;
1362
1363 tty_serial_init(fd, 115200, 'N', 8, 1);
1364 chr = qemu_chr_open_fd(fd, fd);
1365 chr->chr_ioctl = tty_serial_ioctl;
1366 chr->chr_close = qemu_chr_close_tty;
1367 return chr;
1368}
1369
Markus Armbruster1f514702012-02-07 15:09:08 +01001370static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001371{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001372 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001373 int fd;
1374
Markus Armbrusterb181e042012-02-07 15:09:09 +01001375 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001376 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001377 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001378 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001379 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001380}
aliguori6f97dba2008-10-31 18:49:55 +00001381#endif /* __linux__ || __sun__ */
1382
1383#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001384
1385#define HAVE_CHARDEV_PARPORT 1
1386
aliguori6f97dba2008-10-31 18:49:55 +00001387typedef struct {
1388 int fd;
1389 int mode;
1390} ParallelCharDriver;
1391
1392static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1393{
1394 if (s->mode != mode) {
1395 int m = mode;
1396 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1397 return 0;
1398 s->mode = mode;
1399 }
1400 return 1;
1401}
1402
1403static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1404{
1405 ParallelCharDriver *drv = chr->opaque;
1406 int fd = drv->fd;
1407 uint8_t b;
1408
1409 switch(cmd) {
1410 case CHR_IOCTL_PP_READ_DATA:
1411 if (ioctl(fd, PPRDATA, &b) < 0)
1412 return -ENOTSUP;
1413 *(uint8_t *)arg = b;
1414 break;
1415 case CHR_IOCTL_PP_WRITE_DATA:
1416 b = *(uint8_t *)arg;
1417 if (ioctl(fd, PPWDATA, &b) < 0)
1418 return -ENOTSUP;
1419 break;
1420 case CHR_IOCTL_PP_READ_CONTROL:
1421 if (ioctl(fd, PPRCONTROL, &b) < 0)
1422 return -ENOTSUP;
1423 /* Linux gives only the lowest bits, and no way to know data
1424 direction! For better compatibility set the fixed upper
1425 bits. */
1426 *(uint8_t *)arg = b | 0xc0;
1427 break;
1428 case CHR_IOCTL_PP_WRITE_CONTROL:
1429 b = *(uint8_t *)arg;
1430 if (ioctl(fd, PPWCONTROL, &b) < 0)
1431 return -ENOTSUP;
1432 break;
1433 case CHR_IOCTL_PP_READ_STATUS:
1434 if (ioctl(fd, PPRSTATUS, &b) < 0)
1435 return -ENOTSUP;
1436 *(uint8_t *)arg = b;
1437 break;
1438 case CHR_IOCTL_PP_DATA_DIR:
1439 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1440 return -ENOTSUP;
1441 break;
1442 case CHR_IOCTL_PP_EPP_READ_ADDR:
1443 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1444 struct ParallelIOArg *parg = arg;
1445 int n = read(fd, parg->buffer, parg->count);
1446 if (n != parg->count) {
1447 return -EIO;
1448 }
1449 }
1450 break;
1451 case CHR_IOCTL_PP_EPP_READ:
1452 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1453 struct ParallelIOArg *parg = arg;
1454 int n = read(fd, parg->buffer, parg->count);
1455 if (n != parg->count) {
1456 return -EIO;
1457 }
1458 }
1459 break;
1460 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1461 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1462 struct ParallelIOArg *parg = arg;
1463 int n = write(fd, parg->buffer, parg->count);
1464 if (n != parg->count) {
1465 return -EIO;
1466 }
1467 }
1468 break;
1469 case CHR_IOCTL_PP_EPP_WRITE:
1470 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1471 struct ParallelIOArg *parg = arg;
1472 int n = write(fd, parg->buffer, parg->count);
1473 if (n != parg->count) {
1474 return -EIO;
1475 }
1476 }
1477 break;
1478 default:
1479 return -ENOTSUP;
1480 }
1481 return 0;
1482}
1483
1484static void pp_close(CharDriverState *chr)
1485{
1486 ParallelCharDriver *drv = chr->opaque;
1487 int fd = drv->fd;
1488
1489 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1490 ioctl(fd, PPRELEASE);
1491 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001492 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001493 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001494}
1495
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001496static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001497{
1498 CharDriverState *chr;
1499 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001500
1501 if (ioctl(fd, PPCLAIM) < 0) {
1502 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001503 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001504 }
1505
Anthony Liguori7267c092011-08-20 22:09:37 -05001506 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001507 drv->fd = fd;
1508 drv->mode = IEEE1284_MODE_COMPAT;
1509
Anthony Liguori7267c092011-08-20 22:09:37 -05001510 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001511 chr->chr_write = null_chr_write;
1512 chr->chr_ioctl = pp_ioctl;
1513 chr->chr_close = pp_close;
1514 chr->opaque = drv;
1515
Amit Shah127338e2009-11-03 19:59:56 +05301516 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001517
Markus Armbruster1f514702012-02-07 15:09:08 +01001518 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001519}
1520#endif /* __linux__ */
1521
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001522#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001523
1524#define HAVE_CHARDEV_PARPORT 1
1525
blueswir16972f932008-11-22 20:49:12 +00001526static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1527{
Stefan Weile0efb992011-02-23 19:09:16 +01001528 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001529 uint8_t b;
1530
1531 switch(cmd) {
1532 case CHR_IOCTL_PP_READ_DATA:
1533 if (ioctl(fd, PPIGDATA, &b) < 0)
1534 return -ENOTSUP;
1535 *(uint8_t *)arg = b;
1536 break;
1537 case CHR_IOCTL_PP_WRITE_DATA:
1538 b = *(uint8_t *)arg;
1539 if (ioctl(fd, PPISDATA, &b) < 0)
1540 return -ENOTSUP;
1541 break;
1542 case CHR_IOCTL_PP_READ_CONTROL:
1543 if (ioctl(fd, PPIGCTRL, &b) < 0)
1544 return -ENOTSUP;
1545 *(uint8_t *)arg = b;
1546 break;
1547 case CHR_IOCTL_PP_WRITE_CONTROL:
1548 b = *(uint8_t *)arg;
1549 if (ioctl(fd, PPISCTRL, &b) < 0)
1550 return -ENOTSUP;
1551 break;
1552 case CHR_IOCTL_PP_READ_STATUS:
1553 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1554 return -ENOTSUP;
1555 *(uint8_t *)arg = b;
1556 break;
1557 default:
1558 return -ENOTSUP;
1559 }
1560 return 0;
1561}
1562
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001563static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001564{
1565 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001566
Anthony Liguori7267c092011-08-20 22:09:37 -05001567 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001568 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001569 chr->chr_write = null_chr_write;
1570 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001571 return chr;
blueswir16972f932008-11-22 20:49:12 +00001572}
1573#endif
1574
aliguori6f97dba2008-10-31 18:49:55 +00001575#else /* _WIN32 */
1576
1577typedef struct {
1578 int max_size;
1579 HANDLE hcom, hrecv, hsend;
1580 OVERLAPPED orecv, osend;
1581 BOOL fpipe;
1582 DWORD len;
1583} WinCharState;
1584
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001585typedef struct {
1586 HANDLE hStdIn;
1587 HANDLE hInputReadyEvent;
1588 HANDLE hInputDoneEvent;
1589 HANDLE hInputThread;
1590 uint8_t win_stdio_buf;
1591} WinStdioCharState;
1592
aliguori6f97dba2008-10-31 18:49:55 +00001593#define NSENDBUF 2048
1594#define NRECVBUF 2048
1595#define MAXCONNECT 1
1596#define NTIMEOUT 5000
1597
1598static int win_chr_poll(void *opaque);
1599static int win_chr_pipe_poll(void *opaque);
1600
1601static void win_chr_close(CharDriverState *chr)
1602{
1603 WinCharState *s = chr->opaque;
1604
1605 if (s->hsend) {
1606 CloseHandle(s->hsend);
1607 s->hsend = NULL;
1608 }
1609 if (s->hrecv) {
1610 CloseHandle(s->hrecv);
1611 s->hrecv = NULL;
1612 }
1613 if (s->hcom) {
1614 CloseHandle(s->hcom);
1615 s->hcom = NULL;
1616 }
1617 if (s->fpipe)
1618 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1619 else
1620 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301621
Hans de Goedea425d232011-11-19 10:22:43 +01001622 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001623}
1624
1625static int win_chr_init(CharDriverState *chr, const char *filename)
1626{
1627 WinCharState *s = chr->opaque;
1628 COMMCONFIG comcfg;
1629 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1630 COMSTAT comstat;
1631 DWORD size;
1632 DWORD err;
1633
1634 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1635 if (!s->hsend) {
1636 fprintf(stderr, "Failed CreateEvent\n");
1637 goto fail;
1638 }
1639 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1640 if (!s->hrecv) {
1641 fprintf(stderr, "Failed CreateEvent\n");
1642 goto fail;
1643 }
1644
1645 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1646 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1647 if (s->hcom == INVALID_HANDLE_VALUE) {
1648 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1649 s->hcom = NULL;
1650 goto fail;
1651 }
1652
1653 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1654 fprintf(stderr, "Failed SetupComm\n");
1655 goto fail;
1656 }
1657
1658 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1659 size = sizeof(COMMCONFIG);
1660 GetDefaultCommConfig(filename, &comcfg, &size);
1661 comcfg.dcb.DCBlength = sizeof(DCB);
1662 CommConfigDialog(filename, NULL, &comcfg);
1663
1664 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1665 fprintf(stderr, "Failed SetCommState\n");
1666 goto fail;
1667 }
1668
1669 if (!SetCommMask(s->hcom, EV_ERR)) {
1670 fprintf(stderr, "Failed SetCommMask\n");
1671 goto fail;
1672 }
1673
1674 cto.ReadIntervalTimeout = MAXDWORD;
1675 if (!SetCommTimeouts(s->hcom, &cto)) {
1676 fprintf(stderr, "Failed SetCommTimeouts\n");
1677 goto fail;
1678 }
1679
1680 if (!ClearCommError(s->hcom, &err, &comstat)) {
1681 fprintf(stderr, "Failed ClearCommError\n");
1682 goto fail;
1683 }
1684 qemu_add_polling_cb(win_chr_poll, chr);
1685 return 0;
1686
1687 fail:
1688 win_chr_close(chr);
1689 return -1;
1690}
1691
1692static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1693{
1694 WinCharState *s = chr->opaque;
1695 DWORD len, ret, size, err;
1696
1697 len = len1;
1698 ZeroMemory(&s->osend, sizeof(s->osend));
1699 s->osend.hEvent = s->hsend;
1700 while (len > 0) {
1701 if (s->hsend)
1702 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1703 else
1704 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1705 if (!ret) {
1706 err = GetLastError();
1707 if (err == ERROR_IO_PENDING) {
1708 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1709 if (ret) {
1710 buf += size;
1711 len -= size;
1712 } else {
1713 break;
1714 }
1715 } else {
1716 break;
1717 }
1718 } else {
1719 buf += size;
1720 len -= size;
1721 }
1722 }
1723 return len1 - len;
1724}
1725
1726static int win_chr_read_poll(CharDriverState *chr)
1727{
1728 WinCharState *s = chr->opaque;
1729
Anthony Liguori909cda12011-08-15 11:17:31 -05001730 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001731 return s->max_size;
1732}
1733
1734static void win_chr_readfile(CharDriverState *chr)
1735{
1736 WinCharState *s = chr->opaque;
1737 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301738 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001739 DWORD size;
1740
1741 ZeroMemory(&s->orecv, sizeof(s->orecv));
1742 s->orecv.hEvent = s->hrecv;
1743 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1744 if (!ret) {
1745 err = GetLastError();
1746 if (err == ERROR_IO_PENDING) {
1747 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1748 }
1749 }
1750
1751 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001752 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001753 }
1754}
1755
1756static void win_chr_read(CharDriverState *chr)
1757{
1758 WinCharState *s = chr->opaque;
1759
1760 if (s->len > s->max_size)
1761 s->len = s->max_size;
1762 if (s->len == 0)
1763 return;
1764
1765 win_chr_readfile(chr);
1766}
1767
1768static int win_chr_poll(void *opaque)
1769{
1770 CharDriverState *chr = opaque;
1771 WinCharState *s = chr->opaque;
1772 COMSTAT status;
1773 DWORD comerr;
1774
1775 ClearCommError(s->hcom, &comerr, &status);
1776 if (status.cbInQue > 0) {
1777 s->len = status.cbInQue;
1778 win_chr_read_poll(chr);
1779 win_chr_read(chr);
1780 return 1;
1781 }
1782 return 0;
1783}
1784
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001785static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001786{
1787 CharDriverState *chr;
1788 WinCharState *s;
1789
Anthony Liguori7267c092011-08-20 22:09:37 -05001790 chr = g_malloc0(sizeof(CharDriverState));
1791 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001792 chr->opaque = s;
1793 chr->chr_write = win_chr_write;
1794 chr->chr_close = win_chr_close;
1795
1796 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001797 g_free(s);
1798 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001799 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001800 }
Amit Shah127338e2009-11-03 19:59:56 +05301801 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001802 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001803}
1804
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001805static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1806{
1807 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1808}
1809
aliguori6f97dba2008-10-31 18:49:55 +00001810static int win_chr_pipe_poll(void *opaque)
1811{
1812 CharDriverState *chr = opaque;
1813 WinCharState *s = chr->opaque;
1814 DWORD size;
1815
1816 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1817 if (size > 0) {
1818 s->len = size;
1819 win_chr_read_poll(chr);
1820 win_chr_read(chr);
1821 return 1;
1822 }
1823 return 0;
1824}
1825
1826static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1827{
1828 WinCharState *s = chr->opaque;
1829 OVERLAPPED ov;
1830 int ret;
1831 DWORD size;
1832 char openname[256];
1833
1834 s->fpipe = TRUE;
1835
1836 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1837 if (!s->hsend) {
1838 fprintf(stderr, "Failed CreateEvent\n");
1839 goto fail;
1840 }
1841 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1842 if (!s->hrecv) {
1843 fprintf(stderr, "Failed CreateEvent\n");
1844 goto fail;
1845 }
1846
1847 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1848 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1849 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1850 PIPE_WAIT,
1851 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1852 if (s->hcom == INVALID_HANDLE_VALUE) {
1853 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1854 s->hcom = NULL;
1855 goto fail;
1856 }
1857
1858 ZeroMemory(&ov, sizeof(ov));
1859 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1860 ret = ConnectNamedPipe(s->hcom, &ov);
1861 if (ret) {
1862 fprintf(stderr, "Failed ConnectNamedPipe\n");
1863 goto fail;
1864 }
1865
1866 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1867 if (!ret) {
1868 fprintf(stderr, "Failed GetOverlappedResult\n");
1869 if (ov.hEvent) {
1870 CloseHandle(ov.hEvent);
1871 ov.hEvent = NULL;
1872 }
1873 goto fail;
1874 }
1875
1876 if (ov.hEvent) {
1877 CloseHandle(ov.hEvent);
1878 ov.hEvent = NULL;
1879 }
1880 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1881 return 0;
1882
1883 fail:
1884 win_chr_close(chr);
1885 return -1;
1886}
1887
1888
Markus Armbruster1f514702012-02-07 15:09:08 +01001889static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001890{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001891 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001892 CharDriverState *chr;
1893 WinCharState *s;
1894
Anthony Liguori7267c092011-08-20 22:09:37 -05001895 chr = g_malloc0(sizeof(CharDriverState));
1896 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001897 chr->opaque = s;
1898 chr->chr_write = win_chr_write;
1899 chr->chr_close = win_chr_close;
1900
1901 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001902 g_free(s);
1903 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001904 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001905 }
Amit Shah127338e2009-11-03 19:59:56 +05301906 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001907 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001908}
1909
Markus Armbruster1f514702012-02-07 15:09:08 +01001910static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001911{
1912 CharDriverState *chr;
1913 WinCharState *s;
1914
Anthony Liguori7267c092011-08-20 22:09:37 -05001915 chr = g_malloc0(sizeof(CharDriverState));
1916 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001917 s->hcom = fd_out;
1918 chr->opaque = s;
1919 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301920 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001921 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001922}
1923
Markus Armbruster1f514702012-02-07 15:09:08 +01001924static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001925{
Markus Armbruster1f514702012-02-07 15:09:08 +01001926 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001927}
1928
Markus Armbruster1f514702012-02-07 15:09:08 +01001929static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001930{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001931 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001932 HANDLE fd_out;
1933
1934 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1935 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001936 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001937 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001938 }
aliguori6f97dba2008-10-31 18:49:55 +00001939
Markus Armbruster1f514702012-02-07 15:09:08 +01001940 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001941}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001942
1943static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1944{
1945 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1946 DWORD dwSize;
1947 int len1;
1948
1949 len1 = len;
1950
1951 while (len1 > 0) {
1952 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1953 break;
1954 }
1955 buf += dwSize;
1956 len1 -= dwSize;
1957 }
1958
1959 return len - len1;
1960}
1961
1962static void win_stdio_wait_func(void *opaque)
1963{
1964 CharDriverState *chr = opaque;
1965 WinStdioCharState *stdio = chr->opaque;
1966 INPUT_RECORD buf[4];
1967 int ret;
1968 DWORD dwSize;
1969 int i;
1970
1971 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1972 &dwSize);
1973
1974 if (!ret) {
1975 /* Avoid error storm */
1976 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1977 return;
1978 }
1979
1980 for (i = 0; i < dwSize; i++) {
1981 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1982
1983 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1984 int j;
1985 if (kev->uChar.AsciiChar != 0) {
1986 for (j = 0; j < kev->wRepeatCount; j++) {
1987 if (qemu_chr_be_can_write(chr)) {
1988 uint8_t c = kev->uChar.AsciiChar;
1989 qemu_chr_be_write(chr, &c, 1);
1990 }
1991 }
1992 }
1993 }
1994 }
1995}
1996
1997static DWORD WINAPI win_stdio_thread(LPVOID param)
1998{
1999 CharDriverState *chr = param;
2000 WinStdioCharState *stdio = chr->opaque;
2001 int ret;
2002 DWORD dwSize;
2003
2004 while (1) {
2005
2006 /* Wait for one byte */
2007 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2008
2009 /* Exit in case of error, continue if nothing read */
2010 if (!ret) {
2011 break;
2012 }
2013 if (!dwSize) {
2014 continue;
2015 }
2016
2017 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2018 if (stdio->win_stdio_buf == '\r') {
2019 continue;
2020 }
2021
2022 /* Signal the main thread and wait until the byte was eaten */
2023 if (!SetEvent(stdio->hInputReadyEvent)) {
2024 break;
2025 }
2026 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2027 != WAIT_OBJECT_0) {
2028 break;
2029 }
2030 }
2031
2032 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2033 return 0;
2034}
2035
2036static void win_stdio_thread_wait_func(void *opaque)
2037{
2038 CharDriverState *chr = opaque;
2039 WinStdioCharState *stdio = chr->opaque;
2040
2041 if (qemu_chr_be_can_write(chr)) {
2042 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2043 }
2044
2045 SetEvent(stdio->hInputDoneEvent);
2046}
2047
2048static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2049{
2050 WinStdioCharState *stdio = chr->opaque;
2051 DWORD dwMode = 0;
2052
2053 GetConsoleMode(stdio->hStdIn, &dwMode);
2054
2055 if (echo) {
2056 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2057 } else {
2058 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2059 }
2060}
2061
2062static void win_stdio_close(CharDriverState *chr)
2063{
2064 WinStdioCharState *stdio = chr->opaque;
2065
2066 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2067 CloseHandle(stdio->hInputReadyEvent);
2068 }
2069 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2070 CloseHandle(stdio->hInputDoneEvent);
2071 }
2072 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2073 TerminateThread(stdio->hInputThread, 0);
2074 }
2075
2076 g_free(chr->opaque);
2077 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002078}
2079
Markus Armbruster1f514702012-02-07 15:09:08 +01002080static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002081{
2082 CharDriverState *chr;
2083 WinStdioCharState *stdio;
2084 DWORD dwMode;
2085 int is_console = 0;
2086
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002087 chr = g_malloc0(sizeof(CharDriverState));
2088 stdio = g_malloc0(sizeof(WinStdioCharState));
2089
2090 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2091 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2092 fprintf(stderr, "cannot open stdio: invalid handle\n");
2093 exit(1);
2094 }
2095
2096 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2097
2098 chr->opaque = stdio;
2099 chr->chr_write = win_stdio_write;
2100 chr->chr_close = win_stdio_close;
2101
Anthony Liguoried7a1542013-03-05 23:21:17 +05302102 if (is_console) {
2103 if (qemu_add_wait_object(stdio->hStdIn,
2104 win_stdio_wait_func, chr)) {
2105 fprintf(stderr, "qemu_add_wait_object: failed\n");
2106 }
2107 } else {
2108 DWORD dwId;
2109
2110 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2111 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2112 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2113 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002114
Anthony Liguoried7a1542013-03-05 23:21:17 +05302115 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2116 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2117 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2118 fprintf(stderr, "cannot create stdio thread or event\n");
2119 exit(1);
2120 }
2121 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2122 win_stdio_thread_wait_func, chr)) {
2123 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002124 }
2125 }
2126
2127 dwMode |= ENABLE_LINE_INPUT;
2128
Anthony Liguoried7a1542013-03-05 23:21:17 +05302129 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002130 /* set the terminal in raw mode */
2131 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2132 dwMode |= ENABLE_PROCESSED_INPUT;
2133 }
2134
2135 SetConsoleMode(stdio->hStdIn, dwMode);
2136
2137 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2138 qemu_chr_fe_set_echo(chr, false);
2139
Markus Armbruster1f514702012-02-07 15:09:08 +01002140 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002141}
aliguori6f97dba2008-10-31 18:49:55 +00002142#endif /* !_WIN32 */
2143
2144/***********************************************************/
2145/* UDP Net console */
2146
2147typedef struct {
2148 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302149 GIOChannel *chan;
2150 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302151 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002152 int bufcnt;
2153 int bufptr;
2154 int max_size;
2155} NetCharDriver;
2156
2157static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2158{
2159 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302160 gsize bytes_written;
2161 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002162
Anthony Liguori76a96442013-03-05 23:21:21 +05302163 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2164 if (status == G_IO_STATUS_EOF) {
2165 return 0;
2166 } else if (status != G_IO_STATUS_NORMAL) {
2167 return -1;
2168 }
2169
2170 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002171}
2172
2173static int udp_chr_read_poll(void *opaque)
2174{
2175 CharDriverState *chr = opaque;
2176 NetCharDriver *s = chr->opaque;
2177
Anthony Liguori909cda12011-08-15 11:17:31 -05002178 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002179
2180 /* If there were any stray characters in the queue process them
2181 * first
2182 */
2183 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002184 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002185 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002186 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002187 }
2188 return s->max_size;
2189}
2190
Anthony Liguori76a96442013-03-05 23:21:21 +05302191static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002192{
2193 CharDriverState *chr = opaque;
2194 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302195 gsize bytes_read = 0;
2196 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002197
2198 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302199 return FALSE;
2200 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2201 &bytes_read, NULL);
2202 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002203 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302204 if (status != G_IO_STATUS_NORMAL) {
2205 return FALSE;
2206 }
aliguori6f97dba2008-10-31 18:49:55 +00002207
2208 s->bufptr = 0;
2209 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002210 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002211 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002212 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002213 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302214
2215 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002216}
2217
2218static void udp_chr_update_read_handler(CharDriverState *chr)
2219{
2220 NetCharDriver *s = chr->opaque;
2221
Anthony Liguori76a96442013-03-05 23:21:21 +05302222 if (s->tag) {
2223 g_source_remove(s->tag);
2224 s->tag = 0;
2225 }
2226
2227 if (s->chan) {
2228 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002229 }
2230}
2231
aliguori819f56b2009-03-28 17:58:14 +00002232static void udp_chr_close(CharDriverState *chr)
2233{
2234 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302235 if (s->tag) {
2236 g_source_remove(s->tag);
2237 }
2238 if (s->chan) {
2239 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002240 closesocket(s->fd);
2241 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002242 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002243 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002244}
2245
Markus Armbruster1f514702012-02-07 15:09:08 +01002246static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002247{
2248 CharDriverState *chr = NULL;
2249 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002250 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002251 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002252
Anthony Liguori7267c092011-08-20 22:09:37 -05002253 chr = g_malloc0(sizeof(CharDriverState));
2254 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002255
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002256 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002257 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002258 goto return_err;
2259 }
2260
2261 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302262 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002263 s->bufcnt = 0;
2264 s->bufptr = 0;
2265 chr->opaque = s;
2266 chr->chr_write = udp_chr_write;
2267 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002268 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002269 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002270
2271return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002272 if (local_err) {
2273 qerror_report_err(local_err);
2274 error_free(local_err);
2275 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002276 g_free(chr);
2277 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002278 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002279 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002280 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002281 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002282}
2283
2284/***********************************************************/
2285/* TCP Net console */
2286
2287typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302288
2289 GIOChannel *chan, *listen_chan;
2290 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002291 int fd, listen_fd;
2292 int connected;
2293 int max_size;
2294 int do_telnetopt;
2295 int do_nodelay;
2296 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002297 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002298} TCPCharDriver;
2299
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302300static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002301
2302static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2303{
2304 TCPCharDriver *s = chr->opaque;
2305 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302306 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002307 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002308 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002309 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002310 }
2311}
2312
2313static int tcp_chr_read_poll(void *opaque)
2314{
2315 CharDriverState *chr = opaque;
2316 TCPCharDriver *s = chr->opaque;
2317 if (!s->connected)
2318 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002319 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002320 return s->max_size;
2321}
2322
2323#define IAC 255
2324#define IAC_BREAK 243
2325static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2326 TCPCharDriver *s,
2327 uint8_t *buf, int *size)
2328{
2329 /* Handle any telnet client's basic IAC options to satisfy char by
2330 * char mode with no echo. All IAC options will be removed from
2331 * the buf and the do_telnetopt variable will be used to track the
2332 * state of the width of the IAC information.
2333 *
2334 * IAC commands come in sets of 3 bytes with the exception of the
2335 * "IAC BREAK" command and the double IAC.
2336 */
2337
2338 int i;
2339 int j = 0;
2340
2341 for (i = 0; i < *size; i++) {
2342 if (s->do_telnetopt > 1) {
2343 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2344 /* Double IAC means send an IAC */
2345 if (j != i)
2346 buf[j] = buf[i];
2347 j++;
2348 s->do_telnetopt = 1;
2349 } else {
2350 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2351 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002352 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002353 s->do_telnetopt++;
2354 }
2355 s->do_telnetopt++;
2356 }
2357 if (s->do_telnetopt >= 4) {
2358 s->do_telnetopt = 1;
2359 }
2360 } else {
2361 if ((unsigned char)buf[i] == IAC) {
2362 s->do_telnetopt = 2;
2363 } else {
2364 if (j != i)
2365 buf[j] = buf[i];
2366 j++;
2367 }
2368 }
2369 }
2370 *size = j;
2371}
2372
Mark McLoughlin7d174052009-07-22 09:11:39 +01002373static int tcp_get_msgfd(CharDriverState *chr)
2374{
2375 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002376 int fd = s->msgfd;
2377 s->msgfd = -1;
2378 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002379}
2380
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002381#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002382static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2383{
2384 TCPCharDriver *s = chr->opaque;
2385 struct cmsghdr *cmsg;
2386
2387 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2388 int fd;
2389
2390 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2391 cmsg->cmsg_level != SOL_SOCKET ||
2392 cmsg->cmsg_type != SCM_RIGHTS)
2393 continue;
2394
2395 fd = *((int *)CMSG_DATA(cmsg));
2396 if (fd < 0)
2397 continue;
2398
Corey Bryant06138652012-08-14 16:43:42 -04002399#ifndef MSG_CMSG_CLOEXEC
2400 qemu_set_cloexec(fd);
2401#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002402 if (s->msgfd != -1)
2403 close(s->msgfd);
2404 s->msgfd = fd;
2405 }
2406}
2407
Mark McLoughlin9977c892009-07-22 09:11:38 +01002408static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2409{
2410 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002411 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002412 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002413 union {
2414 struct cmsghdr cmsg;
2415 char control[CMSG_SPACE(sizeof(int))];
2416 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002417 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002418 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002419
2420 iov[0].iov_base = buf;
2421 iov[0].iov_len = len;
2422
2423 msg.msg_iov = iov;
2424 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002425 msg.msg_control = &msg_control;
2426 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002427
Corey Bryant06138652012-08-14 16:43:42 -04002428#ifdef MSG_CMSG_CLOEXEC
2429 flags |= MSG_CMSG_CLOEXEC;
2430#endif
2431 ret = recvmsg(s->fd, &msg, flags);
2432 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002433 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002434 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002435
2436 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002437}
2438#else
2439static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2440{
2441 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002442 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002443}
2444#endif
2445
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302446static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002447{
2448 CharDriverState *chr = opaque;
2449 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302450 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002451 int len, size;
2452
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302453 if (!s->connected || s->max_size <= 0) {
2454 return FALSE;
2455 }
aliguori6f97dba2008-10-31 18:49:55 +00002456 len = sizeof(buf);
2457 if (len > s->max_size)
2458 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002459 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002460 if (size == 0) {
2461 /* connection closed */
2462 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302463 if (s->listen_chan) {
2464 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002465 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302466 g_source_remove(s->tag);
2467 s->tag = 0;
2468 g_io_channel_unref(s->chan);
2469 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002470 closesocket(s->fd);
2471 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002472 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002473 } else if (size > 0) {
2474 if (s->do_telnetopt)
2475 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2476 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002477 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002478 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302479
2480 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002481}
2482
Blue Swirl68c18d12010-08-15 09:46:24 +00002483#ifndef _WIN32
2484CharDriverState *qemu_chr_open_eventfd(int eventfd)
2485{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002486 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002487}
Blue Swirl68c18d12010-08-15 09:46:24 +00002488#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002489
aliguori6f97dba2008-10-31 18:49:55 +00002490static void tcp_chr_connect(void *opaque)
2491{
2492 CharDriverState *chr = opaque;
2493 TCPCharDriver *s = chr->opaque;
2494
2495 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302496 if (s->chan) {
2497 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002498 }
Amit Shah127338e2009-11-03 19:59:56 +05302499 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002500}
2501
2502#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2503static void tcp_chr_telnet_init(int fd)
2504{
2505 char buf[3];
2506 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2507 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2508 send(fd, (char *)buf, 3, 0);
2509 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2510 send(fd, (char *)buf, 3, 0);
2511 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2512 send(fd, (char *)buf, 3, 0);
2513 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2514 send(fd, (char *)buf, 3, 0);
2515}
2516
Daniel P. Berrange13661082011-06-23 13:31:42 +01002517static int tcp_chr_add_client(CharDriverState *chr, int fd)
2518{
2519 TCPCharDriver *s = chr->opaque;
2520 if (s->fd != -1)
2521 return -1;
2522
2523 socket_set_nonblock(fd);
2524 if (s->do_nodelay)
2525 socket_set_nodelay(fd);
2526 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302527 s->chan = io_channel_from_socket(fd);
2528 g_source_remove(s->listen_tag);
2529 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002530 tcp_chr_connect(chr);
2531
2532 return 0;
2533}
2534
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302535static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002536{
2537 CharDriverState *chr = opaque;
2538 TCPCharDriver *s = chr->opaque;
2539 struct sockaddr_in saddr;
2540#ifndef _WIN32
2541 struct sockaddr_un uaddr;
2542#endif
2543 struct sockaddr *addr;
2544 socklen_t len;
2545 int fd;
2546
2547 for(;;) {
2548#ifndef _WIN32
2549 if (s->is_unix) {
2550 len = sizeof(uaddr);
2551 addr = (struct sockaddr *)&uaddr;
2552 } else
2553#endif
2554 {
2555 len = sizeof(saddr);
2556 addr = (struct sockaddr *)&saddr;
2557 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002558 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002559 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302560 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002561 } else if (fd >= 0) {
2562 if (s->do_telnetopt)
2563 tcp_chr_telnet_init(fd);
2564 break;
2565 }
2566 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002567 if (tcp_chr_add_client(chr, fd) < 0)
2568 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302569
2570 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002571}
2572
2573static void tcp_chr_close(CharDriverState *chr)
2574{
2575 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002576 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302577 if (s->tag) {
2578 g_source_remove(s->tag);
2579 }
2580 if (s->chan) {
2581 g_io_channel_unref(s->chan);
2582 }
aliguori6f97dba2008-10-31 18:49:55 +00002583 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002584 }
2585 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302586 if (s->listen_tag) {
2587 g_source_remove(s->listen_tag);
2588 }
2589 if (s->listen_chan) {
2590 g_io_channel_unref(s->listen_chan);
2591 }
aliguori6f97dba2008-10-31 18:49:55 +00002592 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002593 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002594 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002595 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002596}
2597
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002598static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2599 bool is_listen, bool is_telnet,
2600 bool is_waitconnect,
2601 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002602{
2603 CharDriverState *chr = NULL;
2604 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002605 char host[NI_MAXHOST], serv[NI_MAXSERV];
2606 const char *left = "", *right = "";
2607 struct sockaddr_storage ss;
2608 socklen_t ss_len = sizeof(ss);
2609
2610 memset(&ss, 0, ss_len);
2611 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2612 error_setg(errp, "getsockname: %s", strerror(errno));
2613 return NULL;
2614 }
2615
2616 chr = g_malloc0(sizeof(CharDriverState));
2617 s = g_malloc0(sizeof(TCPCharDriver));
2618
2619 s->connected = 0;
2620 s->fd = -1;
2621 s->listen_fd = -1;
2622 s->msgfd = -1;
2623
2624 chr->filename = g_malloc(256);
2625 switch (ss.ss_family) {
2626#ifndef _WIN32
2627 case AF_UNIX:
2628 s->is_unix = 1;
2629 snprintf(chr->filename, 256, "unix:%s%s",
2630 ((struct sockaddr_un *)(&ss))->sun_path,
2631 is_listen ? ",server" : "");
2632 break;
2633#endif
2634 case AF_INET6:
2635 left = "[";
2636 right = "]";
2637 /* fall through */
2638 case AF_INET:
2639 s->do_nodelay = do_nodelay;
2640 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2641 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2642 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2643 is_telnet ? "telnet" : "tcp",
2644 left, host, right, serv,
2645 is_listen ? ",server" : "");
2646 break;
2647 }
2648
2649 chr->opaque = s;
2650 chr->chr_write = tcp_chr_write;
2651 chr->chr_close = tcp_chr_close;
2652 chr->get_msgfd = tcp_get_msgfd;
2653 chr->chr_add_client = tcp_chr_add_client;
2654
2655 if (is_listen) {
2656 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302657 s->listen_chan = io_channel_from_socket(s->listen_fd);
2658 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002659 if (is_telnet) {
2660 s->do_telnetopt = 1;
2661 }
2662 } else {
2663 s->connected = 1;
2664 s->fd = fd;
2665 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302666 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002667 tcp_chr_connect(chr);
2668 }
2669
2670 if (is_listen && is_waitconnect) {
2671 printf("QEMU waiting for connection on: %s\n",
2672 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302673 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002674 socket_set_nonblock(s->listen_fd);
2675 }
2676 return chr;
2677}
2678
2679static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2680{
2681 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002682 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002683 int fd = -1;
2684 int is_listen;
2685 int is_waitconnect;
2686 int do_nodelay;
2687 int is_unix;
2688 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002689
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002690 is_listen = qemu_opt_get_bool(opts, "server", 0);
2691 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2692 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2693 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2694 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002695 if (!is_listen)
2696 is_waitconnect = 0;
2697
aliguorif07b6002008-11-11 20:54:09 +00002698 if (is_unix) {
2699 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002700 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002701 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002702 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002703 }
2704 } else {
2705 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002706 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002707 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002708 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002709 }
2710 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002711 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002712 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002713 }
aliguori6f97dba2008-10-31 18:49:55 +00002714
2715 if (!is_waitconnect)
2716 socket_set_nonblock(fd);
2717
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002718 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2719 is_waitconnect, &local_err);
2720 if (error_is_set(&local_err)) {
2721 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002722 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002723 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002724
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002725
aliguori6f97dba2008-10-31 18:49:55 +00002726 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002727 if (local_err) {
2728 qerror_report_err(local_err);
2729 error_free(local_err);
2730 }
2731 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002732 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002733 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002734 if (chr) {
2735 g_free(chr->opaque);
2736 g_free(chr);
2737 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002738 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002739}
2740
Luiz Capitulino999bd672010-10-22 16:09:05 -02002741/***********************************************************/
2742/* Memory chardev */
2743typedef struct {
2744 size_t outbuf_size;
2745 size_t outbuf_capacity;
2746 uint8_t *outbuf;
2747} MemoryDriver;
2748
2749static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2750{
2751 MemoryDriver *d = chr->opaque;
2752
2753 /* TODO: the QString implementation has the same code, we should
2754 * introduce a generic way to do this in cutils.c */
2755 if (d->outbuf_capacity < d->outbuf_size + len) {
2756 /* grow outbuf */
2757 d->outbuf_capacity += len;
2758 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002759 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002760 }
2761
2762 memcpy(d->outbuf + d->outbuf_size, buf, len);
2763 d->outbuf_size += len;
2764
2765 return len;
2766}
2767
2768void qemu_chr_init_mem(CharDriverState *chr)
2769{
2770 MemoryDriver *d;
2771
Anthony Liguori7267c092011-08-20 22:09:37 -05002772 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002773 d->outbuf_size = 0;
2774 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002775 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002776
2777 memset(chr, 0, sizeof(*chr));
2778 chr->opaque = d;
2779 chr->chr_write = mem_chr_write;
2780}
2781
2782QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2783{
2784 MemoryDriver *d = chr->opaque;
2785 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2786}
2787
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002788/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002789void qemu_chr_close_mem(CharDriverState *chr)
2790{
2791 MemoryDriver *d = chr->opaque;
2792
Anthony Liguori7267c092011-08-20 22:09:37 -05002793 g_free(d->outbuf);
2794 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002795 chr->opaque = NULL;
2796 chr->chr_write = NULL;
2797}
2798
2799size_t qemu_chr_mem_osize(const CharDriverState *chr)
2800{
2801 const MemoryDriver *d = chr->opaque;
2802 return d->outbuf_size;
2803}
2804
Lei Li51767e72013-01-25 00:03:19 +08002805/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002806/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002807
2808typedef struct {
2809 size_t size;
2810 size_t prod;
2811 size_t cons;
2812 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002813} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002814
Markus Armbruster3949e592013-02-06 21:27:24 +01002815static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002816{
Markus Armbruster3949e592013-02-06 21:27:24 +01002817 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002818
Markus Armbruster5c230102013-02-06 21:27:23 +01002819 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002820}
2821
Markus Armbruster3949e592013-02-06 21:27:24 +01002822static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002823{
Markus Armbruster3949e592013-02-06 21:27:24 +01002824 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002825 int i;
2826
2827 if (!buf || (len < 0)) {
2828 return -1;
2829 }
2830
2831 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002832 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2833 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002834 d->cons = d->prod - d->size;
2835 }
2836 }
2837
2838 return 0;
2839}
2840
Markus Armbruster3949e592013-02-06 21:27:24 +01002841static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002842{
Markus Armbruster3949e592013-02-06 21:27:24 +01002843 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002844 int i;
2845
Markus Armbruster5c230102013-02-06 21:27:23 +01002846 for (i = 0; i < len && d->cons != d->prod; i++) {
2847 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002848 }
2849
2850 return i;
2851}
2852
Markus Armbruster3949e592013-02-06 21:27:24 +01002853static void ringbuf_chr_close(struct CharDriverState *chr)
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
2857 g_free(d->cbuf);
2858 g_free(d);
2859 chr->opaque = NULL;
2860}
2861
Markus Armbruster3949e592013-02-06 21:27:24 +01002862static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002863{
2864 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002865 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002866
2867 chr = g_malloc0(sizeof(CharDriverState));
2868 d = g_malloc(sizeof(*d));
2869
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002870 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002871 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002872 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002873 }
2874
2875 /* The size must be power of 2 */
2876 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002877 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002878 goto fail;
2879 }
2880
2881 d->prod = 0;
2882 d->cons = 0;
2883 d->cbuf = g_malloc0(d->size);
2884
2885 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002886 chr->chr_write = ringbuf_chr_write;
2887 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002888
2889 return chr;
2890
2891fail:
2892 g_free(d);
2893 g_free(chr);
2894 return NULL;
2895}
2896
Markus Armbruster3949e592013-02-06 21:27:24 +01002897static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002898{
Markus Armbruster3949e592013-02-06 21:27:24 +01002899 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002900}
2901
Markus Armbruster3949e592013-02-06 21:27:24 +01002902void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002903 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002904 Error **errp)
2905{
2906 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002907 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002908 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002909 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002910
2911 chr = qemu_chr_find(device);
2912 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002913 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002914 return;
2915 }
2916
Markus Armbruster3949e592013-02-06 21:27:24 +01002917 if (!chr_is_ringbuf(chr)) {
2918 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002919 return;
2920 }
2921
Lei Li1f590cf2013-01-25 00:03:20 +08002922 if (has_format && (format == DATA_FORMAT_BASE64)) {
2923 write_data = g_base64_decode(data, &write_count);
2924 } else {
2925 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002926 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002927 }
2928
Markus Armbruster3949e592013-02-06 21:27:24 +01002929 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002930
Markus Armbruster13289fb2013-02-06 21:27:18 +01002931 if (write_data != (uint8_t *)data) {
2932 g_free((void *)write_data);
2933 }
2934
Lei Li1f590cf2013-01-25 00:03:20 +08002935 if (ret < 0) {
2936 error_setg(errp, "Failed to write to device %s", device);
2937 return;
2938 }
2939}
2940
Markus Armbruster3949e592013-02-06 21:27:24 +01002941char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002942 bool has_format, enum DataFormat format,
2943 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002944{
2945 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002946 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002947 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002948 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002949
2950 chr = qemu_chr_find(device);
2951 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002952 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002953 return NULL;
2954 }
2955
Markus Armbruster3949e592013-02-06 21:27:24 +01002956 if (!chr_is_ringbuf(chr)) {
2957 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002958 return NULL;
2959 }
2960
2961 if (size <= 0) {
2962 error_setg(errp, "size must be greater than zero");
2963 return NULL;
2964 }
2965
Markus Armbruster3949e592013-02-06 21:27:24 +01002966 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002967 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002968 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002969
Markus Armbruster3949e592013-02-06 21:27:24 +01002970 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002971
2972 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002973 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002974 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002975 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002976 /*
2977 * FIXME should read only complete, valid UTF-8 characters up
2978 * to @size bytes. Invalid sequences should be replaced by a
2979 * suitable replacement character. Except when (and only
2980 * when) ring buffer lost characters since last read, initial
2981 * continuation characters should be dropped.
2982 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002983 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002984 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002985 }
2986
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002987 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002988}
2989
Gerd Hoffmann33521632009-12-08 13:11:49 +01002990QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002991{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002992 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002993 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002994 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002995 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002996 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002997
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002998 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2999 if (error_is_set(&local_err)) {
3000 qerror_report_err(local_err);
3001 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003002 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003003 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003004
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003005 if (strstart(filename, "mon:", &p)) {
3006 filename = p;
3007 qemu_opt_set(opts, "mux", "on");
3008 }
3009
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003010 if (strcmp(filename, "null") == 0 ||
3011 strcmp(filename, "pty") == 0 ||
3012 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003013 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003014 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003015 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003016 return opts;
3017 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003018 if (strstart(filename, "vc", &p)) {
3019 qemu_opt_set(opts, "backend", "vc");
3020 if (*p == ':') {
3021 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3022 /* pixels */
3023 qemu_opt_set(opts, "width", width);
3024 qemu_opt_set(opts, "height", height);
3025 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3026 /* chars */
3027 qemu_opt_set(opts, "cols", width);
3028 qemu_opt_set(opts, "rows", height);
3029 } else {
3030 goto fail;
3031 }
3032 }
3033 return opts;
3034 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003035 if (strcmp(filename, "con:") == 0) {
3036 qemu_opt_set(opts, "backend", "console");
3037 return opts;
3038 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003039 if (strstart(filename, "COM", NULL)) {
3040 qemu_opt_set(opts, "backend", "serial");
3041 qemu_opt_set(opts, "path", filename);
3042 return opts;
3043 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003044 if (strstart(filename, "file:", &p)) {
3045 qemu_opt_set(opts, "backend", "file");
3046 qemu_opt_set(opts, "path", p);
3047 return opts;
3048 }
3049 if (strstart(filename, "pipe:", &p)) {
3050 qemu_opt_set(opts, "backend", "pipe");
3051 qemu_opt_set(opts, "path", p);
3052 return opts;
3053 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003054 if (strstart(filename, "tcp:", &p) ||
3055 strstart(filename, "telnet:", &p)) {
3056 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3057 host[0] = 0;
3058 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3059 goto fail;
3060 }
3061 qemu_opt_set(opts, "backend", "socket");
3062 qemu_opt_set(opts, "host", host);
3063 qemu_opt_set(opts, "port", port);
3064 if (p[pos] == ',') {
3065 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3066 goto fail;
3067 }
3068 if (strstart(filename, "telnet:", &p))
3069 qemu_opt_set(opts, "telnet", "on");
3070 return opts;
3071 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003072 if (strstart(filename, "udp:", &p)) {
3073 qemu_opt_set(opts, "backend", "udp");
3074 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3075 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003076 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003077 goto fail;
3078 }
3079 }
3080 qemu_opt_set(opts, "host", host);
3081 qemu_opt_set(opts, "port", port);
3082 if (p[pos] == '@') {
3083 p += pos + 1;
3084 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3085 host[0] = 0;
3086 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003087 goto fail;
3088 }
3089 }
3090 qemu_opt_set(opts, "localaddr", host);
3091 qemu_opt_set(opts, "localport", port);
3092 }
3093 return opts;
3094 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003095 if (strstart(filename, "unix:", &p)) {
3096 qemu_opt_set(opts, "backend", "socket");
3097 if (qemu_opts_do_parse(opts, p, "path") != 0)
3098 goto fail;
3099 return opts;
3100 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003101 if (strstart(filename, "/dev/parport", NULL) ||
3102 strstart(filename, "/dev/ppi", NULL)) {
3103 qemu_opt_set(opts, "backend", "parport");
3104 qemu_opt_set(opts, "path", filename);
3105 return opts;
3106 }
3107 if (strstart(filename, "/dev/", NULL)) {
3108 qemu_opt_set(opts, "backend", "tty");
3109 qemu_opt_set(opts, "path", filename);
3110 return opts;
3111 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003112
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003113fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003114 qemu_opts_del(opts);
3115 return NULL;
3116}
3117
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003118#ifdef HAVE_CHARDEV_PARPORT
3119
3120static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3121{
3122 const char *filename = qemu_opt_get(opts, "path");
3123 int fd;
3124
3125 fd = qemu_open(filename, O_RDWR);
3126 if (fd < 0) {
3127 return NULL;
3128 }
3129 return qemu_chr_open_pp_fd(fd);
3130}
3131
3132#endif
3133
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003134static const struct {
3135 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01003136 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003137} backend_table[] = {
3138 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003139 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003140 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003141 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Anthony Liguorid82831d2013-02-20 07:43:19 -06003142 { .name = "vc", .open = vc_init },
Markus Armbruster3949e592013-02-06 21:27:24 +01003143 { .name = "memory", .open = qemu_chr_open_ringbuf },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003144#ifdef _WIN32
3145 { .name = "file", .open = qemu_chr_open_win_file_out },
3146 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003147 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003148 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02003149 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003150#else
3151 { .name = "file", .open = qemu_chr_open_file_out },
3152 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02003153 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003154#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003155#ifdef CONFIG_BRLAPI
3156 { .name = "braille", .open = chr_baum_init },
3157#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003158#ifdef HAVE_CHARDEV_TTY
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003159 { .name = "tty", .open = qemu_chr_open_tty },
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003160 { .name = "serial", .open = qemu_chr_open_tty },
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003161 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003162#endif
Gerd Hoffmanne5514982012-12-19 16:35:42 +01003163#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003164 { .name = "parallel", .open = qemu_chr_open_pp },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003165 { .name = "parport", .open = qemu_chr_open_pp },
3166#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003167#ifdef CONFIG_SPICE
3168 { .name = "spicevmc", .open = qemu_chr_open_spice },
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01003169#if SPICE_SERVER_VERSION >= 0x000c02
3170 { .name = "spiceport", .open = qemu_chr_open_spice_port },
3171#endif
Alon Levycbcc6332011-01-19 10:49:50 +02003172#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003173};
3174
Anthony Liguorif69554b2011-08-15 11:17:37 -05003175CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003176 void (*init)(struct CharDriverState *s),
3177 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003178{
3179 CharDriverState *chr;
3180 int i;
3181
3182 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003183 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003184 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003185 }
3186
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003187 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003188 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003189 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003190 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003191 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003192 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
3193 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
3194 break;
3195 }
3196 if (i == ARRAY_SIZE(backend_table)) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003197 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003198 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003199 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003200 }
3201
Markus Armbruster1f514702012-02-07 15:09:08 +01003202 chr = backend_table[i].open(opts);
3203 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003204 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003205 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003206 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003207 }
3208
3209 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003210 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003211 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003212 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003213
3214 if (qemu_opt_get_bool(opts, "mux", 0)) {
3215 CharDriverState *base = chr;
3216 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003217 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003218 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3219 chr = qemu_chr_open_mux(base);
3220 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003221 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003222 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003223 } else {
3224 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003225 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003226 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003227 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003228 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003229
3230err:
3231 qemu_opts_del(opts);
3232 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003233}
3234
Anthony Liguori27143a42011-08-15 11:17:36 -05003235CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003236{
3237 const char *p;
3238 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003239 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003240 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003241
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003242 if (strstart(filename, "chardev:", &p)) {
3243 return qemu_chr_find(p);
3244 }
3245
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003246 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003247 if (!opts)
3248 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003249
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003250 chr = qemu_chr_new_from_opts(opts, init, &err);
3251 if (error_is_set(&err)) {
3252 fprintf(stderr, "%s\n", error_get_pretty(err));
3253 error_free(err);
3254 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003255 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3256 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003257 }
3258 return chr;
3259}
3260
Anthony Liguori15f31512011-08-15 11:17:35 -05003261void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003262{
3263 if (chr->chr_set_echo) {
3264 chr->chr_set_echo(chr, echo);
3265 }
3266}
3267
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003268void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003269{
3270 if (chr->chr_guest_open) {
3271 chr->chr_guest_open(chr);
3272 }
3273}
3274
Anthony Liguori28178222011-08-15 11:17:33 -05003275void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003276{
3277 if (chr->chr_guest_close) {
3278 chr->chr_guest_close(chr);
3279 }
3280}
3281
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003282void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003283{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003284 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003285 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003286 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003287 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003288 g_free(chr->filename);
3289 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003290 if (chr->opts) {
3291 qemu_opts_del(chr->opts);
3292 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003293 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003294}
3295
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003296ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003297{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003298 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003299 CharDriverState *chr;
3300
Blue Swirl72cf2d42009-09-12 07:36:22 +00003301 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003302 ChardevInfoList *info = g_malloc0(sizeof(*info));
3303 info->value = g_malloc0(sizeof(*info->value));
3304 info->value->label = g_strdup(chr->label);
3305 info->value->filename = g_strdup(chr->filename);
3306
3307 info->next = chr_list;
3308 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003309 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003310
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003311 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003312}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003313
3314CharDriverState *qemu_chr_find(const char *name)
3315{
3316 CharDriverState *chr;
3317
Blue Swirl72cf2d42009-09-12 07:36:22 +00003318 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003319 if (strcmp(chr->label, name) != 0)
3320 continue;
3321 return chr;
3322 }
3323 return NULL;
3324}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003325
3326/* Get a character (serial) device interface. */
3327CharDriverState *qemu_char_get_next_serial(void)
3328{
3329 static int next_serial;
3330
3331 /* FIXME: This function needs to go away: use chardev properties! */
3332 return serial_hds[next_serial++];
3333}
3334
Paolo Bonzini4d454572012-11-26 16:03:42 +01003335QemuOptsList qemu_chardev_opts = {
3336 .name = "chardev",
3337 .implied_opt_name = "backend",
3338 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3339 .desc = {
3340 {
3341 .name = "backend",
3342 .type = QEMU_OPT_STRING,
3343 },{
3344 .name = "path",
3345 .type = QEMU_OPT_STRING,
3346 },{
3347 .name = "host",
3348 .type = QEMU_OPT_STRING,
3349 },{
3350 .name = "port",
3351 .type = QEMU_OPT_STRING,
3352 },{
3353 .name = "localaddr",
3354 .type = QEMU_OPT_STRING,
3355 },{
3356 .name = "localport",
3357 .type = QEMU_OPT_STRING,
3358 },{
3359 .name = "to",
3360 .type = QEMU_OPT_NUMBER,
3361 },{
3362 .name = "ipv4",
3363 .type = QEMU_OPT_BOOL,
3364 },{
3365 .name = "ipv6",
3366 .type = QEMU_OPT_BOOL,
3367 },{
3368 .name = "wait",
3369 .type = QEMU_OPT_BOOL,
3370 },{
3371 .name = "server",
3372 .type = QEMU_OPT_BOOL,
3373 },{
3374 .name = "delay",
3375 .type = QEMU_OPT_BOOL,
3376 },{
3377 .name = "telnet",
3378 .type = QEMU_OPT_BOOL,
3379 },{
3380 .name = "width",
3381 .type = QEMU_OPT_NUMBER,
3382 },{
3383 .name = "height",
3384 .type = QEMU_OPT_NUMBER,
3385 },{
3386 .name = "cols",
3387 .type = QEMU_OPT_NUMBER,
3388 },{
3389 .name = "rows",
3390 .type = QEMU_OPT_NUMBER,
3391 },{
3392 .name = "mux",
3393 .type = QEMU_OPT_BOOL,
3394 },{
3395 .name = "signal",
3396 .type = QEMU_OPT_BOOL,
3397 },{
3398 .name = "name",
3399 .type = QEMU_OPT_STRING,
3400 },{
3401 .name = "debug",
3402 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003403 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003404 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003405 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003406 },
3407 { /* end of list */ }
3408 },
3409};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003410
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003411#ifdef _WIN32
3412
3413static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3414{
3415 HANDLE out;
3416
3417 if (file->in) {
3418 error_setg(errp, "input file not supported");
3419 return NULL;
3420 }
3421
3422 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3423 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3424 if (out == INVALID_HANDLE_VALUE) {
3425 error_setg(errp, "open %s failed", file->out);
3426 return NULL;
3427 }
3428 return qemu_chr_open_win_file(out);
3429}
3430
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003431static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3432 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003433{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003434 return qemu_chr_open_win_path(serial->device);
3435}
3436
3437static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3438 Error **errp)
3439{
3440 error_setg(errp, "character device backend type 'parallel' not supported");
3441 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003442}
3443
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003444#else /* WIN32 */
3445
3446static int qmp_chardev_open_file_source(char *src, int flags,
3447 Error **errp)
3448{
3449 int fd = -1;
3450
3451 TFR(fd = qemu_open(src, flags, 0666));
3452 if (fd == -1) {
3453 error_setg(errp, "open %s: %s", src, strerror(errno));
3454 }
3455 return fd;
3456}
3457
3458static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3459{
3460 int flags, in = -1, out = -1;
3461
3462 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3463 out = qmp_chardev_open_file_source(file->out, flags, errp);
3464 if (error_is_set(errp)) {
3465 return NULL;
3466 }
3467
3468 if (file->in) {
3469 flags = O_RDONLY;
3470 in = qmp_chardev_open_file_source(file->in, flags, errp);
3471 if (error_is_set(errp)) {
3472 qemu_close(out);
3473 return NULL;
3474 }
3475 }
3476
3477 return qemu_chr_open_fd(in, out);
3478}
3479
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003480static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3481 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003482{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003483#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003484 int fd;
3485
3486 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3487 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003488 return NULL;
3489 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003490 socket_set_nonblock(fd);
3491 return qemu_chr_open_tty_fd(fd);
3492#else
3493 error_setg(errp, "character device backend type 'serial' not supported");
3494 return NULL;
3495#endif
3496}
3497
3498static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3499 Error **errp)
3500{
3501#ifdef HAVE_CHARDEV_PARPORT
3502 int fd;
3503
3504 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3505 if (error_is_set(errp)) {
3506 return NULL;
3507 }
3508 return qemu_chr_open_pp_fd(fd);
3509#else
3510 error_setg(errp, "character device backend type 'parallel' not supported");
3511 return NULL;
3512#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003513}
3514
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003515#endif /* WIN32 */
3516
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003517static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3518 Error **errp)
3519{
3520 SocketAddress *addr = sock->addr;
3521 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3522 bool is_listen = sock->has_server ? sock->server : true;
3523 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3524 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3525 int fd;
3526
3527 if (is_listen) {
3528 fd = socket_listen(addr, errp);
3529 } else {
3530 fd = socket_connect(addr, errp, NULL, NULL);
3531 }
3532 if (error_is_set(errp)) {
3533 return NULL;
3534 }
3535 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3536 is_telnet, is_waitconnect, errp);
3537}
3538
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003539ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3540 Error **errp)
3541{
3542 ChardevReturn *ret = g_new0(ChardevReturn, 1);
3543 CharDriverState *chr = NULL;
3544
3545 chr = qemu_chr_find(id);
3546 if (chr) {
3547 error_setg(errp, "Chardev '%s' already exists", id);
3548 g_free(ret);
3549 return NULL;
3550 }
3551
3552 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003553 case CHARDEV_BACKEND_KIND_FILE:
3554 chr = qmp_chardev_open_file(backend->file, errp);
3555 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003556 case CHARDEV_BACKEND_KIND_SERIAL:
3557 chr = qmp_chardev_open_serial(backend->serial, errp);
3558 break;
3559 case CHARDEV_BACKEND_KIND_PARALLEL:
3560 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003561 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003562 case CHARDEV_BACKEND_KIND_SOCKET:
3563 chr = qmp_chardev_open_socket(backend->socket, errp);
3564 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003565#ifdef HAVE_CHARDEV_TTY
3566 case CHARDEV_BACKEND_KIND_PTY:
3567 {
3568 /* qemu_chr_open_pty sets "path" in opts */
3569 QemuOpts *opts;
3570 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3571 chr = qemu_chr_open_pty(opts);
3572 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3573 ret->has_pty = true;
3574 qemu_opts_del(opts);
3575 break;
3576 }
3577#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003578 case CHARDEV_BACKEND_KIND_NULL:
3579 chr = qemu_chr_open_null(NULL);
3580 break;
3581 default:
3582 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3583 break;
3584 }
3585
3586 if (chr == NULL && !error_is_set(errp)) {
3587 error_setg(errp, "Failed to create chardev");
3588 }
3589 if (chr) {
3590 chr->label = g_strdup(id);
3591 chr->avail_connections = 1;
3592 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3593 return ret;
3594 } else {
3595 g_free(ret);
3596 return NULL;
3597 }
3598}
3599
3600void qmp_chardev_remove(const char *id, Error **errp)
3601{
3602 CharDriverState *chr;
3603
3604 chr = qemu_chr_find(id);
3605 if (NULL == chr) {
3606 error_setg(errp, "Chardev '%s' not found", id);
3607 return;
3608 }
3609 if (chr->chr_can_read || chr->chr_read ||
3610 chr->chr_event || chr->handler_opaque) {
3611 error_setg(errp, "Chardev '%s' is busy", id);
3612 return;
3613 }
3614 qemu_chr_delete(chr);
3615}