blob: e6337971a577405b7fd91e1bf1ec486061d3b798 [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"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__GLIBC__)
57#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040058#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000062#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040063#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64#include <dev/ppbus/ppi.h>
65#include <dev/ppbus/ppbconf.h>
66#elif defined(__DragonFly__)
67#include <dev/misc/ppi/ppi.h>
68#include <bus/ppbus/ppbconf.h>
69#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010070#else
aliguori6f97dba2008-10-31 18:49:55 +000071#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000072#include <pty.h>
73
74#include <linux/ppdev.h>
75#include <linux/parport.h>
76#endif
77#ifdef __sun__
78#include <sys/stat.h>
79#include <sys/ethernet.h>
80#include <sys/sockio.h>
81#include <netinet/arp.h>
82#include <netinet/in.h>
83#include <netinet/in_systm.h>
84#include <netinet/ip.h>
85#include <netinet/ip_icmp.h> // must come after ip.h
86#include <netinet/udp.h>
87#include <netinet/tcp.h>
88#include <net/if.h>
89#include <syslog.h>
90#include <stropts.h>
91#endif
92#endif
93#endif
94
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010095#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020096#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000097
Amit Shah9bd78542009-11-03 19:59:54 +053098#define READ_BUF_LEN 4096
99
aliguori6f97dba2008-10-31 18:49:55 +0000100/***********************************************************/
101/* character device */
102
Blue Swirl72cf2d42009-09-12 07:36:22 +0000103static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000105
Hans de Goedea425d232011-11-19 10:22:43 +0100106void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000107{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200108 /* Keep track if the char device is open */
109 switch (event) {
110 case CHR_EVENT_OPENED:
111 s->opened = 1;
112 break;
113 case CHR_EVENT_CLOSED:
114 s->opened = 0;
115 break;
116 }
117
aliguori6f97dba2008-10-31 18:49:55 +0000118 if (!s->chr_event)
119 return;
120 s->chr_event(s->handler_opaque, event);
121}
122
Anthony Liguori9f939df2013-03-05 23:21:27 +0530123static gboolean qemu_chr_generic_open_bh(gpointer opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000124{
125 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100126 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Anthony Liguori9f939df2013-03-05 23:21:27 +0530127 s->idle_tag = 0;
128 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +0000129}
130
Amit Shah127338e2009-11-03 19:59:56 +0530131void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000132{
Anthony Liguori9f939df2013-03-05 23:21:27 +0530133 if (s->idle_tag == 0) {
134 s->idle_tag = g_idle_add(qemu_chr_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000135 }
136}
137
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500138int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000139{
140 return s->chr_write(s, buf, len);
141}
142
Anthony Liguori41084f12011-08-15 11:17:34 -0500143int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000144{
145 if (!s->chr_ioctl)
146 return -ENOTSUP;
147 return s->chr_ioctl(s, cmd, arg);
148}
149
Anthony Liguori909cda12011-08-15 11:17:31 -0500150int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000151{
152 if (!s->chr_can_read)
153 return 0;
154 return s->chr_can_read(s->handler_opaque);
155}
156
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500157void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000158{
Stefan Weilac310732012-04-19 22:27:14 +0200159 if (s->chr_read) {
160 s->chr_read(s->handler_opaque, buf, len);
161 }
aliguori6f97dba2008-10-31 18:49:55 +0000162}
163
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500164int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100165{
166 return s->get_msgfd ? s->get_msgfd(s) : -1;
167}
168
Daniel P. Berrange13661082011-06-23 13:31:42 +0100169int qemu_chr_add_client(CharDriverState *s, int fd)
170{
171 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
172}
173
aliguori6f97dba2008-10-31 18:49:55 +0000174void qemu_chr_accept_input(CharDriverState *s)
175{
176 if (s->chr_accept_input)
177 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100178 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000179}
180
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500181void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000182{
Amit Shah9bd78542009-11-03 19:59:54 +0530183 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000184 va_list ap;
185 va_start(ap, fmt);
186 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500187 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000188 va_end(ap);
189}
190
aliguori6f97dba2008-10-31 18:49:55 +0000191void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100192 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000193 IOReadHandler *fd_read,
194 IOEventHandler *fd_event,
195 void *opaque)
196{
Amit Shahda7d9982011-04-25 15:18:22 +0530197 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530198 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900199 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530200 }
aliguori6f97dba2008-10-31 18:49:55 +0000201 s->chr_can_read = fd_can_read;
202 s->chr_read = fd_read;
203 s->chr_event = fd_event;
204 s->handler_opaque = opaque;
205 if (s->chr_update_read_handler)
206 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200207
208 /* We're connecting to an already opened device, so let's make sure we
209 also get the open event */
210 if (s->opened) {
211 qemu_chr_generic_open(s);
212 }
aliguori6f97dba2008-10-31 18:49:55 +0000213}
214
215static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
216{
217 return len;
218}
219
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100220static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000221{
222 CharDriverState *chr;
223
Anthony Liguori7267c092011-08-20 22:09:37 -0500224 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000225 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100226 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000227}
228
229/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000230#define MAX_MUX 4
231#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
232#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
233typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100234 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000235 IOReadHandler *chr_read[MAX_MUX];
236 IOEventHandler *chr_event[MAX_MUX];
237 void *ext_opaque[MAX_MUX];
238 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200239 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000240 int mux_cnt;
241 int term_got_escape;
242 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000243 /* Intermediate input buffer allows to catch escape sequences even if the
244 currently active device is not accepting any input - but only until it
245 is full as well. */
246 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
247 int prod[MAX_MUX];
248 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200249 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200250 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200251 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000252} MuxDriver;
253
254
255static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
256{
257 MuxDriver *d = chr->opaque;
258 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200259 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000260 ret = d->drv->chr_write(d->drv, buf, len);
261 } else {
262 int i;
263
264 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200265 for (i = 0; i < len; i++) {
266 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000267 char buf1[64];
268 int64_t ti;
269 int secs;
270
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100271 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200272 if (d->timestamps_start == -1)
273 d->timestamps_start = ti;
274 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000275 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000276 snprintf(buf1, sizeof(buf1),
277 "[%02d:%02d:%02d.%03d] ",
278 secs / 3600,
279 (secs / 60) % 60,
280 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000281 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000282 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200283 d->linestart = 0;
284 }
285 ret += d->drv->chr_write(d->drv, buf+i, 1);
286 if (buf[i] == '\n') {
287 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000288 }
289 }
290 }
291 return ret;
292}
293
294static const char * const mux_help[] = {
295 "% h print this help\n\r",
296 "% x exit emulator\n\r",
297 "% s save disk data back to file (if -snapshot)\n\r",
298 "% t toggle console timestamps\n\r"
299 "% b send break (magic sysrq)\n\r",
300 "% c switch between console and monitor\n\r",
301 "% % sends %\n\r",
302 NULL
303};
304
305int term_escape_char = 0x01; /* ctrl-a is used for escape */
306static void mux_print_help(CharDriverState *chr)
307{
308 int i, j;
309 char ebuf[15] = "Escape-Char";
310 char cbuf[50] = "\n\r";
311
312 if (term_escape_char > 0 && term_escape_char < 26) {
313 snprintf(cbuf, sizeof(cbuf), "\n\r");
314 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
315 } else {
316 snprintf(cbuf, sizeof(cbuf),
317 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
318 term_escape_char);
319 }
320 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
321 for (i = 0; mux_help[i] != NULL; i++) {
322 for (j=0; mux_help[i][j] != '\0'; j++) {
323 if (mux_help[i][j] == '%')
324 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
325 else
326 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
327 }
328 }
329}
330
aliguori2724b182009-03-05 23:01:47 +0000331static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
332{
333 if (d->chr_event[mux_nr])
334 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
335}
336
aliguori6f97dba2008-10-31 18:49:55 +0000337static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
338{
339 if (d->term_got_escape) {
340 d->term_got_escape = 0;
341 if (ch == term_escape_char)
342 goto send_char;
343 switch(ch) {
344 case '?':
345 case 'h':
346 mux_print_help(chr);
347 break;
348 case 'x':
349 {
350 const char *term = "QEMU: Terminated\n\r";
351 chr->chr_write(chr,(uint8_t *)term,strlen(term));
352 exit(0);
353 break;
354 }
355 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200356 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000357 break;
358 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100359 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000360 break;
361 case 'c':
362 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200363 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
364 d->focus++;
365 if (d->focus >= d->mux_cnt)
366 d->focus = 0;
367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000368 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200369 case 't':
370 d->timestamps = !d->timestamps;
371 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200372 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200373 break;
aliguori6f97dba2008-10-31 18:49:55 +0000374 }
375 } else if (ch == term_escape_char) {
376 d->term_got_escape = 1;
377 } else {
378 send_char:
379 return 1;
380 }
381 return 0;
382}
383
384static void mux_chr_accept_input(CharDriverState *chr)
385{
aliguori6f97dba2008-10-31 18:49:55 +0000386 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200387 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000388
aliguoria80bf992009-03-05 23:00:02 +0000389 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000390 d->chr_can_read[m] &&
391 d->chr_can_read[m](d->ext_opaque[m])) {
392 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000393 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000394 }
395}
396
397static int mux_chr_can_read(void *opaque)
398{
399 CharDriverState *chr = opaque;
400 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200401 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000402
aliguoria80bf992009-03-05 23:00:02 +0000403 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000404 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000405 if (d->chr_can_read[m])
406 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000407 return 0;
408}
409
410static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
411{
412 CharDriverState *chr = opaque;
413 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200414 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000415 int i;
416
417 mux_chr_accept_input (opaque);
418
419 for(i = 0; i < size; i++)
420 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000421 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000422 d->chr_can_read[m] &&
423 d->chr_can_read[m](d->ext_opaque[m]))
424 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
425 else
aliguoria80bf992009-03-05 23:00:02 +0000426 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000427 }
428}
429
430static void mux_chr_event(void *opaque, int event)
431{
432 CharDriverState *chr = opaque;
433 MuxDriver *d = chr->opaque;
434 int i;
435
436 /* Send the event to all registered listeners */
437 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000438 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000439}
440
441static void mux_chr_update_read_handler(CharDriverState *chr)
442{
443 MuxDriver *d = chr->opaque;
444
445 if (d->mux_cnt >= MAX_MUX) {
446 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
447 return;
448 }
449 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
450 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
451 d->chr_read[d->mux_cnt] = chr->chr_read;
452 d->chr_event[d->mux_cnt] = chr->chr_event;
453 /* Fix up the real driver with mux routines */
454 if (d->mux_cnt == 0) {
455 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
456 mux_chr_event, chr);
457 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200458 if (d->focus != -1) {
459 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200460 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200461 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000462 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000464}
465
466static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
467{
468 CharDriverState *chr;
469 MuxDriver *d;
470
Anthony Liguori7267c092011-08-20 22:09:37 -0500471 chr = g_malloc0(sizeof(CharDriverState));
472 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000473
474 chr->opaque = d;
475 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200476 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000477 chr->chr_write = mux_chr_write;
478 chr->chr_update_read_handler = mux_chr_update_read_handler;
479 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100480 /* Frontend guest-open / -close notification is not support with muxes */
481 chr->chr_guest_open = NULL;
482 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200483
484 /* Muxes are always open on creation */
485 qemu_chr_generic_open(chr);
486
aliguori6f97dba2008-10-31 18:49:55 +0000487 return chr;
488}
489
490
491#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000492int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000493{
494 int ret, len;
495
496 len = len1;
497 while (len > 0) {
498 ret = send(fd, buf, len, 0);
499 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000500 errno = WSAGetLastError();
501 if (errno != WSAEWOULDBLOCK) {
502 return -1;
503 }
504 } else if (ret == 0) {
505 break;
506 } else {
507 buf += ret;
508 len -= ret;
509 }
510 }
511 return len1 - len;
512}
513
514#else
515
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100516int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000517{
518 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100519 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000520
521 len = len1;
522 while (len > 0) {
523 ret = write(fd, buf, len);
524 if (ret < 0) {
525 if (errno != EINTR && errno != EAGAIN)
526 return -1;
527 } else if (ret == 0) {
528 break;
529 } else {
530 buf += ret;
531 len -= ret;
532 }
533 }
534 return len1 - len;
535}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500536
537int recv_all(int fd, void *_buf, int len1, bool single_read)
538{
539 int ret, len;
540 uint8_t *buf = _buf;
541
542 len = len1;
543 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
544 if (ret < 0) {
545 if (errno != EINTR && errno != EAGAIN) {
546 return -1;
547 }
548 continue;
549 } else {
550 if (single_read) {
551 return ret;
552 }
553 buf += ret;
554 len -= ret;
555 }
556 }
557 return len1 - len;
558}
559
aliguori6f97dba2008-10-31 18:49:55 +0000560#endif /* !_WIN32 */
561
Anthony Liguori96c63842013-03-05 23:21:18 +0530562typedef struct IOWatchPoll
563{
564 GSource *src;
565 int max_size;
566
567 IOCanReadHandler *fd_can_read;
568 void *opaque;
569
570 QTAILQ_ENTRY(IOWatchPoll) node;
571} IOWatchPoll;
572
573static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
574 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);
575
576static IOWatchPoll *io_watch_poll_from_source(GSource *source)
577{
578 IOWatchPoll *i;
579
580 QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
581 if (i->src == source) {
582 return i;
583 }
584 }
585
586 return NULL;
587}
588
589static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
590{
591 IOWatchPoll *iwp = io_watch_poll_from_source(source);
592
593 iwp->max_size = iwp->fd_can_read(iwp->opaque);
594 if (iwp->max_size == 0) {
595 return FALSE;
596 }
597
598 return g_io_watch_funcs.prepare(source, timeout_);
599}
600
601static gboolean io_watch_poll_check(GSource *source)
602{
603 IOWatchPoll *iwp = io_watch_poll_from_source(source);
604
605 if (iwp->max_size == 0) {
606 return FALSE;
607 }
608
609 return g_io_watch_funcs.check(source);
610}
611
612static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
613 gpointer user_data)
614{
615 return g_io_watch_funcs.dispatch(source, callback, user_data);
616}
617
618static void io_watch_poll_finalize(GSource *source)
619{
620 IOWatchPoll *iwp = io_watch_poll_from_source(source);
621 QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
622 g_io_watch_funcs.finalize(source);
623}
624
625static GSourceFuncs io_watch_poll_funcs = {
626 .prepare = io_watch_poll_prepare,
627 .check = io_watch_poll_check,
628 .dispatch = io_watch_poll_dispatch,
629 .finalize = io_watch_poll_finalize,
630};
631
632/* Can only be used for read */
633static guint io_add_watch_poll(GIOChannel *channel,
634 IOCanReadHandler *fd_can_read,
635 GIOFunc fd_read,
636 gpointer user_data)
637{
638 IOWatchPoll *iwp;
639 GSource *src;
640 guint tag;
641
642 src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
643 g_source_set_funcs(src, &io_watch_poll_funcs);
644 g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
645 tag = g_source_attach(src, NULL);
646 g_source_unref(src);
647
648 iwp = g_malloc0(sizeof(*iwp));
649 iwp->src = src;
650 iwp->max_size = 0;
651 iwp->fd_can_read = fd_can_read;
652 iwp->opaque = user_data;
653
654 QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);
655
656 return tag;
657}
658
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000659#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530660static GIOChannel *io_channel_from_fd(int fd)
661{
662 GIOChannel *chan;
663
664 if (fd == -1) {
665 return NULL;
666 }
667
668 chan = g_io_channel_unix_new(fd);
669
670 g_io_channel_set_encoding(chan, NULL, NULL);
671 g_io_channel_set_buffered(chan, FALSE);
672
673 return chan;
674}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000675#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530676
Anthony Liguori76a96442013-03-05 23:21:21 +0530677static GIOChannel *io_channel_from_socket(int fd)
678{
679 GIOChannel *chan;
680
681 if (fd == -1) {
682 return NULL;
683 }
684
685#ifdef _WIN32
686 chan = g_io_channel_win32_new_socket(fd);
687#else
688 chan = g_io_channel_unix_new(fd);
689#endif
690
691 g_io_channel_set_encoding(chan, NULL, NULL);
692 g_io_channel_set_buffered(chan, FALSE);
693
694 return chan;
695}
696
Anthony Liguori96c63842013-03-05 23:21:18 +0530697static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
698{
699 GIOStatus status;
700 gsize bytes_written;
701 int len;
702 const uint8_t *buf = _buf;
703
704 len = len1;
705 while (len > 0) {
706 status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
707 &bytes_written, NULL);
708 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530709 if (status == G_IO_STATUS_AGAIN) {
710 errno = EAGAIN;
711 return -1;
712 } else {
713 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530714 return -1;
715 }
716 } else if (status == G_IO_STATUS_EOF) {
717 break;
718 } else {
719 buf += bytes_written;
720 len -= bytes_written;
721 }
722 }
723 return len1 - len;
724}
Anthony Liguori96c63842013-03-05 23:21:18 +0530725
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000726#ifndef _WIN32
727
Anthony Liguoria29753f2013-03-05 23:21:19 +0530728typedef struct FDCharDriver {
729 CharDriverState *chr;
730 GIOChannel *fd_in, *fd_out;
731 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000732 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530733 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000734} FDCharDriver;
735
aliguori6f97dba2008-10-31 18:49:55 +0000736static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
737{
738 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530739
740 return io_channel_send_all(s->fd_out, buf, len);
741}
742
743static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
744{
745 CharDriverState *chr = opaque;
746 FDCharDriver *s = chr->opaque;
747 int len;
748 uint8_t buf[READ_BUF_LEN];
749 GIOStatus status;
750 gsize bytes_read;
751
752 len = sizeof(buf);
753 if (len > s->max_size) {
754 len = s->max_size;
755 }
756 if (len == 0) {
757 return FALSE;
758 }
759
760 status = g_io_channel_read_chars(chan, (gchar *)buf,
761 len, &bytes_read, NULL);
762 if (status == G_IO_STATUS_EOF) {
763 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
764 return FALSE;
765 }
766 if (status == G_IO_STATUS_NORMAL) {
767 qemu_chr_be_write(chr, buf, bytes_read);
768 }
769
770 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000771}
772
773static int fd_chr_read_poll(void *opaque)
774{
775 CharDriverState *chr = opaque;
776 FDCharDriver *s = chr->opaque;
777
Anthony Liguori909cda12011-08-15 11:17:31 -0500778 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000779 return s->max_size;
780}
781
Anthony Liguori23673ca2013-03-05 23:21:23 +0530782static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
783{
784 FDCharDriver *s = chr->opaque;
785 return g_io_create_watch(s->fd_out, cond);
786}
787
aliguori6f97dba2008-10-31 18:49:55 +0000788static void fd_chr_update_read_handler(CharDriverState *chr)
789{
790 FDCharDriver *s = chr->opaque;
791
Anthony Liguoria29753f2013-03-05 23:21:19 +0530792 if (s->fd_in_tag) {
793 g_source_remove(s->fd_in_tag);
794 }
795
796 if (s->fd_in) {
797 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 +0000798 }
799}
800
801static void fd_chr_close(struct CharDriverState *chr)
802{
803 FDCharDriver *s = chr->opaque;
804
Anthony Liguoria29753f2013-03-05 23:21:19 +0530805 if (s->fd_in_tag) {
806 g_source_remove(s->fd_in_tag);
807 s->fd_in_tag = 0;
808 }
809
810 if (s->fd_in) {
811 g_io_channel_unref(s->fd_in);
812 }
813 if (s->fd_out) {
814 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000815 }
816
Anthony Liguori7267c092011-08-20 22:09:37 -0500817 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100818 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000819}
820
821/* open a character device to a unix fd */
822static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
823{
824 CharDriverState *chr;
825 FDCharDriver *s;
826
Anthony Liguori7267c092011-08-20 22:09:37 -0500827 chr = g_malloc0(sizeof(CharDriverState));
828 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530829 s->fd_in = io_channel_from_fd(fd_in);
830 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530831 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530832 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000833 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530834 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000835 chr->chr_write = fd_chr_write;
836 chr->chr_update_read_handler = fd_chr_update_read_handler;
837 chr->chr_close = fd_chr_close;
838
Amit Shah127338e2009-11-03 19:59:56 +0530839 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000840
841 return chr;
842}
843
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100844static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000845{
846 int fd_in, fd_out;
847 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100848 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200849
850 if (filename == NULL) {
851 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100852 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200853 }
aliguori6f97dba2008-10-31 18:49:55 +0000854
855 snprintf(filename_in, 256, "%s.in", filename);
856 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100857 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
858 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000859 if (fd_in < 0 || fd_out < 0) {
860 if (fd_in >= 0)
861 close(fd_in);
862 if (fd_out >= 0)
863 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100864 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100865 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100866 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100867 }
aliguori6f97dba2008-10-31 18:49:55 +0000868 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100869 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000870}
871
aliguori6f97dba2008-10-31 18:49:55 +0000872/* init terminal so that we can grab keys */
873static struct termios oldtty;
874static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100875static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000876
877static void term_exit(void)
878{
879 tcsetattr (0, TCSANOW, &oldtty);
880 fcntl(0, F_SETFL, old_fd0_flags);
881}
882
Paolo Bonzinibb002512010-12-23 13:42:50 +0100883static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000884{
885 struct termios tty;
886
Paolo Bonzini03693642010-12-23 13:42:49 +0100887 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100888 if (!echo) {
889 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000890 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100891 tty.c_oflag |= OPOST;
892 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
893 tty.c_cflag &= ~(CSIZE|PARENB);
894 tty.c_cflag |= CS8;
895 tty.c_cc[VMIN] = 1;
896 tty.c_cc[VTIME] = 0;
897 }
aliguori6f97dba2008-10-31 18:49:55 +0000898 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100899 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000900 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000901
902 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000903}
904
905static void qemu_chr_close_stdio(struct CharDriverState *chr)
906{
907 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000908 fd_chr_close(chr);
909}
910
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100911static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000912{
913 CharDriverState *chr;
914
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400915 if (is_daemonized()) {
916 error_report("cannot use stdio with -daemonize");
917 return NULL;
918 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530919 old_fd0_flags = fcntl(0, F_GETFL);
920 tcgetattr (0, &oldtty);
921 fcntl(0, F_SETFL, O_NONBLOCK);
922 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100923
aliguori6f97dba2008-10-31 18:49:55 +0000924 chr = qemu_chr_open_fd(0, 1);
925 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100926 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100927 stdio_allow_signal = display_type != DT_NOGRAPHIC;
928 if (opts->has_signal) {
929 stdio_allow_signal = opts->signal;
930 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500931 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000932
Markus Armbruster1f514702012-02-07 15:09:08 +0100933 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000934}
935
936#ifdef __sun__
937/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000938static int openpty(int *amaster, int *aslave, char *name,
939 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000940{
941 const char *slave;
942 int mfd = -1, sfd = -1;
943
944 *amaster = *aslave = -1;
945
946 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
947 if (mfd < 0)
948 goto err;
949
950 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
951 goto err;
952
953 if ((slave = ptsname(mfd)) == NULL)
954 goto err;
955
956 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
957 goto err;
958
959 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
960 (termp != NULL && tcgetattr(sfd, termp) < 0))
961 goto err;
962
963 if (amaster)
964 *amaster = mfd;
965 if (aslave)
966 *aslave = sfd;
967 if (winp)
968 ioctl(sfd, TIOCSWINSZ, winp);
969
970 return 0;
971
972err:
973 if (sfd != -1)
974 close(sfd);
975 close(mfd);
976 return -1;
977}
978
blueswir13f4cb3d2009-04-13 16:31:01 +0000979static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000980{
981 termios_p->c_iflag &=
982 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
983 termios_p->c_oflag &= ~OPOST;
984 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
985 termios_p->c_cflag &= ~(CSIZE|PARENB);
986 termios_p->c_cflag |= CS8;
987
988 termios_p->c_cc[VMIN] = 0;
989 termios_p->c_cc[VTIME] = 0;
990}
991#endif
992
993#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100994 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
995 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000996
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100997#define HAVE_CHARDEV_TTY 1
998
aliguori6f97dba2008-10-31 18:49:55 +0000999typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301000 GIOChannel *fd;
1001 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001002 int connected;
1003 int polling;
1004 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301005 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001006} PtyCharDriver;
1007
1008static void pty_chr_update_read_handler(CharDriverState *chr);
1009static void pty_chr_state(CharDriverState *chr, int connected);
1010
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301011static gboolean pty_chr_timer(gpointer opaque)
1012{
1013 struct CharDriverState *chr = opaque;
1014 PtyCharDriver *s = chr->opaque;
1015
1016 if (s->connected) {
1017 goto out;
1018 }
1019 if (s->polling) {
1020 /* If we arrive here without polling being cleared due
1021 * read returning -EIO, then we are (re-)connected */
1022 pty_chr_state(chr, 1);
1023 goto out;
1024 }
1025
1026 /* Next poll ... */
1027 pty_chr_update_read_handler(chr);
1028
1029out:
1030 return FALSE;
1031}
1032
1033static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1034{
1035 PtyCharDriver *s = chr->opaque;
1036
1037 if (s->timer_tag) {
1038 g_source_remove(s->timer_tag);
1039 s->timer_tag = 0;
1040 }
1041
1042 if (ms == 1000) {
1043 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1044 } else {
1045 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1046 }
1047}
1048
aliguori6f97dba2008-10-31 18:49:55 +00001049static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1050{
1051 PtyCharDriver *s = chr->opaque;
1052
1053 if (!s->connected) {
1054 /* guest sends data, check for (re-)connect */
1055 pty_chr_update_read_handler(chr);
1056 return 0;
1057 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301058 return io_channel_send_all(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001059}
1060
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301061static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1062{
1063 PtyCharDriver *s = chr->opaque;
1064 return g_io_create_watch(s->fd, cond);
1065}
1066
aliguori6f97dba2008-10-31 18:49:55 +00001067static int pty_chr_read_poll(void *opaque)
1068{
1069 CharDriverState *chr = opaque;
1070 PtyCharDriver *s = chr->opaque;
1071
Anthony Liguori909cda12011-08-15 11:17:31 -05001072 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001073 return s->read_bytes;
1074}
1075
Anthony Liguori093d3a22013-03-05 23:21:20 +05301076static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001077{
1078 CharDriverState *chr = opaque;
1079 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301080 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301081 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301082 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001083
1084 len = sizeof(buf);
1085 if (len > s->read_bytes)
1086 len = s->read_bytes;
1087 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301088 return FALSE;
1089 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1090 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001091 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301092 return FALSE;
1093 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001094 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001095 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001096 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301097 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001098}
1099
1100static void pty_chr_update_read_handler(CharDriverState *chr)
1101{
1102 PtyCharDriver *s = chr->opaque;
1103
Anthony Liguori093d3a22013-03-05 23:21:20 +05301104 if (s->fd_tag) {
1105 g_source_remove(s->fd_tag);
1106 }
1107
1108 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001109 s->polling = 1;
1110 /*
1111 * Short timeout here: just need wait long enougth that qemu makes
1112 * it through the poll loop once. When reconnected we want a
1113 * short timeout so we notice it almost instantly. Otherwise
1114 * read() gives us -EIO instantly, making pty_chr_state() reset the
1115 * timeout to the normal (much longer) poll interval before the
1116 * timer triggers.
1117 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301118 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001119}
1120
1121static void pty_chr_state(CharDriverState *chr, int connected)
1122{
1123 PtyCharDriver *s = chr->opaque;
1124
1125 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301126 g_source_remove(s->fd_tag);
1127 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001128 s->connected = 0;
1129 s->polling = 0;
1130 /* (re-)connect poll interval for idle guests: once per second.
1131 * We check more frequently in case the guests sends data to
1132 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301133 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001134 } else {
1135 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301136 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001137 s->connected = 1;
1138 }
1139}
1140
aliguori6f97dba2008-10-31 18:49:55 +00001141
1142static void pty_chr_close(struct CharDriverState *chr)
1143{
1144 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301145 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001146
Anthony Liguori093d3a22013-03-05 23:21:20 +05301147 if (s->fd_tag) {
1148 g_source_remove(s->fd_tag);
1149 }
1150 fd = g_io_channel_unix_get_fd(s->fd);
1151 g_io_channel_unref(s->fd);
1152 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301153 if (s->timer_tag) {
1154 g_source_remove(s->timer_tag);
1155 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001156 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001157 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001158}
1159
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001160static CharDriverState *qemu_chr_open_pty(const char *id,
1161 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001162{
1163 CharDriverState *chr;
1164 PtyCharDriver *s;
1165 struct termios tty;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001166 int master_fd, slave_fd;
blueswir1c5e97232009-03-07 20:06:23 +00001167#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001168 char pty_name[PATH_MAX];
1169#define q_ptsname(x) pty_name
1170#else
1171 char *pty_name = NULL;
1172#define q_ptsname(x) ptsname(x)
1173#endif
1174
Markus Armbrustera4e26042011-11-11 10:40:05 +01001175 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001176 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001177 }
1178
1179 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001180 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001181 cfmakeraw(&tty);
1182 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1183 close(slave_fd);
1184
Markus Armbrustera4e26042011-11-11 10:40:05 +01001185 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001186
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001187 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1188 ret->pty = g_strdup(q_ptsname(master_fd));
1189 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001190
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001191 fprintf(stderr, "char device redirected to %s (label %s)\n",
1192 q_ptsname(master_fd), id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001193
1194 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001195 chr->opaque = s;
1196 chr->chr_write = pty_chr_write;
1197 chr->chr_update_read_handler = pty_chr_update_read_handler;
1198 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301199 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001200
Anthony Liguori093d3a22013-03-05 23:21:20 +05301201 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301202 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001203
Markus Armbruster1f514702012-02-07 15:09:08 +01001204 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001205}
1206
1207static void tty_serial_init(int fd, int speed,
1208 int parity, int data_bits, int stop_bits)
1209{
1210 struct termios tty;
1211 speed_t spd;
1212
1213#if 0
1214 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1215 speed, parity, data_bits, stop_bits);
1216#endif
1217 tcgetattr (fd, &tty);
1218
Stefan Weil45eea132009-10-26 16:10:10 +01001219#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1220 speed = speed * 10 / 11;
1221 do {
1222 check_speed(50);
1223 check_speed(75);
1224 check_speed(110);
1225 check_speed(134);
1226 check_speed(150);
1227 check_speed(200);
1228 check_speed(300);
1229 check_speed(600);
1230 check_speed(1200);
1231 check_speed(1800);
1232 check_speed(2400);
1233 check_speed(4800);
1234 check_speed(9600);
1235 check_speed(19200);
1236 check_speed(38400);
1237 /* Non-Posix values follow. They may be unsupported on some systems. */
1238 check_speed(57600);
1239 check_speed(115200);
1240#ifdef B230400
1241 check_speed(230400);
1242#endif
1243#ifdef B460800
1244 check_speed(460800);
1245#endif
1246#ifdef B500000
1247 check_speed(500000);
1248#endif
1249#ifdef B576000
1250 check_speed(576000);
1251#endif
1252#ifdef B921600
1253 check_speed(921600);
1254#endif
1255#ifdef B1000000
1256 check_speed(1000000);
1257#endif
1258#ifdef B1152000
1259 check_speed(1152000);
1260#endif
1261#ifdef B1500000
1262 check_speed(1500000);
1263#endif
1264#ifdef B2000000
1265 check_speed(2000000);
1266#endif
1267#ifdef B2500000
1268 check_speed(2500000);
1269#endif
1270#ifdef B3000000
1271 check_speed(3000000);
1272#endif
1273#ifdef B3500000
1274 check_speed(3500000);
1275#endif
1276#ifdef B4000000
1277 check_speed(4000000);
1278#endif
aliguori6f97dba2008-10-31 18:49:55 +00001279 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001280 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001281
1282 cfsetispeed(&tty, spd);
1283 cfsetospeed(&tty, spd);
1284
1285 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1286 |INLCR|IGNCR|ICRNL|IXON);
1287 tty.c_oflag |= OPOST;
1288 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1289 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1290 switch(data_bits) {
1291 default:
1292 case 8:
1293 tty.c_cflag |= CS8;
1294 break;
1295 case 7:
1296 tty.c_cflag |= CS7;
1297 break;
1298 case 6:
1299 tty.c_cflag |= CS6;
1300 break;
1301 case 5:
1302 tty.c_cflag |= CS5;
1303 break;
1304 }
1305 switch(parity) {
1306 default:
1307 case 'N':
1308 break;
1309 case 'E':
1310 tty.c_cflag |= PARENB;
1311 break;
1312 case 'O':
1313 tty.c_cflag |= PARENB | PARODD;
1314 break;
1315 }
1316 if (stop_bits == 2)
1317 tty.c_cflag |= CSTOPB;
1318
1319 tcsetattr (fd, TCSANOW, &tty);
1320}
1321
1322static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1323{
1324 FDCharDriver *s = chr->opaque;
1325
1326 switch(cmd) {
1327 case CHR_IOCTL_SERIAL_SET_PARAMS:
1328 {
1329 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301330 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1331 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001332 ssp->data_bits, ssp->stop_bits);
1333 }
1334 break;
1335 case CHR_IOCTL_SERIAL_SET_BREAK:
1336 {
1337 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301338 if (enable) {
1339 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1340 }
aliguori6f97dba2008-10-31 18:49:55 +00001341 }
1342 break;
1343 case CHR_IOCTL_SERIAL_GET_TIOCM:
1344 {
1345 int sarg = 0;
1346 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301347 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001348 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001349 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001350 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001351 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001352 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001353 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001354 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001355 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001356 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001357 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001358 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001359 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001360 *targ |= CHR_TIOCM_RTS;
1361 }
1362 break;
1363 case CHR_IOCTL_SERIAL_SET_TIOCM:
1364 {
1365 int sarg = *(int *)arg;
1366 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301367 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001368 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1369 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1370 if (sarg & CHR_TIOCM_CTS)
1371 targ |= TIOCM_CTS;
1372 if (sarg & CHR_TIOCM_CAR)
1373 targ |= TIOCM_CAR;
1374 if (sarg & CHR_TIOCM_DSR)
1375 targ |= TIOCM_DSR;
1376 if (sarg & CHR_TIOCM_RI)
1377 targ |= TIOCM_RI;
1378 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001379 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001380 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001381 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301382 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001383 }
1384 break;
1385 default:
1386 return -ENOTSUP;
1387 }
1388 return 0;
1389}
1390
David Ahern4266a132010-02-10 18:27:17 -07001391static void qemu_chr_close_tty(CharDriverState *chr)
1392{
1393 FDCharDriver *s = chr->opaque;
1394 int fd = -1;
1395
1396 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301397 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001398 }
1399
1400 fd_chr_close(chr);
1401
1402 if (fd >= 0) {
1403 close(fd);
1404 }
1405}
1406
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001407static CharDriverState *qemu_chr_open_tty_fd(int fd)
1408{
1409 CharDriverState *chr;
1410
1411 tty_serial_init(fd, 115200, 'N', 8, 1);
1412 chr = qemu_chr_open_fd(fd, fd);
1413 chr->chr_ioctl = tty_serial_ioctl;
1414 chr->chr_close = qemu_chr_close_tty;
1415 return chr;
1416}
aliguori6f97dba2008-10-31 18:49:55 +00001417#endif /* __linux__ || __sun__ */
1418
1419#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001420
1421#define HAVE_CHARDEV_PARPORT 1
1422
aliguori6f97dba2008-10-31 18:49:55 +00001423typedef struct {
1424 int fd;
1425 int mode;
1426} ParallelCharDriver;
1427
1428static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1429{
1430 if (s->mode != mode) {
1431 int m = mode;
1432 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1433 return 0;
1434 s->mode = mode;
1435 }
1436 return 1;
1437}
1438
1439static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1440{
1441 ParallelCharDriver *drv = chr->opaque;
1442 int fd = drv->fd;
1443 uint8_t b;
1444
1445 switch(cmd) {
1446 case CHR_IOCTL_PP_READ_DATA:
1447 if (ioctl(fd, PPRDATA, &b) < 0)
1448 return -ENOTSUP;
1449 *(uint8_t *)arg = b;
1450 break;
1451 case CHR_IOCTL_PP_WRITE_DATA:
1452 b = *(uint8_t *)arg;
1453 if (ioctl(fd, PPWDATA, &b) < 0)
1454 return -ENOTSUP;
1455 break;
1456 case CHR_IOCTL_PP_READ_CONTROL:
1457 if (ioctl(fd, PPRCONTROL, &b) < 0)
1458 return -ENOTSUP;
1459 /* Linux gives only the lowest bits, and no way to know data
1460 direction! For better compatibility set the fixed upper
1461 bits. */
1462 *(uint8_t *)arg = b | 0xc0;
1463 break;
1464 case CHR_IOCTL_PP_WRITE_CONTROL:
1465 b = *(uint8_t *)arg;
1466 if (ioctl(fd, PPWCONTROL, &b) < 0)
1467 return -ENOTSUP;
1468 break;
1469 case CHR_IOCTL_PP_READ_STATUS:
1470 if (ioctl(fd, PPRSTATUS, &b) < 0)
1471 return -ENOTSUP;
1472 *(uint8_t *)arg = b;
1473 break;
1474 case CHR_IOCTL_PP_DATA_DIR:
1475 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1476 return -ENOTSUP;
1477 break;
1478 case CHR_IOCTL_PP_EPP_READ_ADDR:
1479 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1480 struct ParallelIOArg *parg = arg;
1481 int n = read(fd, parg->buffer, parg->count);
1482 if (n != parg->count) {
1483 return -EIO;
1484 }
1485 }
1486 break;
1487 case CHR_IOCTL_PP_EPP_READ:
1488 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1489 struct ParallelIOArg *parg = arg;
1490 int n = read(fd, parg->buffer, parg->count);
1491 if (n != parg->count) {
1492 return -EIO;
1493 }
1494 }
1495 break;
1496 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1497 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1498 struct ParallelIOArg *parg = arg;
1499 int n = write(fd, parg->buffer, parg->count);
1500 if (n != parg->count) {
1501 return -EIO;
1502 }
1503 }
1504 break;
1505 case CHR_IOCTL_PP_EPP_WRITE:
1506 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1507 struct ParallelIOArg *parg = arg;
1508 int n = write(fd, parg->buffer, parg->count);
1509 if (n != parg->count) {
1510 return -EIO;
1511 }
1512 }
1513 break;
1514 default:
1515 return -ENOTSUP;
1516 }
1517 return 0;
1518}
1519
1520static void pp_close(CharDriverState *chr)
1521{
1522 ParallelCharDriver *drv = chr->opaque;
1523 int fd = drv->fd;
1524
1525 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1526 ioctl(fd, PPRELEASE);
1527 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001528 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001529 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001530}
1531
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001532static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001533{
1534 CharDriverState *chr;
1535 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001536
1537 if (ioctl(fd, PPCLAIM) < 0) {
1538 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001539 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001540 }
1541
Anthony Liguori7267c092011-08-20 22:09:37 -05001542 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001543 drv->fd = fd;
1544 drv->mode = IEEE1284_MODE_COMPAT;
1545
Anthony Liguori7267c092011-08-20 22:09:37 -05001546 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001547 chr->chr_write = null_chr_write;
1548 chr->chr_ioctl = pp_ioctl;
1549 chr->chr_close = pp_close;
1550 chr->opaque = drv;
1551
Amit Shah127338e2009-11-03 19:59:56 +05301552 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001553
Markus Armbruster1f514702012-02-07 15:09:08 +01001554 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001555}
1556#endif /* __linux__ */
1557
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001558#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001559
1560#define HAVE_CHARDEV_PARPORT 1
1561
blueswir16972f932008-11-22 20:49:12 +00001562static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1563{
Stefan Weile0efb992011-02-23 19:09:16 +01001564 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001565 uint8_t b;
1566
1567 switch(cmd) {
1568 case CHR_IOCTL_PP_READ_DATA:
1569 if (ioctl(fd, PPIGDATA, &b) < 0)
1570 return -ENOTSUP;
1571 *(uint8_t *)arg = b;
1572 break;
1573 case CHR_IOCTL_PP_WRITE_DATA:
1574 b = *(uint8_t *)arg;
1575 if (ioctl(fd, PPISDATA, &b) < 0)
1576 return -ENOTSUP;
1577 break;
1578 case CHR_IOCTL_PP_READ_CONTROL:
1579 if (ioctl(fd, PPIGCTRL, &b) < 0)
1580 return -ENOTSUP;
1581 *(uint8_t *)arg = b;
1582 break;
1583 case CHR_IOCTL_PP_WRITE_CONTROL:
1584 b = *(uint8_t *)arg;
1585 if (ioctl(fd, PPISCTRL, &b) < 0)
1586 return -ENOTSUP;
1587 break;
1588 case CHR_IOCTL_PP_READ_STATUS:
1589 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1590 return -ENOTSUP;
1591 *(uint8_t *)arg = b;
1592 break;
1593 default:
1594 return -ENOTSUP;
1595 }
1596 return 0;
1597}
1598
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001599static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001600{
1601 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001602
Anthony Liguori7267c092011-08-20 22:09:37 -05001603 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001604 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001605 chr->chr_write = null_chr_write;
1606 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001607 return chr;
blueswir16972f932008-11-22 20:49:12 +00001608}
1609#endif
1610
aliguori6f97dba2008-10-31 18:49:55 +00001611#else /* _WIN32 */
1612
1613typedef struct {
1614 int max_size;
1615 HANDLE hcom, hrecv, hsend;
1616 OVERLAPPED orecv, osend;
1617 BOOL fpipe;
1618 DWORD len;
1619} WinCharState;
1620
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001621typedef struct {
1622 HANDLE hStdIn;
1623 HANDLE hInputReadyEvent;
1624 HANDLE hInputDoneEvent;
1625 HANDLE hInputThread;
1626 uint8_t win_stdio_buf;
1627} WinStdioCharState;
1628
aliguori6f97dba2008-10-31 18:49:55 +00001629#define NSENDBUF 2048
1630#define NRECVBUF 2048
1631#define MAXCONNECT 1
1632#define NTIMEOUT 5000
1633
1634static int win_chr_poll(void *opaque);
1635static int win_chr_pipe_poll(void *opaque);
1636
1637static void win_chr_close(CharDriverState *chr)
1638{
1639 WinCharState *s = chr->opaque;
1640
1641 if (s->hsend) {
1642 CloseHandle(s->hsend);
1643 s->hsend = NULL;
1644 }
1645 if (s->hrecv) {
1646 CloseHandle(s->hrecv);
1647 s->hrecv = NULL;
1648 }
1649 if (s->hcom) {
1650 CloseHandle(s->hcom);
1651 s->hcom = NULL;
1652 }
1653 if (s->fpipe)
1654 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1655 else
1656 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301657
Hans de Goedea425d232011-11-19 10:22:43 +01001658 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001659}
1660
1661static int win_chr_init(CharDriverState *chr, const char *filename)
1662{
1663 WinCharState *s = chr->opaque;
1664 COMMCONFIG comcfg;
1665 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1666 COMSTAT comstat;
1667 DWORD size;
1668 DWORD err;
1669
1670 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1671 if (!s->hsend) {
1672 fprintf(stderr, "Failed CreateEvent\n");
1673 goto fail;
1674 }
1675 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1676 if (!s->hrecv) {
1677 fprintf(stderr, "Failed CreateEvent\n");
1678 goto fail;
1679 }
1680
1681 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1682 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1683 if (s->hcom == INVALID_HANDLE_VALUE) {
1684 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1685 s->hcom = NULL;
1686 goto fail;
1687 }
1688
1689 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1690 fprintf(stderr, "Failed SetupComm\n");
1691 goto fail;
1692 }
1693
1694 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1695 size = sizeof(COMMCONFIG);
1696 GetDefaultCommConfig(filename, &comcfg, &size);
1697 comcfg.dcb.DCBlength = sizeof(DCB);
1698 CommConfigDialog(filename, NULL, &comcfg);
1699
1700 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1701 fprintf(stderr, "Failed SetCommState\n");
1702 goto fail;
1703 }
1704
1705 if (!SetCommMask(s->hcom, EV_ERR)) {
1706 fprintf(stderr, "Failed SetCommMask\n");
1707 goto fail;
1708 }
1709
1710 cto.ReadIntervalTimeout = MAXDWORD;
1711 if (!SetCommTimeouts(s->hcom, &cto)) {
1712 fprintf(stderr, "Failed SetCommTimeouts\n");
1713 goto fail;
1714 }
1715
1716 if (!ClearCommError(s->hcom, &err, &comstat)) {
1717 fprintf(stderr, "Failed ClearCommError\n");
1718 goto fail;
1719 }
1720 qemu_add_polling_cb(win_chr_poll, chr);
1721 return 0;
1722
1723 fail:
1724 win_chr_close(chr);
1725 return -1;
1726}
1727
1728static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1729{
1730 WinCharState *s = chr->opaque;
1731 DWORD len, ret, size, err;
1732
1733 len = len1;
1734 ZeroMemory(&s->osend, sizeof(s->osend));
1735 s->osend.hEvent = s->hsend;
1736 while (len > 0) {
1737 if (s->hsend)
1738 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1739 else
1740 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1741 if (!ret) {
1742 err = GetLastError();
1743 if (err == ERROR_IO_PENDING) {
1744 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1745 if (ret) {
1746 buf += size;
1747 len -= size;
1748 } else {
1749 break;
1750 }
1751 } else {
1752 break;
1753 }
1754 } else {
1755 buf += size;
1756 len -= size;
1757 }
1758 }
1759 return len1 - len;
1760}
1761
1762static int win_chr_read_poll(CharDriverState *chr)
1763{
1764 WinCharState *s = chr->opaque;
1765
Anthony Liguori909cda12011-08-15 11:17:31 -05001766 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001767 return s->max_size;
1768}
1769
1770static void win_chr_readfile(CharDriverState *chr)
1771{
1772 WinCharState *s = chr->opaque;
1773 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301774 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001775 DWORD size;
1776
1777 ZeroMemory(&s->orecv, sizeof(s->orecv));
1778 s->orecv.hEvent = s->hrecv;
1779 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1780 if (!ret) {
1781 err = GetLastError();
1782 if (err == ERROR_IO_PENDING) {
1783 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1784 }
1785 }
1786
1787 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001788 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001789 }
1790}
1791
1792static void win_chr_read(CharDriverState *chr)
1793{
1794 WinCharState *s = chr->opaque;
1795
1796 if (s->len > s->max_size)
1797 s->len = s->max_size;
1798 if (s->len == 0)
1799 return;
1800
1801 win_chr_readfile(chr);
1802}
1803
1804static int win_chr_poll(void *opaque)
1805{
1806 CharDriverState *chr = opaque;
1807 WinCharState *s = chr->opaque;
1808 COMSTAT status;
1809 DWORD comerr;
1810
1811 ClearCommError(s->hcom, &comerr, &status);
1812 if (status.cbInQue > 0) {
1813 s->len = status.cbInQue;
1814 win_chr_read_poll(chr);
1815 win_chr_read(chr);
1816 return 1;
1817 }
1818 return 0;
1819}
1820
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001821static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001822{
1823 CharDriverState *chr;
1824 WinCharState *s;
1825
Anthony Liguori7267c092011-08-20 22:09:37 -05001826 chr = g_malloc0(sizeof(CharDriverState));
1827 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001828 chr->opaque = s;
1829 chr->chr_write = win_chr_write;
1830 chr->chr_close = win_chr_close;
1831
1832 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001833 g_free(s);
1834 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001835 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001836 }
Amit Shah127338e2009-11-03 19:59:56 +05301837 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001838 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001839}
1840
1841static int win_chr_pipe_poll(void *opaque)
1842{
1843 CharDriverState *chr = opaque;
1844 WinCharState *s = chr->opaque;
1845 DWORD size;
1846
1847 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1848 if (size > 0) {
1849 s->len = size;
1850 win_chr_read_poll(chr);
1851 win_chr_read(chr);
1852 return 1;
1853 }
1854 return 0;
1855}
1856
1857static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1858{
1859 WinCharState *s = chr->opaque;
1860 OVERLAPPED ov;
1861 int ret;
1862 DWORD size;
1863 char openname[256];
1864
1865 s->fpipe = TRUE;
1866
1867 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1868 if (!s->hsend) {
1869 fprintf(stderr, "Failed CreateEvent\n");
1870 goto fail;
1871 }
1872 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1873 if (!s->hrecv) {
1874 fprintf(stderr, "Failed CreateEvent\n");
1875 goto fail;
1876 }
1877
1878 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1879 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1880 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1881 PIPE_WAIT,
1882 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1883 if (s->hcom == INVALID_HANDLE_VALUE) {
1884 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1885 s->hcom = NULL;
1886 goto fail;
1887 }
1888
1889 ZeroMemory(&ov, sizeof(ov));
1890 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1891 ret = ConnectNamedPipe(s->hcom, &ov);
1892 if (ret) {
1893 fprintf(stderr, "Failed ConnectNamedPipe\n");
1894 goto fail;
1895 }
1896
1897 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1898 if (!ret) {
1899 fprintf(stderr, "Failed GetOverlappedResult\n");
1900 if (ov.hEvent) {
1901 CloseHandle(ov.hEvent);
1902 ov.hEvent = NULL;
1903 }
1904 goto fail;
1905 }
1906
1907 if (ov.hEvent) {
1908 CloseHandle(ov.hEvent);
1909 ov.hEvent = NULL;
1910 }
1911 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1912 return 0;
1913
1914 fail:
1915 win_chr_close(chr);
1916 return -1;
1917}
1918
1919
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001920static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001921{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001922 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001923 CharDriverState *chr;
1924 WinCharState *s;
1925
Anthony Liguori7267c092011-08-20 22:09:37 -05001926 chr = g_malloc0(sizeof(CharDriverState));
1927 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001928 chr->opaque = s;
1929 chr->chr_write = win_chr_write;
1930 chr->chr_close = win_chr_close;
1931
1932 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001933 g_free(s);
1934 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001935 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001936 }
Amit Shah127338e2009-11-03 19:59:56 +05301937 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001938 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001939}
1940
Markus Armbruster1f514702012-02-07 15:09:08 +01001941static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001942{
1943 CharDriverState *chr;
1944 WinCharState *s;
1945
Anthony Liguori7267c092011-08-20 22:09:37 -05001946 chr = g_malloc0(sizeof(CharDriverState));
1947 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001948 s->hcom = fd_out;
1949 chr->opaque = s;
1950 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301951 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001952 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001953}
1954
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001955static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001956{
Markus Armbruster1f514702012-02-07 15:09:08 +01001957 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001958}
1959
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001960static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1961{
1962 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1963 DWORD dwSize;
1964 int len1;
1965
1966 len1 = len;
1967
1968 while (len1 > 0) {
1969 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1970 break;
1971 }
1972 buf += dwSize;
1973 len1 -= dwSize;
1974 }
1975
1976 return len - len1;
1977}
1978
1979static void win_stdio_wait_func(void *opaque)
1980{
1981 CharDriverState *chr = opaque;
1982 WinStdioCharState *stdio = chr->opaque;
1983 INPUT_RECORD buf[4];
1984 int ret;
1985 DWORD dwSize;
1986 int i;
1987
1988 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1989 &dwSize);
1990
1991 if (!ret) {
1992 /* Avoid error storm */
1993 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1994 return;
1995 }
1996
1997 for (i = 0; i < dwSize; i++) {
1998 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1999
2000 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2001 int j;
2002 if (kev->uChar.AsciiChar != 0) {
2003 for (j = 0; j < kev->wRepeatCount; j++) {
2004 if (qemu_chr_be_can_write(chr)) {
2005 uint8_t c = kev->uChar.AsciiChar;
2006 qemu_chr_be_write(chr, &c, 1);
2007 }
2008 }
2009 }
2010 }
2011 }
2012}
2013
2014static DWORD WINAPI win_stdio_thread(LPVOID param)
2015{
2016 CharDriverState *chr = param;
2017 WinStdioCharState *stdio = chr->opaque;
2018 int ret;
2019 DWORD dwSize;
2020
2021 while (1) {
2022
2023 /* Wait for one byte */
2024 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2025
2026 /* Exit in case of error, continue if nothing read */
2027 if (!ret) {
2028 break;
2029 }
2030 if (!dwSize) {
2031 continue;
2032 }
2033
2034 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2035 if (stdio->win_stdio_buf == '\r') {
2036 continue;
2037 }
2038
2039 /* Signal the main thread and wait until the byte was eaten */
2040 if (!SetEvent(stdio->hInputReadyEvent)) {
2041 break;
2042 }
2043 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2044 != WAIT_OBJECT_0) {
2045 break;
2046 }
2047 }
2048
2049 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2050 return 0;
2051}
2052
2053static void win_stdio_thread_wait_func(void *opaque)
2054{
2055 CharDriverState *chr = opaque;
2056 WinStdioCharState *stdio = chr->opaque;
2057
2058 if (qemu_chr_be_can_write(chr)) {
2059 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2060 }
2061
2062 SetEvent(stdio->hInputDoneEvent);
2063}
2064
2065static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2066{
2067 WinStdioCharState *stdio = chr->opaque;
2068 DWORD dwMode = 0;
2069
2070 GetConsoleMode(stdio->hStdIn, &dwMode);
2071
2072 if (echo) {
2073 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2074 } else {
2075 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2076 }
2077}
2078
2079static void win_stdio_close(CharDriverState *chr)
2080{
2081 WinStdioCharState *stdio = chr->opaque;
2082
2083 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2084 CloseHandle(stdio->hInputReadyEvent);
2085 }
2086 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2087 CloseHandle(stdio->hInputDoneEvent);
2088 }
2089 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2090 TerminateThread(stdio->hInputThread, 0);
2091 }
2092
2093 g_free(chr->opaque);
2094 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002095}
2096
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002097static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002098{
2099 CharDriverState *chr;
2100 WinStdioCharState *stdio;
2101 DWORD dwMode;
2102 int is_console = 0;
2103
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002104 chr = g_malloc0(sizeof(CharDriverState));
2105 stdio = g_malloc0(sizeof(WinStdioCharState));
2106
2107 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2108 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2109 fprintf(stderr, "cannot open stdio: invalid handle\n");
2110 exit(1);
2111 }
2112
2113 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2114
2115 chr->opaque = stdio;
2116 chr->chr_write = win_stdio_write;
2117 chr->chr_close = win_stdio_close;
2118
Anthony Liguoried7a1542013-03-05 23:21:17 +05302119 if (is_console) {
2120 if (qemu_add_wait_object(stdio->hStdIn,
2121 win_stdio_wait_func, chr)) {
2122 fprintf(stderr, "qemu_add_wait_object: failed\n");
2123 }
2124 } else {
2125 DWORD dwId;
2126
2127 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2128 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2129 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2130 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002131
Anthony Liguoried7a1542013-03-05 23:21:17 +05302132 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2133 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2134 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2135 fprintf(stderr, "cannot create stdio thread or event\n");
2136 exit(1);
2137 }
2138 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2139 win_stdio_thread_wait_func, chr)) {
2140 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002141 }
2142 }
2143
2144 dwMode |= ENABLE_LINE_INPUT;
2145
Anthony Liguoried7a1542013-03-05 23:21:17 +05302146 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002147 /* set the terminal in raw mode */
2148 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2149 dwMode |= ENABLE_PROCESSED_INPUT;
2150 }
2151
2152 SetConsoleMode(stdio->hStdIn, dwMode);
2153
2154 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2155 qemu_chr_fe_set_echo(chr, false);
2156
Markus Armbruster1f514702012-02-07 15:09:08 +01002157 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002158}
aliguori6f97dba2008-10-31 18:49:55 +00002159#endif /* !_WIN32 */
2160
Anthony Liguori5ab82112013-03-05 23:21:31 +05302161
aliguori6f97dba2008-10-31 18:49:55 +00002162/***********************************************************/
2163/* UDP Net console */
2164
2165typedef struct {
2166 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302167 GIOChannel *chan;
2168 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302169 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002170 int bufcnt;
2171 int bufptr;
2172 int max_size;
2173} NetCharDriver;
2174
2175static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2176{
2177 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302178 gsize bytes_written;
2179 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002180
Anthony Liguori76a96442013-03-05 23:21:21 +05302181 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2182 if (status == G_IO_STATUS_EOF) {
2183 return 0;
2184 } else if (status != G_IO_STATUS_NORMAL) {
2185 return -1;
2186 }
2187
2188 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002189}
2190
2191static int udp_chr_read_poll(void *opaque)
2192{
2193 CharDriverState *chr = opaque;
2194 NetCharDriver *s = chr->opaque;
2195
Anthony Liguori909cda12011-08-15 11:17:31 -05002196 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002197
2198 /* If there were any stray characters in the queue process them
2199 * first
2200 */
2201 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002202 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002203 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002204 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002205 }
2206 return s->max_size;
2207}
2208
Anthony Liguori76a96442013-03-05 23:21:21 +05302209static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002210{
2211 CharDriverState *chr = opaque;
2212 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302213 gsize bytes_read = 0;
2214 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002215
2216 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302217 return FALSE;
2218 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2219 &bytes_read, NULL);
2220 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002221 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302222 if (status != G_IO_STATUS_NORMAL) {
2223 return FALSE;
2224 }
aliguori6f97dba2008-10-31 18:49:55 +00002225
2226 s->bufptr = 0;
2227 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002228 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002229 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002230 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002231 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302232
2233 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002234}
2235
2236static void udp_chr_update_read_handler(CharDriverState *chr)
2237{
2238 NetCharDriver *s = chr->opaque;
2239
Anthony Liguori76a96442013-03-05 23:21:21 +05302240 if (s->tag) {
2241 g_source_remove(s->tag);
2242 s->tag = 0;
2243 }
2244
2245 if (s->chan) {
2246 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002247 }
2248}
2249
aliguori819f56b2009-03-28 17:58:14 +00002250static void udp_chr_close(CharDriverState *chr)
2251{
2252 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302253 if (s->tag) {
2254 g_source_remove(s->tag);
2255 }
2256 if (s->chan) {
2257 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002258 closesocket(s->fd);
2259 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002260 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002261 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002262}
2263
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002264static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002265{
2266 CharDriverState *chr = NULL;
2267 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002268
Anthony Liguori7267c092011-08-20 22:09:37 -05002269 chr = g_malloc0(sizeof(CharDriverState));
2270 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002271
aliguori6f97dba2008-10-31 18:49:55 +00002272 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302273 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002274 s->bufcnt = 0;
2275 s->bufptr = 0;
2276 chr->opaque = s;
2277 chr->chr_write = udp_chr_write;
2278 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002279 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002280 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002281}
aliguori6f97dba2008-10-31 18:49:55 +00002282
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002283static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2284{
2285 Error *local_err = NULL;
2286 int fd = -1;
2287
2288 fd = inet_dgram_opts(opts, &local_err);
2289 if (fd < 0) {
2290 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002291 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002292 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002293}
2294
2295/***********************************************************/
2296/* TCP Net console */
2297
2298typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302299
2300 GIOChannel *chan, *listen_chan;
2301 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002302 int fd, listen_fd;
2303 int connected;
2304 int max_size;
2305 int do_telnetopt;
2306 int do_nodelay;
2307 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002308 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002309} TCPCharDriver;
2310
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302311static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002312
2313static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2314{
2315 TCPCharDriver *s = chr->opaque;
2316 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302317 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002318 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002319 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002320 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002321 }
2322}
2323
2324static int tcp_chr_read_poll(void *opaque)
2325{
2326 CharDriverState *chr = opaque;
2327 TCPCharDriver *s = chr->opaque;
2328 if (!s->connected)
2329 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002330 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002331 return s->max_size;
2332}
2333
2334#define IAC 255
2335#define IAC_BREAK 243
2336static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2337 TCPCharDriver *s,
2338 uint8_t *buf, int *size)
2339{
2340 /* Handle any telnet client's basic IAC options to satisfy char by
2341 * char mode with no echo. All IAC options will be removed from
2342 * the buf and the do_telnetopt variable will be used to track the
2343 * state of the width of the IAC information.
2344 *
2345 * IAC commands come in sets of 3 bytes with the exception of the
2346 * "IAC BREAK" command and the double IAC.
2347 */
2348
2349 int i;
2350 int j = 0;
2351
2352 for (i = 0; i < *size; i++) {
2353 if (s->do_telnetopt > 1) {
2354 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2355 /* Double IAC means send an IAC */
2356 if (j != i)
2357 buf[j] = buf[i];
2358 j++;
2359 s->do_telnetopt = 1;
2360 } else {
2361 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2362 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002363 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002364 s->do_telnetopt++;
2365 }
2366 s->do_telnetopt++;
2367 }
2368 if (s->do_telnetopt >= 4) {
2369 s->do_telnetopt = 1;
2370 }
2371 } else {
2372 if ((unsigned char)buf[i] == IAC) {
2373 s->do_telnetopt = 2;
2374 } else {
2375 if (j != i)
2376 buf[j] = buf[i];
2377 j++;
2378 }
2379 }
2380 }
2381 *size = j;
2382}
2383
Mark McLoughlin7d174052009-07-22 09:11:39 +01002384static int tcp_get_msgfd(CharDriverState *chr)
2385{
2386 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002387 int fd = s->msgfd;
2388 s->msgfd = -1;
2389 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002390}
2391
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002392#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002393static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2394{
2395 TCPCharDriver *s = chr->opaque;
2396 struct cmsghdr *cmsg;
2397
2398 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2399 int fd;
2400
2401 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2402 cmsg->cmsg_level != SOL_SOCKET ||
2403 cmsg->cmsg_type != SCM_RIGHTS)
2404 continue;
2405
2406 fd = *((int *)CMSG_DATA(cmsg));
2407 if (fd < 0)
2408 continue;
2409
Corey Bryant06138652012-08-14 16:43:42 -04002410#ifndef MSG_CMSG_CLOEXEC
2411 qemu_set_cloexec(fd);
2412#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002413 if (s->msgfd != -1)
2414 close(s->msgfd);
2415 s->msgfd = fd;
2416 }
2417}
2418
Mark McLoughlin9977c892009-07-22 09:11:38 +01002419static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2420{
2421 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002422 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002423 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002424 union {
2425 struct cmsghdr cmsg;
2426 char control[CMSG_SPACE(sizeof(int))];
2427 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002428 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002429 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002430
2431 iov[0].iov_base = buf;
2432 iov[0].iov_len = len;
2433
2434 msg.msg_iov = iov;
2435 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002436 msg.msg_control = &msg_control;
2437 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002438
Corey Bryant06138652012-08-14 16:43:42 -04002439#ifdef MSG_CMSG_CLOEXEC
2440 flags |= MSG_CMSG_CLOEXEC;
2441#endif
2442 ret = recvmsg(s->fd, &msg, flags);
2443 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002444 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002445 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002446
2447 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002448}
2449#else
2450static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2451{
2452 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002453 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002454}
2455#endif
2456
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302457static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2458{
2459 TCPCharDriver *s = chr->opaque;
2460 return g_io_create_watch(s->chan, cond);
2461}
2462
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302463static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002464{
2465 CharDriverState *chr = opaque;
2466 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302467 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002468 int len, size;
2469
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302470 if (!s->connected || s->max_size <= 0) {
2471 return FALSE;
2472 }
aliguori6f97dba2008-10-31 18:49:55 +00002473 len = sizeof(buf);
2474 if (len > s->max_size)
2475 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002476 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002477 if (size == 0) {
2478 /* connection closed */
2479 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302480 if (s->listen_chan) {
2481 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002482 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302483 g_source_remove(s->tag);
2484 s->tag = 0;
2485 g_io_channel_unref(s->chan);
2486 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002487 closesocket(s->fd);
2488 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002489 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002490 } else if (size > 0) {
2491 if (s->do_telnetopt)
2492 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2493 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002494 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002495 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302496
2497 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002498}
2499
Blue Swirl68c18d12010-08-15 09:46:24 +00002500#ifndef _WIN32
2501CharDriverState *qemu_chr_open_eventfd(int eventfd)
2502{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002503 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002504}
Blue Swirl68c18d12010-08-15 09:46:24 +00002505#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002506
aliguori6f97dba2008-10-31 18:49:55 +00002507static void tcp_chr_connect(void *opaque)
2508{
2509 CharDriverState *chr = opaque;
2510 TCPCharDriver *s = chr->opaque;
2511
2512 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302513 if (s->chan) {
2514 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002515 }
Amit Shah127338e2009-11-03 19:59:56 +05302516 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002517}
2518
2519#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2520static void tcp_chr_telnet_init(int fd)
2521{
2522 char buf[3];
2523 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2524 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2525 send(fd, (char *)buf, 3, 0);
2526 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2527 send(fd, (char *)buf, 3, 0);
2528 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2529 send(fd, (char *)buf, 3, 0);
2530 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2531 send(fd, (char *)buf, 3, 0);
2532}
2533
Daniel P. Berrange13661082011-06-23 13:31:42 +01002534static int tcp_chr_add_client(CharDriverState *chr, int fd)
2535{
2536 TCPCharDriver *s = chr->opaque;
2537 if (s->fd != -1)
2538 return -1;
2539
2540 socket_set_nonblock(fd);
2541 if (s->do_nodelay)
2542 socket_set_nodelay(fd);
2543 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302544 s->chan = io_channel_from_socket(fd);
2545 g_source_remove(s->listen_tag);
2546 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002547 tcp_chr_connect(chr);
2548
2549 return 0;
2550}
2551
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302552static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002553{
2554 CharDriverState *chr = opaque;
2555 TCPCharDriver *s = chr->opaque;
2556 struct sockaddr_in saddr;
2557#ifndef _WIN32
2558 struct sockaddr_un uaddr;
2559#endif
2560 struct sockaddr *addr;
2561 socklen_t len;
2562 int fd;
2563
2564 for(;;) {
2565#ifndef _WIN32
2566 if (s->is_unix) {
2567 len = sizeof(uaddr);
2568 addr = (struct sockaddr *)&uaddr;
2569 } else
2570#endif
2571 {
2572 len = sizeof(saddr);
2573 addr = (struct sockaddr *)&saddr;
2574 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002575 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002576 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302577 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002578 } else if (fd >= 0) {
2579 if (s->do_telnetopt)
2580 tcp_chr_telnet_init(fd);
2581 break;
2582 }
2583 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002584 if (tcp_chr_add_client(chr, fd) < 0)
2585 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302586
2587 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002588}
2589
2590static void tcp_chr_close(CharDriverState *chr)
2591{
2592 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002593 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302594 if (s->tag) {
2595 g_source_remove(s->tag);
2596 }
2597 if (s->chan) {
2598 g_io_channel_unref(s->chan);
2599 }
aliguori6f97dba2008-10-31 18:49:55 +00002600 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002601 }
2602 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302603 if (s->listen_tag) {
2604 g_source_remove(s->listen_tag);
2605 }
2606 if (s->listen_chan) {
2607 g_io_channel_unref(s->listen_chan);
2608 }
aliguori6f97dba2008-10-31 18:49:55 +00002609 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002610 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002611 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002612 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002613}
2614
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002615static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2616 bool is_listen, bool is_telnet,
2617 bool is_waitconnect,
2618 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002619{
2620 CharDriverState *chr = NULL;
2621 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002622 char host[NI_MAXHOST], serv[NI_MAXSERV];
2623 const char *left = "", *right = "";
2624 struct sockaddr_storage ss;
2625 socklen_t ss_len = sizeof(ss);
2626
2627 memset(&ss, 0, ss_len);
2628 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2629 error_setg(errp, "getsockname: %s", strerror(errno));
2630 return NULL;
2631 }
2632
2633 chr = g_malloc0(sizeof(CharDriverState));
2634 s = g_malloc0(sizeof(TCPCharDriver));
2635
2636 s->connected = 0;
2637 s->fd = -1;
2638 s->listen_fd = -1;
2639 s->msgfd = -1;
2640
2641 chr->filename = g_malloc(256);
2642 switch (ss.ss_family) {
2643#ifndef _WIN32
2644 case AF_UNIX:
2645 s->is_unix = 1;
2646 snprintf(chr->filename, 256, "unix:%s%s",
2647 ((struct sockaddr_un *)(&ss))->sun_path,
2648 is_listen ? ",server" : "");
2649 break;
2650#endif
2651 case AF_INET6:
2652 left = "[";
2653 right = "]";
2654 /* fall through */
2655 case AF_INET:
2656 s->do_nodelay = do_nodelay;
2657 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2658 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002659 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002660 is_telnet ? "telnet" : "tcp",
2661 left, host, right, serv,
2662 is_listen ? ",server" : "");
2663 break;
2664 }
2665
2666 chr->opaque = s;
2667 chr->chr_write = tcp_chr_write;
2668 chr->chr_close = tcp_chr_close;
2669 chr->get_msgfd = tcp_get_msgfd;
2670 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302671 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002672
2673 if (is_listen) {
2674 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302675 s->listen_chan = io_channel_from_socket(s->listen_fd);
2676 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002677 if (is_telnet) {
2678 s->do_telnetopt = 1;
2679 }
2680 } else {
2681 s->connected = 1;
2682 s->fd = fd;
2683 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302684 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002685 tcp_chr_connect(chr);
2686 }
2687
2688 if (is_listen && is_waitconnect) {
2689 printf("QEMU waiting for connection on: %s\n",
2690 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302691 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002692 socket_set_nonblock(s->listen_fd);
2693 }
2694 return chr;
2695}
2696
2697static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2698{
2699 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002700 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002701 int fd = -1;
2702 int is_listen;
2703 int is_waitconnect;
2704 int do_nodelay;
2705 int is_unix;
2706 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002707
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002708 is_listen = qemu_opt_get_bool(opts, "server", 0);
2709 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2710 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2711 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2712 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002713 if (!is_listen)
2714 is_waitconnect = 0;
2715
aliguorif07b6002008-11-11 20:54:09 +00002716 if (is_unix) {
2717 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002718 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002719 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002720 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002721 }
2722 } else {
2723 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002724 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002725 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002726 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002727 }
2728 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002729 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002730 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002731 }
aliguori6f97dba2008-10-31 18:49:55 +00002732
2733 if (!is_waitconnect)
2734 socket_set_nonblock(fd);
2735
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002736 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2737 is_waitconnect, &local_err);
2738 if (error_is_set(&local_err)) {
2739 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002740 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002741 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002742
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002743
aliguori6f97dba2008-10-31 18:49:55 +00002744 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002745 if (local_err) {
2746 qerror_report_err(local_err);
2747 error_free(local_err);
2748 }
2749 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002750 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002751 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002752 if (chr) {
2753 g_free(chr->opaque);
2754 g_free(chr);
2755 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002756 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002757}
2758
Luiz Capitulino999bd672010-10-22 16:09:05 -02002759/***********************************************************/
2760/* Memory chardev */
2761typedef struct {
2762 size_t outbuf_size;
2763 size_t outbuf_capacity;
2764 uint8_t *outbuf;
2765} MemoryDriver;
2766
2767static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2768{
2769 MemoryDriver *d = chr->opaque;
2770
2771 /* TODO: the QString implementation has the same code, we should
2772 * introduce a generic way to do this in cutils.c */
2773 if (d->outbuf_capacity < d->outbuf_size + len) {
2774 /* grow outbuf */
2775 d->outbuf_capacity += len;
2776 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002777 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002778 }
2779
2780 memcpy(d->outbuf + d->outbuf_size, buf, len);
2781 d->outbuf_size += len;
2782
2783 return len;
2784}
2785
2786void qemu_chr_init_mem(CharDriverState *chr)
2787{
2788 MemoryDriver *d;
2789
Anthony Liguori7267c092011-08-20 22:09:37 -05002790 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002791 d->outbuf_size = 0;
2792 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002793 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002794
2795 memset(chr, 0, sizeof(*chr));
2796 chr->opaque = d;
2797 chr->chr_write = mem_chr_write;
2798}
2799
2800QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2801{
2802 MemoryDriver *d = chr->opaque;
2803 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2804}
2805
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002806/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002807void qemu_chr_close_mem(CharDriverState *chr)
2808{
2809 MemoryDriver *d = chr->opaque;
2810
Anthony Liguori7267c092011-08-20 22:09:37 -05002811 g_free(d->outbuf);
2812 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002813 chr->opaque = NULL;
2814 chr->chr_write = NULL;
2815}
2816
2817size_t qemu_chr_mem_osize(const CharDriverState *chr)
2818{
2819 const MemoryDriver *d = chr->opaque;
2820 return d->outbuf_size;
2821}
2822
Lei Li51767e72013-01-25 00:03:19 +08002823/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002824/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002825
2826typedef struct {
2827 size_t size;
2828 size_t prod;
2829 size_t cons;
2830 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002831} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002832
Markus Armbruster3949e592013-02-06 21:27:24 +01002833static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002834{
Markus Armbruster3949e592013-02-06 21:27:24 +01002835 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002836
Markus Armbruster5c230102013-02-06 21:27:23 +01002837 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002838}
2839
Markus Armbruster3949e592013-02-06 21:27:24 +01002840static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002841{
Markus Armbruster3949e592013-02-06 21:27:24 +01002842 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002843 int i;
2844
2845 if (!buf || (len < 0)) {
2846 return -1;
2847 }
2848
2849 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002850 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2851 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002852 d->cons = d->prod - d->size;
2853 }
2854 }
2855
2856 return 0;
2857}
2858
Markus Armbruster3949e592013-02-06 21:27:24 +01002859static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002860{
Markus Armbruster3949e592013-02-06 21:27:24 +01002861 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002862 int i;
2863
Markus Armbruster5c230102013-02-06 21:27:23 +01002864 for (i = 0; i < len && d->cons != d->prod; i++) {
2865 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002866 }
2867
2868 return i;
2869}
2870
Markus Armbruster3949e592013-02-06 21:27:24 +01002871static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002872{
Markus Armbruster3949e592013-02-06 21:27:24 +01002873 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002874
2875 g_free(d->cbuf);
2876 g_free(d);
2877 chr->opaque = NULL;
2878}
2879
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002880static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2881 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002882{
2883 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002884 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002885
2886 chr = g_malloc0(sizeof(CharDriverState));
2887 d = g_malloc(sizeof(*d));
2888
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002889 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002890
2891 /* The size must be power of 2 */
2892 if (d->size & (d->size - 1)) {
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002893 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002894 goto fail;
2895 }
2896
2897 d->prod = 0;
2898 d->cons = 0;
2899 d->cbuf = g_malloc0(d->size);
2900
2901 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002902 chr->chr_write = ringbuf_chr_write;
2903 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002904
2905 return chr;
2906
2907fail:
2908 g_free(d);
2909 g_free(chr);
2910 return NULL;
2911}
2912
Markus Armbruster3949e592013-02-06 21:27:24 +01002913static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002914{
Markus Armbruster3949e592013-02-06 21:27:24 +01002915 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002916}
2917
Markus Armbruster3949e592013-02-06 21:27:24 +01002918void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002919 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002920 Error **errp)
2921{
2922 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002923 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002924 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002925 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002926
2927 chr = qemu_chr_find(device);
2928 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002929 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002930 return;
2931 }
2932
Markus Armbruster3949e592013-02-06 21:27:24 +01002933 if (!chr_is_ringbuf(chr)) {
2934 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002935 return;
2936 }
2937
Lei Li1f590cf2013-01-25 00:03:20 +08002938 if (has_format && (format == DATA_FORMAT_BASE64)) {
2939 write_data = g_base64_decode(data, &write_count);
2940 } else {
2941 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002942 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002943 }
2944
Markus Armbruster3949e592013-02-06 21:27:24 +01002945 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002946
Markus Armbruster13289fb2013-02-06 21:27:18 +01002947 if (write_data != (uint8_t *)data) {
2948 g_free((void *)write_data);
2949 }
2950
Lei Li1f590cf2013-01-25 00:03:20 +08002951 if (ret < 0) {
2952 error_setg(errp, "Failed to write to device %s", device);
2953 return;
2954 }
2955}
2956
Markus Armbruster3949e592013-02-06 21:27:24 +01002957char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002958 bool has_format, enum DataFormat format,
2959 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002960{
2961 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002962 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002963 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002964 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002965
2966 chr = qemu_chr_find(device);
2967 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002968 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002969 return NULL;
2970 }
2971
Markus Armbruster3949e592013-02-06 21:27:24 +01002972 if (!chr_is_ringbuf(chr)) {
2973 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002974 return NULL;
2975 }
2976
2977 if (size <= 0) {
2978 error_setg(errp, "size must be greater than zero");
2979 return NULL;
2980 }
2981
Markus Armbruster3949e592013-02-06 21:27:24 +01002982 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002983 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002984 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002985
Markus Armbruster3949e592013-02-06 21:27:24 +01002986 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002987
2988 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002989 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002990 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002991 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002992 /*
2993 * FIXME should read only complete, valid UTF-8 characters up
2994 * to @size bytes. Invalid sequences should be replaced by a
2995 * suitable replacement character. Except when (and only
2996 * when) ring buffer lost characters since last read, initial
2997 * continuation characters should be dropped.
2998 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002999 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003000 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003001 }
3002
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003003 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003004}
3005
Gerd Hoffmann33521632009-12-08 13:11:49 +01003006QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003007{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003008 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003009 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003010 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003011 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003012 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003013
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003014 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3015 if (error_is_set(&local_err)) {
3016 qerror_report_err(local_err);
3017 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003018 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003019 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003020
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003021 if (strstart(filename, "mon:", &p)) {
3022 filename = p;
3023 qemu_opt_set(opts, "mux", "on");
3024 }
3025
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003026 if (strcmp(filename, "null") == 0 ||
3027 strcmp(filename, "pty") == 0 ||
3028 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003029 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003030 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003031 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003032 return opts;
3033 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003034 if (strstart(filename, "vc", &p)) {
3035 qemu_opt_set(opts, "backend", "vc");
3036 if (*p == ':') {
3037 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3038 /* pixels */
3039 qemu_opt_set(opts, "width", width);
3040 qemu_opt_set(opts, "height", height);
3041 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3042 /* chars */
3043 qemu_opt_set(opts, "cols", width);
3044 qemu_opt_set(opts, "rows", height);
3045 } else {
3046 goto fail;
3047 }
3048 }
3049 return opts;
3050 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003051 if (strcmp(filename, "con:") == 0) {
3052 qemu_opt_set(opts, "backend", "console");
3053 return opts;
3054 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003055 if (strstart(filename, "COM", NULL)) {
3056 qemu_opt_set(opts, "backend", "serial");
3057 qemu_opt_set(opts, "path", filename);
3058 return opts;
3059 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003060 if (strstart(filename, "file:", &p)) {
3061 qemu_opt_set(opts, "backend", "file");
3062 qemu_opt_set(opts, "path", p);
3063 return opts;
3064 }
3065 if (strstart(filename, "pipe:", &p)) {
3066 qemu_opt_set(opts, "backend", "pipe");
3067 qemu_opt_set(opts, "path", p);
3068 return opts;
3069 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003070 if (strstart(filename, "tcp:", &p) ||
3071 strstart(filename, "telnet:", &p)) {
3072 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3073 host[0] = 0;
3074 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3075 goto fail;
3076 }
3077 qemu_opt_set(opts, "backend", "socket");
3078 qemu_opt_set(opts, "host", host);
3079 qemu_opt_set(opts, "port", port);
3080 if (p[pos] == ',') {
3081 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3082 goto fail;
3083 }
3084 if (strstart(filename, "telnet:", &p))
3085 qemu_opt_set(opts, "telnet", "on");
3086 return opts;
3087 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003088 if (strstart(filename, "udp:", &p)) {
3089 qemu_opt_set(opts, "backend", "udp");
3090 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3091 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003092 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003093 goto fail;
3094 }
3095 }
3096 qemu_opt_set(opts, "host", host);
3097 qemu_opt_set(opts, "port", port);
3098 if (p[pos] == '@') {
3099 p += pos + 1;
3100 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3101 host[0] = 0;
3102 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003103 goto fail;
3104 }
3105 }
3106 qemu_opt_set(opts, "localaddr", host);
3107 qemu_opt_set(opts, "localport", port);
3108 }
3109 return opts;
3110 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003111 if (strstart(filename, "unix:", &p)) {
3112 qemu_opt_set(opts, "backend", "socket");
3113 if (qemu_opts_do_parse(opts, p, "path") != 0)
3114 goto fail;
3115 return opts;
3116 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003117 if (strstart(filename, "/dev/parport", NULL) ||
3118 strstart(filename, "/dev/ppi", NULL)) {
3119 qemu_opt_set(opts, "backend", "parport");
3120 qemu_opt_set(opts, "path", filename);
3121 return opts;
3122 }
3123 if (strstart(filename, "/dev/", NULL)) {
3124 qemu_opt_set(opts, "backend", "tty");
3125 qemu_opt_set(opts, "path", filename);
3126 return opts;
3127 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003128
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003129fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003130 qemu_opts_del(opts);
3131 return NULL;
3132}
3133
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003134static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3135 Error **errp)
3136{
3137 const char *path = qemu_opt_get(opts, "path");
3138
3139 if (path == NULL) {
3140 error_setg(errp, "chardev: file: no filename given");
3141 return;
3142 }
3143 backend->file = g_new0(ChardevFile, 1);
3144 backend->file->out = g_strdup(path);
3145}
3146
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003147static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3148 Error **errp)
3149{
3150 backend->stdio = g_new0(ChardevStdio, 1);
3151 backend->stdio->has_signal = true;
3152 backend->stdio->signal =
3153 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3154}
3155
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003156static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3157 Error **errp)
3158{
3159 const char *device = qemu_opt_get(opts, "path");
3160
3161 if (device == NULL) {
3162 error_setg(errp, "chardev: serial/tty: no device path given");
3163 return;
3164 }
3165 backend->serial = g_new0(ChardevHostdev, 1);
3166 backend->serial->device = g_strdup(device);
3167}
3168
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003169static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3170 Error **errp)
3171{
3172 const char *device = qemu_opt_get(opts, "path");
3173
3174 if (device == NULL) {
3175 error_setg(errp, "chardev: parallel: no device path given");
3176 return;
3177 }
3178 backend->parallel = g_new0(ChardevHostdev, 1);
3179 backend->parallel->device = g_strdup(device);
3180}
3181
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003182static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3183 Error **errp)
3184{
3185 const char *device = qemu_opt_get(opts, "path");
3186
3187 if (device == NULL) {
3188 error_setg(errp, "chardev: pipe: no device path given");
3189 return;
3190 }
3191 backend->pipe = g_new0(ChardevHostdev, 1);
3192 backend->pipe->device = g_strdup(device);
3193}
3194
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003195static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3196 Error **errp)
3197{
3198 int val;
3199
3200 backend->memory = g_new0(ChardevRingbuf, 1);
3201
3202 val = qemu_opt_get_number(opts, "size", 0);
3203 if (val != 0) {
3204 backend->memory->has_size = true;
3205 backend->memory->size = val;
3206 }
3207}
3208
Anthony Liguorid654f342013-03-05 23:21:28 +05303209typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003210 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003211 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003212 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003213 /* new, qapi-based */
3214 int kind;
3215 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303216} CharDriver;
3217
3218static GSList *backends;
3219
3220void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3221{
3222 CharDriver *s;
3223
3224 s = g_malloc0(sizeof(*s));
3225 s->name = g_strdup(name);
3226 s->open = open;
3227
3228 backends = g_slist_append(backends, s);
3229}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003231void register_char_driver_qapi(const char *name, int kind,
3232 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3233{
3234 CharDriver *s;
3235
3236 s = g_malloc0(sizeof(*s));
3237 s->name = g_strdup(name);
3238 s->kind = kind;
3239 s->parse = parse;
3240
3241 backends = g_slist_append(backends, s);
3242}
3243
Anthony Liguorif69554b2011-08-15 11:17:37 -05003244CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003245 void (*init)(struct CharDriverState *s),
3246 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003247{
Anthony Liguorid654f342013-03-05 23:21:28 +05303248 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003249 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303250 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003251
3252 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003253 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003254 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003255 }
3256
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003257 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003258 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003259 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003260 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003261 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303262 for (i = backends; i; i = i->next) {
3263 cd = i->data;
3264
3265 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003266 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303267 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003268 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303269 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003270 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003271 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303272 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003273 }
3274
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003275 if (!cd->open) {
3276 /* using new, qapi init */
3277 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3278 ChardevReturn *ret = NULL;
3279 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003280 const char *bid = NULL;
3281
3282 if (qemu_opt_get_bool(opts, "mux", 0)) {
3283 bid = g_strdup_printf("%s-base", id);
3284 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003285
3286 chr = NULL;
3287 backend->kind = cd->kind;
3288 if (cd->parse) {
3289 cd->parse(opts, backend, errp);
3290 if (error_is_set(errp)) {
3291 goto qapi_out;
3292 }
3293 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003294 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003295 if (error_is_set(errp)) {
3296 goto qapi_out;
3297 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003298
3299 if (bid) {
3300 qapi_free_ChardevBackend(backend);
3301 qapi_free_ChardevReturn(ret);
3302 backend = g_new0(ChardevBackend, 1);
3303 backend->mux = g_new0(ChardevMux, 1);
3304 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3305 backend->mux->chardev = g_strdup(bid);
3306 ret = qmp_chardev_add(id, backend, errp);
3307 if (error_is_set(errp)) {
3308 goto qapi_out;
3309 }
3310 }
3311
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003312 chr = qemu_chr_find(id);
3313
3314 qapi_out:
3315 qapi_free_ChardevBackend(backend);
3316 qapi_free_ChardevReturn(ret);
3317 return chr;
3318 }
3319
Anthony Liguorid654f342013-03-05 23:21:28 +05303320 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003321 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003322 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003323 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003324 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003325 }
3326
3327 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003328 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003329 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003330 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003331
3332 if (qemu_opt_get_bool(opts, "mux", 0)) {
3333 CharDriverState *base = chr;
3334 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003335 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003336 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3337 chr = qemu_chr_open_mux(base);
3338 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003339 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003340 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003341 } else {
3342 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003343 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003344 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003345 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003346 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003347
3348err:
3349 qemu_opts_del(opts);
3350 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003351}
3352
Anthony Liguori27143a42011-08-15 11:17:36 -05003353CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003354{
3355 const char *p;
3356 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003357 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003358 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003359
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003360 if (strstart(filename, "chardev:", &p)) {
3361 return qemu_chr_find(p);
3362 }
3363
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003364 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003365 if (!opts)
3366 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003367
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003368 chr = qemu_chr_new_from_opts(opts, init, &err);
3369 if (error_is_set(&err)) {
3370 fprintf(stderr, "%s\n", error_get_pretty(err));
3371 error_free(err);
3372 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003373 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3374 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003375 }
3376 return chr;
3377}
3378
Anthony Liguori15f31512011-08-15 11:17:35 -05003379void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003380{
3381 if (chr->chr_set_echo) {
3382 chr->chr_set_echo(chr, echo);
3383 }
3384}
3385
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003386void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003387{
3388 if (chr->chr_guest_open) {
3389 chr->chr_guest_open(chr);
3390 }
3391}
3392
Anthony Liguori28178222011-08-15 11:17:33 -05003393void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003394{
3395 if (chr->chr_guest_close) {
3396 chr->chr_guest_close(chr);
3397 }
3398}
3399
Anthony Liguori23673ca2013-03-05 23:21:23 +05303400guint qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3401 GIOFunc func, void *user_data)
3402{
3403 GSource *src;
3404 guint tag;
3405
3406 if (s->chr_add_watch == NULL) {
3407 return -ENOSYS;
3408 }
3409
3410 src = s->chr_add_watch(s, cond);
3411 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3412 tag = g_source_attach(src, NULL);
3413 g_source_unref(src);
3414
3415 return tag;
3416}
3417
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003418void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003419{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003420 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003421 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003422 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003423 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003424 g_free(chr->filename);
3425 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003426 if (chr->opts) {
3427 qemu_opts_del(chr->opts);
3428 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003429 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003430}
3431
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003432ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003433{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003434 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003435 CharDriverState *chr;
3436
Blue Swirl72cf2d42009-09-12 07:36:22 +00003437 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003438 ChardevInfoList *info = g_malloc0(sizeof(*info));
3439 info->value = g_malloc0(sizeof(*info->value));
3440 info->value->label = g_strdup(chr->label);
3441 info->value->filename = g_strdup(chr->filename);
3442
3443 info->next = chr_list;
3444 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003445 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003446
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003447 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003448}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003449
3450CharDriverState *qemu_chr_find(const char *name)
3451{
3452 CharDriverState *chr;
3453
Blue Swirl72cf2d42009-09-12 07:36:22 +00003454 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003455 if (strcmp(chr->label, name) != 0)
3456 continue;
3457 return chr;
3458 }
3459 return NULL;
3460}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003461
3462/* Get a character (serial) device interface. */
3463CharDriverState *qemu_char_get_next_serial(void)
3464{
3465 static int next_serial;
3466
3467 /* FIXME: This function needs to go away: use chardev properties! */
3468 return serial_hds[next_serial++];
3469}
3470
Paolo Bonzini4d454572012-11-26 16:03:42 +01003471QemuOptsList qemu_chardev_opts = {
3472 .name = "chardev",
3473 .implied_opt_name = "backend",
3474 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3475 .desc = {
3476 {
3477 .name = "backend",
3478 .type = QEMU_OPT_STRING,
3479 },{
3480 .name = "path",
3481 .type = QEMU_OPT_STRING,
3482 },{
3483 .name = "host",
3484 .type = QEMU_OPT_STRING,
3485 },{
3486 .name = "port",
3487 .type = QEMU_OPT_STRING,
3488 },{
3489 .name = "localaddr",
3490 .type = QEMU_OPT_STRING,
3491 },{
3492 .name = "localport",
3493 .type = QEMU_OPT_STRING,
3494 },{
3495 .name = "to",
3496 .type = QEMU_OPT_NUMBER,
3497 },{
3498 .name = "ipv4",
3499 .type = QEMU_OPT_BOOL,
3500 },{
3501 .name = "ipv6",
3502 .type = QEMU_OPT_BOOL,
3503 },{
3504 .name = "wait",
3505 .type = QEMU_OPT_BOOL,
3506 },{
3507 .name = "server",
3508 .type = QEMU_OPT_BOOL,
3509 },{
3510 .name = "delay",
3511 .type = QEMU_OPT_BOOL,
3512 },{
3513 .name = "telnet",
3514 .type = QEMU_OPT_BOOL,
3515 },{
3516 .name = "width",
3517 .type = QEMU_OPT_NUMBER,
3518 },{
3519 .name = "height",
3520 .type = QEMU_OPT_NUMBER,
3521 },{
3522 .name = "cols",
3523 .type = QEMU_OPT_NUMBER,
3524 },{
3525 .name = "rows",
3526 .type = QEMU_OPT_NUMBER,
3527 },{
3528 .name = "mux",
3529 .type = QEMU_OPT_BOOL,
3530 },{
3531 .name = "signal",
3532 .type = QEMU_OPT_BOOL,
3533 },{
3534 .name = "name",
3535 .type = QEMU_OPT_STRING,
3536 },{
3537 .name = "debug",
3538 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003539 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003540 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003541 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003542 },
3543 { /* end of list */ }
3544 },
3545};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003546
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003547#ifdef _WIN32
3548
3549static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3550{
3551 HANDLE out;
3552
3553 if (file->in) {
3554 error_setg(errp, "input file not supported");
3555 return NULL;
3556 }
3557
3558 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3559 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3560 if (out == INVALID_HANDLE_VALUE) {
3561 error_setg(errp, "open %s failed", file->out);
3562 return NULL;
3563 }
3564 return qemu_chr_open_win_file(out);
3565}
3566
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003567static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3568 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003569{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003570 return qemu_chr_open_win_path(serial->device);
3571}
3572
3573static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3574 Error **errp)
3575{
3576 error_setg(errp, "character device backend type 'parallel' not supported");
3577 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003578}
3579
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003580#else /* WIN32 */
3581
3582static int qmp_chardev_open_file_source(char *src, int flags,
3583 Error **errp)
3584{
3585 int fd = -1;
3586
3587 TFR(fd = qemu_open(src, flags, 0666));
3588 if (fd == -1) {
3589 error_setg(errp, "open %s: %s", src, strerror(errno));
3590 }
3591 return fd;
3592}
3593
3594static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3595{
3596 int flags, in = -1, out = -1;
3597
3598 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3599 out = qmp_chardev_open_file_source(file->out, flags, errp);
3600 if (error_is_set(errp)) {
3601 return NULL;
3602 }
3603
3604 if (file->in) {
3605 flags = O_RDONLY;
3606 in = qmp_chardev_open_file_source(file->in, flags, errp);
3607 if (error_is_set(errp)) {
3608 qemu_close(out);
3609 return NULL;
3610 }
3611 }
3612
3613 return qemu_chr_open_fd(in, out);
3614}
3615
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003616static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3617 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003618{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003619#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003620 int fd;
3621
3622 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3623 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003624 return NULL;
3625 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003626 socket_set_nonblock(fd);
3627 return qemu_chr_open_tty_fd(fd);
3628#else
3629 error_setg(errp, "character device backend type 'serial' not supported");
3630 return NULL;
3631#endif
3632}
3633
3634static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3635 Error **errp)
3636{
3637#ifdef HAVE_CHARDEV_PARPORT
3638 int fd;
3639
3640 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3641 if (error_is_set(errp)) {
3642 return NULL;
3643 }
3644 return qemu_chr_open_pp_fd(fd);
3645#else
3646 error_setg(errp, "character device backend type 'parallel' not supported");
3647 return NULL;
3648#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003649}
3650
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003651#endif /* WIN32 */
3652
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003653static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3654 Error **errp)
3655{
3656 SocketAddress *addr = sock->addr;
3657 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3658 bool is_listen = sock->has_server ? sock->server : true;
3659 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3660 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3661 int fd;
3662
3663 if (is_listen) {
3664 fd = socket_listen(addr, errp);
3665 } else {
3666 fd = socket_connect(addr, errp, NULL, NULL);
3667 }
3668 if (error_is_set(errp)) {
3669 return NULL;
3670 }
3671 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3672 is_telnet, is_waitconnect, errp);
3673}
3674
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003675static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3676 Error **errp)
3677{
3678 int fd;
3679
3680 fd = socket_dgram(dgram->remote, dgram->local, errp);
3681 if (error_is_set(errp)) {
3682 return NULL;
3683 }
3684 return qemu_chr_open_udp_fd(fd);
3685}
3686
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003687ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3688 Error **errp)
3689{
3690 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003691 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003692
3693 chr = qemu_chr_find(id);
3694 if (chr) {
3695 error_setg(errp, "Chardev '%s' already exists", id);
3696 g_free(ret);
3697 return NULL;
3698 }
3699
3700 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003701 case CHARDEV_BACKEND_KIND_FILE:
3702 chr = qmp_chardev_open_file(backend->file, errp);
3703 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003704 case CHARDEV_BACKEND_KIND_SERIAL:
3705 chr = qmp_chardev_open_serial(backend->serial, errp);
3706 break;
3707 case CHARDEV_BACKEND_KIND_PARALLEL:
3708 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003709 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003710 case CHARDEV_BACKEND_KIND_PIPE:
3711 chr = qemu_chr_open_pipe(backend->pipe);
3712 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003713 case CHARDEV_BACKEND_KIND_SOCKET:
3714 chr = qmp_chardev_open_socket(backend->socket, errp);
3715 break;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003716 case CHARDEV_BACKEND_KIND_DGRAM:
3717 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3718 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003719#ifdef HAVE_CHARDEV_TTY
3720 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003721 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003722 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003723#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003724 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003725 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003726 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003727 case CHARDEV_BACKEND_KIND_MUX:
3728 base = qemu_chr_find(backend->mux->chardev);
3729 if (base == NULL) {
3730 error_setg(errp, "mux: base chardev %s not found",
3731 backend->mux->chardev);
3732 break;
3733 }
3734 chr = qemu_chr_open_mux(base);
3735 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003736 case CHARDEV_BACKEND_KIND_MSMOUSE:
3737 chr = qemu_chr_open_msmouse();
3738 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003739#ifdef CONFIG_BRLAPI
3740 case CHARDEV_BACKEND_KIND_BRAILLE:
3741 chr = chr_baum_init();
3742 break;
3743#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003744 case CHARDEV_BACKEND_KIND_STDIO:
3745 chr = qemu_chr_open_stdio(backend->stdio);
3746 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003747#ifdef _WIN32
3748 case CHARDEV_BACKEND_KIND_CONSOLE:
3749 chr = qemu_chr_open_win_con();
3750 break;
3751#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003752#ifdef CONFIG_SPICE
3753 case CHARDEV_BACKEND_KIND_SPICEVMC:
3754 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3755 break;
3756 case CHARDEV_BACKEND_KIND_SPICEPORT:
3757 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3758 break;
3759#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003760 case CHARDEV_BACKEND_KIND_VC:
3761 chr = vc_init(backend->vc);
3762 break;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003763 case CHARDEV_BACKEND_KIND_MEMORY:
3764 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3765 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003766 default:
3767 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3768 break;
3769 }
3770
3771 if (chr == NULL && !error_is_set(errp)) {
3772 error_setg(errp, "Failed to create chardev");
3773 }
3774 if (chr) {
3775 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003776 chr->avail_connections =
3777 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003778 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3779 return ret;
3780 } else {
3781 g_free(ret);
3782 return NULL;
3783 }
3784}
3785
3786void qmp_chardev_remove(const char *id, Error **errp)
3787{
3788 CharDriverState *chr;
3789
3790 chr = qemu_chr_find(id);
3791 if (NULL == chr) {
3792 error_setg(errp, "Chardev '%s' not found", id);
3793 return;
3794 }
3795 if (chr->chr_can_read || chr->chr_read ||
3796 chr->chr_event || chr->handler_opaque) {
3797 error_setg(errp, "Chardev '%s' is busy", id);
3798 return;
3799 }
3800 qemu_chr_delete(chr);
3801}
Anthony Liguorid654f342013-03-05 23:21:28 +05303802
3803static void register_types(void)
3804{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003805 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303806 register_char_driver("socket", qemu_chr_open_socket);
3807 register_char_driver("udp", qemu_chr_open_udp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003808 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3809 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003810 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3811 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003812 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3813 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003814 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3815 qemu_chr_parse_serial);
3816 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3817 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003818 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3819 qemu_chr_parse_parallel);
3820 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3821 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003822 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003823 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003824 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3825 qemu_chr_parse_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303826}
3827
3828type_init(register_types);