blob: 28ea9f20dfe451dbae8b3eeb7231d69be70cab34 [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{
124 return s->chr_write(s, buf, len);
125}
126
Anthony Liguoricd187202013-03-26 10:04:17 -0500127int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
128{
129 int offset = 0;
130 int res;
131
132 while (offset < len) {
133 do {
134 res = s->chr_write(s, buf + offset, len - offset);
135 if (res == -1 && errno == EAGAIN) {
136 g_usleep(100);
137 }
138 } while (res == -1 && errno == EAGAIN);
139
140 if (res == 0) {
141 break;
142 }
143
144 if (res < 0) {
145 return res;
146 }
147
148 offset += res;
149 }
150
151 return offset;
152}
153
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300154int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
155{
156 int offset = 0, counter = 10;
157 int res;
158
159 if (!s->chr_sync_read) {
160 return 0;
161 }
162
163 while (offset < len) {
164 do {
165 res = s->chr_sync_read(s, buf + offset, len - offset);
166 if (res == -1 && errno == EAGAIN) {
167 g_usleep(100);
168 }
169 } while (res == -1 && errno == EAGAIN);
170
171 if (res == 0) {
172 break;
173 }
174
175 if (res < 0) {
176 return res;
177 }
178
179 offset += res;
180
181 if (!counter--) {
182 break;
183 }
184 }
185
186 return offset;
187}
188
Anthony Liguori41084f12011-08-15 11:17:34 -0500189int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000190{
191 if (!s->chr_ioctl)
192 return -ENOTSUP;
193 return s->chr_ioctl(s, cmd, arg);
194}
195
Anthony Liguori909cda12011-08-15 11:17:31 -0500196int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000197{
198 if (!s->chr_can_read)
199 return 0;
200 return s->chr_can_read(s->handler_opaque);
201}
202
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500203void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000204{
Stefan Weilac310732012-04-19 22:27:14 +0200205 if (s->chr_read) {
206 s->chr_read(s->handler_opaque, buf, len);
207 }
aliguori6f97dba2008-10-31 18:49:55 +0000208}
209
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500210int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100211{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300212 int fd;
213 return (qemu_chr_fe_get_msgfds(s, &fd, 1) >= 0) ? fd : -1;
214}
215
216int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
217{
218 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100219}
220
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300221int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
222{
223 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
224}
225
Daniel P. Berrange13661082011-06-23 13:31:42 +0100226int qemu_chr_add_client(CharDriverState *s, int fd)
227{
228 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
229}
230
aliguori6f97dba2008-10-31 18:49:55 +0000231void qemu_chr_accept_input(CharDriverState *s)
232{
233 if (s->chr_accept_input)
234 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100235 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000236}
237
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500238void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000239{
Amit Shah9bd78542009-11-03 19:59:54 +0530240 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000241 va_list ap;
242 va_start(ap, fmt);
243 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500244 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000245 va_end(ap);
246}
247
Amit Shah386a5a12013-08-28 15:24:05 +0530248static void remove_fd_in_watch(CharDriverState *chr);
249
aliguori6f97dba2008-10-31 18:49:55 +0000250void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100251 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000252 IOReadHandler *fd_read,
253 IOEventHandler *fd_event,
254 void *opaque)
255{
Hans de Goede19083222013-03-26 11:07:56 +0100256 int fe_open;
257
Amit Shahda7d9982011-04-25 15:18:22 +0530258 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100259 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530260 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100261 } else {
262 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530263 }
aliguori6f97dba2008-10-31 18:49:55 +0000264 s->chr_can_read = fd_can_read;
265 s->chr_read = fd_read;
266 s->chr_event = fd_event;
267 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200268 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000269 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200270
Hans de Goede19083222013-03-26 11:07:56 +0100271 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100272 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100273 }
274
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200275 /* We're connecting to an already opened device, so let's make sure we
276 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100277 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100278 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200279 }
aliguori6f97dba2008-10-31 18:49:55 +0000280}
281
282static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
283{
284 return len;
285}
286
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100287static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000288{
289 CharDriverState *chr;
290
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200291 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000292 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500293 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100294 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000295}
296
297/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000298#define MAX_MUX 4
299#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
300#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
301typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100302 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000303 IOReadHandler *chr_read[MAX_MUX];
304 IOEventHandler *chr_event[MAX_MUX];
305 void *ext_opaque[MAX_MUX];
306 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200307 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000308 int mux_cnt;
309 int term_got_escape;
310 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000311 /* Intermediate input buffer allows to catch escape sequences even if the
312 currently active device is not accepting any input - but only until it
313 is full as well. */
314 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
315 int prod[MAX_MUX];
316 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200317 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200318 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200319 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000320} MuxDriver;
321
322
323static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
324{
325 MuxDriver *d = chr->opaque;
326 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200327 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200328 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000329 } else {
330 int i;
331
332 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200333 for (i = 0; i < len; i++) {
334 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000335 char buf1[64];
336 int64_t ti;
337 int secs;
338
Alex Blighbc72ad62013-08-21 16:03:08 +0100339 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200340 if (d->timestamps_start == -1)
341 d->timestamps_start = ti;
342 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000343 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000344 snprintf(buf1, sizeof(buf1),
345 "[%02d:%02d:%02d.%03d] ",
346 secs / 3600,
347 (secs / 60) % 60,
348 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000349 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200350 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200351 d->linestart = 0;
352 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200353 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200354 if (buf[i] == '\n') {
355 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000356 }
357 }
358 }
359 return ret;
360}
361
362static const char * const mux_help[] = {
363 "% h print this help\n\r",
364 "% x exit emulator\n\r",
365 "% s save disk data back to file (if -snapshot)\n\r",
366 "% t toggle console timestamps\n\r"
367 "% b send break (magic sysrq)\n\r",
368 "% c switch between console and monitor\n\r",
369 "% % sends %\n\r",
370 NULL
371};
372
373int term_escape_char = 0x01; /* ctrl-a is used for escape */
374static void mux_print_help(CharDriverState *chr)
375{
376 int i, j;
377 char ebuf[15] = "Escape-Char";
378 char cbuf[50] = "\n\r";
379
380 if (term_escape_char > 0 && term_escape_char < 26) {
381 snprintf(cbuf, sizeof(cbuf), "\n\r");
382 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
383 } else {
384 snprintf(cbuf, sizeof(cbuf),
385 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
386 term_escape_char);
387 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200388 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000389 for (i = 0; mux_help[i] != NULL; i++) {
390 for (j=0; mux_help[i][j] != '\0'; j++) {
391 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200392 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000393 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200394 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000395 }
396 }
397}
398
aliguori2724b182009-03-05 23:01:47 +0000399static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
400{
401 if (d->chr_event[mux_nr])
402 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
403}
404
aliguori6f97dba2008-10-31 18:49:55 +0000405static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
406{
407 if (d->term_got_escape) {
408 d->term_got_escape = 0;
409 if (ch == term_escape_char)
410 goto send_char;
411 switch(ch) {
412 case '?':
413 case 'h':
414 mux_print_help(chr);
415 break;
416 case 'x':
417 {
418 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200419 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000420 exit(0);
421 break;
422 }
423 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200424 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000425 break;
426 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100427 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000428 break;
429 case 'c':
430 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200431 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
432 d->focus++;
433 if (d->focus >= d->mux_cnt)
434 d->focus = 0;
435 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000436 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200437 case 't':
438 d->timestamps = !d->timestamps;
439 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200440 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200441 break;
aliguori6f97dba2008-10-31 18:49:55 +0000442 }
443 } else if (ch == term_escape_char) {
444 d->term_got_escape = 1;
445 } else {
446 send_char:
447 return 1;
448 }
449 return 0;
450}
451
452static void mux_chr_accept_input(CharDriverState *chr)
453{
aliguori6f97dba2008-10-31 18:49:55 +0000454 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200455 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000456
aliguoria80bf992009-03-05 23:00:02 +0000457 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000458 d->chr_can_read[m] &&
459 d->chr_can_read[m](d->ext_opaque[m])) {
460 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000461 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000462 }
463}
464
465static int mux_chr_can_read(void *opaque)
466{
467 CharDriverState *chr = opaque;
468 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200469 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000470
aliguoria80bf992009-03-05 23:00:02 +0000471 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000472 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000473 if (d->chr_can_read[m])
474 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000475 return 0;
476}
477
478static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
479{
480 CharDriverState *chr = opaque;
481 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200482 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000483 int i;
484
485 mux_chr_accept_input (opaque);
486
487 for(i = 0; i < size; i++)
488 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000489 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000490 d->chr_can_read[m] &&
491 d->chr_can_read[m](d->ext_opaque[m]))
492 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
493 else
aliguoria80bf992009-03-05 23:00:02 +0000494 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000495 }
496}
497
498static void mux_chr_event(void *opaque, int event)
499{
500 CharDriverState *chr = opaque;
501 MuxDriver *d = chr->opaque;
502 int i;
503
504 /* Send the event to all registered listeners */
505 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000506 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000507}
508
509static void mux_chr_update_read_handler(CharDriverState *chr)
510{
511 MuxDriver *d = chr->opaque;
512
513 if (d->mux_cnt >= MAX_MUX) {
514 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
515 return;
516 }
517 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
518 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
519 d->chr_read[d->mux_cnt] = chr->chr_read;
520 d->chr_event[d->mux_cnt] = chr->chr_event;
521 /* Fix up the real driver with mux routines */
522 if (d->mux_cnt == 0) {
523 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
524 mux_chr_event, chr);
525 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200526 if (d->focus != -1) {
527 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200528 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200529 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000530 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200531 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000532}
533
Michael Roth7b7ab182013-07-30 13:04:22 -0500534static bool muxes_realized;
535
536/**
537 * Called after processing of default and command-line-specified
538 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
539 * to a mux chardev. This is done here to ensure that
540 * output/prompts/banners are only displayed for the FE that has
541 * focus when initial command-line processing/machine init is
542 * completed.
543 *
544 * After this point, any new FE attached to any new or existing
545 * mux will receive CHR_EVENT_OPENED notifications for the BE
546 * immediately.
547 */
548static void muxes_realize_done(Notifier *notifier, void *unused)
549{
550 CharDriverState *chr;
551
552 QTAILQ_FOREACH(chr, &chardevs, next) {
553 if (chr->is_mux) {
554 MuxDriver *d = chr->opaque;
555 int i;
556
557 /* send OPENED to all already-attached FEs */
558 for (i = 0; i < d->mux_cnt; i++) {
559 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
560 }
561 /* mark mux as OPENED so any new FEs will immediately receive
562 * OPENED event
563 */
564 qemu_chr_be_generic_open(chr);
565 }
566 }
567 muxes_realized = true;
568}
569
570static Notifier muxes_realize_notify = {
571 .notify = muxes_realize_done,
572};
573
aliguori6f97dba2008-10-31 18:49:55 +0000574static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
575{
576 CharDriverState *chr;
577 MuxDriver *d;
578
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200579 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500580 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000581
582 chr->opaque = d;
583 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200584 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000585 chr->chr_write = mux_chr_write;
586 chr->chr_update_read_handler = mux_chr_update_read_handler;
587 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100588 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100589 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500590 /* only default to opened state if we've realized the initial
591 * set of muxes
592 */
593 chr->explicit_be_open = muxes_realized ? 0 : 1;
594 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200595
aliguori6f97dba2008-10-31 18:49:55 +0000596 return chr;
597}
598
599
600#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000601int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000602{
603 int ret, len;
604
605 len = len1;
606 while (len > 0) {
607 ret = send(fd, buf, len, 0);
608 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000609 errno = WSAGetLastError();
610 if (errno != WSAEWOULDBLOCK) {
611 return -1;
612 }
613 } else if (ret == 0) {
614 break;
615 } else {
616 buf += ret;
617 len -= ret;
618 }
619 }
620 return len1 - len;
621}
622
623#else
624
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100625int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000626{
627 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100628 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000629
630 len = len1;
631 while (len > 0) {
632 ret = write(fd, buf, len);
633 if (ret < 0) {
634 if (errno != EINTR && errno != EAGAIN)
635 return -1;
636 } else if (ret == 0) {
637 break;
638 } else {
639 buf += ret;
640 len -= ret;
641 }
642 }
643 return len1 - len;
644}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500645
646int recv_all(int fd, void *_buf, int len1, bool single_read)
647{
648 int ret, len;
649 uint8_t *buf = _buf;
650
651 len = len1;
652 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
653 if (ret < 0) {
654 if (errno != EINTR && errno != EAGAIN) {
655 return -1;
656 }
657 continue;
658 } else {
659 if (single_read) {
660 return ret;
661 }
662 buf += ret;
663 len -= ret;
664 }
665 }
666 return len1 - len;
667}
668
aliguori6f97dba2008-10-31 18:49:55 +0000669#endif /* !_WIN32 */
670
Anthony Liguori96c63842013-03-05 23:21:18 +0530671typedef struct IOWatchPoll
672{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200673 GSource parent;
674
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200675 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530676 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530677
678 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200679 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530680 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530681} IOWatchPoll;
682
Anthony Liguori96c63842013-03-05 23:21:18 +0530683static IOWatchPoll *io_watch_poll_from_source(GSource *source)
684{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200685 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530686}
687
688static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
689{
690 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200691 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200692 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200693 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530694 return FALSE;
695 }
696
Paolo Bonzinid185c092013-04-05 17:59:33 +0200697 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200698 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
699 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200700 g_source_attach(iwp->src, NULL);
701 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200702 g_source_destroy(iwp->src);
703 g_source_unref(iwp->src);
704 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200705 }
706 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530707}
708
709static gboolean io_watch_poll_check(GSource *source)
710{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200711 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530712}
713
714static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
715 gpointer user_data)
716{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200717 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530718}
719
720static void io_watch_poll_finalize(GSource *source)
721{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200722 /* Due to a glib bug, removing the last reference to a source
723 * inside a finalize callback causes recursive locking (and a
724 * deadlock). This is not a problem inside other callbacks,
725 * including dispatch callbacks, so we call io_remove_watch_poll
726 * to remove this source. At this point, iwp->src must
727 * be NULL, or we would leak it.
728 *
729 * This would be solved much more elegantly by child sources,
730 * but we support older glib versions that do not have them.
731 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530732 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200733 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530734}
735
736static GSourceFuncs io_watch_poll_funcs = {
737 .prepare = io_watch_poll_prepare,
738 .check = io_watch_poll_check,
739 .dispatch = io_watch_poll_dispatch,
740 .finalize = io_watch_poll_finalize,
741};
742
743/* Can only be used for read */
744static guint io_add_watch_poll(GIOChannel *channel,
745 IOCanReadHandler *fd_can_read,
746 GIOFunc fd_read,
747 gpointer user_data)
748{
749 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200750 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530751
Paolo Bonzinid185c092013-04-05 17:59:33 +0200752 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530753 iwp->fd_can_read = fd_can_read;
754 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200755 iwp->channel = channel;
756 iwp->fd_read = (GSourceFunc) fd_read;
757 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530758
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200759 tag = g_source_attach(&iwp->parent, NULL);
760 g_source_unref(&iwp->parent);
761 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530762}
763
Paolo Bonzini2b316772013-04-19 17:32:09 +0200764static void io_remove_watch_poll(guint tag)
765{
766 GSource *source;
767 IOWatchPoll *iwp;
768
769 g_return_if_fail (tag > 0);
770
771 source = g_main_context_find_source_by_id(NULL, tag);
772 g_return_if_fail (source != NULL);
773
774 iwp = io_watch_poll_from_source(source);
775 if (iwp->src) {
776 g_source_destroy(iwp->src);
777 g_source_unref(iwp->src);
778 iwp->src = NULL;
779 }
780 g_source_destroy(&iwp->parent);
781}
782
Amit Shah26da70c2013-08-28 15:23:37 +0530783static void remove_fd_in_watch(CharDriverState *chr)
784{
785 if (chr->fd_in_tag) {
786 io_remove_watch_poll(chr->fd_in_tag);
787 chr->fd_in_tag = 0;
788 }
789}
790
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000791#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530792static GIOChannel *io_channel_from_fd(int fd)
793{
794 GIOChannel *chan;
795
796 if (fd == -1) {
797 return NULL;
798 }
799
800 chan = g_io_channel_unix_new(fd);
801
802 g_io_channel_set_encoding(chan, NULL, NULL);
803 g_io_channel_set_buffered(chan, FALSE);
804
805 return chan;
806}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000807#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530808
Anthony Liguori76a96442013-03-05 23:21:21 +0530809static GIOChannel *io_channel_from_socket(int fd)
810{
811 GIOChannel *chan;
812
813 if (fd == -1) {
814 return NULL;
815 }
816
817#ifdef _WIN32
818 chan = g_io_channel_win32_new_socket(fd);
819#else
820 chan = g_io_channel_unix_new(fd);
821#endif
822
823 g_io_channel_set_encoding(chan, NULL, NULL);
824 g_io_channel_set_buffered(chan, FALSE);
825
826 return chan;
827}
828
Anthony Liguori684a0962013-03-29 11:39:50 -0500829static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530830{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200831 size_t offset = 0;
832 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530833
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200834 while (offset < len && status == G_IO_STATUS_NORMAL) {
835 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500836
837 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530838 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500839 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530840 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500841
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200842 if (offset > 0) {
843 return offset;
844 }
845 switch (status) {
846 case G_IO_STATUS_NORMAL:
847 g_assert(len == 0);
848 return 0;
849 case G_IO_STATUS_AGAIN:
850 errno = EAGAIN;
851 return -1;
852 default:
853 break;
854 }
855 errno = EINVAL;
856 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530857}
Anthony Liguori96c63842013-03-05 23:21:18 +0530858
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000859#ifndef _WIN32
860
Anthony Liguoria29753f2013-03-05 23:21:19 +0530861typedef struct FDCharDriver {
862 CharDriverState *chr;
863 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000864 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530865 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000866} FDCharDriver;
867
aliguori6f97dba2008-10-31 18:49:55 +0000868static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
869{
870 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530871
Anthony Liguori684a0962013-03-29 11:39:50 -0500872 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530873}
874
875static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
876{
877 CharDriverState *chr = opaque;
878 FDCharDriver *s = chr->opaque;
879 int len;
880 uint8_t buf[READ_BUF_LEN];
881 GIOStatus status;
882 gsize bytes_read;
883
884 len = sizeof(buf);
885 if (len > s->max_size) {
886 len = s->max_size;
887 }
888 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200889 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530890 }
891
892 status = g_io_channel_read_chars(chan, (gchar *)buf,
893 len, &bytes_read, NULL);
894 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530895 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530896 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
897 return FALSE;
898 }
899 if (status == G_IO_STATUS_NORMAL) {
900 qemu_chr_be_write(chr, buf, bytes_read);
901 }
902
903 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000904}
905
906static int fd_chr_read_poll(void *opaque)
907{
908 CharDriverState *chr = opaque;
909 FDCharDriver *s = chr->opaque;
910
Anthony Liguori909cda12011-08-15 11:17:31 -0500911 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000912 return s->max_size;
913}
914
Anthony Liguori23673ca2013-03-05 23:21:23 +0530915static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
916{
917 FDCharDriver *s = chr->opaque;
918 return g_io_create_watch(s->fd_out, cond);
919}
920
aliguori6f97dba2008-10-31 18:49:55 +0000921static void fd_chr_update_read_handler(CharDriverState *chr)
922{
923 FDCharDriver *s = chr->opaque;
924
Amit Shah26da70c2013-08-28 15:23:37 +0530925 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530926 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530927 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
928 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000929 }
930}
931
932static void fd_chr_close(struct CharDriverState *chr)
933{
934 FDCharDriver *s = chr->opaque;
935
Amit Shah26da70c2013-08-28 15:23:37 +0530936 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530937 if (s->fd_in) {
938 g_io_channel_unref(s->fd_in);
939 }
940 if (s->fd_out) {
941 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000942 }
943
Anthony Liguori7267c092011-08-20 22:09:37 -0500944 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100945 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000946}
947
948/* open a character device to a unix fd */
949static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
950{
951 CharDriverState *chr;
952 FDCharDriver *s;
953
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200954 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500955 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530956 s->fd_in = io_channel_from_fd(fd_in);
957 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530958 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530959 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000960 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530961 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000962 chr->chr_write = fd_chr_write;
963 chr->chr_update_read_handler = fd_chr_update_read_handler;
964 chr->chr_close = fd_chr_close;
965
aliguori6f97dba2008-10-31 18:49:55 +0000966 return chr;
967}
968
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100969static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000970{
971 int fd_in, fd_out;
972 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100973 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200974
975 if (filename == NULL) {
976 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100977 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200978 }
aliguori6f97dba2008-10-31 18:49:55 +0000979
980 snprintf(filename_in, 256, "%s.in", filename);
981 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100982 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
983 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000984 if (fd_in < 0 || fd_out < 0) {
985 if (fd_in >= 0)
986 close(fd_in);
987 if (fd_out >= 0)
988 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100989 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100990 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100991 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100992 }
aliguori6f97dba2008-10-31 18:49:55 +0000993 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100994 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000995}
996
aliguori6f97dba2008-10-31 18:49:55 +0000997/* init terminal so that we can grab keys */
998static struct termios oldtty;
999static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001000static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001001
1002static void term_exit(void)
1003{
1004 tcsetattr (0, TCSANOW, &oldtty);
1005 fcntl(0, F_SETFL, old_fd0_flags);
1006}
1007
Paolo Bonzinibb002512010-12-23 13:42:50 +01001008static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001009{
1010 struct termios tty;
1011
Paolo Bonzini03693642010-12-23 13:42:49 +01001012 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001013 if (!echo) {
1014 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001015 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001016 tty.c_oflag |= OPOST;
1017 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1018 tty.c_cflag &= ~(CSIZE|PARENB);
1019 tty.c_cflag |= CS8;
1020 tty.c_cc[VMIN] = 1;
1021 tty.c_cc[VTIME] = 0;
1022 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001023 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001024 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001025
1026 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001027}
1028
1029static void qemu_chr_close_stdio(struct CharDriverState *chr)
1030{
1031 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001032 fd_chr_close(chr);
1033}
1034
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001035static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001036{
1037 CharDriverState *chr;
1038
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001039 if (is_daemonized()) {
1040 error_report("cannot use stdio with -daemonize");
1041 return NULL;
1042 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301043 old_fd0_flags = fcntl(0, F_GETFL);
1044 tcgetattr (0, &oldtty);
1045 fcntl(0, F_SETFL, O_NONBLOCK);
1046 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001047
aliguori6f97dba2008-10-31 18:49:55 +00001048 chr = qemu_chr_open_fd(0, 1);
1049 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001050 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001051 if (opts->has_signal) {
1052 stdio_allow_signal = opts->signal;
1053 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001054 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001055
Markus Armbruster1f514702012-02-07 15:09:08 +01001056 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001057}
1058
aliguori6f97dba2008-10-31 18:49:55 +00001059#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001060 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1061 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001062
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001063#define HAVE_CHARDEV_TTY 1
1064
aliguori6f97dba2008-10-31 18:49:55 +00001065typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301066 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001067 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001068 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301069 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001070} PtyCharDriver;
1071
1072static void pty_chr_update_read_handler(CharDriverState *chr);
1073static void pty_chr_state(CharDriverState *chr, int connected);
1074
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301075static gboolean pty_chr_timer(gpointer opaque)
1076{
1077 struct CharDriverState *chr = opaque;
1078 PtyCharDriver *s = chr->opaque;
1079
Hans de Goede79f20072013-04-25 13:53:02 +02001080 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001081 if (!s->connected) {
1082 /* Next poll ... */
1083 pty_chr_update_read_handler(chr);
1084 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301085 return FALSE;
1086}
1087
1088static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1089{
1090 PtyCharDriver *s = chr->opaque;
1091
1092 if (s->timer_tag) {
1093 g_source_remove(s->timer_tag);
1094 s->timer_tag = 0;
1095 }
1096
1097 if (ms == 1000) {
1098 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1099 } else {
1100 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1101 }
1102}
1103
aliguori6f97dba2008-10-31 18:49:55 +00001104static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1105{
1106 PtyCharDriver *s = chr->opaque;
1107
1108 if (!s->connected) {
1109 /* guest sends data, check for (re-)connect */
1110 pty_chr_update_read_handler(chr);
1111 return 0;
1112 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001113 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001114}
1115
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301116static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1117{
1118 PtyCharDriver *s = chr->opaque;
1119 return g_io_create_watch(s->fd, cond);
1120}
1121
aliguori6f97dba2008-10-31 18:49:55 +00001122static int pty_chr_read_poll(void *opaque)
1123{
1124 CharDriverState *chr = opaque;
1125 PtyCharDriver *s = chr->opaque;
1126
Anthony Liguori909cda12011-08-15 11:17:31 -05001127 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001128 return s->read_bytes;
1129}
1130
Anthony Liguori093d3a22013-03-05 23:21:20 +05301131static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001132{
1133 CharDriverState *chr = opaque;
1134 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301135 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301136 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301137 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001138
1139 len = sizeof(buf);
1140 if (len > s->read_bytes)
1141 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001142 if (len == 0) {
1143 return TRUE;
1144 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301145 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1146 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001147 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301148 return FALSE;
1149 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001150 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001151 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001152 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301153 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001154}
1155
1156static void pty_chr_update_read_handler(CharDriverState *chr)
1157{
1158 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001159 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001160
Paolo Bonzini85a67692013-04-19 17:32:07 +02001161 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1162 pfd.events = G_IO_OUT;
1163 pfd.revents = 0;
1164 g_poll(&pfd, 1, 0);
1165 if (pfd.revents & G_IO_HUP) {
1166 pty_chr_state(chr, 0);
1167 } else {
1168 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301169 }
aliguori6f97dba2008-10-31 18:49:55 +00001170}
1171
1172static void pty_chr_state(CharDriverState *chr, int connected)
1173{
1174 PtyCharDriver *s = chr->opaque;
1175
1176 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301177 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001178 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001179 /* (re-)connect poll interval for idle guests: once per second.
1180 * We check more frequently in case the guests sends data to
1181 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301182 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001183 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001184 if (s->timer_tag) {
1185 g_source_remove(s->timer_tag);
1186 s->timer_tag = 0;
1187 }
1188 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001189 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001190 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001191 }
1192 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301193 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1194 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001195 }
aliguori6f97dba2008-10-31 18:49:55 +00001196 }
1197}
1198
aliguori6f97dba2008-10-31 18:49:55 +00001199static void pty_chr_close(struct CharDriverState *chr)
1200{
1201 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301202 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001203
Amit Shah26da70c2013-08-28 15:23:37 +05301204 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301205 fd = g_io_channel_unix_get_fd(s->fd);
1206 g_io_channel_unref(s->fd);
1207 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301208 if (s->timer_tag) {
1209 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001210 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301211 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001212 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001213 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001214}
1215
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001216static CharDriverState *qemu_chr_open_pty(const char *id,
1217 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001218{
1219 CharDriverState *chr;
1220 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001221 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001222 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001223
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001224 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1225 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001226 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001227 }
1228
aliguori6f97dba2008-10-31 18:49:55 +00001229 close(slave_fd);
1230
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001231 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001232
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001233 chr->filename = g_strdup_printf("pty:%s", pty_name);
1234 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001235 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001236
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001237 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001238 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001239
1240 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001241 chr->opaque = s;
1242 chr->chr_write = pty_chr_write;
1243 chr->chr_update_read_handler = pty_chr_update_read_handler;
1244 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301245 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001246 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001247
Anthony Liguori093d3a22013-03-05 23:21:20 +05301248 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301249 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001250
Markus Armbruster1f514702012-02-07 15:09:08 +01001251 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001252}
1253
1254static void tty_serial_init(int fd, int speed,
1255 int parity, int data_bits, int stop_bits)
1256{
1257 struct termios tty;
1258 speed_t spd;
1259
1260#if 0
1261 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1262 speed, parity, data_bits, stop_bits);
1263#endif
1264 tcgetattr (fd, &tty);
1265
Stefan Weil45eea132009-10-26 16:10:10 +01001266#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1267 speed = speed * 10 / 11;
1268 do {
1269 check_speed(50);
1270 check_speed(75);
1271 check_speed(110);
1272 check_speed(134);
1273 check_speed(150);
1274 check_speed(200);
1275 check_speed(300);
1276 check_speed(600);
1277 check_speed(1200);
1278 check_speed(1800);
1279 check_speed(2400);
1280 check_speed(4800);
1281 check_speed(9600);
1282 check_speed(19200);
1283 check_speed(38400);
1284 /* Non-Posix values follow. They may be unsupported on some systems. */
1285 check_speed(57600);
1286 check_speed(115200);
1287#ifdef B230400
1288 check_speed(230400);
1289#endif
1290#ifdef B460800
1291 check_speed(460800);
1292#endif
1293#ifdef B500000
1294 check_speed(500000);
1295#endif
1296#ifdef B576000
1297 check_speed(576000);
1298#endif
1299#ifdef B921600
1300 check_speed(921600);
1301#endif
1302#ifdef B1000000
1303 check_speed(1000000);
1304#endif
1305#ifdef B1152000
1306 check_speed(1152000);
1307#endif
1308#ifdef B1500000
1309 check_speed(1500000);
1310#endif
1311#ifdef B2000000
1312 check_speed(2000000);
1313#endif
1314#ifdef B2500000
1315 check_speed(2500000);
1316#endif
1317#ifdef B3000000
1318 check_speed(3000000);
1319#endif
1320#ifdef B3500000
1321 check_speed(3500000);
1322#endif
1323#ifdef B4000000
1324 check_speed(4000000);
1325#endif
aliguori6f97dba2008-10-31 18:49:55 +00001326 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001327 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001328
1329 cfsetispeed(&tty, spd);
1330 cfsetospeed(&tty, spd);
1331
1332 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1333 |INLCR|IGNCR|ICRNL|IXON);
1334 tty.c_oflag |= OPOST;
1335 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1336 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1337 switch(data_bits) {
1338 default:
1339 case 8:
1340 tty.c_cflag |= CS8;
1341 break;
1342 case 7:
1343 tty.c_cflag |= CS7;
1344 break;
1345 case 6:
1346 tty.c_cflag |= CS6;
1347 break;
1348 case 5:
1349 tty.c_cflag |= CS5;
1350 break;
1351 }
1352 switch(parity) {
1353 default:
1354 case 'N':
1355 break;
1356 case 'E':
1357 tty.c_cflag |= PARENB;
1358 break;
1359 case 'O':
1360 tty.c_cflag |= PARENB | PARODD;
1361 break;
1362 }
1363 if (stop_bits == 2)
1364 tty.c_cflag |= CSTOPB;
1365
1366 tcsetattr (fd, TCSANOW, &tty);
1367}
1368
1369static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1370{
1371 FDCharDriver *s = chr->opaque;
1372
1373 switch(cmd) {
1374 case CHR_IOCTL_SERIAL_SET_PARAMS:
1375 {
1376 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301377 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1378 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001379 ssp->data_bits, ssp->stop_bits);
1380 }
1381 break;
1382 case CHR_IOCTL_SERIAL_SET_BREAK:
1383 {
1384 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301385 if (enable) {
1386 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1387 }
aliguori6f97dba2008-10-31 18:49:55 +00001388 }
1389 break;
1390 case CHR_IOCTL_SERIAL_GET_TIOCM:
1391 {
1392 int sarg = 0;
1393 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301394 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001395 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001396 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001397 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001398 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001399 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001400 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001401 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001402 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001403 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001404 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001405 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001406 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001407 *targ |= CHR_TIOCM_RTS;
1408 }
1409 break;
1410 case CHR_IOCTL_SERIAL_SET_TIOCM:
1411 {
1412 int sarg = *(int *)arg;
1413 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301414 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001415 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1416 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1417 if (sarg & CHR_TIOCM_CTS)
1418 targ |= TIOCM_CTS;
1419 if (sarg & CHR_TIOCM_CAR)
1420 targ |= TIOCM_CAR;
1421 if (sarg & CHR_TIOCM_DSR)
1422 targ |= TIOCM_DSR;
1423 if (sarg & CHR_TIOCM_RI)
1424 targ |= TIOCM_RI;
1425 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001426 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001427 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001428 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301429 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001430 }
1431 break;
1432 default:
1433 return -ENOTSUP;
1434 }
1435 return 0;
1436}
1437
David Ahern4266a132010-02-10 18:27:17 -07001438static void qemu_chr_close_tty(CharDriverState *chr)
1439{
1440 FDCharDriver *s = chr->opaque;
1441 int fd = -1;
1442
1443 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301444 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001445 }
1446
1447 fd_chr_close(chr);
1448
1449 if (fd >= 0) {
1450 close(fd);
1451 }
1452}
1453
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001454static CharDriverState *qemu_chr_open_tty_fd(int fd)
1455{
1456 CharDriverState *chr;
1457
1458 tty_serial_init(fd, 115200, 'N', 8, 1);
1459 chr = qemu_chr_open_fd(fd, fd);
1460 chr->chr_ioctl = tty_serial_ioctl;
1461 chr->chr_close = qemu_chr_close_tty;
1462 return chr;
1463}
aliguori6f97dba2008-10-31 18:49:55 +00001464#endif /* __linux__ || __sun__ */
1465
1466#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001467
1468#define HAVE_CHARDEV_PARPORT 1
1469
aliguori6f97dba2008-10-31 18:49:55 +00001470typedef struct {
1471 int fd;
1472 int mode;
1473} ParallelCharDriver;
1474
1475static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1476{
1477 if (s->mode != mode) {
1478 int m = mode;
1479 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1480 return 0;
1481 s->mode = mode;
1482 }
1483 return 1;
1484}
1485
1486static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1487{
1488 ParallelCharDriver *drv = chr->opaque;
1489 int fd = drv->fd;
1490 uint8_t b;
1491
1492 switch(cmd) {
1493 case CHR_IOCTL_PP_READ_DATA:
1494 if (ioctl(fd, PPRDATA, &b) < 0)
1495 return -ENOTSUP;
1496 *(uint8_t *)arg = b;
1497 break;
1498 case CHR_IOCTL_PP_WRITE_DATA:
1499 b = *(uint8_t *)arg;
1500 if (ioctl(fd, PPWDATA, &b) < 0)
1501 return -ENOTSUP;
1502 break;
1503 case CHR_IOCTL_PP_READ_CONTROL:
1504 if (ioctl(fd, PPRCONTROL, &b) < 0)
1505 return -ENOTSUP;
1506 /* Linux gives only the lowest bits, and no way to know data
1507 direction! For better compatibility set the fixed upper
1508 bits. */
1509 *(uint8_t *)arg = b | 0xc0;
1510 break;
1511 case CHR_IOCTL_PP_WRITE_CONTROL:
1512 b = *(uint8_t *)arg;
1513 if (ioctl(fd, PPWCONTROL, &b) < 0)
1514 return -ENOTSUP;
1515 break;
1516 case CHR_IOCTL_PP_READ_STATUS:
1517 if (ioctl(fd, PPRSTATUS, &b) < 0)
1518 return -ENOTSUP;
1519 *(uint8_t *)arg = b;
1520 break;
1521 case CHR_IOCTL_PP_DATA_DIR:
1522 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1523 return -ENOTSUP;
1524 break;
1525 case CHR_IOCTL_PP_EPP_READ_ADDR:
1526 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1527 struct ParallelIOArg *parg = arg;
1528 int n = read(fd, parg->buffer, parg->count);
1529 if (n != parg->count) {
1530 return -EIO;
1531 }
1532 }
1533 break;
1534 case CHR_IOCTL_PP_EPP_READ:
1535 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1536 struct ParallelIOArg *parg = arg;
1537 int n = read(fd, parg->buffer, parg->count);
1538 if (n != parg->count) {
1539 return -EIO;
1540 }
1541 }
1542 break;
1543 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1544 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1545 struct ParallelIOArg *parg = arg;
1546 int n = write(fd, parg->buffer, parg->count);
1547 if (n != parg->count) {
1548 return -EIO;
1549 }
1550 }
1551 break;
1552 case CHR_IOCTL_PP_EPP_WRITE:
1553 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1554 struct ParallelIOArg *parg = arg;
1555 int n = write(fd, parg->buffer, parg->count);
1556 if (n != parg->count) {
1557 return -EIO;
1558 }
1559 }
1560 break;
1561 default:
1562 return -ENOTSUP;
1563 }
1564 return 0;
1565}
1566
1567static void pp_close(CharDriverState *chr)
1568{
1569 ParallelCharDriver *drv = chr->opaque;
1570 int fd = drv->fd;
1571
1572 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1573 ioctl(fd, PPRELEASE);
1574 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001575 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001576 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001577}
1578
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001579static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001580{
1581 CharDriverState *chr;
1582 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001583
1584 if (ioctl(fd, PPCLAIM) < 0) {
1585 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001586 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001587 }
1588
Anthony Liguori7267c092011-08-20 22:09:37 -05001589 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001590 drv->fd = fd;
1591 drv->mode = IEEE1284_MODE_COMPAT;
1592
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001593 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001594 chr->chr_write = null_chr_write;
1595 chr->chr_ioctl = pp_ioctl;
1596 chr->chr_close = pp_close;
1597 chr->opaque = drv;
1598
Markus Armbruster1f514702012-02-07 15:09:08 +01001599 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001600}
1601#endif /* __linux__ */
1602
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001603#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001604
1605#define HAVE_CHARDEV_PARPORT 1
1606
blueswir16972f932008-11-22 20:49:12 +00001607static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1608{
Stefan Weile0efb992011-02-23 19:09:16 +01001609 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001610 uint8_t b;
1611
1612 switch(cmd) {
1613 case CHR_IOCTL_PP_READ_DATA:
1614 if (ioctl(fd, PPIGDATA, &b) < 0)
1615 return -ENOTSUP;
1616 *(uint8_t *)arg = b;
1617 break;
1618 case CHR_IOCTL_PP_WRITE_DATA:
1619 b = *(uint8_t *)arg;
1620 if (ioctl(fd, PPISDATA, &b) < 0)
1621 return -ENOTSUP;
1622 break;
1623 case CHR_IOCTL_PP_READ_CONTROL:
1624 if (ioctl(fd, PPIGCTRL, &b) < 0)
1625 return -ENOTSUP;
1626 *(uint8_t *)arg = b;
1627 break;
1628 case CHR_IOCTL_PP_WRITE_CONTROL:
1629 b = *(uint8_t *)arg;
1630 if (ioctl(fd, PPISCTRL, &b) < 0)
1631 return -ENOTSUP;
1632 break;
1633 case CHR_IOCTL_PP_READ_STATUS:
1634 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1635 return -ENOTSUP;
1636 *(uint8_t *)arg = b;
1637 break;
1638 default:
1639 return -ENOTSUP;
1640 }
1641 return 0;
1642}
1643
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001644static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001645{
1646 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001647
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001648 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001649 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001650 chr->chr_write = null_chr_write;
1651 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001652 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001653 return chr;
blueswir16972f932008-11-22 20:49:12 +00001654}
1655#endif
1656
aliguori6f97dba2008-10-31 18:49:55 +00001657#else /* _WIN32 */
1658
1659typedef struct {
1660 int max_size;
1661 HANDLE hcom, hrecv, hsend;
1662 OVERLAPPED orecv, osend;
1663 BOOL fpipe;
1664 DWORD len;
1665} WinCharState;
1666
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001667typedef struct {
1668 HANDLE hStdIn;
1669 HANDLE hInputReadyEvent;
1670 HANDLE hInputDoneEvent;
1671 HANDLE hInputThread;
1672 uint8_t win_stdio_buf;
1673} WinStdioCharState;
1674
aliguori6f97dba2008-10-31 18:49:55 +00001675#define NSENDBUF 2048
1676#define NRECVBUF 2048
1677#define MAXCONNECT 1
1678#define NTIMEOUT 5000
1679
1680static int win_chr_poll(void *opaque);
1681static int win_chr_pipe_poll(void *opaque);
1682
1683static void win_chr_close(CharDriverState *chr)
1684{
1685 WinCharState *s = chr->opaque;
1686
1687 if (s->hsend) {
1688 CloseHandle(s->hsend);
1689 s->hsend = NULL;
1690 }
1691 if (s->hrecv) {
1692 CloseHandle(s->hrecv);
1693 s->hrecv = NULL;
1694 }
1695 if (s->hcom) {
1696 CloseHandle(s->hcom);
1697 s->hcom = NULL;
1698 }
1699 if (s->fpipe)
1700 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1701 else
1702 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301703
Hans de Goedea425d232011-11-19 10:22:43 +01001704 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001705}
1706
1707static int win_chr_init(CharDriverState *chr, const char *filename)
1708{
1709 WinCharState *s = chr->opaque;
1710 COMMCONFIG comcfg;
1711 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1712 COMSTAT comstat;
1713 DWORD size;
1714 DWORD err;
1715
1716 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1717 if (!s->hsend) {
1718 fprintf(stderr, "Failed CreateEvent\n");
1719 goto fail;
1720 }
1721 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1722 if (!s->hrecv) {
1723 fprintf(stderr, "Failed CreateEvent\n");
1724 goto fail;
1725 }
1726
1727 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1728 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1729 if (s->hcom == INVALID_HANDLE_VALUE) {
1730 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1731 s->hcom = NULL;
1732 goto fail;
1733 }
1734
1735 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1736 fprintf(stderr, "Failed SetupComm\n");
1737 goto fail;
1738 }
1739
1740 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1741 size = sizeof(COMMCONFIG);
1742 GetDefaultCommConfig(filename, &comcfg, &size);
1743 comcfg.dcb.DCBlength = sizeof(DCB);
1744 CommConfigDialog(filename, NULL, &comcfg);
1745
1746 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1747 fprintf(stderr, "Failed SetCommState\n");
1748 goto fail;
1749 }
1750
1751 if (!SetCommMask(s->hcom, EV_ERR)) {
1752 fprintf(stderr, "Failed SetCommMask\n");
1753 goto fail;
1754 }
1755
1756 cto.ReadIntervalTimeout = MAXDWORD;
1757 if (!SetCommTimeouts(s->hcom, &cto)) {
1758 fprintf(stderr, "Failed SetCommTimeouts\n");
1759 goto fail;
1760 }
1761
1762 if (!ClearCommError(s->hcom, &err, &comstat)) {
1763 fprintf(stderr, "Failed ClearCommError\n");
1764 goto fail;
1765 }
1766 qemu_add_polling_cb(win_chr_poll, chr);
1767 return 0;
1768
1769 fail:
1770 win_chr_close(chr);
1771 return -1;
1772}
1773
1774static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1775{
1776 WinCharState *s = chr->opaque;
1777 DWORD len, ret, size, err;
1778
1779 len = len1;
1780 ZeroMemory(&s->osend, sizeof(s->osend));
1781 s->osend.hEvent = s->hsend;
1782 while (len > 0) {
1783 if (s->hsend)
1784 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1785 else
1786 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1787 if (!ret) {
1788 err = GetLastError();
1789 if (err == ERROR_IO_PENDING) {
1790 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1791 if (ret) {
1792 buf += size;
1793 len -= size;
1794 } else {
1795 break;
1796 }
1797 } else {
1798 break;
1799 }
1800 } else {
1801 buf += size;
1802 len -= size;
1803 }
1804 }
1805 return len1 - len;
1806}
1807
1808static int win_chr_read_poll(CharDriverState *chr)
1809{
1810 WinCharState *s = chr->opaque;
1811
Anthony Liguori909cda12011-08-15 11:17:31 -05001812 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001813 return s->max_size;
1814}
1815
1816static void win_chr_readfile(CharDriverState *chr)
1817{
1818 WinCharState *s = chr->opaque;
1819 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301820 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001821 DWORD size;
1822
1823 ZeroMemory(&s->orecv, sizeof(s->orecv));
1824 s->orecv.hEvent = s->hrecv;
1825 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1826 if (!ret) {
1827 err = GetLastError();
1828 if (err == ERROR_IO_PENDING) {
1829 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1830 }
1831 }
1832
1833 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001834 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001835 }
1836}
1837
1838static void win_chr_read(CharDriverState *chr)
1839{
1840 WinCharState *s = chr->opaque;
1841
1842 if (s->len > s->max_size)
1843 s->len = s->max_size;
1844 if (s->len == 0)
1845 return;
1846
1847 win_chr_readfile(chr);
1848}
1849
1850static int win_chr_poll(void *opaque)
1851{
1852 CharDriverState *chr = opaque;
1853 WinCharState *s = chr->opaque;
1854 COMSTAT status;
1855 DWORD comerr;
1856
1857 ClearCommError(s->hcom, &comerr, &status);
1858 if (status.cbInQue > 0) {
1859 s->len = status.cbInQue;
1860 win_chr_read_poll(chr);
1861 win_chr_read(chr);
1862 return 1;
1863 }
1864 return 0;
1865}
1866
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001867static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001868{
1869 CharDriverState *chr;
1870 WinCharState *s;
1871
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001872 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001873 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001874 chr->opaque = s;
1875 chr->chr_write = win_chr_write;
1876 chr->chr_close = win_chr_close;
1877
1878 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001879 g_free(s);
1880 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001881 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001882 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001883 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001884}
1885
1886static int win_chr_pipe_poll(void *opaque)
1887{
1888 CharDriverState *chr = opaque;
1889 WinCharState *s = chr->opaque;
1890 DWORD size;
1891
1892 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1893 if (size > 0) {
1894 s->len = size;
1895 win_chr_read_poll(chr);
1896 win_chr_read(chr);
1897 return 1;
1898 }
1899 return 0;
1900}
1901
1902static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1903{
1904 WinCharState *s = chr->opaque;
1905 OVERLAPPED ov;
1906 int ret;
1907 DWORD size;
1908 char openname[256];
1909
1910 s->fpipe = TRUE;
1911
1912 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1913 if (!s->hsend) {
1914 fprintf(stderr, "Failed CreateEvent\n");
1915 goto fail;
1916 }
1917 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1918 if (!s->hrecv) {
1919 fprintf(stderr, "Failed CreateEvent\n");
1920 goto fail;
1921 }
1922
1923 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1924 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1925 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1926 PIPE_WAIT,
1927 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1928 if (s->hcom == INVALID_HANDLE_VALUE) {
1929 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1930 s->hcom = NULL;
1931 goto fail;
1932 }
1933
1934 ZeroMemory(&ov, sizeof(ov));
1935 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1936 ret = ConnectNamedPipe(s->hcom, &ov);
1937 if (ret) {
1938 fprintf(stderr, "Failed ConnectNamedPipe\n");
1939 goto fail;
1940 }
1941
1942 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1943 if (!ret) {
1944 fprintf(stderr, "Failed GetOverlappedResult\n");
1945 if (ov.hEvent) {
1946 CloseHandle(ov.hEvent);
1947 ov.hEvent = NULL;
1948 }
1949 goto fail;
1950 }
1951
1952 if (ov.hEvent) {
1953 CloseHandle(ov.hEvent);
1954 ov.hEvent = NULL;
1955 }
1956 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1957 return 0;
1958
1959 fail:
1960 win_chr_close(chr);
1961 return -1;
1962}
1963
1964
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001965static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001966{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001967 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001968 CharDriverState *chr;
1969 WinCharState *s;
1970
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001971 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001972 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001973 chr->opaque = s;
1974 chr->chr_write = win_chr_write;
1975 chr->chr_close = win_chr_close;
1976
1977 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001978 g_free(s);
1979 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001980 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001981 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001982 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001983}
1984
Markus Armbruster1f514702012-02-07 15:09:08 +01001985static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001986{
1987 CharDriverState *chr;
1988 WinCharState *s;
1989
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001990 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001991 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001992 s->hcom = fd_out;
1993 chr->opaque = s;
1994 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001995 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001996}
1997
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001998static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001999{
Markus Armbruster1f514702012-02-07 15:09:08 +01002000 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002001}
2002
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002003static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2004{
2005 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2006 DWORD dwSize;
2007 int len1;
2008
2009 len1 = len;
2010
2011 while (len1 > 0) {
2012 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2013 break;
2014 }
2015 buf += dwSize;
2016 len1 -= dwSize;
2017 }
2018
2019 return len - len1;
2020}
2021
2022static void win_stdio_wait_func(void *opaque)
2023{
2024 CharDriverState *chr = opaque;
2025 WinStdioCharState *stdio = chr->opaque;
2026 INPUT_RECORD buf[4];
2027 int ret;
2028 DWORD dwSize;
2029 int i;
2030
Stefan Weildff74242013-12-07 14:48:04 +01002031 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002032
2033 if (!ret) {
2034 /* Avoid error storm */
2035 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2036 return;
2037 }
2038
2039 for (i = 0; i < dwSize; i++) {
2040 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2041
2042 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2043 int j;
2044 if (kev->uChar.AsciiChar != 0) {
2045 for (j = 0; j < kev->wRepeatCount; j++) {
2046 if (qemu_chr_be_can_write(chr)) {
2047 uint8_t c = kev->uChar.AsciiChar;
2048 qemu_chr_be_write(chr, &c, 1);
2049 }
2050 }
2051 }
2052 }
2053 }
2054}
2055
2056static DWORD WINAPI win_stdio_thread(LPVOID param)
2057{
2058 CharDriverState *chr = param;
2059 WinStdioCharState *stdio = chr->opaque;
2060 int ret;
2061 DWORD dwSize;
2062
2063 while (1) {
2064
2065 /* Wait for one byte */
2066 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2067
2068 /* Exit in case of error, continue if nothing read */
2069 if (!ret) {
2070 break;
2071 }
2072 if (!dwSize) {
2073 continue;
2074 }
2075
2076 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2077 if (stdio->win_stdio_buf == '\r') {
2078 continue;
2079 }
2080
2081 /* Signal the main thread and wait until the byte was eaten */
2082 if (!SetEvent(stdio->hInputReadyEvent)) {
2083 break;
2084 }
2085 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2086 != WAIT_OBJECT_0) {
2087 break;
2088 }
2089 }
2090
2091 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2092 return 0;
2093}
2094
2095static void win_stdio_thread_wait_func(void *opaque)
2096{
2097 CharDriverState *chr = opaque;
2098 WinStdioCharState *stdio = chr->opaque;
2099
2100 if (qemu_chr_be_can_write(chr)) {
2101 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2102 }
2103
2104 SetEvent(stdio->hInputDoneEvent);
2105}
2106
2107static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2108{
2109 WinStdioCharState *stdio = chr->opaque;
2110 DWORD dwMode = 0;
2111
2112 GetConsoleMode(stdio->hStdIn, &dwMode);
2113
2114 if (echo) {
2115 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2116 } else {
2117 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2118 }
2119}
2120
2121static void win_stdio_close(CharDriverState *chr)
2122{
2123 WinStdioCharState *stdio = chr->opaque;
2124
2125 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2126 CloseHandle(stdio->hInputReadyEvent);
2127 }
2128 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2129 CloseHandle(stdio->hInputDoneEvent);
2130 }
2131 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2132 TerminateThread(stdio->hInputThread, 0);
2133 }
2134
2135 g_free(chr->opaque);
2136 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002137}
2138
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002139static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002140{
2141 CharDriverState *chr;
2142 WinStdioCharState *stdio;
2143 DWORD dwMode;
2144 int is_console = 0;
2145
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002146 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002147 stdio = g_malloc0(sizeof(WinStdioCharState));
2148
2149 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2150 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2151 fprintf(stderr, "cannot open stdio: invalid handle\n");
2152 exit(1);
2153 }
2154
2155 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2156
2157 chr->opaque = stdio;
2158 chr->chr_write = win_stdio_write;
2159 chr->chr_close = win_stdio_close;
2160
Anthony Liguoried7a1542013-03-05 23:21:17 +05302161 if (is_console) {
2162 if (qemu_add_wait_object(stdio->hStdIn,
2163 win_stdio_wait_func, chr)) {
2164 fprintf(stderr, "qemu_add_wait_object: failed\n");
2165 }
2166 } else {
2167 DWORD dwId;
2168
2169 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2170 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2171 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2172 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002173
Anthony Liguoried7a1542013-03-05 23:21:17 +05302174 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2175 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2176 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2177 fprintf(stderr, "cannot create stdio thread or event\n");
2178 exit(1);
2179 }
2180 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2181 win_stdio_thread_wait_func, chr)) {
2182 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002183 }
2184 }
2185
2186 dwMode |= ENABLE_LINE_INPUT;
2187
Anthony Liguoried7a1542013-03-05 23:21:17 +05302188 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002189 /* set the terminal in raw mode */
2190 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2191 dwMode |= ENABLE_PROCESSED_INPUT;
2192 }
2193
2194 SetConsoleMode(stdio->hStdIn, dwMode);
2195
2196 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2197 qemu_chr_fe_set_echo(chr, false);
2198
Markus Armbruster1f514702012-02-07 15:09:08 +01002199 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002200}
aliguori6f97dba2008-10-31 18:49:55 +00002201#endif /* !_WIN32 */
2202
Anthony Liguori5ab82112013-03-05 23:21:31 +05302203
aliguori6f97dba2008-10-31 18:49:55 +00002204/***********************************************************/
2205/* UDP Net console */
2206
2207typedef struct {
2208 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302209 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302210 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002211 int bufcnt;
2212 int bufptr;
2213 int max_size;
2214} NetCharDriver;
2215
2216static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2217{
2218 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302219 gsize bytes_written;
2220 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002221
Anthony Liguori76a96442013-03-05 23:21:21 +05302222 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2223 if (status == G_IO_STATUS_EOF) {
2224 return 0;
2225 } else if (status != G_IO_STATUS_NORMAL) {
2226 return -1;
2227 }
2228
2229 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002230}
2231
2232static int udp_chr_read_poll(void *opaque)
2233{
2234 CharDriverState *chr = opaque;
2235 NetCharDriver *s = chr->opaque;
2236
Anthony Liguori909cda12011-08-15 11:17:31 -05002237 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002238
2239 /* If there were any stray characters in the queue process them
2240 * first
2241 */
2242 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002243 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002244 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002245 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002246 }
2247 return s->max_size;
2248}
2249
Anthony Liguori76a96442013-03-05 23:21:21 +05302250static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002251{
2252 CharDriverState *chr = opaque;
2253 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302254 gsize bytes_read = 0;
2255 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002256
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002257 if (s->max_size == 0) {
2258 return TRUE;
2259 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302260 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2261 &bytes_read, NULL);
2262 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002263 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302264 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302265 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302266 return FALSE;
2267 }
aliguori6f97dba2008-10-31 18:49:55 +00002268
2269 s->bufptr = 0;
2270 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002271 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002272 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002273 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002274 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302275
2276 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002277}
2278
2279static void udp_chr_update_read_handler(CharDriverState *chr)
2280{
2281 NetCharDriver *s = chr->opaque;
2282
Amit Shah26da70c2013-08-28 15:23:37 +05302283 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302284 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302285 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2286 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002287 }
2288}
2289
aliguori819f56b2009-03-28 17:58:14 +00002290static void udp_chr_close(CharDriverState *chr)
2291{
2292 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302293
2294 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302295 if (s->chan) {
2296 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002297 closesocket(s->fd);
2298 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002299 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002300 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002301}
2302
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002303static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002304{
2305 CharDriverState *chr = NULL;
2306 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002307
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002308 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002309 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002310
aliguori6f97dba2008-10-31 18:49:55 +00002311 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302312 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002313 s->bufcnt = 0;
2314 s->bufptr = 0;
2315 chr->opaque = s;
2316 chr->chr_write = udp_chr_write;
2317 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002318 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002319 /* be isn't opened until we get a connection */
2320 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002321 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002322}
aliguori6f97dba2008-10-31 18:49:55 +00002323
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002324static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2325{
2326 Error *local_err = NULL;
2327 int fd = -1;
2328
2329 fd = inet_dgram_opts(opts, &local_err);
2330 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002331 qerror_report_err(local_err);
2332 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002333 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002334 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002335 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002336}
2337
2338/***********************************************************/
2339/* TCP Net console */
2340
2341typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302342
2343 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302344 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002345 int fd, listen_fd;
2346 int connected;
2347 int max_size;
2348 int do_telnetopt;
2349 int do_nodelay;
2350 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002351 int *read_msgfds;
2352 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002353 int *write_msgfds;
2354 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002355} TCPCharDriver;
2356
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302357static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002358
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002359#ifndef _WIN32
2360static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2361{
2362 TCPCharDriver *s = chr->opaque;
2363 struct msghdr msgh;
2364 struct iovec iov;
2365 int r;
2366
2367 size_t fd_size = s->write_msgfds_num * sizeof(int);
2368 char control[CMSG_SPACE(fd_size)];
2369 struct cmsghdr *cmsg;
2370
2371 memset(&msgh, 0, sizeof(msgh));
2372 memset(control, 0, sizeof(control));
2373
2374 /* set the payload */
2375 iov.iov_base = (uint8_t *) buf;
2376 iov.iov_len = len;
2377
2378 msgh.msg_iov = &iov;
2379 msgh.msg_iovlen = 1;
2380
2381 msgh.msg_control = control;
2382 msgh.msg_controllen = sizeof(control);
2383
2384 cmsg = CMSG_FIRSTHDR(&msgh);
2385
2386 cmsg->cmsg_len = CMSG_LEN(fd_size);
2387 cmsg->cmsg_level = SOL_SOCKET;
2388 cmsg->cmsg_type = SCM_RIGHTS;
2389 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2390
2391 do {
2392 r = sendmsg(s->fd, &msgh, 0);
2393 } while (r < 0 && errno == EINTR);
2394
2395 /* free the written msgfds, no matter what */
2396 if (s->write_msgfds_num) {
2397 g_free(s->write_msgfds);
2398 s->write_msgfds = 0;
2399 s->write_msgfds_num = 0;
2400 }
2401
2402 return r;
2403}
2404#endif
2405
aliguori6f97dba2008-10-31 18:49:55 +00002406static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2407{
2408 TCPCharDriver *s = chr->opaque;
2409 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002410#ifndef _WIN32
2411 if (s->is_unix && s->write_msgfds_num) {
2412 return unix_send_msgfds(chr, buf, len);
2413 } else
2414#endif
2415 {
2416 return io_channel_send(s->chan, buf, len);
2417 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002418 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002419 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002420 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002421 }
2422}
2423
2424static int tcp_chr_read_poll(void *opaque)
2425{
2426 CharDriverState *chr = opaque;
2427 TCPCharDriver *s = chr->opaque;
2428 if (!s->connected)
2429 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002430 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002431 return s->max_size;
2432}
2433
2434#define IAC 255
2435#define IAC_BREAK 243
2436static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2437 TCPCharDriver *s,
2438 uint8_t *buf, int *size)
2439{
2440 /* Handle any telnet client's basic IAC options to satisfy char by
2441 * char mode with no echo. All IAC options will be removed from
2442 * the buf and the do_telnetopt variable will be used to track the
2443 * state of the width of the IAC information.
2444 *
2445 * IAC commands come in sets of 3 bytes with the exception of the
2446 * "IAC BREAK" command and the double IAC.
2447 */
2448
2449 int i;
2450 int j = 0;
2451
2452 for (i = 0; i < *size; i++) {
2453 if (s->do_telnetopt > 1) {
2454 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2455 /* Double IAC means send an IAC */
2456 if (j != i)
2457 buf[j] = buf[i];
2458 j++;
2459 s->do_telnetopt = 1;
2460 } else {
2461 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2462 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002463 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002464 s->do_telnetopt++;
2465 }
2466 s->do_telnetopt++;
2467 }
2468 if (s->do_telnetopt >= 4) {
2469 s->do_telnetopt = 1;
2470 }
2471 } else {
2472 if ((unsigned char)buf[i] == IAC) {
2473 s->do_telnetopt = 2;
2474 } else {
2475 if (j != i)
2476 buf[j] = buf[i];
2477 j++;
2478 }
2479 }
2480 }
2481 *size = j;
2482}
2483
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002484static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002485{
2486 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002487 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2488
2489 if (to_copy) {
2490 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2491
2492 g_free(s->read_msgfds);
2493 s->read_msgfds = 0;
2494 s->read_msgfds_num = 0;
2495 }
2496
2497 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002498}
2499
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002500static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2501{
2502 TCPCharDriver *s = chr->opaque;
2503
2504 /* clear old pending fd array */
2505 if (s->write_msgfds) {
2506 g_free(s->write_msgfds);
2507 }
2508
2509 if (num) {
2510 s->write_msgfds = g_malloc(num * sizeof(int));
2511 memcpy(s->write_msgfds, fds, num * sizeof(int));
2512 }
2513
2514 s->write_msgfds_num = num;
2515
2516 return 0;
2517}
2518
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002519#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002520static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2521{
2522 TCPCharDriver *s = chr->opaque;
2523 struct cmsghdr *cmsg;
2524
2525 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002526 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002527
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002528 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002529 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002530 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002531 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002532 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002533
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002534 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2535
2536 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002537 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002538 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002539
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002540 /* close and clean read_msgfds */
2541 for (i = 0; i < s->read_msgfds_num; i++) {
2542 close(s->read_msgfds[i]);
2543 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002544
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002545 if (s->read_msgfds_num) {
2546 g_free(s->read_msgfds);
2547 }
2548
2549 s->read_msgfds_num = fd_size / sizeof(int);
2550 s->read_msgfds = g_malloc(fd_size);
2551 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2552
2553 for (i = 0; i < s->read_msgfds_num; i++) {
2554 int fd = s->read_msgfds[i];
2555 if (fd < 0) {
2556 continue;
2557 }
2558
2559 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2560 qemu_set_block(fd);
2561
2562 #ifndef MSG_CMSG_CLOEXEC
2563 qemu_set_cloexec(fd);
2564 #endif
2565 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002566 }
2567}
2568
Mark McLoughlin9977c892009-07-22 09:11:38 +01002569static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2570{
2571 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002572 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002573 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002574 union {
2575 struct cmsghdr cmsg;
2576 char control[CMSG_SPACE(sizeof(int))];
2577 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002578 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002579 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002580
2581 iov[0].iov_base = buf;
2582 iov[0].iov_len = len;
2583
2584 msg.msg_iov = iov;
2585 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002586 msg.msg_control = &msg_control;
2587 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002588
Corey Bryant06138652012-08-14 16:43:42 -04002589#ifdef MSG_CMSG_CLOEXEC
2590 flags |= MSG_CMSG_CLOEXEC;
2591#endif
2592 ret = recvmsg(s->fd, &msg, flags);
2593 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002594 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002595 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002596
2597 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002598}
2599#else
2600static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2601{
2602 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002603 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002604}
2605#endif
2606
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302607static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2608{
2609 TCPCharDriver *s = chr->opaque;
2610 return g_io_create_watch(s->chan, cond);
2611}
2612
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002613static void tcp_chr_disconnect(CharDriverState *chr)
2614{
2615 TCPCharDriver *s = chr->opaque;
2616
2617 s->connected = 0;
2618 if (s->listen_chan) {
2619 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2620 tcp_chr_accept, chr);
2621 }
2622 remove_fd_in_watch(chr);
2623 g_io_channel_unref(s->chan);
2624 s->chan = NULL;
2625 closesocket(s->fd);
2626 s->fd = -1;
2627 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2628}
2629
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302630static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002631{
2632 CharDriverState *chr = opaque;
2633 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302634 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002635 int len, size;
2636
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302637 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002638 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302639 }
aliguori6f97dba2008-10-31 18:49:55 +00002640 len = sizeof(buf);
2641 if (len > s->max_size)
2642 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002643 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002644 if (size == 0) {
2645 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002646 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002647 } else if (size > 0) {
2648 if (s->do_telnetopt)
2649 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2650 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002651 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002652 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302653
2654 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002655}
2656
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002657static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2658{
2659 TCPCharDriver *s = chr->opaque;
2660 int size;
2661
2662 if (!s->connected) {
2663 return 0;
2664 }
2665
2666 size = tcp_chr_recv(chr, (void *) buf, len);
2667 if (size == 0) {
2668 /* connection closed */
2669 tcp_chr_disconnect(chr);
2670 }
2671
2672 return size;
2673}
2674
Blue Swirl68c18d12010-08-15 09:46:24 +00002675#ifndef _WIN32
2676CharDriverState *qemu_chr_open_eventfd(int eventfd)
2677{
David Marchande9d21c42014-06-11 17:25:16 +02002678 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2679
2680 if (chr) {
2681 chr->avail_connections = 1;
2682 }
2683
2684 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002685}
Blue Swirl68c18d12010-08-15 09:46:24 +00002686#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002687
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002688static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2689 void *opaque)
2690{
2691 CharDriverState *chr = opaque;
2692
2693 if (cond != G_IO_HUP) {
2694 return FALSE;
2695 }
2696
2697 /* connection closed */
2698 tcp_chr_disconnect(chr);
2699 if (chr->fd_hup_tag) {
2700 g_source_remove(chr->fd_hup_tag);
2701 chr->fd_hup_tag = 0;
2702 }
2703
2704 return TRUE;
2705}
2706
aliguori6f97dba2008-10-31 18:49:55 +00002707static void tcp_chr_connect(void *opaque)
2708{
2709 CharDriverState *chr = opaque;
2710 TCPCharDriver *s = chr->opaque;
2711
2712 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302713 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302714 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2715 tcp_chr_read, chr);
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002716 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2717 chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002718 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002719 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002720}
2721
Gal Hammerac1b84d2014-02-25 12:12:35 +02002722static void tcp_chr_update_read_handler(CharDriverState *chr)
2723{
2724 TCPCharDriver *s = chr->opaque;
2725
2726 remove_fd_in_watch(chr);
2727 if (s->chan) {
2728 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2729 tcp_chr_read, chr);
2730 }
2731}
2732
aliguori6f97dba2008-10-31 18:49:55 +00002733#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2734static void tcp_chr_telnet_init(int fd)
2735{
2736 char buf[3];
2737 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2738 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2739 send(fd, (char *)buf, 3, 0);
2740 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2741 send(fd, (char *)buf, 3, 0);
2742 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2743 send(fd, (char *)buf, 3, 0);
2744 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2745 send(fd, (char *)buf, 3, 0);
2746}
2747
Daniel P. Berrange13661082011-06-23 13:31:42 +01002748static int tcp_chr_add_client(CharDriverState *chr, int fd)
2749{
2750 TCPCharDriver *s = chr->opaque;
2751 if (s->fd != -1)
2752 return -1;
2753
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002754 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002755 if (s->do_nodelay)
2756 socket_set_nodelay(fd);
2757 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302758 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002759 if (s->listen_tag) {
2760 g_source_remove(s->listen_tag);
2761 s->listen_tag = 0;
2762 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002763 tcp_chr_connect(chr);
2764
2765 return 0;
2766}
2767
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302768static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002769{
2770 CharDriverState *chr = opaque;
2771 TCPCharDriver *s = chr->opaque;
2772 struct sockaddr_in saddr;
2773#ifndef _WIN32
2774 struct sockaddr_un uaddr;
2775#endif
2776 struct sockaddr *addr;
2777 socklen_t len;
2778 int fd;
2779
2780 for(;;) {
2781#ifndef _WIN32
2782 if (s->is_unix) {
2783 len = sizeof(uaddr);
2784 addr = (struct sockaddr *)&uaddr;
2785 } else
2786#endif
2787 {
2788 len = sizeof(saddr);
2789 addr = (struct sockaddr *)&saddr;
2790 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002791 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002792 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002793 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302794 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002795 } else if (fd >= 0) {
2796 if (s->do_telnetopt)
2797 tcp_chr_telnet_init(fd);
2798 break;
2799 }
2800 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002801 if (tcp_chr_add_client(chr, fd) < 0)
2802 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302803
2804 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002805}
2806
2807static void tcp_chr_close(CharDriverState *chr)
2808{
2809 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002810 int i;
aliguori819f56b2009-03-28 17:58:14 +00002811 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302812 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302813 if (s->chan) {
2814 g_io_channel_unref(s->chan);
2815 }
aliguori6f97dba2008-10-31 18:49:55 +00002816 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002817 }
2818 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302819 if (s->listen_tag) {
2820 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002821 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302822 }
2823 if (s->listen_chan) {
2824 g_io_channel_unref(s->listen_chan);
2825 }
aliguori6f97dba2008-10-31 18:49:55 +00002826 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002827 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002828 if (s->read_msgfds_num) {
2829 for (i = 0; i < s->read_msgfds_num; i++) {
2830 close(s->read_msgfds[i]);
2831 }
2832 g_free(s->read_msgfds);
2833 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002834 if (s->write_msgfds_num) {
2835 g_free(s->write_msgfds);
2836 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002837 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002838 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002839}
2840
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002841static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2842 bool is_listen, bool is_telnet,
2843 bool is_waitconnect,
2844 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002845{
2846 CharDriverState *chr = NULL;
2847 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002848 char host[NI_MAXHOST], serv[NI_MAXSERV];
2849 const char *left = "", *right = "";
2850 struct sockaddr_storage ss;
2851 socklen_t ss_len = sizeof(ss);
2852
2853 memset(&ss, 0, ss_len);
2854 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002855 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002856 return NULL;
2857 }
2858
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002859 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002860 s = g_malloc0(sizeof(TCPCharDriver));
2861
2862 s->connected = 0;
2863 s->fd = -1;
2864 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002865 s->read_msgfds = 0;
2866 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002867 s->write_msgfds = 0;
2868 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002869
2870 chr->filename = g_malloc(256);
2871 switch (ss.ss_family) {
2872#ifndef _WIN32
2873 case AF_UNIX:
2874 s->is_unix = 1;
2875 snprintf(chr->filename, 256, "unix:%s%s",
2876 ((struct sockaddr_un *)(&ss))->sun_path,
2877 is_listen ? ",server" : "");
2878 break;
2879#endif
2880 case AF_INET6:
2881 left = "[";
2882 right = "]";
2883 /* fall through */
2884 case AF_INET:
2885 s->do_nodelay = do_nodelay;
2886 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2887 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002888 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002889 is_telnet ? "telnet" : "tcp",
2890 left, host, right, serv,
2891 is_listen ? ",server" : "");
2892 break;
2893 }
2894
2895 chr->opaque = s;
2896 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002897 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002898 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002899 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002900 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002901 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302902 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002903 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002904 /* be isn't opened until we get a connection */
2905 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002906
2907 if (is_listen) {
2908 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302909 s->listen_chan = io_channel_from_socket(s->listen_fd);
2910 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002911 if (is_telnet) {
2912 s->do_telnetopt = 1;
2913 }
2914 } else {
2915 s->connected = 1;
2916 s->fd = fd;
2917 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302918 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002919 tcp_chr_connect(chr);
2920 }
2921
2922 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002923 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2924 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302925 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002926 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002927 }
2928 return chr;
2929}
2930
2931static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2932{
2933 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002934 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002935 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002936
liguange990a392013-06-18 11:45:35 +08002937 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2938 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2939 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2940 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2941 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002942
aliguorif07b6002008-11-11 20:54:09 +00002943 if (is_unix) {
2944 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002945 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002946 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002947 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002948 }
2949 } else {
2950 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002951 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002952 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002953 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002954 }
2955 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002956 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002957 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002958 }
aliguori6f97dba2008-10-31 18:49:55 +00002959
2960 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002961 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002962
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002963 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2964 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002965 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002966 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002967 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002968 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002969
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002970
aliguori6f97dba2008-10-31 18:49:55 +00002971 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002972 if (local_err) {
2973 qerror_report_err(local_err);
2974 error_free(local_err);
2975 }
2976 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002977 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002978 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002979 if (chr) {
2980 g_free(chr->opaque);
2981 g_free(chr);
2982 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002983 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002984}
2985
Lei Li51767e72013-01-25 00:03:19 +08002986/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002987/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002988
2989typedef struct {
2990 size_t size;
2991 size_t prod;
2992 size_t cons;
2993 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002994} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002995
Markus Armbruster3949e592013-02-06 21:27:24 +01002996static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002997{
Markus Armbruster3949e592013-02-06 21:27:24 +01002998 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002999
Markus Armbruster5c230102013-02-06 21:27:23 +01003000 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003001}
3002
Markus Armbruster3949e592013-02-06 21:27:24 +01003003static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003004{
Markus Armbruster3949e592013-02-06 21:27:24 +01003005 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003006 int i;
3007
3008 if (!buf || (len < 0)) {
3009 return -1;
3010 }
3011
3012 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003013 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3014 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003015 d->cons = d->prod - d->size;
3016 }
3017 }
3018
3019 return 0;
3020}
3021
Markus Armbruster3949e592013-02-06 21:27:24 +01003022static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003023{
Markus Armbruster3949e592013-02-06 21:27:24 +01003024 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003025 int i;
3026
Markus Armbruster5c230102013-02-06 21:27:23 +01003027 for (i = 0; i < len && d->cons != d->prod; i++) {
3028 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003029 }
3030
3031 return i;
3032}
3033
Markus Armbruster3949e592013-02-06 21:27:24 +01003034static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003035{
Markus Armbruster3949e592013-02-06 21:27:24 +01003036 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003037
3038 g_free(d->cbuf);
3039 g_free(d);
3040 chr->opaque = NULL;
3041}
3042
Markus Armbruster4f573782013-07-26 16:44:32 +02003043static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3044 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003045{
3046 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003047 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003048
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003049 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003050 d = g_malloc(sizeof(*d));
3051
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003052 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003053
3054 /* The size must be power of 2 */
3055 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003056 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003057 goto fail;
3058 }
3059
3060 d->prod = 0;
3061 d->cons = 0;
3062 d->cbuf = g_malloc0(d->size);
3063
3064 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003065 chr->chr_write = ringbuf_chr_write;
3066 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003067
3068 return chr;
3069
3070fail:
3071 g_free(d);
3072 g_free(chr);
3073 return NULL;
3074}
3075
Hani Benhabiles8e597772014-05-27 23:39:30 +01003076bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003077{
Markus Armbruster3949e592013-02-06 21:27:24 +01003078 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003079}
3080
Markus Armbruster3949e592013-02-06 21:27:24 +01003081void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003082 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003083 Error **errp)
3084{
3085 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003086 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003087 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003088 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003089
3090 chr = qemu_chr_find(device);
3091 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003092 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003093 return;
3094 }
3095
Markus Armbruster3949e592013-02-06 21:27:24 +01003096 if (!chr_is_ringbuf(chr)) {
3097 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003098 return;
3099 }
3100
Lei Li1f590cf2013-01-25 00:03:20 +08003101 if (has_format && (format == DATA_FORMAT_BASE64)) {
3102 write_data = g_base64_decode(data, &write_count);
3103 } else {
3104 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003105 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003106 }
3107
Markus Armbruster3949e592013-02-06 21:27:24 +01003108 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003109
Markus Armbruster13289fb2013-02-06 21:27:18 +01003110 if (write_data != (uint8_t *)data) {
3111 g_free((void *)write_data);
3112 }
3113
Lei Li1f590cf2013-01-25 00:03:20 +08003114 if (ret < 0) {
3115 error_setg(errp, "Failed to write to device %s", device);
3116 return;
3117 }
3118}
3119
Markus Armbruster3949e592013-02-06 21:27:24 +01003120char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003121 bool has_format, enum DataFormat format,
3122 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003123{
3124 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003125 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003126 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003127 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003128
3129 chr = qemu_chr_find(device);
3130 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003131 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003132 return NULL;
3133 }
3134
Markus Armbruster3949e592013-02-06 21:27:24 +01003135 if (!chr_is_ringbuf(chr)) {
3136 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003137 return NULL;
3138 }
3139
3140 if (size <= 0) {
3141 error_setg(errp, "size must be greater than zero");
3142 return NULL;
3143 }
3144
Markus Armbruster3949e592013-02-06 21:27:24 +01003145 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003146 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003147 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003148
Markus Armbruster3949e592013-02-06 21:27:24 +01003149 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003150
3151 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003152 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003153 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003154 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003155 /*
3156 * FIXME should read only complete, valid UTF-8 characters up
3157 * to @size bytes. Invalid sequences should be replaced by a
3158 * suitable replacement character. Except when (and only
3159 * when) ring buffer lost characters since last read, initial
3160 * continuation characters should be dropped.
3161 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003162 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003163 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003164 }
3165
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003166 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003167}
3168
Gerd Hoffmann33521632009-12-08 13:11:49 +01003169QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003170{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003171 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003172 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003173 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003174 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003175 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003176
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003177 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003178 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003179 qerror_report_err(local_err);
3180 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003181 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003182 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003183
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003184 if (strstart(filename, "mon:", &p)) {
3185 filename = p;
3186 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003187 if (strcmp(filename, "stdio") == 0) {
3188 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3189 * but pass it to the guest. Handle this only for compat syntax,
3190 * for -chardev syntax we have special option for this.
3191 * This is what -nographic did, redirecting+muxing serial+monitor
3192 * to stdio causing Ctrl+C to be passed to guest. */
3193 qemu_opt_set(opts, "signal", "off");
3194 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003195 }
3196
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003197 if (strcmp(filename, "null") == 0 ||
3198 strcmp(filename, "pty") == 0 ||
3199 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003200 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003201 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003202 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003203 return opts;
3204 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003205 if (strstart(filename, "vc", &p)) {
3206 qemu_opt_set(opts, "backend", "vc");
3207 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003208 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003209 /* pixels */
3210 qemu_opt_set(opts, "width", width);
3211 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003212 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003213 /* chars */
3214 qemu_opt_set(opts, "cols", width);
3215 qemu_opt_set(opts, "rows", height);
3216 } else {
3217 goto fail;
3218 }
3219 }
3220 return opts;
3221 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003222 if (strcmp(filename, "con:") == 0) {
3223 qemu_opt_set(opts, "backend", "console");
3224 return opts;
3225 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003226 if (strstart(filename, "COM", NULL)) {
3227 qemu_opt_set(opts, "backend", "serial");
3228 qemu_opt_set(opts, "path", filename);
3229 return opts;
3230 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003231 if (strstart(filename, "file:", &p)) {
3232 qemu_opt_set(opts, "backend", "file");
3233 qemu_opt_set(opts, "path", p);
3234 return opts;
3235 }
3236 if (strstart(filename, "pipe:", &p)) {
3237 qemu_opt_set(opts, "backend", "pipe");
3238 qemu_opt_set(opts, "path", p);
3239 return opts;
3240 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003241 if (strstart(filename, "tcp:", &p) ||
3242 strstart(filename, "telnet:", &p)) {
3243 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3244 host[0] = 0;
3245 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3246 goto fail;
3247 }
3248 qemu_opt_set(opts, "backend", "socket");
3249 qemu_opt_set(opts, "host", host);
3250 qemu_opt_set(opts, "port", port);
3251 if (p[pos] == ',') {
3252 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3253 goto fail;
3254 }
3255 if (strstart(filename, "telnet:", &p))
3256 qemu_opt_set(opts, "telnet", "on");
3257 return opts;
3258 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003259 if (strstart(filename, "udp:", &p)) {
3260 qemu_opt_set(opts, "backend", "udp");
3261 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3262 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003263 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003264 goto fail;
3265 }
3266 }
3267 qemu_opt_set(opts, "host", host);
3268 qemu_opt_set(opts, "port", port);
3269 if (p[pos] == '@') {
3270 p += pos + 1;
3271 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3272 host[0] = 0;
3273 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003274 goto fail;
3275 }
3276 }
3277 qemu_opt_set(opts, "localaddr", host);
3278 qemu_opt_set(opts, "localport", port);
3279 }
3280 return opts;
3281 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003282 if (strstart(filename, "unix:", &p)) {
3283 qemu_opt_set(opts, "backend", "socket");
3284 if (qemu_opts_do_parse(opts, p, "path") != 0)
3285 goto fail;
3286 return opts;
3287 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003288 if (strstart(filename, "/dev/parport", NULL) ||
3289 strstart(filename, "/dev/ppi", NULL)) {
3290 qemu_opt_set(opts, "backend", "parport");
3291 qemu_opt_set(opts, "path", filename);
3292 return opts;
3293 }
3294 if (strstart(filename, "/dev/", NULL)) {
3295 qemu_opt_set(opts, "backend", "tty");
3296 qemu_opt_set(opts, "path", filename);
3297 return opts;
3298 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003299
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003300fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003301 qemu_opts_del(opts);
3302 return NULL;
3303}
3304
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003305static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3306 Error **errp)
3307{
3308 const char *path = qemu_opt_get(opts, "path");
3309
3310 if (path == NULL) {
3311 error_setg(errp, "chardev: file: no filename given");
3312 return;
3313 }
3314 backend->file = g_new0(ChardevFile, 1);
3315 backend->file->out = g_strdup(path);
3316}
3317
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003318static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3319 Error **errp)
3320{
3321 backend->stdio = g_new0(ChardevStdio, 1);
3322 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003323 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003324}
3325
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003326static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3327 Error **errp)
3328{
3329 const char *device = qemu_opt_get(opts, "path");
3330
3331 if (device == NULL) {
3332 error_setg(errp, "chardev: serial/tty: no device path given");
3333 return;
3334 }
3335 backend->serial = g_new0(ChardevHostdev, 1);
3336 backend->serial->device = g_strdup(device);
3337}
3338
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003339static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3340 Error **errp)
3341{
3342 const char *device = qemu_opt_get(opts, "path");
3343
3344 if (device == NULL) {
3345 error_setg(errp, "chardev: parallel: no device path given");
3346 return;
3347 }
3348 backend->parallel = g_new0(ChardevHostdev, 1);
3349 backend->parallel->device = g_strdup(device);
3350}
3351
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003352static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3353 Error **errp)
3354{
3355 const char *device = qemu_opt_get(opts, "path");
3356
3357 if (device == NULL) {
3358 error_setg(errp, "chardev: pipe: no device path given");
3359 return;
3360 }
3361 backend->pipe = g_new0(ChardevHostdev, 1);
3362 backend->pipe->device = g_strdup(device);
3363}
3364
Markus Armbruster4f573782013-07-26 16:44:32 +02003365static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3366 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003367{
3368 int val;
3369
Markus Armbruster3a1da422013-07-26 16:44:34 +02003370 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003371
Markus Armbruster0f953052013-06-27 16:22:07 +02003372 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003373 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003374 backend->ringbuf->has_size = true;
3375 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003376 }
3377}
3378
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003379static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3380 Error **errp)
3381{
3382 const char *chardev = qemu_opt_get(opts, "chardev");
3383
3384 if (chardev == NULL) {
3385 error_setg(errp, "chardev: mux: no chardev given");
3386 return;
3387 }
3388 backend->mux = g_new0(ChardevMux, 1);
3389 backend->mux->chardev = g_strdup(chardev);
3390}
3391
Anthony Liguorid654f342013-03-05 23:21:28 +05303392typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003393 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003394 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003395 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003396 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003397 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003398 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303399} CharDriver;
3400
3401static GSList *backends;
3402
3403void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3404{
3405 CharDriver *s;
3406
3407 s = g_malloc0(sizeof(*s));
3408 s->name = g_strdup(name);
3409 s->open = open;
3410
3411 backends = g_slist_append(backends, s);
3412}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003413
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003414void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003415 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3416{
3417 CharDriver *s;
3418
3419 s = g_malloc0(sizeof(*s));
3420 s->name = g_strdup(name);
3421 s->kind = kind;
3422 s->parse = parse;
3423
3424 backends = g_slist_append(backends, s);
3425}
3426
Anthony Liguorif69554b2011-08-15 11:17:37 -05003427CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003428 void (*init)(struct CharDriverState *s),
3429 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003430{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003431 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303432 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003433 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303434 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003435
3436 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003437 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003438 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003439 }
3440
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003441 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003442 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003443 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003444 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003445 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303446 for (i = backends; i; i = i->next) {
3447 cd = i->data;
3448
3449 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003450 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303451 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003452 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303453 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003454 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003455 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003456 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003457 }
3458
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003459 if (!cd->open) {
3460 /* using new, qapi init */
3461 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3462 ChardevReturn *ret = NULL;
3463 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003464 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003465
3466 if (qemu_opt_get_bool(opts, "mux", 0)) {
3467 bid = g_strdup_printf("%s-base", id);
3468 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003469
3470 chr = NULL;
3471 backend->kind = cd->kind;
3472 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003473 cd->parse(opts, backend, &local_err);
3474 if (local_err) {
3475 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003476 goto qapi_out;
3477 }
3478 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003479 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003480 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003481 goto qapi_out;
3482 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003483
3484 if (bid) {
3485 qapi_free_ChardevBackend(backend);
3486 qapi_free_ChardevReturn(ret);
3487 backend = g_new0(ChardevBackend, 1);
3488 backend->mux = g_new0(ChardevMux, 1);
3489 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3490 backend->mux->chardev = g_strdup(bid);
3491 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003492 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003493 chr = qemu_chr_find(bid);
3494 qemu_chr_delete(chr);
3495 chr = NULL;
3496 goto qapi_out;
3497 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003498 }
3499
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003500 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003501 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003502
3503 qapi_out:
3504 qapi_free_ChardevBackend(backend);
3505 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003506 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003507 return chr;
3508 }
3509
Anthony Liguorid654f342013-03-05 23:21:28 +05303510 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003511 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003512 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003513 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003514 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003515 }
3516
3517 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003518 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003519 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003520 /* if we didn't create the chardev via qmp_chardev_add, we
3521 * need to send the OPENED event here
3522 */
3523 if (!chr->explicit_be_open) {
3524 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3525 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003526 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003527
3528 if (qemu_opt_get_bool(opts, "mux", 0)) {
3529 CharDriverState *base = chr;
3530 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003531 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003532 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3533 chr = qemu_chr_open_mux(base);
3534 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003535 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003536 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003537 } else {
3538 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003539 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003540 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003541 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003542 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003543
3544err:
3545 qemu_opts_del(opts);
3546 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003547}
3548
Anthony Liguori27143a42011-08-15 11:17:36 -05003549CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003550{
3551 const char *p;
3552 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003553 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003554 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003555
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003556 if (strstart(filename, "chardev:", &p)) {
3557 return qemu_chr_find(p);
3558 }
3559
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003560 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003561 if (!opts)
3562 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003563
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003564 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003565 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003566 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003567 error_free(err);
3568 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003569 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003570 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003571 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003572 }
3573 return chr;
3574}
3575
Anthony Liguori15f31512011-08-15 11:17:35 -05003576void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003577{
3578 if (chr->chr_set_echo) {
3579 chr->chr_set_echo(chr, echo);
3580 }
3581}
3582
Hans de Goede8e25daa2013-03-26 11:07:57 +01003583void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003584{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003585 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003586 return;
3587 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003588 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003589 if (chr->chr_set_fe_open) {
3590 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003591 }
3592}
3593
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003594void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3595{
3596 if (chr->chr_fe_event) {
3597 chr->chr_fe_event(chr, event);
3598 }
3599}
3600
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003601int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3602 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303603{
3604 GSource *src;
3605 guint tag;
3606
3607 if (s->chr_add_watch == NULL) {
3608 return -ENOSYS;
3609 }
3610
3611 src = s->chr_add_watch(s, cond);
3612 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3613 tag = g_source_attach(src, NULL);
3614 g_source_unref(src);
3615
3616 return tag;
3617}
3618
Hans de Goede44c473d2013-03-27 20:29:39 +01003619int qemu_chr_fe_claim(CharDriverState *s)
3620{
3621 if (s->avail_connections < 1) {
3622 return -1;
3623 }
3624 s->avail_connections--;
3625 return 0;
3626}
3627
3628void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3629{
3630 if (qemu_chr_fe_claim(s) != 0) {
3631 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3632 __func__, s->label);
3633 exit(1);
3634 }
3635}
3636
3637void qemu_chr_fe_release(CharDriverState *s)
3638{
3639 s->avail_connections++;
3640}
3641
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003642void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003643{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003644 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003645 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003646 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003647 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003648 g_free(chr->filename);
3649 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003650 if (chr->opts) {
3651 qemu_opts_del(chr->opts);
3652 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003653 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003654}
3655
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003656ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003657{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003658 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003659 CharDriverState *chr;
3660
Blue Swirl72cf2d42009-09-12 07:36:22 +00003661 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003662 ChardevInfoList *info = g_malloc0(sizeof(*info));
3663 info->value = g_malloc0(sizeof(*info->value));
3664 info->value->label = g_strdup(chr->label);
3665 info->value->filename = g_strdup(chr->filename);
3666
3667 info->next = chr_list;
3668 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003669 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003670
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003671 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003672}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003673
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003674ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3675{
3676 ChardevBackendInfoList *backend_list = NULL;
3677 CharDriver *c = NULL;
3678 GSList *i = NULL;
3679
3680 for (i = backends; i; i = i->next) {
3681 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3682 c = i->data;
3683 info->value = g_malloc0(sizeof(*info->value));
3684 info->value->name = g_strdup(c->name);
3685
3686 info->next = backend_list;
3687 backend_list = info;
3688 }
3689
3690 return backend_list;
3691}
3692
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003693CharDriverState *qemu_chr_find(const char *name)
3694{
3695 CharDriverState *chr;
3696
Blue Swirl72cf2d42009-09-12 07:36:22 +00003697 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003698 if (strcmp(chr->label, name) != 0)
3699 continue;
3700 return chr;
3701 }
3702 return NULL;
3703}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003704
3705/* Get a character (serial) device interface. */
3706CharDriverState *qemu_char_get_next_serial(void)
3707{
3708 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003709 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003710
3711 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003712
3713 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3714 chr = serial_hds[next_serial++];
3715 qemu_chr_fe_claim_no_fail(chr);
3716 return chr;
3717 }
3718 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003719}
3720
Paolo Bonzini4d454572012-11-26 16:03:42 +01003721QemuOptsList qemu_chardev_opts = {
3722 .name = "chardev",
3723 .implied_opt_name = "backend",
3724 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3725 .desc = {
3726 {
3727 .name = "backend",
3728 .type = QEMU_OPT_STRING,
3729 },{
3730 .name = "path",
3731 .type = QEMU_OPT_STRING,
3732 },{
3733 .name = "host",
3734 .type = QEMU_OPT_STRING,
3735 },{
3736 .name = "port",
3737 .type = QEMU_OPT_STRING,
3738 },{
3739 .name = "localaddr",
3740 .type = QEMU_OPT_STRING,
3741 },{
3742 .name = "localport",
3743 .type = QEMU_OPT_STRING,
3744 },{
3745 .name = "to",
3746 .type = QEMU_OPT_NUMBER,
3747 },{
3748 .name = "ipv4",
3749 .type = QEMU_OPT_BOOL,
3750 },{
3751 .name = "ipv6",
3752 .type = QEMU_OPT_BOOL,
3753 },{
3754 .name = "wait",
3755 .type = QEMU_OPT_BOOL,
3756 },{
3757 .name = "server",
3758 .type = QEMU_OPT_BOOL,
3759 },{
3760 .name = "delay",
3761 .type = QEMU_OPT_BOOL,
3762 },{
3763 .name = "telnet",
3764 .type = QEMU_OPT_BOOL,
3765 },{
3766 .name = "width",
3767 .type = QEMU_OPT_NUMBER,
3768 },{
3769 .name = "height",
3770 .type = QEMU_OPT_NUMBER,
3771 },{
3772 .name = "cols",
3773 .type = QEMU_OPT_NUMBER,
3774 },{
3775 .name = "rows",
3776 .type = QEMU_OPT_NUMBER,
3777 },{
3778 .name = "mux",
3779 .type = QEMU_OPT_BOOL,
3780 },{
3781 .name = "signal",
3782 .type = QEMU_OPT_BOOL,
3783 },{
3784 .name = "name",
3785 .type = QEMU_OPT_STRING,
3786 },{
3787 .name = "debug",
3788 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003789 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003790 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003791 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003792 },{
3793 .name = "chardev",
3794 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003795 },
3796 { /* end of list */ }
3797 },
3798};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003799
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003800#ifdef _WIN32
3801
3802static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3803{
3804 HANDLE out;
3805
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003806 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003807 error_setg(errp, "input file not supported");
3808 return NULL;
3809 }
3810
3811 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3812 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3813 if (out == INVALID_HANDLE_VALUE) {
3814 error_setg(errp, "open %s failed", file->out);
3815 return NULL;
3816 }
3817 return qemu_chr_open_win_file(out);
3818}
3819
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003820static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3821 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003822{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003823 return qemu_chr_open_win_path(serial->device);
3824}
3825
3826static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3827 Error **errp)
3828{
3829 error_setg(errp, "character device backend type 'parallel' not supported");
3830 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003831}
3832
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003833#else /* WIN32 */
3834
3835static int qmp_chardev_open_file_source(char *src, int flags,
3836 Error **errp)
3837{
3838 int fd = -1;
3839
3840 TFR(fd = qemu_open(src, flags, 0666));
3841 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003842 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003843 }
3844 return fd;
3845}
3846
3847static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3848{
Markus Armbruster5f758362014-05-19 18:57:34 +02003849 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003850
3851 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3852 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003853 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003854 return NULL;
3855 }
3856
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003857 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003858 flags = O_RDONLY;
3859 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003860 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003861 qemu_close(out);
3862 return NULL;
3863 }
3864 }
3865
3866 return qemu_chr_open_fd(in, out);
3867}
3868
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003869static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3870 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003871{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003872#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003873 int fd;
3874
3875 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003876 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003877 return NULL;
3878 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003879 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003880 return qemu_chr_open_tty_fd(fd);
3881#else
3882 error_setg(errp, "character device backend type 'serial' not supported");
3883 return NULL;
3884#endif
3885}
3886
3887static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3888 Error **errp)
3889{
3890#ifdef HAVE_CHARDEV_PARPORT
3891 int fd;
3892
3893 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003894 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003895 return NULL;
3896 }
3897 return qemu_chr_open_pp_fd(fd);
3898#else
3899 error_setg(errp, "character device backend type 'parallel' not supported");
3900 return NULL;
3901#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003902}
3903
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003904#endif /* WIN32 */
3905
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003906static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3907 Error **errp)
3908{
3909 SocketAddress *addr = sock->addr;
3910 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3911 bool is_listen = sock->has_server ? sock->server : true;
3912 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3913 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3914 int fd;
3915
3916 if (is_listen) {
3917 fd = socket_listen(addr, errp);
3918 } else {
3919 fd = socket_connect(addr, errp, NULL, NULL);
3920 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003921 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003922 return NULL;
3923 }
3924 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3925 is_telnet, is_waitconnect, errp);
3926}
3927
Lei Li08d0ab32013-05-20 14:51:03 +08003928static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3929 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003930{
3931 int fd;
3932
Lei Li08d0ab32013-05-20 14:51:03 +08003933 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003934 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003935 return NULL;
3936 }
3937 return qemu_chr_open_udp_fd(fd);
3938}
3939
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003940ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3941 Error **errp)
3942{
3943 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003944 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003945
3946 chr = qemu_chr_find(id);
3947 if (chr) {
3948 error_setg(errp, "Chardev '%s' already exists", id);
3949 g_free(ret);
3950 return NULL;
3951 }
3952
3953 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003954 case CHARDEV_BACKEND_KIND_FILE:
3955 chr = qmp_chardev_open_file(backend->file, errp);
3956 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003957 case CHARDEV_BACKEND_KIND_SERIAL:
3958 chr = qmp_chardev_open_serial(backend->serial, errp);
3959 break;
3960 case CHARDEV_BACKEND_KIND_PARALLEL:
3961 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003962 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003963 case CHARDEV_BACKEND_KIND_PIPE:
3964 chr = qemu_chr_open_pipe(backend->pipe);
3965 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003966 case CHARDEV_BACKEND_KIND_SOCKET:
3967 chr = qmp_chardev_open_socket(backend->socket, errp);
3968 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003969 case CHARDEV_BACKEND_KIND_UDP:
3970 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003971 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003972#ifdef HAVE_CHARDEV_TTY
3973 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003974 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003975 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003976#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003977 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003978 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003979 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003980 case CHARDEV_BACKEND_KIND_MUX:
3981 base = qemu_chr_find(backend->mux->chardev);
3982 if (base == NULL) {
3983 error_setg(errp, "mux: base chardev %s not found",
3984 backend->mux->chardev);
3985 break;
3986 }
3987 chr = qemu_chr_open_mux(base);
3988 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003989 case CHARDEV_BACKEND_KIND_MSMOUSE:
3990 chr = qemu_chr_open_msmouse();
3991 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003992#ifdef CONFIG_BRLAPI
3993 case CHARDEV_BACKEND_KIND_BRAILLE:
3994 chr = chr_baum_init();
3995 break;
3996#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003997 case CHARDEV_BACKEND_KIND_STDIO:
3998 chr = qemu_chr_open_stdio(backend->stdio);
3999 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004000#ifdef _WIN32
4001 case CHARDEV_BACKEND_KIND_CONSOLE:
4002 chr = qemu_chr_open_win_con();
4003 break;
4004#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004005#ifdef CONFIG_SPICE
4006 case CHARDEV_BACKEND_KIND_SPICEVMC:
4007 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4008 break;
4009 case CHARDEV_BACKEND_KIND_SPICEPORT:
4010 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4011 break;
4012#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004013 case CHARDEV_BACKEND_KIND_VC:
4014 chr = vc_init(backend->vc);
4015 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004016 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004017 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004018 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004019 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004020 default:
4021 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4022 break;
4023 }
4024
Markus Armbruster3894c782014-05-19 18:57:36 +02004025 /*
4026 * Character backend open hasn't been fully converted to the Error
4027 * API. Some opens fail without setting an error. Set a generic
4028 * error then.
4029 * TODO full conversion to Error API
4030 */
4031 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004032 error_setg(errp, "Failed to create chardev");
4033 }
4034 if (chr) {
4035 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004036 chr->avail_connections =
4037 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004038 if (!chr->filename) {
4039 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4040 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004041 if (!chr->explicit_be_open) {
4042 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4043 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004044 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4045 return ret;
4046 } else {
4047 g_free(ret);
4048 return NULL;
4049 }
4050}
4051
4052void qmp_chardev_remove(const char *id, Error **errp)
4053{
4054 CharDriverState *chr;
4055
4056 chr = qemu_chr_find(id);
4057 if (NULL == chr) {
4058 error_setg(errp, "Chardev '%s' not found", id);
4059 return;
4060 }
4061 if (chr->chr_can_read || chr->chr_read ||
4062 chr->chr_event || chr->handler_opaque) {
4063 error_setg(errp, "Chardev '%s' is busy", id);
4064 return;
4065 }
4066 qemu_chr_delete(chr);
4067}
Anthony Liguorid654f342013-03-05 23:21:28 +05304068
4069static void register_types(void)
4070{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004071 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304072 register_char_driver("socket", qemu_chr_open_socket);
4073 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004074 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004075 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004076 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4077 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004078 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4079 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004080 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4081 qemu_chr_parse_serial);
4082 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4083 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004084 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4085 qemu_chr_parse_parallel);
4086 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4087 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004088 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004089 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004090 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4091 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004092 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4093 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004094 /* Bug-compatibility: */
4095 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4096 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004097 /* this must be done after machine init, since we register FEs with muxes
4098 * as part of realize functions like serial_isa_realizefn when -nographic
4099 * is specified
4100 */
4101 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304102}
4103
4104type_init(register_types);