blob: e6cbafb09cafbf91b04bab369e9ed015d136bda4 [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 Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000029#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030030#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000031
32#include <unistd.h>
33#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000034#include <time.h>
35#include <errno.h>
36#include <sys/time.h>
37#include <zlib.h>
38
39#ifndef _WIN32
40#include <sys/times.h>
41#include <sys/wait.h>
42#include <termios.h>
43#include <sys/mman.h>
44#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000046#include <sys/socket.h>
47#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <dirent.h>
51#include <netdb.h>
52#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020053#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000054#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040055#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56#include <dev/ppbus/ppi.h>
57#include <dev/ppbus/ppbconf.h>
58#elif defined(__DragonFly__)
59#include <dev/misc/ppi/ppi.h>
60#include <bus/ppbus/ppbconf.h>
61#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
aliguori6f97dba2008-10-31 18:49:55 +000063#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000064#include <linux/ppdev.h>
65#include <linux/parport.h>
66#endif
67#ifdef __sun__
68#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
71#include <netinet/arp.h>
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000078#endif
79#endif
80#endif
81
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010082#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020083#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000084
Amit Shah9bd78542009-11-03 19:59:54 +053085#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030086#define READ_RETRIES 10
Amit Shah9bd78542009-11-03 19:59:54 +053087
aliguori6f97dba2008-10-31 18:49:55 +000088/***********************************************************/
89/* character device */
90
Blue Swirl72cf2d42009-09-12 07:36:22 +000091static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000093
Hans de Goedea425d232011-11-19 10:22:43 +010094void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +000095{
Alexander Graf73cdf3f2010-04-01 18:42:39 +020096 /* Keep track if the char device is open */
97 switch (event) {
98 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +010099 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200100 break;
101 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100102 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200103 break;
104 }
105
aliguori6f97dba2008-10-31 18:49:55 +0000106 if (!s->chr_event)
107 return;
108 s->chr_event(s->handler_opaque, event);
109}
110
Hans de Goedefee204f2013-03-26 11:07:54 +0100111void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000112{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500113 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000114}
115
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500116int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000117{
118 return s->chr_write(s, buf, len);
119}
120
Anthony Liguoricd187202013-03-26 10:04:17 -0500121int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
122{
123 int offset = 0;
124 int res;
125
126 while (offset < len) {
127 do {
128 res = s->chr_write(s, buf + offset, len - offset);
129 if (res == -1 && errno == EAGAIN) {
130 g_usleep(100);
131 }
132 } while (res == -1 && errno == EAGAIN);
133
134 if (res == 0) {
135 break;
136 }
137
138 if (res < 0) {
139 return res;
140 }
141
142 offset += res;
143 }
144
145 return offset;
146}
147
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300148int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
149{
150 int offset = 0, counter = 10;
151 int res;
152
153 if (!s->chr_sync_read) {
154 return 0;
155 }
156
157 while (offset < len) {
158 do {
159 res = s->chr_sync_read(s, buf + offset, len - offset);
160 if (res == -1 && errno == EAGAIN) {
161 g_usleep(100);
162 }
163 } while (res == -1 && errno == EAGAIN);
164
165 if (res == 0) {
166 break;
167 }
168
169 if (res < 0) {
170 return res;
171 }
172
173 offset += res;
174
175 if (!counter--) {
176 break;
177 }
178 }
179
180 return offset;
181}
182
Anthony Liguori41084f12011-08-15 11:17:34 -0500183int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000184{
185 if (!s->chr_ioctl)
186 return -ENOTSUP;
187 return s->chr_ioctl(s, cmd, arg);
188}
189
Anthony Liguori909cda12011-08-15 11:17:31 -0500190int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000191{
192 if (!s->chr_can_read)
193 return 0;
194 return s->chr_can_read(s->handler_opaque);
195}
196
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500197void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000198{
Stefan Weilac310732012-04-19 22:27:14 +0200199 if (s->chr_read) {
200 s->chr_read(s->handler_opaque, buf, len);
201 }
aliguori6f97dba2008-10-31 18:49:55 +0000202}
203
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500204int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100205{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300206 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800207 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300208}
209
210int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
211{
212 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100213}
214
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300215int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
216{
217 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
218}
219
Daniel P. Berrange13661082011-06-23 13:31:42 +0100220int qemu_chr_add_client(CharDriverState *s, int fd)
221{
222 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
223}
224
aliguori6f97dba2008-10-31 18:49:55 +0000225void qemu_chr_accept_input(CharDriverState *s)
226{
227 if (s->chr_accept_input)
228 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100229 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000230}
231
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500232void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000233{
Amit Shah9bd78542009-11-03 19:59:54 +0530234 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000235 va_list ap;
236 va_start(ap, fmt);
237 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500238 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000239 va_end(ap);
240}
241
Amit Shah386a5a12013-08-28 15:24:05 +0530242static void remove_fd_in_watch(CharDriverState *chr);
243
aliguori6f97dba2008-10-31 18:49:55 +0000244void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100245 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000246 IOReadHandler *fd_read,
247 IOEventHandler *fd_event,
248 void *opaque)
249{
Hans de Goede19083222013-03-26 11:07:56 +0100250 int fe_open;
251
Amit Shahda7d9982011-04-25 15:18:22 +0530252 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100253 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530254 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100255 } else {
256 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530257 }
aliguori6f97dba2008-10-31 18:49:55 +0000258 s->chr_can_read = fd_can_read;
259 s->chr_read = fd_read;
260 s->chr_event = fd_event;
261 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200262 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000263 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200264
Hans de Goede19083222013-03-26 11:07:56 +0100265 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100266 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100267 }
268
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200269 /* We're connecting to an already opened device, so let's make sure we
270 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100271 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100272 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200273 }
aliguori6f97dba2008-10-31 18:49:55 +0000274}
275
276static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
277{
278 return len;
279}
280
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100281static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000282{
283 CharDriverState *chr;
284
Anthony Liguori7267c092011-08-20 22:09:37 -0500285 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000286 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500287 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100288 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000289}
290
291/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000292#define MAX_MUX 4
293#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
294#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
295typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100296 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000297 IOReadHandler *chr_read[MAX_MUX];
298 IOEventHandler *chr_event[MAX_MUX];
299 void *ext_opaque[MAX_MUX];
300 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200301 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000302 int mux_cnt;
303 int term_got_escape;
304 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000305 /* Intermediate input buffer allows to catch escape sequences even if the
306 currently active device is not accepting any input - but only until it
307 is full as well. */
308 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
309 int prod[MAX_MUX];
310 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200311 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200312 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200313 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000314} MuxDriver;
315
316
317static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
318{
319 MuxDriver *d = chr->opaque;
320 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200321 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000322 ret = d->drv->chr_write(d->drv, buf, len);
323 } else {
324 int i;
325
326 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200327 for (i = 0; i < len; i++) {
328 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000329 char buf1[64];
330 int64_t ti;
331 int secs;
332
Alex Blighbc72ad62013-08-21 16:03:08 +0100333 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200334 if (d->timestamps_start == -1)
335 d->timestamps_start = ti;
336 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000337 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000338 snprintf(buf1, sizeof(buf1),
339 "[%02d:%02d:%02d.%03d] ",
340 secs / 3600,
341 (secs / 60) % 60,
342 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000343 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000344 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200345 d->linestart = 0;
346 }
347 ret += d->drv->chr_write(d->drv, buf+i, 1);
348 if (buf[i] == '\n') {
349 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000350 }
351 }
352 }
353 return ret;
354}
355
356static const char * const mux_help[] = {
357 "% h print this help\n\r",
358 "% x exit emulator\n\r",
359 "% s save disk data back to file (if -snapshot)\n\r",
360 "% t toggle console timestamps\n\r"
361 "% b send break (magic sysrq)\n\r",
362 "% c switch between console and monitor\n\r",
363 "% % sends %\n\r",
364 NULL
365};
366
367int term_escape_char = 0x01; /* ctrl-a is used for escape */
368static void mux_print_help(CharDriverState *chr)
369{
370 int i, j;
371 char ebuf[15] = "Escape-Char";
372 char cbuf[50] = "\n\r";
373
374 if (term_escape_char > 0 && term_escape_char < 26) {
375 snprintf(cbuf, sizeof(cbuf), "\n\r");
376 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
377 } else {
378 snprintf(cbuf, sizeof(cbuf),
379 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
380 term_escape_char);
381 }
382 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
383 for (i = 0; mux_help[i] != NULL; i++) {
384 for (j=0; mux_help[i][j] != '\0'; j++) {
385 if (mux_help[i][j] == '%')
386 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
387 else
388 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
389 }
390 }
391}
392
aliguori2724b182009-03-05 23:01:47 +0000393static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
394{
395 if (d->chr_event[mux_nr])
396 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
397}
398
aliguori6f97dba2008-10-31 18:49:55 +0000399static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
400{
401 if (d->term_got_escape) {
402 d->term_got_escape = 0;
403 if (ch == term_escape_char)
404 goto send_char;
405 switch(ch) {
406 case '?':
407 case 'h':
408 mux_print_help(chr);
409 break;
410 case 'x':
411 {
412 const char *term = "QEMU: Terminated\n\r";
413 chr->chr_write(chr,(uint8_t *)term,strlen(term));
414 exit(0);
415 break;
416 }
417 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200418 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000419 break;
420 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100421 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000422 break;
423 case 'c':
424 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200425 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
426 d->focus++;
427 if (d->focus >= d->mux_cnt)
428 d->focus = 0;
429 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000430 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200431 case 't':
432 d->timestamps = !d->timestamps;
433 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200434 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200435 break;
aliguori6f97dba2008-10-31 18:49:55 +0000436 }
437 } else if (ch == term_escape_char) {
438 d->term_got_escape = 1;
439 } else {
440 send_char:
441 return 1;
442 }
443 return 0;
444}
445
446static void mux_chr_accept_input(CharDriverState *chr)
447{
aliguori6f97dba2008-10-31 18:49:55 +0000448 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200449 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000450
aliguoria80bf992009-03-05 23:00:02 +0000451 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000452 d->chr_can_read[m] &&
453 d->chr_can_read[m](d->ext_opaque[m])) {
454 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000455 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000456 }
457}
458
459static int mux_chr_can_read(void *opaque)
460{
461 CharDriverState *chr = opaque;
462 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200463 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000464
aliguoria80bf992009-03-05 23:00:02 +0000465 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000466 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000467 if (d->chr_can_read[m])
468 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000469 return 0;
470}
471
472static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
473{
474 CharDriverState *chr = opaque;
475 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200476 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000477 int i;
478
479 mux_chr_accept_input (opaque);
480
481 for(i = 0; i < size; i++)
482 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000483 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000484 d->chr_can_read[m] &&
485 d->chr_can_read[m](d->ext_opaque[m]))
486 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
487 else
aliguoria80bf992009-03-05 23:00:02 +0000488 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000489 }
490}
491
492static void mux_chr_event(void *opaque, int event)
493{
494 CharDriverState *chr = opaque;
495 MuxDriver *d = chr->opaque;
496 int i;
497
498 /* Send the event to all registered listeners */
499 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000500 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000501}
502
503static void mux_chr_update_read_handler(CharDriverState *chr)
504{
505 MuxDriver *d = chr->opaque;
506
507 if (d->mux_cnt >= MAX_MUX) {
508 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
509 return;
510 }
511 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
512 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
513 d->chr_read[d->mux_cnt] = chr->chr_read;
514 d->chr_event[d->mux_cnt] = chr->chr_event;
515 /* Fix up the real driver with mux routines */
516 if (d->mux_cnt == 0) {
517 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
518 mux_chr_event, chr);
519 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200520 if (d->focus != -1) {
521 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200522 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200523 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000524 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200525 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000526}
527
Michael Roth7b7ab182013-07-30 13:04:22 -0500528static bool muxes_realized;
529
530/**
531 * Called after processing of default and command-line-specified
532 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
533 * to a mux chardev. This is done here to ensure that
534 * output/prompts/banners are only displayed for the FE that has
535 * focus when initial command-line processing/machine init is
536 * completed.
537 *
538 * After this point, any new FE attached to any new or existing
539 * mux will receive CHR_EVENT_OPENED notifications for the BE
540 * immediately.
541 */
542static void muxes_realize_done(Notifier *notifier, void *unused)
543{
544 CharDriverState *chr;
545
546 QTAILQ_FOREACH(chr, &chardevs, next) {
547 if (chr->is_mux) {
548 MuxDriver *d = chr->opaque;
549 int i;
550
551 /* send OPENED to all already-attached FEs */
552 for (i = 0; i < d->mux_cnt; i++) {
553 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
554 }
555 /* mark mux as OPENED so any new FEs will immediately receive
556 * OPENED event
557 */
558 qemu_chr_be_generic_open(chr);
559 }
560 }
561 muxes_realized = true;
562}
563
564static Notifier muxes_realize_notify = {
565 .notify = muxes_realize_done,
566};
567
aliguori6f97dba2008-10-31 18:49:55 +0000568static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
569{
570 CharDriverState *chr;
571 MuxDriver *d;
572
Anthony Liguori7267c092011-08-20 22:09:37 -0500573 chr = g_malloc0(sizeof(CharDriverState));
574 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000575
576 chr->opaque = d;
577 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200578 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000579 chr->chr_write = mux_chr_write;
580 chr->chr_update_read_handler = mux_chr_update_read_handler;
581 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100582 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100583 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500584 /* only default to opened state if we've realized the initial
585 * set of muxes
586 */
587 chr->explicit_be_open = muxes_realized ? 0 : 1;
588 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200589
aliguori6f97dba2008-10-31 18:49:55 +0000590 return chr;
591}
592
593
594#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000595int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000596{
597 int ret, len;
598
599 len = len1;
600 while (len > 0) {
601 ret = send(fd, buf, len, 0);
602 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000603 errno = WSAGetLastError();
604 if (errno != WSAEWOULDBLOCK) {
605 return -1;
606 }
607 } else if (ret == 0) {
608 break;
609 } else {
610 buf += ret;
611 len -= ret;
612 }
613 }
614 return len1 - len;
615}
616
617#else
618
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100619int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000620{
621 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100622 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000623
624 len = len1;
625 while (len > 0) {
626 ret = write(fd, buf, len);
627 if (ret < 0) {
628 if (errno != EINTR && errno != EAGAIN)
629 return -1;
630 } else if (ret == 0) {
631 break;
632 } else {
633 buf += ret;
634 len -= ret;
635 }
636 }
637 return len1 - len;
638}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500639
640int recv_all(int fd, void *_buf, int len1, bool single_read)
641{
642 int ret, len;
643 uint8_t *buf = _buf;
644
645 len = len1;
646 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
647 if (ret < 0) {
648 if (errno != EINTR && errno != EAGAIN) {
649 return -1;
650 }
651 continue;
652 } else {
653 if (single_read) {
654 return ret;
655 }
656 buf += ret;
657 len -= ret;
658 }
659 }
660 return len1 - len;
661}
662
aliguori6f97dba2008-10-31 18:49:55 +0000663#endif /* !_WIN32 */
664
Anthony Liguori96c63842013-03-05 23:21:18 +0530665typedef struct IOWatchPoll
666{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200667 GSource parent;
668
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200669 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530670 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530671
672 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200673 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530674 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530675} IOWatchPoll;
676
Anthony Liguori96c63842013-03-05 23:21:18 +0530677static IOWatchPoll *io_watch_poll_from_source(GSource *source)
678{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200679 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530680}
681
682static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
683{
684 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200685 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200686 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200687 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530688 return FALSE;
689 }
690
Paolo Bonzinid185c092013-04-05 17:59:33 +0200691 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200692 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
693 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200694 g_source_attach(iwp->src, NULL);
695 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200696 g_source_destroy(iwp->src);
697 g_source_unref(iwp->src);
698 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200699 }
700 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530701}
702
703static gboolean io_watch_poll_check(GSource *source)
704{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200705 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530706}
707
708static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
709 gpointer user_data)
710{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200711 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530712}
713
714static void io_watch_poll_finalize(GSource *source)
715{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200716 /* Due to a glib bug, removing the last reference to a source
717 * inside a finalize callback causes recursive locking (and a
718 * deadlock). This is not a problem inside other callbacks,
719 * including dispatch callbacks, so we call io_remove_watch_poll
720 * to remove this source. At this point, iwp->src must
721 * be NULL, or we would leak it.
722 *
723 * This would be solved much more elegantly by child sources,
724 * but we support older glib versions that do not have them.
725 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530726 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200727 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530728}
729
730static GSourceFuncs io_watch_poll_funcs = {
731 .prepare = io_watch_poll_prepare,
732 .check = io_watch_poll_check,
733 .dispatch = io_watch_poll_dispatch,
734 .finalize = io_watch_poll_finalize,
735};
736
737/* Can only be used for read */
738static guint io_add_watch_poll(GIOChannel *channel,
739 IOCanReadHandler *fd_can_read,
740 GIOFunc fd_read,
741 gpointer user_data)
742{
743 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200744 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530745
Paolo Bonzinid185c092013-04-05 17:59:33 +0200746 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530747 iwp->fd_can_read = fd_can_read;
748 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200749 iwp->channel = channel;
750 iwp->fd_read = (GSourceFunc) fd_read;
751 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530752
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200753 tag = g_source_attach(&iwp->parent, NULL);
754 g_source_unref(&iwp->parent);
755 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530756}
757
Paolo Bonzini2b316772013-04-19 17:32:09 +0200758static void io_remove_watch_poll(guint tag)
759{
760 GSource *source;
761 IOWatchPoll *iwp;
762
763 g_return_if_fail (tag > 0);
764
765 source = g_main_context_find_source_by_id(NULL, tag);
766 g_return_if_fail (source != NULL);
767
768 iwp = io_watch_poll_from_source(source);
769 if (iwp->src) {
770 g_source_destroy(iwp->src);
771 g_source_unref(iwp->src);
772 iwp->src = NULL;
773 }
774 g_source_destroy(&iwp->parent);
775}
776
Amit Shah26da70c2013-08-28 15:23:37 +0530777static void remove_fd_in_watch(CharDriverState *chr)
778{
779 if (chr->fd_in_tag) {
780 io_remove_watch_poll(chr->fd_in_tag);
781 chr->fd_in_tag = 0;
782 }
783}
784
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000785#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530786static GIOChannel *io_channel_from_fd(int fd)
787{
788 GIOChannel *chan;
789
790 if (fd == -1) {
791 return NULL;
792 }
793
794 chan = g_io_channel_unix_new(fd);
795
796 g_io_channel_set_encoding(chan, NULL, NULL);
797 g_io_channel_set_buffered(chan, FALSE);
798
799 return chan;
800}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000801#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530802
Anthony Liguori76a96442013-03-05 23:21:21 +0530803static GIOChannel *io_channel_from_socket(int fd)
804{
805 GIOChannel *chan;
806
807 if (fd == -1) {
808 return NULL;
809 }
810
811#ifdef _WIN32
812 chan = g_io_channel_win32_new_socket(fd);
813#else
814 chan = g_io_channel_unix_new(fd);
815#endif
816
817 g_io_channel_set_encoding(chan, NULL, NULL);
818 g_io_channel_set_buffered(chan, FALSE);
819
820 return chan;
821}
822
Anthony Liguori684a0962013-03-29 11:39:50 -0500823static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530824{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200825 size_t offset = 0;
826 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530827
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200828 while (offset < len && status == G_IO_STATUS_NORMAL) {
829 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500830
831 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530832 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500833 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530834 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500835
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200836 if (offset > 0) {
837 return offset;
838 }
839 switch (status) {
840 case G_IO_STATUS_NORMAL:
841 g_assert(len == 0);
842 return 0;
843 case G_IO_STATUS_AGAIN:
844 errno = EAGAIN;
845 return -1;
846 default:
847 break;
848 }
849 errno = EINVAL;
850 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530851}
Anthony Liguori96c63842013-03-05 23:21:18 +0530852
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000853#ifndef _WIN32
854
Anthony Liguoria29753f2013-03-05 23:21:19 +0530855typedef struct FDCharDriver {
856 CharDriverState *chr;
857 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000858 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530859 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000860} FDCharDriver;
861
aliguori6f97dba2008-10-31 18:49:55 +0000862static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
863{
864 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530865
Anthony Liguori684a0962013-03-29 11:39:50 -0500866 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530867}
868
869static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
870{
871 CharDriverState *chr = opaque;
872 FDCharDriver *s = chr->opaque;
873 int len;
874 uint8_t buf[READ_BUF_LEN];
875 GIOStatus status;
876 gsize bytes_read;
877
878 len = sizeof(buf);
879 if (len > s->max_size) {
880 len = s->max_size;
881 }
882 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200883 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530884 }
885
886 status = g_io_channel_read_chars(chan, (gchar *)buf,
887 len, &bytes_read, NULL);
888 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530889 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530890 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
891 return FALSE;
892 }
893 if (status == G_IO_STATUS_NORMAL) {
894 qemu_chr_be_write(chr, buf, bytes_read);
895 }
896
897 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000898}
899
900static int fd_chr_read_poll(void *opaque)
901{
902 CharDriverState *chr = opaque;
903 FDCharDriver *s = chr->opaque;
904
Anthony Liguori909cda12011-08-15 11:17:31 -0500905 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000906 return s->max_size;
907}
908
Anthony Liguori23673ca2013-03-05 23:21:23 +0530909static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
910{
911 FDCharDriver *s = chr->opaque;
912 return g_io_create_watch(s->fd_out, cond);
913}
914
aliguori6f97dba2008-10-31 18:49:55 +0000915static void fd_chr_update_read_handler(CharDriverState *chr)
916{
917 FDCharDriver *s = chr->opaque;
918
Amit Shah26da70c2013-08-28 15:23:37 +0530919 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530920 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530921 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
922 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000923 }
924}
925
926static void fd_chr_close(struct CharDriverState *chr)
927{
928 FDCharDriver *s = chr->opaque;
929
Amit Shah26da70c2013-08-28 15:23:37 +0530930 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530931 if (s->fd_in) {
932 g_io_channel_unref(s->fd_in);
933 }
934 if (s->fd_out) {
935 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000936 }
937
Anthony Liguori7267c092011-08-20 22:09:37 -0500938 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100939 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000940}
941
942/* open a character device to a unix fd */
943static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
944{
945 CharDriverState *chr;
946 FDCharDriver *s;
947
Anthony Liguori7267c092011-08-20 22:09:37 -0500948 chr = g_malloc0(sizeof(CharDriverState));
949 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530950 s->fd_in = io_channel_from_fd(fd_in);
951 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530952 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530953 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000954 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530955 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000956 chr->chr_write = fd_chr_write;
957 chr->chr_update_read_handler = fd_chr_update_read_handler;
958 chr->chr_close = fd_chr_close;
959
aliguori6f97dba2008-10-31 18:49:55 +0000960 return chr;
961}
962
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100963static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000964{
965 int fd_in, fd_out;
966 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100967 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200968
969 if (filename == NULL) {
970 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100971 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200972 }
aliguori6f97dba2008-10-31 18:49:55 +0000973
974 snprintf(filename_in, 256, "%s.in", filename);
975 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100976 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
977 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000978 if (fd_in < 0 || fd_out < 0) {
979 if (fd_in >= 0)
980 close(fd_in);
981 if (fd_out >= 0)
982 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100983 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100984 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100985 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100986 }
aliguori6f97dba2008-10-31 18:49:55 +0000987 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100988 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000989}
990
aliguori6f97dba2008-10-31 18:49:55 +0000991/* init terminal so that we can grab keys */
992static struct termios oldtty;
993static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100994static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000995
996static void term_exit(void)
997{
998 tcsetattr (0, TCSANOW, &oldtty);
999 fcntl(0, F_SETFL, old_fd0_flags);
1000}
1001
Paolo Bonzinibb002512010-12-23 13:42:50 +01001002static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001003{
1004 struct termios tty;
1005
Paolo Bonzini03693642010-12-23 13:42:49 +01001006 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001007 if (!echo) {
1008 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001009 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001010 tty.c_oflag |= OPOST;
1011 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1012 tty.c_cflag &= ~(CSIZE|PARENB);
1013 tty.c_cflag |= CS8;
1014 tty.c_cc[VMIN] = 1;
1015 tty.c_cc[VTIME] = 0;
1016 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001017 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001018 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001019
1020 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001021}
1022
1023static void qemu_chr_close_stdio(struct CharDriverState *chr)
1024{
1025 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001026 fd_chr_close(chr);
1027}
1028
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001029static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001030{
1031 CharDriverState *chr;
1032
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001033 if (is_daemonized()) {
1034 error_report("cannot use stdio with -daemonize");
1035 return NULL;
1036 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301037 old_fd0_flags = fcntl(0, F_GETFL);
1038 tcgetattr (0, &oldtty);
1039 fcntl(0, F_SETFL, O_NONBLOCK);
1040 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001041
aliguori6f97dba2008-10-31 18:49:55 +00001042 chr = qemu_chr_open_fd(0, 1);
1043 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001044 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001045 if (opts->has_signal) {
1046 stdio_allow_signal = opts->signal;
1047 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001048 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001049
Markus Armbruster1f514702012-02-07 15:09:08 +01001050 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001051}
1052
aliguori6f97dba2008-10-31 18:49:55 +00001053#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001054 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1055 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001056
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001057#define HAVE_CHARDEV_TTY 1
1058
aliguori6f97dba2008-10-31 18:49:55 +00001059typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301060 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001061 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001062 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301063 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001064} PtyCharDriver;
1065
1066static void pty_chr_update_read_handler(CharDriverState *chr);
1067static void pty_chr_state(CharDriverState *chr, int connected);
1068
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301069static gboolean pty_chr_timer(gpointer opaque)
1070{
1071 struct CharDriverState *chr = opaque;
1072 PtyCharDriver *s = chr->opaque;
1073
Hans de Goede79f20072013-04-25 13:53:02 +02001074 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001075 if (!s->connected) {
1076 /* Next poll ... */
1077 pty_chr_update_read_handler(chr);
1078 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301079 return FALSE;
1080}
1081
1082static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1083{
1084 PtyCharDriver *s = chr->opaque;
1085
1086 if (s->timer_tag) {
1087 g_source_remove(s->timer_tag);
1088 s->timer_tag = 0;
1089 }
1090
1091 if (ms == 1000) {
1092 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1093 } else {
1094 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1095 }
1096}
1097
aliguori6f97dba2008-10-31 18:49:55 +00001098static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1099{
1100 PtyCharDriver *s = chr->opaque;
1101
1102 if (!s->connected) {
1103 /* guest sends data, check for (re-)connect */
1104 pty_chr_update_read_handler(chr);
1105 return 0;
1106 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001107 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001108}
1109
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301110static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1111{
1112 PtyCharDriver *s = chr->opaque;
1113 return g_io_create_watch(s->fd, cond);
1114}
1115
aliguori6f97dba2008-10-31 18:49:55 +00001116static int pty_chr_read_poll(void *opaque)
1117{
1118 CharDriverState *chr = opaque;
1119 PtyCharDriver *s = chr->opaque;
1120
Anthony Liguori909cda12011-08-15 11:17:31 -05001121 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001122 return s->read_bytes;
1123}
1124
Anthony Liguori093d3a22013-03-05 23:21:20 +05301125static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001126{
1127 CharDriverState *chr = opaque;
1128 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301129 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301130 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301131 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001132
1133 len = sizeof(buf);
1134 if (len > s->read_bytes)
1135 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001136 if (len == 0) {
1137 return TRUE;
1138 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301139 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1140 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001141 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301142 return FALSE;
1143 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001144 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001145 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001146 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301147 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001148}
1149
1150static void pty_chr_update_read_handler(CharDriverState *chr)
1151{
1152 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001153 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001154
Paolo Bonzini85a67692013-04-19 17:32:07 +02001155 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1156 pfd.events = G_IO_OUT;
1157 pfd.revents = 0;
1158 g_poll(&pfd, 1, 0);
1159 if (pfd.revents & G_IO_HUP) {
1160 pty_chr_state(chr, 0);
1161 } else {
1162 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301163 }
aliguori6f97dba2008-10-31 18:49:55 +00001164}
1165
1166static void pty_chr_state(CharDriverState *chr, int connected)
1167{
1168 PtyCharDriver *s = chr->opaque;
1169
1170 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301171 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001172 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001173 /* (re-)connect poll interval for idle guests: once per second.
1174 * We check more frequently in case the guests sends data to
1175 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301176 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001177 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001178 if (s->timer_tag) {
1179 g_source_remove(s->timer_tag);
1180 s->timer_tag = 0;
1181 }
1182 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001183 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001184 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001185 }
1186 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301187 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1188 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001189 }
aliguori6f97dba2008-10-31 18:49:55 +00001190 }
1191}
1192
aliguori6f97dba2008-10-31 18:49:55 +00001193static void pty_chr_close(struct CharDriverState *chr)
1194{
1195 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301196 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001197
Amit Shah26da70c2013-08-28 15:23:37 +05301198 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301199 fd = g_io_channel_unix_get_fd(s->fd);
1200 g_io_channel_unref(s->fd);
1201 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301202 if (s->timer_tag) {
1203 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001204 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301205 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001206 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001207 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001208}
1209
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001210static CharDriverState *qemu_chr_open_pty(const char *id,
1211 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001212{
1213 CharDriverState *chr;
1214 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001215 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001216 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001217
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001218 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1219 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001220 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001221 }
1222
aliguori6f97dba2008-10-31 18:49:55 +00001223 close(slave_fd);
1224
Markus Armbrustera4e26042011-11-11 10:40:05 +01001225 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001226
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001227 chr->filename = g_strdup_printf("pty:%s", pty_name);
1228 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001229 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001230
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001231 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001232 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001233
1234 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001235 chr->opaque = s;
1236 chr->chr_write = pty_chr_write;
1237 chr->chr_update_read_handler = pty_chr_update_read_handler;
1238 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301239 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001240 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001241
Anthony Liguori093d3a22013-03-05 23:21:20 +05301242 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301243 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001244
Markus Armbruster1f514702012-02-07 15:09:08 +01001245 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001246}
1247
1248static void tty_serial_init(int fd, int speed,
1249 int parity, int data_bits, int stop_bits)
1250{
1251 struct termios tty;
1252 speed_t spd;
1253
1254#if 0
1255 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1256 speed, parity, data_bits, stop_bits);
1257#endif
1258 tcgetattr (fd, &tty);
1259
Stefan Weil45eea132009-10-26 16:10:10 +01001260#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1261 speed = speed * 10 / 11;
1262 do {
1263 check_speed(50);
1264 check_speed(75);
1265 check_speed(110);
1266 check_speed(134);
1267 check_speed(150);
1268 check_speed(200);
1269 check_speed(300);
1270 check_speed(600);
1271 check_speed(1200);
1272 check_speed(1800);
1273 check_speed(2400);
1274 check_speed(4800);
1275 check_speed(9600);
1276 check_speed(19200);
1277 check_speed(38400);
1278 /* Non-Posix values follow. They may be unsupported on some systems. */
1279 check_speed(57600);
1280 check_speed(115200);
1281#ifdef B230400
1282 check_speed(230400);
1283#endif
1284#ifdef B460800
1285 check_speed(460800);
1286#endif
1287#ifdef B500000
1288 check_speed(500000);
1289#endif
1290#ifdef B576000
1291 check_speed(576000);
1292#endif
1293#ifdef B921600
1294 check_speed(921600);
1295#endif
1296#ifdef B1000000
1297 check_speed(1000000);
1298#endif
1299#ifdef B1152000
1300 check_speed(1152000);
1301#endif
1302#ifdef B1500000
1303 check_speed(1500000);
1304#endif
1305#ifdef B2000000
1306 check_speed(2000000);
1307#endif
1308#ifdef B2500000
1309 check_speed(2500000);
1310#endif
1311#ifdef B3000000
1312 check_speed(3000000);
1313#endif
1314#ifdef B3500000
1315 check_speed(3500000);
1316#endif
1317#ifdef B4000000
1318 check_speed(4000000);
1319#endif
aliguori6f97dba2008-10-31 18:49:55 +00001320 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001321 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001322
1323 cfsetispeed(&tty, spd);
1324 cfsetospeed(&tty, spd);
1325
1326 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1327 |INLCR|IGNCR|ICRNL|IXON);
1328 tty.c_oflag |= OPOST;
1329 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1330 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1331 switch(data_bits) {
1332 default:
1333 case 8:
1334 tty.c_cflag |= CS8;
1335 break;
1336 case 7:
1337 tty.c_cflag |= CS7;
1338 break;
1339 case 6:
1340 tty.c_cflag |= CS6;
1341 break;
1342 case 5:
1343 tty.c_cflag |= CS5;
1344 break;
1345 }
1346 switch(parity) {
1347 default:
1348 case 'N':
1349 break;
1350 case 'E':
1351 tty.c_cflag |= PARENB;
1352 break;
1353 case 'O':
1354 tty.c_cflag |= PARENB | PARODD;
1355 break;
1356 }
1357 if (stop_bits == 2)
1358 tty.c_cflag |= CSTOPB;
1359
1360 tcsetattr (fd, TCSANOW, &tty);
1361}
1362
1363static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1364{
1365 FDCharDriver *s = chr->opaque;
1366
1367 switch(cmd) {
1368 case CHR_IOCTL_SERIAL_SET_PARAMS:
1369 {
1370 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301371 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1372 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001373 ssp->data_bits, ssp->stop_bits);
1374 }
1375 break;
1376 case CHR_IOCTL_SERIAL_SET_BREAK:
1377 {
1378 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301379 if (enable) {
1380 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1381 }
aliguori6f97dba2008-10-31 18:49:55 +00001382 }
1383 break;
1384 case CHR_IOCTL_SERIAL_GET_TIOCM:
1385 {
1386 int sarg = 0;
1387 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301388 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001389 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001390 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001391 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001392 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001393 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001394 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001395 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001396 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001397 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001398 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001399 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001400 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001401 *targ |= CHR_TIOCM_RTS;
1402 }
1403 break;
1404 case CHR_IOCTL_SERIAL_SET_TIOCM:
1405 {
1406 int sarg = *(int *)arg;
1407 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301408 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001409 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1410 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1411 if (sarg & CHR_TIOCM_CTS)
1412 targ |= TIOCM_CTS;
1413 if (sarg & CHR_TIOCM_CAR)
1414 targ |= TIOCM_CAR;
1415 if (sarg & CHR_TIOCM_DSR)
1416 targ |= TIOCM_DSR;
1417 if (sarg & CHR_TIOCM_RI)
1418 targ |= TIOCM_RI;
1419 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001420 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001421 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001422 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301423 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001424 }
1425 break;
1426 default:
1427 return -ENOTSUP;
1428 }
1429 return 0;
1430}
1431
David Ahern4266a132010-02-10 18:27:17 -07001432static void qemu_chr_close_tty(CharDriverState *chr)
1433{
1434 FDCharDriver *s = chr->opaque;
1435 int fd = -1;
1436
1437 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301438 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001439 }
1440
1441 fd_chr_close(chr);
1442
1443 if (fd >= 0) {
1444 close(fd);
1445 }
1446}
1447
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001448static CharDriverState *qemu_chr_open_tty_fd(int fd)
1449{
1450 CharDriverState *chr;
1451
1452 tty_serial_init(fd, 115200, 'N', 8, 1);
1453 chr = qemu_chr_open_fd(fd, fd);
1454 chr->chr_ioctl = tty_serial_ioctl;
1455 chr->chr_close = qemu_chr_close_tty;
1456 return chr;
1457}
aliguori6f97dba2008-10-31 18:49:55 +00001458#endif /* __linux__ || __sun__ */
1459
1460#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001461
1462#define HAVE_CHARDEV_PARPORT 1
1463
aliguori6f97dba2008-10-31 18:49:55 +00001464typedef struct {
1465 int fd;
1466 int mode;
1467} ParallelCharDriver;
1468
1469static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1470{
1471 if (s->mode != mode) {
1472 int m = mode;
1473 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1474 return 0;
1475 s->mode = mode;
1476 }
1477 return 1;
1478}
1479
1480static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1481{
1482 ParallelCharDriver *drv = chr->opaque;
1483 int fd = drv->fd;
1484 uint8_t b;
1485
1486 switch(cmd) {
1487 case CHR_IOCTL_PP_READ_DATA:
1488 if (ioctl(fd, PPRDATA, &b) < 0)
1489 return -ENOTSUP;
1490 *(uint8_t *)arg = b;
1491 break;
1492 case CHR_IOCTL_PP_WRITE_DATA:
1493 b = *(uint8_t *)arg;
1494 if (ioctl(fd, PPWDATA, &b) < 0)
1495 return -ENOTSUP;
1496 break;
1497 case CHR_IOCTL_PP_READ_CONTROL:
1498 if (ioctl(fd, PPRCONTROL, &b) < 0)
1499 return -ENOTSUP;
1500 /* Linux gives only the lowest bits, and no way to know data
1501 direction! For better compatibility set the fixed upper
1502 bits. */
1503 *(uint8_t *)arg = b | 0xc0;
1504 break;
1505 case CHR_IOCTL_PP_WRITE_CONTROL:
1506 b = *(uint8_t *)arg;
1507 if (ioctl(fd, PPWCONTROL, &b) < 0)
1508 return -ENOTSUP;
1509 break;
1510 case CHR_IOCTL_PP_READ_STATUS:
1511 if (ioctl(fd, PPRSTATUS, &b) < 0)
1512 return -ENOTSUP;
1513 *(uint8_t *)arg = b;
1514 break;
1515 case CHR_IOCTL_PP_DATA_DIR:
1516 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1517 return -ENOTSUP;
1518 break;
1519 case CHR_IOCTL_PP_EPP_READ_ADDR:
1520 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1521 struct ParallelIOArg *parg = arg;
1522 int n = read(fd, parg->buffer, parg->count);
1523 if (n != parg->count) {
1524 return -EIO;
1525 }
1526 }
1527 break;
1528 case CHR_IOCTL_PP_EPP_READ:
1529 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1530 struct ParallelIOArg *parg = arg;
1531 int n = read(fd, parg->buffer, parg->count);
1532 if (n != parg->count) {
1533 return -EIO;
1534 }
1535 }
1536 break;
1537 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1538 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1539 struct ParallelIOArg *parg = arg;
1540 int n = write(fd, parg->buffer, parg->count);
1541 if (n != parg->count) {
1542 return -EIO;
1543 }
1544 }
1545 break;
1546 case CHR_IOCTL_PP_EPP_WRITE:
1547 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1548 struct ParallelIOArg *parg = arg;
1549 int n = write(fd, parg->buffer, parg->count);
1550 if (n != parg->count) {
1551 return -EIO;
1552 }
1553 }
1554 break;
1555 default:
1556 return -ENOTSUP;
1557 }
1558 return 0;
1559}
1560
1561static void pp_close(CharDriverState *chr)
1562{
1563 ParallelCharDriver *drv = chr->opaque;
1564 int fd = drv->fd;
1565
1566 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1567 ioctl(fd, PPRELEASE);
1568 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001569 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001570 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001571}
1572
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001573static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001574{
1575 CharDriverState *chr;
1576 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001577
1578 if (ioctl(fd, PPCLAIM) < 0) {
1579 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001580 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001581 }
1582
Anthony Liguori7267c092011-08-20 22:09:37 -05001583 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001584 drv->fd = fd;
1585 drv->mode = IEEE1284_MODE_COMPAT;
1586
Anthony Liguori7267c092011-08-20 22:09:37 -05001587 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001588 chr->chr_write = null_chr_write;
1589 chr->chr_ioctl = pp_ioctl;
1590 chr->chr_close = pp_close;
1591 chr->opaque = drv;
1592
Markus Armbruster1f514702012-02-07 15:09:08 +01001593 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001594}
1595#endif /* __linux__ */
1596
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001597#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001598
1599#define HAVE_CHARDEV_PARPORT 1
1600
blueswir16972f932008-11-22 20:49:12 +00001601static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1602{
Stefan Weile0efb992011-02-23 19:09:16 +01001603 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001604 uint8_t b;
1605
1606 switch(cmd) {
1607 case CHR_IOCTL_PP_READ_DATA:
1608 if (ioctl(fd, PPIGDATA, &b) < 0)
1609 return -ENOTSUP;
1610 *(uint8_t *)arg = b;
1611 break;
1612 case CHR_IOCTL_PP_WRITE_DATA:
1613 b = *(uint8_t *)arg;
1614 if (ioctl(fd, PPISDATA, &b) < 0)
1615 return -ENOTSUP;
1616 break;
1617 case CHR_IOCTL_PP_READ_CONTROL:
1618 if (ioctl(fd, PPIGCTRL, &b) < 0)
1619 return -ENOTSUP;
1620 *(uint8_t *)arg = b;
1621 break;
1622 case CHR_IOCTL_PP_WRITE_CONTROL:
1623 b = *(uint8_t *)arg;
1624 if (ioctl(fd, PPISCTRL, &b) < 0)
1625 return -ENOTSUP;
1626 break;
1627 case CHR_IOCTL_PP_READ_STATUS:
1628 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1629 return -ENOTSUP;
1630 *(uint8_t *)arg = b;
1631 break;
1632 default:
1633 return -ENOTSUP;
1634 }
1635 return 0;
1636}
1637
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001638static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001639{
1640 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001641
Anthony Liguori7267c092011-08-20 22:09:37 -05001642 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001643 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001644 chr->chr_write = null_chr_write;
1645 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001646 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001647 return chr;
blueswir16972f932008-11-22 20:49:12 +00001648}
1649#endif
1650
aliguori6f97dba2008-10-31 18:49:55 +00001651#else /* _WIN32 */
1652
1653typedef struct {
1654 int max_size;
1655 HANDLE hcom, hrecv, hsend;
1656 OVERLAPPED orecv, osend;
1657 BOOL fpipe;
1658 DWORD len;
1659} WinCharState;
1660
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001661typedef struct {
1662 HANDLE hStdIn;
1663 HANDLE hInputReadyEvent;
1664 HANDLE hInputDoneEvent;
1665 HANDLE hInputThread;
1666 uint8_t win_stdio_buf;
1667} WinStdioCharState;
1668
aliguori6f97dba2008-10-31 18:49:55 +00001669#define NSENDBUF 2048
1670#define NRECVBUF 2048
1671#define MAXCONNECT 1
1672#define NTIMEOUT 5000
1673
1674static int win_chr_poll(void *opaque);
1675static int win_chr_pipe_poll(void *opaque);
1676
1677static void win_chr_close(CharDriverState *chr)
1678{
1679 WinCharState *s = chr->opaque;
1680
1681 if (s->hsend) {
1682 CloseHandle(s->hsend);
1683 s->hsend = NULL;
1684 }
1685 if (s->hrecv) {
1686 CloseHandle(s->hrecv);
1687 s->hrecv = NULL;
1688 }
1689 if (s->hcom) {
1690 CloseHandle(s->hcom);
1691 s->hcom = NULL;
1692 }
1693 if (s->fpipe)
1694 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1695 else
1696 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301697
Hans de Goedea425d232011-11-19 10:22:43 +01001698 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001699}
1700
1701static int win_chr_init(CharDriverState *chr, const char *filename)
1702{
1703 WinCharState *s = chr->opaque;
1704 COMMCONFIG comcfg;
1705 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1706 COMSTAT comstat;
1707 DWORD size;
1708 DWORD err;
1709
1710 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1711 if (!s->hsend) {
1712 fprintf(stderr, "Failed CreateEvent\n");
1713 goto fail;
1714 }
1715 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1716 if (!s->hrecv) {
1717 fprintf(stderr, "Failed CreateEvent\n");
1718 goto fail;
1719 }
1720
1721 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1722 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1723 if (s->hcom == INVALID_HANDLE_VALUE) {
1724 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1725 s->hcom = NULL;
1726 goto fail;
1727 }
1728
1729 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1730 fprintf(stderr, "Failed SetupComm\n");
1731 goto fail;
1732 }
1733
1734 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1735 size = sizeof(COMMCONFIG);
1736 GetDefaultCommConfig(filename, &comcfg, &size);
1737 comcfg.dcb.DCBlength = sizeof(DCB);
1738 CommConfigDialog(filename, NULL, &comcfg);
1739
1740 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1741 fprintf(stderr, "Failed SetCommState\n");
1742 goto fail;
1743 }
1744
1745 if (!SetCommMask(s->hcom, EV_ERR)) {
1746 fprintf(stderr, "Failed SetCommMask\n");
1747 goto fail;
1748 }
1749
1750 cto.ReadIntervalTimeout = MAXDWORD;
1751 if (!SetCommTimeouts(s->hcom, &cto)) {
1752 fprintf(stderr, "Failed SetCommTimeouts\n");
1753 goto fail;
1754 }
1755
1756 if (!ClearCommError(s->hcom, &err, &comstat)) {
1757 fprintf(stderr, "Failed ClearCommError\n");
1758 goto fail;
1759 }
1760 qemu_add_polling_cb(win_chr_poll, chr);
1761 return 0;
1762
1763 fail:
1764 win_chr_close(chr);
1765 return -1;
1766}
1767
1768static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1769{
1770 WinCharState *s = chr->opaque;
1771 DWORD len, ret, size, err;
1772
1773 len = len1;
1774 ZeroMemory(&s->osend, sizeof(s->osend));
1775 s->osend.hEvent = s->hsend;
1776 while (len > 0) {
1777 if (s->hsend)
1778 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1779 else
1780 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1781 if (!ret) {
1782 err = GetLastError();
1783 if (err == ERROR_IO_PENDING) {
1784 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1785 if (ret) {
1786 buf += size;
1787 len -= size;
1788 } else {
1789 break;
1790 }
1791 } else {
1792 break;
1793 }
1794 } else {
1795 buf += size;
1796 len -= size;
1797 }
1798 }
1799 return len1 - len;
1800}
1801
1802static int win_chr_read_poll(CharDriverState *chr)
1803{
1804 WinCharState *s = chr->opaque;
1805
Anthony Liguori909cda12011-08-15 11:17:31 -05001806 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001807 return s->max_size;
1808}
1809
1810static void win_chr_readfile(CharDriverState *chr)
1811{
1812 WinCharState *s = chr->opaque;
1813 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301814 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001815 DWORD size;
1816
1817 ZeroMemory(&s->orecv, sizeof(s->orecv));
1818 s->orecv.hEvent = s->hrecv;
1819 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1820 if (!ret) {
1821 err = GetLastError();
1822 if (err == ERROR_IO_PENDING) {
1823 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1824 }
1825 }
1826
1827 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001828 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001829 }
1830}
1831
1832static void win_chr_read(CharDriverState *chr)
1833{
1834 WinCharState *s = chr->opaque;
1835
1836 if (s->len > s->max_size)
1837 s->len = s->max_size;
1838 if (s->len == 0)
1839 return;
1840
1841 win_chr_readfile(chr);
1842}
1843
1844static int win_chr_poll(void *opaque)
1845{
1846 CharDriverState *chr = opaque;
1847 WinCharState *s = chr->opaque;
1848 COMSTAT status;
1849 DWORD comerr;
1850
1851 ClearCommError(s->hcom, &comerr, &status);
1852 if (status.cbInQue > 0) {
1853 s->len = status.cbInQue;
1854 win_chr_read_poll(chr);
1855 win_chr_read(chr);
1856 return 1;
1857 }
1858 return 0;
1859}
1860
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001861static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001862{
1863 CharDriverState *chr;
1864 WinCharState *s;
1865
Anthony Liguori7267c092011-08-20 22:09:37 -05001866 chr = g_malloc0(sizeof(CharDriverState));
1867 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001868 chr->opaque = s;
1869 chr->chr_write = win_chr_write;
1870 chr->chr_close = win_chr_close;
1871
1872 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001873 g_free(s);
1874 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001875 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001876 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001877 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001878}
1879
1880static int win_chr_pipe_poll(void *opaque)
1881{
1882 CharDriverState *chr = opaque;
1883 WinCharState *s = chr->opaque;
1884 DWORD size;
1885
1886 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1887 if (size > 0) {
1888 s->len = size;
1889 win_chr_read_poll(chr);
1890 win_chr_read(chr);
1891 return 1;
1892 }
1893 return 0;
1894}
1895
1896static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1897{
1898 WinCharState *s = chr->opaque;
1899 OVERLAPPED ov;
1900 int ret;
1901 DWORD size;
1902 char openname[256];
1903
1904 s->fpipe = TRUE;
1905
1906 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1907 if (!s->hsend) {
1908 fprintf(stderr, "Failed CreateEvent\n");
1909 goto fail;
1910 }
1911 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1912 if (!s->hrecv) {
1913 fprintf(stderr, "Failed CreateEvent\n");
1914 goto fail;
1915 }
1916
1917 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1918 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1919 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1920 PIPE_WAIT,
1921 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1922 if (s->hcom == INVALID_HANDLE_VALUE) {
1923 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1924 s->hcom = NULL;
1925 goto fail;
1926 }
1927
1928 ZeroMemory(&ov, sizeof(ov));
1929 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1930 ret = ConnectNamedPipe(s->hcom, &ov);
1931 if (ret) {
1932 fprintf(stderr, "Failed ConnectNamedPipe\n");
1933 goto fail;
1934 }
1935
1936 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1937 if (!ret) {
1938 fprintf(stderr, "Failed GetOverlappedResult\n");
1939 if (ov.hEvent) {
1940 CloseHandle(ov.hEvent);
1941 ov.hEvent = NULL;
1942 }
1943 goto fail;
1944 }
1945
1946 if (ov.hEvent) {
1947 CloseHandle(ov.hEvent);
1948 ov.hEvent = NULL;
1949 }
1950 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1951 return 0;
1952
1953 fail:
1954 win_chr_close(chr);
1955 return -1;
1956}
1957
1958
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001959static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001960{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001961 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001962 CharDriverState *chr;
1963 WinCharState *s;
1964
Anthony Liguori7267c092011-08-20 22:09:37 -05001965 chr = g_malloc0(sizeof(CharDriverState));
1966 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001967 chr->opaque = s;
1968 chr->chr_write = win_chr_write;
1969 chr->chr_close = win_chr_close;
1970
1971 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001972 g_free(s);
1973 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001974 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001975 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001976 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001977}
1978
Markus Armbruster1f514702012-02-07 15:09:08 +01001979static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001980{
1981 CharDriverState *chr;
1982 WinCharState *s;
1983
Anthony Liguori7267c092011-08-20 22:09:37 -05001984 chr = g_malloc0(sizeof(CharDriverState));
1985 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001986 s->hcom = fd_out;
1987 chr->opaque = s;
1988 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001989 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001990}
1991
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001992static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001993{
Markus Armbruster1f514702012-02-07 15:09:08 +01001994 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001995}
1996
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001997static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1998{
1999 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2000 DWORD dwSize;
2001 int len1;
2002
2003 len1 = len;
2004
2005 while (len1 > 0) {
2006 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2007 break;
2008 }
2009 buf += dwSize;
2010 len1 -= dwSize;
2011 }
2012
2013 return len - len1;
2014}
2015
2016static void win_stdio_wait_func(void *opaque)
2017{
2018 CharDriverState *chr = opaque;
2019 WinStdioCharState *stdio = chr->opaque;
2020 INPUT_RECORD buf[4];
2021 int ret;
2022 DWORD dwSize;
2023 int i;
2024
Stefan Weildff74242013-12-07 14:48:04 +01002025 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002026
2027 if (!ret) {
2028 /* Avoid error storm */
2029 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2030 return;
2031 }
2032
2033 for (i = 0; i < dwSize; i++) {
2034 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2035
2036 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2037 int j;
2038 if (kev->uChar.AsciiChar != 0) {
2039 for (j = 0; j < kev->wRepeatCount; j++) {
2040 if (qemu_chr_be_can_write(chr)) {
2041 uint8_t c = kev->uChar.AsciiChar;
2042 qemu_chr_be_write(chr, &c, 1);
2043 }
2044 }
2045 }
2046 }
2047 }
2048}
2049
2050static DWORD WINAPI win_stdio_thread(LPVOID param)
2051{
2052 CharDriverState *chr = param;
2053 WinStdioCharState *stdio = chr->opaque;
2054 int ret;
2055 DWORD dwSize;
2056
2057 while (1) {
2058
2059 /* Wait for one byte */
2060 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2061
2062 /* Exit in case of error, continue if nothing read */
2063 if (!ret) {
2064 break;
2065 }
2066 if (!dwSize) {
2067 continue;
2068 }
2069
2070 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2071 if (stdio->win_stdio_buf == '\r') {
2072 continue;
2073 }
2074
2075 /* Signal the main thread and wait until the byte was eaten */
2076 if (!SetEvent(stdio->hInputReadyEvent)) {
2077 break;
2078 }
2079 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2080 != WAIT_OBJECT_0) {
2081 break;
2082 }
2083 }
2084
2085 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2086 return 0;
2087}
2088
2089static void win_stdio_thread_wait_func(void *opaque)
2090{
2091 CharDriverState *chr = opaque;
2092 WinStdioCharState *stdio = chr->opaque;
2093
2094 if (qemu_chr_be_can_write(chr)) {
2095 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2096 }
2097
2098 SetEvent(stdio->hInputDoneEvent);
2099}
2100
2101static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2102{
2103 WinStdioCharState *stdio = chr->opaque;
2104 DWORD dwMode = 0;
2105
2106 GetConsoleMode(stdio->hStdIn, &dwMode);
2107
2108 if (echo) {
2109 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2110 } else {
2111 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2112 }
2113}
2114
2115static void win_stdio_close(CharDriverState *chr)
2116{
2117 WinStdioCharState *stdio = chr->opaque;
2118
2119 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2120 CloseHandle(stdio->hInputReadyEvent);
2121 }
2122 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2123 CloseHandle(stdio->hInputDoneEvent);
2124 }
2125 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2126 TerminateThread(stdio->hInputThread, 0);
2127 }
2128
2129 g_free(chr->opaque);
2130 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002131}
2132
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002133static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002134{
2135 CharDriverState *chr;
2136 WinStdioCharState *stdio;
2137 DWORD dwMode;
2138 int is_console = 0;
2139
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002140 chr = g_malloc0(sizeof(CharDriverState));
2141 stdio = g_malloc0(sizeof(WinStdioCharState));
2142
2143 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2144 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2145 fprintf(stderr, "cannot open stdio: invalid handle\n");
2146 exit(1);
2147 }
2148
2149 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2150
2151 chr->opaque = stdio;
2152 chr->chr_write = win_stdio_write;
2153 chr->chr_close = win_stdio_close;
2154
Anthony Liguoried7a1542013-03-05 23:21:17 +05302155 if (is_console) {
2156 if (qemu_add_wait_object(stdio->hStdIn,
2157 win_stdio_wait_func, chr)) {
2158 fprintf(stderr, "qemu_add_wait_object: failed\n");
2159 }
2160 } else {
2161 DWORD dwId;
2162
2163 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2164 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2165 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2166 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002167
Anthony Liguoried7a1542013-03-05 23:21:17 +05302168 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2169 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2170 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2171 fprintf(stderr, "cannot create stdio thread or event\n");
2172 exit(1);
2173 }
2174 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2175 win_stdio_thread_wait_func, chr)) {
2176 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002177 }
2178 }
2179
2180 dwMode |= ENABLE_LINE_INPUT;
2181
Anthony Liguoried7a1542013-03-05 23:21:17 +05302182 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002183 /* set the terminal in raw mode */
2184 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2185 dwMode |= ENABLE_PROCESSED_INPUT;
2186 }
2187
2188 SetConsoleMode(stdio->hStdIn, dwMode);
2189
2190 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2191 qemu_chr_fe_set_echo(chr, false);
2192
Markus Armbruster1f514702012-02-07 15:09:08 +01002193 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002194}
aliguori6f97dba2008-10-31 18:49:55 +00002195#endif /* !_WIN32 */
2196
Anthony Liguori5ab82112013-03-05 23:21:31 +05302197
aliguori6f97dba2008-10-31 18:49:55 +00002198/***********************************************************/
2199/* UDP Net console */
2200
2201typedef struct {
2202 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302203 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302204 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002205 int bufcnt;
2206 int bufptr;
2207 int max_size;
2208} NetCharDriver;
2209
2210static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2211{
2212 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302213 gsize bytes_written;
2214 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002215
Anthony Liguori76a96442013-03-05 23:21:21 +05302216 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2217 if (status == G_IO_STATUS_EOF) {
2218 return 0;
2219 } else if (status != G_IO_STATUS_NORMAL) {
2220 return -1;
2221 }
2222
2223 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002224}
2225
2226static int udp_chr_read_poll(void *opaque)
2227{
2228 CharDriverState *chr = opaque;
2229 NetCharDriver *s = chr->opaque;
2230
Anthony Liguori909cda12011-08-15 11:17:31 -05002231 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002232
2233 /* If there were any stray characters in the queue process them
2234 * first
2235 */
2236 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002237 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002238 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002239 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002240 }
2241 return s->max_size;
2242}
2243
Anthony Liguori76a96442013-03-05 23:21:21 +05302244static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002245{
2246 CharDriverState *chr = opaque;
2247 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302248 gsize bytes_read = 0;
2249 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002250
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002251 if (s->max_size == 0) {
2252 return TRUE;
2253 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302254 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2255 &bytes_read, NULL);
2256 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002257 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302258 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302259 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302260 return FALSE;
2261 }
aliguori6f97dba2008-10-31 18:49:55 +00002262
2263 s->bufptr = 0;
2264 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002265 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002266 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002267 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002268 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302269
2270 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002271}
2272
2273static void udp_chr_update_read_handler(CharDriverState *chr)
2274{
2275 NetCharDriver *s = chr->opaque;
2276
Amit Shah26da70c2013-08-28 15:23:37 +05302277 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302278 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302279 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2280 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002281 }
2282}
2283
aliguori819f56b2009-03-28 17:58:14 +00002284static void udp_chr_close(CharDriverState *chr)
2285{
2286 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302287
2288 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302289 if (s->chan) {
2290 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002291 closesocket(s->fd);
2292 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002293 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002294 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002295}
2296
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002297static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002298{
2299 CharDriverState *chr = NULL;
2300 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002301
Anthony Liguori7267c092011-08-20 22:09:37 -05002302 chr = g_malloc0(sizeof(CharDriverState));
2303 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002304
aliguori6f97dba2008-10-31 18:49:55 +00002305 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302306 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002307 s->bufcnt = 0;
2308 s->bufptr = 0;
2309 chr->opaque = s;
2310 chr->chr_write = udp_chr_write;
2311 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002312 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002313 /* be isn't opened until we get a connection */
2314 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002315 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002316}
aliguori6f97dba2008-10-31 18:49:55 +00002317
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002318static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2319{
2320 Error *local_err = NULL;
2321 int fd = -1;
2322
2323 fd = inet_dgram_opts(opts, &local_err);
2324 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002325 qerror_report_err(local_err);
2326 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002327 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002328 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002329 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002330}
2331
2332/***********************************************************/
2333/* TCP Net console */
2334
2335typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302336
2337 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302338 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002339 int fd, listen_fd;
2340 int connected;
2341 int max_size;
2342 int do_telnetopt;
2343 int do_nodelay;
2344 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002345 int *read_msgfds;
2346 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002347 int *write_msgfds;
2348 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002349} TCPCharDriver;
2350
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302351static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002352
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002353#ifndef _WIN32
2354static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2355{
2356 TCPCharDriver *s = chr->opaque;
2357 struct msghdr msgh;
2358 struct iovec iov;
2359 int r;
2360
2361 size_t fd_size = s->write_msgfds_num * sizeof(int);
2362 char control[CMSG_SPACE(fd_size)];
2363 struct cmsghdr *cmsg;
2364
2365 memset(&msgh, 0, sizeof(msgh));
2366 memset(control, 0, sizeof(control));
2367
2368 /* set the payload */
2369 iov.iov_base = (uint8_t *) buf;
2370 iov.iov_len = len;
2371
2372 msgh.msg_iov = &iov;
2373 msgh.msg_iovlen = 1;
2374
2375 msgh.msg_control = control;
2376 msgh.msg_controllen = sizeof(control);
2377
2378 cmsg = CMSG_FIRSTHDR(&msgh);
2379
2380 cmsg->cmsg_len = CMSG_LEN(fd_size);
2381 cmsg->cmsg_level = SOL_SOCKET;
2382 cmsg->cmsg_type = SCM_RIGHTS;
2383 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2384
2385 do {
2386 r = sendmsg(s->fd, &msgh, 0);
2387 } while (r < 0 && errno == EINTR);
2388
2389 /* free the written msgfds, no matter what */
2390 if (s->write_msgfds_num) {
2391 g_free(s->write_msgfds);
2392 s->write_msgfds = 0;
2393 s->write_msgfds_num = 0;
2394 }
2395
2396 return r;
2397}
2398#endif
2399
aliguori6f97dba2008-10-31 18:49:55 +00002400static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2401{
2402 TCPCharDriver *s = chr->opaque;
2403 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002404#ifndef _WIN32
2405 if (s->is_unix && s->write_msgfds_num) {
2406 return unix_send_msgfds(chr, buf, len);
2407 } else
2408#endif
2409 {
2410 return io_channel_send(s->chan, buf, len);
2411 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002412 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002413 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002414 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002415 }
2416}
2417
2418static int tcp_chr_read_poll(void *opaque)
2419{
2420 CharDriverState *chr = opaque;
2421 TCPCharDriver *s = chr->opaque;
2422 if (!s->connected)
2423 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002424 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002425 return s->max_size;
2426}
2427
2428#define IAC 255
2429#define IAC_BREAK 243
2430static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2431 TCPCharDriver *s,
2432 uint8_t *buf, int *size)
2433{
2434 /* Handle any telnet client's basic IAC options to satisfy char by
2435 * char mode with no echo. All IAC options will be removed from
2436 * the buf and the do_telnetopt variable will be used to track the
2437 * state of the width of the IAC information.
2438 *
2439 * IAC commands come in sets of 3 bytes with the exception of the
2440 * "IAC BREAK" command and the double IAC.
2441 */
2442
2443 int i;
2444 int j = 0;
2445
2446 for (i = 0; i < *size; i++) {
2447 if (s->do_telnetopt > 1) {
2448 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2449 /* Double IAC means send an IAC */
2450 if (j != i)
2451 buf[j] = buf[i];
2452 j++;
2453 s->do_telnetopt = 1;
2454 } else {
2455 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2456 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002457 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002458 s->do_telnetopt++;
2459 }
2460 s->do_telnetopt++;
2461 }
2462 if (s->do_telnetopt >= 4) {
2463 s->do_telnetopt = 1;
2464 }
2465 } else {
2466 if ((unsigned char)buf[i] == IAC) {
2467 s->do_telnetopt = 2;
2468 } else {
2469 if (j != i)
2470 buf[j] = buf[i];
2471 j++;
2472 }
2473 }
2474 }
2475 *size = j;
2476}
2477
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002478static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002479{
2480 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002481 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2482
2483 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002484 int i;
2485
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002486 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2487
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002488 /* Close unused fds */
2489 for (i = to_copy; i < s->read_msgfds_num; i++) {
2490 close(s->read_msgfds[i]);
2491 }
2492
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002493 g_free(s->read_msgfds);
2494 s->read_msgfds = 0;
2495 s->read_msgfds_num = 0;
2496 }
2497
2498 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002499}
2500
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002501static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2502{
2503 TCPCharDriver *s = chr->opaque;
2504
2505 /* clear old pending fd array */
2506 if (s->write_msgfds) {
2507 g_free(s->write_msgfds);
2508 }
2509
2510 if (num) {
2511 s->write_msgfds = g_malloc(num * sizeof(int));
2512 memcpy(s->write_msgfds, fds, num * sizeof(int));
2513 }
2514
2515 s->write_msgfds_num = num;
2516
2517 return 0;
2518}
2519
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002520#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002521static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2522{
2523 TCPCharDriver *s = chr->opaque;
2524 struct cmsghdr *cmsg;
2525
2526 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002527 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002528
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002529 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002530 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002531 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002532 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002533 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002534
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002535 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2536
2537 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002538 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002539 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002540
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002541 /* close and clean read_msgfds */
2542 for (i = 0; i < s->read_msgfds_num; i++) {
2543 close(s->read_msgfds[i]);
2544 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002545
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002546 if (s->read_msgfds_num) {
2547 g_free(s->read_msgfds);
2548 }
2549
2550 s->read_msgfds_num = fd_size / sizeof(int);
2551 s->read_msgfds = g_malloc(fd_size);
2552 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2553
2554 for (i = 0; i < s->read_msgfds_num; i++) {
2555 int fd = s->read_msgfds[i];
2556 if (fd < 0) {
2557 continue;
2558 }
2559
2560 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2561 qemu_set_block(fd);
2562
2563 #ifndef MSG_CMSG_CLOEXEC
2564 qemu_set_cloexec(fd);
2565 #endif
2566 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002567 }
2568}
2569
Mark McLoughlin9977c892009-07-22 09:11:38 +01002570static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2571{
2572 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002573 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002574 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002575 union {
2576 struct cmsghdr cmsg;
2577 char control[CMSG_SPACE(sizeof(int))];
2578 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002579 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002580 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002581
2582 iov[0].iov_base = buf;
2583 iov[0].iov_len = len;
2584
2585 msg.msg_iov = iov;
2586 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002587 msg.msg_control = &msg_control;
2588 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002589
Corey Bryant06138652012-08-14 16:43:42 -04002590#ifdef MSG_CMSG_CLOEXEC
2591 flags |= MSG_CMSG_CLOEXEC;
2592#endif
2593 ret = recvmsg(s->fd, &msg, flags);
2594 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002595 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002596 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002597
2598 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002599}
2600#else
2601static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2602{
2603 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002604 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002605}
2606#endif
2607
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302608static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2609{
2610 TCPCharDriver *s = chr->opaque;
2611 return g_io_create_watch(s->chan, cond);
2612}
2613
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002614static void tcp_chr_disconnect(CharDriverState *chr)
2615{
2616 TCPCharDriver *s = chr->opaque;
2617
2618 s->connected = 0;
2619 if (s->listen_chan) {
2620 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2621 tcp_chr_accept, chr);
2622 }
2623 remove_fd_in_watch(chr);
2624 g_io_channel_unref(s->chan);
2625 s->chan = NULL;
2626 closesocket(s->fd);
2627 s->fd = -1;
2628 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2629}
2630
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302631static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002632{
2633 CharDriverState *chr = opaque;
2634 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302635 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002636 int len, size;
2637
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302638 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002639 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302640 }
aliguori6f97dba2008-10-31 18:49:55 +00002641 len = sizeof(buf);
2642 if (len > s->max_size)
2643 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002644 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002645 if (size == 0) {
2646 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002647 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002648 } else if (size > 0) {
2649 if (s->do_telnetopt)
2650 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2651 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002652 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002653 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302654
2655 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002656}
2657
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002658static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2659{
2660 TCPCharDriver *s = chr->opaque;
2661 int size;
2662
2663 if (!s->connected) {
2664 return 0;
2665 }
2666
2667 size = tcp_chr_recv(chr, (void *) buf, len);
2668 if (size == 0) {
2669 /* connection closed */
2670 tcp_chr_disconnect(chr);
2671 }
2672
2673 return size;
2674}
2675
Blue Swirl68c18d12010-08-15 09:46:24 +00002676#ifndef _WIN32
2677CharDriverState *qemu_chr_open_eventfd(int eventfd)
2678{
David Marchande9d21c42014-06-11 17:25:16 +02002679 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2680
2681 if (chr) {
2682 chr->avail_connections = 1;
2683 }
2684
2685 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002686}
Blue Swirl68c18d12010-08-15 09:46:24 +00002687#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002688
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002689static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2690 void *opaque)
2691{
2692 CharDriverState *chr = opaque;
2693
2694 if (cond != G_IO_HUP) {
2695 return FALSE;
2696 }
2697
2698 /* connection closed */
2699 tcp_chr_disconnect(chr);
2700 if (chr->fd_hup_tag) {
2701 g_source_remove(chr->fd_hup_tag);
2702 chr->fd_hup_tag = 0;
2703 }
2704
2705 return TRUE;
2706}
2707
aliguori6f97dba2008-10-31 18:49:55 +00002708static void tcp_chr_connect(void *opaque)
2709{
2710 CharDriverState *chr = opaque;
2711 TCPCharDriver *s = chr->opaque;
2712
2713 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302714 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302715 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2716 tcp_chr_read, chr);
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002717 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2718 chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002719 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002720 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002721}
2722
Gal Hammerac1b84d2014-02-25 12:12:35 +02002723static void tcp_chr_update_read_handler(CharDriverState *chr)
2724{
2725 TCPCharDriver *s = chr->opaque;
2726
2727 remove_fd_in_watch(chr);
2728 if (s->chan) {
2729 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2730 tcp_chr_read, chr);
2731 }
2732}
2733
aliguori6f97dba2008-10-31 18:49:55 +00002734#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2735static void tcp_chr_telnet_init(int fd)
2736{
2737 char buf[3];
2738 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2739 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2740 send(fd, (char *)buf, 3, 0);
2741 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2742 send(fd, (char *)buf, 3, 0);
2743 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2744 send(fd, (char *)buf, 3, 0);
2745 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2746 send(fd, (char *)buf, 3, 0);
2747}
2748
Daniel P. Berrange13661082011-06-23 13:31:42 +01002749static int tcp_chr_add_client(CharDriverState *chr, int fd)
2750{
2751 TCPCharDriver *s = chr->opaque;
2752 if (s->fd != -1)
2753 return -1;
2754
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002755 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002756 if (s->do_nodelay)
2757 socket_set_nodelay(fd);
2758 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302759 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002760 if (s->listen_tag) {
2761 g_source_remove(s->listen_tag);
2762 s->listen_tag = 0;
2763 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002764 tcp_chr_connect(chr);
2765
2766 return 0;
2767}
2768
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302769static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002770{
2771 CharDriverState *chr = opaque;
2772 TCPCharDriver *s = chr->opaque;
2773 struct sockaddr_in saddr;
2774#ifndef _WIN32
2775 struct sockaddr_un uaddr;
2776#endif
2777 struct sockaddr *addr;
2778 socklen_t len;
2779 int fd;
2780
2781 for(;;) {
2782#ifndef _WIN32
2783 if (s->is_unix) {
2784 len = sizeof(uaddr);
2785 addr = (struct sockaddr *)&uaddr;
2786 } else
2787#endif
2788 {
2789 len = sizeof(saddr);
2790 addr = (struct sockaddr *)&saddr;
2791 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002792 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002793 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002794 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302795 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002796 } else if (fd >= 0) {
2797 if (s->do_telnetopt)
2798 tcp_chr_telnet_init(fd);
2799 break;
2800 }
2801 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002802 if (tcp_chr_add_client(chr, fd) < 0)
2803 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302804
2805 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002806}
2807
2808static void tcp_chr_close(CharDriverState *chr)
2809{
2810 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002811 int i;
aliguori819f56b2009-03-28 17:58:14 +00002812 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302813 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302814 if (s->chan) {
2815 g_io_channel_unref(s->chan);
2816 }
aliguori6f97dba2008-10-31 18:49:55 +00002817 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002818 }
2819 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302820 if (s->listen_tag) {
2821 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002822 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302823 }
2824 if (s->listen_chan) {
2825 g_io_channel_unref(s->listen_chan);
2826 }
aliguori6f97dba2008-10-31 18:49:55 +00002827 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002828 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002829 if (s->read_msgfds_num) {
2830 for (i = 0; i < s->read_msgfds_num; i++) {
2831 close(s->read_msgfds[i]);
2832 }
2833 g_free(s->read_msgfds);
2834 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002835 if (s->write_msgfds_num) {
2836 g_free(s->write_msgfds);
2837 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002838 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002839 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002840}
2841
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002842static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2843 bool is_listen, bool is_telnet,
2844 bool is_waitconnect,
2845 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002846{
2847 CharDriverState *chr = NULL;
2848 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002849 char host[NI_MAXHOST], serv[NI_MAXSERV];
2850 const char *left = "", *right = "";
2851 struct sockaddr_storage ss;
2852 socklen_t ss_len = sizeof(ss);
2853
2854 memset(&ss, 0, ss_len);
2855 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002856 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002857 return NULL;
2858 }
2859
2860 chr = g_malloc0(sizeof(CharDriverState));
2861 s = g_malloc0(sizeof(TCPCharDriver));
2862
2863 s->connected = 0;
2864 s->fd = -1;
2865 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002866 s->read_msgfds = 0;
2867 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002868 s->write_msgfds = 0;
2869 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002870
2871 chr->filename = g_malloc(256);
2872 switch (ss.ss_family) {
2873#ifndef _WIN32
2874 case AF_UNIX:
2875 s->is_unix = 1;
2876 snprintf(chr->filename, 256, "unix:%s%s",
2877 ((struct sockaddr_un *)(&ss))->sun_path,
2878 is_listen ? ",server" : "");
2879 break;
2880#endif
2881 case AF_INET6:
2882 left = "[";
2883 right = "]";
2884 /* fall through */
2885 case AF_INET:
2886 s->do_nodelay = do_nodelay;
2887 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2888 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002889 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002890 is_telnet ? "telnet" : "tcp",
2891 left, host, right, serv,
2892 is_listen ? ",server" : "");
2893 break;
2894 }
2895
2896 chr->opaque = s;
2897 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002898 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002899 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002900 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002901 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002902 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302903 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002904 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002905 /* be isn't opened until we get a connection */
2906 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002907
2908 if (is_listen) {
2909 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302910 s->listen_chan = io_channel_from_socket(s->listen_fd);
2911 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002912 if (is_telnet) {
2913 s->do_telnetopt = 1;
2914 }
2915 } else {
2916 s->connected = 1;
2917 s->fd = fd;
2918 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302919 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002920 tcp_chr_connect(chr);
2921 }
2922
2923 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002924 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2925 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302926 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002927 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002928 }
2929 return chr;
2930}
2931
2932static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2933{
2934 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002935 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002936 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002937
liguange990a392013-06-18 11:45:35 +08002938 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2939 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2940 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2941 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2942 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002943
aliguorif07b6002008-11-11 20:54:09 +00002944 if (is_unix) {
2945 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002946 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002947 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002948 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002949 }
2950 } else {
2951 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002952 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002953 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002954 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002955 }
2956 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002957 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002958 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002959 }
aliguori6f97dba2008-10-31 18:49:55 +00002960
2961 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002962 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002963
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002964 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2965 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002966 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002967 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002968 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002969 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002970
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002971
aliguori6f97dba2008-10-31 18:49:55 +00002972 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002973 if (local_err) {
2974 qerror_report_err(local_err);
2975 error_free(local_err);
2976 }
2977 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002978 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002979 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002980 if (chr) {
2981 g_free(chr->opaque);
2982 g_free(chr);
2983 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002984 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002985}
2986
Lei Li51767e72013-01-25 00:03:19 +08002987/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002988/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002989
2990typedef struct {
2991 size_t size;
2992 size_t prod;
2993 size_t cons;
2994 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002995} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002996
Markus Armbruster3949e592013-02-06 21:27:24 +01002997static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002998{
Markus Armbruster3949e592013-02-06 21:27:24 +01002999 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003000
Markus Armbruster5c230102013-02-06 21:27:23 +01003001 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003002}
3003
Markus Armbruster3949e592013-02-06 21:27:24 +01003004static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003005{
Markus Armbruster3949e592013-02-06 21:27:24 +01003006 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003007 int i;
3008
3009 if (!buf || (len < 0)) {
3010 return -1;
3011 }
3012
3013 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003014 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3015 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003016 d->cons = d->prod - d->size;
3017 }
3018 }
3019
3020 return 0;
3021}
3022
Markus Armbruster3949e592013-02-06 21:27:24 +01003023static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003024{
Markus Armbruster3949e592013-02-06 21:27:24 +01003025 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003026 int i;
3027
Markus Armbruster5c230102013-02-06 21:27:23 +01003028 for (i = 0; i < len && d->cons != d->prod; i++) {
3029 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003030 }
3031
3032 return i;
3033}
3034
Markus Armbruster3949e592013-02-06 21:27:24 +01003035static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003036{
Markus Armbruster3949e592013-02-06 21:27:24 +01003037 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003038
3039 g_free(d->cbuf);
3040 g_free(d);
3041 chr->opaque = NULL;
3042}
3043
Markus Armbruster4f573782013-07-26 16:44:32 +02003044static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3045 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003046{
3047 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003048 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003049
3050 chr = g_malloc0(sizeof(CharDriverState));
3051 d = g_malloc(sizeof(*d));
3052
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003053 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003054
3055 /* The size must be power of 2 */
3056 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003057 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003058 goto fail;
3059 }
3060
3061 d->prod = 0;
3062 d->cons = 0;
3063 d->cbuf = g_malloc0(d->size);
3064
3065 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003066 chr->chr_write = ringbuf_chr_write;
3067 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003068
3069 return chr;
3070
3071fail:
3072 g_free(d);
3073 g_free(chr);
3074 return NULL;
3075}
3076
Hani Benhabiles8e597772014-05-27 23:39:30 +01003077bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003078{
Markus Armbruster3949e592013-02-06 21:27:24 +01003079 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003080}
3081
Markus Armbruster3949e592013-02-06 21:27:24 +01003082void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003083 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003084 Error **errp)
3085{
3086 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003087 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003088 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003089 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003090
3091 chr = qemu_chr_find(device);
3092 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003093 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003094 return;
3095 }
3096
Markus Armbruster3949e592013-02-06 21:27:24 +01003097 if (!chr_is_ringbuf(chr)) {
3098 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003099 return;
3100 }
3101
Lei Li1f590cf2013-01-25 00:03:20 +08003102 if (has_format && (format == DATA_FORMAT_BASE64)) {
3103 write_data = g_base64_decode(data, &write_count);
3104 } else {
3105 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003106 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003107 }
3108
Markus Armbruster3949e592013-02-06 21:27:24 +01003109 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003110
Markus Armbruster13289fb2013-02-06 21:27:18 +01003111 if (write_data != (uint8_t *)data) {
3112 g_free((void *)write_data);
3113 }
3114
Lei Li1f590cf2013-01-25 00:03:20 +08003115 if (ret < 0) {
3116 error_setg(errp, "Failed to write to device %s", device);
3117 return;
3118 }
3119}
3120
Markus Armbruster3949e592013-02-06 21:27:24 +01003121char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003122 bool has_format, enum DataFormat format,
3123 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003124{
3125 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003126 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003127 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003128 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003129
3130 chr = qemu_chr_find(device);
3131 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003132 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003133 return NULL;
3134 }
3135
Markus Armbruster3949e592013-02-06 21:27:24 +01003136 if (!chr_is_ringbuf(chr)) {
3137 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003138 return NULL;
3139 }
3140
3141 if (size <= 0) {
3142 error_setg(errp, "size must be greater than zero");
3143 return NULL;
3144 }
3145
Markus Armbruster3949e592013-02-06 21:27:24 +01003146 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003147 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003148 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003149
Markus Armbruster3949e592013-02-06 21:27:24 +01003150 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003151
3152 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003153 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003154 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003155 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003156 /*
3157 * FIXME should read only complete, valid UTF-8 characters up
3158 * to @size bytes. Invalid sequences should be replaced by a
3159 * suitable replacement character. Except when (and only
3160 * when) ring buffer lost characters since last read, initial
3161 * continuation characters should be dropped.
3162 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003163 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003164 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003165 }
3166
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003167 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003168}
3169
Gerd Hoffmann33521632009-12-08 13:11:49 +01003170QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003171{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003172 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003173 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003174 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003175 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003176 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003177
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003178 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003179 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003180 qerror_report_err(local_err);
3181 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003182 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003183 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003184
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003185 if (strstart(filename, "mon:", &p)) {
3186 filename = p;
3187 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003188 if (strcmp(filename, "stdio") == 0) {
3189 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3190 * but pass it to the guest. Handle this only for compat syntax,
3191 * for -chardev syntax we have special option for this.
3192 * This is what -nographic did, redirecting+muxing serial+monitor
3193 * to stdio causing Ctrl+C to be passed to guest. */
3194 qemu_opt_set(opts, "signal", "off");
3195 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003196 }
3197
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003198 if (strcmp(filename, "null") == 0 ||
3199 strcmp(filename, "pty") == 0 ||
3200 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003201 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003202 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003203 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003204 return opts;
3205 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003206 if (strstart(filename, "vc", &p)) {
3207 qemu_opt_set(opts, "backend", "vc");
3208 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003209 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003210 /* pixels */
3211 qemu_opt_set(opts, "width", width);
3212 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003213 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003214 /* chars */
3215 qemu_opt_set(opts, "cols", width);
3216 qemu_opt_set(opts, "rows", height);
3217 } else {
3218 goto fail;
3219 }
3220 }
3221 return opts;
3222 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003223 if (strcmp(filename, "con:") == 0) {
3224 qemu_opt_set(opts, "backend", "console");
3225 return opts;
3226 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003227 if (strstart(filename, "COM", NULL)) {
3228 qemu_opt_set(opts, "backend", "serial");
3229 qemu_opt_set(opts, "path", filename);
3230 return opts;
3231 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003232 if (strstart(filename, "file:", &p)) {
3233 qemu_opt_set(opts, "backend", "file");
3234 qemu_opt_set(opts, "path", p);
3235 return opts;
3236 }
3237 if (strstart(filename, "pipe:", &p)) {
3238 qemu_opt_set(opts, "backend", "pipe");
3239 qemu_opt_set(opts, "path", p);
3240 return opts;
3241 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003242 if (strstart(filename, "tcp:", &p) ||
3243 strstart(filename, "telnet:", &p)) {
3244 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3245 host[0] = 0;
3246 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3247 goto fail;
3248 }
3249 qemu_opt_set(opts, "backend", "socket");
3250 qemu_opt_set(opts, "host", host);
3251 qemu_opt_set(opts, "port", port);
3252 if (p[pos] == ',') {
3253 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3254 goto fail;
3255 }
3256 if (strstart(filename, "telnet:", &p))
3257 qemu_opt_set(opts, "telnet", "on");
3258 return opts;
3259 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003260 if (strstart(filename, "udp:", &p)) {
3261 qemu_opt_set(opts, "backend", "udp");
3262 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3263 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003264 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003265 goto fail;
3266 }
3267 }
3268 qemu_opt_set(opts, "host", host);
3269 qemu_opt_set(opts, "port", port);
3270 if (p[pos] == '@') {
3271 p += pos + 1;
3272 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3273 host[0] = 0;
3274 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003275 goto fail;
3276 }
3277 }
3278 qemu_opt_set(opts, "localaddr", host);
3279 qemu_opt_set(opts, "localport", port);
3280 }
3281 return opts;
3282 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003283 if (strstart(filename, "unix:", &p)) {
3284 qemu_opt_set(opts, "backend", "socket");
3285 if (qemu_opts_do_parse(opts, p, "path") != 0)
3286 goto fail;
3287 return opts;
3288 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003289 if (strstart(filename, "/dev/parport", NULL) ||
3290 strstart(filename, "/dev/ppi", NULL)) {
3291 qemu_opt_set(opts, "backend", "parport");
3292 qemu_opt_set(opts, "path", filename);
3293 return opts;
3294 }
3295 if (strstart(filename, "/dev/", NULL)) {
3296 qemu_opt_set(opts, "backend", "tty");
3297 qemu_opt_set(opts, "path", filename);
3298 return opts;
3299 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003300
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003301fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003302 qemu_opts_del(opts);
3303 return NULL;
3304}
3305
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003306static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3307 Error **errp)
3308{
3309 const char *path = qemu_opt_get(opts, "path");
3310
3311 if (path == NULL) {
3312 error_setg(errp, "chardev: file: no filename given");
3313 return;
3314 }
3315 backend->file = g_new0(ChardevFile, 1);
3316 backend->file->out = g_strdup(path);
3317}
3318
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003319static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3320 Error **errp)
3321{
3322 backend->stdio = g_new0(ChardevStdio, 1);
3323 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003324 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003325}
3326
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003327static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3328 Error **errp)
3329{
3330 const char *device = qemu_opt_get(opts, "path");
3331
3332 if (device == NULL) {
3333 error_setg(errp, "chardev: serial/tty: no device path given");
3334 return;
3335 }
3336 backend->serial = g_new0(ChardevHostdev, 1);
3337 backend->serial->device = g_strdup(device);
3338}
3339
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003340static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3341 Error **errp)
3342{
3343 const char *device = qemu_opt_get(opts, "path");
3344
3345 if (device == NULL) {
3346 error_setg(errp, "chardev: parallel: no device path given");
3347 return;
3348 }
3349 backend->parallel = g_new0(ChardevHostdev, 1);
3350 backend->parallel->device = g_strdup(device);
3351}
3352
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003353static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3354 Error **errp)
3355{
3356 const char *device = qemu_opt_get(opts, "path");
3357
3358 if (device == NULL) {
3359 error_setg(errp, "chardev: pipe: no device path given");
3360 return;
3361 }
3362 backend->pipe = g_new0(ChardevHostdev, 1);
3363 backend->pipe->device = g_strdup(device);
3364}
3365
Markus Armbruster4f573782013-07-26 16:44:32 +02003366static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3367 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003368{
3369 int val;
3370
Markus Armbruster3a1da422013-07-26 16:44:34 +02003371 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003372
Markus Armbruster0f953052013-06-27 16:22:07 +02003373 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003374 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003375 backend->ringbuf->has_size = true;
3376 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003377 }
3378}
3379
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003380static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3381 Error **errp)
3382{
3383 const char *chardev = qemu_opt_get(opts, "chardev");
3384
3385 if (chardev == NULL) {
3386 error_setg(errp, "chardev: mux: no chardev given");
3387 return;
3388 }
3389 backend->mux = g_new0(ChardevMux, 1);
3390 backend->mux->chardev = g_strdup(chardev);
3391}
3392
Anthony Liguorid654f342013-03-05 23:21:28 +05303393typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003394 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003395 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003396 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003397 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003398 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003399 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303400} CharDriver;
3401
3402static GSList *backends;
3403
3404void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3405{
3406 CharDriver *s;
3407
3408 s = g_malloc0(sizeof(*s));
3409 s->name = g_strdup(name);
3410 s->open = open;
3411
3412 backends = g_slist_append(backends, s);
3413}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003414
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003415void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003416 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3417{
3418 CharDriver *s;
3419
3420 s = g_malloc0(sizeof(*s));
3421 s->name = g_strdup(name);
3422 s->kind = kind;
3423 s->parse = parse;
3424
3425 backends = g_slist_append(backends, s);
3426}
3427
Anthony Liguorif69554b2011-08-15 11:17:37 -05003428CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003429 void (*init)(struct CharDriverState *s),
3430 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003431{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003432 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303433 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003434 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303435 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003436
3437 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003438 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003439 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003440 }
3441
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003442 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003443 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003444 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003445 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003446 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303447 for (i = backends; i; i = i->next) {
3448 cd = i->data;
3449
3450 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003451 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303452 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003453 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303454 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003455 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003456 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003457 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003458 }
3459
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003460 if (!cd->open) {
3461 /* using new, qapi init */
3462 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3463 ChardevReturn *ret = NULL;
3464 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003465 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003466
3467 if (qemu_opt_get_bool(opts, "mux", 0)) {
3468 bid = g_strdup_printf("%s-base", id);
3469 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003470
3471 chr = NULL;
3472 backend->kind = cd->kind;
3473 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003474 cd->parse(opts, backend, &local_err);
3475 if (local_err) {
3476 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003477 goto qapi_out;
3478 }
3479 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003480 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003481 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003482 goto qapi_out;
3483 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003484
3485 if (bid) {
3486 qapi_free_ChardevBackend(backend);
3487 qapi_free_ChardevReturn(ret);
3488 backend = g_new0(ChardevBackend, 1);
3489 backend->mux = g_new0(ChardevMux, 1);
3490 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3491 backend->mux->chardev = g_strdup(bid);
3492 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003493 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003494 chr = qemu_chr_find(bid);
3495 qemu_chr_delete(chr);
3496 chr = NULL;
3497 goto qapi_out;
3498 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003499 }
3500
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003501 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003502 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003503
3504 qapi_out:
3505 qapi_free_ChardevBackend(backend);
3506 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003507 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003508 return chr;
3509 }
3510
Anthony Liguorid654f342013-03-05 23:21:28 +05303511 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003512 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003513 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003514 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003515 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003516 }
3517
3518 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003519 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003520 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003521 /* if we didn't create the chardev via qmp_chardev_add, we
3522 * need to send the OPENED event here
3523 */
3524 if (!chr->explicit_be_open) {
3525 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3526 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003527 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003528
3529 if (qemu_opt_get_bool(opts, "mux", 0)) {
3530 CharDriverState *base = chr;
3531 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003532 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003533 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3534 chr = qemu_chr_open_mux(base);
3535 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003536 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003537 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003538 } else {
3539 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003540 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003541 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003542 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003543 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003544
3545err:
3546 qemu_opts_del(opts);
3547 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003548}
3549
Anthony Liguori27143a42011-08-15 11:17:36 -05003550CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003551{
3552 const char *p;
3553 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003554 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003555 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003556
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003557 if (strstart(filename, "chardev:", &p)) {
3558 return qemu_chr_find(p);
3559 }
3560
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003561 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003562 if (!opts)
3563 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003564
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003565 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003566 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003567 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003568 error_free(err);
3569 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003570 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003571 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003572 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003573 }
3574 return chr;
3575}
3576
Anthony Liguori15f31512011-08-15 11:17:35 -05003577void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003578{
3579 if (chr->chr_set_echo) {
3580 chr->chr_set_echo(chr, echo);
3581 }
3582}
3583
Hans de Goede8e25daa2013-03-26 11:07:57 +01003584void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003585{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003586 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003587 return;
3588 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003589 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003590 if (chr->chr_set_fe_open) {
3591 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003592 }
3593}
3594
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003595void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3596{
3597 if (chr->chr_fe_event) {
3598 chr->chr_fe_event(chr, event);
3599 }
3600}
3601
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003602int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3603 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303604{
3605 GSource *src;
3606 guint tag;
3607
3608 if (s->chr_add_watch == NULL) {
3609 return -ENOSYS;
3610 }
3611
3612 src = s->chr_add_watch(s, cond);
3613 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3614 tag = g_source_attach(src, NULL);
3615 g_source_unref(src);
3616
3617 return tag;
3618}
3619
Hans de Goede44c473d2013-03-27 20:29:39 +01003620int qemu_chr_fe_claim(CharDriverState *s)
3621{
3622 if (s->avail_connections < 1) {
3623 return -1;
3624 }
3625 s->avail_connections--;
3626 return 0;
3627}
3628
3629void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3630{
3631 if (qemu_chr_fe_claim(s) != 0) {
3632 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3633 __func__, s->label);
3634 exit(1);
3635 }
3636}
3637
3638void qemu_chr_fe_release(CharDriverState *s)
3639{
3640 s->avail_connections++;
3641}
3642
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003643void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003644{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003645 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003646 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003647 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003648 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003649 g_free(chr->filename);
3650 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003651 if (chr->opts) {
3652 qemu_opts_del(chr->opts);
3653 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003654 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003655}
3656
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003657ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003658{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003659 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003660 CharDriverState *chr;
3661
Blue Swirl72cf2d42009-09-12 07:36:22 +00003662 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003663 ChardevInfoList *info = g_malloc0(sizeof(*info));
3664 info->value = g_malloc0(sizeof(*info->value));
3665 info->value->label = g_strdup(chr->label);
3666 info->value->filename = g_strdup(chr->filename);
3667
3668 info->next = chr_list;
3669 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003670 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003671
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003672 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003673}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003674
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003675ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3676{
3677 ChardevBackendInfoList *backend_list = NULL;
3678 CharDriver *c = NULL;
3679 GSList *i = NULL;
3680
3681 for (i = backends; i; i = i->next) {
3682 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3683 c = i->data;
3684 info->value = g_malloc0(sizeof(*info->value));
3685 info->value->name = g_strdup(c->name);
3686
3687 info->next = backend_list;
3688 backend_list = info;
3689 }
3690
3691 return backend_list;
3692}
3693
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003694CharDriverState *qemu_chr_find(const char *name)
3695{
3696 CharDriverState *chr;
3697
Blue Swirl72cf2d42009-09-12 07:36:22 +00003698 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003699 if (strcmp(chr->label, name) != 0)
3700 continue;
3701 return chr;
3702 }
3703 return NULL;
3704}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003705
3706/* Get a character (serial) device interface. */
3707CharDriverState *qemu_char_get_next_serial(void)
3708{
3709 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003710 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003711
3712 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003713
3714 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3715 chr = serial_hds[next_serial++];
3716 qemu_chr_fe_claim_no_fail(chr);
3717 return chr;
3718 }
3719 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003720}
3721
Paolo Bonzini4d454572012-11-26 16:03:42 +01003722QemuOptsList qemu_chardev_opts = {
3723 .name = "chardev",
3724 .implied_opt_name = "backend",
3725 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3726 .desc = {
3727 {
3728 .name = "backend",
3729 .type = QEMU_OPT_STRING,
3730 },{
3731 .name = "path",
3732 .type = QEMU_OPT_STRING,
3733 },{
3734 .name = "host",
3735 .type = QEMU_OPT_STRING,
3736 },{
3737 .name = "port",
3738 .type = QEMU_OPT_STRING,
3739 },{
3740 .name = "localaddr",
3741 .type = QEMU_OPT_STRING,
3742 },{
3743 .name = "localport",
3744 .type = QEMU_OPT_STRING,
3745 },{
3746 .name = "to",
3747 .type = QEMU_OPT_NUMBER,
3748 },{
3749 .name = "ipv4",
3750 .type = QEMU_OPT_BOOL,
3751 },{
3752 .name = "ipv6",
3753 .type = QEMU_OPT_BOOL,
3754 },{
3755 .name = "wait",
3756 .type = QEMU_OPT_BOOL,
3757 },{
3758 .name = "server",
3759 .type = QEMU_OPT_BOOL,
3760 },{
3761 .name = "delay",
3762 .type = QEMU_OPT_BOOL,
3763 },{
3764 .name = "telnet",
3765 .type = QEMU_OPT_BOOL,
3766 },{
3767 .name = "width",
3768 .type = QEMU_OPT_NUMBER,
3769 },{
3770 .name = "height",
3771 .type = QEMU_OPT_NUMBER,
3772 },{
3773 .name = "cols",
3774 .type = QEMU_OPT_NUMBER,
3775 },{
3776 .name = "rows",
3777 .type = QEMU_OPT_NUMBER,
3778 },{
3779 .name = "mux",
3780 .type = QEMU_OPT_BOOL,
3781 },{
3782 .name = "signal",
3783 .type = QEMU_OPT_BOOL,
3784 },{
3785 .name = "name",
3786 .type = QEMU_OPT_STRING,
3787 },{
3788 .name = "debug",
3789 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003790 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003791 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003792 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003793 },{
3794 .name = "chardev",
3795 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003796 },
3797 { /* end of list */ }
3798 },
3799};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003800
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003801#ifdef _WIN32
3802
3803static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3804{
3805 HANDLE out;
3806
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003807 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003808 error_setg(errp, "input file not supported");
3809 return NULL;
3810 }
3811
3812 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3813 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3814 if (out == INVALID_HANDLE_VALUE) {
3815 error_setg(errp, "open %s failed", file->out);
3816 return NULL;
3817 }
3818 return qemu_chr_open_win_file(out);
3819}
3820
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003821static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3822 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003823{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003824 return qemu_chr_open_win_path(serial->device);
3825}
3826
3827static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3828 Error **errp)
3829{
3830 error_setg(errp, "character device backend type 'parallel' not supported");
3831 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003832}
3833
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003834#else /* WIN32 */
3835
3836static int qmp_chardev_open_file_source(char *src, int flags,
3837 Error **errp)
3838{
3839 int fd = -1;
3840
3841 TFR(fd = qemu_open(src, flags, 0666));
3842 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003843 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003844 }
3845 return fd;
3846}
3847
3848static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3849{
Markus Armbruster5f758362014-05-19 18:57:34 +02003850 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003851
3852 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3853 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003854 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003855 return NULL;
3856 }
3857
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003858 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003859 flags = O_RDONLY;
3860 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003861 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003862 qemu_close(out);
3863 return NULL;
3864 }
3865 }
3866
3867 return qemu_chr_open_fd(in, out);
3868}
3869
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003870static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3871 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003872{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003873#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003874 int fd;
3875
3876 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003877 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003878 return NULL;
3879 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003880 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003881 return qemu_chr_open_tty_fd(fd);
3882#else
3883 error_setg(errp, "character device backend type 'serial' not supported");
3884 return NULL;
3885#endif
3886}
3887
3888static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3889 Error **errp)
3890{
3891#ifdef HAVE_CHARDEV_PARPORT
3892 int fd;
3893
3894 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003895 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003896 return NULL;
3897 }
3898 return qemu_chr_open_pp_fd(fd);
3899#else
3900 error_setg(errp, "character device backend type 'parallel' not supported");
3901 return NULL;
3902#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003903}
3904
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003905#endif /* WIN32 */
3906
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003907static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3908 Error **errp)
3909{
3910 SocketAddress *addr = sock->addr;
3911 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3912 bool is_listen = sock->has_server ? sock->server : true;
3913 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3914 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3915 int fd;
3916
3917 if (is_listen) {
3918 fd = socket_listen(addr, errp);
3919 } else {
3920 fd = socket_connect(addr, errp, NULL, NULL);
3921 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003922 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003923 return NULL;
3924 }
3925 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3926 is_telnet, is_waitconnect, errp);
3927}
3928
Lei Li08d0ab32013-05-20 14:51:03 +08003929static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3930 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003931{
3932 int fd;
3933
Lei Li08d0ab32013-05-20 14:51:03 +08003934 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003935 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003936 return NULL;
3937 }
3938 return qemu_chr_open_udp_fd(fd);
3939}
3940
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003941ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3942 Error **errp)
3943{
3944 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003945 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003946
3947 chr = qemu_chr_find(id);
3948 if (chr) {
3949 error_setg(errp, "Chardev '%s' already exists", id);
3950 g_free(ret);
3951 return NULL;
3952 }
3953
3954 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003955 case CHARDEV_BACKEND_KIND_FILE:
3956 chr = qmp_chardev_open_file(backend->file, errp);
3957 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003958 case CHARDEV_BACKEND_KIND_SERIAL:
3959 chr = qmp_chardev_open_serial(backend->serial, errp);
3960 break;
3961 case CHARDEV_BACKEND_KIND_PARALLEL:
3962 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003963 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003964 case CHARDEV_BACKEND_KIND_PIPE:
3965 chr = qemu_chr_open_pipe(backend->pipe);
3966 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003967 case CHARDEV_BACKEND_KIND_SOCKET:
3968 chr = qmp_chardev_open_socket(backend->socket, errp);
3969 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003970 case CHARDEV_BACKEND_KIND_UDP:
3971 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003972 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003973#ifdef HAVE_CHARDEV_TTY
3974 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003975 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003976 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003977#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003978 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003979 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003980 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003981 case CHARDEV_BACKEND_KIND_MUX:
3982 base = qemu_chr_find(backend->mux->chardev);
3983 if (base == NULL) {
3984 error_setg(errp, "mux: base chardev %s not found",
3985 backend->mux->chardev);
3986 break;
3987 }
3988 chr = qemu_chr_open_mux(base);
3989 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003990 case CHARDEV_BACKEND_KIND_MSMOUSE:
3991 chr = qemu_chr_open_msmouse();
3992 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003993#ifdef CONFIG_BRLAPI
3994 case CHARDEV_BACKEND_KIND_BRAILLE:
3995 chr = chr_baum_init();
3996 break;
3997#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003998 case CHARDEV_BACKEND_KIND_STDIO:
3999 chr = qemu_chr_open_stdio(backend->stdio);
4000 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004001#ifdef _WIN32
4002 case CHARDEV_BACKEND_KIND_CONSOLE:
4003 chr = qemu_chr_open_win_con();
4004 break;
4005#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004006#ifdef CONFIG_SPICE
4007 case CHARDEV_BACKEND_KIND_SPICEVMC:
4008 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4009 break;
4010 case CHARDEV_BACKEND_KIND_SPICEPORT:
4011 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4012 break;
4013#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004014 case CHARDEV_BACKEND_KIND_VC:
4015 chr = vc_init(backend->vc);
4016 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004017 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004018 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004019 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004020 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004021 default:
4022 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4023 break;
4024 }
4025
Markus Armbruster3894c782014-05-19 18:57:36 +02004026 /*
4027 * Character backend open hasn't been fully converted to the Error
4028 * API. Some opens fail without setting an error. Set a generic
4029 * error then.
4030 * TODO full conversion to Error API
4031 */
4032 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004033 error_setg(errp, "Failed to create chardev");
4034 }
4035 if (chr) {
4036 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004037 chr->avail_connections =
4038 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004039 if (!chr->filename) {
4040 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4041 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004042 if (!chr->explicit_be_open) {
4043 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4044 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004045 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4046 return ret;
4047 } else {
4048 g_free(ret);
4049 return NULL;
4050 }
4051}
4052
4053void qmp_chardev_remove(const char *id, Error **errp)
4054{
4055 CharDriverState *chr;
4056
4057 chr = qemu_chr_find(id);
4058 if (NULL == chr) {
4059 error_setg(errp, "Chardev '%s' not found", id);
4060 return;
4061 }
4062 if (chr->chr_can_read || chr->chr_read ||
4063 chr->chr_event || chr->handler_opaque) {
4064 error_setg(errp, "Chardev '%s' is busy", id);
4065 return;
4066 }
4067 qemu_chr_delete(chr);
4068}
Anthony Liguorid654f342013-03-05 23:21:28 +05304069
4070static void register_types(void)
4071{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004072 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304073 register_char_driver("socket", qemu_chr_open_socket);
4074 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004075 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004076 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004077 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4078 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004079 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4080 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004081 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4082 qemu_chr_parse_serial);
4083 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4084 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004085 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4086 qemu_chr_parse_parallel);
4087 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4088 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004089 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004090 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004091 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4092 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004093 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4094 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004095 /* Bug-compatibility: */
4096 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4097 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004098 /* this must be done after machine init, since we register FEs with muxes
4099 * as part of realize functions like serial_isa_realizefn when -nographic
4100 * is specified
4101 */
4102 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304103}
4104
4105type_init(register_types);