blob: 9fd94d1bb40512929bb2233397661174f379dd3a [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"
25#include "net.h"
aliguori376253e2009-03-05 23:01:23 +000026#include "monitor.h"
aliguori6f97dba2008-10-31 18:49:55 +000027#include "console.h"
28#include "sysemu.h"
29#include "qemu-timer.h"
30#include "qemu-char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000031#include "hw/usb.h"
32#include "hw/baum.h"
aurel32aa71cf82009-02-08 15:53:20 +000033#include "hw/msmouse.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030034#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
36#include <unistd.h>
37#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000038#include <time.h>
39#include <errno.h>
40#include <sys/time.h>
41#include <zlib.h>
42
43#ifndef _WIN32
44#include <sys/times.h>
45#include <sys/wait.h>
46#include <termios.h>
47#include <sys/mman.h>
48#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <sys/socket.h>
51#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000054#include <dirent.h>
55#include <netdb.h>
56#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020057#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000058#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010059#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
aliguori6f97dba2008-10-31 18:49:55 +000060#include <libutil.h>
blueswir16972f932008-11-22 20:49:12 +000061#include <dev/ppbus/ppi.h>
62#include <dev/ppbus/ppbconf.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010063#if defined(__GLIBC__)
64#include <pty.h>
65#endif
blueswir1c5e97232009-03-07 20:06:23 +000066#elif defined(__DragonFly__)
67#include <libutil.h>
68#include <dev/misc/ppi/ppi.h>
69#include <bus/ppbus/ppbconf.h>
blueswir124646c72008-11-07 16:55:48 +000070#else
71#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000072#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010073#else
aliguori6f97dba2008-10-31 18:49:55 +000074#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000075#include <pty.h>
76
77#include <linux/ppdev.h>
78#include <linux/parport.h>
79#endif
80#ifdef __sun__
81#include <sys/stat.h>
82#include <sys/ethernet.h>
83#include <sys/sockio.h>
84#include <netinet/arp.h>
85#include <netinet/in.h>
86#include <netinet/in_systm.h>
87#include <netinet/ip.h>
88#include <netinet/ip_icmp.h> // must come after ip.h
89#include <netinet/udp.h>
90#include <netinet/tcp.h>
91#include <net/if.h>
92#include <syslog.h>
93#include <stropts.h>
94#endif
95#endif
96#endif
97
98#include "qemu_socket.h"
Alon Levycbcc6332011-01-19 10:49:50 +020099#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +0000100
Amit Shah9bd78542009-11-03 19:59:54 +0530101#define READ_BUF_LEN 4096
102
aliguori6f97dba2008-10-31 18:49:55 +0000103/***********************************************************/
104/* character device */
105
Blue Swirl72cf2d42009-09-12 07:36:22 +0000106static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
107 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000108
aliguori6f97dba2008-10-31 18:49:55 +0000109static void qemu_chr_event(CharDriverState *s, int event)
110{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200111 /* Keep track if the char device is open */
112 switch (event) {
113 case CHR_EVENT_OPENED:
114 s->opened = 1;
115 break;
116 case CHR_EVENT_CLOSED:
117 s->opened = 0;
118 break;
119 }
120
aliguori6f97dba2008-10-31 18:49:55 +0000121 if (!s->chr_event)
122 return;
123 s->chr_event(s->handler_opaque, event);
124}
125
Amit Shah127338e2009-11-03 19:59:56 +0530126static void qemu_chr_generic_open_bh(void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000127{
128 CharDriverState *s = opaque;
Anthony Liguorif7cbc082009-10-27 10:14:50 -0500129 qemu_chr_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000130 qemu_bh_delete(s->bh);
131 s->bh = NULL;
132}
133
Amit Shah127338e2009-11-03 19:59:56 +0530134void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000135{
Amit Shah69795d62009-10-07 18:31:15 +0530136 if (s->bh == NULL) {
Amit Shah127338e2009-11-03 19:59:56 +0530137 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000138 qemu_bh_schedule(s->bh);
139 }
140}
141
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500142int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000143{
144 return s->chr_write(s, buf, len);
145}
146
Anthony Liguori41084f12011-08-15 11:17:34 -0500147int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000148{
149 if (!s->chr_ioctl)
150 return -ENOTSUP;
151 return s->chr_ioctl(s, cmd, arg);
152}
153
Anthony Liguori909cda12011-08-15 11:17:31 -0500154int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000155{
156 if (!s->chr_can_read)
157 return 0;
158 return s->chr_can_read(s->handler_opaque);
159}
160
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500161void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000162{
163 s->chr_read(s->handler_opaque, buf, len);
164}
165
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500166int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100167{
168 return s->get_msgfd ? s->get_msgfd(s) : -1;
169}
170
Daniel P. Berrange13661082011-06-23 13:31:42 +0100171int qemu_chr_add_client(CharDriverState *s, int fd)
172{
173 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
174}
175
aliguori6f97dba2008-10-31 18:49:55 +0000176void qemu_chr_accept_input(CharDriverState *s)
177{
178 if (s->chr_accept_input)
179 s->chr_accept_input(s);
180}
181
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500182void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000183{
Amit Shah9bd78542009-11-03 19:59:54 +0530184 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000185 va_list ap;
186 va_start(ap, fmt);
187 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500188 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000189 va_end(ap);
190}
191
aliguori6f97dba2008-10-31 18:49:55 +0000192void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100193 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000194 IOReadHandler *fd_read,
195 IOEventHandler *fd_event,
196 void *opaque)
197{
Amit Shahda7d9982011-04-25 15:18:22 +0530198 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530199 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900200 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530201 }
aliguori6f97dba2008-10-31 18:49:55 +0000202 s->chr_can_read = fd_can_read;
203 s->chr_read = fd_read;
204 s->chr_event = fd_event;
205 s->handler_opaque = opaque;
206 if (s->chr_update_read_handler)
207 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200208
209 /* We're connecting to an already opened device, so let's make sure we
210 also get the open event */
211 if (s->opened) {
212 qemu_chr_generic_open(s);
213 }
aliguori6f97dba2008-10-31 18:49:55 +0000214}
215
216static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
217{
218 return len;
219}
220
Kevin Wolf6e1db572011-06-01 13:29:11 +0200221static int qemu_chr_open_null(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000222{
223 CharDriverState *chr;
224
Anthony Liguori7267c092011-08-20 22:09:37 -0500225 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000226 chr->chr_write = null_chr_write;
Kevin Wolf6e1db572011-06-01 13:29:11 +0200227
228 *_chr= chr;
229 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000230}
231
232/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000233#define MAX_MUX 4
234#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
235#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
236typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100237 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000238 IOReadHandler *chr_read[MAX_MUX];
239 IOEventHandler *chr_event[MAX_MUX];
240 void *ext_opaque[MAX_MUX];
241 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200242 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000243 int mux_cnt;
244 int term_got_escape;
245 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000246 /* Intermediate input buffer allows to catch escape sequences even if the
247 currently active device is not accepting any input - but only until it
248 is full as well. */
249 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
250 int prod[MAX_MUX];
251 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200252 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200253 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200254 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000255} MuxDriver;
256
257
258static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
259{
260 MuxDriver *d = chr->opaque;
261 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200262 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000263 ret = d->drv->chr_write(d->drv, buf, len);
264 } else {
265 int i;
266
267 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200268 for (i = 0; i < len; i++) {
269 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000270 char buf1[64];
271 int64_t ti;
272 int secs;
273
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100274 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200275 if (d->timestamps_start == -1)
276 d->timestamps_start = ti;
277 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000278 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000279 snprintf(buf1, sizeof(buf1),
280 "[%02d:%02d:%02d.%03d] ",
281 secs / 3600,
282 (secs / 60) % 60,
283 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000284 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000285 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200286 d->linestart = 0;
287 }
288 ret += d->drv->chr_write(d->drv, buf+i, 1);
289 if (buf[i] == '\n') {
290 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000291 }
292 }
293 }
294 return ret;
295}
296
297static const char * const mux_help[] = {
298 "% h print this help\n\r",
299 "% x exit emulator\n\r",
300 "% s save disk data back to file (if -snapshot)\n\r",
301 "% t toggle console timestamps\n\r"
302 "% b send break (magic sysrq)\n\r",
303 "% c switch between console and monitor\n\r",
304 "% % sends %\n\r",
305 NULL
306};
307
308int term_escape_char = 0x01; /* ctrl-a is used for escape */
309static void mux_print_help(CharDriverState *chr)
310{
311 int i, j;
312 char ebuf[15] = "Escape-Char";
313 char cbuf[50] = "\n\r";
314
315 if (term_escape_char > 0 && term_escape_char < 26) {
316 snprintf(cbuf, sizeof(cbuf), "\n\r");
317 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
318 } else {
319 snprintf(cbuf, sizeof(cbuf),
320 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
321 term_escape_char);
322 }
323 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
324 for (i = 0; mux_help[i] != NULL; i++) {
325 for (j=0; mux_help[i][j] != '\0'; j++) {
326 if (mux_help[i][j] == '%')
327 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
328 else
329 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
330 }
331 }
332}
333
aliguori2724b182009-03-05 23:01:47 +0000334static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
335{
336 if (d->chr_event[mux_nr])
337 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
338}
339
aliguori6f97dba2008-10-31 18:49:55 +0000340static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
341{
342 if (d->term_got_escape) {
343 d->term_got_escape = 0;
344 if (ch == term_escape_char)
345 goto send_char;
346 switch(ch) {
347 case '?':
348 case 'h':
349 mux_print_help(chr);
350 break;
351 case 'x':
352 {
353 const char *term = "QEMU: Terminated\n\r";
354 chr->chr_write(chr,(uint8_t *)term,strlen(term));
355 exit(0);
356 break;
357 }
358 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200359 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000360 break;
361 case 'b':
362 qemu_chr_event(chr, CHR_EVENT_BREAK);
363 break;
364 case 'c':
365 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200366 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
367 d->focus++;
368 if (d->focus >= d->mux_cnt)
369 d->focus = 0;
370 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000371 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200372 case 't':
373 d->timestamps = !d->timestamps;
374 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200375 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200376 break;
aliguori6f97dba2008-10-31 18:49:55 +0000377 }
378 } else if (ch == term_escape_char) {
379 d->term_got_escape = 1;
380 } else {
381 send_char:
382 return 1;
383 }
384 return 0;
385}
386
387static void mux_chr_accept_input(CharDriverState *chr)
388{
aliguori6f97dba2008-10-31 18:49:55 +0000389 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200390 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000391
aliguoria80bf992009-03-05 23:00:02 +0000392 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000393 d->chr_can_read[m] &&
394 d->chr_can_read[m](d->ext_opaque[m])) {
395 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000396 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000397 }
398}
399
400static int mux_chr_can_read(void *opaque)
401{
402 CharDriverState *chr = opaque;
403 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200404 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000405
aliguoria80bf992009-03-05 23:00:02 +0000406 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000407 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000408 if (d->chr_can_read[m])
409 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000410 return 0;
411}
412
413static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
414{
415 CharDriverState *chr = opaque;
416 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200417 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000418 int i;
419
420 mux_chr_accept_input (opaque);
421
422 for(i = 0; i < size; i++)
423 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000424 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000425 d->chr_can_read[m] &&
426 d->chr_can_read[m](d->ext_opaque[m]))
427 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
428 else
aliguoria80bf992009-03-05 23:00:02 +0000429 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000430 }
431}
432
433static void mux_chr_event(void *opaque, int event)
434{
435 CharDriverState *chr = opaque;
436 MuxDriver *d = chr->opaque;
437 int i;
438
439 /* Send the event to all registered listeners */
440 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000441 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000442}
443
444static void mux_chr_update_read_handler(CharDriverState *chr)
445{
446 MuxDriver *d = chr->opaque;
447
448 if (d->mux_cnt >= MAX_MUX) {
449 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
450 return;
451 }
452 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
453 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
454 d->chr_read[d->mux_cnt] = chr->chr_read;
455 d->chr_event[d->mux_cnt] = chr->chr_event;
456 /* Fix up the real driver with mux routines */
457 if (d->mux_cnt == 0) {
458 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
459 mux_chr_event, chr);
460 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200461 if (d->focus != -1) {
462 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200463 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200464 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000465 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200466 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000467}
468
469static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
470{
471 CharDriverState *chr;
472 MuxDriver *d;
473
Anthony Liguori7267c092011-08-20 22:09:37 -0500474 chr = g_malloc0(sizeof(CharDriverState));
475 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000476
477 chr->opaque = d;
478 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200479 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000480 chr->chr_write = mux_chr_write;
481 chr->chr_update_read_handler = mux_chr_update_read_handler;
482 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100483 /* Frontend guest-open / -close notification is not support with muxes */
484 chr->chr_guest_open = NULL;
485 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200486
487 /* Muxes are always open on creation */
488 qemu_chr_generic_open(chr);
489
aliguori6f97dba2008-10-31 18:49:55 +0000490 return chr;
491}
492
493
494#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000495int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000496{
497 int ret, len;
498
499 len = len1;
500 while (len > 0) {
501 ret = send(fd, buf, len, 0);
502 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000503 errno = WSAGetLastError();
504 if (errno != WSAEWOULDBLOCK) {
505 return -1;
506 }
507 } else if (ret == 0) {
508 break;
509 } else {
510 buf += ret;
511 len -= ret;
512 }
513 }
514 return len1 - len;
515}
516
517#else
518
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100519int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000520{
521 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100522 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000523
524 len = len1;
525 while (len > 0) {
526 ret = write(fd, buf, len);
527 if (ret < 0) {
528 if (errno != EINTR && errno != EAGAIN)
529 return -1;
530 } else if (ret == 0) {
531 break;
532 } else {
533 buf += ret;
534 len -= ret;
535 }
536 }
537 return len1 - len;
538}
aliguori6f97dba2008-10-31 18:49:55 +0000539#endif /* !_WIN32 */
540
Fabien Chouteaudb418a02011-10-06 16:37:51 +0200541#define STDIO_MAX_CLIENTS 1
542static int stdio_nb_clients;
543
aliguori6f97dba2008-10-31 18:49:55 +0000544#ifndef _WIN32
545
546typedef struct {
547 int fd_in, fd_out;
548 int max_size;
549} FDCharDriver;
550
aliguori6f97dba2008-10-31 18:49:55 +0000551
552static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
553{
554 FDCharDriver *s = chr->opaque;
555 return send_all(s->fd_out, buf, len);
556}
557
558static int fd_chr_read_poll(void *opaque)
559{
560 CharDriverState *chr = opaque;
561 FDCharDriver *s = chr->opaque;
562
Anthony Liguori909cda12011-08-15 11:17:31 -0500563 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000564 return s->max_size;
565}
566
567static void fd_chr_read(void *opaque)
568{
569 CharDriverState *chr = opaque;
570 FDCharDriver *s = chr->opaque;
571 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530572 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000573
574 len = sizeof(buf);
575 if (len > s->max_size)
576 len = s->max_size;
577 if (len == 0)
578 return;
579 size = read(s->fd_in, buf, len);
580 if (size == 0) {
581 /* FD has been closed. Remove it from the active list. */
582 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530583 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000584 return;
585 }
586 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500587 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000588 }
589}
590
591static void fd_chr_update_read_handler(CharDriverState *chr)
592{
593 FDCharDriver *s = chr->opaque;
594
595 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500596 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000597 } else {
598 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
599 fd_chr_read, NULL, chr);
600 }
601 }
602}
603
604static void fd_chr_close(struct CharDriverState *chr)
605{
606 FDCharDriver *s = chr->opaque;
607
608 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500609 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000610 } else {
611 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
612 }
613 }
614
Anthony Liguori7267c092011-08-20 22:09:37 -0500615 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530616 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000617}
618
619/* open a character device to a unix fd */
620static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
621{
622 CharDriverState *chr;
623 FDCharDriver *s;
624
Anthony Liguori7267c092011-08-20 22:09:37 -0500625 chr = g_malloc0(sizeof(CharDriverState));
626 s = g_malloc0(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000627 s->fd_in = fd_in;
628 s->fd_out = fd_out;
629 chr->opaque = s;
630 chr->chr_write = fd_chr_write;
631 chr->chr_update_read_handler = fd_chr_update_read_handler;
632 chr->chr_close = fd_chr_close;
633
Amit Shah127338e2009-11-03 19:59:56 +0530634 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000635
636 return chr;
637}
638
Kevin Wolf6e1db572011-06-01 13:29:11 +0200639static int qemu_chr_open_file_out(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000640{
641 int fd_out;
642
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100643 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200644 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Kevin Wolf6e1db572011-06-01 13:29:11 +0200645 if (fd_out < 0) {
646 return -errno;
647 }
648
649 *_chr = qemu_chr_open_fd(-1, fd_out);
650 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000651}
652
Kevin Wolf6e1db572011-06-01 13:29:11 +0200653static int qemu_chr_open_pipe(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000654{
655 int fd_in, fd_out;
656 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200657 const char *filename = qemu_opt_get(opts, "path");
658
659 if (filename == NULL) {
660 fprintf(stderr, "chardev: pipe: no filename given\n");
Kevin Wolf6e1db572011-06-01 13:29:11 +0200661 return -EINVAL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200662 }
aliguori6f97dba2008-10-31 18:49:55 +0000663
664 snprintf(filename_in, 256, "%s.in", filename);
665 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100666 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
667 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000668 if (fd_in < 0 || fd_out < 0) {
669 if (fd_in >= 0)
670 close(fd_in);
671 if (fd_out >= 0)
672 close(fd_out);
Kevin Wolf6e1db572011-06-01 13:29:11 +0200673 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
674 if (fd_in < 0) {
675 return -errno;
676 }
aliguori6f97dba2008-10-31 18:49:55 +0000677 }
Kevin Wolf6e1db572011-06-01 13:29:11 +0200678
679 *_chr = qemu_chr_open_fd(fd_in, fd_out);
680 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000681}
682
683
684/* for STDIO, we handle the case where several clients use it
685 (nographic mode) */
686
687#define TERM_FIFO_MAX_SIZE 1
688
689static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
690static int term_fifo_size;
691
692static int stdio_read_poll(void *opaque)
693{
694 CharDriverState *chr = opaque;
695
696 /* try to flush the queue if needed */
Anthony Liguori909cda12011-08-15 11:17:31 -0500697 if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500698 qemu_chr_be_write(chr, term_fifo, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000699 term_fifo_size = 0;
700 }
701 /* see if we can absorb more chars */
702 if (term_fifo_size == 0)
703 return 1;
704 else
705 return 0;
706}
707
708static void stdio_read(void *opaque)
709{
710 int size;
711 uint8_t buf[1];
712 CharDriverState *chr = opaque;
713
714 size = read(0, buf, 1);
715 if (size == 0) {
716 /* stdin has been closed. Remove it from the active list. */
717 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530718 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000719 return;
720 }
721 if (size > 0) {
Anthony Liguori909cda12011-08-15 11:17:31 -0500722 if (qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500723 qemu_chr_be_write(chr, buf, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000724 } else if (term_fifo_size == 0) {
725 term_fifo[term_fifo_size++] = buf[0];
726 }
727 }
728}
729
730/* init terminal so that we can grab keys */
731static struct termios oldtty;
732static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100733static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000734
735static void term_exit(void)
736{
737 tcsetattr (0, TCSANOW, &oldtty);
738 fcntl(0, F_SETFL, old_fd0_flags);
739}
740
Paolo Bonzinibb002512010-12-23 13:42:50 +0100741static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000742{
743 struct termios tty;
744
Paolo Bonzini03693642010-12-23 13:42:49 +0100745 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100746 if (!echo) {
747 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000748 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100749 tty.c_oflag |= OPOST;
750 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
751 tty.c_cflag &= ~(CSIZE|PARENB);
752 tty.c_cflag |= CS8;
753 tty.c_cc[VMIN] = 1;
754 tty.c_cc[VTIME] = 0;
755 }
aliguori6f97dba2008-10-31 18:49:55 +0000756 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100757 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000758 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000759
760 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000761}
762
763static void qemu_chr_close_stdio(struct CharDriverState *chr)
764{
765 term_exit();
766 stdio_nb_clients--;
767 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
768 fd_chr_close(chr);
769}
770
Kevin Wolf6e1db572011-06-01 13:29:11 +0200771static int qemu_chr_open_stdio(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000772{
773 CharDriverState *chr;
774
Kevin Wolf6e1db572011-06-01 13:29:11 +0200775 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
776 return -EBUSY;
777 }
778
Paolo Bonzini03693642010-12-23 13:42:49 +0100779 if (stdio_nb_clients == 0) {
780 old_fd0_flags = fcntl(0, F_GETFL);
781 tcgetattr (0, &oldtty);
782 fcntl(0, F_SETFL, O_NONBLOCK);
783 atexit(term_exit);
784 }
785
aliguori6f97dba2008-10-31 18:49:55 +0000786 chr = qemu_chr_open_fd(0, 1);
787 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100788 chr->chr_set_echo = qemu_chr_set_echo_stdio;
aliguori6f97dba2008-10-31 18:49:55 +0000789 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
790 stdio_nb_clients++;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100791 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
792 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500793 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000794
Kevin Wolf6e1db572011-06-01 13:29:11 +0200795 *_chr = chr;
796 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000797}
798
799#ifdef __sun__
800/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000801static int openpty(int *amaster, int *aslave, char *name,
802 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000803{
804 const char *slave;
805 int mfd = -1, sfd = -1;
806
807 *amaster = *aslave = -1;
808
809 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
810 if (mfd < 0)
811 goto err;
812
813 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
814 goto err;
815
816 if ((slave = ptsname(mfd)) == NULL)
817 goto err;
818
819 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
820 goto err;
821
822 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
823 (termp != NULL && tcgetattr(sfd, termp) < 0))
824 goto err;
825
826 if (amaster)
827 *amaster = mfd;
828 if (aslave)
829 *aslave = sfd;
830 if (winp)
831 ioctl(sfd, TIOCSWINSZ, winp);
832
833 return 0;
834
835err:
836 if (sfd != -1)
837 close(sfd);
838 close(mfd);
839 return -1;
840}
841
blueswir13f4cb3d2009-04-13 16:31:01 +0000842static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000843{
844 termios_p->c_iflag &=
845 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
846 termios_p->c_oflag &= ~OPOST;
847 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
848 termios_p->c_cflag &= ~(CSIZE|PARENB);
849 termios_p->c_cflag |= CS8;
850
851 termios_p->c_cc[VMIN] = 0;
852 termios_p->c_cc[VTIME] = 0;
853}
854#endif
855
856#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100857 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
858 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000859
860typedef struct {
861 int fd;
862 int connected;
863 int polling;
864 int read_bytes;
865 QEMUTimer *timer;
866} PtyCharDriver;
867
868static void pty_chr_update_read_handler(CharDriverState *chr);
869static void pty_chr_state(CharDriverState *chr, int connected);
870
871static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
872{
873 PtyCharDriver *s = chr->opaque;
874
875 if (!s->connected) {
876 /* guest sends data, check for (re-)connect */
877 pty_chr_update_read_handler(chr);
878 return 0;
879 }
880 return send_all(s->fd, buf, len);
881}
882
883static int pty_chr_read_poll(void *opaque)
884{
885 CharDriverState *chr = opaque;
886 PtyCharDriver *s = chr->opaque;
887
Anthony Liguori909cda12011-08-15 11:17:31 -0500888 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000889 return s->read_bytes;
890}
891
892static void pty_chr_read(void *opaque)
893{
894 CharDriverState *chr = opaque;
895 PtyCharDriver *s = chr->opaque;
896 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530897 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000898
899 len = sizeof(buf);
900 if (len > s->read_bytes)
901 len = s->read_bytes;
902 if (len == 0)
903 return;
904 size = read(s->fd, buf, len);
905 if ((size == -1 && errno == EIO) ||
906 (size == 0)) {
907 pty_chr_state(chr, 0);
908 return;
909 }
910 if (size > 0) {
911 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500912 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000913 }
914}
915
916static void pty_chr_update_read_handler(CharDriverState *chr)
917{
918 PtyCharDriver *s = chr->opaque;
919
920 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
921 pty_chr_read, NULL, chr);
922 s->polling = 1;
923 /*
924 * Short timeout here: just need wait long enougth that qemu makes
925 * it through the poll loop once. When reconnected we want a
926 * short timeout so we notice it almost instantly. Otherwise
927 * read() gives us -EIO instantly, making pty_chr_state() reset the
928 * timeout to the normal (much longer) poll interval before the
929 * timer triggers.
930 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100931 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +0000932}
933
934static void pty_chr_state(CharDriverState *chr, int connected)
935{
936 PtyCharDriver *s = chr->opaque;
937
938 if (!connected) {
939 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
940 s->connected = 0;
941 s->polling = 0;
942 /* (re-)connect poll interval for idle guests: once per second.
943 * We check more frequently in case the guests sends data to
944 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100945 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +0000946 } else {
947 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530948 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000949 s->connected = 1;
950 }
951}
952
953static void pty_chr_timer(void *opaque)
954{
955 struct CharDriverState *chr = opaque;
956 PtyCharDriver *s = chr->opaque;
957
958 if (s->connected)
959 return;
960 if (s->polling) {
961 /* If we arrive here without polling being cleared due
962 * read returning -EIO, then we are (re-)connected */
963 pty_chr_state(chr, 1);
964 return;
965 }
966
967 /* Next poll ... */
968 pty_chr_update_read_handler(chr);
969}
970
971static void pty_chr_close(struct CharDriverState *chr)
972{
973 PtyCharDriver *s = chr->opaque;
974
975 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
976 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000977 qemu_del_timer(s->timer);
978 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -0500979 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530980 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000981}
982
Kevin Wolf6e1db572011-06-01 13:29:11 +0200983static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000984{
985 CharDriverState *chr;
986 PtyCharDriver *s;
987 struct termios tty;
988 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000989#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000990 char pty_name[PATH_MAX];
991#define q_ptsname(x) pty_name
992#else
993 char *pty_name = NULL;
994#define q_ptsname(x) ptsname(x)
995#endif
996
Anthony Liguori7267c092011-08-20 22:09:37 -0500997 chr = g_malloc0(sizeof(CharDriverState));
998 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000999
1000 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Kevin Wolf6e1db572011-06-01 13:29:11 +02001001 return -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001002 }
1003
1004 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001005 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001006 cfmakeraw(&tty);
1007 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1008 close(slave_fd);
1009
1010 len = strlen(q_ptsname(s->fd)) + 5;
Anthony Liguori7267c092011-08-20 22:09:37 -05001011 chr->filename = g_malloc(len);
aliguori6f97dba2008-10-31 18:49:55 +00001012 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02001013 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +00001014 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
1015
1016 chr->opaque = s;
1017 chr->chr_write = pty_chr_write;
1018 chr->chr_update_read_handler = pty_chr_update_read_handler;
1019 chr->chr_close = pty_chr_close;
1020
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001021 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001022
Kevin Wolf6e1db572011-06-01 13:29:11 +02001023 *_chr = chr;
1024 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001025}
1026
1027static void tty_serial_init(int fd, int speed,
1028 int parity, int data_bits, int stop_bits)
1029{
1030 struct termios tty;
1031 speed_t spd;
1032
1033#if 0
1034 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1035 speed, parity, data_bits, stop_bits);
1036#endif
1037 tcgetattr (fd, &tty);
1038
Stefan Weil45eea132009-10-26 16:10:10 +01001039#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1040 speed = speed * 10 / 11;
1041 do {
1042 check_speed(50);
1043 check_speed(75);
1044 check_speed(110);
1045 check_speed(134);
1046 check_speed(150);
1047 check_speed(200);
1048 check_speed(300);
1049 check_speed(600);
1050 check_speed(1200);
1051 check_speed(1800);
1052 check_speed(2400);
1053 check_speed(4800);
1054 check_speed(9600);
1055 check_speed(19200);
1056 check_speed(38400);
1057 /* Non-Posix values follow. They may be unsupported on some systems. */
1058 check_speed(57600);
1059 check_speed(115200);
1060#ifdef B230400
1061 check_speed(230400);
1062#endif
1063#ifdef B460800
1064 check_speed(460800);
1065#endif
1066#ifdef B500000
1067 check_speed(500000);
1068#endif
1069#ifdef B576000
1070 check_speed(576000);
1071#endif
1072#ifdef B921600
1073 check_speed(921600);
1074#endif
1075#ifdef B1000000
1076 check_speed(1000000);
1077#endif
1078#ifdef B1152000
1079 check_speed(1152000);
1080#endif
1081#ifdef B1500000
1082 check_speed(1500000);
1083#endif
1084#ifdef B2000000
1085 check_speed(2000000);
1086#endif
1087#ifdef B2500000
1088 check_speed(2500000);
1089#endif
1090#ifdef B3000000
1091 check_speed(3000000);
1092#endif
1093#ifdef B3500000
1094 check_speed(3500000);
1095#endif
1096#ifdef B4000000
1097 check_speed(4000000);
1098#endif
aliguori6f97dba2008-10-31 18:49:55 +00001099 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001100 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001101
1102 cfsetispeed(&tty, spd);
1103 cfsetospeed(&tty, spd);
1104
1105 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1106 |INLCR|IGNCR|ICRNL|IXON);
1107 tty.c_oflag |= OPOST;
1108 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1109 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1110 switch(data_bits) {
1111 default:
1112 case 8:
1113 tty.c_cflag |= CS8;
1114 break;
1115 case 7:
1116 tty.c_cflag |= CS7;
1117 break;
1118 case 6:
1119 tty.c_cflag |= CS6;
1120 break;
1121 case 5:
1122 tty.c_cflag |= CS5;
1123 break;
1124 }
1125 switch(parity) {
1126 default:
1127 case 'N':
1128 break;
1129 case 'E':
1130 tty.c_cflag |= PARENB;
1131 break;
1132 case 'O':
1133 tty.c_cflag |= PARENB | PARODD;
1134 break;
1135 }
1136 if (stop_bits == 2)
1137 tty.c_cflag |= CSTOPB;
1138
1139 tcsetattr (fd, TCSANOW, &tty);
1140}
1141
1142static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1143{
1144 FDCharDriver *s = chr->opaque;
1145
1146 switch(cmd) {
1147 case CHR_IOCTL_SERIAL_SET_PARAMS:
1148 {
1149 QEMUSerialSetParams *ssp = arg;
1150 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1151 ssp->data_bits, ssp->stop_bits);
1152 }
1153 break;
1154 case CHR_IOCTL_SERIAL_SET_BREAK:
1155 {
1156 int enable = *(int *)arg;
1157 if (enable)
1158 tcsendbreak(s->fd_in, 1);
1159 }
1160 break;
1161 case CHR_IOCTL_SERIAL_GET_TIOCM:
1162 {
1163 int sarg = 0;
1164 int *targ = (int *)arg;
1165 ioctl(s->fd_in, TIOCMGET, &sarg);
1166 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001167 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001168 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001169 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001170 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001171 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001172 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001173 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001174 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001175 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001176 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001177 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001178 *targ |= CHR_TIOCM_RTS;
1179 }
1180 break;
1181 case CHR_IOCTL_SERIAL_SET_TIOCM:
1182 {
1183 int sarg = *(int *)arg;
1184 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001185 ioctl(s->fd_in, TIOCMGET, &targ);
1186 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1187 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1188 if (sarg & CHR_TIOCM_CTS)
1189 targ |= TIOCM_CTS;
1190 if (sarg & CHR_TIOCM_CAR)
1191 targ |= TIOCM_CAR;
1192 if (sarg & CHR_TIOCM_DSR)
1193 targ |= TIOCM_DSR;
1194 if (sarg & CHR_TIOCM_RI)
1195 targ |= TIOCM_RI;
1196 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001197 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001198 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001199 targ |= TIOCM_RTS;
1200 ioctl(s->fd_in, TIOCMSET, &targ);
1201 }
1202 break;
1203 default:
1204 return -ENOTSUP;
1205 }
1206 return 0;
1207}
1208
David Ahern4266a132010-02-10 18:27:17 -07001209static void qemu_chr_close_tty(CharDriverState *chr)
1210{
1211 FDCharDriver *s = chr->opaque;
1212 int fd = -1;
1213
1214 if (s) {
1215 fd = s->fd_in;
1216 }
1217
1218 fd_chr_close(chr);
1219
1220 if (fd >= 0) {
1221 close(fd);
1222 }
1223}
1224
Kevin Wolf6e1db572011-06-01 13:29:11 +02001225static int qemu_chr_open_tty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001226{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001227 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001228 CharDriverState *chr;
1229 int fd;
1230
Kevin Wolf6e1db572011-06-01 13:29:11 +02001231 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001232 if (fd < 0) {
Kevin Wolf6e1db572011-06-01 13:29:11 +02001233 return -errno;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001234 }
aliguori6f97dba2008-10-31 18:49:55 +00001235 tty_serial_init(fd, 115200, 'N', 8, 1);
1236 chr = qemu_chr_open_fd(fd, fd);
aliguori6f97dba2008-10-31 18:49:55 +00001237 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001238 chr->chr_close = qemu_chr_close_tty;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001239
1240 *_chr = chr;
1241 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001242}
1243#else /* ! __linux__ && ! __sun__ */
Kevin Wolf6e1db572011-06-01 13:29:11 +02001244static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001245{
Kevin Wolf6e1db572011-06-01 13:29:11 +02001246 return -ENOTSUP;
aliguori6f97dba2008-10-31 18:49:55 +00001247}
1248#endif /* __linux__ || __sun__ */
1249
1250#if defined(__linux__)
1251typedef struct {
1252 int fd;
1253 int mode;
1254} ParallelCharDriver;
1255
1256static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1257{
1258 if (s->mode != mode) {
1259 int m = mode;
1260 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1261 return 0;
1262 s->mode = mode;
1263 }
1264 return 1;
1265}
1266
1267static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1268{
1269 ParallelCharDriver *drv = chr->opaque;
1270 int fd = drv->fd;
1271 uint8_t b;
1272
1273 switch(cmd) {
1274 case CHR_IOCTL_PP_READ_DATA:
1275 if (ioctl(fd, PPRDATA, &b) < 0)
1276 return -ENOTSUP;
1277 *(uint8_t *)arg = b;
1278 break;
1279 case CHR_IOCTL_PP_WRITE_DATA:
1280 b = *(uint8_t *)arg;
1281 if (ioctl(fd, PPWDATA, &b) < 0)
1282 return -ENOTSUP;
1283 break;
1284 case CHR_IOCTL_PP_READ_CONTROL:
1285 if (ioctl(fd, PPRCONTROL, &b) < 0)
1286 return -ENOTSUP;
1287 /* Linux gives only the lowest bits, and no way to know data
1288 direction! For better compatibility set the fixed upper
1289 bits. */
1290 *(uint8_t *)arg = b | 0xc0;
1291 break;
1292 case CHR_IOCTL_PP_WRITE_CONTROL:
1293 b = *(uint8_t *)arg;
1294 if (ioctl(fd, PPWCONTROL, &b) < 0)
1295 return -ENOTSUP;
1296 break;
1297 case CHR_IOCTL_PP_READ_STATUS:
1298 if (ioctl(fd, PPRSTATUS, &b) < 0)
1299 return -ENOTSUP;
1300 *(uint8_t *)arg = b;
1301 break;
1302 case CHR_IOCTL_PP_DATA_DIR:
1303 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1304 return -ENOTSUP;
1305 break;
1306 case CHR_IOCTL_PP_EPP_READ_ADDR:
1307 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1308 struct ParallelIOArg *parg = arg;
1309 int n = read(fd, parg->buffer, parg->count);
1310 if (n != parg->count) {
1311 return -EIO;
1312 }
1313 }
1314 break;
1315 case CHR_IOCTL_PP_EPP_READ:
1316 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1317 struct ParallelIOArg *parg = arg;
1318 int n = read(fd, parg->buffer, parg->count);
1319 if (n != parg->count) {
1320 return -EIO;
1321 }
1322 }
1323 break;
1324 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1325 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1326 struct ParallelIOArg *parg = arg;
1327 int n = write(fd, parg->buffer, parg->count);
1328 if (n != parg->count) {
1329 return -EIO;
1330 }
1331 }
1332 break;
1333 case CHR_IOCTL_PP_EPP_WRITE:
1334 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1335 struct ParallelIOArg *parg = arg;
1336 int n = write(fd, parg->buffer, parg->count);
1337 if (n != parg->count) {
1338 return -EIO;
1339 }
1340 }
1341 break;
1342 default:
1343 return -ENOTSUP;
1344 }
1345 return 0;
1346}
1347
1348static void pp_close(CharDriverState *chr)
1349{
1350 ParallelCharDriver *drv = chr->opaque;
1351 int fd = drv->fd;
1352
1353 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1354 ioctl(fd, PPRELEASE);
1355 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001356 g_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301357 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001358}
1359
Kevin Wolf6e1db572011-06-01 13:29:11 +02001360static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001361{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001362 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001363 CharDriverState *chr;
1364 ParallelCharDriver *drv;
1365 int fd;
1366
1367 TFR(fd = open(filename, O_RDWR));
Kevin Wolf6e1db572011-06-01 13:29:11 +02001368 if (fd < 0) {
1369 return -errno;
1370 }
aliguori6f97dba2008-10-31 18:49:55 +00001371
1372 if (ioctl(fd, PPCLAIM) < 0) {
1373 close(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001374 return -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001375 }
1376
Anthony Liguori7267c092011-08-20 22:09:37 -05001377 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001378 drv->fd = fd;
1379 drv->mode = IEEE1284_MODE_COMPAT;
1380
Anthony Liguori7267c092011-08-20 22:09:37 -05001381 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001382 chr->chr_write = null_chr_write;
1383 chr->chr_ioctl = pp_ioctl;
1384 chr->chr_close = pp_close;
1385 chr->opaque = drv;
1386
Amit Shah127338e2009-11-03 19:59:56 +05301387 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001388
Kevin Wolf6e1db572011-06-01 13:29:11 +02001389 *_chr = chr;
1390 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001391}
1392#endif /* __linux__ */
1393
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001394#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001395static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1396{
Stefan Weile0efb992011-02-23 19:09:16 +01001397 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001398 uint8_t b;
1399
1400 switch(cmd) {
1401 case CHR_IOCTL_PP_READ_DATA:
1402 if (ioctl(fd, PPIGDATA, &b) < 0)
1403 return -ENOTSUP;
1404 *(uint8_t *)arg = b;
1405 break;
1406 case CHR_IOCTL_PP_WRITE_DATA:
1407 b = *(uint8_t *)arg;
1408 if (ioctl(fd, PPISDATA, &b) < 0)
1409 return -ENOTSUP;
1410 break;
1411 case CHR_IOCTL_PP_READ_CONTROL:
1412 if (ioctl(fd, PPIGCTRL, &b) < 0)
1413 return -ENOTSUP;
1414 *(uint8_t *)arg = b;
1415 break;
1416 case CHR_IOCTL_PP_WRITE_CONTROL:
1417 b = *(uint8_t *)arg;
1418 if (ioctl(fd, PPISCTRL, &b) < 0)
1419 return -ENOTSUP;
1420 break;
1421 case CHR_IOCTL_PP_READ_STATUS:
1422 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1423 return -ENOTSUP;
1424 *(uint8_t *)arg = b;
1425 break;
1426 default:
1427 return -ENOTSUP;
1428 }
1429 return 0;
1430}
1431
Kevin Wolf6e1db572011-06-01 13:29:11 +02001432static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
blueswir16972f932008-11-22 20:49:12 +00001433{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001434 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001435 CharDriverState *chr;
1436 int fd;
1437
Kevin Wolf6e1db572011-06-01 13:29:11 +02001438 fd = qemu_open(filename, O_RDWR);
1439 if (fd < 0) {
1440 return -errno;
1441 }
blueswir16972f932008-11-22 20:49:12 +00001442
Anthony Liguori7267c092011-08-20 22:09:37 -05001443 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001444 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001445 chr->chr_write = null_chr_write;
1446 chr->chr_ioctl = pp_ioctl;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001447
1448 *_chr = chr;
1449 return 0;
blueswir16972f932008-11-22 20:49:12 +00001450}
1451#endif
1452
aliguori6f97dba2008-10-31 18:49:55 +00001453#else /* _WIN32 */
1454
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001455static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1456
aliguori6f97dba2008-10-31 18:49:55 +00001457typedef struct {
1458 int max_size;
1459 HANDLE hcom, hrecv, hsend;
1460 OVERLAPPED orecv, osend;
1461 BOOL fpipe;
1462 DWORD len;
1463} WinCharState;
1464
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001465typedef struct {
1466 HANDLE hStdIn;
1467 HANDLE hInputReadyEvent;
1468 HANDLE hInputDoneEvent;
1469 HANDLE hInputThread;
1470 uint8_t win_stdio_buf;
1471} WinStdioCharState;
1472
aliguori6f97dba2008-10-31 18:49:55 +00001473#define NSENDBUF 2048
1474#define NRECVBUF 2048
1475#define MAXCONNECT 1
1476#define NTIMEOUT 5000
1477
1478static int win_chr_poll(void *opaque);
1479static int win_chr_pipe_poll(void *opaque);
1480
1481static void win_chr_close(CharDriverState *chr)
1482{
1483 WinCharState *s = chr->opaque;
1484
1485 if (s->hsend) {
1486 CloseHandle(s->hsend);
1487 s->hsend = NULL;
1488 }
1489 if (s->hrecv) {
1490 CloseHandle(s->hrecv);
1491 s->hrecv = NULL;
1492 }
1493 if (s->hcom) {
1494 CloseHandle(s->hcom);
1495 s->hcom = NULL;
1496 }
1497 if (s->fpipe)
1498 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1499 else
1500 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301501
1502 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001503}
1504
1505static int win_chr_init(CharDriverState *chr, const char *filename)
1506{
1507 WinCharState *s = chr->opaque;
1508 COMMCONFIG comcfg;
1509 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1510 COMSTAT comstat;
1511 DWORD size;
1512 DWORD err;
1513
1514 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1515 if (!s->hsend) {
1516 fprintf(stderr, "Failed CreateEvent\n");
1517 goto fail;
1518 }
1519 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1520 if (!s->hrecv) {
1521 fprintf(stderr, "Failed CreateEvent\n");
1522 goto fail;
1523 }
1524
1525 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1526 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1527 if (s->hcom == INVALID_HANDLE_VALUE) {
1528 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1529 s->hcom = NULL;
1530 goto fail;
1531 }
1532
1533 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1534 fprintf(stderr, "Failed SetupComm\n");
1535 goto fail;
1536 }
1537
1538 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1539 size = sizeof(COMMCONFIG);
1540 GetDefaultCommConfig(filename, &comcfg, &size);
1541 comcfg.dcb.DCBlength = sizeof(DCB);
1542 CommConfigDialog(filename, NULL, &comcfg);
1543
1544 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1545 fprintf(stderr, "Failed SetCommState\n");
1546 goto fail;
1547 }
1548
1549 if (!SetCommMask(s->hcom, EV_ERR)) {
1550 fprintf(stderr, "Failed SetCommMask\n");
1551 goto fail;
1552 }
1553
1554 cto.ReadIntervalTimeout = MAXDWORD;
1555 if (!SetCommTimeouts(s->hcom, &cto)) {
1556 fprintf(stderr, "Failed SetCommTimeouts\n");
1557 goto fail;
1558 }
1559
1560 if (!ClearCommError(s->hcom, &err, &comstat)) {
1561 fprintf(stderr, "Failed ClearCommError\n");
1562 goto fail;
1563 }
1564 qemu_add_polling_cb(win_chr_poll, chr);
1565 return 0;
1566
1567 fail:
1568 win_chr_close(chr);
1569 return -1;
1570}
1571
1572static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1573{
1574 WinCharState *s = chr->opaque;
1575 DWORD len, ret, size, err;
1576
1577 len = len1;
1578 ZeroMemory(&s->osend, sizeof(s->osend));
1579 s->osend.hEvent = s->hsend;
1580 while (len > 0) {
1581 if (s->hsend)
1582 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1583 else
1584 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1585 if (!ret) {
1586 err = GetLastError();
1587 if (err == ERROR_IO_PENDING) {
1588 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1589 if (ret) {
1590 buf += size;
1591 len -= size;
1592 } else {
1593 break;
1594 }
1595 } else {
1596 break;
1597 }
1598 } else {
1599 buf += size;
1600 len -= size;
1601 }
1602 }
1603 return len1 - len;
1604}
1605
1606static int win_chr_read_poll(CharDriverState *chr)
1607{
1608 WinCharState *s = chr->opaque;
1609
Anthony Liguori909cda12011-08-15 11:17:31 -05001610 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001611 return s->max_size;
1612}
1613
1614static void win_chr_readfile(CharDriverState *chr)
1615{
1616 WinCharState *s = chr->opaque;
1617 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301618 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001619 DWORD size;
1620
1621 ZeroMemory(&s->orecv, sizeof(s->orecv));
1622 s->orecv.hEvent = s->hrecv;
1623 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1624 if (!ret) {
1625 err = GetLastError();
1626 if (err == ERROR_IO_PENDING) {
1627 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1628 }
1629 }
1630
1631 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001632 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001633 }
1634}
1635
1636static void win_chr_read(CharDriverState *chr)
1637{
1638 WinCharState *s = chr->opaque;
1639
1640 if (s->len > s->max_size)
1641 s->len = s->max_size;
1642 if (s->len == 0)
1643 return;
1644
1645 win_chr_readfile(chr);
1646}
1647
1648static int win_chr_poll(void *opaque)
1649{
1650 CharDriverState *chr = opaque;
1651 WinCharState *s = chr->opaque;
1652 COMSTAT status;
1653 DWORD comerr;
1654
1655 ClearCommError(s->hcom, &comerr, &status);
1656 if (status.cbInQue > 0) {
1657 s->len = status.cbInQue;
1658 win_chr_read_poll(chr);
1659 win_chr_read(chr);
1660 return 1;
1661 }
1662 return 0;
1663}
1664
Kevin Wolf6e1db572011-06-01 13:29:11 +02001665static int qemu_chr_open_win(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001666{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001667 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001668 CharDriverState *chr;
1669 WinCharState *s;
1670
Anthony Liguori7267c092011-08-20 22:09:37 -05001671 chr = g_malloc0(sizeof(CharDriverState));
1672 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001673 chr->opaque = s;
1674 chr->chr_write = win_chr_write;
1675 chr->chr_close = win_chr_close;
1676
1677 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001678 g_free(s);
1679 g_free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001680 return -EIO;
aliguori6f97dba2008-10-31 18:49:55 +00001681 }
Amit Shah127338e2009-11-03 19:59:56 +05301682 qemu_chr_generic_open(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001683
1684 *_chr = chr;
1685 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001686}
1687
1688static int win_chr_pipe_poll(void *opaque)
1689{
1690 CharDriverState *chr = opaque;
1691 WinCharState *s = chr->opaque;
1692 DWORD size;
1693
1694 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1695 if (size > 0) {
1696 s->len = size;
1697 win_chr_read_poll(chr);
1698 win_chr_read(chr);
1699 return 1;
1700 }
1701 return 0;
1702}
1703
1704static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1705{
1706 WinCharState *s = chr->opaque;
1707 OVERLAPPED ov;
1708 int ret;
1709 DWORD size;
1710 char openname[256];
1711
1712 s->fpipe = TRUE;
1713
1714 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1715 if (!s->hsend) {
1716 fprintf(stderr, "Failed CreateEvent\n");
1717 goto fail;
1718 }
1719 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1720 if (!s->hrecv) {
1721 fprintf(stderr, "Failed CreateEvent\n");
1722 goto fail;
1723 }
1724
1725 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1726 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1727 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1728 PIPE_WAIT,
1729 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1730 if (s->hcom == INVALID_HANDLE_VALUE) {
1731 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1732 s->hcom = NULL;
1733 goto fail;
1734 }
1735
1736 ZeroMemory(&ov, sizeof(ov));
1737 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1738 ret = ConnectNamedPipe(s->hcom, &ov);
1739 if (ret) {
1740 fprintf(stderr, "Failed ConnectNamedPipe\n");
1741 goto fail;
1742 }
1743
1744 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1745 if (!ret) {
1746 fprintf(stderr, "Failed GetOverlappedResult\n");
1747 if (ov.hEvent) {
1748 CloseHandle(ov.hEvent);
1749 ov.hEvent = NULL;
1750 }
1751 goto fail;
1752 }
1753
1754 if (ov.hEvent) {
1755 CloseHandle(ov.hEvent);
1756 ov.hEvent = NULL;
1757 }
1758 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1759 return 0;
1760
1761 fail:
1762 win_chr_close(chr);
1763 return -1;
1764}
1765
1766
Kevin Wolf6e1db572011-06-01 13:29:11 +02001767static int qemu_chr_open_win_pipe(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001768{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001769 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001770 CharDriverState *chr;
1771 WinCharState *s;
1772
Anthony Liguori7267c092011-08-20 22:09:37 -05001773 chr = g_malloc0(sizeof(CharDriverState));
1774 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001775 chr->opaque = s;
1776 chr->chr_write = win_chr_write;
1777 chr->chr_close = win_chr_close;
1778
1779 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001780 g_free(s);
1781 g_free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001782 return -EIO;
aliguori6f97dba2008-10-31 18:49:55 +00001783 }
Amit Shah127338e2009-11-03 19:59:56 +05301784 qemu_chr_generic_open(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001785
1786 *_chr = chr;
1787 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001788}
1789
Blue Swirlaad04cd2011-07-23 19:26:08 +00001790static int qemu_chr_open_win_file(HANDLE fd_out, CharDriverState **pchr)
aliguori6f97dba2008-10-31 18:49:55 +00001791{
1792 CharDriverState *chr;
1793 WinCharState *s;
1794
Anthony Liguori7267c092011-08-20 22:09:37 -05001795 chr = g_malloc0(sizeof(CharDriverState));
1796 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001797 s->hcom = fd_out;
1798 chr->opaque = s;
1799 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301800 qemu_chr_generic_open(chr);
Blue Swirlaad04cd2011-07-23 19:26:08 +00001801 *pchr = chr;
1802 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001803}
1804
Blue Swirlaad04cd2011-07-23 19:26:08 +00001805static int qemu_chr_open_win_con(QemuOpts *opts, CharDriverState **chr)
aliguori6f97dba2008-10-31 18:49:55 +00001806{
Kevin Wolf6e1db572011-06-01 13:29:11 +02001807 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE), chr);
aliguori6f97dba2008-10-31 18:49:55 +00001808}
1809
Kevin Wolf6e1db572011-06-01 13:29:11 +02001810static int qemu_chr_open_win_file_out(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001811{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001812 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001813 HANDLE fd_out;
1814
1815 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1816 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001817 if (fd_out == INVALID_HANDLE_VALUE) {
1818 return -EIO;
1819 }
aliguori6f97dba2008-10-31 18:49:55 +00001820
Kevin Wolf6e1db572011-06-01 13:29:11 +02001821 return qemu_chr_open_win_file(fd_out, _chr);
aliguori6f97dba2008-10-31 18:49:55 +00001822}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001823
1824static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1825{
1826 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1827 DWORD dwSize;
1828 int len1;
1829
1830 len1 = len;
1831
1832 while (len1 > 0) {
1833 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1834 break;
1835 }
1836 buf += dwSize;
1837 len1 -= dwSize;
1838 }
1839
1840 return len - len1;
1841}
1842
1843static void win_stdio_wait_func(void *opaque)
1844{
1845 CharDriverState *chr = opaque;
1846 WinStdioCharState *stdio = chr->opaque;
1847 INPUT_RECORD buf[4];
1848 int ret;
1849 DWORD dwSize;
1850 int i;
1851
1852 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1853 &dwSize);
1854
1855 if (!ret) {
1856 /* Avoid error storm */
1857 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1858 return;
1859 }
1860
1861 for (i = 0; i < dwSize; i++) {
1862 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1863
1864 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1865 int j;
1866 if (kev->uChar.AsciiChar != 0) {
1867 for (j = 0; j < kev->wRepeatCount; j++) {
1868 if (qemu_chr_be_can_write(chr)) {
1869 uint8_t c = kev->uChar.AsciiChar;
1870 qemu_chr_be_write(chr, &c, 1);
1871 }
1872 }
1873 }
1874 }
1875 }
1876}
1877
1878static DWORD WINAPI win_stdio_thread(LPVOID param)
1879{
1880 CharDriverState *chr = param;
1881 WinStdioCharState *stdio = chr->opaque;
1882 int ret;
1883 DWORD dwSize;
1884
1885 while (1) {
1886
1887 /* Wait for one byte */
1888 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1889
1890 /* Exit in case of error, continue if nothing read */
1891 if (!ret) {
1892 break;
1893 }
1894 if (!dwSize) {
1895 continue;
1896 }
1897
1898 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1899 if (stdio->win_stdio_buf == '\r') {
1900 continue;
1901 }
1902
1903 /* Signal the main thread and wait until the byte was eaten */
1904 if (!SetEvent(stdio->hInputReadyEvent)) {
1905 break;
1906 }
1907 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1908 != WAIT_OBJECT_0) {
1909 break;
1910 }
1911 }
1912
1913 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
1914 return 0;
1915}
1916
1917static void win_stdio_thread_wait_func(void *opaque)
1918{
1919 CharDriverState *chr = opaque;
1920 WinStdioCharState *stdio = chr->opaque;
1921
1922 if (qemu_chr_be_can_write(chr)) {
1923 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
1924 }
1925
1926 SetEvent(stdio->hInputDoneEvent);
1927}
1928
1929static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
1930{
1931 WinStdioCharState *stdio = chr->opaque;
1932 DWORD dwMode = 0;
1933
1934 GetConsoleMode(stdio->hStdIn, &dwMode);
1935
1936 if (echo) {
1937 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
1938 } else {
1939 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
1940 }
1941}
1942
1943static void win_stdio_close(CharDriverState *chr)
1944{
1945 WinStdioCharState *stdio = chr->opaque;
1946
1947 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
1948 CloseHandle(stdio->hInputReadyEvent);
1949 }
1950 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
1951 CloseHandle(stdio->hInputDoneEvent);
1952 }
1953 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
1954 TerminateThread(stdio->hInputThread, 0);
1955 }
1956
1957 g_free(chr->opaque);
1958 g_free(chr);
1959 stdio_nb_clients--;
1960}
1961
1962static int qemu_chr_open_win_stdio(QemuOpts *opts, CharDriverState **_chr)
1963{
1964 CharDriverState *chr;
1965 WinStdioCharState *stdio;
1966 DWORD dwMode;
1967 int is_console = 0;
1968
1969 if (stdio_nb_clients >= STDIO_MAX_CLIENTS
1970 || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
1971 return -EIO;
1972 }
1973
1974 chr = g_malloc0(sizeof(CharDriverState));
1975 stdio = g_malloc0(sizeof(WinStdioCharState));
1976
1977 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1978 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
1979 fprintf(stderr, "cannot open stdio: invalid handle\n");
1980 exit(1);
1981 }
1982
1983 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
1984
1985 chr->opaque = stdio;
1986 chr->chr_write = win_stdio_write;
1987 chr->chr_close = win_stdio_close;
1988
1989 if (stdio_nb_clients == 0) {
1990 if (is_console) {
1991 if (qemu_add_wait_object(stdio->hStdIn,
1992 win_stdio_wait_func, chr)) {
1993 fprintf(stderr, "qemu_add_wait_object: failed\n");
1994 }
1995 } else {
1996 DWORD dwId;
1997
1998 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1999 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2000 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2001 chr, 0, &dwId);
2002
2003 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2004 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2005 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2006 fprintf(stderr, "cannot create stdio thread or event\n");
2007 exit(1);
2008 }
2009 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2010 win_stdio_thread_wait_func, chr)) {
2011 fprintf(stderr, "qemu_add_wait_object: failed\n");
2012 }
2013 }
2014 }
2015
2016 dwMode |= ENABLE_LINE_INPUT;
2017
2018 stdio_clients[stdio_nb_clients++] = chr;
2019 if (stdio_nb_clients == 1 && is_console) {
2020 /* set the terminal in raw mode */
2021 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2022 dwMode |= ENABLE_PROCESSED_INPUT;
2023 }
2024
2025 SetConsoleMode(stdio->hStdIn, dwMode);
2026
2027 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2028 qemu_chr_fe_set_echo(chr, false);
2029
2030 *_chr = chr;
2031
2032 return 0;
2033}
aliguori6f97dba2008-10-31 18:49:55 +00002034#endif /* !_WIN32 */
2035
2036/***********************************************************/
2037/* UDP Net console */
2038
2039typedef struct {
2040 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05302041 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002042 int bufcnt;
2043 int bufptr;
2044 int max_size;
2045} NetCharDriver;
2046
2047static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2048{
2049 NetCharDriver *s = chr->opaque;
2050
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002051 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00002052}
2053
2054static int udp_chr_read_poll(void *opaque)
2055{
2056 CharDriverState *chr = opaque;
2057 NetCharDriver *s = chr->opaque;
2058
Anthony Liguori909cda12011-08-15 11:17:31 -05002059 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002060
2061 /* If there were any stray characters in the queue process them
2062 * first
2063 */
2064 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002065 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002066 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002067 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002068 }
2069 return s->max_size;
2070}
2071
2072static void udp_chr_read(void *opaque)
2073{
2074 CharDriverState *chr = opaque;
2075 NetCharDriver *s = chr->opaque;
2076
2077 if (s->max_size == 0)
2078 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00002079 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00002080 s->bufptr = s->bufcnt;
2081 if (s->bufcnt <= 0)
2082 return;
2083
2084 s->bufptr = 0;
2085 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002086 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002087 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002088 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002089 }
2090}
2091
2092static void udp_chr_update_read_handler(CharDriverState *chr)
2093{
2094 NetCharDriver *s = chr->opaque;
2095
2096 if (s->fd >= 0) {
2097 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2098 udp_chr_read, NULL, chr);
2099 }
2100}
2101
aliguori819f56b2009-03-28 17:58:14 +00002102static void udp_chr_close(CharDriverState *chr)
2103{
2104 NetCharDriver *s = chr->opaque;
2105 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002106 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00002107 closesocket(s->fd);
2108 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002109 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302110 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002111}
2112
Kevin Wolf6e1db572011-06-01 13:29:11 +02002113static int qemu_chr_open_udp(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00002114{
2115 CharDriverState *chr = NULL;
2116 NetCharDriver *s = NULL;
2117 int fd = -1;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002118 int ret;
aliguori6f97dba2008-10-31 18:49:55 +00002119
Anthony Liguori7267c092011-08-20 22:09:37 -05002120 chr = g_malloc0(sizeof(CharDriverState));
2121 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002122
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002123 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002124 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002125 fprintf(stderr, "inet_dgram_opts failed\n");
Kevin Wolf6e1db572011-06-01 13:29:11 +02002126 ret = -errno;
aliguori6f97dba2008-10-31 18:49:55 +00002127 goto return_err;
2128 }
2129
2130 s->fd = fd;
2131 s->bufcnt = 0;
2132 s->bufptr = 0;
2133 chr->opaque = s;
2134 chr->chr_write = udp_chr_write;
2135 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002136 chr->chr_close = udp_chr_close;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002137
2138 *_chr = chr;
2139 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00002140
2141return_err:
Anthony Liguori7267c092011-08-20 22:09:37 -05002142 g_free(chr);
2143 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002144 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002145 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002146 }
2147 return ret;
aliguori6f97dba2008-10-31 18:49:55 +00002148}
2149
2150/***********************************************************/
2151/* TCP Net console */
2152
2153typedef struct {
2154 int fd, listen_fd;
2155 int connected;
2156 int max_size;
2157 int do_telnetopt;
2158 int do_nodelay;
2159 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002160 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002161} TCPCharDriver;
2162
2163static void tcp_chr_accept(void *opaque);
2164
2165static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2166{
2167 TCPCharDriver *s = chr->opaque;
2168 if (s->connected) {
2169 return send_all(s->fd, buf, len);
2170 } else {
2171 /* XXX: indicate an error ? */
2172 return len;
2173 }
2174}
2175
2176static int tcp_chr_read_poll(void *opaque)
2177{
2178 CharDriverState *chr = opaque;
2179 TCPCharDriver *s = chr->opaque;
2180 if (!s->connected)
2181 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002182 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002183 return s->max_size;
2184}
2185
2186#define IAC 255
2187#define IAC_BREAK 243
2188static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2189 TCPCharDriver *s,
2190 uint8_t *buf, int *size)
2191{
2192 /* Handle any telnet client's basic IAC options to satisfy char by
2193 * char mode with no echo. All IAC options will be removed from
2194 * the buf and the do_telnetopt variable will be used to track the
2195 * state of the width of the IAC information.
2196 *
2197 * IAC commands come in sets of 3 bytes with the exception of the
2198 * "IAC BREAK" command and the double IAC.
2199 */
2200
2201 int i;
2202 int j = 0;
2203
2204 for (i = 0; i < *size; i++) {
2205 if (s->do_telnetopt > 1) {
2206 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2207 /* Double IAC means send an IAC */
2208 if (j != i)
2209 buf[j] = buf[i];
2210 j++;
2211 s->do_telnetopt = 1;
2212 } else {
2213 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2214 /* Handle IAC break commands by sending a serial break */
2215 qemu_chr_event(chr, CHR_EVENT_BREAK);
2216 s->do_telnetopt++;
2217 }
2218 s->do_telnetopt++;
2219 }
2220 if (s->do_telnetopt >= 4) {
2221 s->do_telnetopt = 1;
2222 }
2223 } else {
2224 if ((unsigned char)buf[i] == IAC) {
2225 s->do_telnetopt = 2;
2226 } else {
2227 if (j != i)
2228 buf[j] = buf[i];
2229 j++;
2230 }
2231 }
2232 }
2233 *size = j;
2234}
2235
Mark McLoughlin7d174052009-07-22 09:11:39 +01002236static int tcp_get_msgfd(CharDriverState *chr)
2237{
2238 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002239 int fd = s->msgfd;
2240 s->msgfd = -1;
2241 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002242}
2243
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002244#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002245static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2246{
2247 TCPCharDriver *s = chr->opaque;
2248 struct cmsghdr *cmsg;
2249
2250 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2251 int fd;
2252
2253 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2254 cmsg->cmsg_level != SOL_SOCKET ||
2255 cmsg->cmsg_type != SCM_RIGHTS)
2256 continue;
2257
2258 fd = *((int *)CMSG_DATA(cmsg));
2259 if (fd < 0)
2260 continue;
2261
2262 if (s->msgfd != -1)
2263 close(s->msgfd);
2264 s->msgfd = fd;
2265 }
2266}
2267
Mark McLoughlin9977c892009-07-22 09:11:38 +01002268static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2269{
2270 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002271 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002272 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002273 union {
2274 struct cmsghdr cmsg;
2275 char control[CMSG_SPACE(sizeof(int))];
2276 } msg_control;
2277 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002278
2279 iov[0].iov_base = buf;
2280 iov[0].iov_len = len;
2281
2282 msg.msg_iov = iov;
2283 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002284 msg.msg_control = &msg_control;
2285 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002286
Mark McLoughlin7d174052009-07-22 09:11:39 +01002287 ret = recvmsg(s->fd, &msg, 0);
2288 if (ret > 0 && s->is_unix)
2289 unix_process_msgfd(chr, &msg);
2290
2291 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002292}
2293#else
2294static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2295{
2296 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002297 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002298}
2299#endif
2300
aliguori6f97dba2008-10-31 18:49:55 +00002301static void tcp_chr_read(void *opaque)
2302{
2303 CharDriverState *chr = opaque;
2304 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302305 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002306 int len, size;
2307
2308 if (!s->connected || s->max_size <= 0)
2309 return;
2310 len = sizeof(buf);
2311 if (len > s->max_size)
2312 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002313 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002314 if (size == 0) {
2315 /* connection closed */
2316 s->connected = 0;
2317 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002318 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002319 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002320 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002321 closesocket(s->fd);
2322 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302323 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002324 } else if (size > 0) {
2325 if (s->do_telnetopt)
2326 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2327 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002328 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002329 }
2330}
2331
Blue Swirl68c18d12010-08-15 09:46:24 +00002332#ifndef _WIN32
2333CharDriverState *qemu_chr_open_eventfd(int eventfd)
2334{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002335 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002336}
Blue Swirl68c18d12010-08-15 09:46:24 +00002337#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002338
aliguori6f97dba2008-10-31 18:49:55 +00002339static void tcp_chr_connect(void *opaque)
2340{
2341 CharDriverState *chr = opaque;
2342 TCPCharDriver *s = chr->opaque;
2343
2344 s->connected = 1;
2345 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2346 tcp_chr_read, NULL, chr);
Amit Shah127338e2009-11-03 19:59:56 +05302347 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002348}
2349
2350#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2351static void tcp_chr_telnet_init(int fd)
2352{
2353 char buf[3];
2354 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2355 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2356 send(fd, (char *)buf, 3, 0);
2357 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2358 send(fd, (char *)buf, 3, 0);
2359 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2360 send(fd, (char *)buf, 3, 0);
2361 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2362 send(fd, (char *)buf, 3, 0);
2363}
2364
2365static void socket_set_nodelay(int fd)
2366{
2367 int val = 1;
2368 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2369}
2370
Daniel P. Berrange13661082011-06-23 13:31:42 +01002371static int tcp_chr_add_client(CharDriverState *chr, int fd)
2372{
2373 TCPCharDriver *s = chr->opaque;
2374 if (s->fd != -1)
2375 return -1;
2376
2377 socket_set_nonblock(fd);
2378 if (s->do_nodelay)
2379 socket_set_nodelay(fd);
2380 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002381 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002382 tcp_chr_connect(chr);
2383
2384 return 0;
2385}
2386
aliguori6f97dba2008-10-31 18:49:55 +00002387static void tcp_chr_accept(void *opaque)
2388{
2389 CharDriverState *chr = opaque;
2390 TCPCharDriver *s = chr->opaque;
2391 struct sockaddr_in saddr;
2392#ifndef _WIN32
2393 struct sockaddr_un uaddr;
2394#endif
2395 struct sockaddr *addr;
2396 socklen_t len;
2397 int fd;
2398
2399 for(;;) {
2400#ifndef _WIN32
2401 if (s->is_unix) {
2402 len = sizeof(uaddr);
2403 addr = (struct sockaddr *)&uaddr;
2404 } else
2405#endif
2406 {
2407 len = sizeof(saddr);
2408 addr = (struct sockaddr *)&saddr;
2409 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002410 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002411 if (fd < 0 && errno != EINTR) {
2412 return;
2413 } else if (fd >= 0) {
2414 if (s->do_telnetopt)
2415 tcp_chr_telnet_init(fd);
2416 break;
2417 }
2418 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002419 if (tcp_chr_add_client(chr, fd) < 0)
2420 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002421}
2422
2423static void tcp_chr_close(CharDriverState *chr)
2424{
2425 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002426 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002427 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002428 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002429 }
2430 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002431 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002432 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002433 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002434 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302435 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002436}
2437
Kevin Wolf6e1db572011-06-01 13:29:11 +02002438static int qemu_chr_open_socket(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00002439{
2440 CharDriverState *chr = NULL;
2441 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002442 int fd = -1;
2443 int is_listen;
2444 int is_waitconnect;
2445 int do_nodelay;
2446 int is_unix;
2447 int is_telnet;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002448 int ret;
aliguori6f97dba2008-10-31 18:49:55 +00002449
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002450 is_listen = qemu_opt_get_bool(opts, "server", 0);
2451 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2452 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2453 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2454 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002455 if (!is_listen)
2456 is_waitconnect = 0;
2457
Anthony Liguori7267c092011-08-20 22:09:37 -05002458 chr = g_malloc0(sizeof(CharDriverState));
2459 s = g_malloc0(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002460
aliguorif07b6002008-11-11 20:54:09 +00002461 if (is_unix) {
2462 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002463 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002464 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002465 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002466 }
2467 } else {
2468 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002469 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002470 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002471 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002472 }
2473 }
Kevin Wolf6e1db572011-06-01 13:29:11 +02002474 if (fd < 0) {
2475 ret = -errno;
aliguori6f97dba2008-10-31 18:49:55 +00002476 goto fail;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002477 }
aliguori6f97dba2008-10-31 18:49:55 +00002478
2479 if (!is_waitconnect)
2480 socket_set_nonblock(fd);
2481
2482 s->connected = 0;
2483 s->fd = -1;
2484 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002485 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002486 s->is_unix = is_unix;
2487 s->do_nodelay = do_nodelay && !is_unix;
2488
2489 chr->opaque = s;
2490 chr->chr_write = tcp_chr_write;
2491 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002492 chr->get_msgfd = tcp_get_msgfd;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002493 chr->chr_add_client = tcp_chr_add_client;
aliguori6f97dba2008-10-31 18:49:55 +00002494
2495 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002496 s->listen_fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002497 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002498 if (is_telnet)
2499 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002500
aliguori6f97dba2008-10-31 18:49:55 +00002501 } else {
aliguorif07b6002008-11-11 20:54:09 +00002502 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002503 s->fd = fd;
2504 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002505 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002506 }
2507
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002508 /* for "info chardev" monitor command */
Anthony Liguori7267c092011-08-20 22:09:37 -05002509 chr->filename = g_malloc(256);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002510 if (is_unix) {
2511 snprintf(chr->filename, 256, "unix:%s%s",
2512 qemu_opt_get(opts, "path"),
2513 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2514 } else if (is_telnet) {
2515 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2516 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2517 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2518 } else {
2519 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2520 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2521 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2522 }
2523
aliguori6f97dba2008-10-31 18:49:55 +00002524 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002525 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002526 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002527 tcp_chr_accept(chr);
2528 socket_set_nonblock(s->listen_fd);
2529 }
Kevin Wolf6e1db572011-06-01 13:29:11 +02002530
2531 *_chr = chr;
2532 return 0;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002533
aliguori6f97dba2008-10-31 18:49:55 +00002534 fail:
2535 if (fd >= 0)
2536 closesocket(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002537 g_free(s);
2538 g_free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002539 return ret;
aliguori6f97dba2008-10-31 18:49:55 +00002540}
2541
Luiz Capitulino999bd672010-10-22 16:09:05 -02002542/***********************************************************/
2543/* Memory chardev */
2544typedef struct {
2545 size_t outbuf_size;
2546 size_t outbuf_capacity;
2547 uint8_t *outbuf;
2548} MemoryDriver;
2549
2550static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2551{
2552 MemoryDriver *d = chr->opaque;
2553
2554 /* TODO: the QString implementation has the same code, we should
2555 * introduce a generic way to do this in cutils.c */
2556 if (d->outbuf_capacity < d->outbuf_size + len) {
2557 /* grow outbuf */
2558 d->outbuf_capacity += len;
2559 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002560 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002561 }
2562
2563 memcpy(d->outbuf + d->outbuf_size, buf, len);
2564 d->outbuf_size += len;
2565
2566 return len;
2567}
2568
2569void qemu_chr_init_mem(CharDriverState *chr)
2570{
2571 MemoryDriver *d;
2572
Anthony Liguori7267c092011-08-20 22:09:37 -05002573 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002574 d->outbuf_size = 0;
2575 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002576 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002577
2578 memset(chr, 0, sizeof(*chr));
2579 chr->opaque = d;
2580 chr->chr_write = mem_chr_write;
2581}
2582
2583QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2584{
2585 MemoryDriver *d = chr->opaque;
2586 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2587}
2588
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002589/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002590void qemu_chr_close_mem(CharDriverState *chr)
2591{
2592 MemoryDriver *d = chr->opaque;
2593
Anthony Liguori7267c092011-08-20 22:09:37 -05002594 g_free(d->outbuf);
2595 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002596 chr->opaque = NULL;
2597 chr->chr_write = NULL;
2598}
2599
2600size_t qemu_chr_mem_osize(const CharDriverState *chr)
2601{
2602 const MemoryDriver *d = chr->opaque;
2603 return d->outbuf_size;
2604}
2605
Gerd Hoffmann33521632009-12-08 13:11:49 +01002606QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002607{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002608 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002609 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002610 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002611 QemuOpts *opts;
2612
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002613 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002614 if (NULL == opts)
2615 return NULL;
2616
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002617 if (strstart(filename, "mon:", &p)) {
2618 filename = p;
2619 qemu_opt_set(opts, "mux", "on");
2620 }
2621
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002622 if (strcmp(filename, "null") == 0 ||
2623 strcmp(filename, "pty") == 0 ||
2624 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002625 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002626 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002627 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002628 return opts;
2629 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002630 if (strstart(filename, "vc", &p)) {
2631 qemu_opt_set(opts, "backend", "vc");
2632 if (*p == ':') {
2633 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2634 /* pixels */
2635 qemu_opt_set(opts, "width", width);
2636 qemu_opt_set(opts, "height", height);
2637 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2638 /* chars */
2639 qemu_opt_set(opts, "cols", width);
2640 qemu_opt_set(opts, "rows", height);
2641 } else {
2642 goto fail;
2643 }
2644 }
2645 return opts;
2646 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002647 if (strcmp(filename, "con:") == 0) {
2648 qemu_opt_set(opts, "backend", "console");
2649 return opts;
2650 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002651 if (strstart(filename, "COM", NULL)) {
2652 qemu_opt_set(opts, "backend", "serial");
2653 qemu_opt_set(opts, "path", filename);
2654 return opts;
2655 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002656 if (strstart(filename, "file:", &p)) {
2657 qemu_opt_set(opts, "backend", "file");
2658 qemu_opt_set(opts, "path", p);
2659 return opts;
2660 }
2661 if (strstart(filename, "pipe:", &p)) {
2662 qemu_opt_set(opts, "backend", "pipe");
2663 qemu_opt_set(opts, "path", p);
2664 return opts;
2665 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002666 if (strstart(filename, "tcp:", &p) ||
2667 strstart(filename, "telnet:", &p)) {
2668 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2669 host[0] = 0;
2670 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2671 goto fail;
2672 }
2673 qemu_opt_set(opts, "backend", "socket");
2674 qemu_opt_set(opts, "host", host);
2675 qemu_opt_set(opts, "port", port);
2676 if (p[pos] == ',') {
2677 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2678 goto fail;
2679 }
2680 if (strstart(filename, "telnet:", &p))
2681 qemu_opt_set(opts, "telnet", "on");
2682 return opts;
2683 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002684 if (strstart(filename, "udp:", &p)) {
2685 qemu_opt_set(opts, "backend", "udp");
2686 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2687 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002688 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002689 goto fail;
2690 }
2691 }
2692 qemu_opt_set(opts, "host", host);
2693 qemu_opt_set(opts, "port", port);
2694 if (p[pos] == '@') {
2695 p += pos + 1;
2696 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2697 host[0] = 0;
2698 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002699 goto fail;
2700 }
2701 }
2702 qemu_opt_set(opts, "localaddr", host);
2703 qemu_opt_set(opts, "localport", port);
2704 }
2705 return opts;
2706 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002707 if (strstart(filename, "unix:", &p)) {
2708 qemu_opt_set(opts, "backend", "socket");
2709 if (qemu_opts_do_parse(opts, p, "path") != 0)
2710 goto fail;
2711 return opts;
2712 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002713 if (strstart(filename, "/dev/parport", NULL) ||
2714 strstart(filename, "/dev/ppi", NULL)) {
2715 qemu_opt_set(opts, "backend", "parport");
2716 qemu_opt_set(opts, "path", filename);
2717 return opts;
2718 }
2719 if (strstart(filename, "/dev/", NULL)) {
2720 qemu_opt_set(opts, "backend", "tty");
2721 qemu_opt_set(opts, "path", filename);
2722 return opts;
2723 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002724
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002725fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002726 qemu_opts_del(opts);
2727 return NULL;
2728}
2729
2730static const struct {
2731 const char *name;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002732 int (*open)(QemuOpts *opts, CharDriverState **chr);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002733} backend_table[] = {
2734 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002735 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002736 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002737 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002738 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002739#ifdef _WIN32
2740 { .name = "file", .open = qemu_chr_open_win_file_out },
2741 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002742 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002743 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002744 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002745#else
2746 { .name = "file", .open = qemu_chr_open_file_out },
2747 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002748 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002749 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002750#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002751#ifdef CONFIG_BRLAPI
2752 { .name = "braille", .open = chr_baum_init },
2753#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002754#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002755 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2756 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002757 { .name = "tty", .open = qemu_chr_open_tty },
2758#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002759#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2760 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002761 { .name = "parport", .open = qemu_chr_open_pp },
2762#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002763#ifdef CONFIG_SPICE
2764 { .name = "spicevmc", .open = qemu_chr_open_spice },
2765#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002766};
2767
Anthony Liguorif69554b2011-08-15 11:17:37 -05002768CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002769 void (*init)(struct CharDriverState *s))
2770{
2771 CharDriverState *chr;
2772 int i;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002773 int ret;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002774
2775 if (qemu_opts_id(opts) == NULL) {
2776 fprintf(stderr, "chardev: no id specified\n");
2777 return NULL;
2778 }
2779
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002780 if (qemu_opt_get(opts, "backend") == NULL) {
2781 fprintf(stderr, "chardev: \"%s\" missing backend\n",
2782 qemu_opts_id(opts));
2783 return NULL;
2784 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002785 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2786 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2787 break;
2788 }
2789 if (i == ARRAY_SIZE(backend_table)) {
2790 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2791 qemu_opt_get(opts, "backend"));
2792 return NULL;
2793 }
2794
Kevin Wolf6e1db572011-06-01 13:29:11 +02002795 ret = backend_table[i].open(opts, &chr);
2796 if (ret < 0) {
2797 fprintf(stderr, "chardev: opening backend \"%s\" failed: %s\n",
2798 qemu_opt_get(opts, "backend"), strerror(-ret));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002799 return NULL;
2800 }
2801
2802 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05002803 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002804 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002805 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002806
2807 if (qemu_opt_get_bool(opts, "mux", 0)) {
2808 CharDriverState *base = chr;
2809 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05002810 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002811 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2812 chr = qemu_chr_open_mux(base);
2813 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002814 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002815 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002816 } else {
2817 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002818 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002819 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002820 return chr;
2821}
2822
Anthony Liguori27143a42011-08-15 11:17:36 -05002823CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002824{
2825 const char *p;
2826 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002827 QemuOpts *opts;
2828
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002829 if (strstart(filename, "chardev:", &p)) {
2830 return qemu_chr_find(p);
2831 }
2832
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002833 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002834 if (!opts)
2835 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002836
Anthony Liguorif69554b2011-08-15 11:17:37 -05002837 chr = qemu_chr_new_from_opts(opts, init);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002838 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2839 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002840 }
Paolo Bonzini363f8cb2010-12-23 13:42:54 +01002841 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002842 return chr;
2843}
2844
Anthony Liguori15f31512011-08-15 11:17:35 -05002845void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01002846{
2847 if (chr->chr_set_echo) {
2848 chr->chr_set_echo(chr, echo);
2849 }
2850}
2851
Anthony Liguoric9d830e2011-08-15 11:17:32 -05002852void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002853{
2854 if (chr->chr_guest_open) {
2855 chr->chr_guest_open(chr);
2856 }
2857}
2858
Anthony Liguori28178222011-08-15 11:17:33 -05002859void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002860{
2861 if (chr->chr_guest_close) {
2862 chr->chr_guest_close(chr);
2863 }
2864}
2865
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002866void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00002867{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002868 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002869 if (chr->chr_close)
2870 chr->chr_close(chr);
Anthony Liguori7267c092011-08-20 22:09:37 -05002871 g_free(chr->filename);
2872 g_free(chr->label);
2873 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002874}
2875
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002876ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002877{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002878 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002879 CharDriverState *chr;
2880
Blue Swirl72cf2d42009-09-12 07:36:22 +00002881 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002882 ChardevInfoList *info = g_malloc0(sizeof(*info));
2883 info->value = g_malloc0(sizeof(*info->value));
2884 info->value->label = g_strdup(chr->label);
2885 info->value->filename = g_strdup(chr->filename);
2886
2887 info->next = chr_list;
2888 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00002889 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002890
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002891 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002892}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002893
2894CharDriverState *qemu_chr_find(const char *name)
2895{
2896 CharDriverState *chr;
2897
Blue Swirl72cf2d42009-09-12 07:36:22 +00002898 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002899 if (strcmp(chr->label, name) != 0)
2900 continue;
2901 return chr;
2902 }
2903 return NULL;
2904}