blob: e4eb985b5746270ba65fc63308e47335cab002bf [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;
207 return (qemu_chr_fe_get_msgfds(s, &fd, 1) >= 0) ? fd : -1;
208}
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) {
2484 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2485
2486 g_free(s->read_msgfds);
2487 s->read_msgfds = 0;
2488 s->read_msgfds_num = 0;
2489 }
2490
2491 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002492}
2493
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002494static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2495{
2496 TCPCharDriver *s = chr->opaque;
2497
2498 /* clear old pending fd array */
2499 if (s->write_msgfds) {
2500 g_free(s->write_msgfds);
2501 }
2502
2503 if (num) {
2504 s->write_msgfds = g_malloc(num * sizeof(int));
2505 memcpy(s->write_msgfds, fds, num * sizeof(int));
2506 }
2507
2508 s->write_msgfds_num = num;
2509
2510 return 0;
2511}
2512
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002513#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002514static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2515{
2516 TCPCharDriver *s = chr->opaque;
2517 struct cmsghdr *cmsg;
2518
2519 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002520 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002521
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002522 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002523 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002524 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002525 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002526 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002527
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002528 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2529
2530 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002531 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002532 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002533
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002534 /* close and clean read_msgfds */
2535 for (i = 0; i < s->read_msgfds_num; i++) {
2536 close(s->read_msgfds[i]);
2537 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002538
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002539 if (s->read_msgfds_num) {
2540 g_free(s->read_msgfds);
2541 }
2542
2543 s->read_msgfds_num = fd_size / sizeof(int);
2544 s->read_msgfds = g_malloc(fd_size);
2545 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2546
2547 for (i = 0; i < s->read_msgfds_num; i++) {
2548 int fd = s->read_msgfds[i];
2549 if (fd < 0) {
2550 continue;
2551 }
2552
2553 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2554 qemu_set_block(fd);
2555
2556 #ifndef MSG_CMSG_CLOEXEC
2557 qemu_set_cloexec(fd);
2558 #endif
2559 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002560 }
2561}
2562
Mark McLoughlin9977c892009-07-22 09:11:38 +01002563static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2564{
2565 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002566 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002567 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002568 union {
2569 struct cmsghdr cmsg;
2570 char control[CMSG_SPACE(sizeof(int))];
2571 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002572 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002573 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002574
2575 iov[0].iov_base = buf;
2576 iov[0].iov_len = len;
2577
2578 msg.msg_iov = iov;
2579 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002580 msg.msg_control = &msg_control;
2581 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002582
Corey Bryant06138652012-08-14 16:43:42 -04002583#ifdef MSG_CMSG_CLOEXEC
2584 flags |= MSG_CMSG_CLOEXEC;
2585#endif
2586 ret = recvmsg(s->fd, &msg, flags);
2587 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002588 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002589 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002590
2591 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002592}
2593#else
2594static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2595{
2596 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002597 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002598}
2599#endif
2600
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302601static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2602{
2603 TCPCharDriver *s = chr->opaque;
2604 return g_io_create_watch(s->chan, cond);
2605}
2606
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002607static void tcp_chr_disconnect(CharDriverState *chr)
2608{
2609 TCPCharDriver *s = chr->opaque;
2610
2611 s->connected = 0;
2612 if (s->listen_chan) {
2613 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2614 tcp_chr_accept, chr);
2615 }
2616 remove_fd_in_watch(chr);
2617 g_io_channel_unref(s->chan);
2618 s->chan = NULL;
2619 closesocket(s->fd);
2620 s->fd = -1;
2621 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2622}
2623
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302624static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002625{
2626 CharDriverState *chr = opaque;
2627 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302628 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002629 int len, size;
2630
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302631 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002632 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302633 }
aliguori6f97dba2008-10-31 18:49:55 +00002634 len = sizeof(buf);
2635 if (len > s->max_size)
2636 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002637 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002638 if (size == 0) {
2639 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002640 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002641 } else if (size > 0) {
2642 if (s->do_telnetopt)
2643 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2644 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002645 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002646 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302647
2648 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002649}
2650
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002651static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2652{
2653 TCPCharDriver *s = chr->opaque;
2654 int size;
2655
2656 if (!s->connected) {
2657 return 0;
2658 }
2659
2660 size = tcp_chr_recv(chr, (void *) buf, len);
2661 if (size == 0) {
2662 /* connection closed */
2663 tcp_chr_disconnect(chr);
2664 }
2665
2666 return size;
2667}
2668
Blue Swirl68c18d12010-08-15 09:46:24 +00002669#ifndef _WIN32
2670CharDriverState *qemu_chr_open_eventfd(int eventfd)
2671{
David Marchande9d21c42014-06-11 17:25:16 +02002672 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2673
2674 if (chr) {
2675 chr->avail_connections = 1;
2676 }
2677
2678 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002679}
Blue Swirl68c18d12010-08-15 09:46:24 +00002680#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002681
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002682static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2683 void *opaque)
2684{
2685 CharDriverState *chr = opaque;
2686
2687 if (cond != G_IO_HUP) {
2688 return FALSE;
2689 }
2690
2691 /* connection closed */
2692 tcp_chr_disconnect(chr);
2693 if (chr->fd_hup_tag) {
2694 g_source_remove(chr->fd_hup_tag);
2695 chr->fd_hup_tag = 0;
2696 }
2697
2698 return TRUE;
2699}
2700
aliguori6f97dba2008-10-31 18:49:55 +00002701static void tcp_chr_connect(void *opaque)
2702{
2703 CharDriverState *chr = opaque;
2704 TCPCharDriver *s = chr->opaque;
2705
2706 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302707 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302708 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2709 tcp_chr_read, chr);
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002710 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2711 chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002712 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002713 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002714}
2715
Gal Hammerac1b84d2014-02-25 12:12:35 +02002716static void tcp_chr_update_read_handler(CharDriverState *chr)
2717{
2718 TCPCharDriver *s = chr->opaque;
2719
2720 remove_fd_in_watch(chr);
2721 if (s->chan) {
2722 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2723 tcp_chr_read, chr);
2724 }
2725}
2726
aliguori6f97dba2008-10-31 18:49:55 +00002727#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2728static void tcp_chr_telnet_init(int fd)
2729{
2730 char buf[3];
2731 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2732 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2733 send(fd, (char *)buf, 3, 0);
2734 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2735 send(fd, (char *)buf, 3, 0);
2736 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2737 send(fd, (char *)buf, 3, 0);
2738 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2739 send(fd, (char *)buf, 3, 0);
2740}
2741
Daniel P. Berrange13661082011-06-23 13:31:42 +01002742static int tcp_chr_add_client(CharDriverState *chr, int fd)
2743{
2744 TCPCharDriver *s = chr->opaque;
2745 if (s->fd != -1)
2746 return -1;
2747
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002748 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002749 if (s->do_nodelay)
2750 socket_set_nodelay(fd);
2751 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302752 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002753 if (s->listen_tag) {
2754 g_source_remove(s->listen_tag);
2755 s->listen_tag = 0;
2756 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002757 tcp_chr_connect(chr);
2758
2759 return 0;
2760}
2761
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302762static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002763{
2764 CharDriverState *chr = opaque;
2765 TCPCharDriver *s = chr->opaque;
2766 struct sockaddr_in saddr;
2767#ifndef _WIN32
2768 struct sockaddr_un uaddr;
2769#endif
2770 struct sockaddr *addr;
2771 socklen_t len;
2772 int fd;
2773
2774 for(;;) {
2775#ifndef _WIN32
2776 if (s->is_unix) {
2777 len = sizeof(uaddr);
2778 addr = (struct sockaddr *)&uaddr;
2779 } else
2780#endif
2781 {
2782 len = sizeof(saddr);
2783 addr = (struct sockaddr *)&saddr;
2784 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002785 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002786 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002787 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302788 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002789 } else if (fd >= 0) {
2790 if (s->do_telnetopt)
2791 tcp_chr_telnet_init(fd);
2792 break;
2793 }
2794 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002795 if (tcp_chr_add_client(chr, fd) < 0)
2796 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302797
2798 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002799}
2800
2801static void tcp_chr_close(CharDriverState *chr)
2802{
2803 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002804 int i;
aliguori819f56b2009-03-28 17:58:14 +00002805 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302806 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302807 if (s->chan) {
2808 g_io_channel_unref(s->chan);
2809 }
aliguori6f97dba2008-10-31 18:49:55 +00002810 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002811 }
2812 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302813 if (s->listen_tag) {
2814 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002815 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302816 }
2817 if (s->listen_chan) {
2818 g_io_channel_unref(s->listen_chan);
2819 }
aliguori6f97dba2008-10-31 18:49:55 +00002820 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002821 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002822 if (s->read_msgfds_num) {
2823 for (i = 0; i < s->read_msgfds_num; i++) {
2824 close(s->read_msgfds[i]);
2825 }
2826 g_free(s->read_msgfds);
2827 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002828 if (s->write_msgfds_num) {
2829 g_free(s->write_msgfds);
2830 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002831 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002832 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002833}
2834
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002835static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2836 bool is_listen, bool is_telnet,
2837 bool is_waitconnect,
2838 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002839{
2840 CharDriverState *chr = NULL;
2841 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002842 char host[NI_MAXHOST], serv[NI_MAXSERV];
2843 const char *left = "", *right = "";
2844 struct sockaddr_storage ss;
2845 socklen_t ss_len = sizeof(ss);
2846
2847 memset(&ss, 0, ss_len);
2848 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002849 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002850 return NULL;
2851 }
2852
2853 chr = g_malloc0(sizeof(CharDriverState));
2854 s = g_malloc0(sizeof(TCPCharDriver));
2855
2856 s->connected = 0;
2857 s->fd = -1;
2858 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002859 s->read_msgfds = 0;
2860 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002861 s->write_msgfds = 0;
2862 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002863
2864 chr->filename = g_malloc(256);
2865 switch (ss.ss_family) {
2866#ifndef _WIN32
2867 case AF_UNIX:
2868 s->is_unix = 1;
2869 snprintf(chr->filename, 256, "unix:%s%s",
2870 ((struct sockaddr_un *)(&ss))->sun_path,
2871 is_listen ? ",server" : "");
2872 break;
2873#endif
2874 case AF_INET6:
2875 left = "[";
2876 right = "]";
2877 /* fall through */
2878 case AF_INET:
2879 s->do_nodelay = do_nodelay;
2880 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2881 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002882 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002883 is_telnet ? "telnet" : "tcp",
2884 left, host, right, serv,
2885 is_listen ? ",server" : "");
2886 break;
2887 }
2888
2889 chr->opaque = s;
2890 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002891 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002892 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002893 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002894 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002895 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302896 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002897 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002898 /* be isn't opened until we get a connection */
2899 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002900
2901 if (is_listen) {
2902 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302903 s->listen_chan = io_channel_from_socket(s->listen_fd);
2904 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002905 if (is_telnet) {
2906 s->do_telnetopt = 1;
2907 }
2908 } else {
2909 s->connected = 1;
2910 s->fd = fd;
2911 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302912 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002913 tcp_chr_connect(chr);
2914 }
2915
2916 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002917 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2918 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302919 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002920 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002921 }
2922 return chr;
2923}
2924
2925static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2926{
2927 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002928 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002929 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002930
liguange990a392013-06-18 11:45:35 +08002931 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2932 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2933 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2934 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2935 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002936
aliguorif07b6002008-11-11 20:54:09 +00002937 if (is_unix) {
2938 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002939 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002940 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002941 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002942 }
2943 } else {
2944 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002945 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002946 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002947 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002948 }
2949 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002950 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002951 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002952 }
aliguori6f97dba2008-10-31 18:49:55 +00002953
2954 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002955 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002956
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002957 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2958 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002959 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002960 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002961 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002962 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002963
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002964
aliguori6f97dba2008-10-31 18:49:55 +00002965 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002966 if (local_err) {
2967 qerror_report_err(local_err);
2968 error_free(local_err);
2969 }
2970 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002971 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002972 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002973 if (chr) {
2974 g_free(chr->opaque);
2975 g_free(chr);
2976 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002977 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002978}
2979
Lei Li51767e72013-01-25 00:03:19 +08002980/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002981/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002982
2983typedef struct {
2984 size_t size;
2985 size_t prod;
2986 size_t cons;
2987 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002988} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002989
Markus Armbruster3949e592013-02-06 21:27:24 +01002990static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002991{
Markus Armbruster3949e592013-02-06 21:27:24 +01002992 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002993
Markus Armbruster5c230102013-02-06 21:27:23 +01002994 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002995}
2996
Markus Armbruster3949e592013-02-06 21:27:24 +01002997static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002998{
Markus Armbruster3949e592013-02-06 21:27:24 +01002999 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003000 int i;
3001
3002 if (!buf || (len < 0)) {
3003 return -1;
3004 }
3005
3006 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003007 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3008 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003009 d->cons = d->prod - d->size;
3010 }
3011 }
3012
3013 return 0;
3014}
3015
Markus Armbruster3949e592013-02-06 21:27:24 +01003016static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003017{
Markus Armbruster3949e592013-02-06 21:27:24 +01003018 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003019 int i;
3020
Markus Armbruster5c230102013-02-06 21:27:23 +01003021 for (i = 0; i < len && d->cons != d->prod; i++) {
3022 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003023 }
3024
3025 return i;
3026}
3027
Markus Armbruster3949e592013-02-06 21:27:24 +01003028static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003029{
Markus Armbruster3949e592013-02-06 21:27:24 +01003030 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003031
3032 g_free(d->cbuf);
3033 g_free(d);
3034 chr->opaque = NULL;
3035}
3036
Markus Armbruster4f573782013-07-26 16:44:32 +02003037static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3038 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003039{
3040 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003041 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003042
3043 chr = g_malloc0(sizeof(CharDriverState));
3044 d = g_malloc(sizeof(*d));
3045
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003046 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003047
3048 /* The size must be power of 2 */
3049 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003050 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003051 goto fail;
3052 }
3053
3054 d->prod = 0;
3055 d->cons = 0;
3056 d->cbuf = g_malloc0(d->size);
3057
3058 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003059 chr->chr_write = ringbuf_chr_write;
3060 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003061
3062 return chr;
3063
3064fail:
3065 g_free(d);
3066 g_free(chr);
3067 return NULL;
3068}
3069
Hani Benhabiles8e597772014-05-27 23:39:30 +01003070bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003071{
Markus Armbruster3949e592013-02-06 21:27:24 +01003072 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003073}
3074
Markus Armbruster3949e592013-02-06 21:27:24 +01003075void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003076 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003077 Error **errp)
3078{
3079 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003080 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003081 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003082 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003083
3084 chr = qemu_chr_find(device);
3085 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003086 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003087 return;
3088 }
3089
Markus Armbruster3949e592013-02-06 21:27:24 +01003090 if (!chr_is_ringbuf(chr)) {
3091 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003092 return;
3093 }
3094
Lei Li1f590cf2013-01-25 00:03:20 +08003095 if (has_format && (format == DATA_FORMAT_BASE64)) {
3096 write_data = g_base64_decode(data, &write_count);
3097 } else {
3098 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003099 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003100 }
3101
Markus Armbruster3949e592013-02-06 21:27:24 +01003102 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003103
Markus Armbruster13289fb2013-02-06 21:27:18 +01003104 if (write_data != (uint8_t *)data) {
3105 g_free((void *)write_data);
3106 }
3107
Lei Li1f590cf2013-01-25 00:03:20 +08003108 if (ret < 0) {
3109 error_setg(errp, "Failed to write to device %s", device);
3110 return;
3111 }
3112}
3113
Markus Armbruster3949e592013-02-06 21:27:24 +01003114char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003115 bool has_format, enum DataFormat format,
3116 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003117{
3118 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003119 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003120 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003121 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003122
3123 chr = qemu_chr_find(device);
3124 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003125 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003126 return NULL;
3127 }
3128
Markus Armbruster3949e592013-02-06 21:27:24 +01003129 if (!chr_is_ringbuf(chr)) {
3130 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003131 return NULL;
3132 }
3133
3134 if (size <= 0) {
3135 error_setg(errp, "size must be greater than zero");
3136 return NULL;
3137 }
3138
Markus Armbruster3949e592013-02-06 21:27:24 +01003139 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003140 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003141 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003142
Markus Armbruster3949e592013-02-06 21:27:24 +01003143 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003144
3145 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003146 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003147 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003148 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003149 /*
3150 * FIXME should read only complete, valid UTF-8 characters up
3151 * to @size bytes. Invalid sequences should be replaced by a
3152 * suitable replacement character. Except when (and only
3153 * when) ring buffer lost characters since last read, initial
3154 * continuation characters should be dropped.
3155 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003156 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003157 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003158 }
3159
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003160 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003161}
3162
Gerd Hoffmann33521632009-12-08 13:11:49 +01003163QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003164{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003165 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003166 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003167 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003168 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003169 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003170
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003171 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003172 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003173 qerror_report_err(local_err);
3174 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003175 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003176 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003177
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003178 if (strstart(filename, "mon:", &p)) {
3179 filename = p;
3180 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003181 if (strcmp(filename, "stdio") == 0) {
3182 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3183 * but pass it to the guest. Handle this only for compat syntax,
3184 * for -chardev syntax we have special option for this.
3185 * This is what -nographic did, redirecting+muxing serial+monitor
3186 * to stdio causing Ctrl+C to be passed to guest. */
3187 qemu_opt_set(opts, "signal", "off");
3188 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003189 }
3190
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003191 if (strcmp(filename, "null") == 0 ||
3192 strcmp(filename, "pty") == 0 ||
3193 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003194 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003195 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003196 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003197 return opts;
3198 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003199 if (strstart(filename, "vc", &p)) {
3200 qemu_opt_set(opts, "backend", "vc");
3201 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003202 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003203 /* pixels */
3204 qemu_opt_set(opts, "width", width);
3205 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003206 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003207 /* chars */
3208 qemu_opt_set(opts, "cols", width);
3209 qemu_opt_set(opts, "rows", height);
3210 } else {
3211 goto fail;
3212 }
3213 }
3214 return opts;
3215 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003216 if (strcmp(filename, "con:") == 0) {
3217 qemu_opt_set(opts, "backend", "console");
3218 return opts;
3219 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003220 if (strstart(filename, "COM", NULL)) {
3221 qemu_opt_set(opts, "backend", "serial");
3222 qemu_opt_set(opts, "path", filename);
3223 return opts;
3224 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003225 if (strstart(filename, "file:", &p)) {
3226 qemu_opt_set(opts, "backend", "file");
3227 qemu_opt_set(opts, "path", p);
3228 return opts;
3229 }
3230 if (strstart(filename, "pipe:", &p)) {
3231 qemu_opt_set(opts, "backend", "pipe");
3232 qemu_opt_set(opts, "path", p);
3233 return opts;
3234 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003235 if (strstart(filename, "tcp:", &p) ||
3236 strstart(filename, "telnet:", &p)) {
3237 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3238 host[0] = 0;
3239 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3240 goto fail;
3241 }
3242 qemu_opt_set(opts, "backend", "socket");
3243 qemu_opt_set(opts, "host", host);
3244 qemu_opt_set(opts, "port", port);
3245 if (p[pos] == ',') {
3246 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3247 goto fail;
3248 }
3249 if (strstart(filename, "telnet:", &p))
3250 qemu_opt_set(opts, "telnet", "on");
3251 return opts;
3252 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003253 if (strstart(filename, "udp:", &p)) {
3254 qemu_opt_set(opts, "backend", "udp");
3255 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3256 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003257 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003258 goto fail;
3259 }
3260 }
3261 qemu_opt_set(opts, "host", host);
3262 qemu_opt_set(opts, "port", port);
3263 if (p[pos] == '@') {
3264 p += pos + 1;
3265 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3266 host[0] = 0;
3267 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003268 goto fail;
3269 }
3270 }
3271 qemu_opt_set(opts, "localaddr", host);
3272 qemu_opt_set(opts, "localport", port);
3273 }
3274 return opts;
3275 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003276 if (strstart(filename, "unix:", &p)) {
3277 qemu_opt_set(opts, "backend", "socket");
3278 if (qemu_opts_do_parse(opts, p, "path") != 0)
3279 goto fail;
3280 return opts;
3281 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003282 if (strstart(filename, "/dev/parport", NULL) ||
3283 strstart(filename, "/dev/ppi", NULL)) {
3284 qemu_opt_set(opts, "backend", "parport");
3285 qemu_opt_set(opts, "path", filename);
3286 return opts;
3287 }
3288 if (strstart(filename, "/dev/", NULL)) {
3289 qemu_opt_set(opts, "backend", "tty");
3290 qemu_opt_set(opts, "path", filename);
3291 return opts;
3292 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003293
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003294fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003295 qemu_opts_del(opts);
3296 return NULL;
3297}
3298
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003299static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3300 Error **errp)
3301{
3302 const char *path = qemu_opt_get(opts, "path");
3303
3304 if (path == NULL) {
3305 error_setg(errp, "chardev: file: no filename given");
3306 return;
3307 }
3308 backend->file = g_new0(ChardevFile, 1);
3309 backend->file->out = g_strdup(path);
3310}
3311
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003312static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3313 Error **errp)
3314{
3315 backend->stdio = g_new0(ChardevStdio, 1);
3316 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003317 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003318}
3319
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003320static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3321 Error **errp)
3322{
3323 const char *device = qemu_opt_get(opts, "path");
3324
3325 if (device == NULL) {
3326 error_setg(errp, "chardev: serial/tty: no device path given");
3327 return;
3328 }
3329 backend->serial = g_new0(ChardevHostdev, 1);
3330 backend->serial->device = g_strdup(device);
3331}
3332
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003333static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3334 Error **errp)
3335{
3336 const char *device = qemu_opt_get(opts, "path");
3337
3338 if (device == NULL) {
3339 error_setg(errp, "chardev: parallel: no device path given");
3340 return;
3341 }
3342 backend->parallel = g_new0(ChardevHostdev, 1);
3343 backend->parallel->device = g_strdup(device);
3344}
3345
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003346static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3347 Error **errp)
3348{
3349 const char *device = qemu_opt_get(opts, "path");
3350
3351 if (device == NULL) {
3352 error_setg(errp, "chardev: pipe: no device path given");
3353 return;
3354 }
3355 backend->pipe = g_new0(ChardevHostdev, 1);
3356 backend->pipe->device = g_strdup(device);
3357}
3358
Markus Armbruster4f573782013-07-26 16:44:32 +02003359static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3360 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003361{
3362 int val;
3363
Markus Armbruster3a1da422013-07-26 16:44:34 +02003364 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003365
Markus Armbruster0f953052013-06-27 16:22:07 +02003366 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003367 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003368 backend->ringbuf->has_size = true;
3369 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003370 }
3371}
3372
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003373static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3374 Error **errp)
3375{
3376 const char *chardev = qemu_opt_get(opts, "chardev");
3377
3378 if (chardev == NULL) {
3379 error_setg(errp, "chardev: mux: no chardev given");
3380 return;
3381 }
3382 backend->mux = g_new0(ChardevMux, 1);
3383 backend->mux->chardev = g_strdup(chardev);
3384}
3385
Anthony Liguorid654f342013-03-05 23:21:28 +05303386typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003387 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003388 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003389 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003390 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003391 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003392 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303393} CharDriver;
3394
3395static GSList *backends;
3396
3397void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3398{
3399 CharDriver *s;
3400
3401 s = g_malloc0(sizeof(*s));
3402 s->name = g_strdup(name);
3403 s->open = open;
3404
3405 backends = g_slist_append(backends, s);
3406}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003407
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003408void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003409 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3410{
3411 CharDriver *s;
3412
3413 s = g_malloc0(sizeof(*s));
3414 s->name = g_strdup(name);
3415 s->kind = kind;
3416 s->parse = parse;
3417
3418 backends = g_slist_append(backends, s);
3419}
3420
Anthony Liguorif69554b2011-08-15 11:17:37 -05003421CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003422 void (*init)(struct CharDriverState *s),
3423 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003424{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003425 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303426 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003427 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303428 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003429
3430 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003431 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003432 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003433 }
3434
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003435 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003436 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003437 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003438 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003439 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303440 for (i = backends; i; i = i->next) {
3441 cd = i->data;
3442
3443 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003444 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303445 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003446 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303447 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003448 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003449 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003450 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003451 }
3452
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003453 if (!cd->open) {
3454 /* using new, qapi init */
3455 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3456 ChardevReturn *ret = NULL;
3457 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003458 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003459
3460 if (qemu_opt_get_bool(opts, "mux", 0)) {
3461 bid = g_strdup_printf("%s-base", id);
3462 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003463
3464 chr = NULL;
3465 backend->kind = cd->kind;
3466 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003467 cd->parse(opts, backend, &local_err);
3468 if (local_err) {
3469 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003470 goto qapi_out;
3471 }
3472 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003473 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003474 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003475 goto qapi_out;
3476 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003477
3478 if (bid) {
3479 qapi_free_ChardevBackend(backend);
3480 qapi_free_ChardevReturn(ret);
3481 backend = g_new0(ChardevBackend, 1);
3482 backend->mux = g_new0(ChardevMux, 1);
3483 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3484 backend->mux->chardev = g_strdup(bid);
3485 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003486 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003487 chr = qemu_chr_find(bid);
3488 qemu_chr_delete(chr);
3489 chr = NULL;
3490 goto qapi_out;
3491 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003492 }
3493
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003494 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003495 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003496
3497 qapi_out:
3498 qapi_free_ChardevBackend(backend);
3499 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003500 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003501 return chr;
3502 }
3503
Anthony Liguorid654f342013-03-05 23:21:28 +05303504 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003505 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003506 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003507 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003508 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003509 }
3510
3511 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003512 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003513 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003514 /* if we didn't create the chardev via qmp_chardev_add, we
3515 * need to send the OPENED event here
3516 */
3517 if (!chr->explicit_be_open) {
3518 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3519 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003520 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003521
3522 if (qemu_opt_get_bool(opts, "mux", 0)) {
3523 CharDriverState *base = chr;
3524 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003525 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003526 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3527 chr = qemu_chr_open_mux(base);
3528 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003529 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003530 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003531 } else {
3532 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003533 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003534 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003535 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003536 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003537
3538err:
3539 qemu_opts_del(opts);
3540 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003541}
3542
Anthony Liguori27143a42011-08-15 11:17:36 -05003543CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003544{
3545 const char *p;
3546 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003547 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003548 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003549
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003550 if (strstart(filename, "chardev:", &p)) {
3551 return qemu_chr_find(p);
3552 }
3553
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003554 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003555 if (!opts)
3556 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003557
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003558 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003559 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003560 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003561 error_free(err);
3562 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003563 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003564 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003565 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003566 }
3567 return chr;
3568}
3569
Anthony Liguori15f31512011-08-15 11:17:35 -05003570void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003571{
3572 if (chr->chr_set_echo) {
3573 chr->chr_set_echo(chr, echo);
3574 }
3575}
3576
Hans de Goede8e25daa2013-03-26 11:07:57 +01003577void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003578{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003579 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003580 return;
3581 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003582 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003583 if (chr->chr_set_fe_open) {
3584 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003585 }
3586}
3587
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003588void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3589{
3590 if (chr->chr_fe_event) {
3591 chr->chr_fe_event(chr, event);
3592 }
3593}
3594
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003595int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3596 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303597{
3598 GSource *src;
3599 guint tag;
3600
3601 if (s->chr_add_watch == NULL) {
3602 return -ENOSYS;
3603 }
3604
3605 src = s->chr_add_watch(s, cond);
3606 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3607 tag = g_source_attach(src, NULL);
3608 g_source_unref(src);
3609
3610 return tag;
3611}
3612
Hans de Goede44c473d2013-03-27 20:29:39 +01003613int qemu_chr_fe_claim(CharDriverState *s)
3614{
3615 if (s->avail_connections < 1) {
3616 return -1;
3617 }
3618 s->avail_connections--;
3619 return 0;
3620}
3621
3622void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3623{
3624 if (qemu_chr_fe_claim(s) != 0) {
3625 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3626 __func__, s->label);
3627 exit(1);
3628 }
3629}
3630
3631void qemu_chr_fe_release(CharDriverState *s)
3632{
3633 s->avail_connections++;
3634}
3635
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003636void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003637{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003638 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003639 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003640 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003641 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003642 g_free(chr->filename);
3643 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003644 if (chr->opts) {
3645 qemu_opts_del(chr->opts);
3646 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003647 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003648}
3649
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003650ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003651{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003652 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003653 CharDriverState *chr;
3654
Blue Swirl72cf2d42009-09-12 07:36:22 +00003655 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003656 ChardevInfoList *info = g_malloc0(sizeof(*info));
3657 info->value = g_malloc0(sizeof(*info->value));
3658 info->value->label = g_strdup(chr->label);
3659 info->value->filename = g_strdup(chr->filename);
3660
3661 info->next = chr_list;
3662 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003663 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003664
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003665 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003666}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003667
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003668ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3669{
3670 ChardevBackendInfoList *backend_list = NULL;
3671 CharDriver *c = NULL;
3672 GSList *i = NULL;
3673
3674 for (i = backends; i; i = i->next) {
3675 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3676 c = i->data;
3677 info->value = g_malloc0(sizeof(*info->value));
3678 info->value->name = g_strdup(c->name);
3679
3680 info->next = backend_list;
3681 backend_list = info;
3682 }
3683
3684 return backend_list;
3685}
3686
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003687CharDriverState *qemu_chr_find(const char *name)
3688{
3689 CharDriverState *chr;
3690
Blue Swirl72cf2d42009-09-12 07:36:22 +00003691 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003692 if (strcmp(chr->label, name) != 0)
3693 continue;
3694 return chr;
3695 }
3696 return NULL;
3697}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003698
3699/* Get a character (serial) device interface. */
3700CharDriverState *qemu_char_get_next_serial(void)
3701{
3702 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003703 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003704
3705 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003706
3707 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3708 chr = serial_hds[next_serial++];
3709 qemu_chr_fe_claim_no_fail(chr);
3710 return chr;
3711 }
3712 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003713}
3714
Paolo Bonzini4d454572012-11-26 16:03:42 +01003715QemuOptsList qemu_chardev_opts = {
3716 .name = "chardev",
3717 .implied_opt_name = "backend",
3718 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3719 .desc = {
3720 {
3721 .name = "backend",
3722 .type = QEMU_OPT_STRING,
3723 },{
3724 .name = "path",
3725 .type = QEMU_OPT_STRING,
3726 },{
3727 .name = "host",
3728 .type = QEMU_OPT_STRING,
3729 },{
3730 .name = "port",
3731 .type = QEMU_OPT_STRING,
3732 },{
3733 .name = "localaddr",
3734 .type = QEMU_OPT_STRING,
3735 },{
3736 .name = "localport",
3737 .type = QEMU_OPT_STRING,
3738 },{
3739 .name = "to",
3740 .type = QEMU_OPT_NUMBER,
3741 },{
3742 .name = "ipv4",
3743 .type = QEMU_OPT_BOOL,
3744 },{
3745 .name = "ipv6",
3746 .type = QEMU_OPT_BOOL,
3747 },{
3748 .name = "wait",
3749 .type = QEMU_OPT_BOOL,
3750 },{
3751 .name = "server",
3752 .type = QEMU_OPT_BOOL,
3753 },{
3754 .name = "delay",
3755 .type = QEMU_OPT_BOOL,
3756 },{
3757 .name = "telnet",
3758 .type = QEMU_OPT_BOOL,
3759 },{
3760 .name = "width",
3761 .type = QEMU_OPT_NUMBER,
3762 },{
3763 .name = "height",
3764 .type = QEMU_OPT_NUMBER,
3765 },{
3766 .name = "cols",
3767 .type = QEMU_OPT_NUMBER,
3768 },{
3769 .name = "rows",
3770 .type = QEMU_OPT_NUMBER,
3771 },{
3772 .name = "mux",
3773 .type = QEMU_OPT_BOOL,
3774 },{
3775 .name = "signal",
3776 .type = QEMU_OPT_BOOL,
3777 },{
3778 .name = "name",
3779 .type = QEMU_OPT_STRING,
3780 },{
3781 .name = "debug",
3782 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003783 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003784 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003785 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003786 },{
3787 .name = "chardev",
3788 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003789 },
3790 { /* end of list */ }
3791 },
3792};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003793
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003794#ifdef _WIN32
3795
3796static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3797{
3798 HANDLE out;
3799
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003800 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003801 error_setg(errp, "input file not supported");
3802 return NULL;
3803 }
3804
3805 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3806 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3807 if (out == INVALID_HANDLE_VALUE) {
3808 error_setg(errp, "open %s failed", file->out);
3809 return NULL;
3810 }
3811 return qemu_chr_open_win_file(out);
3812}
3813
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003814static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3815 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003816{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003817 return qemu_chr_open_win_path(serial->device);
3818}
3819
3820static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3821 Error **errp)
3822{
3823 error_setg(errp, "character device backend type 'parallel' not supported");
3824 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003825}
3826
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003827#else /* WIN32 */
3828
3829static int qmp_chardev_open_file_source(char *src, int flags,
3830 Error **errp)
3831{
3832 int fd = -1;
3833
3834 TFR(fd = qemu_open(src, flags, 0666));
3835 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003836 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003837 }
3838 return fd;
3839}
3840
3841static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3842{
Markus Armbruster5f758362014-05-19 18:57:34 +02003843 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003844
3845 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3846 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003847 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003848 return NULL;
3849 }
3850
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003851 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003852 flags = O_RDONLY;
3853 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003854 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003855 qemu_close(out);
3856 return NULL;
3857 }
3858 }
3859
3860 return qemu_chr_open_fd(in, out);
3861}
3862
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003863static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3864 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003865{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003866#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003867 int fd;
3868
3869 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003870 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003871 return NULL;
3872 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003873 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003874 return qemu_chr_open_tty_fd(fd);
3875#else
3876 error_setg(errp, "character device backend type 'serial' not supported");
3877 return NULL;
3878#endif
3879}
3880
3881static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3882 Error **errp)
3883{
3884#ifdef HAVE_CHARDEV_PARPORT
3885 int fd;
3886
3887 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003888 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003889 return NULL;
3890 }
3891 return qemu_chr_open_pp_fd(fd);
3892#else
3893 error_setg(errp, "character device backend type 'parallel' not supported");
3894 return NULL;
3895#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003896}
3897
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003898#endif /* WIN32 */
3899
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003900static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3901 Error **errp)
3902{
3903 SocketAddress *addr = sock->addr;
3904 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3905 bool is_listen = sock->has_server ? sock->server : true;
3906 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3907 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3908 int fd;
3909
3910 if (is_listen) {
3911 fd = socket_listen(addr, errp);
3912 } else {
3913 fd = socket_connect(addr, errp, NULL, NULL);
3914 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003915 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003916 return NULL;
3917 }
3918 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3919 is_telnet, is_waitconnect, errp);
3920}
3921
Lei Li08d0ab32013-05-20 14:51:03 +08003922static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3923 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003924{
3925 int fd;
3926
Lei Li08d0ab32013-05-20 14:51:03 +08003927 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003928 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003929 return NULL;
3930 }
3931 return qemu_chr_open_udp_fd(fd);
3932}
3933
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003934ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3935 Error **errp)
3936{
3937 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003938 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003939
3940 chr = qemu_chr_find(id);
3941 if (chr) {
3942 error_setg(errp, "Chardev '%s' already exists", id);
3943 g_free(ret);
3944 return NULL;
3945 }
3946
3947 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003948 case CHARDEV_BACKEND_KIND_FILE:
3949 chr = qmp_chardev_open_file(backend->file, errp);
3950 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003951 case CHARDEV_BACKEND_KIND_SERIAL:
3952 chr = qmp_chardev_open_serial(backend->serial, errp);
3953 break;
3954 case CHARDEV_BACKEND_KIND_PARALLEL:
3955 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003956 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003957 case CHARDEV_BACKEND_KIND_PIPE:
3958 chr = qemu_chr_open_pipe(backend->pipe);
3959 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003960 case CHARDEV_BACKEND_KIND_SOCKET:
3961 chr = qmp_chardev_open_socket(backend->socket, errp);
3962 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003963 case CHARDEV_BACKEND_KIND_UDP:
3964 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003965 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003966#ifdef HAVE_CHARDEV_TTY
3967 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003968 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003969 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003970#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003971 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003972 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003973 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003974 case CHARDEV_BACKEND_KIND_MUX:
3975 base = qemu_chr_find(backend->mux->chardev);
3976 if (base == NULL) {
3977 error_setg(errp, "mux: base chardev %s not found",
3978 backend->mux->chardev);
3979 break;
3980 }
3981 chr = qemu_chr_open_mux(base);
3982 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003983 case CHARDEV_BACKEND_KIND_MSMOUSE:
3984 chr = qemu_chr_open_msmouse();
3985 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003986#ifdef CONFIG_BRLAPI
3987 case CHARDEV_BACKEND_KIND_BRAILLE:
3988 chr = chr_baum_init();
3989 break;
3990#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003991 case CHARDEV_BACKEND_KIND_STDIO:
3992 chr = qemu_chr_open_stdio(backend->stdio);
3993 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003994#ifdef _WIN32
3995 case CHARDEV_BACKEND_KIND_CONSOLE:
3996 chr = qemu_chr_open_win_con();
3997 break;
3998#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003999#ifdef CONFIG_SPICE
4000 case CHARDEV_BACKEND_KIND_SPICEVMC:
4001 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4002 break;
4003 case CHARDEV_BACKEND_KIND_SPICEPORT:
4004 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4005 break;
4006#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004007 case CHARDEV_BACKEND_KIND_VC:
4008 chr = vc_init(backend->vc);
4009 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004010 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004011 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004012 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004013 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004014 default:
4015 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4016 break;
4017 }
4018
Markus Armbruster3894c782014-05-19 18:57:36 +02004019 /*
4020 * Character backend open hasn't been fully converted to the Error
4021 * API. Some opens fail without setting an error. Set a generic
4022 * error then.
4023 * TODO full conversion to Error API
4024 */
4025 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004026 error_setg(errp, "Failed to create chardev");
4027 }
4028 if (chr) {
4029 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004030 chr->avail_connections =
4031 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004032 if (!chr->filename) {
4033 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4034 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004035 if (!chr->explicit_be_open) {
4036 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4037 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004038 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4039 return ret;
4040 } else {
4041 g_free(ret);
4042 return NULL;
4043 }
4044}
4045
4046void qmp_chardev_remove(const char *id, Error **errp)
4047{
4048 CharDriverState *chr;
4049
4050 chr = qemu_chr_find(id);
4051 if (NULL == chr) {
4052 error_setg(errp, "Chardev '%s' not found", id);
4053 return;
4054 }
4055 if (chr->chr_can_read || chr->chr_read ||
4056 chr->chr_event || chr->handler_opaque) {
4057 error_setg(errp, "Chardev '%s' is busy", id);
4058 return;
4059 }
4060 qemu_chr_delete(chr);
4061}
Anthony Liguorid654f342013-03-05 23:21:28 +05304062
4063static void register_types(void)
4064{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004065 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304066 register_char_driver("socket", qemu_chr_open_socket);
4067 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004068 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004069 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004070 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4071 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004072 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4073 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004074 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4075 qemu_chr_parse_serial);
4076 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4077 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004078 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4079 qemu_chr_parse_parallel);
4080 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4081 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004082 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004083 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004084 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4085 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004086 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4087 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004088 /* Bug-compatibility: */
4089 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4090 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004091 /* this must be done after machine init, since we register FEs with muxes
4092 * as part of realize functions like serial_isa_realizefn when -nographic
4093 * is specified
4094 */
4095 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304096}
4097
4098type_init(register_types);