blob: f9709d9d0d77566d14054e48b937a53b8d3e36a2 [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
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300210int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
211{
212 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
213}
214
Daniel P. Berrange13661082011-06-23 13:31:42 +0100215int qemu_chr_add_client(CharDriverState *s, int fd)
216{
217 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
218}
219
aliguori6f97dba2008-10-31 18:49:55 +0000220void qemu_chr_accept_input(CharDriverState *s)
221{
222 if (s->chr_accept_input)
223 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100224 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000225}
226
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500227void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000228{
Amit Shah9bd78542009-11-03 19:59:54 +0530229 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000230 va_list ap;
231 va_start(ap, fmt);
232 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500233 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000234 va_end(ap);
235}
236
Amit Shah386a5a12013-08-28 15:24:05 +0530237static void remove_fd_in_watch(CharDriverState *chr);
238
aliguori6f97dba2008-10-31 18:49:55 +0000239void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100240 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000241 IOReadHandler *fd_read,
242 IOEventHandler *fd_event,
243 void *opaque)
244{
Hans de Goede19083222013-03-26 11:07:56 +0100245 int fe_open;
246
Amit Shahda7d9982011-04-25 15:18:22 +0530247 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100248 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530249 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100250 } else {
251 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530252 }
aliguori6f97dba2008-10-31 18:49:55 +0000253 s->chr_can_read = fd_can_read;
254 s->chr_read = fd_read;
255 s->chr_event = fd_event;
256 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200257 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000258 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200259
Hans de Goede19083222013-03-26 11:07:56 +0100260 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100261 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100262 }
263
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200264 /* We're connecting to an already opened device, so let's make sure we
265 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100266 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100267 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200268 }
aliguori6f97dba2008-10-31 18:49:55 +0000269}
270
271static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
272{
273 return len;
274}
275
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100276static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000277{
278 CharDriverState *chr;
279
Anthony Liguori7267c092011-08-20 22:09:37 -0500280 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000281 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500282 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100283 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000284}
285
286/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000287#define MAX_MUX 4
288#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
289#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
290typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100291 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000292 IOReadHandler *chr_read[MAX_MUX];
293 IOEventHandler *chr_event[MAX_MUX];
294 void *ext_opaque[MAX_MUX];
295 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200296 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000297 int mux_cnt;
298 int term_got_escape;
299 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000300 /* Intermediate input buffer allows to catch escape sequences even if the
301 currently active device is not accepting any input - but only until it
302 is full as well. */
303 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
304 int prod[MAX_MUX];
305 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200306 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200307 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200308 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000309} MuxDriver;
310
311
312static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
313{
314 MuxDriver *d = chr->opaque;
315 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200316 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000317 ret = d->drv->chr_write(d->drv, buf, len);
318 } else {
319 int i;
320
321 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200322 for (i = 0; i < len; i++) {
323 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000324 char buf1[64];
325 int64_t ti;
326 int secs;
327
Alex Blighbc72ad62013-08-21 16:03:08 +0100328 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200329 if (d->timestamps_start == -1)
330 d->timestamps_start = ti;
331 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000332 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000333 snprintf(buf1, sizeof(buf1),
334 "[%02d:%02d:%02d.%03d] ",
335 secs / 3600,
336 (secs / 60) % 60,
337 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000338 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000339 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200340 d->linestart = 0;
341 }
342 ret += d->drv->chr_write(d->drv, buf+i, 1);
343 if (buf[i] == '\n') {
344 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000345 }
346 }
347 }
348 return ret;
349}
350
351static const char * const mux_help[] = {
352 "% h print this help\n\r",
353 "% x exit emulator\n\r",
354 "% s save disk data back to file (if -snapshot)\n\r",
355 "% t toggle console timestamps\n\r"
356 "% b send break (magic sysrq)\n\r",
357 "% c switch between console and monitor\n\r",
358 "% % sends %\n\r",
359 NULL
360};
361
362int term_escape_char = 0x01; /* ctrl-a is used for escape */
363static void mux_print_help(CharDriverState *chr)
364{
365 int i, j;
366 char ebuf[15] = "Escape-Char";
367 char cbuf[50] = "\n\r";
368
369 if (term_escape_char > 0 && term_escape_char < 26) {
370 snprintf(cbuf, sizeof(cbuf), "\n\r");
371 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
372 } else {
373 snprintf(cbuf, sizeof(cbuf),
374 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
375 term_escape_char);
376 }
377 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
378 for (i = 0; mux_help[i] != NULL; i++) {
379 for (j=0; mux_help[i][j] != '\0'; j++) {
380 if (mux_help[i][j] == '%')
381 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
382 else
383 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
384 }
385 }
386}
387
aliguori2724b182009-03-05 23:01:47 +0000388static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
389{
390 if (d->chr_event[mux_nr])
391 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
392}
393
aliguori6f97dba2008-10-31 18:49:55 +0000394static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
395{
396 if (d->term_got_escape) {
397 d->term_got_escape = 0;
398 if (ch == term_escape_char)
399 goto send_char;
400 switch(ch) {
401 case '?':
402 case 'h':
403 mux_print_help(chr);
404 break;
405 case 'x':
406 {
407 const char *term = "QEMU: Terminated\n\r";
408 chr->chr_write(chr,(uint8_t *)term,strlen(term));
409 exit(0);
410 break;
411 }
412 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200413 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000414 break;
415 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100416 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000417 break;
418 case 'c':
419 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200420 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
421 d->focus++;
422 if (d->focus >= d->mux_cnt)
423 d->focus = 0;
424 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000425 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200426 case 't':
427 d->timestamps = !d->timestamps;
428 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200429 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200430 break;
aliguori6f97dba2008-10-31 18:49:55 +0000431 }
432 } else if (ch == term_escape_char) {
433 d->term_got_escape = 1;
434 } else {
435 send_char:
436 return 1;
437 }
438 return 0;
439}
440
441static void mux_chr_accept_input(CharDriverState *chr)
442{
aliguori6f97dba2008-10-31 18:49:55 +0000443 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200444 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000445
aliguoria80bf992009-03-05 23:00:02 +0000446 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000447 d->chr_can_read[m] &&
448 d->chr_can_read[m](d->ext_opaque[m])) {
449 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000450 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000451 }
452}
453
454static int mux_chr_can_read(void *opaque)
455{
456 CharDriverState *chr = opaque;
457 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200458 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000459
aliguoria80bf992009-03-05 23:00:02 +0000460 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000461 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000462 if (d->chr_can_read[m])
463 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000464 return 0;
465}
466
467static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
468{
469 CharDriverState *chr = opaque;
470 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200471 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000472 int i;
473
474 mux_chr_accept_input (opaque);
475
476 for(i = 0; i < size; i++)
477 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000478 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000479 d->chr_can_read[m] &&
480 d->chr_can_read[m](d->ext_opaque[m]))
481 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
482 else
aliguoria80bf992009-03-05 23:00:02 +0000483 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000484 }
485}
486
487static void mux_chr_event(void *opaque, int event)
488{
489 CharDriverState *chr = opaque;
490 MuxDriver *d = chr->opaque;
491 int i;
492
493 /* Send the event to all registered listeners */
494 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000495 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000496}
497
498static void mux_chr_update_read_handler(CharDriverState *chr)
499{
500 MuxDriver *d = chr->opaque;
501
502 if (d->mux_cnt >= MAX_MUX) {
503 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
504 return;
505 }
506 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
507 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
508 d->chr_read[d->mux_cnt] = chr->chr_read;
509 d->chr_event[d->mux_cnt] = chr->chr_event;
510 /* Fix up the real driver with mux routines */
511 if (d->mux_cnt == 0) {
512 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
513 mux_chr_event, chr);
514 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200515 if (d->focus != -1) {
516 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200517 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200518 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000519 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200520 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000521}
522
Michael Roth7b7ab182013-07-30 13:04:22 -0500523static bool muxes_realized;
524
525/**
526 * Called after processing of default and command-line-specified
527 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
528 * to a mux chardev. This is done here to ensure that
529 * output/prompts/banners are only displayed for the FE that has
530 * focus when initial command-line processing/machine init is
531 * completed.
532 *
533 * After this point, any new FE attached to any new or existing
534 * mux will receive CHR_EVENT_OPENED notifications for the BE
535 * immediately.
536 */
537static void muxes_realize_done(Notifier *notifier, void *unused)
538{
539 CharDriverState *chr;
540
541 QTAILQ_FOREACH(chr, &chardevs, next) {
542 if (chr->is_mux) {
543 MuxDriver *d = chr->opaque;
544 int i;
545
546 /* send OPENED to all already-attached FEs */
547 for (i = 0; i < d->mux_cnt; i++) {
548 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
549 }
550 /* mark mux as OPENED so any new FEs will immediately receive
551 * OPENED event
552 */
553 qemu_chr_be_generic_open(chr);
554 }
555 }
556 muxes_realized = true;
557}
558
559static Notifier muxes_realize_notify = {
560 .notify = muxes_realize_done,
561};
562
aliguori6f97dba2008-10-31 18:49:55 +0000563static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
564{
565 CharDriverState *chr;
566 MuxDriver *d;
567
Anthony Liguori7267c092011-08-20 22:09:37 -0500568 chr = g_malloc0(sizeof(CharDriverState));
569 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000570
571 chr->opaque = d;
572 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200573 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000574 chr->chr_write = mux_chr_write;
575 chr->chr_update_read_handler = mux_chr_update_read_handler;
576 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100577 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100578 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500579 /* only default to opened state if we've realized the initial
580 * set of muxes
581 */
582 chr->explicit_be_open = muxes_realized ? 0 : 1;
583 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200584
aliguori6f97dba2008-10-31 18:49:55 +0000585 return chr;
586}
587
588
589#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000590int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000591{
592 int ret, len;
593
594 len = len1;
595 while (len > 0) {
596 ret = send(fd, buf, len, 0);
597 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000598 errno = WSAGetLastError();
599 if (errno != WSAEWOULDBLOCK) {
600 return -1;
601 }
602 } else if (ret == 0) {
603 break;
604 } else {
605 buf += ret;
606 len -= ret;
607 }
608 }
609 return len1 - len;
610}
611
612#else
613
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100614int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000615{
616 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100617 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000618
619 len = len1;
620 while (len > 0) {
621 ret = write(fd, buf, len);
622 if (ret < 0) {
623 if (errno != EINTR && errno != EAGAIN)
624 return -1;
625 } else if (ret == 0) {
626 break;
627 } else {
628 buf += ret;
629 len -= ret;
630 }
631 }
632 return len1 - len;
633}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500634
635int recv_all(int fd, void *_buf, int len1, bool single_read)
636{
637 int ret, len;
638 uint8_t *buf = _buf;
639
640 len = len1;
641 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
642 if (ret < 0) {
643 if (errno != EINTR && errno != EAGAIN) {
644 return -1;
645 }
646 continue;
647 } else {
648 if (single_read) {
649 return ret;
650 }
651 buf += ret;
652 len -= ret;
653 }
654 }
655 return len1 - len;
656}
657
aliguori6f97dba2008-10-31 18:49:55 +0000658#endif /* !_WIN32 */
659
Anthony Liguori96c63842013-03-05 23:21:18 +0530660typedef struct IOWatchPoll
661{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200662 GSource parent;
663
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200664 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530665 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530666
667 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200668 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530669 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530670} IOWatchPoll;
671
Anthony Liguori96c63842013-03-05 23:21:18 +0530672static IOWatchPoll *io_watch_poll_from_source(GSource *source)
673{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200674 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530675}
676
677static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
678{
679 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200680 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200681 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200682 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530683 return FALSE;
684 }
685
Paolo Bonzinid185c092013-04-05 17:59:33 +0200686 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200687 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
688 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200689 g_source_attach(iwp->src, NULL);
690 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200691 g_source_destroy(iwp->src);
692 g_source_unref(iwp->src);
693 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200694 }
695 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530696}
697
698static gboolean io_watch_poll_check(GSource *source)
699{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200700 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530701}
702
703static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
704 gpointer user_data)
705{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200706 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530707}
708
709static void io_watch_poll_finalize(GSource *source)
710{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200711 /* Due to a glib bug, removing the last reference to a source
712 * inside a finalize callback causes recursive locking (and a
713 * deadlock). This is not a problem inside other callbacks,
714 * including dispatch callbacks, so we call io_remove_watch_poll
715 * to remove this source. At this point, iwp->src must
716 * be NULL, or we would leak it.
717 *
718 * This would be solved much more elegantly by child sources,
719 * but we support older glib versions that do not have them.
720 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530721 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200722 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530723}
724
725static GSourceFuncs io_watch_poll_funcs = {
726 .prepare = io_watch_poll_prepare,
727 .check = io_watch_poll_check,
728 .dispatch = io_watch_poll_dispatch,
729 .finalize = io_watch_poll_finalize,
730};
731
732/* Can only be used for read */
733static guint io_add_watch_poll(GIOChannel *channel,
734 IOCanReadHandler *fd_can_read,
735 GIOFunc fd_read,
736 gpointer user_data)
737{
738 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200739 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530740
Paolo Bonzinid185c092013-04-05 17:59:33 +0200741 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530742 iwp->fd_can_read = fd_can_read;
743 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200744 iwp->channel = channel;
745 iwp->fd_read = (GSourceFunc) fd_read;
746 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530747
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200748 tag = g_source_attach(&iwp->parent, NULL);
749 g_source_unref(&iwp->parent);
750 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530751}
752
Paolo Bonzini2b316772013-04-19 17:32:09 +0200753static void io_remove_watch_poll(guint tag)
754{
755 GSource *source;
756 IOWatchPoll *iwp;
757
758 g_return_if_fail (tag > 0);
759
760 source = g_main_context_find_source_by_id(NULL, tag);
761 g_return_if_fail (source != NULL);
762
763 iwp = io_watch_poll_from_source(source);
764 if (iwp->src) {
765 g_source_destroy(iwp->src);
766 g_source_unref(iwp->src);
767 iwp->src = NULL;
768 }
769 g_source_destroy(&iwp->parent);
770}
771
Amit Shah26da70c2013-08-28 15:23:37 +0530772static void remove_fd_in_watch(CharDriverState *chr)
773{
774 if (chr->fd_in_tag) {
775 io_remove_watch_poll(chr->fd_in_tag);
776 chr->fd_in_tag = 0;
777 }
778}
779
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000780#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530781static GIOChannel *io_channel_from_fd(int fd)
782{
783 GIOChannel *chan;
784
785 if (fd == -1) {
786 return NULL;
787 }
788
789 chan = g_io_channel_unix_new(fd);
790
791 g_io_channel_set_encoding(chan, NULL, NULL);
792 g_io_channel_set_buffered(chan, FALSE);
793
794 return chan;
795}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000796#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530797
Anthony Liguori76a96442013-03-05 23:21:21 +0530798static GIOChannel *io_channel_from_socket(int fd)
799{
800 GIOChannel *chan;
801
802 if (fd == -1) {
803 return NULL;
804 }
805
806#ifdef _WIN32
807 chan = g_io_channel_win32_new_socket(fd);
808#else
809 chan = g_io_channel_unix_new(fd);
810#endif
811
812 g_io_channel_set_encoding(chan, NULL, NULL);
813 g_io_channel_set_buffered(chan, FALSE);
814
815 return chan;
816}
817
Anthony Liguori684a0962013-03-29 11:39:50 -0500818static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530819{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200820 size_t offset = 0;
821 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530822
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200823 while (offset < len && status == G_IO_STATUS_NORMAL) {
824 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500825
826 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530827 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500828 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530829 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500830
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200831 if (offset > 0) {
832 return offset;
833 }
834 switch (status) {
835 case G_IO_STATUS_NORMAL:
836 g_assert(len == 0);
837 return 0;
838 case G_IO_STATUS_AGAIN:
839 errno = EAGAIN;
840 return -1;
841 default:
842 break;
843 }
844 errno = EINVAL;
845 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530846}
Anthony Liguori96c63842013-03-05 23:21:18 +0530847
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000848#ifndef _WIN32
849
Anthony Liguoria29753f2013-03-05 23:21:19 +0530850typedef struct FDCharDriver {
851 CharDriverState *chr;
852 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000853 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530854 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000855} FDCharDriver;
856
aliguori6f97dba2008-10-31 18:49:55 +0000857static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
858{
859 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530860
Anthony Liguori684a0962013-03-29 11:39:50 -0500861 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530862}
863
864static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
865{
866 CharDriverState *chr = opaque;
867 FDCharDriver *s = chr->opaque;
868 int len;
869 uint8_t buf[READ_BUF_LEN];
870 GIOStatus status;
871 gsize bytes_read;
872
873 len = sizeof(buf);
874 if (len > s->max_size) {
875 len = s->max_size;
876 }
877 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200878 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530879 }
880
881 status = g_io_channel_read_chars(chan, (gchar *)buf,
882 len, &bytes_read, NULL);
883 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530884 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530885 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
886 return FALSE;
887 }
888 if (status == G_IO_STATUS_NORMAL) {
889 qemu_chr_be_write(chr, buf, bytes_read);
890 }
891
892 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000893}
894
895static int fd_chr_read_poll(void *opaque)
896{
897 CharDriverState *chr = opaque;
898 FDCharDriver *s = chr->opaque;
899
Anthony Liguori909cda12011-08-15 11:17:31 -0500900 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000901 return s->max_size;
902}
903
Anthony Liguori23673ca2013-03-05 23:21:23 +0530904static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
905{
906 FDCharDriver *s = chr->opaque;
907 return g_io_create_watch(s->fd_out, cond);
908}
909
aliguori6f97dba2008-10-31 18:49:55 +0000910static void fd_chr_update_read_handler(CharDriverState *chr)
911{
912 FDCharDriver *s = chr->opaque;
913
Amit Shah26da70c2013-08-28 15:23:37 +0530914 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530915 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530916 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
917 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000918 }
919}
920
921static void fd_chr_close(struct CharDriverState *chr)
922{
923 FDCharDriver *s = chr->opaque;
924
Amit Shah26da70c2013-08-28 15:23:37 +0530925 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530926 if (s->fd_in) {
927 g_io_channel_unref(s->fd_in);
928 }
929 if (s->fd_out) {
930 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000931 }
932
Anthony Liguori7267c092011-08-20 22:09:37 -0500933 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100934 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000935}
936
937/* open a character device to a unix fd */
938static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
939{
940 CharDriverState *chr;
941 FDCharDriver *s;
942
Anthony Liguori7267c092011-08-20 22:09:37 -0500943 chr = g_malloc0(sizeof(CharDriverState));
944 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530945 s->fd_in = io_channel_from_fd(fd_in);
946 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530947 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530948 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000949 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530950 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000951 chr->chr_write = fd_chr_write;
952 chr->chr_update_read_handler = fd_chr_update_read_handler;
953 chr->chr_close = fd_chr_close;
954
aliguori6f97dba2008-10-31 18:49:55 +0000955 return chr;
956}
957
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100958static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000959{
960 int fd_in, fd_out;
961 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100962 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200963
964 if (filename == NULL) {
965 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100966 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200967 }
aliguori6f97dba2008-10-31 18:49:55 +0000968
969 snprintf(filename_in, 256, "%s.in", filename);
970 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100971 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
972 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000973 if (fd_in < 0 || fd_out < 0) {
974 if (fd_in >= 0)
975 close(fd_in);
976 if (fd_out >= 0)
977 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100978 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100979 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100980 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100981 }
aliguori6f97dba2008-10-31 18:49:55 +0000982 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100983 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000984}
985
aliguori6f97dba2008-10-31 18:49:55 +0000986/* init terminal so that we can grab keys */
987static struct termios oldtty;
988static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100989static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000990
991static void term_exit(void)
992{
993 tcsetattr (0, TCSANOW, &oldtty);
994 fcntl(0, F_SETFL, old_fd0_flags);
995}
996
Paolo Bonzinibb002512010-12-23 13:42:50 +0100997static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000998{
999 struct termios tty;
1000
Paolo Bonzini03693642010-12-23 13:42:49 +01001001 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001002 if (!echo) {
1003 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001004 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001005 tty.c_oflag |= OPOST;
1006 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1007 tty.c_cflag &= ~(CSIZE|PARENB);
1008 tty.c_cflag |= CS8;
1009 tty.c_cc[VMIN] = 1;
1010 tty.c_cc[VTIME] = 0;
1011 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001012 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001013 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001014
1015 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001016}
1017
1018static void qemu_chr_close_stdio(struct CharDriverState *chr)
1019{
1020 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001021 fd_chr_close(chr);
1022}
1023
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001024static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001025{
1026 CharDriverState *chr;
1027
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001028 if (is_daemonized()) {
1029 error_report("cannot use stdio with -daemonize");
1030 return NULL;
1031 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301032 old_fd0_flags = fcntl(0, F_GETFL);
1033 tcgetattr (0, &oldtty);
1034 fcntl(0, F_SETFL, O_NONBLOCK);
1035 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001036
aliguori6f97dba2008-10-31 18:49:55 +00001037 chr = qemu_chr_open_fd(0, 1);
1038 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001039 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001040 if (opts->has_signal) {
1041 stdio_allow_signal = opts->signal;
1042 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001043 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001044
Markus Armbruster1f514702012-02-07 15:09:08 +01001045 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001046}
1047
aliguori6f97dba2008-10-31 18:49:55 +00001048#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001049 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1050 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001051
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001052#define HAVE_CHARDEV_TTY 1
1053
aliguori6f97dba2008-10-31 18:49:55 +00001054typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301055 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001056 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001057 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301058 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001059} PtyCharDriver;
1060
1061static void pty_chr_update_read_handler(CharDriverState *chr);
1062static void pty_chr_state(CharDriverState *chr, int connected);
1063
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301064static gboolean pty_chr_timer(gpointer opaque)
1065{
1066 struct CharDriverState *chr = opaque;
1067 PtyCharDriver *s = chr->opaque;
1068
Hans de Goede79f20072013-04-25 13:53:02 +02001069 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001070 if (!s->connected) {
1071 /* Next poll ... */
1072 pty_chr_update_read_handler(chr);
1073 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301074 return FALSE;
1075}
1076
1077static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1078{
1079 PtyCharDriver *s = chr->opaque;
1080
1081 if (s->timer_tag) {
1082 g_source_remove(s->timer_tag);
1083 s->timer_tag = 0;
1084 }
1085
1086 if (ms == 1000) {
1087 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1088 } else {
1089 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1090 }
1091}
1092
aliguori6f97dba2008-10-31 18:49:55 +00001093static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1094{
1095 PtyCharDriver *s = chr->opaque;
1096
1097 if (!s->connected) {
1098 /* guest sends data, check for (re-)connect */
1099 pty_chr_update_read_handler(chr);
1100 return 0;
1101 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001102 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001103}
1104
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301105static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1106{
1107 PtyCharDriver *s = chr->opaque;
1108 return g_io_create_watch(s->fd, cond);
1109}
1110
aliguori6f97dba2008-10-31 18:49:55 +00001111static int pty_chr_read_poll(void *opaque)
1112{
1113 CharDriverState *chr = opaque;
1114 PtyCharDriver *s = chr->opaque;
1115
Anthony Liguori909cda12011-08-15 11:17:31 -05001116 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001117 return s->read_bytes;
1118}
1119
Anthony Liguori093d3a22013-03-05 23:21:20 +05301120static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001121{
1122 CharDriverState *chr = opaque;
1123 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301124 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301125 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301126 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001127
1128 len = sizeof(buf);
1129 if (len > s->read_bytes)
1130 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001131 if (len == 0) {
1132 return TRUE;
1133 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301134 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1135 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001136 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301137 return FALSE;
1138 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001139 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001140 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001141 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301142 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001143}
1144
1145static void pty_chr_update_read_handler(CharDriverState *chr)
1146{
1147 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001148 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001149
Paolo Bonzini85a67692013-04-19 17:32:07 +02001150 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1151 pfd.events = G_IO_OUT;
1152 pfd.revents = 0;
1153 g_poll(&pfd, 1, 0);
1154 if (pfd.revents & G_IO_HUP) {
1155 pty_chr_state(chr, 0);
1156 } else {
1157 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301158 }
aliguori6f97dba2008-10-31 18:49:55 +00001159}
1160
1161static void pty_chr_state(CharDriverState *chr, int connected)
1162{
1163 PtyCharDriver *s = chr->opaque;
1164
1165 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301166 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001167 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001168 /* (re-)connect poll interval for idle guests: once per second.
1169 * We check more frequently in case the guests sends data to
1170 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301171 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001172 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001173 if (s->timer_tag) {
1174 g_source_remove(s->timer_tag);
1175 s->timer_tag = 0;
1176 }
1177 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001178 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001179 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001180 }
1181 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301182 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1183 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001184 }
aliguori6f97dba2008-10-31 18:49:55 +00001185 }
1186}
1187
aliguori6f97dba2008-10-31 18:49:55 +00001188static void pty_chr_close(struct CharDriverState *chr)
1189{
1190 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301191 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001192
Amit Shah26da70c2013-08-28 15:23:37 +05301193 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301194 fd = g_io_channel_unix_get_fd(s->fd);
1195 g_io_channel_unref(s->fd);
1196 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301197 if (s->timer_tag) {
1198 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001199 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301200 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001201 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001202 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001203}
1204
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001205static CharDriverState *qemu_chr_open_pty(const char *id,
1206 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001207{
1208 CharDriverState *chr;
1209 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001210 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001211 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001212
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001213 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1214 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001215 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001216 }
1217
aliguori6f97dba2008-10-31 18:49:55 +00001218 close(slave_fd);
1219
Markus Armbrustera4e26042011-11-11 10:40:05 +01001220 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001221
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001222 chr->filename = g_strdup_printf("pty:%s", pty_name);
1223 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001224 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001225
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001226 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001227 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001228
1229 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001230 chr->opaque = s;
1231 chr->chr_write = pty_chr_write;
1232 chr->chr_update_read_handler = pty_chr_update_read_handler;
1233 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301234 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001235 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001236
Anthony Liguori093d3a22013-03-05 23:21:20 +05301237 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301238 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001239
Markus Armbruster1f514702012-02-07 15:09:08 +01001240 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001241}
1242
1243static void tty_serial_init(int fd, int speed,
1244 int parity, int data_bits, int stop_bits)
1245{
1246 struct termios tty;
1247 speed_t spd;
1248
1249#if 0
1250 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1251 speed, parity, data_bits, stop_bits);
1252#endif
1253 tcgetattr (fd, &tty);
1254
Stefan Weil45eea132009-10-26 16:10:10 +01001255#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1256 speed = speed * 10 / 11;
1257 do {
1258 check_speed(50);
1259 check_speed(75);
1260 check_speed(110);
1261 check_speed(134);
1262 check_speed(150);
1263 check_speed(200);
1264 check_speed(300);
1265 check_speed(600);
1266 check_speed(1200);
1267 check_speed(1800);
1268 check_speed(2400);
1269 check_speed(4800);
1270 check_speed(9600);
1271 check_speed(19200);
1272 check_speed(38400);
1273 /* Non-Posix values follow. They may be unsupported on some systems. */
1274 check_speed(57600);
1275 check_speed(115200);
1276#ifdef B230400
1277 check_speed(230400);
1278#endif
1279#ifdef B460800
1280 check_speed(460800);
1281#endif
1282#ifdef B500000
1283 check_speed(500000);
1284#endif
1285#ifdef B576000
1286 check_speed(576000);
1287#endif
1288#ifdef B921600
1289 check_speed(921600);
1290#endif
1291#ifdef B1000000
1292 check_speed(1000000);
1293#endif
1294#ifdef B1152000
1295 check_speed(1152000);
1296#endif
1297#ifdef B1500000
1298 check_speed(1500000);
1299#endif
1300#ifdef B2000000
1301 check_speed(2000000);
1302#endif
1303#ifdef B2500000
1304 check_speed(2500000);
1305#endif
1306#ifdef B3000000
1307 check_speed(3000000);
1308#endif
1309#ifdef B3500000
1310 check_speed(3500000);
1311#endif
1312#ifdef B4000000
1313 check_speed(4000000);
1314#endif
aliguori6f97dba2008-10-31 18:49:55 +00001315 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001316 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001317
1318 cfsetispeed(&tty, spd);
1319 cfsetospeed(&tty, spd);
1320
1321 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1322 |INLCR|IGNCR|ICRNL|IXON);
1323 tty.c_oflag |= OPOST;
1324 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1325 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1326 switch(data_bits) {
1327 default:
1328 case 8:
1329 tty.c_cflag |= CS8;
1330 break;
1331 case 7:
1332 tty.c_cflag |= CS7;
1333 break;
1334 case 6:
1335 tty.c_cflag |= CS6;
1336 break;
1337 case 5:
1338 tty.c_cflag |= CS5;
1339 break;
1340 }
1341 switch(parity) {
1342 default:
1343 case 'N':
1344 break;
1345 case 'E':
1346 tty.c_cflag |= PARENB;
1347 break;
1348 case 'O':
1349 tty.c_cflag |= PARENB | PARODD;
1350 break;
1351 }
1352 if (stop_bits == 2)
1353 tty.c_cflag |= CSTOPB;
1354
1355 tcsetattr (fd, TCSANOW, &tty);
1356}
1357
1358static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1359{
1360 FDCharDriver *s = chr->opaque;
1361
1362 switch(cmd) {
1363 case CHR_IOCTL_SERIAL_SET_PARAMS:
1364 {
1365 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301366 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1367 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001368 ssp->data_bits, ssp->stop_bits);
1369 }
1370 break;
1371 case CHR_IOCTL_SERIAL_SET_BREAK:
1372 {
1373 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301374 if (enable) {
1375 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1376 }
aliguori6f97dba2008-10-31 18:49:55 +00001377 }
1378 break;
1379 case CHR_IOCTL_SERIAL_GET_TIOCM:
1380 {
1381 int sarg = 0;
1382 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301383 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001384 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001385 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001386 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001387 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001388 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001389 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001390 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001391 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001392 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001393 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001394 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001395 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001396 *targ |= CHR_TIOCM_RTS;
1397 }
1398 break;
1399 case CHR_IOCTL_SERIAL_SET_TIOCM:
1400 {
1401 int sarg = *(int *)arg;
1402 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301403 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001404 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1405 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1406 if (sarg & CHR_TIOCM_CTS)
1407 targ |= TIOCM_CTS;
1408 if (sarg & CHR_TIOCM_CAR)
1409 targ |= TIOCM_CAR;
1410 if (sarg & CHR_TIOCM_DSR)
1411 targ |= TIOCM_DSR;
1412 if (sarg & CHR_TIOCM_RI)
1413 targ |= TIOCM_RI;
1414 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001415 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001416 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001417 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301418 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001419 }
1420 break;
1421 default:
1422 return -ENOTSUP;
1423 }
1424 return 0;
1425}
1426
David Ahern4266a132010-02-10 18:27:17 -07001427static void qemu_chr_close_tty(CharDriverState *chr)
1428{
1429 FDCharDriver *s = chr->opaque;
1430 int fd = -1;
1431
1432 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301433 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001434 }
1435
1436 fd_chr_close(chr);
1437
1438 if (fd >= 0) {
1439 close(fd);
1440 }
1441}
1442
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001443static CharDriverState *qemu_chr_open_tty_fd(int fd)
1444{
1445 CharDriverState *chr;
1446
1447 tty_serial_init(fd, 115200, 'N', 8, 1);
1448 chr = qemu_chr_open_fd(fd, fd);
1449 chr->chr_ioctl = tty_serial_ioctl;
1450 chr->chr_close = qemu_chr_close_tty;
1451 return chr;
1452}
aliguori6f97dba2008-10-31 18:49:55 +00001453#endif /* __linux__ || __sun__ */
1454
1455#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001456
1457#define HAVE_CHARDEV_PARPORT 1
1458
aliguori6f97dba2008-10-31 18:49:55 +00001459typedef struct {
1460 int fd;
1461 int mode;
1462} ParallelCharDriver;
1463
1464static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1465{
1466 if (s->mode != mode) {
1467 int m = mode;
1468 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1469 return 0;
1470 s->mode = mode;
1471 }
1472 return 1;
1473}
1474
1475static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1476{
1477 ParallelCharDriver *drv = chr->opaque;
1478 int fd = drv->fd;
1479 uint8_t b;
1480
1481 switch(cmd) {
1482 case CHR_IOCTL_PP_READ_DATA:
1483 if (ioctl(fd, PPRDATA, &b) < 0)
1484 return -ENOTSUP;
1485 *(uint8_t *)arg = b;
1486 break;
1487 case CHR_IOCTL_PP_WRITE_DATA:
1488 b = *(uint8_t *)arg;
1489 if (ioctl(fd, PPWDATA, &b) < 0)
1490 return -ENOTSUP;
1491 break;
1492 case CHR_IOCTL_PP_READ_CONTROL:
1493 if (ioctl(fd, PPRCONTROL, &b) < 0)
1494 return -ENOTSUP;
1495 /* Linux gives only the lowest bits, and no way to know data
1496 direction! For better compatibility set the fixed upper
1497 bits. */
1498 *(uint8_t *)arg = b | 0xc0;
1499 break;
1500 case CHR_IOCTL_PP_WRITE_CONTROL:
1501 b = *(uint8_t *)arg;
1502 if (ioctl(fd, PPWCONTROL, &b) < 0)
1503 return -ENOTSUP;
1504 break;
1505 case CHR_IOCTL_PP_READ_STATUS:
1506 if (ioctl(fd, PPRSTATUS, &b) < 0)
1507 return -ENOTSUP;
1508 *(uint8_t *)arg = b;
1509 break;
1510 case CHR_IOCTL_PP_DATA_DIR:
1511 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1512 return -ENOTSUP;
1513 break;
1514 case CHR_IOCTL_PP_EPP_READ_ADDR:
1515 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1516 struct ParallelIOArg *parg = arg;
1517 int n = read(fd, parg->buffer, parg->count);
1518 if (n != parg->count) {
1519 return -EIO;
1520 }
1521 }
1522 break;
1523 case CHR_IOCTL_PP_EPP_READ:
1524 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1525 struct ParallelIOArg *parg = arg;
1526 int n = read(fd, parg->buffer, parg->count);
1527 if (n != parg->count) {
1528 return -EIO;
1529 }
1530 }
1531 break;
1532 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1533 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1534 struct ParallelIOArg *parg = arg;
1535 int n = write(fd, parg->buffer, parg->count);
1536 if (n != parg->count) {
1537 return -EIO;
1538 }
1539 }
1540 break;
1541 case CHR_IOCTL_PP_EPP_WRITE:
1542 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1543 struct ParallelIOArg *parg = arg;
1544 int n = write(fd, parg->buffer, parg->count);
1545 if (n != parg->count) {
1546 return -EIO;
1547 }
1548 }
1549 break;
1550 default:
1551 return -ENOTSUP;
1552 }
1553 return 0;
1554}
1555
1556static void pp_close(CharDriverState *chr)
1557{
1558 ParallelCharDriver *drv = chr->opaque;
1559 int fd = drv->fd;
1560
1561 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1562 ioctl(fd, PPRELEASE);
1563 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001564 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001565 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001566}
1567
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001568static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001569{
1570 CharDriverState *chr;
1571 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001572
1573 if (ioctl(fd, PPCLAIM) < 0) {
1574 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001575 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001576 }
1577
Anthony Liguori7267c092011-08-20 22:09:37 -05001578 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001579 drv->fd = fd;
1580 drv->mode = IEEE1284_MODE_COMPAT;
1581
Anthony Liguori7267c092011-08-20 22:09:37 -05001582 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001583 chr->chr_write = null_chr_write;
1584 chr->chr_ioctl = pp_ioctl;
1585 chr->chr_close = pp_close;
1586 chr->opaque = drv;
1587
Markus Armbruster1f514702012-02-07 15:09:08 +01001588 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001589}
1590#endif /* __linux__ */
1591
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001592#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001593
1594#define HAVE_CHARDEV_PARPORT 1
1595
blueswir16972f932008-11-22 20:49:12 +00001596static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1597{
Stefan Weile0efb992011-02-23 19:09:16 +01001598 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001599 uint8_t b;
1600
1601 switch(cmd) {
1602 case CHR_IOCTL_PP_READ_DATA:
1603 if (ioctl(fd, PPIGDATA, &b) < 0)
1604 return -ENOTSUP;
1605 *(uint8_t *)arg = b;
1606 break;
1607 case CHR_IOCTL_PP_WRITE_DATA:
1608 b = *(uint8_t *)arg;
1609 if (ioctl(fd, PPISDATA, &b) < 0)
1610 return -ENOTSUP;
1611 break;
1612 case CHR_IOCTL_PP_READ_CONTROL:
1613 if (ioctl(fd, PPIGCTRL, &b) < 0)
1614 return -ENOTSUP;
1615 *(uint8_t *)arg = b;
1616 break;
1617 case CHR_IOCTL_PP_WRITE_CONTROL:
1618 b = *(uint8_t *)arg;
1619 if (ioctl(fd, PPISCTRL, &b) < 0)
1620 return -ENOTSUP;
1621 break;
1622 case CHR_IOCTL_PP_READ_STATUS:
1623 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1624 return -ENOTSUP;
1625 *(uint8_t *)arg = b;
1626 break;
1627 default:
1628 return -ENOTSUP;
1629 }
1630 return 0;
1631}
1632
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001633static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001634{
1635 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001636
Anthony Liguori7267c092011-08-20 22:09:37 -05001637 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001638 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001639 chr->chr_write = null_chr_write;
1640 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001641 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001642 return chr;
blueswir16972f932008-11-22 20:49:12 +00001643}
1644#endif
1645
aliguori6f97dba2008-10-31 18:49:55 +00001646#else /* _WIN32 */
1647
1648typedef struct {
1649 int max_size;
1650 HANDLE hcom, hrecv, hsend;
1651 OVERLAPPED orecv, osend;
1652 BOOL fpipe;
1653 DWORD len;
1654} WinCharState;
1655
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001656typedef struct {
1657 HANDLE hStdIn;
1658 HANDLE hInputReadyEvent;
1659 HANDLE hInputDoneEvent;
1660 HANDLE hInputThread;
1661 uint8_t win_stdio_buf;
1662} WinStdioCharState;
1663
aliguori6f97dba2008-10-31 18:49:55 +00001664#define NSENDBUF 2048
1665#define NRECVBUF 2048
1666#define MAXCONNECT 1
1667#define NTIMEOUT 5000
1668
1669static int win_chr_poll(void *opaque);
1670static int win_chr_pipe_poll(void *opaque);
1671
1672static void win_chr_close(CharDriverState *chr)
1673{
1674 WinCharState *s = chr->opaque;
1675
1676 if (s->hsend) {
1677 CloseHandle(s->hsend);
1678 s->hsend = NULL;
1679 }
1680 if (s->hrecv) {
1681 CloseHandle(s->hrecv);
1682 s->hrecv = NULL;
1683 }
1684 if (s->hcom) {
1685 CloseHandle(s->hcom);
1686 s->hcom = NULL;
1687 }
1688 if (s->fpipe)
1689 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1690 else
1691 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301692
Hans de Goedea425d232011-11-19 10:22:43 +01001693 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001694}
1695
1696static int win_chr_init(CharDriverState *chr, const char *filename)
1697{
1698 WinCharState *s = chr->opaque;
1699 COMMCONFIG comcfg;
1700 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1701 COMSTAT comstat;
1702 DWORD size;
1703 DWORD err;
1704
1705 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1706 if (!s->hsend) {
1707 fprintf(stderr, "Failed CreateEvent\n");
1708 goto fail;
1709 }
1710 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1711 if (!s->hrecv) {
1712 fprintf(stderr, "Failed CreateEvent\n");
1713 goto fail;
1714 }
1715
1716 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1717 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1718 if (s->hcom == INVALID_HANDLE_VALUE) {
1719 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1720 s->hcom = NULL;
1721 goto fail;
1722 }
1723
1724 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1725 fprintf(stderr, "Failed SetupComm\n");
1726 goto fail;
1727 }
1728
1729 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1730 size = sizeof(COMMCONFIG);
1731 GetDefaultCommConfig(filename, &comcfg, &size);
1732 comcfg.dcb.DCBlength = sizeof(DCB);
1733 CommConfigDialog(filename, NULL, &comcfg);
1734
1735 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1736 fprintf(stderr, "Failed SetCommState\n");
1737 goto fail;
1738 }
1739
1740 if (!SetCommMask(s->hcom, EV_ERR)) {
1741 fprintf(stderr, "Failed SetCommMask\n");
1742 goto fail;
1743 }
1744
1745 cto.ReadIntervalTimeout = MAXDWORD;
1746 if (!SetCommTimeouts(s->hcom, &cto)) {
1747 fprintf(stderr, "Failed SetCommTimeouts\n");
1748 goto fail;
1749 }
1750
1751 if (!ClearCommError(s->hcom, &err, &comstat)) {
1752 fprintf(stderr, "Failed ClearCommError\n");
1753 goto fail;
1754 }
1755 qemu_add_polling_cb(win_chr_poll, chr);
1756 return 0;
1757
1758 fail:
1759 win_chr_close(chr);
1760 return -1;
1761}
1762
1763static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1764{
1765 WinCharState *s = chr->opaque;
1766 DWORD len, ret, size, err;
1767
1768 len = len1;
1769 ZeroMemory(&s->osend, sizeof(s->osend));
1770 s->osend.hEvent = s->hsend;
1771 while (len > 0) {
1772 if (s->hsend)
1773 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1774 else
1775 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1776 if (!ret) {
1777 err = GetLastError();
1778 if (err == ERROR_IO_PENDING) {
1779 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1780 if (ret) {
1781 buf += size;
1782 len -= size;
1783 } else {
1784 break;
1785 }
1786 } else {
1787 break;
1788 }
1789 } else {
1790 buf += size;
1791 len -= size;
1792 }
1793 }
1794 return len1 - len;
1795}
1796
1797static int win_chr_read_poll(CharDriverState *chr)
1798{
1799 WinCharState *s = chr->opaque;
1800
Anthony Liguori909cda12011-08-15 11:17:31 -05001801 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001802 return s->max_size;
1803}
1804
1805static void win_chr_readfile(CharDriverState *chr)
1806{
1807 WinCharState *s = chr->opaque;
1808 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301809 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001810 DWORD size;
1811
1812 ZeroMemory(&s->orecv, sizeof(s->orecv));
1813 s->orecv.hEvent = s->hrecv;
1814 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1815 if (!ret) {
1816 err = GetLastError();
1817 if (err == ERROR_IO_PENDING) {
1818 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1819 }
1820 }
1821
1822 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001823 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001824 }
1825}
1826
1827static void win_chr_read(CharDriverState *chr)
1828{
1829 WinCharState *s = chr->opaque;
1830
1831 if (s->len > s->max_size)
1832 s->len = s->max_size;
1833 if (s->len == 0)
1834 return;
1835
1836 win_chr_readfile(chr);
1837}
1838
1839static int win_chr_poll(void *opaque)
1840{
1841 CharDriverState *chr = opaque;
1842 WinCharState *s = chr->opaque;
1843 COMSTAT status;
1844 DWORD comerr;
1845
1846 ClearCommError(s->hcom, &comerr, &status);
1847 if (status.cbInQue > 0) {
1848 s->len = status.cbInQue;
1849 win_chr_read_poll(chr);
1850 win_chr_read(chr);
1851 return 1;
1852 }
1853 return 0;
1854}
1855
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001856static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001857{
1858 CharDriverState *chr;
1859 WinCharState *s;
1860
Anthony Liguori7267c092011-08-20 22:09:37 -05001861 chr = g_malloc0(sizeof(CharDriverState));
1862 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001863 chr->opaque = s;
1864 chr->chr_write = win_chr_write;
1865 chr->chr_close = win_chr_close;
1866
1867 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001868 g_free(s);
1869 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001870 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001871 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001872 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001873}
1874
1875static int win_chr_pipe_poll(void *opaque)
1876{
1877 CharDriverState *chr = opaque;
1878 WinCharState *s = chr->opaque;
1879 DWORD size;
1880
1881 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1882 if (size > 0) {
1883 s->len = size;
1884 win_chr_read_poll(chr);
1885 win_chr_read(chr);
1886 return 1;
1887 }
1888 return 0;
1889}
1890
1891static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1892{
1893 WinCharState *s = chr->opaque;
1894 OVERLAPPED ov;
1895 int ret;
1896 DWORD size;
1897 char openname[256];
1898
1899 s->fpipe = TRUE;
1900
1901 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1902 if (!s->hsend) {
1903 fprintf(stderr, "Failed CreateEvent\n");
1904 goto fail;
1905 }
1906 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1907 if (!s->hrecv) {
1908 fprintf(stderr, "Failed CreateEvent\n");
1909 goto fail;
1910 }
1911
1912 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1913 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1914 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1915 PIPE_WAIT,
1916 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1917 if (s->hcom == INVALID_HANDLE_VALUE) {
1918 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1919 s->hcom = NULL;
1920 goto fail;
1921 }
1922
1923 ZeroMemory(&ov, sizeof(ov));
1924 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1925 ret = ConnectNamedPipe(s->hcom, &ov);
1926 if (ret) {
1927 fprintf(stderr, "Failed ConnectNamedPipe\n");
1928 goto fail;
1929 }
1930
1931 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1932 if (!ret) {
1933 fprintf(stderr, "Failed GetOverlappedResult\n");
1934 if (ov.hEvent) {
1935 CloseHandle(ov.hEvent);
1936 ov.hEvent = NULL;
1937 }
1938 goto fail;
1939 }
1940
1941 if (ov.hEvent) {
1942 CloseHandle(ov.hEvent);
1943 ov.hEvent = NULL;
1944 }
1945 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1946 return 0;
1947
1948 fail:
1949 win_chr_close(chr);
1950 return -1;
1951}
1952
1953
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001954static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001955{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001956 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001957 CharDriverState *chr;
1958 WinCharState *s;
1959
Anthony Liguori7267c092011-08-20 22:09:37 -05001960 chr = g_malloc0(sizeof(CharDriverState));
1961 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001962 chr->opaque = s;
1963 chr->chr_write = win_chr_write;
1964 chr->chr_close = win_chr_close;
1965
1966 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001967 g_free(s);
1968 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001969 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001970 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001971 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001972}
1973
Markus Armbruster1f514702012-02-07 15:09:08 +01001974static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001975{
1976 CharDriverState *chr;
1977 WinCharState *s;
1978
Anthony Liguori7267c092011-08-20 22:09:37 -05001979 chr = g_malloc0(sizeof(CharDriverState));
1980 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001981 s->hcom = fd_out;
1982 chr->opaque = s;
1983 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001984 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001985}
1986
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001987static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001988{
Markus Armbruster1f514702012-02-07 15:09:08 +01001989 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001990}
1991
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001992static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1993{
1994 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1995 DWORD dwSize;
1996 int len1;
1997
1998 len1 = len;
1999
2000 while (len1 > 0) {
2001 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2002 break;
2003 }
2004 buf += dwSize;
2005 len1 -= dwSize;
2006 }
2007
2008 return len - len1;
2009}
2010
2011static void win_stdio_wait_func(void *opaque)
2012{
2013 CharDriverState *chr = opaque;
2014 WinStdioCharState *stdio = chr->opaque;
2015 INPUT_RECORD buf[4];
2016 int ret;
2017 DWORD dwSize;
2018 int i;
2019
Stefan Weildff74242013-12-07 14:48:04 +01002020 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002021
2022 if (!ret) {
2023 /* Avoid error storm */
2024 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2025 return;
2026 }
2027
2028 for (i = 0; i < dwSize; i++) {
2029 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2030
2031 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2032 int j;
2033 if (kev->uChar.AsciiChar != 0) {
2034 for (j = 0; j < kev->wRepeatCount; j++) {
2035 if (qemu_chr_be_can_write(chr)) {
2036 uint8_t c = kev->uChar.AsciiChar;
2037 qemu_chr_be_write(chr, &c, 1);
2038 }
2039 }
2040 }
2041 }
2042 }
2043}
2044
2045static DWORD WINAPI win_stdio_thread(LPVOID param)
2046{
2047 CharDriverState *chr = param;
2048 WinStdioCharState *stdio = chr->opaque;
2049 int ret;
2050 DWORD dwSize;
2051
2052 while (1) {
2053
2054 /* Wait for one byte */
2055 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2056
2057 /* Exit in case of error, continue if nothing read */
2058 if (!ret) {
2059 break;
2060 }
2061 if (!dwSize) {
2062 continue;
2063 }
2064
2065 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2066 if (stdio->win_stdio_buf == '\r') {
2067 continue;
2068 }
2069
2070 /* Signal the main thread and wait until the byte was eaten */
2071 if (!SetEvent(stdio->hInputReadyEvent)) {
2072 break;
2073 }
2074 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2075 != WAIT_OBJECT_0) {
2076 break;
2077 }
2078 }
2079
2080 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2081 return 0;
2082}
2083
2084static void win_stdio_thread_wait_func(void *opaque)
2085{
2086 CharDriverState *chr = opaque;
2087 WinStdioCharState *stdio = chr->opaque;
2088
2089 if (qemu_chr_be_can_write(chr)) {
2090 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2091 }
2092
2093 SetEvent(stdio->hInputDoneEvent);
2094}
2095
2096static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2097{
2098 WinStdioCharState *stdio = chr->opaque;
2099 DWORD dwMode = 0;
2100
2101 GetConsoleMode(stdio->hStdIn, &dwMode);
2102
2103 if (echo) {
2104 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2105 } else {
2106 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2107 }
2108}
2109
2110static void win_stdio_close(CharDriverState *chr)
2111{
2112 WinStdioCharState *stdio = chr->opaque;
2113
2114 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2115 CloseHandle(stdio->hInputReadyEvent);
2116 }
2117 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2118 CloseHandle(stdio->hInputDoneEvent);
2119 }
2120 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2121 TerminateThread(stdio->hInputThread, 0);
2122 }
2123
2124 g_free(chr->opaque);
2125 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002126}
2127
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002128static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002129{
2130 CharDriverState *chr;
2131 WinStdioCharState *stdio;
2132 DWORD dwMode;
2133 int is_console = 0;
2134
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002135 chr = g_malloc0(sizeof(CharDriverState));
2136 stdio = g_malloc0(sizeof(WinStdioCharState));
2137
2138 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2139 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2140 fprintf(stderr, "cannot open stdio: invalid handle\n");
2141 exit(1);
2142 }
2143
2144 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2145
2146 chr->opaque = stdio;
2147 chr->chr_write = win_stdio_write;
2148 chr->chr_close = win_stdio_close;
2149
Anthony Liguoried7a1542013-03-05 23:21:17 +05302150 if (is_console) {
2151 if (qemu_add_wait_object(stdio->hStdIn,
2152 win_stdio_wait_func, chr)) {
2153 fprintf(stderr, "qemu_add_wait_object: failed\n");
2154 }
2155 } else {
2156 DWORD dwId;
2157
2158 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2159 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2160 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2161 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002162
Anthony Liguoried7a1542013-03-05 23:21:17 +05302163 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2164 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2165 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2166 fprintf(stderr, "cannot create stdio thread or event\n");
2167 exit(1);
2168 }
2169 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2170 win_stdio_thread_wait_func, chr)) {
2171 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002172 }
2173 }
2174
2175 dwMode |= ENABLE_LINE_INPUT;
2176
Anthony Liguoried7a1542013-03-05 23:21:17 +05302177 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002178 /* set the terminal in raw mode */
2179 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2180 dwMode |= ENABLE_PROCESSED_INPUT;
2181 }
2182
2183 SetConsoleMode(stdio->hStdIn, dwMode);
2184
2185 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2186 qemu_chr_fe_set_echo(chr, false);
2187
Markus Armbruster1f514702012-02-07 15:09:08 +01002188 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002189}
aliguori6f97dba2008-10-31 18:49:55 +00002190#endif /* !_WIN32 */
2191
Anthony Liguori5ab82112013-03-05 23:21:31 +05302192
aliguori6f97dba2008-10-31 18:49:55 +00002193/***********************************************************/
2194/* UDP Net console */
2195
2196typedef struct {
2197 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302198 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302199 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002200 int bufcnt;
2201 int bufptr;
2202 int max_size;
2203} NetCharDriver;
2204
2205static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2206{
2207 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302208 gsize bytes_written;
2209 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002210
Anthony Liguori76a96442013-03-05 23:21:21 +05302211 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2212 if (status == G_IO_STATUS_EOF) {
2213 return 0;
2214 } else if (status != G_IO_STATUS_NORMAL) {
2215 return -1;
2216 }
2217
2218 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002219}
2220
2221static int udp_chr_read_poll(void *opaque)
2222{
2223 CharDriverState *chr = opaque;
2224 NetCharDriver *s = chr->opaque;
2225
Anthony Liguori909cda12011-08-15 11:17:31 -05002226 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002227
2228 /* If there were any stray characters in the queue process them
2229 * first
2230 */
2231 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002232 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002233 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002234 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002235 }
2236 return s->max_size;
2237}
2238
Anthony Liguori76a96442013-03-05 23:21:21 +05302239static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002240{
2241 CharDriverState *chr = opaque;
2242 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302243 gsize bytes_read = 0;
2244 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002245
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002246 if (s->max_size == 0) {
2247 return TRUE;
2248 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302249 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2250 &bytes_read, NULL);
2251 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002252 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302253 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302254 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302255 return FALSE;
2256 }
aliguori6f97dba2008-10-31 18:49:55 +00002257
2258 s->bufptr = 0;
2259 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002260 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002261 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002262 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002263 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302264
2265 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002266}
2267
2268static void udp_chr_update_read_handler(CharDriverState *chr)
2269{
2270 NetCharDriver *s = chr->opaque;
2271
Amit Shah26da70c2013-08-28 15:23:37 +05302272 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302273 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302274 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2275 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002276 }
2277}
2278
aliguori819f56b2009-03-28 17:58:14 +00002279static void udp_chr_close(CharDriverState *chr)
2280{
2281 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302282
2283 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302284 if (s->chan) {
2285 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002286 closesocket(s->fd);
2287 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002288 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002289 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002290}
2291
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002292static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002293{
2294 CharDriverState *chr = NULL;
2295 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002296
Anthony Liguori7267c092011-08-20 22:09:37 -05002297 chr = g_malloc0(sizeof(CharDriverState));
2298 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002299
aliguori6f97dba2008-10-31 18:49:55 +00002300 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302301 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002302 s->bufcnt = 0;
2303 s->bufptr = 0;
2304 chr->opaque = s;
2305 chr->chr_write = udp_chr_write;
2306 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002307 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002308 /* be isn't opened until we get a connection */
2309 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002310 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002311}
aliguori6f97dba2008-10-31 18:49:55 +00002312
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002313static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2314{
2315 Error *local_err = NULL;
2316 int fd = -1;
2317
2318 fd = inet_dgram_opts(opts, &local_err);
2319 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002320 qerror_report_err(local_err);
2321 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002322 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002323 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002324 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002325}
2326
2327/***********************************************************/
2328/* TCP Net console */
2329
2330typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302331
2332 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302333 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002334 int fd, listen_fd;
2335 int connected;
2336 int max_size;
2337 int do_telnetopt;
2338 int do_nodelay;
2339 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002340 int msgfd;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002341 int *write_msgfds;
2342 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002343} TCPCharDriver;
2344
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302345static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002346
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002347#ifndef _WIN32
2348static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2349{
2350 TCPCharDriver *s = chr->opaque;
2351 struct msghdr msgh;
2352 struct iovec iov;
2353 int r;
2354
2355 size_t fd_size = s->write_msgfds_num * sizeof(int);
2356 char control[CMSG_SPACE(fd_size)];
2357 struct cmsghdr *cmsg;
2358
2359 memset(&msgh, 0, sizeof(msgh));
2360 memset(control, 0, sizeof(control));
2361
2362 /* set the payload */
2363 iov.iov_base = (uint8_t *) buf;
2364 iov.iov_len = len;
2365
2366 msgh.msg_iov = &iov;
2367 msgh.msg_iovlen = 1;
2368
2369 msgh.msg_control = control;
2370 msgh.msg_controllen = sizeof(control);
2371
2372 cmsg = CMSG_FIRSTHDR(&msgh);
2373
2374 cmsg->cmsg_len = CMSG_LEN(fd_size);
2375 cmsg->cmsg_level = SOL_SOCKET;
2376 cmsg->cmsg_type = SCM_RIGHTS;
2377 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2378
2379 do {
2380 r = sendmsg(s->fd, &msgh, 0);
2381 } while (r < 0 && errno == EINTR);
2382
2383 /* free the written msgfds, no matter what */
2384 if (s->write_msgfds_num) {
2385 g_free(s->write_msgfds);
2386 s->write_msgfds = 0;
2387 s->write_msgfds_num = 0;
2388 }
2389
2390 return r;
2391}
2392#endif
2393
aliguori6f97dba2008-10-31 18:49:55 +00002394static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2395{
2396 TCPCharDriver *s = chr->opaque;
2397 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002398#ifndef _WIN32
2399 if (s->is_unix && s->write_msgfds_num) {
2400 return unix_send_msgfds(chr, buf, len);
2401 } else
2402#endif
2403 {
2404 return io_channel_send(s->chan, buf, len);
2405 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002406 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002407 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002408 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002409 }
2410}
2411
2412static int tcp_chr_read_poll(void *opaque)
2413{
2414 CharDriverState *chr = opaque;
2415 TCPCharDriver *s = chr->opaque;
2416 if (!s->connected)
2417 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002418 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002419 return s->max_size;
2420}
2421
2422#define IAC 255
2423#define IAC_BREAK 243
2424static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2425 TCPCharDriver *s,
2426 uint8_t *buf, int *size)
2427{
2428 /* Handle any telnet client's basic IAC options to satisfy char by
2429 * char mode with no echo. All IAC options will be removed from
2430 * the buf and the do_telnetopt variable will be used to track the
2431 * state of the width of the IAC information.
2432 *
2433 * IAC commands come in sets of 3 bytes with the exception of the
2434 * "IAC BREAK" command and the double IAC.
2435 */
2436
2437 int i;
2438 int j = 0;
2439
2440 for (i = 0; i < *size; i++) {
2441 if (s->do_telnetopt > 1) {
2442 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2443 /* Double IAC means send an IAC */
2444 if (j != i)
2445 buf[j] = buf[i];
2446 j++;
2447 s->do_telnetopt = 1;
2448 } else {
2449 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2450 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002451 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002452 s->do_telnetopt++;
2453 }
2454 s->do_telnetopt++;
2455 }
2456 if (s->do_telnetopt >= 4) {
2457 s->do_telnetopt = 1;
2458 }
2459 } else {
2460 if ((unsigned char)buf[i] == IAC) {
2461 s->do_telnetopt = 2;
2462 } else {
2463 if (j != i)
2464 buf[j] = buf[i];
2465 j++;
2466 }
2467 }
2468 }
2469 *size = j;
2470}
2471
Mark McLoughlin7d174052009-07-22 09:11:39 +01002472static int tcp_get_msgfd(CharDriverState *chr)
2473{
2474 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002475 int fd = s->msgfd;
2476 s->msgfd = -1;
2477 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002478}
2479
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002480static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2481{
2482 TCPCharDriver *s = chr->opaque;
2483
2484 /* clear old pending fd array */
2485 if (s->write_msgfds) {
2486 g_free(s->write_msgfds);
2487 }
2488
2489 if (num) {
2490 s->write_msgfds = g_malloc(num * sizeof(int));
2491 memcpy(s->write_msgfds, fds, num * sizeof(int));
2492 }
2493
2494 s->write_msgfds_num = num;
2495
2496 return 0;
2497}
2498
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002499#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002500static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2501{
2502 TCPCharDriver *s = chr->opaque;
2503 struct cmsghdr *cmsg;
2504
2505 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2506 int fd;
2507
2508 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2509 cmsg->cmsg_level != SOL_SOCKET ||
2510 cmsg->cmsg_type != SCM_RIGHTS)
2511 continue;
2512
2513 fd = *((int *)CMSG_DATA(cmsg));
2514 if (fd < 0)
2515 continue;
2516
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002517 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2518 qemu_set_block(fd);
2519
Corey Bryant06138652012-08-14 16:43:42 -04002520#ifndef MSG_CMSG_CLOEXEC
2521 qemu_set_cloexec(fd);
2522#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002523 if (s->msgfd != -1)
2524 close(s->msgfd);
2525 s->msgfd = fd;
2526 }
2527}
2528
Mark McLoughlin9977c892009-07-22 09:11:38 +01002529static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2530{
2531 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002532 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002533 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002534 union {
2535 struct cmsghdr cmsg;
2536 char control[CMSG_SPACE(sizeof(int))];
2537 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002538 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002539 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002540
2541 iov[0].iov_base = buf;
2542 iov[0].iov_len = len;
2543
2544 msg.msg_iov = iov;
2545 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002546 msg.msg_control = &msg_control;
2547 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002548
Corey Bryant06138652012-08-14 16:43:42 -04002549#ifdef MSG_CMSG_CLOEXEC
2550 flags |= MSG_CMSG_CLOEXEC;
2551#endif
2552 ret = recvmsg(s->fd, &msg, flags);
2553 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002554 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002555 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002556
2557 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002558}
2559#else
2560static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2561{
2562 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002563 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002564}
2565#endif
2566
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302567static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2568{
2569 TCPCharDriver *s = chr->opaque;
2570 return g_io_create_watch(s->chan, cond);
2571}
2572
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002573static void tcp_chr_disconnect(CharDriverState *chr)
2574{
2575 TCPCharDriver *s = chr->opaque;
2576
2577 s->connected = 0;
2578 if (s->listen_chan) {
2579 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2580 tcp_chr_accept, chr);
2581 }
2582 remove_fd_in_watch(chr);
2583 g_io_channel_unref(s->chan);
2584 s->chan = NULL;
2585 closesocket(s->fd);
2586 s->fd = -1;
2587 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2588}
2589
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302590static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002591{
2592 CharDriverState *chr = opaque;
2593 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302594 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002595 int len, size;
2596
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302597 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002598 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302599 }
aliguori6f97dba2008-10-31 18:49:55 +00002600 len = sizeof(buf);
2601 if (len > s->max_size)
2602 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002603 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002604 if (size == 0) {
2605 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002606 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002607 } else if (size > 0) {
2608 if (s->do_telnetopt)
2609 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2610 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002611 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002612 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302613
2614 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002615}
2616
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002617static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2618{
2619 TCPCharDriver *s = chr->opaque;
2620 int size;
2621
2622 if (!s->connected) {
2623 return 0;
2624 }
2625
2626 size = tcp_chr_recv(chr, (void *) buf, len);
2627 if (size == 0) {
2628 /* connection closed */
2629 tcp_chr_disconnect(chr);
2630 }
2631
2632 return size;
2633}
2634
Blue Swirl68c18d12010-08-15 09:46:24 +00002635#ifndef _WIN32
2636CharDriverState *qemu_chr_open_eventfd(int eventfd)
2637{
David Marchande9d21c42014-06-11 17:25:16 +02002638 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2639
2640 if (chr) {
2641 chr->avail_connections = 1;
2642 }
2643
2644 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002645}
Blue Swirl68c18d12010-08-15 09:46:24 +00002646#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002647
aliguori6f97dba2008-10-31 18:49:55 +00002648static void tcp_chr_connect(void *opaque)
2649{
2650 CharDriverState *chr = opaque;
2651 TCPCharDriver *s = chr->opaque;
2652
2653 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302654 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302655 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2656 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002657 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002658 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002659}
2660
Gal Hammerac1b84d2014-02-25 12:12:35 +02002661static void tcp_chr_update_read_handler(CharDriverState *chr)
2662{
2663 TCPCharDriver *s = chr->opaque;
2664
2665 remove_fd_in_watch(chr);
2666 if (s->chan) {
2667 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2668 tcp_chr_read, chr);
2669 }
2670}
2671
aliguori6f97dba2008-10-31 18:49:55 +00002672#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2673static void tcp_chr_telnet_init(int fd)
2674{
2675 char buf[3];
2676 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2677 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2678 send(fd, (char *)buf, 3, 0);
2679 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2680 send(fd, (char *)buf, 3, 0);
2681 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2682 send(fd, (char *)buf, 3, 0);
2683 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2684 send(fd, (char *)buf, 3, 0);
2685}
2686
Daniel P. Berrange13661082011-06-23 13:31:42 +01002687static int tcp_chr_add_client(CharDriverState *chr, int fd)
2688{
2689 TCPCharDriver *s = chr->opaque;
2690 if (s->fd != -1)
2691 return -1;
2692
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002693 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002694 if (s->do_nodelay)
2695 socket_set_nodelay(fd);
2696 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302697 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002698 if (s->listen_tag) {
2699 g_source_remove(s->listen_tag);
2700 s->listen_tag = 0;
2701 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002702 tcp_chr_connect(chr);
2703
2704 return 0;
2705}
2706
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302707static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002708{
2709 CharDriverState *chr = opaque;
2710 TCPCharDriver *s = chr->opaque;
2711 struct sockaddr_in saddr;
2712#ifndef _WIN32
2713 struct sockaddr_un uaddr;
2714#endif
2715 struct sockaddr *addr;
2716 socklen_t len;
2717 int fd;
2718
2719 for(;;) {
2720#ifndef _WIN32
2721 if (s->is_unix) {
2722 len = sizeof(uaddr);
2723 addr = (struct sockaddr *)&uaddr;
2724 } else
2725#endif
2726 {
2727 len = sizeof(saddr);
2728 addr = (struct sockaddr *)&saddr;
2729 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002730 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002731 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002732 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302733 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002734 } else if (fd >= 0) {
2735 if (s->do_telnetopt)
2736 tcp_chr_telnet_init(fd);
2737 break;
2738 }
2739 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002740 if (tcp_chr_add_client(chr, fd) < 0)
2741 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302742
2743 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002744}
2745
2746static void tcp_chr_close(CharDriverState *chr)
2747{
2748 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002749 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302750 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302751 if (s->chan) {
2752 g_io_channel_unref(s->chan);
2753 }
aliguori6f97dba2008-10-31 18:49:55 +00002754 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002755 }
2756 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302757 if (s->listen_tag) {
2758 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002759 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302760 }
2761 if (s->listen_chan) {
2762 g_io_channel_unref(s->listen_chan);
2763 }
aliguori6f97dba2008-10-31 18:49:55 +00002764 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002765 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002766 if (s->write_msgfds_num) {
2767 g_free(s->write_msgfds);
2768 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002769 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002770 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002771}
2772
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002773static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2774 bool is_listen, bool is_telnet,
2775 bool is_waitconnect,
2776 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002777{
2778 CharDriverState *chr = NULL;
2779 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002780 char host[NI_MAXHOST], serv[NI_MAXSERV];
2781 const char *left = "", *right = "";
2782 struct sockaddr_storage ss;
2783 socklen_t ss_len = sizeof(ss);
2784
2785 memset(&ss, 0, ss_len);
2786 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002787 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002788 return NULL;
2789 }
2790
2791 chr = g_malloc0(sizeof(CharDriverState));
2792 s = g_malloc0(sizeof(TCPCharDriver));
2793
2794 s->connected = 0;
2795 s->fd = -1;
2796 s->listen_fd = -1;
2797 s->msgfd = -1;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002798 s->write_msgfds = 0;
2799 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002800
2801 chr->filename = g_malloc(256);
2802 switch (ss.ss_family) {
2803#ifndef _WIN32
2804 case AF_UNIX:
2805 s->is_unix = 1;
2806 snprintf(chr->filename, 256, "unix:%s%s",
2807 ((struct sockaddr_un *)(&ss))->sun_path,
2808 is_listen ? ",server" : "");
2809 break;
2810#endif
2811 case AF_INET6:
2812 left = "[";
2813 right = "]";
2814 /* fall through */
2815 case AF_INET:
2816 s->do_nodelay = do_nodelay;
2817 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2818 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002819 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002820 is_telnet ? "telnet" : "tcp",
2821 left, host, right, serv,
2822 is_listen ? ",server" : "");
2823 break;
2824 }
2825
2826 chr->opaque = s;
2827 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002828 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002829 chr->chr_close = tcp_chr_close;
2830 chr->get_msgfd = tcp_get_msgfd;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002831 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002832 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302833 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002834 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002835 /* be isn't opened until we get a connection */
2836 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002837
2838 if (is_listen) {
2839 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302840 s->listen_chan = io_channel_from_socket(s->listen_fd);
2841 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002842 if (is_telnet) {
2843 s->do_telnetopt = 1;
2844 }
2845 } else {
2846 s->connected = 1;
2847 s->fd = fd;
2848 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302849 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002850 tcp_chr_connect(chr);
2851 }
2852
2853 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002854 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2855 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302856 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002857 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002858 }
2859 return chr;
2860}
2861
2862static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2863{
2864 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002865 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002866 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002867
liguange990a392013-06-18 11:45:35 +08002868 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2869 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2870 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2871 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2872 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002873
aliguorif07b6002008-11-11 20:54:09 +00002874 if (is_unix) {
2875 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002876 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002877 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002878 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002879 }
2880 } else {
2881 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002882 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002883 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002884 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002885 }
2886 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002887 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002888 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002889 }
aliguori6f97dba2008-10-31 18:49:55 +00002890
2891 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002892 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002893
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002894 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2895 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002896 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002897 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002898 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002899 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002900
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002901
aliguori6f97dba2008-10-31 18:49:55 +00002902 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002903 if (local_err) {
2904 qerror_report_err(local_err);
2905 error_free(local_err);
2906 }
2907 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002908 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002909 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002910 if (chr) {
2911 g_free(chr->opaque);
2912 g_free(chr);
2913 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002914 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002915}
2916
Lei Li51767e72013-01-25 00:03:19 +08002917/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002918/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002919
2920typedef struct {
2921 size_t size;
2922 size_t prod;
2923 size_t cons;
2924 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002925} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002926
Markus Armbruster3949e592013-02-06 21:27:24 +01002927static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002928{
Markus Armbruster3949e592013-02-06 21:27:24 +01002929 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002930
Markus Armbruster5c230102013-02-06 21:27:23 +01002931 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002932}
2933
Markus Armbruster3949e592013-02-06 21:27:24 +01002934static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002935{
Markus Armbruster3949e592013-02-06 21:27:24 +01002936 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002937 int i;
2938
2939 if (!buf || (len < 0)) {
2940 return -1;
2941 }
2942
2943 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002944 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2945 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002946 d->cons = d->prod - d->size;
2947 }
2948 }
2949
2950 return 0;
2951}
2952
Markus Armbruster3949e592013-02-06 21:27:24 +01002953static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002954{
Markus Armbruster3949e592013-02-06 21:27:24 +01002955 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002956 int i;
2957
Markus Armbruster5c230102013-02-06 21:27:23 +01002958 for (i = 0; i < len && d->cons != d->prod; i++) {
2959 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002960 }
2961
2962 return i;
2963}
2964
Markus Armbruster3949e592013-02-06 21:27:24 +01002965static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002966{
Markus Armbruster3949e592013-02-06 21:27:24 +01002967 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002968
2969 g_free(d->cbuf);
2970 g_free(d);
2971 chr->opaque = NULL;
2972}
2973
Markus Armbruster4f573782013-07-26 16:44:32 +02002974static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2975 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002976{
2977 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002978 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002979
2980 chr = g_malloc0(sizeof(CharDriverState));
2981 d = g_malloc(sizeof(*d));
2982
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002983 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002984
2985 /* The size must be power of 2 */
2986 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02002987 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002988 goto fail;
2989 }
2990
2991 d->prod = 0;
2992 d->cons = 0;
2993 d->cbuf = g_malloc0(d->size);
2994
2995 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002996 chr->chr_write = ringbuf_chr_write;
2997 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002998
2999 return chr;
3000
3001fail:
3002 g_free(d);
3003 g_free(chr);
3004 return NULL;
3005}
3006
Hani Benhabiles8e597772014-05-27 23:39:30 +01003007bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003008{
Markus Armbruster3949e592013-02-06 21:27:24 +01003009 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003010}
3011
Markus Armbruster3949e592013-02-06 21:27:24 +01003012void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003013 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003014 Error **errp)
3015{
3016 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003017 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003018 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003019 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003020
3021 chr = qemu_chr_find(device);
3022 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003023 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003024 return;
3025 }
3026
Markus Armbruster3949e592013-02-06 21:27:24 +01003027 if (!chr_is_ringbuf(chr)) {
3028 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003029 return;
3030 }
3031
Lei Li1f590cf2013-01-25 00:03:20 +08003032 if (has_format && (format == DATA_FORMAT_BASE64)) {
3033 write_data = g_base64_decode(data, &write_count);
3034 } else {
3035 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003036 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003037 }
3038
Markus Armbruster3949e592013-02-06 21:27:24 +01003039 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003040
Markus Armbruster13289fb2013-02-06 21:27:18 +01003041 if (write_data != (uint8_t *)data) {
3042 g_free((void *)write_data);
3043 }
3044
Lei Li1f590cf2013-01-25 00:03:20 +08003045 if (ret < 0) {
3046 error_setg(errp, "Failed to write to device %s", device);
3047 return;
3048 }
3049}
3050
Markus Armbruster3949e592013-02-06 21:27:24 +01003051char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003052 bool has_format, enum DataFormat format,
3053 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003054{
3055 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003056 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003057 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003058 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003059
3060 chr = qemu_chr_find(device);
3061 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003062 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003063 return NULL;
3064 }
3065
Markus Armbruster3949e592013-02-06 21:27:24 +01003066 if (!chr_is_ringbuf(chr)) {
3067 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003068 return NULL;
3069 }
3070
3071 if (size <= 0) {
3072 error_setg(errp, "size must be greater than zero");
3073 return NULL;
3074 }
3075
Markus Armbruster3949e592013-02-06 21:27:24 +01003076 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003077 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003078 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003079
Markus Armbruster3949e592013-02-06 21:27:24 +01003080 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003081
3082 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003083 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003084 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003085 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003086 /*
3087 * FIXME should read only complete, valid UTF-8 characters up
3088 * to @size bytes. Invalid sequences should be replaced by a
3089 * suitable replacement character. Except when (and only
3090 * when) ring buffer lost characters since last read, initial
3091 * continuation characters should be dropped.
3092 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003093 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003094 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003095 }
3096
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003097 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003098}
3099
Gerd Hoffmann33521632009-12-08 13:11:49 +01003100QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003101{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003102 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003103 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003104 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003105 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003106 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003107
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003108 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003109 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003110 qerror_report_err(local_err);
3111 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003112 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003113 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003114
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003115 if (strstart(filename, "mon:", &p)) {
3116 filename = p;
3117 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003118 if (strcmp(filename, "stdio") == 0) {
3119 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3120 * but pass it to the guest. Handle this only for compat syntax,
3121 * for -chardev syntax we have special option for this.
3122 * This is what -nographic did, redirecting+muxing serial+monitor
3123 * to stdio causing Ctrl+C to be passed to guest. */
3124 qemu_opt_set(opts, "signal", "off");
3125 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003126 }
3127
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003128 if (strcmp(filename, "null") == 0 ||
3129 strcmp(filename, "pty") == 0 ||
3130 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003131 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003132 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003133 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003134 return opts;
3135 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003136 if (strstart(filename, "vc", &p)) {
3137 qemu_opt_set(opts, "backend", "vc");
3138 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003139 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003140 /* pixels */
3141 qemu_opt_set(opts, "width", width);
3142 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003143 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003144 /* chars */
3145 qemu_opt_set(opts, "cols", width);
3146 qemu_opt_set(opts, "rows", height);
3147 } else {
3148 goto fail;
3149 }
3150 }
3151 return opts;
3152 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003153 if (strcmp(filename, "con:") == 0) {
3154 qemu_opt_set(opts, "backend", "console");
3155 return opts;
3156 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003157 if (strstart(filename, "COM", NULL)) {
3158 qemu_opt_set(opts, "backend", "serial");
3159 qemu_opt_set(opts, "path", filename);
3160 return opts;
3161 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003162 if (strstart(filename, "file:", &p)) {
3163 qemu_opt_set(opts, "backend", "file");
3164 qemu_opt_set(opts, "path", p);
3165 return opts;
3166 }
3167 if (strstart(filename, "pipe:", &p)) {
3168 qemu_opt_set(opts, "backend", "pipe");
3169 qemu_opt_set(opts, "path", p);
3170 return opts;
3171 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003172 if (strstart(filename, "tcp:", &p) ||
3173 strstart(filename, "telnet:", &p)) {
3174 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3175 host[0] = 0;
3176 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3177 goto fail;
3178 }
3179 qemu_opt_set(opts, "backend", "socket");
3180 qemu_opt_set(opts, "host", host);
3181 qemu_opt_set(opts, "port", port);
3182 if (p[pos] == ',') {
3183 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3184 goto fail;
3185 }
3186 if (strstart(filename, "telnet:", &p))
3187 qemu_opt_set(opts, "telnet", "on");
3188 return opts;
3189 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003190 if (strstart(filename, "udp:", &p)) {
3191 qemu_opt_set(opts, "backend", "udp");
3192 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3193 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003194 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003195 goto fail;
3196 }
3197 }
3198 qemu_opt_set(opts, "host", host);
3199 qemu_opt_set(opts, "port", port);
3200 if (p[pos] == '@') {
3201 p += pos + 1;
3202 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3203 host[0] = 0;
3204 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003205 goto fail;
3206 }
3207 }
3208 qemu_opt_set(opts, "localaddr", host);
3209 qemu_opt_set(opts, "localport", port);
3210 }
3211 return opts;
3212 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003213 if (strstart(filename, "unix:", &p)) {
3214 qemu_opt_set(opts, "backend", "socket");
3215 if (qemu_opts_do_parse(opts, p, "path") != 0)
3216 goto fail;
3217 return opts;
3218 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003219 if (strstart(filename, "/dev/parport", NULL) ||
3220 strstart(filename, "/dev/ppi", NULL)) {
3221 qemu_opt_set(opts, "backend", "parport");
3222 qemu_opt_set(opts, "path", filename);
3223 return opts;
3224 }
3225 if (strstart(filename, "/dev/", NULL)) {
3226 qemu_opt_set(opts, "backend", "tty");
3227 qemu_opt_set(opts, "path", filename);
3228 return opts;
3229 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003231fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003232 qemu_opts_del(opts);
3233 return NULL;
3234}
3235
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003236static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3237 Error **errp)
3238{
3239 const char *path = qemu_opt_get(opts, "path");
3240
3241 if (path == NULL) {
3242 error_setg(errp, "chardev: file: no filename given");
3243 return;
3244 }
3245 backend->file = g_new0(ChardevFile, 1);
3246 backend->file->out = g_strdup(path);
3247}
3248
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003249static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3250 Error **errp)
3251{
3252 backend->stdio = g_new0(ChardevStdio, 1);
3253 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003254 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003255}
3256
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003257static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3258 Error **errp)
3259{
3260 const char *device = qemu_opt_get(opts, "path");
3261
3262 if (device == NULL) {
3263 error_setg(errp, "chardev: serial/tty: no device path given");
3264 return;
3265 }
3266 backend->serial = g_new0(ChardevHostdev, 1);
3267 backend->serial->device = g_strdup(device);
3268}
3269
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003270static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3271 Error **errp)
3272{
3273 const char *device = qemu_opt_get(opts, "path");
3274
3275 if (device == NULL) {
3276 error_setg(errp, "chardev: parallel: no device path given");
3277 return;
3278 }
3279 backend->parallel = g_new0(ChardevHostdev, 1);
3280 backend->parallel->device = g_strdup(device);
3281}
3282
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003283static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3284 Error **errp)
3285{
3286 const char *device = qemu_opt_get(opts, "path");
3287
3288 if (device == NULL) {
3289 error_setg(errp, "chardev: pipe: no device path given");
3290 return;
3291 }
3292 backend->pipe = g_new0(ChardevHostdev, 1);
3293 backend->pipe->device = g_strdup(device);
3294}
3295
Markus Armbruster4f573782013-07-26 16:44:32 +02003296static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3297 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003298{
3299 int val;
3300
Markus Armbruster3a1da422013-07-26 16:44:34 +02003301 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003302
Markus Armbruster0f953052013-06-27 16:22:07 +02003303 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003304 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003305 backend->ringbuf->has_size = true;
3306 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003307 }
3308}
3309
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003310static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3311 Error **errp)
3312{
3313 const char *chardev = qemu_opt_get(opts, "chardev");
3314
3315 if (chardev == NULL) {
3316 error_setg(errp, "chardev: mux: no chardev given");
3317 return;
3318 }
3319 backend->mux = g_new0(ChardevMux, 1);
3320 backend->mux->chardev = g_strdup(chardev);
3321}
3322
Anthony Liguorid654f342013-03-05 23:21:28 +05303323typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003324 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003325 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003326 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003327 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003328 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003329 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303330} CharDriver;
3331
3332static GSList *backends;
3333
3334void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3335{
3336 CharDriver *s;
3337
3338 s = g_malloc0(sizeof(*s));
3339 s->name = g_strdup(name);
3340 s->open = open;
3341
3342 backends = g_slist_append(backends, s);
3343}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003344
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003345void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003346 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3347{
3348 CharDriver *s;
3349
3350 s = g_malloc0(sizeof(*s));
3351 s->name = g_strdup(name);
3352 s->kind = kind;
3353 s->parse = parse;
3354
3355 backends = g_slist_append(backends, s);
3356}
3357
Anthony Liguorif69554b2011-08-15 11:17:37 -05003358CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003359 void (*init)(struct CharDriverState *s),
3360 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003361{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003362 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303363 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003364 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303365 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003366
3367 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003368 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003369 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003370 }
3371
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003372 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003373 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003374 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003375 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003376 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303377 for (i = backends; i; i = i->next) {
3378 cd = i->data;
3379
3380 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003381 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303382 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003383 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303384 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003385 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003386 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003387 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003388 }
3389
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003390 if (!cd->open) {
3391 /* using new, qapi init */
3392 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3393 ChardevReturn *ret = NULL;
3394 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003395 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003396
3397 if (qemu_opt_get_bool(opts, "mux", 0)) {
3398 bid = g_strdup_printf("%s-base", id);
3399 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003400
3401 chr = NULL;
3402 backend->kind = cd->kind;
3403 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003404 cd->parse(opts, backend, &local_err);
3405 if (local_err) {
3406 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003407 goto qapi_out;
3408 }
3409 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003410 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003411 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003412 goto qapi_out;
3413 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003414
3415 if (bid) {
3416 qapi_free_ChardevBackend(backend);
3417 qapi_free_ChardevReturn(ret);
3418 backend = g_new0(ChardevBackend, 1);
3419 backend->mux = g_new0(ChardevMux, 1);
3420 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3421 backend->mux->chardev = g_strdup(bid);
3422 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003423 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003424 chr = qemu_chr_find(bid);
3425 qemu_chr_delete(chr);
3426 chr = NULL;
3427 goto qapi_out;
3428 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003429 }
3430
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003431 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003432 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003433
3434 qapi_out:
3435 qapi_free_ChardevBackend(backend);
3436 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003437 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003438 return chr;
3439 }
3440
Anthony Liguorid654f342013-03-05 23:21:28 +05303441 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003442 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003443 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003444 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003445 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003446 }
3447
3448 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003449 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003450 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003451 /* if we didn't create the chardev via qmp_chardev_add, we
3452 * need to send the OPENED event here
3453 */
3454 if (!chr->explicit_be_open) {
3455 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3456 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003457 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003458
3459 if (qemu_opt_get_bool(opts, "mux", 0)) {
3460 CharDriverState *base = chr;
3461 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003462 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003463 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3464 chr = qemu_chr_open_mux(base);
3465 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003466 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003467 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003468 } else {
3469 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003470 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003471 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003472 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003473 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003474
3475err:
3476 qemu_opts_del(opts);
3477 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003478}
3479
Anthony Liguori27143a42011-08-15 11:17:36 -05003480CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003481{
3482 const char *p;
3483 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003484 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003485 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003486
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003487 if (strstart(filename, "chardev:", &p)) {
3488 return qemu_chr_find(p);
3489 }
3490
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003491 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003492 if (!opts)
3493 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003494
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003495 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003496 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003497 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003498 error_free(err);
3499 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003500 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003501 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003502 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003503 }
3504 return chr;
3505}
3506
Anthony Liguori15f31512011-08-15 11:17:35 -05003507void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003508{
3509 if (chr->chr_set_echo) {
3510 chr->chr_set_echo(chr, echo);
3511 }
3512}
3513
Hans de Goede8e25daa2013-03-26 11:07:57 +01003514void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003515{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003516 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003517 return;
3518 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003519 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003520 if (chr->chr_set_fe_open) {
3521 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003522 }
3523}
3524
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003525void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3526{
3527 if (chr->chr_fe_event) {
3528 chr->chr_fe_event(chr, event);
3529 }
3530}
3531
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003532int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3533 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303534{
3535 GSource *src;
3536 guint tag;
3537
3538 if (s->chr_add_watch == NULL) {
3539 return -ENOSYS;
3540 }
3541
3542 src = s->chr_add_watch(s, cond);
3543 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3544 tag = g_source_attach(src, NULL);
3545 g_source_unref(src);
3546
3547 return tag;
3548}
3549
Hans de Goede44c473d2013-03-27 20:29:39 +01003550int qemu_chr_fe_claim(CharDriverState *s)
3551{
3552 if (s->avail_connections < 1) {
3553 return -1;
3554 }
3555 s->avail_connections--;
3556 return 0;
3557}
3558
3559void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3560{
3561 if (qemu_chr_fe_claim(s) != 0) {
3562 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3563 __func__, s->label);
3564 exit(1);
3565 }
3566}
3567
3568void qemu_chr_fe_release(CharDriverState *s)
3569{
3570 s->avail_connections++;
3571}
3572
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003573void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003574{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003575 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003576 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003577 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003578 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003579 g_free(chr->filename);
3580 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003581 if (chr->opts) {
3582 qemu_opts_del(chr->opts);
3583 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003584 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003585}
3586
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003587ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003588{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003589 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003590 CharDriverState *chr;
3591
Blue Swirl72cf2d42009-09-12 07:36:22 +00003592 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003593 ChardevInfoList *info = g_malloc0(sizeof(*info));
3594 info->value = g_malloc0(sizeof(*info->value));
3595 info->value->label = g_strdup(chr->label);
3596 info->value->filename = g_strdup(chr->filename);
3597
3598 info->next = chr_list;
3599 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003600 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003601
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003602 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003603}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003604
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003605ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3606{
3607 ChardevBackendInfoList *backend_list = NULL;
3608 CharDriver *c = NULL;
3609 GSList *i = NULL;
3610
3611 for (i = backends; i; i = i->next) {
3612 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3613 c = i->data;
3614 info->value = g_malloc0(sizeof(*info->value));
3615 info->value->name = g_strdup(c->name);
3616
3617 info->next = backend_list;
3618 backend_list = info;
3619 }
3620
3621 return backend_list;
3622}
3623
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003624CharDriverState *qemu_chr_find(const char *name)
3625{
3626 CharDriverState *chr;
3627
Blue Swirl72cf2d42009-09-12 07:36:22 +00003628 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003629 if (strcmp(chr->label, name) != 0)
3630 continue;
3631 return chr;
3632 }
3633 return NULL;
3634}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003635
3636/* Get a character (serial) device interface. */
3637CharDriverState *qemu_char_get_next_serial(void)
3638{
3639 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003640 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003641
3642 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003643
3644 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3645 chr = serial_hds[next_serial++];
3646 qemu_chr_fe_claim_no_fail(chr);
3647 return chr;
3648 }
3649 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003650}
3651
Paolo Bonzini4d454572012-11-26 16:03:42 +01003652QemuOptsList qemu_chardev_opts = {
3653 .name = "chardev",
3654 .implied_opt_name = "backend",
3655 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3656 .desc = {
3657 {
3658 .name = "backend",
3659 .type = QEMU_OPT_STRING,
3660 },{
3661 .name = "path",
3662 .type = QEMU_OPT_STRING,
3663 },{
3664 .name = "host",
3665 .type = QEMU_OPT_STRING,
3666 },{
3667 .name = "port",
3668 .type = QEMU_OPT_STRING,
3669 },{
3670 .name = "localaddr",
3671 .type = QEMU_OPT_STRING,
3672 },{
3673 .name = "localport",
3674 .type = QEMU_OPT_STRING,
3675 },{
3676 .name = "to",
3677 .type = QEMU_OPT_NUMBER,
3678 },{
3679 .name = "ipv4",
3680 .type = QEMU_OPT_BOOL,
3681 },{
3682 .name = "ipv6",
3683 .type = QEMU_OPT_BOOL,
3684 },{
3685 .name = "wait",
3686 .type = QEMU_OPT_BOOL,
3687 },{
3688 .name = "server",
3689 .type = QEMU_OPT_BOOL,
3690 },{
3691 .name = "delay",
3692 .type = QEMU_OPT_BOOL,
3693 },{
3694 .name = "telnet",
3695 .type = QEMU_OPT_BOOL,
3696 },{
3697 .name = "width",
3698 .type = QEMU_OPT_NUMBER,
3699 },{
3700 .name = "height",
3701 .type = QEMU_OPT_NUMBER,
3702 },{
3703 .name = "cols",
3704 .type = QEMU_OPT_NUMBER,
3705 },{
3706 .name = "rows",
3707 .type = QEMU_OPT_NUMBER,
3708 },{
3709 .name = "mux",
3710 .type = QEMU_OPT_BOOL,
3711 },{
3712 .name = "signal",
3713 .type = QEMU_OPT_BOOL,
3714 },{
3715 .name = "name",
3716 .type = QEMU_OPT_STRING,
3717 },{
3718 .name = "debug",
3719 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003720 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003721 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003722 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003723 },{
3724 .name = "chardev",
3725 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003726 },
3727 { /* end of list */ }
3728 },
3729};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003730
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003731#ifdef _WIN32
3732
3733static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3734{
3735 HANDLE out;
3736
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003737 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003738 error_setg(errp, "input file not supported");
3739 return NULL;
3740 }
3741
3742 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3743 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3744 if (out == INVALID_HANDLE_VALUE) {
3745 error_setg(errp, "open %s failed", file->out);
3746 return NULL;
3747 }
3748 return qemu_chr_open_win_file(out);
3749}
3750
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003751static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3752 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003753{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003754 return qemu_chr_open_win_path(serial->device);
3755}
3756
3757static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3758 Error **errp)
3759{
3760 error_setg(errp, "character device backend type 'parallel' not supported");
3761 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003762}
3763
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003764#else /* WIN32 */
3765
3766static int qmp_chardev_open_file_source(char *src, int flags,
3767 Error **errp)
3768{
3769 int fd = -1;
3770
3771 TFR(fd = qemu_open(src, flags, 0666));
3772 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003773 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003774 }
3775 return fd;
3776}
3777
3778static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3779{
Markus Armbruster5f758362014-05-19 18:57:34 +02003780 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003781
3782 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3783 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003784 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003785 return NULL;
3786 }
3787
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003788 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003789 flags = O_RDONLY;
3790 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003791 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003792 qemu_close(out);
3793 return NULL;
3794 }
3795 }
3796
3797 return qemu_chr_open_fd(in, out);
3798}
3799
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003800static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3801 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003802{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003803#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003804 int fd;
3805
3806 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003807 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003808 return NULL;
3809 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003810 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003811 return qemu_chr_open_tty_fd(fd);
3812#else
3813 error_setg(errp, "character device backend type 'serial' not supported");
3814 return NULL;
3815#endif
3816}
3817
3818static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3819 Error **errp)
3820{
3821#ifdef HAVE_CHARDEV_PARPORT
3822 int fd;
3823
3824 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003825 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003826 return NULL;
3827 }
3828 return qemu_chr_open_pp_fd(fd);
3829#else
3830 error_setg(errp, "character device backend type 'parallel' not supported");
3831 return NULL;
3832#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003833}
3834
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003835#endif /* WIN32 */
3836
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003837static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3838 Error **errp)
3839{
3840 SocketAddress *addr = sock->addr;
3841 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3842 bool is_listen = sock->has_server ? sock->server : true;
3843 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3844 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3845 int fd;
3846
3847 if (is_listen) {
3848 fd = socket_listen(addr, errp);
3849 } else {
3850 fd = socket_connect(addr, errp, NULL, NULL);
3851 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003852 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003853 return NULL;
3854 }
3855 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3856 is_telnet, is_waitconnect, errp);
3857}
3858
Lei Li08d0ab32013-05-20 14:51:03 +08003859static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3860 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003861{
3862 int fd;
3863
Lei Li08d0ab32013-05-20 14:51:03 +08003864 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003865 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003866 return NULL;
3867 }
3868 return qemu_chr_open_udp_fd(fd);
3869}
3870
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003871ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3872 Error **errp)
3873{
3874 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003875 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003876
3877 chr = qemu_chr_find(id);
3878 if (chr) {
3879 error_setg(errp, "Chardev '%s' already exists", id);
3880 g_free(ret);
3881 return NULL;
3882 }
3883
3884 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003885 case CHARDEV_BACKEND_KIND_FILE:
3886 chr = qmp_chardev_open_file(backend->file, errp);
3887 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003888 case CHARDEV_BACKEND_KIND_SERIAL:
3889 chr = qmp_chardev_open_serial(backend->serial, errp);
3890 break;
3891 case CHARDEV_BACKEND_KIND_PARALLEL:
3892 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003893 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003894 case CHARDEV_BACKEND_KIND_PIPE:
3895 chr = qemu_chr_open_pipe(backend->pipe);
3896 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003897 case CHARDEV_BACKEND_KIND_SOCKET:
3898 chr = qmp_chardev_open_socket(backend->socket, errp);
3899 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003900 case CHARDEV_BACKEND_KIND_UDP:
3901 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003902 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003903#ifdef HAVE_CHARDEV_TTY
3904 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003905 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003906 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003907#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003908 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003909 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003910 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003911 case CHARDEV_BACKEND_KIND_MUX:
3912 base = qemu_chr_find(backend->mux->chardev);
3913 if (base == NULL) {
3914 error_setg(errp, "mux: base chardev %s not found",
3915 backend->mux->chardev);
3916 break;
3917 }
3918 chr = qemu_chr_open_mux(base);
3919 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003920 case CHARDEV_BACKEND_KIND_MSMOUSE:
3921 chr = qemu_chr_open_msmouse();
3922 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003923#ifdef CONFIG_BRLAPI
3924 case CHARDEV_BACKEND_KIND_BRAILLE:
3925 chr = chr_baum_init();
3926 break;
3927#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003928 case CHARDEV_BACKEND_KIND_STDIO:
3929 chr = qemu_chr_open_stdio(backend->stdio);
3930 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003931#ifdef _WIN32
3932 case CHARDEV_BACKEND_KIND_CONSOLE:
3933 chr = qemu_chr_open_win_con();
3934 break;
3935#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003936#ifdef CONFIG_SPICE
3937 case CHARDEV_BACKEND_KIND_SPICEVMC:
3938 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3939 break;
3940 case CHARDEV_BACKEND_KIND_SPICEPORT:
3941 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3942 break;
3943#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003944 case CHARDEV_BACKEND_KIND_VC:
3945 chr = vc_init(backend->vc);
3946 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003947 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003948 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003949 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003950 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003951 default:
3952 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3953 break;
3954 }
3955
Markus Armbruster3894c782014-05-19 18:57:36 +02003956 /*
3957 * Character backend open hasn't been fully converted to the Error
3958 * API. Some opens fail without setting an error. Set a generic
3959 * error then.
3960 * TODO full conversion to Error API
3961 */
3962 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003963 error_setg(errp, "Failed to create chardev");
3964 }
3965 if (chr) {
3966 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003967 chr->avail_connections =
3968 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02003969 if (!chr->filename) {
3970 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
3971 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05003972 if (!chr->explicit_be_open) {
3973 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3974 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003975 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3976 return ret;
3977 } else {
3978 g_free(ret);
3979 return NULL;
3980 }
3981}
3982
3983void qmp_chardev_remove(const char *id, Error **errp)
3984{
3985 CharDriverState *chr;
3986
3987 chr = qemu_chr_find(id);
3988 if (NULL == chr) {
3989 error_setg(errp, "Chardev '%s' not found", id);
3990 return;
3991 }
3992 if (chr->chr_can_read || chr->chr_read ||
3993 chr->chr_event || chr->handler_opaque) {
3994 error_setg(errp, "Chardev '%s' is busy", id);
3995 return;
3996 }
3997 qemu_chr_delete(chr);
3998}
Anthony Liguorid654f342013-03-05 23:21:28 +05303999
4000static void register_types(void)
4001{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004002 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304003 register_char_driver("socket", qemu_chr_open_socket);
4004 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004005 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004006 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004007 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4008 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004009 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4010 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004011 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4012 qemu_chr_parse_serial);
4013 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4014 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004015 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4016 qemu_chr_parse_parallel);
4017 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4018 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004019 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004020 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004021 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4022 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004023 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4024 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004025 /* Bug-compatibility: */
4026 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4027 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004028 /* this must be done after machine init, since we register FEs with muxes
4029 * as part of realize functions like serial_isa_realizefn when -nographic
4030 * is specified
4031 */
4032 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304033}
4034
4035type_init(register_types);