blob: 947541a5594a62bfd5856fa09f01569b23a5db2c [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 Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzini927d4872012-12-17 18:20:05 +010029#include "char/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__GLIBC__)
57#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040058#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000062#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040063#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64#include <dev/ppbus/ppi.h>
65#include <dev/ppbus/ppbconf.h>
66#elif defined(__DragonFly__)
67#include <dev/misc/ppi/ppi.h>
68#include <bus/ppbus/ppbconf.h>
69#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010070#else
aliguori6f97dba2008-10-31 18:49:55 +000071#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000072#include <pty.h>
73
74#include <linux/ppdev.h>
75#include <linux/parport.h>
76#endif
77#ifdef __sun__
78#include <sys/stat.h>
79#include <sys/ethernet.h>
80#include <sys/sockio.h>
81#include <netinet/arp.h>
82#include <netinet/in.h>
83#include <netinet/in_systm.h>
84#include <netinet/ip.h>
85#include <netinet/ip_icmp.h> // must come after ip.h
86#include <netinet/udp.h>
87#include <netinet/tcp.h>
88#include <net/if.h>
89#include <syslog.h>
90#include <stropts.h>
91#endif
92#endif
93#endif
94
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010095#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020096#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000097
Amit Shah9bd78542009-11-03 19:59:54 +053098#define READ_BUF_LEN 4096
99
aliguori6f97dba2008-10-31 18:49:55 +0000100/***********************************************************/
101/* character device */
102
Blue Swirl72cf2d42009-09-12 07:36:22 +0000103static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000105
Hans de Goedea425d232011-11-19 10:22:43 +0100106void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000107{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200108 /* Keep track if the char device is open */
109 switch (event) {
110 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100111 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200112 break;
113 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100114 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200115 break;
116 }
117
aliguori6f97dba2008-10-31 18:49:55 +0000118 if (!s->chr_event)
119 return;
120 s->chr_event(s->handler_opaque, event);
121}
122
Hans de Goedefee204f2013-03-26 11:07:54 +0100123static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000124{
125 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100126 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Anthony Liguori9f939df2013-03-05 23:21:27 +0530127 s->idle_tag = 0;
128 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +0000129}
130
Hans de Goedefee204f2013-03-26 11:07:54 +0100131void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000132{
Anthony Liguori9f939df2013-03-05 23:21:27 +0530133 if (s->idle_tag == 0) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100134 s->idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000135 }
136}
137
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500138int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000139{
140 return s->chr_write(s, buf, len);
141}
142
Anthony Liguoricd187202013-03-26 10:04:17 -0500143int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
144{
145 int offset = 0;
146 int res;
147
148 while (offset < len) {
149 do {
150 res = s->chr_write(s, buf + offset, len - offset);
151 if (res == -1 && errno == EAGAIN) {
152 g_usleep(100);
153 }
154 } while (res == -1 && errno == EAGAIN);
155
156 if (res == 0) {
157 break;
158 }
159
160 if (res < 0) {
161 return res;
162 }
163
164 offset += res;
165 }
166
167 return offset;
168}
169
Anthony Liguori41084f12011-08-15 11:17:34 -0500170int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000171{
172 if (!s->chr_ioctl)
173 return -ENOTSUP;
174 return s->chr_ioctl(s, cmd, arg);
175}
176
Anthony Liguori909cda12011-08-15 11:17:31 -0500177int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000178{
179 if (!s->chr_can_read)
180 return 0;
181 return s->chr_can_read(s->handler_opaque);
182}
183
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500184void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000185{
Stefan Weilac310732012-04-19 22:27:14 +0200186 if (s->chr_read) {
187 s->chr_read(s->handler_opaque, buf, len);
188 }
aliguori6f97dba2008-10-31 18:49:55 +0000189}
190
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500191int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100192{
193 return s->get_msgfd ? s->get_msgfd(s) : -1;
194}
195
Daniel P. Berrange13661082011-06-23 13:31:42 +0100196int qemu_chr_add_client(CharDriverState *s, int fd)
197{
198 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
199}
200
aliguori6f97dba2008-10-31 18:49:55 +0000201void qemu_chr_accept_input(CharDriverState *s)
202{
203 if (s->chr_accept_input)
204 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100205 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000206}
207
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500208void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000209{
Amit Shah9bd78542009-11-03 19:59:54 +0530210 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000211 va_list ap;
212 va_start(ap, fmt);
213 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500214 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000215 va_end(ap);
216}
217
aliguori6f97dba2008-10-31 18:49:55 +0000218void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100219 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000220 IOReadHandler *fd_read,
221 IOEventHandler *fd_event,
222 void *opaque)
223{
Hans de Goede19083222013-03-26 11:07:56 +0100224 int fe_open;
225
Amit Shahda7d9982011-04-25 15:18:22 +0530226 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530227 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900228 ++s->avail_connections;
Hans de Goede19083222013-03-26 11:07:56 +0100229 fe_open = 0;
230 } else {
231 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530232 }
aliguori6f97dba2008-10-31 18:49:55 +0000233 s->chr_can_read = fd_can_read;
234 s->chr_read = fd_read;
235 s->chr_event = fd_event;
236 s->handler_opaque = opaque;
237 if (s->chr_update_read_handler)
238 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200239
Hans de Goede19083222013-03-26 11:07:56 +0100240 if (!s->explicit_fe_open) {
241 if (fe_open) {
242 qemu_chr_fe_open(s);
243 } else {
244 qemu_chr_fe_close(s);
245 }
246 }
247
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200248 /* We're connecting to an already opened device, so let's make sure we
249 also get the open event */
Hans de Goede16665b92013-03-26 11:07:53 +0100250 if (s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100251 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200252 }
aliguori6f97dba2008-10-31 18:49:55 +0000253}
254
255static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
256{
257 return len;
258}
259
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100260static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000261{
262 CharDriverState *chr;
263
Anthony Liguori7267c092011-08-20 22:09:37 -0500264 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000265 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100266 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000267}
268
269/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000270#define MAX_MUX 4
271#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
272#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
273typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100274 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000275 IOReadHandler *chr_read[MAX_MUX];
276 IOEventHandler *chr_event[MAX_MUX];
277 void *ext_opaque[MAX_MUX];
278 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200279 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000280 int mux_cnt;
281 int term_got_escape;
282 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000283 /* Intermediate input buffer allows to catch escape sequences even if the
284 currently active device is not accepting any input - but only until it
285 is full as well. */
286 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
287 int prod[MAX_MUX];
288 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200289 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200290 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200291 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000292} MuxDriver;
293
294
295static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
296{
297 MuxDriver *d = chr->opaque;
298 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200299 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000300 ret = d->drv->chr_write(d->drv, buf, len);
301 } else {
302 int i;
303
304 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200305 for (i = 0; i < len; i++) {
306 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000307 char buf1[64];
308 int64_t ti;
309 int secs;
310
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100311 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200312 if (d->timestamps_start == -1)
313 d->timestamps_start = ti;
314 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000315 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000316 snprintf(buf1, sizeof(buf1),
317 "[%02d:%02d:%02d.%03d] ",
318 secs / 3600,
319 (secs / 60) % 60,
320 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000321 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000322 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200323 d->linestart = 0;
324 }
325 ret += d->drv->chr_write(d->drv, buf+i, 1);
326 if (buf[i] == '\n') {
327 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000328 }
329 }
330 }
331 return ret;
332}
333
334static const char * const mux_help[] = {
335 "% h print this help\n\r",
336 "% x exit emulator\n\r",
337 "% s save disk data back to file (if -snapshot)\n\r",
338 "% t toggle console timestamps\n\r"
339 "% b send break (magic sysrq)\n\r",
340 "% c switch between console and monitor\n\r",
341 "% % sends %\n\r",
342 NULL
343};
344
345int term_escape_char = 0x01; /* ctrl-a is used for escape */
346static void mux_print_help(CharDriverState *chr)
347{
348 int i, j;
349 char ebuf[15] = "Escape-Char";
350 char cbuf[50] = "\n\r";
351
352 if (term_escape_char > 0 && term_escape_char < 26) {
353 snprintf(cbuf, sizeof(cbuf), "\n\r");
354 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
355 } else {
356 snprintf(cbuf, sizeof(cbuf),
357 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
358 term_escape_char);
359 }
360 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
361 for (i = 0; mux_help[i] != NULL; i++) {
362 for (j=0; mux_help[i][j] != '\0'; j++) {
363 if (mux_help[i][j] == '%')
364 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
365 else
366 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
367 }
368 }
369}
370
aliguori2724b182009-03-05 23:01:47 +0000371static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
372{
373 if (d->chr_event[mux_nr])
374 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
375}
376
aliguori6f97dba2008-10-31 18:49:55 +0000377static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
378{
379 if (d->term_got_escape) {
380 d->term_got_escape = 0;
381 if (ch == term_escape_char)
382 goto send_char;
383 switch(ch) {
384 case '?':
385 case 'h':
386 mux_print_help(chr);
387 break;
388 case 'x':
389 {
390 const char *term = "QEMU: Terminated\n\r";
391 chr->chr_write(chr,(uint8_t *)term,strlen(term));
392 exit(0);
393 break;
394 }
395 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200396 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000397 break;
398 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100399 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000400 break;
401 case 'c':
402 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200403 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
404 d->focus++;
405 if (d->focus >= d->mux_cnt)
406 d->focus = 0;
407 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000408 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200409 case 't':
410 d->timestamps = !d->timestamps;
411 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200412 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200413 break;
aliguori6f97dba2008-10-31 18:49:55 +0000414 }
415 } else if (ch == term_escape_char) {
416 d->term_got_escape = 1;
417 } else {
418 send_char:
419 return 1;
420 }
421 return 0;
422}
423
424static void mux_chr_accept_input(CharDriverState *chr)
425{
aliguori6f97dba2008-10-31 18:49:55 +0000426 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200427 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000428
aliguoria80bf992009-03-05 23:00:02 +0000429 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000430 d->chr_can_read[m] &&
431 d->chr_can_read[m](d->ext_opaque[m])) {
432 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000433 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000434 }
435}
436
437static int mux_chr_can_read(void *opaque)
438{
439 CharDriverState *chr = opaque;
440 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200441 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000442
aliguoria80bf992009-03-05 23:00:02 +0000443 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000444 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000445 if (d->chr_can_read[m])
446 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000447 return 0;
448}
449
450static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
451{
452 CharDriverState *chr = opaque;
453 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200454 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000455 int i;
456
457 mux_chr_accept_input (opaque);
458
459 for(i = 0; i < size; i++)
460 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000461 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000462 d->chr_can_read[m] &&
463 d->chr_can_read[m](d->ext_opaque[m]))
464 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
465 else
aliguoria80bf992009-03-05 23:00:02 +0000466 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000467 }
468}
469
470static void mux_chr_event(void *opaque, int event)
471{
472 CharDriverState *chr = opaque;
473 MuxDriver *d = chr->opaque;
474 int i;
475
476 /* Send the event to all registered listeners */
477 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000478 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000479}
480
481static void mux_chr_update_read_handler(CharDriverState *chr)
482{
483 MuxDriver *d = chr->opaque;
484
485 if (d->mux_cnt >= MAX_MUX) {
486 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
487 return;
488 }
489 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
490 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
491 d->chr_read[d->mux_cnt] = chr->chr_read;
492 d->chr_event[d->mux_cnt] = chr->chr_event;
493 /* Fix up the real driver with mux routines */
494 if (d->mux_cnt == 0) {
495 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
496 mux_chr_event, chr);
497 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200498 if (d->focus != -1) {
499 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200500 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200501 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000502 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200503 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000504}
505
506static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
507{
508 CharDriverState *chr;
509 MuxDriver *d;
510
Anthony Liguori7267c092011-08-20 22:09:37 -0500511 chr = g_malloc0(sizeof(CharDriverState));
512 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000513
514 chr->opaque = d;
515 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200516 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000517 chr->chr_write = mux_chr_write;
518 chr->chr_update_read_handler = mux_chr_update_read_handler;
519 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100520 /* Frontend guest-open / -close notification is not support with muxes */
521 chr->chr_guest_open = NULL;
522 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200523
524 /* Muxes are always open on creation */
Hans de Goedefee204f2013-03-26 11:07:54 +0100525 qemu_chr_be_generic_open(chr);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200526
aliguori6f97dba2008-10-31 18:49:55 +0000527 return chr;
528}
529
530
531#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000532int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000533{
534 int ret, len;
535
536 len = len1;
537 while (len > 0) {
538 ret = send(fd, buf, len, 0);
539 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000540 errno = WSAGetLastError();
541 if (errno != WSAEWOULDBLOCK) {
542 return -1;
543 }
544 } else if (ret == 0) {
545 break;
546 } else {
547 buf += ret;
548 len -= ret;
549 }
550 }
551 return len1 - len;
552}
553
554#else
555
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100556int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000557{
558 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100559 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000560
561 len = len1;
562 while (len > 0) {
563 ret = write(fd, buf, len);
564 if (ret < 0) {
565 if (errno != EINTR && errno != EAGAIN)
566 return -1;
567 } else if (ret == 0) {
568 break;
569 } else {
570 buf += ret;
571 len -= ret;
572 }
573 }
574 return len1 - len;
575}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500576
577int recv_all(int fd, void *_buf, int len1, bool single_read)
578{
579 int ret, len;
580 uint8_t *buf = _buf;
581
582 len = len1;
583 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
584 if (ret < 0) {
585 if (errno != EINTR && errno != EAGAIN) {
586 return -1;
587 }
588 continue;
589 } else {
590 if (single_read) {
591 return ret;
592 }
593 buf += ret;
594 len -= ret;
595 }
596 }
597 return len1 - len;
598}
599
aliguori6f97dba2008-10-31 18:49:55 +0000600#endif /* !_WIN32 */
601
Anthony Liguori96c63842013-03-05 23:21:18 +0530602typedef struct IOWatchPoll
603{
604 GSource *src;
605 int max_size;
606
607 IOCanReadHandler *fd_can_read;
608 void *opaque;
609
610 QTAILQ_ENTRY(IOWatchPoll) node;
611} IOWatchPoll;
612
613static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
614 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);
615
616static IOWatchPoll *io_watch_poll_from_source(GSource *source)
617{
618 IOWatchPoll *i;
619
620 QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
621 if (i->src == source) {
622 return i;
623 }
624 }
625
626 return NULL;
627}
628
629static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
630{
631 IOWatchPoll *iwp = io_watch_poll_from_source(source);
632
633 iwp->max_size = iwp->fd_can_read(iwp->opaque);
634 if (iwp->max_size == 0) {
635 return FALSE;
636 }
637
638 return g_io_watch_funcs.prepare(source, timeout_);
639}
640
641static gboolean io_watch_poll_check(GSource *source)
642{
643 IOWatchPoll *iwp = io_watch_poll_from_source(source);
644
645 if (iwp->max_size == 0) {
646 return FALSE;
647 }
648
649 return g_io_watch_funcs.check(source);
650}
651
652static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
653 gpointer user_data)
654{
655 return g_io_watch_funcs.dispatch(source, callback, user_data);
656}
657
658static void io_watch_poll_finalize(GSource *source)
659{
660 IOWatchPoll *iwp = io_watch_poll_from_source(source);
661 QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
662 g_io_watch_funcs.finalize(source);
663}
664
665static GSourceFuncs io_watch_poll_funcs = {
666 .prepare = io_watch_poll_prepare,
667 .check = io_watch_poll_check,
668 .dispatch = io_watch_poll_dispatch,
669 .finalize = io_watch_poll_finalize,
670};
671
672/* Can only be used for read */
673static guint io_add_watch_poll(GIOChannel *channel,
674 IOCanReadHandler *fd_can_read,
675 GIOFunc fd_read,
676 gpointer user_data)
677{
678 IOWatchPoll *iwp;
679 GSource *src;
680 guint tag;
681
682 src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
683 g_source_set_funcs(src, &io_watch_poll_funcs);
684 g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
685 tag = g_source_attach(src, NULL);
686 g_source_unref(src);
687
688 iwp = g_malloc0(sizeof(*iwp));
689 iwp->src = src;
690 iwp->max_size = 0;
691 iwp->fd_can_read = fd_can_read;
692 iwp->opaque = user_data;
693
694 QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);
695
696 return tag;
697}
698
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000699#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530700static GIOChannel *io_channel_from_fd(int fd)
701{
702 GIOChannel *chan;
703
704 if (fd == -1) {
705 return NULL;
706 }
707
708 chan = g_io_channel_unix_new(fd);
709
710 g_io_channel_set_encoding(chan, NULL, NULL);
711 g_io_channel_set_buffered(chan, FALSE);
712
713 return chan;
714}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000715#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530716
Anthony Liguori76a96442013-03-05 23:21:21 +0530717static GIOChannel *io_channel_from_socket(int fd)
718{
719 GIOChannel *chan;
720
721 if (fd == -1) {
722 return NULL;
723 }
724
725#ifdef _WIN32
726 chan = g_io_channel_win32_new_socket(fd);
727#else
728 chan = g_io_channel_unix_new(fd);
729#endif
730
731 g_io_channel_set_encoding(chan, NULL, NULL);
732 g_io_channel_set_buffered(chan, FALSE);
733
734 return chan;
735}
736
Anthony Liguori96c63842013-03-05 23:21:18 +0530737static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
738{
739 GIOStatus status;
740 gsize bytes_written;
741 int len;
742 const uint8_t *buf = _buf;
743
744 len = len1;
745 while (len > 0) {
746 status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
747 &bytes_written, NULL);
748 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530749 if (status == G_IO_STATUS_AGAIN) {
750 errno = EAGAIN;
751 return -1;
752 } else {
753 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530754 return -1;
755 }
756 } else if (status == G_IO_STATUS_EOF) {
757 break;
758 } else {
759 buf += bytes_written;
760 len -= bytes_written;
761 }
762 }
763 return len1 - len;
764}
Anthony Liguori96c63842013-03-05 23:21:18 +0530765
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000766#ifndef _WIN32
767
Anthony Liguoria29753f2013-03-05 23:21:19 +0530768typedef struct FDCharDriver {
769 CharDriverState *chr;
770 GIOChannel *fd_in, *fd_out;
771 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000772 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530773 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000774} FDCharDriver;
775
aliguori6f97dba2008-10-31 18:49:55 +0000776static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
777{
778 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530779
780 return io_channel_send_all(s->fd_out, buf, len);
781}
782
783static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
784{
785 CharDriverState *chr = opaque;
786 FDCharDriver *s = chr->opaque;
787 int len;
788 uint8_t buf[READ_BUF_LEN];
789 GIOStatus status;
790 gsize bytes_read;
791
792 len = sizeof(buf);
793 if (len > s->max_size) {
794 len = s->max_size;
795 }
796 if (len == 0) {
797 return FALSE;
798 }
799
800 status = g_io_channel_read_chars(chan, (gchar *)buf,
801 len, &bytes_read, NULL);
802 if (status == G_IO_STATUS_EOF) {
803 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
804 return FALSE;
805 }
806 if (status == G_IO_STATUS_NORMAL) {
807 qemu_chr_be_write(chr, buf, bytes_read);
808 }
809
810 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000811}
812
813static int fd_chr_read_poll(void *opaque)
814{
815 CharDriverState *chr = opaque;
816 FDCharDriver *s = chr->opaque;
817
Anthony Liguori909cda12011-08-15 11:17:31 -0500818 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000819 return s->max_size;
820}
821
Anthony Liguori23673ca2013-03-05 23:21:23 +0530822static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
823{
824 FDCharDriver *s = chr->opaque;
825 return g_io_create_watch(s->fd_out, cond);
826}
827
aliguori6f97dba2008-10-31 18:49:55 +0000828static void fd_chr_update_read_handler(CharDriverState *chr)
829{
830 FDCharDriver *s = chr->opaque;
831
Anthony Liguoria29753f2013-03-05 23:21:19 +0530832 if (s->fd_in_tag) {
833 g_source_remove(s->fd_in_tag);
834 }
835
836 if (s->fd_in) {
837 s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000838 }
839}
840
841static void fd_chr_close(struct CharDriverState *chr)
842{
843 FDCharDriver *s = chr->opaque;
844
Anthony Liguoria29753f2013-03-05 23:21:19 +0530845 if (s->fd_in_tag) {
846 g_source_remove(s->fd_in_tag);
847 s->fd_in_tag = 0;
848 }
849
850 if (s->fd_in) {
851 g_io_channel_unref(s->fd_in);
852 }
853 if (s->fd_out) {
854 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000855 }
856
Anthony Liguori7267c092011-08-20 22:09:37 -0500857 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100858 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000859}
860
861/* open a character device to a unix fd */
862static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
863{
864 CharDriverState *chr;
865 FDCharDriver *s;
866
Anthony Liguori7267c092011-08-20 22:09:37 -0500867 chr = g_malloc0(sizeof(CharDriverState));
868 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530869 s->fd_in = io_channel_from_fd(fd_in);
870 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530871 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530872 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000873 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530874 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000875 chr->chr_write = fd_chr_write;
876 chr->chr_update_read_handler = fd_chr_update_read_handler;
877 chr->chr_close = fd_chr_close;
878
Hans de Goedefee204f2013-03-26 11:07:54 +0100879 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000880
881 return chr;
882}
883
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100884static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000885{
886 int fd_in, fd_out;
887 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100888 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200889
890 if (filename == NULL) {
891 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100892 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200893 }
aliguori6f97dba2008-10-31 18:49:55 +0000894
895 snprintf(filename_in, 256, "%s.in", filename);
896 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100897 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
898 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000899 if (fd_in < 0 || fd_out < 0) {
900 if (fd_in >= 0)
901 close(fd_in);
902 if (fd_out >= 0)
903 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100904 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100905 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100906 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100907 }
aliguori6f97dba2008-10-31 18:49:55 +0000908 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100909 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000910}
911
aliguori6f97dba2008-10-31 18:49:55 +0000912/* init terminal so that we can grab keys */
913static struct termios oldtty;
914static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100915static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000916
917static void term_exit(void)
918{
919 tcsetattr (0, TCSANOW, &oldtty);
920 fcntl(0, F_SETFL, old_fd0_flags);
921}
922
Paolo Bonzinibb002512010-12-23 13:42:50 +0100923static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000924{
925 struct termios tty;
926
Paolo Bonzini03693642010-12-23 13:42:49 +0100927 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100928 if (!echo) {
929 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000930 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100931 tty.c_oflag |= OPOST;
932 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
933 tty.c_cflag &= ~(CSIZE|PARENB);
934 tty.c_cflag |= CS8;
935 tty.c_cc[VMIN] = 1;
936 tty.c_cc[VTIME] = 0;
937 }
aliguori6f97dba2008-10-31 18:49:55 +0000938 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100939 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000940 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000941
942 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000943}
944
945static void qemu_chr_close_stdio(struct CharDriverState *chr)
946{
947 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000948 fd_chr_close(chr);
949}
950
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100951static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000952{
953 CharDriverState *chr;
954
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400955 if (is_daemonized()) {
956 error_report("cannot use stdio with -daemonize");
957 return NULL;
958 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530959 old_fd0_flags = fcntl(0, F_GETFL);
960 tcgetattr (0, &oldtty);
961 fcntl(0, F_SETFL, O_NONBLOCK);
962 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100963
aliguori6f97dba2008-10-31 18:49:55 +0000964 chr = qemu_chr_open_fd(0, 1);
965 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100966 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100967 stdio_allow_signal = display_type != DT_NOGRAPHIC;
968 if (opts->has_signal) {
969 stdio_allow_signal = opts->signal;
970 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500971 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000972
Markus Armbruster1f514702012-02-07 15:09:08 +0100973 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000974}
975
976#ifdef __sun__
977/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000978static int openpty(int *amaster, int *aslave, char *name,
979 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000980{
981 const char *slave;
982 int mfd = -1, sfd = -1;
983
984 *amaster = *aslave = -1;
985
986 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
987 if (mfd < 0)
988 goto err;
989
990 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
991 goto err;
992
993 if ((slave = ptsname(mfd)) == NULL)
994 goto err;
995
996 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
997 goto err;
998
999 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
1000 (termp != NULL && tcgetattr(sfd, termp) < 0))
1001 goto err;
1002
1003 if (amaster)
1004 *amaster = mfd;
1005 if (aslave)
1006 *aslave = sfd;
1007 if (winp)
1008 ioctl(sfd, TIOCSWINSZ, winp);
1009
1010 return 0;
1011
1012err:
1013 if (sfd != -1)
1014 close(sfd);
1015 close(mfd);
1016 return -1;
1017}
1018
blueswir13f4cb3d2009-04-13 16:31:01 +00001019static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +00001020{
1021 termios_p->c_iflag &=
1022 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
1023 termios_p->c_oflag &= ~OPOST;
1024 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1025 termios_p->c_cflag &= ~(CSIZE|PARENB);
1026 termios_p->c_cflag |= CS8;
1027
1028 termios_p->c_cc[VMIN] = 0;
1029 termios_p->c_cc[VTIME] = 0;
1030}
1031#endif
1032
1033#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001034 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1035 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001036
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001037#define HAVE_CHARDEV_TTY 1
1038
aliguori6f97dba2008-10-31 18:49:55 +00001039typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301040 GIOChannel *fd;
1041 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001042 int connected;
1043 int polling;
1044 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301045 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001046} PtyCharDriver;
1047
1048static void pty_chr_update_read_handler(CharDriverState *chr);
1049static void pty_chr_state(CharDriverState *chr, int connected);
1050
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301051static gboolean pty_chr_timer(gpointer opaque)
1052{
1053 struct CharDriverState *chr = opaque;
1054 PtyCharDriver *s = chr->opaque;
1055
1056 if (s->connected) {
1057 goto out;
1058 }
1059 if (s->polling) {
1060 /* If we arrive here without polling being cleared due
1061 * read returning -EIO, then we are (re-)connected */
1062 pty_chr_state(chr, 1);
1063 goto out;
1064 }
1065
1066 /* Next poll ... */
1067 pty_chr_update_read_handler(chr);
1068
1069out:
1070 return FALSE;
1071}
1072
1073static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1074{
1075 PtyCharDriver *s = chr->opaque;
1076
1077 if (s->timer_tag) {
1078 g_source_remove(s->timer_tag);
1079 s->timer_tag = 0;
1080 }
1081
1082 if (ms == 1000) {
1083 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1084 } else {
1085 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1086 }
1087}
1088
aliguori6f97dba2008-10-31 18:49:55 +00001089static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1090{
1091 PtyCharDriver *s = chr->opaque;
1092
1093 if (!s->connected) {
1094 /* guest sends data, check for (re-)connect */
1095 pty_chr_update_read_handler(chr);
1096 return 0;
1097 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301098 return io_channel_send_all(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001099}
1100
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301101static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1102{
1103 PtyCharDriver *s = chr->opaque;
1104 return g_io_create_watch(s->fd, cond);
1105}
1106
aliguori6f97dba2008-10-31 18:49:55 +00001107static int pty_chr_read_poll(void *opaque)
1108{
1109 CharDriverState *chr = opaque;
1110 PtyCharDriver *s = chr->opaque;
1111
Anthony Liguori909cda12011-08-15 11:17:31 -05001112 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001113 return s->read_bytes;
1114}
1115
Anthony Liguori093d3a22013-03-05 23:21:20 +05301116static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001117{
1118 CharDriverState *chr = opaque;
1119 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301120 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301121 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301122 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001123
1124 len = sizeof(buf);
1125 if (len > s->read_bytes)
1126 len = s->read_bytes;
1127 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301128 return FALSE;
1129 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1130 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001131 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301132 return FALSE;
1133 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001134 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001135 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001136 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301137 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001138}
1139
1140static void pty_chr_update_read_handler(CharDriverState *chr)
1141{
1142 PtyCharDriver *s = chr->opaque;
1143
Anthony Liguori093d3a22013-03-05 23:21:20 +05301144 if (s->fd_tag) {
1145 g_source_remove(s->fd_tag);
1146 }
1147
1148 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001149 s->polling = 1;
1150 /*
1151 * Short timeout here: just need wait long enougth that qemu makes
1152 * it through the poll loop once. When reconnected we want a
1153 * short timeout so we notice it almost instantly. Otherwise
1154 * read() gives us -EIO instantly, making pty_chr_state() reset the
1155 * timeout to the normal (much longer) poll interval before the
1156 * timer triggers.
1157 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301158 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001159}
1160
1161static void pty_chr_state(CharDriverState *chr, int connected)
1162{
1163 PtyCharDriver *s = chr->opaque;
1164
1165 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301166 g_source_remove(s->fd_tag);
1167 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001168 s->connected = 0;
1169 s->polling = 0;
1170 /* (re-)connect poll interval for idle guests: once per second.
1171 * We check more frequently in case the guests sends data to
1172 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301173 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001174 } else {
1175 if (!s->connected)
Hans de Goedefee204f2013-03-26 11:07:54 +01001176 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001177 s->connected = 1;
1178 }
1179}
1180
aliguori6f97dba2008-10-31 18:49:55 +00001181
1182static void pty_chr_close(struct CharDriverState *chr)
1183{
1184 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301185 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001186
Anthony Liguori093d3a22013-03-05 23:21:20 +05301187 if (s->fd_tag) {
1188 g_source_remove(s->fd_tag);
1189 }
1190 fd = g_io_channel_unix_get_fd(s->fd);
1191 g_io_channel_unref(s->fd);
1192 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301193 if (s->timer_tag) {
1194 g_source_remove(s->timer_tag);
1195 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001196 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001197 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001198}
1199
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001200static CharDriverState *qemu_chr_open_pty(const char *id,
1201 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001202{
1203 CharDriverState *chr;
1204 PtyCharDriver *s;
1205 struct termios tty;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001206 int master_fd, slave_fd;
blueswir1c5e97232009-03-07 20:06:23 +00001207#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001208 char pty_name[PATH_MAX];
1209#define q_ptsname(x) pty_name
1210#else
1211 char *pty_name = NULL;
1212#define q_ptsname(x) ptsname(x)
1213#endif
1214
Markus Armbrustera4e26042011-11-11 10:40:05 +01001215 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001216 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001217 }
1218
1219 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001220 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001221 cfmakeraw(&tty);
1222 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1223 close(slave_fd);
1224
Markus Armbrustera4e26042011-11-11 10:40:05 +01001225 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001226
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001227 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1228 ret->pty = g_strdup(q_ptsname(master_fd));
1229 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001230
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001231 fprintf(stderr, "char device redirected to %s (label %s)\n",
1232 q_ptsname(master_fd), id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001233
1234 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001235 chr->opaque = s;
1236 chr->chr_write = pty_chr_write;
1237 chr->chr_update_read_handler = pty_chr_update_read_handler;
1238 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301239 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001240
Anthony Liguori093d3a22013-03-05 23:21:20 +05301241 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301242 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001243
Markus Armbruster1f514702012-02-07 15:09:08 +01001244 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001245}
1246
1247static void tty_serial_init(int fd, int speed,
1248 int parity, int data_bits, int stop_bits)
1249{
1250 struct termios tty;
1251 speed_t spd;
1252
1253#if 0
1254 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1255 speed, parity, data_bits, stop_bits);
1256#endif
1257 tcgetattr (fd, &tty);
1258
Stefan Weil45eea132009-10-26 16:10:10 +01001259#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1260 speed = speed * 10 / 11;
1261 do {
1262 check_speed(50);
1263 check_speed(75);
1264 check_speed(110);
1265 check_speed(134);
1266 check_speed(150);
1267 check_speed(200);
1268 check_speed(300);
1269 check_speed(600);
1270 check_speed(1200);
1271 check_speed(1800);
1272 check_speed(2400);
1273 check_speed(4800);
1274 check_speed(9600);
1275 check_speed(19200);
1276 check_speed(38400);
1277 /* Non-Posix values follow. They may be unsupported on some systems. */
1278 check_speed(57600);
1279 check_speed(115200);
1280#ifdef B230400
1281 check_speed(230400);
1282#endif
1283#ifdef B460800
1284 check_speed(460800);
1285#endif
1286#ifdef B500000
1287 check_speed(500000);
1288#endif
1289#ifdef B576000
1290 check_speed(576000);
1291#endif
1292#ifdef B921600
1293 check_speed(921600);
1294#endif
1295#ifdef B1000000
1296 check_speed(1000000);
1297#endif
1298#ifdef B1152000
1299 check_speed(1152000);
1300#endif
1301#ifdef B1500000
1302 check_speed(1500000);
1303#endif
1304#ifdef B2000000
1305 check_speed(2000000);
1306#endif
1307#ifdef B2500000
1308 check_speed(2500000);
1309#endif
1310#ifdef B3000000
1311 check_speed(3000000);
1312#endif
1313#ifdef B3500000
1314 check_speed(3500000);
1315#endif
1316#ifdef B4000000
1317 check_speed(4000000);
1318#endif
aliguori6f97dba2008-10-31 18:49:55 +00001319 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001320 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001321
1322 cfsetispeed(&tty, spd);
1323 cfsetospeed(&tty, spd);
1324
1325 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1326 |INLCR|IGNCR|ICRNL|IXON);
1327 tty.c_oflag |= OPOST;
1328 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1329 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1330 switch(data_bits) {
1331 default:
1332 case 8:
1333 tty.c_cflag |= CS8;
1334 break;
1335 case 7:
1336 tty.c_cflag |= CS7;
1337 break;
1338 case 6:
1339 tty.c_cflag |= CS6;
1340 break;
1341 case 5:
1342 tty.c_cflag |= CS5;
1343 break;
1344 }
1345 switch(parity) {
1346 default:
1347 case 'N':
1348 break;
1349 case 'E':
1350 tty.c_cflag |= PARENB;
1351 break;
1352 case 'O':
1353 tty.c_cflag |= PARENB | PARODD;
1354 break;
1355 }
1356 if (stop_bits == 2)
1357 tty.c_cflag |= CSTOPB;
1358
1359 tcsetattr (fd, TCSANOW, &tty);
1360}
1361
1362static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1363{
1364 FDCharDriver *s = chr->opaque;
1365
1366 switch(cmd) {
1367 case CHR_IOCTL_SERIAL_SET_PARAMS:
1368 {
1369 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301370 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1371 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001372 ssp->data_bits, ssp->stop_bits);
1373 }
1374 break;
1375 case CHR_IOCTL_SERIAL_SET_BREAK:
1376 {
1377 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301378 if (enable) {
1379 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1380 }
aliguori6f97dba2008-10-31 18:49:55 +00001381 }
1382 break;
1383 case CHR_IOCTL_SERIAL_GET_TIOCM:
1384 {
1385 int sarg = 0;
1386 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301387 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001388 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001389 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001390 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001391 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001392 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001393 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001394 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001395 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001396 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001397 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001398 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001399 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001400 *targ |= CHR_TIOCM_RTS;
1401 }
1402 break;
1403 case CHR_IOCTL_SERIAL_SET_TIOCM:
1404 {
1405 int sarg = *(int *)arg;
1406 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301407 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001408 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1409 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1410 if (sarg & CHR_TIOCM_CTS)
1411 targ |= TIOCM_CTS;
1412 if (sarg & CHR_TIOCM_CAR)
1413 targ |= TIOCM_CAR;
1414 if (sarg & CHR_TIOCM_DSR)
1415 targ |= TIOCM_DSR;
1416 if (sarg & CHR_TIOCM_RI)
1417 targ |= TIOCM_RI;
1418 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001419 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001420 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001421 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301422 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001423 }
1424 break;
1425 default:
1426 return -ENOTSUP;
1427 }
1428 return 0;
1429}
1430
David Ahern4266a132010-02-10 18:27:17 -07001431static void qemu_chr_close_tty(CharDriverState *chr)
1432{
1433 FDCharDriver *s = chr->opaque;
1434 int fd = -1;
1435
1436 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301437 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001438 }
1439
1440 fd_chr_close(chr);
1441
1442 if (fd >= 0) {
1443 close(fd);
1444 }
1445}
1446
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001447static CharDriverState *qemu_chr_open_tty_fd(int fd)
1448{
1449 CharDriverState *chr;
1450
1451 tty_serial_init(fd, 115200, 'N', 8, 1);
1452 chr = qemu_chr_open_fd(fd, fd);
1453 chr->chr_ioctl = tty_serial_ioctl;
1454 chr->chr_close = qemu_chr_close_tty;
1455 return chr;
1456}
aliguori6f97dba2008-10-31 18:49:55 +00001457#endif /* __linux__ || __sun__ */
1458
1459#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001460
1461#define HAVE_CHARDEV_PARPORT 1
1462
aliguori6f97dba2008-10-31 18:49:55 +00001463typedef struct {
1464 int fd;
1465 int mode;
1466} ParallelCharDriver;
1467
1468static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1469{
1470 if (s->mode != mode) {
1471 int m = mode;
1472 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1473 return 0;
1474 s->mode = mode;
1475 }
1476 return 1;
1477}
1478
1479static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1480{
1481 ParallelCharDriver *drv = chr->opaque;
1482 int fd = drv->fd;
1483 uint8_t b;
1484
1485 switch(cmd) {
1486 case CHR_IOCTL_PP_READ_DATA:
1487 if (ioctl(fd, PPRDATA, &b) < 0)
1488 return -ENOTSUP;
1489 *(uint8_t *)arg = b;
1490 break;
1491 case CHR_IOCTL_PP_WRITE_DATA:
1492 b = *(uint8_t *)arg;
1493 if (ioctl(fd, PPWDATA, &b) < 0)
1494 return -ENOTSUP;
1495 break;
1496 case CHR_IOCTL_PP_READ_CONTROL:
1497 if (ioctl(fd, PPRCONTROL, &b) < 0)
1498 return -ENOTSUP;
1499 /* Linux gives only the lowest bits, and no way to know data
1500 direction! For better compatibility set the fixed upper
1501 bits. */
1502 *(uint8_t *)arg = b | 0xc0;
1503 break;
1504 case CHR_IOCTL_PP_WRITE_CONTROL:
1505 b = *(uint8_t *)arg;
1506 if (ioctl(fd, PPWCONTROL, &b) < 0)
1507 return -ENOTSUP;
1508 break;
1509 case CHR_IOCTL_PP_READ_STATUS:
1510 if (ioctl(fd, PPRSTATUS, &b) < 0)
1511 return -ENOTSUP;
1512 *(uint8_t *)arg = b;
1513 break;
1514 case CHR_IOCTL_PP_DATA_DIR:
1515 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1516 return -ENOTSUP;
1517 break;
1518 case CHR_IOCTL_PP_EPP_READ_ADDR:
1519 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1520 struct ParallelIOArg *parg = arg;
1521 int n = read(fd, parg->buffer, parg->count);
1522 if (n != parg->count) {
1523 return -EIO;
1524 }
1525 }
1526 break;
1527 case CHR_IOCTL_PP_EPP_READ:
1528 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1529 struct ParallelIOArg *parg = arg;
1530 int n = read(fd, parg->buffer, parg->count);
1531 if (n != parg->count) {
1532 return -EIO;
1533 }
1534 }
1535 break;
1536 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1537 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1538 struct ParallelIOArg *parg = arg;
1539 int n = write(fd, parg->buffer, parg->count);
1540 if (n != parg->count) {
1541 return -EIO;
1542 }
1543 }
1544 break;
1545 case CHR_IOCTL_PP_EPP_WRITE:
1546 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1547 struct ParallelIOArg *parg = arg;
1548 int n = write(fd, parg->buffer, parg->count);
1549 if (n != parg->count) {
1550 return -EIO;
1551 }
1552 }
1553 break;
1554 default:
1555 return -ENOTSUP;
1556 }
1557 return 0;
1558}
1559
1560static void pp_close(CharDriverState *chr)
1561{
1562 ParallelCharDriver *drv = chr->opaque;
1563 int fd = drv->fd;
1564
1565 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1566 ioctl(fd, PPRELEASE);
1567 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001568 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001569 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001570}
1571
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001572static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001573{
1574 CharDriverState *chr;
1575 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001576
1577 if (ioctl(fd, PPCLAIM) < 0) {
1578 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001579 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001580 }
1581
Anthony Liguori7267c092011-08-20 22:09:37 -05001582 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001583 drv->fd = fd;
1584 drv->mode = IEEE1284_MODE_COMPAT;
1585
Anthony Liguori7267c092011-08-20 22:09:37 -05001586 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001587 chr->chr_write = null_chr_write;
1588 chr->chr_ioctl = pp_ioctl;
1589 chr->chr_close = pp_close;
1590 chr->opaque = drv;
1591
Hans de Goedefee204f2013-03-26 11:07:54 +01001592 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001593
Markus Armbruster1f514702012-02-07 15:09:08 +01001594 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001595}
1596#endif /* __linux__ */
1597
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001598#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001599
1600#define HAVE_CHARDEV_PARPORT 1
1601
blueswir16972f932008-11-22 20:49:12 +00001602static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1603{
Stefan Weile0efb992011-02-23 19:09:16 +01001604 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001605 uint8_t b;
1606
1607 switch(cmd) {
1608 case CHR_IOCTL_PP_READ_DATA:
1609 if (ioctl(fd, PPIGDATA, &b) < 0)
1610 return -ENOTSUP;
1611 *(uint8_t *)arg = b;
1612 break;
1613 case CHR_IOCTL_PP_WRITE_DATA:
1614 b = *(uint8_t *)arg;
1615 if (ioctl(fd, PPISDATA, &b) < 0)
1616 return -ENOTSUP;
1617 break;
1618 case CHR_IOCTL_PP_READ_CONTROL:
1619 if (ioctl(fd, PPIGCTRL, &b) < 0)
1620 return -ENOTSUP;
1621 *(uint8_t *)arg = b;
1622 break;
1623 case CHR_IOCTL_PP_WRITE_CONTROL:
1624 b = *(uint8_t *)arg;
1625 if (ioctl(fd, PPISCTRL, &b) < 0)
1626 return -ENOTSUP;
1627 break;
1628 case CHR_IOCTL_PP_READ_STATUS:
1629 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1630 return -ENOTSUP;
1631 *(uint8_t *)arg = b;
1632 break;
1633 default:
1634 return -ENOTSUP;
1635 }
1636 return 0;
1637}
1638
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001639static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001640{
1641 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001642
Anthony Liguori7267c092011-08-20 22:09:37 -05001643 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001644 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001645 chr->chr_write = null_chr_write;
1646 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001647 return chr;
blueswir16972f932008-11-22 20:49:12 +00001648}
1649#endif
1650
aliguori6f97dba2008-10-31 18:49:55 +00001651#else /* _WIN32 */
1652
1653typedef struct {
1654 int max_size;
1655 HANDLE hcom, hrecv, hsend;
1656 OVERLAPPED orecv, osend;
1657 BOOL fpipe;
1658 DWORD len;
1659} WinCharState;
1660
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001661typedef struct {
1662 HANDLE hStdIn;
1663 HANDLE hInputReadyEvent;
1664 HANDLE hInputDoneEvent;
1665 HANDLE hInputThread;
1666 uint8_t win_stdio_buf;
1667} WinStdioCharState;
1668
aliguori6f97dba2008-10-31 18:49:55 +00001669#define NSENDBUF 2048
1670#define NRECVBUF 2048
1671#define MAXCONNECT 1
1672#define NTIMEOUT 5000
1673
1674static int win_chr_poll(void *opaque);
1675static int win_chr_pipe_poll(void *opaque);
1676
1677static void win_chr_close(CharDriverState *chr)
1678{
1679 WinCharState *s = chr->opaque;
1680
1681 if (s->hsend) {
1682 CloseHandle(s->hsend);
1683 s->hsend = NULL;
1684 }
1685 if (s->hrecv) {
1686 CloseHandle(s->hrecv);
1687 s->hrecv = NULL;
1688 }
1689 if (s->hcom) {
1690 CloseHandle(s->hcom);
1691 s->hcom = NULL;
1692 }
1693 if (s->fpipe)
1694 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1695 else
1696 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301697
Hans de Goedea425d232011-11-19 10:22:43 +01001698 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001699}
1700
1701static int win_chr_init(CharDriverState *chr, const char *filename)
1702{
1703 WinCharState *s = chr->opaque;
1704 COMMCONFIG comcfg;
1705 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1706 COMSTAT comstat;
1707 DWORD size;
1708 DWORD err;
1709
1710 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1711 if (!s->hsend) {
1712 fprintf(stderr, "Failed CreateEvent\n");
1713 goto fail;
1714 }
1715 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1716 if (!s->hrecv) {
1717 fprintf(stderr, "Failed CreateEvent\n");
1718 goto fail;
1719 }
1720
1721 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1722 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1723 if (s->hcom == INVALID_HANDLE_VALUE) {
1724 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1725 s->hcom = NULL;
1726 goto fail;
1727 }
1728
1729 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1730 fprintf(stderr, "Failed SetupComm\n");
1731 goto fail;
1732 }
1733
1734 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1735 size = sizeof(COMMCONFIG);
1736 GetDefaultCommConfig(filename, &comcfg, &size);
1737 comcfg.dcb.DCBlength = sizeof(DCB);
1738 CommConfigDialog(filename, NULL, &comcfg);
1739
1740 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1741 fprintf(stderr, "Failed SetCommState\n");
1742 goto fail;
1743 }
1744
1745 if (!SetCommMask(s->hcom, EV_ERR)) {
1746 fprintf(stderr, "Failed SetCommMask\n");
1747 goto fail;
1748 }
1749
1750 cto.ReadIntervalTimeout = MAXDWORD;
1751 if (!SetCommTimeouts(s->hcom, &cto)) {
1752 fprintf(stderr, "Failed SetCommTimeouts\n");
1753 goto fail;
1754 }
1755
1756 if (!ClearCommError(s->hcom, &err, &comstat)) {
1757 fprintf(stderr, "Failed ClearCommError\n");
1758 goto fail;
1759 }
1760 qemu_add_polling_cb(win_chr_poll, chr);
1761 return 0;
1762
1763 fail:
1764 win_chr_close(chr);
1765 return -1;
1766}
1767
1768static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1769{
1770 WinCharState *s = chr->opaque;
1771 DWORD len, ret, size, err;
1772
1773 len = len1;
1774 ZeroMemory(&s->osend, sizeof(s->osend));
1775 s->osend.hEvent = s->hsend;
1776 while (len > 0) {
1777 if (s->hsend)
1778 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1779 else
1780 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1781 if (!ret) {
1782 err = GetLastError();
1783 if (err == ERROR_IO_PENDING) {
1784 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1785 if (ret) {
1786 buf += size;
1787 len -= size;
1788 } else {
1789 break;
1790 }
1791 } else {
1792 break;
1793 }
1794 } else {
1795 buf += size;
1796 len -= size;
1797 }
1798 }
1799 return len1 - len;
1800}
1801
1802static int win_chr_read_poll(CharDriverState *chr)
1803{
1804 WinCharState *s = chr->opaque;
1805
Anthony Liguori909cda12011-08-15 11:17:31 -05001806 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001807 return s->max_size;
1808}
1809
1810static void win_chr_readfile(CharDriverState *chr)
1811{
1812 WinCharState *s = chr->opaque;
1813 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301814 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001815 DWORD size;
1816
1817 ZeroMemory(&s->orecv, sizeof(s->orecv));
1818 s->orecv.hEvent = s->hrecv;
1819 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1820 if (!ret) {
1821 err = GetLastError();
1822 if (err == ERROR_IO_PENDING) {
1823 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1824 }
1825 }
1826
1827 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001828 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001829 }
1830}
1831
1832static void win_chr_read(CharDriverState *chr)
1833{
1834 WinCharState *s = chr->opaque;
1835
1836 if (s->len > s->max_size)
1837 s->len = s->max_size;
1838 if (s->len == 0)
1839 return;
1840
1841 win_chr_readfile(chr);
1842}
1843
1844static int win_chr_poll(void *opaque)
1845{
1846 CharDriverState *chr = opaque;
1847 WinCharState *s = chr->opaque;
1848 COMSTAT status;
1849 DWORD comerr;
1850
1851 ClearCommError(s->hcom, &comerr, &status);
1852 if (status.cbInQue > 0) {
1853 s->len = status.cbInQue;
1854 win_chr_read_poll(chr);
1855 win_chr_read(chr);
1856 return 1;
1857 }
1858 return 0;
1859}
1860
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001861static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001862{
1863 CharDriverState *chr;
1864 WinCharState *s;
1865
Anthony Liguori7267c092011-08-20 22:09:37 -05001866 chr = g_malloc0(sizeof(CharDriverState));
1867 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001868 chr->opaque = s;
1869 chr->chr_write = win_chr_write;
1870 chr->chr_close = win_chr_close;
1871
1872 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001873 g_free(s);
1874 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001875 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001876 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001877 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001878 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001879}
1880
1881static int win_chr_pipe_poll(void *opaque)
1882{
1883 CharDriverState *chr = opaque;
1884 WinCharState *s = chr->opaque;
1885 DWORD size;
1886
1887 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1888 if (size > 0) {
1889 s->len = size;
1890 win_chr_read_poll(chr);
1891 win_chr_read(chr);
1892 return 1;
1893 }
1894 return 0;
1895}
1896
1897static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1898{
1899 WinCharState *s = chr->opaque;
1900 OVERLAPPED ov;
1901 int ret;
1902 DWORD size;
1903 char openname[256];
1904
1905 s->fpipe = TRUE;
1906
1907 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1908 if (!s->hsend) {
1909 fprintf(stderr, "Failed CreateEvent\n");
1910 goto fail;
1911 }
1912 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1913 if (!s->hrecv) {
1914 fprintf(stderr, "Failed CreateEvent\n");
1915 goto fail;
1916 }
1917
1918 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1919 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1920 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1921 PIPE_WAIT,
1922 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1923 if (s->hcom == INVALID_HANDLE_VALUE) {
1924 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1925 s->hcom = NULL;
1926 goto fail;
1927 }
1928
1929 ZeroMemory(&ov, sizeof(ov));
1930 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1931 ret = ConnectNamedPipe(s->hcom, &ov);
1932 if (ret) {
1933 fprintf(stderr, "Failed ConnectNamedPipe\n");
1934 goto fail;
1935 }
1936
1937 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1938 if (!ret) {
1939 fprintf(stderr, "Failed GetOverlappedResult\n");
1940 if (ov.hEvent) {
1941 CloseHandle(ov.hEvent);
1942 ov.hEvent = NULL;
1943 }
1944 goto fail;
1945 }
1946
1947 if (ov.hEvent) {
1948 CloseHandle(ov.hEvent);
1949 ov.hEvent = NULL;
1950 }
1951 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1952 return 0;
1953
1954 fail:
1955 win_chr_close(chr);
1956 return -1;
1957}
1958
1959
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001960static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001961{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001962 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001963 CharDriverState *chr;
1964 WinCharState *s;
1965
Anthony Liguori7267c092011-08-20 22:09:37 -05001966 chr = g_malloc0(sizeof(CharDriverState));
1967 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001968 chr->opaque = s;
1969 chr->chr_write = win_chr_write;
1970 chr->chr_close = win_chr_close;
1971
1972 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001973 g_free(s);
1974 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001975 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001976 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001977 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001978 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001979}
1980
Markus Armbruster1f514702012-02-07 15:09:08 +01001981static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001982{
1983 CharDriverState *chr;
1984 WinCharState *s;
1985
Anthony Liguori7267c092011-08-20 22:09:37 -05001986 chr = g_malloc0(sizeof(CharDriverState));
1987 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001988 s->hcom = fd_out;
1989 chr->opaque = s;
1990 chr->chr_write = win_chr_write;
Hans de Goedefee204f2013-03-26 11:07:54 +01001991 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001992 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001993}
1994
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001995static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001996{
Markus Armbruster1f514702012-02-07 15:09:08 +01001997 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001998}
1999
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002000static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2001{
2002 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2003 DWORD dwSize;
2004 int len1;
2005
2006 len1 = len;
2007
2008 while (len1 > 0) {
2009 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2010 break;
2011 }
2012 buf += dwSize;
2013 len1 -= dwSize;
2014 }
2015
2016 return len - len1;
2017}
2018
2019static void win_stdio_wait_func(void *opaque)
2020{
2021 CharDriverState *chr = opaque;
2022 WinStdioCharState *stdio = chr->opaque;
2023 INPUT_RECORD buf[4];
2024 int ret;
2025 DWORD dwSize;
2026 int i;
2027
2028 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2029 &dwSize);
2030
2031 if (!ret) {
2032 /* Avoid error storm */
2033 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2034 return;
2035 }
2036
2037 for (i = 0; i < dwSize; i++) {
2038 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2039
2040 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2041 int j;
2042 if (kev->uChar.AsciiChar != 0) {
2043 for (j = 0; j < kev->wRepeatCount; j++) {
2044 if (qemu_chr_be_can_write(chr)) {
2045 uint8_t c = kev->uChar.AsciiChar;
2046 qemu_chr_be_write(chr, &c, 1);
2047 }
2048 }
2049 }
2050 }
2051 }
2052}
2053
2054static DWORD WINAPI win_stdio_thread(LPVOID param)
2055{
2056 CharDriverState *chr = param;
2057 WinStdioCharState *stdio = chr->opaque;
2058 int ret;
2059 DWORD dwSize;
2060
2061 while (1) {
2062
2063 /* Wait for one byte */
2064 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2065
2066 /* Exit in case of error, continue if nothing read */
2067 if (!ret) {
2068 break;
2069 }
2070 if (!dwSize) {
2071 continue;
2072 }
2073
2074 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2075 if (stdio->win_stdio_buf == '\r') {
2076 continue;
2077 }
2078
2079 /* Signal the main thread and wait until the byte was eaten */
2080 if (!SetEvent(stdio->hInputReadyEvent)) {
2081 break;
2082 }
2083 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2084 != WAIT_OBJECT_0) {
2085 break;
2086 }
2087 }
2088
2089 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2090 return 0;
2091}
2092
2093static void win_stdio_thread_wait_func(void *opaque)
2094{
2095 CharDriverState *chr = opaque;
2096 WinStdioCharState *stdio = chr->opaque;
2097
2098 if (qemu_chr_be_can_write(chr)) {
2099 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2100 }
2101
2102 SetEvent(stdio->hInputDoneEvent);
2103}
2104
2105static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2106{
2107 WinStdioCharState *stdio = chr->opaque;
2108 DWORD dwMode = 0;
2109
2110 GetConsoleMode(stdio->hStdIn, &dwMode);
2111
2112 if (echo) {
2113 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2114 } else {
2115 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2116 }
2117}
2118
2119static void win_stdio_close(CharDriverState *chr)
2120{
2121 WinStdioCharState *stdio = chr->opaque;
2122
2123 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2124 CloseHandle(stdio->hInputReadyEvent);
2125 }
2126 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2127 CloseHandle(stdio->hInputDoneEvent);
2128 }
2129 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2130 TerminateThread(stdio->hInputThread, 0);
2131 }
2132
2133 g_free(chr->opaque);
2134 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002135}
2136
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002137static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002138{
2139 CharDriverState *chr;
2140 WinStdioCharState *stdio;
2141 DWORD dwMode;
2142 int is_console = 0;
2143
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002144 chr = g_malloc0(sizeof(CharDriverState));
2145 stdio = g_malloc0(sizeof(WinStdioCharState));
2146
2147 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2148 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2149 fprintf(stderr, "cannot open stdio: invalid handle\n");
2150 exit(1);
2151 }
2152
2153 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2154
2155 chr->opaque = stdio;
2156 chr->chr_write = win_stdio_write;
2157 chr->chr_close = win_stdio_close;
2158
Anthony Liguoried7a1542013-03-05 23:21:17 +05302159 if (is_console) {
2160 if (qemu_add_wait_object(stdio->hStdIn,
2161 win_stdio_wait_func, chr)) {
2162 fprintf(stderr, "qemu_add_wait_object: failed\n");
2163 }
2164 } else {
2165 DWORD dwId;
2166
2167 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2168 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2169 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2170 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002171
Anthony Liguoried7a1542013-03-05 23:21:17 +05302172 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2173 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2174 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2175 fprintf(stderr, "cannot create stdio thread or event\n");
2176 exit(1);
2177 }
2178 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2179 win_stdio_thread_wait_func, chr)) {
2180 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002181 }
2182 }
2183
2184 dwMode |= ENABLE_LINE_INPUT;
2185
Anthony Liguoried7a1542013-03-05 23:21:17 +05302186 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002187 /* set the terminal in raw mode */
2188 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2189 dwMode |= ENABLE_PROCESSED_INPUT;
2190 }
2191
2192 SetConsoleMode(stdio->hStdIn, dwMode);
2193
2194 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2195 qemu_chr_fe_set_echo(chr, false);
2196
Markus Armbruster1f514702012-02-07 15:09:08 +01002197 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002198}
aliguori6f97dba2008-10-31 18:49:55 +00002199#endif /* !_WIN32 */
2200
Anthony Liguori5ab82112013-03-05 23:21:31 +05302201
aliguori6f97dba2008-10-31 18:49:55 +00002202/***********************************************************/
2203/* UDP Net console */
2204
2205typedef struct {
2206 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302207 GIOChannel *chan;
2208 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302209 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002210 int bufcnt;
2211 int bufptr;
2212 int max_size;
2213} NetCharDriver;
2214
2215static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2216{
2217 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302218 gsize bytes_written;
2219 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002220
Anthony Liguori76a96442013-03-05 23:21:21 +05302221 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2222 if (status == G_IO_STATUS_EOF) {
2223 return 0;
2224 } else if (status != G_IO_STATUS_NORMAL) {
2225 return -1;
2226 }
2227
2228 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002229}
2230
2231static int udp_chr_read_poll(void *opaque)
2232{
2233 CharDriverState *chr = opaque;
2234 NetCharDriver *s = chr->opaque;
2235
Anthony Liguori909cda12011-08-15 11:17:31 -05002236 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002237
2238 /* If there were any stray characters in the queue process them
2239 * first
2240 */
2241 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002242 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002243 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002244 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002245 }
2246 return s->max_size;
2247}
2248
Anthony Liguori76a96442013-03-05 23:21:21 +05302249static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002250{
2251 CharDriverState *chr = opaque;
2252 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302253 gsize bytes_read = 0;
2254 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002255
2256 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302257 return FALSE;
2258 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2259 &bytes_read, NULL);
2260 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002261 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302262 if (status != G_IO_STATUS_NORMAL) {
2263 return FALSE;
2264 }
aliguori6f97dba2008-10-31 18:49:55 +00002265
2266 s->bufptr = 0;
2267 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002268 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002269 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002270 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002271 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302272
2273 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002274}
2275
2276static void udp_chr_update_read_handler(CharDriverState *chr)
2277{
2278 NetCharDriver *s = chr->opaque;
2279
Anthony Liguori76a96442013-03-05 23:21:21 +05302280 if (s->tag) {
2281 g_source_remove(s->tag);
2282 s->tag = 0;
2283 }
2284
2285 if (s->chan) {
2286 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002287 }
2288}
2289
aliguori819f56b2009-03-28 17:58:14 +00002290static void udp_chr_close(CharDriverState *chr)
2291{
2292 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302293 if (s->tag) {
2294 g_source_remove(s->tag);
2295 }
2296 if (s->chan) {
2297 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002298 closesocket(s->fd);
2299 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002300 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002301 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002302}
2303
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002304static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002305{
2306 CharDriverState *chr = NULL;
2307 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002308
Anthony Liguori7267c092011-08-20 22:09:37 -05002309 chr = g_malloc0(sizeof(CharDriverState));
2310 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002311
aliguori6f97dba2008-10-31 18:49:55 +00002312 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302313 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002314 s->bufcnt = 0;
2315 s->bufptr = 0;
2316 chr->opaque = s;
2317 chr->chr_write = udp_chr_write;
2318 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002319 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002320 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002321}
aliguori6f97dba2008-10-31 18:49:55 +00002322
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002323static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2324{
2325 Error *local_err = NULL;
2326 int fd = -1;
2327
2328 fd = inet_dgram_opts(opts, &local_err);
2329 if (fd < 0) {
2330 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002331 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002332 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002333}
2334
2335/***********************************************************/
2336/* TCP Net console */
2337
2338typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302339
2340 GIOChannel *chan, *listen_chan;
2341 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002342 int fd, listen_fd;
2343 int connected;
2344 int max_size;
2345 int do_telnetopt;
2346 int do_nodelay;
2347 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002348 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002349} TCPCharDriver;
2350
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302351static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002352
2353static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2354{
2355 TCPCharDriver *s = chr->opaque;
2356 if (s->connected) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302357 return io_channel_send_all(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002358 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002359 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002360 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002361 }
2362}
2363
2364static int tcp_chr_read_poll(void *opaque)
2365{
2366 CharDriverState *chr = opaque;
2367 TCPCharDriver *s = chr->opaque;
2368 if (!s->connected)
2369 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002370 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002371 return s->max_size;
2372}
2373
2374#define IAC 255
2375#define IAC_BREAK 243
2376static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2377 TCPCharDriver *s,
2378 uint8_t *buf, int *size)
2379{
2380 /* Handle any telnet client's basic IAC options to satisfy char by
2381 * char mode with no echo. All IAC options will be removed from
2382 * the buf and the do_telnetopt variable will be used to track the
2383 * state of the width of the IAC information.
2384 *
2385 * IAC commands come in sets of 3 bytes with the exception of the
2386 * "IAC BREAK" command and the double IAC.
2387 */
2388
2389 int i;
2390 int j = 0;
2391
2392 for (i = 0; i < *size; i++) {
2393 if (s->do_telnetopt > 1) {
2394 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2395 /* Double IAC means send an IAC */
2396 if (j != i)
2397 buf[j] = buf[i];
2398 j++;
2399 s->do_telnetopt = 1;
2400 } else {
2401 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2402 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002403 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002404 s->do_telnetopt++;
2405 }
2406 s->do_telnetopt++;
2407 }
2408 if (s->do_telnetopt >= 4) {
2409 s->do_telnetopt = 1;
2410 }
2411 } else {
2412 if ((unsigned char)buf[i] == IAC) {
2413 s->do_telnetopt = 2;
2414 } else {
2415 if (j != i)
2416 buf[j] = buf[i];
2417 j++;
2418 }
2419 }
2420 }
2421 *size = j;
2422}
2423
Mark McLoughlin7d174052009-07-22 09:11:39 +01002424static int tcp_get_msgfd(CharDriverState *chr)
2425{
2426 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002427 int fd = s->msgfd;
2428 s->msgfd = -1;
2429 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002430}
2431
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002432#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002433static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2434{
2435 TCPCharDriver *s = chr->opaque;
2436 struct cmsghdr *cmsg;
2437
2438 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2439 int fd;
2440
2441 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2442 cmsg->cmsg_level != SOL_SOCKET ||
2443 cmsg->cmsg_type != SCM_RIGHTS)
2444 continue;
2445
2446 fd = *((int *)CMSG_DATA(cmsg));
2447 if (fd < 0)
2448 continue;
2449
Corey Bryant06138652012-08-14 16:43:42 -04002450#ifndef MSG_CMSG_CLOEXEC
2451 qemu_set_cloexec(fd);
2452#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002453 if (s->msgfd != -1)
2454 close(s->msgfd);
2455 s->msgfd = fd;
2456 }
2457}
2458
Mark McLoughlin9977c892009-07-22 09:11:38 +01002459static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2460{
2461 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002462 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002463 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002464 union {
2465 struct cmsghdr cmsg;
2466 char control[CMSG_SPACE(sizeof(int))];
2467 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002468 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002469 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002470
2471 iov[0].iov_base = buf;
2472 iov[0].iov_len = len;
2473
2474 msg.msg_iov = iov;
2475 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002476 msg.msg_control = &msg_control;
2477 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002478
Corey Bryant06138652012-08-14 16:43:42 -04002479#ifdef MSG_CMSG_CLOEXEC
2480 flags |= MSG_CMSG_CLOEXEC;
2481#endif
2482 ret = recvmsg(s->fd, &msg, flags);
2483 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002484 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002485 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002486
2487 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002488}
2489#else
2490static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2491{
2492 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002493 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002494}
2495#endif
2496
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302497static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2498{
2499 TCPCharDriver *s = chr->opaque;
2500 return g_io_create_watch(s->chan, cond);
2501}
2502
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302503static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002504{
2505 CharDriverState *chr = opaque;
2506 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302507 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002508 int len, size;
2509
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302510 if (!s->connected || s->max_size <= 0) {
2511 return FALSE;
2512 }
aliguori6f97dba2008-10-31 18:49:55 +00002513 len = sizeof(buf);
2514 if (len > s->max_size)
2515 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002516 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002517 if (size == 0) {
2518 /* connection closed */
2519 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302520 if (s->listen_chan) {
2521 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002522 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302523 g_source_remove(s->tag);
2524 s->tag = 0;
2525 g_io_channel_unref(s->chan);
2526 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002527 closesocket(s->fd);
2528 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002529 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002530 } else if (size > 0) {
2531 if (s->do_telnetopt)
2532 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2533 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002534 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002535 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302536
2537 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002538}
2539
Blue Swirl68c18d12010-08-15 09:46:24 +00002540#ifndef _WIN32
2541CharDriverState *qemu_chr_open_eventfd(int eventfd)
2542{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002543 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002544}
Blue Swirl68c18d12010-08-15 09:46:24 +00002545#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002546
aliguori6f97dba2008-10-31 18:49:55 +00002547static void tcp_chr_connect(void *opaque)
2548{
2549 CharDriverState *chr = opaque;
2550 TCPCharDriver *s = chr->opaque;
2551
2552 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302553 if (s->chan) {
2554 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002555 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002556 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002557}
2558
2559#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2560static void tcp_chr_telnet_init(int fd)
2561{
2562 char buf[3];
2563 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2564 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2565 send(fd, (char *)buf, 3, 0);
2566 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2567 send(fd, (char *)buf, 3, 0);
2568 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2569 send(fd, (char *)buf, 3, 0);
2570 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2571 send(fd, (char *)buf, 3, 0);
2572}
2573
Daniel P. Berrange13661082011-06-23 13:31:42 +01002574static int tcp_chr_add_client(CharDriverState *chr, int fd)
2575{
2576 TCPCharDriver *s = chr->opaque;
2577 if (s->fd != -1)
2578 return -1;
2579
2580 socket_set_nonblock(fd);
2581 if (s->do_nodelay)
2582 socket_set_nodelay(fd);
2583 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302584 s->chan = io_channel_from_socket(fd);
2585 g_source_remove(s->listen_tag);
2586 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002587 tcp_chr_connect(chr);
2588
2589 return 0;
2590}
2591
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302592static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002593{
2594 CharDriverState *chr = opaque;
2595 TCPCharDriver *s = chr->opaque;
2596 struct sockaddr_in saddr;
2597#ifndef _WIN32
2598 struct sockaddr_un uaddr;
2599#endif
2600 struct sockaddr *addr;
2601 socklen_t len;
2602 int fd;
2603
2604 for(;;) {
2605#ifndef _WIN32
2606 if (s->is_unix) {
2607 len = sizeof(uaddr);
2608 addr = (struct sockaddr *)&uaddr;
2609 } else
2610#endif
2611 {
2612 len = sizeof(saddr);
2613 addr = (struct sockaddr *)&saddr;
2614 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002615 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002616 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302617 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002618 } else if (fd >= 0) {
2619 if (s->do_telnetopt)
2620 tcp_chr_telnet_init(fd);
2621 break;
2622 }
2623 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002624 if (tcp_chr_add_client(chr, fd) < 0)
2625 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302626
2627 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002628}
2629
2630static void tcp_chr_close(CharDriverState *chr)
2631{
2632 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002633 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302634 if (s->tag) {
2635 g_source_remove(s->tag);
2636 }
2637 if (s->chan) {
2638 g_io_channel_unref(s->chan);
2639 }
aliguori6f97dba2008-10-31 18:49:55 +00002640 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002641 }
2642 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302643 if (s->listen_tag) {
2644 g_source_remove(s->listen_tag);
2645 }
2646 if (s->listen_chan) {
2647 g_io_channel_unref(s->listen_chan);
2648 }
aliguori6f97dba2008-10-31 18:49:55 +00002649 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002650 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002651 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002652 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002653}
2654
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002655static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2656 bool is_listen, bool is_telnet,
2657 bool is_waitconnect,
2658 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002659{
2660 CharDriverState *chr = NULL;
2661 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002662 char host[NI_MAXHOST], serv[NI_MAXSERV];
2663 const char *left = "", *right = "";
2664 struct sockaddr_storage ss;
2665 socklen_t ss_len = sizeof(ss);
2666
2667 memset(&ss, 0, ss_len);
2668 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2669 error_setg(errp, "getsockname: %s", strerror(errno));
2670 return NULL;
2671 }
2672
2673 chr = g_malloc0(sizeof(CharDriverState));
2674 s = g_malloc0(sizeof(TCPCharDriver));
2675
2676 s->connected = 0;
2677 s->fd = -1;
2678 s->listen_fd = -1;
2679 s->msgfd = -1;
2680
2681 chr->filename = g_malloc(256);
2682 switch (ss.ss_family) {
2683#ifndef _WIN32
2684 case AF_UNIX:
2685 s->is_unix = 1;
2686 snprintf(chr->filename, 256, "unix:%s%s",
2687 ((struct sockaddr_un *)(&ss))->sun_path,
2688 is_listen ? ",server" : "");
2689 break;
2690#endif
2691 case AF_INET6:
2692 left = "[";
2693 right = "]";
2694 /* fall through */
2695 case AF_INET:
2696 s->do_nodelay = do_nodelay;
2697 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2698 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002699 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002700 is_telnet ? "telnet" : "tcp",
2701 left, host, right, serv,
2702 is_listen ? ",server" : "");
2703 break;
2704 }
2705
2706 chr->opaque = s;
2707 chr->chr_write = tcp_chr_write;
2708 chr->chr_close = tcp_chr_close;
2709 chr->get_msgfd = tcp_get_msgfd;
2710 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302711 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002712
2713 if (is_listen) {
2714 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302715 s->listen_chan = io_channel_from_socket(s->listen_fd);
2716 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002717 if (is_telnet) {
2718 s->do_telnetopt = 1;
2719 }
2720 } else {
2721 s->connected = 1;
2722 s->fd = fd;
2723 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302724 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002725 tcp_chr_connect(chr);
2726 }
2727
2728 if (is_listen && is_waitconnect) {
2729 printf("QEMU waiting for connection on: %s\n",
2730 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302731 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002732 socket_set_nonblock(s->listen_fd);
2733 }
2734 return chr;
2735}
2736
2737static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2738{
2739 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002740 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002741 int fd = -1;
2742 int is_listen;
2743 int is_waitconnect;
2744 int do_nodelay;
2745 int is_unix;
2746 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002747
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002748 is_listen = qemu_opt_get_bool(opts, "server", 0);
2749 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2750 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2751 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2752 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002753 if (!is_listen)
2754 is_waitconnect = 0;
2755
aliguorif07b6002008-11-11 20:54:09 +00002756 if (is_unix) {
2757 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002758 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002759 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002760 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002761 }
2762 } else {
2763 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002764 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002765 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002766 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002767 }
2768 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002769 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002770 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002771 }
aliguori6f97dba2008-10-31 18:49:55 +00002772
2773 if (!is_waitconnect)
2774 socket_set_nonblock(fd);
2775
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002776 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2777 is_waitconnect, &local_err);
2778 if (error_is_set(&local_err)) {
2779 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002780 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002781 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002782
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002783
aliguori6f97dba2008-10-31 18:49:55 +00002784 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002785 if (local_err) {
2786 qerror_report_err(local_err);
2787 error_free(local_err);
2788 }
2789 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002790 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002791 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002792 if (chr) {
2793 g_free(chr->opaque);
2794 g_free(chr);
2795 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002796 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002797}
2798
Luiz Capitulino999bd672010-10-22 16:09:05 -02002799/***********************************************************/
2800/* Memory chardev */
2801typedef struct {
2802 size_t outbuf_size;
2803 size_t outbuf_capacity;
2804 uint8_t *outbuf;
2805} MemoryDriver;
2806
2807static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2808{
2809 MemoryDriver *d = chr->opaque;
2810
2811 /* TODO: the QString implementation has the same code, we should
2812 * introduce a generic way to do this in cutils.c */
2813 if (d->outbuf_capacity < d->outbuf_size + len) {
2814 /* grow outbuf */
2815 d->outbuf_capacity += len;
2816 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002817 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002818 }
2819
2820 memcpy(d->outbuf + d->outbuf_size, buf, len);
2821 d->outbuf_size += len;
2822
2823 return len;
2824}
2825
2826void qemu_chr_init_mem(CharDriverState *chr)
2827{
2828 MemoryDriver *d;
2829
Anthony Liguori7267c092011-08-20 22:09:37 -05002830 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002831 d->outbuf_size = 0;
2832 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002833 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002834
2835 memset(chr, 0, sizeof(*chr));
2836 chr->opaque = d;
2837 chr->chr_write = mem_chr_write;
2838}
2839
2840QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2841{
2842 MemoryDriver *d = chr->opaque;
2843 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2844}
2845
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002846/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002847void qemu_chr_close_mem(CharDriverState *chr)
2848{
2849 MemoryDriver *d = chr->opaque;
2850
Anthony Liguori7267c092011-08-20 22:09:37 -05002851 g_free(d->outbuf);
2852 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002853 chr->opaque = NULL;
2854 chr->chr_write = NULL;
2855}
2856
2857size_t qemu_chr_mem_osize(const CharDriverState *chr)
2858{
2859 const MemoryDriver *d = chr->opaque;
2860 return d->outbuf_size;
2861}
2862
Lei Li51767e72013-01-25 00:03:19 +08002863/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002864/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002865
2866typedef struct {
2867 size_t size;
2868 size_t prod;
2869 size_t cons;
2870 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002871} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002872
Markus Armbruster3949e592013-02-06 21:27:24 +01002873static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002874{
Markus Armbruster3949e592013-02-06 21:27:24 +01002875 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002876
Markus Armbruster5c230102013-02-06 21:27:23 +01002877 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002878}
2879
Markus Armbruster3949e592013-02-06 21:27:24 +01002880static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002881{
Markus Armbruster3949e592013-02-06 21:27:24 +01002882 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002883 int i;
2884
2885 if (!buf || (len < 0)) {
2886 return -1;
2887 }
2888
2889 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002890 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2891 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002892 d->cons = d->prod - d->size;
2893 }
2894 }
2895
2896 return 0;
2897}
2898
Markus Armbruster3949e592013-02-06 21:27:24 +01002899static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002900{
Markus Armbruster3949e592013-02-06 21:27:24 +01002901 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002902 int i;
2903
Markus Armbruster5c230102013-02-06 21:27:23 +01002904 for (i = 0; i < len && d->cons != d->prod; i++) {
2905 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002906 }
2907
2908 return i;
2909}
2910
Markus Armbruster3949e592013-02-06 21:27:24 +01002911static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002912{
Markus Armbruster3949e592013-02-06 21:27:24 +01002913 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002914
2915 g_free(d->cbuf);
2916 g_free(d);
2917 chr->opaque = NULL;
2918}
2919
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002920static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2921 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002922{
2923 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002924 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002925
2926 chr = g_malloc0(sizeof(CharDriverState));
2927 d = g_malloc(sizeof(*d));
2928
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002929 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002930
2931 /* The size must be power of 2 */
2932 if (d->size & (d->size - 1)) {
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002933 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002934 goto fail;
2935 }
2936
2937 d->prod = 0;
2938 d->cons = 0;
2939 d->cbuf = g_malloc0(d->size);
2940
2941 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002942 chr->chr_write = ringbuf_chr_write;
2943 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002944
2945 return chr;
2946
2947fail:
2948 g_free(d);
2949 g_free(chr);
2950 return NULL;
2951}
2952
Markus Armbruster3949e592013-02-06 21:27:24 +01002953static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002954{
Markus Armbruster3949e592013-02-06 21:27:24 +01002955 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002956}
2957
Markus Armbruster3949e592013-02-06 21:27:24 +01002958void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002959 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002960 Error **errp)
2961{
2962 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002963 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002964 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002965 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002966
2967 chr = qemu_chr_find(device);
2968 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002969 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002970 return;
2971 }
2972
Markus Armbruster3949e592013-02-06 21:27:24 +01002973 if (!chr_is_ringbuf(chr)) {
2974 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002975 return;
2976 }
2977
Lei Li1f590cf2013-01-25 00:03:20 +08002978 if (has_format && (format == DATA_FORMAT_BASE64)) {
2979 write_data = g_base64_decode(data, &write_count);
2980 } else {
2981 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002982 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002983 }
2984
Markus Armbruster3949e592013-02-06 21:27:24 +01002985 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002986
Markus Armbruster13289fb2013-02-06 21:27:18 +01002987 if (write_data != (uint8_t *)data) {
2988 g_free((void *)write_data);
2989 }
2990
Lei Li1f590cf2013-01-25 00:03:20 +08002991 if (ret < 0) {
2992 error_setg(errp, "Failed to write to device %s", device);
2993 return;
2994 }
2995}
2996
Markus Armbruster3949e592013-02-06 21:27:24 +01002997char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002998 bool has_format, enum DataFormat format,
2999 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003000{
3001 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003002 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003003 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003004 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003005
3006 chr = qemu_chr_find(device);
3007 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003008 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003009 return NULL;
3010 }
3011
Markus Armbruster3949e592013-02-06 21:27:24 +01003012 if (!chr_is_ringbuf(chr)) {
3013 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003014 return NULL;
3015 }
3016
3017 if (size <= 0) {
3018 error_setg(errp, "size must be greater than zero");
3019 return NULL;
3020 }
3021
Markus Armbruster3949e592013-02-06 21:27:24 +01003022 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003023 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003024 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003025
Markus Armbruster3949e592013-02-06 21:27:24 +01003026 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003027
3028 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003029 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003030 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003031 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003032 /*
3033 * FIXME should read only complete, valid UTF-8 characters up
3034 * to @size bytes. Invalid sequences should be replaced by a
3035 * suitable replacement character. Except when (and only
3036 * when) ring buffer lost characters since last read, initial
3037 * continuation characters should be dropped.
3038 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003039 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003040 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003041 }
3042
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003043 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003044}
3045
Gerd Hoffmann33521632009-12-08 13:11:49 +01003046QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003047{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003048 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003049 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003050 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003051 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003052 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003053
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003054 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
3055 if (error_is_set(&local_err)) {
3056 qerror_report_err(local_err);
3057 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003058 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003059 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003060
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003061 if (strstart(filename, "mon:", &p)) {
3062 filename = p;
3063 qemu_opt_set(opts, "mux", "on");
3064 }
3065
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003066 if (strcmp(filename, "null") == 0 ||
3067 strcmp(filename, "pty") == 0 ||
3068 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003069 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003070 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003071 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003072 return opts;
3073 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003074 if (strstart(filename, "vc", &p)) {
3075 qemu_opt_set(opts, "backend", "vc");
3076 if (*p == ':') {
3077 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3078 /* pixels */
3079 qemu_opt_set(opts, "width", width);
3080 qemu_opt_set(opts, "height", height);
3081 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3082 /* chars */
3083 qemu_opt_set(opts, "cols", width);
3084 qemu_opt_set(opts, "rows", height);
3085 } else {
3086 goto fail;
3087 }
3088 }
3089 return opts;
3090 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003091 if (strcmp(filename, "con:") == 0) {
3092 qemu_opt_set(opts, "backend", "console");
3093 return opts;
3094 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003095 if (strstart(filename, "COM", NULL)) {
3096 qemu_opt_set(opts, "backend", "serial");
3097 qemu_opt_set(opts, "path", filename);
3098 return opts;
3099 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003100 if (strstart(filename, "file:", &p)) {
3101 qemu_opt_set(opts, "backend", "file");
3102 qemu_opt_set(opts, "path", p);
3103 return opts;
3104 }
3105 if (strstart(filename, "pipe:", &p)) {
3106 qemu_opt_set(opts, "backend", "pipe");
3107 qemu_opt_set(opts, "path", p);
3108 return opts;
3109 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003110 if (strstart(filename, "tcp:", &p) ||
3111 strstart(filename, "telnet:", &p)) {
3112 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3113 host[0] = 0;
3114 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3115 goto fail;
3116 }
3117 qemu_opt_set(opts, "backend", "socket");
3118 qemu_opt_set(opts, "host", host);
3119 qemu_opt_set(opts, "port", port);
3120 if (p[pos] == ',') {
3121 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3122 goto fail;
3123 }
3124 if (strstart(filename, "telnet:", &p))
3125 qemu_opt_set(opts, "telnet", "on");
3126 return opts;
3127 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003128 if (strstart(filename, "udp:", &p)) {
3129 qemu_opt_set(opts, "backend", "udp");
3130 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3131 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003132 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003133 goto fail;
3134 }
3135 }
3136 qemu_opt_set(opts, "host", host);
3137 qemu_opt_set(opts, "port", port);
3138 if (p[pos] == '@') {
3139 p += pos + 1;
3140 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3141 host[0] = 0;
3142 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003143 goto fail;
3144 }
3145 }
3146 qemu_opt_set(opts, "localaddr", host);
3147 qemu_opt_set(opts, "localport", port);
3148 }
3149 return opts;
3150 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003151 if (strstart(filename, "unix:", &p)) {
3152 qemu_opt_set(opts, "backend", "socket");
3153 if (qemu_opts_do_parse(opts, p, "path") != 0)
3154 goto fail;
3155 return opts;
3156 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003157 if (strstart(filename, "/dev/parport", NULL) ||
3158 strstart(filename, "/dev/ppi", NULL)) {
3159 qemu_opt_set(opts, "backend", "parport");
3160 qemu_opt_set(opts, "path", filename);
3161 return opts;
3162 }
3163 if (strstart(filename, "/dev/", NULL)) {
3164 qemu_opt_set(opts, "backend", "tty");
3165 qemu_opt_set(opts, "path", filename);
3166 return opts;
3167 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003168
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003169fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003170 qemu_opts_del(opts);
3171 return NULL;
3172}
3173
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003174static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3175 Error **errp)
3176{
3177 const char *path = qemu_opt_get(opts, "path");
3178
3179 if (path == NULL) {
3180 error_setg(errp, "chardev: file: no filename given");
3181 return;
3182 }
3183 backend->file = g_new0(ChardevFile, 1);
3184 backend->file->out = g_strdup(path);
3185}
3186
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003187static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3188 Error **errp)
3189{
3190 backend->stdio = g_new0(ChardevStdio, 1);
3191 backend->stdio->has_signal = true;
3192 backend->stdio->signal =
3193 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3194}
3195
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003196static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3197 Error **errp)
3198{
3199 const char *device = qemu_opt_get(opts, "path");
3200
3201 if (device == NULL) {
3202 error_setg(errp, "chardev: serial/tty: no device path given");
3203 return;
3204 }
3205 backend->serial = g_new0(ChardevHostdev, 1);
3206 backend->serial->device = g_strdup(device);
3207}
3208
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003209static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3210 Error **errp)
3211{
3212 const char *device = qemu_opt_get(opts, "path");
3213
3214 if (device == NULL) {
3215 error_setg(errp, "chardev: parallel: no device path given");
3216 return;
3217 }
3218 backend->parallel = g_new0(ChardevHostdev, 1);
3219 backend->parallel->device = g_strdup(device);
3220}
3221
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003222static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3223 Error **errp)
3224{
3225 const char *device = qemu_opt_get(opts, "path");
3226
3227 if (device == NULL) {
3228 error_setg(errp, "chardev: pipe: no device path given");
3229 return;
3230 }
3231 backend->pipe = g_new0(ChardevHostdev, 1);
3232 backend->pipe->device = g_strdup(device);
3233}
3234
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003235static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3236 Error **errp)
3237{
3238 int val;
3239
3240 backend->memory = g_new0(ChardevRingbuf, 1);
3241
3242 val = qemu_opt_get_number(opts, "size", 0);
3243 if (val != 0) {
3244 backend->memory->has_size = true;
3245 backend->memory->size = val;
3246 }
3247}
3248
Anthony Liguorid654f342013-03-05 23:21:28 +05303249typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003250 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003251 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003252 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003253 /* new, qapi-based */
3254 int kind;
3255 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303256} CharDriver;
3257
3258static GSList *backends;
3259
3260void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3261{
3262 CharDriver *s;
3263
3264 s = g_malloc0(sizeof(*s));
3265 s->name = g_strdup(name);
3266 s->open = open;
3267
3268 backends = g_slist_append(backends, s);
3269}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003270
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003271void register_char_driver_qapi(const char *name, int kind,
3272 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3273{
3274 CharDriver *s;
3275
3276 s = g_malloc0(sizeof(*s));
3277 s->name = g_strdup(name);
3278 s->kind = kind;
3279 s->parse = parse;
3280
3281 backends = g_slist_append(backends, s);
3282}
3283
Anthony Liguorif69554b2011-08-15 11:17:37 -05003284CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003285 void (*init)(struct CharDriverState *s),
3286 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003287{
Anthony Liguorid654f342013-03-05 23:21:28 +05303288 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003289 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303290 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003291
3292 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003293 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003294 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003295 }
3296
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003297 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003298 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003299 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003300 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003301 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303302 for (i = backends; i; i = i->next) {
3303 cd = i->data;
3304
3305 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003306 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303307 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003308 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303309 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003310 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003311 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303312 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003313 }
3314
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003315 if (!cd->open) {
3316 /* using new, qapi init */
3317 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3318 ChardevReturn *ret = NULL;
3319 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003320 const char *bid = NULL;
3321
3322 if (qemu_opt_get_bool(opts, "mux", 0)) {
3323 bid = g_strdup_printf("%s-base", id);
3324 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003325
3326 chr = NULL;
3327 backend->kind = cd->kind;
3328 if (cd->parse) {
3329 cd->parse(opts, backend, errp);
3330 if (error_is_set(errp)) {
3331 goto qapi_out;
3332 }
3333 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003334 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003335 if (error_is_set(errp)) {
3336 goto qapi_out;
3337 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003338
3339 if (bid) {
3340 qapi_free_ChardevBackend(backend);
3341 qapi_free_ChardevReturn(ret);
3342 backend = g_new0(ChardevBackend, 1);
3343 backend->mux = g_new0(ChardevMux, 1);
3344 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3345 backend->mux->chardev = g_strdup(bid);
3346 ret = qmp_chardev_add(id, backend, errp);
3347 if (error_is_set(errp)) {
3348 goto qapi_out;
3349 }
3350 }
3351
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003352 chr = qemu_chr_find(id);
3353
3354 qapi_out:
3355 qapi_free_ChardevBackend(backend);
3356 qapi_free_ChardevReturn(ret);
3357 return chr;
3358 }
3359
Anthony Liguorid654f342013-03-05 23:21:28 +05303360 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003361 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003362 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003363 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003364 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003365 }
3366
3367 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003368 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003369 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003370 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003371
3372 if (qemu_opt_get_bool(opts, "mux", 0)) {
3373 CharDriverState *base = chr;
3374 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003375 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003376 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3377 chr = qemu_chr_open_mux(base);
3378 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003379 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003380 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003381 } else {
3382 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003383 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003384 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003385 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003386 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003387
3388err:
3389 qemu_opts_del(opts);
3390 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003391}
3392
Anthony Liguori27143a42011-08-15 11:17:36 -05003393CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003394{
3395 const char *p;
3396 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003397 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003398 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003399
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003400 if (strstart(filename, "chardev:", &p)) {
3401 return qemu_chr_find(p);
3402 }
3403
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003404 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003405 if (!opts)
3406 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003407
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003408 chr = qemu_chr_new_from_opts(opts, init, &err);
3409 if (error_is_set(&err)) {
3410 fprintf(stderr, "%s\n", error_get_pretty(err));
3411 error_free(err);
3412 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003413 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
3414 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003415 }
3416 return chr;
3417}
3418
Anthony Liguori15f31512011-08-15 11:17:35 -05003419void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003420{
3421 if (chr->chr_set_echo) {
3422 chr->chr_set_echo(chr, echo);
3423 }
3424}
3425
Anthony Liguoric9d830e2011-08-15 11:17:32 -05003426void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003427{
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003428 if (chr->fe_open) {
3429 return;
3430 }
3431 chr->fe_open = 1;
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003432 if (chr->chr_guest_open) {
3433 chr->chr_guest_open(chr);
3434 }
3435}
3436
Anthony Liguori28178222011-08-15 11:17:33 -05003437void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003438{
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003439 if (!chr->fe_open) {
3440 return;
3441 }
3442 chr->fe_open = 0;
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003443 if (chr->chr_guest_close) {
3444 chr->chr_guest_close(chr);
3445 }
3446}
3447
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003448int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3449 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303450{
3451 GSource *src;
3452 guint tag;
3453
3454 if (s->chr_add_watch == NULL) {
3455 return -ENOSYS;
3456 }
3457
3458 src = s->chr_add_watch(s, cond);
3459 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3460 tag = g_source_attach(src, NULL);
3461 g_source_unref(src);
3462
3463 return tag;
3464}
3465
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003466void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003467{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003468 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003469 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003470 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003471 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003472 g_free(chr->filename);
3473 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003474 if (chr->opts) {
3475 qemu_opts_del(chr->opts);
3476 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003477 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003478}
3479
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003480ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003481{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003482 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003483 CharDriverState *chr;
3484
Blue Swirl72cf2d42009-09-12 07:36:22 +00003485 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003486 ChardevInfoList *info = g_malloc0(sizeof(*info));
3487 info->value = g_malloc0(sizeof(*info->value));
3488 info->value->label = g_strdup(chr->label);
3489 info->value->filename = g_strdup(chr->filename);
3490
3491 info->next = chr_list;
3492 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003493 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003494
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003495 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003496}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003497
3498CharDriverState *qemu_chr_find(const char *name)
3499{
3500 CharDriverState *chr;
3501
Blue Swirl72cf2d42009-09-12 07:36:22 +00003502 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003503 if (strcmp(chr->label, name) != 0)
3504 continue;
3505 return chr;
3506 }
3507 return NULL;
3508}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003509
3510/* Get a character (serial) device interface. */
3511CharDriverState *qemu_char_get_next_serial(void)
3512{
3513 static int next_serial;
3514
3515 /* FIXME: This function needs to go away: use chardev properties! */
3516 return serial_hds[next_serial++];
3517}
3518
Paolo Bonzini4d454572012-11-26 16:03:42 +01003519QemuOptsList qemu_chardev_opts = {
3520 .name = "chardev",
3521 .implied_opt_name = "backend",
3522 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3523 .desc = {
3524 {
3525 .name = "backend",
3526 .type = QEMU_OPT_STRING,
3527 },{
3528 .name = "path",
3529 .type = QEMU_OPT_STRING,
3530 },{
3531 .name = "host",
3532 .type = QEMU_OPT_STRING,
3533 },{
3534 .name = "port",
3535 .type = QEMU_OPT_STRING,
3536 },{
3537 .name = "localaddr",
3538 .type = QEMU_OPT_STRING,
3539 },{
3540 .name = "localport",
3541 .type = QEMU_OPT_STRING,
3542 },{
3543 .name = "to",
3544 .type = QEMU_OPT_NUMBER,
3545 },{
3546 .name = "ipv4",
3547 .type = QEMU_OPT_BOOL,
3548 },{
3549 .name = "ipv6",
3550 .type = QEMU_OPT_BOOL,
3551 },{
3552 .name = "wait",
3553 .type = QEMU_OPT_BOOL,
3554 },{
3555 .name = "server",
3556 .type = QEMU_OPT_BOOL,
3557 },{
3558 .name = "delay",
3559 .type = QEMU_OPT_BOOL,
3560 },{
3561 .name = "telnet",
3562 .type = QEMU_OPT_BOOL,
3563 },{
3564 .name = "width",
3565 .type = QEMU_OPT_NUMBER,
3566 },{
3567 .name = "height",
3568 .type = QEMU_OPT_NUMBER,
3569 },{
3570 .name = "cols",
3571 .type = QEMU_OPT_NUMBER,
3572 },{
3573 .name = "rows",
3574 .type = QEMU_OPT_NUMBER,
3575 },{
3576 .name = "mux",
3577 .type = QEMU_OPT_BOOL,
3578 },{
3579 .name = "signal",
3580 .type = QEMU_OPT_BOOL,
3581 },{
3582 .name = "name",
3583 .type = QEMU_OPT_STRING,
3584 },{
3585 .name = "debug",
3586 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003587 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003588 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003589 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003590 },
3591 { /* end of list */ }
3592 },
3593};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003594
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003595#ifdef _WIN32
3596
3597static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3598{
3599 HANDLE out;
3600
3601 if (file->in) {
3602 error_setg(errp, "input file not supported");
3603 return NULL;
3604 }
3605
3606 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3607 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3608 if (out == INVALID_HANDLE_VALUE) {
3609 error_setg(errp, "open %s failed", file->out);
3610 return NULL;
3611 }
3612 return qemu_chr_open_win_file(out);
3613}
3614
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003615static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3616 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003617{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003618 return qemu_chr_open_win_path(serial->device);
3619}
3620
3621static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3622 Error **errp)
3623{
3624 error_setg(errp, "character device backend type 'parallel' not supported");
3625 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003626}
3627
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003628#else /* WIN32 */
3629
3630static int qmp_chardev_open_file_source(char *src, int flags,
3631 Error **errp)
3632{
3633 int fd = -1;
3634
3635 TFR(fd = qemu_open(src, flags, 0666));
3636 if (fd == -1) {
3637 error_setg(errp, "open %s: %s", src, strerror(errno));
3638 }
3639 return fd;
3640}
3641
3642static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3643{
3644 int flags, in = -1, out = -1;
3645
3646 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3647 out = qmp_chardev_open_file_source(file->out, flags, errp);
3648 if (error_is_set(errp)) {
3649 return NULL;
3650 }
3651
3652 if (file->in) {
3653 flags = O_RDONLY;
3654 in = qmp_chardev_open_file_source(file->in, flags, errp);
3655 if (error_is_set(errp)) {
3656 qemu_close(out);
3657 return NULL;
3658 }
3659 }
3660
3661 return qemu_chr_open_fd(in, out);
3662}
3663
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003664static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3665 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003666{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003667#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003668 int fd;
3669
3670 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3671 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003672 return NULL;
3673 }
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003674 socket_set_nonblock(fd);
3675 return qemu_chr_open_tty_fd(fd);
3676#else
3677 error_setg(errp, "character device backend type 'serial' not supported");
3678 return NULL;
3679#endif
3680}
3681
3682static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3683 Error **errp)
3684{
3685#ifdef HAVE_CHARDEV_PARPORT
3686 int fd;
3687
3688 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3689 if (error_is_set(errp)) {
3690 return NULL;
3691 }
3692 return qemu_chr_open_pp_fd(fd);
3693#else
3694 error_setg(errp, "character device backend type 'parallel' not supported");
3695 return NULL;
3696#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003697}
3698
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003699#endif /* WIN32 */
3700
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003701static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3702 Error **errp)
3703{
3704 SocketAddress *addr = sock->addr;
3705 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3706 bool is_listen = sock->has_server ? sock->server : true;
3707 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3708 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3709 int fd;
3710
3711 if (is_listen) {
3712 fd = socket_listen(addr, errp);
3713 } else {
3714 fd = socket_connect(addr, errp, NULL, NULL);
3715 }
3716 if (error_is_set(errp)) {
3717 return NULL;
3718 }
3719 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3720 is_telnet, is_waitconnect, errp);
3721}
3722
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003723static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3724 Error **errp)
3725{
3726 int fd;
3727
3728 fd = socket_dgram(dgram->remote, dgram->local, errp);
3729 if (error_is_set(errp)) {
3730 return NULL;
3731 }
3732 return qemu_chr_open_udp_fd(fd);
3733}
3734
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003735ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3736 Error **errp)
3737{
3738 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003739 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003740
3741 chr = qemu_chr_find(id);
3742 if (chr) {
3743 error_setg(errp, "Chardev '%s' already exists", id);
3744 g_free(ret);
3745 return NULL;
3746 }
3747
3748 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003749 case CHARDEV_BACKEND_KIND_FILE:
3750 chr = qmp_chardev_open_file(backend->file, errp);
3751 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003752 case CHARDEV_BACKEND_KIND_SERIAL:
3753 chr = qmp_chardev_open_serial(backend->serial, errp);
3754 break;
3755 case CHARDEV_BACKEND_KIND_PARALLEL:
3756 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003757 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003758 case CHARDEV_BACKEND_KIND_PIPE:
3759 chr = qemu_chr_open_pipe(backend->pipe);
3760 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003761 case CHARDEV_BACKEND_KIND_SOCKET:
3762 chr = qmp_chardev_open_socket(backend->socket, errp);
3763 break;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003764 case CHARDEV_BACKEND_KIND_DGRAM:
3765 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3766 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003767#ifdef HAVE_CHARDEV_TTY
3768 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003769 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003770 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003771#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003772 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003773 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003774 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003775 case CHARDEV_BACKEND_KIND_MUX:
3776 base = qemu_chr_find(backend->mux->chardev);
3777 if (base == NULL) {
3778 error_setg(errp, "mux: base chardev %s not found",
3779 backend->mux->chardev);
3780 break;
3781 }
3782 chr = qemu_chr_open_mux(base);
3783 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003784 case CHARDEV_BACKEND_KIND_MSMOUSE:
3785 chr = qemu_chr_open_msmouse();
3786 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003787#ifdef CONFIG_BRLAPI
3788 case CHARDEV_BACKEND_KIND_BRAILLE:
3789 chr = chr_baum_init();
3790 break;
3791#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003792 case CHARDEV_BACKEND_KIND_STDIO:
3793 chr = qemu_chr_open_stdio(backend->stdio);
3794 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003795#ifdef _WIN32
3796 case CHARDEV_BACKEND_KIND_CONSOLE:
3797 chr = qemu_chr_open_win_con();
3798 break;
3799#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003800#ifdef CONFIG_SPICE
3801 case CHARDEV_BACKEND_KIND_SPICEVMC:
3802 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3803 break;
3804 case CHARDEV_BACKEND_KIND_SPICEPORT:
3805 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3806 break;
3807#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003808 case CHARDEV_BACKEND_KIND_VC:
3809 chr = vc_init(backend->vc);
3810 break;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003811 case CHARDEV_BACKEND_KIND_MEMORY:
3812 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3813 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003814 default:
3815 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3816 break;
3817 }
3818
3819 if (chr == NULL && !error_is_set(errp)) {
3820 error_setg(errp, "Failed to create chardev");
3821 }
3822 if (chr) {
3823 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003824 chr->avail_connections =
3825 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003826 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3827 return ret;
3828 } else {
3829 g_free(ret);
3830 return NULL;
3831 }
3832}
3833
3834void qmp_chardev_remove(const char *id, Error **errp)
3835{
3836 CharDriverState *chr;
3837
3838 chr = qemu_chr_find(id);
3839 if (NULL == chr) {
3840 error_setg(errp, "Chardev '%s' not found", id);
3841 return;
3842 }
3843 if (chr->chr_can_read || chr->chr_read ||
3844 chr->chr_event || chr->handler_opaque) {
3845 error_setg(errp, "Chardev '%s' is busy", id);
3846 return;
3847 }
3848 qemu_chr_delete(chr);
3849}
Anthony Liguorid654f342013-03-05 23:21:28 +05303850
3851static void register_types(void)
3852{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003853 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303854 register_char_driver("socket", qemu_chr_open_socket);
3855 register_char_driver("udp", qemu_chr_open_udp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003856 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3857 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003858 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3859 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003860 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3861 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003862 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3863 qemu_chr_parse_serial);
3864 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3865 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003866 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3867 qemu_chr_parse_parallel);
3868 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3869 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003870 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003871 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003872 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3873 qemu_chr_parse_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303874}
3875
3876type_init(register_types);