blob: a9025e642e779c2b2ad0c462ae863d761365b35d [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000029#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030030#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000031
32#include <unistd.h>
33#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000034#include <time.h>
35#include <errno.h>
36#include <sys/time.h>
37#include <zlib.h>
38
39#ifndef _WIN32
40#include <sys/times.h>
41#include <sys/wait.h>
42#include <termios.h>
43#include <sys/mman.h>
44#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000046#include <sys/socket.h>
47#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <dirent.h>
51#include <netdb.h>
52#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020053#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000054#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040055#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56#include <dev/ppbus/ppi.h>
57#include <dev/ppbus/ppbconf.h>
58#elif defined(__DragonFly__)
59#include <dev/misc/ppi/ppi.h>
60#include <bus/ppbus/ppbconf.h>
61#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
aliguori6f97dba2008-10-31 18:49:55 +000063#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000064#include <linux/ppdev.h>
65#include <linux/parport.h>
66#endif
67#ifdef __sun__
68#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
71#include <netinet/arp.h>
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000078#endif
79#endif
80#endif
81
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010082#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020083#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000084
Amit Shah9bd78542009-11-03 19:59:54 +053085#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030086#define READ_RETRIES 10
Amit Shah9bd78542009-11-03 19:59:54 +053087
aliguori6f97dba2008-10-31 18:49:55 +000088/***********************************************************/
89/* character device */
90
Blue Swirl72cf2d42009-09-12 07:36:22 +000091static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000093
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020094CharDriverState *qemu_chr_alloc(void)
95{
96 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
97 return chr;
98}
99
Hans de Goedea425d232011-11-19 10:22:43 +0100100void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000101{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200102 /* Keep track if the char device is open */
103 switch (event) {
104 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100105 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200106 break;
107 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100108 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200109 break;
110 }
111
aliguori6f97dba2008-10-31 18:49:55 +0000112 if (!s->chr_event)
113 return;
114 s->chr_event(s->handler_opaque, event);
115}
116
Hans de Goedefee204f2013-03-26 11:07:54 +0100117void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000118{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500119 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000120}
121
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500122int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000123{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200124 int ret;
125
126 qemu_mutex_lock(&s->chr_write_lock);
127 ret = s->chr_write(s, buf, len);
128 qemu_mutex_unlock(&s->chr_write_lock);
129 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000130}
131
Anthony Liguoricd187202013-03-26 10:04:17 -0500132int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
133{
134 int offset = 0;
135 int res;
136
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200137 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500138 while (offset < len) {
139 do {
140 res = s->chr_write(s, buf + offset, len - offset);
141 if (res == -1 && errno == EAGAIN) {
142 g_usleep(100);
143 }
144 } while (res == -1 && errno == EAGAIN);
145
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200146 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500147 break;
148 }
149
Anthony Liguoricd187202013-03-26 10:04:17 -0500150 offset += res;
151 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200152 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500153
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200154 if (res < 0) {
155 return res;
156 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500157 return offset;
158}
159
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300160int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
161{
162 int offset = 0, counter = 10;
163 int res;
164
165 if (!s->chr_sync_read) {
166 return 0;
167 }
168
169 while (offset < len) {
170 do {
171 res = s->chr_sync_read(s, buf + offset, len - offset);
172 if (res == -1 && errno == EAGAIN) {
173 g_usleep(100);
174 }
175 } while (res == -1 && errno == EAGAIN);
176
177 if (res == 0) {
178 break;
179 }
180
181 if (res < 0) {
182 return res;
183 }
184
185 offset += res;
186
187 if (!counter--) {
188 break;
189 }
190 }
191
192 return offset;
193}
194
Anthony Liguori41084f12011-08-15 11:17:34 -0500195int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000196{
197 if (!s->chr_ioctl)
198 return -ENOTSUP;
199 return s->chr_ioctl(s, cmd, arg);
200}
201
Anthony Liguori909cda12011-08-15 11:17:31 -0500202int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000203{
204 if (!s->chr_can_read)
205 return 0;
206 return s->chr_can_read(s->handler_opaque);
207}
208
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500209void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000210{
Stefan Weilac310732012-04-19 22:27:14 +0200211 if (s->chr_read) {
212 s->chr_read(s->handler_opaque, buf, len);
213 }
aliguori6f97dba2008-10-31 18:49:55 +0000214}
215
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500216int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100217{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300218 int fd;
219 return (qemu_chr_fe_get_msgfds(s, &fd, 1) >= 0) ? fd : -1;
220}
221
222int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
223{
224 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100225}
226
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300227int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
228{
229 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
230}
231
Daniel P. Berrange13661082011-06-23 13:31:42 +0100232int qemu_chr_add_client(CharDriverState *s, int fd)
233{
234 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
235}
236
aliguori6f97dba2008-10-31 18:49:55 +0000237void qemu_chr_accept_input(CharDriverState *s)
238{
239 if (s->chr_accept_input)
240 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100241 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000242}
243
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500244void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000245{
Amit Shah9bd78542009-11-03 19:59:54 +0530246 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000247 va_list ap;
248 va_start(ap, fmt);
249 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500250 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000251 va_end(ap);
252}
253
Amit Shah386a5a12013-08-28 15:24:05 +0530254static void remove_fd_in_watch(CharDriverState *chr);
255
aliguori6f97dba2008-10-31 18:49:55 +0000256void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100257 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000258 IOReadHandler *fd_read,
259 IOEventHandler *fd_event,
260 void *opaque)
261{
Hans de Goede19083222013-03-26 11:07:56 +0100262 int fe_open;
263
Amit Shahda7d9982011-04-25 15:18:22 +0530264 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100265 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530266 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100267 } else {
268 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530269 }
aliguori6f97dba2008-10-31 18:49:55 +0000270 s->chr_can_read = fd_can_read;
271 s->chr_read = fd_read;
272 s->chr_event = fd_event;
273 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200274 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000275 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200276
Hans de Goede19083222013-03-26 11:07:56 +0100277 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100278 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100279 }
280
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200281 /* We're connecting to an already opened device, so let's make sure we
282 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100283 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100284 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200285 }
aliguori6f97dba2008-10-31 18:49:55 +0000286}
287
288static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
289{
290 return len;
291}
292
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100293static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000294{
295 CharDriverState *chr;
296
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200297 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000298 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500299 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100300 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000301}
302
303/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000304#define MAX_MUX 4
305#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
306#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
307typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100308 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000309 IOReadHandler *chr_read[MAX_MUX];
310 IOEventHandler *chr_event[MAX_MUX];
311 void *ext_opaque[MAX_MUX];
312 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200313 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000314 int mux_cnt;
315 int term_got_escape;
316 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000317 /* Intermediate input buffer allows to catch escape sequences even if the
318 currently active device is not accepting any input - but only until it
319 is full as well. */
320 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
321 int prod[MAX_MUX];
322 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200323 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200324
325 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200326 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200327 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000328} MuxDriver;
329
330
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200331/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000332static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
333{
334 MuxDriver *d = chr->opaque;
335 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200336 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200337 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000338 } else {
339 int i;
340
341 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200342 for (i = 0; i < len; i++) {
343 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000344 char buf1[64];
345 int64_t ti;
346 int secs;
347
Alex Blighbc72ad62013-08-21 16:03:08 +0100348 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200349 if (d->timestamps_start == -1)
350 d->timestamps_start = ti;
351 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000352 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000353 snprintf(buf1, sizeof(buf1),
354 "[%02d:%02d:%02d.%03d] ",
355 secs / 3600,
356 (secs / 60) % 60,
357 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000358 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200359 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200360 d->linestart = 0;
361 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200362 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200363 if (buf[i] == '\n') {
364 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000365 }
366 }
367 }
368 return ret;
369}
370
371static const char * const mux_help[] = {
372 "% h print this help\n\r",
373 "% x exit emulator\n\r",
374 "% s save disk data back to file (if -snapshot)\n\r",
375 "% t toggle console timestamps\n\r"
376 "% b send break (magic sysrq)\n\r",
377 "% c switch between console and monitor\n\r",
378 "% % sends %\n\r",
379 NULL
380};
381
382int term_escape_char = 0x01; /* ctrl-a is used for escape */
383static void mux_print_help(CharDriverState *chr)
384{
385 int i, j;
386 char ebuf[15] = "Escape-Char";
387 char cbuf[50] = "\n\r";
388
389 if (term_escape_char > 0 && term_escape_char < 26) {
390 snprintf(cbuf, sizeof(cbuf), "\n\r");
391 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
392 } else {
393 snprintf(cbuf, sizeof(cbuf),
394 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
395 term_escape_char);
396 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200397 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000398 for (i = 0; mux_help[i] != NULL; i++) {
399 for (j=0; mux_help[i][j] != '\0'; j++) {
400 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200401 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000402 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200403 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000404 }
405 }
406}
407
aliguori2724b182009-03-05 23:01:47 +0000408static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
409{
410 if (d->chr_event[mux_nr])
411 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
412}
413
aliguori6f97dba2008-10-31 18:49:55 +0000414static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
415{
416 if (d->term_got_escape) {
417 d->term_got_escape = 0;
418 if (ch == term_escape_char)
419 goto send_char;
420 switch(ch) {
421 case '?':
422 case 'h':
423 mux_print_help(chr);
424 break;
425 case 'x':
426 {
427 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200428 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000429 exit(0);
430 break;
431 }
432 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200433 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000434 break;
435 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100436 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000437 break;
438 case 'c':
439 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200440 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
441 d->focus++;
442 if (d->focus >= d->mux_cnt)
443 d->focus = 0;
444 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000445 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200446 case 't':
447 d->timestamps = !d->timestamps;
448 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200449 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200450 break;
aliguori6f97dba2008-10-31 18:49:55 +0000451 }
452 } else if (ch == term_escape_char) {
453 d->term_got_escape = 1;
454 } else {
455 send_char:
456 return 1;
457 }
458 return 0;
459}
460
461static void mux_chr_accept_input(CharDriverState *chr)
462{
aliguori6f97dba2008-10-31 18:49:55 +0000463 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200464 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000465
aliguoria80bf992009-03-05 23:00:02 +0000466 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000467 d->chr_can_read[m] &&
468 d->chr_can_read[m](d->ext_opaque[m])) {
469 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000470 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000471 }
472}
473
474static int mux_chr_can_read(void *opaque)
475{
476 CharDriverState *chr = opaque;
477 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200478 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000479
aliguoria80bf992009-03-05 23:00:02 +0000480 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000481 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000482 if (d->chr_can_read[m])
483 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000484 return 0;
485}
486
487static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
488{
489 CharDriverState *chr = opaque;
490 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200491 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000492 int i;
493
494 mux_chr_accept_input (opaque);
495
496 for(i = 0; i < size; i++)
497 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000498 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000499 d->chr_can_read[m] &&
500 d->chr_can_read[m](d->ext_opaque[m]))
501 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
502 else
aliguoria80bf992009-03-05 23:00:02 +0000503 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000504 }
505}
506
507static void mux_chr_event(void *opaque, int event)
508{
509 CharDriverState *chr = opaque;
510 MuxDriver *d = chr->opaque;
511 int i;
512
513 /* Send the event to all registered listeners */
514 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000515 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000516}
517
518static void mux_chr_update_read_handler(CharDriverState *chr)
519{
520 MuxDriver *d = chr->opaque;
521
522 if (d->mux_cnt >= MAX_MUX) {
523 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
524 return;
525 }
526 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
527 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
528 d->chr_read[d->mux_cnt] = chr->chr_read;
529 d->chr_event[d->mux_cnt] = chr->chr_event;
530 /* Fix up the real driver with mux routines */
531 if (d->mux_cnt == 0) {
532 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
533 mux_chr_event, chr);
534 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200535 if (d->focus != -1) {
536 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200537 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200538 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000539 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200540 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000541}
542
Michael Roth7b7ab182013-07-30 13:04:22 -0500543static bool muxes_realized;
544
545/**
546 * Called after processing of default and command-line-specified
547 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
548 * to a mux chardev. This is done here to ensure that
549 * output/prompts/banners are only displayed for the FE that has
550 * focus when initial command-line processing/machine init is
551 * completed.
552 *
553 * After this point, any new FE attached to any new or existing
554 * mux will receive CHR_EVENT_OPENED notifications for the BE
555 * immediately.
556 */
557static void muxes_realize_done(Notifier *notifier, void *unused)
558{
559 CharDriverState *chr;
560
561 QTAILQ_FOREACH(chr, &chardevs, next) {
562 if (chr->is_mux) {
563 MuxDriver *d = chr->opaque;
564 int i;
565
566 /* send OPENED to all already-attached FEs */
567 for (i = 0; i < d->mux_cnt; i++) {
568 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
569 }
570 /* mark mux as OPENED so any new FEs will immediately receive
571 * OPENED event
572 */
573 qemu_chr_be_generic_open(chr);
574 }
575 }
576 muxes_realized = true;
577}
578
579static Notifier muxes_realize_notify = {
580 .notify = muxes_realize_done,
581};
582
aliguori6f97dba2008-10-31 18:49:55 +0000583static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
584{
585 CharDriverState *chr;
586 MuxDriver *d;
587
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200588 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500589 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000590
591 chr->opaque = d;
592 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200593 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000594 chr->chr_write = mux_chr_write;
595 chr->chr_update_read_handler = mux_chr_update_read_handler;
596 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100597 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100598 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500599 /* only default to opened state if we've realized the initial
600 * set of muxes
601 */
602 chr->explicit_be_open = muxes_realized ? 0 : 1;
603 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200604
aliguori6f97dba2008-10-31 18:49:55 +0000605 return chr;
606}
607
608
609#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000610int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000611{
612 int ret, len;
613
614 len = len1;
615 while (len > 0) {
616 ret = send(fd, buf, len, 0);
617 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000618 errno = WSAGetLastError();
619 if (errno != WSAEWOULDBLOCK) {
620 return -1;
621 }
622 } else if (ret == 0) {
623 break;
624 } else {
625 buf += ret;
626 len -= ret;
627 }
628 }
629 return len1 - len;
630}
631
632#else
633
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100634int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000635{
636 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100637 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000638
639 len = len1;
640 while (len > 0) {
641 ret = write(fd, buf, len);
642 if (ret < 0) {
643 if (errno != EINTR && errno != EAGAIN)
644 return -1;
645 } else if (ret == 0) {
646 break;
647 } else {
648 buf += ret;
649 len -= ret;
650 }
651 }
652 return len1 - len;
653}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500654
655int recv_all(int fd, void *_buf, int len1, bool single_read)
656{
657 int ret, len;
658 uint8_t *buf = _buf;
659
660 len = len1;
661 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
662 if (ret < 0) {
663 if (errno != EINTR && errno != EAGAIN) {
664 return -1;
665 }
666 continue;
667 } else {
668 if (single_read) {
669 return ret;
670 }
671 buf += ret;
672 len -= ret;
673 }
674 }
675 return len1 - len;
676}
677
aliguori6f97dba2008-10-31 18:49:55 +0000678#endif /* !_WIN32 */
679
Anthony Liguori96c63842013-03-05 23:21:18 +0530680typedef struct IOWatchPoll
681{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200682 GSource parent;
683
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200684 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530685 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530686
687 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200688 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530689 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530690} IOWatchPoll;
691
Anthony Liguori96c63842013-03-05 23:21:18 +0530692static IOWatchPoll *io_watch_poll_from_source(GSource *source)
693{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200694 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530695}
696
697static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
698{
699 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200700 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200701 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200702 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530703 return FALSE;
704 }
705
Paolo Bonzinid185c092013-04-05 17:59:33 +0200706 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200707 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
708 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200709 g_source_attach(iwp->src, NULL);
710 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200711 g_source_destroy(iwp->src);
712 g_source_unref(iwp->src);
713 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200714 }
715 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530716}
717
718static gboolean io_watch_poll_check(GSource *source)
719{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200720 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530721}
722
723static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
724 gpointer user_data)
725{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200726 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530727}
728
729static void io_watch_poll_finalize(GSource *source)
730{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200731 /* Due to a glib bug, removing the last reference to a source
732 * inside a finalize callback causes recursive locking (and a
733 * deadlock). This is not a problem inside other callbacks,
734 * including dispatch callbacks, so we call io_remove_watch_poll
735 * to remove this source. At this point, iwp->src must
736 * be NULL, or we would leak it.
737 *
738 * This would be solved much more elegantly by child sources,
739 * but we support older glib versions that do not have them.
740 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530741 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200742 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530743}
744
745static GSourceFuncs io_watch_poll_funcs = {
746 .prepare = io_watch_poll_prepare,
747 .check = io_watch_poll_check,
748 .dispatch = io_watch_poll_dispatch,
749 .finalize = io_watch_poll_finalize,
750};
751
752/* Can only be used for read */
753static guint io_add_watch_poll(GIOChannel *channel,
754 IOCanReadHandler *fd_can_read,
755 GIOFunc fd_read,
756 gpointer user_data)
757{
758 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200759 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530760
Paolo Bonzinid185c092013-04-05 17:59:33 +0200761 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530762 iwp->fd_can_read = fd_can_read;
763 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200764 iwp->channel = channel;
765 iwp->fd_read = (GSourceFunc) fd_read;
766 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530767
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200768 tag = g_source_attach(&iwp->parent, NULL);
769 g_source_unref(&iwp->parent);
770 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530771}
772
Paolo Bonzini2b316772013-04-19 17:32:09 +0200773static void io_remove_watch_poll(guint tag)
774{
775 GSource *source;
776 IOWatchPoll *iwp;
777
778 g_return_if_fail (tag > 0);
779
780 source = g_main_context_find_source_by_id(NULL, tag);
781 g_return_if_fail (source != NULL);
782
783 iwp = io_watch_poll_from_source(source);
784 if (iwp->src) {
785 g_source_destroy(iwp->src);
786 g_source_unref(iwp->src);
787 iwp->src = NULL;
788 }
789 g_source_destroy(&iwp->parent);
790}
791
Amit Shah26da70c2013-08-28 15:23:37 +0530792static void remove_fd_in_watch(CharDriverState *chr)
793{
794 if (chr->fd_in_tag) {
795 io_remove_watch_poll(chr->fd_in_tag);
796 chr->fd_in_tag = 0;
797 }
798}
799
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000800#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530801static GIOChannel *io_channel_from_fd(int fd)
802{
803 GIOChannel *chan;
804
805 if (fd == -1) {
806 return NULL;
807 }
808
809 chan = g_io_channel_unix_new(fd);
810
811 g_io_channel_set_encoding(chan, NULL, NULL);
812 g_io_channel_set_buffered(chan, FALSE);
813
814 return chan;
815}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000816#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530817
Anthony Liguori76a96442013-03-05 23:21:21 +0530818static GIOChannel *io_channel_from_socket(int fd)
819{
820 GIOChannel *chan;
821
822 if (fd == -1) {
823 return NULL;
824 }
825
826#ifdef _WIN32
827 chan = g_io_channel_win32_new_socket(fd);
828#else
829 chan = g_io_channel_unix_new(fd);
830#endif
831
832 g_io_channel_set_encoding(chan, NULL, NULL);
833 g_io_channel_set_buffered(chan, FALSE);
834
835 return chan;
836}
837
Anthony Liguori684a0962013-03-29 11:39:50 -0500838static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530839{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200840 size_t offset = 0;
841 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530842
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200843 while (offset < len && status == G_IO_STATUS_NORMAL) {
844 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500845
846 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530847 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500848 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530849 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500850
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200851 if (offset > 0) {
852 return offset;
853 }
854 switch (status) {
855 case G_IO_STATUS_NORMAL:
856 g_assert(len == 0);
857 return 0;
858 case G_IO_STATUS_AGAIN:
859 errno = EAGAIN;
860 return -1;
861 default:
862 break;
863 }
864 errno = EINVAL;
865 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530866}
Anthony Liguori96c63842013-03-05 23:21:18 +0530867
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000868#ifndef _WIN32
869
Anthony Liguoria29753f2013-03-05 23:21:19 +0530870typedef struct FDCharDriver {
871 CharDriverState *chr;
872 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000873 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530874 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000875} FDCharDriver;
876
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200877/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000878static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
879{
880 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530881
Anthony Liguori684a0962013-03-29 11:39:50 -0500882 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530883}
884
885static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
886{
887 CharDriverState *chr = opaque;
888 FDCharDriver *s = chr->opaque;
889 int len;
890 uint8_t buf[READ_BUF_LEN];
891 GIOStatus status;
892 gsize bytes_read;
893
894 len = sizeof(buf);
895 if (len > s->max_size) {
896 len = s->max_size;
897 }
898 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200899 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530900 }
901
902 status = g_io_channel_read_chars(chan, (gchar *)buf,
903 len, &bytes_read, NULL);
904 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530905 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530906 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
907 return FALSE;
908 }
909 if (status == G_IO_STATUS_NORMAL) {
910 qemu_chr_be_write(chr, buf, bytes_read);
911 }
912
913 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000914}
915
916static int fd_chr_read_poll(void *opaque)
917{
918 CharDriverState *chr = opaque;
919 FDCharDriver *s = chr->opaque;
920
Anthony Liguori909cda12011-08-15 11:17:31 -0500921 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000922 return s->max_size;
923}
924
Anthony Liguori23673ca2013-03-05 23:21:23 +0530925static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
926{
927 FDCharDriver *s = chr->opaque;
928 return g_io_create_watch(s->fd_out, cond);
929}
930
aliguori6f97dba2008-10-31 18:49:55 +0000931static void fd_chr_update_read_handler(CharDriverState *chr)
932{
933 FDCharDriver *s = chr->opaque;
934
Amit Shah26da70c2013-08-28 15:23:37 +0530935 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530936 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530937 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
938 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000939 }
940}
941
942static void fd_chr_close(struct 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) {
948 g_io_channel_unref(s->fd_in);
949 }
950 if (s->fd_out) {
951 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000952 }
953
Anthony Liguori7267c092011-08-20 22:09:37 -0500954 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100955 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000956}
957
958/* open a character device to a unix fd */
959static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
960{
961 CharDriverState *chr;
962 FDCharDriver *s;
963
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200964 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500965 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530966 s->fd_in = io_channel_from_fd(fd_in);
967 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530968 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530969 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000970 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530971 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000972 chr->chr_write = fd_chr_write;
973 chr->chr_update_read_handler = fd_chr_update_read_handler;
974 chr->chr_close = fd_chr_close;
975
aliguori6f97dba2008-10-31 18:49:55 +0000976 return chr;
977}
978
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100979static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000980{
981 int fd_in, fd_out;
982 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100983 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200984
985 if (filename == NULL) {
986 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100987 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200988 }
aliguori6f97dba2008-10-31 18:49:55 +0000989
990 snprintf(filename_in, 256, "%s.in", filename);
991 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100992 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
993 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000994 if (fd_in < 0 || fd_out < 0) {
995 if (fd_in >= 0)
996 close(fd_in);
997 if (fd_out >= 0)
998 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100999 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001000 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001001 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001002 }
aliguori6f97dba2008-10-31 18:49:55 +00001003 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001004 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001005}
1006
aliguori6f97dba2008-10-31 18:49:55 +00001007/* init terminal so that we can grab keys */
1008static struct termios oldtty;
1009static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001010static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001011
1012static void term_exit(void)
1013{
1014 tcsetattr (0, TCSANOW, &oldtty);
1015 fcntl(0, F_SETFL, old_fd0_flags);
1016}
1017
Paolo Bonzinibb002512010-12-23 13:42:50 +01001018static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001019{
1020 struct termios tty;
1021
Paolo Bonzini03693642010-12-23 13:42:49 +01001022 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001023 if (!echo) {
1024 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001025 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001026 tty.c_oflag |= OPOST;
1027 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1028 tty.c_cflag &= ~(CSIZE|PARENB);
1029 tty.c_cflag |= CS8;
1030 tty.c_cc[VMIN] = 1;
1031 tty.c_cc[VTIME] = 0;
1032 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001033 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001034 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001035
1036 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001037}
1038
1039static void qemu_chr_close_stdio(struct CharDriverState *chr)
1040{
1041 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001042 fd_chr_close(chr);
1043}
1044
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001045static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001046{
1047 CharDriverState *chr;
1048
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001049 if (is_daemonized()) {
1050 error_report("cannot use stdio with -daemonize");
1051 return NULL;
1052 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301053 old_fd0_flags = fcntl(0, F_GETFL);
1054 tcgetattr (0, &oldtty);
1055 fcntl(0, F_SETFL, O_NONBLOCK);
1056 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001057
aliguori6f97dba2008-10-31 18:49:55 +00001058 chr = qemu_chr_open_fd(0, 1);
1059 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001060 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001061 if (opts->has_signal) {
1062 stdio_allow_signal = opts->signal;
1063 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001064 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001065
Markus Armbruster1f514702012-02-07 15:09:08 +01001066 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001067}
1068
aliguori6f97dba2008-10-31 18:49:55 +00001069#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001070 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1071 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001072
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001073#define HAVE_CHARDEV_TTY 1
1074
aliguori6f97dba2008-10-31 18:49:55 +00001075typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301076 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001077 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001078
1079 /* Protected by the CharDriverState chr_write_lock. */
1080 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301081 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001082} PtyCharDriver;
1083
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001084static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001085static void pty_chr_state(CharDriverState *chr, int connected);
1086
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301087static gboolean pty_chr_timer(gpointer opaque)
1088{
1089 struct CharDriverState *chr = opaque;
1090 PtyCharDriver *s = chr->opaque;
1091
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001092 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001093 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001094 if (!s->connected) {
1095 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001096 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001097 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001098 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301099 return FALSE;
1100}
1101
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001102/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301103static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1104{
1105 PtyCharDriver *s = chr->opaque;
1106
1107 if (s->timer_tag) {
1108 g_source_remove(s->timer_tag);
1109 s->timer_tag = 0;
1110 }
1111
1112 if (ms == 1000) {
1113 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1114 } else {
1115 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1116 }
1117}
1118
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001119/* Called with chr_write_lock held. */
1120static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001121{
1122 PtyCharDriver *s = chr->opaque;
1123 GPollFD pfd;
1124
1125 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1126 pfd.events = G_IO_OUT;
1127 pfd.revents = 0;
1128 g_poll(&pfd, 1, 0);
1129 if (pfd.revents & G_IO_HUP) {
1130 pty_chr_state(chr, 0);
1131 } else {
1132 pty_chr_state(chr, 1);
1133 }
1134}
1135
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001136static void pty_chr_update_read_handler(CharDriverState *chr)
1137{
1138 qemu_mutex_lock(&chr->chr_write_lock);
1139 pty_chr_update_read_handler_locked(chr);
1140 qemu_mutex_unlock(&chr->chr_write_lock);
1141}
1142
1143/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001144static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1145{
1146 PtyCharDriver *s = chr->opaque;
1147
1148 if (!s->connected) {
1149 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001150 pty_chr_update_read_handler_locked(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001151 return 0;
1152 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001153 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001154}
1155
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301156static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1157{
1158 PtyCharDriver *s = chr->opaque;
1159 return g_io_create_watch(s->fd, cond);
1160}
1161
aliguori6f97dba2008-10-31 18:49:55 +00001162static int pty_chr_read_poll(void *opaque)
1163{
1164 CharDriverState *chr = opaque;
1165 PtyCharDriver *s = chr->opaque;
1166
Anthony Liguori909cda12011-08-15 11:17:31 -05001167 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001168 return s->read_bytes;
1169}
1170
Anthony Liguori093d3a22013-03-05 23:21:20 +05301171static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001172{
1173 CharDriverState *chr = opaque;
1174 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301175 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301176 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301177 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001178
1179 len = sizeof(buf);
1180 if (len > s->read_bytes)
1181 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001182 if (len == 0) {
1183 return TRUE;
1184 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301185 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1186 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001187 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301188 return FALSE;
1189 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001190 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001191 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001192 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301193 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001194}
1195
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001196/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001197static void pty_chr_state(CharDriverState *chr, int connected)
1198{
1199 PtyCharDriver *s = chr->opaque;
1200
1201 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301202 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001203 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001204 /* (re-)connect poll interval for idle guests: once per second.
1205 * We check more frequently in case the guests sends data to
1206 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301207 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001208 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001209 if (s->timer_tag) {
1210 g_source_remove(s->timer_tag);
1211 s->timer_tag = 0;
1212 }
1213 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001214 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001215 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001216 }
1217 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301218 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1219 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001220 }
aliguori6f97dba2008-10-31 18:49:55 +00001221 }
1222}
1223
aliguori6f97dba2008-10-31 18:49:55 +00001224static void pty_chr_close(struct CharDriverState *chr)
1225{
1226 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301227 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001228
Amit Shah26da70c2013-08-28 15:23:37 +05301229 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301230 fd = g_io_channel_unix_get_fd(s->fd);
1231 g_io_channel_unref(s->fd);
1232 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301233 if (s->timer_tag) {
1234 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001235 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301236 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001237 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001238 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001239}
1240
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001241static CharDriverState *qemu_chr_open_pty(const char *id,
1242 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001243{
1244 CharDriverState *chr;
1245 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001246 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001247 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001248
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001249 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1250 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001251 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001252 }
1253
aliguori6f97dba2008-10-31 18:49:55 +00001254 close(slave_fd);
1255
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001256 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001257
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001258 chr->filename = g_strdup_printf("pty:%s", pty_name);
1259 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001260 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001261
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001262 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001263 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001264
1265 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001266 chr->opaque = s;
1267 chr->chr_write = pty_chr_write;
1268 chr->chr_update_read_handler = pty_chr_update_read_handler;
1269 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301270 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001271 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001272
Anthony Liguori093d3a22013-03-05 23:21:20 +05301273 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301274 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001275
Markus Armbruster1f514702012-02-07 15:09:08 +01001276 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001277}
1278
1279static void tty_serial_init(int fd, int speed,
1280 int parity, int data_bits, int stop_bits)
1281{
1282 struct termios tty;
1283 speed_t spd;
1284
1285#if 0
1286 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1287 speed, parity, data_bits, stop_bits);
1288#endif
1289 tcgetattr (fd, &tty);
1290
Stefan Weil45eea132009-10-26 16:10:10 +01001291#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1292 speed = speed * 10 / 11;
1293 do {
1294 check_speed(50);
1295 check_speed(75);
1296 check_speed(110);
1297 check_speed(134);
1298 check_speed(150);
1299 check_speed(200);
1300 check_speed(300);
1301 check_speed(600);
1302 check_speed(1200);
1303 check_speed(1800);
1304 check_speed(2400);
1305 check_speed(4800);
1306 check_speed(9600);
1307 check_speed(19200);
1308 check_speed(38400);
1309 /* Non-Posix values follow. They may be unsupported on some systems. */
1310 check_speed(57600);
1311 check_speed(115200);
1312#ifdef B230400
1313 check_speed(230400);
1314#endif
1315#ifdef B460800
1316 check_speed(460800);
1317#endif
1318#ifdef B500000
1319 check_speed(500000);
1320#endif
1321#ifdef B576000
1322 check_speed(576000);
1323#endif
1324#ifdef B921600
1325 check_speed(921600);
1326#endif
1327#ifdef B1000000
1328 check_speed(1000000);
1329#endif
1330#ifdef B1152000
1331 check_speed(1152000);
1332#endif
1333#ifdef B1500000
1334 check_speed(1500000);
1335#endif
1336#ifdef B2000000
1337 check_speed(2000000);
1338#endif
1339#ifdef B2500000
1340 check_speed(2500000);
1341#endif
1342#ifdef B3000000
1343 check_speed(3000000);
1344#endif
1345#ifdef B3500000
1346 check_speed(3500000);
1347#endif
1348#ifdef B4000000
1349 check_speed(4000000);
1350#endif
aliguori6f97dba2008-10-31 18:49:55 +00001351 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001352 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001353
1354 cfsetispeed(&tty, spd);
1355 cfsetospeed(&tty, spd);
1356
1357 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1358 |INLCR|IGNCR|ICRNL|IXON);
1359 tty.c_oflag |= OPOST;
1360 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1361 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1362 switch(data_bits) {
1363 default:
1364 case 8:
1365 tty.c_cflag |= CS8;
1366 break;
1367 case 7:
1368 tty.c_cflag |= CS7;
1369 break;
1370 case 6:
1371 tty.c_cflag |= CS6;
1372 break;
1373 case 5:
1374 tty.c_cflag |= CS5;
1375 break;
1376 }
1377 switch(parity) {
1378 default:
1379 case 'N':
1380 break;
1381 case 'E':
1382 tty.c_cflag |= PARENB;
1383 break;
1384 case 'O':
1385 tty.c_cflag |= PARENB | PARODD;
1386 break;
1387 }
1388 if (stop_bits == 2)
1389 tty.c_cflag |= CSTOPB;
1390
1391 tcsetattr (fd, TCSANOW, &tty);
1392}
1393
1394static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1395{
1396 FDCharDriver *s = chr->opaque;
1397
1398 switch(cmd) {
1399 case CHR_IOCTL_SERIAL_SET_PARAMS:
1400 {
1401 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301402 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1403 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001404 ssp->data_bits, ssp->stop_bits);
1405 }
1406 break;
1407 case CHR_IOCTL_SERIAL_SET_BREAK:
1408 {
1409 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301410 if (enable) {
1411 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1412 }
aliguori6f97dba2008-10-31 18:49:55 +00001413 }
1414 break;
1415 case CHR_IOCTL_SERIAL_GET_TIOCM:
1416 {
1417 int sarg = 0;
1418 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301419 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001420 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001421 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001422 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001423 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001424 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001425 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001426 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001427 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001428 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001429 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001430 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001431 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001432 *targ |= CHR_TIOCM_RTS;
1433 }
1434 break;
1435 case CHR_IOCTL_SERIAL_SET_TIOCM:
1436 {
1437 int sarg = *(int *)arg;
1438 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301439 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001440 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1441 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1442 if (sarg & CHR_TIOCM_CTS)
1443 targ |= TIOCM_CTS;
1444 if (sarg & CHR_TIOCM_CAR)
1445 targ |= TIOCM_CAR;
1446 if (sarg & CHR_TIOCM_DSR)
1447 targ |= TIOCM_DSR;
1448 if (sarg & CHR_TIOCM_RI)
1449 targ |= TIOCM_RI;
1450 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001451 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001452 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001453 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301454 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001455 }
1456 break;
1457 default:
1458 return -ENOTSUP;
1459 }
1460 return 0;
1461}
1462
David Ahern4266a132010-02-10 18:27:17 -07001463static void qemu_chr_close_tty(CharDriverState *chr)
1464{
1465 FDCharDriver *s = chr->opaque;
1466 int fd = -1;
1467
1468 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301469 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001470 }
1471
1472 fd_chr_close(chr);
1473
1474 if (fd >= 0) {
1475 close(fd);
1476 }
1477}
1478
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001479static CharDriverState *qemu_chr_open_tty_fd(int fd)
1480{
1481 CharDriverState *chr;
1482
1483 tty_serial_init(fd, 115200, 'N', 8, 1);
1484 chr = qemu_chr_open_fd(fd, fd);
1485 chr->chr_ioctl = tty_serial_ioctl;
1486 chr->chr_close = qemu_chr_close_tty;
1487 return chr;
1488}
aliguori6f97dba2008-10-31 18:49:55 +00001489#endif /* __linux__ || __sun__ */
1490
1491#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001492
1493#define HAVE_CHARDEV_PARPORT 1
1494
aliguori6f97dba2008-10-31 18:49:55 +00001495typedef struct {
1496 int fd;
1497 int mode;
1498} ParallelCharDriver;
1499
1500static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1501{
1502 if (s->mode != mode) {
1503 int m = mode;
1504 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1505 return 0;
1506 s->mode = mode;
1507 }
1508 return 1;
1509}
1510
1511static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1512{
1513 ParallelCharDriver *drv = chr->opaque;
1514 int fd = drv->fd;
1515 uint8_t b;
1516
1517 switch(cmd) {
1518 case CHR_IOCTL_PP_READ_DATA:
1519 if (ioctl(fd, PPRDATA, &b) < 0)
1520 return -ENOTSUP;
1521 *(uint8_t *)arg = b;
1522 break;
1523 case CHR_IOCTL_PP_WRITE_DATA:
1524 b = *(uint8_t *)arg;
1525 if (ioctl(fd, PPWDATA, &b) < 0)
1526 return -ENOTSUP;
1527 break;
1528 case CHR_IOCTL_PP_READ_CONTROL:
1529 if (ioctl(fd, PPRCONTROL, &b) < 0)
1530 return -ENOTSUP;
1531 /* Linux gives only the lowest bits, and no way to know data
1532 direction! For better compatibility set the fixed upper
1533 bits. */
1534 *(uint8_t *)arg = b | 0xc0;
1535 break;
1536 case CHR_IOCTL_PP_WRITE_CONTROL:
1537 b = *(uint8_t *)arg;
1538 if (ioctl(fd, PPWCONTROL, &b) < 0)
1539 return -ENOTSUP;
1540 break;
1541 case CHR_IOCTL_PP_READ_STATUS:
1542 if (ioctl(fd, PPRSTATUS, &b) < 0)
1543 return -ENOTSUP;
1544 *(uint8_t *)arg = b;
1545 break;
1546 case CHR_IOCTL_PP_DATA_DIR:
1547 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1548 return -ENOTSUP;
1549 break;
1550 case CHR_IOCTL_PP_EPP_READ_ADDR:
1551 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1552 struct ParallelIOArg *parg = arg;
1553 int n = read(fd, parg->buffer, parg->count);
1554 if (n != parg->count) {
1555 return -EIO;
1556 }
1557 }
1558 break;
1559 case CHR_IOCTL_PP_EPP_READ:
1560 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1561 struct ParallelIOArg *parg = arg;
1562 int n = read(fd, parg->buffer, parg->count);
1563 if (n != parg->count) {
1564 return -EIO;
1565 }
1566 }
1567 break;
1568 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1569 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1570 struct ParallelIOArg *parg = arg;
1571 int n = write(fd, parg->buffer, parg->count);
1572 if (n != parg->count) {
1573 return -EIO;
1574 }
1575 }
1576 break;
1577 case CHR_IOCTL_PP_EPP_WRITE:
1578 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1579 struct ParallelIOArg *parg = arg;
1580 int n = write(fd, parg->buffer, parg->count);
1581 if (n != parg->count) {
1582 return -EIO;
1583 }
1584 }
1585 break;
1586 default:
1587 return -ENOTSUP;
1588 }
1589 return 0;
1590}
1591
1592static void pp_close(CharDriverState *chr)
1593{
1594 ParallelCharDriver *drv = chr->opaque;
1595 int fd = drv->fd;
1596
1597 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1598 ioctl(fd, PPRELEASE);
1599 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001600 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001601 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001602}
1603
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001604static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001605{
1606 CharDriverState *chr;
1607 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001608
1609 if (ioctl(fd, PPCLAIM) < 0) {
1610 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001611 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001612 }
1613
Anthony Liguori7267c092011-08-20 22:09:37 -05001614 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001615 drv->fd = fd;
1616 drv->mode = IEEE1284_MODE_COMPAT;
1617
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001618 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001619 chr->chr_write = null_chr_write;
1620 chr->chr_ioctl = pp_ioctl;
1621 chr->chr_close = pp_close;
1622 chr->opaque = drv;
1623
Markus Armbruster1f514702012-02-07 15:09:08 +01001624 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001625}
1626#endif /* __linux__ */
1627
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001628#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001629
1630#define HAVE_CHARDEV_PARPORT 1
1631
blueswir16972f932008-11-22 20:49:12 +00001632static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1633{
Stefan Weile0efb992011-02-23 19:09:16 +01001634 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001635 uint8_t b;
1636
1637 switch(cmd) {
1638 case CHR_IOCTL_PP_READ_DATA:
1639 if (ioctl(fd, PPIGDATA, &b) < 0)
1640 return -ENOTSUP;
1641 *(uint8_t *)arg = b;
1642 break;
1643 case CHR_IOCTL_PP_WRITE_DATA:
1644 b = *(uint8_t *)arg;
1645 if (ioctl(fd, PPISDATA, &b) < 0)
1646 return -ENOTSUP;
1647 break;
1648 case CHR_IOCTL_PP_READ_CONTROL:
1649 if (ioctl(fd, PPIGCTRL, &b) < 0)
1650 return -ENOTSUP;
1651 *(uint8_t *)arg = b;
1652 break;
1653 case CHR_IOCTL_PP_WRITE_CONTROL:
1654 b = *(uint8_t *)arg;
1655 if (ioctl(fd, PPISCTRL, &b) < 0)
1656 return -ENOTSUP;
1657 break;
1658 case CHR_IOCTL_PP_READ_STATUS:
1659 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1660 return -ENOTSUP;
1661 *(uint8_t *)arg = b;
1662 break;
1663 default:
1664 return -ENOTSUP;
1665 }
1666 return 0;
1667}
1668
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001669static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001670{
1671 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001672
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001673 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001674 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001675 chr->chr_write = null_chr_write;
1676 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001677 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001678 return chr;
blueswir16972f932008-11-22 20:49:12 +00001679}
1680#endif
1681
aliguori6f97dba2008-10-31 18:49:55 +00001682#else /* _WIN32 */
1683
1684typedef struct {
1685 int max_size;
1686 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001687 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001688 BOOL fpipe;
1689 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001690
1691 /* Protected by the CharDriverState chr_write_lock. */
1692 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001693} WinCharState;
1694
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001695typedef struct {
1696 HANDLE hStdIn;
1697 HANDLE hInputReadyEvent;
1698 HANDLE hInputDoneEvent;
1699 HANDLE hInputThread;
1700 uint8_t win_stdio_buf;
1701} WinStdioCharState;
1702
aliguori6f97dba2008-10-31 18:49:55 +00001703#define NSENDBUF 2048
1704#define NRECVBUF 2048
1705#define MAXCONNECT 1
1706#define NTIMEOUT 5000
1707
1708static int win_chr_poll(void *opaque);
1709static int win_chr_pipe_poll(void *opaque);
1710
1711static void win_chr_close(CharDriverState *chr)
1712{
1713 WinCharState *s = chr->opaque;
1714
1715 if (s->hsend) {
1716 CloseHandle(s->hsend);
1717 s->hsend = NULL;
1718 }
1719 if (s->hrecv) {
1720 CloseHandle(s->hrecv);
1721 s->hrecv = NULL;
1722 }
1723 if (s->hcom) {
1724 CloseHandle(s->hcom);
1725 s->hcom = NULL;
1726 }
1727 if (s->fpipe)
1728 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1729 else
1730 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301731
Hans de Goedea425d232011-11-19 10:22:43 +01001732 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001733}
1734
1735static int win_chr_init(CharDriverState *chr, const char *filename)
1736{
1737 WinCharState *s = chr->opaque;
1738 COMMCONFIG comcfg;
1739 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1740 COMSTAT comstat;
1741 DWORD size;
1742 DWORD err;
1743
1744 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1745 if (!s->hsend) {
1746 fprintf(stderr, "Failed CreateEvent\n");
1747 goto fail;
1748 }
1749 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1750 if (!s->hrecv) {
1751 fprintf(stderr, "Failed CreateEvent\n");
1752 goto fail;
1753 }
1754
1755 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1756 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1757 if (s->hcom == INVALID_HANDLE_VALUE) {
1758 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1759 s->hcom = NULL;
1760 goto fail;
1761 }
1762
1763 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1764 fprintf(stderr, "Failed SetupComm\n");
1765 goto fail;
1766 }
1767
1768 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1769 size = sizeof(COMMCONFIG);
1770 GetDefaultCommConfig(filename, &comcfg, &size);
1771 comcfg.dcb.DCBlength = sizeof(DCB);
1772 CommConfigDialog(filename, NULL, &comcfg);
1773
1774 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1775 fprintf(stderr, "Failed SetCommState\n");
1776 goto fail;
1777 }
1778
1779 if (!SetCommMask(s->hcom, EV_ERR)) {
1780 fprintf(stderr, "Failed SetCommMask\n");
1781 goto fail;
1782 }
1783
1784 cto.ReadIntervalTimeout = MAXDWORD;
1785 if (!SetCommTimeouts(s->hcom, &cto)) {
1786 fprintf(stderr, "Failed SetCommTimeouts\n");
1787 goto fail;
1788 }
1789
1790 if (!ClearCommError(s->hcom, &err, &comstat)) {
1791 fprintf(stderr, "Failed ClearCommError\n");
1792 goto fail;
1793 }
1794 qemu_add_polling_cb(win_chr_poll, chr);
1795 return 0;
1796
1797 fail:
1798 win_chr_close(chr);
1799 return -1;
1800}
1801
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001802/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001803static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1804{
1805 WinCharState *s = chr->opaque;
1806 DWORD len, ret, size, err;
1807
1808 len = len1;
1809 ZeroMemory(&s->osend, sizeof(s->osend));
1810 s->osend.hEvent = s->hsend;
1811 while (len > 0) {
1812 if (s->hsend)
1813 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1814 else
1815 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1816 if (!ret) {
1817 err = GetLastError();
1818 if (err == ERROR_IO_PENDING) {
1819 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1820 if (ret) {
1821 buf += size;
1822 len -= size;
1823 } else {
1824 break;
1825 }
1826 } else {
1827 break;
1828 }
1829 } else {
1830 buf += size;
1831 len -= size;
1832 }
1833 }
1834 return len1 - len;
1835}
1836
1837static int win_chr_read_poll(CharDriverState *chr)
1838{
1839 WinCharState *s = chr->opaque;
1840
Anthony Liguori909cda12011-08-15 11:17:31 -05001841 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001842 return s->max_size;
1843}
1844
1845static void win_chr_readfile(CharDriverState *chr)
1846{
1847 WinCharState *s = chr->opaque;
1848 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301849 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001850 DWORD size;
1851
1852 ZeroMemory(&s->orecv, sizeof(s->orecv));
1853 s->orecv.hEvent = s->hrecv;
1854 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1855 if (!ret) {
1856 err = GetLastError();
1857 if (err == ERROR_IO_PENDING) {
1858 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1859 }
1860 }
1861
1862 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001863 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001864 }
1865}
1866
1867static void win_chr_read(CharDriverState *chr)
1868{
1869 WinCharState *s = chr->opaque;
1870
1871 if (s->len > s->max_size)
1872 s->len = s->max_size;
1873 if (s->len == 0)
1874 return;
1875
1876 win_chr_readfile(chr);
1877}
1878
1879static int win_chr_poll(void *opaque)
1880{
1881 CharDriverState *chr = opaque;
1882 WinCharState *s = chr->opaque;
1883 COMSTAT status;
1884 DWORD comerr;
1885
1886 ClearCommError(s->hcom, &comerr, &status);
1887 if (status.cbInQue > 0) {
1888 s->len = status.cbInQue;
1889 win_chr_read_poll(chr);
1890 win_chr_read(chr);
1891 return 1;
1892 }
1893 return 0;
1894}
1895
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001896static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001897{
1898 CharDriverState *chr;
1899 WinCharState *s;
1900
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001901 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001902 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001903 chr->opaque = s;
1904 chr->chr_write = win_chr_write;
1905 chr->chr_close = win_chr_close;
1906
1907 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001908 g_free(s);
1909 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001910 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001911 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001912 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001913}
1914
1915static int win_chr_pipe_poll(void *opaque)
1916{
1917 CharDriverState *chr = opaque;
1918 WinCharState *s = chr->opaque;
1919 DWORD size;
1920
1921 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1922 if (size > 0) {
1923 s->len = size;
1924 win_chr_read_poll(chr);
1925 win_chr_read(chr);
1926 return 1;
1927 }
1928 return 0;
1929}
1930
1931static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1932{
1933 WinCharState *s = chr->opaque;
1934 OVERLAPPED ov;
1935 int ret;
1936 DWORD size;
1937 char openname[256];
1938
1939 s->fpipe = TRUE;
1940
1941 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1942 if (!s->hsend) {
1943 fprintf(stderr, "Failed CreateEvent\n");
1944 goto fail;
1945 }
1946 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1947 if (!s->hrecv) {
1948 fprintf(stderr, "Failed CreateEvent\n");
1949 goto fail;
1950 }
1951
1952 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1953 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1954 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1955 PIPE_WAIT,
1956 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1957 if (s->hcom == INVALID_HANDLE_VALUE) {
1958 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1959 s->hcom = NULL;
1960 goto fail;
1961 }
1962
1963 ZeroMemory(&ov, sizeof(ov));
1964 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1965 ret = ConnectNamedPipe(s->hcom, &ov);
1966 if (ret) {
1967 fprintf(stderr, "Failed ConnectNamedPipe\n");
1968 goto fail;
1969 }
1970
1971 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1972 if (!ret) {
1973 fprintf(stderr, "Failed GetOverlappedResult\n");
1974 if (ov.hEvent) {
1975 CloseHandle(ov.hEvent);
1976 ov.hEvent = NULL;
1977 }
1978 goto fail;
1979 }
1980
1981 if (ov.hEvent) {
1982 CloseHandle(ov.hEvent);
1983 ov.hEvent = NULL;
1984 }
1985 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1986 return 0;
1987
1988 fail:
1989 win_chr_close(chr);
1990 return -1;
1991}
1992
1993
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001994static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001995{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001996 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001997 CharDriverState *chr;
1998 WinCharState *s;
1999
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002000 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002001 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002002 chr->opaque = s;
2003 chr->chr_write = win_chr_write;
2004 chr->chr_close = win_chr_close;
2005
2006 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002007 g_free(s);
2008 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002009 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002010 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002011 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002012}
2013
Markus Armbruster1f514702012-02-07 15:09:08 +01002014static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002015{
2016 CharDriverState *chr;
2017 WinCharState *s;
2018
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002019 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002020 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002021 s->hcom = fd_out;
2022 chr->opaque = s;
2023 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002024 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002025}
2026
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002027static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002028{
Markus Armbruster1f514702012-02-07 15:09:08 +01002029 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002030}
2031
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002032static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2033{
2034 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2035 DWORD dwSize;
2036 int len1;
2037
2038 len1 = len;
2039
2040 while (len1 > 0) {
2041 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2042 break;
2043 }
2044 buf += dwSize;
2045 len1 -= dwSize;
2046 }
2047
2048 return len - len1;
2049}
2050
2051static void win_stdio_wait_func(void *opaque)
2052{
2053 CharDriverState *chr = opaque;
2054 WinStdioCharState *stdio = chr->opaque;
2055 INPUT_RECORD buf[4];
2056 int ret;
2057 DWORD dwSize;
2058 int i;
2059
Stefan Weildff74242013-12-07 14:48:04 +01002060 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002061
2062 if (!ret) {
2063 /* Avoid error storm */
2064 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2065 return;
2066 }
2067
2068 for (i = 0; i < dwSize; i++) {
2069 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2070
2071 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2072 int j;
2073 if (kev->uChar.AsciiChar != 0) {
2074 for (j = 0; j < kev->wRepeatCount; j++) {
2075 if (qemu_chr_be_can_write(chr)) {
2076 uint8_t c = kev->uChar.AsciiChar;
2077 qemu_chr_be_write(chr, &c, 1);
2078 }
2079 }
2080 }
2081 }
2082 }
2083}
2084
2085static DWORD WINAPI win_stdio_thread(LPVOID param)
2086{
2087 CharDriverState *chr = param;
2088 WinStdioCharState *stdio = chr->opaque;
2089 int ret;
2090 DWORD dwSize;
2091
2092 while (1) {
2093
2094 /* Wait for one byte */
2095 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2096
2097 /* Exit in case of error, continue if nothing read */
2098 if (!ret) {
2099 break;
2100 }
2101 if (!dwSize) {
2102 continue;
2103 }
2104
2105 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2106 if (stdio->win_stdio_buf == '\r') {
2107 continue;
2108 }
2109
2110 /* Signal the main thread and wait until the byte was eaten */
2111 if (!SetEvent(stdio->hInputReadyEvent)) {
2112 break;
2113 }
2114 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2115 != WAIT_OBJECT_0) {
2116 break;
2117 }
2118 }
2119
2120 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2121 return 0;
2122}
2123
2124static void win_stdio_thread_wait_func(void *opaque)
2125{
2126 CharDriverState *chr = opaque;
2127 WinStdioCharState *stdio = chr->opaque;
2128
2129 if (qemu_chr_be_can_write(chr)) {
2130 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2131 }
2132
2133 SetEvent(stdio->hInputDoneEvent);
2134}
2135
2136static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2137{
2138 WinStdioCharState *stdio = chr->opaque;
2139 DWORD dwMode = 0;
2140
2141 GetConsoleMode(stdio->hStdIn, &dwMode);
2142
2143 if (echo) {
2144 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2145 } else {
2146 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2147 }
2148}
2149
2150static void win_stdio_close(CharDriverState *chr)
2151{
2152 WinStdioCharState *stdio = chr->opaque;
2153
2154 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2155 CloseHandle(stdio->hInputReadyEvent);
2156 }
2157 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2158 CloseHandle(stdio->hInputDoneEvent);
2159 }
2160 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2161 TerminateThread(stdio->hInputThread, 0);
2162 }
2163
2164 g_free(chr->opaque);
2165 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002166}
2167
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002168static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002169{
2170 CharDriverState *chr;
2171 WinStdioCharState *stdio;
2172 DWORD dwMode;
2173 int is_console = 0;
2174
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002175 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002176 stdio = g_malloc0(sizeof(WinStdioCharState));
2177
2178 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2179 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2180 fprintf(stderr, "cannot open stdio: invalid handle\n");
2181 exit(1);
2182 }
2183
2184 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2185
2186 chr->opaque = stdio;
2187 chr->chr_write = win_stdio_write;
2188 chr->chr_close = win_stdio_close;
2189
Anthony Liguoried7a1542013-03-05 23:21:17 +05302190 if (is_console) {
2191 if (qemu_add_wait_object(stdio->hStdIn,
2192 win_stdio_wait_func, chr)) {
2193 fprintf(stderr, "qemu_add_wait_object: failed\n");
2194 }
2195 } else {
2196 DWORD dwId;
2197
2198 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2199 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2200 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2201 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002202
Anthony Liguoried7a1542013-03-05 23:21:17 +05302203 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2204 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2205 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2206 fprintf(stderr, "cannot create stdio thread or event\n");
2207 exit(1);
2208 }
2209 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2210 win_stdio_thread_wait_func, chr)) {
2211 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002212 }
2213 }
2214
2215 dwMode |= ENABLE_LINE_INPUT;
2216
Anthony Liguoried7a1542013-03-05 23:21:17 +05302217 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002218 /* set the terminal in raw mode */
2219 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2220 dwMode |= ENABLE_PROCESSED_INPUT;
2221 }
2222
2223 SetConsoleMode(stdio->hStdIn, dwMode);
2224
2225 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2226 qemu_chr_fe_set_echo(chr, false);
2227
Markus Armbruster1f514702012-02-07 15:09:08 +01002228 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002229}
aliguori6f97dba2008-10-31 18:49:55 +00002230#endif /* !_WIN32 */
2231
Anthony Liguori5ab82112013-03-05 23:21:31 +05302232
aliguori6f97dba2008-10-31 18:49:55 +00002233/***********************************************************/
2234/* UDP Net console */
2235
2236typedef struct {
2237 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302238 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302239 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002240 int bufcnt;
2241 int bufptr;
2242 int max_size;
2243} NetCharDriver;
2244
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002245/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002246static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2247{
2248 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302249 gsize bytes_written;
2250 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002251
Anthony Liguori76a96442013-03-05 23:21:21 +05302252 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2253 if (status == G_IO_STATUS_EOF) {
2254 return 0;
2255 } else if (status != G_IO_STATUS_NORMAL) {
2256 return -1;
2257 }
2258
2259 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002260}
2261
2262static int udp_chr_read_poll(void *opaque)
2263{
2264 CharDriverState *chr = opaque;
2265 NetCharDriver *s = chr->opaque;
2266
Anthony Liguori909cda12011-08-15 11:17:31 -05002267 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002268
2269 /* If there were any stray characters in the queue process them
2270 * first
2271 */
2272 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002273 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002274 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002275 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002276 }
2277 return s->max_size;
2278}
2279
Anthony Liguori76a96442013-03-05 23:21:21 +05302280static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002281{
2282 CharDriverState *chr = opaque;
2283 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302284 gsize bytes_read = 0;
2285 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002286
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002287 if (s->max_size == 0) {
2288 return TRUE;
2289 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302290 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2291 &bytes_read, NULL);
2292 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002293 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302294 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302295 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302296 return FALSE;
2297 }
aliguori6f97dba2008-10-31 18:49:55 +00002298
2299 s->bufptr = 0;
2300 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002301 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002302 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002303 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002304 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302305
2306 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002307}
2308
2309static void udp_chr_update_read_handler(CharDriverState *chr)
2310{
2311 NetCharDriver *s = chr->opaque;
2312
Amit Shah26da70c2013-08-28 15:23:37 +05302313 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302314 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302315 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2316 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002317 }
2318}
2319
aliguori819f56b2009-03-28 17:58:14 +00002320static void udp_chr_close(CharDriverState *chr)
2321{
2322 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302323
2324 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302325 if (s->chan) {
2326 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002327 closesocket(s->fd);
2328 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002329 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002330 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002331}
2332
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002333static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002334{
2335 CharDriverState *chr = NULL;
2336 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002337
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002338 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002339 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002340
aliguori6f97dba2008-10-31 18:49:55 +00002341 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302342 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002343 s->bufcnt = 0;
2344 s->bufptr = 0;
2345 chr->opaque = s;
2346 chr->chr_write = udp_chr_write;
2347 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002348 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002349 /* be isn't opened until we get a connection */
2350 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002351 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002352}
aliguori6f97dba2008-10-31 18:49:55 +00002353
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002354static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2355{
2356 Error *local_err = NULL;
2357 int fd = -1;
2358
2359 fd = inet_dgram_opts(opts, &local_err);
2360 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002361 qerror_report_err(local_err);
2362 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002363 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002364 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002365 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002366}
2367
2368/***********************************************************/
2369/* TCP Net console */
2370
2371typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302372
2373 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302374 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002375 int fd, listen_fd;
2376 int connected;
2377 int max_size;
2378 int do_telnetopt;
2379 int do_nodelay;
2380 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002381 int *read_msgfds;
2382 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002383 int *write_msgfds;
2384 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002385} TCPCharDriver;
2386
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302387static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002388
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002389#ifndef _WIN32
2390static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2391{
2392 TCPCharDriver *s = chr->opaque;
2393 struct msghdr msgh;
2394 struct iovec iov;
2395 int r;
2396
2397 size_t fd_size = s->write_msgfds_num * sizeof(int);
2398 char control[CMSG_SPACE(fd_size)];
2399 struct cmsghdr *cmsg;
2400
2401 memset(&msgh, 0, sizeof(msgh));
2402 memset(control, 0, sizeof(control));
2403
2404 /* set the payload */
2405 iov.iov_base = (uint8_t *) buf;
2406 iov.iov_len = len;
2407
2408 msgh.msg_iov = &iov;
2409 msgh.msg_iovlen = 1;
2410
2411 msgh.msg_control = control;
2412 msgh.msg_controllen = sizeof(control);
2413
2414 cmsg = CMSG_FIRSTHDR(&msgh);
2415
2416 cmsg->cmsg_len = CMSG_LEN(fd_size);
2417 cmsg->cmsg_level = SOL_SOCKET;
2418 cmsg->cmsg_type = SCM_RIGHTS;
2419 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2420
2421 do {
2422 r = sendmsg(s->fd, &msgh, 0);
2423 } while (r < 0 && errno == EINTR);
2424
2425 /* free the written msgfds, no matter what */
2426 if (s->write_msgfds_num) {
2427 g_free(s->write_msgfds);
2428 s->write_msgfds = 0;
2429 s->write_msgfds_num = 0;
2430 }
2431
2432 return r;
2433}
2434#endif
2435
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002436/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002437static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2438{
2439 TCPCharDriver *s = chr->opaque;
2440 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002441#ifndef _WIN32
2442 if (s->is_unix && s->write_msgfds_num) {
2443 return unix_send_msgfds(chr, buf, len);
2444 } else
2445#endif
2446 {
2447 return io_channel_send(s->chan, buf, len);
2448 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002449 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002450 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002451 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002452 }
2453}
2454
2455static int tcp_chr_read_poll(void *opaque)
2456{
2457 CharDriverState *chr = opaque;
2458 TCPCharDriver *s = chr->opaque;
2459 if (!s->connected)
2460 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002461 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002462 return s->max_size;
2463}
2464
2465#define IAC 255
2466#define IAC_BREAK 243
2467static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2468 TCPCharDriver *s,
2469 uint8_t *buf, int *size)
2470{
2471 /* Handle any telnet client's basic IAC options to satisfy char by
2472 * char mode with no echo. All IAC options will be removed from
2473 * the buf and the do_telnetopt variable will be used to track the
2474 * state of the width of the IAC information.
2475 *
2476 * IAC commands come in sets of 3 bytes with the exception of the
2477 * "IAC BREAK" command and the double IAC.
2478 */
2479
2480 int i;
2481 int j = 0;
2482
2483 for (i = 0; i < *size; i++) {
2484 if (s->do_telnetopt > 1) {
2485 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2486 /* Double IAC means send an IAC */
2487 if (j != i)
2488 buf[j] = buf[i];
2489 j++;
2490 s->do_telnetopt = 1;
2491 } else {
2492 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2493 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002494 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002495 s->do_telnetopt++;
2496 }
2497 s->do_telnetopt++;
2498 }
2499 if (s->do_telnetopt >= 4) {
2500 s->do_telnetopt = 1;
2501 }
2502 } else {
2503 if ((unsigned char)buf[i] == IAC) {
2504 s->do_telnetopt = 2;
2505 } else {
2506 if (j != i)
2507 buf[j] = buf[i];
2508 j++;
2509 }
2510 }
2511 }
2512 *size = j;
2513}
2514
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002515static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002516{
2517 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002518 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2519
2520 if (to_copy) {
2521 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2522
2523 g_free(s->read_msgfds);
2524 s->read_msgfds = 0;
2525 s->read_msgfds_num = 0;
2526 }
2527
2528 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002529}
2530
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002531static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2532{
2533 TCPCharDriver *s = chr->opaque;
2534
2535 /* clear old pending fd array */
2536 if (s->write_msgfds) {
2537 g_free(s->write_msgfds);
2538 }
2539
2540 if (num) {
2541 s->write_msgfds = g_malloc(num * sizeof(int));
2542 memcpy(s->write_msgfds, fds, num * sizeof(int));
2543 }
2544
2545 s->write_msgfds_num = num;
2546
2547 return 0;
2548}
2549
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002550#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002551static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2552{
2553 TCPCharDriver *s = chr->opaque;
2554 struct cmsghdr *cmsg;
2555
2556 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002557 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002558
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002559 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002560 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002561 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002562 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002563 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002564
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002565 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2566
2567 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002568 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002569 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002570
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002571 /* close and clean read_msgfds */
2572 for (i = 0; i < s->read_msgfds_num; i++) {
2573 close(s->read_msgfds[i]);
2574 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002575
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002576 if (s->read_msgfds_num) {
2577 g_free(s->read_msgfds);
2578 }
2579
2580 s->read_msgfds_num = fd_size / sizeof(int);
2581 s->read_msgfds = g_malloc(fd_size);
2582 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2583
2584 for (i = 0; i < s->read_msgfds_num; i++) {
2585 int fd = s->read_msgfds[i];
2586 if (fd < 0) {
2587 continue;
2588 }
2589
2590 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2591 qemu_set_block(fd);
2592
2593 #ifndef MSG_CMSG_CLOEXEC
2594 qemu_set_cloexec(fd);
2595 #endif
2596 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002597 }
2598}
2599
Mark McLoughlin9977c892009-07-22 09:11:38 +01002600static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2601{
2602 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002603 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002604 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002605 union {
2606 struct cmsghdr cmsg;
2607 char control[CMSG_SPACE(sizeof(int))];
2608 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002609 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002610 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002611
2612 iov[0].iov_base = buf;
2613 iov[0].iov_len = len;
2614
2615 msg.msg_iov = iov;
2616 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002617 msg.msg_control = &msg_control;
2618 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002619
Corey Bryant06138652012-08-14 16:43:42 -04002620#ifdef MSG_CMSG_CLOEXEC
2621 flags |= MSG_CMSG_CLOEXEC;
2622#endif
2623 ret = recvmsg(s->fd, &msg, flags);
2624 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002625 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002626 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002627
2628 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002629}
2630#else
2631static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2632{
2633 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002634 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002635}
2636#endif
2637
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302638static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2639{
2640 TCPCharDriver *s = chr->opaque;
2641 return g_io_create_watch(s->chan, cond);
2642}
2643
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002644static void tcp_chr_disconnect(CharDriverState *chr)
2645{
2646 TCPCharDriver *s = chr->opaque;
2647
2648 s->connected = 0;
2649 if (s->listen_chan) {
2650 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2651 tcp_chr_accept, chr);
2652 }
2653 remove_fd_in_watch(chr);
2654 g_io_channel_unref(s->chan);
2655 s->chan = NULL;
2656 closesocket(s->fd);
2657 s->fd = -1;
2658 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2659}
2660
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302661static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002662{
2663 CharDriverState *chr = opaque;
2664 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302665 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002666 int len, size;
2667
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302668 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002669 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302670 }
aliguori6f97dba2008-10-31 18:49:55 +00002671 len = sizeof(buf);
2672 if (len > s->max_size)
2673 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002674 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002675 if (size == 0) {
2676 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002677 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002678 } else if (size > 0) {
2679 if (s->do_telnetopt)
2680 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2681 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002682 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002683 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302684
2685 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002686}
2687
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002688static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2689{
2690 TCPCharDriver *s = chr->opaque;
2691 int size;
2692
2693 if (!s->connected) {
2694 return 0;
2695 }
2696
2697 size = tcp_chr_recv(chr, (void *) buf, len);
2698 if (size == 0) {
2699 /* connection closed */
2700 tcp_chr_disconnect(chr);
2701 }
2702
2703 return size;
2704}
2705
Blue Swirl68c18d12010-08-15 09:46:24 +00002706#ifndef _WIN32
2707CharDriverState *qemu_chr_open_eventfd(int eventfd)
2708{
David Marchande9d21c42014-06-11 17:25:16 +02002709 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2710
2711 if (chr) {
2712 chr->avail_connections = 1;
2713 }
2714
2715 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002716}
Blue Swirl68c18d12010-08-15 09:46:24 +00002717#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002718
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002719static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2720 void *opaque)
2721{
2722 CharDriverState *chr = opaque;
2723
2724 if (cond != G_IO_HUP) {
2725 return FALSE;
2726 }
2727
2728 /* connection closed */
2729 tcp_chr_disconnect(chr);
2730 if (chr->fd_hup_tag) {
2731 g_source_remove(chr->fd_hup_tag);
2732 chr->fd_hup_tag = 0;
2733 }
2734
2735 return TRUE;
2736}
2737
aliguori6f97dba2008-10-31 18:49:55 +00002738static void tcp_chr_connect(void *opaque)
2739{
2740 CharDriverState *chr = opaque;
2741 TCPCharDriver *s = chr->opaque;
2742
2743 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302744 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302745 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2746 tcp_chr_read, chr);
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002747 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2748 chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002749 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002750 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002751}
2752
Gal Hammerac1b84d2014-02-25 12:12:35 +02002753static void tcp_chr_update_read_handler(CharDriverState *chr)
2754{
2755 TCPCharDriver *s = chr->opaque;
2756
2757 remove_fd_in_watch(chr);
2758 if (s->chan) {
2759 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2760 tcp_chr_read, chr);
2761 }
2762}
2763
aliguori6f97dba2008-10-31 18:49:55 +00002764#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2765static void tcp_chr_telnet_init(int fd)
2766{
2767 char buf[3];
2768 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2769 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2770 send(fd, (char *)buf, 3, 0);
2771 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2772 send(fd, (char *)buf, 3, 0);
2773 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2774 send(fd, (char *)buf, 3, 0);
2775 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2776 send(fd, (char *)buf, 3, 0);
2777}
2778
Daniel P. Berrange13661082011-06-23 13:31:42 +01002779static int tcp_chr_add_client(CharDriverState *chr, int fd)
2780{
2781 TCPCharDriver *s = chr->opaque;
2782 if (s->fd != -1)
2783 return -1;
2784
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002785 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002786 if (s->do_nodelay)
2787 socket_set_nodelay(fd);
2788 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302789 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002790 if (s->listen_tag) {
2791 g_source_remove(s->listen_tag);
2792 s->listen_tag = 0;
2793 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002794 tcp_chr_connect(chr);
2795
2796 return 0;
2797}
2798
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302799static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002800{
2801 CharDriverState *chr = opaque;
2802 TCPCharDriver *s = chr->opaque;
2803 struct sockaddr_in saddr;
2804#ifndef _WIN32
2805 struct sockaddr_un uaddr;
2806#endif
2807 struct sockaddr *addr;
2808 socklen_t len;
2809 int fd;
2810
2811 for(;;) {
2812#ifndef _WIN32
2813 if (s->is_unix) {
2814 len = sizeof(uaddr);
2815 addr = (struct sockaddr *)&uaddr;
2816 } else
2817#endif
2818 {
2819 len = sizeof(saddr);
2820 addr = (struct sockaddr *)&saddr;
2821 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002822 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002823 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002824 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302825 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002826 } else if (fd >= 0) {
2827 if (s->do_telnetopt)
2828 tcp_chr_telnet_init(fd);
2829 break;
2830 }
2831 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002832 if (tcp_chr_add_client(chr, fd) < 0)
2833 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302834
2835 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002836}
2837
2838static void tcp_chr_close(CharDriverState *chr)
2839{
2840 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002841 int i;
aliguori819f56b2009-03-28 17:58:14 +00002842 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302843 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302844 if (s->chan) {
2845 g_io_channel_unref(s->chan);
2846 }
aliguori6f97dba2008-10-31 18:49:55 +00002847 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002848 }
2849 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302850 if (s->listen_tag) {
2851 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002852 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302853 }
2854 if (s->listen_chan) {
2855 g_io_channel_unref(s->listen_chan);
2856 }
aliguori6f97dba2008-10-31 18:49:55 +00002857 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002858 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002859 if (s->read_msgfds_num) {
2860 for (i = 0; i < s->read_msgfds_num; i++) {
2861 close(s->read_msgfds[i]);
2862 }
2863 g_free(s->read_msgfds);
2864 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002865 if (s->write_msgfds_num) {
2866 g_free(s->write_msgfds);
2867 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002868 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002869 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002870}
2871
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002872static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2873 bool is_listen, bool is_telnet,
2874 bool is_waitconnect,
2875 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002876{
2877 CharDriverState *chr = NULL;
2878 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002879 char host[NI_MAXHOST], serv[NI_MAXSERV];
2880 const char *left = "", *right = "";
2881 struct sockaddr_storage ss;
2882 socklen_t ss_len = sizeof(ss);
2883
2884 memset(&ss, 0, ss_len);
2885 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002886 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002887 return NULL;
2888 }
2889
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002890 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002891 s = g_malloc0(sizeof(TCPCharDriver));
2892
2893 s->connected = 0;
2894 s->fd = -1;
2895 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002896 s->read_msgfds = 0;
2897 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002898 s->write_msgfds = 0;
2899 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002900
2901 chr->filename = g_malloc(256);
2902 switch (ss.ss_family) {
2903#ifndef _WIN32
2904 case AF_UNIX:
2905 s->is_unix = 1;
2906 snprintf(chr->filename, 256, "unix:%s%s",
2907 ((struct sockaddr_un *)(&ss))->sun_path,
2908 is_listen ? ",server" : "");
2909 break;
2910#endif
2911 case AF_INET6:
2912 left = "[";
2913 right = "]";
2914 /* fall through */
2915 case AF_INET:
2916 s->do_nodelay = do_nodelay;
2917 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2918 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002919 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002920 is_telnet ? "telnet" : "tcp",
2921 left, host, right, serv,
2922 is_listen ? ",server" : "");
2923 break;
2924 }
2925
2926 chr->opaque = s;
2927 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002928 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002929 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002930 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002931 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002932 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302933 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002934 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002935 /* be isn't opened until we get a connection */
2936 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002937
2938 if (is_listen) {
2939 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302940 s->listen_chan = io_channel_from_socket(s->listen_fd);
2941 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002942 if (is_telnet) {
2943 s->do_telnetopt = 1;
2944 }
2945 } else {
2946 s->connected = 1;
2947 s->fd = fd;
2948 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302949 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002950 tcp_chr_connect(chr);
2951 }
2952
2953 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002954 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2955 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302956 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002957 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002958 }
2959 return chr;
2960}
2961
2962static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2963{
2964 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002965 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002966 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002967
liguange990a392013-06-18 11:45:35 +08002968 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2969 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2970 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2971 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2972 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002973
aliguorif07b6002008-11-11 20:54:09 +00002974 if (is_unix) {
2975 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002976 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002977 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002978 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002979 }
2980 } else {
2981 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002982 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002983 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002984 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002985 }
2986 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002987 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002988 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002989 }
aliguori6f97dba2008-10-31 18:49:55 +00002990
2991 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002992 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002993
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002994 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2995 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002996 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002997 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002998 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002999 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003000
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003001
aliguori6f97dba2008-10-31 18:49:55 +00003002 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003003 if (local_err) {
3004 qerror_report_err(local_err);
3005 error_free(local_err);
3006 }
3007 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00003008 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003009 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003010 if (chr) {
3011 g_free(chr->opaque);
3012 g_free(chr);
3013 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003014 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003015}
3016
Lei Li51767e72013-01-25 00:03:19 +08003017/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003018/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003019
3020typedef struct {
3021 size_t size;
3022 size_t prod;
3023 size_t cons;
3024 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003025} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003026
Markus Armbruster3949e592013-02-06 21:27:24 +01003027static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003028{
Markus Armbruster3949e592013-02-06 21:27:24 +01003029 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003030
Markus Armbruster5c230102013-02-06 21:27:23 +01003031 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003032}
3033
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003034/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003035static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003036{
Markus Armbruster3949e592013-02-06 21:27:24 +01003037 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003038 int i;
3039
3040 if (!buf || (len < 0)) {
3041 return -1;
3042 }
3043
3044 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003045 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3046 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003047 d->cons = d->prod - d->size;
3048 }
3049 }
3050
3051 return 0;
3052}
3053
Markus Armbruster3949e592013-02-06 21:27:24 +01003054static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003055{
Markus Armbruster3949e592013-02-06 21:27:24 +01003056 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003057 int i;
3058
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003059 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003060 for (i = 0; i < len && d->cons != d->prod; i++) {
3061 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003062 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003063 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003064
3065 return i;
3066}
3067
Markus Armbruster3949e592013-02-06 21:27:24 +01003068static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003069{
Markus Armbruster3949e592013-02-06 21:27:24 +01003070 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003071
3072 g_free(d->cbuf);
3073 g_free(d);
3074 chr->opaque = NULL;
3075}
3076
Markus Armbruster4f573782013-07-26 16:44:32 +02003077static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3078 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003079{
3080 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003081 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003082
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003083 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003084 d = g_malloc(sizeof(*d));
3085
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003086 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003087
3088 /* The size must be power of 2 */
3089 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003090 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003091 goto fail;
3092 }
3093
3094 d->prod = 0;
3095 d->cons = 0;
3096 d->cbuf = g_malloc0(d->size);
3097
3098 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003099 chr->chr_write = ringbuf_chr_write;
3100 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003101
3102 return chr;
3103
3104fail:
3105 g_free(d);
3106 g_free(chr);
3107 return NULL;
3108}
3109
Hani Benhabiles8e597772014-05-27 23:39:30 +01003110bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003111{
Markus Armbruster3949e592013-02-06 21:27:24 +01003112 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003113}
3114
Markus Armbruster3949e592013-02-06 21:27:24 +01003115void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003116 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003117 Error **errp)
3118{
3119 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003120 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003121 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003122 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003123
3124 chr = qemu_chr_find(device);
3125 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003126 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003127 return;
3128 }
3129
Markus Armbruster3949e592013-02-06 21:27:24 +01003130 if (!chr_is_ringbuf(chr)) {
3131 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003132 return;
3133 }
3134
Lei Li1f590cf2013-01-25 00:03:20 +08003135 if (has_format && (format == DATA_FORMAT_BASE64)) {
3136 write_data = g_base64_decode(data, &write_count);
3137 } else {
3138 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003139 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003140 }
3141
Markus Armbruster3949e592013-02-06 21:27:24 +01003142 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003143
Markus Armbruster13289fb2013-02-06 21:27:18 +01003144 if (write_data != (uint8_t *)data) {
3145 g_free((void *)write_data);
3146 }
3147
Lei Li1f590cf2013-01-25 00:03:20 +08003148 if (ret < 0) {
3149 error_setg(errp, "Failed to write to device %s", device);
3150 return;
3151 }
3152}
3153
Markus Armbruster3949e592013-02-06 21:27:24 +01003154char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003155 bool has_format, enum DataFormat format,
3156 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003157{
3158 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003159 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003160 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003161 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003162
3163 chr = qemu_chr_find(device);
3164 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003165 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003166 return NULL;
3167 }
3168
Markus Armbruster3949e592013-02-06 21:27:24 +01003169 if (!chr_is_ringbuf(chr)) {
3170 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003171 return NULL;
3172 }
3173
3174 if (size <= 0) {
3175 error_setg(errp, "size must be greater than zero");
3176 return NULL;
3177 }
3178
Markus Armbruster3949e592013-02-06 21:27:24 +01003179 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003180 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003181 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003182
Markus Armbruster3949e592013-02-06 21:27:24 +01003183 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003184
3185 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003186 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003187 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003188 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003189 /*
3190 * FIXME should read only complete, valid UTF-8 characters up
3191 * to @size bytes. Invalid sequences should be replaced by a
3192 * suitable replacement character. Except when (and only
3193 * when) ring buffer lost characters since last read, initial
3194 * continuation characters should be dropped.
3195 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003196 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003197 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003198 }
3199
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003200 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003201}
3202
Gerd Hoffmann33521632009-12-08 13:11:49 +01003203QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003204{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003205 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003206 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003207 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003208 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003209 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003210
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003211 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003212 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003213 qerror_report_err(local_err);
3214 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003215 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003216 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003217
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003218 if (strstart(filename, "mon:", &p)) {
3219 filename = p;
3220 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003221 if (strcmp(filename, "stdio") == 0) {
3222 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3223 * but pass it to the guest. Handle this only for compat syntax,
3224 * for -chardev syntax we have special option for this.
3225 * This is what -nographic did, redirecting+muxing serial+monitor
3226 * to stdio causing Ctrl+C to be passed to guest. */
3227 qemu_opt_set(opts, "signal", "off");
3228 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003229 }
3230
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003231 if (strcmp(filename, "null") == 0 ||
3232 strcmp(filename, "pty") == 0 ||
3233 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003234 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003235 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003236 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003237 return opts;
3238 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003239 if (strstart(filename, "vc", &p)) {
3240 qemu_opt_set(opts, "backend", "vc");
3241 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003242 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003243 /* pixels */
3244 qemu_opt_set(opts, "width", width);
3245 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003246 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003247 /* chars */
3248 qemu_opt_set(opts, "cols", width);
3249 qemu_opt_set(opts, "rows", height);
3250 } else {
3251 goto fail;
3252 }
3253 }
3254 return opts;
3255 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003256 if (strcmp(filename, "con:") == 0) {
3257 qemu_opt_set(opts, "backend", "console");
3258 return opts;
3259 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003260 if (strstart(filename, "COM", NULL)) {
3261 qemu_opt_set(opts, "backend", "serial");
3262 qemu_opt_set(opts, "path", filename);
3263 return opts;
3264 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003265 if (strstart(filename, "file:", &p)) {
3266 qemu_opt_set(opts, "backend", "file");
3267 qemu_opt_set(opts, "path", p);
3268 return opts;
3269 }
3270 if (strstart(filename, "pipe:", &p)) {
3271 qemu_opt_set(opts, "backend", "pipe");
3272 qemu_opt_set(opts, "path", p);
3273 return opts;
3274 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003275 if (strstart(filename, "tcp:", &p) ||
3276 strstart(filename, "telnet:", &p)) {
3277 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3278 host[0] = 0;
3279 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3280 goto fail;
3281 }
3282 qemu_opt_set(opts, "backend", "socket");
3283 qemu_opt_set(opts, "host", host);
3284 qemu_opt_set(opts, "port", port);
3285 if (p[pos] == ',') {
3286 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3287 goto fail;
3288 }
3289 if (strstart(filename, "telnet:", &p))
3290 qemu_opt_set(opts, "telnet", "on");
3291 return opts;
3292 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003293 if (strstart(filename, "udp:", &p)) {
3294 qemu_opt_set(opts, "backend", "udp");
3295 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3296 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003297 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003298 goto fail;
3299 }
3300 }
3301 qemu_opt_set(opts, "host", host);
3302 qemu_opt_set(opts, "port", port);
3303 if (p[pos] == '@') {
3304 p += pos + 1;
3305 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3306 host[0] = 0;
3307 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003308 goto fail;
3309 }
3310 }
3311 qemu_opt_set(opts, "localaddr", host);
3312 qemu_opt_set(opts, "localport", port);
3313 }
3314 return opts;
3315 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003316 if (strstart(filename, "unix:", &p)) {
3317 qemu_opt_set(opts, "backend", "socket");
3318 if (qemu_opts_do_parse(opts, p, "path") != 0)
3319 goto fail;
3320 return opts;
3321 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003322 if (strstart(filename, "/dev/parport", NULL) ||
3323 strstart(filename, "/dev/ppi", NULL)) {
3324 qemu_opt_set(opts, "backend", "parport");
3325 qemu_opt_set(opts, "path", filename);
3326 return opts;
3327 }
3328 if (strstart(filename, "/dev/", NULL)) {
3329 qemu_opt_set(opts, "backend", "tty");
3330 qemu_opt_set(opts, "path", filename);
3331 return opts;
3332 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003333
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003334fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003335 qemu_opts_del(opts);
3336 return NULL;
3337}
3338
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003339static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3340 Error **errp)
3341{
3342 const char *path = qemu_opt_get(opts, "path");
3343
3344 if (path == NULL) {
3345 error_setg(errp, "chardev: file: no filename given");
3346 return;
3347 }
3348 backend->file = g_new0(ChardevFile, 1);
3349 backend->file->out = g_strdup(path);
3350}
3351
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003352static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3353 Error **errp)
3354{
3355 backend->stdio = g_new0(ChardevStdio, 1);
3356 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003357 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003358}
3359
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003360static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3361 Error **errp)
3362{
3363 const char *device = qemu_opt_get(opts, "path");
3364
3365 if (device == NULL) {
3366 error_setg(errp, "chardev: serial/tty: no device path given");
3367 return;
3368 }
3369 backend->serial = g_new0(ChardevHostdev, 1);
3370 backend->serial->device = g_strdup(device);
3371}
3372
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003373static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3374 Error **errp)
3375{
3376 const char *device = qemu_opt_get(opts, "path");
3377
3378 if (device == NULL) {
3379 error_setg(errp, "chardev: parallel: no device path given");
3380 return;
3381 }
3382 backend->parallel = g_new0(ChardevHostdev, 1);
3383 backend->parallel->device = g_strdup(device);
3384}
3385
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003386static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3387 Error **errp)
3388{
3389 const char *device = qemu_opt_get(opts, "path");
3390
3391 if (device == NULL) {
3392 error_setg(errp, "chardev: pipe: no device path given");
3393 return;
3394 }
3395 backend->pipe = g_new0(ChardevHostdev, 1);
3396 backend->pipe->device = g_strdup(device);
3397}
3398
Markus Armbruster4f573782013-07-26 16:44:32 +02003399static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3400 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003401{
3402 int val;
3403
Markus Armbruster3a1da422013-07-26 16:44:34 +02003404 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003405
Markus Armbruster0f953052013-06-27 16:22:07 +02003406 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003407 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003408 backend->ringbuf->has_size = true;
3409 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003410 }
3411}
3412
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003413static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3414 Error **errp)
3415{
3416 const char *chardev = qemu_opt_get(opts, "chardev");
3417
3418 if (chardev == NULL) {
3419 error_setg(errp, "chardev: mux: no chardev given");
3420 return;
3421 }
3422 backend->mux = g_new0(ChardevMux, 1);
3423 backend->mux->chardev = g_strdup(chardev);
3424}
3425
Anthony Liguorid654f342013-03-05 23:21:28 +05303426typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003427 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003428 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003429 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003430 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003431 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003432 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303433} CharDriver;
3434
3435static GSList *backends;
3436
3437void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3438{
3439 CharDriver *s;
3440
3441 s = g_malloc0(sizeof(*s));
3442 s->name = g_strdup(name);
3443 s->open = open;
3444
3445 backends = g_slist_append(backends, s);
3446}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003447
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003448void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003449 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3450{
3451 CharDriver *s;
3452
3453 s = g_malloc0(sizeof(*s));
3454 s->name = g_strdup(name);
3455 s->kind = kind;
3456 s->parse = parse;
3457
3458 backends = g_slist_append(backends, s);
3459}
3460
Anthony Liguorif69554b2011-08-15 11:17:37 -05003461CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003462 void (*init)(struct CharDriverState *s),
3463 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003464{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003465 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303466 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003467 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303468 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003469
3470 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003471 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003472 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003473 }
3474
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003475 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003476 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003477 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003478 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003479 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303480 for (i = backends; i; i = i->next) {
3481 cd = i->data;
3482
3483 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003484 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303485 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003486 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303487 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003488 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003489 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003490 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003491 }
3492
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003493 if (!cd->open) {
3494 /* using new, qapi init */
3495 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3496 ChardevReturn *ret = NULL;
3497 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003498 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003499
3500 if (qemu_opt_get_bool(opts, "mux", 0)) {
3501 bid = g_strdup_printf("%s-base", id);
3502 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003503
3504 chr = NULL;
3505 backend->kind = cd->kind;
3506 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003507 cd->parse(opts, backend, &local_err);
3508 if (local_err) {
3509 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003510 goto qapi_out;
3511 }
3512 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003513 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003514 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003515 goto qapi_out;
3516 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003517
3518 if (bid) {
3519 qapi_free_ChardevBackend(backend);
3520 qapi_free_ChardevReturn(ret);
3521 backend = g_new0(ChardevBackend, 1);
3522 backend->mux = g_new0(ChardevMux, 1);
3523 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3524 backend->mux->chardev = g_strdup(bid);
3525 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003526 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003527 chr = qemu_chr_find(bid);
3528 qemu_chr_delete(chr);
3529 chr = NULL;
3530 goto qapi_out;
3531 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003532 }
3533
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003534 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003535 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003536
3537 qapi_out:
3538 qapi_free_ChardevBackend(backend);
3539 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003540 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003541 return chr;
3542 }
3543
Anthony Liguorid654f342013-03-05 23:21:28 +05303544 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003545 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003546 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003547 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003548 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003549 }
3550
3551 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003552 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003553 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003554 /* if we didn't create the chardev via qmp_chardev_add, we
3555 * need to send the OPENED event here
3556 */
3557 if (!chr->explicit_be_open) {
3558 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3559 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003560 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003561
3562 if (qemu_opt_get_bool(opts, "mux", 0)) {
3563 CharDriverState *base = chr;
3564 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003565 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003566 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3567 chr = qemu_chr_open_mux(base);
3568 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003569 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003570 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003571 } else {
3572 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003573 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003574 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003575 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003576 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003577
3578err:
3579 qemu_opts_del(opts);
3580 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003581}
3582
Anthony Liguori27143a42011-08-15 11:17:36 -05003583CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003584{
3585 const char *p;
3586 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003587 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003588 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003589
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003590 if (strstart(filename, "chardev:", &p)) {
3591 return qemu_chr_find(p);
3592 }
3593
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003594 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003595 if (!opts)
3596 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003597
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003598 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003599 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003600 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003601 error_free(err);
3602 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003603 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003604 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003605 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003606 }
3607 return chr;
3608}
3609
Anthony Liguori15f31512011-08-15 11:17:35 -05003610void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003611{
3612 if (chr->chr_set_echo) {
3613 chr->chr_set_echo(chr, echo);
3614 }
3615}
3616
Hans de Goede8e25daa2013-03-26 11:07:57 +01003617void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003618{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003619 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003620 return;
3621 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003622 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003623 if (chr->chr_set_fe_open) {
3624 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003625 }
3626}
3627
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003628void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3629{
3630 if (chr->chr_fe_event) {
3631 chr->chr_fe_event(chr, event);
3632 }
3633}
3634
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003635int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3636 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303637{
3638 GSource *src;
3639 guint tag;
3640
3641 if (s->chr_add_watch == NULL) {
3642 return -ENOSYS;
3643 }
3644
3645 src = s->chr_add_watch(s, cond);
3646 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3647 tag = g_source_attach(src, NULL);
3648 g_source_unref(src);
3649
3650 return tag;
3651}
3652
Hans de Goede44c473d2013-03-27 20:29:39 +01003653int qemu_chr_fe_claim(CharDriverState *s)
3654{
3655 if (s->avail_connections < 1) {
3656 return -1;
3657 }
3658 s->avail_connections--;
3659 return 0;
3660}
3661
3662void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3663{
3664 if (qemu_chr_fe_claim(s) != 0) {
3665 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3666 __func__, s->label);
3667 exit(1);
3668 }
3669}
3670
3671void qemu_chr_fe_release(CharDriverState *s)
3672{
3673 s->avail_connections++;
3674}
3675
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003676void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003677{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003678 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003679 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003680 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003681 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003682 g_free(chr->filename);
3683 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003684 if (chr->opts) {
3685 qemu_opts_del(chr->opts);
3686 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003687 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003688}
3689
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003690ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003691{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003692 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003693 CharDriverState *chr;
3694
Blue Swirl72cf2d42009-09-12 07:36:22 +00003695 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003696 ChardevInfoList *info = g_malloc0(sizeof(*info));
3697 info->value = g_malloc0(sizeof(*info->value));
3698 info->value->label = g_strdup(chr->label);
3699 info->value->filename = g_strdup(chr->filename);
3700
3701 info->next = chr_list;
3702 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003703 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003704
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003705 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003706}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003707
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003708ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3709{
3710 ChardevBackendInfoList *backend_list = NULL;
3711 CharDriver *c = NULL;
3712 GSList *i = NULL;
3713
3714 for (i = backends; i; i = i->next) {
3715 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3716 c = i->data;
3717 info->value = g_malloc0(sizeof(*info->value));
3718 info->value->name = g_strdup(c->name);
3719
3720 info->next = backend_list;
3721 backend_list = info;
3722 }
3723
3724 return backend_list;
3725}
3726
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003727CharDriverState *qemu_chr_find(const char *name)
3728{
3729 CharDriverState *chr;
3730
Blue Swirl72cf2d42009-09-12 07:36:22 +00003731 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003732 if (strcmp(chr->label, name) != 0)
3733 continue;
3734 return chr;
3735 }
3736 return NULL;
3737}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003738
3739/* Get a character (serial) device interface. */
3740CharDriverState *qemu_char_get_next_serial(void)
3741{
3742 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003743 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003744
3745 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003746
3747 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3748 chr = serial_hds[next_serial++];
3749 qemu_chr_fe_claim_no_fail(chr);
3750 return chr;
3751 }
3752 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003753}
3754
Paolo Bonzini4d454572012-11-26 16:03:42 +01003755QemuOptsList qemu_chardev_opts = {
3756 .name = "chardev",
3757 .implied_opt_name = "backend",
3758 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3759 .desc = {
3760 {
3761 .name = "backend",
3762 .type = QEMU_OPT_STRING,
3763 },{
3764 .name = "path",
3765 .type = QEMU_OPT_STRING,
3766 },{
3767 .name = "host",
3768 .type = QEMU_OPT_STRING,
3769 },{
3770 .name = "port",
3771 .type = QEMU_OPT_STRING,
3772 },{
3773 .name = "localaddr",
3774 .type = QEMU_OPT_STRING,
3775 },{
3776 .name = "localport",
3777 .type = QEMU_OPT_STRING,
3778 },{
3779 .name = "to",
3780 .type = QEMU_OPT_NUMBER,
3781 },{
3782 .name = "ipv4",
3783 .type = QEMU_OPT_BOOL,
3784 },{
3785 .name = "ipv6",
3786 .type = QEMU_OPT_BOOL,
3787 },{
3788 .name = "wait",
3789 .type = QEMU_OPT_BOOL,
3790 },{
3791 .name = "server",
3792 .type = QEMU_OPT_BOOL,
3793 },{
3794 .name = "delay",
3795 .type = QEMU_OPT_BOOL,
3796 },{
3797 .name = "telnet",
3798 .type = QEMU_OPT_BOOL,
3799 },{
3800 .name = "width",
3801 .type = QEMU_OPT_NUMBER,
3802 },{
3803 .name = "height",
3804 .type = QEMU_OPT_NUMBER,
3805 },{
3806 .name = "cols",
3807 .type = QEMU_OPT_NUMBER,
3808 },{
3809 .name = "rows",
3810 .type = QEMU_OPT_NUMBER,
3811 },{
3812 .name = "mux",
3813 .type = QEMU_OPT_BOOL,
3814 },{
3815 .name = "signal",
3816 .type = QEMU_OPT_BOOL,
3817 },{
3818 .name = "name",
3819 .type = QEMU_OPT_STRING,
3820 },{
3821 .name = "debug",
3822 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003823 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003824 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003825 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003826 },{
3827 .name = "chardev",
3828 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003829 },
3830 { /* end of list */ }
3831 },
3832};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003833
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003834#ifdef _WIN32
3835
3836static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3837{
3838 HANDLE out;
3839
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003840 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003841 error_setg(errp, "input file not supported");
3842 return NULL;
3843 }
3844
3845 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3846 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3847 if (out == INVALID_HANDLE_VALUE) {
3848 error_setg(errp, "open %s failed", file->out);
3849 return NULL;
3850 }
3851 return qemu_chr_open_win_file(out);
3852}
3853
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003854static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3855 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003856{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003857 return qemu_chr_open_win_path(serial->device);
3858}
3859
3860static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3861 Error **errp)
3862{
3863 error_setg(errp, "character device backend type 'parallel' not supported");
3864 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003865}
3866
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003867#else /* WIN32 */
3868
3869static int qmp_chardev_open_file_source(char *src, int flags,
3870 Error **errp)
3871{
3872 int fd = -1;
3873
3874 TFR(fd = qemu_open(src, flags, 0666));
3875 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003876 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003877 }
3878 return fd;
3879}
3880
3881static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3882{
Markus Armbruster5f758362014-05-19 18:57:34 +02003883 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003884
3885 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3886 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003887 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003888 return NULL;
3889 }
3890
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003891 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003892 flags = O_RDONLY;
3893 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003894 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003895 qemu_close(out);
3896 return NULL;
3897 }
3898 }
3899
3900 return qemu_chr_open_fd(in, out);
3901}
3902
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003903static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3904 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003905{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003906#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003907 int fd;
3908
3909 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003910 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003911 return NULL;
3912 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003913 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003914 return qemu_chr_open_tty_fd(fd);
3915#else
3916 error_setg(errp, "character device backend type 'serial' not supported");
3917 return NULL;
3918#endif
3919}
3920
3921static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3922 Error **errp)
3923{
3924#ifdef HAVE_CHARDEV_PARPORT
3925 int fd;
3926
3927 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003928 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003929 return NULL;
3930 }
3931 return qemu_chr_open_pp_fd(fd);
3932#else
3933 error_setg(errp, "character device backend type 'parallel' not supported");
3934 return NULL;
3935#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003936}
3937
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003938#endif /* WIN32 */
3939
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003940static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3941 Error **errp)
3942{
3943 SocketAddress *addr = sock->addr;
3944 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3945 bool is_listen = sock->has_server ? sock->server : true;
3946 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3947 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3948 int fd;
3949
3950 if (is_listen) {
3951 fd = socket_listen(addr, errp);
3952 } else {
3953 fd = socket_connect(addr, errp, NULL, NULL);
3954 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003955 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003956 return NULL;
3957 }
3958 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3959 is_telnet, is_waitconnect, errp);
3960}
3961
Lei Li08d0ab32013-05-20 14:51:03 +08003962static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3963 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003964{
3965 int fd;
3966
Lei Li08d0ab32013-05-20 14:51:03 +08003967 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003968 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003969 return NULL;
3970 }
3971 return qemu_chr_open_udp_fd(fd);
3972}
3973
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003974ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3975 Error **errp)
3976{
3977 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003978 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003979
3980 chr = qemu_chr_find(id);
3981 if (chr) {
3982 error_setg(errp, "Chardev '%s' already exists", id);
3983 g_free(ret);
3984 return NULL;
3985 }
3986
3987 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003988 case CHARDEV_BACKEND_KIND_FILE:
3989 chr = qmp_chardev_open_file(backend->file, errp);
3990 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003991 case CHARDEV_BACKEND_KIND_SERIAL:
3992 chr = qmp_chardev_open_serial(backend->serial, errp);
3993 break;
3994 case CHARDEV_BACKEND_KIND_PARALLEL:
3995 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003996 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003997 case CHARDEV_BACKEND_KIND_PIPE:
3998 chr = qemu_chr_open_pipe(backend->pipe);
3999 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004000 case CHARDEV_BACKEND_KIND_SOCKET:
4001 chr = qmp_chardev_open_socket(backend->socket, errp);
4002 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004003 case CHARDEV_BACKEND_KIND_UDP:
4004 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004005 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004006#ifdef HAVE_CHARDEV_TTY
4007 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004008 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004009 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004010#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004011 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004012 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004013 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004014 case CHARDEV_BACKEND_KIND_MUX:
4015 base = qemu_chr_find(backend->mux->chardev);
4016 if (base == NULL) {
4017 error_setg(errp, "mux: base chardev %s not found",
4018 backend->mux->chardev);
4019 break;
4020 }
4021 chr = qemu_chr_open_mux(base);
4022 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004023 case CHARDEV_BACKEND_KIND_MSMOUSE:
4024 chr = qemu_chr_open_msmouse();
4025 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004026#ifdef CONFIG_BRLAPI
4027 case CHARDEV_BACKEND_KIND_BRAILLE:
4028 chr = chr_baum_init();
4029 break;
4030#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004031 case CHARDEV_BACKEND_KIND_STDIO:
4032 chr = qemu_chr_open_stdio(backend->stdio);
4033 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004034#ifdef _WIN32
4035 case CHARDEV_BACKEND_KIND_CONSOLE:
4036 chr = qemu_chr_open_win_con();
4037 break;
4038#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004039#ifdef CONFIG_SPICE
4040 case CHARDEV_BACKEND_KIND_SPICEVMC:
4041 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4042 break;
4043 case CHARDEV_BACKEND_KIND_SPICEPORT:
4044 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4045 break;
4046#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004047 case CHARDEV_BACKEND_KIND_VC:
4048 chr = vc_init(backend->vc);
4049 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004050 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004051 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004052 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004053 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004054 default:
4055 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4056 break;
4057 }
4058
Markus Armbruster3894c782014-05-19 18:57:36 +02004059 /*
4060 * Character backend open hasn't been fully converted to the Error
4061 * API. Some opens fail without setting an error. Set a generic
4062 * error then.
4063 * TODO full conversion to Error API
4064 */
4065 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004066 error_setg(errp, "Failed to create chardev");
4067 }
4068 if (chr) {
4069 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004070 chr->avail_connections =
4071 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004072 if (!chr->filename) {
4073 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4074 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004075 if (!chr->explicit_be_open) {
4076 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4077 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004078 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4079 return ret;
4080 } else {
4081 g_free(ret);
4082 return NULL;
4083 }
4084}
4085
4086void qmp_chardev_remove(const char *id, Error **errp)
4087{
4088 CharDriverState *chr;
4089
4090 chr = qemu_chr_find(id);
4091 if (NULL == chr) {
4092 error_setg(errp, "Chardev '%s' not found", id);
4093 return;
4094 }
4095 if (chr->chr_can_read || chr->chr_read ||
4096 chr->chr_event || chr->handler_opaque) {
4097 error_setg(errp, "Chardev '%s' is busy", id);
4098 return;
4099 }
4100 qemu_chr_delete(chr);
4101}
Anthony Liguorid654f342013-03-05 23:21:28 +05304102
4103static void register_types(void)
4104{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004105 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304106 register_char_driver("socket", qemu_chr_open_socket);
4107 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004108 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004109 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004110 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4111 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004112 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4113 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004114 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4115 qemu_chr_parse_serial);
4116 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4117 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004118 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4119 qemu_chr_parse_parallel);
4120 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4121 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004122 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004123 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004124 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4125 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004126 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4127 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004128 /* Bug-compatibility: */
4129 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4130 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004131 /* this must be done after machine init, since we register FEs with muxes
4132 * as part of realize functions like serial_isa_realizefn when -nographic
4133 * is specified
4134 */
4135 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304136}
4137
4138type_init(register_types);