blob: f6bdf2fae4b734038c4c767f94da0d07a6b1d4ef [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000029#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030030#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000031
32#include <unistd.h>
33#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000034#include <time.h>
35#include <errno.h>
36#include <sys/time.h>
37#include <zlib.h>
38
39#ifndef _WIN32
40#include <sys/times.h>
41#include <sys/wait.h>
42#include <termios.h>
43#include <sys/mman.h>
44#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000046#include <sys/socket.h>
47#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <dirent.h>
51#include <netdb.h>
52#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020053#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000054#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040055#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56#include <dev/ppbus/ppi.h>
57#include <dev/ppbus/ppbconf.h>
58#elif defined(__DragonFly__)
59#include <dev/misc/ppi/ppi.h>
60#include <bus/ppbus/ppbconf.h>
61#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
aliguori6f97dba2008-10-31 18:49:55 +000063#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000064#include <linux/ppdev.h>
65#include <linux/parport.h>
66#endif
67#ifdef __sun__
68#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
71#include <netinet/arp.h>
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000078#endif
79#endif
80#endif
81
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010082#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020083#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000084
Amit Shah9bd78542009-11-03 19:59:54 +053085#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030086#define READ_RETRIES 10
Amit Shah9bd78542009-11-03 19:59:54 +053087
aliguori6f97dba2008-10-31 18:49:55 +000088/***********************************************************/
89/* character device */
90
Blue Swirl72cf2d42009-09-12 07:36:22 +000091static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000093
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020094CharDriverState *qemu_chr_alloc(void)
95{
96 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
97 return chr;
98}
99
Hans de Goedea425d232011-11-19 10:22:43 +0100100void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000101{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200102 /* Keep track if the char device is open */
103 switch (event) {
104 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100105 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200106 break;
107 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100108 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200109 break;
110 }
111
aliguori6f97dba2008-10-31 18:49:55 +0000112 if (!s->chr_event)
113 return;
114 s->chr_event(s->handler_opaque, event);
115}
116
Hans de Goedefee204f2013-03-26 11:07:54 +0100117void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000118{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500119 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000120}
121
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500122int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000123{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200124 int ret;
125
126 qemu_mutex_lock(&s->chr_write_lock);
127 ret = s->chr_write(s, buf, len);
128 qemu_mutex_unlock(&s->chr_write_lock);
129 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000130}
131
Anthony Liguoricd187202013-03-26 10:04:17 -0500132int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
133{
134 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200135 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500136
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200137 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500138 while (offset < len) {
139 do {
140 res = s->chr_write(s, buf + offset, len - offset);
141 if (res == -1 && errno == EAGAIN) {
142 g_usleep(100);
143 }
144 } while (res == -1 && errno == EAGAIN);
145
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200146 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500147 break;
148 }
149
Anthony Liguoricd187202013-03-26 10:04:17 -0500150 offset += res;
151 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200152 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500153
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200154 if (res < 0) {
155 return res;
156 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500157 return offset;
158}
159
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300160int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
161{
162 int offset = 0, counter = 10;
163 int res;
164
165 if (!s->chr_sync_read) {
166 return 0;
167 }
168
169 while (offset < len) {
170 do {
171 res = s->chr_sync_read(s, buf + offset, len - offset);
172 if (res == -1 && errno == EAGAIN) {
173 g_usleep(100);
174 }
175 } while (res == -1 && errno == EAGAIN);
176
177 if (res == 0) {
178 break;
179 }
180
181 if (res < 0) {
182 return res;
183 }
184
185 offset += res;
186
187 if (!counter--) {
188 break;
189 }
190 }
191
192 return offset;
193}
194
Anthony Liguori41084f12011-08-15 11:17:34 -0500195int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000196{
197 if (!s->chr_ioctl)
198 return -ENOTSUP;
199 return s->chr_ioctl(s, cmd, arg);
200}
201
Anthony Liguori909cda12011-08-15 11:17:31 -0500202int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000203{
204 if (!s->chr_can_read)
205 return 0;
206 return s->chr_can_read(s->handler_opaque);
207}
208
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500209void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000210{
Stefan Weilac310732012-04-19 22:27:14 +0200211 if (s->chr_read) {
212 s->chr_read(s->handler_opaque, buf, len);
213 }
aliguori6f97dba2008-10-31 18:49:55 +0000214}
215
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500216int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100217{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300218 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800219 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300220}
221
222int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
223{
224 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100225}
226
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300227int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
228{
229 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
230}
231
Daniel P. Berrange13661082011-06-23 13:31:42 +0100232int qemu_chr_add_client(CharDriverState *s, int fd)
233{
234 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
235}
236
aliguori6f97dba2008-10-31 18:49:55 +0000237void qemu_chr_accept_input(CharDriverState *s)
238{
239 if (s->chr_accept_input)
240 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100241 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000242}
243
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500244void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000245{
Amit Shah9bd78542009-11-03 19:59:54 +0530246 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000247 va_list ap;
248 va_start(ap, fmt);
249 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500250 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000251 va_end(ap);
252}
253
Amit Shah386a5a12013-08-28 15:24:05 +0530254static void remove_fd_in_watch(CharDriverState *chr);
255
aliguori6f97dba2008-10-31 18:49:55 +0000256void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100257 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000258 IOReadHandler *fd_read,
259 IOEventHandler *fd_event,
260 void *opaque)
261{
Hans de Goede19083222013-03-26 11:07:56 +0100262 int fe_open;
263
Amit Shahda7d9982011-04-25 15:18:22 +0530264 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100265 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530266 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100267 } else {
268 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530269 }
aliguori6f97dba2008-10-31 18:49:55 +0000270 s->chr_can_read = fd_can_read;
271 s->chr_read = fd_read;
272 s->chr_event = fd_event;
273 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200274 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000275 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200276
Hans de Goede19083222013-03-26 11:07:56 +0100277 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100278 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100279 }
280
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200281 /* We're connecting to an already opened device, so let's make sure we
282 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100283 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100284 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200285 }
aliguori6f97dba2008-10-31 18:49:55 +0000286}
287
288static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
289{
290 return len;
291}
292
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100293static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000294{
295 CharDriverState *chr;
296
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200297 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000298 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500299 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100300 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000301}
302
303/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000304#define MAX_MUX 4
305#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
306#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
307typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100308 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000309 IOReadHandler *chr_read[MAX_MUX];
310 IOEventHandler *chr_event[MAX_MUX];
311 void *ext_opaque[MAX_MUX];
312 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200313 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000314 int mux_cnt;
315 int term_got_escape;
316 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000317 /* Intermediate input buffer allows to catch escape sequences even if the
318 currently active device is not accepting any input - but only until it
319 is full as well. */
320 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
321 int prod[MAX_MUX];
322 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200323 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200324
325 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200326 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200327 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000328} MuxDriver;
329
330
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200331/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000332static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
333{
334 MuxDriver *d = chr->opaque;
335 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200336 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200337 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000338 } else {
339 int i;
340
341 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200342 for (i = 0; i < len; i++) {
343 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000344 char buf1[64];
345 int64_t ti;
346 int secs;
347
Alex Blighbc72ad62013-08-21 16:03:08 +0100348 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200349 if (d->timestamps_start == -1)
350 d->timestamps_start = ti;
351 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000352 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000353 snprintf(buf1, sizeof(buf1),
354 "[%02d:%02d:%02d.%03d] ",
355 secs / 3600,
356 (secs / 60) % 60,
357 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000358 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200359 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200360 d->linestart = 0;
361 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200362 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200363 if (buf[i] == '\n') {
364 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000365 }
366 }
367 }
368 return ret;
369}
370
371static const char * const mux_help[] = {
372 "% h print this help\n\r",
373 "% x exit emulator\n\r",
374 "% s save disk data back to file (if -snapshot)\n\r",
375 "% t toggle console timestamps\n\r"
376 "% b send break (magic sysrq)\n\r",
377 "% c switch between console and monitor\n\r",
378 "% % sends %\n\r",
379 NULL
380};
381
382int term_escape_char = 0x01; /* ctrl-a is used for escape */
383static void mux_print_help(CharDriverState *chr)
384{
385 int i, j;
386 char ebuf[15] = "Escape-Char";
387 char cbuf[50] = "\n\r";
388
389 if (term_escape_char > 0 && term_escape_char < 26) {
390 snprintf(cbuf, sizeof(cbuf), "\n\r");
391 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
392 } else {
393 snprintf(cbuf, sizeof(cbuf),
394 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
395 term_escape_char);
396 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200397 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000398 for (i = 0; mux_help[i] != NULL; i++) {
399 for (j=0; mux_help[i][j] != '\0'; j++) {
400 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200401 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000402 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200403 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000404 }
405 }
406}
407
aliguori2724b182009-03-05 23:01:47 +0000408static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
409{
410 if (d->chr_event[mux_nr])
411 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
412}
413
aliguori6f97dba2008-10-31 18:49:55 +0000414static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
415{
416 if (d->term_got_escape) {
417 d->term_got_escape = 0;
418 if (ch == term_escape_char)
419 goto send_char;
420 switch(ch) {
421 case '?':
422 case 'h':
423 mux_print_help(chr);
424 break;
425 case 'x':
426 {
427 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200428 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000429 exit(0);
430 break;
431 }
432 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200433 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000434 break;
435 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100436 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000437 break;
438 case 'c':
439 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200440 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
441 d->focus++;
442 if (d->focus >= d->mux_cnt)
443 d->focus = 0;
444 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000445 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200446 case 't':
447 d->timestamps = !d->timestamps;
448 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200449 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200450 break;
aliguori6f97dba2008-10-31 18:49:55 +0000451 }
452 } else if (ch == term_escape_char) {
453 d->term_got_escape = 1;
454 } else {
455 send_char:
456 return 1;
457 }
458 return 0;
459}
460
461static void mux_chr_accept_input(CharDriverState *chr)
462{
aliguori6f97dba2008-10-31 18:49:55 +0000463 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200464 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000465
aliguoria80bf992009-03-05 23:00:02 +0000466 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000467 d->chr_can_read[m] &&
468 d->chr_can_read[m](d->ext_opaque[m])) {
469 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000470 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000471 }
472}
473
474static int mux_chr_can_read(void *opaque)
475{
476 CharDriverState *chr = opaque;
477 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200478 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000479
aliguoria80bf992009-03-05 23:00:02 +0000480 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000481 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000482 if (d->chr_can_read[m])
483 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000484 return 0;
485}
486
487static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
488{
489 CharDriverState *chr = opaque;
490 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200491 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000492 int i;
493
494 mux_chr_accept_input (opaque);
495
496 for(i = 0; i < size; i++)
497 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000498 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000499 d->chr_can_read[m] &&
500 d->chr_can_read[m](d->ext_opaque[m]))
501 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
502 else
aliguoria80bf992009-03-05 23:00:02 +0000503 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000504 }
505}
506
507static void mux_chr_event(void *opaque, int event)
508{
509 CharDriverState *chr = opaque;
510 MuxDriver *d = chr->opaque;
511 int i;
512
513 /* Send the event to all registered listeners */
514 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000515 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000516}
517
518static void mux_chr_update_read_handler(CharDriverState *chr)
519{
520 MuxDriver *d = chr->opaque;
521
522 if (d->mux_cnt >= MAX_MUX) {
523 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
524 return;
525 }
526 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
527 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
528 d->chr_read[d->mux_cnt] = chr->chr_read;
529 d->chr_event[d->mux_cnt] = chr->chr_event;
530 /* Fix up the real driver with mux routines */
531 if (d->mux_cnt == 0) {
532 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
533 mux_chr_event, chr);
534 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200535 if (d->focus != -1) {
536 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200537 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200538 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000539 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200540 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000541}
542
Michael Roth7b7ab182013-07-30 13:04:22 -0500543static bool muxes_realized;
544
545/**
546 * Called after processing of default and command-line-specified
547 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
548 * to a mux chardev. This is done here to ensure that
549 * output/prompts/banners are only displayed for the FE that has
550 * focus when initial command-line processing/machine init is
551 * completed.
552 *
553 * After this point, any new FE attached to any new or existing
554 * mux will receive CHR_EVENT_OPENED notifications for the BE
555 * immediately.
556 */
557static void muxes_realize_done(Notifier *notifier, void *unused)
558{
559 CharDriverState *chr;
560
561 QTAILQ_FOREACH(chr, &chardevs, next) {
562 if (chr->is_mux) {
563 MuxDriver *d = chr->opaque;
564 int i;
565
566 /* send OPENED to all already-attached FEs */
567 for (i = 0; i < d->mux_cnt; i++) {
568 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
569 }
570 /* mark mux as OPENED so any new FEs will immediately receive
571 * OPENED event
572 */
573 qemu_chr_be_generic_open(chr);
574 }
575 }
576 muxes_realized = true;
577}
578
579static Notifier muxes_realize_notify = {
580 .notify = muxes_realize_done,
581};
582
aliguori6f97dba2008-10-31 18:49:55 +0000583static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
584{
585 CharDriverState *chr;
586 MuxDriver *d;
587
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200588 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500589 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000590
591 chr->opaque = d;
592 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200593 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000594 chr->chr_write = mux_chr_write;
595 chr->chr_update_read_handler = mux_chr_update_read_handler;
596 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100597 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100598 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500599 /* only default to opened state if we've realized the initial
600 * set of muxes
601 */
602 chr->explicit_be_open = muxes_realized ? 0 : 1;
603 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200604
aliguori6f97dba2008-10-31 18:49:55 +0000605 return chr;
606}
607
608
609#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000610int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000611{
612 int ret, len;
613
614 len = len1;
615 while (len > 0) {
616 ret = send(fd, buf, len, 0);
617 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000618 errno = WSAGetLastError();
619 if (errno != WSAEWOULDBLOCK) {
620 return -1;
621 }
622 } else if (ret == 0) {
623 break;
624 } else {
625 buf += ret;
626 len -= ret;
627 }
628 }
629 return len1 - len;
630}
631
632#else
633
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100634int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000635{
636 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100637 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000638
639 len = len1;
640 while (len > 0) {
641 ret = write(fd, buf, len);
642 if (ret < 0) {
643 if (errno != EINTR && errno != EAGAIN)
644 return -1;
645 } else if (ret == 0) {
646 break;
647 } else {
648 buf += ret;
649 len -= ret;
650 }
651 }
652 return len1 - len;
653}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500654
655int recv_all(int fd, void *_buf, int len1, bool single_read)
656{
657 int ret, len;
658 uint8_t *buf = _buf;
659
660 len = len1;
661 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
662 if (ret < 0) {
663 if (errno != EINTR && errno != EAGAIN) {
664 return -1;
665 }
666 continue;
667 } else {
668 if (single_read) {
669 return ret;
670 }
671 buf += ret;
672 len -= ret;
673 }
674 }
675 return len1 - len;
676}
677
aliguori6f97dba2008-10-31 18:49:55 +0000678#endif /* !_WIN32 */
679
Anthony Liguori96c63842013-03-05 23:21:18 +0530680typedef struct IOWatchPoll
681{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200682 GSource parent;
683
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200684 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530685 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530686
687 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200688 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530689 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530690} IOWatchPoll;
691
Anthony Liguori96c63842013-03-05 23:21:18 +0530692static IOWatchPoll *io_watch_poll_from_source(GSource *source)
693{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200694 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530695}
696
697static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
698{
699 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200700 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200701 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200702 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530703 return FALSE;
704 }
705
Paolo Bonzinid185c092013-04-05 17:59:33 +0200706 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200707 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
708 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200709 g_source_attach(iwp->src, NULL);
710 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200711 g_source_destroy(iwp->src);
712 g_source_unref(iwp->src);
713 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200714 }
715 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530716}
717
718static gboolean io_watch_poll_check(GSource *source)
719{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200720 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530721}
722
723static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
724 gpointer user_data)
725{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200726 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530727}
728
729static void io_watch_poll_finalize(GSource *source)
730{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200731 /* Due to a glib bug, removing the last reference to a source
732 * inside a finalize callback causes recursive locking (and a
733 * deadlock). This is not a problem inside other callbacks,
734 * including dispatch callbacks, so we call io_remove_watch_poll
735 * to remove this source. At this point, iwp->src must
736 * be NULL, or we would leak it.
737 *
738 * This would be solved much more elegantly by child sources,
739 * but we support older glib versions that do not have them.
740 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530741 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200742 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530743}
744
745static GSourceFuncs io_watch_poll_funcs = {
746 .prepare = io_watch_poll_prepare,
747 .check = io_watch_poll_check,
748 .dispatch = io_watch_poll_dispatch,
749 .finalize = io_watch_poll_finalize,
750};
751
752/* Can only be used for read */
753static guint io_add_watch_poll(GIOChannel *channel,
754 IOCanReadHandler *fd_can_read,
755 GIOFunc fd_read,
756 gpointer user_data)
757{
758 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200759 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530760
Paolo Bonzinid185c092013-04-05 17:59:33 +0200761 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530762 iwp->fd_can_read = fd_can_read;
763 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200764 iwp->channel = channel;
765 iwp->fd_read = (GSourceFunc) fd_read;
766 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530767
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200768 tag = g_source_attach(&iwp->parent, NULL);
769 g_source_unref(&iwp->parent);
770 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530771}
772
Paolo Bonzini2b316772013-04-19 17:32:09 +0200773static void io_remove_watch_poll(guint tag)
774{
775 GSource *source;
776 IOWatchPoll *iwp;
777
778 g_return_if_fail (tag > 0);
779
780 source = g_main_context_find_source_by_id(NULL, tag);
781 g_return_if_fail (source != NULL);
782
783 iwp = io_watch_poll_from_source(source);
784 if (iwp->src) {
785 g_source_destroy(iwp->src);
786 g_source_unref(iwp->src);
787 iwp->src = NULL;
788 }
789 g_source_destroy(&iwp->parent);
790}
791
Amit Shah26da70c2013-08-28 15:23:37 +0530792static void remove_fd_in_watch(CharDriverState *chr)
793{
794 if (chr->fd_in_tag) {
795 io_remove_watch_poll(chr->fd_in_tag);
796 chr->fd_in_tag = 0;
797 }
798}
799
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000800#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530801static GIOChannel *io_channel_from_fd(int fd)
802{
803 GIOChannel *chan;
804
805 if (fd == -1) {
806 return NULL;
807 }
808
809 chan = g_io_channel_unix_new(fd);
810
811 g_io_channel_set_encoding(chan, NULL, NULL);
812 g_io_channel_set_buffered(chan, FALSE);
813
814 return chan;
815}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000816#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530817
Anthony Liguori76a96442013-03-05 23:21:21 +0530818static GIOChannel *io_channel_from_socket(int fd)
819{
820 GIOChannel *chan;
821
822 if (fd == -1) {
823 return NULL;
824 }
825
826#ifdef _WIN32
827 chan = g_io_channel_win32_new_socket(fd);
828#else
829 chan = g_io_channel_unix_new(fd);
830#endif
831
832 g_io_channel_set_encoding(chan, NULL, NULL);
833 g_io_channel_set_buffered(chan, FALSE);
834
835 return chan;
836}
837
Anthony Liguori684a0962013-03-29 11:39:50 -0500838static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530839{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200840 size_t offset = 0;
841 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530842
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200843 while (offset < len && status == G_IO_STATUS_NORMAL) {
844 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500845
846 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530847 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500848 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530849 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500850
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200851 if (offset > 0) {
852 return offset;
853 }
854 switch (status) {
855 case G_IO_STATUS_NORMAL:
856 g_assert(len == 0);
857 return 0;
858 case G_IO_STATUS_AGAIN:
859 errno = EAGAIN;
860 return -1;
861 default:
862 break;
863 }
864 errno = EINVAL;
865 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530866}
Anthony Liguori96c63842013-03-05 23:21:18 +0530867
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000868#ifndef _WIN32
869
Anthony Liguoria29753f2013-03-05 23:21:19 +0530870typedef struct FDCharDriver {
871 CharDriverState *chr;
872 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000873 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530874 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000875} FDCharDriver;
876
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200877/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000878static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
879{
880 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530881
Anthony Liguori684a0962013-03-29 11:39:50 -0500882 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530883}
884
885static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
886{
887 CharDriverState *chr = opaque;
888 FDCharDriver *s = chr->opaque;
889 int len;
890 uint8_t buf[READ_BUF_LEN];
891 GIOStatus status;
892 gsize bytes_read;
893
894 len = sizeof(buf);
895 if (len > s->max_size) {
896 len = s->max_size;
897 }
898 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200899 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530900 }
901
902 status = g_io_channel_read_chars(chan, (gchar *)buf,
903 len, &bytes_read, NULL);
904 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530905 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530906 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
907 return FALSE;
908 }
909 if (status == G_IO_STATUS_NORMAL) {
910 qemu_chr_be_write(chr, buf, bytes_read);
911 }
912
913 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000914}
915
916static int fd_chr_read_poll(void *opaque)
917{
918 CharDriverState *chr = opaque;
919 FDCharDriver *s = chr->opaque;
920
Anthony Liguori909cda12011-08-15 11:17:31 -0500921 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000922 return s->max_size;
923}
924
Anthony Liguori23673ca2013-03-05 23:21:23 +0530925static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
926{
927 FDCharDriver *s = chr->opaque;
928 return g_io_create_watch(s->fd_out, cond);
929}
930
aliguori6f97dba2008-10-31 18:49:55 +0000931static void fd_chr_update_read_handler(CharDriverState *chr)
932{
933 FDCharDriver *s = chr->opaque;
934
Amit Shah26da70c2013-08-28 15:23:37 +0530935 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530936 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530937 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
938 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000939 }
940}
941
942static void fd_chr_close(struct CharDriverState *chr)
943{
944 FDCharDriver *s = chr->opaque;
945
Amit Shah26da70c2013-08-28 15:23:37 +0530946 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530947 if (s->fd_in) {
948 g_io_channel_unref(s->fd_in);
949 }
950 if (s->fd_out) {
951 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000952 }
953
Anthony Liguori7267c092011-08-20 22:09:37 -0500954 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100955 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000956}
957
958/* open a character device to a unix fd */
959static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
960{
961 CharDriverState *chr;
962 FDCharDriver *s;
963
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200964 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500965 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530966 s->fd_in = io_channel_from_fd(fd_in);
967 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530968 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530969 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000970 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530971 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000972 chr->chr_write = fd_chr_write;
973 chr->chr_update_read_handler = fd_chr_update_read_handler;
974 chr->chr_close = fd_chr_close;
975
aliguori6f97dba2008-10-31 18:49:55 +0000976 return chr;
977}
978
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100979static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000980{
981 int fd_in, fd_out;
982 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100983 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200984
985 if (filename == NULL) {
986 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100987 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200988 }
aliguori6f97dba2008-10-31 18:49:55 +0000989
990 snprintf(filename_in, 256, "%s.in", filename);
991 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100992 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
993 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000994 if (fd_in < 0 || fd_out < 0) {
995 if (fd_in >= 0)
996 close(fd_in);
997 if (fd_out >= 0)
998 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100999 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001000 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001001 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001002 }
aliguori6f97dba2008-10-31 18:49:55 +00001003 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001004 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001005}
1006
aliguori6f97dba2008-10-31 18:49:55 +00001007/* init terminal so that we can grab keys */
1008static struct termios oldtty;
1009static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001010static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001011
1012static void term_exit(void)
1013{
1014 tcsetattr (0, TCSANOW, &oldtty);
1015 fcntl(0, F_SETFL, old_fd0_flags);
1016}
1017
Paolo Bonzinibb002512010-12-23 13:42:50 +01001018static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001019{
1020 struct termios tty;
1021
Paolo Bonzini03693642010-12-23 13:42:49 +01001022 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001023 if (!echo) {
1024 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001025 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001026 tty.c_oflag |= OPOST;
1027 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1028 tty.c_cflag &= ~(CSIZE|PARENB);
1029 tty.c_cflag |= CS8;
1030 tty.c_cc[VMIN] = 1;
1031 tty.c_cc[VTIME] = 0;
1032 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001033 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001034 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001035
1036 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001037}
1038
1039static void qemu_chr_close_stdio(struct CharDriverState *chr)
1040{
1041 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001042 fd_chr_close(chr);
1043}
1044
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001045static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001046{
1047 CharDriverState *chr;
1048
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001049 if (is_daemonized()) {
1050 error_report("cannot use stdio with -daemonize");
1051 return NULL;
1052 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301053 old_fd0_flags = fcntl(0, F_GETFL);
1054 tcgetattr (0, &oldtty);
1055 fcntl(0, F_SETFL, O_NONBLOCK);
1056 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001057
aliguori6f97dba2008-10-31 18:49:55 +00001058 chr = qemu_chr_open_fd(0, 1);
1059 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001060 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001061 if (opts->has_signal) {
1062 stdio_allow_signal = opts->signal;
1063 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001064 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001065
Markus Armbruster1f514702012-02-07 15:09:08 +01001066 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001067}
1068
aliguori6f97dba2008-10-31 18:49:55 +00001069#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001070 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1071 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001072
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001073#define HAVE_CHARDEV_TTY 1
1074
aliguori6f97dba2008-10-31 18:49:55 +00001075typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301076 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001077 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001078
1079 /* Protected by the CharDriverState chr_write_lock. */
1080 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301081 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001082} PtyCharDriver;
1083
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001084static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001085static void pty_chr_state(CharDriverState *chr, int connected);
1086
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301087static gboolean pty_chr_timer(gpointer opaque)
1088{
1089 struct CharDriverState *chr = opaque;
1090 PtyCharDriver *s = chr->opaque;
1091
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001092 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001093 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001094 if (!s->connected) {
1095 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001096 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001097 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001098 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301099 return FALSE;
1100}
1101
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001102/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301103static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1104{
1105 PtyCharDriver *s = chr->opaque;
1106
1107 if (s->timer_tag) {
1108 g_source_remove(s->timer_tag);
1109 s->timer_tag = 0;
1110 }
1111
1112 if (ms == 1000) {
1113 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1114 } else {
1115 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1116 }
1117}
1118
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001119/* Called with chr_write_lock held. */
1120static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001121{
1122 PtyCharDriver *s = chr->opaque;
1123 GPollFD pfd;
1124
1125 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1126 pfd.events = G_IO_OUT;
1127 pfd.revents = 0;
1128 g_poll(&pfd, 1, 0);
1129 if (pfd.revents & G_IO_HUP) {
1130 pty_chr_state(chr, 0);
1131 } else {
1132 pty_chr_state(chr, 1);
1133 }
1134}
1135
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001136static void pty_chr_update_read_handler(CharDriverState *chr)
1137{
1138 qemu_mutex_lock(&chr->chr_write_lock);
1139 pty_chr_update_read_handler_locked(chr);
1140 qemu_mutex_unlock(&chr->chr_write_lock);
1141}
1142
1143/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001144static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1145{
1146 PtyCharDriver *s = chr->opaque;
1147
1148 if (!s->connected) {
1149 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001150 pty_chr_update_read_handler_locked(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001151 return 0;
1152 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001153 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001154}
1155
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301156static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1157{
1158 PtyCharDriver *s = chr->opaque;
1159 return g_io_create_watch(s->fd, cond);
1160}
1161
aliguori6f97dba2008-10-31 18:49:55 +00001162static int pty_chr_read_poll(void *opaque)
1163{
1164 CharDriverState *chr = opaque;
1165 PtyCharDriver *s = chr->opaque;
1166
Anthony Liguori909cda12011-08-15 11:17:31 -05001167 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001168 return s->read_bytes;
1169}
1170
Anthony Liguori093d3a22013-03-05 23:21:20 +05301171static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001172{
1173 CharDriverState *chr = opaque;
1174 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301175 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301176 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301177 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001178
1179 len = sizeof(buf);
1180 if (len > s->read_bytes)
1181 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001182 if (len == 0) {
1183 return TRUE;
1184 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301185 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1186 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001187 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301188 return FALSE;
1189 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001190 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001191 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001192 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301193 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001194}
1195
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001196/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001197static void pty_chr_state(CharDriverState *chr, int connected)
1198{
1199 PtyCharDriver *s = chr->opaque;
1200
1201 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301202 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001203 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001204 /* (re-)connect poll interval for idle guests: once per second.
1205 * We check more frequently in case the guests sends data to
1206 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301207 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001208 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001209 if (s->timer_tag) {
1210 g_source_remove(s->timer_tag);
1211 s->timer_tag = 0;
1212 }
1213 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001214 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001215 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001216 }
1217 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301218 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1219 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001220 }
aliguori6f97dba2008-10-31 18:49:55 +00001221 }
1222}
1223
aliguori6f97dba2008-10-31 18:49:55 +00001224static void pty_chr_close(struct CharDriverState *chr)
1225{
1226 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301227 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001228
Amit Shah26da70c2013-08-28 15:23:37 +05301229 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301230 fd = g_io_channel_unix_get_fd(s->fd);
1231 g_io_channel_unref(s->fd);
1232 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301233 if (s->timer_tag) {
1234 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001235 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301236 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001237 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001238 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001239}
1240
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001241static CharDriverState *qemu_chr_open_pty(const char *id,
1242 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001243{
1244 CharDriverState *chr;
1245 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001246 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001247 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001248
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001249 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1250 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001251 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001252 }
1253
aliguori6f97dba2008-10-31 18:49:55 +00001254 close(slave_fd);
1255
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001256 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001257
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001258 chr->filename = g_strdup_printf("pty:%s", pty_name);
1259 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001260 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001261
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001262 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001263 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001264
1265 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001266 chr->opaque = s;
1267 chr->chr_write = pty_chr_write;
1268 chr->chr_update_read_handler = pty_chr_update_read_handler;
1269 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301270 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001271 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001272
Anthony Liguori093d3a22013-03-05 23:21:20 +05301273 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301274 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001275
Markus Armbruster1f514702012-02-07 15:09:08 +01001276 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001277}
1278
1279static void tty_serial_init(int fd, int speed,
1280 int parity, int data_bits, int stop_bits)
1281{
1282 struct termios tty;
1283 speed_t spd;
1284
1285#if 0
1286 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1287 speed, parity, data_bits, stop_bits);
1288#endif
1289 tcgetattr (fd, &tty);
1290
Stefan Weil45eea132009-10-26 16:10:10 +01001291#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1292 speed = speed * 10 / 11;
1293 do {
1294 check_speed(50);
1295 check_speed(75);
1296 check_speed(110);
1297 check_speed(134);
1298 check_speed(150);
1299 check_speed(200);
1300 check_speed(300);
1301 check_speed(600);
1302 check_speed(1200);
1303 check_speed(1800);
1304 check_speed(2400);
1305 check_speed(4800);
1306 check_speed(9600);
1307 check_speed(19200);
1308 check_speed(38400);
1309 /* Non-Posix values follow. They may be unsupported on some systems. */
1310 check_speed(57600);
1311 check_speed(115200);
1312#ifdef B230400
1313 check_speed(230400);
1314#endif
1315#ifdef B460800
1316 check_speed(460800);
1317#endif
1318#ifdef B500000
1319 check_speed(500000);
1320#endif
1321#ifdef B576000
1322 check_speed(576000);
1323#endif
1324#ifdef B921600
1325 check_speed(921600);
1326#endif
1327#ifdef B1000000
1328 check_speed(1000000);
1329#endif
1330#ifdef B1152000
1331 check_speed(1152000);
1332#endif
1333#ifdef B1500000
1334 check_speed(1500000);
1335#endif
1336#ifdef B2000000
1337 check_speed(2000000);
1338#endif
1339#ifdef B2500000
1340 check_speed(2500000);
1341#endif
1342#ifdef B3000000
1343 check_speed(3000000);
1344#endif
1345#ifdef B3500000
1346 check_speed(3500000);
1347#endif
1348#ifdef B4000000
1349 check_speed(4000000);
1350#endif
aliguori6f97dba2008-10-31 18:49:55 +00001351 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001352 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001353
1354 cfsetispeed(&tty, spd);
1355 cfsetospeed(&tty, spd);
1356
1357 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1358 |INLCR|IGNCR|ICRNL|IXON);
1359 tty.c_oflag |= OPOST;
1360 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1361 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1362 switch(data_bits) {
1363 default:
1364 case 8:
1365 tty.c_cflag |= CS8;
1366 break;
1367 case 7:
1368 tty.c_cflag |= CS7;
1369 break;
1370 case 6:
1371 tty.c_cflag |= CS6;
1372 break;
1373 case 5:
1374 tty.c_cflag |= CS5;
1375 break;
1376 }
1377 switch(parity) {
1378 default:
1379 case 'N':
1380 break;
1381 case 'E':
1382 tty.c_cflag |= PARENB;
1383 break;
1384 case 'O':
1385 tty.c_cflag |= PARENB | PARODD;
1386 break;
1387 }
1388 if (stop_bits == 2)
1389 tty.c_cflag |= CSTOPB;
1390
1391 tcsetattr (fd, TCSANOW, &tty);
1392}
1393
1394static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1395{
1396 FDCharDriver *s = chr->opaque;
1397
1398 switch(cmd) {
1399 case CHR_IOCTL_SERIAL_SET_PARAMS:
1400 {
1401 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301402 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1403 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001404 ssp->data_bits, ssp->stop_bits);
1405 }
1406 break;
1407 case CHR_IOCTL_SERIAL_SET_BREAK:
1408 {
1409 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301410 if (enable) {
1411 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1412 }
aliguori6f97dba2008-10-31 18:49:55 +00001413 }
1414 break;
1415 case CHR_IOCTL_SERIAL_GET_TIOCM:
1416 {
1417 int sarg = 0;
1418 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301419 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001420 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001421 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001422 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001423 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001424 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001425 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001426 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001427 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001428 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001429 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001430 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001431 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001432 *targ |= CHR_TIOCM_RTS;
1433 }
1434 break;
1435 case CHR_IOCTL_SERIAL_SET_TIOCM:
1436 {
1437 int sarg = *(int *)arg;
1438 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301439 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001440 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1441 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1442 if (sarg & CHR_TIOCM_CTS)
1443 targ |= TIOCM_CTS;
1444 if (sarg & CHR_TIOCM_CAR)
1445 targ |= TIOCM_CAR;
1446 if (sarg & CHR_TIOCM_DSR)
1447 targ |= TIOCM_DSR;
1448 if (sarg & CHR_TIOCM_RI)
1449 targ |= TIOCM_RI;
1450 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001451 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001452 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001453 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301454 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001455 }
1456 break;
1457 default:
1458 return -ENOTSUP;
1459 }
1460 return 0;
1461}
1462
David Ahern4266a132010-02-10 18:27:17 -07001463static void qemu_chr_close_tty(CharDriverState *chr)
1464{
1465 FDCharDriver *s = chr->opaque;
1466 int fd = -1;
1467
1468 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301469 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001470 }
1471
1472 fd_chr_close(chr);
1473
1474 if (fd >= 0) {
1475 close(fd);
1476 }
1477}
1478
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001479static CharDriverState *qemu_chr_open_tty_fd(int fd)
1480{
1481 CharDriverState *chr;
1482
1483 tty_serial_init(fd, 115200, 'N', 8, 1);
1484 chr = qemu_chr_open_fd(fd, fd);
1485 chr->chr_ioctl = tty_serial_ioctl;
1486 chr->chr_close = qemu_chr_close_tty;
1487 return chr;
1488}
aliguori6f97dba2008-10-31 18:49:55 +00001489#endif /* __linux__ || __sun__ */
1490
1491#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001492
1493#define HAVE_CHARDEV_PARPORT 1
1494
aliguori6f97dba2008-10-31 18:49:55 +00001495typedef struct {
1496 int fd;
1497 int mode;
1498} ParallelCharDriver;
1499
1500static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1501{
1502 if (s->mode != mode) {
1503 int m = mode;
1504 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1505 return 0;
1506 s->mode = mode;
1507 }
1508 return 1;
1509}
1510
1511static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1512{
1513 ParallelCharDriver *drv = chr->opaque;
1514 int fd = drv->fd;
1515 uint8_t b;
1516
1517 switch(cmd) {
1518 case CHR_IOCTL_PP_READ_DATA:
1519 if (ioctl(fd, PPRDATA, &b) < 0)
1520 return -ENOTSUP;
1521 *(uint8_t *)arg = b;
1522 break;
1523 case CHR_IOCTL_PP_WRITE_DATA:
1524 b = *(uint8_t *)arg;
1525 if (ioctl(fd, PPWDATA, &b) < 0)
1526 return -ENOTSUP;
1527 break;
1528 case CHR_IOCTL_PP_READ_CONTROL:
1529 if (ioctl(fd, PPRCONTROL, &b) < 0)
1530 return -ENOTSUP;
1531 /* Linux gives only the lowest bits, and no way to know data
1532 direction! For better compatibility set the fixed upper
1533 bits. */
1534 *(uint8_t *)arg = b | 0xc0;
1535 break;
1536 case CHR_IOCTL_PP_WRITE_CONTROL:
1537 b = *(uint8_t *)arg;
1538 if (ioctl(fd, PPWCONTROL, &b) < 0)
1539 return -ENOTSUP;
1540 break;
1541 case CHR_IOCTL_PP_READ_STATUS:
1542 if (ioctl(fd, PPRSTATUS, &b) < 0)
1543 return -ENOTSUP;
1544 *(uint8_t *)arg = b;
1545 break;
1546 case CHR_IOCTL_PP_DATA_DIR:
1547 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1548 return -ENOTSUP;
1549 break;
1550 case CHR_IOCTL_PP_EPP_READ_ADDR:
1551 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1552 struct ParallelIOArg *parg = arg;
1553 int n = read(fd, parg->buffer, parg->count);
1554 if (n != parg->count) {
1555 return -EIO;
1556 }
1557 }
1558 break;
1559 case CHR_IOCTL_PP_EPP_READ:
1560 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1561 struct ParallelIOArg *parg = arg;
1562 int n = read(fd, parg->buffer, parg->count);
1563 if (n != parg->count) {
1564 return -EIO;
1565 }
1566 }
1567 break;
1568 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1569 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1570 struct ParallelIOArg *parg = arg;
1571 int n = write(fd, parg->buffer, parg->count);
1572 if (n != parg->count) {
1573 return -EIO;
1574 }
1575 }
1576 break;
1577 case CHR_IOCTL_PP_EPP_WRITE:
1578 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1579 struct ParallelIOArg *parg = arg;
1580 int n = write(fd, parg->buffer, parg->count);
1581 if (n != parg->count) {
1582 return -EIO;
1583 }
1584 }
1585 break;
1586 default:
1587 return -ENOTSUP;
1588 }
1589 return 0;
1590}
1591
1592static void pp_close(CharDriverState *chr)
1593{
1594 ParallelCharDriver *drv = chr->opaque;
1595 int fd = drv->fd;
1596
1597 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1598 ioctl(fd, PPRELEASE);
1599 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001600 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001601 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001602}
1603
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001604static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001605{
1606 CharDriverState *chr;
1607 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001608
1609 if (ioctl(fd, PPCLAIM) < 0) {
1610 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001611 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001612 }
1613
Anthony Liguori7267c092011-08-20 22:09:37 -05001614 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001615 drv->fd = fd;
1616 drv->mode = IEEE1284_MODE_COMPAT;
1617
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001618 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001619 chr->chr_write = null_chr_write;
1620 chr->chr_ioctl = pp_ioctl;
1621 chr->chr_close = pp_close;
1622 chr->opaque = drv;
1623
Markus Armbruster1f514702012-02-07 15:09:08 +01001624 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001625}
1626#endif /* __linux__ */
1627
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001628#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001629
1630#define HAVE_CHARDEV_PARPORT 1
1631
blueswir16972f932008-11-22 20:49:12 +00001632static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1633{
Stefan Weile0efb992011-02-23 19:09:16 +01001634 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001635 uint8_t b;
1636
1637 switch(cmd) {
1638 case CHR_IOCTL_PP_READ_DATA:
1639 if (ioctl(fd, PPIGDATA, &b) < 0)
1640 return -ENOTSUP;
1641 *(uint8_t *)arg = b;
1642 break;
1643 case CHR_IOCTL_PP_WRITE_DATA:
1644 b = *(uint8_t *)arg;
1645 if (ioctl(fd, PPISDATA, &b) < 0)
1646 return -ENOTSUP;
1647 break;
1648 case CHR_IOCTL_PP_READ_CONTROL:
1649 if (ioctl(fd, PPIGCTRL, &b) < 0)
1650 return -ENOTSUP;
1651 *(uint8_t *)arg = b;
1652 break;
1653 case CHR_IOCTL_PP_WRITE_CONTROL:
1654 b = *(uint8_t *)arg;
1655 if (ioctl(fd, PPISCTRL, &b) < 0)
1656 return -ENOTSUP;
1657 break;
1658 case CHR_IOCTL_PP_READ_STATUS:
1659 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1660 return -ENOTSUP;
1661 *(uint8_t *)arg = b;
1662 break;
1663 default:
1664 return -ENOTSUP;
1665 }
1666 return 0;
1667}
1668
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001669static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001670{
1671 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001672
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001673 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001674 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001675 chr->chr_write = null_chr_write;
1676 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001677 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001678 return chr;
blueswir16972f932008-11-22 20:49:12 +00001679}
1680#endif
1681
aliguori6f97dba2008-10-31 18:49:55 +00001682#else /* _WIN32 */
1683
1684typedef struct {
1685 int max_size;
1686 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001687 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001688 BOOL fpipe;
1689 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001690
1691 /* Protected by the CharDriverState chr_write_lock. */
1692 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001693} WinCharState;
1694
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001695typedef struct {
1696 HANDLE hStdIn;
1697 HANDLE hInputReadyEvent;
1698 HANDLE hInputDoneEvent;
1699 HANDLE hInputThread;
1700 uint8_t win_stdio_buf;
1701} WinStdioCharState;
1702
aliguori6f97dba2008-10-31 18:49:55 +00001703#define NSENDBUF 2048
1704#define NRECVBUF 2048
1705#define MAXCONNECT 1
1706#define NTIMEOUT 5000
1707
1708static int win_chr_poll(void *opaque);
1709static int win_chr_pipe_poll(void *opaque);
1710
1711static void win_chr_close(CharDriverState *chr)
1712{
1713 WinCharState *s = chr->opaque;
1714
1715 if (s->hsend) {
1716 CloseHandle(s->hsend);
1717 s->hsend = NULL;
1718 }
1719 if (s->hrecv) {
1720 CloseHandle(s->hrecv);
1721 s->hrecv = NULL;
1722 }
1723 if (s->hcom) {
1724 CloseHandle(s->hcom);
1725 s->hcom = NULL;
1726 }
1727 if (s->fpipe)
1728 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1729 else
1730 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301731
Hans de Goedea425d232011-11-19 10:22:43 +01001732 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001733}
1734
1735static int win_chr_init(CharDriverState *chr, const char *filename)
1736{
1737 WinCharState *s = chr->opaque;
1738 COMMCONFIG comcfg;
1739 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1740 COMSTAT comstat;
1741 DWORD size;
1742 DWORD err;
1743
1744 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1745 if (!s->hsend) {
1746 fprintf(stderr, "Failed CreateEvent\n");
1747 goto fail;
1748 }
1749 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1750 if (!s->hrecv) {
1751 fprintf(stderr, "Failed CreateEvent\n");
1752 goto fail;
1753 }
1754
1755 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1756 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1757 if (s->hcom == INVALID_HANDLE_VALUE) {
1758 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1759 s->hcom = NULL;
1760 goto fail;
1761 }
1762
1763 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1764 fprintf(stderr, "Failed SetupComm\n");
1765 goto fail;
1766 }
1767
1768 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1769 size = sizeof(COMMCONFIG);
1770 GetDefaultCommConfig(filename, &comcfg, &size);
1771 comcfg.dcb.DCBlength = sizeof(DCB);
1772 CommConfigDialog(filename, NULL, &comcfg);
1773
1774 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1775 fprintf(stderr, "Failed SetCommState\n");
1776 goto fail;
1777 }
1778
1779 if (!SetCommMask(s->hcom, EV_ERR)) {
1780 fprintf(stderr, "Failed SetCommMask\n");
1781 goto fail;
1782 }
1783
1784 cto.ReadIntervalTimeout = MAXDWORD;
1785 if (!SetCommTimeouts(s->hcom, &cto)) {
1786 fprintf(stderr, "Failed SetCommTimeouts\n");
1787 goto fail;
1788 }
1789
1790 if (!ClearCommError(s->hcom, &err, &comstat)) {
1791 fprintf(stderr, "Failed ClearCommError\n");
1792 goto fail;
1793 }
1794 qemu_add_polling_cb(win_chr_poll, chr);
1795 return 0;
1796
1797 fail:
1798 win_chr_close(chr);
1799 return -1;
1800}
1801
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001802/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001803static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1804{
1805 WinCharState *s = chr->opaque;
1806 DWORD len, ret, size, err;
1807
1808 len = len1;
1809 ZeroMemory(&s->osend, sizeof(s->osend));
1810 s->osend.hEvent = s->hsend;
1811 while (len > 0) {
1812 if (s->hsend)
1813 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1814 else
1815 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1816 if (!ret) {
1817 err = GetLastError();
1818 if (err == ERROR_IO_PENDING) {
1819 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1820 if (ret) {
1821 buf += size;
1822 len -= size;
1823 } else {
1824 break;
1825 }
1826 } else {
1827 break;
1828 }
1829 } else {
1830 buf += size;
1831 len -= size;
1832 }
1833 }
1834 return len1 - len;
1835}
1836
1837static int win_chr_read_poll(CharDriverState *chr)
1838{
1839 WinCharState *s = chr->opaque;
1840
Anthony Liguori909cda12011-08-15 11:17:31 -05001841 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001842 return s->max_size;
1843}
1844
1845static void win_chr_readfile(CharDriverState *chr)
1846{
1847 WinCharState *s = chr->opaque;
1848 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301849 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001850 DWORD size;
1851
1852 ZeroMemory(&s->orecv, sizeof(s->orecv));
1853 s->orecv.hEvent = s->hrecv;
1854 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1855 if (!ret) {
1856 err = GetLastError();
1857 if (err == ERROR_IO_PENDING) {
1858 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1859 }
1860 }
1861
1862 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001863 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001864 }
1865}
1866
1867static void win_chr_read(CharDriverState *chr)
1868{
1869 WinCharState *s = chr->opaque;
1870
1871 if (s->len > s->max_size)
1872 s->len = s->max_size;
1873 if (s->len == 0)
1874 return;
1875
1876 win_chr_readfile(chr);
1877}
1878
1879static int win_chr_poll(void *opaque)
1880{
1881 CharDriverState *chr = opaque;
1882 WinCharState *s = chr->opaque;
1883 COMSTAT status;
1884 DWORD comerr;
1885
1886 ClearCommError(s->hcom, &comerr, &status);
1887 if (status.cbInQue > 0) {
1888 s->len = status.cbInQue;
1889 win_chr_read_poll(chr);
1890 win_chr_read(chr);
1891 return 1;
1892 }
1893 return 0;
1894}
1895
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001896static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001897{
1898 CharDriverState *chr;
1899 WinCharState *s;
1900
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001901 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001902 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001903 chr->opaque = s;
1904 chr->chr_write = win_chr_write;
1905 chr->chr_close = win_chr_close;
1906
1907 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001908 g_free(s);
1909 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001910 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001911 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001912 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001913}
1914
1915static int win_chr_pipe_poll(void *opaque)
1916{
1917 CharDriverState *chr = opaque;
1918 WinCharState *s = chr->opaque;
1919 DWORD size;
1920
1921 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1922 if (size > 0) {
1923 s->len = size;
1924 win_chr_read_poll(chr);
1925 win_chr_read(chr);
1926 return 1;
1927 }
1928 return 0;
1929}
1930
1931static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1932{
1933 WinCharState *s = chr->opaque;
1934 OVERLAPPED ov;
1935 int ret;
1936 DWORD size;
1937 char openname[256];
1938
1939 s->fpipe = TRUE;
1940
1941 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1942 if (!s->hsend) {
1943 fprintf(stderr, "Failed CreateEvent\n");
1944 goto fail;
1945 }
1946 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1947 if (!s->hrecv) {
1948 fprintf(stderr, "Failed CreateEvent\n");
1949 goto fail;
1950 }
1951
1952 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1953 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1954 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1955 PIPE_WAIT,
1956 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1957 if (s->hcom == INVALID_HANDLE_VALUE) {
1958 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1959 s->hcom = NULL;
1960 goto fail;
1961 }
1962
1963 ZeroMemory(&ov, sizeof(ov));
1964 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1965 ret = ConnectNamedPipe(s->hcom, &ov);
1966 if (ret) {
1967 fprintf(stderr, "Failed ConnectNamedPipe\n");
1968 goto fail;
1969 }
1970
1971 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1972 if (!ret) {
1973 fprintf(stderr, "Failed GetOverlappedResult\n");
1974 if (ov.hEvent) {
1975 CloseHandle(ov.hEvent);
1976 ov.hEvent = NULL;
1977 }
1978 goto fail;
1979 }
1980
1981 if (ov.hEvent) {
1982 CloseHandle(ov.hEvent);
1983 ov.hEvent = NULL;
1984 }
1985 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1986 return 0;
1987
1988 fail:
1989 win_chr_close(chr);
1990 return -1;
1991}
1992
1993
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001994static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001995{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001996 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001997 CharDriverState *chr;
1998 WinCharState *s;
1999
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002000 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002001 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002002 chr->opaque = s;
2003 chr->chr_write = win_chr_write;
2004 chr->chr_close = win_chr_close;
2005
2006 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002007 g_free(s);
2008 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002009 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002010 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002011 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002012}
2013
Markus Armbruster1f514702012-02-07 15:09:08 +01002014static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002015{
2016 CharDriverState *chr;
2017 WinCharState *s;
2018
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002019 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002020 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002021 s->hcom = fd_out;
2022 chr->opaque = s;
2023 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002024 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002025}
2026
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002027static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002028{
Markus Armbruster1f514702012-02-07 15:09:08 +01002029 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002030}
2031
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002032static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2033{
2034 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2035 DWORD dwSize;
2036 int len1;
2037
2038 len1 = len;
2039
2040 while (len1 > 0) {
2041 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2042 break;
2043 }
2044 buf += dwSize;
2045 len1 -= dwSize;
2046 }
2047
2048 return len - len1;
2049}
2050
2051static void win_stdio_wait_func(void *opaque)
2052{
2053 CharDriverState *chr = opaque;
2054 WinStdioCharState *stdio = chr->opaque;
2055 INPUT_RECORD buf[4];
2056 int ret;
2057 DWORD dwSize;
2058 int i;
2059
Stefan Weildff74242013-12-07 14:48:04 +01002060 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002061
2062 if (!ret) {
2063 /* Avoid error storm */
2064 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2065 return;
2066 }
2067
2068 for (i = 0; i < dwSize; i++) {
2069 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2070
2071 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2072 int j;
2073 if (kev->uChar.AsciiChar != 0) {
2074 for (j = 0; j < kev->wRepeatCount; j++) {
2075 if (qemu_chr_be_can_write(chr)) {
2076 uint8_t c = kev->uChar.AsciiChar;
2077 qemu_chr_be_write(chr, &c, 1);
2078 }
2079 }
2080 }
2081 }
2082 }
2083}
2084
2085static DWORD WINAPI win_stdio_thread(LPVOID param)
2086{
2087 CharDriverState *chr = param;
2088 WinStdioCharState *stdio = chr->opaque;
2089 int ret;
2090 DWORD dwSize;
2091
2092 while (1) {
2093
2094 /* Wait for one byte */
2095 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2096
2097 /* Exit in case of error, continue if nothing read */
2098 if (!ret) {
2099 break;
2100 }
2101 if (!dwSize) {
2102 continue;
2103 }
2104
2105 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2106 if (stdio->win_stdio_buf == '\r') {
2107 continue;
2108 }
2109
2110 /* Signal the main thread and wait until the byte was eaten */
2111 if (!SetEvent(stdio->hInputReadyEvent)) {
2112 break;
2113 }
2114 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2115 != WAIT_OBJECT_0) {
2116 break;
2117 }
2118 }
2119
2120 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2121 return 0;
2122}
2123
2124static void win_stdio_thread_wait_func(void *opaque)
2125{
2126 CharDriverState *chr = opaque;
2127 WinStdioCharState *stdio = chr->opaque;
2128
2129 if (qemu_chr_be_can_write(chr)) {
2130 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2131 }
2132
2133 SetEvent(stdio->hInputDoneEvent);
2134}
2135
2136static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2137{
2138 WinStdioCharState *stdio = chr->opaque;
2139 DWORD dwMode = 0;
2140
2141 GetConsoleMode(stdio->hStdIn, &dwMode);
2142
2143 if (echo) {
2144 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2145 } else {
2146 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2147 }
2148}
2149
2150static void win_stdio_close(CharDriverState *chr)
2151{
2152 WinStdioCharState *stdio = chr->opaque;
2153
2154 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2155 CloseHandle(stdio->hInputReadyEvent);
2156 }
2157 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2158 CloseHandle(stdio->hInputDoneEvent);
2159 }
2160 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2161 TerminateThread(stdio->hInputThread, 0);
2162 }
2163
2164 g_free(chr->opaque);
2165 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002166}
2167
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002168static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002169{
2170 CharDriverState *chr;
2171 WinStdioCharState *stdio;
2172 DWORD dwMode;
2173 int is_console = 0;
2174
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002175 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002176 stdio = g_malloc0(sizeof(WinStdioCharState));
2177
2178 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2179 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2180 fprintf(stderr, "cannot open stdio: invalid handle\n");
2181 exit(1);
2182 }
2183
2184 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2185
2186 chr->opaque = stdio;
2187 chr->chr_write = win_stdio_write;
2188 chr->chr_close = win_stdio_close;
2189
Anthony Liguoried7a1542013-03-05 23:21:17 +05302190 if (is_console) {
2191 if (qemu_add_wait_object(stdio->hStdIn,
2192 win_stdio_wait_func, chr)) {
2193 fprintf(stderr, "qemu_add_wait_object: failed\n");
2194 }
2195 } else {
2196 DWORD dwId;
2197
2198 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2199 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2200 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2201 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002202
Anthony Liguoried7a1542013-03-05 23:21:17 +05302203 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2204 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2205 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2206 fprintf(stderr, "cannot create stdio thread or event\n");
2207 exit(1);
2208 }
2209 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2210 win_stdio_thread_wait_func, chr)) {
2211 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002212 }
2213 }
2214
2215 dwMode |= ENABLE_LINE_INPUT;
2216
Anthony Liguoried7a1542013-03-05 23:21:17 +05302217 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002218 /* set the terminal in raw mode */
2219 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2220 dwMode |= ENABLE_PROCESSED_INPUT;
2221 }
2222
2223 SetConsoleMode(stdio->hStdIn, dwMode);
2224
2225 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2226 qemu_chr_fe_set_echo(chr, false);
2227
Markus Armbruster1f514702012-02-07 15:09:08 +01002228 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002229}
aliguori6f97dba2008-10-31 18:49:55 +00002230#endif /* !_WIN32 */
2231
Anthony Liguori5ab82112013-03-05 23:21:31 +05302232
aliguori6f97dba2008-10-31 18:49:55 +00002233/***********************************************************/
2234/* UDP Net console */
2235
2236typedef struct {
2237 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302238 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302239 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002240 int bufcnt;
2241 int bufptr;
2242 int max_size;
2243} NetCharDriver;
2244
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002245/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002246static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2247{
2248 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302249 gsize bytes_written;
2250 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002251
Anthony Liguori76a96442013-03-05 23:21:21 +05302252 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2253 if (status == G_IO_STATUS_EOF) {
2254 return 0;
2255 } else if (status != G_IO_STATUS_NORMAL) {
2256 return -1;
2257 }
2258
2259 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002260}
2261
2262static int udp_chr_read_poll(void *opaque)
2263{
2264 CharDriverState *chr = opaque;
2265 NetCharDriver *s = chr->opaque;
2266
Anthony Liguori909cda12011-08-15 11:17:31 -05002267 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002268
2269 /* If there were any stray characters in the queue process them
2270 * first
2271 */
2272 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002273 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002274 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002275 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002276 }
2277 return s->max_size;
2278}
2279
Anthony Liguori76a96442013-03-05 23:21:21 +05302280static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002281{
2282 CharDriverState *chr = opaque;
2283 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302284 gsize bytes_read = 0;
2285 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002286
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002287 if (s->max_size == 0) {
2288 return TRUE;
2289 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302290 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2291 &bytes_read, NULL);
2292 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002293 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302294 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302295 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302296 return FALSE;
2297 }
aliguori6f97dba2008-10-31 18:49:55 +00002298
2299 s->bufptr = 0;
2300 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002301 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002302 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002303 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002304 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302305
2306 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002307}
2308
2309static void udp_chr_update_read_handler(CharDriverState *chr)
2310{
2311 NetCharDriver *s = chr->opaque;
2312
Amit Shah26da70c2013-08-28 15:23:37 +05302313 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302314 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302315 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2316 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002317 }
2318}
2319
aliguori819f56b2009-03-28 17:58:14 +00002320static void udp_chr_close(CharDriverState *chr)
2321{
2322 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302323
2324 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302325 if (s->chan) {
2326 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002327 closesocket(s->fd);
2328 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002329 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002330 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002331}
2332
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002333static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002334{
2335 CharDriverState *chr = NULL;
2336 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002337
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002338 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002339 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002340
aliguori6f97dba2008-10-31 18:49:55 +00002341 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302342 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002343 s->bufcnt = 0;
2344 s->bufptr = 0;
2345 chr->opaque = s;
2346 chr->chr_write = udp_chr_write;
2347 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002348 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002349 /* be isn't opened until we get a connection */
2350 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002351 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002352}
aliguori6f97dba2008-10-31 18:49:55 +00002353
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002354static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2355{
2356 Error *local_err = NULL;
2357 int fd = -1;
2358
2359 fd = inet_dgram_opts(opts, &local_err);
2360 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002361 qerror_report_err(local_err);
2362 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002363 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002364 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002365 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002366}
2367
2368/***********************************************************/
2369/* TCP Net console */
2370
2371typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302372
2373 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302374 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002375 int fd, listen_fd;
2376 int connected;
2377 int max_size;
2378 int do_telnetopt;
2379 int do_nodelay;
2380 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002381 int *read_msgfds;
2382 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002383 int *write_msgfds;
2384 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002385} TCPCharDriver;
2386
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302387static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002388
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002389#ifndef _WIN32
2390static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2391{
2392 TCPCharDriver *s = chr->opaque;
2393 struct msghdr msgh;
2394 struct iovec iov;
2395 int r;
2396
2397 size_t fd_size = s->write_msgfds_num * sizeof(int);
2398 char control[CMSG_SPACE(fd_size)];
2399 struct cmsghdr *cmsg;
2400
2401 memset(&msgh, 0, sizeof(msgh));
2402 memset(control, 0, sizeof(control));
2403
2404 /* set the payload */
2405 iov.iov_base = (uint8_t *) buf;
2406 iov.iov_len = len;
2407
2408 msgh.msg_iov = &iov;
2409 msgh.msg_iovlen = 1;
2410
2411 msgh.msg_control = control;
2412 msgh.msg_controllen = sizeof(control);
2413
2414 cmsg = CMSG_FIRSTHDR(&msgh);
2415
2416 cmsg->cmsg_len = CMSG_LEN(fd_size);
2417 cmsg->cmsg_level = SOL_SOCKET;
2418 cmsg->cmsg_type = SCM_RIGHTS;
2419 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2420
2421 do {
2422 r = sendmsg(s->fd, &msgh, 0);
2423 } while (r < 0 && errno == EINTR);
2424
2425 /* free the written msgfds, no matter what */
2426 if (s->write_msgfds_num) {
2427 g_free(s->write_msgfds);
2428 s->write_msgfds = 0;
2429 s->write_msgfds_num = 0;
2430 }
2431
2432 return r;
2433}
2434#endif
2435
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002436/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002437static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2438{
2439 TCPCharDriver *s = chr->opaque;
2440 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002441#ifndef _WIN32
2442 if (s->is_unix && s->write_msgfds_num) {
2443 return unix_send_msgfds(chr, buf, len);
2444 } else
2445#endif
2446 {
2447 return io_channel_send(s->chan, buf, len);
2448 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002449 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002450 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002451 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002452 }
2453}
2454
2455static int tcp_chr_read_poll(void *opaque)
2456{
2457 CharDriverState *chr = opaque;
2458 TCPCharDriver *s = chr->opaque;
2459 if (!s->connected)
2460 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002461 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002462 return s->max_size;
2463}
2464
2465#define IAC 255
2466#define IAC_BREAK 243
2467static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2468 TCPCharDriver *s,
2469 uint8_t *buf, int *size)
2470{
2471 /* Handle any telnet client's basic IAC options to satisfy char by
2472 * char mode with no echo. All IAC options will be removed from
2473 * the buf and the do_telnetopt variable will be used to track the
2474 * state of the width of the IAC information.
2475 *
2476 * IAC commands come in sets of 3 bytes with the exception of the
2477 * "IAC BREAK" command and the double IAC.
2478 */
2479
2480 int i;
2481 int j = 0;
2482
2483 for (i = 0; i < *size; i++) {
2484 if (s->do_telnetopt > 1) {
2485 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2486 /* Double IAC means send an IAC */
2487 if (j != i)
2488 buf[j] = buf[i];
2489 j++;
2490 s->do_telnetopt = 1;
2491 } else {
2492 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2493 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002494 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002495 s->do_telnetopt++;
2496 }
2497 s->do_telnetopt++;
2498 }
2499 if (s->do_telnetopt >= 4) {
2500 s->do_telnetopt = 1;
2501 }
2502 } else {
2503 if ((unsigned char)buf[i] == IAC) {
2504 s->do_telnetopt = 2;
2505 } else {
2506 if (j != i)
2507 buf[j] = buf[i];
2508 j++;
2509 }
2510 }
2511 }
2512 *size = j;
2513}
2514
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002515static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002516{
2517 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002518 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2519
2520 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002521 int i;
2522
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002523 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2524
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002525 /* Close unused fds */
2526 for (i = to_copy; i < s->read_msgfds_num; i++) {
2527 close(s->read_msgfds[i]);
2528 }
2529
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002530 g_free(s->read_msgfds);
2531 s->read_msgfds = 0;
2532 s->read_msgfds_num = 0;
2533 }
2534
2535 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002536}
2537
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002538static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2539{
2540 TCPCharDriver *s = chr->opaque;
2541
2542 /* clear old pending fd array */
2543 if (s->write_msgfds) {
2544 g_free(s->write_msgfds);
2545 }
2546
2547 if (num) {
2548 s->write_msgfds = g_malloc(num * sizeof(int));
2549 memcpy(s->write_msgfds, fds, num * sizeof(int));
2550 }
2551
2552 s->write_msgfds_num = num;
2553
2554 return 0;
2555}
2556
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002557#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002558static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2559{
2560 TCPCharDriver *s = chr->opaque;
2561 struct cmsghdr *cmsg;
2562
2563 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002564 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002565
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002566 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002567 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002568 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002569 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002570 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002571
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002572 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2573
2574 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002575 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002576 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002577
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002578 /* close and clean read_msgfds */
2579 for (i = 0; i < s->read_msgfds_num; i++) {
2580 close(s->read_msgfds[i]);
2581 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002582
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002583 if (s->read_msgfds_num) {
2584 g_free(s->read_msgfds);
2585 }
2586
2587 s->read_msgfds_num = fd_size / sizeof(int);
2588 s->read_msgfds = g_malloc(fd_size);
2589 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2590
2591 for (i = 0; i < s->read_msgfds_num; i++) {
2592 int fd = s->read_msgfds[i];
2593 if (fd < 0) {
2594 continue;
2595 }
2596
2597 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2598 qemu_set_block(fd);
2599
2600 #ifndef MSG_CMSG_CLOEXEC
2601 qemu_set_cloexec(fd);
2602 #endif
2603 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002604 }
2605}
2606
Mark McLoughlin9977c892009-07-22 09:11:38 +01002607static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2608{
2609 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002610 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002611 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002612 union {
2613 struct cmsghdr cmsg;
2614 char control[CMSG_SPACE(sizeof(int))];
2615 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002616 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002617 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002618
2619 iov[0].iov_base = buf;
2620 iov[0].iov_len = len;
2621
2622 msg.msg_iov = iov;
2623 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002624 msg.msg_control = &msg_control;
2625 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002626
Corey Bryant06138652012-08-14 16:43:42 -04002627#ifdef MSG_CMSG_CLOEXEC
2628 flags |= MSG_CMSG_CLOEXEC;
2629#endif
2630 ret = recvmsg(s->fd, &msg, flags);
2631 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002632 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002633 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002634
2635 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002636}
2637#else
2638static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2639{
2640 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002641 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002642}
2643#endif
2644
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302645static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2646{
2647 TCPCharDriver *s = chr->opaque;
2648 return g_io_create_watch(s->chan, cond);
2649}
2650
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002651static void tcp_chr_disconnect(CharDriverState *chr)
2652{
2653 TCPCharDriver *s = chr->opaque;
2654
2655 s->connected = 0;
2656 if (s->listen_chan) {
2657 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2658 tcp_chr_accept, chr);
2659 }
2660 remove_fd_in_watch(chr);
2661 g_io_channel_unref(s->chan);
2662 s->chan = NULL;
2663 closesocket(s->fd);
2664 s->fd = -1;
2665 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2666}
2667
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302668static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002669{
2670 CharDriverState *chr = opaque;
2671 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302672 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002673 int len, size;
2674
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302675 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002676 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302677 }
aliguori6f97dba2008-10-31 18:49:55 +00002678 len = sizeof(buf);
2679 if (len > s->max_size)
2680 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002681 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002682 if (size == 0) {
2683 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002684 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002685 } else if (size > 0) {
2686 if (s->do_telnetopt)
2687 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2688 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002689 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002690 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302691
2692 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002693}
2694
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002695static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2696{
2697 TCPCharDriver *s = chr->opaque;
2698 int size;
2699
2700 if (!s->connected) {
2701 return 0;
2702 }
2703
2704 size = tcp_chr_recv(chr, (void *) buf, len);
2705 if (size == 0) {
2706 /* connection closed */
2707 tcp_chr_disconnect(chr);
2708 }
2709
2710 return size;
2711}
2712
Blue Swirl68c18d12010-08-15 09:46:24 +00002713#ifndef _WIN32
2714CharDriverState *qemu_chr_open_eventfd(int eventfd)
2715{
David Marchande9d21c42014-06-11 17:25:16 +02002716 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2717
2718 if (chr) {
2719 chr->avail_connections = 1;
2720 }
2721
2722 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002723}
Blue Swirl68c18d12010-08-15 09:46:24 +00002724#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002725
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002726static gboolean tcp_chr_chan_close(GIOChannel *channel, GIOCondition cond,
2727 void *opaque)
2728{
2729 CharDriverState *chr = opaque;
2730
2731 if (cond != G_IO_HUP) {
2732 return FALSE;
2733 }
2734
2735 /* connection closed */
2736 tcp_chr_disconnect(chr);
2737 if (chr->fd_hup_tag) {
2738 g_source_remove(chr->fd_hup_tag);
2739 chr->fd_hup_tag = 0;
2740 }
2741
2742 return TRUE;
2743}
2744
aliguori6f97dba2008-10-31 18:49:55 +00002745static void tcp_chr_connect(void *opaque)
2746{
2747 CharDriverState *chr = opaque;
2748 TCPCharDriver *s = chr->opaque;
2749
2750 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302751 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302752 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2753 tcp_chr_read, chr);
Nikolay Nikolaevcdaa86a2014-05-27 15:04:28 +03002754 chr->fd_hup_tag = g_io_add_watch(s->chan, G_IO_HUP, tcp_chr_chan_close,
2755 chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002756 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002757 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002758}
2759
Gal Hammerac1b84d2014-02-25 12:12:35 +02002760static void tcp_chr_update_read_handler(CharDriverState *chr)
2761{
2762 TCPCharDriver *s = chr->opaque;
2763
2764 remove_fd_in_watch(chr);
2765 if (s->chan) {
2766 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2767 tcp_chr_read, chr);
2768 }
2769}
2770
aliguori6f97dba2008-10-31 18:49:55 +00002771#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2772static void tcp_chr_telnet_init(int fd)
2773{
2774 char buf[3];
2775 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2776 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2777 send(fd, (char *)buf, 3, 0);
2778 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2779 send(fd, (char *)buf, 3, 0);
2780 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2781 send(fd, (char *)buf, 3, 0);
2782 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2783 send(fd, (char *)buf, 3, 0);
2784}
2785
Daniel P. Berrange13661082011-06-23 13:31:42 +01002786static int tcp_chr_add_client(CharDriverState *chr, int fd)
2787{
2788 TCPCharDriver *s = chr->opaque;
2789 if (s->fd != -1)
2790 return -1;
2791
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002792 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002793 if (s->do_nodelay)
2794 socket_set_nodelay(fd);
2795 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302796 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002797 if (s->listen_tag) {
2798 g_source_remove(s->listen_tag);
2799 s->listen_tag = 0;
2800 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002801 tcp_chr_connect(chr);
2802
2803 return 0;
2804}
2805
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302806static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002807{
2808 CharDriverState *chr = opaque;
2809 TCPCharDriver *s = chr->opaque;
2810 struct sockaddr_in saddr;
2811#ifndef _WIN32
2812 struct sockaddr_un uaddr;
2813#endif
2814 struct sockaddr *addr;
2815 socklen_t len;
2816 int fd;
2817
2818 for(;;) {
2819#ifndef _WIN32
2820 if (s->is_unix) {
2821 len = sizeof(uaddr);
2822 addr = (struct sockaddr *)&uaddr;
2823 } else
2824#endif
2825 {
2826 len = sizeof(saddr);
2827 addr = (struct sockaddr *)&saddr;
2828 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002829 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002830 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002831 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302832 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002833 } else if (fd >= 0) {
2834 if (s->do_telnetopt)
2835 tcp_chr_telnet_init(fd);
2836 break;
2837 }
2838 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002839 if (tcp_chr_add_client(chr, fd) < 0)
2840 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302841
2842 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002843}
2844
2845static void tcp_chr_close(CharDriverState *chr)
2846{
2847 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002848 int i;
aliguori819f56b2009-03-28 17:58:14 +00002849 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302850 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302851 if (s->chan) {
2852 g_io_channel_unref(s->chan);
2853 }
aliguori6f97dba2008-10-31 18:49:55 +00002854 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002855 }
2856 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302857 if (s->listen_tag) {
2858 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002859 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302860 }
2861 if (s->listen_chan) {
2862 g_io_channel_unref(s->listen_chan);
2863 }
aliguori6f97dba2008-10-31 18:49:55 +00002864 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002865 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002866 if (s->read_msgfds_num) {
2867 for (i = 0; i < s->read_msgfds_num; i++) {
2868 close(s->read_msgfds[i]);
2869 }
2870 g_free(s->read_msgfds);
2871 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002872 if (s->write_msgfds_num) {
2873 g_free(s->write_msgfds);
2874 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002875 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002876 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002877}
2878
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002879static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2880 bool is_listen, bool is_telnet,
2881 bool is_waitconnect,
2882 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002883{
2884 CharDriverState *chr = NULL;
2885 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002886 char host[NI_MAXHOST], serv[NI_MAXSERV];
2887 const char *left = "", *right = "";
2888 struct sockaddr_storage ss;
2889 socklen_t ss_len = sizeof(ss);
2890
2891 memset(&ss, 0, ss_len);
2892 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002893 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002894 return NULL;
2895 }
2896
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002897 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002898 s = g_malloc0(sizeof(TCPCharDriver));
2899
2900 s->connected = 0;
2901 s->fd = -1;
2902 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002903 s->read_msgfds = 0;
2904 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002905 s->write_msgfds = 0;
2906 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002907
2908 chr->filename = g_malloc(256);
2909 switch (ss.ss_family) {
2910#ifndef _WIN32
2911 case AF_UNIX:
2912 s->is_unix = 1;
2913 snprintf(chr->filename, 256, "unix:%s%s",
2914 ((struct sockaddr_un *)(&ss))->sun_path,
2915 is_listen ? ",server" : "");
2916 break;
2917#endif
2918 case AF_INET6:
2919 left = "[";
2920 right = "]";
2921 /* fall through */
2922 case AF_INET:
2923 s->do_nodelay = do_nodelay;
2924 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2925 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002926 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002927 is_telnet ? "telnet" : "tcp",
2928 left, host, right, serv,
2929 is_listen ? ",server" : "");
2930 break;
2931 }
2932
2933 chr->opaque = s;
2934 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002935 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002936 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002937 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002938 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002939 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302940 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002941 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002942 /* be isn't opened until we get a connection */
2943 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002944
2945 if (is_listen) {
2946 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302947 s->listen_chan = io_channel_from_socket(s->listen_fd);
2948 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002949 if (is_telnet) {
2950 s->do_telnetopt = 1;
2951 }
2952 } else {
2953 s->connected = 1;
2954 s->fd = fd;
2955 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302956 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002957 tcp_chr_connect(chr);
2958 }
2959
2960 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002961 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2962 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302963 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002964 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002965 }
2966 return chr;
2967}
2968
2969static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2970{
2971 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002972 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002973 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002974
liguange990a392013-06-18 11:45:35 +08002975 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2976 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2977 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2978 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2979 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002980
aliguorif07b6002008-11-11 20:54:09 +00002981 if (is_unix) {
2982 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002983 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002984 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002985 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002986 }
2987 } else {
2988 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002989 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002990 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002991 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002992 }
2993 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002994 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002995 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002996 }
aliguori6f97dba2008-10-31 18:49:55 +00002997
2998 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002999 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00003000
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003001 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
3002 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003003 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003004 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00003005 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003006 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003007
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003008
aliguori6f97dba2008-10-31 18:49:55 +00003009 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003010 if (local_err) {
3011 qerror_report_err(local_err);
3012 error_free(local_err);
3013 }
3014 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00003015 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02003016 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003017 if (chr) {
3018 g_free(chr->opaque);
3019 g_free(chr);
3020 }
Markus Armbruster1f514702012-02-07 15:09:08 +01003021 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003022}
3023
Lei Li51767e72013-01-25 00:03:19 +08003024/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003025/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003026
3027typedef struct {
3028 size_t size;
3029 size_t prod;
3030 size_t cons;
3031 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003032} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003033
Markus Armbruster3949e592013-02-06 21:27:24 +01003034static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003035{
Markus Armbruster3949e592013-02-06 21:27:24 +01003036 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003037
Markus Armbruster5c230102013-02-06 21:27:23 +01003038 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003039}
3040
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003041/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003042static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003043{
Markus Armbruster3949e592013-02-06 21:27:24 +01003044 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003045 int i;
3046
3047 if (!buf || (len < 0)) {
3048 return -1;
3049 }
3050
3051 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003052 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3053 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003054 d->cons = d->prod - d->size;
3055 }
3056 }
3057
3058 return 0;
3059}
3060
Markus Armbruster3949e592013-02-06 21:27:24 +01003061static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003062{
Markus Armbruster3949e592013-02-06 21:27:24 +01003063 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003064 int i;
3065
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003066 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003067 for (i = 0; i < len && d->cons != d->prod; i++) {
3068 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003069 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003070 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003071
3072 return i;
3073}
3074
Markus Armbruster3949e592013-02-06 21:27:24 +01003075static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003076{
Markus Armbruster3949e592013-02-06 21:27:24 +01003077 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003078
3079 g_free(d->cbuf);
3080 g_free(d);
3081 chr->opaque = NULL;
3082}
3083
Markus Armbruster4f573782013-07-26 16:44:32 +02003084static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3085 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003086{
3087 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003088 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003089
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003090 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003091 d = g_malloc(sizeof(*d));
3092
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003093 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003094
3095 /* The size must be power of 2 */
3096 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003097 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003098 goto fail;
3099 }
3100
3101 d->prod = 0;
3102 d->cons = 0;
3103 d->cbuf = g_malloc0(d->size);
3104
3105 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003106 chr->chr_write = ringbuf_chr_write;
3107 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003108
3109 return chr;
3110
3111fail:
3112 g_free(d);
3113 g_free(chr);
3114 return NULL;
3115}
3116
Hani Benhabiles8e597772014-05-27 23:39:30 +01003117bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003118{
Markus Armbruster3949e592013-02-06 21:27:24 +01003119 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003120}
3121
Markus Armbruster3949e592013-02-06 21:27:24 +01003122void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003123 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003124 Error **errp)
3125{
3126 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003127 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003128 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003129 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003130
3131 chr = qemu_chr_find(device);
3132 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003133 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003134 return;
3135 }
3136
Markus Armbruster3949e592013-02-06 21:27:24 +01003137 if (!chr_is_ringbuf(chr)) {
3138 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003139 return;
3140 }
3141
Lei Li1f590cf2013-01-25 00:03:20 +08003142 if (has_format && (format == DATA_FORMAT_BASE64)) {
3143 write_data = g_base64_decode(data, &write_count);
3144 } else {
3145 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003146 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003147 }
3148
Markus Armbruster3949e592013-02-06 21:27:24 +01003149 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003150
Markus Armbruster13289fb2013-02-06 21:27:18 +01003151 if (write_data != (uint8_t *)data) {
3152 g_free((void *)write_data);
3153 }
3154
Lei Li1f590cf2013-01-25 00:03:20 +08003155 if (ret < 0) {
3156 error_setg(errp, "Failed to write to device %s", device);
3157 return;
3158 }
3159}
3160
Markus Armbruster3949e592013-02-06 21:27:24 +01003161char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003162 bool has_format, enum DataFormat format,
3163 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003164{
3165 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003166 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003167 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003168 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003169
3170 chr = qemu_chr_find(device);
3171 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003172 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003173 return NULL;
3174 }
3175
Markus Armbruster3949e592013-02-06 21:27:24 +01003176 if (!chr_is_ringbuf(chr)) {
3177 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003178 return NULL;
3179 }
3180
3181 if (size <= 0) {
3182 error_setg(errp, "size must be greater than zero");
3183 return NULL;
3184 }
3185
Markus Armbruster3949e592013-02-06 21:27:24 +01003186 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003187 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003188 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003189
Markus Armbruster3949e592013-02-06 21:27:24 +01003190 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003191
3192 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003193 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003194 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003195 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003196 /*
3197 * FIXME should read only complete, valid UTF-8 characters up
3198 * to @size bytes. Invalid sequences should be replaced by a
3199 * suitable replacement character. Except when (and only
3200 * when) ring buffer lost characters since last read, initial
3201 * continuation characters should be dropped.
3202 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003203 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003204 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003205 }
3206
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003207 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003208}
3209
Gerd Hoffmann33521632009-12-08 13:11:49 +01003210QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003211{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003212 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003213 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003214 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003215 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003216 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003217
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003218 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003219 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003220 qerror_report_err(local_err);
3221 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003222 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003223 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003224
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003225 if (strstart(filename, "mon:", &p)) {
3226 filename = p;
3227 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003228 if (strcmp(filename, "stdio") == 0) {
3229 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3230 * but pass it to the guest. Handle this only for compat syntax,
3231 * for -chardev syntax we have special option for this.
3232 * This is what -nographic did, redirecting+muxing serial+monitor
3233 * to stdio causing Ctrl+C to be passed to guest. */
3234 qemu_opt_set(opts, "signal", "off");
3235 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003236 }
3237
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003238 if (strcmp(filename, "null") == 0 ||
3239 strcmp(filename, "pty") == 0 ||
3240 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003241 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003242 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003243 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003244 return opts;
3245 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003246 if (strstart(filename, "vc", &p)) {
3247 qemu_opt_set(opts, "backend", "vc");
3248 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003249 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003250 /* pixels */
3251 qemu_opt_set(opts, "width", width);
3252 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003253 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003254 /* chars */
3255 qemu_opt_set(opts, "cols", width);
3256 qemu_opt_set(opts, "rows", height);
3257 } else {
3258 goto fail;
3259 }
3260 }
3261 return opts;
3262 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003263 if (strcmp(filename, "con:") == 0) {
3264 qemu_opt_set(opts, "backend", "console");
3265 return opts;
3266 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003267 if (strstart(filename, "COM", NULL)) {
3268 qemu_opt_set(opts, "backend", "serial");
3269 qemu_opt_set(opts, "path", filename);
3270 return opts;
3271 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003272 if (strstart(filename, "file:", &p)) {
3273 qemu_opt_set(opts, "backend", "file");
3274 qemu_opt_set(opts, "path", p);
3275 return opts;
3276 }
3277 if (strstart(filename, "pipe:", &p)) {
3278 qemu_opt_set(opts, "backend", "pipe");
3279 qemu_opt_set(opts, "path", p);
3280 return opts;
3281 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003282 if (strstart(filename, "tcp:", &p) ||
3283 strstart(filename, "telnet:", &p)) {
3284 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3285 host[0] = 0;
3286 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3287 goto fail;
3288 }
3289 qemu_opt_set(opts, "backend", "socket");
3290 qemu_opt_set(opts, "host", host);
3291 qemu_opt_set(opts, "port", port);
3292 if (p[pos] == ',') {
3293 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3294 goto fail;
3295 }
3296 if (strstart(filename, "telnet:", &p))
3297 qemu_opt_set(opts, "telnet", "on");
3298 return opts;
3299 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003300 if (strstart(filename, "udp:", &p)) {
3301 qemu_opt_set(opts, "backend", "udp");
3302 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3303 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003304 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003305 goto fail;
3306 }
3307 }
3308 qemu_opt_set(opts, "host", host);
3309 qemu_opt_set(opts, "port", port);
3310 if (p[pos] == '@') {
3311 p += pos + 1;
3312 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3313 host[0] = 0;
3314 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003315 goto fail;
3316 }
3317 }
3318 qemu_opt_set(opts, "localaddr", host);
3319 qemu_opt_set(opts, "localport", port);
3320 }
3321 return opts;
3322 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003323 if (strstart(filename, "unix:", &p)) {
3324 qemu_opt_set(opts, "backend", "socket");
3325 if (qemu_opts_do_parse(opts, p, "path") != 0)
3326 goto fail;
3327 return opts;
3328 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003329 if (strstart(filename, "/dev/parport", NULL) ||
3330 strstart(filename, "/dev/ppi", NULL)) {
3331 qemu_opt_set(opts, "backend", "parport");
3332 qemu_opt_set(opts, "path", filename);
3333 return opts;
3334 }
3335 if (strstart(filename, "/dev/", NULL)) {
3336 qemu_opt_set(opts, "backend", "tty");
3337 qemu_opt_set(opts, "path", filename);
3338 return opts;
3339 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003340
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003341fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003342 qemu_opts_del(opts);
3343 return NULL;
3344}
3345
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003346static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3347 Error **errp)
3348{
3349 const char *path = qemu_opt_get(opts, "path");
3350
3351 if (path == NULL) {
3352 error_setg(errp, "chardev: file: no filename given");
3353 return;
3354 }
3355 backend->file = g_new0(ChardevFile, 1);
3356 backend->file->out = g_strdup(path);
3357}
3358
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003359static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3360 Error **errp)
3361{
3362 backend->stdio = g_new0(ChardevStdio, 1);
3363 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003364 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003365}
3366
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003367static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3368 Error **errp)
3369{
3370 const char *device = qemu_opt_get(opts, "path");
3371
3372 if (device == NULL) {
3373 error_setg(errp, "chardev: serial/tty: no device path given");
3374 return;
3375 }
3376 backend->serial = g_new0(ChardevHostdev, 1);
3377 backend->serial->device = g_strdup(device);
3378}
3379
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003380static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3381 Error **errp)
3382{
3383 const char *device = qemu_opt_get(opts, "path");
3384
3385 if (device == NULL) {
3386 error_setg(errp, "chardev: parallel: no device path given");
3387 return;
3388 }
3389 backend->parallel = g_new0(ChardevHostdev, 1);
3390 backend->parallel->device = g_strdup(device);
3391}
3392
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003393static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3394 Error **errp)
3395{
3396 const char *device = qemu_opt_get(opts, "path");
3397
3398 if (device == NULL) {
3399 error_setg(errp, "chardev: pipe: no device path given");
3400 return;
3401 }
3402 backend->pipe = g_new0(ChardevHostdev, 1);
3403 backend->pipe->device = g_strdup(device);
3404}
3405
Markus Armbruster4f573782013-07-26 16:44:32 +02003406static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3407 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003408{
3409 int val;
3410
Markus Armbruster3a1da422013-07-26 16:44:34 +02003411 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003412
Markus Armbruster0f953052013-06-27 16:22:07 +02003413 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003414 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003415 backend->ringbuf->has_size = true;
3416 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003417 }
3418}
3419
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003420static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3421 Error **errp)
3422{
3423 const char *chardev = qemu_opt_get(opts, "chardev");
3424
3425 if (chardev == NULL) {
3426 error_setg(errp, "chardev: mux: no chardev given");
3427 return;
3428 }
3429 backend->mux = g_new0(ChardevMux, 1);
3430 backend->mux->chardev = g_strdup(chardev);
3431}
3432
Anthony Liguorid654f342013-03-05 23:21:28 +05303433typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003434 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003435 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003436 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003437 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003438 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003439 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303440} CharDriver;
3441
3442static GSList *backends;
3443
3444void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3445{
3446 CharDriver *s;
3447
3448 s = g_malloc0(sizeof(*s));
3449 s->name = g_strdup(name);
3450 s->open = open;
3451
3452 backends = g_slist_append(backends, s);
3453}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003454
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003455void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003456 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3457{
3458 CharDriver *s;
3459
3460 s = g_malloc0(sizeof(*s));
3461 s->name = g_strdup(name);
3462 s->kind = kind;
3463 s->parse = parse;
3464
3465 backends = g_slist_append(backends, s);
3466}
3467
Anthony Liguorif69554b2011-08-15 11:17:37 -05003468CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003469 void (*init)(struct CharDriverState *s),
3470 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003471{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003472 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303473 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003474 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303475 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003476
3477 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003478 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003479 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003480 }
3481
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003482 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003483 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003484 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003485 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003486 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303487 for (i = backends; i; i = i->next) {
3488 cd = i->data;
3489
3490 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003491 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303492 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003493 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303494 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003495 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003496 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003497 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003498 }
3499
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003500 if (!cd->open) {
3501 /* using new, qapi init */
3502 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3503 ChardevReturn *ret = NULL;
3504 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003505 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003506
3507 if (qemu_opt_get_bool(opts, "mux", 0)) {
3508 bid = g_strdup_printf("%s-base", id);
3509 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003510
3511 chr = NULL;
3512 backend->kind = cd->kind;
3513 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003514 cd->parse(opts, backend, &local_err);
3515 if (local_err) {
3516 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003517 goto qapi_out;
3518 }
3519 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003520 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003521 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003522 goto qapi_out;
3523 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003524
3525 if (bid) {
3526 qapi_free_ChardevBackend(backend);
3527 qapi_free_ChardevReturn(ret);
3528 backend = g_new0(ChardevBackend, 1);
3529 backend->mux = g_new0(ChardevMux, 1);
3530 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3531 backend->mux->chardev = g_strdup(bid);
3532 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003533 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003534 chr = qemu_chr_find(bid);
3535 qemu_chr_delete(chr);
3536 chr = NULL;
3537 goto qapi_out;
3538 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003539 }
3540
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003541 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003542 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003543
3544 qapi_out:
3545 qapi_free_ChardevBackend(backend);
3546 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003547 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003548 return chr;
3549 }
3550
Anthony Liguorid654f342013-03-05 23:21:28 +05303551 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003552 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003553 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003554 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003555 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003556 }
3557
3558 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003559 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003560 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003561 /* if we didn't create the chardev via qmp_chardev_add, we
3562 * need to send the OPENED event here
3563 */
3564 if (!chr->explicit_be_open) {
3565 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3566 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003567 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003568
3569 if (qemu_opt_get_bool(opts, "mux", 0)) {
3570 CharDriverState *base = chr;
3571 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003572 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003573 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3574 chr = qemu_chr_open_mux(base);
3575 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003576 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003577 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003578 } else {
3579 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003580 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003581 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003582 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003583 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003584
3585err:
3586 qemu_opts_del(opts);
3587 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003588}
3589
Anthony Liguori27143a42011-08-15 11:17:36 -05003590CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003591{
3592 const char *p;
3593 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003594 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003595 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003596
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003597 if (strstart(filename, "chardev:", &p)) {
3598 return qemu_chr_find(p);
3599 }
3600
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003601 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003602 if (!opts)
3603 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003604
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003605 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003606 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003607 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003608 error_free(err);
3609 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003610 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003611 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003612 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003613 }
3614 return chr;
3615}
3616
Anthony Liguori15f31512011-08-15 11:17:35 -05003617void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003618{
3619 if (chr->chr_set_echo) {
3620 chr->chr_set_echo(chr, echo);
3621 }
3622}
3623
Hans de Goede8e25daa2013-03-26 11:07:57 +01003624void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003625{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003626 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003627 return;
3628 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003629 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003630 if (chr->chr_set_fe_open) {
3631 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003632 }
3633}
3634
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003635void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3636{
3637 if (chr->chr_fe_event) {
3638 chr->chr_fe_event(chr, event);
3639 }
3640}
3641
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003642int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3643 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303644{
3645 GSource *src;
3646 guint tag;
3647
3648 if (s->chr_add_watch == NULL) {
3649 return -ENOSYS;
3650 }
3651
3652 src = s->chr_add_watch(s, cond);
3653 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3654 tag = g_source_attach(src, NULL);
3655 g_source_unref(src);
3656
3657 return tag;
3658}
3659
Hans de Goede44c473d2013-03-27 20:29:39 +01003660int qemu_chr_fe_claim(CharDriverState *s)
3661{
3662 if (s->avail_connections < 1) {
3663 return -1;
3664 }
3665 s->avail_connections--;
3666 return 0;
3667}
3668
3669void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3670{
3671 if (qemu_chr_fe_claim(s) != 0) {
3672 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3673 __func__, s->label);
3674 exit(1);
3675 }
3676}
3677
3678void qemu_chr_fe_release(CharDriverState *s)
3679{
3680 s->avail_connections++;
3681}
3682
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003683void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003684{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003685 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003686 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003687 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003688 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003689 g_free(chr->filename);
3690 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003691 if (chr->opts) {
3692 qemu_opts_del(chr->opts);
3693 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003694 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003695}
3696
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003697ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003698{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003699 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003700 CharDriverState *chr;
3701
Blue Swirl72cf2d42009-09-12 07:36:22 +00003702 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003703 ChardevInfoList *info = g_malloc0(sizeof(*info));
3704 info->value = g_malloc0(sizeof(*info->value));
3705 info->value->label = g_strdup(chr->label);
3706 info->value->filename = g_strdup(chr->filename);
3707
3708 info->next = chr_list;
3709 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003710 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003711
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003712 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003713}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003714
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003715ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3716{
3717 ChardevBackendInfoList *backend_list = NULL;
3718 CharDriver *c = NULL;
3719 GSList *i = NULL;
3720
3721 for (i = backends; i; i = i->next) {
3722 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3723 c = i->data;
3724 info->value = g_malloc0(sizeof(*info->value));
3725 info->value->name = g_strdup(c->name);
3726
3727 info->next = backend_list;
3728 backend_list = info;
3729 }
3730
3731 return backend_list;
3732}
3733
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003734CharDriverState *qemu_chr_find(const char *name)
3735{
3736 CharDriverState *chr;
3737
Blue Swirl72cf2d42009-09-12 07:36:22 +00003738 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003739 if (strcmp(chr->label, name) != 0)
3740 continue;
3741 return chr;
3742 }
3743 return NULL;
3744}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003745
3746/* Get a character (serial) device interface. */
3747CharDriverState *qemu_char_get_next_serial(void)
3748{
3749 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003750 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003751
3752 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003753
3754 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3755 chr = serial_hds[next_serial++];
3756 qemu_chr_fe_claim_no_fail(chr);
3757 return chr;
3758 }
3759 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003760}
3761
Paolo Bonzini4d454572012-11-26 16:03:42 +01003762QemuOptsList qemu_chardev_opts = {
3763 .name = "chardev",
3764 .implied_opt_name = "backend",
3765 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3766 .desc = {
3767 {
3768 .name = "backend",
3769 .type = QEMU_OPT_STRING,
3770 },{
3771 .name = "path",
3772 .type = QEMU_OPT_STRING,
3773 },{
3774 .name = "host",
3775 .type = QEMU_OPT_STRING,
3776 },{
3777 .name = "port",
3778 .type = QEMU_OPT_STRING,
3779 },{
3780 .name = "localaddr",
3781 .type = QEMU_OPT_STRING,
3782 },{
3783 .name = "localport",
3784 .type = QEMU_OPT_STRING,
3785 },{
3786 .name = "to",
3787 .type = QEMU_OPT_NUMBER,
3788 },{
3789 .name = "ipv4",
3790 .type = QEMU_OPT_BOOL,
3791 },{
3792 .name = "ipv6",
3793 .type = QEMU_OPT_BOOL,
3794 },{
3795 .name = "wait",
3796 .type = QEMU_OPT_BOOL,
3797 },{
3798 .name = "server",
3799 .type = QEMU_OPT_BOOL,
3800 },{
3801 .name = "delay",
3802 .type = QEMU_OPT_BOOL,
3803 },{
3804 .name = "telnet",
3805 .type = QEMU_OPT_BOOL,
3806 },{
3807 .name = "width",
3808 .type = QEMU_OPT_NUMBER,
3809 },{
3810 .name = "height",
3811 .type = QEMU_OPT_NUMBER,
3812 },{
3813 .name = "cols",
3814 .type = QEMU_OPT_NUMBER,
3815 },{
3816 .name = "rows",
3817 .type = QEMU_OPT_NUMBER,
3818 },{
3819 .name = "mux",
3820 .type = QEMU_OPT_BOOL,
3821 },{
3822 .name = "signal",
3823 .type = QEMU_OPT_BOOL,
3824 },{
3825 .name = "name",
3826 .type = QEMU_OPT_STRING,
3827 },{
3828 .name = "debug",
3829 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003830 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003831 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003832 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003833 },{
3834 .name = "chardev",
3835 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003836 },
3837 { /* end of list */ }
3838 },
3839};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003840
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003841#ifdef _WIN32
3842
3843static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3844{
3845 HANDLE out;
3846
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003847 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003848 error_setg(errp, "input file not supported");
3849 return NULL;
3850 }
3851
3852 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3853 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3854 if (out == INVALID_HANDLE_VALUE) {
3855 error_setg(errp, "open %s failed", file->out);
3856 return NULL;
3857 }
3858 return qemu_chr_open_win_file(out);
3859}
3860
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003861static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3862 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003863{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003864 return qemu_chr_open_win_path(serial->device);
3865}
3866
3867static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3868 Error **errp)
3869{
3870 error_setg(errp, "character device backend type 'parallel' not supported");
3871 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003872}
3873
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003874#else /* WIN32 */
3875
3876static int qmp_chardev_open_file_source(char *src, int flags,
3877 Error **errp)
3878{
3879 int fd = -1;
3880
3881 TFR(fd = qemu_open(src, flags, 0666));
3882 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003883 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003884 }
3885 return fd;
3886}
3887
3888static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3889{
Markus Armbruster5f758362014-05-19 18:57:34 +02003890 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003891
3892 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3893 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003894 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003895 return NULL;
3896 }
3897
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003898 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003899 flags = O_RDONLY;
3900 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003901 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003902 qemu_close(out);
3903 return NULL;
3904 }
3905 }
3906
3907 return qemu_chr_open_fd(in, out);
3908}
3909
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003910static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3911 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003912{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003913#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003914 int fd;
3915
3916 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003917 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003918 return NULL;
3919 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003920 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003921 return qemu_chr_open_tty_fd(fd);
3922#else
3923 error_setg(errp, "character device backend type 'serial' not supported");
3924 return NULL;
3925#endif
3926}
3927
3928static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3929 Error **errp)
3930{
3931#ifdef HAVE_CHARDEV_PARPORT
3932 int fd;
3933
3934 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003935 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003936 return NULL;
3937 }
3938 return qemu_chr_open_pp_fd(fd);
3939#else
3940 error_setg(errp, "character device backend type 'parallel' not supported");
3941 return NULL;
3942#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003943}
3944
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003945#endif /* WIN32 */
3946
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003947static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3948 Error **errp)
3949{
3950 SocketAddress *addr = sock->addr;
3951 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3952 bool is_listen = sock->has_server ? sock->server : true;
3953 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3954 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3955 int fd;
3956
3957 if (is_listen) {
3958 fd = socket_listen(addr, errp);
3959 } else {
3960 fd = socket_connect(addr, errp, NULL, NULL);
3961 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003962 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003963 return NULL;
3964 }
3965 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3966 is_telnet, is_waitconnect, errp);
3967}
3968
Lei Li08d0ab32013-05-20 14:51:03 +08003969static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3970 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003971{
3972 int fd;
3973
Lei Li08d0ab32013-05-20 14:51:03 +08003974 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003975 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003976 return NULL;
3977 }
3978 return qemu_chr_open_udp_fd(fd);
3979}
3980
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003981ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3982 Error **errp)
3983{
3984 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003985 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003986
3987 chr = qemu_chr_find(id);
3988 if (chr) {
3989 error_setg(errp, "Chardev '%s' already exists", id);
3990 g_free(ret);
3991 return NULL;
3992 }
3993
3994 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003995 case CHARDEV_BACKEND_KIND_FILE:
3996 chr = qmp_chardev_open_file(backend->file, errp);
3997 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003998 case CHARDEV_BACKEND_KIND_SERIAL:
3999 chr = qmp_chardev_open_serial(backend->serial, errp);
4000 break;
4001 case CHARDEV_BACKEND_KIND_PARALLEL:
4002 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004003 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004004 case CHARDEV_BACKEND_KIND_PIPE:
4005 chr = qemu_chr_open_pipe(backend->pipe);
4006 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004007 case CHARDEV_BACKEND_KIND_SOCKET:
4008 chr = qmp_chardev_open_socket(backend->socket, errp);
4009 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004010 case CHARDEV_BACKEND_KIND_UDP:
4011 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004012 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004013#ifdef HAVE_CHARDEV_TTY
4014 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004015 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004016 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004017#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004018 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004019 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004020 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004021 case CHARDEV_BACKEND_KIND_MUX:
4022 base = qemu_chr_find(backend->mux->chardev);
4023 if (base == NULL) {
4024 error_setg(errp, "mux: base chardev %s not found",
4025 backend->mux->chardev);
4026 break;
4027 }
4028 chr = qemu_chr_open_mux(base);
4029 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004030 case CHARDEV_BACKEND_KIND_MSMOUSE:
4031 chr = qemu_chr_open_msmouse();
4032 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004033#ifdef CONFIG_BRLAPI
4034 case CHARDEV_BACKEND_KIND_BRAILLE:
4035 chr = chr_baum_init();
4036 break;
4037#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004038 case CHARDEV_BACKEND_KIND_STDIO:
4039 chr = qemu_chr_open_stdio(backend->stdio);
4040 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004041#ifdef _WIN32
4042 case CHARDEV_BACKEND_KIND_CONSOLE:
4043 chr = qemu_chr_open_win_con();
4044 break;
4045#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004046#ifdef CONFIG_SPICE
4047 case CHARDEV_BACKEND_KIND_SPICEVMC:
4048 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4049 break;
4050 case CHARDEV_BACKEND_KIND_SPICEPORT:
4051 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4052 break;
4053#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004054 case CHARDEV_BACKEND_KIND_VC:
4055 chr = vc_init(backend->vc);
4056 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004057 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004058 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004059 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004060 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004061 default:
4062 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4063 break;
4064 }
4065
Markus Armbruster3894c782014-05-19 18:57:36 +02004066 /*
4067 * Character backend open hasn't been fully converted to the Error
4068 * API. Some opens fail without setting an error. Set a generic
4069 * error then.
4070 * TODO full conversion to Error API
4071 */
4072 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004073 error_setg(errp, "Failed to create chardev");
4074 }
4075 if (chr) {
4076 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004077 chr->avail_connections =
4078 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004079 if (!chr->filename) {
4080 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4081 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004082 if (!chr->explicit_be_open) {
4083 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4084 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004085 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4086 return ret;
4087 } else {
4088 g_free(ret);
4089 return NULL;
4090 }
4091}
4092
4093void qmp_chardev_remove(const char *id, Error **errp)
4094{
4095 CharDriverState *chr;
4096
4097 chr = qemu_chr_find(id);
4098 if (NULL == chr) {
4099 error_setg(errp, "Chardev '%s' not found", id);
4100 return;
4101 }
4102 if (chr->chr_can_read || chr->chr_read ||
4103 chr->chr_event || chr->handler_opaque) {
4104 error_setg(errp, "Chardev '%s' is busy", id);
4105 return;
4106 }
4107 qemu_chr_delete(chr);
4108}
Anthony Liguorid654f342013-03-05 23:21:28 +05304109
4110static void register_types(void)
4111{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004112 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304113 register_char_driver("socket", qemu_chr_open_socket);
4114 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004115 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004116 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004117 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4118 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004119 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4120 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004121 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4122 qemu_chr_parse_serial);
4123 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4124 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004125 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4126 qemu_chr_parse_parallel);
4127 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4128 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004129 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004130 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004131 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4132 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004133 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4134 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004135 /* Bug-compatibility: */
4136 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4137 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004138 /* this must be done after machine init, since we register FEs with muxes
4139 * as part of realize functions like serial_isa_realizefn when -nographic
4140 * is specified
4141 */
4142 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304143}
4144
4145type_init(register_types);