blob: 6f8bb7abd15584575d6312ab3d4eabceb58b4779 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000029#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030030#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000031
32#include <unistd.h>
33#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000034#include <time.h>
35#include <errno.h>
36#include <sys/time.h>
37#include <zlib.h>
38
39#ifndef _WIN32
40#include <sys/times.h>
41#include <sys/wait.h>
42#include <termios.h>
43#include <sys/mman.h>
44#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000046#include <sys/socket.h>
47#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <dirent.h>
51#include <netdb.h>
52#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020053#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000054#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040055#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56#include <dev/ppbus/ppi.h>
57#include <dev/ppbus/ppbconf.h>
58#elif defined(__DragonFly__)
59#include <dev/misc/ppi/ppi.h>
60#include <bus/ppbus/ppbconf.h>
61#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
aliguori6f97dba2008-10-31 18:49:55 +000063#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000064#include <linux/ppdev.h>
65#include <linux/parport.h>
66#endif
67#ifdef __sun__
68#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
71#include <netinet/arp.h>
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000078#endif
79#endif
80#endif
81
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010082#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020083#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000084
Amit Shah9bd78542009-11-03 19:59:54 +053085#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030086#define READ_RETRIES 10
Amit Shah9bd78542009-11-03 19:59:54 +053087
aliguori6f97dba2008-10-31 18:49:55 +000088/***********************************************************/
89/* character device */
90
Blue Swirl72cf2d42009-09-12 07:36:22 +000091static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
92 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000093
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020094CharDriverState *qemu_chr_alloc(void)
95{
96 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +020097 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +020098 return chr;
99}
100
Hans de Goedea425d232011-11-19 10:22:43 +0100101void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000102{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200103 /* Keep track if the char device is open */
104 switch (event) {
105 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100106 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200107 break;
108 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100109 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200110 break;
111 }
112
aliguori6f97dba2008-10-31 18:49:55 +0000113 if (!s->chr_event)
114 return;
115 s->chr_event(s->handler_opaque, event);
116}
117
Hans de Goedefee204f2013-03-26 11:07:54 +0100118void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000119{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500120 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000121}
122
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500123int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000124{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200125 int ret;
126
127 qemu_mutex_lock(&s->chr_write_lock);
128 ret = s->chr_write(s, buf, len);
129 qemu_mutex_unlock(&s->chr_write_lock);
130 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000131}
132
Anthony Liguoricd187202013-03-26 10:04:17 -0500133int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
134{
135 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200136 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500137
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200138 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500139 while (offset < len) {
140 do {
141 res = s->chr_write(s, buf + offset, len - offset);
142 if (res == -1 && errno == EAGAIN) {
143 g_usleep(100);
144 }
145 } while (res == -1 && errno == EAGAIN);
146
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200147 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500148 break;
149 }
150
Anthony Liguoricd187202013-03-26 10:04:17 -0500151 offset += res;
152 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200153 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500154
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200155 if (res < 0) {
156 return res;
157 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500158 return offset;
159}
160
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300161int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
162{
163 int offset = 0, counter = 10;
164 int res;
165
166 if (!s->chr_sync_read) {
167 return 0;
168 }
169
170 while (offset < len) {
171 do {
172 res = s->chr_sync_read(s, buf + offset, len - offset);
173 if (res == -1 && errno == EAGAIN) {
174 g_usleep(100);
175 }
176 } while (res == -1 && errno == EAGAIN);
177
178 if (res == 0) {
179 break;
180 }
181
182 if (res < 0) {
183 return res;
184 }
185
186 offset += res;
187
188 if (!counter--) {
189 break;
190 }
191 }
192
193 return offset;
194}
195
Anthony Liguori41084f12011-08-15 11:17:34 -0500196int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000197{
198 if (!s->chr_ioctl)
199 return -ENOTSUP;
200 return s->chr_ioctl(s, cmd, arg);
201}
202
Anthony Liguori909cda12011-08-15 11:17:31 -0500203int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000204{
205 if (!s->chr_can_read)
206 return 0;
207 return s->chr_can_read(s->handler_opaque);
208}
209
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500210void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000211{
Stefan Weilac310732012-04-19 22:27:14 +0200212 if (s->chr_read) {
213 s->chr_read(s->handler_opaque, buf, len);
214 }
aliguori6f97dba2008-10-31 18:49:55 +0000215}
216
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500217int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100218{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300219 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800220 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300221}
222
223int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
224{
225 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100226}
227
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300228int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
229{
230 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
231}
232
Daniel P. Berrange13661082011-06-23 13:31:42 +0100233int qemu_chr_add_client(CharDriverState *s, int fd)
234{
235 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
236}
237
Peter Maydell1e3328b2014-06-16 18:36:39 +0100238int qemu_chr_del_client(CharDriverState *s)
239{
240 return s->chr_del_client ? s->chr_del_client(s) : -1;
241}
242
aliguori6f97dba2008-10-31 18:49:55 +0000243void qemu_chr_accept_input(CharDriverState *s)
244{
245 if (s->chr_accept_input)
246 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100247 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000248}
249
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500250void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000251{
Amit Shah9bd78542009-11-03 19:59:54 +0530252 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000253 va_list ap;
254 va_start(ap, fmt);
255 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500256 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000257 va_end(ap);
258}
259
Amit Shah386a5a12013-08-28 15:24:05 +0530260static void remove_fd_in_watch(CharDriverState *chr);
261
aliguori6f97dba2008-10-31 18:49:55 +0000262void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100263 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000264 IOReadHandler *fd_read,
265 IOEventHandler *fd_event,
266 void *opaque)
267{
Hans de Goede19083222013-03-26 11:07:56 +0100268 int fe_open;
269
Amit Shahda7d9982011-04-25 15:18:22 +0530270 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100271 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530272 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100273 } else {
274 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530275 }
aliguori6f97dba2008-10-31 18:49:55 +0000276 s->chr_can_read = fd_can_read;
277 s->chr_read = fd_read;
278 s->chr_event = fd_event;
279 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200280 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000281 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200282
Hans de Goede19083222013-03-26 11:07:56 +0100283 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100284 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100285 }
286
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200287 /* We're connecting to an already opened device, so let's make sure we
288 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100289 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100290 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200291 }
aliguori6f97dba2008-10-31 18:49:55 +0000292}
293
294static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
295{
296 return len;
297}
298
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100299static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000300{
301 CharDriverState *chr;
302
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200303 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000304 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500305 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100306 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000307}
308
309/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000310#define MAX_MUX 4
311#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
312#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
313typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100314 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000315 IOReadHandler *chr_read[MAX_MUX];
316 IOEventHandler *chr_event[MAX_MUX];
317 void *ext_opaque[MAX_MUX];
318 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200319 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000320 int mux_cnt;
321 int term_got_escape;
322 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000323 /* Intermediate input buffer allows to catch escape sequences even if the
324 currently active device is not accepting any input - but only until it
325 is full as well. */
326 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
327 int prod[MAX_MUX];
328 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200329 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200330
331 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200332 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200333 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000334} MuxDriver;
335
336
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200337/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000338static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
339{
340 MuxDriver *d = chr->opaque;
341 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200342 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200343 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000344 } else {
345 int i;
346
347 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200348 for (i = 0; i < len; i++) {
349 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000350 char buf1[64];
351 int64_t ti;
352 int secs;
353
Alex Blighbc72ad62013-08-21 16:03:08 +0100354 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200355 if (d->timestamps_start == -1)
356 d->timestamps_start = ti;
357 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000358 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000359 snprintf(buf1, sizeof(buf1),
360 "[%02d:%02d:%02d.%03d] ",
361 secs / 3600,
362 (secs / 60) % 60,
363 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000364 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200365 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200366 d->linestart = 0;
367 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200368 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200369 if (buf[i] == '\n') {
370 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000371 }
372 }
373 }
374 return ret;
375}
376
377static const char * const mux_help[] = {
378 "% h print this help\n\r",
379 "% x exit emulator\n\r",
380 "% s save disk data back to file (if -snapshot)\n\r",
381 "% t toggle console timestamps\n\r"
382 "% b send break (magic sysrq)\n\r",
383 "% c switch between console and monitor\n\r",
384 "% % sends %\n\r",
385 NULL
386};
387
388int term_escape_char = 0x01; /* ctrl-a is used for escape */
389static void mux_print_help(CharDriverState *chr)
390{
391 int i, j;
392 char ebuf[15] = "Escape-Char";
393 char cbuf[50] = "\n\r";
394
395 if (term_escape_char > 0 && term_escape_char < 26) {
396 snprintf(cbuf, sizeof(cbuf), "\n\r");
397 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
398 } else {
399 snprintf(cbuf, sizeof(cbuf),
400 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
401 term_escape_char);
402 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200403 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000404 for (i = 0; mux_help[i] != NULL; i++) {
405 for (j=0; mux_help[i][j] != '\0'; j++) {
406 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200407 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000408 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200409 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000410 }
411 }
412}
413
aliguori2724b182009-03-05 23:01:47 +0000414static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
415{
416 if (d->chr_event[mux_nr])
417 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
418}
419
aliguori6f97dba2008-10-31 18:49:55 +0000420static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
421{
422 if (d->term_got_escape) {
423 d->term_got_escape = 0;
424 if (ch == term_escape_char)
425 goto send_char;
426 switch(ch) {
427 case '?':
428 case 'h':
429 mux_print_help(chr);
430 break;
431 case 'x':
432 {
433 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200434 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000435 exit(0);
436 break;
437 }
438 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200439 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000440 break;
441 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100442 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000443 break;
444 case 'c':
445 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200446 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
447 d->focus++;
448 if (d->focus >= d->mux_cnt)
449 d->focus = 0;
450 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000451 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200452 case 't':
453 d->timestamps = !d->timestamps;
454 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200455 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200456 break;
aliguori6f97dba2008-10-31 18:49:55 +0000457 }
458 } else if (ch == term_escape_char) {
459 d->term_got_escape = 1;
460 } else {
461 send_char:
462 return 1;
463 }
464 return 0;
465}
466
467static void mux_chr_accept_input(CharDriverState *chr)
468{
aliguori6f97dba2008-10-31 18:49:55 +0000469 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200470 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000471
aliguoria80bf992009-03-05 23:00:02 +0000472 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000473 d->chr_can_read[m] &&
474 d->chr_can_read[m](d->ext_opaque[m])) {
475 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000476 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000477 }
478}
479
480static int mux_chr_can_read(void *opaque)
481{
482 CharDriverState *chr = opaque;
483 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200484 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000485
aliguoria80bf992009-03-05 23:00:02 +0000486 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000487 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000488 if (d->chr_can_read[m])
489 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000490 return 0;
491}
492
493static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
494{
495 CharDriverState *chr = opaque;
496 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200497 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000498 int i;
499
500 mux_chr_accept_input (opaque);
501
502 for(i = 0; i < size; i++)
503 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000504 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000505 d->chr_can_read[m] &&
506 d->chr_can_read[m](d->ext_opaque[m]))
507 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
508 else
aliguoria80bf992009-03-05 23:00:02 +0000509 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000510 }
511}
512
513static void mux_chr_event(void *opaque, int event)
514{
515 CharDriverState *chr = opaque;
516 MuxDriver *d = chr->opaque;
517 int i;
518
519 /* Send the event to all registered listeners */
520 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000521 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000522}
523
524static void mux_chr_update_read_handler(CharDriverState *chr)
525{
526 MuxDriver *d = chr->opaque;
527
528 if (d->mux_cnt >= MAX_MUX) {
529 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
530 return;
531 }
532 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
533 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
534 d->chr_read[d->mux_cnt] = chr->chr_read;
535 d->chr_event[d->mux_cnt] = chr->chr_event;
536 /* Fix up the real driver with mux routines */
537 if (d->mux_cnt == 0) {
538 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
539 mux_chr_event, chr);
540 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200541 if (d->focus != -1) {
542 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200543 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200544 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000545 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200546 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000547}
548
Michael Roth7b7ab182013-07-30 13:04:22 -0500549static bool muxes_realized;
550
551/**
552 * Called after processing of default and command-line-specified
553 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
554 * to a mux chardev. This is done here to ensure that
555 * output/prompts/banners are only displayed for the FE that has
556 * focus when initial command-line processing/machine init is
557 * completed.
558 *
559 * After this point, any new FE attached to any new or existing
560 * mux will receive CHR_EVENT_OPENED notifications for the BE
561 * immediately.
562 */
563static void muxes_realize_done(Notifier *notifier, void *unused)
564{
565 CharDriverState *chr;
566
567 QTAILQ_FOREACH(chr, &chardevs, next) {
568 if (chr->is_mux) {
569 MuxDriver *d = chr->opaque;
570 int i;
571
572 /* send OPENED to all already-attached FEs */
573 for (i = 0; i < d->mux_cnt; i++) {
574 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
575 }
576 /* mark mux as OPENED so any new FEs will immediately receive
577 * OPENED event
578 */
579 qemu_chr_be_generic_open(chr);
580 }
581 }
582 muxes_realized = true;
583}
584
585static Notifier muxes_realize_notify = {
586 .notify = muxes_realize_done,
587};
588
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400589static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
590{
591 MuxDriver *d = s->opaque;
592 return d->drv->chr_add_watch(d->drv, cond);
593}
594
aliguori6f97dba2008-10-31 18:49:55 +0000595static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
596{
597 CharDriverState *chr;
598 MuxDriver *d;
599
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200600 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500601 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000602
603 chr->opaque = d;
604 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200605 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000606 chr->chr_write = mux_chr_write;
607 chr->chr_update_read_handler = mux_chr_update_read_handler;
608 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100609 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100610 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400611 if (drv->chr_add_watch) {
612 chr->chr_add_watch = mux_chr_add_watch;
613 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500614 /* only default to opened state if we've realized the initial
615 * set of muxes
616 */
617 chr->explicit_be_open = muxes_realized ? 0 : 1;
618 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200619
aliguori6f97dba2008-10-31 18:49:55 +0000620 return chr;
621}
622
623
624#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000625int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000626{
627 int ret, len;
628
629 len = len1;
630 while (len > 0) {
631 ret = send(fd, buf, len, 0);
632 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000633 errno = WSAGetLastError();
634 if (errno != WSAEWOULDBLOCK) {
635 return -1;
636 }
637 } else if (ret == 0) {
638 break;
639 } else {
640 buf += ret;
641 len -= ret;
642 }
643 }
644 return len1 - len;
645}
646
647#else
648
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100649int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000650{
651 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100652 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000653
654 len = len1;
655 while (len > 0) {
656 ret = write(fd, buf, len);
657 if (ret < 0) {
658 if (errno != EINTR && errno != EAGAIN)
659 return -1;
660 } else if (ret == 0) {
661 break;
662 } else {
663 buf += ret;
664 len -= ret;
665 }
666 }
667 return len1 - len;
668}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500669
670int recv_all(int fd, void *_buf, int len1, bool single_read)
671{
672 int ret, len;
673 uint8_t *buf = _buf;
674
675 len = len1;
676 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
677 if (ret < 0) {
678 if (errno != EINTR && errno != EAGAIN) {
679 return -1;
680 }
681 continue;
682 } else {
683 if (single_read) {
684 return ret;
685 }
686 buf += ret;
687 len -= ret;
688 }
689 }
690 return len1 - len;
691}
692
aliguori6f97dba2008-10-31 18:49:55 +0000693#endif /* !_WIN32 */
694
Anthony Liguori96c63842013-03-05 23:21:18 +0530695typedef struct IOWatchPoll
696{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200697 GSource parent;
698
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200699 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530700 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530701
702 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200703 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530704 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530705} IOWatchPoll;
706
Anthony Liguori96c63842013-03-05 23:21:18 +0530707static IOWatchPoll *io_watch_poll_from_source(GSource *source)
708{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200709 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530710}
711
712static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
713{
714 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200715 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200716 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200717 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530718 return FALSE;
719 }
720
Paolo Bonzinid185c092013-04-05 17:59:33 +0200721 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200722 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
723 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200724 g_source_attach(iwp->src, NULL);
725 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200726 g_source_destroy(iwp->src);
727 g_source_unref(iwp->src);
728 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200729 }
730 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530731}
732
733static gboolean io_watch_poll_check(GSource *source)
734{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200735 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530736}
737
738static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
739 gpointer user_data)
740{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200741 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530742}
743
744static void io_watch_poll_finalize(GSource *source)
745{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200746 /* Due to a glib bug, removing the last reference to a source
747 * inside a finalize callback causes recursive locking (and a
748 * deadlock). This is not a problem inside other callbacks,
749 * including dispatch callbacks, so we call io_remove_watch_poll
750 * to remove this source. At this point, iwp->src must
751 * be NULL, or we would leak it.
752 *
753 * This would be solved much more elegantly by child sources,
754 * but we support older glib versions that do not have them.
755 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530756 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200757 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530758}
759
760static GSourceFuncs io_watch_poll_funcs = {
761 .prepare = io_watch_poll_prepare,
762 .check = io_watch_poll_check,
763 .dispatch = io_watch_poll_dispatch,
764 .finalize = io_watch_poll_finalize,
765};
766
767/* Can only be used for read */
768static guint io_add_watch_poll(GIOChannel *channel,
769 IOCanReadHandler *fd_can_read,
770 GIOFunc fd_read,
771 gpointer user_data)
772{
773 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200774 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530775
Paolo Bonzinid185c092013-04-05 17:59:33 +0200776 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530777 iwp->fd_can_read = fd_can_read;
778 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200779 iwp->channel = channel;
780 iwp->fd_read = (GSourceFunc) fd_read;
781 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530782
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200783 tag = g_source_attach(&iwp->parent, NULL);
784 g_source_unref(&iwp->parent);
785 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530786}
787
Paolo Bonzini2b316772013-04-19 17:32:09 +0200788static void io_remove_watch_poll(guint tag)
789{
790 GSource *source;
791 IOWatchPoll *iwp;
792
793 g_return_if_fail (tag > 0);
794
795 source = g_main_context_find_source_by_id(NULL, tag);
796 g_return_if_fail (source != NULL);
797
798 iwp = io_watch_poll_from_source(source);
799 if (iwp->src) {
800 g_source_destroy(iwp->src);
801 g_source_unref(iwp->src);
802 iwp->src = NULL;
803 }
804 g_source_destroy(&iwp->parent);
805}
806
Amit Shah26da70c2013-08-28 15:23:37 +0530807static void remove_fd_in_watch(CharDriverState *chr)
808{
809 if (chr->fd_in_tag) {
810 io_remove_watch_poll(chr->fd_in_tag);
811 chr->fd_in_tag = 0;
812 }
813}
814
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000815#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530816static GIOChannel *io_channel_from_fd(int fd)
817{
818 GIOChannel *chan;
819
820 if (fd == -1) {
821 return NULL;
822 }
823
824 chan = g_io_channel_unix_new(fd);
825
826 g_io_channel_set_encoding(chan, NULL, NULL);
827 g_io_channel_set_buffered(chan, FALSE);
828
829 return chan;
830}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000831#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530832
Anthony Liguori76a96442013-03-05 23:21:21 +0530833static GIOChannel *io_channel_from_socket(int fd)
834{
835 GIOChannel *chan;
836
837 if (fd == -1) {
838 return NULL;
839 }
840
841#ifdef _WIN32
842 chan = g_io_channel_win32_new_socket(fd);
843#else
844 chan = g_io_channel_unix_new(fd);
845#endif
846
847 g_io_channel_set_encoding(chan, NULL, NULL);
848 g_io_channel_set_buffered(chan, FALSE);
849
850 return chan;
851}
852
Anthony Liguori684a0962013-03-29 11:39:50 -0500853static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530854{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200855 size_t offset = 0;
856 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530857
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200858 while (offset < len && status == G_IO_STATUS_NORMAL) {
859 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500860
861 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530862 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500863 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530864 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500865
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200866 if (offset > 0) {
867 return offset;
868 }
869 switch (status) {
870 case G_IO_STATUS_NORMAL:
871 g_assert(len == 0);
872 return 0;
873 case G_IO_STATUS_AGAIN:
874 errno = EAGAIN;
875 return -1;
876 default:
877 break;
878 }
879 errno = EINVAL;
880 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530881}
Anthony Liguori96c63842013-03-05 23:21:18 +0530882
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000883#ifndef _WIN32
884
Anthony Liguoria29753f2013-03-05 23:21:19 +0530885typedef struct FDCharDriver {
886 CharDriverState *chr;
887 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000888 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530889 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000890} FDCharDriver;
891
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200892/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000893static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
894{
895 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530896
Anthony Liguori684a0962013-03-29 11:39:50 -0500897 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530898}
899
900static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
901{
902 CharDriverState *chr = opaque;
903 FDCharDriver *s = chr->opaque;
904 int len;
905 uint8_t buf[READ_BUF_LEN];
906 GIOStatus status;
907 gsize bytes_read;
908
909 len = sizeof(buf);
910 if (len > s->max_size) {
911 len = s->max_size;
912 }
913 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200914 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530915 }
916
917 status = g_io_channel_read_chars(chan, (gchar *)buf,
918 len, &bytes_read, NULL);
919 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530920 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530921 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
922 return FALSE;
923 }
924 if (status == G_IO_STATUS_NORMAL) {
925 qemu_chr_be_write(chr, buf, bytes_read);
926 }
927
928 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000929}
930
931static int fd_chr_read_poll(void *opaque)
932{
933 CharDriverState *chr = opaque;
934 FDCharDriver *s = chr->opaque;
935
Anthony Liguori909cda12011-08-15 11:17:31 -0500936 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000937 return s->max_size;
938}
939
Anthony Liguori23673ca2013-03-05 23:21:23 +0530940static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
941{
942 FDCharDriver *s = chr->opaque;
943 return g_io_create_watch(s->fd_out, cond);
944}
945
aliguori6f97dba2008-10-31 18:49:55 +0000946static void fd_chr_update_read_handler(CharDriverState *chr)
947{
948 FDCharDriver *s = chr->opaque;
949
Amit Shah26da70c2013-08-28 15:23:37 +0530950 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530951 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530952 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
953 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000954 }
955}
956
957static void fd_chr_close(struct CharDriverState *chr)
958{
959 FDCharDriver *s = chr->opaque;
960
Amit Shah26da70c2013-08-28 15:23:37 +0530961 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530962 if (s->fd_in) {
963 g_io_channel_unref(s->fd_in);
964 }
965 if (s->fd_out) {
966 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000967 }
968
Anthony Liguori7267c092011-08-20 22:09:37 -0500969 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100970 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000971}
972
973/* open a character device to a unix fd */
974static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
975{
976 CharDriverState *chr;
977 FDCharDriver *s;
978
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200979 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500980 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530981 s->fd_in = io_channel_from_fd(fd_in);
982 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +0800983 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530984 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000985 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530986 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000987 chr->chr_write = fd_chr_write;
988 chr->chr_update_read_handler = fd_chr_update_read_handler;
989 chr->chr_close = fd_chr_close;
990
aliguori6f97dba2008-10-31 18:49:55 +0000991 return chr;
992}
993
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100994static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000995{
996 int fd_in, fd_out;
997 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100998 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200999
1000 if (filename == NULL) {
1001 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +01001002 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001003 }
aliguori6f97dba2008-10-31 18:49:55 +00001004
1005 snprintf(filename_in, 256, "%s.in", filename);
1006 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001007 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1008 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001009 if (fd_in < 0 || fd_out < 0) {
1010 if (fd_in >= 0)
1011 close(fd_in);
1012 if (fd_out >= 0)
1013 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001014 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001015 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001016 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001017 }
aliguori6f97dba2008-10-31 18:49:55 +00001018 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001019 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001020}
1021
aliguori6f97dba2008-10-31 18:49:55 +00001022/* init terminal so that we can grab keys */
1023static struct termios oldtty;
1024static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001025static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001026static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001027
1028static void term_exit(void)
1029{
1030 tcsetattr (0, TCSANOW, &oldtty);
1031 fcntl(0, F_SETFL, old_fd0_flags);
1032}
1033
Paolo Bonzinibb002512010-12-23 13:42:50 +01001034static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001035{
1036 struct termios tty;
1037
Paolo Bonzini03693642010-12-23 13:42:49 +01001038 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001039 if (!echo) {
1040 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001041 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001042 tty.c_oflag |= OPOST;
1043 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1044 tty.c_cflag &= ~(CSIZE|PARENB);
1045 tty.c_cflag |= CS8;
1046 tty.c_cc[VMIN] = 1;
1047 tty.c_cc[VTIME] = 0;
1048 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001049 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001050 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001051
1052 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001053}
1054
1055static void qemu_chr_close_stdio(struct CharDriverState *chr)
1056{
1057 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001058 fd_chr_close(chr);
1059}
1060
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001061static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001062{
1063 CharDriverState *chr;
1064
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001065 if (is_daemonized()) {
1066 error_report("cannot use stdio with -daemonize");
1067 return NULL;
1068 }
Li Liuc88930a2014-09-09 19:19:48 +08001069
1070 if (stdio_in_use) {
1071 error_report("cannot use stdio by multiple character devices");
1072 exit(1);
1073 }
1074
1075 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301076 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001077 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001078 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301079 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001080
aliguori6f97dba2008-10-31 18:49:55 +00001081 chr = qemu_chr_open_fd(0, 1);
1082 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001083 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001084 if (opts->has_signal) {
1085 stdio_allow_signal = opts->signal;
1086 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001087 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001088
Markus Armbruster1f514702012-02-07 15:09:08 +01001089 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001090}
1091
aliguori6f97dba2008-10-31 18:49:55 +00001092#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001093 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1094 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001095
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001096#define HAVE_CHARDEV_TTY 1
1097
aliguori6f97dba2008-10-31 18:49:55 +00001098typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301099 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001100 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001101
1102 /* Protected by the CharDriverState chr_write_lock. */
1103 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301104 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001105 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001106} PtyCharDriver;
1107
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001108static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001109static void pty_chr_state(CharDriverState *chr, int connected);
1110
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301111static gboolean pty_chr_timer(gpointer opaque)
1112{
1113 struct CharDriverState *chr = opaque;
1114 PtyCharDriver *s = chr->opaque;
1115
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001116 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001117 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001118 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001119 if (!s->connected) {
1120 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001121 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001122 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001123 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301124 return FALSE;
1125}
1126
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001127/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301128static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1129{
1130 PtyCharDriver *s = chr->opaque;
1131
1132 if (s->timer_tag) {
1133 g_source_remove(s->timer_tag);
1134 s->timer_tag = 0;
1135 }
1136
1137 if (ms == 1000) {
1138 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1139 } else {
1140 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1141 }
1142}
1143
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001144/* Called with chr_write_lock held. */
1145static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001146{
1147 PtyCharDriver *s = chr->opaque;
1148 GPollFD pfd;
1149
1150 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1151 pfd.events = G_IO_OUT;
1152 pfd.revents = 0;
1153 g_poll(&pfd, 1, 0);
1154 if (pfd.revents & G_IO_HUP) {
1155 pty_chr_state(chr, 0);
1156 } else {
1157 pty_chr_state(chr, 1);
1158 }
1159}
1160
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001161static void pty_chr_update_read_handler(CharDriverState *chr)
1162{
1163 qemu_mutex_lock(&chr->chr_write_lock);
1164 pty_chr_update_read_handler_locked(chr);
1165 qemu_mutex_unlock(&chr->chr_write_lock);
1166}
1167
1168/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001169static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1170{
1171 PtyCharDriver *s = chr->opaque;
1172
1173 if (!s->connected) {
1174 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001175 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001176 if (!s->connected) {
1177 return 0;
1178 }
aliguori6f97dba2008-10-31 18:49:55 +00001179 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001180 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001181}
1182
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301183static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1184{
1185 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001186 if (!s->connected) {
1187 return NULL;
1188 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301189 return g_io_create_watch(s->fd, cond);
1190}
1191
aliguori6f97dba2008-10-31 18:49:55 +00001192static int pty_chr_read_poll(void *opaque)
1193{
1194 CharDriverState *chr = opaque;
1195 PtyCharDriver *s = chr->opaque;
1196
Anthony Liguori909cda12011-08-15 11:17:31 -05001197 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001198 return s->read_bytes;
1199}
1200
Anthony Liguori093d3a22013-03-05 23:21:20 +05301201static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001202{
1203 CharDriverState *chr = opaque;
1204 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301205 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301206 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301207 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001208
1209 len = sizeof(buf);
1210 if (len > s->read_bytes)
1211 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001212 if (len == 0) {
1213 return TRUE;
1214 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301215 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1216 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001217 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301218 return FALSE;
1219 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001220 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001221 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001222 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301223 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001224}
1225
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001226static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1227{
1228 CharDriverState *chr = opaque;
1229 PtyCharDriver *s = chr->opaque;
1230
1231 s->open_tag = 0;
1232 qemu_chr_be_generic_open(chr);
1233 return FALSE;
1234}
1235
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001236/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001237static void pty_chr_state(CharDriverState *chr, int connected)
1238{
1239 PtyCharDriver *s = chr->opaque;
1240
1241 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001242 if (s->open_tag) {
1243 g_source_remove(s->open_tag);
1244 s->open_tag = 0;
1245 }
Amit Shah26da70c2013-08-28 15:23:37 +05301246 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001247 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001248 /* (re-)connect poll interval for idle guests: once per second.
1249 * We check more frequently in case the guests sends data to
1250 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301251 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001252 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001253 if (s->timer_tag) {
1254 g_source_remove(s->timer_tag);
1255 s->timer_tag = 0;
1256 }
1257 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001258 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001259 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001260 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001261 }
1262 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301263 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1264 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001265 }
aliguori6f97dba2008-10-31 18:49:55 +00001266 }
1267}
1268
aliguori6f97dba2008-10-31 18:49:55 +00001269static void pty_chr_close(struct CharDriverState *chr)
1270{
1271 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301272 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001273
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001274 qemu_mutex_lock(&chr->chr_write_lock);
1275 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301276 fd = g_io_channel_unix_get_fd(s->fd);
1277 g_io_channel_unref(s->fd);
1278 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301279 if (s->timer_tag) {
1280 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001281 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301282 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001283 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001284 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001285 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001286}
1287
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001288static CharDriverState *qemu_chr_open_pty(const char *id,
1289 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001290{
1291 CharDriverState *chr;
1292 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001293 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001294 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001295
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001296 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1297 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001298 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001299 }
1300
aliguori6f97dba2008-10-31 18:49:55 +00001301 close(slave_fd);
1302
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001303 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001304
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001305 chr->filename = g_strdup_printf("pty:%s", pty_name);
1306 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001307 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001308
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001309 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001310 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001311
1312 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001313 chr->opaque = s;
1314 chr->chr_write = pty_chr_write;
1315 chr->chr_update_read_handler = pty_chr_update_read_handler;
1316 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301317 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001318 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001319
Anthony Liguori093d3a22013-03-05 23:21:20 +05301320 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301321 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001322
Markus Armbruster1f514702012-02-07 15:09:08 +01001323 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001324}
1325
1326static void tty_serial_init(int fd, int speed,
1327 int parity, int data_bits, int stop_bits)
1328{
1329 struct termios tty;
1330 speed_t spd;
1331
1332#if 0
1333 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1334 speed, parity, data_bits, stop_bits);
1335#endif
1336 tcgetattr (fd, &tty);
1337
Stefan Weil45eea132009-10-26 16:10:10 +01001338#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1339 speed = speed * 10 / 11;
1340 do {
1341 check_speed(50);
1342 check_speed(75);
1343 check_speed(110);
1344 check_speed(134);
1345 check_speed(150);
1346 check_speed(200);
1347 check_speed(300);
1348 check_speed(600);
1349 check_speed(1200);
1350 check_speed(1800);
1351 check_speed(2400);
1352 check_speed(4800);
1353 check_speed(9600);
1354 check_speed(19200);
1355 check_speed(38400);
1356 /* Non-Posix values follow. They may be unsupported on some systems. */
1357 check_speed(57600);
1358 check_speed(115200);
1359#ifdef B230400
1360 check_speed(230400);
1361#endif
1362#ifdef B460800
1363 check_speed(460800);
1364#endif
1365#ifdef B500000
1366 check_speed(500000);
1367#endif
1368#ifdef B576000
1369 check_speed(576000);
1370#endif
1371#ifdef B921600
1372 check_speed(921600);
1373#endif
1374#ifdef B1000000
1375 check_speed(1000000);
1376#endif
1377#ifdef B1152000
1378 check_speed(1152000);
1379#endif
1380#ifdef B1500000
1381 check_speed(1500000);
1382#endif
1383#ifdef B2000000
1384 check_speed(2000000);
1385#endif
1386#ifdef B2500000
1387 check_speed(2500000);
1388#endif
1389#ifdef B3000000
1390 check_speed(3000000);
1391#endif
1392#ifdef B3500000
1393 check_speed(3500000);
1394#endif
1395#ifdef B4000000
1396 check_speed(4000000);
1397#endif
aliguori6f97dba2008-10-31 18:49:55 +00001398 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001399 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001400
1401 cfsetispeed(&tty, spd);
1402 cfsetospeed(&tty, spd);
1403
1404 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1405 |INLCR|IGNCR|ICRNL|IXON);
1406 tty.c_oflag |= OPOST;
1407 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1408 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1409 switch(data_bits) {
1410 default:
1411 case 8:
1412 tty.c_cflag |= CS8;
1413 break;
1414 case 7:
1415 tty.c_cflag |= CS7;
1416 break;
1417 case 6:
1418 tty.c_cflag |= CS6;
1419 break;
1420 case 5:
1421 tty.c_cflag |= CS5;
1422 break;
1423 }
1424 switch(parity) {
1425 default:
1426 case 'N':
1427 break;
1428 case 'E':
1429 tty.c_cflag |= PARENB;
1430 break;
1431 case 'O':
1432 tty.c_cflag |= PARENB | PARODD;
1433 break;
1434 }
1435 if (stop_bits == 2)
1436 tty.c_cflag |= CSTOPB;
1437
1438 tcsetattr (fd, TCSANOW, &tty);
1439}
1440
1441static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1442{
1443 FDCharDriver *s = chr->opaque;
1444
1445 switch(cmd) {
1446 case CHR_IOCTL_SERIAL_SET_PARAMS:
1447 {
1448 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301449 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1450 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001451 ssp->data_bits, ssp->stop_bits);
1452 }
1453 break;
1454 case CHR_IOCTL_SERIAL_SET_BREAK:
1455 {
1456 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301457 if (enable) {
1458 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1459 }
aliguori6f97dba2008-10-31 18:49:55 +00001460 }
1461 break;
1462 case CHR_IOCTL_SERIAL_GET_TIOCM:
1463 {
1464 int sarg = 0;
1465 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301466 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001467 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001468 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001469 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001470 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001471 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001472 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001473 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001474 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001475 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001476 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001477 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001478 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001479 *targ |= CHR_TIOCM_RTS;
1480 }
1481 break;
1482 case CHR_IOCTL_SERIAL_SET_TIOCM:
1483 {
1484 int sarg = *(int *)arg;
1485 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301486 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001487 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1488 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1489 if (sarg & CHR_TIOCM_CTS)
1490 targ |= TIOCM_CTS;
1491 if (sarg & CHR_TIOCM_CAR)
1492 targ |= TIOCM_CAR;
1493 if (sarg & CHR_TIOCM_DSR)
1494 targ |= TIOCM_DSR;
1495 if (sarg & CHR_TIOCM_RI)
1496 targ |= TIOCM_RI;
1497 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001498 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001499 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001500 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301501 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001502 }
1503 break;
1504 default:
1505 return -ENOTSUP;
1506 }
1507 return 0;
1508}
1509
David Ahern4266a132010-02-10 18:27:17 -07001510static void qemu_chr_close_tty(CharDriverState *chr)
1511{
1512 FDCharDriver *s = chr->opaque;
1513 int fd = -1;
1514
1515 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301516 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001517 }
1518
1519 fd_chr_close(chr);
1520
1521 if (fd >= 0) {
1522 close(fd);
1523 }
1524}
1525
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001526static CharDriverState *qemu_chr_open_tty_fd(int fd)
1527{
1528 CharDriverState *chr;
1529
1530 tty_serial_init(fd, 115200, 'N', 8, 1);
1531 chr = qemu_chr_open_fd(fd, fd);
1532 chr->chr_ioctl = tty_serial_ioctl;
1533 chr->chr_close = qemu_chr_close_tty;
1534 return chr;
1535}
aliguori6f97dba2008-10-31 18:49:55 +00001536#endif /* __linux__ || __sun__ */
1537
1538#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001539
1540#define HAVE_CHARDEV_PARPORT 1
1541
aliguori6f97dba2008-10-31 18:49:55 +00001542typedef struct {
1543 int fd;
1544 int mode;
1545} ParallelCharDriver;
1546
1547static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1548{
1549 if (s->mode != mode) {
1550 int m = mode;
1551 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1552 return 0;
1553 s->mode = mode;
1554 }
1555 return 1;
1556}
1557
1558static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1559{
1560 ParallelCharDriver *drv = chr->opaque;
1561 int fd = drv->fd;
1562 uint8_t b;
1563
1564 switch(cmd) {
1565 case CHR_IOCTL_PP_READ_DATA:
1566 if (ioctl(fd, PPRDATA, &b) < 0)
1567 return -ENOTSUP;
1568 *(uint8_t *)arg = b;
1569 break;
1570 case CHR_IOCTL_PP_WRITE_DATA:
1571 b = *(uint8_t *)arg;
1572 if (ioctl(fd, PPWDATA, &b) < 0)
1573 return -ENOTSUP;
1574 break;
1575 case CHR_IOCTL_PP_READ_CONTROL:
1576 if (ioctl(fd, PPRCONTROL, &b) < 0)
1577 return -ENOTSUP;
1578 /* Linux gives only the lowest bits, and no way to know data
1579 direction! For better compatibility set the fixed upper
1580 bits. */
1581 *(uint8_t *)arg = b | 0xc0;
1582 break;
1583 case CHR_IOCTL_PP_WRITE_CONTROL:
1584 b = *(uint8_t *)arg;
1585 if (ioctl(fd, PPWCONTROL, &b) < 0)
1586 return -ENOTSUP;
1587 break;
1588 case CHR_IOCTL_PP_READ_STATUS:
1589 if (ioctl(fd, PPRSTATUS, &b) < 0)
1590 return -ENOTSUP;
1591 *(uint8_t *)arg = b;
1592 break;
1593 case CHR_IOCTL_PP_DATA_DIR:
1594 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1595 return -ENOTSUP;
1596 break;
1597 case CHR_IOCTL_PP_EPP_READ_ADDR:
1598 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1599 struct ParallelIOArg *parg = arg;
1600 int n = read(fd, parg->buffer, parg->count);
1601 if (n != parg->count) {
1602 return -EIO;
1603 }
1604 }
1605 break;
1606 case CHR_IOCTL_PP_EPP_READ:
1607 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1608 struct ParallelIOArg *parg = arg;
1609 int n = read(fd, parg->buffer, parg->count);
1610 if (n != parg->count) {
1611 return -EIO;
1612 }
1613 }
1614 break;
1615 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1616 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1617 struct ParallelIOArg *parg = arg;
1618 int n = write(fd, parg->buffer, parg->count);
1619 if (n != parg->count) {
1620 return -EIO;
1621 }
1622 }
1623 break;
1624 case CHR_IOCTL_PP_EPP_WRITE:
1625 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1626 struct ParallelIOArg *parg = arg;
1627 int n = write(fd, parg->buffer, parg->count);
1628 if (n != parg->count) {
1629 return -EIO;
1630 }
1631 }
1632 break;
1633 default:
1634 return -ENOTSUP;
1635 }
1636 return 0;
1637}
1638
1639static void pp_close(CharDriverState *chr)
1640{
1641 ParallelCharDriver *drv = chr->opaque;
1642 int fd = drv->fd;
1643
1644 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1645 ioctl(fd, PPRELEASE);
1646 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001647 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001648 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001649}
1650
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001651static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001652{
1653 CharDriverState *chr;
1654 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001655
1656 if (ioctl(fd, PPCLAIM) < 0) {
1657 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001658 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001659 }
1660
Anthony Liguori7267c092011-08-20 22:09:37 -05001661 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001662 drv->fd = fd;
1663 drv->mode = IEEE1284_MODE_COMPAT;
1664
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001665 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001666 chr->chr_write = null_chr_write;
1667 chr->chr_ioctl = pp_ioctl;
1668 chr->chr_close = pp_close;
1669 chr->opaque = drv;
1670
Markus Armbruster1f514702012-02-07 15:09:08 +01001671 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001672}
1673#endif /* __linux__ */
1674
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001675#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001676
1677#define HAVE_CHARDEV_PARPORT 1
1678
blueswir16972f932008-11-22 20:49:12 +00001679static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1680{
Stefan Weile0efb992011-02-23 19:09:16 +01001681 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001682 uint8_t b;
1683
1684 switch(cmd) {
1685 case CHR_IOCTL_PP_READ_DATA:
1686 if (ioctl(fd, PPIGDATA, &b) < 0)
1687 return -ENOTSUP;
1688 *(uint8_t *)arg = b;
1689 break;
1690 case CHR_IOCTL_PP_WRITE_DATA:
1691 b = *(uint8_t *)arg;
1692 if (ioctl(fd, PPISDATA, &b) < 0)
1693 return -ENOTSUP;
1694 break;
1695 case CHR_IOCTL_PP_READ_CONTROL:
1696 if (ioctl(fd, PPIGCTRL, &b) < 0)
1697 return -ENOTSUP;
1698 *(uint8_t *)arg = b;
1699 break;
1700 case CHR_IOCTL_PP_WRITE_CONTROL:
1701 b = *(uint8_t *)arg;
1702 if (ioctl(fd, PPISCTRL, &b) < 0)
1703 return -ENOTSUP;
1704 break;
1705 case CHR_IOCTL_PP_READ_STATUS:
1706 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1707 return -ENOTSUP;
1708 *(uint8_t *)arg = b;
1709 break;
1710 default:
1711 return -ENOTSUP;
1712 }
1713 return 0;
1714}
1715
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001716static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001717{
1718 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001719
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001720 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001721 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001722 chr->chr_write = null_chr_write;
1723 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001724 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001725 return chr;
blueswir16972f932008-11-22 20:49:12 +00001726}
1727#endif
1728
aliguori6f97dba2008-10-31 18:49:55 +00001729#else /* _WIN32 */
1730
1731typedef struct {
1732 int max_size;
1733 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001734 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001735 BOOL fpipe;
1736 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001737
1738 /* Protected by the CharDriverState chr_write_lock. */
1739 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001740} WinCharState;
1741
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001742typedef struct {
1743 HANDLE hStdIn;
1744 HANDLE hInputReadyEvent;
1745 HANDLE hInputDoneEvent;
1746 HANDLE hInputThread;
1747 uint8_t win_stdio_buf;
1748} WinStdioCharState;
1749
aliguori6f97dba2008-10-31 18:49:55 +00001750#define NSENDBUF 2048
1751#define NRECVBUF 2048
1752#define MAXCONNECT 1
1753#define NTIMEOUT 5000
1754
1755static int win_chr_poll(void *opaque);
1756static int win_chr_pipe_poll(void *opaque);
1757
1758static void win_chr_close(CharDriverState *chr)
1759{
1760 WinCharState *s = chr->opaque;
1761
1762 if (s->hsend) {
1763 CloseHandle(s->hsend);
1764 s->hsend = NULL;
1765 }
1766 if (s->hrecv) {
1767 CloseHandle(s->hrecv);
1768 s->hrecv = NULL;
1769 }
1770 if (s->hcom) {
1771 CloseHandle(s->hcom);
1772 s->hcom = NULL;
1773 }
1774 if (s->fpipe)
1775 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1776 else
1777 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301778
Hans de Goedea425d232011-11-19 10:22:43 +01001779 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001780}
1781
1782static int win_chr_init(CharDriverState *chr, const char *filename)
1783{
1784 WinCharState *s = chr->opaque;
1785 COMMCONFIG comcfg;
1786 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1787 COMSTAT comstat;
1788 DWORD size;
1789 DWORD err;
1790
1791 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1792 if (!s->hsend) {
1793 fprintf(stderr, "Failed CreateEvent\n");
1794 goto fail;
1795 }
1796 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1797 if (!s->hrecv) {
1798 fprintf(stderr, "Failed CreateEvent\n");
1799 goto fail;
1800 }
1801
1802 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1803 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1804 if (s->hcom == INVALID_HANDLE_VALUE) {
1805 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1806 s->hcom = NULL;
1807 goto fail;
1808 }
1809
1810 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1811 fprintf(stderr, "Failed SetupComm\n");
1812 goto fail;
1813 }
1814
1815 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1816 size = sizeof(COMMCONFIG);
1817 GetDefaultCommConfig(filename, &comcfg, &size);
1818 comcfg.dcb.DCBlength = sizeof(DCB);
1819 CommConfigDialog(filename, NULL, &comcfg);
1820
1821 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1822 fprintf(stderr, "Failed SetCommState\n");
1823 goto fail;
1824 }
1825
1826 if (!SetCommMask(s->hcom, EV_ERR)) {
1827 fprintf(stderr, "Failed SetCommMask\n");
1828 goto fail;
1829 }
1830
1831 cto.ReadIntervalTimeout = MAXDWORD;
1832 if (!SetCommTimeouts(s->hcom, &cto)) {
1833 fprintf(stderr, "Failed SetCommTimeouts\n");
1834 goto fail;
1835 }
1836
1837 if (!ClearCommError(s->hcom, &err, &comstat)) {
1838 fprintf(stderr, "Failed ClearCommError\n");
1839 goto fail;
1840 }
1841 qemu_add_polling_cb(win_chr_poll, chr);
1842 return 0;
1843
1844 fail:
1845 win_chr_close(chr);
1846 return -1;
1847}
1848
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001849/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001850static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1851{
1852 WinCharState *s = chr->opaque;
1853 DWORD len, ret, size, err;
1854
1855 len = len1;
1856 ZeroMemory(&s->osend, sizeof(s->osend));
1857 s->osend.hEvent = s->hsend;
1858 while (len > 0) {
1859 if (s->hsend)
1860 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1861 else
1862 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1863 if (!ret) {
1864 err = GetLastError();
1865 if (err == ERROR_IO_PENDING) {
1866 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1867 if (ret) {
1868 buf += size;
1869 len -= size;
1870 } else {
1871 break;
1872 }
1873 } else {
1874 break;
1875 }
1876 } else {
1877 buf += size;
1878 len -= size;
1879 }
1880 }
1881 return len1 - len;
1882}
1883
1884static int win_chr_read_poll(CharDriverState *chr)
1885{
1886 WinCharState *s = chr->opaque;
1887
Anthony Liguori909cda12011-08-15 11:17:31 -05001888 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001889 return s->max_size;
1890}
1891
1892static void win_chr_readfile(CharDriverState *chr)
1893{
1894 WinCharState *s = chr->opaque;
1895 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301896 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001897 DWORD size;
1898
1899 ZeroMemory(&s->orecv, sizeof(s->orecv));
1900 s->orecv.hEvent = s->hrecv;
1901 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1902 if (!ret) {
1903 err = GetLastError();
1904 if (err == ERROR_IO_PENDING) {
1905 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1906 }
1907 }
1908
1909 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001910 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001911 }
1912}
1913
1914static void win_chr_read(CharDriverState *chr)
1915{
1916 WinCharState *s = chr->opaque;
1917
1918 if (s->len > s->max_size)
1919 s->len = s->max_size;
1920 if (s->len == 0)
1921 return;
1922
1923 win_chr_readfile(chr);
1924}
1925
1926static int win_chr_poll(void *opaque)
1927{
1928 CharDriverState *chr = opaque;
1929 WinCharState *s = chr->opaque;
1930 COMSTAT status;
1931 DWORD comerr;
1932
1933 ClearCommError(s->hcom, &comerr, &status);
1934 if (status.cbInQue > 0) {
1935 s->len = status.cbInQue;
1936 win_chr_read_poll(chr);
1937 win_chr_read(chr);
1938 return 1;
1939 }
1940 return 0;
1941}
1942
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001943static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001944{
1945 CharDriverState *chr;
1946 WinCharState *s;
1947
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001948 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001949 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001950 chr->opaque = s;
1951 chr->chr_write = win_chr_write;
1952 chr->chr_close = win_chr_close;
1953
1954 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001955 g_free(s);
1956 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001957 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001958 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001959 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001960}
1961
1962static int win_chr_pipe_poll(void *opaque)
1963{
1964 CharDriverState *chr = opaque;
1965 WinCharState *s = chr->opaque;
1966 DWORD size;
1967
1968 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1969 if (size > 0) {
1970 s->len = size;
1971 win_chr_read_poll(chr);
1972 win_chr_read(chr);
1973 return 1;
1974 }
1975 return 0;
1976}
1977
1978static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1979{
1980 WinCharState *s = chr->opaque;
1981 OVERLAPPED ov;
1982 int ret;
1983 DWORD size;
1984 char openname[256];
1985
1986 s->fpipe = TRUE;
1987
1988 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1989 if (!s->hsend) {
1990 fprintf(stderr, "Failed CreateEvent\n");
1991 goto fail;
1992 }
1993 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1994 if (!s->hrecv) {
1995 fprintf(stderr, "Failed CreateEvent\n");
1996 goto fail;
1997 }
1998
1999 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2000 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2001 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2002 PIPE_WAIT,
2003 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2004 if (s->hcom == INVALID_HANDLE_VALUE) {
2005 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2006 s->hcom = NULL;
2007 goto fail;
2008 }
2009
2010 ZeroMemory(&ov, sizeof(ov));
2011 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2012 ret = ConnectNamedPipe(s->hcom, &ov);
2013 if (ret) {
2014 fprintf(stderr, "Failed ConnectNamedPipe\n");
2015 goto fail;
2016 }
2017
2018 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2019 if (!ret) {
2020 fprintf(stderr, "Failed GetOverlappedResult\n");
2021 if (ov.hEvent) {
2022 CloseHandle(ov.hEvent);
2023 ov.hEvent = NULL;
2024 }
2025 goto fail;
2026 }
2027
2028 if (ov.hEvent) {
2029 CloseHandle(ov.hEvent);
2030 ov.hEvent = NULL;
2031 }
2032 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2033 return 0;
2034
2035 fail:
2036 win_chr_close(chr);
2037 return -1;
2038}
2039
2040
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002041static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002042{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002043 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002044 CharDriverState *chr;
2045 WinCharState *s;
2046
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002047 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002048 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002049 chr->opaque = s;
2050 chr->chr_write = win_chr_write;
2051 chr->chr_close = win_chr_close;
2052
2053 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002054 g_free(s);
2055 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002056 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002057 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002058 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002059}
2060
Markus Armbruster1f514702012-02-07 15:09:08 +01002061static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002062{
2063 CharDriverState *chr;
2064 WinCharState *s;
2065
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002066 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002067 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002068 s->hcom = fd_out;
2069 chr->opaque = s;
2070 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002071 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002072}
2073
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002074static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002075{
Markus Armbruster1f514702012-02-07 15:09:08 +01002076 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002077}
2078
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002079static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2080{
2081 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2082 DWORD dwSize;
2083 int len1;
2084
2085 len1 = len;
2086
2087 while (len1 > 0) {
2088 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2089 break;
2090 }
2091 buf += dwSize;
2092 len1 -= dwSize;
2093 }
2094
2095 return len - len1;
2096}
2097
2098static void win_stdio_wait_func(void *opaque)
2099{
2100 CharDriverState *chr = opaque;
2101 WinStdioCharState *stdio = chr->opaque;
2102 INPUT_RECORD buf[4];
2103 int ret;
2104 DWORD dwSize;
2105 int i;
2106
Stefan Weildff74242013-12-07 14:48:04 +01002107 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002108
2109 if (!ret) {
2110 /* Avoid error storm */
2111 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2112 return;
2113 }
2114
2115 for (i = 0; i < dwSize; i++) {
2116 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2117
2118 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2119 int j;
2120 if (kev->uChar.AsciiChar != 0) {
2121 for (j = 0; j < kev->wRepeatCount; j++) {
2122 if (qemu_chr_be_can_write(chr)) {
2123 uint8_t c = kev->uChar.AsciiChar;
2124 qemu_chr_be_write(chr, &c, 1);
2125 }
2126 }
2127 }
2128 }
2129 }
2130}
2131
2132static DWORD WINAPI win_stdio_thread(LPVOID param)
2133{
2134 CharDriverState *chr = param;
2135 WinStdioCharState *stdio = chr->opaque;
2136 int ret;
2137 DWORD dwSize;
2138
2139 while (1) {
2140
2141 /* Wait for one byte */
2142 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2143
2144 /* Exit in case of error, continue if nothing read */
2145 if (!ret) {
2146 break;
2147 }
2148 if (!dwSize) {
2149 continue;
2150 }
2151
2152 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2153 if (stdio->win_stdio_buf == '\r') {
2154 continue;
2155 }
2156
2157 /* Signal the main thread and wait until the byte was eaten */
2158 if (!SetEvent(stdio->hInputReadyEvent)) {
2159 break;
2160 }
2161 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2162 != WAIT_OBJECT_0) {
2163 break;
2164 }
2165 }
2166
2167 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2168 return 0;
2169}
2170
2171static void win_stdio_thread_wait_func(void *opaque)
2172{
2173 CharDriverState *chr = opaque;
2174 WinStdioCharState *stdio = chr->opaque;
2175
2176 if (qemu_chr_be_can_write(chr)) {
2177 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2178 }
2179
2180 SetEvent(stdio->hInputDoneEvent);
2181}
2182
2183static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2184{
2185 WinStdioCharState *stdio = chr->opaque;
2186 DWORD dwMode = 0;
2187
2188 GetConsoleMode(stdio->hStdIn, &dwMode);
2189
2190 if (echo) {
2191 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2192 } else {
2193 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2194 }
2195}
2196
2197static void win_stdio_close(CharDriverState *chr)
2198{
2199 WinStdioCharState *stdio = chr->opaque;
2200
2201 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2202 CloseHandle(stdio->hInputReadyEvent);
2203 }
2204 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2205 CloseHandle(stdio->hInputDoneEvent);
2206 }
2207 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2208 TerminateThread(stdio->hInputThread, 0);
2209 }
2210
2211 g_free(chr->opaque);
2212 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002213}
2214
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002215static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002216{
2217 CharDriverState *chr;
2218 WinStdioCharState *stdio;
2219 DWORD dwMode;
2220 int is_console = 0;
2221
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002222 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002223 stdio = g_malloc0(sizeof(WinStdioCharState));
2224
2225 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2226 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2227 fprintf(stderr, "cannot open stdio: invalid handle\n");
2228 exit(1);
2229 }
2230
2231 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2232
2233 chr->opaque = stdio;
2234 chr->chr_write = win_stdio_write;
2235 chr->chr_close = win_stdio_close;
2236
Anthony Liguoried7a1542013-03-05 23:21:17 +05302237 if (is_console) {
2238 if (qemu_add_wait_object(stdio->hStdIn,
2239 win_stdio_wait_func, chr)) {
2240 fprintf(stderr, "qemu_add_wait_object: failed\n");
2241 }
2242 } else {
2243 DWORD dwId;
2244
2245 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2246 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2247 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2248 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002249
Anthony Liguoried7a1542013-03-05 23:21:17 +05302250 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2251 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2252 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2253 fprintf(stderr, "cannot create stdio thread or event\n");
2254 exit(1);
2255 }
2256 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2257 win_stdio_thread_wait_func, chr)) {
2258 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002259 }
2260 }
2261
2262 dwMode |= ENABLE_LINE_INPUT;
2263
Anthony Liguoried7a1542013-03-05 23:21:17 +05302264 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002265 /* set the terminal in raw mode */
2266 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2267 dwMode |= ENABLE_PROCESSED_INPUT;
2268 }
2269
2270 SetConsoleMode(stdio->hStdIn, dwMode);
2271
2272 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2273 qemu_chr_fe_set_echo(chr, false);
2274
Markus Armbruster1f514702012-02-07 15:09:08 +01002275 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002276}
aliguori6f97dba2008-10-31 18:49:55 +00002277#endif /* !_WIN32 */
2278
Anthony Liguori5ab82112013-03-05 23:21:31 +05302279
aliguori6f97dba2008-10-31 18:49:55 +00002280/***********************************************************/
2281/* UDP Net console */
2282
2283typedef struct {
2284 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302285 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302286 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002287 int bufcnt;
2288 int bufptr;
2289 int max_size;
2290} NetCharDriver;
2291
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002292/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002293static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2294{
2295 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302296 gsize bytes_written;
2297 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002298
Anthony Liguori76a96442013-03-05 23:21:21 +05302299 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2300 if (status == G_IO_STATUS_EOF) {
2301 return 0;
2302 } else if (status != G_IO_STATUS_NORMAL) {
2303 return -1;
2304 }
2305
2306 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002307}
2308
2309static int udp_chr_read_poll(void *opaque)
2310{
2311 CharDriverState *chr = opaque;
2312 NetCharDriver *s = chr->opaque;
2313
Anthony Liguori909cda12011-08-15 11:17:31 -05002314 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002315
2316 /* If there were any stray characters in the queue process them
2317 * first
2318 */
2319 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002320 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002321 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002322 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002323 }
2324 return s->max_size;
2325}
2326
Anthony Liguori76a96442013-03-05 23:21:21 +05302327static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002328{
2329 CharDriverState *chr = opaque;
2330 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302331 gsize bytes_read = 0;
2332 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002333
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002334 if (s->max_size == 0) {
2335 return TRUE;
2336 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302337 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2338 &bytes_read, NULL);
2339 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002340 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302341 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302342 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302343 return FALSE;
2344 }
aliguori6f97dba2008-10-31 18:49:55 +00002345
2346 s->bufptr = 0;
2347 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002348 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002349 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002350 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002351 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302352
2353 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002354}
2355
2356static void udp_chr_update_read_handler(CharDriverState *chr)
2357{
2358 NetCharDriver *s = chr->opaque;
2359
Amit Shah26da70c2013-08-28 15:23:37 +05302360 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302361 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302362 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2363 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002364 }
2365}
2366
aliguori819f56b2009-03-28 17:58:14 +00002367static void udp_chr_close(CharDriverState *chr)
2368{
2369 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302370
2371 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302372 if (s->chan) {
2373 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002374 closesocket(s->fd);
2375 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002376 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002377 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002378}
2379
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002380static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002381{
2382 CharDriverState *chr = NULL;
2383 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002384
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002385 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002386 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002387
aliguori6f97dba2008-10-31 18:49:55 +00002388 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302389 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002390 s->bufcnt = 0;
2391 s->bufptr = 0;
2392 chr->opaque = s;
2393 chr->chr_write = udp_chr_write;
2394 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002395 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002396 /* be isn't opened until we get a connection */
2397 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002398 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002399}
aliguori6f97dba2008-10-31 18:49:55 +00002400
aliguori6f97dba2008-10-31 18:49:55 +00002401/***********************************************************/
2402/* TCP Net console */
2403
2404typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302405
2406 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302407 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002408 int fd, listen_fd;
2409 int connected;
2410 int max_size;
2411 int do_telnetopt;
2412 int do_nodelay;
2413 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002414 int *read_msgfds;
2415 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002416 int *write_msgfds;
2417 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002418} TCPCharDriver;
2419
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302420static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002421
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002422#ifndef _WIN32
2423static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2424{
2425 TCPCharDriver *s = chr->opaque;
2426 struct msghdr msgh;
2427 struct iovec iov;
2428 int r;
2429
2430 size_t fd_size = s->write_msgfds_num * sizeof(int);
2431 char control[CMSG_SPACE(fd_size)];
2432 struct cmsghdr *cmsg;
2433
2434 memset(&msgh, 0, sizeof(msgh));
2435 memset(control, 0, sizeof(control));
2436
2437 /* set the payload */
2438 iov.iov_base = (uint8_t *) buf;
2439 iov.iov_len = len;
2440
2441 msgh.msg_iov = &iov;
2442 msgh.msg_iovlen = 1;
2443
2444 msgh.msg_control = control;
2445 msgh.msg_controllen = sizeof(control);
2446
2447 cmsg = CMSG_FIRSTHDR(&msgh);
2448
2449 cmsg->cmsg_len = CMSG_LEN(fd_size);
2450 cmsg->cmsg_level = SOL_SOCKET;
2451 cmsg->cmsg_type = SCM_RIGHTS;
2452 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2453
2454 do {
2455 r = sendmsg(s->fd, &msgh, 0);
2456 } while (r < 0 && errno == EINTR);
2457
2458 /* free the written msgfds, no matter what */
2459 if (s->write_msgfds_num) {
2460 g_free(s->write_msgfds);
2461 s->write_msgfds = 0;
2462 s->write_msgfds_num = 0;
2463 }
2464
2465 return r;
2466}
2467#endif
2468
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002469/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002470static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2471{
2472 TCPCharDriver *s = chr->opaque;
2473 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002474#ifndef _WIN32
2475 if (s->is_unix && s->write_msgfds_num) {
2476 return unix_send_msgfds(chr, buf, len);
2477 } else
2478#endif
2479 {
2480 return io_channel_send(s->chan, buf, len);
2481 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002482 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002483 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002484 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002485 }
2486}
2487
2488static int tcp_chr_read_poll(void *opaque)
2489{
2490 CharDriverState *chr = opaque;
2491 TCPCharDriver *s = chr->opaque;
2492 if (!s->connected)
2493 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002494 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002495 return s->max_size;
2496}
2497
2498#define IAC 255
2499#define IAC_BREAK 243
2500static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2501 TCPCharDriver *s,
2502 uint8_t *buf, int *size)
2503{
2504 /* Handle any telnet client's basic IAC options to satisfy char by
2505 * char mode with no echo. All IAC options will be removed from
2506 * the buf and the do_telnetopt variable will be used to track the
2507 * state of the width of the IAC information.
2508 *
2509 * IAC commands come in sets of 3 bytes with the exception of the
2510 * "IAC BREAK" command and the double IAC.
2511 */
2512
2513 int i;
2514 int j = 0;
2515
2516 for (i = 0; i < *size; i++) {
2517 if (s->do_telnetopt > 1) {
2518 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2519 /* Double IAC means send an IAC */
2520 if (j != i)
2521 buf[j] = buf[i];
2522 j++;
2523 s->do_telnetopt = 1;
2524 } else {
2525 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2526 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002527 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002528 s->do_telnetopt++;
2529 }
2530 s->do_telnetopt++;
2531 }
2532 if (s->do_telnetopt >= 4) {
2533 s->do_telnetopt = 1;
2534 }
2535 } else {
2536 if ((unsigned char)buf[i] == IAC) {
2537 s->do_telnetopt = 2;
2538 } else {
2539 if (j != i)
2540 buf[j] = buf[i];
2541 j++;
2542 }
2543 }
2544 }
2545 *size = j;
2546}
2547
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002548static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002549{
2550 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002551 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2552
2553 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002554 int i;
2555
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002556 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2557
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002558 /* Close unused fds */
2559 for (i = to_copy; i < s->read_msgfds_num; i++) {
2560 close(s->read_msgfds[i]);
2561 }
2562
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002563 g_free(s->read_msgfds);
2564 s->read_msgfds = 0;
2565 s->read_msgfds_num = 0;
2566 }
2567
2568 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002569}
2570
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002571static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2572{
2573 TCPCharDriver *s = chr->opaque;
2574
2575 /* clear old pending fd array */
2576 if (s->write_msgfds) {
2577 g_free(s->write_msgfds);
2578 }
2579
2580 if (num) {
2581 s->write_msgfds = g_malloc(num * sizeof(int));
2582 memcpy(s->write_msgfds, fds, num * sizeof(int));
2583 }
2584
2585 s->write_msgfds_num = num;
2586
2587 return 0;
2588}
2589
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002590#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002591static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2592{
2593 TCPCharDriver *s = chr->opaque;
2594 struct cmsghdr *cmsg;
2595
2596 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002597 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002598
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002599 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002600 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002601 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002602 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002603 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002604
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002605 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2606
2607 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002608 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002609 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002610
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002611 /* close and clean read_msgfds */
2612 for (i = 0; i < s->read_msgfds_num; i++) {
2613 close(s->read_msgfds[i]);
2614 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002615
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002616 if (s->read_msgfds_num) {
2617 g_free(s->read_msgfds);
2618 }
2619
2620 s->read_msgfds_num = fd_size / sizeof(int);
2621 s->read_msgfds = g_malloc(fd_size);
2622 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2623
2624 for (i = 0; i < s->read_msgfds_num; i++) {
2625 int fd = s->read_msgfds[i];
2626 if (fd < 0) {
2627 continue;
2628 }
2629
2630 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2631 qemu_set_block(fd);
2632
2633 #ifndef MSG_CMSG_CLOEXEC
2634 qemu_set_cloexec(fd);
2635 #endif
2636 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002637 }
2638}
2639
Mark McLoughlin9977c892009-07-22 09:11:38 +01002640static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2641{
2642 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002643 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002644 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002645 union {
2646 struct cmsghdr cmsg;
2647 char control[CMSG_SPACE(sizeof(int))];
2648 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002649 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002650 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002651
2652 iov[0].iov_base = buf;
2653 iov[0].iov_len = len;
2654
2655 msg.msg_iov = iov;
2656 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002657 msg.msg_control = &msg_control;
2658 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002659
Corey Bryant06138652012-08-14 16:43:42 -04002660#ifdef MSG_CMSG_CLOEXEC
2661 flags |= MSG_CMSG_CLOEXEC;
2662#endif
2663 ret = recvmsg(s->fd, &msg, flags);
2664 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002665 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002666 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002667
2668 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002669}
2670#else
2671static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2672{
2673 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002674 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002675}
2676#endif
2677
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302678static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2679{
2680 TCPCharDriver *s = chr->opaque;
2681 return g_io_create_watch(s->chan, cond);
2682}
2683
Peter Maydell1e3328b2014-06-16 18:36:39 +01002684static int tcp_chr_del_client(CharDriverState *chr)
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002685{
2686 TCPCharDriver *s = chr->opaque;
2687
Peter Maydell1e3328b2014-06-16 18:36:39 +01002688 if (!s->connected) {
2689 return -1;
2690 }
2691
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002692 s->connected = 0;
2693 if (s->listen_chan) {
2694 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2695 tcp_chr_accept, chr);
2696 }
2697 remove_fd_in_watch(chr);
2698 g_io_channel_unref(s->chan);
2699 s->chan = NULL;
2700 closesocket(s->fd);
2701 s->fd = -1;
2702 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Peter Maydell1e3328b2014-06-16 18:36:39 +01002703 return 0;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002704}
2705
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302706static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002707{
2708 CharDriverState *chr = opaque;
2709 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302710 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002711 int len, size;
2712
Kirill Batuzov812c1052014-07-01 15:52:32 +04002713 if (cond & G_IO_HUP) {
2714 /* connection closed */
Peter Maydell1e3328b2014-06-16 18:36:39 +01002715 tcp_chr_del_client(chr);
Kirill Batuzov812c1052014-07-01 15:52:32 +04002716 return TRUE;
2717 }
2718
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302719 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002720 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302721 }
aliguori6f97dba2008-10-31 18:49:55 +00002722 len = sizeof(buf);
2723 if (len > s->max_size)
2724 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002725 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002726 if (size == 0) {
2727 /* connection closed */
Peter Maydell1e3328b2014-06-16 18:36:39 +01002728 tcp_chr_del_client(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002729 } else if (size > 0) {
2730 if (s->do_telnetopt)
2731 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2732 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002733 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002734 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302735
2736 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002737}
2738
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002739static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2740{
2741 TCPCharDriver *s = chr->opaque;
2742 int size;
2743
2744 if (!s->connected) {
2745 return 0;
2746 }
2747
2748 size = tcp_chr_recv(chr, (void *) buf, len);
2749 if (size == 0) {
2750 /* connection closed */
Peter Maydell1e3328b2014-06-16 18:36:39 +01002751 tcp_chr_del_client(chr);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002752 }
2753
2754 return size;
2755}
2756
Blue Swirl68c18d12010-08-15 09:46:24 +00002757#ifndef _WIN32
2758CharDriverState *qemu_chr_open_eventfd(int eventfd)
2759{
David Marchande9d21c42014-06-11 17:25:16 +02002760 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2761
2762 if (chr) {
2763 chr->avail_connections = 1;
2764 }
2765
2766 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002767}
Blue Swirl68c18d12010-08-15 09:46:24 +00002768#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002769
aliguori6f97dba2008-10-31 18:49:55 +00002770static void tcp_chr_connect(void *opaque)
2771{
2772 CharDriverState *chr = opaque;
2773 TCPCharDriver *s = chr->opaque;
2774
2775 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302776 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302777 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2778 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002779 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002780 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002781}
2782
Gal Hammerac1b84d2014-02-25 12:12:35 +02002783static void tcp_chr_update_read_handler(CharDriverState *chr)
2784{
2785 TCPCharDriver *s = chr->opaque;
2786
2787 remove_fd_in_watch(chr);
2788 if (s->chan) {
2789 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2790 tcp_chr_read, chr);
2791 }
2792}
2793
aliguori6f97dba2008-10-31 18:49:55 +00002794#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2795static void tcp_chr_telnet_init(int fd)
2796{
2797 char buf[3];
2798 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2799 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2800 send(fd, (char *)buf, 3, 0);
2801 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2802 send(fd, (char *)buf, 3, 0);
2803 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2804 send(fd, (char *)buf, 3, 0);
2805 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2806 send(fd, (char *)buf, 3, 0);
2807}
2808
Daniel P. Berrange13661082011-06-23 13:31:42 +01002809static int tcp_chr_add_client(CharDriverState *chr, int fd)
2810{
2811 TCPCharDriver *s = chr->opaque;
2812 if (s->fd != -1)
2813 return -1;
2814
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002815 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002816 if (s->do_nodelay)
2817 socket_set_nodelay(fd);
2818 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302819 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002820 if (s->listen_tag) {
2821 g_source_remove(s->listen_tag);
2822 s->listen_tag = 0;
2823 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002824 tcp_chr_connect(chr);
2825
2826 return 0;
2827}
2828
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302829static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002830{
2831 CharDriverState *chr = opaque;
2832 TCPCharDriver *s = chr->opaque;
2833 struct sockaddr_in saddr;
2834#ifndef _WIN32
2835 struct sockaddr_un uaddr;
2836#endif
2837 struct sockaddr *addr;
2838 socklen_t len;
2839 int fd;
2840
2841 for(;;) {
2842#ifndef _WIN32
2843 if (s->is_unix) {
2844 len = sizeof(uaddr);
2845 addr = (struct sockaddr *)&uaddr;
2846 } else
2847#endif
2848 {
2849 len = sizeof(saddr);
2850 addr = (struct sockaddr *)&saddr;
2851 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002852 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002853 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002854 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302855 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002856 } else if (fd >= 0) {
2857 if (s->do_telnetopt)
2858 tcp_chr_telnet_init(fd);
2859 break;
2860 }
2861 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002862 if (tcp_chr_add_client(chr, fd) < 0)
2863 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302864
2865 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002866}
2867
2868static void tcp_chr_close(CharDriverState *chr)
2869{
2870 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002871 int i;
aliguori819f56b2009-03-28 17:58:14 +00002872 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302873 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302874 if (s->chan) {
2875 g_io_channel_unref(s->chan);
2876 }
aliguori6f97dba2008-10-31 18:49:55 +00002877 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002878 }
2879 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302880 if (s->listen_tag) {
2881 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002882 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302883 }
2884 if (s->listen_chan) {
2885 g_io_channel_unref(s->listen_chan);
2886 }
aliguori6f97dba2008-10-31 18:49:55 +00002887 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002888 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002889 if (s->read_msgfds_num) {
2890 for (i = 0; i < s->read_msgfds_num; i++) {
2891 close(s->read_msgfds[i]);
2892 }
2893 g_free(s->read_msgfds);
2894 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002895 if (s->write_msgfds_num) {
2896 g_free(s->write_msgfds);
2897 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002898 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002899 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002900}
2901
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002902static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2903 bool is_listen, bool is_telnet,
2904 bool is_waitconnect,
2905 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002906{
2907 CharDriverState *chr = NULL;
2908 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002909 char host[NI_MAXHOST], serv[NI_MAXSERV];
2910 const char *left = "", *right = "";
2911 struct sockaddr_storage ss;
2912 socklen_t ss_len = sizeof(ss);
2913
2914 memset(&ss, 0, ss_len);
2915 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002916 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002917 return NULL;
2918 }
2919
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002920 chr = qemu_chr_alloc();
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002921 s = g_malloc0(sizeof(TCPCharDriver));
2922
2923 s->connected = 0;
2924 s->fd = -1;
2925 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002926 s->read_msgfds = 0;
2927 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002928 s->write_msgfds = 0;
2929 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002930
2931 chr->filename = g_malloc(256);
2932 switch (ss.ss_family) {
2933#ifndef _WIN32
2934 case AF_UNIX:
2935 s->is_unix = 1;
2936 snprintf(chr->filename, 256, "unix:%s%s",
2937 ((struct sockaddr_un *)(&ss))->sun_path,
2938 is_listen ? ",server" : "");
2939 break;
2940#endif
2941 case AF_INET6:
2942 left = "[";
2943 right = "]";
2944 /* fall through */
2945 case AF_INET:
2946 s->do_nodelay = do_nodelay;
2947 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2948 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002949 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002950 is_telnet ? "telnet" : "tcp",
2951 left, host, right, serv,
2952 is_listen ? ",server" : "");
2953 break;
2954 }
2955
2956 chr->opaque = s;
2957 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002958 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002959 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002960 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002961 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002962 chr->chr_add_client = tcp_chr_add_client;
Peter Maydell1e3328b2014-06-16 18:36:39 +01002963 chr->chr_del_client = tcp_chr_del_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302964 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002965 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002966 /* be isn't opened until we get a connection */
2967 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002968
2969 if (is_listen) {
2970 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302971 s->listen_chan = io_channel_from_socket(s->listen_fd);
2972 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002973 if (is_telnet) {
2974 s->do_telnetopt = 1;
2975 }
2976 } else {
2977 s->connected = 1;
2978 s->fd = fd;
2979 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302980 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002981 tcp_chr_connect(chr);
2982 }
2983
2984 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002985 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2986 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302987 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002988 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002989 }
2990 return chr;
2991}
2992
Lei Li51767e72013-01-25 00:03:19 +08002993/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002994/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002995
2996typedef struct {
2997 size_t size;
2998 size_t prod;
2999 size_t cons;
3000 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003001} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003002
Markus Armbruster3949e592013-02-06 21:27:24 +01003003static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003004{
Markus Armbruster3949e592013-02-06 21:27:24 +01003005 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003006
Markus Armbruster5c230102013-02-06 21:27:23 +01003007 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003008}
3009
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003010/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003011static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003012{
Markus Armbruster3949e592013-02-06 21:27:24 +01003013 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003014 int i;
3015
3016 if (!buf || (len < 0)) {
3017 return -1;
3018 }
3019
3020 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003021 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3022 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003023 d->cons = d->prod - d->size;
3024 }
3025 }
3026
3027 return 0;
3028}
3029
Markus Armbruster3949e592013-02-06 21:27:24 +01003030static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003031{
Markus Armbruster3949e592013-02-06 21:27:24 +01003032 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003033 int i;
3034
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003035 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003036 for (i = 0; i < len && d->cons != d->prod; i++) {
3037 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003038 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003039 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003040
3041 return i;
3042}
3043
Markus Armbruster3949e592013-02-06 21:27:24 +01003044static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003045{
Markus Armbruster3949e592013-02-06 21:27:24 +01003046 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003047
3048 g_free(d->cbuf);
3049 g_free(d);
3050 chr->opaque = NULL;
3051}
3052
Markus Armbruster4f573782013-07-26 16:44:32 +02003053static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3054 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003055{
3056 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003057 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003058
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003059 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003060 d = g_malloc(sizeof(*d));
3061
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003062 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003063
3064 /* The size must be power of 2 */
3065 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003066 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003067 goto fail;
3068 }
3069
3070 d->prod = 0;
3071 d->cons = 0;
3072 d->cbuf = g_malloc0(d->size);
3073
3074 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003075 chr->chr_write = ringbuf_chr_write;
3076 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003077
3078 return chr;
3079
3080fail:
3081 g_free(d);
3082 g_free(chr);
3083 return NULL;
3084}
3085
Hani Benhabiles8e597772014-05-27 23:39:30 +01003086bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003087{
Markus Armbruster3949e592013-02-06 21:27:24 +01003088 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003089}
3090
Markus Armbruster3949e592013-02-06 21:27:24 +01003091void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003092 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003093 Error **errp)
3094{
3095 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003096 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003097 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003098 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003099
3100 chr = qemu_chr_find(device);
3101 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003102 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003103 return;
3104 }
3105
Markus Armbruster3949e592013-02-06 21:27:24 +01003106 if (!chr_is_ringbuf(chr)) {
3107 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003108 return;
3109 }
3110
Lei Li1f590cf2013-01-25 00:03:20 +08003111 if (has_format && (format == DATA_FORMAT_BASE64)) {
3112 write_data = g_base64_decode(data, &write_count);
3113 } else {
3114 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003115 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003116 }
3117
Markus Armbruster3949e592013-02-06 21:27:24 +01003118 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003119
Markus Armbruster13289fb2013-02-06 21:27:18 +01003120 if (write_data != (uint8_t *)data) {
3121 g_free((void *)write_data);
3122 }
3123
Lei Li1f590cf2013-01-25 00:03:20 +08003124 if (ret < 0) {
3125 error_setg(errp, "Failed to write to device %s", device);
3126 return;
3127 }
3128}
3129
Markus Armbruster3949e592013-02-06 21:27:24 +01003130char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003131 bool has_format, enum DataFormat format,
3132 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003133{
3134 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003135 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003136 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003137 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003138
3139 chr = qemu_chr_find(device);
3140 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003141 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003142 return NULL;
3143 }
3144
Markus Armbruster3949e592013-02-06 21:27:24 +01003145 if (!chr_is_ringbuf(chr)) {
3146 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003147 return NULL;
3148 }
3149
3150 if (size <= 0) {
3151 error_setg(errp, "size must be greater than zero");
3152 return NULL;
3153 }
3154
Markus Armbruster3949e592013-02-06 21:27:24 +01003155 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003156 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003157 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003158
Markus Armbruster3949e592013-02-06 21:27:24 +01003159 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003160
3161 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003162 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003163 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003164 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003165 /*
3166 * FIXME should read only complete, valid UTF-8 characters up
3167 * to @size bytes. Invalid sequences should be replaced by a
3168 * suitable replacement character. Except when (and only
3169 * when) ring buffer lost characters since last read, initial
3170 * continuation characters should be dropped.
3171 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003172 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003173 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003174 }
3175
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003176 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003177}
3178
Gerd Hoffmann33521632009-12-08 13:11:49 +01003179QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003180{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003181 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003182 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003183 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003184 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003185 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003186
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003187 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003188 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003189 qerror_report_err(local_err);
3190 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003191 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003192 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003193
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003194 if (strstart(filename, "mon:", &p)) {
3195 filename = p;
3196 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003197 if (strcmp(filename, "stdio") == 0) {
3198 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3199 * but pass it to the guest. Handle this only for compat syntax,
3200 * for -chardev syntax we have special option for this.
3201 * This is what -nographic did, redirecting+muxing serial+monitor
3202 * to stdio causing Ctrl+C to be passed to guest. */
3203 qemu_opt_set(opts, "signal", "off");
3204 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003205 }
3206
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003207 if (strcmp(filename, "null") == 0 ||
3208 strcmp(filename, "pty") == 0 ||
3209 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003210 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003211 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003212 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003213 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003214 return opts;
3215 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003216 if (strstart(filename, "vc", &p)) {
3217 qemu_opt_set(opts, "backend", "vc");
3218 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003219 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003220 /* pixels */
3221 qemu_opt_set(opts, "width", width);
3222 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003223 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003224 /* chars */
3225 qemu_opt_set(opts, "cols", width);
3226 qemu_opt_set(opts, "rows", height);
3227 } else {
3228 goto fail;
3229 }
3230 }
3231 return opts;
3232 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003233 if (strcmp(filename, "con:") == 0) {
3234 qemu_opt_set(opts, "backend", "console");
3235 return opts;
3236 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003237 if (strstart(filename, "COM", NULL)) {
3238 qemu_opt_set(opts, "backend", "serial");
3239 qemu_opt_set(opts, "path", filename);
3240 return opts;
3241 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003242 if (strstart(filename, "file:", &p)) {
3243 qemu_opt_set(opts, "backend", "file");
3244 qemu_opt_set(opts, "path", p);
3245 return opts;
3246 }
3247 if (strstart(filename, "pipe:", &p)) {
3248 qemu_opt_set(opts, "backend", "pipe");
3249 qemu_opt_set(opts, "path", p);
3250 return opts;
3251 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003252 if (strstart(filename, "tcp:", &p) ||
3253 strstart(filename, "telnet:", &p)) {
3254 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3255 host[0] = 0;
3256 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3257 goto fail;
3258 }
3259 qemu_opt_set(opts, "backend", "socket");
3260 qemu_opt_set(opts, "host", host);
3261 qemu_opt_set(opts, "port", port);
3262 if (p[pos] == ',') {
3263 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3264 goto fail;
3265 }
3266 if (strstart(filename, "telnet:", &p))
3267 qemu_opt_set(opts, "telnet", "on");
3268 return opts;
3269 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003270 if (strstart(filename, "udp:", &p)) {
3271 qemu_opt_set(opts, "backend", "udp");
3272 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3273 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003274 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003275 goto fail;
3276 }
3277 }
3278 qemu_opt_set(opts, "host", host);
3279 qemu_opt_set(opts, "port", port);
3280 if (p[pos] == '@') {
3281 p += pos + 1;
3282 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3283 host[0] = 0;
3284 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003285 goto fail;
3286 }
3287 }
3288 qemu_opt_set(opts, "localaddr", host);
3289 qemu_opt_set(opts, "localport", port);
3290 }
3291 return opts;
3292 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003293 if (strstart(filename, "unix:", &p)) {
3294 qemu_opt_set(opts, "backend", "socket");
3295 if (qemu_opts_do_parse(opts, p, "path") != 0)
3296 goto fail;
3297 return opts;
3298 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003299 if (strstart(filename, "/dev/parport", NULL) ||
3300 strstart(filename, "/dev/ppi", NULL)) {
3301 qemu_opt_set(opts, "backend", "parport");
3302 qemu_opt_set(opts, "path", filename);
3303 return opts;
3304 }
3305 if (strstart(filename, "/dev/", NULL)) {
3306 qemu_opt_set(opts, "backend", "tty");
3307 qemu_opt_set(opts, "path", filename);
3308 return opts;
3309 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003310
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003311fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003312 qemu_opts_del(opts);
3313 return NULL;
3314}
3315
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003316static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3317 Error **errp)
3318{
3319 const char *path = qemu_opt_get(opts, "path");
3320
3321 if (path == NULL) {
3322 error_setg(errp, "chardev: file: no filename given");
3323 return;
3324 }
3325 backend->file = g_new0(ChardevFile, 1);
3326 backend->file->out = g_strdup(path);
3327}
3328
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003329static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3330 Error **errp)
3331{
3332 backend->stdio = g_new0(ChardevStdio, 1);
3333 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003334 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003335}
3336
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003337static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3338 Error **errp)
3339{
3340 const char *device = qemu_opt_get(opts, "path");
3341
3342 if (device == NULL) {
3343 error_setg(errp, "chardev: serial/tty: no device path given");
3344 return;
3345 }
3346 backend->serial = g_new0(ChardevHostdev, 1);
3347 backend->serial->device = g_strdup(device);
3348}
3349
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003350static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3351 Error **errp)
3352{
3353 const char *device = qemu_opt_get(opts, "path");
3354
3355 if (device == NULL) {
3356 error_setg(errp, "chardev: parallel: no device path given");
3357 return;
3358 }
3359 backend->parallel = g_new0(ChardevHostdev, 1);
3360 backend->parallel->device = g_strdup(device);
3361}
3362
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003363static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3364 Error **errp)
3365{
3366 const char *device = qemu_opt_get(opts, "path");
3367
3368 if (device == NULL) {
3369 error_setg(errp, "chardev: pipe: no device path given");
3370 return;
3371 }
3372 backend->pipe = g_new0(ChardevHostdev, 1);
3373 backend->pipe->device = g_strdup(device);
3374}
3375
Markus Armbruster4f573782013-07-26 16:44:32 +02003376static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3377 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003378{
3379 int val;
3380
Markus Armbruster3a1da422013-07-26 16:44:34 +02003381 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003382
Markus Armbruster0f953052013-06-27 16:22:07 +02003383 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003384 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003385 backend->ringbuf->has_size = true;
3386 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003387 }
3388}
3389
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003390static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3391 Error **errp)
3392{
3393 const char *chardev = qemu_opt_get(opts, "chardev");
3394
3395 if (chardev == NULL) {
3396 error_setg(errp, "chardev: mux: no chardev given");
3397 return;
3398 }
3399 backend->mux = g_new0(ChardevMux, 1);
3400 backend->mux->chardev = g_strdup(chardev);
3401}
3402
Peter Maydelldafd3252014-09-02 11:24:13 +01003403static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3404 Error **errp)
3405{
3406 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3407 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3408 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3409 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
3410 const char *path = qemu_opt_get(opts, "path");
3411 const char *host = qemu_opt_get(opts, "host");
3412 const char *port = qemu_opt_get(opts, "port");
3413 SocketAddress *addr;
3414
3415 if (!path) {
3416 if (!host) {
3417 error_setg(errp, "chardev: socket: no host given");
3418 return;
3419 }
3420 if (!port) {
3421 error_setg(errp, "chardev: socket: no port given");
3422 return;
3423 }
3424 }
3425
3426 backend->socket = g_new0(ChardevSocket, 1);
3427
3428 backend->socket->has_nodelay = true;
3429 backend->socket->nodelay = do_nodelay;
3430 backend->socket->has_server = true;
3431 backend->socket->server = is_listen;
3432 backend->socket->has_telnet = true;
3433 backend->socket->telnet = is_telnet;
3434 backend->socket->has_wait = true;
3435 backend->socket->wait = is_waitconnect;
3436
3437 addr = g_new0(SocketAddress, 1);
3438 if (path) {
3439 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3440 addr->q_unix = g_new0(UnixSocketAddress, 1);
3441 addr->q_unix->path = g_strdup(path);
3442 } else {
3443 addr->kind = SOCKET_ADDRESS_KIND_INET;
3444 addr->inet = g_new0(InetSocketAddress, 1);
3445 addr->inet->host = g_strdup(host);
3446 addr->inet->port = g_strdup(port);
3447 addr->inet->has_to = qemu_opt_get(opts, "to");
3448 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3449 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3450 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3451 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3452 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3453 }
3454 backend->socket->addr = addr;
3455}
3456
Peter Maydell90a14bf2014-09-02 11:24:15 +01003457static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3458 Error **errp)
3459{
3460 const char *host = qemu_opt_get(opts, "host");
3461 const char *port = qemu_opt_get(opts, "port");
3462 const char *localaddr = qemu_opt_get(opts, "localaddr");
3463 const char *localport = qemu_opt_get(opts, "localport");
3464 bool has_local = false;
3465 SocketAddress *addr;
3466
3467 if (host == NULL || strlen(host) == 0) {
3468 host = "localhost";
3469 }
3470 if (port == NULL || strlen(port) == 0) {
3471 error_setg(errp, "chardev: udp: remote port not specified");
3472 return;
3473 }
3474 if (localport == NULL || strlen(localport) == 0) {
3475 localport = "0";
3476 } else {
3477 has_local = true;
3478 }
3479 if (localaddr == NULL || strlen(localaddr) == 0) {
3480 localaddr = "";
3481 } else {
3482 has_local = true;
3483 }
3484
3485 backend->udp = g_new0(ChardevUdp, 1);
3486
3487 addr = g_new0(SocketAddress, 1);
3488 addr->kind = SOCKET_ADDRESS_KIND_INET;
3489 addr->inet = g_new0(InetSocketAddress, 1);
3490 addr->inet->host = g_strdup(host);
3491 addr->inet->port = g_strdup(port);
3492 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3493 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3494 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3495 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3496 backend->udp->remote = addr;
3497
3498 if (has_local) {
3499 backend->udp->has_local = true;
3500 addr = g_new0(SocketAddress, 1);
3501 addr->kind = SOCKET_ADDRESS_KIND_INET;
3502 addr->inet = g_new0(InetSocketAddress, 1);
3503 addr->inet->host = g_strdup(localaddr);
3504 addr->inet->port = g_strdup(localport);
3505 backend->udp->local = addr;
3506 }
3507}
3508
Anthony Liguorid654f342013-03-05 23:21:28 +05303509typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003510 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003511 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003512 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303513} CharDriver;
3514
3515static GSList *backends;
3516
Peter Maydelle4d50d42014-09-02 11:24:17 +01003517void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003518 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3519{
3520 CharDriver *s;
3521
3522 s = g_malloc0(sizeof(*s));
3523 s->name = g_strdup(name);
3524 s->kind = kind;
3525 s->parse = parse;
3526
3527 backends = g_slist_append(backends, s);
3528}
3529
Anthony Liguorif69554b2011-08-15 11:17:37 -05003530CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003531 void (*init)(struct CharDriverState *s),
3532 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003533{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003534 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303535 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003536 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303537 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003538 ChardevReturn *ret = NULL;
3539 ChardevBackend *backend;
3540 const char *id = qemu_opts_id(opts);
3541 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003542
Peter Maydella61ae7f2014-09-02 11:24:16 +01003543 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003544 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003545 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003546 }
3547
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003548 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003549 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003550 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003551 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003552 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303553 for (i = backends; i; i = i->next) {
3554 cd = i->data;
3555
3556 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003557 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303558 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003559 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303560 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003561 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003562 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003563 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003564 }
3565
Peter Maydella61ae7f2014-09-02 11:24:16 +01003566 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003567
3568 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003569 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003570 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003571
3572 chr = NULL;
3573 backend->kind = cd->kind;
3574 if (cd->parse) {
3575 cd->parse(opts, backend, &local_err);
3576 if (local_err) {
3577 error_propagate(errp, local_err);
3578 goto qapi_out;
3579 }
3580 }
3581 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3582 if (!ret) {
3583 goto qapi_out;
3584 }
3585
3586 if (bid) {
3587 qapi_free_ChardevBackend(backend);
3588 qapi_free_ChardevReturn(ret);
3589 backend = g_new0(ChardevBackend, 1);
3590 backend->mux = g_new0(ChardevMux, 1);
3591 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3592 backend->mux->chardev = g_strdup(bid);
3593 ret = qmp_chardev_add(id, backend, errp);
3594 if (!ret) {
3595 chr = qemu_chr_find(bid);
3596 qemu_chr_delete(chr);
3597 chr = NULL;
3598 goto qapi_out;
3599 }
3600 }
3601
3602 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003603 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003604
3605qapi_out:
3606 qapi_free_ChardevBackend(backend);
3607 qapi_free_ChardevReturn(ret);
3608 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003609 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003610
3611err:
3612 qemu_opts_del(opts);
3613 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003614}
3615
Anthony Liguori27143a42011-08-15 11:17:36 -05003616CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003617{
3618 const char *p;
3619 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003620 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003621 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003622
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003623 if (strstart(filename, "chardev:", &p)) {
3624 return qemu_chr_find(p);
3625 }
3626
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003627 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003628 if (!opts)
3629 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003630
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003631 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003632 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003633 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003634 error_free(err);
3635 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003636 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003637 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003638 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003639 }
3640 return chr;
3641}
3642
Anthony Liguori15f31512011-08-15 11:17:35 -05003643void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003644{
3645 if (chr->chr_set_echo) {
3646 chr->chr_set_echo(chr, echo);
3647 }
3648}
3649
Hans de Goede8e25daa2013-03-26 11:07:57 +01003650void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003651{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003652 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003653 return;
3654 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003655 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003656 if (chr->chr_set_fe_open) {
3657 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003658 }
3659}
3660
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003661void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3662{
3663 if (chr->chr_fe_event) {
3664 chr->chr_fe_event(chr, event);
3665 }
3666}
3667
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003668int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3669 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303670{
3671 GSource *src;
3672 guint tag;
3673
3674 if (s->chr_add_watch == NULL) {
3675 return -ENOSYS;
3676 }
3677
3678 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003679 if (!src) {
3680 return -EINVAL;
3681 }
3682
Anthony Liguori23673ca2013-03-05 23:21:23 +05303683 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3684 tag = g_source_attach(src, NULL);
3685 g_source_unref(src);
3686
3687 return tag;
3688}
3689
Hans de Goede44c473d2013-03-27 20:29:39 +01003690int qemu_chr_fe_claim(CharDriverState *s)
3691{
3692 if (s->avail_connections < 1) {
3693 return -1;
3694 }
3695 s->avail_connections--;
3696 return 0;
3697}
3698
3699void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3700{
3701 if (qemu_chr_fe_claim(s) != 0) {
3702 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3703 __func__, s->label);
3704 exit(1);
3705 }
3706}
3707
3708void qemu_chr_fe_release(CharDriverState *s)
3709{
3710 s->avail_connections++;
3711}
3712
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003713void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003714{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003715 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003716 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003717 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003718 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003719 g_free(chr->filename);
3720 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003721 if (chr->opts) {
3722 qemu_opts_del(chr->opts);
3723 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003724 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003725}
3726
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003727ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003728{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003729 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003730 CharDriverState *chr;
3731
Blue Swirl72cf2d42009-09-12 07:36:22 +00003732 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003733 ChardevInfoList *info = g_malloc0(sizeof(*info));
3734 info->value = g_malloc0(sizeof(*info->value));
3735 info->value->label = g_strdup(chr->label);
3736 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003737 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003738
3739 info->next = chr_list;
3740 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003741 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003742
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003743 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003744}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003745
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003746ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3747{
3748 ChardevBackendInfoList *backend_list = NULL;
3749 CharDriver *c = NULL;
3750 GSList *i = NULL;
3751
3752 for (i = backends; i; i = i->next) {
3753 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3754 c = i->data;
3755 info->value = g_malloc0(sizeof(*info->value));
3756 info->value->name = g_strdup(c->name);
3757
3758 info->next = backend_list;
3759 backend_list = info;
3760 }
3761
3762 return backend_list;
3763}
3764
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003765CharDriverState *qemu_chr_find(const char *name)
3766{
3767 CharDriverState *chr;
3768
Blue Swirl72cf2d42009-09-12 07:36:22 +00003769 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003770 if (strcmp(chr->label, name) != 0)
3771 continue;
3772 return chr;
3773 }
3774 return NULL;
3775}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003776
3777/* Get a character (serial) device interface. */
3778CharDriverState *qemu_char_get_next_serial(void)
3779{
3780 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003781 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003782
3783 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003784
3785 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3786 chr = serial_hds[next_serial++];
3787 qemu_chr_fe_claim_no_fail(chr);
3788 return chr;
3789 }
3790 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003791}
3792
Paolo Bonzini4d454572012-11-26 16:03:42 +01003793QemuOptsList qemu_chardev_opts = {
3794 .name = "chardev",
3795 .implied_opt_name = "backend",
3796 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3797 .desc = {
3798 {
3799 .name = "backend",
3800 .type = QEMU_OPT_STRING,
3801 },{
3802 .name = "path",
3803 .type = QEMU_OPT_STRING,
3804 },{
3805 .name = "host",
3806 .type = QEMU_OPT_STRING,
3807 },{
3808 .name = "port",
3809 .type = QEMU_OPT_STRING,
3810 },{
3811 .name = "localaddr",
3812 .type = QEMU_OPT_STRING,
3813 },{
3814 .name = "localport",
3815 .type = QEMU_OPT_STRING,
3816 },{
3817 .name = "to",
3818 .type = QEMU_OPT_NUMBER,
3819 },{
3820 .name = "ipv4",
3821 .type = QEMU_OPT_BOOL,
3822 },{
3823 .name = "ipv6",
3824 .type = QEMU_OPT_BOOL,
3825 },{
3826 .name = "wait",
3827 .type = QEMU_OPT_BOOL,
3828 },{
3829 .name = "server",
3830 .type = QEMU_OPT_BOOL,
3831 },{
3832 .name = "delay",
3833 .type = QEMU_OPT_BOOL,
3834 },{
3835 .name = "telnet",
3836 .type = QEMU_OPT_BOOL,
3837 },{
3838 .name = "width",
3839 .type = QEMU_OPT_NUMBER,
3840 },{
3841 .name = "height",
3842 .type = QEMU_OPT_NUMBER,
3843 },{
3844 .name = "cols",
3845 .type = QEMU_OPT_NUMBER,
3846 },{
3847 .name = "rows",
3848 .type = QEMU_OPT_NUMBER,
3849 },{
3850 .name = "mux",
3851 .type = QEMU_OPT_BOOL,
3852 },{
3853 .name = "signal",
3854 .type = QEMU_OPT_BOOL,
3855 },{
3856 .name = "name",
3857 .type = QEMU_OPT_STRING,
3858 },{
3859 .name = "debug",
3860 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003861 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003862 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003863 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003864 },{
3865 .name = "chardev",
3866 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003867 },
3868 { /* end of list */ }
3869 },
3870};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003871
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003872#ifdef _WIN32
3873
3874static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3875{
3876 HANDLE out;
3877
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003878 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003879 error_setg(errp, "input file not supported");
3880 return NULL;
3881 }
3882
3883 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3884 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3885 if (out == INVALID_HANDLE_VALUE) {
3886 error_setg(errp, "open %s failed", file->out);
3887 return NULL;
3888 }
3889 return qemu_chr_open_win_file(out);
3890}
3891
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003892static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3893 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003894{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003895 return qemu_chr_open_win_path(serial->device);
3896}
3897
3898static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3899 Error **errp)
3900{
3901 error_setg(errp, "character device backend type 'parallel' not supported");
3902 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003903}
3904
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003905#else /* WIN32 */
3906
3907static int qmp_chardev_open_file_source(char *src, int flags,
3908 Error **errp)
3909{
3910 int fd = -1;
3911
3912 TFR(fd = qemu_open(src, flags, 0666));
3913 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003914 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003915 }
3916 return fd;
3917}
3918
3919static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3920{
Markus Armbruster5f758362014-05-19 18:57:34 +02003921 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003922
3923 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3924 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003925 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003926 return NULL;
3927 }
3928
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003929 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003930 flags = O_RDONLY;
3931 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003932 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003933 qemu_close(out);
3934 return NULL;
3935 }
3936 }
3937
3938 return qemu_chr_open_fd(in, out);
3939}
3940
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003941static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3942 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003943{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003944#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003945 int fd;
3946
3947 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003948 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003949 return NULL;
3950 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003951 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003952 return qemu_chr_open_tty_fd(fd);
3953#else
3954 error_setg(errp, "character device backend type 'serial' not supported");
3955 return NULL;
3956#endif
3957}
3958
3959static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3960 Error **errp)
3961{
3962#ifdef HAVE_CHARDEV_PARPORT
3963 int fd;
3964
3965 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003966 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003967 return NULL;
3968 }
3969 return qemu_chr_open_pp_fd(fd);
3970#else
3971 error_setg(errp, "character device backend type 'parallel' not supported");
3972 return NULL;
3973#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003974}
3975
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003976#endif /* WIN32 */
3977
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003978static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3979 Error **errp)
3980{
3981 SocketAddress *addr = sock->addr;
3982 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3983 bool is_listen = sock->has_server ? sock->server : true;
3984 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3985 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3986 int fd;
3987
3988 if (is_listen) {
3989 fd = socket_listen(addr, errp);
3990 } else {
3991 fd = socket_connect(addr, errp, NULL, NULL);
3992 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003993 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003994 return NULL;
3995 }
3996 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3997 is_telnet, is_waitconnect, errp);
3998}
3999
Lei Li08d0ab32013-05-20 14:51:03 +08004000static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4001 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004002{
4003 int fd;
4004
Lei Li08d0ab32013-05-20 14:51:03 +08004005 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004006 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004007 return NULL;
4008 }
4009 return qemu_chr_open_udp_fd(fd);
4010}
4011
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004012ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4013 Error **errp)
4014{
4015 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004016 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004017
4018 chr = qemu_chr_find(id);
4019 if (chr) {
4020 error_setg(errp, "Chardev '%s' already exists", id);
4021 g_free(ret);
4022 return NULL;
4023 }
4024
4025 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004026 case CHARDEV_BACKEND_KIND_FILE:
4027 chr = qmp_chardev_open_file(backend->file, errp);
4028 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004029 case CHARDEV_BACKEND_KIND_SERIAL:
4030 chr = qmp_chardev_open_serial(backend->serial, errp);
4031 break;
4032 case CHARDEV_BACKEND_KIND_PARALLEL:
4033 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004034 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004035 case CHARDEV_BACKEND_KIND_PIPE:
4036 chr = qemu_chr_open_pipe(backend->pipe);
4037 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004038 case CHARDEV_BACKEND_KIND_SOCKET:
4039 chr = qmp_chardev_open_socket(backend->socket, errp);
4040 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004041 case CHARDEV_BACKEND_KIND_UDP:
4042 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004043 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004044#ifdef HAVE_CHARDEV_TTY
4045 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004046 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004047 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004048#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004049 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004050 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004051 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004052 case CHARDEV_BACKEND_KIND_MUX:
4053 base = qemu_chr_find(backend->mux->chardev);
4054 if (base == NULL) {
4055 error_setg(errp, "mux: base chardev %s not found",
4056 backend->mux->chardev);
4057 break;
4058 }
4059 chr = qemu_chr_open_mux(base);
4060 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004061 case CHARDEV_BACKEND_KIND_MSMOUSE:
4062 chr = qemu_chr_open_msmouse();
4063 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004064#ifdef CONFIG_BRLAPI
4065 case CHARDEV_BACKEND_KIND_BRAILLE:
4066 chr = chr_baum_init();
4067 break;
4068#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004069 case CHARDEV_BACKEND_KIND_TESTDEV:
4070 chr = chr_testdev_init();
4071 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004072 case CHARDEV_BACKEND_KIND_STDIO:
4073 chr = qemu_chr_open_stdio(backend->stdio);
4074 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004075#ifdef _WIN32
4076 case CHARDEV_BACKEND_KIND_CONSOLE:
4077 chr = qemu_chr_open_win_con();
4078 break;
4079#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004080#ifdef CONFIG_SPICE
4081 case CHARDEV_BACKEND_KIND_SPICEVMC:
4082 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4083 break;
4084 case CHARDEV_BACKEND_KIND_SPICEPORT:
4085 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4086 break;
4087#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004088 case CHARDEV_BACKEND_KIND_VC:
4089 chr = vc_init(backend->vc);
4090 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004091 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004092 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004093 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004094 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004095 default:
4096 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4097 break;
4098 }
4099
Markus Armbruster3894c782014-05-19 18:57:36 +02004100 /*
4101 * Character backend open hasn't been fully converted to the Error
4102 * API. Some opens fail without setting an error. Set a generic
4103 * error then.
4104 * TODO full conversion to Error API
4105 */
4106 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004107 error_setg(errp, "Failed to create chardev");
4108 }
4109 if (chr) {
4110 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004111 chr->avail_connections =
4112 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004113 if (!chr->filename) {
4114 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4115 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004116 if (!chr->explicit_be_open) {
4117 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4118 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004119 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4120 return ret;
4121 } else {
4122 g_free(ret);
4123 return NULL;
4124 }
4125}
4126
4127void qmp_chardev_remove(const char *id, Error **errp)
4128{
4129 CharDriverState *chr;
4130
4131 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004132 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004133 error_setg(errp, "Chardev '%s' not found", id);
4134 return;
4135 }
4136 if (chr->chr_can_read || chr->chr_read ||
4137 chr->chr_event || chr->handler_opaque) {
4138 error_setg(errp, "Chardev '%s' is busy", id);
4139 return;
4140 }
4141 qemu_chr_delete(chr);
4142}
Anthony Liguorid654f342013-03-05 23:21:28 +05304143
4144static void register_types(void)
4145{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004146 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4147 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4148 qemu_chr_parse_socket);
4149 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4150 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4151 qemu_chr_parse_ringbuf);
4152 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4153 qemu_chr_parse_file_out);
4154 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4155 qemu_chr_parse_stdio);
4156 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4157 qemu_chr_parse_serial);
4158 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4159 qemu_chr_parse_serial);
4160 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4161 qemu_chr_parse_parallel);
4162 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4163 qemu_chr_parse_parallel);
4164 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4165 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4166 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4167 qemu_chr_parse_pipe);
4168 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004169 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004170 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4171 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004172 /* this must be done after machine init, since we register FEs with muxes
4173 * as part of realize functions like serial_isa_realizefn when -nographic
4174 * is specified
4175 */
4176 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304177}
4178
4179type_init(register_types);