blob: 2a3cb9fb05ef4fe2ae34260aeccf28d3f132e667 [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));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +020097 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020098 return chr;
99}
100
Hans de Goedea425d232011-11-19 10:22:43 +0100101void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000102{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200103 /* Keep track if the char device is open */
104 switch (event) {
105 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100106 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200107 break;
108 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100109 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200110 break;
111 }
112
aliguori6f97dba2008-10-31 18:49:55 +0000113 if (!s->chr_event)
114 return;
115 s->chr_event(s->handler_opaque, event);
116}
117
Hans de Goedefee204f2013-03-26 11:07:54 +0100118void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000119{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500120 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000121}
122
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500123int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000124{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200125 int ret;
126
127 qemu_mutex_lock(&s->chr_write_lock);
128 ret = s->chr_write(s, buf, len);
129 qemu_mutex_unlock(&s->chr_write_lock);
130 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000131}
132
Anthony Liguoricd187202013-03-26 10:04:17 -0500133int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
134{
135 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200136 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500137
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200138 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500139 while (offset < len) {
140 do {
141 res = s->chr_write(s, buf + offset, len - offset);
142 if (res == -1 && errno == EAGAIN) {
143 g_usleep(100);
144 }
145 } while (res == -1 && errno == EAGAIN);
146
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200147 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500148 break;
149 }
150
Anthony Liguoricd187202013-03-26 10:04:17 -0500151 offset += res;
152 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200153 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500154
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200155 if (res < 0) {
156 return res;
157 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500158 return offset;
159}
160
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300161int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
162{
163 int offset = 0, counter = 10;
164 int res;
165
166 if (!s->chr_sync_read) {
167 return 0;
168 }
169
170 while (offset < len) {
171 do {
172 res = s->chr_sync_read(s, buf + offset, len - offset);
173 if (res == -1 && errno == EAGAIN) {
174 g_usleep(100);
175 }
176 } while (res == -1 && errno == EAGAIN);
177
178 if (res == 0) {
179 break;
180 }
181
182 if (res < 0) {
183 return res;
184 }
185
186 offset += res;
187
188 if (!counter--) {
189 break;
190 }
191 }
192
193 return offset;
194}
195
Anthony Liguori41084f12011-08-15 11:17:34 -0500196int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000197{
198 if (!s->chr_ioctl)
199 return -ENOTSUP;
200 return s->chr_ioctl(s, cmd, arg);
201}
202
Anthony Liguori909cda12011-08-15 11:17:31 -0500203int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000204{
205 if (!s->chr_can_read)
206 return 0;
207 return s->chr_can_read(s->handler_opaque);
208}
209
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500210void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000211{
Stefan Weilac310732012-04-19 22:27:14 +0200212 if (s->chr_read) {
213 s->chr_read(s->handler_opaque, buf, len);
214 }
aliguori6f97dba2008-10-31 18:49:55 +0000215}
216
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500217int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100218{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300219 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800220 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300221}
222
223int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
224{
225 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100226}
227
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300228int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
229{
230 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
231}
232
Daniel P. Berrange13661082011-06-23 13:31:42 +0100233int qemu_chr_add_client(CharDriverState *s, int fd)
234{
235 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
236}
237
aliguori6f97dba2008-10-31 18:49:55 +0000238void qemu_chr_accept_input(CharDriverState *s)
239{
240 if (s->chr_accept_input)
241 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100242 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000243}
244
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500245void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000246{
Amit Shah9bd78542009-11-03 19:59:54 +0530247 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000248 va_list ap;
249 va_start(ap, fmt);
250 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500251 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000252 va_end(ap);
253}
254
Amit Shah386a5a12013-08-28 15:24:05 +0530255static void remove_fd_in_watch(CharDriverState *chr);
256
aliguori6f97dba2008-10-31 18:49:55 +0000257void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100258 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000259 IOReadHandler *fd_read,
260 IOEventHandler *fd_event,
261 void *opaque)
262{
Hans de Goede19083222013-03-26 11:07:56 +0100263 int fe_open;
264
Amit Shahda7d9982011-04-25 15:18:22 +0530265 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100266 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530267 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100268 } else {
269 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530270 }
aliguori6f97dba2008-10-31 18:49:55 +0000271 s->chr_can_read = fd_can_read;
272 s->chr_read = fd_read;
273 s->chr_event = fd_event;
274 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200275 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000276 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200277
Hans de Goede19083222013-03-26 11:07:56 +0100278 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100279 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100280 }
281
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200282 /* We're connecting to an already opened device, so let's make sure we
283 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100284 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100285 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200286 }
aliguori6f97dba2008-10-31 18:49:55 +0000287}
288
289static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
290{
291 return len;
292}
293
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100294static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000295{
296 CharDriverState *chr;
297
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200298 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000299 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500300 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100301 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000302}
303
304/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000305#define MAX_MUX 4
306#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
307#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
308typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100309 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000310 IOReadHandler *chr_read[MAX_MUX];
311 IOEventHandler *chr_event[MAX_MUX];
312 void *ext_opaque[MAX_MUX];
313 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200314 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000315 int mux_cnt;
316 int term_got_escape;
317 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000318 /* Intermediate input buffer allows to catch escape sequences even if the
319 currently active device is not accepting any input - but only until it
320 is full as well. */
321 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
322 int prod[MAX_MUX];
323 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200324 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200325
326 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200327 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200328 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000329} MuxDriver;
330
331
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200332/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000333static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
334{
335 MuxDriver *d = chr->opaque;
336 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200337 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200338 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000339 } else {
340 int i;
341
342 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200343 for (i = 0; i < len; i++) {
344 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000345 char buf1[64];
346 int64_t ti;
347 int secs;
348
Alex Blighbc72ad62013-08-21 16:03:08 +0100349 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200350 if (d->timestamps_start == -1)
351 d->timestamps_start = ti;
352 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000353 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000354 snprintf(buf1, sizeof(buf1),
355 "[%02d:%02d:%02d.%03d] ",
356 secs / 3600,
357 (secs / 60) % 60,
358 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000359 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200360 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200361 d->linestart = 0;
362 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200363 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200364 if (buf[i] == '\n') {
365 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000366 }
367 }
368 }
369 return ret;
370}
371
372static const char * const mux_help[] = {
373 "% h print this help\n\r",
374 "% x exit emulator\n\r",
375 "% s save disk data back to file (if -snapshot)\n\r",
376 "% t toggle console timestamps\n\r"
377 "% b send break (magic sysrq)\n\r",
378 "% c switch between console and monitor\n\r",
379 "% % sends %\n\r",
380 NULL
381};
382
383int term_escape_char = 0x01; /* ctrl-a is used for escape */
384static void mux_print_help(CharDriverState *chr)
385{
386 int i, j;
387 char ebuf[15] = "Escape-Char";
388 char cbuf[50] = "\n\r";
389
390 if (term_escape_char > 0 && term_escape_char < 26) {
391 snprintf(cbuf, sizeof(cbuf), "\n\r");
392 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
393 } else {
394 snprintf(cbuf, sizeof(cbuf),
395 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
396 term_escape_char);
397 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200398 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000399 for (i = 0; mux_help[i] != NULL; i++) {
400 for (j=0; mux_help[i][j] != '\0'; j++) {
401 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200402 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000403 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200404 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000405 }
406 }
407}
408
aliguori2724b182009-03-05 23:01:47 +0000409static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
410{
411 if (d->chr_event[mux_nr])
412 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
413}
414
aliguori6f97dba2008-10-31 18:49:55 +0000415static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
416{
417 if (d->term_got_escape) {
418 d->term_got_escape = 0;
419 if (ch == term_escape_char)
420 goto send_char;
421 switch(ch) {
422 case '?':
423 case 'h':
424 mux_print_help(chr);
425 break;
426 case 'x':
427 {
428 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200429 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000430 exit(0);
431 break;
432 }
433 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200434 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000435 break;
436 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100437 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000438 break;
439 case 'c':
440 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200441 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
442 d->focus++;
443 if (d->focus >= d->mux_cnt)
444 d->focus = 0;
445 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000446 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200447 case 't':
448 d->timestamps = !d->timestamps;
449 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200450 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200451 break;
aliguori6f97dba2008-10-31 18:49:55 +0000452 }
453 } else if (ch == term_escape_char) {
454 d->term_got_escape = 1;
455 } else {
456 send_char:
457 return 1;
458 }
459 return 0;
460}
461
462static void mux_chr_accept_input(CharDriverState *chr)
463{
aliguori6f97dba2008-10-31 18:49:55 +0000464 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200465 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000466
aliguoria80bf992009-03-05 23:00:02 +0000467 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000468 d->chr_can_read[m] &&
469 d->chr_can_read[m](d->ext_opaque[m])) {
470 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000471 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000472 }
473}
474
475static int mux_chr_can_read(void *opaque)
476{
477 CharDriverState *chr = opaque;
478 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200479 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000480
aliguoria80bf992009-03-05 23:00:02 +0000481 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000482 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000483 if (d->chr_can_read[m])
484 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000485 return 0;
486}
487
488static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
489{
490 CharDriverState *chr = opaque;
491 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200492 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000493 int i;
494
495 mux_chr_accept_input (opaque);
496
497 for(i = 0; i < size; i++)
498 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000499 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000500 d->chr_can_read[m] &&
501 d->chr_can_read[m](d->ext_opaque[m]))
502 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
503 else
aliguoria80bf992009-03-05 23:00:02 +0000504 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000505 }
506}
507
508static void mux_chr_event(void *opaque, int event)
509{
510 CharDriverState *chr = opaque;
511 MuxDriver *d = chr->opaque;
512 int i;
513
514 /* Send the event to all registered listeners */
515 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000516 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000517}
518
519static void mux_chr_update_read_handler(CharDriverState *chr)
520{
521 MuxDriver *d = chr->opaque;
522
523 if (d->mux_cnt >= MAX_MUX) {
524 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
525 return;
526 }
527 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
528 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
529 d->chr_read[d->mux_cnt] = chr->chr_read;
530 d->chr_event[d->mux_cnt] = chr->chr_event;
531 /* Fix up the real driver with mux routines */
532 if (d->mux_cnt == 0) {
533 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
534 mux_chr_event, chr);
535 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200536 if (d->focus != -1) {
537 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200538 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200539 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000540 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200541 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000542}
543
Michael Roth7b7ab182013-07-30 13:04:22 -0500544static bool muxes_realized;
545
546/**
547 * Called after processing of default and command-line-specified
548 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
549 * to a mux chardev. This is done here to ensure that
550 * output/prompts/banners are only displayed for the FE that has
551 * focus when initial command-line processing/machine init is
552 * completed.
553 *
554 * After this point, any new FE attached to any new or existing
555 * mux will receive CHR_EVENT_OPENED notifications for the BE
556 * immediately.
557 */
558static void muxes_realize_done(Notifier *notifier, void *unused)
559{
560 CharDriverState *chr;
561
562 QTAILQ_FOREACH(chr, &chardevs, next) {
563 if (chr->is_mux) {
564 MuxDriver *d = chr->opaque;
565 int i;
566
567 /* send OPENED to all already-attached FEs */
568 for (i = 0; i < d->mux_cnt; i++) {
569 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
570 }
571 /* mark mux as OPENED so any new FEs will immediately receive
572 * OPENED event
573 */
574 qemu_chr_be_generic_open(chr);
575 }
576 }
577 muxes_realized = true;
578}
579
580static Notifier muxes_realize_notify = {
581 .notify = muxes_realize_done,
582};
583
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400584static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
585{
586 MuxDriver *d = s->opaque;
587 return d->drv->chr_add_watch(d->drv, cond);
588}
589
aliguori6f97dba2008-10-31 18:49:55 +0000590static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
591{
592 CharDriverState *chr;
593 MuxDriver *d;
594
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200595 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500596 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000597
598 chr->opaque = d;
599 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200600 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000601 chr->chr_write = mux_chr_write;
602 chr->chr_update_read_handler = mux_chr_update_read_handler;
603 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100604 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100605 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400606 if (drv->chr_add_watch) {
607 chr->chr_add_watch = mux_chr_add_watch;
608 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500609 /* only default to opened state if we've realized the initial
610 * set of muxes
611 */
612 chr->explicit_be_open = muxes_realized ? 0 : 1;
613 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200614
aliguori6f97dba2008-10-31 18:49:55 +0000615 return chr;
616}
617
618
619#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000620int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000621{
622 int ret, len;
623
624 len = len1;
625 while (len > 0) {
626 ret = send(fd, buf, len, 0);
627 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000628 errno = WSAGetLastError();
629 if (errno != WSAEWOULDBLOCK) {
630 return -1;
631 }
632 } else if (ret == 0) {
633 break;
634 } else {
635 buf += ret;
636 len -= ret;
637 }
638 }
639 return len1 - len;
640}
641
642#else
643
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100644int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000645{
646 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100647 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000648
649 len = len1;
650 while (len > 0) {
651 ret = write(fd, buf, len);
652 if (ret < 0) {
653 if (errno != EINTR && errno != EAGAIN)
654 return -1;
655 } else if (ret == 0) {
656 break;
657 } else {
658 buf += ret;
659 len -= ret;
660 }
661 }
662 return len1 - len;
663}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500664
665int recv_all(int fd, void *_buf, int len1, bool single_read)
666{
667 int ret, len;
668 uint8_t *buf = _buf;
669
670 len = len1;
671 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
672 if (ret < 0) {
673 if (errno != EINTR && errno != EAGAIN) {
674 return -1;
675 }
676 continue;
677 } else {
678 if (single_read) {
679 return ret;
680 }
681 buf += ret;
682 len -= ret;
683 }
684 }
685 return len1 - len;
686}
687
aliguori6f97dba2008-10-31 18:49:55 +0000688#endif /* !_WIN32 */
689
Anthony Liguori96c63842013-03-05 23:21:18 +0530690typedef struct IOWatchPoll
691{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200692 GSource parent;
693
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200694 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530695 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530696
697 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200698 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530699 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530700} IOWatchPoll;
701
Anthony Liguori96c63842013-03-05 23:21:18 +0530702static IOWatchPoll *io_watch_poll_from_source(GSource *source)
703{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200704 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530705}
706
707static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
708{
709 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200710 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200711 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200712 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530713 return FALSE;
714 }
715
Paolo Bonzinid185c092013-04-05 17:59:33 +0200716 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200717 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
718 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200719 g_source_attach(iwp->src, NULL);
720 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200721 g_source_destroy(iwp->src);
722 g_source_unref(iwp->src);
723 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200724 }
725 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530726}
727
728static gboolean io_watch_poll_check(GSource *source)
729{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200730 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530731}
732
733static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
734 gpointer user_data)
735{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200736 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530737}
738
739static void io_watch_poll_finalize(GSource *source)
740{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200741 /* Due to a glib bug, removing the last reference to a source
742 * inside a finalize callback causes recursive locking (and a
743 * deadlock). This is not a problem inside other callbacks,
744 * including dispatch callbacks, so we call io_remove_watch_poll
745 * to remove this source. At this point, iwp->src must
746 * be NULL, or we would leak it.
747 *
748 * This would be solved much more elegantly by child sources,
749 * but we support older glib versions that do not have them.
750 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530751 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200752 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530753}
754
755static GSourceFuncs io_watch_poll_funcs = {
756 .prepare = io_watch_poll_prepare,
757 .check = io_watch_poll_check,
758 .dispatch = io_watch_poll_dispatch,
759 .finalize = io_watch_poll_finalize,
760};
761
762/* Can only be used for read */
763static guint io_add_watch_poll(GIOChannel *channel,
764 IOCanReadHandler *fd_can_read,
765 GIOFunc fd_read,
766 gpointer user_data)
767{
768 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200769 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530770
Paolo Bonzinid185c092013-04-05 17:59:33 +0200771 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530772 iwp->fd_can_read = fd_can_read;
773 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200774 iwp->channel = channel;
775 iwp->fd_read = (GSourceFunc) fd_read;
776 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530777
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200778 tag = g_source_attach(&iwp->parent, NULL);
779 g_source_unref(&iwp->parent);
780 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530781}
782
Paolo Bonzini2b316772013-04-19 17:32:09 +0200783static void io_remove_watch_poll(guint tag)
784{
785 GSource *source;
786 IOWatchPoll *iwp;
787
788 g_return_if_fail (tag > 0);
789
790 source = g_main_context_find_source_by_id(NULL, tag);
791 g_return_if_fail (source != NULL);
792
793 iwp = io_watch_poll_from_source(source);
794 if (iwp->src) {
795 g_source_destroy(iwp->src);
796 g_source_unref(iwp->src);
797 iwp->src = NULL;
798 }
799 g_source_destroy(&iwp->parent);
800}
801
Amit Shah26da70c2013-08-28 15:23:37 +0530802static void remove_fd_in_watch(CharDriverState *chr)
803{
804 if (chr->fd_in_tag) {
805 io_remove_watch_poll(chr->fd_in_tag);
806 chr->fd_in_tag = 0;
807 }
808}
809
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000810#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530811static GIOChannel *io_channel_from_fd(int fd)
812{
813 GIOChannel *chan;
814
815 if (fd == -1) {
816 return NULL;
817 }
818
819 chan = g_io_channel_unix_new(fd);
820
821 g_io_channel_set_encoding(chan, NULL, NULL);
822 g_io_channel_set_buffered(chan, FALSE);
823
824 return chan;
825}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000826#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530827
Anthony Liguori76a96442013-03-05 23:21:21 +0530828static GIOChannel *io_channel_from_socket(int fd)
829{
830 GIOChannel *chan;
831
832 if (fd == -1) {
833 return NULL;
834 }
835
836#ifdef _WIN32
837 chan = g_io_channel_win32_new_socket(fd);
838#else
839 chan = g_io_channel_unix_new(fd);
840#endif
841
842 g_io_channel_set_encoding(chan, NULL, NULL);
843 g_io_channel_set_buffered(chan, FALSE);
844
845 return chan;
846}
847
Anthony Liguori684a0962013-03-29 11:39:50 -0500848static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530849{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200850 size_t offset = 0;
851 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530852
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200853 while (offset < len && status == G_IO_STATUS_NORMAL) {
854 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500855
856 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530857 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500858 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530859 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500860
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200861 if (offset > 0) {
862 return offset;
863 }
864 switch (status) {
865 case G_IO_STATUS_NORMAL:
866 g_assert(len == 0);
867 return 0;
868 case G_IO_STATUS_AGAIN:
869 errno = EAGAIN;
870 return -1;
871 default:
872 break;
873 }
874 errno = EINVAL;
875 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530876}
Anthony Liguori96c63842013-03-05 23:21:18 +0530877
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000878#ifndef _WIN32
879
Anthony Liguoria29753f2013-03-05 23:21:19 +0530880typedef struct FDCharDriver {
881 CharDriverState *chr;
882 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000883 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530884 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000885} FDCharDriver;
886
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200887/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000888static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
889{
890 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530891
Anthony Liguori684a0962013-03-29 11:39:50 -0500892 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530893}
894
895static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
896{
897 CharDriverState *chr = opaque;
898 FDCharDriver *s = chr->opaque;
899 int len;
900 uint8_t buf[READ_BUF_LEN];
901 GIOStatus status;
902 gsize bytes_read;
903
904 len = sizeof(buf);
905 if (len > s->max_size) {
906 len = s->max_size;
907 }
908 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200909 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530910 }
911
912 status = g_io_channel_read_chars(chan, (gchar *)buf,
913 len, &bytes_read, NULL);
914 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530915 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530916 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
917 return FALSE;
918 }
919 if (status == G_IO_STATUS_NORMAL) {
920 qemu_chr_be_write(chr, buf, bytes_read);
921 }
922
923 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000924}
925
926static int fd_chr_read_poll(void *opaque)
927{
928 CharDriverState *chr = opaque;
929 FDCharDriver *s = chr->opaque;
930
Anthony Liguori909cda12011-08-15 11:17:31 -0500931 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000932 return s->max_size;
933}
934
Anthony Liguori23673ca2013-03-05 23:21:23 +0530935static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
936{
937 FDCharDriver *s = chr->opaque;
938 return g_io_create_watch(s->fd_out, cond);
939}
940
aliguori6f97dba2008-10-31 18:49:55 +0000941static void fd_chr_update_read_handler(CharDriverState *chr)
942{
943 FDCharDriver *s = chr->opaque;
944
Amit Shah26da70c2013-08-28 15:23:37 +0530945 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530946 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530947 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
948 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000949 }
950}
951
952static void fd_chr_close(struct CharDriverState *chr)
953{
954 FDCharDriver *s = chr->opaque;
955
Amit Shah26da70c2013-08-28 15:23:37 +0530956 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530957 if (s->fd_in) {
958 g_io_channel_unref(s->fd_in);
959 }
960 if (s->fd_out) {
961 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000962 }
963
Anthony Liguori7267c092011-08-20 22:09:37 -0500964 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100965 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000966}
967
968/* open a character device to a unix fd */
969static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
970{
971 CharDriverState *chr;
972 FDCharDriver *s;
973
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200974 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500975 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530976 s->fd_in = io_channel_from_fd(fd_in);
977 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +0800978 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530979 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000980 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530981 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000982 chr->chr_write = fd_chr_write;
983 chr->chr_update_read_handler = fd_chr_update_read_handler;
984 chr->chr_close = fd_chr_close;
985
aliguori6f97dba2008-10-31 18:49:55 +0000986 return chr;
987}
988
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100989static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000990{
991 int fd_in, fd_out;
992 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100993 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200994
995 if (filename == NULL) {
996 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100997 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200998 }
aliguori6f97dba2008-10-31 18:49:55 +0000999
1000 snprintf(filename_in, 256, "%s.in", filename);
1001 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001002 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1003 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001004 if (fd_in < 0 || fd_out < 0) {
1005 if (fd_in >= 0)
1006 close(fd_in);
1007 if (fd_out >= 0)
1008 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001009 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001010 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001011 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001012 }
aliguori6f97dba2008-10-31 18:49:55 +00001013 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001014 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001015}
1016
aliguori6f97dba2008-10-31 18:49:55 +00001017/* init terminal so that we can grab keys */
1018static struct termios oldtty;
1019static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001020static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001021
1022static void term_exit(void)
1023{
1024 tcsetattr (0, TCSANOW, &oldtty);
1025 fcntl(0, F_SETFL, old_fd0_flags);
1026}
1027
Paolo Bonzinibb002512010-12-23 13:42:50 +01001028static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001029{
1030 struct termios tty;
1031
Paolo Bonzini03693642010-12-23 13:42:49 +01001032 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001033 if (!echo) {
1034 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001035 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001036 tty.c_oflag |= OPOST;
1037 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1038 tty.c_cflag &= ~(CSIZE|PARENB);
1039 tty.c_cflag |= CS8;
1040 tty.c_cc[VMIN] = 1;
1041 tty.c_cc[VTIME] = 0;
1042 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001043 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001044 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001045
1046 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001047}
1048
1049static void qemu_chr_close_stdio(struct CharDriverState *chr)
1050{
1051 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001052 fd_chr_close(chr);
1053}
1054
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001055static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001056{
1057 CharDriverState *chr;
1058
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001059 if (is_daemonized()) {
1060 error_report("cannot use stdio with -daemonize");
1061 return NULL;
1062 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301063 old_fd0_flags = fcntl(0, F_GETFL);
1064 tcgetattr (0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001065 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301066 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001067
aliguori6f97dba2008-10-31 18:49:55 +00001068 chr = qemu_chr_open_fd(0, 1);
1069 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001070 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001071 if (opts->has_signal) {
1072 stdio_allow_signal = opts->signal;
1073 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001074 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001075
Markus Armbruster1f514702012-02-07 15:09:08 +01001076 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001077}
1078
aliguori6f97dba2008-10-31 18:49:55 +00001079#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001080 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1081 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001082
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001083#define HAVE_CHARDEV_TTY 1
1084
aliguori6f97dba2008-10-31 18:49:55 +00001085typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301086 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001087 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001088
1089 /* Protected by the CharDriverState chr_write_lock. */
1090 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301091 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001092 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001093} PtyCharDriver;
1094
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001095static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001096static void pty_chr_state(CharDriverState *chr, int connected);
1097
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301098static gboolean pty_chr_timer(gpointer opaque)
1099{
1100 struct CharDriverState *chr = opaque;
1101 PtyCharDriver *s = chr->opaque;
1102
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001103 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001104 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001105 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001106 if (!s->connected) {
1107 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001108 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001109 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001110 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301111 return FALSE;
1112}
1113
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001114/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301115static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1116{
1117 PtyCharDriver *s = chr->opaque;
1118
1119 if (s->timer_tag) {
1120 g_source_remove(s->timer_tag);
1121 s->timer_tag = 0;
1122 }
1123
1124 if (ms == 1000) {
1125 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1126 } else {
1127 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1128 }
1129}
1130
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001131/* Called with chr_write_lock held. */
1132static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001133{
1134 PtyCharDriver *s = chr->opaque;
1135 GPollFD pfd;
1136
1137 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1138 pfd.events = G_IO_OUT;
1139 pfd.revents = 0;
1140 g_poll(&pfd, 1, 0);
1141 if (pfd.revents & G_IO_HUP) {
1142 pty_chr_state(chr, 0);
1143 } else {
1144 pty_chr_state(chr, 1);
1145 }
1146}
1147
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001148static void pty_chr_update_read_handler(CharDriverState *chr)
1149{
1150 qemu_mutex_lock(&chr->chr_write_lock);
1151 pty_chr_update_read_handler_locked(chr);
1152 qemu_mutex_unlock(&chr->chr_write_lock);
1153}
1154
1155/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001156static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1157{
1158 PtyCharDriver *s = chr->opaque;
1159
1160 if (!s->connected) {
1161 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001162 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001163 if (!s->connected) {
1164 return 0;
1165 }
aliguori6f97dba2008-10-31 18:49:55 +00001166 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001167 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001168}
1169
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301170static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1171{
1172 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001173 if (!s->connected) {
1174 return NULL;
1175 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301176 return g_io_create_watch(s->fd, cond);
1177}
1178
aliguori6f97dba2008-10-31 18:49:55 +00001179static int pty_chr_read_poll(void *opaque)
1180{
1181 CharDriverState *chr = opaque;
1182 PtyCharDriver *s = chr->opaque;
1183
Anthony Liguori909cda12011-08-15 11:17:31 -05001184 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001185 return s->read_bytes;
1186}
1187
Anthony Liguori093d3a22013-03-05 23:21:20 +05301188static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001189{
1190 CharDriverState *chr = opaque;
1191 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301192 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301193 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301194 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001195
1196 len = sizeof(buf);
1197 if (len > s->read_bytes)
1198 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001199 if (len == 0) {
1200 return TRUE;
1201 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301202 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1203 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001204 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301205 return FALSE;
1206 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001207 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001208 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001209 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301210 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001211}
1212
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001213static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1214{
1215 CharDriverState *chr = opaque;
1216 PtyCharDriver *s = chr->opaque;
1217
1218 s->open_tag = 0;
1219 qemu_chr_be_generic_open(chr);
1220 return FALSE;
1221}
1222
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001223/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001224static void pty_chr_state(CharDriverState *chr, int connected)
1225{
1226 PtyCharDriver *s = chr->opaque;
1227
1228 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001229 if (s->open_tag) {
1230 g_source_remove(s->open_tag);
1231 s->open_tag = 0;
1232 }
Amit Shah26da70c2013-08-28 15:23:37 +05301233 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001234 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001235 /* (re-)connect poll interval for idle guests: once per second.
1236 * We check more frequently in case the guests sends data to
1237 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301238 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001239 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001240 if (s->timer_tag) {
1241 g_source_remove(s->timer_tag);
1242 s->timer_tag = 0;
1243 }
1244 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001245 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001246 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001247 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001248 }
1249 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301250 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1251 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001252 }
aliguori6f97dba2008-10-31 18:49:55 +00001253 }
1254}
1255
aliguori6f97dba2008-10-31 18:49:55 +00001256static void pty_chr_close(struct CharDriverState *chr)
1257{
1258 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301259 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001260
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001261 qemu_mutex_lock(&chr->chr_write_lock);
1262 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301263 fd = g_io_channel_unix_get_fd(s->fd);
1264 g_io_channel_unref(s->fd);
1265 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301266 if (s->timer_tag) {
1267 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001268 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301269 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001270 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001271 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001272 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001273}
1274
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001275static CharDriverState *qemu_chr_open_pty(const char *id,
1276 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001277{
1278 CharDriverState *chr;
1279 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001280 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001281 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001282
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001283 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1284 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001285 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001286 }
1287
aliguori6f97dba2008-10-31 18:49:55 +00001288 close(slave_fd);
1289
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001290 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001291
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001292 chr->filename = g_strdup_printf("pty:%s", pty_name);
1293 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001294 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001295
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001296 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001297 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001298
1299 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001300 chr->opaque = s;
1301 chr->chr_write = pty_chr_write;
1302 chr->chr_update_read_handler = pty_chr_update_read_handler;
1303 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301304 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001305 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001306
Anthony Liguori093d3a22013-03-05 23:21:20 +05301307 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301308 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001309
Markus Armbruster1f514702012-02-07 15:09:08 +01001310 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001311}
1312
1313static void tty_serial_init(int fd, int speed,
1314 int parity, int data_bits, int stop_bits)
1315{
1316 struct termios tty;
1317 speed_t spd;
1318
1319#if 0
1320 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1321 speed, parity, data_bits, stop_bits);
1322#endif
1323 tcgetattr (fd, &tty);
1324
Stefan Weil45eea132009-10-26 16:10:10 +01001325#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1326 speed = speed * 10 / 11;
1327 do {
1328 check_speed(50);
1329 check_speed(75);
1330 check_speed(110);
1331 check_speed(134);
1332 check_speed(150);
1333 check_speed(200);
1334 check_speed(300);
1335 check_speed(600);
1336 check_speed(1200);
1337 check_speed(1800);
1338 check_speed(2400);
1339 check_speed(4800);
1340 check_speed(9600);
1341 check_speed(19200);
1342 check_speed(38400);
1343 /* Non-Posix values follow. They may be unsupported on some systems. */
1344 check_speed(57600);
1345 check_speed(115200);
1346#ifdef B230400
1347 check_speed(230400);
1348#endif
1349#ifdef B460800
1350 check_speed(460800);
1351#endif
1352#ifdef B500000
1353 check_speed(500000);
1354#endif
1355#ifdef B576000
1356 check_speed(576000);
1357#endif
1358#ifdef B921600
1359 check_speed(921600);
1360#endif
1361#ifdef B1000000
1362 check_speed(1000000);
1363#endif
1364#ifdef B1152000
1365 check_speed(1152000);
1366#endif
1367#ifdef B1500000
1368 check_speed(1500000);
1369#endif
1370#ifdef B2000000
1371 check_speed(2000000);
1372#endif
1373#ifdef B2500000
1374 check_speed(2500000);
1375#endif
1376#ifdef B3000000
1377 check_speed(3000000);
1378#endif
1379#ifdef B3500000
1380 check_speed(3500000);
1381#endif
1382#ifdef B4000000
1383 check_speed(4000000);
1384#endif
aliguori6f97dba2008-10-31 18:49:55 +00001385 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001386 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001387
1388 cfsetispeed(&tty, spd);
1389 cfsetospeed(&tty, spd);
1390
1391 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1392 |INLCR|IGNCR|ICRNL|IXON);
1393 tty.c_oflag |= OPOST;
1394 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1395 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1396 switch(data_bits) {
1397 default:
1398 case 8:
1399 tty.c_cflag |= CS8;
1400 break;
1401 case 7:
1402 tty.c_cflag |= CS7;
1403 break;
1404 case 6:
1405 tty.c_cflag |= CS6;
1406 break;
1407 case 5:
1408 tty.c_cflag |= CS5;
1409 break;
1410 }
1411 switch(parity) {
1412 default:
1413 case 'N':
1414 break;
1415 case 'E':
1416 tty.c_cflag |= PARENB;
1417 break;
1418 case 'O':
1419 tty.c_cflag |= PARENB | PARODD;
1420 break;
1421 }
1422 if (stop_bits == 2)
1423 tty.c_cflag |= CSTOPB;
1424
1425 tcsetattr (fd, TCSANOW, &tty);
1426}
1427
1428static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1429{
1430 FDCharDriver *s = chr->opaque;
1431
1432 switch(cmd) {
1433 case CHR_IOCTL_SERIAL_SET_PARAMS:
1434 {
1435 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301436 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1437 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001438 ssp->data_bits, ssp->stop_bits);
1439 }
1440 break;
1441 case CHR_IOCTL_SERIAL_SET_BREAK:
1442 {
1443 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301444 if (enable) {
1445 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1446 }
aliguori6f97dba2008-10-31 18:49:55 +00001447 }
1448 break;
1449 case CHR_IOCTL_SERIAL_GET_TIOCM:
1450 {
1451 int sarg = 0;
1452 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301453 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001454 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001455 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001456 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001457 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001458 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001459 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001460 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001461 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001462 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001463 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001464 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001465 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001466 *targ |= CHR_TIOCM_RTS;
1467 }
1468 break;
1469 case CHR_IOCTL_SERIAL_SET_TIOCM:
1470 {
1471 int sarg = *(int *)arg;
1472 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301473 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001474 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1475 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1476 if (sarg & CHR_TIOCM_CTS)
1477 targ |= TIOCM_CTS;
1478 if (sarg & CHR_TIOCM_CAR)
1479 targ |= TIOCM_CAR;
1480 if (sarg & CHR_TIOCM_DSR)
1481 targ |= TIOCM_DSR;
1482 if (sarg & CHR_TIOCM_RI)
1483 targ |= TIOCM_RI;
1484 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001485 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001486 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001487 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301488 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001489 }
1490 break;
1491 default:
1492 return -ENOTSUP;
1493 }
1494 return 0;
1495}
1496
David Ahern4266a132010-02-10 18:27:17 -07001497static void qemu_chr_close_tty(CharDriverState *chr)
1498{
1499 FDCharDriver *s = chr->opaque;
1500 int fd = -1;
1501
1502 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301503 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001504 }
1505
1506 fd_chr_close(chr);
1507
1508 if (fd >= 0) {
1509 close(fd);
1510 }
1511}
1512
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001513static CharDriverState *qemu_chr_open_tty_fd(int fd)
1514{
1515 CharDriverState *chr;
1516
1517 tty_serial_init(fd, 115200, 'N', 8, 1);
1518 chr = qemu_chr_open_fd(fd, fd);
1519 chr->chr_ioctl = tty_serial_ioctl;
1520 chr->chr_close = qemu_chr_close_tty;
1521 return chr;
1522}
aliguori6f97dba2008-10-31 18:49:55 +00001523#endif /* __linux__ || __sun__ */
1524
1525#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001526
1527#define HAVE_CHARDEV_PARPORT 1
1528
aliguori6f97dba2008-10-31 18:49:55 +00001529typedef struct {
1530 int fd;
1531 int mode;
1532} ParallelCharDriver;
1533
1534static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1535{
1536 if (s->mode != mode) {
1537 int m = mode;
1538 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1539 return 0;
1540 s->mode = mode;
1541 }
1542 return 1;
1543}
1544
1545static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1546{
1547 ParallelCharDriver *drv = chr->opaque;
1548 int fd = drv->fd;
1549 uint8_t b;
1550
1551 switch(cmd) {
1552 case CHR_IOCTL_PP_READ_DATA:
1553 if (ioctl(fd, PPRDATA, &b) < 0)
1554 return -ENOTSUP;
1555 *(uint8_t *)arg = b;
1556 break;
1557 case CHR_IOCTL_PP_WRITE_DATA:
1558 b = *(uint8_t *)arg;
1559 if (ioctl(fd, PPWDATA, &b) < 0)
1560 return -ENOTSUP;
1561 break;
1562 case CHR_IOCTL_PP_READ_CONTROL:
1563 if (ioctl(fd, PPRCONTROL, &b) < 0)
1564 return -ENOTSUP;
1565 /* Linux gives only the lowest bits, and no way to know data
1566 direction! For better compatibility set the fixed upper
1567 bits. */
1568 *(uint8_t *)arg = b | 0xc0;
1569 break;
1570 case CHR_IOCTL_PP_WRITE_CONTROL:
1571 b = *(uint8_t *)arg;
1572 if (ioctl(fd, PPWCONTROL, &b) < 0)
1573 return -ENOTSUP;
1574 break;
1575 case CHR_IOCTL_PP_READ_STATUS:
1576 if (ioctl(fd, PPRSTATUS, &b) < 0)
1577 return -ENOTSUP;
1578 *(uint8_t *)arg = b;
1579 break;
1580 case CHR_IOCTL_PP_DATA_DIR:
1581 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1582 return -ENOTSUP;
1583 break;
1584 case CHR_IOCTL_PP_EPP_READ_ADDR:
1585 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1586 struct ParallelIOArg *parg = arg;
1587 int n = read(fd, parg->buffer, parg->count);
1588 if (n != parg->count) {
1589 return -EIO;
1590 }
1591 }
1592 break;
1593 case CHR_IOCTL_PP_EPP_READ:
1594 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1595 struct ParallelIOArg *parg = arg;
1596 int n = read(fd, parg->buffer, parg->count);
1597 if (n != parg->count) {
1598 return -EIO;
1599 }
1600 }
1601 break;
1602 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1603 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1604 struct ParallelIOArg *parg = arg;
1605 int n = write(fd, parg->buffer, parg->count);
1606 if (n != parg->count) {
1607 return -EIO;
1608 }
1609 }
1610 break;
1611 case CHR_IOCTL_PP_EPP_WRITE:
1612 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1613 struct ParallelIOArg *parg = arg;
1614 int n = write(fd, parg->buffer, parg->count);
1615 if (n != parg->count) {
1616 return -EIO;
1617 }
1618 }
1619 break;
1620 default:
1621 return -ENOTSUP;
1622 }
1623 return 0;
1624}
1625
1626static void pp_close(CharDriverState *chr)
1627{
1628 ParallelCharDriver *drv = chr->opaque;
1629 int fd = drv->fd;
1630
1631 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1632 ioctl(fd, PPRELEASE);
1633 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001634 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001635 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001636}
1637
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001638static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001639{
1640 CharDriverState *chr;
1641 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001642
1643 if (ioctl(fd, PPCLAIM) < 0) {
1644 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001645 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001646 }
1647
Anthony Liguori7267c092011-08-20 22:09:37 -05001648 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001649 drv->fd = fd;
1650 drv->mode = IEEE1284_MODE_COMPAT;
1651
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001652 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001653 chr->chr_write = null_chr_write;
1654 chr->chr_ioctl = pp_ioctl;
1655 chr->chr_close = pp_close;
1656 chr->opaque = drv;
1657
Markus Armbruster1f514702012-02-07 15:09:08 +01001658 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001659}
1660#endif /* __linux__ */
1661
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001662#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001663
1664#define HAVE_CHARDEV_PARPORT 1
1665
blueswir16972f932008-11-22 20:49:12 +00001666static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1667{
Stefan Weile0efb992011-02-23 19:09:16 +01001668 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001669 uint8_t b;
1670
1671 switch(cmd) {
1672 case CHR_IOCTL_PP_READ_DATA:
1673 if (ioctl(fd, PPIGDATA, &b) < 0)
1674 return -ENOTSUP;
1675 *(uint8_t *)arg = b;
1676 break;
1677 case CHR_IOCTL_PP_WRITE_DATA:
1678 b = *(uint8_t *)arg;
1679 if (ioctl(fd, PPISDATA, &b) < 0)
1680 return -ENOTSUP;
1681 break;
1682 case CHR_IOCTL_PP_READ_CONTROL:
1683 if (ioctl(fd, PPIGCTRL, &b) < 0)
1684 return -ENOTSUP;
1685 *(uint8_t *)arg = b;
1686 break;
1687 case CHR_IOCTL_PP_WRITE_CONTROL:
1688 b = *(uint8_t *)arg;
1689 if (ioctl(fd, PPISCTRL, &b) < 0)
1690 return -ENOTSUP;
1691 break;
1692 case CHR_IOCTL_PP_READ_STATUS:
1693 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1694 return -ENOTSUP;
1695 *(uint8_t *)arg = b;
1696 break;
1697 default:
1698 return -ENOTSUP;
1699 }
1700 return 0;
1701}
1702
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001703static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001704{
1705 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001706
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001707 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001708 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001709 chr->chr_write = null_chr_write;
1710 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001711 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001712 return chr;
blueswir16972f932008-11-22 20:49:12 +00001713}
1714#endif
1715
aliguori6f97dba2008-10-31 18:49:55 +00001716#else /* _WIN32 */
1717
1718typedef struct {
1719 int max_size;
1720 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001721 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001722 BOOL fpipe;
1723 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001724
1725 /* Protected by the CharDriverState chr_write_lock. */
1726 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001727} WinCharState;
1728
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001729typedef struct {
1730 HANDLE hStdIn;
1731 HANDLE hInputReadyEvent;
1732 HANDLE hInputDoneEvent;
1733 HANDLE hInputThread;
1734 uint8_t win_stdio_buf;
1735} WinStdioCharState;
1736
aliguori6f97dba2008-10-31 18:49:55 +00001737#define NSENDBUF 2048
1738#define NRECVBUF 2048
1739#define MAXCONNECT 1
1740#define NTIMEOUT 5000
1741
1742static int win_chr_poll(void *opaque);
1743static int win_chr_pipe_poll(void *opaque);
1744
1745static void win_chr_close(CharDriverState *chr)
1746{
1747 WinCharState *s = chr->opaque;
1748
1749 if (s->hsend) {
1750 CloseHandle(s->hsend);
1751 s->hsend = NULL;
1752 }
1753 if (s->hrecv) {
1754 CloseHandle(s->hrecv);
1755 s->hrecv = NULL;
1756 }
1757 if (s->hcom) {
1758 CloseHandle(s->hcom);
1759 s->hcom = NULL;
1760 }
1761 if (s->fpipe)
1762 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1763 else
1764 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301765
Hans de Goedea425d232011-11-19 10:22:43 +01001766 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001767}
1768
1769static int win_chr_init(CharDriverState *chr, const char *filename)
1770{
1771 WinCharState *s = chr->opaque;
1772 COMMCONFIG comcfg;
1773 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1774 COMSTAT comstat;
1775 DWORD size;
1776 DWORD err;
1777
1778 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1779 if (!s->hsend) {
1780 fprintf(stderr, "Failed CreateEvent\n");
1781 goto fail;
1782 }
1783 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1784 if (!s->hrecv) {
1785 fprintf(stderr, "Failed CreateEvent\n");
1786 goto fail;
1787 }
1788
1789 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1790 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1791 if (s->hcom == INVALID_HANDLE_VALUE) {
1792 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1793 s->hcom = NULL;
1794 goto fail;
1795 }
1796
1797 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1798 fprintf(stderr, "Failed SetupComm\n");
1799 goto fail;
1800 }
1801
1802 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1803 size = sizeof(COMMCONFIG);
1804 GetDefaultCommConfig(filename, &comcfg, &size);
1805 comcfg.dcb.DCBlength = sizeof(DCB);
1806 CommConfigDialog(filename, NULL, &comcfg);
1807
1808 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1809 fprintf(stderr, "Failed SetCommState\n");
1810 goto fail;
1811 }
1812
1813 if (!SetCommMask(s->hcom, EV_ERR)) {
1814 fprintf(stderr, "Failed SetCommMask\n");
1815 goto fail;
1816 }
1817
1818 cto.ReadIntervalTimeout = MAXDWORD;
1819 if (!SetCommTimeouts(s->hcom, &cto)) {
1820 fprintf(stderr, "Failed SetCommTimeouts\n");
1821 goto fail;
1822 }
1823
1824 if (!ClearCommError(s->hcom, &err, &comstat)) {
1825 fprintf(stderr, "Failed ClearCommError\n");
1826 goto fail;
1827 }
1828 qemu_add_polling_cb(win_chr_poll, chr);
1829 return 0;
1830
1831 fail:
1832 win_chr_close(chr);
1833 return -1;
1834}
1835
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001836/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001837static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1838{
1839 WinCharState *s = chr->opaque;
1840 DWORD len, ret, size, err;
1841
1842 len = len1;
1843 ZeroMemory(&s->osend, sizeof(s->osend));
1844 s->osend.hEvent = s->hsend;
1845 while (len > 0) {
1846 if (s->hsend)
1847 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1848 else
1849 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1850 if (!ret) {
1851 err = GetLastError();
1852 if (err == ERROR_IO_PENDING) {
1853 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1854 if (ret) {
1855 buf += size;
1856 len -= size;
1857 } else {
1858 break;
1859 }
1860 } else {
1861 break;
1862 }
1863 } else {
1864 buf += size;
1865 len -= size;
1866 }
1867 }
1868 return len1 - len;
1869}
1870
1871static int win_chr_read_poll(CharDriverState *chr)
1872{
1873 WinCharState *s = chr->opaque;
1874
Anthony Liguori909cda12011-08-15 11:17:31 -05001875 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001876 return s->max_size;
1877}
1878
1879static void win_chr_readfile(CharDriverState *chr)
1880{
1881 WinCharState *s = chr->opaque;
1882 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301883 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001884 DWORD size;
1885
1886 ZeroMemory(&s->orecv, sizeof(s->orecv));
1887 s->orecv.hEvent = s->hrecv;
1888 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1889 if (!ret) {
1890 err = GetLastError();
1891 if (err == ERROR_IO_PENDING) {
1892 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1893 }
1894 }
1895
1896 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001897 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001898 }
1899}
1900
1901static void win_chr_read(CharDriverState *chr)
1902{
1903 WinCharState *s = chr->opaque;
1904
1905 if (s->len > s->max_size)
1906 s->len = s->max_size;
1907 if (s->len == 0)
1908 return;
1909
1910 win_chr_readfile(chr);
1911}
1912
1913static int win_chr_poll(void *opaque)
1914{
1915 CharDriverState *chr = opaque;
1916 WinCharState *s = chr->opaque;
1917 COMSTAT status;
1918 DWORD comerr;
1919
1920 ClearCommError(s->hcom, &comerr, &status);
1921 if (status.cbInQue > 0) {
1922 s->len = status.cbInQue;
1923 win_chr_read_poll(chr);
1924 win_chr_read(chr);
1925 return 1;
1926 }
1927 return 0;
1928}
1929
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001930static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001931{
1932 CharDriverState *chr;
1933 WinCharState *s;
1934
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001935 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001936 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001937 chr->opaque = s;
1938 chr->chr_write = win_chr_write;
1939 chr->chr_close = win_chr_close;
1940
1941 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001942 g_free(s);
1943 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001944 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001945 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001946 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001947}
1948
1949static int win_chr_pipe_poll(void *opaque)
1950{
1951 CharDriverState *chr = opaque;
1952 WinCharState *s = chr->opaque;
1953 DWORD size;
1954
1955 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1956 if (size > 0) {
1957 s->len = size;
1958 win_chr_read_poll(chr);
1959 win_chr_read(chr);
1960 return 1;
1961 }
1962 return 0;
1963}
1964
1965static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1966{
1967 WinCharState *s = chr->opaque;
1968 OVERLAPPED ov;
1969 int ret;
1970 DWORD size;
1971 char openname[256];
1972
1973 s->fpipe = TRUE;
1974
1975 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1976 if (!s->hsend) {
1977 fprintf(stderr, "Failed CreateEvent\n");
1978 goto fail;
1979 }
1980 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1981 if (!s->hrecv) {
1982 fprintf(stderr, "Failed CreateEvent\n");
1983 goto fail;
1984 }
1985
1986 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1987 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1988 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1989 PIPE_WAIT,
1990 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1991 if (s->hcom == INVALID_HANDLE_VALUE) {
1992 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1993 s->hcom = NULL;
1994 goto fail;
1995 }
1996
1997 ZeroMemory(&ov, sizeof(ov));
1998 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1999 ret = ConnectNamedPipe(s->hcom, &ov);
2000 if (ret) {
2001 fprintf(stderr, "Failed ConnectNamedPipe\n");
2002 goto fail;
2003 }
2004
2005 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2006 if (!ret) {
2007 fprintf(stderr, "Failed GetOverlappedResult\n");
2008 if (ov.hEvent) {
2009 CloseHandle(ov.hEvent);
2010 ov.hEvent = NULL;
2011 }
2012 goto fail;
2013 }
2014
2015 if (ov.hEvent) {
2016 CloseHandle(ov.hEvent);
2017 ov.hEvent = NULL;
2018 }
2019 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2020 return 0;
2021
2022 fail:
2023 win_chr_close(chr);
2024 return -1;
2025}
2026
2027
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002028static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002029{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002030 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002031 CharDriverState *chr;
2032 WinCharState *s;
2033
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002034 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002035 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002036 chr->opaque = s;
2037 chr->chr_write = win_chr_write;
2038 chr->chr_close = win_chr_close;
2039
2040 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002041 g_free(s);
2042 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002043 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002044 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002045 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002046}
2047
Markus Armbruster1f514702012-02-07 15:09:08 +01002048static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002049{
2050 CharDriverState *chr;
2051 WinCharState *s;
2052
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002053 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002054 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002055 s->hcom = fd_out;
2056 chr->opaque = s;
2057 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002058 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002059}
2060
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002061static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002062{
Markus Armbruster1f514702012-02-07 15:09:08 +01002063 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002064}
2065
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002066static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2067{
2068 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2069 DWORD dwSize;
2070 int len1;
2071
2072 len1 = len;
2073
2074 while (len1 > 0) {
2075 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2076 break;
2077 }
2078 buf += dwSize;
2079 len1 -= dwSize;
2080 }
2081
2082 return len - len1;
2083}
2084
2085static void win_stdio_wait_func(void *opaque)
2086{
2087 CharDriverState *chr = opaque;
2088 WinStdioCharState *stdio = chr->opaque;
2089 INPUT_RECORD buf[4];
2090 int ret;
2091 DWORD dwSize;
2092 int i;
2093
Stefan Weildff74242013-12-07 14:48:04 +01002094 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002095
2096 if (!ret) {
2097 /* Avoid error storm */
2098 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2099 return;
2100 }
2101
2102 for (i = 0; i < dwSize; i++) {
2103 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2104
2105 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2106 int j;
2107 if (kev->uChar.AsciiChar != 0) {
2108 for (j = 0; j < kev->wRepeatCount; j++) {
2109 if (qemu_chr_be_can_write(chr)) {
2110 uint8_t c = kev->uChar.AsciiChar;
2111 qemu_chr_be_write(chr, &c, 1);
2112 }
2113 }
2114 }
2115 }
2116 }
2117}
2118
2119static DWORD WINAPI win_stdio_thread(LPVOID param)
2120{
2121 CharDriverState *chr = param;
2122 WinStdioCharState *stdio = chr->opaque;
2123 int ret;
2124 DWORD dwSize;
2125
2126 while (1) {
2127
2128 /* Wait for one byte */
2129 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2130
2131 /* Exit in case of error, continue if nothing read */
2132 if (!ret) {
2133 break;
2134 }
2135 if (!dwSize) {
2136 continue;
2137 }
2138
2139 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2140 if (stdio->win_stdio_buf == '\r') {
2141 continue;
2142 }
2143
2144 /* Signal the main thread and wait until the byte was eaten */
2145 if (!SetEvent(stdio->hInputReadyEvent)) {
2146 break;
2147 }
2148 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2149 != WAIT_OBJECT_0) {
2150 break;
2151 }
2152 }
2153
2154 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2155 return 0;
2156}
2157
2158static void win_stdio_thread_wait_func(void *opaque)
2159{
2160 CharDriverState *chr = opaque;
2161 WinStdioCharState *stdio = chr->opaque;
2162
2163 if (qemu_chr_be_can_write(chr)) {
2164 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2165 }
2166
2167 SetEvent(stdio->hInputDoneEvent);
2168}
2169
2170static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2171{
2172 WinStdioCharState *stdio = chr->opaque;
2173 DWORD dwMode = 0;
2174
2175 GetConsoleMode(stdio->hStdIn, &dwMode);
2176
2177 if (echo) {
2178 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2179 } else {
2180 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2181 }
2182}
2183
2184static void win_stdio_close(CharDriverState *chr)
2185{
2186 WinStdioCharState *stdio = chr->opaque;
2187
2188 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2189 CloseHandle(stdio->hInputReadyEvent);
2190 }
2191 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2192 CloseHandle(stdio->hInputDoneEvent);
2193 }
2194 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2195 TerminateThread(stdio->hInputThread, 0);
2196 }
2197
2198 g_free(chr->opaque);
2199 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002200}
2201
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002202static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002203{
2204 CharDriverState *chr;
2205 WinStdioCharState *stdio;
2206 DWORD dwMode;
2207 int is_console = 0;
2208
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002209 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002210 stdio = g_malloc0(sizeof(WinStdioCharState));
2211
2212 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2213 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2214 fprintf(stderr, "cannot open stdio: invalid handle\n");
2215 exit(1);
2216 }
2217
2218 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2219
2220 chr->opaque = stdio;
2221 chr->chr_write = win_stdio_write;
2222 chr->chr_close = win_stdio_close;
2223
Anthony Liguoried7a1542013-03-05 23:21:17 +05302224 if (is_console) {
2225 if (qemu_add_wait_object(stdio->hStdIn,
2226 win_stdio_wait_func, chr)) {
2227 fprintf(stderr, "qemu_add_wait_object: failed\n");
2228 }
2229 } else {
2230 DWORD dwId;
2231
2232 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2233 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2234 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2235 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002236
Anthony Liguoried7a1542013-03-05 23:21:17 +05302237 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2238 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2239 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2240 fprintf(stderr, "cannot create stdio thread or event\n");
2241 exit(1);
2242 }
2243 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2244 win_stdio_thread_wait_func, chr)) {
2245 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002246 }
2247 }
2248
2249 dwMode |= ENABLE_LINE_INPUT;
2250
Anthony Liguoried7a1542013-03-05 23:21:17 +05302251 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002252 /* set the terminal in raw mode */
2253 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2254 dwMode |= ENABLE_PROCESSED_INPUT;
2255 }
2256
2257 SetConsoleMode(stdio->hStdIn, dwMode);
2258
2259 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2260 qemu_chr_fe_set_echo(chr, false);
2261
Markus Armbruster1f514702012-02-07 15:09:08 +01002262 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002263}
aliguori6f97dba2008-10-31 18:49:55 +00002264#endif /* !_WIN32 */
2265
Anthony Liguori5ab82112013-03-05 23:21:31 +05302266
aliguori6f97dba2008-10-31 18:49:55 +00002267/***********************************************************/
2268/* UDP Net console */
2269
2270typedef struct {
2271 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302272 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302273 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002274 int bufcnt;
2275 int bufptr;
2276 int max_size;
2277} NetCharDriver;
2278
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002279/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002280static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2281{
2282 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302283 gsize bytes_written;
2284 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002285
Anthony Liguori76a96442013-03-05 23:21:21 +05302286 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2287 if (status == G_IO_STATUS_EOF) {
2288 return 0;
2289 } else if (status != G_IO_STATUS_NORMAL) {
2290 return -1;
2291 }
2292
2293 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002294}
2295
2296static int udp_chr_read_poll(void *opaque)
2297{
2298 CharDriverState *chr = opaque;
2299 NetCharDriver *s = chr->opaque;
2300
Anthony Liguori909cda12011-08-15 11:17:31 -05002301 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002302
2303 /* If there were any stray characters in the queue process them
2304 * first
2305 */
2306 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002307 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002308 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002309 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002310 }
2311 return s->max_size;
2312}
2313
Anthony Liguori76a96442013-03-05 23:21:21 +05302314static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002315{
2316 CharDriverState *chr = opaque;
2317 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302318 gsize bytes_read = 0;
2319 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002320
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002321 if (s->max_size == 0) {
2322 return TRUE;
2323 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302324 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2325 &bytes_read, NULL);
2326 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002327 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302328 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302329 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302330 return FALSE;
2331 }
aliguori6f97dba2008-10-31 18:49:55 +00002332
2333 s->bufptr = 0;
2334 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002335 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002336 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002337 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002338 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302339
2340 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002341}
2342
2343static void udp_chr_update_read_handler(CharDriverState *chr)
2344{
2345 NetCharDriver *s = chr->opaque;
2346
Amit Shah26da70c2013-08-28 15:23:37 +05302347 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302348 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302349 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2350 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002351 }
2352}
2353
aliguori819f56b2009-03-28 17:58:14 +00002354static void udp_chr_close(CharDriverState *chr)
2355{
2356 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302357
2358 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302359 if (s->chan) {
2360 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002361 closesocket(s->fd);
2362 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002363 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002364 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002365}
2366
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002367static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002368{
2369 CharDriverState *chr = NULL;
2370 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002371
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002372 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002373 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002374
aliguori6f97dba2008-10-31 18:49:55 +00002375 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302376 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002377 s->bufcnt = 0;
2378 s->bufptr = 0;
2379 chr->opaque = s;
2380 chr->chr_write = udp_chr_write;
2381 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002382 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002383 /* be isn't opened until we get a connection */
2384 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002385 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002386}
aliguori6f97dba2008-10-31 18:49:55 +00002387
aliguori6f97dba2008-10-31 18:49:55 +00002388/***********************************************************/
2389/* TCP Net console */
2390
2391typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302392
2393 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302394 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002395 int fd, listen_fd;
2396 int connected;
2397 int max_size;
2398 int do_telnetopt;
2399 int do_nodelay;
2400 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002401 int *read_msgfds;
2402 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002403 int *write_msgfds;
2404 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002405} TCPCharDriver;
2406
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302407static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002408
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002409#ifndef _WIN32
2410static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2411{
2412 TCPCharDriver *s = chr->opaque;
2413 struct msghdr msgh;
2414 struct iovec iov;
2415 int r;
2416
2417 size_t fd_size = s->write_msgfds_num * sizeof(int);
2418 char control[CMSG_SPACE(fd_size)];
2419 struct cmsghdr *cmsg;
2420
2421 memset(&msgh, 0, sizeof(msgh));
2422 memset(control, 0, sizeof(control));
2423
2424 /* set the payload */
2425 iov.iov_base = (uint8_t *) buf;
2426 iov.iov_len = len;
2427
2428 msgh.msg_iov = &iov;
2429 msgh.msg_iovlen = 1;
2430
2431 msgh.msg_control = control;
2432 msgh.msg_controllen = sizeof(control);
2433
2434 cmsg = CMSG_FIRSTHDR(&msgh);
2435
2436 cmsg->cmsg_len = CMSG_LEN(fd_size);
2437 cmsg->cmsg_level = SOL_SOCKET;
2438 cmsg->cmsg_type = SCM_RIGHTS;
2439 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2440
2441 do {
2442 r = sendmsg(s->fd, &msgh, 0);
2443 } while (r < 0 && errno == EINTR);
2444
2445 /* free the written msgfds, no matter what */
2446 if (s->write_msgfds_num) {
2447 g_free(s->write_msgfds);
2448 s->write_msgfds = 0;
2449 s->write_msgfds_num = 0;
2450 }
2451
2452 return r;
2453}
2454#endif
2455
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002456/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002457static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2458{
2459 TCPCharDriver *s = chr->opaque;
2460 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002461#ifndef _WIN32
2462 if (s->is_unix && s->write_msgfds_num) {
2463 return unix_send_msgfds(chr, buf, len);
2464 } else
2465#endif
2466 {
2467 return io_channel_send(s->chan, buf, len);
2468 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002469 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002470 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002471 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002472 }
2473}
2474
2475static int tcp_chr_read_poll(void *opaque)
2476{
2477 CharDriverState *chr = opaque;
2478 TCPCharDriver *s = chr->opaque;
2479 if (!s->connected)
2480 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002481 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002482 return s->max_size;
2483}
2484
2485#define IAC 255
2486#define IAC_BREAK 243
2487static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2488 TCPCharDriver *s,
2489 uint8_t *buf, int *size)
2490{
2491 /* Handle any telnet client's basic IAC options to satisfy char by
2492 * char mode with no echo. All IAC options will be removed from
2493 * the buf and the do_telnetopt variable will be used to track the
2494 * state of the width of the IAC information.
2495 *
2496 * IAC commands come in sets of 3 bytes with the exception of the
2497 * "IAC BREAK" command and the double IAC.
2498 */
2499
2500 int i;
2501 int j = 0;
2502
2503 for (i = 0; i < *size; i++) {
2504 if (s->do_telnetopt > 1) {
2505 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2506 /* Double IAC means send an IAC */
2507 if (j != i)
2508 buf[j] = buf[i];
2509 j++;
2510 s->do_telnetopt = 1;
2511 } else {
2512 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2513 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002514 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002515 s->do_telnetopt++;
2516 }
2517 s->do_telnetopt++;
2518 }
2519 if (s->do_telnetopt >= 4) {
2520 s->do_telnetopt = 1;
2521 }
2522 } else {
2523 if ((unsigned char)buf[i] == IAC) {
2524 s->do_telnetopt = 2;
2525 } else {
2526 if (j != i)
2527 buf[j] = buf[i];
2528 j++;
2529 }
2530 }
2531 }
2532 *size = j;
2533}
2534
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002535static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002536{
2537 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002538 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2539
2540 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002541 int i;
2542
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002543 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2544
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002545 /* Close unused fds */
2546 for (i = to_copy; i < s->read_msgfds_num; i++) {
2547 close(s->read_msgfds[i]);
2548 }
2549
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002550 g_free(s->read_msgfds);
2551 s->read_msgfds = 0;
2552 s->read_msgfds_num = 0;
2553 }
2554
2555 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002556}
2557
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002558static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2559{
2560 TCPCharDriver *s = chr->opaque;
2561
2562 /* clear old pending fd array */
2563 if (s->write_msgfds) {
2564 g_free(s->write_msgfds);
2565 }
2566
2567 if (num) {
2568 s->write_msgfds = g_malloc(num * sizeof(int));
2569 memcpy(s->write_msgfds, fds, num * sizeof(int));
2570 }
2571
2572 s->write_msgfds_num = num;
2573
2574 return 0;
2575}
2576
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002577#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002578static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2579{
2580 TCPCharDriver *s = chr->opaque;
2581 struct cmsghdr *cmsg;
2582
2583 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002584 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002585
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002586 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002587 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002588 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002589 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002590 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002591
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002592 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2593
2594 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002595 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002596 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002597
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002598 /* close and clean read_msgfds */
2599 for (i = 0; i < s->read_msgfds_num; i++) {
2600 close(s->read_msgfds[i]);
2601 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002602
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002603 if (s->read_msgfds_num) {
2604 g_free(s->read_msgfds);
2605 }
2606
2607 s->read_msgfds_num = fd_size / sizeof(int);
2608 s->read_msgfds = g_malloc(fd_size);
2609 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2610
2611 for (i = 0; i < s->read_msgfds_num; i++) {
2612 int fd = s->read_msgfds[i];
2613 if (fd < 0) {
2614 continue;
2615 }
2616
2617 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2618 qemu_set_block(fd);
2619
2620 #ifndef MSG_CMSG_CLOEXEC
2621 qemu_set_cloexec(fd);
2622 #endif
2623 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002624 }
2625}
2626
Mark McLoughlin9977c892009-07-22 09:11:38 +01002627static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2628{
2629 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002630 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002631 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002632 union {
2633 struct cmsghdr cmsg;
2634 char control[CMSG_SPACE(sizeof(int))];
2635 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002636 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002637 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002638
2639 iov[0].iov_base = buf;
2640 iov[0].iov_len = len;
2641
2642 msg.msg_iov = iov;
2643 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002644 msg.msg_control = &msg_control;
2645 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002646
Corey Bryant06138652012-08-14 16:43:42 -04002647#ifdef MSG_CMSG_CLOEXEC
2648 flags |= MSG_CMSG_CLOEXEC;
2649#endif
2650 ret = recvmsg(s->fd, &msg, flags);
2651 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002652 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002653 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002654
2655 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002656}
2657#else
2658static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2659{
2660 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002661 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002662}
2663#endif
2664
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302665static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2666{
2667 TCPCharDriver *s = chr->opaque;
2668 return g_io_create_watch(s->chan, cond);
2669}
2670
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002671static void tcp_chr_disconnect(CharDriverState *chr)
2672{
2673 TCPCharDriver *s = chr->opaque;
2674
2675 s->connected = 0;
2676 if (s->listen_chan) {
2677 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2678 tcp_chr_accept, chr);
2679 }
2680 remove_fd_in_watch(chr);
2681 g_io_channel_unref(s->chan);
2682 s->chan = NULL;
2683 closesocket(s->fd);
2684 s->fd = -1;
2685 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2686}
2687
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302688static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002689{
2690 CharDriverState *chr = opaque;
2691 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302692 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002693 int len, size;
2694
Kirill Batuzov812c1052014-07-01 15:52:32 +04002695 if (cond & G_IO_HUP) {
2696 /* connection closed */
2697 tcp_chr_disconnect(chr);
2698 return TRUE;
2699 }
2700
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302701 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002702 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302703 }
aliguori6f97dba2008-10-31 18:49:55 +00002704 len = sizeof(buf);
2705 if (len > s->max_size)
2706 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002707 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002708 if (size == 0) {
2709 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002710 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002711 } else if (size > 0) {
2712 if (s->do_telnetopt)
2713 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2714 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002715 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002716 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302717
2718 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002719}
2720
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002721static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2722{
2723 TCPCharDriver *s = chr->opaque;
2724 int size;
2725
2726 if (!s->connected) {
2727 return 0;
2728 }
2729
2730 size = tcp_chr_recv(chr, (void *) buf, len);
2731 if (size == 0) {
2732 /* connection closed */
2733 tcp_chr_disconnect(chr);
2734 }
2735
2736 return size;
2737}
2738
Blue Swirl68c18d12010-08-15 09:46:24 +00002739#ifndef _WIN32
2740CharDriverState *qemu_chr_open_eventfd(int eventfd)
2741{
David Marchande9d21c42014-06-11 17:25:16 +02002742 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2743
2744 if (chr) {
2745 chr->avail_connections = 1;
2746 }
2747
2748 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002749}
Blue Swirl68c18d12010-08-15 09:46:24 +00002750#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002751
aliguori6f97dba2008-10-31 18:49:55 +00002752static void tcp_chr_connect(void *opaque)
2753{
2754 CharDriverState *chr = opaque;
2755 TCPCharDriver *s = chr->opaque;
2756
2757 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302758 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302759 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2760 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002761 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002762 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002763}
2764
Gal Hammerac1b84d2014-02-25 12:12:35 +02002765static void tcp_chr_update_read_handler(CharDriverState *chr)
2766{
2767 TCPCharDriver *s = chr->opaque;
2768
2769 remove_fd_in_watch(chr);
2770 if (s->chan) {
2771 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2772 tcp_chr_read, chr);
2773 }
2774}
2775
aliguori6f97dba2008-10-31 18:49:55 +00002776#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2777static void tcp_chr_telnet_init(int fd)
2778{
2779 char buf[3];
2780 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2781 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2782 send(fd, (char *)buf, 3, 0);
2783 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2784 send(fd, (char *)buf, 3, 0);
2785 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2786 send(fd, (char *)buf, 3, 0);
2787 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2788 send(fd, (char *)buf, 3, 0);
2789}
2790
Daniel P. Berrange13661082011-06-23 13:31:42 +01002791static int tcp_chr_add_client(CharDriverState *chr, int fd)
2792{
2793 TCPCharDriver *s = chr->opaque;
2794 if (s->fd != -1)
2795 return -1;
2796
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002797 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002798 if (s->do_nodelay)
2799 socket_set_nodelay(fd);
2800 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302801 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002802 if (s->listen_tag) {
2803 g_source_remove(s->listen_tag);
2804 s->listen_tag = 0;
2805 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002806 tcp_chr_connect(chr);
2807
2808 return 0;
2809}
2810
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302811static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002812{
2813 CharDriverState *chr = opaque;
2814 TCPCharDriver *s = chr->opaque;
2815 struct sockaddr_in saddr;
2816#ifndef _WIN32
2817 struct sockaddr_un uaddr;
2818#endif
2819 struct sockaddr *addr;
2820 socklen_t len;
2821 int fd;
2822
2823 for(;;) {
2824#ifndef _WIN32
2825 if (s->is_unix) {
2826 len = sizeof(uaddr);
2827 addr = (struct sockaddr *)&uaddr;
2828 } else
2829#endif
2830 {
2831 len = sizeof(saddr);
2832 addr = (struct sockaddr *)&saddr;
2833 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002834 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002835 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002836 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302837 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002838 } else if (fd >= 0) {
2839 if (s->do_telnetopt)
2840 tcp_chr_telnet_init(fd);
2841 break;
2842 }
2843 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002844 if (tcp_chr_add_client(chr, fd) < 0)
2845 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302846
2847 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002848}
2849
2850static void tcp_chr_close(CharDriverState *chr)
2851{
2852 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002853 int i;
aliguori819f56b2009-03-28 17:58:14 +00002854 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302855 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302856 if (s->chan) {
2857 g_io_channel_unref(s->chan);
2858 }
aliguori6f97dba2008-10-31 18:49:55 +00002859 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002860 }
2861 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302862 if (s->listen_tag) {
2863 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002864 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302865 }
2866 if (s->listen_chan) {
2867 g_io_channel_unref(s->listen_chan);
2868 }
aliguori6f97dba2008-10-31 18:49:55 +00002869 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002870 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002871 if (s->read_msgfds_num) {
2872 for (i = 0; i < s->read_msgfds_num; i++) {
2873 close(s->read_msgfds[i]);
2874 }
2875 g_free(s->read_msgfds);
2876 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002877 if (s->write_msgfds_num) {
2878 g_free(s->write_msgfds);
2879 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002880 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002881 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002882}
2883
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002884static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2885 bool is_listen, bool is_telnet,
2886 bool is_waitconnect,
2887 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002888{
2889 CharDriverState *chr = NULL;
2890 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002891 char host[NI_MAXHOST], serv[NI_MAXSERV];
2892 const char *left = "", *right = "";
2893 struct sockaddr_storage ss;
2894 socklen_t ss_len = sizeof(ss);
2895
2896 memset(&ss, 0, ss_len);
2897 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002898 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002899 return NULL;
2900 }
2901
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002902 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002903 s = g_malloc0(sizeof(TCPCharDriver));
2904
2905 s->connected = 0;
2906 s->fd = -1;
2907 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002908 s->read_msgfds = 0;
2909 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002910 s->write_msgfds = 0;
2911 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002912
2913 chr->filename = g_malloc(256);
2914 switch (ss.ss_family) {
2915#ifndef _WIN32
2916 case AF_UNIX:
2917 s->is_unix = 1;
2918 snprintf(chr->filename, 256, "unix:%s%s",
2919 ((struct sockaddr_un *)(&ss))->sun_path,
2920 is_listen ? ",server" : "");
2921 break;
2922#endif
2923 case AF_INET6:
2924 left = "[";
2925 right = "]";
2926 /* fall through */
2927 case AF_INET:
2928 s->do_nodelay = do_nodelay;
2929 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2930 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002931 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002932 is_telnet ? "telnet" : "tcp",
2933 left, host, right, serv,
2934 is_listen ? ",server" : "");
2935 break;
2936 }
2937
2938 chr->opaque = s;
2939 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002940 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002941 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002942 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002943 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002944 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302945 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002946 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002947 /* be isn't opened until we get a connection */
2948 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002949
2950 if (is_listen) {
2951 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302952 s->listen_chan = io_channel_from_socket(s->listen_fd);
2953 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002954 if (is_telnet) {
2955 s->do_telnetopt = 1;
2956 }
2957 } else {
2958 s->connected = 1;
2959 s->fd = fd;
2960 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302961 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002962 tcp_chr_connect(chr);
2963 }
2964
2965 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002966 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2967 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302968 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002969 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002970 }
2971 return chr;
2972}
2973
Lei Li51767e72013-01-25 00:03:19 +08002974/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002975/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002976
2977typedef struct {
2978 size_t size;
2979 size_t prod;
2980 size_t cons;
2981 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002982} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002983
Markus Armbruster3949e592013-02-06 21:27:24 +01002984static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002985{
Markus Armbruster3949e592013-02-06 21:27:24 +01002986 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002987
Markus Armbruster5c230102013-02-06 21:27:23 +01002988 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002989}
2990
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002991/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01002992static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002993{
Markus Armbruster3949e592013-02-06 21:27:24 +01002994 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002995 int i;
2996
2997 if (!buf || (len < 0)) {
2998 return -1;
2999 }
3000
3001 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003002 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3003 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003004 d->cons = d->prod - d->size;
3005 }
3006 }
3007
3008 return 0;
3009}
3010
Markus Armbruster3949e592013-02-06 21:27:24 +01003011static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003012{
Markus Armbruster3949e592013-02-06 21:27:24 +01003013 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003014 int i;
3015
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003016 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003017 for (i = 0; i < len && d->cons != d->prod; i++) {
3018 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003019 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003020 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003021
3022 return i;
3023}
3024
Markus Armbruster3949e592013-02-06 21:27:24 +01003025static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003026{
Markus Armbruster3949e592013-02-06 21:27:24 +01003027 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003028
3029 g_free(d->cbuf);
3030 g_free(d);
3031 chr->opaque = NULL;
3032}
3033
Markus Armbruster4f573782013-07-26 16:44:32 +02003034static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3035 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003036{
3037 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003038 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003039
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003040 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003041 d = g_malloc(sizeof(*d));
3042
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003043 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003044
3045 /* The size must be power of 2 */
3046 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003047 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003048 goto fail;
3049 }
3050
3051 d->prod = 0;
3052 d->cons = 0;
3053 d->cbuf = g_malloc0(d->size);
3054
3055 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003056 chr->chr_write = ringbuf_chr_write;
3057 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003058
3059 return chr;
3060
3061fail:
3062 g_free(d);
3063 g_free(chr);
3064 return NULL;
3065}
3066
Hani Benhabiles8e597772014-05-27 23:39:30 +01003067bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003068{
Markus Armbruster3949e592013-02-06 21:27:24 +01003069 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003070}
3071
Markus Armbruster3949e592013-02-06 21:27:24 +01003072void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003073 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003074 Error **errp)
3075{
3076 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003077 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003078 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003079 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003080
3081 chr = qemu_chr_find(device);
3082 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003083 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003084 return;
3085 }
3086
Markus Armbruster3949e592013-02-06 21:27:24 +01003087 if (!chr_is_ringbuf(chr)) {
3088 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003089 return;
3090 }
3091
Lei Li1f590cf2013-01-25 00:03:20 +08003092 if (has_format && (format == DATA_FORMAT_BASE64)) {
3093 write_data = g_base64_decode(data, &write_count);
3094 } else {
3095 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003096 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003097 }
3098
Markus Armbruster3949e592013-02-06 21:27:24 +01003099 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003100
Markus Armbruster13289fb2013-02-06 21:27:18 +01003101 if (write_data != (uint8_t *)data) {
3102 g_free((void *)write_data);
3103 }
3104
Lei Li1f590cf2013-01-25 00:03:20 +08003105 if (ret < 0) {
3106 error_setg(errp, "Failed to write to device %s", device);
3107 return;
3108 }
3109}
3110
Markus Armbruster3949e592013-02-06 21:27:24 +01003111char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003112 bool has_format, enum DataFormat format,
3113 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003114{
3115 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003116 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003117 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003118 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003119
3120 chr = qemu_chr_find(device);
3121 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003122 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003123 return NULL;
3124 }
3125
Markus Armbruster3949e592013-02-06 21:27:24 +01003126 if (!chr_is_ringbuf(chr)) {
3127 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003128 return NULL;
3129 }
3130
3131 if (size <= 0) {
3132 error_setg(errp, "size must be greater than zero");
3133 return NULL;
3134 }
3135
Markus Armbruster3949e592013-02-06 21:27:24 +01003136 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003137 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003138 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003139
Markus Armbruster3949e592013-02-06 21:27:24 +01003140 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003141
3142 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003143 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003144 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003145 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003146 /*
3147 * FIXME should read only complete, valid UTF-8 characters up
3148 * to @size bytes. Invalid sequences should be replaced by a
3149 * suitable replacement character. Except when (and only
3150 * when) ring buffer lost characters since last read, initial
3151 * continuation characters should be dropped.
3152 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003153 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003154 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003155 }
3156
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003157 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003158}
3159
Gerd Hoffmann33521632009-12-08 13:11:49 +01003160QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003161{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003162 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003163 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003164 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003165 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003166 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003167
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003168 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003169 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003170 qerror_report_err(local_err);
3171 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003172 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003173 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003174
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003175 if (strstart(filename, "mon:", &p)) {
3176 filename = p;
3177 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003178 if (strcmp(filename, "stdio") == 0) {
3179 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3180 * but pass it to the guest. Handle this only for compat syntax,
3181 * for -chardev syntax we have special option for this.
3182 * This is what -nographic did, redirecting+muxing serial+monitor
3183 * to stdio causing Ctrl+C to be passed to guest. */
3184 qemu_opt_set(opts, "signal", "off");
3185 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003186 }
3187
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003188 if (strcmp(filename, "null") == 0 ||
3189 strcmp(filename, "pty") == 0 ||
3190 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003191 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003192 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003193 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003194 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003195 return opts;
3196 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003197 if (strstart(filename, "vc", &p)) {
3198 qemu_opt_set(opts, "backend", "vc");
3199 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003200 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003201 /* pixels */
3202 qemu_opt_set(opts, "width", width);
3203 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003204 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003205 /* chars */
3206 qemu_opt_set(opts, "cols", width);
3207 qemu_opt_set(opts, "rows", height);
3208 } else {
3209 goto fail;
3210 }
3211 }
3212 return opts;
3213 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003214 if (strcmp(filename, "con:") == 0) {
3215 qemu_opt_set(opts, "backend", "console");
3216 return opts;
3217 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003218 if (strstart(filename, "COM", NULL)) {
3219 qemu_opt_set(opts, "backend", "serial");
3220 qemu_opt_set(opts, "path", filename);
3221 return opts;
3222 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003223 if (strstart(filename, "file:", &p)) {
3224 qemu_opt_set(opts, "backend", "file");
3225 qemu_opt_set(opts, "path", p);
3226 return opts;
3227 }
3228 if (strstart(filename, "pipe:", &p)) {
3229 qemu_opt_set(opts, "backend", "pipe");
3230 qemu_opt_set(opts, "path", p);
3231 return opts;
3232 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003233 if (strstart(filename, "tcp:", &p) ||
3234 strstart(filename, "telnet:", &p)) {
3235 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3236 host[0] = 0;
3237 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3238 goto fail;
3239 }
3240 qemu_opt_set(opts, "backend", "socket");
3241 qemu_opt_set(opts, "host", host);
3242 qemu_opt_set(opts, "port", port);
3243 if (p[pos] == ',') {
3244 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3245 goto fail;
3246 }
3247 if (strstart(filename, "telnet:", &p))
3248 qemu_opt_set(opts, "telnet", "on");
3249 return opts;
3250 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003251 if (strstart(filename, "udp:", &p)) {
3252 qemu_opt_set(opts, "backend", "udp");
3253 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3254 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003255 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003256 goto fail;
3257 }
3258 }
3259 qemu_opt_set(opts, "host", host);
3260 qemu_opt_set(opts, "port", port);
3261 if (p[pos] == '@') {
3262 p += pos + 1;
3263 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3264 host[0] = 0;
3265 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003266 goto fail;
3267 }
3268 }
3269 qemu_opt_set(opts, "localaddr", host);
3270 qemu_opt_set(opts, "localport", port);
3271 }
3272 return opts;
3273 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003274 if (strstart(filename, "unix:", &p)) {
3275 qemu_opt_set(opts, "backend", "socket");
3276 if (qemu_opts_do_parse(opts, p, "path") != 0)
3277 goto fail;
3278 return opts;
3279 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003280 if (strstart(filename, "/dev/parport", NULL) ||
3281 strstart(filename, "/dev/ppi", NULL)) {
3282 qemu_opt_set(opts, "backend", "parport");
3283 qemu_opt_set(opts, "path", filename);
3284 return opts;
3285 }
3286 if (strstart(filename, "/dev/", NULL)) {
3287 qemu_opt_set(opts, "backend", "tty");
3288 qemu_opt_set(opts, "path", filename);
3289 return opts;
3290 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003291
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003292fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003293 qemu_opts_del(opts);
3294 return NULL;
3295}
3296
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003297static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3298 Error **errp)
3299{
3300 const char *path = qemu_opt_get(opts, "path");
3301
3302 if (path == NULL) {
3303 error_setg(errp, "chardev: file: no filename given");
3304 return;
3305 }
3306 backend->file = g_new0(ChardevFile, 1);
3307 backend->file->out = g_strdup(path);
3308}
3309
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003310static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3311 Error **errp)
3312{
3313 backend->stdio = g_new0(ChardevStdio, 1);
3314 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003315 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003316}
3317
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003318static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3319 Error **errp)
3320{
3321 const char *device = qemu_opt_get(opts, "path");
3322
3323 if (device == NULL) {
3324 error_setg(errp, "chardev: serial/tty: no device path given");
3325 return;
3326 }
3327 backend->serial = g_new0(ChardevHostdev, 1);
3328 backend->serial->device = g_strdup(device);
3329}
3330
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003331static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3332 Error **errp)
3333{
3334 const char *device = qemu_opt_get(opts, "path");
3335
3336 if (device == NULL) {
3337 error_setg(errp, "chardev: parallel: no device path given");
3338 return;
3339 }
3340 backend->parallel = g_new0(ChardevHostdev, 1);
3341 backend->parallel->device = g_strdup(device);
3342}
3343
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003344static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3345 Error **errp)
3346{
3347 const char *device = qemu_opt_get(opts, "path");
3348
3349 if (device == NULL) {
3350 error_setg(errp, "chardev: pipe: no device path given");
3351 return;
3352 }
3353 backend->pipe = g_new0(ChardevHostdev, 1);
3354 backend->pipe->device = g_strdup(device);
3355}
3356
Markus Armbruster4f573782013-07-26 16:44:32 +02003357static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3358 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003359{
3360 int val;
3361
Markus Armbruster3a1da422013-07-26 16:44:34 +02003362 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003363
Markus Armbruster0f953052013-06-27 16:22:07 +02003364 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003365 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003366 backend->ringbuf->has_size = true;
3367 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003368 }
3369}
3370
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003371static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3372 Error **errp)
3373{
3374 const char *chardev = qemu_opt_get(opts, "chardev");
3375
3376 if (chardev == NULL) {
3377 error_setg(errp, "chardev: mux: no chardev given");
3378 return;
3379 }
3380 backend->mux = g_new0(ChardevMux, 1);
3381 backend->mux->chardev = g_strdup(chardev);
3382}
3383
Peter Maydelldafd3252014-09-02 11:24:13 +01003384static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3385 Error **errp)
3386{
3387 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3388 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3389 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3390 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
3391 const char *path = qemu_opt_get(opts, "path");
3392 const char *host = qemu_opt_get(opts, "host");
3393 const char *port = qemu_opt_get(opts, "port");
3394 SocketAddress *addr;
3395
3396 if (!path) {
3397 if (!host) {
3398 error_setg(errp, "chardev: socket: no host given");
3399 return;
3400 }
3401 if (!port) {
3402 error_setg(errp, "chardev: socket: no port given");
3403 return;
3404 }
3405 }
3406
3407 backend->socket = g_new0(ChardevSocket, 1);
3408
3409 backend->socket->has_nodelay = true;
3410 backend->socket->nodelay = do_nodelay;
3411 backend->socket->has_server = true;
3412 backend->socket->server = is_listen;
3413 backend->socket->has_telnet = true;
3414 backend->socket->telnet = is_telnet;
3415 backend->socket->has_wait = true;
3416 backend->socket->wait = is_waitconnect;
3417
3418 addr = g_new0(SocketAddress, 1);
3419 if (path) {
3420 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3421 addr->q_unix = g_new0(UnixSocketAddress, 1);
3422 addr->q_unix->path = g_strdup(path);
3423 } else {
3424 addr->kind = SOCKET_ADDRESS_KIND_INET;
3425 addr->inet = g_new0(InetSocketAddress, 1);
3426 addr->inet->host = g_strdup(host);
3427 addr->inet->port = g_strdup(port);
3428 addr->inet->has_to = qemu_opt_get(opts, "to");
3429 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3430 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3431 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3432 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3433 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3434 }
3435 backend->socket->addr = addr;
3436}
3437
Peter Maydell90a14bf2014-09-02 11:24:15 +01003438static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3439 Error **errp)
3440{
3441 const char *host = qemu_opt_get(opts, "host");
3442 const char *port = qemu_opt_get(opts, "port");
3443 const char *localaddr = qemu_opt_get(opts, "localaddr");
3444 const char *localport = qemu_opt_get(opts, "localport");
3445 bool has_local = false;
3446 SocketAddress *addr;
3447
3448 if (host == NULL || strlen(host) == 0) {
3449 host = "localhost";
3450 }
3451 if (port == NULL || strlen(port) == 0) {
3452 error_setg(errp, "chardev: udp: remote port not specified");
3453 return;
3454 }
3455 if (localport == NULL || strlen(localport) == 0) {
3456 localport = "0";
3457 } else {
3458 has_local = true;
3459 }
3460 if (localaddr == NULL || strlen(localaddr) == 0) {
3461 localaddr = "";
3462 } else {
3463 has_local = true;
3464 }
3465
3466 backend->udp = g_new0(ChardevUdp, 1);
3467
3468 addr = g_new0(SocketAddress, 1);
3469 addr->kind = SOCKET_ADDRESS_KIND_INET;
3470 addr->inet = g_new0(InetSocketAddress, 1);
3471 addr->inet->host = g_strdup(host);
3472 addr->inet->port = g_strdup(port);
3473 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3474 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3475 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3476 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3477 backend->udp->remote = addr;
3478
3479 if (has_local) {
3480 backend->udp->has_local = true;
3481 addr = g_new0(SocketAddress, 1);
3482 addr->kind = SOCKET_ADDRESS_KIND_INET;
3483 addr->inet = g_new0(InetSocketAddress, 1);
3484 addr->inet->host = g_strdup(localaddr);
3485 addr->inet->port = g_strdup(localport);
3486 backend->udp->local = addr;
3487 }
3488}
3489
Anthony Liguorid654f342013-03-05 23:21:28 +05303490typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003491 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003492 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003493 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303494} CharDriver;
3495
3496static GSList *backends;
3497
Peter Maydelle4d50d42014-09-02 11:24:17 +01003498void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003499 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3500{
3501 CharDriver *s;
3502
3503 s = g_malloc0(sizeof(*s));
3504 s->name = g_strdup(name);
3505 s->kind = kind;
3506 s->parse = parse;
3507
3508 backends = g_slist_append(backends, s);
3509}
3510
Anthony Liguorif69554b2011-08-15 11:17:37 -05003511CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003512 void (*init)(struct CharDriverState *s),
3513 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003514{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003515 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303516 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003517 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303518 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003519 ChardevReturn *ret = NULL;
3520 ChardevBackend *backend;
3521 const char *id = qemu_opts_id(opts);
3522 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003523
Peter Maydella61ae7f2014-09-02 11:24:16 +01003524 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003525 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003526 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003527 }
3528
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003529 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003530 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003531 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003532 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003533 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303534 for (i = backends; i; i = i->next) {
3535 cd = i->data;
3536
3537 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003538 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303539 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003540 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303541 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003542 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003543 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003544 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003545 }
3546
Peter Maydella61ae7f2014-09-02 11:24:16 +01003547 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003548
3549 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003550 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003551 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003552
3553 chr = NULL;
3554 backend->kind = cd->kind;
3555 if (cd->parse) {
3556 cd->parse(opts, backend, &local_err);
3557 if (local_err) {
3558 error_propagate(errp, local_err);
3559 goto qapi_out;
3560 }
3561 }
3562 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3563 if (!ret) {
3564 goto qapi_out;
3565 }
3566
3567 if (bid) {
3568 qapi_free_ChardevBackend(backend);
3569 qapi_free_ChardevReturn(ret);
3570 backend = g_new0(ChardevBackend, 1);
3571 backend->mux = g_new0(ChardevMux, 1);
3572 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3573 backend->mux->chardev = g_strdup(bid);
3574 ret = qmp_chardev_add(id, backend, errp);
3575 if (!ret) {
3576 chr = qemu_chr_find(bid);
3577 qemu_chr_delete(chr);
3578 chr = NULL;
3579 goto qapi_out;
3580 }
3581 }
3582
3583 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003584 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003585
3586qapi_out:
3587 qapi_free_ChardevBackend(backend);
3588 qapi_free_ChardevReturn(ret);
3589 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003590 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003591
3592err:
3593 qemu_opts_del(opts);
3594 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003595}
3596
Anthony Liguori27143a42011-08-15 11:17:36 -05003597CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003598{
3599 const char *p;
3600 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003601 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003602 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003603
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003604 if (strstart(filename, "chardev:", &p)) {
3605 return qemu_chr_find(p);
3606 }
3607
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003608 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003609 if (!opts)
3610 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003611
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003612 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003613 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003614 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003615 error_free(err);
3616 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003617 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003618 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003619 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003620 }
3621 return chr;
3622}
3623
Anthony Liguori15f31512011-08-15 11:17:35 -05003624void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003625{
3626 if (chr->chr_set_echo) {
3627 chr->chr_set_echo(chr, echo);
3628 }
3629}
3630
Hans de Goede8e25daa2013-03-26 11:07:57 +01003631void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003632{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003633 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003634 return;
3635 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003636 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003637 if (chr->chr_set_fe_open) {
3638 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003639 }
3640}
3641
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003642void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3643{
3644 if (chr->chr_fe_event) {
3645 chr->chr_fe_event(chr, event);
3646 }
3647}
3648
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003649int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3650 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303651{
3652 GSource *src;
3653 guint tag;
3654
3655 if (s->chr_add_watch == NULL) {
3656 return -ENOSYS;
3657 }
3658
3659 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003660 if (!src) {
3661 return -EINVAL;
3662 }
3663
Anthony Liguori23673ca2013-03-05 23:21:23 +05303664 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3665 tag = g_source_attach(src, NULL);
3666 g_source_unref(src);
3667
3668 return tag;
3669}
3670
Hans de Goede44c473d2013-03-27 20:29:39 +01003671int qemu_chr_fe_claim(CharDriverState *s)
3672{
3673 if (s->avail_connections < 1) {
3674 return -1;
3675 }
3676 s->avail_connections--;
3677 return 0;
3678}
3679
3680void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3681{
3682 if (qemu_chr_fe_claim(s) != 0) {
3683 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3684 __func__, s->label);
3685 exit(1);
3686 }
3687}
3688
3689void qemu_chr_fe_release(CharDriverState *s)
3690{
3691 s->avail_connections++;
3692}
3693
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003694void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003695{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003696 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003697 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003698 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003699 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003700 g_free(chr->filename);
3701 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003702 if (chr->opts) {
3703 qemu_opts_del(chr->opts);
3704 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003705 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003706}
3707
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003708ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003709{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003710 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003711 CharDriverState *chr;
3712
Blue Swirl72cf2d42009-09-12 07:36:22 +00003713 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003714 ChardevInfoList *info = g_malloc0(sizeof(*info));
3715 info->value = g_malloc0(sizeof(*info->value));
3716 info->value->label = g_strdup(chr->label);
3717 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003718 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003719
3720 info->next = chr_list;
3721 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003722 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003723
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003724 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003725}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003726
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003727ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3728{
3729 ChardevBackendInfoList *backend_list = NULL;
3730 CharDriver *c = NULL;
3731 GSList *i = NULL;
3732
3733 for (i = backends; i; i = i->next) {
3734 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3735 c = i->data;
3736 info->value = g_malloc0(sizeof(*info->value));
3737 info->value->name = g_strdup(c->name);
3738
3739 info->next = backend_list;
3740 backend_list = info;
3741 }
3742
3743 return backend_list;
3744}
3745
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003746CharDriverState *qemu_chr_find(const char *name)
3747{
3748 CharDriverState *chr;
3749
Blue Swirl72cf2d42009-09-12 07:36:22 +00003750 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003751 if (strcmp(chr->label, name) != 0)
3752 continue;
3753 return chr;
3754 }
3755 return NULL;
3756}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003757
3758/* Get a character (serial) device interface. */
3759CharDriverState *qemu_char_get_next_serial(void)
3760{
3761 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003762 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003763
3764 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003765
3766 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3767 chr = serial_hds[next_serial++];
3768 qemu_chr_fe_claim_no_fail(chr);
3769 return chr;
3770 }
3771 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003772}
3773
Paolo Bonzini4d454572012-11-26 16:03:42 +01003774QemuOptsList qemu_chardev_opts = {
3775 .name = "chardev",
3776 .implied_opt_name = "backend",
3777 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3778 .desc = {
3779 {
3780 .name = "backend",
3781 .type = QEMU_OPT_STRING,
3782 },{
3783 .name = "path",
3784 .type = QEMU_OPT_STRING,
3785 },{
3786 .name = "host",
3787 .type = QEMU_OPT_STRING,
3788 },{
3789 .name = "port",
3790 .type = QEMU_OPT_STRING,
3791 },{
3792 .name = "localaddr",
3793 .type = QEMU_OPT_STRING,
3794 },{
3795 .name = "localport",
3796 .type = QEMU_OPT_STRING,
3797 },{
3798 .name = "to",
3799 .type = QEMU_OPT_NUMBER,
3800 },{
3801 .name = "ipv4",
3802 .type = QEMU_OPT_BOOL,
3803 },{
3804 .name = "ipv6",
3805 .type = QEMU_OPT_BOOL,
3806 },{
3807 .name = "wait",
3808 .type = QEMU_OPT_BOOL,
3809 },{
3810 .name = "server",
3811 .type = QEMU_OPT_BOOL,
3812 },{
3813 .name = "delay",
3814 .type = QEMU_OPT_BOOL,
3815 },{
3816 .name = "telnet",
3817 .type = QEMU_OPT_BOOL,
3818 },{
3819 .name = "width",
3820 .type = QEMU_OPT_NUMBER,
3821 },{
3822 .name = "height",
3823 .type = QEMU_OPT_NUMBER,
3824 },{
3825 .name = "cols",
3826 .type = QEMU_OPT_NUMBER,
3827 },{
3828 .name = "rows",
3829 .type = QEMU_OPT_NUMBER,
3830 },{
3831 .name = "mux",
3832 .type = QEMU_OPT_BOOL,
3833 },{
3834 .name = "signal",
3835 .type = QEMU_OPT_BOOL,
3836 },{
3837 .name = "name",
3838 .type = QEMU_OPT_STRING,
3839 },{
3840 .name = "debug",
3841 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003842 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003843 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003844 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003845 },{
3846 .name = "chardev",
3847 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003848 },
3849 { /* end of list */ }
3850 },
3851};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003852
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003853#ifdef _WIN32
3854
3855static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3856{
3857 HANDLE out;
3858
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003859 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003860 error_setg(errp, "input file not supported");
3861 return NULL;
3862 }
3863
3864 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3865 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3866 if (out == INVALID_HANDLE_VALUE) {
3867 error_setg(errp, "open %s failed", file->out);
3868 return NULL;
3869 }
3870 return qemu_chr_open_win_file(out);
3871}
3872
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003873static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3874 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003875{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003876 return qemu_chr_open_win_path(serial->device);
3877}
3878
3879static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3880 Error **errp)
3881{
3882 error_setg(errp, "character device backend type 'parallel' not supported");
3883 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003884}
3885
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003886#else /* WIN32 */
3887
3888static int qmp_chardev_open_file_source(char *src, int flags,
3889 Error **errp)
3890{
3891 int fd = -1;
3892
3893 TFR(fd = qemu_open(src, flags, 0666));
3894 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003895 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003896 }
3897 return fd;
3898}
3899
3900static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3901{
Markus Armbruster5f758362014-05-19 18:57:34 +02003902 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003903
3904 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3905 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003906 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003907 return NULL;
3908 }
3909
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003910 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003911 flags = O_RDONLY;
3912 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003913 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003914 qemu_close(out);
3915 return NULL;
3916 }
3917 }
3918
3919 return qemu_chr_open_fd(in, out);
3920}
3921
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003922static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3923 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003924{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003925#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003926 int fd;
3927
3928 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003929 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003930 return NULL;
3931 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003932 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003933 return qemu_chr_open_tty_fd(fd);
3934#else
3935 error_setg(errp, "character device backend type 'serial' not supported");
3936 return NULL;
3937#endif
3938}
3939
3940static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3941 Error **errp)
3942{
3943#ifdef HAVE_CHARDEV_PARPORT
3944 int fd;
3945
3946 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003947 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003948 return NULL;
3949 }
3950 return qemu_chr_open_pp_fd(fd);
3951#else
3952 error_setg(errp, "character device backend type 'parallel' not supported");
3953 return NULL;
3954#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003955}
3956
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003957#endif /* WIN32 */
3958
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003959static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3960 Error **errp)
3961{
3962 SocketAddress *addr = sock->addr;
3963 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3964 bool is_listen = sock->has_server ? sock->server : true;
3965 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3966 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3967 int fd;
3968
3969 if (is_listen) {
3970 fd = socket_listen(addr, errp);
3971 } else {
3972 fd = socket_connect(addr, errp, NULL, NULL);
3973 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003974 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003975 return NULL;
3976 }
3977 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3978 is_telnet, is_waitconnect, errp);
3979}
3980
Lei Li08d0ab32013-05-20 14:51:03 +08003981static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3982 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003983{
3984 int fd;
3985
Lei Li08d0ab32013-05-20 14:51:03 +08003986 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003987 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003988 return NULL;
3989 }
3990 return qemu_chr_open_udp_fd(fd);
3991}
3992
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003993ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3994 Error **errp)
3995{
3996 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003997 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003998
3999 chr = qemu_chr_find(id);
4000 if (chr) {
4001 error_setg(errp, "Chardev '%s' already exists", id);
4002 g_free(ret);
4003 return NULL;
4004 }
4005
4006 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004007 case CHARDEV_BACKEND_KIND_FILE:
4008 chr = qmp_chardev_open_file(backend->file, errp);
4009 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004010 case CHARDEV_BACKEND_KIND_SERIAL:
4011 chr = qmp_chardev_open_serial(backend->serial, errp);
4012 break;
4013 case CHARDEV_BACKEND_KIND_PARALLEL:
4014 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004015 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004016 case CHARDEV_BACKEND_KIND_PIPE:
4017 chr = qemu_chr_open_pipe(backend->pipe);
4018 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004019 case CHARDEV_BACKEND_KIND_SOCKET:
4020 chr = qmp_chardev_open_socket(backend->socket, errp);
4021 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004022 case CHARDEV_BACKEND_KIND_UDP:
4023 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004024 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004025#ifdef HAVE_CHARDEV_TTY
4026 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004027 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004028 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004029#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004030 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004031 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004032 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004033 case CHARDEV_BACKEND_KIND_MUX:
4034 base = qemu_chr_find(backend->mux->chardev);
4035 if (base == NULL) {
4036 error_setg(errp, "mux: base chardev %s not found",
4037 backend->mux->chardev);
4038 break;
4039 }
4040 chr = qemu_chr_open_mux(base);
4041 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004042 case CHARDEV_BACKEND_KIND_MSMOUSE:
4043 chr = qemu_chr_open_msmouse();
4044 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004045#ifdef CONFIG_BRLAPI
4046 case CHARDEV_BACKEND_KIND_BRAILLE:
4047 chr = chr_baum_init();
4048 break;
4049#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004050 case CHARDEV_BACKEND_KIND_TESTDEV:
4051 chr = chr_testdev_init();
4052 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004053 case CHARDEV_BACKEND_KIND_STDIO:
4054 chr = qemu_chr_open_stdio(backend->stdio);
4055 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004056#ifdef _WIN32
4057 case CHARDEV_BACKEND_KIND_CONSOLE:
4058 chr = qemu_chr_open_win_con();
4059 break;
4060#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004061#ifdef CONFIG_SPICE
4062 case CHARDEV_BACKEND_KIND_SPICEVMC:
4063 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4064 break;
4065 case CHARDEV_BACKEND_KIND_SPICEPORT:
4066 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4067 break;
4068#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004069 case CHARDEV_BACKEND_KIND_VC:
4070 chr = vc_init(backend->vc);
4071 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004072 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004073 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004074 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004075 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004076 default:
4077 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4078 break;
4079 }
4080
Markus Armbruster3894c782014-05-19 18:57:36 +02004081 /*
4082 * Character backend open hasn't been fully converted to the Error
4083 * API. Some opens fail without setting an error. Set a generic
4084 * error then.
4085 * TODO full conversion to Error API
4086 */
4087 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004088 error_setg(errp, "Failed to create chardev");
4089 }
4090 if (chr) {
4091 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004092 chr->avail_connections =
4093 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004094 if (!chr->filename) {
4095 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4096 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004097 if (!chr->explicit_be_open) {
4098 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4099 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004100 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4101 return ret;
4102 } else {
4103 g_free(ret);
4104 return NULL;
4105 }
4106}
4107
4108void qmp_chardev_remove(const char *id, Error **errp)
4109{
4110 CharDriverState *chr;
4111
4112 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004113 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004114 error_setg(errp, "Chardev '%s' not found", id);
4115 return;
4116 }
4117 if (chr->chr_can_read || chr->chr_read ||
4118 chr->chr_event || chr->handler_opaque) {
4119 error_setg(errp, "Chardev '%s' is busy", id);
4120 return;
4121 }
4122 qemu_chr_delete(chr);
4123}
Anthony Liguorid654f342013-03-05 23:21:28 +05304124
4125static void register_types(void)
4126{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004127 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4128 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4129 qemu_chr_parse_socket);
4130 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4131 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4132 qemu_chr_parse_ringbuf);
4133 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4134 qemu_chr_parse_file_out);
4135 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4136 qemu_chr_parse_stdio);
4137 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4138 qemu_chr_parse_serial);
4139 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4140 qemu_chr_parse_serial);
4141 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4142 qemu_chr_parse_parallel);
4143 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4144 qemu_chr_parse_parallel);
4145 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4146 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4147 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4148 qemu_chr_parse_pipe);
4149 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004150 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004151 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4152 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004153 /* this must be done after machine init, since we register FEs with muxes
4154 * as part of realize functions like serial_isa_realizefn when -nographic
4155 * is specified
4156 */
4157 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304158}
4159
4160type_init(register_types);