blob: dcfeb73a22a9f68152f73f02c6b62e5a8912b4ae [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
Corey Minyard9f781162014-10-02 11:17:33 -050087#define CHR_MAX_FILENAME_SIZE 256
Amit Shah9bd78542009-11-03 19:59:54 +053088
aliguori6f97dba2008-10-31 18:49:55 +000089/***********************************************************/
90/* character device */
91
Blue Swirl72cf2d42009-09-12 07:36:22 +000092static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
93 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000094
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020095CharDriverState *qemu_chr_alloc(void)
96{
97 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +020098 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020099 return chr;
100}
101
Hans de Goedea425d232011-11-19 10:22:43 +0100102void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000103{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200104 /* Keep track if the char device is open */
105 switch (event) {
106 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100107 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200108 break;
109 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100110 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200111 break;
112 }
113
aliguori6f97dba2008-10-31 18:49:55 +0000114 if (!s->chr_event)
115 return;
116 s->chr_event(s->handler_opaque, event);
117}
118
Hans de Goedefee204f2013-03-26 11:07:54 +0100119void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000120{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500121 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000122}
123
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500124int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000125{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200126 int ret;
127
128 qemu_mutex_lock(&s->chr_write_lock);
129 ret = s->chr_write(s, buf, len);
130 qemu_mutex_unlock(&s->chr_write_lock);
131 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000132}
133
Anthony Liguoricd187202013-03-26 10:04:17 -0500134int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
135{
136 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200137 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500138
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200139 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500140 while (offset < len) {
141 do {
142 res = s->chr_write(s, buf + offset, len - offset);
143 if (res == -1 && errno == EAGAIN) {
144 g_usleep(100);
145 }
146 } while (res == -1 && errno == EAGAIN);
147
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200148 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500149 break;
150 }
151
Anthony Liguoricd187202013-03-26 10:04:17 -0500152 offset += res;
153 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200154 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500155
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200156 if (res < 0) {
157 return res;
158 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500159 return offset;
160}
161
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300162int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
163{
164 int offset = 0, counter = 10;
165 int res;
166
167 if (!s->chr_sync_read) {
168 return 0;
169 }
170
171 while (offset < len) {
172 do {
173 res = s->chr_sync_read(s, buf + offset, len - offset);
174 if (res == -1 && errno == EAGAIN) {
175 g_usleep(100);
176 }
177 } while (res == -1 && errno == EAGAIN);
178
179 if (res == 0) {
180 break;
181 }
182
183 if (res < 0) {
184 return res;
185 }
186
187 offset += res;
188
189 if (!counter--) {
190 break;
191 }
192 }
193
194 return offset;
195}
196
Anthony Liguori41084f12011-08-15 11:17:34 -0500197int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000198{
199 if (!s->chr_ioctl)
200 return -ENOTSUP;
201 return s->chr_ioctl(s, cmd, arg);
202}
203
Anthony Liguori909cda12011-08-15 11:17:31 -0500204int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000205{
206 if (!s->chr_can_read)
207 return 0;
208 return s->chr_can_read(s->handler_opaque);
209}
210
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500211void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000212{
Stefan Weilac310732012-04-19 22:27:14 +0200213 if (s->chr_read) {
214 s->chr_read(s->handler_opaque, buf, len);
215 }
aliguori6f97dba2008-10-31 18:49:55 +0000216}
217
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500218int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100219{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300220 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800221 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300222}
223
224int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
225{
226 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100227}
228
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300229int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
230{
231 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
232}
233
Daniel P. Berrange13661082011-06-23 13:31:42 +0100234int qemu_chr_add_client(CharDriverState *s, int fd)
235{
236 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
237}
238
aliguori6f97dba2008-10-31 18:49:55 +0000239void qemu_chr_accept_input(CharDriverState *s)
240{
241 if (s->chr_accept_input)
242 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100243 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000244}
245
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500246void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000247{
Amit Shah9bd78542009-11-03 19:59:54 +0530248 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000249 va_list ap;
250 va_start(ap, fmt);
251 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500252 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000253 va_end(ap);
254}
255
Amit Shah386a5a12013-08-28 15:24:05 +0530256static void remove_fd_in_watch(CharDriverState *chr);
257
aliguori6f97dba2008-10-31 18:49:55 +0000258void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100259 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000260 IOReadHandler *fd_read,
261 IOEventHandler *fd_event,
262 void *opaque)
263{
Hans de Goede19083222013-03-26 11:07:56 +0100264 int fe_open;
265
Amit Shahda7d9982011-04-25 15:18:22 +0530266 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100267 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530268 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100269 } else {
270 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530271 }
aliguori6f97dba2008-10-31 18:49:55 +0000272 s->chr_can_read = fd_can_read;
273 s->chr_read = fd_read;
274 s->chr_event = fd_event;
275 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200276 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000277 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200278
Hans de Goede19083222013-03-26 11:07:56 +0100279 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100280 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100281 }
282
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200283 /* We're connecting to an already opened device, so let's make sure we
284 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100285 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100286 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200287 }
aliguori6f97dba2008-10-31 18:49:55 +0000288}
289
290static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
291{
292 return len;
293}
294
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100295static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000296{
297 CharDriverState *chr;
298
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200299 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000300 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500301 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100302 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000303}
304
305/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000306#define MAX_MUX 4
307#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
308#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
309typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100310 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000311 IOReadHandler *chr_read[MAX_MUX];
312 IOEventHandler *chr_event[MAX_MUX];
313 void *ext_opaque[MAX_MUX];
314 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200315 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000316 int mux_cnt;
317 int term_got_escape;
318 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000319 /* Intermediate input buffer allows to catch escape sequences even if the
320 currently active device is not accepting any input - but only until it
321 is full as well. */
322 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
323 int prod[MAX_MUX];
324 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200325 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200326
327 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200328 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200329 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000330} MuxDriver;
331
332
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200333/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000334static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
335{
336 MuxDriver *d = chr->opaque;
337 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200338 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200339 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000340 } else {
341 int i;
342
343 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200344 for (i = 0; i < len; i++) {
345 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000346 char buf1[64];
347 int64_t ti;
348 int secs;
349
Alex Blighbc72ad62013-08-21 16:03:08 +0100350 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200351 if (d->timestamps_start == -1)
352 d->timestamps_start = ti;
353 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000354 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000355 snprintf(buf1, sizeof(buf1),
356 "[%02d:%02d:%02d.%03d] ",
357 secs / 3600,
358 (secs / 60) % 60,
359 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000360 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200361 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200362 d->linestart = 0;
363 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200364 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200365 if (buf[i] == '\n') {
366 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000367 }
368 }
369 }
370 return ret;
371}
372
373static const char * const mux_help[] = {
374 "% h print this help\n\r",
375 "% x exit emulator\n\r",
376 "% s save disk data back to file (if -snapshot)\n\r",
377 "% t toggle console timestamps\n\r"
378 "% b send break (magic sysrq)\n\r",
379 "% c switch between console and monitor\n\r",
380 "% % sends %\n\r",
381 NULL
382};
383
384int term_escape_char = 0x01; /* ctrl-a is used for escape */
385static void mux_print_help(CharDriverState *chr)
386{
387 int i, j;
388 char ebuf[15] = "Escape-Char";
389 char cbuf[50] = "\n\r";
390
391 if (term_escape_char > 0 && term_escape_char < 26) {
392 snprintf(cbuf, sizeof(cbuf), "\n\r");
393 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
394 } else {
395 snprintf(cbuf, sizeof(cbuf),
396 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
397 term_escape_char);
398 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200399 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000400 for (i = 0; mux_help[i] != NULL; i++) {
401 for (j=0; mux_help[i][j] != '\0'; j++) {
402 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200403 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000404 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200405 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000406 }
407 }
408}
409
aliguori2724b182009-03-05 23:01:47 +0000410static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
411{
412 if (d->chr_event[mux_nr])
413 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
414}
415
aliguori6f97dba2008-10-31 18:49:55 +0000416static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
417{
418 if (d->term_got_escape) {
419 d->term_got_escape = 0;
420 if (ch == term_escape_char)
421 goto send_char;
422 switch(ch) {
423 case '?':
424 case 'h':
425 mux_print_help(chr);
426 break;
427 case 'x':
428 {
429 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200430 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000431 exit(0);
432 break;
433 }
434 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200435 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000436 break;
437 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100438 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000439 break;
440 case 'c':
441 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200442 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
443 d->focus++;
444 if (d->focus >= d->mux_cnt)
445 d->focus = 0;
446 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000447 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200448 case 't':
449 d->timestamps = !d->timestamps;
450 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200451 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200452 break;
aliguori6f97dba2008-10-31 18:49:55 +0000453 }
454 } else if (ch == term_escape_char) {
455 d->term_got_escape = 1;
456 } else {
457 send_char:
458 return 1;
459 }
460 return 0;
461}
462
463static void mux_chr_accept_input(CharDriverState *chr)
464{
aliguori6f97dba2008-10-31 18:49:55 +0000465 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200466 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000467
aliguoria80bf992009-03-05 23:00:02 +0000468 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000469 d->chr_can_read[m] &&
470 d->chr_can_read[m](d->ext_opaque[m])) {
471 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000472 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000473 }
474}
475
476static int mux_chr_can_read(void *opaque)
477{
478 CharDriverState *chr = opaque;
479 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200480 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000481
aliguoria80bf992009-03-05 23:00:02 +0000482 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000483 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000484 if (d->chr_can_read[m])
485 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000486 return 0;
487}
488
489static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
490{
491 CharDriverState *chr = opaque;
492 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200493 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000494 int i;
495
496 mux_chr_accept_input (opaque);
497
498 for(i = 0; i < size; i++)
499 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000500 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000501 d->chr_can_read[m] &&
502 d->chr_can_read[m](d->ext_opaque[m]))
503 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
504 else
aliguoria80bf992009-03-05 23:00:02 +0000505 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000506 }
507}
508
509static void mux_chr_event(void *opaque, int event)
510{
511 CharDriverState *chr = opaque;
512 MuxDriver *d = chr->opaque;
513 int i;
514
515 /* Send the event to all registered listeners */
516 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000517 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000518}
519
520static void mux_chr_update_read_handler(CharDriverState *chr)
521{
522 MuxDriver *d = chr->opaque;
523
524 if (d->mux_cnt >= MAX_MUX) {
525 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
526 return;
527 }
528 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
529 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
530 d->chr_read[d->mux_cnt] = chr->chr_read;
531 d->chr_event[d->mux_cnt] = chr->chr_event;
532 /* Fix up the real driver with mux routines */
533 if (d->mux_cnt == 0) {
534 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
535 mux_chr_event, chr);
536 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200537 if (d->focus != -1) {
538 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200539 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200540 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000541 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200542 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000543}
544
Michael Roth7b7ab182013-07-30 13:04:22 -0500545static bool muxes_realized;
546
547/**
548 * Called after processing of default and command-line-specified
549 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
550 * to a mux chardev. This is done here to ensure that
551 * output/prompts/banners are only displayed for the FE that has
552 * focus when initial command-line processing/machine init is
553 * completed.
554 *
555 * After this point, any new FE attached to any new or existing
556 * mux will receive CHR_EVENT_OPENED notifications for the BE
557 * immediately.
558 */
559static void muxes_realize_done(Notifier *notifier, void *unused)
560{
561 CharDriverState *chr;
562
563 QTAILQ_FOREACH(chr, &chardevs, next) {
564 if (chr->is_mux) {
565 MuxDriver *d = chr->opaque;
566 int i;
567
568 /* send OPENED to all already-attached FEs */
569 for (i = 0; i < d->mux_cnt; i++) {
570 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
571 }
572 /* mark mux as OPENED so any new FEs will immediately receive
573 * OPENED event
574 */
575 qemu_chr_be_generic_open(chr);
576 }
577 }
578 muxes_realized = true;
579}
580
581static Notifier muxes_realize_notify = {
582 .notify = muxes_realize_done,
583};
584
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400585static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
586{
587 MuxDriver *d = s->opaque;
588 return d->drv->chr_add_watch(d->drv, cond);
589}
590
aliguori6f97dba2008-10-31 18:49:55 +0000591static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
592{
593 CharDriverState *chr;
594 MuxDriver *d;
595
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200596 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500597 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000598
599 chr->opaque = d;
600 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200601 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000602 chr->chr_write = mux_chr_write;
603 chr->chr_update_read_handler = mux_chr_update_read_handler;
604 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100605 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100606 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400607 if (drv->chr_add_watch) {
608 chr->chr_add_watch = mux_chr_add_watch;
609 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500610 /* only default to opened state if we've realized the initial
611 * set of muxes
612 */
613 chr->explicit_be_open = muxes_realized ? 0 : 1;
614 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200615
aliguori6f97dba2008-10-31 18:49:55 +0000616 return chr;
617}
618
619
620#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000621int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000622{
623 int ret, len;
624
625 len = len1;
626 while (len > 0) {
627 ret = send(fd, buf, len, 0);
628 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000629 errno = WSAGetLastError();
630 if (errno != WSAEWOULDBLOCK) {
631 return -1;
632 }
633 } else if (ret == 0) {
634 break;
635 } else {
636 buf += ret;
637 len -= ret;
638 }
639 }
640 return len1 - len;
641}
642
643#else
644
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100645int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000646{
647 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100648 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000649
650 len = len1;
651 while (len > 0) {
652 ret = write(fd, buf, len);
653 if (ret < 0) {
654 if (errno != EINTR && errno != EAGAIN)
655 return -1;
656 } else if (ret == 0) {
657 break;
658 } else {
659 buf += ret;
660 len -= ret;
661 }
662 }
663 return len1 - len;
664}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500665
666int recv_all(int fd, void *_buf, int len1, bool single_read)
667{
668 int ret, len;
669 uint8_t *buf = _buf;
670
671 len = len1;
672 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
673 if (ret < 0) {
674 if (errno != EINTR && errno != EAGAIN) {
675 return -1;
676 }
677 continue;
678 } else {
679 if (single_read) {
680 return ret;
681 }
682 buf += ret;
683 len -= ret;
684 }
685 }
686 return len1 - len;
687}
688
aliguori6f97dba2008-10-31 18:49:55 +0000689#endif /* !_WIN32 */
690
Anthony Liguori96c63842013-03-05 23:21:18 +0530691typedef struct IOWatchPoll
692{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200693 GSource parent;
694
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200695 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530696 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530697
698 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200699 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530700 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530701} IOWatchPoll;
702
Anthony Liguori96c63842013-03-05 23:21:18 +0530703static IOWatchPoll *io_watch_poll_from_source(GSource *source)
704{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200705 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530706}
707
708static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
709{
710 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200711 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200712 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200713 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530714 return FALSE;
715 }
716
Paolo Bonzinid185c092013-04-05 17:59:33 +0200717 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200718 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
719 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200720 g_source_attach(iwp->src, NULL);
721 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200722 g_source_destroy(iwp->src);
723 g_source_unref(iwp->src);
724 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200725 }
726 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530727}
728
729static gboolean io_watch_poll_check(GSource *source)
730{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200731 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530732}
733
734static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
735 gpointer user_data)
736{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200737 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530738}
739
740static void io_watch_poll_finalize(GSource *source)
741{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200742 /* Due to a glib bug, removing the last reference to a source
743 * inside a finalize callback causes recursive locking (and a
744 * deadlock). This is not a problem inside other callbacks,
745 * including dispatch callbacks, so we call io_remove_watch_poll
746 * to remove this source. At this point, iwp->src must
747 * be NULL, or we would leak it.
748 *
749 * This would be solved much more elegantly by child sources,
750 * but we support older glib versions that do not have them.
751 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530752 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200753 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530754}
755
756static GSourceFuncs io_watch_poll_funcs = {
757 .prepare = io_watch_poll_prepare,
758 .check = io_watch_poll_check,
759 .dispatch = io_watch_poll_dispatch,
760 .finalize = io_watch_poll_finalize,
761};
762
763/* Can only be used for read */
764static guint io_add_watch_poll(GIOChannel *channel,
765 IOCanReadHandler *fd_can_read,
766 GIOFunc fd_read,
767 gpointer user_data)
768{
769 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200770 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530771
Paolo Bonzinid185c092013-04-05 17:59:33 +0200772 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530773 iwp->fd_can_read = fd_can_read;
774 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200775 iwp->channel = channel;
776 iwp->fd_read = (GSourceFunc) fd_read;
777 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530778
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200779 tag = g_source_attach(&iwp->parent, NULL);
780 g_source_unref(&iwp->parent);
781 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530782}
783
Paolo Bonzini2b316772013-04-19 17:32:09 +0200784static void io_remove_watch_poll(guint tag)
785{
786 GSource *source;
787 IOWatchPoll *iwp;
788
789 g_return_if_fail (tag > 0);
790
791 source = g_main_context_find_source_by_id(NULL, tag);
792 g_return_if_fail (source != NULL);
793
794 iwp = io_watch_poll_from_source(source);
795 if (iwp->src) {
796 g_source_destroy(iwp->src);
797 g_source_unref(iwp->src);
798 iwp->src = NULL;
799 }
800 g_source_destroy(&iwp->parent);
801}
802
Amit Shah26da70c2013-08-28 15:23:37 +0530803static void remove_fd_in_watch(CharDriverState *chr)
804{
805 if (chr->fd_in_tag) {
806 io_remove_watch_poll(chr->fd_in_tag);
807 chr->fd_in_tag = 0;
808 }
809}
810
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000811#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530812static GIOChannel *io_channel_from_fd(int fd)
813{
814 GIOChannel *chan;
815
816 if (fd == -1) {
817 return NULL;
818 }
819
820 chan = g_io_channel_unix_new(fd);
821
822 g_io_channel_set_encoding(chan, NULL, NULL);
823 g_io_channel_set_buffered(chan, FALSE);
824
825 return chan;
826}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000827#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530828
Anthony Liguori76a96442013-03-05 23:21:21 +0530829static GIOChannel *io_channel_from_socket(int fd)
830{
831 GIOChannel *chan;
832
833 if (fd == -1) {
834 return NULL;
835 }
836
837#ifdef _WIN32
838 chan = g_io_channel_win32_new_socket(fd);
839#else
840 chan = g_io_channel_unix_new(fd);
841#endif
842
843 g_io_channel_set_encoding(chan, NULL, NULL);
844 g_io_channel_set_buffered(chan, FALSE);
845
846 return chan;
847}
848
Anthony Liguori684a0962013-03-29 11:39:50 -0500849static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530850{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200851 size_t offset = 0;
852 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530853
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200854 while (offset < len && status == G_IO_STATUS_NORMAL) {
855 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500856
857 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530858 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500859 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530860 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500861
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200862 if (offset > 0) {
863 return offset;
864 }
865 switch (status) {
866 case G_IO_STATUS_NORMAL:
867 g_assert(len == 0);
868 return 0;
869 case G_IO_STATUS_AGAIN:
870 errno = EAGAIN;
871 return -1;
872 default:
873 break;
874 }
875 errno = EINVAL;
876 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530877}
Anthony Liguori96c63842013-03-05 23:21:18 +0530878
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000879#ifndef _WIN32
880
Anthony Liguoria29753f2013-03-05 23:21:19 +0530881typedef struct FDCharDriver {
882 CharDriverState *chr;
883 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000884 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530885 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000886} FDCharDriver;
887
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200888/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000889static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
890{
891 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530892
Anthony Liguori684a0962013-03-29 11:39:50 -0500893 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530894}
895
896static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
897{
898 CharDriverState *chr = opaque;
899 FDCharDriver *s = chr->opaque;
900 int len;
901 uint8_t buf[READ_BUF_LEN];
902 GIOStatus status;
903 gsize bytes_read;
904
905 len = sizeof(buf);
906 if (len > s->max_size) {
907 len = s->max_size;
908 }
909 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200910 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530911 }
912
913 status = g_io_channel_read_chars(chan, (gchar *)buf,
914 len, &bytes_read, NULL);
915 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530916 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530917 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
918 return FALSE;
919 }
920 if (status == G_IO_STATUS_NORMAL) {
921 qemu_chr_be_write(chr, buf, bytes_read);
922 }
923
924 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000925}
926
927static int fd_chr_read_poll(void *opaque)
928{
929 CharDriverState *chr = opaque;
930 FDCharDriver *s = chr->opaque;
931
Anthony Liguori909cda12011-08-15 11:17:31 -0500932 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000933 return s->max_size;
934}
935
Anthony Liguori23673ca2013-03-05 23:21:23 +0530936static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
937{
938 FDCharDriver *s = chr->opaque;
939 return g_io_create_watch(s->fd_out, cond);
940}
941
aliguori6f97dba2008-10-31 18:49:55 +0000942static void fd_chr_update_read_handler(CharDriverState *chr)
943{
944 FDCharDriver *s = chr->opaque;
945
Amit Shah26da70c2013-08-28 15:23:37 +0530946 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530947 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530948 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
949 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000950 }
951}
952
953static void fd_chr_close(struct CharDriverState *chr)
954{
955 FDCharDriver *s = chr->opaque;
956
Amit Shah26da70c2013-08-28 15:23:37 +0530957 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530958 if (s->fd_in) {
959 g_io_channel_unref(s->fd_in);
960 }
961 if (s->fd_out) {
962 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000963 }
964
Anthony Liguori7267c092011-08-20 22:09:37 -0500965 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100966 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000967}
968
969/* open a character device to a unix fd */
970static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
971{
972 CharDriverState *chr;
973 FDCharDriver *s;
974
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200975 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500976 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530977 s->fd_in = io_channel_from_fd(fd_in);
978 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +0800979 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530980 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000981 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530982 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000983 chr->chr_write = fd_chr_write;
984 chr->chr_update_read_handler = fd_chr_update_read_handler;
985 chr->chr_close = fd_chr_close;
986
aliguori6f97dba2008-10-31 18:49:55 +0000987 return chr;
988}
989
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100990static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000991{
992 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -0500993 char filename_in[CHR_MAX_FILENAME_SIZE];
994 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100995 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200996
997 if (filename == NULL) {
998 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100999 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001000 }
aliguori6f97dba2008-10-31 18:49:55 +00001001
Corey Minyard9f781162014-10-02 11:17:33 -05001002 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1003 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001004 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1005 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001006 if (fd_in < 0 || fd_out < 0) {
1007 if (fd_in >= 0)
1008 close(fd_in);
1009 if (fd_out >= 0)
1010 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001011 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001012 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001013 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001014 }
aliguori6f97dba2008-10-31 18:49:55 +00001015 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001016 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001017}
1018
aliguori6f97dba2008-10-31 18:49:55 +00001019/* init terminal so that we can grab keys */
1020static struct termios oldtty;
1021static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001022static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001023static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001024
1025static void term_exit(void)
1026{
1027 tcsetattr (0, TCSANOW, &oldtty);
1028 fcntl(0, F_SETFL, old_fd0_flags);
1029}
1030
Paolo Bonzinibb002512010-12-23 13:42:50 +01001031static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001032{
1033 struct termios tty;
1034
Paolo Bonzini03693642010-12-23 13:42:49 +01001035 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001036 if (!echo) {
1037 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001038 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001039 tty.c_oflag |= OPOST;
1040 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1041 tty.c_cflag &= ~(CSIZE|PARENB);
1042 tty.c_cflag |= CS8;
1043 tty.c_cc[VMIN] = 1;
1044 tty.c_cc[VTIME] = 0;
1045 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001046 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001047 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001048
1049 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001050}
1051
1052static void qemu_chr_close_stdio(struct CharDriverState *chr)
1053{
1054 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001055 fd_chr_close(chr);
1056}
1057
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001058static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001059{
1060 CharDriverState *chr;
1061
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001062 if (is_daemonized()) {
1063 error_report("cannot use stdio with -daemonize");
1064 return NULL;
1065 }
Li Liuc88930a2014-09-09 19:19:48 +08001066
1067 if (stdio_in_use) {
1068 error_report("cannot use stdio by multiple character devices");
1069 exit(1);
1070 }
1071
1072 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301073 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001074 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001075 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301076 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001077
aliguori6f97dba2008-10-31 18:49:55 +00001078 chr = qemu_chr_open_fd(0, 1);
1079 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001080 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001081 if (opts->has_signal) {
1082 stdio_allow_signal = opts->signal;
1083 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001084 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001085
Markus Armbruster1f514702012-02-07 15:09:08 +01001086 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001087}
1088
aliguori6f97dba2008-10-31 18:49:55 +00001089#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001090 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1091 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001092
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001093#define HAVE_CHARDEV_TTY 1
1094
aliguori6f97dba2008-10-31 18:49:55 +00001095typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301096 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001097 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001098
1099 /* Protected by the CharDriverState chr_write_lock. */
1100 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301101 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001102 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001103} PtyCharDriver;
1104
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001105static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001106static void pty_chr_state(CharDriverState *chr, int connected);
1107
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301108static gboolean pty_chr_timer(gpointer opaque)
1109{
1110 struct CharDriverState *chr = opaque;
1111 PtyCharDriver *s = chr->opaque;
1112
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001113 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001114 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001115 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001116 if (!s->connected) {
1117 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001118 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001119 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001120 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301121 return FALSE;
1122}
1123
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001124/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301125static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1126{
1127 PtyCharDriver *s = chr->opaque;
1128
1129 if (s->timer_tag) {
1130 g_source_remove(s->timer_tag);
1131 s->timer_tag = 0;
1132 }
1133
1134 if (ms == 1000) {
1135 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1136 } else {
1137 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1138 }
1139}
1140
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001141/* Called with chr_write_lock held. */
1142static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001143{
1144 PtyCharDriver *s = chr->opaque;
1145 GPollFD pfd;
1146
1147 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1148 pfd.events = G_IO_OUT;
1149 pfd.revents = 0;
1150 g_poll(&pfd, 1, 0);
1151 if (pfd.revents & G_IO_HUP) {
1152 pty_chr_state(chr, 0);
1153 } else {
1154 pty_chr_state(chr, 1);
1155 }
1156}
1157
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001158static void pty_chr_update_read_handler(CharDriverState *chr)
1159{
1160 qemu_mutex_lock(&chr->chr_write_lock);
1161 pty_chr_update_read_handler_locked(chr);
1162 qemu_mutex_unlock(&chr->chr_write_lock);
1163}
1164
1165/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001166static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1167{
1168 PtyCharDriver *s = chr->opaque;
1169
1170 if (!s->connected) {
1171 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001172 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001173 if (!s->connected) {
1174 return 0;
1175 }
aliguori6f97dba2008-10-31 18:49:55 +00001176 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001177 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001178}
1179
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301180static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1181{
1182 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001183 if (!s->connected) {
1184 return NULL;
1185 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301186 return g_io_create_watch(s->fd, cond);
1187}
1188
aliguori6f97dba2008-10-31 18:49:55 +00001189static int pty_chr_read_poll(void *opaque)
1190{
1191 CharDriverState *chr = opaque;
1192 PtyCharDriver *s = chr->opaque;
1193
Anthony Liguori909cda12011-08-15 11:17:31 -05001194 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001195 return s->read_bytes;
1196}
1197
Anthony Liguori093d3a22013-03-05 23:21:20 +05301198static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001199{
1200 CharDriverState *chr = opaque;
1201 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301202 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301203 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301204 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001205
1206 len = sizeof(buf);
1207 if (len > s->read_bytes)
1208 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001209 if (len == 0) {
1210 return TRUE;
1211 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301212 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1213 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001214 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301215 return FALSE;
1216 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001217 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001218 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001219 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301220 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001221}
1222
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001223static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1224{
1225 CharDriverState *chr = opaque;
1226 PtyCharDriver *s = chr->opaque;
1227
1228 s->open_tag = 0;
1229 qemu_chr_be_generic_open(chr);
1230 return FALSE;
1231}
1232
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001233/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001234static void pty_chr_state(CharDriverState *chr, int connected)
1235{
1236 PtyCharDriver *s = chr->opaque;
1237
1238 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001239 if (s->open_tag) {
1240 g_source_remove(s->open_tag);
1241 s->open_tag = 0;
1242 }
Amit Shah26da70c2013-08-28 15:23:37 +05301243 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001244 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001245 /* (re-)connect poll interval for idle guests: once per second.
1246 * We check more frequently in case the guests sends data to
1247 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301248 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001249 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001250 if (s->timer_tag) {
1251 g_source_remove(s->timer_tag);
1252 s->timer_tag = 0;
1253 }
1254 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001255 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001256 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001257 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001258 }
1259 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301260 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1261 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001262 }
aliguori6f97dba2008-10-31 18:49:55 +00001263 }
1264}
1265
aliguori6f97dba2008-10-31 18:49:55 +00001266static void pty_chr_close(struct CharDriverState *chr)
1267{
1268 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301269 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001270
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001271 qemu_mutex_lock(&chr->chr_write_lock);
1272 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301273 fd = g_io_channel_unix_get_fd(s->fd);
1274 g_io_channel_unref(s->fd);
1275 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301276 if (s->timer_tag) {
1277 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001278 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301279 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001280 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001281 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001282 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001283}
1284
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001285static CharDriverState *qemu_chr_open_pty(const char *id,
1286 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001287{
1288 CharDriverState *chr;
1289 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001290 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001291 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001292
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001293 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1294 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001295 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001296 }
1297
aliguori6f97dba2008-10-31 18:49:55 +00001298 close(slave_fd);
1299
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001300 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001301
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001302 chr->filename = g_strdup_printf("pty:%s", pty_name);
1303 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001304 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001305
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001306 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001307 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001308
1309 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001310 chr->opaque = s;
1311 chr->chr_write = pty_chr_write;
1312 chr->chr_update_read_handler = pty_chr_update_read_handler;
1313 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301314 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001315 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001316
Anthony Liguori093d3a22013-03-05 23:21:20 +05301317 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301318 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001319
Markus Armbruster1f514702012-02-07 15:09:08 +01001320 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001321}
1322
1323static void tty_serial_init(int fd, int speed,
1324 int parity, int data_bits, int stop_bits)
1325{
1326 struct termios tty;
1327 speed_t spd;
1328
1329#if 0
1330 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1331 speed, parity, data_bits, stop_bits);
1332#endif
1333 tcgetattr (fd, &tty);
1334
Stefan Weil45eea132009-10-26 16:10:10 +01001335#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1336 speed = speed * 10 / 11;
1337 do {
1338 check_speed(50);
1339 check_speed(75);
1340 check_speed(110);
1341 check_speed(134);
1342 check_speed(150);
1343 check_speed(200);
1344 check_speed(300);
1345 check_speed(600);
1346 check_speed(1200);
1347 check_speed(1800);
1348 check_speed(2400);
1349 check_speed(4800);
1350 check_speed(9600);
1351 check_speed(19200);
1352 check_speed(38400);
1353 /* Non-Posix values follow. They may be unsupported on some systems. */
1354 check_speed(57600);
1355 check_speed(115200);
1356#ifdef B230400
1357 check_speed(230400);
1358#endif
1359#ifdef B460800
1360 check_speed(460800);
1361#endif
1362#ifdef B500000
1363 check_speed(500000);
1364#endif
1365#ifdef B576000
1366 check_speed(576000);
1367#endif
1368#ifdef B921600
1369 check_speed(921600);
1370#endif
1371#ifdef B1000000
1372 check_speed(1000000);
1373#endif
1374#ifdef B1152000
1375 check_speed(1152000);
1376#endif
1377#ifdef B1500000
1378 check_speed(1500000);
1379#endif
1380#ifdef B2000000
1381 check_speed(2000000);
1382#endif
1383#ifdef B2500000
1384 check_speed(2500000);
1385#endif
1386#ifdef B3000000
1387 check_speed(3000000);
1388#endif
1389#ifdef B3500000
1390 check_speed(3500000);
1391#endif
1392#ifdef B4000000
1393 check_speed(4000000);
1394#endif
aliguori6f97dba2008-10-31 18:49:55 +00001395 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001396 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001397
1398 cfsetispeed(&tty, spd);
1399 cfsetospeed(&tty, spd);
1400
1401 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1402 |INLCR|IGNCR|ICRNL|IXON);
1403 tty.c_oflag |= OPOST;
1404 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1405 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1406 switch(data_bits) {
1407 default:
1408 case 8:
1409 tty.c_cflag |= CS8;
1410 break;
1411 case 7:
1412 tty.c_cflag |= CS7;
1413 break;
1414 case 6:
1415 tty.c_cflag |= CS6;
1416 break;
1417 case 5:
1418 tty.c_cflag |= CS5;
1419 break;
1420 }
1421 switch(parity) {
1422 default:
1423 case 'N':
1424 break;
1425 case 'E':
1426 tty.c_cflag |= PARENB;
1427 break;
1428 case 'O':
1429 tty.c_cflag |= PARENB | PARODD;
1430 break;
1431 }
1432 if (stop_bits == 2)
1433 tty.c_cflag |= CSTOPB;
1434
1435 tcsetattr (fd, TCSANOW, &tty);
1436}
1437
1438static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1439{
1440 FDCharDriver *s = chr->opaque;
1441
1442 switch(cmd) {
1443 case CHR_IOCTL_SERIAL_SET_PARAMS:
1444 {
1445 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301446 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1447 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001448 ssp->data_bits, ssp->stop_bits);
1449 }
1450 break;
1451 case CHR_IOCTL_SERIAL_SET_BREAK:
1452 {
1453 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301454 if (enable) {
1455 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1456 }
aliguori6f97dba2008-10-31 18:49:55 +00001457 }
1458 break;
1459 case CHR_IOCTL_SERIAL_GET_TIOCM:
1460 {
1461 int sarg = 0;
1462 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301463 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001464 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001465 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001466 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001467 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001468 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001469 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001470 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001471 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001472 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001473 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001474 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001475 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001476 *targ |= CHR_TIOCM_RTS;
1477 }
1478 break;
1479 case CHR_IOCTL_SERIAL_SET_TIOCM:
1480 {
1481 int sarg = *(int *)arg;
1482 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301483 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001484 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1485 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1486 if (sarg & CHR_TIOCM_CTS)
1487 targ |= TIOCM_CTS;
1488 if (sarg & CHR_TIOCM_CAR)
1489 targ |= TIOCM_CAR;
1490 if (sarg & CHR_TIOCM_DSR)
1491 targ |= TIOCM_DSR;
1492 if (sarg & CHR_TIOCM_RI)
1493 targ |= TIOCM_RI;
1494 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001495 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001496 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001497 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301498 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001499 }
1500 break;
1501 default:
1502 return -ENOTSUP;
1503 }
1504 return 0;
1505}
1506
David Ahern4266a132010-02-10 18:27:17 -07001507static void qemu_chr_close_tty(CharDriverState *chr)
1508{
1509 FDCharDriver *s = chr->opaque;
1510 int fd = -1;
1511
1512 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301513 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001514 }
1515
1516 fd_chr_close(chr);
1517
1518 if (fd >= 0) {
1519 close(fd);
1520 }
1521}
1522
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001523static CharDriverState *qemu_chr_open_tty_fd(int fd)
1524{
1525 CharDriverState *chr;
1526
1527 tty_serial_init(fd, 115200, 'N', 8, 1);
1528 chr = qemu_chr_open_fd(fd, fd);
1529 chr->chr_ioctl = tty_serial_ioctl;
1530 chr->chr_close = qemu_chr_close_tty;
1531 return chr;
1532}
aliguori6f97dba2008-10-31 18:49:55 +00001533#endif /* __linux__ || __sun__ */
1534
1535#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001536
1537#define HAVE_CHARDEV_PARPORT 1
1538
aliguori6f97dba2008-10-31 18:49:55 +00001539typedef struct {
1540 int fd;
1541 int mode;
1542} ParallelCharDriver;
1543
1544static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1545{
1546 if (s->mode != mode) {
1547 int m = mode;
1548 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1549 return 0;
1550 s->mode = mode;
1551 }
1552 return 1;
1553}
1554
1555static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1556{
1557 ParallelCharDriver *drv = chr->opaque;
1558 int fd = drv->fd;
1559 uint8_t b;
1560
1561 switch(cmd) {
1562 case CHR_IOCTL_PP_READ_DATA:
1563 if (ioctl(fd, PPRDATA, &b) < 0)
1564 return -ENOTSUP;
1565 *(uint8_t *)arg = b;
1566 break;
1567 case CHR_IOCTL_PP_WRITE_DATA:
1568 b = *(uint8_t *)arg;
1569 if (ioctl(fd, PPWDATA, &b) < 0)
1570 return -ENOTSUP;
1571 break;
1572 case CHR_IOCTL_PP_READ_CONTROL:
1573 if (ioctl(fd, PPRCONTROL, &b) < 0)
1574 return -ENOTSUP;
1575 /* Linux gives only the lowest bits, and no way to know data
1576 direction! For better compatibility set the fixed upper
1577 bits. */
1578 *(uint8_t *)arg = b | 0xc0;
1579 break;
1580 case CHR_IOCTL_PP_WRITE_CONTROL:
1581 b = *(uint8_t *)arg;
1582 if (ioctl(fd, PPWCONTROL, &b) < 0)
1583 return -ENOTSUP;
1584 break;
1585 case CHR_IOCTL_PP_READ_STATUS:
1586 if (ioctl(fd, PPRSTATUS, &b) < 0)
1587 return -ENOTSUP;
1588 *(uint8_t *)arg = b;
1589 break;
1590 case CHR_IOCTL_PP_DATA_DIR:
1591 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1592 return -ENOTSUP;
1593 break;
1594 case CHR_IOCTL_PP_EPP_READ_ADDR:
1595 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1596 struct ParallelIOArg *parg = arg;
1597 int n = read(fd, parg->buffer, parg->count);
1598 if (n != parg->count) {
1599 return -EIO;
1600 }
1601 }
1602 break;
1603 case CHR_IOCTL_PP_EPP_READ:
1604 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1605 struct ParallelIOArg *parg = arg;
1606 int n = read(fd, parg->buffer, parg->count);
1607 if (n != parg->count) {
1608 return -EIO;
1609 }
1610 }
1611 break;
1612 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1613 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1614 struct ParallelIOArg *parg = arg;
1615 int n = write(fd, parg->buffer, parg->count);
1616 if (n != parg->count) {
1617 return -EIO;
1618 }
1619 }
1620 break;
1621 case CHR_IOCTL_PP_EPP_WRITE:
1622 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1623 struct ParallelIOArg *parg = arg;
1624 int n = write(fd, parg->buffer, parg->count);
1625 if (n != parg->count) {
1626 return -EIO;
1627 }
1628 }
1629 break;
1630 default:
1631 return -ENOTSUP;
1632 }
1633 return 0;
1634}
1635
1636static void pp_close(CharDriverState *chr)
1637{
1638 ParallelCharDriver *drv = chr->opaque;
1639 int fd = drv->fd;
1640
1641 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1642 ioctl(fd, PPRELEASE);
1643 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001644 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001645 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001646}
1647
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001648static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001649{
1650 CharDriverState *chr;
1651 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001652
1653 if (ioctl(fd, PPCLAIM) < 0) {
1654 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001655 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001656 }
1657
Anthony Liguori7267c092011-08-20 22:09:37 -05001658 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001659 drv->fd = fd;
1660 drv->mode = IEEE1284_MODE_COMPAT;
1661
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001662 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001663 chr->chr_write = null_chr_write;
1664 chr->chr_ioctl = pp_ioctl;
1665 chr->chr_close = pp_close;
1666 chr->opaque = drv;
1667
Markus Armbruster1f514702012-02-07 15:09:08 +01001668 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001669}
1670#endif /* __linux__ */
1671
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001672#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001673
1674#define HAVE_CHARDEV_PARPORT 1
1675
blueswir16972f932008-11-22 20:49:12 +00001676static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1677{
Stefan Weile0efb992011-02-23 19:09:16 +01001678 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001679 uint8_t b;
1680
1681 switch(cmd) {
1682 case CHR_IOCTL_PP_READ_DATA:
1683 if (ioctl(fd, PPIGDATA, &b) < 0)
1684 return -ENOTSUP;
1685 *(uint8_t *)arg = b;
1686 break;
1687 case CHR_IOCTL_PP_WRITE_DATA:
1688 b = *(uint8_t *)arg;
1689 if (ioctl(fd, PPISDATA, &b) < 0)
1690 return -ENOTSUP;
1691 break;
1692 case CHR_IOCTL_PP_READ_CONTROL:
1693 if (ioctl(fd, PPIGCTRL, &b) < 0)
1694 return -ENOTSUP;
1695 *(uint8_t *)arg = b;
1696 break;
1697 case CHR_IOCTL_PP_WRITE_CONTROL:
1698 b = *(uint8_t *)arg;
1699 if (ioctl(fd, PPISCTRL, &b) < 0)
1700 return -ENOTSUP;
1701 break;
1702 case CHR_IOCTL_PP_READ_STATUS:
1703 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1704 return -ENOTSUP;
1705 *(uint8_t *)arg = b;
1706 break;
1707 default:
1708 return -ENOTSUP;
1709 }
1710 return 0;
1711}
1712
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001713static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001714{
1715 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001716
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001717 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001718 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001719 chr->chr_write = null_chr_write;
1720 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001721 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001722 return chr;
blueswir16972f932008-11-22 20:49:12 +00001723}
1724#endif
1725
aliguori6f97dba2008-10-31 18:49:55 +00001726#else /* _WIN32 */
1727
1728typedef struct {
1729 int max_size;
1730 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001731 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001732 BOOL fpipe;
1733 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001734
1735 /* Protected by the CharDriverState chr_write_lock. */
1736 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001737} WinCharState;
1738
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001739typedef struct {
1740 HANDLE hStdIn;
1741 HANDLE hInputReadyEvent;
1742 HANDLE hInputDoneEvent;
1743 HANDLE hInputThread;
1744 uint8_t win_stdio_buf;
1745} WinStdioCharState;
1746
aliguori6f97dba2008-10-31 18:49:55 +00001747#define NSENDBUF 2048
1748#define NRECVBUF 2048
1749#define MAXCONNECT 1
1750#define NTIMEOUT 5000
1751
1752static int win_chr_poll(void *opaque);
1753static int win_chr_pipe_poll(void *opaque);
1754
1755static void win_chr_close(CharDriverState *chr)
1756{
1757 WinCharState *s = chr->opaque;
1758
1759 if (s->hsend) {
1760 CloseHandle(s->hsend);
1761 s->hsend = NULL;
1762 }
1763 if (s->hrecv) {
1764 CloseHandle(s->hrecv);
1765 s->hrecv = NULL;
1766 }
1767 if (s->hcom) {
1768 CloseHandle(s->hcom);
1769 s->hcom = NULL;
1770 }
1771 if (s->fpipe)
1772 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1773 else
1774 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301775
Hans de Goedea425d232011-11-19 10:22:43 +01001776 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001777}
1778
1779static int win_chr_init(CharDriverState *chr, const char *filename)
1780{
1781 WinCharState *s = chr->opaque;
1782 COMMCONFIG comcfg;
1783 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1784 COMSTAT comstat;
1785 DWORD size;
1786 DWORD err;
1787
1788 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1789 if (!s->hsend) {
1790 fprintf(stderr, "Failed CreateEvent\n");
1791 goto fail;
1792 }
1793 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1794 if (!s->hrecv) {
1795 fprintf(stderr, "Failed CreateEvent\n");
1796 goto fail;
1797 }
1798
1799 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1800 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1801 if (s->hcom == INVALID_HANDLE_VALUE) {
1802 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1803 s->hcom = NULL;
1804 goto fail;
1805 }
1806
1807 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1808 fprintf(stderr, "Failed SetupComm\n");
1809 goto fail;
1810 }
1811
1812 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1813 size = sizeof(COMMCONFIG);
1814 GetDefaultCommConfig(filename, &comcfg, &size);
1815 comcfg.dcb.DCBlength = sizeof(DCB);
1816 CommConfigDialog(filename, NULL, &comcfg);
1817
1818 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1819 fprintf(stderr, "Failed SetCommState\n");
1820 goto fail;
1821 }
1822
1823 if (!SetCommMask(s->hcom, EV_ERR)) {
1824 fprintf(stderr, "Failed SetCommMask\n");
1825 goto fail;
1826 }
1827
1828 cto.ReadIntervalTimeout = MAXDWORD;
1829 if (!SetCommTimeouts(s->hcom, &cto)) {
1830 fprintf(stderr, "Failed SetCommTimeouts\n");
1831 goto fail;
1832 }
1833
1834 if (!ClearCommError(s->hcom, &err, &comstat)) {
1835 fprintf(stderr, "Failed ClearCommError\n");
1836 goto fail;
1837 }
1838 qemu_add_polling_cb(win_chr_poll, chr);
1839 return 0;
1840
1841 fail:
1842 win_chr_close(chr);
1843 return -1;
1844}
1845
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001846/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001847static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1848{
1849 WinCharState *s = chr->opaque;
1850 DWORD len, ret, size, err;
1851
1852 len = len1;
1853 ZeroMemory(&s->osend, sizeof(s->osend));
1854 s->osend.hEvent = s->hsend;
1855 while (len > 0) {
1856 if (s->hsend)
1857 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1858 else
1859 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1860 if (!ret) {
1861 err = GetLastError();
1862 if (err == ERROR_IO_PENDING) {
1863 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1864 if (ret) {
1865 buf += size;
1866 len -= size;
1867 } else {
1868 break;
1869 }
1870 } else {
1871 break;
1872 }
1873 } else {
1874 buf += size;
1875 len -= size;
1876 }
1877 }
1878 return len1 - len;
1879}
1880
1881static int win_chr_read_poll(CharDriverState *chr)
1882{
1883 WinCharState *s = chr->opaque;
1884
Anthony Liguori909cda12011-08-15 11:17:31 -05001885 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001886 return s->max_size;
1887}
1888
1889static void win_chr_readfile(CharDriverState *chr)
1890{
1891 WinCharState *s = chr->opaque;
1892 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301893 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001894 DWORD size;
1895
1896 ZeroMemory(&s->orecv, sizeof(s->orecv));
1897 s->orecv.hEvent = s->hrecv;
1898 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1899 if (!ret) {
1900 err = GetLastError();
1901 if (err == ERROR_IO_PENDING) {
1902 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1903 }
1904 }
1905
1906 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001907 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001908 }
1909}
1910
1911static void win_chr_read(CharDriverState *chr)
1912{
1913 WinCharState *s = chr->opaque;
1914
1915 if (s->len > s->max_size)
1916 s->len = s->max_size;
1917 if (s->len == 0)
1918 return;
1919
1920 win_chr_readfile(chr);
1921}
1922
1923static int win_chr_poll(void *opaque)
1924{
1925 CharDriverState *chr = opaque;
1926 WinCharState *s = chr->opaque;
1927 COMSTAT status;
1928 DWORD comerr;
1929
1930 ClearCommError(s->hcom, &comerr, &status);
1931 if (status.cbInQue > 0) {
1932 s->len = status.cbInQue;
1933 win_chr_read_poll(chr);
1934 win_chr_read(chr);
1935 return 1;
1936 }
1937 return 0;
1938}
1939
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001940static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001941{
1942 CharDriverState *chr;
1943 WinCharState *s;
1944
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001945 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001946 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001947 chr->opaque = s;
1948 chr->chr_write = win_chr_write;
1949 chr->chr_close = win_chr_close;
1950
1951 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001952 g_free(s);
1953 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001954 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001955 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001956 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001957}
1958
1959static int win_chr_pipe_poll(void *opaque)
1960{
1961 CharDriverState *chr = opaque;
1962 WinCharState *s = chr->opaque;
1963 DWORD size;
1964
1965 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1966 if (size > 0) {
1967 s->len = size;
1968 win_chr_read_poll(chr);
1969 win_chr_read(chr);
1970 return 1;
1971 }
1972 return 0;
1973}
1974
1975static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1976{
1977 WinCharState *s = chr->opaque;
1978 OVERLAPPED ov;
1979 int ret;
1980 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05001981 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00001982
1983 s->fpipe = TRUE;
1984
1985 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1986 if (!s->hsend) {
1987 fprintf(stderr, "Failed CreateEvent\n");
1988 goto fail;
1989 }
1990 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1991 if (!s->hrecv) {
1992 fprintf(stderr, "Failed CreateEvent\n");
1993 goto fail;
1994 }
1995
1996 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1997 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1998 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1999 PIPE_WAIT,
2000 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2001 if (s->hcom == INVALID_HANDLE_VALUE) {
2002 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2003 s->hcom = NULL;
2004 goto fail;
2005 }
2006
2007 ZeroMemory(&ov, sizeof(ov));
2008 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2009 ret = ConnectNamedPipe(s->hcom, &ov);
2010 if (ret) {
2011 fprintf(stderr, "Failed ConnectNamedPipe\n");
2012 goto fail;
2013 }
2014
2015 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2016 if (!ret) {
2017 fprintf(stderr, "Failed GetOverlappedResult\n");
2018 if (ov.hEvent) {
2019 CloseHandle(ov.hEvent);
2020 ov.hEvent = NULL;
2021 }
2022 goto fail;
2023 }
2024
2025 if (ov.hEvent) {
2026 CloseHandle(ov.hEvent);
2027 ov.hEvent = NULL;
2028 }
2029 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2030 return 0;
2031
2032 fail:
2033 win_chr_close(chr);
2034 return -1;
2035}
2036
2037
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002038static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002039{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002040 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002041 CharDriverState *chr;
2042 WinCharState *s;
2043
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002044 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002045 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002046 chr->opaque = s;
2047 chr->chr_write = win_chr_write;
2048 chr->chr_close = win_chr_close;
2049
2050 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002051 g_free(s);
2052 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002053 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002054 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002055 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002056}
2057
Markus Armbruster1f514702012-02-07 15:09:08 +01002058static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002059{
2060 CharDriverState *chr;
2061 WinCharState *s;
2062
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002063 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002064 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002065 s->hcom = fd_out;
2066 chr->opaque = s;
2067 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002068 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002069}
2070
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002071static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002072{
Markus Armbruster1f514702012-02-07 15:09:08 +01002073 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002074}
2075
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002076static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2077{
2078 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2079 DWORD dwSize;
2080 int len1;
2081
2082 len1 = len;
2083
2084 while (len1 > 0) {
2085 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2086 break;
2087 }
2088 buf += dwSize;
2089 len1 -= dwSize;
2090 }
2091
2092 return len - len1;
2093}
2094
2095static void win_stdio_wait_func(void *opaque)
2096{
2097 CharDriverState *chr = opaque;
2098 WinStdioCharState *stdio = chr->opaque;
2099 INPUT_RECORD buf[4];
2100 int ret;
2101 DWORD dwSize;
2102 int i;
2103
Stefan Weildff74242013-12-07 14:48:04 +01002104 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002105
2106 if (!ret) {
2107 /* Avoid error storm */
2108 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2109 return;
2110 }
2111
2112 for (i = 0; i < dwSize; i++) {
2113 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2114
2115 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2116 int j;
2117 if (kev->uChar.AsciiChar != 0) {
2118 for (j = 0; j < kev->wRepeatCount; j++) {
2119 if (qemu_chr_be_can_write(chr)) {
2120 uint8_t c = kev->uChar.AsciiChar;
2121 qemu_chr_be_write(chr, &c, 1);
2122 }
2123 }
2124 }
2125 }
2126 }
2127}
2128
2129static DWORD WINAPI win_stdio_thread(LPVOID param)
2130{
2131 CharDriverState *chr = param;
2132 WinStdioCharState *stdio = chr->opaque;
2133 int ret;
2134 DWORD dwSize;
2135
2136 while (1) {
2137
2138 /* Wait for one byte */
2139 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2140
2141 /* Exit in case of error, continue if nothing read */
2142 if (!ret) {
2143 break;
2144 }
2145 if (!dwSize) {
2146 continue;
2147 }
2148
2149 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2150 if (stdio->win_stdio_buf == '\r') {
2151 continue;
2152 }
2153
2154 /* Signal the main thread and wait until the byte was eaten */
2155 if (!SetEvent(stdio->hInputReadyEvent)) {
2156 break;
2157 }
2158 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2159 != WAIT_OBJECT_0) {
2160 break;
2161 }
2162 }
2163
2164 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2165 return 0;
2166}
2167
2168static void win_stdio_thread_wait_func(void *opaque)
2169{
2170 CharDriverState *chr = opaque;
2171 WinStdioCharState *stdio = chr->opaque;
2172
2173 if (qemu_chr_be_can_write(chr)) {
2174 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2175 }
2176
2177 SetEvent(stdio->hInputDoneEvent);
2178}
2179
2180static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2181{
2182 WinStdioCharState *stdio = chr->opaque;
2183 DWORD dwMode = 0;
2184
2185 GetConsoleMode(stdio->hStdIn, &dwMode);
2186
2187 if (echo) {
2188 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2189 } else {
2190 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2191 }
2192}
2193
2194static void win_stdio_close(CharDriverState *chr)
2195{
2196 WinStdioCharState *stdio = chr->opaque;
2197
2198 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2199 CloseHandle(stdio->hInputReadyEvent);
2200 }
2201 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2202 CloseHandle(stdio->hInputDoneEvent);
2203 }
2204 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2205 TerminateThread(stdio->hInputThread, 0);
2206 }
2207
2208 g_free(chr->opaque);
2209 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002210}
2211
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002212static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002213{
2214 CharDriverState *chr;
2215 WinStdioCharState *stdio;
2216 DWORD dwMode;
2217 int is_console = 0;
2218
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002219 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002220 stdio = g_malloc0(sizeof(WinStdioCharState));
2221
2222 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2223 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2224 fprintf(stderr, "cannot open stdio: invalid handle\n");
2225 exit(1);
2226 }
2227
2228 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2229
2230 chr->opaque = stdio;
2231 chr->chr_write = win_stdio_write;
2232 chr->chr_close = win_stdio_close;
2233
Anthony Liguoried7a1542013-03-05 23:21:17 +05302234 if (is_console) {
2235 if (qemu_add_wait_object(stdio->hStdIn,
2236 win_stdio_wait_func, chr)) {
2237 fprintf(stderr, "qemu_add_wait_object: failed\n");
2238 }
2239 } else {
2240 DWORD dwId;
2241
2242 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2243 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2244 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2245 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002246
Anthony Liguoried7a1542013-03-05 23:21:17 +05302247 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2248 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2249 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2250 fprintf(stderr, "cannot create stdio thread or event\n");
2251 exit(1);
2252 }
2253 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2254 win_stdio_thread_wait_func, chr)) {
2255 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002256 }
2257 }
2258
2259 dwMode |= ENABLE_LINE_INPUT;
2260
Anthony Liguoried7a1542013-03-05 23:21:17 +05302261 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002262 /* set the terminal in raw mode */
2263 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2264 dwMode |= ENABLE_PROCESSED_INPUT;
2265 }
2266
2267 SetConsoleMode(stdio->hStdIn, dwMode);
2268
2269 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2270 qemu_chr_fe_set_echo(chr, false);
2271
Markus Armbruster1f514702012-02-07 15:09:08 +01002272 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002273}
aliguori6f97dba2008-10-31 18:49:55 +00002274#endif /* !_WIN32 */
2275
Anthony Liguori5ab82112013-03-05 23:21:31 +05302276
aliguori6f97dba2008-10-31 18:49:55 +00002277/***********************************************************/
2278/* UDP Net console */
2279
2280typedef struct {
2281 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302282 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302283 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002284 int bufcnt;
2285 int bufptr;
2286 int max_size;
2287} NetCharDriver;
2288
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002289/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002290static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2291{
2292 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302293 gsize bytes_written;
2294 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002295
Anthony Liguori76a96442013-03-05 23:21:21 +05302296 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2297 if (status == G_IO_STATUS_EOF) {
2298 return 0;
2299 } else if (status != G_IO_STATUS_NORMAL) {
2300 return -1;
2301 }
2302
2303 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002304}
2305
2306static int udp_chr_read_poll(void *opaque)
2307{
2308 CharDriverState *chr = opaque;
2309 NetCharDriver *s = chr->opaque;
2310
Anthony Liguori909cda12011-08-15 11:17:31 -05002311 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002312
2313 /* If there were any stray characters in the queue process them
2314 * first
2315 */
2316 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002317 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002318 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002319 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002320 }
2321 return s->max_size;
2322}
2323
Anthony Liguori76a96442013-03-05 23:21:21 +05302324static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002325{
2326 CharDriverState *chr = opaque;
2327 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302328 gsize bytes_read = 0;
2329 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002330
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002331 if (s->max_size == 0) {
2332 return TRUE;
2333 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302334 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2335 &bytes_read, NULL);
2336 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002337 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302338 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302339 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302340 return FALSE;
2341 }
aliguori6f97dba2008-10-31 18:49:55 +00002342
2343 s->bufptr = 0;
2344 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002345 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002346 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002347 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002348 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302349
2350 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002351}
2352
2353static void udp_chr_update_read_handler(CharDriverState *chr)
2354{
2355 NetCharDriver *s = chr->opaque;
2356
Amit Shah26da70c2013-08-28 15:23:37 +05302357 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302358 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302359 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2360 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002361 }
2362}
2363
aliguori819f56b2009-03-28 17:58:14 +00002364static void udp_chr_close(CharDriverState *chr)
2365{
2366 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302367
2368 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302369 if (s->chan) {
2370 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002371 closesocket(s->fd);
2372 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002373 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002374 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002375}
2376
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002377static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002378{
2379 CharDriverState *chr = NULL;
2380 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002381
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002382 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002383 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002384
aliguori6f97dba2008-10-31 18:49:55 +00002385 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302386 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002387 s->bufcnt = 0;
2388 s->bufptr = 0;
2389 chr->opaque = s;
2390 chr->chr_write = udp_chr_write;
2391 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002392 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002393 /* be isn't opened until we get a connection */
2394 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002395 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002396}
aliguori6f97dba2008-10-31 18:49:55 +00002397
aliguori6f97dba2008-10-31 18:49:55 +00002398/***********************************************************/
2399/* TCP Net console */
2400
2401typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302402
2403 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302404 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002405 int fd, listen_fd;
2406 int connected;
2407 int max_size;
2408 int do_telnetopt;
2409 int do_nodelay;
2410 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002411 int *read_msgfds;
2412 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002413 int *write_msgfds;
2414 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002415} TCPCharDriver;
2416
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302417static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002418
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002419#ifndef _WIN32
2420static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2421{
2422 TCPCharDriver *s = chr->opaque;
2423 struct msghdr msgh;
2424 struct iovec iov;
2425 int r;
2426
2427 size_t fd_size = s->write_msgfds_num * sizeof(int);
2428 char control[CMSG_SPACE(fd_size)];
2429 struct cmsghdr *cmsg;
2430
2431 memset(&msgh, 0, sizeof(msgh));
2432 memset(control, 0, sizeof(control));
2433
2434 /* set the payload */
2435 iov.iov_base = (uint8_t *) buf;
2436 iov.iov_len = len;
2437
2438 msgh.msg_iov = &iov;
2439 msgh.msg_iovlen = 1;
2440
2441 msgh.msg_control = control;
2442 msgh.msg_controllen = sizeof(control);
2443
2444 cmsg = CMSG_FIRSTHDR(&msgh);
2445
2446 cmsg->cmsg_len = CMSG_LEN(fd_size);
2447 cmsg->cmsg_level = SOL_SOCKET;
2448 cmsg->cmsg_type = SCM_RIGHTS;
2449 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2450
2451 do {
2452 r = sendmsg(s->fd, &msgh, 0);
2453 } while (r < 0 && errno == EINTR);
2454
2455 /* free the written msgfds, no matter what */
2456 if (s->write_msgfds_num) {
2457 g_free(s->write_msgfds);
2458 s->write_msgfds = 0;
2459 s->write_msgfds_num = 0;
2460 }
2461
2462 return r;
2463}
2464#endif
2465
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002466/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002467static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2468{
2469 TCPCharDriver *s = chr->opaque;
2470 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002471#ifndef _WIN32
2472 if (s->is_unix && s->write_msgfds_num) {
2473 return unix_send_msgfds(chr, buf, len);
2474 } else
2475#endif
2476 {
2477 return io_channel_send(s->chan, buf, len);
2478 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002479 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002480 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002481 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002482 }
2483}
2484
2485static int tcp_chr_read_poll(void *opaque)
2486{
2487 CharDriverState *chr = opaque;
2488 TCPCharDriver *s = chr->opaque;
2489 if (!s->connected)
2490 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002491 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002492 return s->max_size;
2493}
2494
2495#define IAC 255
2496#define IAC_BREAK 243
2497static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2498 TCPCharDriver *s,
2499 uint8_t *buf, int *size)
2500{
2501 /* Handle any telnet client's basic IAC options to satisfy char by
2502 * char mode with no echo. All IAC options will be removed from
2503 * the buf and the do_telnetopt variable will be used to track the
2504 * state of the width of the IAC information.
2505 *
2506 * IAC commands come in sets of 3 bytes with the exception of the
2507 * "IAC BREAK" command and the double IAC.
2508 */
2509
2510 int i;
2511 int j = 0;
2512
2513 for (i = 0; i < *size; i++) {
2514 if (s->do_telnetopt > 1) {
2515 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2516 /* Double IAC means send an IAC */
2517 if (j != i)
2518 buf[j] = buf[i];
2519 j++;
2520 s->do_telnetopt = 1;
2521 } else {
2522 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2523 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002524 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002525 s->do_telnetopt++;
2526 }
2527 s->do_telnetopt++;
2528 }
2529 if (s->do_telnetopt >= 4) {
2530 s->do_telnetopt = 1;
2531 }
2532 } else {
2533 if ((unsigned char)buf[i] == IAC) {
2534 s->do_telnetopt = 2;
2535 } else {
2536 if (j != i)
2537 buf[j] = buf[i];
2538 j++;
2539 }
2540 }
2541 }
2542 *size = j;
2543}
2544
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002545static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002546{
2547 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002548 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2549
2550 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002551 int i;
2552
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002553 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2554
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002555 /* Close unused fds */
2556 for (i = to_copy; i < s->read_msgfds_num; i++) {
2557 close(s->read_msgfds[i]);
2558 }
2559
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002560 g_free(s->read_msgfds);
2561 s->read_msgfds = 0;
2562 s->read_msgfds_num = 0;
2563 }
2564
2565 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002566}
2567
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002568static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2569{
2570 TCPCharDriver *s = chr->opaque;
2571
2572 /* clear old pending fd array */
2573 if (s->write_msgfds) {
2574 g_free(s->write_msgfds);
2575 }
2576
2577 if (num) {
2578 s->write_msgfds = g_malloc(num * sizeof(int));
2579 memcpy(s->write_msgfds, fds, num * sizeof(int));
2580 }
2581
2582 s->write_msgfds_num = num;
2583
2584 return 0;
2585}
2586
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002587#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002588static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2589{
2590 TCPCharDriver *s = chr->opaque;
2591 struct cmsghdr *cmsg;
2592
2593 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002594 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002595
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002596 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002597 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002598 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002599 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002600 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002601
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002602 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2603
2604 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002605 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002606 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002607
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002608 /* close and clean read_msgfds */
2609 for (i = 0; i < s->read_msgfds_num; i++) {
2610 close(s->read_msgfds[i]);
2611 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002612
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002613 if (s->read_msgfds_num) {
2614 g_free(s->read_msgfds);
2615 }
2616
2617 s->read_msgfds_num = fd_size / sizeof(int);
2618 s->read_msgfds = g_malloc(fd_size);
2619 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2620
2621 for (i = 0; i < s->read_msgfds_num; i++) {
2622 int fd = s->read_msgfds[i];
2623 if (fd < 0) {
2624 continue;
2625 }
2626
2627 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2628 qemu_set_block(fd);
2629
2630 #ifndef MSG_CMSG_CLOEXEC
2631 qemu_set_cloexec(fd);
2632 #endif
2633 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002634 }
2635}
2636
Mark McLoughlin9977c892009-07-22 09:11:38 +01002637static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2638{
2639 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002640 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002641 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002642 union {
2643 struct cmsghdr cmsg;
2644 char control[CMSG_SPACE(sizeof(int))];
2645 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002646 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002647 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002648
2649 iov[0].iov_base = buf;
2650 iov[0].iov_len = len;
2651
2652 msg.msg_iov = iov;
2653 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002654 msg.msg_control = &msg_control;
2655 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002656
Corey Bryant06138652012-08-14 16:43:42 -04002657#ifdef MSG_CMSG_CLOEXEC
2658 flags |= MSG_CMSG_CLOEXEC;
2659#endif
2660 ret = recvmsg(s->fd, &msg, flags);
2661 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002662 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002663 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002664
2665 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002666}
2667#else
2668static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2669{
2670 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002671 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002672}
2673#endif
2674
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302675static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2676{
2677 TCPCharDriver *s = chr->opaque;
2678 return g_io_create_watch(s->chan, cond);
2679}
2680
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002681static void tcp_chr_disconnect(CharDriverState *chr)
2682{
2683 TCPCharDriver *s = chr->opaque;
2684
2685 s->connected = 0;
2686 if (s->listen_chan) {
2687 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2688 tcp_chr_accept, chr);
2689 }
2690 remove_fd_in_watch(chr);
2691 g_io_channel_unref(s->chan);
2692 s->chan = NULL;
2693 closesocket(s->fd);
2694 s->fd = -1;
2695 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2696}
2697
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302698static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002699{
2700 CharDriverState *chr = opaque;
2701 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302702 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002703 int len, size;
2704
Kirill Batuzov812c1052014-07-01 15:52:32 +04002705 if (cond & G_IO_HUP) {
2706 /* connection closed */
2707 tcp_chr_disconnect(chr);
2708 return TRUE;
2709 }
2710
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302711 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002712 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302713 }
aliguori6f97dba2008-10-31 18:49:55 +00002714 len = sizeof(buf);
2715 if (len > s->max_size)
2716 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002717 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002718 if (size == 0) {
2719 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002720 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002721 } else if (size > 0) {
2722 if (s->do_telnetopt)
2723 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2724 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002725 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002726 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302727
2728 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002729}
2730
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002731static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2732{
2733 TCPCharDriver *s = chr->opaque;
2734 int size;
2735
2736 if (!s->connected) {
2737 return 0;
2738 }
2739
2740 size = tcp_chr_recv(chr, (void *) buf, len);
2741 if (size == 0) {
2742 /* connection closed */
2743 tcp_chr_disconnect(chr);
2744 }
2745
2746 return size;
2747}
2748
Blue Swirl68c18d12010-08-15 09:46:24 +00002749#ifndef _WIN32
2750CharDriverState *qemu_chr_open_eventfd(int eventfd)
2751{
David Marchande9d21c42014-06-11 17:25:16 +02002752 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2753
2754 if (chr) {
2755 chr->avail_connections = 1;
2756 }
2757
2758 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002759}
Blue Swirl68c18d12010-08-15 09:46:24 +00002760#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002761
aliguori6f97dba2008-10-31 18:49:55 +00002762static void tcp_chr_connect(void *opaque)
2763{
2764 CharDriverState *chr = opaque;
2765 TCPCharDriver *s = chr->opaque;
2766
2767 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302768 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302769 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2770 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002771 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002772 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002773}
2774
Gal Hammerac1b84d2014-02-25 12:12:35 +02002775static void tcp_chr_update_read_handler(CharDriverState *chr)
2776{
2777 TCPCharDriver *s = chr->opaque;
2778
2779 remove_fd_in_watch(chr);
2780 if (s->chan) {
2781 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2782 tcp_chr_read, chr);
2783 }
2784}
2785
aliguori6f97dba2008-10-31 18:49:55 +00002786#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2787static void tcp_chr_telnet_init(int fd)
2788{
2789 char buf[3];
2790 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2791 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2792 send(fd, (char *)buf, 3, 0);
2793 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2794 send(fd, (char *)buf, 3, 0);
2795 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2796 send(fd, (char *)buf, 3, 0);
2797 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2798 send(fd, (char *)buf, 3, 0);
2799}
2800
Daniel P. Berrange13661082011-06-23 13:31:42 +01002801static int tcp_chr_add_client(CharDriverState *chr, int fd)
2802{
2803 TCPCharDriver *s = chr->opaque;
2804 if (s->fd != -1)
2805 return -1;
2806
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002807 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002808 if (s->do_nodelay)
2809 socket_set_nodelay(fd);
2810 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302811 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002812 if (s->listen_tag) {
2813 g_source_remove(s->listen_tag);
2814 s->listen_tag = 0;
2815 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002816 tcp_chr_connect(chr);
2817
2818 return 0;
2819}
2820
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302821static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002822{
2823 CharDriverState *chr = opaque;
2824 TCPCharDriver *s = chr->opaque;
2825 struct sockaddr_in saddr;
2826#ifndef _WIN32
2827 struct sockaddr_un uaddr;
2828#endif
2829 struct sockaddr *addr;
2830 socklen_t len;
2831 int fd;
2832
2833 for(;;) {
2834#ifndef _WIN32
2835 if (s->is_unix) {
2836 len = sizeof(uaddr);
2837 addr = (struct sockaddr *)&uaddr;
2838 } else
2839#endif
2840 {
2841 len = sizeof(saddr);
2842 addr = (struct sockaddr *)&saddr;
2843 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002844 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002845 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002846 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302847 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002848 } else if (fd >= 0) {
2849 if (s->do_telnetopt)
2850 tcp_chr_telnet_init(fd);
2851 break;
2852 }
2853 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002854 if (tcp_chr_add_client(chr, fd) < 0)
2855 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302856
2857 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002858}
2859
2860static void tcp_chr_close(CharDriverState *chr)
2861{
2862 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002863 int i;
aliguori819f56b2009-03-28 17:58:14 +00002864 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302865 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302866 if (s->chan) {
2867 g_io_channel_unref(s->chan);
2868 }
aliguori6f97dba2008-10-31 18:49:55 +00002869 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002870 }
2871 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302872 if (s->listen_tag) {
2873 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002874 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302875 }
2876 if (s->listen_chan) {
2877 g_io_channel_unref(s->listen_chan);
2878 }
aliguori6f97dba2008-10-31 18:49:55 +00002879 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002880 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002881 if (s->read_msgfds_num) {
2882 for (i = 0; i < s->read_msgfds_num; i++) {
2883 close(s->read_msgfds[i]);
2884 }
2885 g_free(s->read_msgfds);
2886 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002887 if (s->write_msgfds_num) {
2888 g_free(s->write_msgfds);
2889 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002890 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002891 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002892}
2893
Corey Minyard43ded1a2014-10-02 11:17:34 -05002894static bool qemu_chr_finish_socket_connection(CharDriverState *chr, int fd,
2895 bool is_listen, bool is_telnet,
2896 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002897{
Corey Minyard43ded1a2014-10-02 11:17:34 -05002898 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002899 char host[NI_MAXHOST], serv[NI_MAXSERV];
2900 const char *left = "", *right = "";
2901 struct sockaddr_storage ss;
2902 socklen_t ss_len = sizeof(ss);
2903
2904 memset(&ss, 0, ss_len);
2905 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05002906 closesocket(fd);
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002907 error_setg_errno(errp, errno, "getsockname");
Corey Minyard43ded1a2014-10-02 11:17:34 -05002908 return false;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002909 }
2910
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002911 switch (ss.ss_family) {
2912#ifndef _WIN32
2913 case AF_UNIX:
Corey Minyard9f781162014-10-02 11:17:33 -05002914 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE, "unix:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002915 ((struct sockaddr_un *)(&ss))->sun_path,
2916 is_listen ? ",server" : "");
2917 break;
2918#endif
2919 case AF_INET6:
2920 left = "[";
2921 right = "]";
2922 /* fall through */
2923 case AF_INET:
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002924 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2925 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Corey Minyard9f781162014-10-02 11:17:33 -05002926 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002927 is_telnet ? "telnet" : "tcp",
2928 left, host, right, serv,
2929 is_listen ? ",server" : "");
2930 break;
2931 }
2932
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002933 if (is_listen) {
2934 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302935 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05002936 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2937 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002938 } else {
2939 s->connected = 1;
2940 s->fd = fd;
2941 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302942 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002943 tcp_chr_connect(chr);
2944 }
2945
Corey Minyard43ded1a2014-10-02 11:17:34 -05002946 return true;
2947}
2948
2949static bool qemu_chr_open_socket_fd(CharDriverState *chr, SocketAddress *addr,
2950 bool is_listen, bool is_telnet,
2951 Error **errp)
2952{
2953 int fd;
2954
2955 if (is_listen) {
2956 fd = socket_listen(addr, errp);
2957 } else {
2958 fd = socket_connect(addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002959 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05002960 if (fd < 0) {
2961 return false;
2962 }
2963
2964 return qemu_chr_finish_socket_connection(chr, fd, is_listen, is_telnet,
2965 errp);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002966}
2967
Lei Li51767e72013-01-25 00:03:19 +08002968/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002969/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002970
2971typedef struct {
2972 size_t size;
2973 size_t prod;
2974 size_t cons;
2975 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002976} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002977
Markus Armbruster3949e592013-02-06 21:27:24 +01002978static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002979{
Markus Armbruster3949e592013-02-06 21:27:24 +01002980 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002981
Markus Armbruster5c230102013-02-06 21:27:23 +01002982 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002983}
2984
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002985/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01002986static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002987{
Markus Armbruster3949e592013-02-06 21:27:24 +01002988 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002989 int i;
2990
2991 if (!buf || (len < 0)) {
2992 return -1;
2993 }
2994
2995 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002996 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2997 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002998 d->cons = d->prod - d->size;
2999 }
3000 }
3001
3002 return 0;
3003}
3004
Markus Armbruster3949e592013-02-06 21:27:24 +01003005static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003006{
Markus Armbruster3949e592013-02-06 21:27:24 +01003007 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003008 int i;
3009
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003010 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003011 for (i = 0; i < len && d->cons != d->prod; i++) {
3012 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003013 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003014 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003015
3016 return i;
3017}
3018
Markus Armbruster3949e592013-02-06 21:27:24 +01003019static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003020{
Markus Armbruster3949e592013-02-06 21:27:24 +01003021 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003022
3023 g_free(d->cbuf);
3024 g_free(d);
3025 chr->opaque = NULL;
3026}
3027
Markus Armbruster4f573782013-07-26 16:44:32 +02003028static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3029 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003030{
3031 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003032 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003033
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003034 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003035 d = g_malloc(sizeof(*d));
3036
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003037 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003038
3039 /* The size must be power of 2 */
3040 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003041 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003042 goto fail;
3043 }
3044
3045 d->prod = 0;
3046 d->cons = 0;
3047 d->cbuf = g_malloc0(d->size);
3048
3049 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003050 chr->chr_write = ringbuf_chr_write;
3051 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003052
3053 return chr;
3054
3055fail:
3056 g_free(d);
3057 g_free(chr);
3058 return NULL;
3059}
3060
Hani Benhabiles8e597772014-05-27 23:39:30 +01003061bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003062{
Markus Armbruster3949e592013-02-06 21:27:24 +01003063 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003064}
3065
Markus Armbruster3949e592013-02-06 21:27:24 +01003066void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003067 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003068 Error **errp)
3069{
3070 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003071 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003072 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003073 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003074
3075 chr = qemu_chr_find(device);
3076 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003077 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003078 return;
3079 }
3080
Markus Armbruster3949e592013-02-06 21:27:24 +01003081 if (!chr_is_ringbuf(chr)) {
3082 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003083 return;
3084 }
3085
Lei Li1f590cf2013-01-25 00:03:20 +08003086 if (has_format && (format == DATA_FORMAT_BASE64)) {
3087 write_data = g_base64_decode(data, &write_count);
3088 } else {
3089 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003090 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003091 }
3092
Markus Armbruster3949e592013-02-06 21:27:24 +01003093 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003094
Markus Armbruster13289fb2013-02-06 21:27:18 +01003095 if (write_data != (uint8_t *)data) {
3096 g_free((void *)write_data);
3097 }
3098
Lei Li1f590cf2013-01-25 00:03:20 +08003099 if (ret < 0) {
3100 error_setg(errp, "Failed to write to device %s", device);
3101 return;
3102 }
3103}
3104
Markus Armbruster3949e592013-02-06 21:27:24 +01003105char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003106 bool has_format, enum DataFormat format,
3107 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003108{
3109 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003110 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003111 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003112 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003113
3114 chr = qemu_chr_find(device);
3115 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003116 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003117 return NULL;
3118 }
3119
Markus Armbruster3949e592013-02-06 21:27:24 +01003120 if (!chr_is_ringbuf(chr)) {
3121 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003122 return NULL;
3123 }
3124
3125 if (size <= 0) {
3126 error_setg(errp, "size must be greater than zero");
3127 return NULL;
3128 }
3129
Markus Armbruster3949e592013-02-06 21:27:24 +01003130 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003131 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003132 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003133
Markus Armbruster3949e592013-02-06 21:27:24 +01003134 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003135
3136 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003137 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003138 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003139 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003140 /*
3141 * FIXME should read only complete, valid UTF-8 characters up
3142 * to @size bytes. Invalid sequences should be replaced by a
3143 * suitable replacement character. Except when (and only
3144 * when) ring buffer lost characters since last read, initial
3145 * continuation characters should be dropped.
3146 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003147 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003148 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003149 }
3150
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003151 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003152}
3153
Gerd Hoffmann33521632009-12-08 13:11:49 +01003154QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003155{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003156 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003157 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003158 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003159 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003160 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003161
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003162 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003163 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003164 qerror_report_err(local_err);
3165 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003166 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003167 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003168
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003169 if (strstart(filename, "mon:", &p)) {
3170 filename = p;
3171 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003172 if (strcmp(filename, "stdio") == 0) {
3173 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3174 * but pass it to the guest. Handle this only for compat syntax,
3175 * for -chardev syntax we have special option for this.
3176 * This is what -nographic did, redirecting+muxing serial+monitor
3177 * to stdio causing Ctrl+C to be passed to guest. */
3178 qemu_opt_set(opts, "signal", "off");
3179 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003180 }
3181
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003182 if (strcmp(filename, "null") == 0 ||
3183 strcmp(filename, "pty") == 0 ||
3184 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003185 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003186 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003187 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003188 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003189 return opts;
3190 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003191 if (strstart(filename, "vc", &p)) {
3192 qemu_opt_set(opts, "backend", "vc");
3193 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003194 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003195 /* pixels */
3196 qemu_opt_set(opts, "width", width);
3197 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003198 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003199 /* chars */
3200 qemu_opt_set(opts, "cols", width);
3201 qemu_opt_set(opts, "rows", height);
3202 } else {
3203 goto fail;
3204 }
3205 }
3206 return opts;
3207 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003208 if (strcmp(filename, "con:") == 0) {
3209 qemu_opt_set(opts, "backend", "console");
3210 return opts;
3211 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003212 if (strstart(filename, "COM", NULL)) {
3213 qemu_opt_set(opts, "backend", "serial");
3214 qemu_opt_set(opts, "path", filename);
3215 return opts;
3216 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003217 if (strstart(filename, "file:", &p)) {
3218 qemu_opt_set(opts, "backend", "file");
3219 qemu_opt_set(opts, "path", p);
3220 return opts;
3221 }
3222 if (strstart(filename, "pipe:", &p)) {
3223 qemu_opt_set(opts, "backend", "pipe");
3224 qemu_opt_set(opts, "path", p);
3225 return opts;
3226 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003227 if (strstart(filename, "tcp:", &p) ||
3228 strstart(filename, "telnet:", &p)) {
3229 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3230 host[0] = 0;
3231 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3232 goto fail;
3233 }
3234 qemu_opt_set(opts, "backend", "socket");
3235 qemu_opt_set(opts, "host", host);
3236 qemu_opt_set(opts, "port", port);
3237 if (p[pos] == ',') {
3238 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3239 goto fail;
3240 }
3241 if (strstart(filename, "telnet:", &p))
3242 qemu_opt_set(opts, "telnet", "on");
3243 return opts;
3244 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003245 if (strstart(filename, "udp:", &p)) {
3246 qemu_opt_set(opts, "backend", "udp");
3247 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3248 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003249 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003250 goto fail;
3251 }
3252 }
3253 qemu_opt_set(opts, "host", host);
3254 qemu_opt_set(opts, "port", port);
3255 if (p[pos] == '@') {
3256 p += pos + 1;
3257 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3258 host[0] = 0;
3259 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003260 goto fail;
3261 }
3262 }
3263 qemu_opt_set(opts, "localaddr", host);
3264 qemu_opt_set(opts, "localport", port);
3265 }
3266 return opts;
3267 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003268 if (strstart(filename, "unix:", &p)) {
3269 qemu_opt_set(opts, "backend", "socket");
3270 if (qemu_opts_do_parse(opts, p, "path") != 0)
3271 goto fail;
3272 return opts;
3273 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003274 if (strstart(filename, "/dev/parport", NULL) ||
3275 strstart(filename, "/dev/ppi", NULL)) {
3276 qemu_opt_set(opts, "backend", "parport");
3277 qemu_opt_set(opts, "path", filename);
3278 return opts;
3279 }
3280 if (strstart(filename, "/dev/", NULL)) {
3281 qemu_opt_set(opts, "backend", "tty");
3282 qemu_opt_set(opts, "path", filename);
3283 return opts;
3284 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003285
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003286fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003287 qemu_opts_del(opts);
3288 return NULL;
3289}
3290
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003291static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3292 Error **errp)
3293{
3294 const char *path = qemu_opt_get(opts, "path");
3295
3296 if (path == NULL) {
3297 error_setg(errp, "chardev: file: no filename given");
3298 return;
3299 }
3300 backend->file = g_new0(ChardevFile, 1);
3301 backend->file->out = g_strdup(path);
3302}
3303
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003304static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3305 Error **errp)
3306{
3307 backend->stdio = g_new0(ChardevStdio, 1);
3308 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003309 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003310}
3311
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003312static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3313 Error **errp)
3314{
3315 const char *device = qemu_opt_get(opts, "path");
3316
3317 if (device == NULL) {
3318 error_setg(errp, "chardev: serial/tty: no device path given");
3319 return;
3320 }
3321 backend->serial = g_new0(ChardevHostdev, 1);
3322 backend->serial->device = g_strdup(device);
3323}
3324
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003325static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3326 Error **errp)
3327{
3328 const char *device = qemu_opt_get(opts, "path");
3329
3330 if (device == NULL) {
3331 error_setg(errp, "chardev: parallel: no device path given");
3332 return;
3333 }
3334 backend->parallel = g_new0(ChardevHostdev, 1);
3335 backend->parallel->device = g_strdup(device);
3336}
3337
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003338static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3339 Error **errp)
3340{
3341 const char *device = qemu_opt_get(opts, "path");
3342
3343 if (device == NULL) {
3344 error_setg(errp, "chardev: pipe: no device path given");
3345 return;
3346 }
3347 backend->pipe = g_new0(ChardevHostdev, 1);
3348 backend->pipe->device = g_strdup(device);
3349}
3350
Markus Armbruster4f573782013-07-26 16:44:32 +02003351static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3352 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003353{
3354 int val;
3355
Markus Armbruster3a1da422013-07-26 16:44:34 +02003356 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003357
Markus Armbruster0f953052013-06-27 16:22:07 +02003358 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003359 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003360 backend->ringbuf->has_size = true;
3361 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003362 }
3363}
3364
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003365static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3366 Error **errp)
3367{
3368 const char *chardev = qemu_opt_get(opts, "chardev");
3369
3370 if (chardev == NULL) {
3371 error_setg(errp, "chardev: mux: no chardev given");
3372 return;
3373 }
3374 backend->mux = g_new0(ChardevMux, 1);
3375 backend->mux->chardev = g_strdup(chardev);
3376}
3377
Peter Maydelldafd3252014-09-02 11:24:13 +01003378static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3379 Error **errp)
3380{
3381 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3382 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3383 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3384 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
3385 const char *path = qemu_opt_get(opts, "path");
3386 const char *host = qemu_opt_get(opts, "host");
3387 const char *port = qemu_opt_get(opts, "port");
3388 SocketAddress *addr;
3389
3390 if (!path) {
3391 if (!host) {
3392 error_setg(errp, "chardev: socket: no host given");
3393 return;
3394 }
3395 if (!port) {
3396 error_setg(errp, "chardev: socket: no port given");
3397 return;
3398 }
3399 }
3400
3401 backend->socket = g_new0(ChardevSocket, 1);
3402
3403 backend->socket->has_nodelay = true;
3404 backend->socket->nodelay = do_nodelay;
3405 backend->socket->has_server = true;
3406 backend->socket->server = is_listen;
3407 backend->socket->has_telnet = true;
3408 backend->socket->telnet = is_telnet;
3409 backend->socket->has_wait = true;
3410 backend->socket->wait = is_waitconnect;
3411
3412 addr = g_new0(SocketAddress, 1);
3413 if (path) {
3414 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3415 addr->q_unix = g_new0(UnixSocketAddress, 1);
3416 addr->q_unix->path = g_strdup(path);
3417 } else {
3418 addr->kind = SOCKET_ADDRESS_KIND_INET;
3419 addr->inet = g_new0(InetSocketAddress, 1);
3420 addr->inet->host = g_strdup(host);
3421 addr->inet->port = g_strdup(port);
3422 addr->inet->has_to = qemu_opt_get(opts, "to");
3423 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3424 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3425 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3426 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3427 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3428 }
3429 backend->socket->addr = addr;
3430}
3431
Peter Maydell90a14bf2014-09-02 11:24:15 +01003432static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3433 Error **errp)
3434{
3435 const char *host = qemu_opt_get(opts, "host");
3436 const char *port = qemu_opt_get(opts, "port");
3437 const char *localaddr = qemu_opt_get(opts, "localaddr");
3438 const char *localport = qemu_opt_get(opts, "localport");
3439 bool has_local = false;
3440 SocketAddress *addr;
3441
3442 if (host == NULL || strlen(host) == 0) {
3443 host = "localhost";
3444 }
3445 if (port == NULL || strlen(port) == 0) {
3446 error_setg(errp, "chardev: udp: remote port not specified");
3447 return;
3448 }
3449 if (localport == NULL || strlen(localport) == 0) {
3450 localport = "0";
3451 } else {
3452 has_local = true;
3453 }
3454 if (localaddr == NULL || strlen(localaddr) == 0) {
3455 localaddr = "";
3456 } else {
3457 has_local = true;
3458 }
3459
3460 backend->udp = g_new0(ChardevUdp, 1);
3461
3462 addr = g_new0(SocketAddress, 1);
3463 addr->kind = SOCKET_ADDRESS_KIND_INET;
3464 addr->inet = g_new0(InetSocketAddress, 1);
3465 addr->inet->host = g_strdup(host);
3466 addr->inet->port = g_strdup(port);
3467 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3468 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3469 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3470 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3471 backend->udp->remote = addr;
3472
3473 if (has_local) {
3474 backend->udp->has_local = true;
3475 addr = g_new0(SocketAddress, 1);
3476 addr->kind = SOCKET_ADDRESS_KIND_INET;
3477 addr->inet = g_new0(InetSocketAddress, 1);
3478 addr->inet->host = g_strdup(localaddr);
3479 addr->inet->port = g_strdup(localport);
3480 backend->udp->local = addr;
3481 }
3482}
3483
Anthony Liguorid654f342013-03-05 23:21:28 +05303484typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003485 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003486 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003487 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303488} CharDriver;
3489
3490static GSList *backends;
3491
Peter Maydelle4d50d42014-09-02 11:24:17 +01003492void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003493 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3494{
3495 CharDriver *s;
3496
3497 s = g_malloc0(sizeof(*s));
3498 s->name = g_strdup(name);
3499 s->kind = kind;
3500 s->parse = parse;
3501
3502 backends = g_slist_append(backends, s);
3503}
3504
Anthony Liguorif69554b2011-08-15 11:17:37 -05003505CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003506 void (*init)(struct CharDriverState *s),
3507 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003508{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003509 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303510 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003511 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303512 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003513 ChardevReturn *ret = NULL;
3514 ChardevBackend *backend;
3515 const char *id = qemu_opts_id(opts);
3516 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003517
Peter Maydella61ae7f2014-09-02 11:24:16 +01003518 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003519 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003520 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003521 }
3522
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003523 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003524 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003525 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003526 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003527 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303528 for (i = backends; i; i = i->next) {
3529 cd = i->data;
3530
3531 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003532 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303533 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003534 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303535 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003536 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003537 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003538 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003539 }
3540
Peter Maydella61ae7f2014-09-02 11:24:16 +01003541 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003542
3543 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003544 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003545 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003546
3547 chr = NULL;
3548 backend->kind = cd->kind;
3549 if (cd->parse) {
3550 cd->parse(opts, backend, &local_err);
3551 if (local_err) {
3552 error_propagate(errp, local_err);
3553 goto qapi_out;
3554 }
3555 }
3556 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3557 if (!ret) {
3558 goto qapi_out;
3559 }
3560
3561 if (bid) {
3562 qapi_free_ChardevBackend(backend);
3563 qapi_free_ChardevReturn(ret);
3564 backend = g_new0(ChardevBackend, 1);
3565 backend->mux = g_new0(ChardevMux, 1);
3566 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3567 backend->mux->chardev = g_strdup(bid);
3568 ret = qmp_chardev_add(id, backend, errp);
3569 if (!ret) {
3570 chr = qemu_chr_find(bid);
3571 qemu_chr_delete(chr);
3572 chr = NULL;
3573 goto qapi_out;
3574 }
3575 }
3576
3577 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003578 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003579
3580qapi_out:
3581 qapi_free_ChardevBackend(backend);
3582 qapi_free_ChardevReturn(ret);
3583 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003584 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003585
3586err:
3587 qemu_opts_del(opts);
3588 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003589}
3590
Anthony Liguori27143a42011-08-15 11:17:36 -05003591CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003592{
3593 const char *p;
3594 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003595 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003596 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003597
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003598 if (strstart(filename, "chardev:", &p)) {
3599 return qemu_chr_find(p);
3600 }
3601
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003602 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003603 if (!opts)
3604 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003605
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003606 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003607 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003608 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003609 error_free(err);
3610 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003611 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003612 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003613 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003614 }
3615 return chr;
3616}
3617
Anthony Liguori15f31512011-08-15 11:17:35 -05003618void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003619{
3620 if (chr->chr_set_echo) {
3621 chr->chr_set_echo(chr, echo);
3622 }
3623}
3624
Hans de Goede8e25daa2013-03-26 11:07:57 +01003625void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003626{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003627 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003628 return;
3629 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003630 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003631 if (chr->chr_set_fe_open) {
3632 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003633 }
3634}
3635
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003636void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3637{
3638 if (chr->chr_fe_event) {
3639 chr->chr_fe_event(chr, event);
3640 }
3641}
3642
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003643int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3644 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303645{
3646 GSource *src;
3647 guint tag;
3648
3649 if (s->chr_add_watch == NULL) {
3650 return -ENOSYS;
3651 }
3652
3653 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003654 if (!src) {
3655 return -EINVAL;
3656 }
3657
Anthony Liguori23673ca2013-03-05 23:21:23 +05303658 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3659 tag = g_source_attach(src, NULL);
3660 g_source_unref(src);
3661
3662 return tag;
3663}
3664
Hans de Goede44c473d2013-03-27 20:29:39 +01003665int qemu_chr_fe_claim(CharDriverState *s)
3666{
3667 if (s->avail_connections < 1) {
3668 return -1;
3669 }
3670 s->avail_connections--;
3671 return 0;
3672}
3673
3674void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3675{
3676 if (qemu_chr_fe_claim(s) != 0) {
3677 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3678 __func__, s->label);
3679 exit(1);
3680 }
3681}
3682
3683void qemu_chr_fe_release(CharDriverState *s)
3684{
3685 s->avail_connections++;
3686}
3687
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003688void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003689{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003690 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003691 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003692 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003693 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003694 g_free(chr->filename);
3695 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003696 if (chr->opts) {
3697 qemu_opts_del(chr->opts);
3698 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003699 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003700}
3701
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003702ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003703{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003704 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003705 CharDriverState *chr;
3706
Blue Swirl72cf2d42009-09-12 07:36:22 +00003707 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003708 ChardevInfoList *info = g_malloc0(sizeof(*info));
3709 info->value = g_malloc0(sizeof(*info->value));
3710 info->value->label = g_strdup(chr->label);
3711 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003712 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003713
3714 info->next = chr_list;
3715 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003716 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003717
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003718 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003719}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003720
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003721ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3722{
3723 ChardevBackendInfoList *backend_list = NULL;
3724 CharDriver *c = NULL;
3725 GSList *i = NULL;
3726
3727 for (i = backends; i; i = i->next) {
3728 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3729 c = i->data;
3730 info->value = g_malloc0(sizeof(*info->value));
3731 info->value->name = g_strdup(c->name);
3732
3733 info->next = backend_list;
3734 backend_list = info;
3735 }
3736
3737 return backend_list;
3738}
3739
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003740CharDriverState *qemu_chr_find(const char *name)
3741{
3742 CharDriverState *chr;
3743
Blue Swirl72cf2d42009-09-12 07:36:22 +00003744 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003745 if (strcmp(chr->label, name) != 0)
3746 continue;
3747 return chr;
3748 }
3749 return NULL;
3750}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003751
3752/* Get a character (serial) device interface. */
3753CharDriverState *qemu_char_get_next_serial(void)
3754{
3755 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003756 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003757
3758 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003759
3760 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3761 chr = serial_hds[next_serial++];
3762 qemu_chr_fe_claim_no_fail(chr);
3763 return chr;
3764 }
3765 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003766}
3767
Paolo Bonzini4d454572012-11-26 16:03:42 +01003768QemuOptsList qemu_chardev_opts = {
3769 .name = "chardev",
3770 .implied_opt_name = "backend",
3771 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3772 .desc = {
3773 {
3774 .name = "backend",
3775 .type = QEMU_OPT_STRING,
3776 },{
3777 .name = "path",
3778 .type = QEMU_OPT_STRING,
3779 },{
3780 .name = "host",
3781 .type = QEMU_OPT_STRING,
3782 },{
3783 .name = "port",
3784 .type = QEMU_OPT_STRING,
3785 },{
3786 .name = "localaddr",
3787 .type = QEMU_OPT_STRING,
3788 },{
3789 .name = "localport",
3790 .type = QEMU_OPT_STRING,
3791 },{
3792 .name = "to",
3793 .type = QEMU_OPT_NUMBER,
3794 },{
3795 .name = "ipv4",
3796 .type = QEMU_OPT_BOOL,
3797 },{
3798 .name = "ipv6",
3799 .type = QEMU_OPT_BOOL,
3800 },{
3801 .name = "wait",
3802 .type = QEMU_OPT_BOOL,
3803 },{
3804 .name = "server",
3805 .type = QEMU_OPT_BOOL,
3806 },{
3807 .name = "delay",
3808 .type = QEMU_OPT_BOOL,
3809 },{
3810 .name = "telnet",
3811 .type = QEMU_OPT_BOOL,
3812 },{
3813 .name = "width",
3814 .type = QEMU_OPT_NUMBER,
3815 },{
3816 .name = "height",
3817 .type = QEMU_OPT_NUMBER,
3818 },{
3819 .name = "cols",
3820 .type = QEMU_OPT_NUMBER,
3821 },{
3822 .name = "rows",
3823 .type = QEMU_OPT_NUMBER,
3824 },{
3825 .name = "mux",
3826 .type = QEMU_OPT_BOOL,
3827 },{
3828 .name = "signal",
3829 .type = QEMU_OPT_BOOL,
3830 },{
3831 .name = "name",
3832 .type = QEMU_OPT_STRING,
3833 },{
3834 .name = "debug",
3835 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003836 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003837 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003838 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003839 },{
3840 .name = "chardev",
3841 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003842 },
3843 { /* end of list */ }
3844 },
3845};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003846
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003847#ifdef _WIN32
3848
3849static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3850{
3851 HANDLE out;
3852
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003853 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003854 error_setg(errp, "input file not supported");
3855 return NULL;
3856 }
3857
3858 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3859 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3860 if (out == INVALID_HANDLE_VALUE) {
3861 error_setg(errp, "open %s failed", file->out);
3862 return NULL;
3863 }
3864 return qemu_chr_open_win_file(out);
3865}
3866
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003867static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3868 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003869{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003870 return qemu_chr_open_win_path(serial->device);
3871}
3872
3873static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3874 Error **errp)
3875{
3876 error_setg(errp, "character device backend type 'parallel' not supported");
3877 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003878}
3879
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003880#else /* WIN32 */
3881
3882static int qmp_chardev_open_file_source(char *src, int flags,
3883 Error **errp)
3884{
3885 int fd = -1;
3886
3887 TFR(fd = qemu_open(src, flags, 0666));
3888 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003889 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003890 }
3891 return fd;
3892}
3893
3894static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3895{
Markus Armbruster5f758362014-05-19 18:57:34 +02003896 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003897
3898 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3899 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003900 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003901 return NULL;
3902 }
3903
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003904 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003905 flags = O_RDONLY;
3906 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003907 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003908 qemu_close(out);
3909 return NULL;
3910 }
3911 }
3912
3913 return qemu_chr_open_fd(in, out);
3914}
3915
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003916static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3917 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003918{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003919#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003920 int fd;
3921
3922 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003923 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003924 return NULL;
3925 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003926 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003927 return qemu_chr_open_tty_fd(fd);
3928#else
3929 error_setg(errp, "character device backend type 'serial' not supported");
3930 return NULL;
3931#endif
3932}
3933
3934static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3935 Error **errp)
3936{
3937#ifdef HAVE_CHARDEV_PARPORT
3938 int fd;
3939
3940 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003941 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003942 return NULL;
3943 }
3944 return qemu_chr_open_pp_fd(fd);
3945#else
3946 error_setg(errp, "character device backend type 'parallel' not supported");
3947 return NULL;
3948#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003949}
3950
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003951#endif /* WIN32 */
3952
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003953static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3954 Error **errp)
3955{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003956 CharDriverState *chr;
3957 TCPCharDriver *s;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003958 SocketAddress *addr = sock->addr;
3959 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3960 bool is_listen = sock->has_server ? sock->server : true;
3961 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3962 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003963
3964 chr = qemu_chr_alloc();
3965 s = g_malloc0(sizeof(TCPCharDriver));
3966
3967 s->fd = -1;
3968 s->listen_fd = -1;
3969 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
3970 s->do_nodelay = do_nodelay;
3971
3972 chr->opaque = s;
3973 chr->chr_write = tcp_chr_write;
3974 chr->chr_sync_read = tcp_chr_sync_read;
3975 chr->chr_close = tcp_chr_close;
3976 chr->get_msgfds = tcp_get_msgfds;
3977 chr->set_msgfds = tcp_set_msgfds;
3978 chr->chr_add_client = tcp_chr_add_client;
3979 chr->chr_add_watch = tcp_chr_add_watch;
3980 chr->chr_update_read_handler = tcp_chr_update_read_handler;
3981 /* be isn't opened until we get a connection */
3982 chr->explicit_be_open = true;
3983
3984 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003985
3986 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05003987 if (is_telnet) {
3988 s->do_telnetopt = 1;
3989 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003990 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003991
3992 if (!qemu_chr_open_socket_fd(chr, addr, is_listen, is_telnet, errp)) {
3993 g_free(s);
3994 g_free(chr->filename);
3995 g_free(chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003996 return NULL;
3997 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003998
3999 if (is_listen && is_waitconnect) {
4000 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4001 chr->filename);
4002 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4003 qemu_set_nonblock(s->listen_fd);
4004 }
4005
4006 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004007}
4008
Lei Li08d0ab32013-05-20 14:51:03 +08004009static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4010 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004011{
4012 int fd;
4013
Lei Li08d0ab32013-05-20 14:51:03 +08004014 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004015 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004016 return NULL;
4017 }
4018 return qemu_chr_open_udp_fd(fd);
4019}
4020
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004021ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4022 Error **errp)
4023{
4024 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004025 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004026
4027 chr = qemu_chr_find(id);
4028 if (chr) {
4029 error_setg(errp, "Chardev '%s' already exists", id);
4030 g_free(ret);
4031 return NULL;
4032 }
4033
4034 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004035 case CHARDEV_BACKEND_KIND_FILE:
4036 chr = qmp_chardev_open_file(backend->file, errp);
4037 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004038 case CHARDEV_BACKEND_KIND_SERIAL:
4039 chr = qmp_chardev_open_serial(backend->serial, errp);
4040 break;
4041 case CHARDEV_BACKEND_KIND_PARALLEL:
4042 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004043 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004044 case CHARDEV_BACKEND_KIND_PIPE:
4045 chr = qemu_chr_open_pipe(backend->pipe);
4046 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004047 case CHARDEV_BACKEND_KIND_SOCKET:
4048 chr = qmp_chardev_open_socket(backend->socket, errp);
4049 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004050 case CHARDEV_BACKEND_KIND_UDP:
4051 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004052 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004053#ifdef HAVE_CHARDEV_TTY
4054 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004055 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004056 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004057#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004058 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004059 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004060 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004061 case CHARDEV_BACKEND_KIND_MUX:
4062 base = qemu_chr_find(backend->mux->chardev);
4063 if (base == NULL) {
4064 error_setg(errp, "mux: base chardev %s not found",
4065 backend->mux->chardev);
4066 break;
4067 }
4068 chr = qemu_chr_open_mux(base);
4069 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004070 case CHARDEV_BACKEND_KIND_MSMOUSE:
4071 chr = qemu_chr_open_msmouse();
4072 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004073#ifdef CONFIG_BRLAPI
4074 case CHARDEV_BACKEND_KIND_BRAILLE:
4075 chr = chr_baum_init();
4076 break;
4077#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004078 case CHARDEV_BACKEND_KIND_TESTDEV:
4079 chr = chr_testdev_init();
4080 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004081 case CHARDEV_BACKEND_KIND_STDIO:
4082 chr = qemu_chr_open_stdio(backend->stdio);
4083 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004084#ifdef _WIN32
4085 case CHARDEV_BACKEND_KIND_CONSOLE:
4086 chr = qemu_chr_open_win_con();
4087 break;
4088#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004089#ifdef CONFIG_SPICE
4090 case CHARDEV_BACKEND_KIND_SPICEVMC:
4091 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4092 break;
4093 case CHARDEV_BACKEND_KIND_SPICEPORT:
4094 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4095 break;
4096#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004097 case CHARDEV_BACKEND_KIND_VC:
4098 chr = vc_init(backend->vc);
4099 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004100 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004101 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004102 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004103 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004104 default:
4105 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4106 break;
4107 }
4108
Markus Armbruster3894c782014-05-19 18:57:36 +02004109 /*
4110 * Character backend open hasn't been fully converted to the Error
4111 * API. Some opens fail without setting an error. Set a generic
4112 * error then.
4113 * TODO full conversion to Error API
4114 */
4115 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004116 error_setg(errp, "Failed to create chardev");
4117 }
4118 if (chr) {
4119 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004120 chr->avail_connections =
4121 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004122 if (!chr->filename) {
4123 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4124 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004125 if (!chr->explicit_be_open) {
4126 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4127 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004128 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4129 return ret;
4130 } else {
4131 g_free(ret);
4132 return NULL;
4133 }
4134}
4135
4136void qmp_chardev_remove(const char *id, Error **errp)
4137{
4138 CharDriverState *chr;
4139
4140 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004141 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004142 error_setg(errp, "Chardev '%s' not found", id);
4143 return;
4144 }
4145 if (chr->chr_can_read || chr->chr_read ||
4146 chr->chr_event || chr->handler_opaque) {
4147 error_setg(errp, "Chardev '%s' is busy", id);
4148 return;
4149 }
4150 qemu_chr_delete(chr);
4151}
Anthony Liguorid654f342013-03-05 23:21:28 +05304152
4153static void register_types(void)
4154{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004155 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4156 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4157 qemu_chr_parse_socket);
4158 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4159 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4160 qemu_chr_parse_ringbuf);
4161 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4162 qemu_chr_parse_file_out);
4163 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4164 qemu_chr_parse_stdio);
4165 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4166 qemu_chr_parse_serial);
4167 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4168 qemu_chr_parse_serial);
4169 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4170 qemu_chr_parse_parallel);
4171 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4172 qemu_chr_parse_parallel);
4173 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4174 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4175 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4176 qemu_chr_parse_pipe);
4177 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004178 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004179 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4180 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004181 /* this must be done after machine init, since we register FEs with muxes
4182 * as part of realize functions like serial_isa_realizefn when -nographic
4183 * is specified
4184 */
4185 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304186}
4187
4188type_init(register_types);