blob: 8fb11c834135325b9ecca46dc70f27efd3f9e2aa [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 Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/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>
Michael Tokarev3294ce12012-06-02 23:43:33 +040056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57#include <dev/ppbus/ppi.h>
58#include <dev/ppbus/ppbconf.h>
59#elif defined(__DragonFly__)
60#include <dev/misc/ppi/ppi.h>
61#include <bus/ppbus/ppbconf.h>
62#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010063#else
aliguori6f97dba2008-10-31 18:49:55 +000064#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000065#include <linux/ppdev.h>
66#include <linux/parport.h>
67#endif
68#ifdef __sun__
69#include <sys/stat.h>
70#include <sys/ethernet.h>
71#include <sys/sockio.h>
72#include <netinet/arp.h>
73#include <netinet/in.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_icmp.h> // must come after ip.h
77#include <netinet/udp.h>
78#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000079#endif
80#endif
81#endif
82
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010083#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020084#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000085
Amit Shah9bd78542009-11-03 19:59:54 +053086#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030087#define READ_RETRIES 10
Amit Shah9bd78542009-11-03 19:59:54 +053088
aliguori6f97dba2008-10-31 18:49:55 +000089/***********************************************************/
90/* character device */
91
Blue Swirl72cf2d42009-09-12 07:36:22 +000092static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
93 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000094
Hans de Goedea425d232011-11-19 10:22:43 +010095void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +000096{
Alexander Graf73cdf3f2010-04-01 18:42:39 +020097 /* Keep track if the char device is open */
98 switch (event) {
99 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100100 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200101 break;
102 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100103 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200104 break;
105 }
106
aliguori6f97dba2008-10-31 18:49:55 +0000107 if (!s->chr_event)
108 return;
109 s->chr_event(s->handler_opaque, event);
110}
111
Hans de Goedefee204f2013-03-26 11:07:54 +0100112void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000113{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500114 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000115}
116
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500117int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000118{
119 return s->chr_write(s, buf, len);
120}
121
Anthony Liguoricd187202013-03-26 10:04:17 -0500122int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
123{
124 int offset = 0;
125 int res;
126
127 while (offset < len) {
128 do {
129 res = s->chr_write(s, buf + offset, len - offset);
130 if (res == -1 && errno == EAGAIN) {
131 g_usleep(100);
132 }
133 } while (res == -1 && errno == EAGAIN);
134
135 if (res == 0) {
136 break;
137 }
138
139 if (res < 0) {
140 return res;
141 }
142
143 offset += res;
144 }
145
146 return offset;
147}
148
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300149int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
150{
151 int offset = 0, counter = 10;
152 int res;
153
154 if (!s->chr_sync_read) {
155 return 0;
156 }
157
158 while (offset < len) {
159 do {
160 res = s->chr_sync_read(s, buf + offset, len - offset);
161 if (res == -1 && errno == EAGAIN) {
162 g_usleep(100);
163 }
164 } while (res == -1 && errno == EAGAIN);
165
166 if (res == 0) {
167 break;
168 }
169
170 if (res < 0) {
171 return res;
172 }
173
174 offset += res;
175
176 if (!counter--) {
177 break;
178 }
179 }
180
181 return offset;
182}
183
Anthony Liguori41084f12011-08-15 11:17:34 -0500184int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000185{
186 if (!s->chr_ioctl)
187 return -ENOTSUP;
188 return s->chr_ioctl(s, cmd, arg);
189}
190
Anthony Liguori909cda12011-08-15 11:17:31 -0500191int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000192{
193 if (!s->chr_can_read)
194 return 0;
195 return s->chr_can_read(s->handler_opaque);
196}
197
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500198void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000199{
Stefan Weilac310732012-04-19 22:27:14 +0200200 if (s->chr_read) {
201 s->chr_read(s->handler_opaque, buf, len);
202 }
aliguori6f97dba2008-10-31 18:49:55 +0000203}
204
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500205int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100206{
207 return s->get_msgfd ? s->get_msgfd(s) : -1;
208}
209
Daniel P. Berrange13661082011-06-23 13:31:42 +0100210int qemu_chr_add_client(CharDriverState *s, int fd)
211{
212 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
213}
214
aliguori6f97dba2008-10-31 18:49:55 +0000215void qemu_chr_accept_input(CharDriverState *s)
216{
217 if (s->chr_accept_input)
218 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100219 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000220}
221
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500222void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000223{
Amit Shah9bd78542009-11-03 19:59:54 +0530224 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000225 va_list ap;
226 va_start(ap, fmt);
227 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500228 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000229 va_end(ap);
230}
231
Amit Shah386a5a12013-08-28 15:24:05 +0530232static void remove_fd_in_watch(CharDriverState *chr);
233
aliguori6f97dba2008-10-31 18:49:55 +0000234void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100235 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000236 IOReadHandler *fd_read,
237 IOEventHandler *fd_event,
238 void *opaque)
239{
Hans de Goede19083222013-03-26 11:07:56 +0100240 int fe_open;
241
Amit Shahda7d9982011-04-25 15:18:22 +0530242 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100243 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530244 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100245 } else {
246 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530247 }
aliguori6f97dba2008-10-31 18:49:55 +0000248 s->chr_can_read = fd_can_read;
249 s->chr_read = fd_read;
250 s->chr_event = fd_event;
251 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200252 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000253 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200254
Hans de Goede19083222013-03-26 11:07:56 +0100255 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100256 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100257 }
258
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200259 /* We're connecting to an already opened device, so let's make sure we
260 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100261 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100262 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200263 }
aliguori6f97dba2008-10-31 18:49:55 +0000264}
265
266static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
267{
268 return len;
269}
270
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100271static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000272{
273 CharDriverState *chr;
274
Anthony Liguori7267c092011-08-20 22:09:37 -0500275 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000276 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500277 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100278 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000279}
280
281/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000282#define MAX_MUX 4
283#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
284#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
285typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100286 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000287 IOReadHandler *chr_read[MAX_MUX];
288 IOEventHandler *chr_event[MAX_MUX];
289 void *ext_opaque[MAX_MUX];
290 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200291 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000292 int mux_cnt;
293 int term_got_escape;
294 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000295 /* Intermediate input buffer allows to catch escape sequences even if the
296 currently active device is not accepting any input - but only until it
297 is full as well. */
298 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
299 int prod[MAX_MUX];
300 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200301 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200302 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200303 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000304} MuxDriver;
305
306
307static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
308{
309 MuxDriver *d = chr->opaque;
310 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200311 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000312 ret = d->drv->chr_write(d->drv, buf, len);
313 } else {
314 int i;
315
316 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200317 for (i = 0; i < len; i++) {
318 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000319 char buf1[64];
320 int64_t ti;
321 int secs;
322
Alex Blighbc72ad62013-08-21 16:03:08 +0100323 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200324 if (d->timestamps_start == -1)
325 d->timestamps_start = ti;
326 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000327 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000328 snprintf(buf1, sizeof(buf1),
329 "[%02d:%02d:%02d.%03d] ",
330 secs / 3600,
331 (secs / 60) % 60,
332 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000333 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000334 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200335 d->linestart = 0;
336 }
337 ret += d->drv->chr_write(d->drv, buf+i, 1);
338 if (buf[i] == '\n') {
339 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000340 }
341 }
342 }
343 return ret;
344}
345
346static const char * const mux_help[] = {
347 "% h print this help\n\r",
348 "% x exit emulator\n\r",
349 "% s save disk data back to file (if -snapshot)\n\r",
350 "% t toggle console timestamps\n\r"
351 "% b send break (magic sysrq)\n\r",
352 "% c switch between console and monitor\n\r",
353 "% % sends %\n\r",
354 NULL
355};
356
357int term_escape_char = 0x01; /* ctrl-a is used for escape */
358static void mux_print_help(CharDriverState *chr)
359{
360 int i, j;
361 char ebuf[15] = "Escape-Char";
362 char cbuf[50] = "\n\r";
363
364 if (term_escape_char > 0 && term_escape_char < 26) {
365 snprintf(cbuf, sizeof(cbuf), "\n\r");
366 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
367 } else {
368 snprintf(cbuf, sizeof(cbuf),
369 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
370 term_escape_char);
371 }
372 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
373 for (i = 0; mux_help[i] != NULL; i++) {
374 for (j=0; mux_help[i][j] != '\0'; j++) {
375 if (mux_help[i][j] == '%')
376 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
377 else
378 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
379 }
380 }
381}
382
aliguori2724b182009-03-05 23:01:47 +0000383static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
384{
385 if (d->chr_event[mux_nr])
386 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
387}
388
aliguori6f97dba2008-10-31 18:49:55 +0000389static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
390{
391 if (d->term_got_escape) {
392 d->term_got_escape = 0;
393 if (ch == term_escape_char)
394 goto send_char;
395 switch(ch) {
396 case '?':
397 case 'h':
398 mux_print_help(chr);
399 break;
400 case 'x':
401 {
402 const char *term = "QEMU: Terminated\n\r";
403 chr->chr_write(chr,(uint8_t *)term,strlen(term));
404 exit(0);
405 break;
406 }
407 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200408 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000409 break;
410 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100411 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000412 break;
413 case 'c':
414 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200415 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
416 d->focus++;
417 if (d->focus >= d->mux_cnt)
418 d->focus = 0;
419 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000420 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200421 case 't':
422 d->timestamps = !d->timestamps;
423 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200424 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200425 break;
aliguori6f97dba2008-10-31 18:49:55 +0000426 }
427 } else if (ch == term_escape_char) {
428 d->term_got_escape = 1;
429 } else {
430 send_char:
431 return 1;
432 }
433 return 0;
434}
435
436static void mux_chr_accept_input(CharDriverState *chr)
437{
aliguori6f97dba2008-10-31 18:49:55 +0000438 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200439 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000440
aliguoria80bf992009-03-05 23:00:02 +0000441 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000442 d->chr_can_read[m] &&
443 d->chr_can_read[m](d->ext_opaque[m])) {
444 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000445 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000446 }
447}
448
449static int mux_chr_can_read(void *opaque)
450{
451 CharDriverState *chr = opaque;
452 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200453 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000454
aliguoria80bf992009-03-05 23:00:02 +0000455 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000456 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000457 if (d->chr_can_read[m])
458 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000459 return 0;
460}
461
462static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
463{
464 CharDriverState *chr = opaque;
465 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200466 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000467 int i;
468
469 mux_chr_accept_input (opaque);
470
471 for(i = 0; i < size; i++)
472 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000473 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000474 d->chr_can_read[m] &&
475 d->chr_can_read[m](d->ext_opaque[m]))
476 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
477 else
aliguoria80bf992009-03-05 23:00:02 +0000478 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000479 }
480}
481
482static void mux_chr_event(void *opaque, int event)
483{
484 CharDriverState *chr = opaque;
485 MuxDriver *d = chr->opaque;
486 int i;
487
488 /* Send the event to all registered listeners */
489 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000490 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000491}
492
493static void mux_chr_update_read_handler(CharDriverState *chr)
494{
495 MuxDriver *d = chr->opaque;
496
497 if (d->mux_cnt >= MAX_MUX) {
498 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
499 return;
500 }
501 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
502 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
503 d->chr_read[d->mux_cnt] = chr->chr_read;
504 d->chr_event[d->mux_cnt] = chr->chr_event;
505 /* Fix up the real driver with mux routines */
506 if (d->mux_cnt == 0) {
507 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
508 mux_chr_event, chr);
509 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200510 if (d->focus != -1) {
511 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200512 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200513 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000514 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200515 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000516}
517
Michael Roth7b7ab182013-07-30 13:04:22 -0500518static bool muxes_realized;
519
520/**
521 * Called after processing of default and command-line-specified
522 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
523 * to a mux chardev. This is done here to ensure that
524 * output/prompts/banners are only displayed for the FE that has
525 * focus when initial command-line processing/machine init is
526 * completed.
527 *
528 * After this point, any new FE attached to any new or existing
529 * mux will receive CHR_EVENT_OPENED notifications for the BE
530 * immediately.
531 */
532static void muxes_realize_done(Notifier *notifier, void *unused)
533{
534 CharDriverState *chr;
535
536 QTAILQ_FOREACH(chr, &chardevs, next) {
537 if (chr->is_mux) {
538 MuxDriver *d = chr->opaque;
539 int i;
540
541 /* send OPENED to all already-attached FEs */
542 for (i = 0; i < d->mux_cnt; i++) {
543 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
544 }
545 /* mark mux as OPENED so any new FEs will immediately receive
546 * OPENED event
547 */
548 qemu_chr_be_generic_open(chr);
549 }
550 }
551 muxes_realized = true;
552}
553
554static Notifier muxes_realize_notify = {
555 .notify = muxes_realize_done,
556};
557
aliguori6f97dba2008-10-31 18:49:55 +0000558static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
559{
560 CharDriverState *chr;
561 MuxDriver *d;
562
Anthony Liguori7267c092011-08-20 22:09:37 -0500563 chr = g_malloc0(sizeof(CharDriverState));
564 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000565
566 chr->opaque = d;
567 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200568 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000569 chr->chr_write = mux_chr_write;
570 chr->chr_update_read_handler = mux_chr_update_read_handler;
571 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100572 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100573 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500574 /* only default to opened state if we've realized the initial
575 * set of muxes
576 */
577 chr->explicit_be_open = muxes_realized ? 0 : 1;
578 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200579
aliguori6f97dba2008-10-31 18:49:55 +0000580 return chr;
581}
582
583
584#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000585int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000586{
587 int ret, len;
588
589 len = len1;
590 while (len > 0) {
591 ret = send(fd, buf, len, 0);
592 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000593 errno = WSAGetLastError();
594 if (errno != WSAEWOULDBLOCK) {
595 return -1;
596 }
597 } else if (ret == 0) {
598 break;
599 } else {
600 buf += ret;
601 len -= ret;
602 }
603 }
604 return len1 - len;
605}
606
607#else
608
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100609int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000610{
611 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100612 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000613
614 len = len1;
615 while (len > 0) {
616 ret = write(fd, buf, len);
617 if (ret < 0) {
618 if (errno != EINTR && errno != EAGAIN)
619 return -1;
620 } else if (ret == 0) {
621 break;
622 } else {
623 buf += ret;
624 len -= ret;
625 }
626 }
627 return len1 - len;
628}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500629
630int recv_all(int fd, void *_buf, int len1, bool single_read)
631{
632 int ret, len;
633 uint8_t *buf = _buf;
634
635 len = len1;
636 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
637 if (ret < 0) {
638 if (errno != EINTR && errno != EAGAIN) {
639 return -1;
640 }
641 continue;
642 } else {
643 if (single_read) {
644 return ret;
645 }
646 buf += ret;
647 len -= ret;
648 }
649 }
650 return len1 - len;
651}
652
aliguori6f97dba2008-10-31 18:49:55 +0000653#endif /* !_WIN32 */
654
Anthony Liguori96c63842013-03-05 23:21:18 +0530655typedef struct IOWatchPoll
656{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200657 GSource parent;
658
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200659 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530660 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530661
662 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200663 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530664 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530665} IOWatchPoll;
666
Anthony Liguori96c63842013-03-05 23:21:18 +0530667static IOWatchPoll *io_watch_poll_from_source(GSource *source)
668{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200669 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530670}
671
672static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
673{
674 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200675 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200676 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200677 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530678 return FALSE;
679 }
680
Paolo Bonzinid185c092013-04-05 17:59:33 +0200681 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200682 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
683 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200684 g_source_attach(iwp->src, NULL);
685 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200686 g_source_destroy(iwp->src);
687 g_source_unref(iwp->src);
688 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200689 }
690 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530691}
692
693static gboolean io_watch_poll_check(GSource *source)
694{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200695 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530696}
697
698static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
699 gpointer user_data)
700{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200701 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530702}
703
704static void io_watch_poll_finalize(GSource *source)
705{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200706 /* Due to a glib bug, removing the last reference to a source
707 * inside a finalize callback causes recursive locking (and a
708 * deadlock). This is not a problem inside other callbacks,
709 * including dispatch callbacks, so we call io_remove_watch_poll
710 * to remove this source. At this point, iwp->src must
711 * be NULL, or we would leak it.
712 *
713 * This would be solved much more elegantly by child sources,
714 * but we support older glib versions that do not have them.
715 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530716 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200717 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530718}
719
720static GSourceFuncs io_watch_poll_funcs = {
721 .prepare = io_watch_poll_prepare,
722 .check = io_watch_poll_check,
723 .dispatch = io_watch_poll_dispatch,
724 .finalize = io_watch_poll_finalize,
725};
726
727/* Can only be used for read */
728static guint io_add_watch_poll(GIOChannel *channel,
729 IOCanReadHandler *fd_can_read,
730 GIOFunc fd_read,
731 gpointer user_data)
732{
733 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200734 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530735
Paolo Bonzinid185c092013-04-05 17:59:33 +0200736 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530737 iwp->fd_can_read = fd_can_read;
738 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200739 iwp->channel = channel;
740 iwp->fd_read = (GSourceFunc) fd_read;
741 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530742
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200743 tag = g_source_attach(&iwp->parent, NULL);
744 g_source_unref(&iwp->parent);
745 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530746}
747
Paolo Bonzini2b316772013-04-19 17:32:09 +0200748static void io_remove_watch_poll(guint tag)
749{
750 GSource *source;
751 IOWatchPoll *iwp;
752
753 g_return_if_fail (tag > 0);
754
755 source = g_main_context_find_source_by_id(NULL, tag);
756 g_return_if_fail (source != NULL);
757
758 iwp = io_watch_poll_from_source(source);
759 if (iwp->src) {
760 g_source_destroy(iwp->src);
761 g_source_unref(iwp->src);
762 iwp->src = NULL;
763 }
764 g_source_destroy(&iwp->parent);
765}
766
Amit Shah26da70c2013-08-28 15:23:37 +0530767static void remove_fd_in_watch(CharDriverState *chr)
768{
769 if (chr->fd_in_tag) {
770 io_remove_watch_poll(chr->fd_in_tag);
771 chr->fd_in_tag = 0;
772 }
773}
774
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000775#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530776static GIOChannel *io_channel_from_fd(int fd)
777{
778 GIOChannel *chan;
779
780 if (fd == -1) {
781 return NULL;
782 }
783
784 chan = g_io_channel_unix_new(fd);
785
786 g_io_channel_set_encoding(chan, NULL, NULL);
787 g_io_channel_set_buffered(chan, FALSE);
788
789 return chan;
790}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000791#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530792
Anthony Liguori76a96442013-03-05 23:21:21 +0530793static GIOChannel *io_channel_from_socket(int fd)
794{
795 GIOChannel *chan;
796
797 if (fd == -1) {
798 return NULL;
799 }
800
801#ifdef _WIN32
802 chan = g_io_channel_win32_new_socket(fd);
803#else
804 chan = g_io_channel_unix_new(fd);
805#endif
806
807 g_io_channel_set_encoding(chan, NULL, NULL);
808 g_io_channel_set_buffered(chan, FALSE);
809
810 return chan;
811}
812
Anthony Liguori684a0962013-03-29 11:39:50 -0500813static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530814{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200815 size_t offset = 0;
816 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530817
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200818 while (offset < len && status == G_IO_STATUS_NORMAL) {
819 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500820
821 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530822 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500823 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530824 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500825
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200826 if (offset > 0) {
827 return offset;
828 }
829 switch (status) {
830 case G_IO_STATUS_NORMAL:
831 g_assert(len == 0);
832 return 0;
833 case G_IO_STATUS_AGAIN:
834 errno = EAGAIN;
835 return -1;
836 default:
837 break;
838 }
839 errno = EINVAL;
840 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530841}
Anthony Liguori96c63842013-03-05 23:21:18 +0530842
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000843#ifndef _WIN32
844
Anthony Liguoria29753f2013-03-05 23:21:19 +0530845typedef struct FDCharDriver {
846 CharDriverState *chr;
847 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000848 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530849 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000850} FDCharDriver;
851
aliguori6f97dba2008-10-31 18:49:55 +0000852static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
853{
854 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530855
Anthony Liguori684a0962013-03-29 11:39:50 -0500856 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530857}
858
859static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
860{
861 CharDriverState *chr = opaque;
862 FDCharDriver *s = chr->opaque;
863 int len;
864 uint8_t buf[READ_BUF_LEN];
865 GIOStatus status;
866 gsize bytes_read;
867
868 len = sizeof(buf);
869 if (len > s->max_size) {
870 len = s->max_size;
871 }
872 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200873 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530874 }
875
876 status = g_io_channel_read_chars(chan, (gchar *)buf,
877 len, &bytes_read, NULL);
878 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530879 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530880 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
881 return FALSE;
882 }
883 if (status == G_IO_STATUS_NORMAL) {
884 qemu_chr_be_write(chr, buf, bytes_read);
885 }
886
887 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000888}
889
890static int fd_chr_read_poll(void *opaque)
891{
892 CharDriverState *chr = opaque;
893 FDCharDriver *s = chr->opaque;
894
Anthony Liguori909cda12011-08-15 11:17:31 -0500895 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000896 return s->max_size;
897}
898
Anthony Liguori23673ca2013-03-05 23:21:23 +0530899static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
900{
901 FDCharDriver *s = chr->opaque;
902 return g_io_create_watch(s->fd_out, cond);
903}
904
aliguori6f97dba2008-10-31 18:49:55 +0000905static void fd_chr_update_read_handler(CharDriverState *chr)
906{
907 FDCharDriver *s = chr->opaque;
908
Amit Shah26da70c2013-08-28 15:23:37 +0530909 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530910 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530911 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
912 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000913 }
914}
915
916static void fd_chr_close(struct CharDriverState *chr)
917{
918 FDCharDriver *s = chr->opaque;
919
Amit Shah26da70c2013-08-28 15:23:37 +0530920 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530921 if (s->fd_in) {
922 g_io_channel_unref(s->fd_in);
923 }
924 if (s->fd_out) {
925 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000926 }
927
Anthony Liguori7267c092011-08-20 22:09:37 -0500928 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100929 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000930}
931
932/* open a character device to a unix fd */
933static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
934{
935 CharDriverState *chr;
936 FDCharDriver *s;
937
Anthony Liguori7267c092011-08-20 22:09:37 -0500938 chr = g_malloc0(sizeof(CharDriverState));
939 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530940 s->fd_in = io_channel_from_fd(fd_in);
941 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530942 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530943 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000944 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530945 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000946 chr->chr_write = fd_chr_write;
947 chr->chr_update_read_handler = fd_chr_update_read_handler;
948 chr->chr_close = fd_chr_close;
949
aliguori6f97dba2008-10-31 18:49:55 +0000950 return chr;
951}
952
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100953static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000954{
955 int fd_in, fd_out;
956 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100957 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200958
959 if (filename == NULL) {
960 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100961 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200962 }
aliguori6f97dba2008-10-31 18:49:55 +0000963
964 snprintf(filename_in, 256, "%s.in", filename);
965 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100966 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
967 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000968 if (fd_in < 0 || fd_out < 0) {
969 if (fd_in >= 0)
970 close(fd_in);
971 if (fd_out >= 0)
972 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100973 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100974 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100975 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100976 }
aliguori6f97dba2008-10-31 18:49:55 +0000977 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100978 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000979}
980
aliguori6f97dba2008-10-31 18:49:55 +0000981/* init terminal so that we can grab keys */
982static struct termios oldtty;
983static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100984static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000985
986static void term_exit(void)
987{
988 tcsetattr (0, TCSANOW, &oldtty);
989 fcntl(0, F_SETFL, old_fd0_flags);
990}
991
Paolo Bonzinibb002512010-12-23 13:42:50 +0100992static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000993{
994 struct termios tty;
995
Paolo Bonzini03693642010-12-23 13:42:49 +0100996 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100997 if (!echo) {
998 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000999 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001000 tty.c_oflag |= OPOST;
1001 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1002 tty.c_cflag &= ~(CSIZE|PARENB);
1003 tty.c_cflag |= CS8;
1004 tty.c_cc[VMIN] = 1;
1005 tty.c_cc[VTIME] = 0;
1006 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001007 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001008 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001009
1010 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001011}
1012
1013static void qemu_chr_close_stdio(struct CharDriverState *chr)
1014{
1015 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001016 fd_chr_close(chr);
1017}
1018
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001019static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001020{
1021 CharDriverState *chr;
1022
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001023 if (is_daemonized()) {
1024 error_report("cannot use stdio with -daemonize");
1025 return NULL;
1026 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301027 old_fd0_flags = fcntl(0, F_GETFL);
1028 tcgetattr (0, &oldtty);
1029 fcntl(0, F_SETFL, O_NONBLOCK);
1030 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001031
aliguori6f97dba2008-10-31 18:49:55 +00001032 chr = qemu_chr_open_fd(0, 1);
1033 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001034 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001035 if (opts->has_signal) {
1036 stdio_allow_signal = opts->signal;
1037 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001038 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001039
Markus Armbruster1f514702012-02-07 15:09:08 +01001040 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001041}
1042
aliguori6f97dba2008-10-31 18:49:55 +00001043#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001044 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1045 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001046
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001047#define HAVE_CHARDEV_TTY 1
1048
aliguori6f97dba2008-10-31 18:49:55 +00001049typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301050 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001051 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001052 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301053 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001054} PtyCharDriver;
1055
1056static void pty_chr_update_read_handler(CharDriverState *chr);
1057static void pty_chr_state(CharDriverState *chr, int connected);
1058
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301059static gboolean pty_chr_timer(gpointer opaque)
1060{
1061 struct CharDriverState *chr = opaque;
1062 PtyCharDriver *s = chr->opaque;
1063
Hans de Goede79f20072013-04-25 13:53:02 +02001064 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001065 if (!s->connected) {
1066 /* Next poll ... */
1067 pty_chr_update_read_handler(chr);
1068 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301069 return FALSE;
1070}
1071
1072static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1073{
1074 PtyCharDriver *s = chr->opaque;
1075
1076 if (s->timer_tag) {
1077 g_source_remove(s->timer_tag);
1078 s->timer_tag = 0;
1079 }
1080
1081 if (ms == 1000) {
1082 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1083 } else {
1084 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1085 }
1086}
1087
aliguori6f97dba2008-10-31 18:49:55 +00001088static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1089{
1090 PtyCharDriver *s = chr->opaque;
1091
1092 if (!s->connected) {
1093 /* guest sends data, check for (re-)connect */
1094 pty_chr_update_read_handler(chr);
1095 return 0;
1096 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001097 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001098}
1099
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301100static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1101{
1102 PtyCharDriver *s = chr->opaque;
1103 return g_io_create_watch(s->fd, cond);
1104}
1105
aliguori6f97dba2008-10-31 18:49:55 +00001106static int pty_chr_read_poll(void *opaque)
1107{
1108 CharDriverState *chr = opaque;
1109 PtyCharDriver *s = chr->opaque;
1110
Anthony Liguori909cda12011-08-15 11:17:31 -05001111 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001112 return s->read_bytes;
1113}
1114
Anthony Liguori093d3a22013-03-05 23:21:20 +05301115static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001116{
1117 CharDriverState *chr = opaque;
1118 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301119 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301120 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301121 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001122
1123 len = sizeof(buf);
1124 if (len > s->read_bytes)
1125 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001126 if (len == 0) {
1127 return TRUE;
1128 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301129 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1130 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001131 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301132 return FALSE;
1133 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001134 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001135 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001136 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301137 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001138}
1139
1140static void pty_chr_update_read_handler(CharDriverState *chr)
1141{
1142 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001143 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001144
Paolo Bonzini85a67692013-04-19 17:32:07 +02001145 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1146 pfd.events = G_IO_OUT;
1147 pfd.revents = 0;
1148 g_poll(&pfd, 1, 0);
1149 if (pfd.revents & G_IO_HUP) {
1150 pty_chr_state(chr, 0);
1151 } else {
1152 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301153 }
aliguori6f97dba2008-10-31 18:49:55 +00001154}
1155
1156static void pty_chr_state(CharDriverState *chr, int connected)
1157{
1158 PtyCharDriver *s = chr->opaque;
1159
1160 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301161 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001162 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001163 /* (re-)connect poll interval for idle guests: once per second.
1164 * We check more frequently in case the guests sends data to
1165 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301166 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001167 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001168 if (s->timer_tag) {
1169 g_source_remove(s->timer_tag);
1170 s->timer_tag = 0;
1171 }
1172 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001173 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001174 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001175 }
1176 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301177 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1178 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001179 }
aliguori6f97dba2008-10-31 18:49:55 +00001180 }
1181}
1182
aliguori6f97dba2008-10-31 18:49:55 +00001183static void pty_chr_close(struct CharDriverState *chr)
1184{
1185 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301186 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001187
Amit Shah26da70c2013-08-28 15:23:37 +05301188 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301189 fd = g_io_channel_unix_get_fd(s->fd);
1190 g_io_channel_unref(s->fd);
1191 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301192 if (s->timer_tag) {
1193 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001194 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301195 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001196 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001197 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001198}
1199
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001200static CharDriverState *qemu_chr_open_pty(const char *id,
1201 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001202{
1203 CharDriverState *chr;
1204 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001205 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001206 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001207
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001208 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1209 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001210 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001211 }
1212
aliguori6f97dba2008-10-31 18:49:55 +00001213 close(slave_fd);
1214
Markus Armbrustera4e26042011-11-11 10:40:05 +01001215 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001216
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001217 chr->filename = g_strdup_printf("pty:%s", pty_name);
1218 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001219 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001220
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001221 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001222 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001223
1224 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001225 chr->opaque = s;
1226 chr->chr_write = pty_chr_write;
1227 chr->chr_update_read_handler = pty_chr_update_read_handler;
1228 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301229 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001230 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001231
Anthony Liguori093d3a22013-03-05 23:21:20 +05301232 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301233 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001234
Markus Armbruster1f514702012-02-07 15:09:08 +01001235 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001236}
1237
1238static void tty_serial_init(int fd, int speed,
1239 int parity, int data_bits, int stop_bits)
1240{
1241 struct termios tty;
1242 speed_t spd;
1243
1244#if 0
1245 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1246 speed, parity, data_bits, stop_bits);
1247#endif
1248 tcgetattr (fd, &tty);
1249
Stefan Weil45eea132009-10-26 16:10:10 +01001250#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1251 speed = speed * 10 / 11;
1252 do {
1253 check_speed(50);
1254 check_speed(75);
1255 check_speed(110);
1256 check_speed(134);
1257 check_speed(150);
1258 check_speed(200);
1259 check_speed(300);
1260 check_speed(600);
1261 check_speed(1200);
1262 check_speed(1800);
1263 check_speed(2400);
1264 check_speed(4800);
1265 check_speed(9600);
1266 check_speed(19200);
1267 check_speed(38400);
1268 /* Non-Posix values follow. They may be unsupported on some systems. */
1269 check_speed(57600);
1270 check_speed(115200);
1271#ifdef B230400
1272 check_speed(230400);
1273#endif
1274#ifdef B460800
1275 check_speed(460800);
1276#endif
1277#ifdef B500000
1278 check_speed(500000);
1279#endif
1280#ifdef B576000
1281 check_speed(576000);
1282#endif
1283#ifdef B921600
1284 check_speed(921600);
1285#endif
1286#ifdef B1000000
1287 check_speed(1000000);
1288#endif
1289#ifdef B1152000
1290 check_speed(1152000);
1291#endif
1292#ifdef B1500000
1293 check_speed(1500000);
1294#endif
1295#ifdef B2000000
1296 check_speed(2000000);
1297#endif
1298#ifdef B2500000
1299 check_speed(2500000);
1300#endif
1301#ifdef B3000000
1302 check_speed(3000000);
1303#endif
1304#ifdef B3500000
1305 check_speed(3500000);
1306#endif
1307#ifdef B4000000
1308 check_speed(4000000);
1309#endif
aliguori6f97dba2008-10-31 18:49:55 +00001310 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001311 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001312
1313 cfsetispeed(&tty, spd);
1314 cfsetospeed(&tty, spd);
1315
1316 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1317 |INLCR|IGNCR|ICRNL|IXON);
1318 tty.c_oflag |= OPOST;
1319 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1320 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1321 switch(data_bits) {
1322 default:
1323 case 8:
1324 tty.c_cflag |= CS8;
1325 break;
1326 case 7:
1327 tty.c_cflag |= CS7;
1328 break;
1329 case 6:
1330 tty.c_cflag |= CS6;
1331 break;
1332 case 5:
1333 tty.c_cflag |= CS5;
1334 break;
1335 }
1336 switch(parity) {
1337 default:
1338 case 'N':
1339 break;
1340 case 'E':
1341 tty.c_cflag |= PARENB;
1342 break;
1343 case 'O':
1344 tty.c_cflag |= PARENB | PARODD;
1345 break;
1346 }
1347 if (stop_bits == 2)
1348 tty.c_cflag |= CSTOPB;
1349
1350 tcsetattr (fd, TCSANOW, &tty);
1351}
1352
1353static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1354{
1355 FDCharDriver *s = chr->opaque;
1356
1357 switch(cmd) {
1358 case CHR_IOCTL_SERIAL_SET_PARAMS:
1359 {
1360 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301361 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1362 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001363 ssp->data_bits, ssp->stop_bits);
1364 }
1365 break;
1366 case CHR_IOCTL_SERIAL_SET_BREAK:
1367 {
1368 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301369 if (enable) {
1370 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1371 }
aliguori6f97dba2008-10-31 18:49:55 +00001372 }
1373 break;
1374 case CHR_IOCTL_SERIAL_GET_TIOCM:
1375 {
1376 int sarg = 0;
1377 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301378 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001379 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001380 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001381 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001382 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001383 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001384 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001385 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001386 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001387 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001388 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001389 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001390 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001391 *targ |= CHR_TIOCM_RTS;
1392 }
1393 break;
1394 case CHR_IOCTL_SERIAL_SET_TIOCM:
1395 {
1396 int sarg = *(int *)arg;
1397 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301398 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001399 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1400 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1401 if (sarg & CHR_TIOCM_CTS)
1402 targ |= TIOCM_CTS;
1403 if (sarg & CHR_TIOCM_CAR)
1404 targ |= TIOCM_CAR;
1405 if (sarg & CHR_TIOCM_DSR)
1406 targ |= TIOCM_DSR;
1407 if (sarg & CHR_TIOCM_RI)
1408 targ |= TIOCM_RI;
1409 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001410 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001411 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001412 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301413 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001414 }
1415 break;
1416 default:
1417 return -ENOTSUP;
1418 }
1419 return 0;
1420}
1421
David Ahern4266a132010-02-10 18:27:17 -07001422static void qemu_chr_close_tty(CharDriverState *chr)
1423{
1424 FDCharDriver *s = chr->opaque;
1425 int fd = -1;
1426
1427 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301428 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001429 }
1430
1431 fd_chr_close(chr);
1432
1433 if (fd >= 0) {
1434 close(fd);
1435 }
1436}
1437
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001438static CharDriverState *qemu_chr_open_tty_fd(int fd)
1439{
1440 CharDriverState *chr;
1441
1442 tty_serial_init(fd, 115200, 'N', 8, 1);
1443 chr = qemu_chr_open_fd(fd, fd);
1444 chr->chr_ioctl = tty_serial_ioctl;
1445 chr->chr_close = qemu_chr_close_tty;
1446 return chr;
1447}
aliguori6f97dba2008-10-31 18:49:55 +00001448#endif /* __linux__ || __sun__ */
1449
1450#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001451
1452#define HAVE_CHARDEV_PARPORT 1
1453
aliguori6f97dba2008-10-31 18:49:55 +00001454typedef struct {
1455 int fd;
1456 int mode;
1457} ParallelCharDriver;
1458
1459static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1460{
1461 if (s->mode != mode) {
1462 int m = mode;
1463 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1464 return 0;
1465 s->mode = mode;
1466 }
1467 return 1;
1468}
1469
1470static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1471{
1472 ParallelCharDriver *drv = chr->opaque;
1473 int fd = drv->fd;
1474 uint8_t b;
1475
1476 switch(cmd) {
1477 case CHR_IOCTL_PP_READ_DATA:
1478 if (ioctl(fd, PPRDATA, &b) < 0)
1479 return -ENOTSUP;
1480 *(uint8_t *)arg = b;
1481 break;
1482 case CHR_IOCTL_PP_WRITE_DATA:
1483 b = *(uint8_t *)arg;
1484 if (ioctl(fd, PPWDATA, &b) < 0)
1485 return -ENOTSUP;
1486 break;
1487 case CHR_IOCTL_PP_READ_CONTROL:
1488 if (ioctl(fd, PPRCONTROL, &b) < 0)
1489 return -ENOTSUP;
1490 /* Linux gives only the lowest bits, and no way to know data
1491 direction! For better compatibility set the fixed upper
1492 bits. */
1493 *(uint8_t *)arg = b | 0xc0;
1494 break;
1495 case CHR_IOCTL_PP_WRITE_CONTROL:
1496 b = *(uint8_t *)arg;
1497 if (ioctl(fd, PPWCONTROL, &b) < 0)
1498 return -ENOTSUP;
1499 break;
1500 case CHR_IOCTL_PP_READ_STATUS:
1501 if (ioctl(fd, PPRSTATUS, &b) < 0)
1502 return -ENOTSUP;
1503 *(uint8_t *)arg = b;
1504 break;
1505 case CHR_IOCTL_PP_DATA_DIR:
1506 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1507 return -ENOTSUP;
1508 break;
1509 case CHR_IOCTL_PP_EPP_READ_ADDR:
1510 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1511 struct ParallelIOArg *parg = arg;
1512 int n = read(fd, parg->buffer, parg->count);
1513 if (n != parg->count) {
1514 return -EIO;
1515 }
1516 }
1517 break;
1518 case CHR_IOCTL_PP_EPP_READ:
1519 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1520 struct ParallelIOArg *parg = arg;
1521 int n = read(fd, parg->buffer, parg->count);
1522 if (n != parg->count) {
1523 return -EIO;
1524 }
1525 }
1526 break;
1527 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1528 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1529 struct ParallelIOArg *parg = arg;
1530 int n = write(fd, parg->buffer, parg->count);
1531 if (n != parg->count) {
1532 return -EIO;
1533 }
1534 }
1535 break;
1536 case CHR_IOCTL_PP_EPP_WRITE:
1537 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1538 struct ParallelIOArg *parg = arg;
1539 int n = write(fd, parg->buffer, parg->count);
1540 if (n != parg->count) {
1541 return -EIO;
1542 }
1543 }
1544 break;
1545 default:
1546 return -ENOTSUP;
1547 }
1548 return 0;
1549}
1550
1551static void pp_close(CharDriverState *chr)
1552{
1553 ParallelCharDriver *drv = chr->opaque;
1554 int fd = drv->fd;
1555
1556 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1557 ioctl(fd, PPRELEASE);
1558 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001559 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001560 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001561}
1562
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001563static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001564{
1565 CharDriverState *chr;
1566 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001567
1568 if (ioctl(fd, PPCLAIM) < 0) {
1569 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001570 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001571 }
1572
Anthony Liguori7267c092011-08-20 22:09:37 -05001573 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001574 drv->fd = fd;
1575 drv->mode = IEEE1284_MODE_COMPAT;
1576
Anthony Liguori7267c092011-08-20 22:09:37 -05001577 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001578 chr->chr_write = null_chr_write;
1579 chr->chr_ioctl = pp_ioctl;
1580 chr->chr_close = pp_close;
1581 chr->opaque = drv;
1582
Markus Armbruster1f514702012-02-07 15:09:08 +01001583 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001584}
1585#endif /* __linux__ */
1586
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001587#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001588
1589#define HAVE_CHARDEV_PARPORT 1
1590
blueswir16972f932008-11-22 20:49:12 +00001591static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1592{
Stefan Weile0efb992011-02-23 19:09:16 +01001593 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001594 uint8_t b;
1595
1596 switch(cmd) {
1597 case CHR_IOCTL_PP_READ_DATA:
1598 if (ioctl(fd, PPIGDATA, &b) < 0)
1599 return -ENOTSUP;
1600 *(uint8_t *)arg = b;
1601 break;
1602 case CHR_IOCTL_PP_WRITE_DATA:
1603 b = *(uint8_t *)arg;
1604 if (ioctl(fd, PPISDATA, &b) < 0)
1605 return -ENOTSUP;
1606 break;
1607 case CHR_IOCTL_PP_READ_CONTROL:
1608 if (ioctl(fd, PPIGCTRL, &b) < 0)
1609 return -ENOTSUP;
1610 *(uint8_t *)arg = b;
1611 break;
1612 case CHR_IOCTL_PP_WRITE_CONTROL:
1613 b = *(uint8_t *)arg;
1614 if (ioctl(fd, PPISCTRL, &b) < 0)
1615 return -ENOTSUP;
1616 break;
1617 case CHR_IOCTL_PP_READ_STATUS:
1618 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1619 return -ENOTSUP;
1620 *(uint8_t *)arg = b;
1621 break;
1622 default:
1623 return -ENOTSUP;
1624 }
1625 return 0;
1626}
1627
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001628static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001629{
1630 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001631
Anthony Liguori7267c092011-08-20 22:09:37 -05001632 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001633 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001634 chr->chr_write = null_chr_write;
1635 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001636 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001637 return chr;
blueswir16972f932008-11-22 20:49:12 +00001638}
1639#endif
1640
aliguori6f97dba2008-10-31 18:49:55 +00001641#else /* _WIN32 */
1642
1643typedef struct {
1644 int max_size;
1645 HANDLE hcom, hrecv, hsend;
1646 OVERLAPPED orecv, osend;
1647 BOOL fpipe;
1648 DWORD len;
1649} WinCharState;
1650
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001651typedef struct {
1652 HANDLE hStdIn;
1653 HANDLE hInputReadyEvent;
1654 HANDLE hInputDoneEvent;
1655 HANDLE hInputThread;
1656 uint8_t win_stdio_buf;
1657} WinStdioCharState;
1658
aliguori6f97dba2008-10-31 18:49:55 +00001659#define NSENDBUF 2048
1660#define NRECVBUF 2048
1661#define MAXCONNECT 1
1662#define NTIMEOUT 5000
1663
1664static int win_chr_poll(void *opaque);
1665static int win_chr_pipe_poll(void *opaque);
1666
1667static void win_chr_close(CharDriverState *chr)
1668{
1669 WinCharState *s = chr->opaque;
1670
1671 if (s->hsend) {
1672 CloseHandle(s->hsend);
1673 s->hsend = NULL;
1674 }
1675 if (s->hrecv) {
1676 CloseHandle(s->hrecv);
1677 s->hrecv = NULL;
1678 }
1679 if (s->hcom) {
1680 CloseHandle(s->hcom);
1681 s->hcom = NULL;
1682 }
1683 if (s->fpipe)
1684 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1685 else
1686 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301687
Hans de Goedea425d232011-11-19 10:22:43 +01001688 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001689}
1690
1691static int win_chr_init(CharDriverState *chr, const char *filename)
1692{
1693 WinCharState *s = chr->opaque;
1694 COMMCONFIG comcfg;
1695 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1696 COMSTAT comstat;
1697 DWORD size;
1698 DWORD err;
1699
1700 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1701 if (!s->hsend) {
1702 fprintf(stderr, "Failed CreateEvent\n");
1703 goto fail;
1704 }
1705 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1706 if (!s->hrecv) {
1707 fprintf(stderr, "Failed CreateEvent\n");
1708 goto fail;
1709 }
1710
1711 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1712 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1713 if (s->hcom == INVALID_HANDLE_VALUE) {
1714 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1715 s->hcom = NULL;
1716 goto fail;
1717 }
1718
1719 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1720 fprintf(stderr, "Failed SetupComm\n");
1721 goto fail;
1722 }
1723
1724 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1725 size = sizeof(COMMCONFIG);
1726 GetDefaultCommConfig(filename, &comcfg, &size);
1727 comcfg.dcb.DCBlength = sizeof(DCB);
1728 CommConfigDialog(filename, NULL, &comcfg);
1729
1730 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1731 fprintf(stderr, "Failed SetCommState\n");
1732 goto fail;
1733 }
1734
1735 if (!SetCommMask(s->hcom, EV_ERR)) {
1736 fprintf(stderr, "Failed SetCommMask\n");
1737 goto fail;
1738 }
1739
1740 cto.ReadIntervalTimeout = MAXDWORD;
1741 if (!SetCommTimeouts(s->hcom, &cto)) {
1742 fprintf(stderr, "Failed SetCommTimeouts\n");
1743 goto fail;
1744 }
1745
1746 if (!ClearCommError(s->hcom, &err, &comstat)) {
1747 fprintf(stderr, "Failed ClearCommError\n");
1748 goto fail;
1749 }
1750 qemu_add_polling_cb(win_chr_poll, chr);
1751 return 0;
1752
1753 fail:
1754 win_chr_close(chr);
1755 return -1;
1756}
1757
1758static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1759{
1760 WinCharState *s = chr->opaque;
1761 DWORD len, ret, size, err;
1762
1763 len = len1;
1764 ZeroMemory(&s->osend, sizeof(s->osend));
1765 s->osend.hEvent = s->hsend;
1766 while (len > 0) {
1767 if (s->hsend)
1768 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1769 else
1770 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1771 if (!ret) {
1772 err = GetLastError();
1773 if (err == ERROR_IO_PENDING) {
1774 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1775 if (ret) {
1776 buf += size;
1777 len -= size;
1778 } else {
1779 break;
1780 }
1781 } else {
1782 break;
1783 }
1784 } else {
1785 buf += size;
1786 len -= size;
1787 }
1788 }
1789 return len1 - len;
1790}
1791
1792static int win_chr_read_poll(CharDriverState *chr)
1793{
1794 WinCharState *s = chr->opaque;
1795
Anthony Liguori909cda12011-08-15 11:17:31 -05001796 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001797 return s->max_size;
1798}
1799
1800static void win_chr_readfile(CharDriverState *chr)
1801{
1802 WinCharState *s = chr->opaque;
1803 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301804 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001805 DWORD size;
1806
1807 ZeroMemory(&s->orecv, sizeof(s->orecv));
1808 s->orecv.hEvent = s->hrecv;
1809 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1810 if (!ret) {
1811 err = GetLastError();
1812 if (err == ERROR_IO_PENDING) {
1813 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1814 }
1815 }
1816
1817 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001818 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001819 }
1820}
1821
1822static void win_chr_read(CharDriverState *chr)
1823{
1824 WinCharState *s = chr->opaque;
1825
1826 if (s->len > s->max_size)
1827 s->len = s->max_size;
1828 if (s->len == 0)
1829 return;
1830
1831 win_chr_readfile(chr);
1832}
1833
1834static int win_chr_poll(void *opaque)
1835{
1836 CharDriverState *chr = opaque;
1837 WinCharState *s = chr->opaque;
1838 COMSTAT status;
1839 DWORD comerr;
1840
1841 ClearCommError(s->hcom, &comerr, &status);
1842 if (status.cbInQue > 0) {
1843 s->len = status.cbInQue;
1844 win_chr_read_poll(chr);
1845 win_chr_read(chr);
1846 return 1;
1847 }
1848 return 0;
1849}
1850
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001851static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001852{
1853 CharDriverState *chr;
1854 WinCharState *s;
1855
Anthony Liguori7267c092011-08-20 22:09:37 -05001856 chr = g_malloc0(sizeof(CharDriverState));
1857 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001858 chr->opaque = s;
1859 chr->chr_write = win_chr_write;
1860 chr->chr_close = win_chr_close;
1861
1862 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001863 g_free(s);
1864 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001865 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001866 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001867 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001868}
1869
1870static int win_chr_pipe_poll(void *opaque)
1871{
1872 CharDriverState *chr = opaque;
1873 WinCharState *s = chr->opaque;
1874 DWORD size;
1875
1876 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1877 if (size > 0) {
1878 s->len = size;
1879 win_chr_read_poll(chr);
1880 win_chr_read(chr);
1881 return 1;
1882 }
1883 return 0;
1884}
1885
1886static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1887{
1888 WinCharState *s = chr->opaque;
1889 OVERLAPPED ov;
1890 int ret;
1891 DWORD size;
1892 char openname[256];
1893
1894 s->fpipe = TRUE;
1895
1896 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1897 if (!s->hsend) {
1898 fprintf(stderr, "Failed CreateEvent\n");
1899 goto fail;
1900 }
1901 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1902 if (!s->hrecv) {
1903 fprintf(stderr, "Failed CreateEvent\n");
1904 goto fail;
1905 }
1906
1907 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1908 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1909 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1910 PIPE_WAIT,
1911 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1912 if (s->hcom == INVALID_HANDLE_VALUE) {
1913 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1914 s->hcom = NULL;
1915 goto fail;
1916 }
1917
1918 ZeroMemory(&ov, sizeof(ov));
1919 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1920 ret = ConnectNamedPipe(s->hcom, &ov);
1921 if (ret) {
1922 fprintf(stderr, "Failed ConnectNamedPipe\n");
1923 goto fail;
1924 }
1925
1926 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1927 if (!ret) {
1928 fprintf(stderr, "Failed GetOverlappedResult\n");
1929 if (ov.hEvent) {
1930 CloseHandle(ov.hEvent);
1931 ov.hEvent = NULL;
1932 }
1933 goto fail;
1934 }
1935
1936 if (ov.hEvent) {
1937 CloseHandle(ov.hEvent);
1938 ov.hEvent = NULL;
1939 }
1940 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1941 return 0;
1942
1943 fail:
1944 win_chr_close(chr);
1945 return -1;
1946}
1947
1948
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001949static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001950{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001951 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001952 CharDriverState *chr;
1953 WinCharState *s;
1954
Anthony Liguori7267c092011-08-20 22:09:37 -05001955 chr = g_malloc0(sizeof(CharDriverState));
1956 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001957 chr->opaque = s;
1958 chr->chr_write = win_chr_write;
1959 chr->chr_close = win_chr_close;
1960
1961 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001962 g_free(s);
1963 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001964 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001965 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001966 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001967}
1968
Markus Armbruster1f514702012-02-07 15:09:08 +01001969static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001970{
1971 CharDriverState *chr;
1972 WinCharState *s;
1973
Anthony Liguori7267c092011-08-20 22:09:37 -05001974 chr = g_malloc0(sizeof(CharDriverState));
1975 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001976 s->hcom = fd_out;
1977 chr->opaque = s;
1978 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001979 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001980}
1981
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001982static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001983{
Markus Armbruster1f514702012-02-07 15:09:08 +01001984 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001985}
1986
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001987static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1988{
1989 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1990 DWORD dwSize;
1991 int len1;
1992
1993 len1 = len;
1994
1995 while (len1 > 0) {
1996 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1997 break;
1998 }
1999 buf += dwSize;
2000 len1 -= dwSize;
2001 }
2002
2003 return len - len1;
2004}
2005
2006static void win_stdio_wait_func(void *opaque)
2007{
2008 CharDriverState *chr = opaque;
2009 WinStdioCharState *stdio = chr->opaque;
2010 INPUT_RECORD buf[4];
2011 int ret;
2012 DWORD dwSize;
2013 int i;
2014
Stefan Weildff74242013-12-07 14:48:04 +01002015 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002016
2017 if (!ret) {
2018 /* Avoid error storm */
2019 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2020 return;
2021 }
2022
2023 for (i = 0; i < dwSize; i++) {
2024 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2025
2026 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2027 int j;
2028 if (kev->uChar.AsciiChar != 0) {
2029 for (j = 0; j < kev->wRepeatCount; j++) {
2030 if (qemu_chr_be_can_write(chr)) {
2031 uint8_t c = kev->uChar.AsciiChar;
2032 qemu_chr_be_write(chr, &c, 1);
2033 }
2034 }
2035 }
2036 }
2037 }
2038}
2039
2040static DWORD WINAPI win_stdio_thread(LPVOID param)
2041{
2042 CharDriverState *chr = param;
2043 WinStdioCharState *stdio = chr->opaque;
2044 int ret;
2045 DWORD dwSize;
2046
2047 while (1) {
2048
2049 /* Wait for one byte */
2050 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2051
2052 /* Exit in case of error, continue if nothing read */
2053 if (!ret) {
2054 break;
2055 }
2056 if (!dwSize) {
2057 continue;
2058 }
2059
2060 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2061 if (stdio->win_stdio_buf == '\r') {
2062 continue;
2063 }
2064
2065 /* Signal the main thread and wait until the byte was eaten */
2066 if (!SetEvent(stdio->hInputReadyEvent)) {
2067 break;
2068 }
2069 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2070 != WAIT_OBJECT_0) {
2071 break;
2072 }
2073 }
2074
2075 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2076 return 0;
2077}
2078
2079static void win_stdio_thread_wait_func(void *opaque)
2080{
2081 CharDriverState *chr = opaque;
2082 WinStdioCharState *stdio = chr->opaque;
2083
2084 if (qemu_chr_be_can_write(chr)) {
2085 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2086 }
2087
2088 SetEvent(stdio->hInputDoneEvent);
2089}
2090
2091static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2092{
2093 WinStdioCharState *stdio = chr->opaque;
2094 DWORD dwMode = 0;
2095
2096 GetConsoleMode(stdio->hStdIn, &dwMode);
2097
2098 if (echo) {
2099 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2100 } else {
2101 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2102 }
2103}
2104
2105static void win_stdio_close(CharDriverState *chr)
2106{
2107 WinStdioCharState *stdio = chr->opaque;
2108
2109 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2110 CloseHandle(stdio->hInputReadyEvent);
2111 }
2112 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2113 CloseHandle(stdio->hInputDoneEvent);
2114 }
2115 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2116 TerminateThread(stdio->hInputThread, 0);
2117 }
2118
2119 g_free(chr->opaque);
2120 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002121}
2122
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002123static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002124{
2125 CharDriverState *chr;
2126 WinStdioCharState *stdio;
2127 DWORD dwMode;
2128 int is_console = 0;
2129
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002130 chr = g_malloc0(sizeof(CharDriverState));
2131 stdio = g_malloc0(sizeof(WinStdioCharState));
2132
2133 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2134 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2135 fprintf(stderr, "cannot open stdio: invalid handle\n");
2136 exit(1);
2137 }
2138
2139 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2140
2141 chr->opaque = stdio;
2142 chr->chr_write = win_stdio_write;
2143 chr->chr_close = win_stdio_close;
2144
Anthony Liguoried7a1542013-03-05 23:21:17 +05302145 if (is_console) {
2146 if (qemu_add_wait_object(stdio->hStdIn,
2147 win_stdio_wait_func, chr)) {
2148 fprintf(stderr, "qemu_add_wait_object: failed\n");
2149 }
2150 } else {
2151 DWORD dwId;
2152
2153 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2154 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2155 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2156 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002157
Anthony Liguoried7a1542013-03-05 23:21:17 +05302158 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2159 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2160 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2161 fprintf(stderr, "cannot create stdio thread or event\n");
2162 exit(1);
2163 }
2164 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2165 win_stdio_thread_wait_func, chr)) {
2166 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002167 }
2168 }
2169
2170 dwMode |= ENABLE_LINE_INPUT;
2171
Anthony Liguoried7a1542013-03-05 23:21:17 +05302172 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002173 /* set the terminal in raw mode */
2174 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2175 dwMode |= ENABLE_PROCESSED_INPUT;
2176 }
2177
2178 SetConsoleMode(stdio->hStdIn, dwMode);
2179
2180 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2181 qemu_chr_fe_set_echo(chr, false);
2182
Markus Armbruster1f514702012-02-07 15:09:08 +01002183 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002184}
aliguori6f97dba2008-10-31 18:49:55 +00002185#endif /* !_WIN32 */
2186
Anthony Liguori5ab82112013-03-05 23:21:31 +05302187
aliguori6f97dba2008-10-31 18:49:55 +00002188/***********************************************************/
2189/* UDP Net console */
2190
2191typedef struct {
2192 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302193 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302194 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002195 int bufcnt;
2196 int bufptr;
2197 int max_size;
2198} NetCharDriver;
2199
2200static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2201{
2202 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302203 gsize bytes_written;
2204 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002205
Anthony Liguori76a96442013-03-05 23:21:21 +05302206 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2207 if (status == G_IO_STATUS_EOF) {
2208 return 0;
2209 } else if (status != G_IO_STATUS_NORMAL) {
2210 return -1;
2211 }
2212
2213 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002214}
2215
2216static int udp_chr_read_poll(void *opaque)
2217{
2218 CharDriverState *chr = opaque;
2219 NetCharDriver *s = chr->opaque;
2220
Anthony Liguori909cda12011-08-15 11:17:31 -05002221 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002222
2223 /* If there were any stray characters in the queue process them
2224 * first
2225 */
2226 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002227 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002228 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002229 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002230 }
2231 return s->max_size;
2232}
2233
Anthony Liguori76a96442013-03-05 23:21:21 +05302234static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002235{
2236 CharDriverState *chr = opaque;
2237 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302238 gsize bytes_read = 0;
2239 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002240
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002241 if (s->max_size == 0) {
2242 return TRUE;
2243 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302244 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2245 &bytes_read, NULL);
2246 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002247 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302248 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302249 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302250 return FALSE;
2251 }
aliguori6f97dba2008-10-31 18:49:55 +00002252
2253 s->bufptr = 0;
2254 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002255 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002256 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002257 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002258 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302259
2260 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002261}
2262
2263static void udp_chr_update_read_handler(CharDriverState *chr)
2264{
2265 NetCharDriver *s = chr->opaque;
2266
Amit Shah26da70c2013-08-28 15:23:37 +05302267 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302268 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302269 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2270 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002271 }
2272}
2273
aliguori819f56b2009-03-28 17:58:14 +00002274static void udp_chr_close(CharDriverState *chr)
2275{
2276 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302277
2278 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302279 if (s->chan) {
2280 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002281 closesocket(s->fd);
2282 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002283 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002284 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002285}
2286
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002287static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002288{
2289 CharDriverState *chr = NULL;
2290 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002291
Anthony Liguori7267c092011-08-20 22:09:37 -05002292 chr = g_malloc0(sizeof(CharDriverState));
2293 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002294
aliguori6f97dba2008-10-31 18:49:55 +00002295 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302296 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002297 s->bufcnt = 0;
2298 s->bufptr = 0;
2299 chr->opaque = s;
2300 chr->chr_write = udp_chr_write;
2301 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002302 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002303 /* be isn't opened until we get a connection */
2304 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002305 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002306}
aliguori6f97dba2008-10-31 18:49:55 +00002307
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002308static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2309{
2310 Error *local_err = NULL;
2311 int fd = -1;
2312
2313 fd = inet_dgram_opts(opts, &local_err);
2314 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002315 qerror_report_err(local_err);
2316 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002317 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002318 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002319 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002320}
2321
2322/***********************************************************/
2323/* TCP Net console */
2324
2325typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302326
2327 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302328 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002329 int fd, listen_fd;
2330 int connected;
2331 int max_size;
2332 int do_telnetopt;
2333 int do_nodelay;
2334 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002335 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002336} TCPCharDriver;
2337
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302338static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002339
2340static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2341{
2342 TCPCharDriver *s = chr->opaque;
2343 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002344 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002345 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002346 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002347 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002348 }
2349}
2350
2351static int tcp_chr_read_poll(void *opaque)
2352{
2353 CharDriverState *chr = opaque;
2354 TCPCharDriver *s = chr->opaque;
2355 if (!s->connected)
2356 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002357 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002358 return s->max_size;
2359}
2360
2361#define IAC 255
2362#define IAC_BREAK 243
2363static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2364 TCPCharDriver *s,
2365 uint8_t *buf, int *size)
2366{
2367 /* Handle any telnet client's basic IAC options to satisfy char by
2368 * char mode with no echo. All IAC options will be removed from
2369 * the buf and the do_telnetopt variable will be used to track the
2370 * state of the width of the IAC information.
2371 *
2372 * IAC commands come in sets of 3 bytes with the exception of the
2373 * "IAC BREAK" command and the double IAC.
2374 */
2375
2376 int i;
2377 int j = 0;
2378
2379 for (i = 0; i < *size; i++) {
2380 if (s->do_telnetopt > 1) {
2381 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2382 /* Double IAC means send an IAC */
2383 if (j != i)
2384 buf[j] = buf[i];
2385 j++;
2386 s->do_telnetopt = 1;
2387 } else {
2388 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2389 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002390 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002391 s->do_telnetopt++;
2392 }
2393 s->do_telnetopt++;
2394 }
2395 if (s->do_telnetopt >= 4) {
2396 s->do_telnetopt = 1;
2397 }
2398 } else {
2399 if ((unsigned char)buf[i] == IAC) {
2400 s->do_telnetopt = 2;
2401 } else {
2402 if (j != i)
2403 buf[j] = buf[i];
2404 j++;
2405 }
2406 }
2407 }
2408 *size = j;
2409}
2410
Mark McLoughlin7d174052009-07-22 09:11:39 +01002411static int tcp_get_msgfd(CharDriverState *chr)
2412{
2413 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002414 int fd = s->msgfd;
2415 s->msgfd = -1;
2416 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002417}
2418
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002419#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002420static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2421{
2422 TCPCharDriver *s = chr->opaque;
2423 struct cmsghdr *cmsg;
2424
2425 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2426 int fd;
2427
2428 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2429 cmsg->cmsg_level != SOL_SOCKET ||
2430 cmsg->cmsg_type != SCM_RIGHTS)
2431 continue;
2432
2433 fd = *((int *)CMSG_DATA(cmsg));
2434 if (fd < 0)
2435 continue;
2436
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002437 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2438 qemu_set_block(fd);
2439
Corey Bryant06138652012-08-14 16:43:42 -04002440#ifndef MSG_CMSG_CLOEXEC
2441 qemu_set_cloexec(fd);
2442#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002443 if (s->msgfd != -1)
2444 close(s->msgfd);
2445 s->msgfd = fd;
2446 }
2447}
2448
Mark McLoughlin9977c892009-07-22 09:11:38 +01002449static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2450{
2451 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002452 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002453 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002454 union {
2455 struct cmsghdr cmsg;
2456 char control[CMSG_SPACE(sizeof(int))];
2457 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002458 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002459 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002460
2461 iov[0].iov_base = buf;
2462 iov[0].iov_len = len;
2463
2464 msg.msg_iov = iov;
2465 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002466 msg.msg_control = &msg_control;
2467 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002468
Corey Bryant06138652012-08-14 16:43:42 -04002469#ifdef MSG_CMSG_CLOEXEC
2470 flags |= MSG_CMSG_CLOEXEC;
2471#endif
2472 ret = recvmsg(s->fd, &msg, flags);
2473 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002474 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002475 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002476
2477 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002478}
2479#else
2480static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2481{
2482 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002483 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002484}
2485#endif
2486
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302487static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2488{
2489 TCPCharDriver *s = chr->opaque;
2490 return g_io_create_watch(s->chan, cond);
2491}
2492
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002493static void tcp_chr_disconnect(CharDriverState *chr)
2494{
2495 TCPCharDriver *s = chr->opaque;
2496
2497 s->connected = 0;
2498 if (s->listen_chan) {
2499 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2500 tcp_chr_accept, chr);
2501 }
2502 remove_fd_in_watch(chr);
2503 g_io_channel_unref(s->chan);
2504 s->chan = NULL;
2505 closesocket(s->fd);
2506 s->fd = -1;
2507 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2508}
2509
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302510static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002511{
2512 CharDriverState *chr = opaque;
2513 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302514 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002515 int len, size;
2516
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302517 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002518 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302519 }
aliguori6f97dba2008-10-31 18:49:55 +00002520 len = sizeof(buf);
2521 if (len > s->max_size)
2522 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002523 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002524 if (size == 0) {
2525 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002526 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002527 } else if (size > 0) {
2528 if (s->do_telnetopt)
2529 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2530 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002531 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002532 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302533
2534 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002535}
2536
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002537static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2538{
2539 TCPCharDriver *s = chr->opaque;
2540 int size;
2541
2542 if (!s->connected) {
2543 return 0;
2544 }
2545
2546 size = tcp_chr_recv(chr, (void *) buf, len);
2547 if (size == 0) {
2548 /* connection closed */
2549 tcp_chr_disconnect(chr);
2550 }
2551
2552 return size;
2553}
2554
Blue Swirl68c18d12010-08-15 09:46:24 +00002555#ifndef _WIN32
2556CharDriverState *qemu_chr_open_eventfd(int eventfd)
2557{
David Marchande9d21c42014-06-11 17:25:16 +02002558 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2559
2560 if (chr) {
2561 chr->avail_connections = 1;
2562 }
2563
2564 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002565}
Blue Swirl68c18d12010-08-15 09:46:24 +00002566#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002567
aliguori6f97dba2008-10-31 18:49:55 +00002568static void tcp_chr_connect(void *opaque)
2569{
2570 CharDriverState *chr = opaque;
2571 TCPCharDriver *s = chr->opaque;
2572
2573 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302574 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302575 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2576 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002577 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002578 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002579}
2580
Gal Hammerac1b84d2014-02-25 12:12:35 +02002581static void tcp_chr_update_read_handler(CharDriverState *chr)
2582{
2583 TCPCharDriver *s = chr->opaque;
2584
2585 remove_fd_in_watch(chr);
2586 if (s->chan) {
2587 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2588 tcp_chr_read, chr);
2589 }
2590}
2591
aliguori6f97dba2008-10-31 18:49:55 +00002592#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2593static void tcp_chr_telnet_init(int fd)
2594{
2595 char buf[3];
2596 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2597 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2598 send(fd, (char *)buf, 3, 0);
2599 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2600 send(fd, (char *)buf, 3, 0);
2601 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2602 send(fd, (char *)buf, 3, 0);
2603 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2604 send(fd, (char *)buf, 3, 0);
2605}
2606
Daniel P. Berrange13661082011-06-23 13:31:42 +01002607static int tcp_chr_add_client(CharDriverState *chr, int fd)
2608{
2609 TCPCharDriver *s = chr->opaque;
2610 if (s->fd != -1)
2611 return -1;
2612
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002613 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002614 if (s->do_nodelay)
2615 socket_set_nodelay(fd);
2616 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302617 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002618 if (s->listen_tag) {
2619 g_source_remove(s->listen_tag);
2620 s->listen_tag = 0;
2621 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002622 tcp_chr_connect(chr);
2623
2624 return 0;
2625}
2626
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302627static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002628{
2629 CharDriverState *chr = opaque;
2630 TCPCharDriver *s = chr->opaque;
2631 struct sockaddr_in saddr;
2632#ifndef _WIN32
2633 struct sockaddr_un uaddr;
2634#endif
2635 struct sockaddr *addr;
2636 socklen_t len;
2637 int fd;
2638
2639 for(;;) {
2640#ifndef _WIN32
2641 if (s->is_unix) {
2642 len = sizeof(uaddr);
2643 addr = (struct sockaddr *)&uaddr;
2644 } else
2645#endif
2646 {
2647 len = sizeof(saddr);
2648 addr = (struct sockaddr *)&saddr;
2649 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002650 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002651 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002652 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302653 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002654 } else if (fd >= 0) {
2655 if (s->do_telnetopt)
2656 tcp_chr_telnet_init(fd);
2657 break;
2658 }
2659 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002660 if (tcp_chr_add_client(chr, fd) < 0)
2661 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302662
2663 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002664}
2665
2666static void tcp_chr_close(CharDriverState *chr)
2667{
2668 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002669 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302670 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302671 if (s->chan) {
2672 g_io_channel_unref(s->chan);
2673 }
aliguori6f97dba2008-10-31 18:49:55 +00002674 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002675 }
2676 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302677 if (s->listen_tag) {
2678 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002679 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302680 }
2681 if (s->listen_chan) {
2682 g_io_channel_unref(s->listen_chan);
2683 }
aliguori6f97dba2008-10-31 18:49:55 +00002684 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002685 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002686 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002687 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002688}
2689
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002690static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2691 bool is_listen, bool is_telnet,
2692 bool is_waitconnect,
2693 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002694{
2695 CharDriverState *chr = NULL;
2696 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002697 char host[NI_MAXHOST], serv[NI_MAXSERV];
2698 const char *left = "", *right = "";
2699 struct sockaddr_storage ss;
2700 socklen_t ss_len = sizeof(ss);
2701
2702 memset(&ss, 0, ss_len);
2703 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002704 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002705 return NULL;
2706 }
2707
2708 chr = g_malloc0(sizeof(CharDriverState));
2709 s = g_malloc0(sizeof(TCPCharDriver));
2710
2711 s->connected = 0;
2712 s->fd = -1;
2713 s->listen_fd = -1;
2714 s->msgfd = -1;
2715
2716 chr->filename = g_malloc(256);
2717 switch (ss.ss_family) {
2718#ifndef _WIN32
2719 case AF_UNIX:
2720 s->is_unix = 1;
2721 snprintf(chr->filename, 256, "unix:%s%s",
2722 ((struct sockaddr_un *)(&ss))->sun_path,
2723 is_listen ? ",server" : "");
2724 break;
2725#endif
2726 case AF_INET6:
2727 left = "[";
2728 right = "]";
2729 /* fall through */
2730 case AF_INET:
2731 s->do_nodelay = do_nodelay;
2732 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2733 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002734 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002735 is_telnet ? "telnet" : "tcp",
2736 left, host, right, serv,
2737 is_listen ? ",server" : "");
2738 break;
2739 }
2740
2741 chr->opaque = s;
2742 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002743 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002744 chr->chr_close = tcp_chr_close;
2745 chr->get_msgfd = tcp_get_msgfd;
2746 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302747 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002748 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002749 /* be isn't opened until we get a connection */
2750 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002751
2752 if (is_listen) {
2753 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302754 s->listen_chan = io_channel_from_socket(s->listen_fd);
2755 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002756 if (is_telnet) {
2757 s->do_telnetopt = 1;
2758 }
2759 } else {
2760 s->connected = 1;
2761 s->fd = fd;
2762 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302763 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002764 tcp_chr_connect(chr);
2765 }
2766
2767 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002768 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2769 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302770 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002771 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002772 }
2773 return chr;
2774}
2775
2776static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2777{
2778 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002779 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002780 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002781
liguange990a392013-06-18 11:45:35 +08002782 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2783 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2784 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2785 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2786 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002787
aliguorif07b6002008-11-11 20:54:09 +00002788 if (is_unix) {
2789 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002790 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002791 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002792 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002793 }
2794 } else {
2795 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002796 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002797 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002798 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002799 }
2800 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002801 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002802 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002803 }
aliguori6f97dba2008-10-31 18:49:55 +00002804
2805 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002806 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002807
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002808 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2809 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002810 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002811 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002812 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002813 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002814
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002815
aliguori6f97dba2008-10-31 18:49:55 +00002816 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002817 if (local_err) {
2818 qerror_report_err(local_err);
2819 error_free(local_err);
2820 }
2821 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002822 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002823 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002824 if (chr) {
2825 g_free(chr->opaque);
2826 g_free(chr);
2827 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002828 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002829}
2830
Lei Li51767e72013-01-25 00:03:19 +08002831/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002832/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002833
2834typedef struct {
2835 size_t size;
2836 size_t prod;
2837 size_t cons;
2838 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002839} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002840
Markus Armbruster3949e592013-02-06 21:27:24 +01002841static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002842{
Markus Armbruster3949e592013-02-06 21:27:24 +01002843 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002844
Markus Armbruster5c230102013-02-06 21:27:23 +01002845 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002846}
2847
Markus Armbruster3949e592013-02-06 21:27:24 +01002848static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002849{
Markus Armbruster3949e592013-02-06 21:27:24 +01002850 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002851 int i;
2852
2853 if (!buf || (len < 0)) {
2854 return -1;
2855 }
2856
2857 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002858 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2859 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002860 d->cons = d->prod - d->size;
2861 }
2862 }
2863
2864 return 0;
2865}
2866
Markus Armbruster3949e592013-02-06 21:27:24 +01002867static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002868{
Markus Armbruster3949e592013-02-06 21:27:24 +01002869 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002870 int i;
2871
Markus Armbruster5c230102013-02-06 21:27:23 +01002872 for (i = 0; i < len && d->cons != d->prod; i++) {
2873 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002874 }
2875
2876 return i;
2877}
2878
Markus Armbruster3949e592013-02-06 21:27:24 +01002879static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002880{
Markus Armbruster3949e592013-02-06 21:27:24 +01002881 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002882
2883 g_free(d->cbuf);
2884 g_free(d);
2885 chr->opaque = NULL;
2886}
2887
Markus Armbruster4f573782013-07-26 16:44:32 +02002888static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2889 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002890{
2891 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002892 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002893
2894 chr = g_malloc0(sizeof(CharDriverState));
2895 d = g_malloc(sizeof(*d));
2896
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002897 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002898
2899 /* The size must be power of 2 */
2900 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02002901 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002902 goto fail;
2903 }
2904
2905 d->prod = 0;
2906 d->cons = 0;
2907 d->cbuf = g_malloc0(d->size);
2908
2909 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002910 chr->chr_write = ringbuf_chr_write;
2911 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002912
2913 return chr;
2914
2915fail:
2916 g_free(d);
2917 g_free(chr);
2918 return NULL;
2919}
2920
Hani Benhabiles8e597772014-05-27 23:39:30 +01002921bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002922{
Markus Armbruster3949e592013-02-06 21:27:24 +01002923 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002924}
2925
Markus Armbruster3949e592013-02-06 21:27:24 +01002926void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002927 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002928 Error **errp)
2929{
2930 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002931 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002932 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10002933 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002934
2935 chr = qemu_chr_find(device);
2936 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002937 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002938 return;
2939 }
2940
Markus Armbruster3949e592013-02-06 21:27:24 +01002941 if (!chr_is_ringbuf(chr)) {
2942 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002943 return;
2944 }
2945
Lei Li1f590cf2013-01-25 00:03:20 +08002946 if (has_format && (format == DATA_FORMAT_BASE64)) {
2947 write_data = g_base64_decode(data, &write_count);
2948 } else {
2949 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002950 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002951 }
2952
Markus Armbruster3949e592013-02-06 21:27:24 +01002953 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002954
Markus Armbruster13289fb2013-02-06 21:27:18 +01002955 if (write_data != (uint8_t *)data) {
2956 g_free((void *)write_data);
2957 }
2958
Lei Li1f590cf2013-01-25 00:03:20 +08002959 if (ret < 0) {
2960 error_setg(errp, "Failed to write to device %s", device);
2961 return;
2962 }
2963}
2964
Markus Armbruster3949e592013-02-06 21:27:24 +01002965char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002966 bool has_format, enum DataFormat format,
2967 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002968{
2969 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002970 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002971 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002972 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002973
2974 chr = qemu_chr_find(device);
2975 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002976 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002977 return NULL;
2978 }
2979
Markus Armbruster3949e592013-02-06 21:27:24 +01002980 if (!chr_is_ringbuf(chr)) {
2981 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002982 return NULL;
2983 }
2984
2985 if (size <= 0) {
2986 error_setg(errp, "size must be greater than zero");
2987 return NULL;
2988 }
2989
Markus Armbruster3949e592013-02-06 21:27:24 +01002990 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002991 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002992 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002993
Markus Armbruster3949e592013-02-06 21:27:24 +01002994 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002995
2996 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002997 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002998 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002999 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003000 /*
3001 * FIXME should read only complete, valid UTF-8 characters up
3002 * to @size bytes. Invalid sequences should be replaced by a
3003 * suitable replacement character. Except when (and only
3004 * when) ring buffer lost characters since last read, initial
3005 * continuation characters should be dropped.
3006 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003007 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003008 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003009 }
3010
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003011 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003012}
3013
Gerd Hoffmann33521632009-12-08 13:11:49 +01003014QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003015{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003016 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003017 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003018 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003019 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003020 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003021
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003022 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003023 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003024 qerror_report_err(local_err);
3025 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003026 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003027 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003028
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003029 if (strstart(filename, "mon:", &p)) {
3030 filename = p;
3031 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003032 if (strcmp(filename, "stdio") == 0) {
3033 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3034 * but pass it to the guest. Handle this only for compat syntax,
3035 * for -chardev syntax we have special option for this.
3036 * This is what -nographic did, redirecting+muxing serial+monitor
3037 * to stdio causing Ctrl+C to be passed to guest. */
3038 qemu_opt_set(opts, "signal", "off");
3039 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003040 }
3041
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003042 if (strcmp(filename, "null") == 0 ||
3043 strcmp(filename, "pty") == 0 ||
3044 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003045 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003046 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003047 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003048 return opts;
3049 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003050 if (strstart(filename, "vc", &p)) {
3051 qemu_opt_set(opts, "backend", "vc");
3052 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003053 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003054 /* pixels */
3055 qemu_opt_set(opts, "width", width);
3056 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003057 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003058 /* chars */
3059 qemu_opt_set(opts, "cols", width);
3060 qemu_opt_set(opts, "rows", height);
3061 } else {
3062 goto fail;
3063 }
3064 }
3065 return opts;
3066 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003067 if (strcmp(filename, "con:") == 0) {
3068 qemu_opt_set(opts, "backend", "console");
3069 return opts;
3070 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003071 if (strstart(filename, "COM", NULL)) {
3072 qemu_opt_set(opts, "backend", "serial");
3073 qemu_opt_set(opts, "path", filename);
3074 return opts;
3075 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003076 if (strstart(filename, "file:", &p)) {
3077 qemu_opt_set(opts, "backend", "file");
3078 qemu_opt_set(opts, "path", p);
3079 return opts;
3080 }
3081 if (strstart(filename, "pipe:", &p)) {
3082 qemu_opt_set(opts, "backend", "pipe");
3083 qemu_opt_set(opts, "path", p);
3084 return opts;
3085 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003086 if (strstart(filename, "tcp:", &p) ||
3087 strstart(filename, "telnet:", &p)) {
3088 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3089 host[0] = 0;
3090 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3091 goto fail;
3092 }
3093 qemu_opt_set(opts, "backend", "socket");
3094 qemu_opt_set(opts, "host", host);
3095 qemu_opt_set(opts, "port", port);
3096 if (p[pos] == ',') {
3097 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3098 goto fail;
3099 }
3100 if (strstart(filename, "telnet:", &p))
3101 qemu_opt_set(opts, "telnet", "on");
3102 return opts;
3103 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003104 if (strstart(filename, "udp:", &p)) {
3105 qemu_opt_set(opts, "backend", "udp");
3106 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3107 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003108 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003109 goto fail;
3110 }
3111 }
3112 qemu_opt_set(opts, "host", host);
3113 qemu_opt_set(opts, "port", port);
3114 if (p[pos] == '@') {
3115 p += pos + 1;
3116 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3117 host[0] = 0;
3118 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003119 goto fail;
3120 }
3121 }
3122 qemu_opt_set(opts, "localaddr", host);
3123 qemu_opt_set(opts, "localport", port);
3124 }
3125 return opts;
3126 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003127 if (strstart(filename, "unix:", &p)) {
3128 qemu_opt_set(opts, "backend", "socket");
3129 if (qemu_opts_do_parse(opts, p, "path") != 0)
3130 goto fail;
3131 return opts;
3132 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003133 if (strstart(filename, "/dev/parport", NULL) ||
3134 strstart(filename, "/dev/ppi", NULL)) {
3135 qemu_opt_set(opts, "backend", "parport");
3136 qemu_opt_set(opts, "path", filename);
3137 return opts;
3138 }
3139 if (strstart(filename, "/dev/", NULL)) {
3140 qemu_opt_set(opts, "backend", "tty");
3141 qemu_opt_set(opts, "path", filename);
3142 return opts;
3143 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003144
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003145fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003146 qemu_opts_del(opts);
3147 return NULL;
3148}
3149
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003150static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3151 Error **errp)
3152{
3153 const char *path = qemu_opt_get(opts, "path");
3154
3155 if (path == NULL) {
3156 error_setg(errp, "chardev: file: no filename given");
3157 return;
3158 }
3159 backend->file = g_new0(ChardevFile, 1);
3160 backend->file->out = g_strdup(path);
3161}
3162
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003163static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3164 Error **errp)
3165{
3166 backend->stdio = g_new0(ChardevStdio, 1);
3167 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003168 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003169}
3170
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003171static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3172 Error **errp)
3173{
3174 const char *device = qemu_opt_get(opts, "path");
3175
3176 if (device == NULL) {
3177 error_setg(errp, "chardev: serial/tty: no device path given");
3178 return;
3179 }
3180 backend->serial = g_new0(ChardevHostdev, 1);
3181 backend->serial->device = g_strdup(device);
3182}
3183
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003184static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3185 Error **errp)
3186{
3187 const char *device = qemu_opt_get(opts, "path");
3188
3189 if (device == NULL) {
3190 error_setg(errp, "chardev: parallel: no device path given");
3191 return;
3192 }
3193 backend->parallel = g_new0(ChardevHostdev, 1);
3194 backend->parallel->device = g_strdup(device);
3195}
3196
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003197static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3198 Error **errp)
3199{
3200 const char *device = qemu_opt_get(opts, "path");
3201
3202 if (device == NULL) {
3203 error_setg(errp, "chardev: pipe: no device path given");
3204 return;
3205 }
3206 backend->pipe = g_new0(ChardevHostdev, 1);
3207 backend->pipe->device = g_strdup(device);
3208}
3209
Markus Armbruster4f573782013-07-26 16:44:32 +02003210static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3211 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003212{
3213 int val;
3214
Markus Armbruster3a1da422013-07-26 16:44:34 +02003215 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003216
Markus Armbruster0f953052013-06-27 16:22:07 +02003217 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003218 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003219 backend->ringbuf->has_size = true;
3220 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003221 }
3222}
3223
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003224static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3225 Error **errp)
3226{
3227 const char *chardev = qemu_opt_get(opts, "chardev");
3228
3229 if (chardev == NULL) {
3230 error_setg(errp, "chardev: mux: no chardev given");
3231 return;
3232 }
3233 backend->mux = g_new0(ChardevMux, 1);
3234 backend->mux->chardev = g_strdup(chardev);
3235}
3236
Anthony Liguorid654f342013-03-05 23:21:28 +05303237typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003238 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003239 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003240 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003241 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003242 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003243 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303244} CharDriver;
3245
3246static GSList *backends;
3247
3248void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3249{
3250 CharDriver *s;
3251
3252 s = g_malloc0(sizeof(*s));
3253 s->name = g_strdup(name);
3254 s->open = open;
3255
3256 backends = g_slist_append(backends, s);
3257}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003258
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003259void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003260 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3261{
3262 CharDriver *s;
3263
3264 s = g_malloc0(sizeof(*s));
3265 s->name = g_strdup(name);
3266 s->kind = kind;
3267 s->parse = parse;
3268
3269 backends = g_slist_append(backends, s);
3270}
3271
Anthony Liguorif69554b2011-08-15 11:17:37 -05003272CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003273 void (*init)(struct CharDriverState *s),
3274 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003275{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003276 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303277 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003278 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303279 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003280
3281 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003282 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003283 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003284 }
3285
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003286 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003287 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003288 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003289 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003290 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303291 for (i = backends; i; i = i->next) {
3292 cd = i->data;
3293
3294 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003295 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303296 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003297 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303298 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003299 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003300 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003301 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003302 }
3303
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003304 if (!cd->open) {
3305 /* using new, qapi init */
3306 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3307 ChardevReturn *ret = NULL;
3308 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003309 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003310
3311 if (qemu_opt_get_bool(opts, "mux", 0)) {
3312 bid = g_strdup_printf("%s-base", id);
3313 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003314
3315 chr = NULL;
3316 backend->kind = cd->kind;
3317 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003318 cd->parse(opts, backend, &local_err);
3319 if (local_err) {
3320 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003321 goto qapi_out;
3322 }
3323 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003324 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003325 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003326 goto qapi_out;
3327 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003328
3329 if (bid) {
3330 qapi_free_ChardevBackend(backend);
3331 qapi_free_ChardevReturn(ret);
3332 backend = g_new0(ChardevBackend, 1);
3333 backend->mux = g_new0(ChardevMux, 1);
3334 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3335 backend->mux->chardev = g_strdup(bid);
3336 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003337 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003338 chr = qemu_chr_find(bid);
3339 qemu_chr_delete(chr);
3340 chr = NULL;
3341 goto qapi_out;
3342 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003343 }
3344
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003345 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003346 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003347
3348 qapi_out:
3349 qapi_free_ChardevBackend(backend);
3350 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003351 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003352 return chr;
3353 }
3354
Anthony Liguorid654f342013-03-05 23:21:28 +05303355 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003356 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003357 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003358 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003359 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003360 }
3361
3362 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003363 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003364 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003365 /* if we didn't create the chardev via qmp_chardev_add, we
3366 * need to send the OPENED event here
3367 */
3368 if (!chr->explicit_be_open) {
3369 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3370 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003371 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003372
3373 if (qemu_opt_get_bool(opts, "mux", 0)) {
3374 CharDriverState *base = chr;
3375 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003376 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003377 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3378 chr = qemu_chr_open_mux(base);
3379 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003380 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003381 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003382 } else {
3383 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003384 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003385 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003386 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003387 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003388
3389err:
3390 qemu_opts_del(opts);
3391 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003392}
3393
Anthony Liguori27143a42011-08-15 11:17:36 -05003394CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003395{
3396 const char *p;
3397 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003398 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003399 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003400
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003401 if (strstart(filename, "chardev:", &p)) {
3402 return qemu_chr_find(p);
3403 }
3404
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003405 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003406 if (!opts)
3407 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003408
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003409 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003410 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003411 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003412 error_free(err);
3413 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003414 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003415 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003416 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003417 }
3418 return chr;
3419}
3420
Anthony Liguori15f31512011-08-15 11:17:35 -05003421void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003422{
3423 if (chr->chr_set_echo) {
3424 chr->chr_set_echo(chr, echo);
3425 }
3426}
3427
Hans de Goede8e25daa2013-03-26 11:07:57 +01003428void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003429{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003430 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003431 return;
3432 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003433 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003434 if (chr->chr_set_fe_open) {
3435 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003436 }
3437}
3438
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003439void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3440{
3441 if (chr->chr_fe_event) {
3442 chr->chr_fe_event(chr, event);
3443 }
3444}
3445
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003446int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3447 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303448{
3449 GSource *src;
3450 guint tag;
3451
3452 if (s->chr_add_watch == NULL) {
3453 return -ENOSYS;
3454 }
3455
3456 src = s->chr_add_watch(s, cond);
3457 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3458 tag = g_source_attach(src, NULL);
3459 g_source_unref(src);
3460
3461 return tag;
3462}
3463
Hans de Goede44c473d2013-03-27 20:29:39 +01003464int qemu_chr_fe_claim(CharDriverState *s)
3465{
3466 if (s->avail_connections < 1) {
3467 return -1;
3468 }
3469 s->avail_connections--;
3470 return 0;
3471}
3472
3473void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3474{
3475 if (qemu_chr_fe_claim(s) != 0) {
3476 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3477 __func__, s->label);
3478 exit(1);
3479 }
3480}
3481
3482void qemu_chr_fe_release(CharDriverState *s)
3483{
3484 s->avail_connections++;
3485}
3486
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003487void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003488{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003489 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003490 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003491 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003492 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003493 g_free(chr->filename);
3494 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003495 if (chr->opts) {
3496 qemu_opts_del(chr->opts);
3497 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003498 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003499}
3500
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003501ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003502{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003503 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003504 CharDriverState *chr;
3505
Blue Swirl72cf2d42009-09-12 07:36:22 +00003506 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003507 ChardevInfoList *info = g_malloc0(sizeof(*info));
3508 info->value = g_malloc0(sizeof(*info->value));
3509 info->value->label = g_strdup(chr->label);
3510 info->value->filename = g_strdup(chr->filename);
3511
3512 info->next = chr_list;
3513 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003514 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003515
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003516 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003517}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003518
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003519ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3520{
3521 ChardevBackendInfoList *backend_list = NULL;
3522 CharDriver *c = NULL;
3523 GSList *i = NULL;
3524
3525 for (i = backends; i; i = i->next) {
3526 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3527 c = i->data;
3528 info->value = g_malloc0(sizeof(*info->value));
3529 info->value->name = g_strdup(c->name);
3530
3531 info->next = backend_list;
3532 backend_list = info;
3533 }
3534
3535 return backend_list;
3536}
3537
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003538CharDriverState *qemu_chr_find(const char *name)
3539{
3540 CharDriverState *chr;
3541
Blue Swirl72cf2d42009-09-12 07:36:22 +00003542 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003543 if (strcmp(chr->label, name) != 0)
3544 continue;
3545 return chr;
3546 }
3547 return NULL;
3548}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003549
3550/* Get a character (serial) device interface. */
3551CharDriverState *qemu_char_get_next_serial(void)
3552{
3553 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003554 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003555
3556 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003557
3558 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3559 chr = serial_hds[next_serial++];
3560 qemu_chr_fe_claim_no_fail(chr);
3561 return chr;
3562 }
3563 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003564}
3565
Paolo Bonzini4d454572012-11-26 16:03:42 +01003566QemuOptsList qemu_chardev_opts = {
3567 .name = "chardev",
3568 .implied_opt_name = "backend",
3569 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3570 .desc = {
3571 {
3572 .name = "backend",
3573 .type = QEMU_OPT_STRING,
3574 },{
3575 .name = "path",
3576 .type = QEMU_OPT_STRING,
3577 },{
3578 .name = "host",
3579 .type = QEMU_OPT_STRING,
3580 },{
3581 .name = "port",
3582 .type = QEMU_OPT_STRING,
3583 },{
3584 .name = "localaddr",
3585 .type = QEMU_OPT_STRING,
3586 },{
3587 .name = "localport",
3588 .type = QEMU_OPT_STRING,
3589 },{
3590 .name = "to",
3591 .type = QEMU_OPT_NUMBER,
3592 },{
3593 .name = "ipv4",
3594 .type = QEMU_OPT_BOOL,
3595 },{
3596 .name = "ipv6",
3597 .type = QEMU_OPT_BOOL,
3598 },{
3599 .name = "wait",
3600 .type = QEMU_OPT_BOOL,
3601 },{
3602 .name = "server",
3603 .type = QEMU_OPT_BOOL,
3604 },{
3605 .name = "delay",
3606 .type = QEMU_OPT_BOOL,
3607 },{
3608 .name = "telnet",
3609 .type = QEMU_OPT_BOOL,
3610 },{
3611 .name = "width",
3612 .type = QEMU_OPT_NUMBER,
3613 },{
3614 .name = "height",
3615 .type = QEMU_OPT_NUMBER,
3616 },{
3617 .name = "cols",
3618 .type = QEMU_OPT_NUMBER,
3619 },{
3620 .name = "rows",
3621 .type = QEMU_OPT_NUMBER,
3622 },{
3623 .name = "mux",
3624 .type = QEMU_OPT_BOOL,
3625 },{
3626 .name = "signal",
3627 .type = QEMU_OPT_BOOL,
3628 },{
3629 .name = "name",
3630 .type = QEMU_OPT_STRING,
3631 },{
3632 .name = "debug",
3633 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003634 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003635 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003636 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003637 },{
3638 .name = "chardev",
3639 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003640 },
3641 { /* end of list */ }
3642 },
3643};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003644
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003645#ifdef _WIN32
3646
3647static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3648{
3649 HANDLE out;
3650
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003651 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003652 error_setg(errp, "input file not supported");
3653 return NULL;
3654 }
3655
3656 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3657 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3658 if (out == INVALID_HANDLE_VALUE) {
3659 error_setg(errp, "open %s failed", file->out);
3660 return NULL;
3661 }
3662 return qemu_chr_open_win_file(out);
3663}
3664
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003665static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3666 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003667{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003668 return qemu_chr_open_win_path(serial->device);
3669}
3670
3671static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3672 Error **errp)
3673{
3674 error_setg(errp, "character device backend type 'parallel' not supported");
3675 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003676}
3677
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003678#else /* WIN32 */
3679
3680static int qmp_chardev_open_file_source(char *src, int flags,
3681 Error **errp)
3682{
3683 int fd = -1;
3684
3685 TFR(fd = qemu_open(src, flags, 0666));
3686 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003687 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003688 }
3689 return fd;
3690}
3691
3692static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3693{
Markus Armbruster5f758362014-05-19 18:57:34 +02003694 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003695
3696 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3697 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003698 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003699 return NULL;
3700 }
3701
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003702 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003703 flags = O_RDONLY;
3704 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003705 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003706 qemu_close(out);
3707 return NULL;
3708 }
3709 }
3710
3711 return qemu_chr_open_fd(in, out);
3712}
3713
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003714static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3715 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003716{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003717#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003718 int fd;
3719
3720 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003721 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003722 return NULL;
3723 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003724 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003725 return qemu_chr_open_tty_fd(fd);
3726#else
3727 error_setg(errp, "character device backend type 'serial' not supported");
3728 return NULL;
3729#endif
3730}
3731
3732static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3733 Error **errp)
3734{
3735#ifdef HAVE_CHARDEV_PARPORT
3736 int fd;
3737
3738 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003739 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003740 return NULL;
3741 }
3742 return qemu_chr_open_pp_fd(fd);
3743#else
3744 error_setg(errp, "character device backend type 'parallel' not supported");
3745 return NULL;
3746#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003747}
3748
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003749#endif /* WIN32 */
3750
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003751static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3752 Error **errp)
3753{
3754 SocketAddress *addr = sock->addr;
3755 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3756 bool is_listen = sock->has_server ? sock->server : true;
3757 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3758 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3759 int fd;
3760
3761 if (is_listen) {
3762 fd = socket_listen(addr, errp);
3763 } else {
3764 fd = socket_connect(addr, errp, NULL, NULL);
3765 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003766 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003767 return NULL;
3768 }
3769 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3770 is_telnet, is_waitconnect, errp);
3771}
3772
Lei Li08d0ab32013-05-20 14:51:03 +08003773static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3774 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003775{
3776 int fd;
3777
Lei Li08d0ab32013-05-20 14:51:03 +08003778 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003779 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003780 return NULL;
3781 }
3782 return qemu_chr_open_udp_fd(fd);
3783}
3784
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003785ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3786 Error **errp)
3787{
3788 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003789 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003790
3791 chr = qemu_chr_find(id);
3792 if (chr) {
3793 error_setg(errp, "Chardev '%s' already exists", id);
3794 g_free(ret);
3795 return NULL;
3796 }
3797
3798 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003799 case CHARDEV_BACKEND_KIND_FILE:
3800 chr = qmp_chardev_open_file(backend->file, errp);
3801 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003802 case CHARDEV_BACKEND_KIND_SERIAL:
3803 chr = qmp_chardev_open_serial(backend->serial, errp);
3804 break;
3805 case CHARDEV_BACKEND_KIND_PARALLEL:
3806 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003807 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003808 case CHARDEV_BACKEND_KIND_PIPE:
3809 chr = qemu_chr_open_pipe(backend->pipe);
3810 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003811 case CHARDEV_BACKEND_KIND_SOCKET:
3812 chr = qmp_chardev_open_socket(backend->socket, errp);
3813 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003814 case CHARDEV_BACKEND_KIND_UDP:
3815 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003816 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003817#ifdef HAVE_CHARDEV_TTY
3818 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003819 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003820 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003821#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003822 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003823 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003824 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003825 case CHARDEV_BACKEND_KIND_MUX:
3826 base = qemu_chr_find(backend->mux->chardev);
3827 if (base == NULL) {
3828 error_setg(errp, "mux: base chardev %s not found",
3829 backend->mux->chardev);
3830 break;
3831 }
3832 chr = qemu_chr_open_mux(base);
3833 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003834 case CHARDEV_BACKEND_KIND_MSMOUSE:
3835 chr = qemu_chr_open_msmouse();
3836 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003837#ifdef CONFIG_BRLAPI
3838 case CHARDEV_BACKEND_KIND_BRAILLE:
3839 chr = chr_baum_init();
3840 break;
3841#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003842 case CHARDEV_BACKEND_KIND_STDIO:
3843 chr = qemu_chr_open_stdio(backend->stdio);
3844 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003845#ifdef _WIN32
3846 case CHARDEV_BACKEND_KIND_CONSOLE:
3847 chr = qemu_chr_open_win_con();
3848 break;
3849#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003850#ifdef CONFIG_SPICE
3851 case CHARDEV_BACKEND_KIND_SPICEVMC:
3852 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3853 break;
3854 case CHARDEV_BACKEND_KIND_SPICEPORT:
3855 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3856 break;
3857#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003858 case CHARDEV_BACKEND_KIND_VC:
3859 chr = vc_init(backend->vc);
3860 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003861 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003862 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003863 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003864 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003865 default:
3866 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3867 break;
3868 }
3869
Markus Armbruster3894c782014-05-19 18:57:36 +02003870 /*
3871 * Character backend open hasn't been fully converted to the Error
3872 * API. Some opens fail without setting an error. Set a generic
3873 * error then.
3874 * TODO full conversion to Error API
3875 */
3876 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003877 error_setg(errp, "Failed to create chardev");
3878 }
3879 if (chr) {
3880 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003881 chr->avail_connections =
3882 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02003883 if (!chr->filename) {
3884 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
3885 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05003886 if (!chr->explicit_be_open) {
3887 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3888 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003889 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3890 return ret;
3891 } else {
3892 g_free(ret);
3893 return NULL;
3894 }
3895}
3896
3897void qmp_chardev_remove(const char *id, Error **errp)
3898{
3899 CharDriverState *chr;
3900
3901 chr = qemu_chr_find(id);
3902 if (NULL == chr) {
3903 error_setg(errp, "Chardev '%s' not found", id);
3904 return;
3905 }
3906 if (chr->chr_can_read || chr->chr_read ||
3907 chr->chr_event || chr->handler_opaque) {
3908 error_setg(errp, "Chardev '%s' is busy", id);
3909 return;
3910 }
3911 qemu_chr_delete(chr);
3912}
Anthony Liguorid654f342013-03-05 23:21:28 +05303913
3914static void register_types(void)
3915{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003916 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303917 register_char_driver("socket", qemu_chr_open_socket);
3918 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02003919 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02003920 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003921 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3922 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003923 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3924 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003925 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3926 qemu_chr_parse_serial);
3927 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3928 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003929 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3930 qemu_chr_parse_parallel);
3931 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3932 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003933 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003934 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003935 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3936 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003937 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
3938 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02003939 /* Bug-compatibility: */
3940 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3941 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05003942 /* this must be done after machine init, since we register FEs with muxes
3943 * as part of realize functions like serial_isa_realizefn when -nographic
3944 * is specified
3945 */
3946 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05303947}
3948
3949type_init(register_types);