blob: cbd6b9a025b61480dad5b28d56ce91828a0cc690 [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
aliguori6f97dba2008-10-31 18:49:55 +0000584static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
585{
586 CharDriverState *chr;
587 MuxDriver *d;
588
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200589 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500590 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000591
592 chr->opaque = d;
593 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200594 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000595 chr->chr_write = mux_chr_write;
596 chr->chr_update_read_handler = mux_chr_update_read_handler;
597 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100598 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100599 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500600 /* only default to opened state if we've realized the initial
601 * set of muxes
602 */
603 chr->explicit_be_open = muxes_realized ? 0 : 1;
604 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200605
aliguori6f97dba2008-10-31 18:49:55 +0000606 return chr;
607}
608
609
610#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000611int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000612{
613 int ret, len;
614
615 len = len1;
616 while (len > 0) {
617 ret = send(fd, buf, len, 0);
618 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000619 errno = WSAGetLastError();
620 if (errno != WSAEWOULDBLOCK) {
621 return -1;
622 }
623 } else if (ret == 0) {
624 break;
625 } else {
626 buf += ret;
627 len -= ret;
628 }
629 }
630 return len1 - len;
631}
632
633#else
634
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100635int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000636{
637 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100638 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000639
640 len = len1;
641 while (len > 0) {
642 ret = write(fd, buf, len);
643 if (ret < 0) {
644 if (errno != EINTR && errno != EAGAIN)
645 return -1;
646 } else if (ret == 0) {
647 break;
648 } else {
649 buf += ret;
650 len -= ret;
651 }
652 }
653 return len1 - len;
654}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500655
656int recv_all(int fd, void *_buf, int len1, bool single_read)
657{
658 int ret, len;
659 uint8_t *buf = _buf;
660
661 len = len1;
662 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
663 if (ret < 0) {
664 if (errno != EINTR && errno != EAGAIN) {
665 return -1;
666 }
667 continue;
668 } else {
669 if (single_read) {
670 return ret;
671 }
672 buf += ret;
673 len -= ret;
674 }
675 }
676 return len1 - len;
677}
678
aliguori6f97dba2008-10-31 18:49:55 +0000679#endif /* !_WIN32 */
680
Anthony Liguori96c63842013-03-05 23:21:18 +0530681typedef struct IOWatchPoll
682{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200683 GSource parent;
684
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200685 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530686 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530687
688 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200689 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530690 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530691} IOWatchPoll;
692
Anthony Liguori96c63842013-03-05 23:21:18 +0530693static IOWatchPoll *io_watch_poll_from_source(GSource *source)
694{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200695 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530696}
697
698static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
699{
700 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200701 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200702 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200703 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530704 return FALSE;
705 }
706
Paolo Bonzinid185c092013-04-05 17:59:33 +0200707 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200708 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
709 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200710 g_source_attach(iwp->src, NULL);
711 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200712 g_source_destroy(iwp->src);
713 g_source_unref(iwp->src);
714 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200715 }
716 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530717}
718
719static gboolean io_watch_poll_check(GSource *source)
720{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200721 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530722}
723
724static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
725 gpointer user_data)
726{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200727 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530728}
729
730static void io_watch_poll_finalize(GSource *source)
731{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200732 /* Due to a glib bug, removing the last reference to a source
733 * inside a finalize callback causes recursive locking (and a
734 * deadlock). This is not a problem inside other callbacks,
735 * including dispatch callbacks, so we call io_remove_watch_poll
736 * to remove this source. At this point, iwp->src must
737 * be NULL, or we would leak it.
738 *
739 * This would be solved much more elegantly by child sources,
740 * but we support older glib versions that do not have them.
741 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530742 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200743 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530744}
745
746static GSourceFuncs io_watch_poll_funcs = {
747 .prepare = io_watch_poll_prepare,
748 .check = io_watch_poll_check,
749 .dispatch = io_watch_poll_dispatch,
750 .finalize = io_watch_poll_finalize,
751};
752
753/* Can only be used for read */
754static guint io_add_watch_poll(GIOChannel *channel,
755 IOCanReadHandler *fd_can_read,
756 GIOFunc fd_read,
757 gpointer user_data)
758{
759 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200760 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530761
Paolo Bonzinid185c092013-04-05 17:59:33 +0200762 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530763 iwp->fd_can_read = fd_can_read;
764 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200765 iwp->channel = channel;
766 iwp->fd_read = (GSourceFunc) fd_read;
767 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530768
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200769 tag = g_source_attach(&iwp->parent, NULL);
770 g_source_unref(&iwp->parent);
771 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530772}
773
Paolo Bonzini2b316772013-04-19 17:32:09 +0200774static void io_remove_watch_poll(guint tag)
775{
776 GSource *source;
777 IOWatchPoll *iwp;
778
779 g_return_if_fail (tag > 0);
780
781 source = g_main_context_find_source_by_id(NULL, tag);
782 g_return_if_fail (source != NULL);
783
784 iwp = io_watch_poll_from_source(source);
785 if (iwp->src) {
786 g_source_destroy(iwp->src);
787 g_source_unref(iwp->src);
788 iwp->src = NULL;
789 }
790 g_source_destroy(&iwp->parent);
791}
792
Amit Shah26da70c2013-08-28 15:23:37 +0530793static void remove_fd_in_watch(CharDriverState *chr)
794{
795 if (chr->fd_in_tag) {
796 io_remove_watch_poll(chr->fd_in_tag);
797 chr->fd_in_tag = 0;
798 }
799}
800
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000801#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530802static GIOChannel *io_channel_from_fd(int fd)
803{
804 GIOChannel *chan;
805
806 if (fd == -1) {
807 return NULL;
808 }
809
810 chan = g_io_channel_unix_new(fd);
811
812 g_io_channel_set_encoding(chan, NULL, NULL);
813 g_io_channel_set_buffered(chan, FALSE);
814
815 return chan;
816}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000817#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530818
Anthony Liguori76a96442013-03-05 23:21:21 +0530819static GIOChannel *io_channel_from_socket(int fd)
820{
821 GIOChannel *chan;
822
823 if (fd == -1) {
824 return NULL;
825 }
826
827#ifdef _WIN32
828 chan = g_io_channel_win32_new_socket(fd);
829#else
830 chan = g_io_channel_unix_new(fd);
831#endif
832
833 g_io_channel_set_encoding(chan, NULL, NULL);
834 g_io_channel_set_buffered(chan, FALSE);
835
836 return chan;
837}
838
Anthony Liguori684a0962013-03-29 11:39:50 -0500839static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530840{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200841 size_t offset = 0;
842 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530843
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200844 while (offset < len && status == G_IO_STATUS_NORMAL) {
845 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500846
847 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530848 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500849 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530850 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500851
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200852 if (offset > 0) {
853 return offset;
854 }
855 switch (status) {
856 case G_IO_STATUS_NORMAL:
857 g_assert(len == 0);
858 return 0;
859 case G_IO_STATUS_AGAIN:
860 errno = EAGAIN;
861 return -1;
862 default:
863 break;
864 }
865 errno = EINVAL;
866 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530867}
Anthony Liguori96c63842013-03-05 23:21:18 +0530868
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000869#ifndef _WIN32
870
Anthony Liguoria29753f2013-03-05 23:21:19 +0530871typedef struct FDCharDriver {
872 CharDriverState *chr;
873 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000874 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530875 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000876} FDCharDriver;
877
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200878/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000879static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
880{
881 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530882
Anthony Liguori684a0962013-03-29 11:39:50 -0500883 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530884}
885
886static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
887{
888 CharDriverState *chr = opaque;
889 FDCharDriver *s = chr->opaque;
890 int len;
891 uint8_t buf[READ_BUF_LEN];
892 GIOStatus status;
893 gsize bytes_read;
894
895 len = sizeof(buf);
896 if (len > s->max_size) {
897 len = s->max_size;
898 }
899 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200900 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530901 }
902
903 status = g_io_channel_read_chars(chan, (gchar *)buf,
904 len, &bytes_read, NULL);
905 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530906 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530907 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
908 return FALSE;
909 }
910 if (status == G_IO_STATUS_NORMAL) {
911 qemu_chr_be_write(chr, buf, bytes_read);
912 }
913
914 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000915}
916
917static int fd_chr_read_poll(void *opaque)
918{
919 CharDriverState *chr = opaque;
920 FDCharDriver *s = chr->opaque;
921
Anthony Liguori909cda12011-08-15 11:17:31 -0500922 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000923 return s->max_size;
924}
925
Anthony Liguori23673ca2013-03-05 23:21:23 +0530926static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
927{
928 FDCharDriver *s = chr->opaque;
929 return g_io_create_watch(s->fd_out, cond);
930}
931
aliguori6f97dba2008-10-31 18:49:55 +0000932static void fd_chr_update_read_handler(CharDriverState *chr)
933{
934 FDCharDriver *s = chr->opaque;
935
Amit Shah26da70c2013-08-28 15:23:37 +0530936 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530937 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530938 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
939 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000940 }
941}
942
943static void fd_chr_close(struct CharDriverState *chr)
944{
945 FDCharDriver *s = chr->opaque;
946
Amit Shah26da70c2013-08-28 15:23:37 +0530947 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530948 if (s->fd_in) {
949 g_io_channel_unref(s->fd_in);
950 }
951 if (s->fd_out) {
952 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000953 }
954
Anthony Liguori7267c092011-08-20 22:09:37 -0500955 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100956 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000957}
958
959/* open a character device to a unix fd */
960static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
961{
962 CharDriverState *chr;
963 FDCharDriver *s;
964
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200965 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500966 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530967 s->fd_in = io_channel_from_fd(fd_in);
968 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530969 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530970 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000971 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530972 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000973 chr->chr_write = fd_chr_write;
974 chr->chr_update_read_handler = fd_chr_update_read_handler;
975 chr->chr_close = fd_chr_close;
976
aliguori6f97dba2008-10-31 18:49:55 +0000977 return chr;
978}
979
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100980static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000981{
982 int fd_in, fd_out;
983 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100984 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200985
986 if (filename == NULL) {
987 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100988 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200989 }
aliguori6f97dba2008-10-31 18:49:55 +0000990
991 snprintf(filename_in, 256, "%s.in", filename);
992 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100993 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
994 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000995 if (fd_in < 0 || fd_out < 0) {
996 if (fd_in >= 0)
997 close(fd_in);
998 if (fd_out >= 0)
999 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001000 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001001 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001002 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001003 }
aliguori6f97dba2008-10-31 18:49:55 +00001004 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001005 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001006}
1007
aliguori6f97dba2008-10-31 18:49:55 +00001008/* init terminal so that we can grab keys */
1009static struct termios oldtty;
1010static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001011static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001012
1013static void term_exit(void)
1014{
1015 tcsetattr (0, TCSANOW, &oldtty);
1016 fcntl(0, F_SETFL, old_fd0_flags);
1017}
1018
Paolo Bonzinibb002512010-12-23 13:42:50 +01001019static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001020{
1021 struct termios tty;
1022
Paolo Bonzini03693642010-12-23 13:42:49 +01001023 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001024 if (!echo) {
1025 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001026 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001027 tty.c_oflag |= OPOST;
1028 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1029 tty.c_cflag &= ~(CSIZE|PARENB);
1030 tty.c_cflag |= CS8;
1031 tty.c_cc[VMIN] = 1;
1032 tty.c_cc[VTIME] = 0;
1033 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001034 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001035 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001036
1037 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001038}
1039
1040static void qemu_chr_close_stdio(struct CharDriverState *chr)
1041{
1042 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001043 fd_chr_close(chr);
1044}
1045
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001046static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001047{
1048 CharDriverState *chr;
1049
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001050 if (is_daemonized()) {
1051 error_report("cannot use stdio with -daemonize");
1052 return NULL;
1053 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301054 old_fd0_flags = fcntl(0, F_GETFL);
1055 tcgetattr (0, &oldtty);
1056 fcntl(0, F_SETFL, O_NONBLOCK);
1057 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001058
aliguori6f97dba2008-10-31 18:49:55 +00001059 chr = qemu_chr_open_fd(0, 1);
1060 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001061 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001062 if (opts->has_signal) {
1063 stdio_allow_signal = opts->signal;
1064 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001065 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001066
Markus Armbruster1f514702012-02-07 15:09:08 +01001067 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001068}
1069
aliguori6f97dba2008-10-31 18:49:55 +00001070#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001071 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1072 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001073
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001074#define HAVE_CHARDEV_TTY 1
1075
aliguori6f97dba2008-10-31 18:49:55 +00001076typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301077 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001078 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001079
1080 /* Protected by the CharDriverState chr_write_lock. */
1081 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301082 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001083} PtyCharDriver;
1084
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001085static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001086static void pty_chr_state(CharDriverState *chr, int connected);
1087
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301088static gboolean pty_chr_timer(gpointer opaque)
1089{
1090 struct CharDriverState *chr = opaque;
1091 PtyCharDriver *s = chr->opaque;
1092
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001093 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001094 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001095 if (!s->connected) {
1096 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001097 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001098 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001099 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301100 return FALSE;
1101}
1102
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001103/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301104static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1105{
1106 PtyCharDriver *s = chr->opaque;
1107
1108 if (s->timer_tag) {
1109 g_source_remove(s->timer_tag);
1110 s->timer_tag = 0;
1111 }
1112
1113 if (ms == 1000) {
1114 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1115 } else {
1116 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1117 }
1118}
1119
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001120/* Called with chr_write_lock held. */
1121static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001122{
1123 PtyCharDriver *s = chr->opaque;
1124 GPollFD pfd;
1125
1126 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1127 pfd.events = G_IO_OUT;
1128 pfd.revents = 0;
1129 g_poll(&pfd, 1, 0);
1130 if (pfd.revents & G_IO_HUP) {
1131 pty_chr_state(chr, 0);
1132 } else {
1133 pty_chr_state(chr, 1);
1134 }
1135}
1136
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001137static void pty_chr_update_read_handler(CharDriverState *chr)
1138{
1139 qemu_mutex_lock(&chr->chr_write_lock);
1140 pty_chr_update_read_handler_locked(chr);
1141 qemu_mutex_unlock(&chr->chr_write_lock);
1142}
1143
1144/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001145static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1146{
1147 PtyCharDriver *s = chr->opaque;
1148
1149 if (!s->connected) {
1150 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001151 pty_chr_update_read_handler_locked(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001152 return 0;
1153 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001154 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001155}
1156
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301157static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1158{
1159 PtyCharDriver *s = chr->opaque;
1160 return g_io_create_watch(s->fd, cond);
1161}
1162
aliguori6f97dba2008-10-31 18:49:55 +00001163static int pty_chr_read_poll(void *opaque)
1164{
1165 CharDriverState *chr = opaque;
1166 PtyCharDriver *s = chr->opaque;
1167
Anthony Liguori909cda12011-08-15 11:17:31 -05001168 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001169 return s->read_bytes;
1170}
1171
Anthony Liguori093d3a22013-03-05 23:21:20 +05301172static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001173{
1174 CharDriverState *chr = opaque;
1175 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301176 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301177 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301178 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001179
1180 len = sizeof(buf);
1181 if (len > s->read_bytes)
1182 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001183 if (len == 0) {
1184 return TRUE;
1185 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301186 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1187 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001188 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301189 return FALSE;
1190 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001191 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001192 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001193 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301194 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001195}
1196
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001197/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001198static void pty_chr_state(CharDriverState *chr, int connected)
1199{
1200 PtyCharDriver *s = chr->opaque;
1201
1202 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301203 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001204 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001205 /* (re-)connect poll interval for idle guests: once per second.
1206 * We check more frequently in case the guests sends data to
1207 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301208 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001209 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001210 if (s->timer_tag) {
1211 g_source_remove(s->timer_tag);
1212 s->timer_tag = 0;
1213 }
1214 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001215 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001216 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001217 }
1218 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301219 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1220 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001221 }
aliguori6f97dba2008-10-31 18:49:55 +00001222 }
1223}
1224
aliguori6f97dba2008-10-31 18:49:55 +00001225static void pty_chr_close(struct CharDriverState *chr)
1226{
1227 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301228 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001229
Amit Shah26da70c2013-08-28 15:23:37 +05301230 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301231 fd = g_io_channel_unix_get_fd(s->fd);
1232 g_io_channel_unref(s->fd);
1233 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301234 if (s->timer_tag) {
1235 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001236 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301237 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001238 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001239 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001240}
1241
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001242static CharDriverState *qemu_chr_open_pty(const char *id,
1243 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001244{
1245 CharDriverState *chr;
1246 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001247 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001248 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001249
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001250 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1251 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001252 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001253 }
1254
aliguori6f97dba2008-10-31 18:49:55 +00001255 close(slave_fd);
1256
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001257 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001258
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001259 chr->filename = g_strdup_printf("pty:%s", pty_name);
1260 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001261 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001262
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001263 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001264 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001265
1266 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001267 chr->opaque = s;
1268 chr->chr_write = pty_chr_write;
1269 chr->chr_update_read_handler = pty_chr_update_read_handler;
1270 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301271 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001272 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001273
Anthony Liguori093d3a22013-03-05 23:21:20 +05301274 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301275 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001276
Markus Armbruster1f514702012-02-07 15:09:08 +01001277 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001278}
1279
1280static void tty_serial_init(int fd, int speed,
1281 int parity, int data_bits, int stop_bits)
1282{
1283 struct termios tty;
1284 speed_t spd;
1285
1286#if 0
1287 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1288 speed, parity, data_bits, stop_bits);
1289#endif
1290 tcgetattr (fd, &tty);
1291
Stefan Weil45eea132009-10-26 16:10:10 +01001292#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1293 speed = speed * 10 / 11;
1294 do {
1295 check_speed(50);
1296 check_speed(75);
1297 check_speed(110);
1298 check_speed(134);
1299 check_speed(150);
1300 check_speed(200);
1301 check_speed(300);
1302 check_speed(600);
1303 check_speed(1200);
1304 check_speed(1800);
1305 check_speed(2400);
1306 check_speed(4800);
1307 check_speed(9600);
1308 check_speed(19200);
1309 check_speed(38400);
1310 /* Non-Posix values follow. They may be unsupported on some systems. */
1311 check_speed(57600);
1312 check_speed(115200);
1313#ifdef B230400
1314 check_speed(230400);
1315#endif
1316#ifdef B460800
1317 check_speed(460800);
1318#endif
1319#ifdef B500000
1320 check_speed(500000);
1321#endif
1322#ifdef B576000
1323 check_speed(576000);
1324#endif
1325#ifdef B921600
1326 check_speed(921600);
1327#endif
1328#ifdef B1000000
1329 check_speed(1000000);
1330#endif
1331#ifdef B1152000
1332 check_speed(1152000);
1333#endif
1334#ifdef B1500000
1335 check_speed(1500000);
1336#endif
1337#ifdef B2000000
1338 check_speed(2000000);
1339#endif
1340#ifdef B2500000
1341 check_speed(2500000);
1342#endif
1343#ifdef B3000000
1344 check_speed(3000000);
1345#endif
1346#ifdef B3500000
1347 check_speed(3500000);
1348#endif
1349#ifdef B4000000
1350 check_speed(4000000);
1351#endif
aliguori6f97dba2008-10-31 18:49:55 +00001352 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001353 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001354
1355 cfsetispeed(&tty, spd);
1356 cfsetospeed(&tty, spd);
1357
1358 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1359 |INLCR|IGNCR|ICRNL|IXON);
1360 tty.c_oflag |= OPOST;
1361 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1362 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1363 switch(data_bits) {
1364 default:
1365 case 8:
1366 tty.c_cflag |= CS8;
1367 break;
1368 case 7:
1369 tty.c_cflag |= CS7;
1370 break;
1371 case 6:
1372 tty.c_cflag |= CS6;
1373 break;
1374 case 5:
1375 tty.c_cflag |= CS5;
1376 break;
1377 }
1378 switch(parity) {
1379 default:
1380 case 'N':
1381 break;
1382 case 'E':
1383 tty.c_cflag |= PARENB;
1384 break;
1385 case 'O':
1386 tty.c_cflag |= PARENB | PARODD;
1387 break;
1388 }
1389 if (stop_bits == 2)
1390 tty.c_cflag |= CSTOPB;
1391
1392 tcsetattr (fd, TCSANOW, &tty);
1393}
1394
1395static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1396{
1397 FDCharDriver *s = chr->opaque;
1398
1399 switch(cmd) {
1400 case CHR_IOCTL_SERIAL_SET_PARAMS:
1401 {
1402 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301403 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1404 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001405 ssp->data_bits, ssp->stop_bits);
1406 }
1407 break;
1408 case CHR_IOCTL_SERIAL_SET_BREAK:
1409 {
1410 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301411 if (enable) {
1412 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1413 }
aliguori6f97dba2008-10-31 18:49:55 +00001414 }
1415 break;
1416 case CHR_IOCTL_SERIAL_GET_TIOCM:
1417 {
1418 int sarg = 0;
1419 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301420 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001421 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001422 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001423 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001424 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001425 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001426 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001427 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001428 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001429 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001430 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001431 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001432 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001433 *targ |= CHR_TIOCM_RTS;
1434 }
1435 break;
1436 case CHR_IOCTL_SERIAL_SET_TIOCM:
1437 {
1438 int sarg = *(int *)arg;
1439 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301440 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001441 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1442 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1443 if (sarg & CHR_TIOCM_CTS)
1444 targ |= TIOCM_CTS;
1445 if (sarg & CHR_TIOCM_CAR)
1446 targ |= TIOCM_CAR;
1447 if (sarg & CHR_TIOCM_DSR)
1448 targ |= TIOCM_DSR;
1449 if (sarg & CHR_TIOCM_RI)
1450 targ |= TIOCM_RI;
1451 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001452 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001453 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001454 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301455 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001456 }
1457 break;
1458 default:
1459 return -ENOTSUP;
1460 }
1461 return 0;
1462}
1463
David Ahern4266a132010-02-10 18:27:17 -07001464static void qemu_chr_close_tty(CharDriverState *chr)
1465{
1466 FDCharDriver *s = chr->opaque;
1467 int fd = -1;
1468
1469 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301470 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001471 }
1472
1473 fd_chr_close(chr);
1474
1475 if (fd >= 0) {
1476 close(fd);
1477 }
1478}
1479
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001480static CharDriverState *qemu_chr_open_tty_fd(int fd)
1481{
1482 CharDriverState *chr;
1483
1484 tty_serial_init(fd, 115200, 'N', 8, 1);
1485 chr = qemu_chr_open_fd(fd, fd);
1486 chr->chr_ioctl = tty_serial_ioctl;
1487 chr->chr_close = qemu_chr_close_tty;
1488 return chr;
1489}
aliguori6f97dba2008-10-31 18:49:55 +00001490#endif /* __linux__ || __sun__ */
1491
1492#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001493
1494#define HAVE_CHARDEV_PARPORT 1
1495
aliguori6f97dba2008-10-31 18:49:55 +00001496typedef struct {
1497 int fd;
1498 int mode;
1499} ParallelCharDriver;
1500
1501static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1502{
1503 if (s->mode != mode) {
1504 int m = mode;
1505 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1506 return 0;
1507 s->mode = mode;
1508 }
1509 return 1;
1510}
1511
1512static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1513{
1514 ParallelCharDriver *drv = chr->opaque;
1515 int fd = drv->fd;
1516 uint8_t b;
1517
1518 switch(cmd) {
1519 case CHR_IOCTL_PP_READ_DATA:
1520 if (ioctl(fd, PPRDATA, &b) < 0)
1521 return -ENOTSUP;
1522 *(uint8_t *)arg = b;
1523 break;
1524 case CHR_IOCTL_PP_WRITE_DATA:
1525 b = *(uint8_t *)arg;
1526 if (ioctl(fd, PPWDATA, &b) < 0)
1527 return -ENOTSUP;
1528 break;
1529 case CHR_IOCTL_PP_READ_CONTROL:
1530 if (ioctl(fd, PPRCONTROL, &b) < 0)
1531 return -ENOTSUP;
1532 /* Linux gives only the lowest bits, and no way to know data
1533 direction! For better compatibility set the fixed upper
1534 bits. */
1535 *(uint8_t *)arg = b | 0xc0;
1536 break;
1537 case CHR_IOCTL_PP_WRITE_CONTROL:
1538 b = *(uint8_t *)arg;
1539 if (ioctl(fd, PPWCONTROL, &b) < 0)
1540 return -ENOTSUP;
1541 break;
1542 case CHR_IOCTL_PP_READ_STATUS:
1543 if (ioctl(fd, PPRSTATUS, &b) < 0)
1544 return -ENOTSUP;
1545 *(uint8_t *)arg = b;
1546 break;
1547 case CHR_IOCTL_PP_DATA_DIR:
1548 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1549 return -ENOTSUP;
1550 break;
1551 case CHR_IOCTL_PP_EPP_READ_ADDR:
1552 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1553 struct ParallelIOArg *parg = arg;
1554 int n = read(fd, parg->buffer, parg->count);
1555 if (n != parg->count) {
1556 return -EIO;
1557 }
1558 }
1559 break;
1560 case CHR_IOCTL_PP_EPP_READ:
1561 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
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_WRITE_ADDR:
1570 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1571 struct ParallelIOArg *parg = arg;
1572 int n = write(fd, parg->buffer, parg->count);
1573 if (n != parg->count) {
1574 return -EIO;
1575 }
1576 }
1577 break;
1578 case CHR_IOCTL_PP_EPP_WRITE:
1579 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
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 default:
1588 return -ENOTSUP;
1589 }
1590 return 0;
1591}
1592
1593static void pp_close(CharDriverState *chr)
1594{
1595 ParallelCharDriver *drv = chr->opaque;
1596 int fd = drv->fd;
1597
1598 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1599 ioctl(fd, PPRELEASE);
1600 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001601 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001602 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001603}
1604
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001605static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001606{
1607 CharDriverState *chr;
1608 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001609
1610 if (ioctl(fd, PPCLAIM) < 0) {
1611 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001612 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001613 }
1614
Anthony Liguori7267c092011-08-20 22:09:37 -05001615 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001616 drv->fd = fd;
1617 drv->mode = IEEE1284_MODE_COMPAT;
1618
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001619 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001620 chr->chr_write = null_chr_write;
1621 chr->chr_ioctl = pp_ioctl;
1622 chr->chr_close = pp_close;
1623 chr->opaque = drv;
1624
Markus Armbruster1f514702012-02-07 15:09:08 +01001625 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001626}
1627#endif /* __linux__ */
1628
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001629#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001630
1631#define HAVE_CHARDEV_PARPORT 1
1632
blueswir16972f932008-11-22 20:49:12 +00001633static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1634{
Stefan Weile0efb992011-02-23 19:09:16 +01001635 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001636 uint8_t b;
1637
1638 switch(cmd) {
1639 case CHR_IOCTL_PP_READ_DATA:
1640 if (ioctl(fd, PPIGDATA, &b) < 0)
1641 return -ENOTSUP;
1642 *(uint8_t *)arg = b;
1643 break;
1644 case CHR_IOCTL_PP_WRITE_DATA:
1645 b = *(uint8_t *)arg;
1646 if (ioctl(fd, PPISDATA, &b) < 0)
1647 return -ENOTSUP;
1648 break;
1649 case CHR_IOCTL_PP_READ_CONTROL:
1650 if (ioctl(fd, PPIGCTRL, &b) < 0)
1651 return -ENOTSUP;
1652 *(uint8_t *)arg = b;
1653 break;
1654 case CHR_IOCTL_PP_WRITE_CONTROL:
1655 b = *(uint8_t *)arg;
1656 if (ioctl(fd, PPISCTRL, &b) < 0)
1657 return -ENOTSUP;
1658 break;
1659 case CHR_IOCTL_PP_READ_STATUS:
1660 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1661 return -ENOTSUP;
1662 *(uint8_t *)arg = b;
1663 break;
1664 default:
1665 return -ENOTSUP;
1666 }
1667 return 0;
1668}
1669
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001670static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001671{
1672 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001673
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001674 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001675 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001676 chr->chr_write = null_chr_write;
1677 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001678 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001679 return chr;
blueswir16972f932008-11-22 20:49:12 +00001680}
1681#endif
1682
aliguori6f97dba2008-10-31 18:49:55 +00001683#else /* _WIN32 */
1684
1685typedef struct {
1686 int max_size;
1687 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001688 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001689 BOOL fpipe;
1690 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001691
1692 /* Protected by the CharDriverState chr_write_lock. */
1693 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001694} WinCharState;
1695
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001696typedef struct {
1697 HANDLE hStdIn;
1698 HANDLE hInputReadyEvent;
1699 HANDLE hInputDoneEvent;
1700 HANDLE hInputThread;
1701 uint8_t win_stdio_buf;
1702} WinStdioCharState;
1703
aliguori6f97dba2008-10-31 18:49:55 +00001704#define NSENDBUF 2048
1705#define NRECVBUF 2048
1706#define MAXCONNECT 1
1707#define NTIMEOUT 5000
1708
1709static int win_chr_poll(void *opaque);
1710static int win_chr_pipe_poll(void *opaque);
1711
1712static void win_chr_close(CharDriverState *chr)
1713{
1714 WinCharState *s = chr->opaque;
1715
1716 if (s->hsend) {
1717 CloseHandle(s->hsend);
1718 s->hsend = NULL;
1719 }
1720 if (s->hrecv) {
1721 CloseHandle(s->hrecv);
1722 s->hrecv = NULL;
1723 }
1724 if (s->hcom) {
1725 CloseHandle(s->hcom);
1726 s->hcom = NULL;
1727 }
1728 if (s->fpipe)
1729 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1730 else
1731 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301732
Hans de Goedea425d232011-11-19 10:22:43 +01001733 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001734}
1735
1736static int win_chr_init(CharDriverState *chr, const char *filename)
1737{
1738 WinCharState *s = chr->opaque;
1739 COMMCONFIG comcfg;
1740 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1741 COMSTAT comstat;
1742 DWORD size;
1743 DWORD err;
1744
1745 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1746 if (!s->hsend) {
1747 fprintf(stderr, "Failed CreateEvent\n");
1748 goto fail;
1749 }
1750 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1751 if (!s->hrecv) {
1752 fprintf(stderr, "Failed CreateEvent\n");
1753 goto fail;
1754 }
1755
1756 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1757 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1758 if (s->hcom == INVALID_HANDLE_VALUE) {
1759 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1760 s->hcom = NULL;
1761 goto fail;
1762 }
1763
1764 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1765 fprintf(stderr, "Failed SetupComm\n");
1766 goto fail;
1767 }
1768
1769 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1770 size = sizeof(COMMCONFIG);
1771 GetDefaultCommConfig(filename, &comcfg, &size);
1772 comcfg.dcb.DCBlength = sizeof(DCB);
1773 CommConfigDialog(filename, NULL, &comcfg);
1774
1775 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1776 fprintf(stderr, "Failed SetCommState\n");
1777 goto fail;
1778 }
1779
1780 if (!SetCommMask(s->hcom, EV_ERR)) {
1781 fprintf(stderr, "Failed SetCommMask\n");
1782 goto fail;
1783 }
1784
1785 cto.ReadIntervalTimeout = MAXDWORD;
1786 if (!SetCommTimeouts(s->hcom, &cto)) {
1787 fprintf(stderr, "Failed SetCommTimeouts\n");
1788 goto fail;
1789 }
1790
1791 if (!ClearCommError(s->hcom, &err, &comstat)) {
1792 fprintf(stderr, "Failed ClearCommError\n");
1793 goto fail;
1794 }
1795 qemu_add_polling_cb(win_chr_poll, chr);
1796 return 0;
1797
1798 fail:
1799 win_chr_close(chr);
1800 return -1;
1801}
1802
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001803/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001804static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1805{
1806 WinCharState *s = chr->opaque;
1807 DWORD len, ret, size, err;
1808
1809 len = len1;
1810 ZeroMemory(&s->osend, sizeof(s->osend));
1811 s->osend.hEvent = s->hsend;
1812 while (len > 0) {
1813 if (s->hsend)
1814 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1815 else
1816 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1817 if (!ret) {
1818 err = GetLastError();
1819 if (err == ERROR_IO_PENDING) {
1820 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1821 if (ret) {
1822 buf += size;
1823 len -= size;
1824 } else {
1825 break;
1826 }
1827 } else {
1828 break;
1829 }
1830 } else {
1831 buf += size;
1832 len -= size;
1833 }
1834 }
1835 return len1 - len;
1836}
1837
1838static int win_chr_read_poll(CharDriverState *chr)
1839{
1840 WinCharState *s = chr->opaque;
1841
Anthony Liguori909cda12011-08-15 11:17:31 -05001842 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001843 return s->max_size;
1844}
1845
1846static void win_chr_readfile(CharDriverState *chr)
1847{
1848 WinCharState *s = chr->opaque;
1849 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301850 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001851 DWORD size;
1852
1853 ZeroMemory(&s->orecv, sizeof(s->orecv));
1854 s->orecv.hEvent = s->hrecv;
1855 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1856 if (!ret) {
1857 err = GetLastError();
1858 if (err == ERROR_IO_PENDING) {
1859 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1860 }
1861 }
1862
1863 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001864 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001865 }
1866}
1867
1868static void win_chr_read(CharDriverState *chr)
1869{
1870 WinCharState *s = chr->opaque;
1871
1872 if (s->len > s->max_size)
1873 s->len = s->max_size;
1874 if (s->len == 0)
1875 return;
1876
1877 win_chr_readfile(chr);
1878}
1879
1880static int win_chr_poll(void *opaque)
1881{
1882 CharDriverState *chr = opaque;
1883 WinCharState *s = chr->opaque;
1884 COMSTAT status;
1885 DWORD comerr;
1886
1887 ClearCommError(s->hcom, &comerr, &status);
1888 if (status.cbInQue > 0) {
1889 s->len = status.cbInQue;
1890 win_chr_read_poll(chr);
1891 win_chr_read(chr);
1892 return 1;
1893 }
1894 return 0;
1895}
1896
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001897static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001898{
1899 CharDriverState *chr;
1900 WinCharState *s;
1901
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001902 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001903 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001904 chr->opaque = s;
1905 chr->chr_write = win_chr_write;
1906 chr->chr_close = win_chr_close;
1907
1908 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001909 g_free(s);
1910 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001911 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001912 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001913 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001914}
1915
1916static int win_chr_pipe_poll(void *opaque)
1917{
1918 CharDriverState *chr = opaque;
1919 WinCharState *s = chr->opaque;
1920 DWORD size;
1921
1922 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1923 if (size > 0) {
1924 s->len = size;
1925 win_chr_read_poll(chr);
1926 win_chr_read(chr);
1927 return 1;
1928 }
1929 return 0;
1930}
1931
1932static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1933{
1934 WinCharState *s = chr->opaque;
1935 OVERLAPPED ov;
1936 int ret;
1937 DWORD size;
1938 char openname[256];
1939
1940 s->fpipe = TRUE;
1941
1942 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1943 if (!s->hsend) {
1944 fprintf(stderr, "Failed CreateEvent\n");
1945 goto fail;
1946 }
1947 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1948 if (!s->hrecv) {
1949 fprintf(stderr, "Failed CreateEvent\n");
1950 goto fail;
1951 }
1952
1953 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1954 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1955 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1956 PIPE_WAIT,
1957 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1958 if (s->hcom == INVALID_HANDLE_VALUE) {
1959 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1960 s->hcom = NULL;
1961 goto fail;
1962 }
1963
1964 ZeroMemory(&ov, sizeof(ov));
1965 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1966 ret = ConnectNamedPipe(s->hcom, &ov);
1967 if (ret) {
1968 fprintf(stderr, "Failed ConnectNamedPipe\n");
1969 goto fail;
1970 }
1971
1972 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1973 if (!ret) {
1974 fprintf(stderr, "Failed GetOverlappedResult\n");
1975 if (ov.hEvent) {
1976 CloseHandle(ov.hEvent);
1977 ov.hEvent = NULL;
1978 }
1979 goto fail;
1980 }
1981
1982 if (ov.hEvent) {
1983 CloseHandle(ov.hEvent);
1984 ov.hEvent = NULL;
1985 }
1986 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1987 return 0;
1988
1989 fail:
1990 win_chr_close(chr);
1991 return -1;
1992}
1993
1994
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001995static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001996{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001997 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001998 CharDriverState *chr;
1999 WinCharState *s;
2000
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002001 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002002 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002003 chr->opaque = s;
2004 chr->chr_write = win_chr_write;
2005 chr->chr_close = win_chr_close;
2006
2007 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002008 g_free(s);
2009 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002010 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002011 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002012 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002013}
2014
Markus Armbruster1f514702012-02-07 15:09:08 +01002015static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002016{
2017 CharDriverState *chr;
2018 WinCharState *s;
2019
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002020 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002021 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002022 s->hcom = fd_out;
2023 chr->opaque = s;
2024 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002025 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002026}
2027
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002028static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002029{
Markus Armbruster1f514702012-02-07 15:09:08 +01002030 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002031}
2032
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002033static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2034{
2035 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2036 DWORD dwSize;
2037 int len1;
2038
2039 len1 = len;
2040
2041 while (len1 > 0) {
2042 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2043 break;
2044 }
2045 buf += dwSize;
2046 len1 -= dwSize;
2047 }
2048
2049 return len - len1;
2050}
2051
2052static void win_stdio_wait_func(void *opaque)
2053{
2054 CharDriverState *chr = opaque;
2055 WinStdioCharState *stdio = chr->opaque;
2056 INPUT_RECORD buf[4];
2057 int ret;
2058 DWORD dwSize;
2059 int i;
2060
Stefan Weildff74242013-12-07 14:48:04 +01002061 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002062
2063 if (!ret) {
2064 /* Avoid error storm */
2065 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2066 return;
2067 }
2068
2069 for (i = 0; i < dwSize; i++) {
2070 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2071
2072 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2073 int j;
2074 if (kev->uChar.AsciiChar != 0) {
2075 for (j = 0; j < kev->wRepeatCount; j++) {
2076 if (qemu_chr_be_can_write(chr)) {
2077 uint8_t c = kev->uChar.AsciiChar;
2078 qemu_chr_be_write(chr, &c, 1);
2079 }
2080 }
2081 }
2082 }
2083 }
2084}
2085
2086static DWORD WINAPI win_stdio_thread(LPVOID param)
2087{
2088 CharDriverState *chr = param;
2089 WinStdioCharState *stdio = chr->opaque;
2090 int ret;
2091 DWORD dwSize;
2092
2093 while (1) {
2094
2095 /* Wait for one byte */
2096 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2097
2098 /* Exit in case of error, continue if nothing read */
2099 if (!ret) {
2100 break;
2101 }
2102 if (!dwSize) {
2103 continue;
2104 }
2105
2106 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2107 if (stdio->win_stdio_buf == '\r') {
2108 continue;
2109 }
2110
2111 /* Signal the main thread and wait until the byte was eaten */
2112 if (!SetEvent(stdio->hInputReadyEvent)) {
2113 break;
2114 }
2115 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2116 != WAIT_OBJECT_0) {
2117 break;
2118 }
2119 }
2120
2121 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2122 return 0;
2123}
2124
2125static void win_stdio_thread_wait_func(void *opaque)
2126{
2127 CharDriverState *chr = opaque;
2128 WinStdioCharState *stdio = chr->opaque;
2129
2130 if (qemu_chr_be_can_write(chr)) {
2131 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2132 }
2133
2134 SetEvent(stdio->hInputDoneEvent);
2135}
2136
2137static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2138{
2139 WinStdioCharState *stdio = chr->opaque;
2140 DWORD dwMode = 0;
2141
2142 GetConsoleMode(stdio->hStdIn, &dwMode);
2143
2144 if (echo) {
2145 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2146 } else {
2147 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2148 }
2149}
2150
2151static void win_stdio_close(CharDriverState *chr)
2152{
2153 WinStdioCharState *stdio = chr->opaque;
2154
2155 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2156 CloseHandle(stdio->hInputReadyEvent);
2157 }
2158 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2159 CloseHandle(stdio->hInputDoneEvent);
2160 }
2161 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2162 TerminateThread(stdio->hInputThread, 0);
2163 }
2164
2165 g_free(chr->opaque);
2166 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002167}
2168
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002169static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002170{
2171 CharDriverState *chr;
2172 WinStdioCharState *stdio;
2173 DWORD dwMode;
2174 int is_console = 0;
2175
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002176 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002177 stdio = g_malloc0(sizeof(WinStdioCharState));
2178
2179 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2180 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2181 fprintf(stderr, "cannot open stdio: invalid handle\n");
2182 exit(1);
2183 }
2184
2185 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2186
2187 chr->opaque = stdio;
2188 chr->chr_write = win_stdio_write;
2189 chr->chr_close = win_stdio_close;
2190
Anthony Liguoried7a1542013-03-05 23:21:17 +05302191 if (is_console) {
2192 if (qemu_add_wait_object(stdio->hStdIn,
2193 win_stdio_wait_func, chr)) {
2194 fprintf(stderr, "qemu_add_wait_object: failed\n");
2195 }
2196 } else {
2197 DWORD dwId;
2198
2199 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2200 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2201 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2202 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002203
Anthony Liguoried7a1542013-03-05 23:21:17 +05302204 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2205 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2206 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2207 fprintf(stderr, "cannot create stdio thread or event\n");
2208 exit(1);
2209 }
2210 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2211 win_stdio_thread_wait_func, chr)) {
2212 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002213 }
2214 }
2215
2216 dwMode |= ENABLE_LINE_INPUT;
2217
Anthony Liguoried7a1542013-03-05 23:21:17 +05302218 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002219 /* set the terminal in raw mode */
2220 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2221 dwMode |= ENABLE_PROCESSED_INPUT;
2222 }
2223
2224 SetConsoleMode(stdio->hStdIn, dwMode);
2225
2226 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2227 qemu_chr_fe_set_echo(chr, false);
2228
Markus Armbruster1f514702012-02-07 15:09:08 +01002229 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002230}
aliguori6f97dba2008-10-31 18:49:55 +00002231#endif /* !_WIN32 */
2232
Anthony Liguori5ab82112013-03-05 23:21:31 +05302233
aliguori6f97dba2008-10-31 18:49:55 +00002234/***********************************************************/
2235/* UDP Net console */
2236
2237typedef struct {
2238 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302239 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302240 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002241 int bufcnt;
2242 int bufptr;
2243 int max_size;
2244} NetCharDriver;
2245
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002246/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002247static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2248{
2249 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302250 gsize bytes_written;
2251 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002252
Anthony Liguori76a96442013-03-05 23:21:21 +05302253 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2254 if (status == G_IO_STATUS_EOF) {
2255 return 0;
2256 } else if (status != G_IO_STATUS_NORMAL) {
2257 return -1;
2258 }
2259
2260 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002261}
2262
2263static int udp_chr_read_poll(void *opaque)
2264{
2265 CharDriverState *chr = opaque;
2266 NetCharDriver *s = chr->opaque;
2267
Anthony Liguori909cda12011-08-15 11:17:31 -05002268 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002269
2270 /* If there were any stray characters in the queue process them
2271 * first
2272 */
2273 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002274 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002275 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002276 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002277 }
2278 return s->max_size;
2279}
2280
Anthony Liguori76a96442013-03-05 23:21:21 +05302281static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002282{
2283 CharDriverState *chr = opaque;
2284 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302285 gsize bytes_read = 0;
2286 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002287
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002288 if (s->max_size == 0) {
2289 return TRUE;
2290 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302291 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2292 &bytes_read, NULL);
2293 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002294 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302295 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302296 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302297 return FALSE;
2298 }
aliguori6f97dba2008-10-31 18:49:55 +00002299
2300 s->bufptr = 0;
2301 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002302 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002303 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002304 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002305 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302306
2307 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002308}
2309
2310static void udp_chr_update_read_handler(CharDriverState *chr)
2311{
2312 NetCharDriver *s = chr->opaque;
2313
Amit Shah26da70c2013-08-28 15:23:37 +05302314 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302315 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302316 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2317 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002318 }
2319}
2320
aliguori819f56b2009-03-28 17:58:14 +00002321static void udp_chr_close(CharDriverState *chr)
2322{
2323 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302324
2325 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302326 if (s->chan) {
2327 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002328 closesocket(s->fd);
2329 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002330 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002331 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002332}
2333
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002334static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002335{
2336 CharDriverState *chr = NULL;
2337 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002338
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002339 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002340 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002341
aliguori6f97dba2008-10-31 18:49:55 +00002342 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302343 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002344 s->bufcnt = 0;
2345 s->bufptr = 0;
2346 chr->opaque = s;
2347 chr->chr_write = udp_chr_write;
2348 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002349 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002350 /* be isn't opened until we get a connection */
2351 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002352 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002353}
aliguori6f97dba2008-10-31 18:49:55 +00002354
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002355static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2356{
2357 Error *local_err = NULL;
2358 int fd = -1;
2359
2360 fd = inet_dgram_opts(opts, &local_err);
2361 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002362 qerror_report_err(local_err);
2363 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002364 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002365 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002366 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002367}
2368
2369/***********************************************************/
2370/* TCP Net console */
2371
2372typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302373
2374 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302375 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002376 int fd, listen_fd;
2377 int connected;
2378 int max_size;
2379 int do_telnetopt;
2380 int do_nodelay;
2381 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002382 int *read_msgfds;
2383 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002384 int *write_msgfds;
2385 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002386} TCPCharDriver;
2387
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302388static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002389
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002390#ifndef _WIN32
2391static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2392{
2393 TCPCharDriver *s = chr->opaque;
2394 struct msghdr msgh;
2395 struct iovec iov;
2396 int r;
2397
2398 size_t fd_size = s->write_msgfds_num * sizeof(int);
2399 char control[CMSG_SPACE(fd_size)];
2400 struct cmsghdr *cmsg;
2401
2402 memset(&msgh, 0, sizeof(msgh));
2403 memset(control, 0, sizeof(control));
2404
2405 /* set the payload */
2406 iov.iov_base = (uint8_t *) buf;
2407 iov.iov_len = len;
2408
2409 msgh.msg_iov = &iov;
2410 msgh.msg_iovlen = 1;
2411
2412 msgh.msg_control = control;
2413 msgh.msg_controllen = sizeof(control);
2414
2415 cmsg = CMSG_FIRSTHDR(&msgh);
2416
2417 cmsg->cmsg_len = CMSG_LEN(fd_size);
2418 cmsg->cmsg_level = SOL_SOCKET;
2419 cmsg->cmsg_type = SCM_RIGHTS;
2420 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2421
2422 do {
2423 r = sendmsg(s->fd, &msgh, 0);
2424 } while (r < 0 && errno == EINTR);
2425
2426 /* free the written msgfds, no matter what */
2427 if (s->write_msgfds_num) {
2428 g_free(s->write_msgfds);
2429 s->write_msgfds = 0;
2430 s->write_msgfds_num = 0;
2431 }
2432
2433 return r;
2434}
2435#endif
2436
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002437/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002438static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2439{
2440 TCPCharDriver *s = chr->opaque;
2441 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002442#ifndef _WIN32
2443 if (s->is_unix && s->write_msgfds_num) {
2444 return unix_send_msgfds(chr, buf, len);
2445 } else
2446#endif
2447 {
2448 return io_channel_send(s->chan, buf, len);
2449 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002450 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002451 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002452 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002453 }
2454}
2455
2456static int tcp_chr_read_poll(void *opaque)
2457{
2458 CharDriverState *chr = opaque;
2459 TCPCharDriver *s = chr->opaque;
2460 if (!s->connected)
2461 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002462 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002463 return s->max_size;
2464}
2465
2466#define IAC 255
2467#define IAC_BREAK 243
2468static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2469 TCPCharDriver *s,
2470 uint8_t *buf, int *size)
2471{
2472 /* Handle any telnet client's basic IAC options to satisfy char by
2473 * char mode with no echo. All IAC options will be removed from
2474 * the buf and the do_telnetopt variable will be used to track the
2475 * state of the width of the IAC information.
2476 *
2477 * IAC commands come in sets of 3 bytes with the exception of the
2478 * "IAC BREAK" command and the double IAC.
2479 */
2480
2481 int i;
2482 int j = 0;
2483
2484 for (i = 0; i < *size; i++) {
2485 if (s->do_telnetopt > 1) {
2486 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2487 /* Double IAC means send an IAC */
2488 if (j != i)
2489 buf[j] = buf[i];
2490 j++;
2491 s->do_telnetopt = 1;
2492 } else {
2493 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2494 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002495 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002496 s->do_telnetopt++;
2497 }
2498 s->do_telnetopt++;
2499 }
2500 if (s->do_telnetopt >= 4) {
2501 s->do_telnetopt = 1;
2502 }
2503 } else {
2504 if ((unsigned char)buf[i] == IAC) {
2505 s->do_telnetopt = 2;
2506 } else {
2507 if (j != i)
2508 buf[j] = buf[i];
2509 j++;
2510 }
2511 }
2512 }
2513 *size = j;
2514}
2515
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002516static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002517{
2518 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002519 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2520
2521 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002522 int i;
2523
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002524 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2525
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002526 /* Close unused fds */
2527 for (i = to_copy; i < s->read_msgfds_num; i++) {
2528 close(s->read_msgfds[i]);
2529 }
2530
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002531 g_free(s->read_msgfds);
2532 s->read_msgfds = 0;
2533 s->read_msgfds_num = 0;
2534 }
2535
2536 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002537}
2538
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002539static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2540{
2541 TCPCharDriver *s = chr->opaque;
2542
2543 /* clear old pending fd array */
2544 if (s->write_msgfds) {
2545 g_free(s->write_msgfds);
2546 }
2547
2548 if (num) {
2549 s->write_msgfds = g_malloc(num * sizeof(int));
2550 memcpy(s->write_msgfds, fds, num * sizeof(int));
2551 }
2552
2553 s->write_msgfds_num = num;
2554
2555 return 0;
2556}
2557
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002558#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002559static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2560{
2561 TCPCharDriver *s = chr->opaque;
2562 struct cmsghdr *cmsg;
2563
2564 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002565 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002566
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002567 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002568 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002569 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002570 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002571 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002572
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002573 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2574
2575 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002576 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002577 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002578
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002579 /* close and clean read_msgfds */
2580 for (i = 0; i < s->read_msgfds_num; i++) {
2581 close(s->read_msgfds[i]);
2582 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002583
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002584 if (s->read_msgfds_num) {
2585 g_free(s->read_msgfds);
2586 }
2587
2588 s->read_msgfds_num = fd_size / sizeof(int);
2589 s->read_msgfds = g_malloc(fd_size);
2590 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2591
2592 for (i = 0; i < s->read_msgfds_num; i++) {
2593 int fd = s->read_msgfds[i];
2594 if (fd < 0) {
2595 continue;
2596 }
2597
2598 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2599 qemu_set_block(fd);
2600
2601 #ifndef MSG_CMSG_CLOEXEC
2602 qemu_set_cloexec(fd);
2603 #endif
2604 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002605 }
2606}
2607
Mark McLoughlin9977c892009-07-22 09:11:38 +01002608static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2609{
2610 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002611 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002612 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002613 union {
2614 struct cmsghdr cmsg;
2615 char control[CMSG_SPACE(sizeof(int))];
2616 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002617 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002618 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002619
2620 iov[0].iov_base = buf;
2621 iov[0].iov_len = len;
2622
2623 msg.msg_iov = iov;
2624 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002625 msg.msg_control = &msg_control;
2626 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002627
Corey Bryant06138652012-08-14 16:43:42 -04002628#ifdef MSG_CMSG_CLOEXEC
2629 flags |= MSG_CMSG_CLOEXEC;
2630#endif
2631 ret = recvmsg(s->fd, &msg, flags);
2632 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002633 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002634 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002635
2636 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002637}
2638#else
2639static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2640{
2641 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002642 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002643}
2644#endif
2645
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302646static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2647{
2648 TCPCharDriver *s = chr->opaque;
2649 return g_io_create_watch(s->chan, cond);
2650}
2651
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002652static void tcp_chr_disconnect(CharDriverState *chr)
2653{
2654 TCPCharDriver *s = chr->opaque;
2655
2656 s->connected = 0;
2657 if (s->listen_chan) {
2658 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2659 tcp_chr_accept, chr);
2660 }
2661 remove_fd_in_watch(chr);
2662 g_io_channel_unref(s->chan);
2663 s->chan = NULL;
2664 closesocket(s->fd);
2665 s->fd = -1;
2666 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2667}
2668
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302669static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002670{
2671 CharDriverState *chr = opaque;
2672 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302673 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002674 int len, size;
2675
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302676 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002677 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302678 }
aliguori6f97dba2008-10-31 18:49:55 +00002679 len = sizeof(buf);
2680 if (len > s->max_size)
2681 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002682 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002683 if (size == 0) {
2684 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002685 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002686 } else if (size > 0) {
2687 if (s->do_telnetopt)
2688 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2689 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002690 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002691 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302692
2693 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002694}
2695
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002696static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2697{
2698 TCPCharDriver *s = chr->opaque;
2699 int size;
2700
2701 if (!s->connected) {
2702 return 0;
2703 }
2704
2705 size = tcp_chr_recv(chr, (void *) buf, len);
2706 if (size == 0) {
2707 /* connection closed */
2708 tcp_chr_disconnect(chr);
2709 }
2710
2711 return size;
2712}
2713
Blue Swirl68c18d12010-08-15 09:46:24 +00002714#ifndef _WIN32
2715CharDriverState *qemu_chr_open_eventfd(int eventfd)
2716{
David Marchande9d21c42014-06-11 17:25:16 +02002717 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2718
2719 if (chr) {
2720 chr->avail_connections = 1;
2721 }
2722
2723 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002724}
Blue Swirl68c18d12010-08-15 09:46:24 +00002725#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002726
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002727static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2728 void *opaque)
2729{
2730 CharDriverState *chr = opaque;
2731
2732 if (cond != G_IO_HUP) {
2733 return FALSE;
2734 }
2735
2736 /* connection closed */
2737 tcp_chr_disconnect(chr);
2738 if (chr->fd_hup_tag) {
2739 g_source_remove(chr->fd_hup_tag);
2740 chr->fd_hup_tag = 0;
2741 }
2742
2743 return TRUE;
2744}
2745
aliguori6f97dba2008-10-31 18:49:55 +00002746static void tcp_chr_connect(void *opaque)
2747{
2748 CharDriverState *chr = opaque;
2749 TCPCharDriver *s = chr->opaque;
2750
2751 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302752 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302753 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2754 tcp_chr_read, chr);
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002755 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2756 chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002757 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002758 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002759}
2760
Gal Hammerac1b84d2014-02-25 12:12:35 +02002761static void tcp_chr_update_read_handler(CharDriverState *chr)
2762{
2763 TCPCharDriver *s = chr->opaque;
2764
2765 remove_fd_in_watch(chr);
2766 if (s->chan) {
2767 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2768 tcp_chr_read, chr);
2769 }
2770}
2771
aliguori6f97dba2008-10-31 18:49:55 +00002772#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2773static void tcp_chr_telnet_init(int fd)
2774{
2775 char buf[3];
2776 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2777 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2778 send(fd, (char *)buf, 3, 0);
2779 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2780 send(fd, (char *)buf, 3, 0);
2781 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2782 send(fd, (char *)buf, 3, 0);
2783 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2784 send(fd, (char *)buf, 3, 0);
2785}
2786
Daniel P. Berrange13661082011-06-23 13:31:42 +01002787static int tcp_chr_add_client(CharDriverState *chr, int fd)
2788{
2789 TCPCharDriver *s = chr->opaque;
2790 if (s->fd != -1)
2791 return -1;
2792
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002793 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002794 if (s->do_nodelay)
2795 socket_set_nodelay(fd);
2796 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302797 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002798 if (s->listen_tag) {
2799 g_source_remove(s->listen_tag);
2800 s->listen_tag = 0;
2801 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002802 tcp_chr_connect(chr);
2803
2804 return 0;
2805}
2806
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302807static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002808{
2809 CharDriverState *chr = opaque;
2810 TCPCharDriver *s = chr->opaque;
2811 struct sockaddr_in saddr;
2812#ifndef _WIN32
2813 struct sockaddr_un uaddr;
2814#endif
2815 struct sockaddr *addr;
2816 socklen_t len;
2817 int fd;
2818
2819 for(;;) {
2820#ifndef _WIN32
2821 if (s->is_unix) {
2822 len = sizeof(uaddr);
2823 addr = (struct sockaddr *)&uaddr;
2824 } else
2825#endif
2826 {
2827 len = sizeof(saddr);
2828 addr = (struct sockaddr *)&saddr;
2829 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002830 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002831 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002832 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302833 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002834 } else if (fd >= 0) {
2835 if (s->do_telnetopt)
2836 tcp_chr_telnet_init(fd);
2837 break;
2838 }
2839 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002840 if (tcp_chr_add_client(chr, fd) < 0)
2841 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302842
2843 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002844}
2845
2846static void tcp_chr_close(CharDriverState *chr)
2847{
2848 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002849 int i;
aliguori819f56b2009-03-28 17:58:14 +00002850 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302851 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302852 if (s->chan) {
2853 g_io_channel_unref(s->chan);
2854 }
aliguori6f97dba2008-10-31 18:49:55 +00002855 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002856 }
2857 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302858 if (s->listen_tag) {
2859 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002860 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302861 }
2862 if (s->listen_chan) {
2863 g_io_channel_unref(s->listen_chan);
2864 }
aliguori6f97dba2008-10-31 18:49:55 +00002865 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002866 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002867 if (s->read_msgfds_num) {
2868 for (i = 0; i < s->read_msgfds_num; i++) {
2869 close(s->read_msgfds[i]);
2870 }
2871 g_free(s->read_msgfds);
2872 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002873 if (s->write_msgfds_num) {
2874 g_free(s->write_msgfds);
2875 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002876 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002877 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002878}
2879
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002880static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2881 bool is_listen, bool is_telnet,
2882 bool is_waitconnect,
2883 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002884{
2885 CharDriverState *chr = NULL;
2886 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002887 char host[NI_MAXHOST], serv[NI_MAXSERV];
2888 const char *left = "", *right = "";
2889 struct sockaddr_storage ss;
2890 socklen_t ss_len = sizeof(ss);
2891
2892 memset(&ss, 0, ss_len);
2893 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002894 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002895 return NULL;
2896 }
2897
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002898 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002899 s = g_malloc0(sizeof(TCPCharDriver));
2900
2901 s->connected = 0;
2902 s->fd = -1;
2903 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002904 s->read_msgfds = 0;
2905 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002906 s->write_msgfds = 0;
2907 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002908
2909 chr->filename = g_malloc(256);
2910 switch (ss.ss_family) {
2911#ifndef _WIN32
2912 case AF_UNIX:
2913 s->is_unix = 1;
2914 snprintf(chr->filename, 256, "unix:%s%s",
2915 ((struct sockaddr_un *)(&ss))->sun_path,
2916 is_listen ? ",server" : "");
2917 break;
2918#endif
2919 case AF_INET6:
2920 left = "[";
2921 right = "]";
2922 /* fall through */
2923 case AF_INET:
2924 s->do_nodelay = do_nodelay;
2925 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2926 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002927 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002928 is_telnet ? "telnet" : "tcp",
2929 left, host, right, serv,
2930 is_listen ? ",server" : "");
2931 break;
2932 }
2933
2934 chr->opaque = s;
2935 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002936 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002937 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002938 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002939 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002940 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302941 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002942 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002943 /* be isn't opened until we get a connection */
2944 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002945
2946 if (is_listen) {
2947 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302948 s->listen_chan = io_channel_from_socket(s->listen_fd);
2949 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002950 if (is_telnet) {
2951 s->do_telnetopt = 1;
2952 }
2953 } else {
2954 s->connected = 1;
2955 s->fd = fd;
2956 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302957 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002958 tcp_chr_connect(chr);
2959 }
2960
2961 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002962 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2963 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302964 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002965 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002966 }
2967 return chr;
2968}
2969
2970static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2971{
2972 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002973 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002974 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002975
liguange990a392013-06-18 11:45:35 +08002976 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2977 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2978 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2979 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2980 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002981
aliguorif07b6002008-11-11 20:54:09 +00002982 if (is_unix) {
2983 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002984 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002985 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002986 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002987 }
2988 } else {
2989 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002990 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002991 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002992 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002993 }
2994 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002995 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002996 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002997 }
aliguori6f97dba2008-10-31 18:49:55 +00002998
2999 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003000 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00003001
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003002 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
3003 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003004 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003005 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00003006 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003007 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003008
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003009
aliguori6f97dba2008-10-31 18:49:55 +00003010 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003011 if (local_err) {
3012 qerror_report_err(local_err);
3013 error_free(local_err);
3014 }
3015 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00003016 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003017 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003018 if (chr) {
3019 g_free(chr->opaque);
3020 g_free(chr);
3021 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003022 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003023}
3024
Lei Li51767e72013-01-25 00:03:19 +08003025/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003026/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003027
3028typedef struct {
3029 size_t size;
3030 size_t prod;
3031 size_t cons;
3032 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003033} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003034
Markus Armbruster3949e592013-02-06 21:27:24 +01003035static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003036{
Markus Armbruster3949e592013-02-06 21:27:24 +01003037 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003038
Markus Armbruster5c230102013-02-06 21:27:23 +01003039 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003040}
3041
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003042/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003043static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003044{
Markus Armbruster3949e592013-02-06 21:27:24 +01003045 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003046 int i;
3047
3048 if (!buf || (len < 0)) {
3049 return -1;
3050 }
3051
3052 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003053 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3054 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003055 d->cons = d->prod - d->size;
3056 }
3057 }
3058
3059 return 0;
3060}
3061
Markus Armbruster3949e592013-02-06 21:27:24 +01003062static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003063{
Markus Armbruster3949e592013-02-06 21:27:24 +01003064 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003065 int i;
3066
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003067 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003068 for (i = 0; i < len && d->cons != d->prod; i++) {
3069 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003070 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003071 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003072
3073 return i;
3074}
3075
Markus Armbruster3949e592013-02-06 21:27:24 +01003076static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003077{
Markus Armbruster3949e592013-02-06 21:27:24 +01003078 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003079
3080 g_free(d->cbuf);
3081 g_free(d);
3082 chr->opaque = NULL;
3083}
3084
Markus Armbruster4f573782013-07-26 16:44:32 +02003085static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3086 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003087{
3088 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003089 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003090
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003091 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003092 d = g_malloc(sizeof(*d));
3093
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003094 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003095
3096 /* The size must be power of 2 */
3097 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003098 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003099 goto fail;
3100 }
3101
3102 d->prod = 0;
3103 d->cons = 0;
3104 d->cbuf = g_malloc0(d->size);
3105
3106 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003107 chr->chr_write = ringbuf_chr_write;
3108 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003109
3110 return chr;
3111
3112fail:
3113 g_free(d);
3114 g_free(chr);
3115 return NULL;
3116}
3117
Hani Benhabiles8e597772014-05-27 23:39:30 +01003118bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003119{
Markus Armbruster3949e592013-02-06 21:27:24 +01003120 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003121}
3122
Markus Armbruster3949e592013-02-06 21:27:24 +01003123void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003124 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003125 Error **errp)
3126{
3127 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003128 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003129 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003130 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003131
3132 chr = qemu_chr_find(device);
3133 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003134 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003135 return;
3136 }
3137
Markus Armbruster3949e592013-02-06 21:27:24 +01003138 if (!chr_is_ringbuf(chr)) {
3139 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003140 return;
3141 }
3142
Lei Li1f590cf2013-01-25 00:03:20 +08003143 if (has_format && (format == DATA_FORMAT_BASE64)) {
3144 write_data = g_base64_decode(data, &write_count);
3145 } else {
3146 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003147 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003148 }
3149
Markus Armbruster3949e592013-02-06 21:27:24 +01003150 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003151
Markus Armbruster13289fb2013-02-06 21:27:18 +01003152 if (write_data != (uint8_t *)data) {
3153 g_free((void *)write_data);
3154 }
3155
Lei Li1f590cf2013-01-25 00:03:20 +08003156 if (ret < 0) {
3157 error_setg(errp, "Failed to write to device %s", device);
3158 return;
3159 }
3160}
3161
Markus Armbruster3949e592013-02-06 21:27:24 +01003162char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003163 bool has_format, enum DataFormat format,
3164 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003165{
3166 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003167 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003168 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003169 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003170
3171 chr = qemu_chr_find(device);
3172 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003173 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003174 return NULL;
3175 }
3176
Markus Armbruster3949e592013-02-06 21:27:24 +01003177 if (!chr_is_ringbuf(chr)) {
3178 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003179 return NULL;
3180 }
3181
3182 if (size <= 0) {
3183 error_setg(errp, "size must be greater than zero");
3184 return NULL;
3185 }
3186
Markus Armbruster3949e592013-02-06 21:27:24 +01003187 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003188 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003189 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003190
Markus Armbruster3949e592013-02-06 21:27:24 +01003191 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003192
3193 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003194 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003195 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003196 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003197 /*
3198 * FIXME should read only complete, valid UTF-8 characters up
3199 * to @size bytes. Invalid sequences should be replaced by a
3200 * suitable replacement character. Except when (and only
3201 * when) ring buffer lost characters since last read, initial
3202 * continuation characters should be dropped.
3203 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003204 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003205 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003206 }
3207
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003208 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003209}
3210
Gerd Hoffmann33521632009-12-08 13:11:49 +01003211QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003212{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003213 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003214 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003215 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003216 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003217 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003218
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003219 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003220 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003221 qerror_report_err(local_err);
3222 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003223 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003224 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003225
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003226 if (strstart(filename, "mon:", &p)) {
3227 filename = p;
3228 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003229 if (strcmp(filename, "stdio") == 0) {
3230 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3231 * but pass it to the guest. Handle this only for compat syntax,
3232 * for -chardev syntax we have special option for this.
3233 * This is what -nographic did, redirecting+muxing serial+monitor
3234 * to stdio causing Ctrl+C to be passed to guest. */
3235 qemu_opt_set(opts, "signal", "off");
3236 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003237 }
3238
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003239 if (strcmp(filename, "null") == 0 ||
3240 strcmp(filename, "pty") == 0 ||
3241 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003242 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003243 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003244 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003245 return opts;
3246 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003247 if (strstart(filename, "vc", &p)) {
3248 qemu_opt_set(opts, "backend", "vc");
3249 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003250 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003251 /* pixels */
3252 qemu_opt_set(opts, "width", width);
3253 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003254 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003255 /* chars */
3256 qemu_opt_set(opts, "cols", width);
3257 qemu_opt_set(opts, "rows", height);
3258 } else {
3259 goto fail;
3260 }
3261 }
3262 return opts;
3263 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003264 if (strcmp(filename, "con:") == 0) {
3265 qemu_opt_set(opts, "backend", "console");
3266 return opts;
3267 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003268 if (strstart(filename, "COM", NULL)) {
3269 qemu_opt_set(opts, "backend", "serial");
3270 qemu_opt_set(opts, "path", filename);
3271 return opts;
3272 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003273 if (strstart(filename, "file:", &p)) {
3274 qemu_opt_set(opts, "backend", "file");
3275 qemu_opt_set(opts, "path", p);
3276 return opts;
3277 }
3278 if (strstart(filename, "pipe:", &p)) {
3279 qemu_opt_set(opts, "backend", "pipe");
3280 qemu_opt_set(opts, "path", p);
3281 return opts;
3282 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003283 if (strstart(filename, "tcp:", &p) ||
3284 strstart(filename, "telnet:", &p)) {
3285 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3286 host[0] = 0;
3287 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3288 goto fail;
3289 }
3290 qemu_opt_set(opts, "backend", "socket");
3291 qemu_opt_set(opts, "host", host);
3292 qemu_opt_set(opts, "port", port);
3293 if (p[pos] == ',') {
3294 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3295 goto fail;
3296 }
3297 if (strstart(filename, "telnet:", &p))
3298 qemu_opt_set(opts, "telnet", "on");
3299 return opts;
3300 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003301 if (strstart(filename, "udp:", &p)) {
3302 qemu_opt_set(opts, "backend", "udp");
3303 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3304 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003305 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003306 goto fail;
3307 }
3308 }
3309 qemu_opt_set(opts, "host", host);
3310 qemu_opt_set(opts, "port", port);
3311 if (p[pos] == '@') {
3312 p += pos + 1;
3313 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3314 host[0] = 0;
3315 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003316 goto fail;
3317 }
3318 }
3319 qemu_opt_set(opts, "localaddr", host);
3320 qemu_opt_set(opts, "localport", port);
3321 }
3322 return opts;
3323 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003324 if (strstart(filename, "unix:", &p)) {
3325 qemu_opt_set(opts, "backend", "socket");
3326 if (qemu_opts_do_parse(opts, p, "path") != 0)
3327 goto fail;
3328 return opts;
3329 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003330 if (strstart(filename, "/dev/parport", NULL) ||
3331 strstart(filename, "/dev/ppi", NULL)) {
3332 qemu_opt_set(opts, "backend", "parport");
3333 qemu_opt_set(opts, "path", filename);
3334 return opts;
3335 }
3336 if (strstart(filename, "/dev/", NULL)) {
3337 qemu_opt_set(opts, "backend", "tty");
3338 qemu_opt_set(opts, "path", filename);
3339 return opts;
3340 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003341
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003342fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003343 qemu_opts_del(opts);
3344 return NULL;
3345}
3346
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003347static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3348 Error **errp)
3349{
3350 const char *path = qemu_opt_get(opts, "path");
3351
3352 if (path == NULL) {
3353 error_setg(errp, "chardev: file: no filename given");
3354 return;
3355 }
3356 backend->file = g_new0(ChardevFile, 1);
3357 backend->file->out = g_strdup(path);
3358}
3359
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003360static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3361 Error **errp)
3362{
3363 backend->stdio = g_new0(ChardevStdio, 1);
3364 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003365 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003366}
3367
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003368static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3369 Error **errp)
3370{
3371 const char *device = qemu_opt_get(opts, "path");
3372
3373 if (device == NULL) {
3374 error_setg(errp, "chardev: serial/tty: no device path given");
3375 return;
3376 }
3377 backend->serial = g_new0(ChardevHostdev, 1);
3378 backend->serial->device = g_strdup(device);
3379}
3380
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003381static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3382 Error **errp)
3383{
3384 const char *device = qemu_opt_get(opts, "path");
3385
3386 if (device == NULL) {
3387 error_setg(errp, "chardev: parallel: no device path given");
3388 return;
3389 }
3390 backend->parallel = g_new0(ChardevHostdev, 1);
3391 backend->parallel->device = g_strdup(device);
3392}
3393
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003394static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3395 Error **errp)
3396{
3397 const char *device = qemu_opt_get(opts, "path");
3398
3399 if (device == NULL) {
3400 error_setg(errp, "chardev: pipe: no device path given");
3401 return;
3402 }
3403 backend->pipe = g_new0(ChardevHostdev, 1);
3404 backend->pipe->device = g_strdup(device);
3405}
3406
Markus Armbruster4f573782013-07-26 16:44:32 +02003407static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3408 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003409{
3410 int val;
3411
Markus Armbruster3a1da422013-07-26 16:44:34 +02003412 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003413
Markus Armbruster0f953052013-06-27 16:22:07 +02003414 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003415 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003416 backend->ringbuf->has_size = true;
3417 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003418 }
3419}
3420
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003421static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3422 Error **errp)
3423{
3424 const char *chardev = qemu_opt_get(opts, "chardev");
3425
3426 if (chardev == NULL) {
3427 error_setg(errp, "chardev: mux: no chardev given");
3428 return;
3429 }
3430 backend->mux = g_new0(ChardevMux, 1);
3431 backend->mux->chardev = g_strdup(chardev);
3432}
3433
Anthony Liguorid654f342013-03-05 23:21:28 +05303434typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003435 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003436 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003437 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003438 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003439 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003440 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303441} CharDriver;
3442
3443static GSList *backends;
3444
3445void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3446{
3447 CharDriver *s;
3448
3449 s = g_malloc0(sizeof(*s));
3450 s->name = g_strdup(name);
3451 s->open = open;
3452
3453 backends = g_slist_append(backends, s);
3454}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003455
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003456void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003457 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3458{
3459 CharDriver *s;
3460
3461 s = g_malloc0(sizeof(*s));
3462 s->name = g_strdup(name);
3463 s->kind = kind;
3464 s->parse = parse;
3465
3466 backends = g_slist_append(backends, s);
3467}
3468
Anthony Liguorif69554b2011-08-15 11:17:37 -05003469CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003470 void (*init)(struct CharDriverState *s),
3471 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003472{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003473 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303474 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003475 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303476 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003477
3478 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003479 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003480 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003481 }
3482
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003483 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003484 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003485 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003486 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003487 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303488 for (i = backends; i; i = i->next) {
3489 cd = i->data;
3490
3491 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003492 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303493 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003494 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303495 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003496 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003497 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003498 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003499 }
3500
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003501 if (!cd->open) {
3502 /* using new, qapi init */
3503 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3504 ChardevReturn *ret = NULL;
3505 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003506 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003507
3508 if (qemu_opt_get_bool(opts, "mux", 0)) {
3509 bid = g_strdup_printf("%s-base", id);
3510 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003511
3512 chr = NULL;
3513 backend->kind = cd->kind;
3514 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003515 cd->parse(opts, backend, &local_err);
3516 if (local_err) {
3517 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003518 goto qapi_out;
3519 }
3520 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003521 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003522 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003523 goto qapi_out;
3524 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003525
3526 if (bid) {
3527 qapi_free_ChardevBackend(backend);
3528 qapi_free_ChardevReturn(ret);
3529 backend = g_new0(ChardevBackend, 1);
3530 backend->mux = g_new0(ChardevMux, 1);
3531 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3532 backend->mux->chardev = g_strdup(bid);
3533 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003534 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003535 chr = qemu_chr_find(bid);
3536 qemu_chr_delete(chr);
3537 chr = NULL;
3538 goto qapi_out;
3539 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003540 }
3541
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003542 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003543 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003544
3545 qapi_out:
3546 qapi_free_ChardevBackend(backend);
3547 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003548 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003549 return chr;
3550 }
3551
Anthony Liguorid654f342013-03-05 23:21:28 +05303552 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003553 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003554 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003555 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003556 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003557 }
3558
3559 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003560 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003561 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003562 /* if we didn't create the chardev via qmp_chardev_add, we
3563 * need to send the OPENED event here
3564 */
3565 if (!chr->explicit_be_open) {
3566 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3567 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003568 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003569
3570 if (qemu_opt_get_bool(opts, "mux", 0)) {
3571 CharDriverState *base = chr;
3572 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003573 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003574 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3575 chr = qemu_chr_open_mux(base);
3576 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003577 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003578 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003579 } else {
3580 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003581 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003582 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003583 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003584 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003585
3586err:
3587 qemu_opts_del(opts);
3588 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003589}
3590
Anthony Liguori27143a42011-08-15 11:17:36 -05003591CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003592{
3593 const char *p;
3594 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003595 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003596 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003597
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003598 if (strstart(filename, "chardev:", &p)) {
3599 return qemu_chr_find(p);
3600 }
3601
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003602 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003603 if (!opts)
3604 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003605
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003606 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003607 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003608 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003609 error_free(err);
3610 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003611 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003612 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003613 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003614 }
3615 return chr;
3616}
3617
Anthony Liguori15f31512011-08-15 11:17:35 -05003618void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003619{
3620 if (chr->chr_set_echo) {
3621 chr->chr_set_echo(chr, echo);
3622 }
3623}
3624
Hans de Goede8e25daa2013-03-26 11:07:57 +01003625void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003626{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003627 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003628 return;
3629 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003630 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003631 if (chr->chr_set_fe_open) {
3632 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003633 }
3634}
3635
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003636void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3637{
3638 if (chr->chr_fe_event) {
3639 chr->chr_fe_event(chr, event);
3640 }
3641}
3642
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003643int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3644 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303645{
3646 GSource *src;
3647 guint tag;
3648
3649 if (s->chr_add_watch == NULL) {
3650 return -ENOSYS;
3651 }
3652
3653 src = s->chr_add_watch(s, cond);
3654 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3655 tag = g_source_attach(src, NULL);
3656 g_source_unref(src);
3657
3658 return tag;
3659}
3660
Hans de Goede44c473d2013-03-27 20:29:39 +01003661int qemu_chr_fe_claim(CharDriverState *s)
3662{
3663 if (s->avail_connections < 1) {
3664 return -1;
3665 }
3666 s->avail_connections--;
3667 return 0;
3668}
3669
3670void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3671{
3672 if (qemu_chr_fe_claim(s) != 0) {
3673 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3674 __func__, s->label);
3675 exit(1);
3676 }
3677}
3678
3679void qemu_chr_fe_release(CharDriverState *s)
3680{
3681 s->avail_connections++;
3682}
3683
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003684void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003685{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003686 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003687 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003688 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003689 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003690 g_free(chr->filename);
3691 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003692 if (chr->opts) {
3693 qemu_opts_del(chr->opts);
3694 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003695 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003696}
3697
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003698ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003699{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003700 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003701 CharDriverState *chr;
3702
Blue Swirl72cf2d42009-09-12 07:36:22 +00003703 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003704 ChardevInfoList *info = g_malloc0(sizeof(*info));
3705 info->value = g_malloc0(sizeof(*info->value));
3706 info->value->label = g_strdup(chr->label);
3707 info->value->filename = g_strdup(chr->filename);
3708
3709 info->next = chr_list;
3710 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003711 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003712
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003713 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003714}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003715
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003716ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3717{
3718 ChardevBackendInfoList *backend_list = NULL;
3719 CharDriver *c = NULL;
3720 GSList *i = NULL;
3721
3722 for (i = backends; i; i = i->next) {
3723 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3724 c = i->data;
3725 info->value = g_malloc0(sizeof(*info->value));
3726 info->value->name = g_strdup(c->name);
3727
3728 info->next = backend_list;
3729 backend_list = info;
3730 }
3731
3732 return backend_list;
3733}
3734
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003735CharDriverState *qemu_chr_find(const char *name)
3736{
3737 CharDriverState *chr;
3738
Blue Swirl72cf2d42009-09-12 07:36:22 +00003739 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003740 if (strcmp(chr->label, name) != 0)
3741 continue;
3742 return chr;
3743 }
3744 return NULL;
3745}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003746
3747/* Get a character (serial) device interface. */
3748CharDriverState *qemu_char_get_next_serial(void)
3749{
3750 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003751 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003752
3753 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003754
3755 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3756 chr = serial_hds[next_serial++];
3757 qemu_chr_fe_claim_no_fail(chr);
3758 return chr;
3759 }
3760 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003761}
3762
Paolo Bonzini4d454572012-11-26 16:03:42 +01003763QemuOptsList qemu_chardev_opts = {
3764 .name = "chardev",
3765 .implied_opt_name = "backend",
3766 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3767 .desc = {
3768 {
3769 .name = "backend",
3770 .type = QEMU_OPT_STRING,
3771 },{
3772 .name = "path",
3773 .type = QEMU_OPT_STRING,
3774 },{
3775 .name = "host",
3776 .type = QEMU_OPT_STRING,
3777 },{
3778 .name = "port",
3779 .type = QEMU_OPT_STRING,
3780 },{
3781 .name = "localaddr",
3782 .type = QEMU_OPT_STRING,
3783 },{
3784 .name = "localport",
3785 .type = QEMU_OPT_STRING,
3786 },{
3787 .name = "to",
3788 .type = QEMU_OPT_NUMBER,
3789 },{
3790 .name = "ipv4",
3791 .type = QEMU_OPT_BOOL,
3792 },{
3793 .name = "ipv6",
3794 .type = QEMU_OPT_BOOL,
3795 },{
3796 .name = "wait",
3797 .type = QEMU_OPT_BOOL,
3798 },{
3799 .name = "server",
3800 .type = QEMU_OPT_BOOL,
3801 },{
3802 .name = "delay",
3803 .type = QEMU_OPT_BOOL,
3804 },{
3805 .name = "telnet",
3806 .type = QEMU_OPT_BOOL,
3807 },{
3808 .name = "width",
3809 .type = QEMU_OPT_NUMBER,
3810 },{
3811 .name = "height",
3812 .type = QEMU_OPT_NUMBER,
3813 },{
3814 .name = "cols",
3815 .type = QEMU_OPT_NUMBER,
3816 },{
3817 .name = "rows",
3818 .type = QEMU_OPT_NUMBER,
3819 },{
3820 .name = "mux",
3821 .type = QEMU_OPT_BOOL,
3822 },{
3823 .name = "signal",
3824 .type = QEMU_OPT_BOOL,
3825 },{
3826 .name = "name",
3827 .type = QEMU_OPT_STRING,
3828 },{
3829 .name = "debug",
3830 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003831 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003832 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003833 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003834 },{
3835 .name = "chardev",
3836 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003837 },
3838 { /* end of list */ }
3839 },
3840};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003841
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003842#ifdef _WIN32
3843
3844static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3845{
3846 HANDLE out;
3847
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003848 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003849 error_setg(errp, "input file not supported");
3850 return NULL;
3851 }
3852
3853 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3854 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3855 if (out == INVALID_HANDLE_VALUE) {
3856 error_setg(errp, "open %s failed", file->out);
3857 return NULL;
3858 }
3859 return qemu_chr_open_win_file(out);
3860}
3861
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003862static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3863 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003864{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003865 return qemu_chr_open_win_path(serial->device);
3866}
3867
3868static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3869 Error **errp)
3870{
3871 error_setg(errp, "character device backend type 'parallel' not supported");
3872 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003873}
3874
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003875#else /* WIN32 */
3876
3877static int qmp_chardev_open_file_source(char *src, int flags,
3878 Error **errp)
3879{
3880 int fd = -1;
3881
3882 TFR(fd = qemu_open(src, flags, 0666));
3883 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003884 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003885 }
3886 return fd;
3887}
3888
3889static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3890{
Markus Armbruster5f758362014-05-19 18:57:34 +02003891 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003892
3893 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3894 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003895 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003896 return NULL;
3897 }
3898
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003899 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003900 flags = O_RDONLY;
3901 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003902 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003903 qemu_close(out);
3904 return NULL;
3905 }
3906 }
3907
3908 return qemu_chr_open_fd(in, out);
3909}
3910
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003911static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3912 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003913{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003914#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003915 int fd;
3916
3917 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003918 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003919 return NULL;
3920 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003921 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003922 return qemu_chr_open_tty_fd(fd);
3923#else
3924 error_setg(errp, "character device backend type 'serial' not supported");
3925 return NULL;
3926#endif
3927}
3928
3929static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3930 Error **errp)
3931{
3932#ifdef HAVE_CHARDEV_PARPORT
3933 int fd;
3934
3935 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003936 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003937 return NULL;
3938 }
3939 return qemu_chr_open_pp_fd(fd);
3940#else
3941 error_setg(errp, "character device backend type 'parallel' not supported");
3942 return NULL;
3943#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003944}
3945
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003946#endif /* WIN32 */
3947
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003948static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3949 Error **errp)
3950{
3951 SocketAddress *addr = sock->addr;
3952 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3953 bool is_listen = sock->has_server ? sock->server : true;
3954 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3955 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3956 int fd;
3957
3958 if (is_listen) {
3959 fd = socket_listen(addr, errp);
3960 } else {
3961 fd = socket_connect(addr, errp, NULL, NULL);
3962 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003963 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003964 return NULL;
3965 }
3966 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3967 is_telnet, is_waitconnect, errp);
3968}
3969
Lei Li08d0ab32013-05-20 14:51:03 +08003970static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3971 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003972{
3973 int fd;
3974
Lei Li08d0ab32013-05-20 14:51:03 +08003975 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003976 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003977 return NULL;
3978 }
3979 return qemu_chr_open_udp_fd(fd);
3980}
3981
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003982ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3983 Error **errp)
3984{
3985 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003986 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003987
3988 chr = qemu_chr_find(id);
3989 if (chr) {
3990 error_setg(errp, "Chardev '%s' already exists", id);
3991 g_free(ret);
3992 return NULL;
3993 }
3994
3995 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003996 case CHARDEV_BACKEND_KIND_FILE:
3997 chr = qmp_chardev_open_file(backend->file, errp);
3998 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003999 case CHARDEV_BACKEND_KIND_SERIAL:
4000 chr = qmp_chardev_open_serial(backend->serial, errp);
4001 break;
4002 case CHARDEV_BACKEND_KIND_PARALLEL:
4003 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004004 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004005 case CHARDEV_BACKEND_KIND_PIPE:
4006 chr = qemu_chr_open_pipe(backend->pipe);
4007 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004008 case CHARDEV_BACKEND_KIND_SOCKET:
4009 chr = qmp_chardev_open_socket(backend->socket, errp);
4010 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004011 case CHARDEV_BACKEND_KIND_UDP:
4012 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004013 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004014#ifdef HAVE_CHARDEV_TTY
4015 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004016 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004017 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004018#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004019 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004020 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004021 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004022 case CHARDEV_BACKEND_KIND_MUX:
4023 base = qemu_chr_find(backend->mux->chardev);
4024 if (base == NULL) {
4025 error_setg(errp, "mux: base chardev %s not found",
4026 backend->mux->chardev);
4027 break;
4028 }
4029 chr = qemu_chr_open_mux(base);
4030 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004031 case CHARDEV_BACKEND_KIND_MSMOUSE:
4032 chr = qemu_chr_open_msmouse();
4033 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004034#ifdef CONFIG_BRLAPI
4035 case CHARDEV_BACKEND_KIND_BRAILLE:
4036 chr = chr_baum_init();
4037 break;
4038#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004039 case CHARDEV_BACKEND_KIND_STDIO:
4040 chr = qemu_chr_open_stdio(backend->stdio);
4041 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004042#ifdef _WIN32
4043 case CHARDEV_BACKEND_KIND_CONSOLE:
4044 chr = qemu_chr_open_win_con();
4045 break;
4046#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004047#ifdef CONFIG_SPICE
4048 case CHARDEV_BACKEND_KIND_SPICEVMC:
4049 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4050 break;
4051 case CHARDEV_BACKEND_KIND_SPICEPORT:
4052 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4053 break;
4054#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004055 case CHARDEV_BACKEND_KIND_VC:
4056 chr = vc_init(backend->vc);
4057 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004058 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004059 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004060 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004061 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004062 default:
4063 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4064 break;
4065 }
4066
Markus Armbruster3894c782014-05-19 18:57:36 +02004067 /*
4068 * Character backend open hasn't been fully converted to the Error
4069 * API. Some opens fail without setting an error. Set a generic
4070 * error then.
4071 * TODO full conversion to Error API
4072 */
4073 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004074 error_setg(errp, "Failed to create chardev");
4075 }
4076 if (chr) {
4077 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004078 chr->avail_connections =
4079 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004080 if (!chr->filename) {
4081 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4082 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004083 if (!chr->explicit_be_open) {
4084 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4085 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004086 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4087 return ret;
4088 } else {
4089 g_free(ret);
4090 return NULL;
4091 }
4092}
4093
4094void qmp_chardev_remove(const char *id, Error **errp)
4095{
4096 CharDriverState *chr;
4097
4098 chr = qemu_chr_find(id);
4099 if (NULL == chr) {
4100 error_setg(errp, "Chardev '%s' not found", id);
4101 return;
4102 }
4103 if (chr->chr_can_read || chr->chr_read ||
4104 chr->chr_event || chr->handler_opaque) {
4105 error_setg(errp, "Chardev '%s' is busy", id);
4106 return;
4107 }
4108 qemu_chr_delete(chr);
4109}
Anthony Liguorid654f342013-03-05 23:21:28 +05304110
4111static void register_types(void)
4112{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004113 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304114 register_char_driver("socket", qemu_chr_open_socket);
4115 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004116 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004117 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004118 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4119 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004120 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4121 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004122 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4123 qemu_chr_parse_serial);
4124 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4125 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004126 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4127 qemu_chr_parse_parallel);
4128 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4129 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004130 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004131 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004132 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4133 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004134 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4135 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004136 /* Bug-compatibility: */
4137 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4138 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004139 /* this must be done after machine init, since we register FEs with muxes
4140 * as part of realize functions like serial_isa_realizefn when -nographic
4141 * is specified
4142 */
4143 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304144}
4145
4146type_init(register_types);