blob: 85ffd8e9094d550b915e19ac595a1aa24c56d883 [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
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100911static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000912{
913 CharDriverState *chr;
914
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400915 if (is_daemonized()) {
916 error_report("cannot use stdio with -daemonize");
917 return NULL;
918 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530919 old_fd0_flags = fcntl(0, F_GETFL);
920 tcgetattr (0, &oldtty);
921 fcntl(0, F_SETFL, O_NONBLOCK);
922 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100923
aliguori6f97dba2008-10-31 18:49:55 +0000924 chr = qemu_chr_open_fd(0, 1);
925 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100926 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100927 stdio_allow_signal = display_type != DT_NOGRAPHIC;
928 if (opts->has_signal) {
929 stdio_allow_signal = opts->signal;
930 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500931 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000932
Markus Armbruster1f514702012-02-07 15:09:08 +0100933 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000934}
935
936#ifdef __sun__
937/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000938static int openpty(int *amaster, int *aslave, char *name,
939 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000940{
941 const char *slave;
942 int mfd = -1, sfd = -1;
943
944 *amaster = *aslave = -1;
945
946 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
947 if (mfd < 0)
948 goto err;
949
950 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
951 goto err;
952
953 if ((slave = ptsname(mfd)) == NULL)
954 goto err;
955
956 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
957 goto err;
958
959 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
960 (termp != NULL && tcgetattr(sfd, termp) < 0))
961 goto err;
962
963 if (amaster)
964 *amaster = mfd;
965 if (aslave)
966 *aslave = sfd;
967 if (winp)
968 ioctl(sfd, TIOCSWINSZ, winp);
969
970 return 0;
971
972err:
973 if (sfd != -1)
974 close(sfd);
975 close(mfd);
976 return -1;
977}
978
blueswir13f4cb3d2009-04-13 16:31:01 +0000979static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000980{
981 termios_p->c_iflag &=
982 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
983 termios_p->c_oflag &= ~OPOST;
984 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
985 termios_p->c_cflag &= ~(CSIZE|PARENB);
986 termios_p->c_cflag |= CS8;
987
988 termios_p->c_cc[VMIN] = 0;
989 termios_p->c_cc[VTIME] = 0;
990}
991#endif
992
993#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100994 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
995 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000996
Gerd Hoffmanne5514982012-12-19 16:35:42 +0100997#define HAVE_CHARDEV_TTY 1
998
aliguori6f97dba2008-10-31 18:49:55 +0000999typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301000 GIOChannel *fd;
1001 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001002 int connected;
1003 int polling;
1004 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301005 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001006} PtyCharDriver;
1007
1008static void pty_chr_update_read_handler(CharDriverState *chr);
1009static void pty_chr_state(CharDriverState *chr, int connected);
1010
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301011static gboolean pty_chr_timer(gpointer opaque)
1012{
1013 struct CharDriverState *chr = opaque;
1014 PtyCharDriver *s = chr->opaque;
1015
1016 if (s->connected) {
1017 goto out;
1018 }
1019 if (s->polling) {
1020 /* If we arrive here without polling being cleared due
1021 * read returning -EIO, then we are (re-)connected */
1022 pty_chr_state(chr, 1);
1023 goto out;
1024 }
1025
1026 /* Next poll ... */
1027 pty_chr_update_read_handler(chr);
1028
1029out:
1030 return FALSE;
1031}
1032
1033static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1034{
1035 PtyCharDriver *s = chr->opaque;
1036
1037 if (s->timer_tag) {
1038 g_source_remove(s->timer_tag);
1039 s->timer_tag = 0;
1040 }
1041
1042 if (ms == 1000) {
1043 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1044 } else {
1045 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1046 }
1047}
1048
aliguori6f97dba2008-10-31 18:49:55 +00001049static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1050{
1051 PtyCharDriver *s = chr->opaque;
1052
1053 if (!s->connected) {
1054 /* guest sends data, check for (re-)connect */
1055 pty_chr_update_read_handler(chr);
1056 return 0;
1057 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301058 return io_channel_send_all(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001059}
1060
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301061static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1062{
1063 PtyCharDriver *s = chr->opaque;
1064 return g_io_create_watch(s->fd, cond);
1065}
1066
aliguori6f97dba2008-10-31 18:49:55 +00001067static int pty_chr_read_poll(void *opaque)
1068{
1069 CharDriverState *chr = opaque;
1070 PtyCharDriver *s = chr->opaque;
1071
Anthony Liguori909cda12011-08-15 11:17:31 -05001072 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001073 return s->read_bytes;
1074}
1075
Anthony Liguori093d3a22013-03-05 23:21:20 +05301076static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001077{
1078 CharDriverState *chr = opaque;
1079 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301080 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301081 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301082 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001083
1084 len = sizeof(buf);
1085 if (len > s->read_bytes)
1086 len = s->read_bytes;
1087 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301088 return FALSE;
1089 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1090 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001091 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301092 return FALSE;
1093 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001094 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001095 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001096 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301097 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001098}
1099
1100static void pty_chr_update_read_handler(CharDriverState *chr)
1101{
1102 PtyCharDriver *s = chr->opaque;
1103
Anthony Liguori093d3a22013-03-05 23:21:20 +05301104 if (s->fd_tag) {
1105 g_source_remove(s->fd_tag);
1106 }
1107
1108 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001109 s->polling = 1;
1110 /*
1111 * Short timeout here: just need wait long enougth that qemu makes
1112 * it through the poll loop once. When reconnected we want a
1113 * short timeout so we notice it almost instantly. Otherwise
1114 * read() gives us -EIO instantly, making pty_chr_state() reset the
1115 * timeout to the normal (much longer) poll interval before the
1116 * timer triggers.
1117 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301118 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001119}
1120
1121static void pty_chr_state(CharDriverState *chr, int connected)
1122{
1123 PtyCharDriver *s = chr->opaque;
1124
1125 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301126 g_source_remove(s->fd_tag);
1127 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001128 s->connected = 0;
1129 s->polling = 0;
1130 /* (re-)connect poll interval for idle guests: once per second.
1131 * We check more frequently in case the guests sends data to
1132 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301133 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001134 } else {
1135 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +05301136 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001137 s->connected = 1;
1138 }
1139}
1140
aliguori6f97dba2008-10-31 18:49:55 +00001141
1142static void pty_chr_close(struct CharDriverState *chr)
1143{
1144 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301145 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001146
Anthony Liguori093d3a22013-03-05 23:21:20 +05301147 if (s->fd_tag) {
1148 g_source_remove(s->fd_tag);
1149 }
1150 fd = g_io_channel_unix_get_fd(s->fd);
1151 g_io_channel_unref(s->fd);
1152 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301153 if (s->timer_tag) {
1154 g_source_remove(s->timer_tag);
1155 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001156 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001157 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001158}
1159
Markus Armbruster1f514702012-02-07 15:09:08 +01001160static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001161{
1162 CharDriverState *chr;
1163 PtyCharDriver *s;
1164 struct termios tty;
Lei Li58650212012-12-21 12:26:38 +08001165 const char *label;
Markus Armbrustera4e26042011-11-11 10:40:05 +01001166 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +00001167#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001168 char pty_name[PATH_MAX];
1169#define q_ptsname(x) pty_name
1170#else
1171 char *pty_name = NULL;
1172#define q_ptsname(x) ptsname(x)
1173#endif
1174
Markus Armbrustera4e26042011-11-11 10:40:05 +01001175 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001176 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001177 }
1178
1179 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001180 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001181 cfmakeraw(&tty);
1182 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1183 close(slave_fd);
1184
Markus Armbrustera4e26042011-11-11 10:40:05 +01001185 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001186
Markus Armbrustera4e26042011-11-11 10:40:05 +01001187 len = strlen(q_ptsname(master_fd)) + 5;
1188 chr->filename = g_malloc(len);
1189 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1190 qemu_opt_set(opts, "path", q_ptsname(master_fd));
Lei Li58650212012-12-21 12:26:38 +08001191
1192 label = qemu_opts_id(opts);
Gerd Hoffmann25bbf612013-01-03 14:23:03 +01001193 fprintf(stderr, "char device redirected to %s%s%s%s\n",
1194 q_ptsname(master_fd),
1195 label ? " (label " : "",
1196 label ? label : "",
1197 label ? ")" : "");
Markus Armbrustera4e26042011-11-11 10:40:05 +01001198
1199 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001200 chr->opaque = s;
1201 chr->chr_write = pty_chr_write;
1202 chr->chr_update_read_handler = pty_chr_update_read_handler;
1203 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301204 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001205
Anthony Liguori093d3a22013-03-05 23:21:20 +05301206 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301207 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001208
Markus Armbruster1f514702012-02-07 15:09:08 +01001209 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001210}
1211
1212static void tty_serial_init(int fd, int speed,
1213 int parity, int data_bits, int stop_bits)
1214{
1215 struct termios tty;
1216 speed_t spd;
1217
1218#if 0
1219 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1220 speed, parity, data_bits, stop_bits);
1221#endif
1222 tcgetattr (fd, &tty);
1223
Stefan Weil45eea132009-10-26 16:10:10 +01001224#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1225 speed = speed * 10 / 11;
1226 do {
1227 check_speed(50);
1228 check_speed(75);
1229 check_speed(110);
1230 check_speed(134);
1231 check_speed(150);
1232 check_speed(200);
1233 check_speed(300);
1234 check_speed(600);
1235 check_speed(1200);
1236 check_speed(1800);
1237 check_speed(2400);
1238 check_speed(4800);
1239 check_speed(9600);
1240 check_speed(19200);
1241 check_speed(38400);
1242 /* Non-Posix values follow. They may be unsupported on some systems. */
1243 check_speed(57600);
1244 check_speed(115200);
1245#ifdef B230400
1246 check_speed(230400);
1247#endif
1248#ifdef B460800
1249 check_speed(460800);
1250#endif
1251#ifdef B500000
1252 check_speed(500000);
1253#endif
1254#ifdef B576000
1255 check_speed(576000);
1256#endif
1257#ifdef B921600
1258 check_speed(921600);
1259#endif
1260#ifdef B1000000
1261 check_speed(1000000);
1262#endif
1263#ifdef B1152000
1264 check_speed(1152000);
1265#endif
1266#ifdef B1500000
1267 check_speed(1500000);
1268#endif
1269#ifdef B2000000
1270 check_speed(2000000);
1271#endif
1272#ifdef B2500000
1273 check_speed(2500000);
1274#endif
1275#ifdef B3000000
1276 check_speed(3000000);
1277#endif
1278#ifdef B3500000
1279 check_speed(3500000);
1280#endif
1281#ifdef B4000000
1282 check_speed(4000000);
1283#endif
aliguori6f97dba2008-10-31 18:49:55 +00001284 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001285 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001286
1287 cfsetispeed(&tty, spd);
1288 cfsetospeed(&tty, spd);
1289
1290 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1291 |INLCR|IGNCR|ICRNL|IXON);
1292 tty.c_oflag |= OPOST;
1293 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1294 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1295 switch(data_bits) {
1296 default:
1297 case 8:
1298 tty.c_cflag |= CS8;
1299 break;
1300 case 7:
1301 tty.c_cflag |= CS7;
1302 break;
1303 case 6:
1304 tty.c_cflag |= CS6;
1305 break;
1306 case 5:
1307 tty.c_cflag |= CS5;
1308 break;
1309 }
1310 switch(parity) {
1311 default:
1312 case 'N':
1313 break;
1314 case 'E':
1315 tty.c_cflag |= PARENB;
1316 break;
1317 case 'O':
1318 tty.c_cflag |= PARENB | PARODD;
1319 break;
1320 }
1321 if (stop_bits == 2)
1322 tty.c_cflag |= CSTOPB;
1323
1324 tcsetattr (fd, TCSANOW, &tty);
1325}
1326
1327static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1328{
1329 FDCharDriver *s = chr->opaque;
1330
1331 switch(cmd) {
1332 case CHR_IOCTL_SERIAL_SET_PARAMS:
1333 {
1334 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301335 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1336 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001337 ssp->data_bits, ssp->stop_bits);
1338 }
1339 break;
1340 case CHR_IOCTL_SERIAL_SET_BREAK:
1341 {
1342 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301343 if (enable) {
1344 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1345 }
aliguori6f97dba2008-10-31 18:49:55 +00001346 }
1347 break;
1348 case CHR_IOCTL_SERIAL_GET_TIOCM:
1349 {
1350 int sarg = 0;
1351 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301352 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001353 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001354 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001355 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001356 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001357 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001358 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001359 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001360 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001361 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001362 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001363 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001364 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001365 *targ |= CHR_TIOCM_RTS;
1366 }
1367 break;
1368 case CHR_IOCTL_SERIAL_SET_TIOCM:
1369 {
1370 int sarg = *(int *)arg;
1371 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301372 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001373 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1374 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1375 if (sarg & CHR_TIOCM_CTS)
1376 targ |= TIOCM_CTS;
1377 if (sarg & CHR_TIOCM_CAR)
1378 targ |= TIOCM_CAR;
1379 if (sarg & CHR_TIOCM_DSR)
1380 targ |= TIOCM_DSR;
1381 if (sarg & CHR_TIOCM_RI)
1382 targ |= TIOCM_RI;
1383 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001384 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001385 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001386 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301387 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001388 }
1389 break;
1390 default:
1391 return -ENOTSUP;
1392 }
1393 return 0;
1394}
1395
David Ahern4266a132010-02-10 18:27:17 -07001396static void qemu_chr_close_tty(CharDriverState *chr)
1397{
1398 FDCharDriver *s = chr->opaque;
1399 int fd = -1;
1400
1401 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301402 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001403 }
1404
1405 fd_chr_close(chr);
1406
1407 if (fd >= 0) {
1408 close(fd);
1409 }
1410}
1411
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001412static CharDriverState *qemu_chr_open_tty_fd(int fd)
1413{
1414 CharDriverState *chr;
1415
1416 tty_serial_init(fd, 115200, 'N', 8, 1);
1417 chr = qemu_chr_open_fd(fd, fd);
1418 chr->chr_ioctl = tty_serial_ioctl;
1419 chr->chr_close = qemu_chr_close_tty;
1420 return chr;
1421}
1422
Markus Armbruster1f514702012-02-07 15:09:08 +01001423static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001424{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001425 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001426 int fd;
1427
Markus Armbrusterb181e042012-02-07 15:09:09 +01001428 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001429 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001430 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001431 }
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001432 return qemu_chr_open_tty_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00001433}
aliguori6f97dba2008-10-31 18:49:55 +00001434#endif /* __linux__ || __sun__ */
1435
1436#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001437
1438#define HAVE_CHARDEV_PARPORT 1
1439
aliguori6f97dba2008-10-31 18:49:55 +00001440typedef struct {
1441 int fd;
1442 int mode;
1443} ParallelCharDriver;
1444
1445static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1446{
1447 if (s->mode != mode) {
1448 int m = mode;
1449 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1450 return 0;
1451 s->mode = mode;
1452 }
1453 return 1;
1454}
1455
1456static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1457{
1458 ParallelCharDriver *drv = chr->opaque;
1459 int fd = drv->fd;
1460 uint8_t b;
1461
1462 switch(cmd) {
1463 case CHR_IOCTL_PP_READ_DATA:
1464 if (ioctl(fd, PPRDATA, &b) < 0)
1465 return -ENOTSUP;
1466 *(uint8_t *)arg = b;
1467 break;
1468 case CHR_IOCTL_PP_WRITE_DATA:
1469 b = *(uint8_t *)arg;
1470 if (ioctl(fd, PPWDATA, &b) < 0)
1471 return -ENOTSUP;
1472 break;
1473 case CHR_IOCTL_PP_READ_CONTROL:
1474 if (ioctl(fd, PPRCONTROL, &b) < 0)
1475 return -ENOTSUP;
1476 /* Linux gives only the lowest bits, and no way to know data
1477 direction! For better compatibility set the fixed upper
1478 bits. */
1479 *(uint8_t *)arg = b | 0xc0;
1480 break;
1481 case CHR_IOCTL_PP_WRITE_CONTROL:
1482 b = *(uint8_t *)arg;
1483 if (ioctl(fd, PPWCONTROL, &b) < 0)
1484 return -ENOTSUP;
1485 break;
1486 case CHR_IOCTL_PP_READ_STATUS:
1487 if (ioctl(fd, PPRSTATUS, &b) < 0)
1488 return -ENOTSUP;
1489 *(uint8_t *)arg = b;
1490 break;
1491 case CHR_IOCTL_PP_DATA_DIR:
1492 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1493 return -ENOTSUP;
1494 break;
1495 case CHR_IOCTL_PP_EPP_READ_ADDR:
1496 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1497 struct ParallelIOArg *parg = arg;
1498 int n = read(fd, parg->buffer, parg->count);
1499 if (n != parg->count) {
1500 return -EIO;
1501 }
1502 }
1503 break;
1504 case CHR_IOCTL_PP_EPP_READ:
1505 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1506 struct ParallelIOArg *parg = arg;
1507 int n = read(fd, parg->buffer, parg->count);
1508 if (n != parg->count) {
1509 return -EIO;
1510 }
1511 }
1512 break;
1513 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1514 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1515 struct ParallelIOArg *parg = arg;
1516 int n = write(fd, parg->buffer, parg->count);
1517 if (n != parg->count) {
1518 return -EIO;
1519 }
1520 }
1521 break;
1522 case CHR_IOCTL_PP_EPP_WRITE:
1523 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1524 struct ParallelIOArg *parg = arg;
1525 int n = write(fd, parg->buffer, parg->count);
1526 if (n != parg->count) {
1527 return -EIO;
1528 }
1529 }
1530 break;
1531 default:
1532 return -ENOTSUP;
1533 }
1534 return 0;
1535}
1536
1537static void pp_close(CharDriverState *chr)
1538{
1539 ParallelCharDriver *drv = chr->opaque;
1540 int fd = drv->fd;
1541
1542 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1543 ioctl(fd, PPRELEASE);
1544 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001545 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001546 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001547}
1548
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001549static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001550{
1551 CharDriverState *chr;
1552 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001553
1554 if (ioctl(fd, PPCLAIM) < 0) {
1555 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001556 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001557 }
1558
Anthony Liguori7267c092011-08-20 22:09:37 -05001559 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001560 drv->fd = fd;
1561 drv->mode = IEEE1284_MODE_COMPAT;
1562
Anthony Liguori7267c092011-08-20 22:09:37 -05001563 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001564 chr->chr_write = null_chr_write;
1565 chr->chr_ioctl = pp_ioctl;
1566 chr->chr_close = pp_close;
1567 chr->opaque = drv;
1568
Amit Shah127338e2009-11-03 19:59:56 +05301569 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001570
Markus Armbruster1f514702012-02-07 15:09:08 +01001571 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001572}
1573#endif /* __linux__ */
1574
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001575#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001576
1577#define HAVE_CHARDEV_PARPORT 1
1578
blueswir16972f932008-11-22 20:49:12 +00001579static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1580{
Stefan Weile0efb992011-02-23 19:09:16 +01001581 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001582 uint8_t b;
1583
1584 switch(cmd) {
1585 case CHR_IOCTL_PP_READ_DATA:
1586 if (ioctl(fd, PPIGDATA, &b) < 0)
1587 return -ENOTSUP;
1588 *(uint8_t *)arg = b;
1589 break;
1590 case CHR_IOCTL_PP_WRITE_DATA:
1591 b = *(uint8_t *)arg;
1592 if (ioctl(fd, PPISDATA, &b) < 0)
1593 return -ENOTSUP;
1594 break;
1595 case CHR_IOCTL_PP_READ_CONTROL:
1596 if (ioctl(fd, PPIGCTRL, &b) < 0)
1597 return -ENOTSUP;
1598 *(uint8_t *)arg = b;
1599 break;
1600 case CHR_IOCTL_PP_WRITE_CONTROL:
1601 b = *(uint8_t *)arg;
1602 if (ioctl(fd, PPISCTRL, &b) < 0)
1603 return -ENOTSUP;
1604 break;
1605 case CHR_IOCTL_PP_READ_STATUS:
1606 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1607 return -ENOTSUP;
1608 *(uint8_t *)arg = b;
1609 break;
1610 default:
1611 return -ENOTSUP;
1612 }
1613 return 0;
1614}
1615
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001616static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001617{
1618 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001619
Anthony Liguori7267c092011-08-20 22:09:37 -05001620 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001621 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001622 chr->chr_write = null_chr_write;
1623 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001624 return chr;
blueswir16972f932008-11-22 20:49:12 +00001625}
1626#endif
1627
aliguori6f97dba2008-10-31 18:49:55 +00001628#else /* _WIN32 */
1629
1630typedef struct {
1631 int max_size;
1632 HANDLE hcom, hrecv, hsend;
1633 OVERLAPPED orecv, osend;
1634 BOOL fpipe;
1635 DWORD len;
1636} WinCharState;
1637
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001638typedef struct {
1639 HANDLE hStdIn;
1640 HANDLE hInputReadyEvent;
1641 HANDLE hInputDoneEvent;
1642 HANDLE hInputThread;
1643 uint8_t win_stdio_buf;
1644} WinStdioCharState;
1645
aliguori6f97dba2008-10-31 18:49:55 +00001646#define NSENDBUF 2048
1647#define NRECVBUF 2048
1648#define MAXCONNECT 1
1649#define NTIMEOUT 5000
1650
1651static int win_chr_poll(void *opaque);
1652static int win_chr_pipe_poll(void *opaque);
1653
1654static void win_chr_close(CharDriverState *chr)
1655{
1656 WinCharState *s = chr->opaque;
1657
1658 if (s->hsend) {
1659 CloseHandle(s->hsend);
1660 s->hsend = NULL;
1661 }
1662 if (s->hrecv) {
1663 CloseHandle(s->hrecv);
1664 s->hrecv = NULL;
1665 }
1666 if (s->hcom) {
1667 CloseHandle(s->hcom);
1668 s->hcom = NULL;
1669 }
1670 if (s->fpipe)
1671 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1672 else
1673 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301674
Hans de Goedea425d232011-11-19 10:22:43 +01001675 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001676}
1677
1678static int win_chr_init(CharDriverState *chr, const char *filename)
1679{
1680 WinCharState *s = chr->opaque;
1681 COMMCONFIG comcfg;
1682 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1683 COMSTAT comstat;
1684 DWORD size;
1685 DWORD err;
1686
1687 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1688 if (!s->hsend) {
1689 fprintf(stderr, "Failed CreateEvent\n");
1690 goto fail;
1691 }
1692 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1693 if (!s->hrecv) {
1694 fprintf(stderr, "Failed CreateEvent\n");
1695 goto fail;
1696 }
1697
1698 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1699 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1700 if (s->hcom == INVALID_HANDLE_VALUE) {
1701 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1702 s->hcom = NULL;
1703 goto fail;
1704 }
1705
1706 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1707 fprintf(stderr, "Failed SetupComm\n");
1708 goto fail;
1709 }
1710
1711 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1712 size = sizeof(COMMCONFIG);
1713 GetDefaultCommConfig(filename, &comcfg, &size);
1714 comcfg.dcb.DCBlength = sizeof(DCB);
1715 CommConfigDialog(filename, NULL, &comcfg);
1716
1717 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1718 fprintf(stderr, "Failed SetCommState\n");
1719 goto fail;
1720 }
1721
1722 if (!SetCommMask(s->hcom, EV_ERR)) {
1723 fprintf(stderr, "Failed SetCommMask\n");
1724 goto fail;
1725 }
1726
1727 cto.ReadIntervalTimeout = MAXDWORD;
1728 if (!SetCommTimeouts(s->hcom, &cto)) {
1729 fprintf(stderr, "Failed SetCommTimeouts\n");
1730 goto fail;
1731 }
1732
1733 if (!ClearCommError(s->hcom, &err, &comstat)) {
1734 fprintf(stderr, "Failed ClearCommError\n");
1735 goto fail;
1736 }
1737 qemu_add_polling_cb(win_chr_poll, chr);
1738 return 0;
1739
1740 fail:
1741 win_chr_close(chr);
1742 return -1;
1743}
1744
1745static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1746{
1747 WinCharState *s = chr->opaque;
1748 DWORD len, ret, size, err;
1749
1750 len = len1;
1751 ZeroMemory(&s->osend, sizeof(s->osend));
1752 s->osend.hEvent = s->hsend;
1753 while (len > 0) {
1754 if (s->hsend)
1755 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1756 else
1757 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1758 if (!ret) {
1759 err = GetLastError();
1760 if (err == ERROR_IO_PENDING) {
1761 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1762 if (ret) {
1763 buf += size;
1764 len -= size;
1765 } else {
1766 break;
1767 }
1768 } else {
1769 break;
1770 }
1771 } else {
1772 buf += size;
1773 len -= size;
1774 }
1775 }
1776 return len1 - len;
1777}
1778
1779static int win_chr_read_poll(CharDriverState *chr)
1780{
1781 WinCharState *s = chr->opaque;
1782
Anthony Liguori909cda12011-08-15 11:17:31 -05001783 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001784 return s->max_size;
1785}
1786
1787static void win_chr_readfile(CharDriverState *chr)
1788{
1789 WinCharState *s = chr->opaque;
1790 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301791 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001792 DWORD size;
1793
1794 ZeroMemory(&s->orecv, sizeof(s->orecv));
1795 s->orecv.hEvent = s->hrecv;
1796 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1797 if (!ret) {
1798 err = GetLastError();
1799 if (err == ERROR_IO_PENDING) {
1800 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1801 }
1802 }
1803
1804 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001805 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001806 }
1807}
1808
1809static void win_chr_read(CharDriverState *chr)
1810{
1811 WinCharState *s = chr->opaque;
1812
1813 if (s->len > s->max_size)
1814 s->len = s->max_size;
1815 if (s->len == 0)
1816 return;
1817
1818 win_chr_readfile(chr);
1819}
1820
1821static int win_chr_poll(void *opaque)
1822{
1823 CharDriverState *chr = opaque;
1824 WinCharState *s = chr->opaque;
1825 COMSTAT status;
1826 DWORD comerr;
1827
1828 ClearCommError(s->hcom, &comerr, &status);
1829 if (status.cbInQue > 0) {
1830 s->len = status.cbInQue;
1831 win_chr_read_poll(chr);
1832 win_chr_read(chr);
1833 return 1;
1834 }
1835 return 0;
1836}
1837
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001838static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001839{
1840 CharDriverState *chr;
1841 WinCharState *s;
1842
Anthony Liguori7267c092011-08-20 22:09:37 -05001843 chr = g_malloc0(sizeof(CharDriverState));
1844 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001845 chr->opaque = s;
1846 chr->chr_write = win_chr_write;
1847 chr->chr_close = win_chr_close;
1848
1849 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001850 g_free(s);
1851 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001852 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001853 }
Amit Shah127338e2009-11-03 19:59:56 +05301854 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001855 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001856}
1857
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001858static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1859{
1860 return qemu_chr_open_win_path(qemu_opt_get(opts, "path"));
1861}
1862
aliguori6f97dba2008-10-31 18:49:55 +00001863static int win_chr_pipe_poll(void *opaque)
1864{
1865 CharDriverState *chr = opaque;
1866 WinCharState *s = chr->opaque;
1867 DWORD size;
1868
1869 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1870 if (size > 0) {
1871 s->len = size;
1872 win_chr_read_poll(chr);
1873 win_chr_read(chr);
1874 return 1;
1875 }
1876 return 0;
1877}
1878
1879static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1880{
1881 WinCharState *s = chr->opaque;
1882 OVERLAPPED ov;
1883 int ret;
1884 DWORD size;
1885 char openname[256];
1886
1887 s->fpipe = TRUE;
1888
1889 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1890 if (!s->hsend) {
1891 fprintf(stderr, "Failed CreateEvent\n");
1892 goto fail;
1893 }
1894 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1895 if (!s->hrecv) {
1896 fprintf(stderr, "Failed CreateEvent\n");
1897 goto fail;
1898 }
1899
1900 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1901 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1902 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1903 PIPE_WAIT,
1904 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1905 if (s->hcom == INVALID_HANDLE_VALUE) {
1906 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1907 s->hcom = NULL;
1908 goto fail;
1909 }
1910
1911 ZeroMemory(&ov, sizeof(ov));
1912 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1913 ret = ConnectNamedPipe(s->hcom, &ov);
1914 if (ret) {
1915 fprintf(stderr, "Failed ConnectNamedPipe\n");
1916 goto fail;
1917 }
1918
1919 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1920 if (!ret) {
1921 fprintf(stderr, "Failed GetOverlappedResult\n");
1922 if (ov.hEvent) {
1923 CloseHandle(ov.hEvent);
1924 ov.hEvent = NULL;
1925 }
1926 goto fail;
1927 }
1928
1929 if (ov.hEvent) {
1930 CloseHandle(ov.hEvent);
1931 ov.hEvent = NULL;
1932 }
1933 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1934 return 0;
1935
1936 fail:
1937 win_chr_close(chr);
1938 return -1;
1939}
1940
1941
Markus Armbruster1f514702012-02-07 15:09:08 +01001942static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001943{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001944 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001945 CharDriverState *chr;
1946 WinCharState *s;
1947
Anthony Liguori7267c092011-08-20 22:09:37 -05001948 chr = g_malloc0(sizeof(CharDriverState));
1949 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001950 chr->opaque = s;
1951 chr->chr_write = win_chr_write;
1952 chr->chr_close = win_chr_close;
1953
1954 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001955 g_free(s);
1956 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001957 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001958 }
Amit Shah127338e2009-11-03 19:59:56 +05301959 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001960 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001961}
1962
Markus Armbruster1f514702012-02-07 15:09:08 +01001963static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001964{
1965 CharDriverState *chr;
1966 WinCharState *s;
1967
Anthony Liguori7267c092011-08-20 22:09:37 -05001968 chr = g_malloc0(sizeof(CharDriverState));
1969 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001970 s->hcom = fd_out;
1971 chr->opaque = s;
1972 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301973 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001974 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001975}
1976
Markus Armbruster1f514702012-02-07 15:09:08 +01001977static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001978{
Markus Armbruster1f514702012-02-07 15:09:08 +01001979 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001980}
1981
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001982static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1983{
1984 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1985 DWORD dwSize;
1986 int len1;
1987
1988 len1 = len;
1989
1990 while (len1 > 0) {
1991 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1992 break;
1993 }
1994 buf += dwSize;
1995 len1 -= dwSize;
1996 }
1997
1998 return len - len1;
1999}
2000
2001static void win_stdio_wait_func(void *opaque)
2002{
2003 CharDriverState *chr = opaque;
2004 WinStdioCharState *stdio = chr->opaque;
2005 INPUT_RECORD buf[4];
2006 int ret;
2007 DWORD dwSize;
2008 int i;
2009
2010 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2011 &dwSize);
2012
2013 if (!ret) {
2014 /* Avoid error storm */
2015 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2016 return;
2017 }
2018
2019 for (i = 0; i < dwSize; i++) {
2020 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2021
2022 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2023 int j;
2024 if (kev->uChar.AsciiChar != 0) {
2025 for (j = 0; j < kev->wRepeatCount; j++) {
2026 if (qemu_chr_be_can_write(chr)) {
2027 uint8_t c = kev->uChar.AsciiChar;
2028 qemu_chr_be_write(chr, &c, 1);
2029 }
2030 }
2031 }
2032 }
2033 }
2034}
2035
2036static DWORD WINAPI win_stdio_thread(LPVOID param)
2037{
2038 CharDriverState *chr = param;
2039 WinStdioCharState *stdio = chr->opaque;
2040 int ret;
2041 DWORD dwSize;
2042
2043 while (1) {
2044
2045 /* Wait for one byte */
2046 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2047
2048 /* Exit in case of error, continue if nothing read */
2049 if (!ret) {
2050 break;
2051 }
2052 if (!dwSize) {
2053 continue;
2054 }
2055
2056 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2057 if (stdio->win_stdio_buf == '\r') {
2058 continue;
2059 }
2060
2061 /* Signal the main thread and wait until the byte was eaten */
2062 if (!SetEvent(stdio->hInputReadyEvent)) {
2063 break;
2064 }
2065 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2066 != WAIT_OBJECT_0) {
2067 break;
2068 }
2069 }
2070
2071 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2072 return 0;
2073}
2074
2075static void win_stdio_thread_wait_func(void *opaque)
2076{
2077 CharDriverState *chr = opaque;
2078 WinStdioCharState *stdio = chr->opaque;
2079
2080 if (qemu_chr_be_can_write(chr)) {
2081 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2082 }
2083
2084 SetEvent(stdio->hInputDoneEvent);
2085}
2086
2087static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2088{
2089 WinStdioCharState *stdio = chr->opaque;
2090 DWORD dwMode = 0;
2091
2092 GetConsoleMode(stdio->hStdIn, &dwMode);
2093
2094 if (echo) {
2095 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2096 } else {
2097 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2098 }
2099}
2100
2101static void win_stdio_close(CharDriverState *chr)
2102{
2103 WinStdioCharState *stdio = chr->opaque;
2104
2105 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2106 CloseHandle(stdio->hInputReadyEvent);
2107 }
2108 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2109 CloseHandle(stdio->hInputDoneEvent);
2110 }
2111 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2112 TerminateThread(stdio->hInputThread, 0);
2113 }
2114
2115 g_free(chr->opaque);
2116 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002117}
2118
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002119static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002120{
2121 CharDriverState *chr;
2122 WinStdioCharState *stdio;
2123 DWORD dwMode;
2124 int is_console = 0;
2125
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002126 chr = g_malloc0(sizeof(CharDriverState));
2127 stdio = g_malloc0(sizeof(WinStdioCharState));
2128
2129 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2130 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2131 fprintf(stderr, "cannot open stdio: invalid handle\n");
2132 exit(1);
2133 }
2134
2135 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2136
2137 chr->opaque = stdio;
2138 chr->chr_write = win_stdio_write;
2139 chr->chr_close = win_stdio_close;
2140
Anthony Liguoried7a1542013-03-05 23:21:17 +05302141 if (is_console) {
2142 if (qemu_add_wait_object(stdio->hStdIn,
2143 win_stdio_wait_func, chr)) {
2144 fprintf(stderr, "qemu_add_wait_object: failed\n");
2145 }
2146 } else {
2147 DWORD dwId;
2148
2149 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2150 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2151 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2152 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002153
Anthony Liguoried7a1542013-03-05 23:21:17 +05302154 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2155 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2156 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2157 fprintf(stderr, "cannot create stdio thread or event\n");
2158 exit(1);
2159 }
2160 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2161 win_stdio_thread_wait_func, chr)) {
2162 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002163 }
2164 }
2165
2166 dwMode |= ENABLE_LINE_INPUT;
2167
Anthony Liguoried7a1542013-03-05 23:21:17 +05302168 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002169 /* set the terminal in raw mode */
2170 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2171 dwMode |= ENABLE_PROCESSED_INPUT;
2172 }
2173
2174 SetConsoleMode(stdio->hStdIn, dwMode);
2175
2176 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2177 qemu_chr_fe_set_echo(chr, false);
2178
Markus Armbruster1f514702012-02-07 15:09:08 +01002179 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002180}
aliguori6f97dba2008-10-31 18:49:55 +00002181#endif /* !_WIN32 */
2182
Anthony Liguori5ab82112013-03-05 23:21:31 +05302183
aliguori6f97dba2008-10-31 18:49:55 +00002184/***********************************************************/
2185/* UDP Net console */
2186
2187typedef struct {
2188 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302189 GIOChannel *chan;
2190 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302191 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002192 int bufcnt;
2193 int bufptr;
2194 int max_size;
2195} NetCharDriver;
2196
2197static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2198{
2199 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302200 gsize bytes_written;
2201 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002202
Anthony Liguori76a96442013-03-05 23:21:21 +05302203 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2204 if (status == G_IO_STATUS_EOF) {
2205 return 0;
2206 } else if (status != G_IO_STATUS_NORMAL) {
2207 return -1;
2208 }
2209
2210 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002211}
2212
2213static int udp_chr_read_poll(void *opaque)
2214{
2215 CharDriverState *chr = opaque;
2216 NetCharDriver *s = chr->opaque;
2217
Anthony Liguori909cda12011-08-15 11:17:31 -05002218 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002219
2220 /* If there were any stray characters in the queue process them
2221 * first
2222 */
2223 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002224 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002225 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002226 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002227 }
2228 return s->max_size;
2229}
2230
Anthony Liguori76a96442013-03-05 23:21:21 +05302231static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002232{
2233 CharDriverState *chr = opaque;
2234 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302235 gsize bytes_read = 0;
2236 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002237
2238 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302239 return FALSE;
2240 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2241 &bytes_read, NULL);
2242 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002243 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302244 if (status != G_IO_STATUS_NORMAL) {
2245 return FALSE;
2246 }
aliguori6f97dba2008-10-31 18:49:55 +00002247
2248 s->bufptr = 0;
2249 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002250 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002251 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002252 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002253 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302254
2255 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002256}
2257
2258static void udp_chr_update_read_handler(CharDriverState *chr)
2259{
2260 NetCharDriver *s = chr->opaque;
2261
Anthony Liguori76a96442013-03-05 23:21:21 +05302262 if (s->tag) {
2263 g_source_remove(s->tag);
2264 s->tag = 0;
2265 }
2266
2267 if (s->chan) {
2268 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002269 }
2270}
2271
aliguori819f56b2009-03-28 17:58:14 +00002272static void udp_chr_close(CharDriverState *chr)
2273{
2274 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302275 if (s->tag) {
2276 g_source_remove(s->tag);
2277 }
2278 if (s->chan) {
2279 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002280 closesocket(s->fd);
2281 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002282 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002283 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002284}
2285
Markus Armbruster1f514702012-02-07 15:09:08 +01002286static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002287{
2288 CharDriverState *chr = NULL;
2289 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002290 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002291 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002292
Anthony Liguori7267c092011-08-20 22:09:37 -05002293 chr = g_malloc0(sizeof(CharDriverState));
2294 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002295
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002296 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002297 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002298 goto return_err;
2299 }
2300
2301 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302302 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002303 s->bufcnt = 0;
2304 s->bufptr = 0;
2305 chr->opaque = s;
2306 chr->chr_write = udp_chr_write;
2307 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002308 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002309 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002310
2311return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002312 if (local_err) {
2313 qerror_report_err(local_err);
2314 error_free(local_err);
2315 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002316 g_free(chr);
2317 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002318 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002319 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002320 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002321 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002322}
2323
2324/***********************************************************/
2325/* TCP Net console */
2326
2327typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302328
2329 GIOChannel *chan, *listen_chan;
2330 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002331 int fd, listen_fd;
2332 int connected;
2333 int max_size;
2334 int do_telnetopt;
2335 int do_nodelay;
2336 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002337 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002338} TCPCharDriver;
2339
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302340static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002341
2342static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2343{
2344 TCPCharDriver *s = chr->opaque;
2345 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302346 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002347 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002348 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002349 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002350 }
2351}
2352
2353static int tcp_chr_read_poll(void *opaque)
2354{
2355 CharDriverState *chr = opaque;
2356 TCPCharDriver *s = chr->opaque;
2357 if (!s->connected)
2358 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002359 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002360 return s->max_size;
2361}
2362
2363#define IAC 255
2364#define IAC_BREAK 243
2365static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2366 TCPCharDriver *s,
2367 uint8_t *buf, int *size)
2368{
2369 /* Handle any telnet client's basic IAC options to satisfy char by
2370 * char mode with no echo. All IAC options will be removed from
2371 * the buf and the do_telnetopt variable will be used to track the
2372 * state of the width of the IAC information.
2373 *
2374 * IAC commands come in sets of 3 bytes with the exception of the
2375 * "IAC BREAK" command and the double IAC.
2376 */
2377
2378 int i;
2379 int j = 0;
2380
2381 for (i = 0; i < *size; i++) {
2382 if (s->do_telnetopt > 1) {
2383 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2384 /* Double IAC means send an IAC */
2385 if (j != i)
2386 buf[j] = buf[i];
2387 j++;
2388 s->do_telnetopt = 1;
2389 } else {
2390 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2391 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002392 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002393 s->do_telnetopt++;
2394 }
2395 s->do_telnetopt++;
2396 }
2397 if (s->do_telnetopt >= 4) {
2398 s->do_telnetopt = 1;
2399 }
2400 } else {
2401 if ((unsigned char)buf[i] == IAC) {
2402 s->do_telnetopt = 2;
2403 } else {
2404 if (j != i)
2405 buf[j] = buf[i];
2406 j++;
2407 }
2408 }
2409 }
2410 *size = j;
2411}
2412
Mark McLoughlin7d174052009-07-22 09:11:39 +01002413static int tcp_get_msgfd(CharDriverState *chr)
2414{
2415 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002416 int fd = s->msgfd;
2417 s->msgfd = -1;
2418 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002419}
2420
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002421#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002422static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2423{
2424 TCPCharDriver *s = chr->opaque;
2425 struct cmsghdr *cmsg;
2426
2427 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2428 int fd;
2429
2430 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2431 cmsg->cmsg_level != SOL_SOCKET ||
2432 cmsg->cmsg_type != SCM_RIGHTS)
2433 continue;
2434
2435 fd = *((int *)CMSG_DATA(cmsg));
2436 if (fd < 0)
2437 continue;
2438
Corey Bryant06138652012-08-14 16:43:42 -04002439#ifndef MSG_CMSG_CLOEXEC
2440 qemu_set_cloexec(fd);
2441#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002442 if (s->msgfd != -1)
2443 close(s->msgfd);
2444 s->msgfd = fd;
2445 }
2446}
2447
Mark McLoughlin9977c892009-07-22 09:11:38 +01002448static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2449{
2450 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002451 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002452 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002453 union {
2454 struct cmsghdr cmsg;
2455 char control[CMSG_SPACE(sizeof(int))];
2456 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002457 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002458 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002459
2460 iov[0].iov_base = buf;
2461 iov[0].iov_len = len;
2462
2463 msg.msg_iov = iov;
2464 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002465 msg.msg_control = &msg_control;
2466 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002467
Corey Bryant06138652012-08-14 16:43:42 -04002468#ifdef MSG_CMSG_CLOEXEC
2469 flags |= MSG_CMSG_CLOEXEC;
2470#endif
2471 ret = recvmsg(s->fd, &msg, flags);
2472 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002473 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002474 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002475
2476 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002477}
2478#else
2479static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2480{
2481 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002482 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002483}
2484#endif
2485
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302486static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2487{
2488 TCPCharDriver *s = chr->opaque;
2489 return g_io_create_watch(s->chan, cond);
2490}
2491
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302492static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002493{
2494 CharDriverState *chr = opaque;
2495 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302496 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002497 int len, size;
2498
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302499 if (!s->connected || s->max_size <= 0) {
2500 return FALSE;
2501 }
aliguori6f97dba2008-10-31 18:49:55 +00002502 len = sizeof(buf);
2503 if (len > s->max_size)
2504 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002505 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002506 if (size == 0) {
2507 /* connection closed */
2508 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302509 if (s->listen_chan) {
2510 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002511 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302512 g_source_remove(s->tag);
2513 s->tag = 0;
2514 g_io_channel_unref(s->chan);
2515 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002516 closesocket(s->fd);
2517 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002518 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002519 } else if (size > 0) {
2520 if (s->do_telnetopt)
2521 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2522 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002523 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002524 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302525
2526 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002527}
2528
Blue Swirl68c18d12010-08-15 09:46:24 +00002529#ifndef _WIN32
2530CharDriverState *qemu_chr_open_eventfd(int eventfd)
2531{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002532 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002533}
Blue Swirl68c18d12010-08-15 09:46:24 +00002534#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002535
aliguori6f97dba2008-10-31 18:49:55 +00002536static void tcp_chr_connect(void *opaque)
2537{
2538 CharDriverState *chr = opaque;
2539 TCPCharDriver *s = chr->opaque;
2540
2541 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302542 if (s->chan) {
2543 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002544 }
Amit Shah127338e2009-11-03 19:59:56 +05302545 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002546}
2547
2548#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2549static void tcp_chr_telnet_init(int fd)
2550{
2551 char buf[3];
2552 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2553 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2554 send(fd, (char *)buf, 3, 0);
2555 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2556 send(fd, (char *)buf, 3, 0);
2557 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2558 send(fd, (char *)buf, 3, 0);
2559 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2560 send(fd, (char *)buf, 3, 0);
2561}
2562
Daniel P. Berrange13661082011-06-23 13:31:42 +01002563static int tcp_chr_add_client(CharDriverState *chr, int fd)
2564{
2565 TCPCharDriver *s = chr->opaque;
2566 if (s->fd != -1)
2567 return -1;
2568
2569 socket_set_nonblock(fd);
2570 if (s->do_nodelay)
2571 socket_set_nodelay(fd);
2572 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302573 s->chan = io_channel_from_socket(fd);
2574 g_source_remove(s->listen_tag);
2575 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002576 tcp_chr_connect(chr);
2577
2578 return 0;
2579}
2580
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302581static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002582{
2583 CharDriverState *chr = opaque;
2584 TCPCharDriver *s = chr->opaque;
2585 struct sockaddr_in saddr;
2586#ifndef _WIN32
2587 struct sockaddr_un uaddr;
2588#endif
2589 struct sockaddr *addr;
2590 socklen_t len;
2591 int fd;
2592
2593 for(;;) {
2594#ifndef _WIN32
2595 if (s->is_unix) {
2596 len = sizeof(uaddr);
2597 addr = (struct sockaddr *)&uaddr;
2598 } else
2599#endif
2600 {
2601 len = sizeof(saddr);
2602 addr = (struct sockaddr *)&saddr;
2603 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002604 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002605 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302606 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002607 } else if (fd >= 0) {
2608 if (s->do_telnetopt)
2609 tcp_chr_telnet_init(fd);
2610 break;
2611 }
2612 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002613 if (tcp_chr_add_client(chr, fd) < 0)
2614 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302615
2616 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002617}
2618
2619static void tcp_chr_close(CharDriverState *chr)
2620{
2621 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002622 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302623 if (s->tag) {
2624 g_source_remove(s->tag);
2625 }
2626 if (s->chan) {
2627 g_io_channel_unref(s->chan);
2628 }
aliguori6f97dba2008-10-31 18:49:55 +00002629 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002630 }
2631 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302632 if (s->listen_tag) {
2633 g_source_remove(s->listen_tag);
2634 }
2635 if (s->listen_chan) {
2636 g_io_channel_unref(s->listen_chan);
2637 }
aliguori6f97dba2008-10-31 18:49:55 +00002638 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002639 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002640 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002641 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002642}
2643
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002644static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2645 bool is_listen, bool is_telnet,
2646 bool is_waitconnect,
2647 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002648{
2649 CharDriverState *chr = NULL;
2650 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002651 char host[NI_MAXHOST], serv[NI_MAXSERV];
2652 const char *left = "", *right = "";
2653 struct sockaddr_storage ss;
2654 socklen_t ss_len = sizeof(ss);
2655
2656 memset(&ss, 0, ss_len);
2657 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2658 error_setg(errp, "getsockname: %s", strerror(errno));
2659 return NULL;
2660 }
2661
2662 chr = g_malloc0(sizeof(CharDriverState));
2663 s = g_malloc0(sizeof(TCPCharDriver));
2664
2665 s->connected = 0;
2666 s->fd = -1;
2667 s->listen_fd = -1;
2668 s->msgfd = -1;
2669
2670 chr->filename = g_malloc(256);
2671 switch (ss.ss_family) {
2672#ifndef _WIN32
2673 case AF_UNIX:
2674 s->is_unix = 1;
2675 snprintf(chr->filename, 256, "unix:%s%s",
2676 ((struct sockaddr_un *)(&ss))->sun_path,
2677 is_listen ? ",server" : "");
2678 break;
2679#endif
2680 case AF_INET6:
2681 left = "[";
2682 right = "]";
2683 /* fall through */
2684 case AF_INET:
2685 s->do_nodelay = do_nodelay;
2686 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2687 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
2688 snprintf(chr->filename, 256, "%s:%s:%s%s%s%s",
2689 is_telnet ? "telnet" : "tcp",
2690 left, host, right, serv,
2691 is_listen ? ",server" : "");
2692 break;
2693 }
2694
2695 chr->opaque = s;
2696 chr->chr_write = tcp_chr_write;
2697 chr->chr_close = tcp_chr_close;
2698 chr->get_msgfd = tcp_get_msgfd;
2699 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302700 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002701
2702 if (is_listen) {
2703 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302704 s->listen_chan = io_channel_from_socket(s->listen_fd);
2705 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002706 if (is_telnet) {
2707 s->do_telnetopt = 1;
2708 }
2709 } else {
2710 s->connected = 1;
2711 s->fd = fd;
2712 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302713 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002714 tcp_chr_connect(chr);
2715 }
2716
2717 if (is_listen && is_waitconnect) {
2718 printf("QEMU waiting for connection on: %s\n",
2719 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302720 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002721 socket_set_nonblock(s->listen_fd);
2722 }
2723 return chr;
2724}
2725
2726static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2727{
2728 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002729 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002730 int fd = -1;
2731 int is_listen;
2732 int is_waitconnect;
2733 int do_nodelay;
2734 int is_unix;
2735 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002736
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002737 is_listen = qemu_opt_get_bool(opts, "server", 0);
2738 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2739 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2740 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2741 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002742 if (!is_listen)
2743 is_waitconnect = 0;
2744
aliguorif07b6002008-11-11 20:54:09 +00002745 if (is_unix) {
2746 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002747 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002748 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002749 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002750 }
2751 } else {
2752 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002753 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002754 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002755 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002756 }
2757 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002758 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002759 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002760 }
aliguori6f97dba2008-10-31 18:49:55 +00002761
2762 if (!is_waitconnect)
2763 socket_set_nonblock(fd);
2764
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002765 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2766 is_waitconnect, &local_err);
2767 if (error_is_set(&local_err)) {
2768 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002769 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002770 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002771
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002772
aliguori6f97dba2008-10-31 18:49:55 +00002773 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002774 if (local_err) {
2775 qerror_report_err(local_err);
2776 error_free(local_err);
2777 }
2778 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002779 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002780 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002781 if (chr) {
2782 g_free(chr->opaque);
2783 g_free(chr);
2784 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002785 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002786}
2787
Luiz Capitulino999bd672010-10-22 16:09:05 -02002788/***********************************************************/
2789/* Memory chardev */
2790typedef struct {
2791 size_t outbuf_size;
2792 size_t outbuf_capacity;
2793 uint8_t *outbuf;
2794} MemoryDriver;
2795
2796static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2797{
2798 MemoryDriver *d = chr->opaque;
2799
2800 /* TODO: the QString implementation has the same code, we should
2801 * introduce a generic way to do this in cutils.c */
2802 if (d->outbuf_capacity < d->outbuf_size + len) {
2803 /* grow outbuf */
2804 d->outbuf_capacity += len;
2805 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002806 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002807 }
2808
2809 memcpy(d->outbuf + d->outbuf_size, buf, len);
2810 d->outbuf_size += len;
2811
2812 return len;
2813}
2814
2815void qemu_chr_init_mem(CharDriverState *chr)
2816{
2817 MemoryDriver *d;
2818
Anthony Liguori7267c092011-08-20 22:09:37 -05002819 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002820 d->outbuf_size = 0;
2821 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002822 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002823
2824 memset(chr, 0, sizeof(*chr));
2825 chr->opaque = d;
2826 chr->chr_write = mem_chr_write;
2827}
2828
2829QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2830{
2831 MemoryDriver *d = chr->opaque;
2832 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2833}
2834
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002835/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002836void qemu_chr_close_mem(CharDriverState *chr)
2837{
2838 MemoryDriver *d = chr->opaque;
2839
Anthony Liguori7267c092011-08-20 22:09:37 -05002840 g_free(d->outbuf);
2841 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002842 chr->opaque = NULL;
2843 chr->chr_write = NULL;
2844}
2845
2846size_t qemu_chr_mem_osize(const CharDriverState *chr)
2847{
2848 const MemoryDriver *d = chr->opaque;
2849 return d->outbuf_size;
2850}
2851
Lei Li51767e72013-01-25 00:03:19 +08002852/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002853/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002854
2855typedef struct {
2856 size_t size;
2857 size_t prod;
2858 size_t cons;
2859 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002860} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002861
Markus Armbruster3949e592013-02-06 21:27:24 +01002862static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002863{
Markus Armbruster3949e592013-02-06 21:27:24 +01002864 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002865
Markus Armbruster5c230102013-02-06 21:27:23 +01002866 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002867}
2868
Markus Armbruster3949e592013-02-06 21:27:24 +01002869static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002870{
Markus Armbruster3949e592013-02-06 21:27:24 +01002871 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002872 int i;
2873
2874 if (!buf || (len < 0)) {
2875 return -1;
2876 }
2877
2878 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002879 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2880 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002881 d->cons = d->prod - d->size;
2882 }
2883 }
2884
2885 return 0;
2886}
2887
Markus Armbruster3949e592013-02-06 21:27:24 +01002888static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002889{
Markus Armbruster3949e592013-02-06 21:27:24 +01002890 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002891 int i;
2892
Markus Armbruster5c230102013-02-06 21:27:23 +01002893 for (i = 0; i < len && d->cons != d->prod; i++) {
2894 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002895 }
2896
2897 return i;
2898}
2899
Markus Armbruster3949e592013-02-06 21:27:24 +01002900static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002901{
Markus Armbruster3949e592013-02-06 21:27:24 +01002902 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002903
2904 g_free(d->cbuf);
2905 g_free(d);
2906 chr->opaque = NULL;
2907}
2908
Markus Armbruster3949e592013-02-06 21:27:24 +01002909static CharDriverState *qemu_chr_open_ringbuf(QemuOpts *opts)
Lei Li51767e72013-01-25 00:03:19 +08002910{
2911 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002912 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002913
2914 chr = g_malloc0(sizeof(CharDriverState));
2915 d = g_malloc(sizeof(*d));
2916
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002917 d->size = qemu_opt_get_size(opts, "size", 0);
Lei Li51767e72013-01-25 00:03:19 +08002918 if (d->size == 0) {
Markus Armbrusterde1cc362013-02-06 21:27:25 +01002919 d->size = 65536;
Lei Li51767e72013-01-25 00:03:19 +08002920 }
2921
2922 /* The size must be power of 2 */
2923 if (d->size & (d->size - 1)) {
Markus Armbruster3949e592013-02-06 21:27:24 +01002924 error_report("size of ringbuf device must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002925 goto fail;
2926 }
2927
2928 d->prod = 0;
2929 d->cons = 0;
2930 d->cbuf = g_malloc0(d->size);
2931
2932 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002933 chr->chr_write = ringbuf_chr_write;
2934 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002935
2936 return chr;
2937
2938fail:
2939 g_free(d);
2940 g_free(chr);
2941 return NULL;
2942}
2943
Markus Armbruster3949e592013-02-06 21:27:24 +01002944static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002945{
Markus Armbruster3949e592013-02-06 21:27:24 +01002946 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002947}
2948
Markus Armbruster3949e592013-02-06 21:27:24 +01002949void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002950 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002951 Error **errp)
2952{
2953 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002954 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002955 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002956 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002957
2958 chr = qemu_chr_find(device);
2959 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002960 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002961 return;
2962 }
2963
Markus Armbruster3949e592013-02-06 21:27:24 +01002964 if (!chr_is_ringbuf(chr)) {
2965 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002966 return;
2967 }
2968
Lei Li1f590cf2013-01-25 00:03:20 +08002969 if (has_format && (format == DATA_FORMAT_BASE64)) {
2970 write_data = g_base64_decode(data, &write_count);
2971 } else {
2972 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002973 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002974 }
2975
Markus Armbruster3949e592013-02-06 21:27:24 +01002976 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002977
Markus Armbruster13289fb2013-02-06 21:27:18 +01002978 if (write_data != (uint8_t *)data) {
2979 g_free((void *)write_data);
2980 }
2981
Lei Li1f590cf2013-01-25 00:03:20 +08002982 if (ret < 0) {
2983 error_setg(errp, "Failed to write to device %s", device);
2984 return;
2985 }
2986}
2987
Markus Armbruster3949e592013-02-06 21:27:24 +01002988char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002989 bool has_format, enum DataFormat format,
2990 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002991{
2992 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002993 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002994 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002995 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002996
2997 chr = qemu_chr_find(device);
2998 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002999 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003000 return NULL;
3001 }
3002
Markus Armbruster3949e592013-02-06 21:27:24 +01003003 if (!chr_is_ringbuf(chr)) {
3004 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003005 return NULL;
3006 }
3007
3008 if (size <= 0) {
3009 error_setg(errp, "size must be greater than zero");
3010 return NULL;
3011 }
3012
Markus Armbruster3949e592013-02-06 21:27:24 +01003013 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003014 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003015 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003016
Markus Armbruster3949e592013-02-06 21:27:24 +01003017 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003018
3019 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003020 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003021 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003022 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003023 /*
3024 * FIXME should read only complete, valid UTF-8 characters up
3025 * to @size bytes. Invalid sequences should be replaced by a
3026 * suitable replacement character. Except when (and only
3027 * when) ring buffer lost characters since last read, initial
3028 * continuation characters should be dropped.
3029 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003030 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003031 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003032 }
3033
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003034 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003035}
3036
Gerd Hoffmann33521632009-12-08 13:11:49 +01003037QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003038{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003039 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003040 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003041 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003042 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003043 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003044
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003045 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3046 if (error_is_set(&local_err)) {
3047 qerror_report_err(local_err);
3048 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003049 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003050 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003051
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003052 if (strstart(filename, "mon:", &p)) {
3053 filename = p;
3054 qemu_opt_set(opts, "mux", "on");
3055 }
3056
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003057 if (strcmp(filename, "null") == 0 ||
3058 strcmp(filename, "pty") == 0 ||
3059 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003060 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003061 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003062 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003063 return opts;
3064 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003065 if (strstart(filename, "vc", &p)) {
3066 qemu_opt_set(opts, "backend", "vc");
3067 if (*p == ':') {
3068 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3069 /* pixels */
3070 qemu_opt_set(opts, "width", width);
3071 qemu_opt_set(opts, "height", height);
3072 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3073 /* chars */
3074 qemu_opt_set(opts, "cols", width);
3075 qemu_opt_set(opts, "rows", height);
3076 } else {
3077 goto fail;
3078 }
3079 }
3080 return opts;
3081 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003082 if (strcmp(filename, "con:") == 0) {
3083 qemu_opt_set(opts, "backend", "console");
3084 return opts;
3085 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003086 if (strstart(filename, "COM", NULL)) {
3087 qemu_opt_set(opts, "backend", "serial");
3088 qemu_opt_set(opts, "path", filename);
3089 return opts;
3090 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003091 if (strstart(filename, "file:", &p)) {
3092 qemu_opt_set(opts, "backend", "file");
3093 qemu_opt_set(opts, "path", p);
3094 return opts;
3095 }
3096 if (strstart(filename, "pipe:", &p)) {
3097 qemu_opt_set(opts, "backend", "pipe");
3098 qemu_opt_set(opts, "path", p);
3099 return opts;
3100 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003101 if (strstart(filename, "tcp:", &p) ||
3102 strstart(filename, "telnet:", &p)) {
3103 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3104 host[0] = 0;
3105 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3106 goto fail;
3107 }
3108 qemu_opt_set(opts, "backend", "socket");
3109 qemu_opt_set(opts, "host", host);
3110 qemu_opt_set(opts, "port", port);
3111 if (p[pos] == ',') {
3112 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3113 goto fail;
3114 }
3115 if (strstart(filename, "telnet:", &p))
3116 qemu_opt_set(opts, "telnet", "on");
3117 return opts;
3118 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003119 if (strstart(filename, "udp:", &p)) {
3120 qemu_opt_set(opts, "backend", "udp");
3121 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3122 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003123 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003124 goto fail;
3125 }
3126 }
3127 qemu_opt_set(opts, "host", host);
3128 qemu_opt_set(opts, "port", port);
3129 if (p[pos] == '@') {
3130 p += pos + 1;
3131 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3132 host[0] = 0;
3133 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003134 goto fail;
3135 }
3136 }
3137 qemu_opt_set(opts, "localaddr", host);
3138 qemu_opt_set(opts, "localport", port);
3139 }
3140 return opts;
3141 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003142 if (strstart(filename, "unix:", &p)) {
3143 qemu_opt_set(opts, "backend", "socket");
3144 if (qemu_opts_do_parse(opts, p, "path") != 0)
3145 goto fail;
3146 return opts;
3147 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003148 if (strstart(filename, "/dev/parport", NULL) ||
3149 strstart(filename, "/dev/ppi", NULL)) {
3150 qemu_opt_set(opts, "backend", "parport");
3151 qemu_opt_set(opts, "path", filename);
3152 return opts;
3153 }
3154 if (strstart(filename, "/dev/", NULL)) {
3155 qemu_opt_set(opts, "backend", "tty");
3156 qemu_opt_set(opts, "path", filename);
3157 return opts;
3158 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003159
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003160fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003161 qemu_opts_del(opts);
3162 return NULL;
3163}
3164
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01003165#ifdef HAVE_CHARDEV_PARPORT
3166
3167static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
3168{
3169 const char *filename = qemu_opt_get(opts, "path");
3170 int fd;
3171
3172 fd = qemu_open(filename, O_RDWR);
3173 if (fd < 0) {
3174 return NULL;
3175 }
3176 return qemu_chr_open_pp_fd(fd);
3177}
3178
3179#endif
3180
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003181static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3182 Error **errp)
3183{
3184 const char *path = qemu_opt_get(opts, "path");
3185
3186 if (path == NULL) {
3187 error_setg(errp, "chardev: file: no filename given");
3188 return;
3189 }
3190 backend->file = g_new0(ChardevFile, 1);
3191 backend->file->out = g_strdup(path);
3192}
3193
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003194static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3195 Error **errp)
3196{
3197 backend->stdio = g_new0(ChardevStdio, 1);
3198 backend->stdio->has_signal = true;
3199 backend->stdio->signal =
3200 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3201}
3202
Anthony Liguorid654f342013-03-05 23:21:28 +05303203typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003204 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003205 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003206 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003207 /* new, qapi-based */
3208 int kind;
3209 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303210} CharDriver;
3211
3212static GSList *backends;
3213
3214void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3215{
3216 CharDriver *s;
3217
3218 s = g_malloc0(sizeof(*s));
3219 s->name = g_strdup(name);
3220 s->open = open;
3221
3222 backends = g_slist_append(backends, s);
3223}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003224
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003225void register_char_driver_qapi(const char *name, int kind,
3226 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3227{
3228 CharDriver *s;
3229
3230 s = g_malloc0(sizeof(*s));
3231 s->name = g_strdup(name);
3232 s->kind = kind;
3233 s->parse = parse;
3234
3235 backends = g_slist_append(backends, s);
3236}
3237
Anthony Liguorif69554b2011-08-15 11:17:37 -05003238CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003239 void (*init)(struct CharDriverState *s),
3240 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003241{
Anthony Liguorid654f342013-03-05 23:21:28 +05303242 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003243 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303244 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003245
3246 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003247 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003248 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003249 }
3250
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003251 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003252 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003253 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003254 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003255 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303256 for (i = backends; i; i = i->next) {
3257 cd = i->data;
3258
3259 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003260 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303261 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003262 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303263 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003264 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003265 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303266 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003267 }
3268
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003269 if (!cd->open) {
3270 /* using new, qapi init */
3271 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3272 ChardevReturn *ret = NULL;
3273 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003274 const char *bid = NULL;
3275
3276 if (qemu_opt_get_bool(opts, "mux", 0)) {
3277 bid = g_strdup_printf("%s-base", id);
3278 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003279
3280 chr = NULL;
3281 backend->kind = cd->kind;
3282 if (cd->parse) {
3283 cd->parse(opts, backend, errp);
3284 if (error_is_set(errp)) {
3285 goto qapi_out;
3286 }
3287 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003288 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003289 if (error_is_set(errp)) {
3290 goto qapi_out;
3291 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003292
3293 if (bid) {
3294 qapi_free_ChardevBackend(backend);
3295 qapi_free_ChardevReturn(ret);
3296 backend = g_new0(ChardevBackend, 1);
3297 backend->mux = g_new0(ChardevMux, 1);
3298 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3299 backend->mux->chardev = g_strdup(bid);
3300 ret = qmp_chardev_add(id, backend, errp);
3301 if (error_is_set(errp)) {
3302 goto qapi_out;
3303 }
3304 }
3305
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003306 chr = qemu_chr_find(id);
3307
3308 qapi_out:
3309 qapi_free_ChardevBackend(backend);
3310 qapi_free_ChardevReturn(ret);
3311 return chr;
3312 }
3313
Anthony Liguorid654f342013-03-05 23:21:28 +05303314 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003315 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003316 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003317 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003318 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003319 }
3320
3321 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003322 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003323 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003324 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003325
3326 if (qemu_opt_get_bool(opts, "mux", 0)) {
3327 CharDriverState *base = chr;
3328 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003329 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003330 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3331 chr = qemu_chr_open_mux(base);
3332 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003333 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003334 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003335 } else {
3336 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003337 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003338 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003339 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003340 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003341
3342err:
3343 qemu_opts_del(opts);
3344 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003345}
3346
Anthony Liguori27143a42011-08-15 11:17:36 -05003347CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003348{
3349 const char *p;
3350 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003351 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003352 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003353
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003354 if (strstart(filename, "chardev:", &p)) {
3355 return qemu_chr_find(p);
3356 }
3357
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003358 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003359 if (!opts)
3360 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003361
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003362 chr = qemu_chr_new_from_opts(opts, init, &err);
3363 if (error_is_set(&err)) {
3364 fprintf(stderr, "%s\n", error_get_pretty(err));
3365 error_free(err);
3366 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003367 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3368 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003369 }
3370 return chr;
3371}
3372
Anthony Liguori15f31512011-08-15 11:17:35 -05003373void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003374{
3375 if (chr->chr_set_echo) {
3376 chr->chr_set_echo(chr, echo);
3377 }
3378}
3379
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003380void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003381{
3382 if (chr->chr_guest_open) {
3383 chr->chr_guest_open(chr);
3384 }
3385}
3386
Anthony Liguori28178222011-08-15 11:17:33 -05003387void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003388{
3389 if (chr->chr_guest_close) {
3390 chr->chr_guest_close(chr);
3391 }
3392}
3393
Anthony Liguori23673ca2013-03-05 23:21:23 +05303394guint qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3395 GIOFunc func, void *user_data)
3396{
3397 GSource *src;
3398 guint tag;
3399
3400 if (s->chr_add_watch == NULL) {
3401 return -ENOSYS;
3402 }
3403
3404 src = s->chr_add_watch(s, cond);
3405 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3406 tag = g_source_attach(src, NULL);
3407 g_source_unref(src);
3408
3409 return tag;
3410}
3411
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003412void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003413{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003414 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003415 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003416 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003417 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003418 g_free(chr->filename);
3419 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003420 if (chr->opts) {
3421 qemu_opts_del(chr->opts);
3422 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003423 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003424}
3425
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003426ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003427{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003428 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003429 CharDriverState *chr;
3430
Blue Swirl72cf2d42009-09-12 07:36:22 +00003431 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003432 ChardevInfoList *info = g_malloc0(sizeof(*info));
3433 info->value = g_malloc0(sizeof(*info->value));
3434 info->value->label = g_strdup(chr->label);
3435 info->value->filename = g_strdup(chr->filename);
3436
3437 info->next = chr_list;
3438 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003439 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003440
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003441 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003442}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003443
3444CharDriverState *qemu_chr_find(const char *name)
3445{
3446 CharDriverState *chr;
3447
Blue Swirl72cf2d42009-09-12 07:36:22 +00003448 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003449 if (strcmp(chr->label, name) != 0)
3450 continue;
3451 return chr;
3452 }
3453 return NULL;
3454}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003455
3456/* Get a character (serial) device interface. */
3457CharDriverState *qemu_char_get_next_serial(void)
3458{
3459 static int next_serial;
3460
3461 /* FIXME: This function needs to go away: use chardev properties! */
3462 return serial_hds[next_serial++];
3463}
3464
Paolo Bonzini4d454572012-11-26 16:03:42 +01003465QemuOptsList qemu_chardev_opts = {
3466 .name = "chardev",
3467 .implied_opt_name = "backend",
3468 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3469 .desc = {
3470 {
3471 .name = "backend",
3472 .type = QEMU_OPT_STRING,
3473 },{
3474 .name = "path",
3475 .type = QEMU_OPT_STRING,
3476 },{
3477 .name = "host",
3478 .type = QEMU_OPT_STRING,
3479 },{
3480 .name = "port",
3481 .type = QEMU_OPT_STRING,
3482 },{
3483 .name = "localaddr",
3484 .type = QEMU_OPT_STRING,
3485 },{
3486 .name = "localport",
3487 .type = QEMU_OPT_STRING,
3488 },{
3489 .name = "to",
3490 .type = QEMU_OPT_NUMBER,
3491 },{
3492 .name = "ipv4",
3493 .type = QEMU_OPT_BOOL,
3494 },{
3495 .name = "ipv6",
3496 .type = QEMU_OPT_BOOL,
3497 },{
3498 .name = "wait",
3499 .type = QEMU_OPT_BOOL,
3500 },{
3501 .name = "server",
3502 .type = QEMU_OPT_BOOL,
3503 },{
3504 .name = "delay",
3505 .type = QEMU_OPT_BOOL,
3506 },{
3507 .name = "telnet",
3508 .type = QEMU_OPT_BOOL,
3509 },{
3510 .name = "width",
3511 .type = QEMU_OPT_NUMBER,
3512 },{
3513 .name = "height",
3514 .type = QEMU_OPT_NUMBER,
3515 },{
3516 .name = "cols",
3517 .type = QEMU_OPT_NUMBER,
3518 },{
3519 .name = "rows",
3520 .type = QEMU_OPT_NUMBER,
3521 },{
3522 .name = "mux",
3523 .type = QEMU_OPT_BOOL,
3524 },{
3525 .name = "signal",
3526 .type = QEMU_OPT_BOOL,
3527 },{
3528 .name = "name",
3529 .type = QEMU_OPT_STRING,
3530 },{
3531 .name = "debug",
3532 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003533 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003534 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003535 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003536 },
3537 { /* end of list */ }
3538 },
3539};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003540
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003541#ifdef _WIN32
3542
3543static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3544{
3545 HANDLE out;
3546
3547 if (file->in) {
3548 error_setg(errp, "input file not supported");
3549 return NULL;
3550 }
3551
3552 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3553 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3554 if (out == INVALID_HANDLE_VALUE) {
3555 error_setg(errp, "open %s failed", file->out);
3556 return NULL;
3557 }
3558 return qemu_chr_open_win_file(out);
3559}
3560
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003561static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3562 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003563{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003564 return qemu_chr_open_win_path(serial->device);
3565}
3566
3567static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3568 Error **errp)
3569{
3570 error_setg(errp, "character device backend type 'parallel' not supported");
3571 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003572}
3573
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003574#else /* WIN32 */
3575
3576static int qmp_chardev_open_file_source(char *src, int flags,
3577 Error **errp)
3578{
3579 int fd = -1;
3580
3581 TFR(fd = qemu_open(src, flags, 0666));
3582 if (fd == -1) {
3583 error_setg(errp, "open %s: %s", src, strerror(errno));
3584 }
3585 return fd;
3586}
3587
3588static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3589{
3590 int flags, in = -1, out = -1;
3591
3592 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3593 out = qmp_chardev_open_file_source(file->out, flags, errp);
3594 if (error_is_set(errp)) {
3595 return NULL;
3596 }
3597
3598 if (file->in) {
3599 flags = O_RDONLY;
3600 in = qmp_chardev_open_file_source(file->in, flags, errp);
3601 if (error_is_set(errp)) {
3602 qemu_close(out);
3603 return NULL;
3604 }
3605 }
3606
3607 return qemu_chr_open_fd(in, out);
3608}
3609
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003610static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3611 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003612{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003613#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003614 int fd;
3615
3616 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3617 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003618 return NULL;
3619 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003620 socket_set_nonblock(fd);
3621 return qemu_chr_open_tty_fd(fd);
3622#else
3623 error_setg(errp, "character device backend type 'serial' not supported");
3624 return NULL;
3625#endif
3626}
3627
3628static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3629 Error **errp)
3630{
3631#ifdef HAVE_CHARDEV_PARPORT
3632 int fd;
3633
3634 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3635 if (error_is_set(errp)) {
3636 return NULL;
3637 }
3638 return qemu_chr_open_pp_fd(fd);
3639#else
3640 error_setg(errp, "character device backend type 'parallel' not supported");
3641 return NULL;
3642#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003643}
3644
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003645#endif /* WIN32 */
3646
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003647static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3648 Error **errp)
3649{
3650 SocketAddress *addr = sock->addr;
3651 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3652 bool is_listen = sock->has_server ? sock->server : true;
3653 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3654 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3655 int fd;
3656
3657 if (is_listen) {
3658 fd = socket_listen(addr, errp);
3659 } else {
3660 fd = socket_connect(addr, errp, NULL, NULL);
3661 }
3662 if (error_is_set(errp)) {
3663 return NULL;
3664 }
3665 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3666 is_telnet, is_waitconnect, errp);
3667}
3668
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003669ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3670 Error **errp)
3671{
3672 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003673 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003674
3675 chr = qemu_chr_find(id);
3676 if (chr) {
3677 error_setg(errp, "Chardev '%s' already exists", id);
3678 g_free(ret);
3679 return NULL;
3680 }
3681
3682 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003683 case CHARDEV_BACKEND_KIND_FILE:
3684 chr = qmp_chardev_open_file(backend->file, errp);
3685 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003686 case CHARDEV_BACKEND_KIND_SERIAL:
3687 chr = qmp_chardev_open_serial(backend->serial, errp);
3688 break;
3689 case CHARDEV_BACKEND_KIND_PARALLEL:
3690 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003691 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003692 case CHARDEV_BACKEND_KIND_SOCKET:
3693 chr = qmp_chardev_open_socket(backend->socket, errp);
3694 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003695#ifdef HAVE_CHARDEV_TTY
3696 case CHARDEV_BACKEND_KIND_PTY:
3697 {
3698 /* qemu_chr_open_pty sets "path" in opts */
3699 QemuOpts *opts;
3700 opts = qemu_opts_create_nofail(qemu_find_opts("chardev"));
3701 chr = qemu_chr_open_pty(opts);
3702 ret->pty = g_strdup(qemu_opt_get(opts, "path"));
3703 ret->has_pty = true;
3704 qemu_opts_del(opts);
3705 break;
3706 }
3707#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003708 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003709 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003710 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003711 case CHARDEV_BACKEND_KIND_MUX:
3712 base = qemu_chr_find(backend->mux->chardev);
3713 if (base == NULL) {
3714 error_setg(errp, "mux: base chardev %s not found",
3715 backend->mux->chardev);
3716 break;
3717 }
3718 chr = qemu_chr_open_mux(base);
3719 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003720 case CHARDEV_BACKEND_KIND_MSMOUSE:
3721 chr = qemu_chr_open_msmouse();
3722 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003723#ifdef CONFIG_BRLAPI
3724 case CHARDEV_BACKEND_KIND_BRAILLE:
3725 chr = chr_baum_init();
3726 break;
3727#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003728 case CHARDEV_BACKEND_KIND_STDIO:
3729 chr = qemu_chr_open_stdio(backend->stdio);
3730 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003731 default:
3732 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3733 break;
3734 }
3735
3736 if (chr == NULL && !error_is_set(errp)) {
3737 error_setg(errp, "Failed to create chardev");
3738 }
3739 if (chr) {
3740 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003741 chr->avail_connections =
3742 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003743 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3744 return ret;
3745 } else {
3746 g_free(ret);
3747 return NULL;
3748 }
3749}
3750
3751void qmp_chardev_remove(const char *id, Error **errp)
3752{
3753 CharDriverState *chr;
3754
3755 chr = qemu_chr_find(id);
3756 if (NULL == chr) {
3757 error_setg(errp, "Chardev '%s' not found", id);
3758 return;
3759 }
3760 if (chr->chr_can_read || chr->chr_read ||
3761 chr->chr_event || chr->handler_opaque) {
3762 error_setg(errp, "Chardev '%s' is busy", id);
3763 return;
3764 }
3765 qemu_chr_delete(chr);
3766}
Anthony Liguorid654f342013-03-05 23:21:28 +05303767
3768static void register_types(void)
3769{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003770 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303771 register_char_driver("socket", qemu_chr_open_socket);
3772 register_char_driver("udp", qemu_chr_open_udp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303773 register_char_driver("memory", qemu_chr_open_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003774 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3775 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003776 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3777 qemu_chr_parse_stdio);
Anthony Liguorid654f342013-03-05 23:21:28 +05303778#ifdef _WIN32
Anthony Liguorid654f342013-03-05 23:21:28 +05303779 register_char_driver("pipe", qemu_chr_open_win_pipe);
3780 register_char_driver("console", qemu_chr_open_win_con);
3781 register_char_driver("serial", qemu_chr_open_win);
Anthony Liguorid654f342013-03-05 23:21:28 +05303782#else
Anthony Liguorid654f342013-03-05 23:21:28 +05303783 register_char_driver("pipe", qemu_chr_open_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303784#endif
Anthony Liguorid654f342013-03-05 23:21:28 +05303785#ifdef HAVE_CHARDEV_TTY
3786 register_char_driver("tty", qemu_chr_open_tty);
3787 register_char_driver("serial", qemu_chr_open_tty);
3788 register_char_driver("pty", qemu_chr_open_pty);
3789#endif
3790#ifdef HAVE_CHARDEV_PARPORT
3791 register_char_driver("parallel", qemu_chr_open_pp);
3792 register_char_driver("parport", qemu_chr_open_pp);
3793#endif
Anthony Liguorid654f342013-03-05 23:21:28 +05303794}
3795
3796type_init(register_types);