blob: 55e372cf320d2135a053cde4097cc34bbbb1a6c0 [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);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530978 fcntl(fd_out, F_SETFL, O_NONBLOCK);
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);
1065 fcntl(0, F_SETFL, O_NONBLOCK);
1066 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;
aliguori6f97dba2008-10-31 18:49:55 +00001092} PtyCharDriver;
1093
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001094static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001095static void pty_chr_state(CharDriverState *chr, int connected);
1096
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301097static gboolean pty_chr_timer(gpointer opaque)
1098{
1099 struct CharDriverState *chr = opaque;
1100 PtyCharDriver *s = chr->opaque;
1101
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001102 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001103 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001104 if (!s->connected) {
1105 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001106 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001107 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001108 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301109 return FALSE;
1110}
1111
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001112/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301113static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1114{
1115 PtyCharDriver *s = chr->opaque;
1116
1117 if (s->timer_tag) {
1118 g_source_remove(s->timer_tag);
1119 s->timer_tag = 0;
1120 }
1121
1122 if (ms == 1000) {
1123 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1124 } else {
1125 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1126 }
1127}
1128
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001129/* Called with chr_write_lock held. */
1130static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001131{
1132 PtyCharDriver *s = chr->opaque;
1133 GPollFD pfd;
1134
1135 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1136 pfd.events = G_IO_OUT;
1137 pfd.revents = 0;
1138 g_poll(&pfd, 1, 0);
1139 if (pfd.revents & G_IO_HUP) {
1140 pty_chr_state(chr, 0);
1141 } else {
1142 pty_chr_state(chr, 1);
1143 }
1144}
1145
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001146static void pty_chr_update_read_handler(CharDriverState *chr)
1147{
1148 qemu_mutex_lock(&chr->chr_write_lock);
1149 pty_chr_update_read_handler_locked(chr);
1150 qemu_mutex_unlock(&chr->chr_write_lock);
1151}
1152
1153/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001154static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1155{
1156 PtyCharDriver *s = chr->opaque;
1157
1158 if (!s->connected) {
1159 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001160 pty_chr_update_read_handler_locked(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001161 return 0;
1162 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001163 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001164}
1165
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301166static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1167{
1168 PtyCharDriver *s = chr->opaque;
1169 return g_io_create_watch(s->fd, cond);
1170}
1171
aliguori6f97dba2008-10-31 18:49:55 +00001172static int pty_chr_read_poll(void *opaque)
1173{
1174 CharDriverState *chr = opaque;
1175 PtyCharDriver *s = chr->opaque;
1176
Anthony Liguori909cda12011-08-15 11:17:31 -05001177 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001178 return s->read_bytes;
1179}
1180
Anthony Liguori093d3a22013-03-05 23:21:20 +05301181static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001182{
1183 CharDriverState *chr = opaque;
1184 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301185 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301186 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301187 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001188
1189 len = sizeof(buf);
1190 if (len > s->read_bytes)
1191 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001192 if (len == 0) {
1193 return TRUE;
1194 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301195 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1196 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001197 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301198 return FALSE;
1199 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001200 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001201 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001202 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301203 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001204}
1205
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001206/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001207static void pty_chr_state(CharDriverState *chr, int connected)
1208{
1209 PtyCharDriver *s = chr->opaque;
1210
1211 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301212 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001213 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001214 /* (re-)connect poll interval for idle guests: once per second.
1215 * We check more frequently in case the guests sends data to
1216 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301217 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001218 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001219 if (s->timer_tag) {
1220 g_source_remove(s->timer_tag);
1221 s->timer_tag = 0;
1222 }
1223 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001224 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001225 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001226 }
1227 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301228 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1229 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001230 }
aliguori6f97dba2008-10-31 18:49:55 +00001231 }
1232}
1233
aliguori6f97dba2008-10-31 18:49:55 +00001234static void pty_chr_close(struct CharDriverState *chr)
1235{
1236 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301237 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001238
Amit Shah26da70c2013-08-28 15:23:37 +05301239 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301240 fd = g_io_channel_unix_get_fd(s->fd);
1241 g_io_channel_unref(s->fd);
1242 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301243 if (s->timer_tag) {
1244 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001245 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301246 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001247 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001248 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001249}
1250
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001251static CharDriverState *qemu_chr_open_pty(const char *id,
1252 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001253{
1254 CharDriverState *chr;
1255 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001256 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001257 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001258
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001259 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1260 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001261 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001262 }
1263
aliguori6f97dba2008-10-31 18:49:55 +00001264 close(slave_fd);
1265
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001266 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001267
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001268 chr->filename = g_strdup_printf("pty:%s", pty_name);
1269 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001270 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001271
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001272 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001273 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001274
1275 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001276 chr->opaque = s;
1277 chr->chr_write = pty_chr_write;
1278 chr->chr_update_read_handler = pty_chr_update_read_handler;
1279 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301280 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001281 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001282
Anthony Liguori093d3a22013-03-05 23:21:20 +05301283 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301284 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001285
Markus Armbruster1f514702012-02-07 15:09:08 +01001286 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001287}
1288
1289static void tty_serial_init(int fd, int speed,
1290 int parity, int data_bits, int stop_bits)
1291{
1292 struct termios tty;
1293 speed_t spd;
1294
1295#if 0
1296 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1297 speed, parity, data_bits, stop_bits);
1298#endif
1299 tcgetattr (fd, &tty);
1300
Stefan Weil45eea132009-10-26 16:10:10 +01001301#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1302 speed = speed * 10 / 11;
1303 do {
1304 check_speed(50);
1305 check_speed(75);
1306 check_speed(110);
1307 check_speed(134);
1308 check_speed(150);
1309 check_speed(200);
1310 check_speed(300);
1311 check_speed(600);
1312 check_speed(1200);
1313 check_speed(1800);
1314 check_speed(2400);
1315 check_speed(4800);
1316 check_speed(9600);
1317 check_speed(19200);
1318 check_speed(38400);
1319 /* Non-Posix values follow. They may be unsupported on some systems. */
1320 check_speed(57600);
1321 check_speed(115200);
1322#ifdef B230400
1323 check_speed(230400);
1324#endif
1325#ifdef B460800
1326 check_speed(460800);
1327#endif
1328#ifdef B500000
1329 check_speed(500000);
1330#endif
1331#ifdef B576000
1332 check_speed(576000);
1333#endif
1334#ifdef B921600
1335 check_speed(921600);
1336#endif
1337#ifdef B1000000
1338 check_speed(1000000);
1339#endif
1340#ifdef B1152000
1341 check_speed(1152000);
1342#endif
1343#ifdef B1500000
1344 check_speed(1500000);
1345#endif
1346#ifdef B2000000
1347 check_speed(2000000);
1348#endif
1349#ifdef B2500000
1350 check_speed(2500000);
1351#endif
1352#ifdef B3000000
1353 check_speed(3000000);
1354#endif
1355#ifdef B3500000
1356 check_speed(3500000);
1357#endif
1358#ifdef B4000000
1359 check_speed(4000000);
1360#endif
aliguori6f97dba2008-10-31 18:49:55 +00001361 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001362 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001363
1364 cfsetispeed(&tty, spd);
1365 cfsetospeed(&tty, spd);
1366
1367 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1368 |INLCR|IGNCR|ICRNL|IXON);
1369 tty.c_oflag |= OPOST;
1370 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1371 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1372 switch(data_bits) {
1373 default:
1374 case 8:
1375 tty.c_cflag |= CS8;
1376 break;
1377 case 7:
1378 tty.c_cflag |= CS7;
1379 break;
1380 case 6:
1381 tty.c_cflag |= CS6;
1382 break;
1383 case 5:
1384 tty.c_cflag |= CS5;
1385 break;
1386 }
1387 switch(parity) {
1388 default:
1389 case 'N':
1390 break;
1391 case 'E':
1392 tty.c_cflag |= PARENB;
1393 break;
1394 case 'O':
1395 tty.c_cflag |= PARENB | PARODD;
1396 break;
1397 }
1398 if (stop_bits == 2)
1399 tty.c_cflag |= CSTOPB;
1400
1401 tcsetattr (fd, TCSANOW, &tty);
1402}
1403
1404static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1405{
1406 FDCharDriver *s = chr->opaque;
1407
1408 switch(cmd) {
1409 case CHR_IOCTL_SERIAL_SET_PARAMS:
1410 {
1411 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301412 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1413 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001414 ssp->data_bits, ssp->stop_bits);
1415 }
1416 break;
1417 case CHR_IOCTL_SERIAL_SET_BREAK:
1418 {
1419 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301420 if (enable) {
1421 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1422 }
aliguori6f97dba2008-10-31 18:49:55 +00001423 }
1424 break;
1425 case CHR_IOCTL_SERIAL_GET_TIOCM:
1426 {
1427 int sarg = 0;
1428 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301429 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001430 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001431 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001432 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001433 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001434 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001435 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001436 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001437 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001438 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001439 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001440 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001441 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001442 *targ |= CHR_TIOCM_RTS;
1443 }
1444 break;
1445 case CHR_IOCTL_SERIAL_SET_TIOCM:
1446 {
1447 int sarg = *(int *)arg;
1448 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301449 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001450 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1451 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1452 if (sarg & CHR_TIOCM_CTS)
1453 targ |= TIOCM_CTS;
1454 if (sarg & CHR_TIOCM_CAR)
1455 targ |= TIOCM_CAR;
1456 if (sarg & CHR_TIOCM_DSR)
1457 targ |= TIOCM_DSR;
1458 if (sarg & CHR_TIOCM_RI)
1459 targ |= TIOCM_RI;
1460 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001461 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001462 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001463 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301464 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001465 }
1466 break;
1467 default:
1468 return -ENOTSUP;
1469 }
1470 return 0;
1471}
1472
David Ahern4266a132010-02-10 18:27:17 -07001473static void qemu_chr_close_tty(CharDriverState *chr)
1474{
1475 FDCharDriver *s = chr->opaque;
1476 int fd = -1;
1477
1478 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301479 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001480 }
1481
1482 fd_chr_close(chr);
1483
1484 if (fd >= 0) {
1485 close(fd);
1486 }
1487}
1488
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001489static CharDriverState *qemu_chr_open_tty_fd(int fd)
1490{
1491 CharDriverState *chr;
1492
1493 tty_serial_init(fd, 115200, 'N', 8, 1);
1494 chr = qemu_chr_open_fd(fd, fd);
1495 chr->chr_ioctl = tty_serial_ioctl;
1496 chr->chr_close = qemu_chr_close_tty;
1497 return chr;
1498}
aliguori6f97dba2008-10-31 18:49:55 +00001499#endif /* __linux__ || __sun__ */
1500
1501#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001502
1503#define HAVE_CHARDEV_PARPORT 1
1504
aliguori6f97dba2008-10-31 18:49:55 +00001505typedef struct {
1506 int fd;
1507 int mode;
1508} ParallelCharDriver;
1509
1510static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1511{
1512 if (s->mode != mode) {
1513 int m = mode;
1514 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1515 return 0;
1516 s->mode = mode;
1517 }
1518 return 1;
1519}
1520
1521static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1522{
1523 ParallelCharDriver *drv = chr->opaque;
1524 int fd = drv->fd;
1525 uint8_t b;
1526
1527 switch(cmd) {
1528 case CHR_IOCTL_PP_READ_DATA:
1529 if (ioctl(fd, PPRDATA, &b) < 0)
1530 return -ENOTSUP;
1531 *(uint8_t *)arg = b;
1532 break;
1533 case CHR_IOCTL_PP_WRITE_DATA:
1534 b = *(uint8_t *)arg;
1535 if (ioctl(fd, PPWDATA, &b) < 0)
1536 return -ENOTSUP;
1537 break;
1538 case CHR_IOCTL_PP_READ_CONTROL:
1539 if (ioctl(fd, PPRCONTROL, &b) < 0)
1540 return -ENOTSUP;
1541 /* Linux gives only the lowest bits, and no way to know data
1542 direction! For better compatibility set the fixed upper
1543 bits. */
1544 *(uint8_t *)arg = b | 0xc0;
1545 break;
1546 case CHR_IOCTL_PP_WRITE_CONTROL:
1547 b = *(uint8_t *)arg;
1548 if (ioctl(fd, PPWCONTROL, &b) < 0)
1549 return -ENOTSUP;
1550 break;
1551 case CHR_IOCTL_PP_READ_STATUS:
1552 if (ioctl(fd, PPRSTATUS, &b) < 0)
1553 return -ENOTSUP;
1554 *(uint8_t *)arg = b;
1555 break;
1556 case CHR_IOCTL_PP_DATA_DIR:
1557 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1558 return -ENOTSUP;
1559 break;
1560 case CHR_IOCTL_PP_EPP_READ_ADDR:
1561 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1562 struct ParallelIOArg *parg = arg;
1563 int n = read(fd, parg->buffer, parg->count);
1564 if (n != parg->count) {
1565 return -EIO;
1566 }
1567 }
1568 break;
1569 case CHR_IOCTL_PP_EPP_READ:
1570 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1571 struct ParallelIOArg *parg = arg;
1572 int n = read(fd, parg->buffer, parg->count);
1573 if (n != parg->count) {
1574 return -EIO;
1575 }
1576 }
1577 break;
1578 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1579 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1580 struct ParallelIOArg *parg = arg;
1581 int n = write(fd, parg->buffer, parg->count);
1582 if (n != parg->count) {
1583 return -EIO;
1584 }
1585 }
1586 break;
1587 case CHR_IOCTL_PP_EPP_WRITE:
1588 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1589 struct ParallelIOArg *parg = arg;
1590 int n = write(fd, parg->buffer, parg->count);
1591 if (n != parg->count) {
1592 return -EIO;
1593 }
1594 }
1595 break;
1596 default:
1597 return -ENOTSUP;
1598 }
1599 return 0;
1600}
1601
1602static void pp_close(CharDriverState *chr)
1603{
1604 ParallelCharDriver *drv = chr->opaque;
1605 int fd = drv->fd;
1606
1607 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1608 ioctl(fd, PPRELEASE);
1609 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001610 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001611 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001612}
1613
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001614static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001615{
1616 CharDriverState *chr;
1617 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001618
1619 if (ioctl(fd, PPCLAIM) < 0) {
1620 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001621 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001622 }
1623
Anthony Liguori7267c092011-08-20 22:09:37 -05001624 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001625 drv->fd = fd;
1626 drv->mode = IEEE1284_MODE_COMPAT;
1627
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001628 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001629 chr->chr_write = null_chr_write;
1630 chr->chr_ioctl = pp_ioctl;
1631 chr->chr_close = pp_close;
1632 chr->opaque = drv;
1633
Markus Armbruster1f514702012-02-07 15:09:08 +01001634 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001635}
1636#endif /* __linux__ */
1637
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001638#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001639
1640#define HAVE_CHARDEV_PARPORT 1
1641
blueswir16972f932008-11-22 20:49:12 +00001642static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1643{
Stefan Weile0efb992011-02-23 19:09:16 +01001644 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001645 uint8_t b;
1646
1647 switch(cmd) {
1648 case CHR_IOCTL_PP_READ_DATA:
1649 if (ioctl(fd, PPIGDATA, &b) < 0)
1650 return -ENOTSUP;
1651 *(uint8_t *)arg = b;
1652 break;
1653 case CHR_IOCTL_PP_WRITE_DATA:
1654 b = *(uint8_t *)arg;
1655 if (ioctl(fd, PPISDATA, &b) < 0)
1656 return -ENOTSUP;
1657 break;
1658 case CHR_IOCTL_PP_READ_CONTROL:
1659 if (ioctl(fd, PPIGCTRL, &b) < 0)
1660 return -ENOTSUP;
1661 *(uint8_t *)arg = b;
1662 break;
1663 case CHR_IOCTL_PP_WRITE_CONTROL:
1664 b = *(uint8_t *)arg;
1665 if (ioctl(fd, PPISCTRL, &b) < 0)
1666 return -ENOTSUP;
1667 break;
1668 case CHR_IOCTL_PP_READ_STATUS:
1669 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1670 return -ENOTSUP;
1671 *(uint8_t *)arg = b;
1672 break;
1673 default:
1674 return -ENOTSUP;
1675 }
1676 return 0;
1677}
1678
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001679static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001680{
1681 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001682
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001683 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001684 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001685 chr->chr_write = null_chr_write;
1686 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001687 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001688 return chr;
blueswir16972f932008-11-22 20:49:12 +00001689}
1690#endif
1691
aliguori6f97dba2008-10-31 18:49:55 +00001692#else /* _WIN32 */
1693
1694typedef struct {
1695 int max_size;
1696 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001697 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001698 BOOL fpipe;
1699 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001700
1701 /* Protected by the CharDriverState chr_write_lock. */
1702 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001703} WinCharState;
1704
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001705typedef struct {
1706 HANDLE hStdIn;
1707 HANDLE hInputReadyEvent;
1708 HANDLE hInputDoneEvent;
1709 HANDLE hInputThread;
1710 uint8_t win_stdio_buf;
1711} WinStdioCharState;
1712
aliguori6f97dba2008-10-31 18:49:55 +00001713#define NSENDBUF 2048
1714#define NRECVBUF 2048
1715#define MAXCONNECT 1
1716#define NTIMEOUT 5000
1717
1718static int win_chr_poll(void *opaque);
1719static int win_chr_pipe_poll(void *opaque);
1720
1721static void win_chr_close(CharDriverState *chr)
1722{
1723 WinCharState *s = chr->opaque;
1724
1725 if (s->hsend) {
1726 CloseHandle(s->hsend);
1727 s->hsend = NULL;
1728 }
1729 if (s->hrecv) {
1730 CloseHandle(s->hrecv);
1731 s->hrecv = NULL;
1732 }
1733 if (s->hcom) {
1734 CloseHandle(s->hcom);
1735 s->hcom = NULL;
1736 }
1737 if (s->fpipe)
1738 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1739 else
1740 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301741
Hans de Goedea425d232011-11-19 10:22:43 +01001742 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001743}
1744
1745static int win_chr_init(CharDriverState *chr, const char *filename)
1746{
1747 WinCharState *s = chr->opaque;
1748 COMMCONFIG comcfg;
1749 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1750 COMSTAT comstat;
1751 DWORD size;
1752 DWORD err;
1753
1754 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1755 if (!s->hsend) {
1756 fprintf(stderr, "Failed CreateEvent\n");
1757 goto fail;
1758 }
1759 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1760 if (!s->hrecv) {
1761 fprintf(stderr, "Failed CreateEvent\n");
1762 goto fail;
1763 }
1764
1765 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1766 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1767 if (s->hcom == INVALID_HANDLE_VALUE) {
1768 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1769 s->hcom = NULL;
1770 goto fail;
1771 }
1772
1773 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1774 fprintf(stderr, "Failed SetupComm\n");
1775 goto fail;
1776 }
1777
1778 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1779 size = sizeof(COMMCONFIG);
1780 GetDefaultCommConfig(filename, &comcfg, &size);
1781 comcfg.dcb.DCBlength = sizeof(DCB);
1782 CommConfigDialog(filename, NULL, &comcfg);
1783
1784 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1785 fprintf(stderr, "Failed SetCommState\n");
1786 goto fail;
1787 }
1788
1789 if (!SetCommMask(s->hcom, EV_ERR)) {
1790 fprintf(stderr, "Failed SetCommMask\n");
1791 goto fail;
1792 }
1793
1794 cto.ReadIntervalTimeout = MAXDWORD;
1795 if (!SetCommTimeouts(s->hcom, &cto)) {
1796 fprintf(stderr, "Failed SetCommTimeouts\n");
1797 goto fail;
1798 }
1799
1800 if (!ClearCommError(s->hcom, &err, &comstat)) {
1801 fprintf(stderr, "Failed ClearCommError\n");
1802 goto fail;
1803 }
1804 qemu_add_polling_cb(win_chr_poll, chr);
1805 return 0;
1806
1807 fail:
1808 win_chr_close(chr);
1809 return -1;
1810}
1811
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001812/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001813static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1814{
1815 WinCharState *s = chr->opaque;
1816 DWORD len, ret, size, err;
1817
1818 len = len1;
1819 ZeroMemory(&s->osend, sizeof(s->osend));
1820 s->osend.hEvent = s->hsend;
1821 while (len > 0) {
1822 if (s->hsend)
1823 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1824 else
1825 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1826 if (!ret) {
1827 err = GetLastError();
1828 if (err == ERROR_IO_PENDING) {
1829 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1830 if (ret) {
1831 buf += size;
1832 len -= size;
1833 } else {
1834 break;
1835 }
1836 } else {
1837 break;
1838 }
1839 } else {
1840 buf += size;
1841 len -= size;
1842 }
1843 }
1844 return len1 - len;
1845}
1846
1847static int win_chr_read_poll(CharDriverState *chr)
1848{
1849 WinCharState *s = chr->opaque;
1850
Anthony Liguori909cda12011-08-15 11:17:31 -05001851 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001852 return s->max_size;
1853}
1854
1855static void win_chr_readfile(CharDriverState *chr)
1856{
1857 WinCharState *s = chr->opaque;
1858 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301859 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001860 DWORD size;
1861
1862 ZeroMemory(&s->orecv, sizeof(s->orecv));
1863 s->orecv.hEvent = s->hrecv;
1864 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1865 if (!ret) {
1866 err = GetLastError();
1867 if (err == ERROR_IO_PENDING) {
1868 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1869 }
1870 }
1871
1872 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001873 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001874 }
1875}
1876
1877static void win_chr_read(CharDriverState *chr)
1878{
1879 WinCharState *s = chr->opaque;
1880
1881 if (s->len > s->max_size)
1882 s->len = s->max_size;
1883 if (s->len == 0)
1884 return;
1885
1886 win_chr_readfile(chr);
1887}
1888
1889static int win_chr_poll(void *opaque)
1890{
1891 CharDriverState *chr = opaque;
1892 WinCharState *s = chr->opaque;
1893 COMSTAT status;
1894 DWORD comerr;
1895
1896 ClearCommError(s->hcom, &comerr, &status);
1897 if (status.cbInQue > 0) {
1898 s->len = status.cbInQue;
1899 win_chr_read_poll(chr);
1900 win_chr_read(chr);
1901 return 1;
1902 }
1903 return 0;
1904}
1905
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001906static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001907{
1908 CharDriverState *chr;
1909 WinCharState *s;
1910
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001911 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001912 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001913 chr->opaque = s;
1914 chr->chr_write = win_chr_write;
1915 chr->chr_close = win_chr_close;
1916
1917 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001918 g_free(s);
1919 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001920 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001921 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001922 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001923}
1924
1925static int win_chr_pipe_poll(void *opaque)
1926{
1927 CharDriverState *chr = opaque;
1928 WinCharState *s = chr->opaque;
1929 DWORD size;
1930
1931 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1932 if (size > 0) {
1933 s->len = size;
1934 win_chr_read_poll(chr);
1935 win_chr_read(chr);
1936 return 1;
1937 }
1938 return 0;
1939}
1940
1941static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1942{
1943 WinCharState *s = chr->opaque;
1944 OVERLAPPED ov;
1945 int ret;
1946 DWORD size;
1947 char openname[256];
1948
1949 s->fpipe = TRUE;
1950
1951 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1952 if (!s->hsend) {
1953 fprintf(stderr, "Failed CreateEvent\n");
1954 goto fail;
1955 }
1956 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1957 if (!s->hrecv) {
1958 fprintf(stderr, "Failed CreateEvent\n");
1959 goto fail;
1960 }
1961
1962 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1963 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1964 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1965 PIPE_WAIT,
1966 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1967 if (s->hcom == INVALID_HANDLE_VALUE) {
1968 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1969 s->hcom = NULL;
1970 goto fail;
1971 }
1972
1973 ZeroMemory(&ov, sizeof(ov));
1974 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1975 ret = ConnectNamedPipe(s->hcom, &ov);
1976 if (ret) {
1977 fprintf(stderr, "Failed ConnectNamedPipe\n");
1978 goto fail;
1979 }
1980
1981 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1982 if (!ret) {
1983 fprintf(stderr, "Failed GetOverlappedResult\n");
1984 if (ov.hEvent) {
1985 CloseHandle(ov.hEvent);
1986 ov.hEvent = NULL;
1987 }
1988 goto fail;
1989 }
1990
1991 if (ov.hEvent) {
1992 CloseHandle(ov.hEvent);
1993 ov.hEvent = NULL;
1994 }
1995 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1996 return 0;
1997
1998 fail:
1999 win_chr_close(chr);
2000 return -1;
2001}
2002
2003
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002004static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002005{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002006 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002007 CharDriverState *chr;
2008 WinCharState *s;
2009
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002010 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002011 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002012 chr->opaque = s;
2013 chr->chr_write = win_chr_write;
2014 chr->chr_close = win_chr_close;
2015
2016 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002017 g_free(s);
2018 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002019 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002020 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002021 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002022}
2023
Markus Armbruster1f514702012-02-07 15:09:08 +01002024static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002025{
2026 CharDriverState *chr;
2027 WinCharState *s;
2028
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002029 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002030 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002031 s->hcom = fd_out;
2032 chr->opaque = s;
2033 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002034 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002035}
2036
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002037static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002038{
Markus Armbruster1f514702012-02-07 15:09:08 +01002039 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002040}
2041
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002042static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2043{
2044 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2045 DWORD dwSize;
2046 int len1;
2047
2048 len1 = len;
2049
2050 while (len1 > 0) {
2051 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2052 break;
2053 }
2054 buf += dwSize;
2055 len1 -= dwSize;
2056 }
2057
2058 return len - len1;
2059}
2060
2061static void win_stdio_wait_func(void *opaque)
2062{
2063 CharDriverState *chr = opaque;
2064 WinStdioCharState *stdio = chr->opaque;
2065 INPUT_RECORD buf[4];
2066 int ret;
2067 DWORD dwSize;
2068 int i;
2069
Stefan Weildff74242013-12-07 14:48:04 +01002070 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002071
2072 if (!ret) {
2073 /* Avoid error storm */
2074 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2075 return;
2076 }
2077
2078 for (i = 0; i < dwSize; i++) {
2079 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2080
2081 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2082 int j;
2083 if (kev->uChar.AsciiChar != 0) {
2084 for (j = 0; j < kev->wRepeatCount; j++) {
2085 if (qemu_chr_be_can_write(chr)) {
2086 uint8_t c = kev->uChar.AsciiChar;
2087 qemu_chr_be_write(chr, &c, 1);
2088 }
2089 }
2090 }
2091 }
2092 }
2093}
2094
2095static DWORD WINAPI win_stdio_thread(LPVOID param)
2096{
2097 CharDriverState *chr = param;
2098 WinStdioCharState *stdio = chr->opaque;
2099 int ret;
2100 DWORD dwSize;
2101
2102 while (1) {
2103
2104 /* Wait for one byte */
2105 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2106
2107 /* Exit in case of error, continue if nothing read */
2108 if (!ret) {
2109 break;
2110 }
2111 if (!dwSize) {
2112 continue;
2113 }
2114
2115 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2116 if (stdio->win_stdio_buf == '\r') {
2117 continue;
2118 }
2119
2120 /* Signal the main thread and wait until the byte was eaten */
2121 if (!SetEvent(stdio->hInputReadyEvent)) {
2122 break;
2123 }
2124 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2125 != WAIT_OBJECT_0) {
2126 break;
2127 }
2128 }
2129
2130 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2131 return 0;
2132}
2133
2134static void win_stdio_thread_wait_func(void *opaque)
2135{
2136 CharDriverState *chr = opaque;
2137 WinStdioCharState *stdio = chr->opaque;
2138
2139 if (qemu_chr_be_can_write(chr)) {
2140 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2141 }
2142
2143 SetEvent(stdio->hInputDoneEvent);
2144}
2145
2146static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2147{
2148 WinStdioCharState *stdio = chr->opaque;
2149 DWORD dwMode = 0;
2150
2151 GetConsoleMode(stdio->hStdIn, &dwMode);
2152
2153 if (echo) {
2154 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2155 } else {
2156 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2157 }
2158}
2159
2160static void win_stdio_close(CharDriverState *chr)
2161{
2162 WinStdioCharState *stdio = chr->opaque;
2163
2164 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2165 CloseHandle(stdio->hInputReadyEvent);
2166 }
2167 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2168 CloseHandle(stdio->hInputDoneEvent);
2169 }
2170 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2171 TerminateThread(stdio->hInputThread, 0);
2172 }
2173
2174 g_free(chr->opaque);
2175 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002176}
2177
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002178static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002179{
2180 CharDriverState *chr;
2181 WinStdioCharState *stdio;
2182 DWORD dwMode;
2183 int is_console = 0;
2184
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002185 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002186 stdio = g_malloc0(sizeof(WinStdioCharState));
2187
2188 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2189 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2190 fprintf(stderr, "cannot open stdio: invalid handle\n");
2191 exit(1);
2192 }
2193
2194 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2195
2196 chr->opaque = stdio;
2197 chr->chr_write = win_stdio_write;
2198 chr->chr_close = win_stdio_close;
2199
Anthony Liguoried7a1542013-03-05 23:21:17 +05302200 if (is_console) {
2201 if (qemu_add_wait_object(stdio->hStdIn,
2202 win_stdio_wait_func, chr)) {
2203 fprintf(stderr, "qemu_add_wait_object: failed\n");
2204 }
2205 } else {
2206 DWORD dwId;
2207
2208 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2209 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2210 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2211 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002212
Anthony Liguoried7a1542013-03-05 23:21:17 +05302213 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2214 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2215 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2216 fprintf(stderr, "cannot create stdio thread or event\n");
2217 exit(1);
2218 }
2219 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2220 win_stdio_thread_wait_func, chr)) {
2221 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002222 }
2223 }
2224
2225 dwMode |= ENABLE_LINE_INPUT;
2226
Anthony Liguoried7a1542013-03-05 23:21:17 +05302227 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002228 /* set the terminal in raw mode */
2229 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2230 dwMode |= ENABLE_PROCESSED_INPUT;
2231 }
2232
2233 SetConsoleMode(stdio->hStdIn, dwMode);
2234
2235 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2236 qemu_chr_fe_set_echo(chr, false);
2237
Markus Armbruster1f514702012-02-07 15:09:08 +01002238 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002239}
aliguori6f97dba2008-10-31 18:49:55 +00002240#endif /* !_WIN32 */
2241
Anthony Liguori5ab82112013-03-05 23:21:31 +05302242
aliguori6f97dba2008-10-31 18:49:55 +00002243/***********************************************************/
2244/* UDP Net console */
2245
2246typedef struct {
2247 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302248 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302249 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002250 int bufcnt;
2251 int bufptr;
2252 int max_size;
2253} NetCharDriver;
2254
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002255/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002256static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2257{
2258 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302259 gsize bytes_written;
2260 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002261
Anthony Liguori76a96442013-03-05 23:21:21 +05302262 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2263 if (status == G_IO_STATUS_EOF) {
2264 return 0;
2265 } else if (status != G_IO_STATUS_NORMAL) {
2266 return -1;
2267 }
2268
2269 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002270}
2271
2272static int udp_chr_read_poll(void *opaque)
2273{
2274 CharDriverState *chr = opaque;
2275 NetCharDriver *s = chr->opaque;
2276
Anthony Liguori909cda12011-08-15 11:17:31 -05002277 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002278
2279 /* If there were any stray characters in the queue process them
2280 * first
2281 */
2282 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002283 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002284 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002285 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002286 }
2287 return s->max_size;
2288}
2289
Anthony Liguori76a96442013-03-05 23:21:21 +05302290static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002291{
2292 CharDriverState *chr = opaque;
2293 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302294 gsize bytes_read = 0;
2295 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002296
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002297 if (s->max_size == 0) {
2298 return TRUE;
2299 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302300 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2301 &bytes_read, NULL);
2302 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002303 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302304 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302305 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302306 return FALSE;
2307 }
aliguori6f97dba2008-10-31 18:49:55 +00002308
2309 s->bufptr = 0;
2310 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002311 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002312 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002313 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002314 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302315
2316 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002317}
2318
2319static void udp_chr_update_read_handler(CharDriverState *chr)
2320{
2321 NetCharDriver *s = chr->opaque;
2322
Amit Shah26da70c2013-08-28 15:23:37 +05302323 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302324 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302325 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2326 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002327 }
2328}
2329
aliguori819f56b2009-03-28 17:58:14 +00002330static void udp_chr_close(CharDriverState *chr)
2331{
2332 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302333
2334 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302335 if (s->chan) {
2336 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002337 closesocket(s->fd);
2338 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002339 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002340 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002341}
2342
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002343static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002344{
2345 CharDriverState *chr = NULL;
2346 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002347
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002348 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002349 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002350
aliguori6f97dba2008-10-31 18:49:55 +00002351 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302352 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002353 s->bufcnt = 0;
2354 s->bufptr = 0;
2355 chr->opaque = s;
2356 chr->chr_write = udp_chr_write;
2357 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002358 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002359 /* be isn't opened until we get a connection */
2360 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002361 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002362}
aliguori6f97dba2008-10-31 18:49:55 +00002363
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002364static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2365{
2366 Error *local_err = NULL;
2367 int fd = -1;
2368
2369 fd = inet_dgram_opts(opts, &local_err);
2370 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002371 qerror_report_err(local_err);
2372 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002373 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002374 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002375 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002376}
2377
2378/***********************************************************/
2379/* TCP Net console */
2380
2381typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302382
2383 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302384 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002385 int fd, listen_fd;
2386 int connected;
2387 int max_size;
2388 int do_telnetopt;
2389 int do_nodelay;
2390 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002391 int *read_msgfds;
2392 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002393 int *write_msgfds;
2394 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002395} TCPCharDriver;
2396
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302397static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002398
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002399#ifndef _WIN32
2400static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2401{
2402 TCPCharDriver *s = chr->opaque;
2403 struct msghdr msgh;
2404 struct iovec iov;
2405 int r;
2406
2407 size_t fd_size = s->write_msgfds_num * sizeof(int);
2408 char control[CMSG_SPACE(fd_size)];
2409 struct cmsghdr *cmsg;
2410
2411 memset(&msgh, 0, sizeof(msgh));
2412 memset(control, 0, sizeof(control));
2413
2414 /* set the payload */
2415 iov.iov_base = (uint8_t *) buf;
2416 iov.iov_len = len;
2417
2418 msgh.msg_iov = &iov;
2419 msgh.msg_iovlen = 1;
2420
2421 msgh.msg_control = control;
2422 msgh.msg_controllen = sizeof(control);
2423
2424 cmsg = CMSG_FIRSTHDR(&msgh);
2425
2426 cmsg->cmsg_len = CMSG_LEN(fd_size);
2427 cmsg->cmsg_level = SOL_SOCKET;
2428 cmsg->cmsg_type = SCM_RIGHTS;
2429 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2430
2431 do {
2432 r = sendmsg(s->fd, &msgh, 0);
2433 } while (r < 0 && errno == EINTR);
2434
2435 /* free the written msgfds, no matter what */
2436 if (s->write_msgfds_num) {
2437 g_free(s->write_msgfds);
2438 s->write_msgfds = 0;
2439 s->write_msgfds_num = 0;
2440 }
2441
2442 return r;
2443}
2444#endif
2445
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002446/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002447static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2448{
2449 TCPCharDriver *s = chr->opaque;
2450 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002451#ifndef _WIN32
2452 if (s->is_unix && s->write_msgfds_num) {
2453 return unix_send_msgfds(chr, buf, len);
2454 } else
2455#endif
2456 {
2457 return io_channel_send(s->chan, buf, len);
2458 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002459 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002460 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002461 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002462 }
2463}
2464
2465static int tcp_chr_read_poll(void *opaque)
2466{
2467 CharDriverState *chr = opaque;
2468 TCPCharDriver *s = chr->opaque;
2469 if (!s->connected)
2470 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002471 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002472 return s->max_size;
2473}
2474
2475#define IAC 255
2476#define IAC_BREAK 243
2477static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2478 TCPCharDriver *s,
2479 uint8_t *buf, int *size)
2480{
2481 /* Handle any telnet client's basic IAC options to satisfy char by
2482 * char mode with no echo. All IAC options will be removed from
2483 * the buf and the do_telnetopt variable will be used to track the
2484 * state of the width of the IAC information.
2485 *
2486 * IAC commands come in sets of 3 bytes with the exception of the
2487 * "IAC BREAK" command and the double IAC.
2488 */
2489
2490 int i;
2491 int j = 0;
2492
2493 for (i = 0; i < *size; i++) {
2494 if (s->do_telnetopt > 1) {
2495 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2496 /* Double IAC means send an IAC */
2497 if (j != i)
2498 buf[j] = buf[i];
2499 j++;
2500 s->do_telnetopt = 1;
2501 } else {
2502 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2503 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002504 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002505 s->do_telnetopt++;
2506 }
2507 s->do_telnetopt++;
2508 }
2509 if (s->do_telnetopt >= 4) {
2510 s->do_telnetopt = 1;
2511 }
2512 } else {
2513 if ((unsigned char)buf[i] == IAC) {
2514 s->do_telnetopt = 2;
2515 } else {
2516 if (j != i)
2517 buf[j] = buf[i];
2518 j++;
2519 }
2520 }
2521 }
2522 *size = j;
2523}
2524
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002525static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002526{
2527 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002528 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2529
2530 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002531 int i;
2532
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002533 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2534
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002535 /* Close unused fds */
2536 for (i = to_copy; i < s->read_msgfds_num; i++) {
2537 close(s->read_msgfds[i]);
2538 }
2539
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002540 g_free(s->read_msgfds);
2541 s->read_msgfds = 0;
2542 s->read_msgfds_num = 0;
2543 }
2544
2545 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002546}
2547
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002548static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2549{
2550 TCPCharDriver *s = chr->opaque;
2551
2552 /* clear old pending fd array */
2553 if (s->write_msgfds) {
2554 g_free(s->write_msgfds);
2555 }
2556
2557 if (num) {
2558 s->write_msgfds = g_malloc(num * sizeof(int));
2559 memcpy(s->write_msgfds, fds, num * sizeof(int));
2560 }
2561
2562 s->write_msgfds_num = num;
2563
2564 return 0;
2565}
2566
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002567#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002568static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2569{
2570 TCPCharDriver *s = chr->opaque;
2571 struct cmsghdr *cmsg;
2572
2573 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002574 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002575
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002576 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002577 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002578 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002579 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002580 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002581
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002582 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2583
2584 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002585 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002586 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002587
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002588 /* close and clean read_msgfds */
2589 for (i = 0; i < s->read_msgfds_num; i++) {
2590 close(s->read_msgfds[i]);
2591 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002592
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002593 if (s->read_msgfds_num) {
2594 g_free(s->read_msgfds);
2595 }
2596
2597 s->read_msgfds_num = fd_size / sizeof(int);
2598 s->read_msgfds = g_malloc(fd_size);
2599 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2600
2601 for (i = 0; i < s->read_msgfds_num; i++) {
2602 int fd = s->read_msgfds[i];
2603 if (fd < 0) {
2604 continue;
2605 }
2606
2607 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2608 qemu_set_block(fd);
2609
2610 #ifndef MSG_CMSG_CLOEXEC
2611 qemu_set_cloexec(fd);
2612 #endif
2613 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002614 }
2615}
2616
Mark McLoughlin9977c892009-07-22 09:11:38 +01002617static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2618{
2619 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002620 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002621 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002622 union {
2623 struct cmsghdr cmsg;
2624 char control[CMSG_SPACE(sizeof(int))];
2625 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002626 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002627 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002628
2629 iov[0].iov_base = buf;
2630 iov[0].iov_len = len;
2631
2632 msg.msg_iov = iov;
2633 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002634 msg.msg_control = &msg_control;
2635 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002636
Corey Bryant06138652012-08-14 16:43:42 -04002637#ifdef MSG_CMSG_CLOEXEC
2638 flags |= MSG_CMSG_CLOEXEC;
2639#endif
2640 ret = recvmsg(s->fd, &msg, flags);
2641 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002642 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002643 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002644
2645 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002646}
2647#else
2648static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2649{
2650 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002651 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002652}
2653#endif
2654
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302655static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2656{
2657 TCPCharDriver *s = chr->opaque;
2658 return g_io_create_watch(s->chan, cond);
2659}
2660
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002661static void tcp_chr_disconnect(CharDriverState *chr)
2662{
2663 TCPCharDriver *s = chr->opaque;
2664
2665 s->connected = 0;
2666 if (s->listen_chan) {
2667 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2668 tcp_chr_accept, chr);
2669 }
2670 remove_fd_in_watch(chr);
2671 g_io_channel_unref(s->chan);
2672 s->chan = NULL;
2673 closesocket(s->fd);
2674 s->fd = -1;
2675 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2676}
2677
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302678static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002679{
2680 CharDriverState *chr = opaque;
2681 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302682 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002683 int len, size;
2684
Kirill Batuzov812c1052014-07-01 15:52:32 +04002685 if (cond & G_IO_HUP) {
2686 /* connection closed */
2687 tcp_chr_disconnect(chr);
2688 return TRUE;
2689 }
2690
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302691 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002692 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302693 }
aliguori6f97dba2008-10-31 18:49:55 +00002694 len = sizeof(buf);
2695 if (len > s->max_size)
2696 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002697 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002698 if (size == 0) {
2699 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002700 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002701 } else if (size > 0) {
2702 if (s->do_telnetopt)
2703 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2704 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002705 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002706 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302707
2708 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002709}
2710
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002711static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2712{
2713 TCPCharDriver *s = chr->opaque;
2714 int size;
2715
2716 if (!s->connected) {
2717 return 0;
2718 }
2719
2720 size = tcp_chr_recv(chr, (void *) buf, len);
2721 if (size == 0) {
2722 /* connection closed */
2723 tcp_chr_disconnect(chr);
2724 }
2725
2726 return size;
2727}
2728
Blue Swirl68c18d12010-08-15 09:46:24 +00002729#ifndef _WIN32
2730CharDriverState *qemu_chr_open_eventfd(int eventfd)
2731{
David Marchande9d21c42014-06-11 17:25:16 +02002732 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2733
2734 if (chr) {
2735 chr->avail_connections = 1;
2736 }
2737
2738 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002739}
Blue Swirl68c18d12010-08-15 09:46:24 +00002740#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002741
aliguori6f97dba2008-10-31 18:49:55 +00002742static void tcp_chr_connect(void *opaque)
2743{
2744 CharDriverState *chr = opaque;
2745 TCPCharDriver *s = chr->opaque;
2746
2747 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302748 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302749 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2750 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002751 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002752 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002753}
2754
Gal Hammerac1b84d2014-02-25 12:12:35 +02002755static void tcp_chr_update_read_handler(CharDriverState *chr)
2756{
2757 TCPCharDriver *s = chr->opaque;
2758
2759 remove_fd_in_watch(chr);
2760 if (s->chan) {
2761 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2762 tcp_chr_read, chr);
2763 }
2764}
2765
aliguori6f97dba2008-10-31 18:49:55 +00002766#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2767static void tcp_chr_telnet_init(int fd)
2768{
2769 char buf[3];
2770 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2771 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2772 send(fd, (char *)buf, 3, 0);
2773 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2774 send(fd, (char *)buf, 3, 0);
2775 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2776 send(fd, (char *)buf, 3, 0);
2777 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2778 send(fd, (char *)buf, 3, 0);
2779}
2780
Daniel P. Berrange13661082011-06-23 13:31:42 +01002781static int tcp_chr_add_client(CharDriverState *chr, int fd)
2782{
2783 TCPCharDriver *s = chr->opaque;
2784 if (s->fd != -1)
2785 return -1;
2786
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002787 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002788 if (s->do_nodelay)
2789 socket_set_nodelay(fd);
2790 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302791 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002792 if (s->listen_tag) {
2793 g_source_remove(s->listen_tag);
2794 s->listen_tag = 0;
2795 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002796 tcp_chr_connect(chr);
2797
2798 return 0;
2799}
2800
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302801static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002802{
2803 CharDriverState *chr = opaque;
2804 TCPCharDriver *s = chr->opaque;
2805 struct sockaddr_in saddr;
2806#ifndef _WIN32
2807 struct sockaddr_un uaddr;
2808#endif
2809 struct sockaddr *addr;
2810 socklen_t len;
2811 int fd;
2812
2813 for(;;) {
2814#ifndef _WIN32
2815 if (s->is_unix) {
2816 len = sizeof(uaddr);
2817 addr = (struct sockaddr *)&uaddr;
2818 } else
2819#endif
2820 {
2821 len = sizeof(saddr);
2822 addr = (struct sockaddr *)&saddr;
2823 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002824 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002825 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002826 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302827 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002828 } else if (fd >= 0) {
2829 if (s->do_telnetopt)
2830 tcp_chr_telnet_init(fd);
2831 break;
2832 }
2833 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002834 if (tcp_chr_add_client(chr, fd) < 0)
2835 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302836
2837 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002838}
2839
2840static void tcp_chr_close(CharDriverState *chr)
2841{
2842 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002843 int i;
aliguori819f56b2009-03-28 17:58:14 +00002844 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302845 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302846 if (s->chan) {
2847 g_io_channel_unref(s->chan);
2848 }
aliguori6f97dba2008-10-31 18:49:55 +00002849 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002850 }
2851 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302852 if (s->listen_tag) {
2853 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002854 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302855 }
2856 if (s->listen_chan) {
2857 g_io_channel_unref(s->listen_chan);
2858 }
aliguori6f97dba2008-10-31 18:49:55 +00002859 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002860 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002861 if (s->read_msgfds_num) {
2862 for (i = 0; i < s->read_msgfds_num; i++) {
2863 close(s->read_msgfds[i]);
2864 }
2865 g_free(s->read_msgfds);
2866 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002867 if (s->write_msgfds_num) {
2868 g_free(s->write_msgfds);
2869 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002870 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002871 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002872}
2873
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002874static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2875 bool is_listen, bool is_telnet,
2876 bool is_waitconnect,
2877 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002878{
2879 CharDriverState *chr = NULL;
2880 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002881 char host[NI_MAXHOST], serv[NI_MAXSERV];
2882 const char *left = "", *right = "";
2883 struct sockaddr_storage ss;
2884 socklen_t ss_len = sizeof(ss);
2885
2886 memset(&ss, 0, ss_len);
2887 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002888 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002889 return NULL;
2890 }
2891
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002892 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002893 s = g_malloc0(sizeof(TCPCharDriver));
2894
2895 s->connected = 0;
2896 s->fd = -1;
2897 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002898 s->read_msgfds = 0;
2899 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002900 s->write_msgfds = 0;
2901 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002902
2903 chr->filename = g_malloc(256);
2904 switch (ss.ss_family) {
2905#ifndef _WIN32
2906 case AF_UNIX:
2907 s->is_unix = 1;
2908 snprintf(chr->filename, 256, "unix:%s%s",
2909 ((struct sockaddr_un *)(&ss))->sun_path,
2910 is_listen ? ",server" : "");
2911 break;
2912#endif
2913 case AF_INET6:
2914 left = "[";
2915 right = "]";
2916 /* fall through */
2917 case AF_INET:
2918 s->do_nodelay = do_nodelay;
2919 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2920 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002921 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002922 is_telnet ? "telnet" : "tcp",
2923 left, host, right, serv,
2924 is_listen ? ",server" : "");
2925 break;
2926 }
2927
2928 chr->opaque = s;
2929 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002930 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002931 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002932 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002933 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002934 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302935 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002936 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002937 /* be isn't opened until we get a connection */
2938 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002939
2940 if (is_listen) {
2941 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302942 s->listen_chan = io_channel_from_socket(s->listen_fd);
2943 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002944 if (is_telnet) {
2945 s->do_telnetopt = 1;
2946 }
2947 } else {
2948 s->connected = 1;
2949 s->fd = fd;
2950 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302951 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002952 tcp_chr_connect(chr);
2953 }
2954
2955 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002956 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2957 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302958 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002959 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002960 }
2961 return chr;
2962}
2963
2964static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2965{
2966 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002967 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002968 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002969
liguange990a392013-06-18 11:45:35 +08002970 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2971 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2972 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2973 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2974 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002975
aliguorif07b6002008-11-11 20:54:09 +00002976 if (is_unix) {
2977 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002978 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002979 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002980 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002981 }
2982 } else {
2983 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002984 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002985 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002986 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002987 }
2988 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002989 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002990 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002991 }
aliguori6f97dba2008-10-31 18:49:55 +00002992
2993 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002994 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002995
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002996 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2997 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002998 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002999 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00003000 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003001 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003002
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003003
aliguori6f97dba2008-10-31 18:49:55 +00003004 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003005 if (local_err) {
3006 qerror_report_err(local_err);
3007 error_free(local_err);
3008 }
3009 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00003010 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003011 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003012 if (chr) {
3013 g_free(chr->opaque);
3014 g_free(chr);
3015 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003016 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003017}
3018
Lei Li51767e72013-01-25 00:03:19 +08003019/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003020/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003021
3022typedef struct {
3023 size_t size;
3024 size_t prod;
3025 size_t cons;
3026 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003027} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003028
Markus Armbruster3949e592013-02-06 21:27:24 +01003029static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003030{
Markus Armbruster3949e592013-02-06 21:27:24 +01003031 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003032
Markus Armbruster5c230102013-02-06 21:27:23 +01003033 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003034}
3035
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003036/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003037static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003038{
Markus Armbruster3949e592013-02-06 21:27:24 +01003039 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003040 int i;
3041
3042 if (!buf || (len < 0)) {
3043 return -1;
3044 }
3045
3046 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003047 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3048 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003049 d->cons = d->prod - d->size;
3050 }
3051 }
3052
3053 return 0;
3054}
3055
Markus Armbruster3949e592013-02-06 21:27:24 +01003056static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003057{
Markus Armbruster3949e592013-02-06 21:27:24 +01003058 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003059 int i;
3060
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003061 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003062 for (i = 0; i < len && d->cons != d->prod; i++) {
3063 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003064 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003065 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003066
3067 return i;
3068}
3069
Markus Armbruster3949e592013-02-06 21:27:24 +01003070static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003071{
Markus Armbruster3949e592013-02-06 21:27:24 +01003072 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003073
3074 g_free(d->cbuf);
3075 g_free(d);
3076 chr->opaque = NULL;
3077}
3078
Markus Armbruster4f573782013-07-26 16:44:32 +02003079static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3080 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003081{
3082 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003083 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003084
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003085 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003086 d = g_malloc(sizeof(*d));
3087
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003088 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003089
3090 /* The size must be power of 2 */
3091 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003092 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003093 goto fail;
3094 }
3095
3096 d->prod = 0;
3097 d->cons = 0;
3098 d->cbuf = g_malloc0(d->size);
3099
3100 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003101 chr->chr_write = ringbuf_chr_write;
3102 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003103
3104 return chr;
3105
3106fail:
3107 g_free(d);
3108 g_free(chr);
3109 return NULL;
3110}
3111
Hani Benhabiles8e597772014-05-27 23:39:30 +01003112bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003113{
Markus Armbruster3949e592013-02-06 21:27:24 +01003114 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003115}
3116
Markus Armbruster3949e592013-02-06 21:27:24 +01003117void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003118 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003119 Error **errp)
3120{
3121 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003122 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003123 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003124 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003125
3126 chr = qemu_chr_find(device);
3127 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003128 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003129 return;
3130 }
3131
Markus Armbruster3949e592013-02-06 21:27:24 +01003132 if (!chr_is_ringbuf(chr)) {
3133 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003134 return;
3135 }
3136
Lei Li1f590cf2013-01-25 00:03:20 +08003137 if (has_format && (format == DATA_FORMAT_BASE64)) {
3138 write_data = g_base64_decode(data, &write_count);
3139 } else {
3140 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003141 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003142 }
3143
Markus Armbruster3949e592013-02-06 21:27:24 +01003144 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003145
Markus Armbruster13289fb2013-02-06 21:27:18 +01003146 if (write_data != (uint8_t *)data) {
3147 g_free((void *)write_data);
3148 }
3149
Lei Li1f590cf2013-01-25 00:03:20 +08003150 if (ret < 0) {
3151 error_setg(errp, "Failed to write to device %s", device);
3152 return;
3153 }
3154}
3155
Markus Armbruster3949e592013-02-06 21:27:24 +01003156char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003157 bool has_format, enum DataFormat format,
3158 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003159{
3160 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003161 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003162 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003163 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003164
3165 chr = qemu_chr_find(device);
3166 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003167 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003168 return NULL;
3169 }
3170
Markus Armbruster3949e592013-02-06 21:27:24 +01003171 if (!chr_is_ringbuf(chr)) {
3172 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003173 return NULL;
3174 }
3175
3176 if (size <= 0) {
3177 error_setg(errp, "size must be greater than zero");
3178 return NULL;
3179 }
3180
Markus Armbruster3949e592013-02-06 21:27:24 +01003181 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003182 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003183 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003184
Markus Armbruster3949e592013-02-06 21:27:24 +01003185 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003186
3187 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003188 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003189 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003190 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003191 /*
3192 * FIXME should read only complete, valid UTF-8 characters up
3193 * to @size bytes. Invalid sequences should be replaced by a
3194 * suitable replacement character. Except when (and only
3195 * when) ring buffer lost characters since last read, initial
3196 * continuation characters should be dropped.
3197 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003198 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003199 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003200 }
3201
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003202 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003203}
3204
Gerd Hoffmann33521632009-12-08 13:11:49 +01003205QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003206{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003207 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003208 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003209 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003210 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003211 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003212
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003213 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003214 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003215 qerror_report_err(local_err);
3216 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003217 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003218 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003219
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003220 if (strstart(filename, "mon:", &p)) {
3221 filename = p;
3222 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003223 if (strcmp(filename, "stdio") == 0) {
3224 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3225 * but pass it to the guest. Handle this only for compat syntax,
3226 * for -chardev syntax we have special option for this.
3227 * This is what -nographic did, redirecting+muxing serial+monitor
3228 * to stdio causing Ctrl+C to be passed to guest. */
3229 qemu_opt_set(opts, "signal", "off");
3230 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003231 }
3232
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003233 if (strcmp(filename, "null") == 0 ||
3234 strcmp(filename, "pty") == 0 ||
3235 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003236 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003237 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003238 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003239 return opts;
3240 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003241 if (strstart(filename, "vc", &p)) {
3242 qemu_opt_set(opts, "backend", "vc");
3243 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003244 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003245 /* pixels */
3246 qemu_opt_set(opts, "width", width);
3247 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003248 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003249 /* chars */
3250 qemu_opt_set(opts, "cols", width);
3251 qemu_opt_set(opts, "rows", height);
3252 } else {
3253 goto fail;
3254 }
3255 }
3256 return opts;
3257 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003258 if (strcmp(filename, "con:") == 0) {
3259 qemu_opt_set(opts, "backend", "console");
3260 return opts;
3261 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003262 if (strstart(filename, "COM", NULL)) {
3263 qemu_opt_set(opts, "backend", "serial");
3264 qemu_opt_set(opts, "path", filename);
3265 return opts;
3266 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003267 if (strstart(filename, "file:", &p)) {
3268 qemu_opt_set(opts, "backend", "file");
3269 qemu_opt_set(opts, "path", p);
3270 return opts;
3271 }
3272 if (strstart(filename, "pipe:", &p)) {
3273 qemu_opt_set(opts, "backend", "pipe");
3274 qemu_opt_set(opts, "path", p);
3275 return opts;
3276 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003277 if (strstart(filename, "tcp:", &p) ||
3278 strstart(filename, "telnet:", &p)) {
3279 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3280 host[0] = 0;
3281 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3282 goto fail;
3283 }
3284 qemu_opt_set(opts, "backend", "socket");
3285 qemu_opt_set(opts, "host", host);
3286 qemu_opt_set(opts, "port", port);
3287 if (p[pos] == ',') {
3288 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3289 goto fail;
3290 }
3291 if (strstart(filename, "telnet:", &p))
3292 qemu_opt_set(opts, "telnet", "on");
3293 return opts;
3294 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003295 if (strstart(filename, "udp:", &p)) {
3296 qemu_opt_set(opts, "backend", "udp");
3297 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3298 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003299 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003300 goto fail;
3301 }
3302 }
3303 qemu_opt_set(opts, "host", host);
3304 qemu_opt_set(opts, "port", port);
3305 if (p[pos] == '@') {
3306 p += pos + 1;
3307 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3308 host[0] = 0;
3309 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003310 goto fail;
3311 }
3312 }
3313 qemu_opt_set(opts, "localaddr", host);
3314 qemu_opt_set(opts, "localport", port);
3315 }
3316 return opts;
3317 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003318 if (strstart(filename, "unix:", &p)) {
3319 qemu_opt_set(opts, "backend", "socket");
3320 if (qemu_opts_do_parse(opts, p, "path") != 0)
3321 goto fail;
3322 return opts;
3323 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003324 if (strstart(filename, "/dev/parport", NULL) ||
3325 strstart(filename, "/dev/ppi", NULL)) {
3326 qemu_opt_set(opts, "backend", "parport");
3327 qemu_opt_set(opts, "path", filename);
3328 return opts;
3329 }
3330 if (strstart(filename, "/dev/", NULL)) {
3331 qemu_opt_set(opts, "backend", "tty");
3332 qemu_opt_set(opts, "path", filename);
3333 return opts;
3334 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003335
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003336fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003337 qemu_opts_del(opts);
3338 return NULL;
3339}
3340
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003341static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3342 Error **errp)
3343{
3344 const char *path = qemu_opt_get(opts, "path");
3345
3346 if (path == NULL) {
3347 error_setg(errp, "chardev: file: no filename given");
3348 return;
3349 }
3350 backend->file = g_new0(ChardevFile, 1);
3351 backend->file->out = g_strdup(path);
3352}
3353
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003354static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3355 Error **errp)
3356{
3357 backend->stdio = g_new0(ChardevStdio, 1);
3358 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003359 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003360}
3361
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003362static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3363 Error **errp)
3364{
3365 const char *device = qemu_opt_get(opts, "path");
3366
3367 if (device == NULL) {
3368 error_setg(errp, "chardev: serial/tty: no device path given");
3369 return;
3370 }
3371 backend->serial = g_new0(ChardevHostdev, 1);
3372 backend->serial->device = g_strdup(device);
3373}
3374
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003375static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3376 Error **errp)
3377{
3378 const char *device = qemu_opt_get(opts, "path");
3379
3380 if (device == NULL) {
3381 error_setg(errp, "chardev: parallel: no device path given");
3382 return;
3383 }
3384 backend->parallel = g_new0(ChardevHostdev, 1);
3385 backend->parallel->device = g_strdup(device);
3386}
3387
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003388static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3389 Error **errp)
3390{
3391 const char *device = qemu_opt_get(opts, "path");
3392
3393 if (device == NULL) {
3394 error_setg(errp, "chardev: pipe: no device path given");
3395 return;
3396 }
3397 backend->pipe = g_new0(ChardevHostdev, 1);
3398 backend->pipe->device = g_strdup(device);
3399}
3400
Markus Armbruster4f573782013-07-26 16:44:32 +02003401static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3402 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003403{
3404 int val;
3405
Markus Armbruster3a1da422013-07-26 16:44:34 +02003406 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003407
Markus Armbruster0f953052013-06-27 16:22:07 +02003408 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003409 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003410 backend->ringbuf->has_size = true;
3411 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003412 }
3413}
3414
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003415static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3416 Error **errp)
3417{
3418 const char *chardev = qemu_opt_get(opts, "chardev");
3419
3420 if (chardev == NULL) {
3421 error_setg(errp, "chardev: mux: no chardev given");
3422 return;
3423 }
3424 backend->mux = g_new0(ChardevMux, 1);
3425 backend->mux->chardev = g_strdup(chardev);
3426}
3427
Anthony Liguorid654f342013-03-05 23:21:28 +05303428typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003429 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003430 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003431 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003432 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003433 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003434 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303435} CharDriver;
3436
3437static GSList *backends;
3438
3439void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3440{
3441 CharDriver *s;
3442
3443 s = g_malloc0(sizeof(*s));
3444 s->name = g_strdup(name);
3445 s->open = open;
3446
3447 backends = g_slist_append(backends, s);
3448}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003449
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003450void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003451 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3452{
3453 CharDriver *s;
3454
3455 s = g_malloc0(sizeof(*s));
3456 s->name = g_strdup(name);
3457 s->kind = kind;
3458 s->parse = parse;
3459
3460 backends = g_slist_append(backends, s);
3461}
3462
Anthony Liguorif69554b2011-08-15 11:17:37 -05003463CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003464 void (*init)(struct CharDriverState *s),
3465 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003466{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003467 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303468 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003469 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303470 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003471
3472 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003473 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003474 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003475 }
3476
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003477 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003478 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003479 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003480 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003481 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303482 for (i = backends; i; i = i->next) {
3483 cd = i->data;
3484
3485 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003486 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303487 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003488 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303489 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003490 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003491 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003492 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003493 }
3494
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003495 if (!cd->open) {
3496 /* using new, qapi init */
3497 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3498 ChardevReturn *ret = NULL;
3499 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003500 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003501
3502 if (qemu_opt_get_bool(opts, "mux", 0)) {
3503 bid = g_strdup_printf("%s-base", id);
3504 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003505
3506 chr = NULL;
3507 backend->kind = cd->kind;
3508 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003509 cd->parse(opts, backend, &local_err);
3510 if (local_err) {
3511 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003512 goto qapi_out;
3513 }
3514 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003515 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003516 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003517 goto qapi_out;
3518 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003519
3520 if (bid) {
3521 qapi_free_ChardevBackend(backend);
3522 qapi_free_ChardevReturn(ret);
3523 backend = g_new0(ChardevBackend, 1);
3524 backend->mux = g_new0(ChardevMux, 1);
3525 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3526 backend->mux->chardev = g_strdup(bid);
3527 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003528 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003529 chr = qemu_chr_find(bid);
3530 qemu_chr_delete(chr);
3531 chr = NULL;
3532 goto qapi_out;
3533 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003534 }
3535
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003536 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003537 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003538
3539 qapi_out:
3540 qapi_free_ChardevBackend(backend);
3541 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003542 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003543 return chr;
3544 }
3545
Anthony Liguorid654f342013-03-05 23:21:28 +05303546 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003547 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003548 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003549 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003550 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003551 }
3552
3553 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003554 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003555 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003556 /* if we didn't create the chardev via qmp_chardev_add, we
3557 * need to send the OPENED event here
3558 */
3559 if (!chr->explicit_be_open) {
3560 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3561 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003562 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003563
3564 if (qemu_opt_get_bool(opts, "mux", 0)) {
3565 CharDriverState *base = chr;
3566 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003567 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003568 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3569 chr = qemu_chr_open_mux(base);
3570 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003571 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003572 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003573 } else {
3574 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003575 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003576 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003577 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003578 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003579
3580err:
3581 qemu_opts_del(opts);
3582 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003583}
3584
Anthony Liguori27143a42011-08-15 11:17:36 -05003585CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003586{
3587 const char *p;
3588 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003589 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003590 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003591
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003592 if (strstart(filename, "chardev:", &p)) {
3593 return qemu_chr_find(p);
3594 }
3595
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003596 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003597 if (!opts)
3598 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003599
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003600 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003601 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003602 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003603 error_free(err);
3604 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003605 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003606 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003607 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003608 }
3609 return chr;
3610}
3611
Anthony Liguori15f31512011-08-15 11:17:35 -05003612void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003613{
3614 if (chr->chr_set_echo) {
3615 chr->chr_set_echo(chr, echo);
3616 }
3617}
3618
Hans de Goede8e25daa2013-03-26 11:07:57 +01003619void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003620{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003621 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003622 return;
3623 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003624 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003625 if (chr->chr_set_fe_open) {
3626 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003627 }
3628}
3629
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003630void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3631{
3632 if (chr->chr_fe_event) {
3633 chr->chr_fe_event(chr, event);
3634 }
3635}
3636
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003637int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3638 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303639{
3640 GSource *src;
3641 guint tag;
3642
3643 if (s->chr_add_watch == NULL) {
3644 return -ENOSYS;
3645 }
3646
3647 src = s->chr_add_watch(s, cond);
3648 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3649 tag = g_source_attach(src, NULL);
3650 g_source_unref(src);
3651
3652 return tag;
3653}
3654
Hans de Goede44c473d2013-03-27 20:29:39 +01003655int qemu_chr_fe_claim(CharDriverState *s)
3656{
3657 if (s->avail_connections < 1) {
3658 return -1;
3659 }
3660 s->avail_connections--;
3661 return 0;
3662}
3663
3664void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3665{
3666 if (qemu_chr_fe_claim(s) != 0) {
3667 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3668 __func__, s->label);
3669 exit(1);
3670 }
3671}
3672
3673void qemu_chr_fe_release(CharDriverState *s)
3674{
3675 s->avail_connections++;
3676}
3677
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003678void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003679{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003680 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003681 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003682 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003683 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003684 g_free(chr->filename);
3685 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003686 if (chr->opts) {
3687 qemu_opts_del(chr->opts);
3688 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003689 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003690}
3691
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003692ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003693{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003694 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003695 CharDriverState *chr;
3696
Blue Swirl72cf2d42009-09-12 07:36:22 +00003697 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003698 ChardevInfoList *info = g_malloc0(sizeof(*info));
3699 info->value = g_malloc0(sizeof(*info->value));
3700 info->value->label = g_strdup(chr->label);
3701 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003702 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003703
3704 info->next = chr_list;
3705 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003706 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003707
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003708 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003709}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003710
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003711ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3712{
3713 ChardevBackendInfoList *backend_list = NULL;
3714 CharDriver *c = NULL;
3715 GSList *i = NULL;
3716
3717 for (i = backends; i; i = i->next) {
3718 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3719 c = i->data;
3720 info->value = g_malloc0(sizeof(*info->value));
3721 info->value->name = g_strdup(c->name);
3722
3723 info->next = backend_list;
3724 backend_list = info;
3725 }
3726
3727 return backend_list;
3728}
3729
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003730CharDriverState *qemu_chr_find(const char *name)
3731{
3732 CharDriverState *chr;
3733
Blue Swirl72cf2d42009-09-12 07:36:22 +00003734 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003735 if (strcmp(chr->label, name) != 0)
3736 continue;
3737 return chr;
3738 }
3739 return NULL;
3740}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003741
3742/* Get a character (serial) device interface. */
3743CharDriverState *qemu_char_get_next_serial(void)
3744{
3745 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003746 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003747
3748 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003749
3750 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3751 chr = serial_hds[next_serial++];
3752 qemu_chr_fe_claim_no_fail(chr);
3753 return chr;
3754 }
3755 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003756}
3757
Paolo Bonzini4d454572012-11-26 16:03:42 +01003758QemuOptsList qemu_chardev_opts = {
3759 .name = "chardev",
3760 .implied_opt_name = "backend",
3761 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3762 .desc = {
3763 {
3764 .name = "backend",
3765 .type = QEMU_OPT_STRING,
3766 },{
3767 .name = "path",
3768 .type = QEMU_OPT_STRING,
3769 },{
3770 .name = "host",
3771 .type = QEMU_OPT_STRING,
3772 },{
3773 .name = "port",
3774 .type = QEMU_OPT_STRING,
3775 },{
3776 .name = "localaddr",
3777 .type = QEMU_OPT_STRING,
3778 },{
3779 .name = "localport",
3780 .type = QEMU_OPT_STRING,
3781 },{
3782 .name = "to",
3783 .type = QEMU_OPT_NUMBER,
3784 },{
3785 .name = "ipv4",
3786 .type = QEMU_OPT_BOOL,
3787 },{
3788 .name = "ipv6",
3789 .type = QEMU_OPT_BOOL,
3790 },{
3791 .name = "wait",
3792 .type = QEMU_OPT_BOOL,
3793 },{
3794 .name = "server",
3795 .type = QEMU_OPT_BOOL,
3796 },{
3797 .name = "delay",
3798 .type = QEMU_OPT_BOOL,
3799 },{
3800 .name = "telnet",
3801 .type = QEMU_OPT_BOOL,
3802 },{
3803 .name = "width",
3804 .type = QEMU_OPT_NUMBER,
3805 },{
3806 .name = "height",
3807 .type = QEMU_OPT_NUMBER,
3808 },{
3809 .name = "cols",
3810 .type = QEMU_OPT_NUMBER,
3811 },{
3812 .name = "rows",
3813 .type = QEMU_OPT_NUMBER,
3814 },{
3815 .name = "mux",
3816 .type = QEMU_OPT_BOOL,
3817 },{
3818 .name = "signal",
3819 .type = QEMU_OPT_BOOL,
3820 },{
3821 .name = "name",
3822 .type = QEMU_OPT_STRING,
3823 },{
3824 .name = "debug",
3825 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003826 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003827 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003828 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003829 },{
3830 .name = "chardev",
3831 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003832 },
3833 { /* end of list */ }
3834 },
3835};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003836
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003837#ifdef _WIN32
3838
3839static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3840{
3841 HANDLE out;
3842
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003843 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003844 error_setg(errp, "input file not supported");
3845 return NULL;
3846 }
3847
3848 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3849 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3850 if (out == INVALID_HANDLE_VALUE) {
3851 error_setg(errp, "open %s failed", file->out);
3852 return NULL;
3853 }
3854 return qemu_chr_open_win_file(out);
3855}
3856
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003857static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3858 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003859{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003860 return qemu_chr_open_win_path(serial->device);
3861}
3862
3863static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3864 Error **errp)
3865{
3866 error_setg(errp, "character device backend type 'parallel' not supported");
3867 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003868}
3869
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003870#else /* WIN32 */
3871
3872static int qmp_chardev_open_file_source(char *src, int flags,
3873 Error **errp)
3874{
3875 int fd = -1;
3876
3877 TFR(fd = qemu_open(src, flags, 0666));
3878 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003879 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003880 }
3881 return fd;
3882}
3883
3884static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3885{
Markus Armbruster5f758362014-05-19 18:57:34 +02003886 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003887
3888 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3889 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003890 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003891 return NULL;
3892 }
3893
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003894 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003895 flags = O_RDONLY;
3896 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003897 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003898 qemu_close(out);
3899 return NULL;
3900 }
3901 }
3902
3903 return qemu_chr_open_fd(in, out);
3904}
3905
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003906static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3907 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003908{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003909#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003910 int fd;
3911
3912 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003913 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003914 return NULL;
3915 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003916 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003917 return qemu_chr_open_tty_fd(fd);
3918#else
3919 error_setg(errp, "character device backend type 'serial' not supported");
3920 return NULL;
3921#endif
3922}
3923
3924static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3925 Error **errp)
3926{
3927#ifdef HAVE_CHARDEV_PARPORT
3928 int fd;
3929
3930 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003931 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003932 return NULL;
3933 }
3934 return qemu_chr_open_pp_fd(fd);
3935#else
3936 error_setg(errp, "character device backend type 'parallel' not supported");
3937 return NULL;
3938#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003939}
3940
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003941#endif /* WIN32 */
3942
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003943static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3944 Error **errp)
3945{
3946 SocketAddress *addr = sock->addr;
3947 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3948 bool is_listen = sock->has_server ? sock->server : true;
3949 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3950 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3951 int fd;
3952
3953 if (is_listen) {
3954 fd = socket_listen(addr, errp);
3955 } else {
3956 fd = socket_connect(addr, errp, NULL, NULL);
3957 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003958 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003959 return NULL;
3960 }
3961 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3962 is_telnet, is_waitconnect, errp);
3963}
3964
Lei Li08d0ab32013-05-20 14:51:03 +08003965static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3966 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003967{
3968 int fd;
3969
Lei Li08d0ab32013-05-20 14:51:03 +08003970 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003971 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003972 return NULL;
3973 }
3974 return qemu_chr_open_udp_fd(fd);
3975}
3976
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003977ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3978 Error **errp)
3979{
3980 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003981 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003982
3983 chr = qemu_chr_find(id);
3984 if (chr) {
3985 error_setg(errp, "Chardev '%s' already exists", id);
3986 g_free(ret);
3987 return NULL;
3988 }
3989
3990 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003991 case CHARDEV_BACKEND_KIND_FILE:
3992 chr = qmp_chardev_open_file(backend->file, errp);
3993 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003994 case CHARDEV_BACKEND_KIND_SERIAL:
3995 chr = qmp_chardev_open_serial(backend->serial, errp);
3996 break;
3997 case CHARDEV_BACKEND_KIND_PARALLEL:
3998 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003999 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004000 case CHARDEV_BACKEND_KIND_PIPE:
4001 chr = qemu_chr_open_pipe(backend->pipe);
4002 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004003 case CHARDEV_BACKEND_KIND_SOCKET:
4004 chr = qmp_chardev_open_socket(backend->socket, errp);
4005 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004006 case CHARDEV_BACKEND_KIND_UDP:
4007 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004008 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004009#ifdef HAVE_CHARDEV_TTY
4010 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004011 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004012 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004013#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004014 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004015 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004016 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004017 case CHARDEV_BACKEND_KIND_MUX:
4018 base = qemu_chr_find(backend->mux->chardev);
4019 if (base == NULL) {
4020 error_setg(errp, "mux: base chardev %s not found",
4021 backend->mux->chardev);
4022 break;
4023 }
4024 chr = qemu_chr_open_mux(base);
4025 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004026 case CHARDEV_BACKEND_KIND_MSMOUSE:
4027 chr = qemu_chr_open_msmouse();
4028 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004029#ifdef CONFIG_BRLAPI
4030 case CHARDEV_BACKEND_KIND_BRAILLE:
4031 chr = chr_baum_init();
4032 break;
4033#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004034 case CHARDEV_BACKEND_KIND_STDIO:
4035 chr = qemu_chr_open_stdio(backend->stdio);
4036 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004037#ifdef _WIN32
4038 case CHARDEV_BACKEND_KIND_CONSOLE:
4039 chr = qemu_chr_open_win_con();
4040 break;
4041#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004042#ifdef CONFIG_SPICE
4043 case CHARDEV_BACKEND_KIND_SPICEVMC:
4044 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4045 break;
4046 case CHARDEV_BACKEND_KIND_SPICEPORT:
4047 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4048 break;
4049#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004050 case CHARDEV_BACKEND_KIND_VC:
4051 chr = vc_init(backend->vc);
4052 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004053 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004054 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004055 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004056 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004057 default:
4058 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4059 break;
4060 }
4061
Markus Armbruster3894c782014-05-19 18:57:36 +02004062 /*
4063 * Character backend open hasn't been fully converted to the Error
4064 * API. Some opens fail without setting an error. Set a generic
4065 * error then.
4066 * TODO full conversion to Error API
4067 */
4068 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004069 error_setg(errp, "Failed to create chardev");
4070 }
4071 if (chr) {
4072 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004073 chr->avail_connections =
4074 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004075 if (!chr->filename) {
4076 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4077 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004078 if (!chr->explicit_be_open) {
4079 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4080 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004081 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4082 return ret;
4083 } else {
4084 g_free(ret);
4085 return NULL;
4086 }
4087}
4088
4089void qmp_chardev_remove(const char *id, Error **errp)
4090{
4091 CharDriverState *chr;
4092
4093 chr = qemu_chr_find(id);
4094 if (NULL == chr) {
4095 error_setg(errp, "Chardev '%s' not found", id);
4096 return;
4097 }
4098 if (chr->chr_can_read || chr->chr_read ||
4099 chr->chr_event || chr->handler_opaque) {
4100 error_setg(errp, "Chardev '%s' is busy", id);
4101 return;
4102 }
4103 qemu_chr_delete(chr);
4104}
Anthony Liguorid654f342013-03-05 23:21:28 +05304105
4106static void register_types(void)
4107{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004108 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304109 register_char_driver("socket", qemu_chr_open_socket);
4110 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004111 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004112 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004113 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4114 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004115 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4116 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004117 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4118 qemu_chr_parse_serial);
4119 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4120 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004121 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4122 qemu_chr_parse_parallel);
4123 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4124 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004125 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004126 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004127 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4128 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004129 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4130 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004131 /* Bug-compatibility: */
4132 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4133 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004134 /* this must be done after machine init, since we register FEs with muxes
4135 * as part of realize functions like serial_isa_realizefn when -nographic
4136 * is specified
4137 */
4138 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304139}
4140
4141type_init(register_types);