blob: 4b9caca92038d396986e6b54ec6a9bd9edc9ecf8 [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
Markus Armbruster1f514702012-02-07 15:09:08 +0100844static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000845{
846 int fd_in, fd_out;
847 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200848 const char *filename = qemu_opt_get(opts, "path");
849
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
Markus Armbruster1f514702012-02-07 15:09:08 +0100911static CharDriverState *qemu_chr_open_stdio(QemuOpts *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;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100927 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
928 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500929 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000930
Markus Armbruster1f514702012-02-07 15:09:08 +0100931 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000932}
933
934#ifdef __sun__
935/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000936static int openpty(int *amaster, int *aslave, char *name,
937 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000938{
939 const char *slave;
940 int mfd = -1, sfd = -1;
941
942 *amaster = *aslave = -1;
943
944 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
945 if (mfd < 0)
946 goto err;
947
948 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
949 goto err;
950
951 if ((slave = ptsname(mfd)) == NULL)
952 goto err;
953
954 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
955 goto err;
956
957 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
958 (termp != NULL && tcgetattr(sfd, termp) < 0))
959 goto err;
960
961 if (amaster)
962 *amaster = mfd;
963 if (aslave)
964 *aslave = sfd;
965 if (winp)
966 ioctl(sfd, TIOCSWINSZ, winp);
967
968 return 0;
969
970err:
971 if (sfd != -1)
972 close(sfd);
973 close(mfd);
974 return -1;
975}
976
blueswir13f4cb3d2009-04-13 16:31:01 +0000977static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000978{
979 termios_p->c_iflag &=
980 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
981 termios_p->c_oflag &= ~OPOST;
982 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
983 termios_p->c_cflag &= ~(CSIZE|PARENB);
984 termios_p->c_cflag |= CS8;
985
986 termios_p->c_cc[VMIN] = 0;
987 termios_p->c_cc[VTIME] = 0;
988}
989#endif
990
991#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100992 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
993 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000994
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100995#define HAVE_CHARDEV_TTY 1
996
aliguori6f97dba2008-10-31 18:49:55 +0000997typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +0530998 GIOChannel *fd;
999 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001000 int connected;
1001 int polling;
1002 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301003 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001004} PtyCharDriver;
1005
1006static void pty_chr_update_read_handler(CharDriverState *chr);
1007static void pty_chr_state(CharDriverState *chr, int connected);
1008
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301009static gboolean pty_chr_timer(gpointer opaque)
1010{
1011 struct CharDriverState *chr = opaque;
1012 PtyCharDriver *s = chr->opaque;
1013
1014 if (s->connected) {
1015 goto out;
1016 }
1017 if (s->polling) {
1018 /* If we arrive here without polling being cleared due
1019 * read returning -EIO, then we are (re-)connected */
1020 pty_chr_state(chr, 1);
1021 goto out;
1022 }
1023
1024 /* Next poll ... */
1025 pty_chr_update_read_handler(chr);
1026
1027out:
1028 return FALSE;
1029}
1030
1031static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1032{
1033 PtyCharDriver *s = chr->opaque;
1034
1035 if (s->timer_tag) {
1036 g_source_remove(s->timer_tag);
1037 s->timer_tag = 0;
1038 }
1039
1040 if (ms == 1000) {
1041 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1042 } else {
1043 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1044 }
1045}
1046
aliguori6f97dba2008-10-31 18:49:55 +00001047static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1048{
1049 PtyCharDriver *s = chr->opaque;
1050
1051 if (!s->connected) {
1052 /* guest sends data, check for (re-)connect */
1053 pty_chr_update_read_handler(chr);
1054 return 0;
1055 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301056 return io_channel_send_all(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001057}
1058
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301059static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1060{
1061 PtyCharDriver *s = chr->opaque;
1062 return g_io_create_watch(s->fd, cond);
1063}
1064
aliguori6f97dba2008-10-31 18:49:55 +00001065static int pty_chr_read_poll(void *opaque)
1066{
1067 CharDriverState *chr = opaque;
1068 PtyCharDriver *s = chr->opaque;
1069
Anthony Liguori909cda12011-08-15 11:17:31 -05001070 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001071 return s->read_bytes;
1072}
1073
Anthony Liguori093d3a22013-03-05 23:21:20 +05301074static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001075{
1076 CharDriverState *chr = opaque;
1077 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301078 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301079 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301080 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001081
1082 len = sizeof(buf);
1083 if (len > s->read_bytes)
1084 len = s->read_bytes;
1085 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301086 return FALSE;
1087 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1088 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001089 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301090 return FALSE;
1091 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001092 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001093 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001094 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301095 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001096}
1097
1098static void pty_chr_update_read_handler(CharDriverState *chr)
1099{
1100 PtyCharDriver *s = chr->opaque;
1101
Anthony Liguori093d3a22013-03-05 23:21:20 +05301102 if (s->fd_tag) {
1103 g_source_remove(s->fd_tag);
1104 }
1105
1106 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001107 s->polling = 1;
1108 /*
1109 * Short timeout here: just need wait long enougth that qemu makes
1110 * it through the poll loop once. When reconnected we want a
1111 * short timeout so we notice it almost instantly. Otherwise
1112 * read() gives us -EIO instantly, making pty_chr_state() reset the
1113 * timeout to the normal (much longer) poll interval before the
1114 * timer triggers.
1115 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301116 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001117}
1118
1119static void pty_chr_state(CharDriverState *chr, int connected)
1120{
1121 PtyCharDriver *s = chr->opaque;
1122
1123 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301124 g_source_remove(s->fd_tag);
1125 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001126 s->connected = 0;
1127 s->polling = 0;
1128 /* (re-)connect poll interval for idle guests: once per second.
1129 * We check more frequently in case the guests sends data to
1130 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301131 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001132 } else {
1133 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301134 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001135 s->connected = 1;
1136 }
1137}
1138
aliguori6f97dba2008-10-31 18:49:55 +00001139
1140static void pty_chr_close(struct CharDriverState *chr)
1141{
1142 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301143 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001144
Anthony Liguori093d3a22013-03-05 23:21:20 +05301145 if (s->fd_tag) {
1146 g_source_remove(s->fd_tag);
1147 }
1148 fd = g_io_channel_unix_get_fd(s->fd);
1149 g_io_channel_unref(s->fd);
1150 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301151 if (s->timer_tag) {
1152 g_source_remove(s->timer_tag);
1153 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001154 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001155 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001156}
1157
Markus Armbruster1f514702012-02-07 15:09:08 +01001158static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001159{
1160 CharDriverState *chr;
1161 PtyCharDriver *s;
1162 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001163 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001164 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001165#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001166 char pty_name[PATH_MAX];
1167#define q_ptsname(x) pty_name
1168#else
1169 char *pty_name = NULL;
1170#define q_ptsname(x) ptsname(x)
1171#endif
1172
Markus Armbrustera4e26042011-11-11 10:40:05 +01001173 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001174 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001175 }
1176
1177 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001178 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001179 cfmakeraw(&tty);
1180 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1181 close(slave_fd);
1182
Markus Armbrustera4e26042011-11-11 10:40:05 +01001183 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001184
Markus Armbrustera4e26042011-11-11 10:40:05 +01001185 len = strlen(q_ptsname(master_fd)) + 5;
1186 chr->filename = g_malloc(len);
1187 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1188 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001189
1190 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001191 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1192 q_ptsname(master_fd),
1193 label ? " (label " : "",
1194 label ? label : "",
1195 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001196
1197 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001198 chr->opaque = s;
1199 chr->chr_write = pty_chr_write;
1200 chr->chr_update_read_handler = pty_chr_update_read_handler;
1201 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301202 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001203
Anthony Liguori093d3a22013-03-05 23:21:20 +05301204 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301205 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001206
Markus Armbruster1f514702012-02-07 15:09:08 +01001207 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001208}
1209
1210static void tty_serial_init(int fd, int speed,
1211 int parity, int data_bits, int stop_bits)
1212{
1213 struct termios tty;
1214 speed_t spd;
1215
1216#if 0
1217 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1218 speed, parity, data_bits, stop_bits);
1219#endif
1220 tcgetattr (fd, &tty);
1221
Stefan Weil45eea132009-10-26 16:10:10 +01001222#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1223 speed = speed * 10 / 11;
1224 do {
1225 check_speed(50);
1226 check_speed(75);
1227 check_speed(110);
1228 check_speed(134);
1229 check_speed(150);
1230 check_speed(200);
1231 check_speed(300);
1232 check_speed(600);
1233 check_speed(1200);
1234 check_speed(1800);
1235 check_speed(2400);
1236 check_speed(4800);
1237 check_speed(9600);
1238 check_speed(19200);
1239 check_speed(38400);
1240 /* Non-Posix values follow. They may be unsupported on some systems. */
1241 check_speed(57600);
1242 check_speed(115200);
1243#ifdef B230400
1244 check_speed(230400);
1245#endif
1246#ifdef B460800
1247 check_speed(460800);
1248#endif
1249#ifdef B500000
1250 check_speed(500000);
1251#endif
1252#ifdef B576000
1253 check_speed(576000);
1254#endif
1255#ifdef B921600
1256 check_speed(921600);
1257#endif
1258#ifdef B1000000
1259 check_speed(1000000);
1260#endif
1261#ifdef B1152000
1262 check_speed(1152000);
1263#endif
1264#ifdef B1500000
1265 check_speed(1500000);
1266#endif
1267#ifdef B2000000
1268 check_speed(2000000);
1269#endif
1270#ifdef B2500000
1271 check_speed(2500000);
1272#endif
1273#ifdef B3000000
1274 check_speed(3000000);
1275#endif
1276#ifdef B3500000
1277 check_speed(3500000);
1278#endif
1279#ifdef B4000000
1280 check_speed(4000000);
1281#endif
aliguori6f97dba2008-10-31 18:49:55 +00001282 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001283 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001284
1285 cfsetispeed(&tty, spd);
1286 cfsetospeed(&tty, spd);
1287
1288 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1289 |INLCR|IGNCR|ICRNL|IXON);
1290 tty.c_oflag |= OPOST;
1291 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1292 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1293 switch(data_bits) {
1294 default:
1295 case 8:
1296 tty.c_cflag |= CS8;
1297 break;
1298 case 7:
1299 tty.c_cflag |= CS7;
1300 break;
1301 case 6:
1302 tty.c_cflag |= CS6;
1303 break;
1304 case 5:
1305 tty.c_cflag |= CS5;
1306 break;
1307 }
1308 switch(parity) {
1309 default:
1310 case 'N':
1311 break;
1312 case 'E':
1313 tty.c_cflag |= PARENB;
1314 break;
1315 case 'O':
1316 tty.c_cflag |= PARENB | PARODD;
1317 break;
1318 }
1319 if (stop_bits == 2)
1320 tty.c_cflag |= CSTOPB;
1321
1322 tcsetattr (fd, TCSANOW, &tty);
1323}
1324
1325static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1326{
1327 FDCharDriver *s = chr->opaque;
1328
1329 switch(cmd) {
1330 case CHR_IOCTL_SERIAL_SET_PARAMS:
1331 {
1332 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301333 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1334 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001335 ssp->data_bits, ssp->stop_bits);
1336 }
1337 break;
1338 case CHR_IOCTL_SERIAL_SET_BREAK:
1339 {
1340 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301341 if (enable) {
1342 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1343 }
aliguori6f97dba2008-10-31 18:49:55 +00001344 }
1345 break;
1346 case CHR_IOCTL_SERIAL_GET_TIOCM:
1347 {
1348 int sarg = 0;
1349 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301350 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001351 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001352 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001353 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001354 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001355 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001356 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001357 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001358 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001359 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001360 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001361 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001362 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001363 *targ |= CHR_TIOCM_RTS;
1364 }
1365 break;
1366 case CHR_IOCTL_SERIAL_SET_TIOCM:
1367 {
1368 int sarg = *(int *)arg;
1369 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301370 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001371 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1372 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1373 if (sarg & CHR_TIOCM_CTS)
1374 targ |= TIOCM_CTS;
1375 if (sarg & CHR_TIOCM_CAR)
1376 targ |= TIOCM_CAR;
1377 if (sarg & CHR_TIOCM_DSR)
1378 targ |= TIOCM_DSR;
1379 if (sarg & CHR_TIOCM_RI)
1380 targ |= TIOCM_RI;
1381 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001382 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001383 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001384 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301385 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001386 }
1387 break;
1388 default:
1389 return -ENOTSUP;
1390 }
1391 return 0;
1392}
1393
David Ahern4266a132010-02-10 18:27:17 -07001394static void qemu_chr_close_tty(CharDriverState *chr)
1395{
1396 FDCharDriver *s = chr->opaque;
1397 int fd = -1;
1398
1399 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301400 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001401 }
1402
1403 fd_chr_close(chr);
1404
1405 if (fd >= 0) {
1406 close(fd);
1407 }
1408}
1409
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001410static CharDriverState *qemu_chr_open_tty_fd(int fd)
1411{
1412 CharDriverState *chr;
1413
1414 tty_serial_init(fd, 115200, 'N', 8, 1);
1415 chr = qemu_chr_open_fd(fd, fd);
1416 chr->chr_ioctl = tty_serial_ioctl;
1417 chr->chr_close = qemu_chr_close_tty;
1418 return chr;
1419}
1420
Markus Armbruster1f514702012-02-07 15:09:08 +01001421static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001422{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001423 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001424 int fd;
1425
Markus Armbrusterb181e042012-02-07 15:09:09 +01001426 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001427 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001428 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001429 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001430 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001431}
aliguori6f97dba2008-10-31 18:49:55 +00001432#endif /* __linux__ || __sun__ */
1433
1434#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001435
1436#define HAVE_CHARDEV_PARPORT 1
1437
aliguori6f97dba2008-10-31 18:49:55 +00001438typedef struct {
1439 int fd;
1440 int mode;
1441} ParallelCharDriver;
1442
1443static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1444{
1445 if (s->mode != mode) {
1446 int m = mode;
1447 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1448 return 0;
1449 s->mode = mode;
1450 }
1451 return 1;
1452}
1453
1454static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1455{
1456 ParallelCharDriver *drv = chr->opaque;
1457 int fd = drv->fd;
1458 uint8_t b;
1459
1460 switch(cmd) {
1461 case CHR_IOCTL_PP_READ_DATA:
1462 if (ioctl(fd, PPRDATA, &b) < 0)
1463 return -ENOTSUP;
1464 *(uint8_t *)arg = b;
1465 break;
1466 case CHR_IOCTL_PP_WRITE_DATA:
1467 b = *(uint8_t *)arg;
1468 if (ioctl(fd, PPWDATA, &b) < 0)
1469 return -ENOTSUP;
1470 break;
1471 case CHR_IOCTL_PP_READ_CONTROL:
1472 if (ioctl(fd, PPRCONTROL, &b) < 0)
1473 return -ENOTSUP;
1474 /* Linux gives only the lowest bits, and no way to know data
1475 direction! For better compatibility set the fixed upper
1476 bits. */
1477 *(uint8_t *)arg = b | 0xc0;
1478 break;
1479 case CHR_IOCTL_PP_WRITE_CONTROL:
1480 b = *(uint8_t *)arg;
1481 if (ioctl(fd, PPWCONTROL, &b) < 0)
1482 return -ENOTSUP;
1483 break;
1484 case CHR_IOCTL_PP_READ_STATUS:
1485 if (ioctl(fd, PPRSTATUS, &b) < 0)
1486 return -ENOTSUP;
1487 *(uint8_t *)arg = b;
1488 break;
1489 case CHR_IOCTL_PP_DATA_DIR:
1490 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1491 return -ENOTSUP;
1492 break;
1493 case CHR_IOCTL_PP_EPP_READ_ADDR:
1494 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1495 struct ParallelIOArg *parg = arg;
1496 int n = read(fd, parg->buffer, parg->count);
1497 if (n != parg->count) {
1498 return -EIO;
1499 }
1500 }
1501 break;
1502 case CHR_IOCTL_PP_EPP_READ:
1503 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1504 struct ParallelIOArg *parg = arg;
1505 int n = read(fd, parg->buffer, parg->count);
1506 if (n != parg->count) {
1507 return -EIO;
1508 }
1509 }
1510 break;
1511 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1512 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1513 struct ParallelIOArg *parg = arg;
1514 int n = write(fd, parg->buffer, parg->count);
1515 if (n != parg->count) {
1516 return -EIO;
1517 }
1518 }
1519 break;
1520 case CHR_IOCTL_PP_EPP_WRITE:
1521 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1522 struct ParallelIOArg *parg = arg;
1523 int n = write(fd, parg->buffer, parg->count);
1524 if (n != parg->count) {
1525 return -EIO;
1526 }
1527 }
1528 break;
1529 default:
1530 return -ENOTSUP;
1531 }
1532 return 0;
1533}
1534
1535static void pp_close(CharDriverState *chr)
1536{
1537 ParallelCharDriver *drv = chr->opaque;
1538 int fd = drv->fd;
1539
1540 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1541 ioctl(fd, PPRELEASE);
1542 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001543 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001544 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001545}
1546
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001547static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001548{
1549 CharDriverState *chr;
1550 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001551
1552 if (ioctl(fd, PPCLAIM) < 0) {
1553 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001554 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001555 }
1556
Anthony Liguori7267c092011-08-20 22:09:37 -05001557 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001558 drv->fd = fd;
1559 drv->mode = IEEE1284_MODE_COMPAT;
1560
Anthony Liguori7267c092011-08-20 22:09:37 -05001561 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001562 chr->chr_write = null_chr_write;
1563 chr->chr_ioctl = pp_ioctl;
1564 chr->chr_close = pp_close;
1565 chr->opaque = drv;
1566
Amit Shah127338e2009-11-03 19:59:56 +05301567 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001568
Markus Armbruster1f514702012-02-07 15:09:08 +01001569 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001570}
1571#endif /* __linux__ */
1572
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001573#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001574
1575#define HAVE_CHARDEV_PARPORT 1
1576
blueswir16972f932008-11-22 20:49:12 +00001577static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1578{
Stefan Weile0efb992011-02-23 19:09:16 +01001579 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001580 uint8_t b;
1581
1582 switch(cmd) {
1583 case CHR_IOCTL_PP_READ_DATA:
1584 if (ioctl(fd, PPIGDATA, &b) < 0)
1585 return -ENOTSUP;
1586 *(uint8_t *)arg = b;
1587 break;
1588 case CHR_IOCTL_PP_WRITE_DATA:
1589 b = *(uint8_t *)arg;
1590 if (ioctl(fd, PPISDATA, &b) < 0)
1591 return -ENOTSUP;
1592 break;
1593 case CHR_IOCTL_PP_READ_CONTROL:
1594 if (ioctl(fd, PPIGCTRL, &b) < 0)
1595 return -ENOTSUP;
1596 *(uint8_t *)arg = b;
1597 break;
1598 case CHR_IOCTL_PP_WRITE_CONTROL:
1599 b = *(uint8_t *)arg;
1600 if (ioctl(fd, PPISCTRL, &b) < 0)
1601 return -ENOTSUP;
1602 break;
1603 case CHR_IOCTL_PP_READ_STATUS:
1604 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1605 return -ENOTSUP;
1606 *(uint8_t *)arg = b;
1607 break;
1608 default:
1609 return -ENOTSUP;
1610 }
1611 return 0;
1612}
1613
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001614static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001615{
1616 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001617
Anthony Liguori7267c092011-08-20 22:09:37 -05001618 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001619 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001620 chr->chr_write = null_chr_write;
1621 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001622 return chr;
blueswir16972f932008-11-22 20:49:12 +00001623}
1624#endif
1625
aliguori6f97dba2008-10-31 18:49:55 +00001626#else /* _WIN32 */
1627
1628typedef struct {
1629 int max_size;
1630 HANDLE hcom, hrecv, hsend;
1631 OVERLAPPED orecv, osend;
1632 BOOL fpipe;
1633 DWORD len;
1634} WinCharState;
1635
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001636typedef struct {
1637 HANDLE hStdIn;
1638 HANDLE hInputReadyEvent;
1639 HANDLE hInputDoneEvent;
1640 HANDLE hInputThread;
1641 uint8_t win_stdio_buf;
1642} WinStdioCharState;
1643
aliguori6f97dba2008-10-31 18:49:55 +00001644#define NSENDBUF 2048
1645#define NRECVBUF 2048
1646#define MAXCONNECT 1
1647#define NTIMEOUT 5000
1648
1649static int win_chr_poll(void *opaque);
1650static int win_chr_pipe_poll(void *opaque);
1651
1652static void win_chr_close(CharDriverState *chr)
1653{
1654 WinCharState *s = chr->opaque;
1655
1656 if (s->hsend) {
1657 CloseHandle(s->hsend);
1658 s->hsend = NULL;
1659 }
1660 if (s->hrecv) {
1661 CloseHandle(s->hrecv);
1662 s->hrecv = NULL;
1663 }
1664 if (s->hcom) {
1665 CloseHandle(s->hcom);
1666 s->hcom = NULL;
1667 }
1668 if (s->fpipe)
1669 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1670 else
1671 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301672
Hans de Goedea425d232011-11-19 10:22:43 +01001673 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001674}
1675
1676static int win_chr_init(CharDriverState *chr, const char *filename)
1677{
1678 WinCharState *s = chr->opaque;
1679 COMMCONFIG comcfg;
1680 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1681 COMSTAT comstat;
1682 DWORD size;
1683 DWORD err;
1684
1685 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1686 if (!s->hsend) {
1687 fprintf(stderr, "Failed CreateEvent\n");
1688 goto fail;
1689 }
1690 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1691 if (!s->hrecv) {
1692 fprintf(stderr, "Failed CreateEvent\n");
1693 goto fail;
1694 }
1695
1696 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1697 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1698 if (s->hcom == INVALID_HANDLE_VALUE) {
1699 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1700 s->hcom = NULL;
1701 goto fail;
1702 }
1703
1704 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1705 fprintf(stderr, "Failed SetupComm\n");
1706 goto fail;
1707 }
1708
1709 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1710 size = sizeof(COMMCONFIG);
1711 GetDefaultCommConfig(filename, &comcfg, &size);
1712 comcfg.dcb.DCBlength = sizeof(DCB);
1713 CommConfigDialog(filename, NULL, &comcfg);
1714
1715 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1716 fprintf(stderr, "Failed SetCommState\n");
1717 goto fail;
1718 }
1719
1720 if (!SetCommMask(s->hcom, EV_ERR)) {
1721 fprintf(stderr, "Failed SetCommMask\n");
1722 goto fail;
1723 }
1724
1725 cto.ReadIntervalTimeout = MAXDWORD;
1726 if (!SetCommTimeouts(s->hcom, &cto)) {
1727 fprintf(stderr, "Failed SetCommTimeouts\n");
1728 goto fail;
1729 }
1730
1731 if (!ClearCommError(s->hcom, &err, &comstat)) {
1732 fprintf(stderr, "Failed ClearCommError\n");
1733 goto fail;
1734 }
1735 qemu_add_polling_cb(win_chr_poll, chr);
1736 return 0;
1737
1738 fail:
1739 win_chr_close(chr);
1740 return -1;
1741}
1742
1743static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1744{
1745 WinCharState *s = chr->opaque;
1746 DWORD len, ret, size, err;
1747
1748 len = len1;
1749 ZeroMemory(&s->osend, sizeof(s->osend));
1750 s->osend.hEvent = s->hsend;
1751 while (len > 0) {
1752 if (s->hsend)
1753 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1754 else
1755 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1756 if (!ret) {
1757 err = GetLastError();
1758 if (err == ERROR_IO_PENDING) {
1759 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1760 if (ret) {
1761 buf += size;
1762 len -= size;
1763 } else {
1764 break;
1765 }
1766 } else {
1767 break;
1768 }
1769 } else {
1770 buf += size;
1771 len -= size;
1772 }
1773 }
1774 return len1 - len;
1775}
1776
1777static int win_chr_read_poll(CharDriverState *chr)
1778{
1779 WinCharState *s = chr->opaque;
1780
Anthony Liguori909cda12011-08-15 11:17:31 -05001781 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001782 return s->max_size;
1783}
1784
1785static void win_chr_readfile(CharDriverState *chr)
1786{
1787 WinCharState *s = chr->opaque;
1788 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301789 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001790 DWORD size;
1791
1792 ZeroMemory(&s->orecv, sizeof(s->orecv));
1793 s->orecv.hEvent = s->hrecv;
1794 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1795 if (!ret) {
1796 err = GetLastError();
1797 if (err == ERROR_IO_PENDING) {
1798 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1799 }
1800 }
1801
1802 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001803 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001804 }
1805}
1806
1807static void win_chr_read(CharDriverState *chr)
1808{
1809 WinCharState *s = chr->opaque;
1810
1811 if (s->len > s->max_size)
1812 s->len = s->max_size;
1813 if (s->len == 0)
1814 return;
1815
1816 win_chr_readfile(chr);
1817}
1818
1819static int win_chr_poll(void *opaque)
1820{
1821 CharDriverState *chr = opaque;
1822 WinCharState *s = chr->opaque;
1823 COMSTAT status;
1824 DWORD comerr;
1825
1826 ClearCommError(s->hcom, &comerr, &status);
1827 if (status.cbInQue > 0) {
1828 s->len = status.cbInQue;
1829 win_chr_read_poll(chr);
1830 win_chr_read(chr);
1831 return 1;
1832 }
1833 return 0;
1834}
1835
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001836static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001837{
1838 CharDriverState *chr;
1839 WinCharState *s;
1840
Anthony Liguori7267c092011-08-20 22:09:37 -05001841 chr = g_malloc0(sizeof(CharDriverState));
1842 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001843 chr->opaque = s;
1844 chr->chr_write = win_chr_write;
1845 chr->chr_close = win_chr_close;
1846
1847 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001848 g_free(s);
1849 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001850 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001851 }
Amit Shah127338e2009-11-03 19:59:56 +05301852 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001853 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001854}
1855
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001856static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1857{
1858 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1859}
1860
aliguori6f97dba2008-10-31 18:49:55 +00001861static int win_chr_pipe_poll(void *opaque)
1862{
1863 CharDriverState *chr = opaque;
1864 WinCharState *s = chr->opaque;
1865 DWORD size;
1866
1867 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1868 if (size > 0) {
1869 s->len = size;
1870 win_chr_read_poll(chr);
1871 win_chr_read(chr);
1872 return 1;
1873 }
1874 return 0;
1875}
1876
1877static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1878{
1879 WinCharState *s = chr->opaque;
1880 OVERLAPPED ov;
1881 int ret;
1882 DWORD size;
1883 char openname[256];
1884
1885 s->fpipe = TRUE;
1886
1887 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1888 if (!s->hsend) {
1889 fprintf(stderr, "Failed CreateEvent\n");
1890 goto fail;
1891 }
1892 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1893 if (!s->hrecv) {
1894 fprintf(stderr, "Failed CreateEvent\n");
1895 goto fail;
1896 }
1897
1898 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1899 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1900 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1901 PIPE_WAIT,
1902 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1903 if (s->hcom == INVALID_HANDLE_VALUE) {
1904 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1905 s->hcom = NULL;
1906 goto fail;
1907 }
1908
1909 ZeroMemory(&ov, sizeof(ov));
1910 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1911 ret = ConnectNamedPipe(s->hcom, &ov);
1912 if (ret) {
1913 fprintf(stderr, "Failed ConnectNamedPipe\n");
1914 goto fail;
1915 }
1916
1917 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1918 if (!ret) {
1919 fprintf(stderr, "Failed GetOverlappedResult\n");
1920 if (ov.hEvent) {
1921 CloseHandle(ov.hEvent);
1922 ov.hEvent = NULL;
1923 }
1924 goto fail;
1925 }
1926
1927 if (ov.hEvent) {
1928 CloseHandle(ov.hEvent);
1929 ov.hEvent = NULL;
1930 }
1931 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1932 return 0;
1933
1934 fail:
1935 win_chr_close(chr);
1936 return -1;
1937}
1938
1939
Markus Armbruster1f514702012-02-07 15:09:08 +01001940static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001941{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001942 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001943 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 chr->opaque = s;
1949 chr->chr_write = win_chr_write;
1950 chr->chr_close = win_chr_close;
1951
1952 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001953 g_free(s);
1954 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001955 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001956 }
Amit Shah127338e2009-11-03 19:59:56 +05301957 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001958 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001959}
1960
Markus Armbruster1f514702012-02-07 15:09:08 +01001961static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001962{
1963 CharDriverState *chr;
1964 WinCharState *s;
1965
Anthony Liguori7267c092011-08-20 22:09:37 -05001966 chr = g_malloc0(sizeof(CharDriverState));
1967 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001968 s->hcom = fd_out;
1969 chr->opaque = s;
1970 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301971 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001972 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001973}
1974
Markus Armbruster1f514702012-02-07 15:09:08 +01001975static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001976{
Markus Armbruster1f514702012-02-07 15:09:08 +01001977 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001978}
1979
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001980static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1981{
1982 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1983 DWORD dwSize;
1984 int len1;
1985
1986 len1 = len;
1987
1988 while (len1 > 0) {
1989 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1990 break;
1991 }
1992 buf += dwSize;
1993 len1 -= dwSize;
1994 }
1995
1996 return len - len1;
1997}
1998
1999static void win_stdio_wait_func(void *opaque)
2000{
2001 CharDriverState *chr = opaque;
2002 WinStdioCharState *stdio = chr->opaque;
2003 INPUT_RECORD buf[4];
2004 int ret;
2005 DWORD dwSize;
2006 int i;
2007
2008 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2009 &dwSize);
2010
2011 if (!ret) {
2012 /* Avoid error storm */
2013 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2014 return;
2015 }
2016
2017 for (i = 0; i < dwSize; i++) {
2018 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2019
2020 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2021 int j;
2022 if (kev->uChar.AsciiChar != 0) {
2023 for (j = 0; j < kev->wRepeatCount; j++) {
2024 if (qemu_chr_be_can_write(chr)) {
2025 uint8_t c = kev->uChar.AsciiChar;
2026 qemu_chr_be_write(chr, &c, 1);
2027 }
2028 }
2029 }
2030 }
2031 }
2032}
2033
2034static DWORD WINAPI win_stdio_thread(LPVOID param)
2035{
2036 CharDriverState *chr = param;
2037 WinStdioCharState *stdio = chr->opaque;
2038 int ret;
2039 DWORD dwSize;
2040
2041 while (1) {
2042
2043 /* Wait for one byte */
2044 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2045
2046 /* Exit in case of error, continue if nothing read */
2047 if (!ret) {
2048 break;
2049 }
2050 if (!dwSize) {
2051 continue;
2052 }
2053
2054 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2055 if (stdio->win_stdio_buf == '\r') {
2056 continue;
2057 }
2058
2059 /* Signal the main thread and wait until the byte was eaten */
2060 if (!SetEvent(stdio->hInputReadyEvent)) {
2061 break;
2062 }
2063 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2064 != WAIT_OBJECT_0) {
2065 break;
2066 }
2067 }
2068
2069 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2070 return 0;
2071}
2072
2073static void win_stdio_thread_wait_func(void *opaque)
2074{
2075 CharDriverState *chr = opaque;
2076 WinStdioCharState *stdio = chr->opaque;
2077
2078 if (qemu_chr_be_can_write(chr)) {
2079 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2080 }
2081
2082 SetEvent(stdio->hInputDoneEvent);
2083}
2084
2085static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2086{
2087 WinStdioCharState *stdio = chr->opaque;
2088 DWORD dwMode = 0;
2089
2090 GetConsoleMode(stdio->hStdIn, &dwMode);
2091
2092 if (echo) {
2093 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2094 } else {
2095 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2096 }
2097}
2098
2099static void win_stdio_close(CharDriverState *chr)
2100{
2101 WinStdioCharState *stdio = chr->opaque;
2102
2103 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2104 CloseHandle(stdio->hInputReadyEvent);
2105 }
2106 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2107 CloseHandle(stdio->hInputDoneEvent);
2108 }
2109 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2110 TerminateThread(stdio->hInputThread, 0);
2111 }
2112
2113 g_free(chr->opaque);
2114 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002115}
2116
Markus Armbruster1f514702012-02-07 15:09:08 +01002117static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002118{
2119 CharDriverState *chr;
2120 WinStdioCharState *stdio;
2121 DWORD dwMode;
2122 int is_console = 0;
2123
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002124 chr = g_malloc0(sizeof(CharDriverState));
2125 stdio = g_malloc0(sizeof(WinStdioCharState));
2126
2127 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2128 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2129 fprintf(stderr, "cannot open stdio: invalid handle\n");
2130 exit(1);
2131 }
2132
2133 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2134
2135 chr->opaque = stdio;
2136 chr->chr_write = win_stdio_write;
2137 chr->chr_close = win_stdio_close;
2138
Anthony Liguoried7a1542013-03-05 23:21:17 +05302139 if (is_console) {
2140 if (qemu_add_wait_object(stdio->hStdIn,
2141 win_stdio_wait_func, chr)) {
2142 fprintf(stderr, "qemu_add_wait_object: failed\n");
2143 }
2144 } else {
2145 DWORD dwId;
2146
2147 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2148 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2149 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2150 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002151
Anthony Liguoried7a1542013-03-05 23:21:17 +05302152 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2153 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2154 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2155 fprintf(stderr, "cannot create stdio thread or event\n");
2156 exit(1);
2157 }
2158 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2159 win_stdio_thread_wait_func, chr)) {
2160 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002161 }
2162 }
2163
2164 dwMode |= ENABLE_LINE_INPUT;
2165
Anthony Liguoried7a1542013-03-05 23:21:17 +05302166 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002167 /* set the terminal in raw mode */
2168 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2169 dwMode |= ENABLE_PROCESSED_INPUT;
2170 }
2171
2172 SetConsoleMode(stdio->hStdIn, dwMode);
2173
2174 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2175 qemu_chr_fe_set_echo(chr, false);
2176
Markus Armbruster1f514702012-02-07 15:09:08 +01002177 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002178}
aliguori6f97dba2008-10-31 18:49:55 +00002179#endif /* !_WIN32 */
2180
Anthony Liguori5ab82112013-03-05 23:21:31 +05302181
aliguori6f97dba2008-10-31 18:49:55 +00002182/***********************************************************/
2183/* UDP Net console */
2184
2185typedef struct {
2186 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302187 GIOChannel *chan;
2188 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302189 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002190 int bufcnt;
2191 int bufptr;
2192 int max_size;
2193} NetCharDriver;
2194
2195static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2196{
2197 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302198 gsize bytes_written;
2199 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002200
Anthony Liguori76a96442013-03-05 23:21:21 +05302201 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2202 if (status == G_IO_STATUS_EOF) {
2203 return 0;
2204 } else if (status != G_IO_STATUS_NORMAL) {
2205 return -1;
2206 }
2207
2208 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002209}
2210
2211static int udp_chr_read_poll(void *opaque)
2212{
2213 CharDriverState *chr = opaque;
2214 NetCharDriver *s = chr->opaque;
2215
Anthony Liguori909cda12011-08-15 11:17:31 -05002216 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002217
2218 /* If there were any stray characters in the queue process them
2219 * first
2220 */
2221 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002222 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002223 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002224 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002225 }
2226 return s->max_size;
2227}
2228
Anthony Liguori76a96442013-03-05 23:21:21 +05302229static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002230{
2231 CharDriverState *chr = opaque;
2232 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302233 gsize bytes_read = 0;
2234 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002235
2236 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302237 return FALSE;
2238 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2239 &bytes_read, NULL);
2240 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002241 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302242 if (status != G_IO_STATUS_NORMAL) {
2243 return FALSE;
2244 }
aliguori6f97dba2008-10-31 18:49:55 +00002245
2246 s->bufptr = 0;
2247 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002248 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002249 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002250 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002251 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302252
2253 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002254}
2255
2256static void udp_chr_update_read_handler(CharDriverState *chr)
2257{
2258 NetCharDriver *s = chr->opaque;
2259
Anthony Liguori76a96442013-03-05 23:21:21 +05302260 if (s->tag) {
2261 g_source_remove(s->tag);
2262 s->tag = 0;
2263 }
2264
2265 if (s->chan) {
2266 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002267 }
2268}
2269
aliguori819f56b2009-03-28 17:58:14 +00002270static void udp_chr_close(CharDriverState *chr)
2271{
2272 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302273 if (s->tag) {
2274 g_source_remove(s->tag);
2275 }
2276 if (s->chan) {
2277 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002278 closesocket(s->fd);
2279 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002280 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002281 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002282}
2283
Markus Armbruster1f514702012-02-07 15:09:08 +01002284static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002285{
2286 CharDriverState *chr = NULL;
2287 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002288 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002289 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002290
Anthony Liguori7267c092011-08-20 22:09:37 -05002291 chr = g_malloc0(sizeof(CharDriverState));
2292 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002293
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002294 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002295 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002296 goto return_err;
2297 }
2298
2299 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302300 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002301 s->bufcnt = 0;
2302 s->bufptr = 0;
2303 chr->opaque = s;
2304 chr->chr_write = udp_chr_write;
2305 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002306 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002307 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002308
2309return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002310 if (local_err) {
2311 qerror_report_err(local_err);
2312 error_free(local_err);
2313 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002314 g_free(chr);
2315 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002316 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002317 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002318 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002319 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002320}
2321
2322/***********************************************************/
2323/* TCP Net console */
2324
2325typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302326
2327 GIOChannel *chan, *listen_chan;
2328 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002329 int fd, listen_fd;
2330 int connected;
2331 int max_size;
2332 int do_telnetopt;
2333 int do_nodelay;
2334 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002335 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002336} TCPCharDriver;
2337
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302338static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002339
2340static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2341{
2342 TCPCharDriver *s = chr->opaque;
2343 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302344 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002345 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002346 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002347 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002348 }
2349}
2350
2351static int tcp_chr_read_poll(void *opaque)
2352{
2353 CharDriverState *chr = opaque;
2354 TCPCharDriver *s = chr->opaque;
2355 if (!s->connected)
2356 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002357 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002358 return s->max_size;
2359}
2360
2361#define IAC 255
2362#define IAC_BREAK 243
2363static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2364 TCPCharDriver *s,
2365 uint8_t *buf, int *size)
2366{
2367 /* Handle any telnet client's basic IAC options to satisfy char by
2368 * char mode with no echo. All IAC options will be removed from
2369 * the buf and the do_telnetopt variable will be used to track the
2370 * state of the width of the IAC information.
2371 *
2372 * IAC commands come in sets of 3 bytes with the exception of the
2373 * "IAC BREAK" command and the double IAC.
2374 */
2375
2376 int i;
2377 int j = 0;
2378
2379 for (i = 0; i < *size; i++) {
2380 if (s->do_telnetopt > 1) {
2381 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2382 /* Double IAC means send an IAC */
2383 if (j != i)
2384 buf[j] = buf[i];
2385 j++;
2386 s->do_telnetopt = 1;
2387 } else {
2388 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2389 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002390 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002391 s->do_telnetopt++;
2392 }
2393 s->do_telnetopt++;
2394 }
2395 if (s->do_telnetopt >= 4) {
2396 s->do_telnetopt = 1;
2397 }
2398 } else {
2399 if ((unsigned char)buf[i] == IAC) {
2400 s->do_telnetopt = 2;
2401 } else {
2402 if (j != i)
2403 buf[j] = buf[i];
2404 j++;
2405 }
2406 }
2407 }
2408 *size = j;
2409}
2410
Mark McLoughlin7d174052009-07-22 09:11:39 +01002411static int tcp_get_msgfd(CharDriverState *chr)
2412{
2413 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002414 int fd = s->msgfd;
2415 s->msgfd = -1;
2416 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002417}
2418
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002419#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002420static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2421{
2422 TCPCharDriver *s = chr->opaque;
2423 struct cmsghdr *cmsg;
2424
2425 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2426 int fd;
2427
2428 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2429 cmsg->cmsg_level != SOL_SOCKET ||
2430 cmsg->cmsg_type != SCM_RIGHTS)
2431 continue;
2432
2433 fd = *((int *)CMSG_DATA(cmsg));
2434 if (fd < 0)
2435 continue;
2436
Corey Bryant06138652012-08-14 16:43:42 -04002437#ifndef MSG_CMSG_CLOEXEC
2438 qemu_set_cloexec(fd);
2439#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002440 if (s->msgfd != -1)
2441 close(s->msgfd);
2442 s->msgfd = fd;
2443 }
2444}
2445
Mark McLoughlin9977c892009-07-22 09:11:38 +01002446static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2447{
2448 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002449 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002450 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002451 union {
2452 struct cmsghdr cmsg;
2453 char control[CMSG_SPACE(sizeof(int))];
2454 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002455 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002456 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002457
2458 iov[0].iov_base = buf;
2459 iov[0].iov_len = len;
2460
2461 msg.msg_iov = iov;
2462 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002463 msg.msg_control = &msg_control;
2464 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002465
Corey Bryant06138652012-08-14 16:43:42 -04002466#ifdef MSG_CMSG_CLOEXEC
2467 flags |= MSG_CMSG_CLOEXEC;
2468#endif
2469 ret = recvmsg(s->fd, &msg, flags);
2470 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002471 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002472 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002473
2474 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002475}
2476#else
2477static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2478{
2479 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002480 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002481}
2482#endif
2483
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302484static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2485{
2486 TCPCharDriver *s = chr->opaque;
2487 return g_io_create_watch(s->chan, cond);
2488}
2489
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302490static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002491{
2492 CharDriverState *chr = opaque;
2493 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302494 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002495 int len, size;
2496
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302497 if (!s->connected || s->max_size <= 0) {
2498 return FALSE;
2499 }
aliguori6f97dba2008-10-31 18:49:55 +00002500 len = sizeof(buf);
2501 if (len > s->max_size)
2502 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002503 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002504 if (size == 0) {
2505 /* connection closed */
2506 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302507 if (s->listen_chan) {
2508 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002509 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302510 g_source_remove(s->tag);
2511 s->tag = 0;
2512 g_io_channel_unref(s->chan);
2513 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002514 closesocket(s->fd);
2515 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002516 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002517 } else if (size > 0) {
2518 if (s->do_telnetopt)
2519 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2520 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002521 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002522 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302523
2524 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002525}
2526
Blue Swirl68c18d12010-08-15 09:46:24 +00002527#ifndef _WIN32
2528CharDriverState *qemu_chr_open_eventfd(int eventfd)
2529{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002530 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002531}
Blue Swirl68c18d12010-08-15 09:46:24 +00002532#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002533
aliguori6f97dba2008-10-31 18:49:55 +00002534static void tcp_chr_connect(void *opaque)
2535{
2536 CharDriverState *chr = opaque;
2537 TCPCharDriver *s = chr->opaque;
2538
2539 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302540 if (s->chan) {
2541 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002542 }
Amit Shah127338e2009-11-03 19:59:56 +05302543 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002544}
2545
2546#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2547static void tcp_chr_telnet_init(int fd)
2548{
2549 char buf[3];
2550 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2551 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2552 send(fd, (char *)buf, 3, 0);
2553 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2554 send(fd, (char *)buf, 3, 0);
2555 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2556 send(fd, (char *)buf, 3, 0);
2557 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2558 send(fd, (char *)buf, 3, 0);
2559}
2560
Daniel P. Berrange13661082011-06-23 13:31:42 +01002561static int tcp_chr_add_client(CharDriverState *chr, int fd)
2562{
2563 TCPCharDriver *s = chr->opaque;
2564 if (s->fd != -1)
2565 return -1;
2566
2567 socket_set_nonblock(fd);
2568 if (s->do_nodelay)
2569 socket_set_nodelay(fd);
2570 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302571 s->chan = io_channel_from_socket(fd);
2572 g_source_remove(s->listen_tag);
2573 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002574 tcp_chr_connect(chr);
2575
2576 return 0;
2577}
2578
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302579static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002580{
2581 CharDriverState *chr = opaque;
2582 TCPCharDriver *s = chr->opaque;
2583 struct sockaddr_in saddr;
2584#ifndef _WIN32
2585 struct sockaddr_un uaddr;
2586#endif
2587 struct sockaddr *addr;
2588 socklen_t len;
2589 int fd;
2590
2591 for(;;) {
2592#ifndef _WIN32
2593 if (s->is_unix) {
2594 len = sizeof(uaddr);
2595 addr = (struct sockaddr *)&uaddr;
2596 } else
2597#endif
2598 {
2599 len = sizeof(saddr);
2600 addr = (struct sockaddr *)&saddr;
2601 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002602 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002603 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302604 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002605 } else if (fd >= 0) {
2606 if (s->do_telnetopt)
2607 tcp_chr_telnet_init(fd);
2608 break;
2609 }
2610 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002611 if (tcp_chr_add_client(chr, fd) < 0)
2612 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302613
2614 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002615}
2616
2617static void tcp_chr_close(CharDriverState *chr)
2618{
2619 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002620 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302621 if (s->tag) {
2622 g_source_remove(s->tag);
2623 }
2624 if (s->chan) {
2625 g_io_channel_unref(s->chan);
2626 }
aliguori6f97dba2008-10-31 18:49:55 +00002627 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002628 }
2629 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302630 if (s->listen_tag) {
2631 g_source_remove(s->listen_tag);
2632 }
2633 if (s->listen_chan) {
2634 g_io_channel_unref(s->listen_chan);
2635 }
aliguori6f97dba2008-10-31 18:49:55 +00002636 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002637 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002638 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002639 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002640}
2641
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002642static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2643 bool is_listen, bool is_telnet,
2644 bool is_waitconnect,
2645 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002646{
2647 CharDriverState *chr = NULL;
2648 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002649 char host[NI_MAXHOST], serv[NI_MAXSERV];
2650 const char *left = "", *right = "";
2651 struct sockaddr_storage ss;
2652 socklen_t ss_len = sizeof(ss);
2653
2654 memset(&ss, 0, ss_len);
2655 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2656 error_setg(errp, "getsockname: %s", strerror(errno));
2657 return NULL;
2658 }
2659
2660 chr = g_malloc0(sizeof(CharDriverState));
2661 s = g_malloc0(sizeof(TCPCharDriver));
2662
2663 s->connected = 0;
2664 s->fd = -1;
2665 s->listen_fd = -1;
2666 s->msgfd = -1;
2667
2668 chr->filename = g_malloc(256);
2669 switch (ss.ss_family) {
2670#ifndef _WIN32
2671 case AF_UNIX:
2672 s->is_unix = 1;
2673 snprintf(chr->filename, 256, "unix:%s%s",
2674 ((struct sockaddr_un *)(&ss))->sun_path,
2675 is_listen ? ",server" : "");
2676 break;
2677#endif
2678 case AF_INET6:
2679 left = "[";
2680 right = "]";
2681 /* fall through */
2682 case AF_INET:
2683 s->do_nodelay = do_nodelay;
2684 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2685 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2686 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2687 is_telnet ? "telnet" : "tcp",
2688 left, host, right, serv,
2689 is_listen ? ",server" : "");
2690 break;
2691 }
2692
2693 chr->opaque = s;
2694 chr->chr_write = tcp_chr_write;
2695 chr->chr_close = tcp_chr_close;
2696 chr->get_msgfd = tcp_get_msgfd;
2697 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302698 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002699
2700 if (is_listen) {
2701 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302702 s->listen_chan = io_channel_from_socket(s->listen_fd);
2703 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002704 if (is_telnet) {
2705 s->do_telnetopt = 1;
2706 }
2707 } else {
2708 s->connected = 1;
2709 s->fd = fd;
2710 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302711 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002712 tcp_chr_connect(chr);
2713 }
2714
2715 if (is_listen && is_waitconnect) {
2716 printf("QEMU waiting for connection on: %s\n",
2717 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302718 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002719 socket_set_nonblock(s->listen_fd);
2720 }
2721 return chr;
2722}
2723
2724static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2725{
2726 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002727 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002728 int fd = -1;
2729 int is_listen;
2730 int is_waitconnect;
2731 int do_nodelay;
2732 int is_unix;
2733 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002734
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002735 is_listen = qemu_opt_get_bool(opts, "server", 0);
2736 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2737 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2738 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2739 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002740 if (!is_listen)
2741 is_waitconnect = 0;
2742
aliguorif07b6002008-11-11 20:54:09 +00002743 if (is_unix) {
2744 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002745 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002746 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002747 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002748 }
2749 } else {
2750 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002751 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002752 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002753 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002754 }
2755 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002756 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002757 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002758 }
aliguori6f97dba2008-10-31 18:49:55 +00002759
2760 if (!is_waitconnect)
2761 socket_set_nonblock(fd);
2762
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002763 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2764 is_waitconnect, &local_err);
2765 if (error_is_set(&local_err)) {
2766 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002767 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002768 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002769
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002770
aliguori6f97dba2008-10-31 18:49:55 +00002771 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002772 if (local_err) {
2773 qerror_report_err(local_err);
2774 error_free(local_err);
2775 }
2776 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002777 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002778 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002779 if (chr) {
2780 g_free(chr->opaque);
2781 g_free(chr);
2782 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002783 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002784}
2785
Luiz Capitulino999bd672010-10-22 16:09:05 -02002786/***********************************************************/
2787/* Memory chardev */
2788typedef struct {
2789 size_t outbuf_size;
2790 size_t outbuf_capacity;
2791 uint8_t *outbuf;
2792} MemoryDriver;
2793
2794static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2795{
2796 MemoryDriver *d = chr->opaque;
2797
2798 /* TODO: the QString implementation has the same code, we should
2799 * introduce a generic way to do this in cutils.c */
2800 if (d->outbuf_capacity < d->outbuf_size + len) {
2801 /* grow outbuf */
2802 d->outbuf_capacity += len;
2803 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002804 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002805 }
2806
2807 memcpy(d->outbuf + d->outbuf_size, buf, len);
2808 d->outbuf_size += len;
2809
2810 return len;
2811}
2812
2813void qemu_chr_init_mem(CharDriverState *chr)
2814{
2815 MemoryDriver *d;
2816
Anthony Liguori7267c092011-08-20 22:09:37 -05002817 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002818 d->outbuf_size = 0;
2819 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002820 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002821
2822 memset(chr, 0, sizeof(*chr));
2823 chr->opaque = d;
2824 chr->chr_write = mem_chr_write;
2825}
2826
2827QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2828{
2829 MemoryDriver *d = chr->opaque;
2830 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2831}
2832
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002833/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002834void qemu_chr_close_mem(CharDriverState *chr)
2835{
2836 MemoryDriver *d = chr->opaque;
2837
Anthony Liguori7267c092011-08-20 22:09:37 -05002838 g_free(d->outbuf);
2839 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002840 chr->opaque = NULL;
2841 chr->chr_write = NULL;
2842}
2843
2844size_t qemu_chr_mem_osize(const CharDriverState *chr)
2845{
2846 const MemoryDriver *d = chr->opaque;
2847 return d->outbuf_size;
2848}
2849
Lei Li51767e72013-01-25 00:03:19 +08002850/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002851/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002852
2853typedef struct {
2854 size_t size;
2855 size_t prod;
2856 size_t cons;
2857 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002858} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002859
Markus Armbruster3949e592013-02-06 21:27:24 +01002860static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002861{
Markus Armbruster3949e592013-02-06 21:27:24 +01002862 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002863
Markus Armbruster5c230102013-02-06 21:27:23 +01002864 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002865}
2866
Markus Armbruster3949e592013-02-06 21:27:24 +01002867static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002868{
Markus Armbruster3949e592013-02-06 21:27:24 +01002869 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002870 int i;
2871
2872 if (!buf || (len < 0)) {
2873 return -1;
2874 }
2875
2876 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002877 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2878 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002879 d->cons = d->prod - d->size;
2880 }
2881 }
2882
2883 return 0;
2884}
2885
Markus Armbruster3949e592013-02-06 21:27:24 +01002886static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002887{
Markus Armbruster3949e592013-02-06 21:27:24 +01002888 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002889 int i;
2890
Markus Armbruster5c230102013-02-06 21:27:23 +01002891 for (i = 0; i < len && d->cons != d->prod; i++) {
2892 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002893 }
2894
2895 return i;
2896}
2897
Markus Armbruster3949e592013-02-06 21:27:24 +01002898static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002899{
Markus Armbruster3949e592013-02-06 21:27:24 +01002900 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002901
2902 g_free(d->cbuf);
2903 g_free(d);
2904 chr->opaque = NULL;
2905}
2906
Markus Armbruster3949e592013-02-06 21:27:24 +01002907static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002908{
2909 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002910 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002911
2912 chr = g_malloc0(sizeof(CharDriverState));
2913 d = g_malloc(sizeof(*d));
2914
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002915 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002916 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002917 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002918 }
2919
2920 /* The size must be power of 2 */
2921 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002922 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002923 goto fail;
2924 }
2925
2926 d->prod = 0;
2927 d->cons = 0;
2928 d->cbuf = g_malloc0(d->size);
2929
2930 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002931 chr->chr_write = ringbuf_chr_write;
2932 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002933
2934 return chr;
2935
2936fail:
2937 g_free(d);
2938 g_free(chr);
2939 return NULL;
2940}
2941
Markus Armbruster3949e592013-02-06 21:27:24 +01002942static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002943{
Markus Armbruster3949e592013-02-06 21:27:24 +01002944 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002945}
2946
Markus Armbruster3949e592013-02-06 21:27:24 +01002947void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002948 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002949 Error **errp)
2950{
2951 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002952 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002953 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002954 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002955
2956 chr = qemu_chr_find(device);
2957 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002958 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002959 return;
2960 }
2961
Markus Armbruster3949e592013-02-06 21:27:24 +01002962 if (!chr_is_ringbuf(chr)) {
2963 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002964 return;
2965 }
2966
Lei Li1f590cf2013-01-25 00:03:20 +08002967 if (has_format && (format == DATA_FORMAT_BASE64)) {
2968 write_data = g_base64_decode(data, &write_count);
2969 } else {
2970 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002971 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002972 }
2973
Markus Armbruster3949e592013-02-06 21:27:24 +01002974 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002975
Markus Armbruster13289fb2013-02-06 21:27:18 +01002976 if (write_data != (uint8_t *)data) {
2977 g_free((void *)write_data);
2978 }
2979
Lei Li1f590cf2013-01-25 00:03:20 +08002980 if (ret < 0) {
2981 error_setg(errp, "Failed to write to device %s", device);
2982 return;
2983 }
2984}
2985
Markus Armbruster3949e592013-02-06 21:27:24 +01002986char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002987 bool has_format, enum DataFormat format,
2988 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002989{
2990 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002991 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002992 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002993 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002994
2995 chr = qemu_chr_find(device);
2996 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002997 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002998 return NULL;
2999 }
3000
Markus Armbruster3949e592013-02-06 21:27:24 +01003001 if (!chr_is_ringbuf(chr)) {
3002 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003003 return NULL;
3004 }
3005
3006 if (size <= 0) {
3007 error_setg(errp, "size must be greater than zero");
3008 return NULL;
3009 }
3010
Markus Armbruster3949e592013-02-06 21:27:24 +01003011 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003012 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003013 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003014
Markus Armbruster3949e592013-02-06 21:27:24 +01003015 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003016
3017 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003018 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003019 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003020 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003021 /*
3022 * FIXME should read only complete, valid UTF-8 characters up
3023 * to @size bytes. Invalid sequences should be replaced by a
3024 * suitable replacement character. Except when (and only
3025 * when) ring buffer lost characters since last read, initial
3026 * continuation characters should be dropped.
3027 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003028 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003029 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003030 }
3031
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003032 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003033}
3034
Gerd Hoffmann33521632009-12-08 13:11:49 +01003035QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003036{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003037 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003038 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003039 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003040 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003041 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003042
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003043 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3044 if (error_is_set(&local_err)) {
3045 qerror_report_err(local_err);
3046 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003047 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003048 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003049
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003050 if (strstart(filename, "mon:", &p)) {
3051 filename = p;
3052 qemu_opt_set(opts, "mux", "on");
3053 }
3054
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003055 if (strcmp(filename, "null") == 0 ||
3056 strcmp(filename, "pty") == 0 ||
3057 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003058 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003059 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003060 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003061 return opts;
3062 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003063 if (strstart(filename, "vc", &p)) {
3064 qemu_opt_set(opts, "backend", "vc");
3065 if (*p == ':') {
3066 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3067 /* pixels */
3068 qemu_opt_set(opts, "width", width);
3069 qemu_opt_set(opts, "height", height);
3070 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3071 /* chars */
3072 qemu_opt_set(opts, "cols", width);
3073 qemu_opt_set(opts, "rows", height);
3074 } else {
3075 goto fail;
3076 }
3077 }
3078 return opts;
3079 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003080 if (strcmp(filename, "con:") == 0) {
3081 qemu_opt_set(opts, "backend", "console");
3082 return opts;
3083 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003084 if (strstart(filename, "COM", NULL)) {
3085 qemu_opt_set(opts, "backend", "serial");
3086 qemu_opt_set(opts, "path", filename);
3087 return opts;
3088 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003089 if (strstart(filename, "file:", &p)) {
3090 qemu_opt_set(opts, "backend", "file");
3091 qemu_opt_set(opts, "path", p);
3092 return opts;
3093 }
3094 if (strstart(filename, "pipe:", &p)) {
3095 qemu_opt_set(opts, "backend", "pipe");
3096 qemu_opt_set(opts, "path", p);
3097 return opts;
3098 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003099 if (strstart(filename, "tcp:", &p) ||
3100 strstart(filename, "telnet:", &p)) {
3101 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3102 host[0] = 0;
3103 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3104 goto fail;
3105 }
3106 qemu_opt_set(opts, "backend", "socket");
3107 qemu_opt_set(opts, "host", host);
3108 qemu_opt_set(opts, "port", port);
3109 if (p[pos] == ',') {
3110 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3111 goto fail;
3112 }
3113 if (strstart(filename, "telnet:", &p))
3114 qemu_opt_set(opts, "telnet", "on");
3115 return opts;
3116 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003117 if (strstart(filename, "udp:", &p)) {
3118 qemu_opt_set(opts, "backend", "udp");
3119 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3120 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003121 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003122 goto fail;
3123 }
3124 }
3125 qemu_opt_set(opts, "host", host);
3126 qemu_opt_set(opts, "port", port);
3127 if (p[pos] == '@') {
3128 p += pos + 1;
3129 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3130 host[0] = 0;
3131 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003132 goto fail;
3133 }
3134 }
3135 qemu_opt_set(opts, "localaddr", host);
3136 qemu_opt_set(opts, "localport", port);
3137 }
3138 return opts;
3139 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003140 if (strstart(filename, "unix:", &p)) {
3141 qemu_opt_set(opts, "backend", "socket");
3142 if (qemu_opts_do_parse(opts, p, "path") != 0)
3143 goto fail;
3144 return opts;
3145 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003146 if (strstart(filename, "/dev/parport", NULL) ||
3147 strstart(filename, "/dev/ppi", NULL)) {
3148 qemu_opt_set(opts, "backend", "parport");
3149 qemu_opt_set(opts, "path", filename);
3150 return opts;
3151 }
3152 if (strstart(filename, "/dev/", NULL)) {
3153 qemu_opt_set(opts, "backend", "tty");
3154 qemu_opt_set(opts, "path", filename);
3155 return opts;
3156 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003157
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003158fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003159 qemu_opts_del(opts);
3160 return NULL;
3161}
3162
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003163#ifdef HAVE_CHARDEV_PARPORT
3164
3165static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3166{
3167 const char *filename = qemu_opt_get(opts, "path");
3168 int fd;
3169
3170 fd = qemu_open(filename, O_RDWR);
3171 if (fd < 0) {
3172 return NULL;
3173 }
3174 return qemu_chr_open_pp_fd(fd);
3175}
3176
3177#endif
3178
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003179static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3180 Error **errp)
3181{
3182 const char *path = qemu_opt_get(opts, "path");
3183
3184 if (path == NULL) {
3185 error_setg(errp, "chardev: file: no filename given");
3186 return;
3187 }
3188 backend->file = g_new0(ChardevFile, 1);
3189 backend->file->out = g_strdup(path);
3190}
3191
Anthony Liguorid654f342013-03-05 23:21:28 +05303192typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003193 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003194 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003195 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003196 /* new, qapi-based */
3197 int kind;
3198 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303199} CharDriver;
3200
3201static GSList *backends;
3202
3203void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3204{
3205 CharDriver *s;
3206
3207 s = g_malloc0(sizeof(*s));
3208 s->name = g_strdup(name);
3209 s->open = open;
3210
3211 backends = g_slist_append(backends, s);
3212}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003213
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003214void register_char_driver_qapi(const char *name, int kind,
3215 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3216{
3217 CharDriver *s;
3218
3219 s = g_malloc0(sizeof(*s));
3220 s->name = g_strdup(name);
3221 s->kind = kind;
3222 s->parse = parse;
3223
3224 backends = g_slist_append(backends, s);
3225}
3226
Anthony Liguorif69554b2011-08-15 11:17:37 -05003227CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003228 void (*init)(struct CharDriverState *s),
3229 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230{
Anthony Liguorid654f342013-03-05 23:21:28 +05303231 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003232 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303233 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003234
3235 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003236 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003237 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003238 }
3239
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003240 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003241 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003242 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003243 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003244 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303245 for (i = backends; i; i = i->next) {
3246 cd = i->data;
3247
3248 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003249 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303250 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003251 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303252 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003253 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003254 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303255 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003256 }
3257
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003258 if (!cd->open) {
3259 /* using new, qapi init */
3260 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3261 ChardevReturn *ret = NULL;
3262 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003263 const char *bid = NULL;
3264
3265 if (qemu_opt_get_bool(opts, "mux", 0)) {
3266 bid = g_strdup_printf("%s-base", id);
3267 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003268
3269 chr = NULL;
3270 backend->kind = cd->kind;
3271 if (cd->parse) {
3272 cd->parse(opts, backend, errp);
3273 if (error_is_set(errp)) {
3274 goto qapi_out;
3275 }
3276 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003277 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003278 if (error_is_set(errp)) {
3279 goto qapi_out;
3280 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003281
3282 if (bid) {
3283 qapi_free_ChardevBackend(backend);
3284 qapi_free_ChardevReturn(ret);
3285 backend = g_new0(ChardevBackend, 1);
3286 backend->mux = g_new0(ChardevMux, 1);
3287 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3288 backend->mux->chardev = g_strdup(bid);
3289 ret = qmp_chardev_add(id, backend, errp);
3290 if (error_is_set(errp)) {
3291 goto qapi_out;
3292 }
3293 }
3294
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003295 chr = qemu_chr_find(id);
3296
3297 qapi_out:
3298 qapi_free_ChardevBackend(backend);
3299 qapi_free_ChardevReturn(ret);
3300 return chr;
3301 }
3302
Anthony Liguorid654f342013-03-05 23:21:28 +05303303 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003304 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003305 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003306 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003307 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003308 }
3309
3310 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003311 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003312 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003313 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003314
3315 if (qemu_opt_get_bool(opts, "mux", 0)) {
3316 CharDriverState *base = chr;
3317 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003318 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003319 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3320 chr = qemu_chr_open_mux(base);
3321 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003322 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003323 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003324 } else {
3325 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003326 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003327 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003328 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003329 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003330
3331err:
3332 qemu_opts_del(opts);
3333 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003334}
3335
Anthony Liguori27143a42011-08-15 11:17:36 -05003336CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003337{
3338 const char *p;
3339 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003340 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003341 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003342
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003343 if (strstart(filename, "chardev:", &p)) {
3344 return qemu_chr_find(p);
3345 }
3346
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003347 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003348 if (!opts)
3349 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003350
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003351 chr = qemu_chr_new_from_opts(opts, init, &err);
3352 if (error_is_set(&err)) {
3353 fprintf(stderr, "%s\n", error_get_pretty(err));
3354 error_free(err);
3355 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003356 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3357 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003358 }
3359 return chr;
3360}
3361
Anthony Liguori15f31512011-08-15 11:17:35 -05003362void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003363{
3364 if (chr->chr_set_echo) {
3365 chr->chr_set_echo(chr, echo);
3366 }
3367}
3368
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003369void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003370{
3371 if (chr->chr_guest_open) {
3372 chr->chr_guest_open(chr);
3373 }
3374}
3375
Anthony Liguori28178222011-08-15 11:17:33 -05003376void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003377{
3378 if (chr->chr_guest_close) {
3379 chr->chr_guest_close(chr);
3380 }
3381}
3382
Anthony Liguori23673ca2013-03-05 23:21:23 +05303383guint qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3384 GIOFunc func, void *user_data)
3385{
3386 GSource *src;
3387 guint tag;
3388
3389 if (s->chr_add_watch == NULL) {
3390 return -ENOSYS;
3391 }
3392
3393 src = s->chr_add_watch(s, cond);
3394 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3395 tag = g_source_attach(src, NULL);
3396 g_source_unref(src);
3397
3398 return tag;
3399}
3400
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003401void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003402{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003403 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003404 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003405 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003406 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003407 g_free(chr->filename);
3408 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003409 if (chr->opts) {
3410 qemu_opts_del(chr->opts);
3411 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003412 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003413}
3414
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003415ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003416{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003417 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003418 CharDriverState *chr;
3419
Blue Swirl72cf2d42009-09-12 07:36:22 +00003420 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003421 ChardevInfoList *info = g_malloc0(sizeof(*info));
3422 info->value = g_malloc0(sizeof(*info->value));
3423 info->value->label = g_strdup(chr->label);
3424 info->value->filename = g_strdup(chr->filename);
3425
3426 info->next = chr_list;
3427 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003428 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003429
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003430 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003431}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003432
3433CharDriverState *qemu_chr_find(const char *name)
3434{
3435 CharDriverState *chr;
3436
Blue Swirl72cf2d42009-09-12 07:36:22 +00003437 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003438 if (strcmp(chr->label, name) != 0)
3439 continue;
3440 return chr;
3441 }
3442 return NULL;
3443}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003444
3445/* Get a character (serial) device interface. */
3446CharDriverState *qemu_char_get_next_serial(void)
3447{
3448 static int next_serial;
3449
3450 /* FIXME: This function needs to go away: use chardev properties! */
3451 return serial_hds[next_serial++];
3452}
3453
Paolo Bonzini4d454572012-11-26 16:03:42 +01003454QemuOptsList qemu_chardev_opts = {
3455 .name = "chardev",
3456 .implied_opt_name = "backend",
3457 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3458 .desc = {
3459 {
3460 .name = "backend",
3461 .type = QEMU_OPT_STRING,
3462 },{
3463 .name = "path",
3464 .type = QEMU_OPT_STRING,
3465 },{
3466 .name = "host",
3467 .type = QEMU_OPT_STRING,
3468 },{
3469 .name = "port",
3470 .type = QEMU_OPT_STRING,
3471 },{
3472 .name = "localaddr",
3473 .type = QEMU_OPT_STRING,
3474 },{
3475 .name = "localport",
3476 .type = QEMU_OPT_STRING,
3477 },{
3478 .name = "to",
3479 .type = QEMU_OPT_NUMBER,
3480 },{
3481 .name = "ipv4",
3482 .type = QEMU_OPT_BOOL,
3483 },{
3484 .name = "ipv6",
3485 .type = QEMU_OPT_BOOL,
3486 },{
3487 .name = "wait",
3488 .type = QEMU_OPT_BOOL,
3489 },{
3490 .name = "server",
3491 .type = QEMU_OPT_BOOL,
3492 },{
3493 .name = "delay",
3494 .type = QEMU_OPT_BOOL,
3495 },{
3496 .name = "telnet",
3497 .type = QEMU_OPT_BOOL,
3498 },{
3499 .name = "width",
3500 .type = QEMU_OPT_NUMBER,
3501 },{
3502 .name = "height",
3503 .type = QEMU_OPT_NUMBER,
3504 },{
3505 .name = "cols",
3506 .type = QEMU_OPT_NUMBER,
3507 },{
3508 .name = "rows",
3509 .type = QEMU_OPT_NUMBER,
3510 },{
3511 .name = "mux",
3512 .type = QEMU_OPT_BOOL,
3513 },{
3514 .name = "signal",
3515 .type = QEMU_OPT_BOOL,
3516 },{
3517 .name = "name",
3518 .type = QEMU_OPT_STRING,
3519 },{
3520 .name = "debug",
3521 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003522 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003523 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003524 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003525 },
3526 { /* end of list */ }
3527 },
3528};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003529
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003530#ifdef _WIN32
3531
3532static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3533{
3534 HANDLE out;
3535
3536 if (file->in) {
3537 error_setg(errp, "input file not supported");
3538 return NULL;
3539 }
3540
3541 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3542 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3543 if (out == INVALID_HANDLE_VALUE) {
3544 error_setg(errp, "open %s failed", file->out);
3545 return NULL;
3546 }
3547 return qemu_chr_open_win_file(out);
3548}
3549
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003550static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3551 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003552{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003553 return qemu_chr_open_win_path(serial->device);
3554}
3555
3556static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3557 Error **errp)
3558{
3559 error_setg(errp, "character device backend type 'parallel' not supported");
3560 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003561}
3562
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003563#else /* WIN32 */
3564
3565static int qmp_chardev_open_file_source(char *src, int flags,
3566 Error **errp)
3567{
3568 int fd = -1;
3569
3570 TFR(fd = qemu_open(src, flags, 0666));
3571 if (fd == -1) {
3572 error_setg(errp, "open %s: %s", src, strerror(errno));
3573 }
3574 return fd;
3575}
3576
3577static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3578{
3579 int flags, in = -1, out = -1;
3580
3581 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3582 out = qmp_chardev_open_file_source(file->out, flags, errp);
3583 if (error_is_set(errp)) {
3584 return NULL;
3585 }
3586
3587 if (file->in) {
3588 flags = O_RDONLY;
3589 in = qmp_chardev_open_file_source(file->in, flags, errp);
3590 if (error_is_set(errp)) {
3591 qemu_close(out);
3592 return NULL;
3593 }
3594 }
3595
3596 return qemu_chr_open_fd(in, out);
3597}
3598
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003599static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3600 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003601{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003602#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003603 int fd;
3604
3605 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3606 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003607 return NULL;
3608 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003609 socket_set_nonblock(fd);
3610 return qemu_chr_open_tty_fd(fd);
3611#else
3612 error_setg(errp, "character device backend type 'serial' not supported");
3613 return NULL;
3614#endif
3615}
3616
3617static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3618 Error **errp)
3619{
3620#ifdef HAVE_CHARDEV_PARPORT
3621 int fd;
3622
3623 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3624 if (error_is_set(errp)) {
3625 return NULL;
3626 }
3627 return qemu_chr_open_pp_fd(fd);
3628#else
3629 error_setg(errp, "character device backend type 'parallel' not supported");
3630 return NULL;
3631#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003632}
3633
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003634#endif /* WIN32 */
3635
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003636static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3637 Error **errp)
3638{
3639 SocketAddress *addr = sock->addr;
3640 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3641 bool is_listen = sock->has_server ? sock->server : true;
3642 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3643 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3644 int fd;
3645
3646 if (is_listen) {
3647 fd = socket_listen(addr, errp);
3648 } else {
3649 fd = socket_connect(addr, errp, NULL, NULL);
3650 }
3651 if (error_is_set(errp)) {
3652 return NULL;
3653 }
3654 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3655 is_telnet, is_waitconnect, errp);
3656}
3657
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003658ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3659 Error **errp)
3660{
3661 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003662 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003663
3664 chr = qemu_chr_find(id);
3665 if (chr) {
3666 error_setg(errp, "Chardev '%s' already exists", id);
3667 g_free(ret);
3668 return NULL;
3669 }
3670
3671 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003672 case CHARDEV_BACKEND_KIND_FILE:
3673 chr = qmp_chardev_open_file(backend->file, errp);
3674 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003675 case CHARDEV_BACKEND_KIND_SERIAL:
3676 chr = qmp_chardev_open_serial(backend->serial, errp);
3677 break;
3678 case CHARDEV_BACKEND_KIND_PARALLEL:
3679 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003680 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003681 case CHARDEV_BACKEND_KIND_SOCKET:
3682 chr = qmp_chardev_open_socket(backend->socket, errp);
3683 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003684#ifdef HAVE_CHARDEV_TTY
3685 case CHARDEV_BACKEND_KIND_PTY:
3686 {
3687 /* qemu_chr_open_pty sets "path" in opts */
3688 QemuOpts *opts;
3689 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3690 chr = qemu_chr_open_pty(opts);
3691 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3692 ret->has_pty = true;
3693 qemu_opts_del(opts);
3694 break;
3695 }
3696#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003697 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003698 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003699 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003700 case CHARDEV_BACKEND_KIND_MUX:
3701 base = qemu_chr_find(backend->mux->chardev);
3702 if (base == NULL) {
3703 error_setg(errp, "mux: base chardev %s not found",
3704 backend->mux->chardev);
3705 break;
3706 }
3707 chr = qemu_chr_open_mux(base);
3708 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003709 case CHARDEV_BACKEND_KIND_MSMOUSE:
3710 chr = qemu_chr_open_msmouse();
3711 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003712#ifdef CONFIG_BRLAPI
3713 case CHARDEV_BACKEND_KIND_BRAILLE:
3714 chr = chr_baum_init();
3715 break;
3716#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003717 default:
3718 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3719 break;
3720 }
3721
3722 if (chr == NULL && !error_is_set(errp)) {
3723 error_setg(errp, "Failed to create chardev");
3724 }
3725 if (chr) {
3726 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003727 chr->avail_connections =
3728 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003729 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3730 return ret;
3731 } else {
3732 g_free(ret);
3733 return NULL;
3734 }
3735}
3736
3737void qmp_chardev_remove(const char *id, Error **errp)
3738{
3739 CharDriverState *chr;
3740
3741 chr = qemu_chr_find(id);
3742 if (NULL == chr) {
3743 error_setg(errp, "Chardev '%s' not found", id);
3744 return;
3745 }
3746 if (chr->chr_can_read || chr->chr_read ||
3747 chr->chr_event || chr->handler_opaque) {
3748 error_setg(errp, "Chardev '%s' is busy", id);
3749 return;
3750 }
3751 qemu_chr_delete(chr);
3752}
Anthony Liguorid654f342013-03-05 23:21:28 +05303753
3754static void register_types(void)
3755{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003756 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303757 register_char_driver("socket", qemu_chr_open_socket);
3758 register_char_driver("udp", qemu_chr_open_udp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303759 register_char_driver("memory", qemu_chr_open_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003760 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3761 qemu_chr_parse_file_out);
Anthony Liguorid654f342013-03-05 23:21:28 +05303762#ifdef _WIN32
Anthony Liguorid654f342013-03-05 23:21:28 +05303763 register_char_driver("pipe", qemu_chr_open_win_pipe);
3764 register_char_driver("console", qemu_chr_open_win_con);
3765 register_char_driver("serial", qemu_chr_open_win);
3766 register_char_driver("stdio", qemu_chr_open_win_stdio);
3767#else
Anthony Liguorid654f342013-03-05 23:21:28 +05303768 register_char_driver("pipe", qemu_chr_open_pipe);
3769 register_char_driver("stdio", qemu_chr_open_stdio);
3770#endif
Anthony Liguorid654f342013-03-05 23:21:28 +05303771#ifdef HAVE_CHARDEV_TTY
3772 register_char_driver("tty", qemu_chr_open_tty);
3773 register_char_driver("serial", qemu_chr_open_tty);
3774 register_char_driver("pty", qemu_chr_open_pty);
3775#endif
3776#ifdef HAVE_CHARDEV_PARPORT
3777 register_char_driver("parallel", qemu_chr_open_pp);
3778 register_char_driver("parport", qemu_chr_open_pp);
3779#endif
Anthony Liguorid654f342013-03-05 23:21:28 +05303780}
3781
3782type_init(register_types);