blob: 09d2309eb6b2b29d683b7097a781ecf6f976ceca [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 Capitulino588b3832009-12-10 17:16:08 -020034#include "qemu-objects.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
541#ifndef _WIN32
542
543typedef struct {
544 int fd_in, fd_out;
545 int max_size;
546} FDCharDriver;
547
548#define STDIO_MAX_CLIENTS 1
549static int stdio_nb_clients = 0;
550
551static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
552{
553 FDCharDriver *s = chr->opaque;
554 return send_all(s->fd_out, buf, len);
555}
556
557static int fd_chr_read_poll(void *opaque)
558{
559 CharDriverState *chr = opaque;
560 FDCharDriver *s = chr->opaque;
561
Anthony Liguori909cda12011-08-15 11:17:31 -0500562 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000563 return s->max_size;
564}
565
566static void fd_chr_read(void *opaque)
567{
568 CharDriverState *chr = opaque;
569 FDCharDriver *s = chr->opaque;
570 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530571 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000572
573 len = sizeof(buf);
574 if (len > s->max_size)
575 len = s->max_size;
576 if (len == 0)
577 return;
578 size = read(s->fd_in, buf, len);
579 if (size == 0) {
580 /* FD has been closed. Remove it from the active list. */
581 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530582 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000583 return;
584 }
585 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500586 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000587 }
588}
589
590static void fd_chr_update_read_handler(CharDriverState *chr)
591{
592 FDCharDriver *s = chr->opaque;
593
594 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500595 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000596 } else {
597 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
598 fd_chr_read, NULL, chr);
599 }
600 }
601}
602
603static void fd_chr_close(struct CharDriverState *chr)
604{
605 FDCharDriver *s = chr->opaque;
606
607 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500608 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000609 } else {
610 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
611 }
612 }
613
Anthony Liguori7267c092011-08-20 22:09:37 -0500614 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530615 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000616}
617
618/* open a character device to a unix fd */
619static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
620{
621 CharDriverState *chr;
622 FDCharDriver *s;
623
Anthony Liguori7267c092011-08-20 22:09:37 -0500624 chr = g_malloc0(sizeof(CharDriverState));
625 s = g_malloc0(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000626 s->fd_in = fd_in;
627 s->fd_out = fd_out;
628 chr->opaque = s;
629 chr->chr_write = fd_chr_write;
630 chr->chr_update_read_handler = fd_chr_update_read_handler;
631 chr->chr_close = fd_chr_close;
632
Amit Shah127338e2009-11-03 19:59:56 +0530633 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000634
635 return chr;
636}
637
Kevin Wolf6e1db572011-06-01 13:29:11 +0200638static int qemu_chr_open_file_out(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000639{
640 int fd_out;
641
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100642 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200643 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Kevin Wolf6e1db572011-06-01 13:29:11 +0200644 if (fd_out < 0) {
645 return -errno;
646 }
647
648 *_chr = qemu_chr_open_fd(-1, fd_out);
649 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000650}
651
Kevin Wolf6e1db572011-06-01 13:29:11 +0200652static int qemu_chr_open_pipe(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000653{
654 int fd_in, fd_out;
655 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200656 const char *filename = qemu_opt_get(opts, "path");
657
658 if (filename == NULL) {
659 fprintf(stderr, "chardev: pipe: no filename given\n");
Kevin Wolf6e1db572011-06-01 13:29:11 +0200660 return -EINVAL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200661 }
aliguori6f97dba2008-10-31 18:49:55 +0000662
663 snprintf(filename_in, 256, "%s.in", filename);
664 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100665 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
666 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000667 if (fd_in < 0 || fd_out < 0) {
668 if (fd_in >= 0)
669 close(fd_in);
670 if (fd_out >= 0)
671 close(fd_out);
Kevin Wolf6e1db572011-06-01 13:29:11 +0200672 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
673 if (fd_in < 0) {
674 return -errno;
675 }
aliguori6f97dba2008-10-31 18:49:55 +0000676 }
Kevin Wolf6e1db572011-06-01 13:29:11 +0200677
678 *_chr = qemu_chr_open_fd(fd_in, fd_out);
679 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000680}
681
682
683/* for STDIO, we handle the case where several clients use it
684 (nographic mode) */
685
686#define TERM_FIFO_MAX_SIZE 1
687
688static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
689static int term_fifo_size;
690
691static int stdio_read_poll(void *opaque)
692{
693 CharDriverState *chr = opaque;
694
695 /* try to flush the queue if needed */
Anthony Liguori909cda12011-08-15 11:17:31 -0500696 if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500697 qemu_chr_be_write(chr, term_fifo, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000698 term_fifo_size = 0;
699 }
700 /* see if we can absorb more chars */
701 if (term_fifo_size == 0)
702 return 1;
703 else
704 return 0;
705}
706
707static void stdio_read(void *opaque)
708{
709 int size;
710 uint8_t buf[1];
711 CharDriverState *chr = opaque;
712
713 size = read(0, buf, 1);
714 if (size == 0) {
715 /* stdin has been closed. Remove it from the active list. */
716 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530717 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000718 return;
719 }
720 if (size > 0) {
Anthony Liguori909cda12011-08-15 11:17:31 -0500721 if (qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500722 qemu_chr_be_write(chr, buf, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000723 } else if (term_fifo_size == 0) {
724 term_fifo[term_fifo_size++] = buf[0];
725 }
726 }
727}
728
729/* init terminal so that we can grab keys */
730static struct termios oldtty;
731static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100732static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000733
734static void term_exit(void)
735{
736 tcsetattr (0, TCSANOW, &oldtty);
737 fcntl(0, F_SETFL, old_fd0_flags);
738}
739
Paolo Bonzinibb002512010-12-23 13:42:50 +0100740static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000741{
742 struct termios tty;
743
Paolo Bonzini03693642010-12-23 13:42:49 +0100744 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100745 if (!echo) {
746 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000747 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100748 tty.c_oflag |= OPOST;
749 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
750 tty.c_cflag &= ~(CSIZE|PARENB);
751 tty.c_cflag |= CS8;
752 tty.c_cc[VMIN] = 1;
753 tty.c_cc[VTIME] = 0;
754 }
aliguori6f97dba2008-10-31 18:49:55 +0000755 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100756 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000757 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000758
759 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000760}
761
762static void qemu_chr_close_stdio(struct CharDriverState *chr)
763{
764 term_exit();
765 stdio_nb_clients--;
766 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
767 fd_chr_close(chr);
768}
769
Kevin Wolf6e1db572011-06-01 13:29:11 +0200770static int qemu_chr_open_stdio(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000771{
772 CharDriverState *chr;
773
Kevin Wolf6e1db572011-06-01 13:29:11 +0200774 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
775 return -EBUSY;
776 }
777
Paolo Bonzini03693642010-12-23 13:42:49 +0100778 if (stdio_nb_clients == 0) {
779 old_fd0_flags = fcntl(0, F_GETFL);
780 tcgetattr (0, &oldtty);
781 fcntl(0, F_SETFL, O_NONBLOCK);
782 atexit(term_exit);
783 }
784
aliguori6f97dba2008-10-31 18:49:55 +0000785 chr = qemu_chr_open_fd(0, 1);
786 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100787 chr->chr_set_echo = qemu_chr_set_echo_stdio;
aliguori6f97dba2008-10-31 18:49:55 +0000788 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
789 stdio_nb_clients++;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100790 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
791 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500792 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000793
Kevin Wolf6e1db572011-06-01 13:29:11 +0200794 *_chr = chr;
795 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000796}
797
798#ifdef __sun__
799/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000800static int openpty(int *amaster, int *aslave, char *name,
801 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000802{
803 const char *slave;
804 int mfd = -1, sfd = -1;
805
806 *amaster = *aslave = -1;
807
808 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
809 if (mfd < 0)
810 goto err;
811
812 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
813 goto err;
814
815 if ((slave = ptsname(mfd)) == NULL)
816 goto err;
817
818 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
819 goto err;
820
821 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
822 (termp != NULL && tcgetattr(sfd, termp) < 0))
823 goto err;
824
825 if (amaster)
826 *amaster = mfd;
827 if (aslave)
828 *aslave = sfd;
829 if (winp)
830 ioctl(sfd, TIOCSWINSZ, winp);
831
832 return 0;
833
834err:
835 if (sfd != -1)
836 close(sfd);
837 close(mfd);
838 return -1;
839}
840
blueswir13f4cb3d2009-04-13 16:31:01 +0000841static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000842{
843 termios_p->c_iflag &=
844 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
845 termios_p->c_oflag &= ~OPOST;
846 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
847 termios_p->c_cflag &= ~(CSIZE|PARENB);
848 termios_p->c_cflag |= CS8;
849
850 termios_p->c_cc[VMIN] = 0;
851 termios_p->c_cc[VTIME] = 0;
852}
853#endif
854
855#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100856 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
857 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000858
859typedef struct {
860 int fd;
861 int connected;
862 int polling;
863 int read_bytes;
864 QEMUTimer *timer;
865} PtyCharDriver;
866
867static void pty_chr_update_read_handler(CharDriverState *chr);
868static void pty_chr_state(CharDriverState *chr, int connected);
869
870static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
871{
872 PtyCharDriver *s = chr->opaque;
873
874 if (!s->connected) {
875 /* guest sends data, check for (re-)connect */
876 pty_chr_update_read_handler(chr);
877 return 0;
878 }
879 return send_all(s->fd, buf, len);
880}
881
882static int pty_chr_read_poll(void *opaque)
883{
884 CharDriverState *chr = opaque;
885 PtyCharDriver *s = chr->opaque;
886
Anthony Liguori909cda12011-08-15 11:17:31 -0500887 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000888 return s->read_bytes;
889}
890
891static void pty_chr_read(void *opaque)
892{
893 CharDriverState *chr = opaque;
894 PtyCharDriver *s = chr->opaque;
895 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530896 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000897
898 len = sizeof(buf);
899 if (len > s->read_bytes)
900 len = s->read_bytes;
901 if (len == 0)
902 return;
903 size = read(s->fd, buf, len);
904 if ((size == -1 && errno == EIO) ||
905 (size == 0)) {
906 pty_chr_state(chr, 0);
907 return;
908 }
909 if (size > 0) {
910 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500911 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000912 }
913}
914
915static void pty_chr_update_read_handler(CharDriverState *chr)
916{
917 PtyCharDriver *s = chr->opaque;
918
919 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
920 pty_chr_read, NULL, chr);
921 s->polling = 1;
922 /*
923 * Short timeout here: just need wait long enougth that qemu makes
924 * it through the poll loop once. When reconnected we want a
925 * short timeout so we notice it almost instantly. Otherwise
926 * read() gives us -EIO instantly, making pty_chr_state() reset the
927 * timeout to the normal (much longer) poll interval before the
928 * timer triggers.
929 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100930 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +0000931}
932
933static void pty_chr_state(CharDriverState *chr, int connected)
934{
935 PtyCharDriver *s = chr->opaque;
936
937 if (!connected) {
938 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
939 s->connected = 0;
940 s->polling = 0;
941 /* (re-)connect poll interval for idle guests: once per second.
942 * We check more frequently in case the guests sends data to
943 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100944 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +0000945 } else {
946 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530947 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000948 s->connected = 1;
949 }
950}
951
952static void pty_chr_timer(void *opaque)
953{
954 struct CharDriverState *chr = opaque;
955 PtyCharDriver *s = chr->opaque;
956
957 if (s->connected)
958 return;
959 if (s->polling) {
960 /* If we arrive here without polling being cleared due
961 * read returning -EIO, then we are (re-)connected */
962 pty_chr_state(chr, 1);
963 return;
964 }
965
966 /* Next poll ... */
967 pty_chr_update_read_handler(chr);
968}
969
970static void pty_chr_close(struct CharDriverState *chr)
971{
972 PtyCharDriver *s = chr->opaque;
973
974 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
975 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000976 qemu_del_timer(s->timer);
977 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -0500978 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530979 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000980}
981
Kevin Wolf6e1db572011-06-01 13:29:11 +0200982static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000983{
984 CharDriverState *chr;
985 PtyCharDriver *s;
986 struct termios tty;
987 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000988#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000989 char pty_name[PATH_MAX];
990#define q_ptsname(x) pty_name
991#else
992 char *pty_name = NULL;
993#define q_ptsname(x) ptsname(x)
994#endif
995
Anthony Liguori7267c092011-08-20 22:09:37 -0500996 chr = g_malloc0(sizeof(CharDriverState));
997 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000998
999 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Kevin Wolf6e1db572011-06-01 13:29:11 +02001000 return -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001001 }
1002
1003 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001004 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001005 cfmakeraw(&tty);
1006 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1007 close(slave_fd);
1008
1009 len = strlen(q_ptsname(s->fd)) + 5;
Anthony Liguori7267c092011-08-20 22:09:37 -05001010 chr->filename = g_malloc(len);
aliguori6f97dba2008-10-31 18:49:55 +00001011 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02001012 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +00001013 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
1014
1015 chr->opaque = s;
1016 chr->chr_write = pty_chr_write;
1017 chr->chr_update_read_handler = pty_chr_update_read_handler;
1018 chr->chr_close = pty_chr_close;
1019
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001020 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001021
Kevin Wolf6e1db572011-06-01 13:29:11 +02001022 *_chr = chr;
1023 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001024}
1025
1026static void tty_serial_init(int fd, int speed,
1027 int parity, int data_bits, int stop_bits)
1028{
1029 struct termios tty;
1030 speed_t spd;
1031
1032#if 0
1033 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1034 speed, parity, data_bits, stop_bits);
1035#endif
1036 tcgetattr (fd, &tty);
1037
Stefan Weil45eea132009-10-26 16:10:10 +01001038#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1039 speed = speed * 10 / 11;
1040 do {
1041 check_speed(50);
1042 check_speed(75);
1043 check_speed(110);
1044 check_speed(134);
1045 check_speed(150);
1046 check_speed(200);
1047 check_speed(300);
1048 check_speed(600);
1049 check_speed(1200);
1050 check_speed(1800);
1051 check_speed(2400);
1052 check_speed(4800);
1053 check_speed(9600);
1054 check_speed(19200);
1055 check_speed(38400);
1056 /* Non-Posix values follow. They may be unsupported on some systems. */
1057 check_speed(57600);
1058 check_speed(115200);
1059#ifdef B230400
1060 check_speed(230400);
1061#endif
1062#ifdef B460800
1063 check_speed(460800);
1064#endif
1065#ifdef B500000
1066 check_speed(500000);
1067#endif
1068#ifdef B576000
1069 check_speed(576000);
1070#endif
1071#ifdef B921600
1072 check_speed(921600);
1073#endif
1074#ifdef B1000000
1075 check_speed(1000000);
1076#endif
1077#ifdef B1152000
1078 check_speed(1152000);
1079#endif
1080#ifdef B1500000
1081 check_speed(1500000);
1082#endif
1083#ifdef B2000000
1084 check_speed(2000000);
1085#endif
1086#ifdef B2500000
1087 check_speed(2500000);
1088#endif
1089#ifdef B3000000
1090 check_speed(3000000);
1091#endif
1092#ifdef B3500000
1093 check_speed(3500000);
1094#endif
1095#ifdef B4000000
1096 check_speed(4000000);
1097#endif
aliguori6f97dba2008-10-31 18:49:55 +00001098 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001099 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001100
1101 cfsetispeed(&tty, spd);
1102 cfsetospeed(&tty, spd);
1103
1104 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1105 |INLCR|IGNCR|ICRNL|IXON);
1106 tty.c_oflag |= OPOST;
1107 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1108 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1109 switch(data_bits) {
1110 default:
1111 case 8:
1112 tty.c_cflag |= CS8;
1113 break;
1114 case 7:
1115 tty.c_cflag |= CS7;
1116 break;
1117 case 6:
1118 tty.c_cflag |= CS6;
1119 break;
1120 case 5:
1121 tty.c_cflag |= CS5;
1122 break;
1123 }
1124 switch(parity) {
1125 default:
1126 case 'N':
1127 break;
1128 case 'E':
1129 tty.c_cflag |= PARENB;
1130 break;
1131 case 'O':
1132 tty.c_cflag |= PARENB | PARODD;
1133 break;
1134 }
1135 if (stop_bits == 2)
1136 tty.c_cflag |= CSTOPB;
1137
1138 tcsetattr (fd, TCSANOW, &tty);
1139}
1140
1141static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1142{
1143 FDCharDriver *s = chr->opaque;
1144
1145 switch(cmd) {
1146 case CHR_IOCTL_SERIAL_SET_PARAMS:
1147 {
1148 QEMUSerialSetParams *ssp = arg;
1149 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1150 ssp->data_bits, ssp->stop_bits);
1151 }
1152 break;
1153 case CHR_IOCTL_SERIAL_SET_BREAK:
1154 {
1155 int enable = *(int *)arg;
1156 if (enable)
1157 tcsendbreak(s->fd_in, 1);
1158 }
1159 break;
1160 case CHR_IOCTL_SERIAL_GET_TIOCM:
1161 {
1162 int sarg = 0;
1163 int *targ = (int *)arg;
1164 ioctl(s->fd_in, TIOCMGET, &sarg);
1165 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001166 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001167 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001168 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001169 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001170 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001171 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001172 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001173 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001174 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001175 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001176 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001177 *targ |= CHR_TIOCM_RTS;
1178 }
1179 break;
1180 case CHR_IOCTL_SERIAL_SET_TIOCM:
1181 {
1182 int sarg = *(int *)arg;
1183 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001184 ioctl(s->fd_in, TIOCMGET, &targ);
1185 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1186 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1187 if (sarg & CHR_TIOCM_CTS)
1188 targ |= TIOCM_CTS;
1189 if (sarg & CHR_TIOCM_CAR)
1190 targ |= TIOCM_CAR;
1191 if (sarg & CHR_TIOCM_DSR)
1192 targ |= TIOCM_DSR;
1193 if (sarg & CHR_TIOCM_RI)
1194 targ |= TIOCM_RI;
1195 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001196 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001197 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001198 targ |= TIOCM_RTS;
1199 ioctl(s->fd_in, TIOCMSET, &targ);
1200 }
1201 break;
1202 default:
1203 return -ENOTSUP;
1204 }
1205 return 0;
1206}
1207
David Ahern4266a132010-02-10 18:27:17 -07001208static void qemu_chr_close_tty(CharDriverState *chr)
1209{
1210 FDCharDriver *s = chr->opaque;
1211 int fd = -1;
1212
1213 if (s) {
1214 fd = s->fd_in;
1215 }
1216
1217 fd_chr_close(chr);
1218
1219 if (fd >= 0) {
1220 close(fd);
1221 }
1222}
1223
Kevin Wolf6e1db572011-06-01 13:29:11 +02001224static int qemu_chr_open_tty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001225{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001226 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001227 CharDriverState *chr;
1228 int fd;
1229
Kevin Wolf6e1db572011-06-01 13:29:11 +02001230 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001231 if (fd < 0) {
Kevin Wolf6e1db572011-06-01 13:29:11 +02001232 return -errno;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001233 }
aliguori6f97dba2008-10-31 18:49:55 +00001234 tty_serial_init(fd, 115200, 'N', 8, 1);
1235 chr = qemu_chr_open_fd(fd, fd);
aliguori6f97dba2008-10-31 18:49:55 +00001236 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001237 chr->chr_close = qemu_chr_close_tty;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001238
1239 *_chr = chr;
1240 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001241}
1242#else /* ! __linux__ && ! __sun__ */
Kevin Wolf6e1db572011-06-01 13:29:11 +02001243static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001244{
Kevin Wolf6e1db572011-06-01 13:29:11 +02001245 return -ENOTSUP;
aliguori6f97dba2008-10-31 18:49:55 +00001246}
1247#endif /* __linux__ || __sun__ */
1248
1249#if defined(__linux__)
1250typedef struct {
1251 int fd;
1252 int mode;
1253} ParallelCharDriver;
1254
1255static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1256{
1257 if (s->mode != mode) {
1258 int m = mode;
1259 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1260 return 0;
1261 s->mode = mode;
1262 }
1263 return 1;
1264}
1265
1266static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1267{
1268 ParallelCharDriver *drv = chr->opaque;
1269 int fd = drv->fd;
1270 uint8_t b;
1271
1272 switch(cmd) {
1273 case CHR_IOCTL_PP_READ_DATA:
1274 if (ioctl(fd, PPRDATA, &b) < 0)
1275 return -ENOTSUP;
1276 *(uint8_t *)arg = b;
1277 break;
1278 case CHR_IOCTL_PP_WRITE_DATA:
1279 b = *(uint8_t *)arg;
1280 if (ioctl(fd, PPWDATA, &b) < 0)
1281 return -ENOTSUP;
1282 break;
1283 case CHR_IOCTL_PP_READ_CONTROL:
1284 if (ioctl(fd, PPRCONTROL, &b) < 0)
1285 return -ENOTSUP;
1286 /* Linux gives only the lowest bits, and no way to know data
1287 direction! For better compatibility set the fixed upper
1288 bits. */
1289 *(uint8_t *)arg = b | 0xc0;
1290 break;
1291 case CHR_IOCTL_PP_WRITE_CONTROL:
1292 b = *(uint8_t *)arg;
1293 if (ioctl(fd, PPWCONTROL, &b) < 0)
1294 return -ENOTSUP;
1295 break;
1296 case CHR_IOCTL_PP_READ_STATUS:
1297 if (ioctl(fd, PPRSTATUS, &b) < 0)
1298 return -ENOTSUP;
1299 *(uint8_t *)arg = b;
1300 break;
1301 case CHR_IOCTL_PP_DATA_DIR:
1302 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1303 return -ENOTSUP;
1304 break;
1305 case CHR_IOCTL_PP_EPP_READ_ADDR:
1306 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1307 struct ParallelIOArg *parg = arg;
1308 int n = read(fd, parg->buffer, parg->count);
1309 if (n != parg->count) {
1310 return -EIO;
1311 }
1312 }
1313 break;
1314 case CHR_IOCTL_PP_EPP_READ:
1315 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1316 struct ParallelIOArg *parg = arg;
1317 int n = read(fd, parg->buffer, parg->count);
1318 if (n != parg->count) {
1319 return -EIO;
1320 }
1321 }
1322 break;
1323 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1324 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1325 struct ParallelIOArg *parg = arg;
1326 int n = write(fd, parg->buffer, parg->count);
1327 if (n != parg->count) {
1328 return -EIO;
1329 }
1330 }
1331 break;
1332 case CHR_IOCTL_PP_EPP_WRITE:
1333 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1334 struct ParallelIOArg *parg = arg;
1335 int n = write(fd, parg->buffer, parg->count);
1336 if (n != parg->count) {
1337 return -EIO;
1338 }
1339 }
1340 break;
1341 default:
1342 return -ENOTSUP;
1343 }
1344 return 0;
1345}
1346
1347static void pp_close(CharDriverState *chr)
1348{
1349 ParallelCharDriver *drv = chr->opaque;
1350 int fd = drv->fd;
1351
1352 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1353 ioctl(fd, PPRELEASE);
1354 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001355 g_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301356 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001357}
1358
Kevin Wolf6e1db572011-06-01 13:29:11 +02001359static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001360{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001361 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001362 CharDriverState *chr;
1363 ParallelCharDriver *drv;
1364 int fd;
1365
1366 TFR(fd = open(filename, O_RDWR));
Kevin Wolf6e1db572011-06-01 13:29:11 +02001367 if (fd < 0) {
1368 return -errno;
1369 }
aliguori6f97dba2008-10-31 18:49:55 +00001370
1371 if (ioctl(fd, PPCLAIM) < 0) {
1372 close(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001373 return -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001374 }
1375
Anthony Liguori7267c092011-08-20 22:09:37 -05001376 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001377 drv->fd = fd;
1378 drv->mode = IEEE1284_MODE_COMPAT;
1379
Anthony Liguori7267c092011-08-20 22:09:37 -05001380 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001381 chr->chr_write = null_chr_write;
1382 chr->chr_ioctl = pp_ioctl;
1383 chr->chr_close = pp_close;
1384 chr->opaque = drv;
1385
Amit Shah127338e2009-11-03 19:59:56 +05301386 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001387
Kevin Wolf6e1db572011-06-01 13:29:11 +02001388 *_chr = chr;
1389 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001390}
1391#endif /* __linux__ */
1392
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001393#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001394static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1395{
Stefan Weile0efb992011-02-23 19:09:16 +01001396 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001397 uint8_t b;
1398
1399 switch(cmd) {
1400 case CHR_IOCTL_PP_READ_DATA:
1401 if (ioctl(fd, PPIGDATA, &b) < 0)
1402 return -ENOTSUP;
1403 *(uint8_t *)arg = b;
1404 break;
1405 case CHR_IOCTL_PP_WRITE_DATA:
1406 b = *(uint8_t *)arg;
1407 if (ioctl(fd, PPISDATA, &b) < 0)
1408 return -ENOTSUP;
1409 break;
1410 case CHR_IOCTL_PP_READ_CONTROL:
1411 if (ioctl(fd, PPIGCTRL, &b) < 0)
1412 return -ENOTSUP;
1413 *(uint8_t *)arg = b;
1414 break;
1415 case CHR_IOCTL_PP_WRITE_CONTROL:
1416 b = *(uint8_t *)arg;
1417 if (ioctl(fd, PPISCTRL, &b) < 0)
1418 return -ENOTSUP;
1419 break;
1420 case CHR_IOCTL_PP_READ_STATUS:
1421 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1422 return -ENOTSUP;
1423 *(uint8_t *)arg = b;
1424 break;
1425 default:
1426 return -ENOTSUP;
1427 }
1428 return 0;
1429}
1430
Kevin Wolf6e1db572011-06-01 13:29:11 +02001431static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
blueswir16972f932008-11-22 20:49:12 +00001432{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001433 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001434 CharDriverState *chr;
1435 int fd;
1436
Kevin Wolf6e1db572011-06-01 13:29:11 +02001437 fd = qemu_open(filename, O_RDWR);
1438 if (fd < 0) {
1439 return -errno;
1440 }
blueswir16972f932008-11-22 20:49:12 +00001441
Anthony Liguori7267c092011-08-20 22:09:37 -05001442 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001443 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001444 chr->chr_write = null_chr_write;
1445 chr->chr_ioctl = pp_ioctl;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001446
1447 *_chr = chr;
1448 return 0;
blueswir16972f932008-11-22 20:49:12 +00001449}
1450#endif
1451
aliguori6f97dba2008-10-31 18:49:55 +00001452#else /* _WIN32 */
1453
1454typedef struct {
1455 int max_size;
1456 HANDLE hcom, hrecv, hsend;
1457 OVERLAPPED orecv, osend;
1458 BOOL fpipe;
1459 DWORD len;
1460} WinCharState;
1461
1462#define NSENDBUF 2048
1463#define NRECVBUF 2048
1464#define MAXCONNECT 1
1465#define NTIMEOUT 5000
1466
1467static int win_chr_poll(void *opaque);
1468static int win_chr_pipe_poll(void *opaque);
1469
1470static void win_chr_close(CharDriverState *chr)
1471{
1472 WinCharState *s = chr->opaque;
1473
1474 if (s->hsend) {
1475 CloseHandle(s->hsend);
1476 s->hsend = NULL;
1477 }
1478 if (s->hrecv) {
1479 CloseHandle(s->hrecv);
1480 s->hrecv = NULL;
1481 }
1482 if (s->hcom) {
1483 CloseHandle(s->hcom);
1484 s->hcom = NULL;
1485 }
1486 if (s->fpipe)
1487 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1488 else
1489 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301490
1491 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001492}
1493
1494static int win_chr_init(CharDriverState *chr, const char *filename)
1495{
1496 WinCharState *s = chr->opaque;
1497 COMMCONFIG comcfg;
1498 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1499 COMSTAT comstat;
1500 DWORD size;
1501 DWORD err;
1502
1503 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1504 if (!s->hsend) {
1505 fprintf(stderr, "Failed CreateEvent\n");
1506 goto fail;
1507 }
1508 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1509 if (!s->hrecv) {
1510 fprintf(stderr, "Failed CreateEvent\n");
1511 goto fail;
1512 }
1513
1514 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1515 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1516 if (s->hcom == INVALID_HANDLE_VALUE) {
1517 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1518 s->hcom = NULL;
1519 goto fail;
1520 }
1521
1522 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1523 fprintf(stderr, "Failed SetupComm\n");
1524 goto fail;
1525 }
1526
1527 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1528 size = sizeof(COMMCONFIG);
1529 GetDefaultCommConfig(filename, &comcfg, &size);
1530 comcfg.dcb.DCBlength = sizeof(DCB);
1531 CommConfigDialog(filename, NULL, &comcfg);
1532
1533 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1534 fprintf(stderr, "Failed SetCommState\n");
1535 goto fail;
1536 }
1537
1538 if (!SetCommMask(s->hcom, EV_ERR)) {
1539 fprintf(stderr, "Failed SetCommMask\n");
1540 goto fail;
1541 }
1542
1543 cto.ReadIntervalTimeout = MAXDWORD;
1544 if (!SetCommTimeouts(s->hcom, &cto)) {
1545 fprintf(stderr, "Failed SetCommTimeouts\n");
1546 goto fail;
1547 }
1548
1549 if (!ClearCommError(s->hcom, &err, &comstat)) {
1550 fprintf(stderr, "Failed ClearCommError\n");
1551 goto fail;
1552 }
1553 qemu_add_polling_cb(win_chr_poll, chr);
1554 return 0;
1555
1556 fail:
1557 win_chr_close(chr);
1558 return -1;
1559}
1560
1561static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1562{
1563 WinCharState *s = chr->opaque;
1564 DWORD len, ret, size, err;
1565
1566 len = len1;
1567 ZeroMemory(&s->osend, sizeof(s->osend));
1568 s->osend.hEvent = s->hsend;
1569 while (len > 0) {
1570 if (s->hsend)
1571 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1572 else
1573 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1574 if (!ret) {
1575 err = GetLastError();
1576 if (err == ERROR_IO_PENDING) {
1577 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1578 if (ret) {
1579 buf += size;
1580 len -= size;
1581 } else {
1582 break;
1583 }
1584 } else {
1585 break;
1586 }
1587 } else {
1588 buf += size;
1589 len -= size;
1590 }
1591 }
1592 return len1 - len;
1593}
1594
1595static int win_chr_read_poll(CharDriverState *chr)
1596{
1597 WinCharState *s = chr->opaque;
1598
Anthony Liguori909cda12011-08-15 11:17:31 -05001599 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001600 return s->max_size;
1601}
1602
1603static void win_chr_readfile(CharDriverState *chr)
1604{
1605 WinCharState *s = chr->opaque;
1606 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301607 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001608 DWORD size;
1609
1610 ZeroMemory(&s->orecv, sizeof(s->orecv));
1611 s->orecv.hEvent = s->hrecv;
1612 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1613 if (!ret) {
1614 err = GetLastError();
1615 if (err == ERROR_IO_PENDING) {
1616 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1617 }
1618 }
1619
1620 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001621 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001622 }
1623}
1624
1625static void win_chr_read(CharDriverState *chr)
1626{
1627 WinCharState *s = chr->opaque;
1628
1629 if (s->len > s->max_size)
1630 s->len = s->max_size;
1631 if (s->len == 0)
1632 return;
1633
1634 win_chr_readfile(chr);
1635}
1636
1637static int win_chr_poll(void *opaque)
1638{
1639 CharDriverState *chr = opaque;
1640 WinCharState *s = chr->opaque;
1641 COMSTAT status;
1642 DWORD comerr;
1643
1644 ClearCommError(s->hcom, &comerr, &status);
1645 if (status.cbInQue > 0) {
1646 s->len = status.cbInQue;
1647 win_chr_read_poll(chr);
1648 win_chr_read(chr);
1649 return 1;
1650 }
1651 return 0;
1652}
1653
Kevin Wolf6e1db572011-06-01 13:29:11 +02001654static int qemu_chr_open_win(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001655{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001656 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001657 CharDriverState *chr;
1658 WinCharState *s;
1659
Anthony Liguori7267c092011-08-20 22:09:37 -05001660 chr = g_malloc0(sizeof(CharDriverState));
1661 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001662 chr->opaque = s;
1663 chr->chr_write = win_chr_write;
1664 chr->chr_close = win_chr_close;
1665
1666 if (win_chr_init(chr, filename) < 0) {
1667 free(s);
1668 free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001669 return -EIO;
aliguori6f97dba2008-10-31 18:49:55 +00001670 }
Amit Shah127338e2009-11-03 19:59:56 +05301671 qemu_chr_generic_open(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001672
1673 *_chr = chr;
1674 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001675}
1676
1677static int win_chr_pipe_poll(void *opaque)
1678{
1679 CharDriverState *chr = opaque;
1680 WinCharState *s = chr->opaque;
1681 DWORD size;
1682
1683 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1684 if (size > 0) {
1685 s->len = size;
1686 win_chr_read_poll(chr);
1687 win_chr_read(chr);
1688 return 1;
1689 }
1690 return 0;
1691}
1692
1693static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1694{
1695 WinCharState *s = chr->opaque;
1696 OVERLAPPED ov;
1697 int ret;
1698 DWORD size;
1699 char openname[256];
1700
1701 s->fpipe = TRUE;
1702
1703 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1704 if (!s->hsend) {
1705 fprintf(stderr, "Failed CreateEvent\n");
1706 goto fail;
1707 }
1708 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1709 if (!s->hrecv) {
1710 fprintf(stderr, "Failed CreateEvent\n");
1711 goto fail;
1712 }
1713
1714 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1715 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1716 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1717 PIPE_WAIT,
1718 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1719 if (s->hcom == INVALID_HANDLE_VALUE) {
1720 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1721 s->hcom = NULL;
1722 goto fail;
1723 }
1724
1725 ZeroMemory(&ov, sizeof(ov));
1726 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1727 ret = ConnectNamedPipe(s->hcom, &ov);
1728 if (ret) {
1729 fprintf(stderr, "Failed ConnectNamedPipe\n");
1730 goto fail;
1731 }
1732
1733 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1734 if (!ret) {
1735 fprintf(stderr, "Failed GetOverlappedResult\n");
1736 if (ov.hEvent) {
1737 CloseHandle(ov.hEvent);
1738 ov.hEvent = NULL;
1739 }
1740 goto fail;
1741 }
1742
1743 if (ov.hEvent) {
1744 CloseHandle(ov.hEvent);
1745 ov.hEvent = NULL;
1746 }
1747 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1748 return 0;
1749
1750 fail:
1751 win_chr_close(chr);
1752 return -1;
1753}
1754
1755
Kevin Wolf6e1db572011-06-01 13:29:11 +02001756static int qemu_chr_open_win_pipe(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001757{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001758 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001759 CharDriverState *chr;
1760 WinCharState *s;
1761
Anthony Liguori7267c092011-08-20 22:09:37 -05001762 chr = g_malloc0(sizeof(CharDriverState));
1763 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001764 chr->opaque = s;
1765 chr->chr_write = win_chr_write;
1766 chr->chr_close = win_chr_close;
1767
1768 if (win_chr_pipe_init(chr, filename) < 0) {
1769 free(s);
1770 free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001771 return -EIO;
aliguori6f97dba2008-10-31 18:49:55 +00001772 }
Amit Shah127338e2009-11-03 19:59:56 +05301773 qemu_chr_generic_open(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001774
1775 *_chr = chr;
1776 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001777}
1778
Blue Swirlaad04cd2011-07-23 19:26:08 +00001779static int qemu_chr_open_win_file(HANDLE fd_out, CharDriverState **pchr)
aliguori6f97dba2008-10-31 18:49:55 +00001780{
1781 CharDriverState *chr;
1782 WinCharState *s;
1783
Anthony Liguori7267c092011-08-20 22:09:37 -05001784 chr = g_malloc0(sizeof(CharDriverState));
1785 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001786 s->hcom = fd_out;
1787 chr->opaque = s;
1788 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301789 qemu_chr_generic_open(chr);
Blue Swirlaad04cd2011-07-23 19:26:08 +00001790 *pchr = chr;
1791 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001792}
1793
Blue Swirlaad04cd2011-07-23 19:26:08 +00001794static int qemu_chr_open_win_con(QemuOpts *opts, CharDriverState **chr)
aliguori6f97dba2008-10-31 18:49:55 +00001795{
Kevin Wolf6e1db572011-06-01 13:29:11 +02001796 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE), chr);
aliguori6f97dba2008-10-31 18:49:55 +00001797}
1798
Kevin Wolf6e1db572011-06-01 13:29:11 +02001799static int qemu_chr_open_win_file_out(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001800{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001801 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001802 HANDLE fd_out;
1803
1804 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1805 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001806 if (fd_out == INVALID_HANDLE_VALUE) {
1807 return -EIO;
1808 }
aliguori6f97dba2008-10-31 18:49:55 +00001809
Kevin Wolf6e1db572011-06-01 13:29:11 +02001810 return qemu_chr_open_win_file(fd_out, _chr);
aliguori6f97dba2008-10-31 18:49:55 +00001811}
1812#endif /* !_WIN32 */
1813
1814/***********************************************************/
1815/* UDP Net console */
1816
1817typedef struct {
1818 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05301819 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001820 int bufcnt;
1821 int bufptr;
1822 int max_size;
1823} NetCharDriver;
1824
1825static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1826{
1827 NetCharDriver *s = chr->opaque;
1828
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001829 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00001830}
1831
1832static int udp_chr_read_poll(void *opaque)
1833{
1834 CharDriverState *chr = opaque;
1835 NetCharDriver *s = chr->opaque;
1836
Anthony Liguori909cda12011-08-15 11:17:31 -05001837 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001838
1839 /* If there were any stray characters in the queue process them
1840 * first
1841 */
1842 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001843 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00001844 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05001845 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001846 }
1847 return s->max_size;
1848}
1849
1850static void udp_chr_read(void *opaque)
1851{
1852 CharDriverState *chr = opaque;
1853 NetCharDriver *s = chr->opaque;
1854
1855 if (s->max_size == 0)
1856 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00001857 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00001858 s->bufptr = s->bufcnt;
1859 if (s->bufcnt <= 0)
1860 return;
1861
1862 s->bufptr = 0;
1863 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001864 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00001865 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05001866 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001867 }
1868}
1869
1870static void udp_chr_update_read_handler(CharDriverState *chr)
1871{
1872 NetCharDriver *s = chr->opaque;
1873
1874 if (s->fd >= 0) {
1875 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1876 udp_chr_read, NULL, chr);
1877 }
1878}
1879
aliguori819f56b2009-03-28 17:58:14 +00001880static void udp_chr_close(CharDriverState *chr)
1881{
1882 NetCharDriver *s = chr->opaque;
1883 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03001884 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00001885 closesocket(s->fd);
1886 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001887 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05301888 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00001889}
1890
Kevin Wolf6e1db572011-06-01 13:29:11 +02001891static int qemu_chr_open_udp(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001892{
1893 CharDriverState *chr = NULL;
1894 NetCharDriver *s = NULL;
1895 int fd = -1;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001896 int ret;
aliguori6f97dba2008-10-31 18:49:55 +00001897
Anthony Liguori7267c092011-08-20 22:09:37 -05001898 chr = g_malloc0(sizeof(CharDriverState));
1899 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001900
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001901 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00001902 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001903 fprintf(stderr, "inet_dgram_opts failed\n");
Kevin Wolf6e1db572011-06-01 13:29:11 +02001904 ret = -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001905 goto return_err;
1906 }
1907
1908 s->fd = fd;
1909 s->bufcnt = 0;
1910 s->bufptr = 0;
1911 chr->opaque = s;
1912 chr->chr_write = udp_chr_write;
1913 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00001914 chr->chr_close = udp_chr_close;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001915
1916 *_chr = chr;
1917 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001918
1919return_err:
Anthony Liguori7267c092011-08-20 22:09:37 -05001920 g_free(chr);
1921 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001922 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00001923 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001924 }
1925 return ret;
aliguori6f97dba2008-10-31 18:49:55 +00001926}
1927
1928/***********************************************************/
1929/* TCP Net console */
1930
1931typedef struct {
1932 int fd, listen_fd;
1933 int connected;
1934 int max_size;
1935 int do_telnetopt;
1936 int do_nodelay;
1937 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001938 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00001939} TCPCharDriver;
1940
1941static void tcp_chr_accept(void *opaque);
1942
1943static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1944{
1945 TCPCharDriver *s = chr->opaque;
1946 if (s->connected) {
1947 return send_all(s->fd, buf, len);
1948 } else {
1949 /* XXX: indicate an error ? */
1950 return len;
1951 }
1952}
1953
1954static int tcp_chr_read_poll(void *opaque)
1955{
1956 CharDriverState *chr = opaque;
1957 TCPCharDriver *s = chr->opaque;
1958 if (!s->connected)
1959 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05001960 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001961 return s->max_size;
1962}
1963
1964#define IAC 255
1965#define IAC_BREAK 243
1966static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1967 TCPCharDriver *s,
1968 uint8_t *buf, int *size)
1969{
1970 /* Handle any telnet client's basic IAC options to satisfy char by
1971 * char mode with no echo. All IAC options will be removed from
1972 * the buf and the do_telnetopt variable will be used to track the
1973 * state of the width of the IAC information.
1974 *
1975 * IAC commands come in sets of 3 bytes with the exception of the
1976 * "IAC BREAK" command and the double IAC.
1977 */
1978
1979 int i;
1980 int j = 0;
1981
1982 for (i = 0; i < *size; i++) {
1983 if (s->do_telnetopt > 1) {
1984 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1985 /* Double IAC means send an IAC */
1986 if (j != i)
1987 buf[j] = buf[i];
1988 j++;
1989 s->do_telnetopt = 1;
1990 } else {
1991 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1992 /* Handle IAC break commands by sending a serial break */
1993 qemu_chr_event(chr, CHR_EVENT_BREAK);
1994 s->do_telnetopt++;
1995 }
1996 s->do_telnetopt++;
1997 }
1998 if (s->do_telnetopt >= 4) {
1999 s->do_telnetopt = 1;
2000 }
2001 } else {
2002 if ((unsigned char)buf[i] == IAC) {
2003 s->do_telnetopt = 2;
2004 } else {
2005 if (j != i)
2006 buf[j] = buf[i];
2007 j++;
2008 }
2009 }
2010 }
2011 *size = j;
2012}
2013
Mark McLoughlin7d174052009-07-22 09:11:39 +01002014static int tcp_get_msgfd(CharDriverState *chr)
2015{
2016 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002017 int fd = s->msgfd;
2018 s->msgfd = -1;
2019 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002020}
2021
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002022#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002023static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2024{
2025 TCPCharDriver *s = chr->opaque;
2026 struct cmsghdr *cmsg;
2027
2028 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2029 int fd;
2030
2031 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2032 cmsg->cmsg_level != SOL_SOCKET ||
2033 cmsg->cmsg_type != SCM_RIGHTS)
2034 continue;
2035
2036 fd = *((int *)CMSG_DATA(cmsg));
2037 if (fd < 0)
2038 continue;
2039
2040 if (s->msgfd != -1)
2041 close(s->msgfd);
2042 s->msgfd = fd;
2043 }
2044}
2045
Mark McLoughlin9977c892009-07-22 09:11:38 +01002046static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2047{
2048 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002049 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002050 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002051 union {
2052 struct cmsghdr cmsg;
2053 char control[CMSG_SPACE(sizeof(int))];
2054 } msg_control;
2055 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002056
2057 iov[0].iov_base = buf;
2058 iov[0].iov_len = len;
2059
2060 msg.msg_iov = iov;
2061 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002062 msg.msg_control = &msg_control;
2063 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002064
Mark McLoughlin7d174052009-07-22 09:11:39 +01002065 ret = recvmsg(s->fd, &msg, 0);
2066 if (ret > 0 && s->is_unix)
2067 unix_process_msgfd(chr, &msg);
2068
2069 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002070}
2071#else
2072static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2073{
2074 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002075 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002076}
2077#endif
2078
aliguori6f97dba2008-10-31 18:49:55 +00002079static void tcp_chr_read(void *opaque)
2080{
2081 CharDriverState *chr = opaque;
2082 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302083 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002084 int len, size;
2085
2086 if (!s->connected || s->max_size <= 0)
2087 return;
2088 len = sizeof(buf);
2089 if (len > s->max_size)
2090 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002091 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002092 if (size == 0) {
2093 /* connection closed */
2094 s->connected = 0;
2095 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002096 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002097 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002098 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002099 closesocket(s->fd);
2100 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302101 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002102 } else if (size > 0) {
2103 if (s->do_telnetopt)
2104 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2105 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002106 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002107 }
2108}
2109
Blue Swirl68c18d12010-08-15 09:46:24 +00002110#ifndef _WIN32
2111CharDriverState *qemu_chr_open_eventfd(int eventfd)
2112{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002113 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002114}
Blue Swirl68c18d12010-08-15 09:46:24 +00002115#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002116
aliguori6f97dba2008-10-31 18:49:55 +00002117static void tcp_chr_connect(void *opaque)
2118{
2119 CharDriverState *chr = opaque;
2120 TCPCharDriver *s = chr->opaque;
2121
2122 s->connected = 1;
2123 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2124 tcp_chr_read, NULL, chr);
Amit Shah127338e2009-11-03 19:59:56 +05302125 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002126}
2127
2128#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2129static void tcp_chr_telnet_init(int fd)
2130{
2131 char buf[3];
2132 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2133 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2134 send(fd, (char *)buf, 3, 0);
2135 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2136 send(fd, (char *)buf, 3, 0);
2137 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2138 send(fd, (char *)buf, 3, 0);
2139 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2140 send(fd, (char *)buf, 3, 0);
2141}
2142
2143static void socket_set_nodelay(int fd)
2144{
2145 int val = 1;
2146 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2147}
2148
Daniel P. Berrange13661082011-06-23 13:31:42 +01002149static int tcp_chr_add_client(CharDriverState *chr, int fd)
2150{
2151 TCPCharDriver *s = chr->opaque;
2152 if (s->fd != -1)
2153 return -1;
2154
2155 socket_set_nonblock(fd);
2156 if (s->do_nodelay)
2157 socket_set_nodelay(fd);
2158 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002159 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002160 tcp_chr_connect(chr);
2161
2162 return 0;
2163}
2164
aliguori6f97dba2008-10-31 18:49:55 +00002165static void tcp_chr_accept(void *opaque)
2166{
2167 CharDriverState *chr = opaque;
2168 TCPCharDriver *s = chr->opaque;
2169 struct sockaddr_in saddr;
2170#ifndef _WIN32
2171 struct sockaddr_un uaddr;
2172#endif
2173 struct sockaddr *addr;
2174 socklen_t len;
2175 int fd;
2176
2177 for(;;) {
2178#ifndef _WIN32
2179 if (s->is_unix) {
2180 len = sizeof(uaddr);
2181 addr = (struct sockaddr *)&uaddr;
2182 } else
2183#endif
2184 {
2185 len = sizeof(saddr);
2186 addr = (struct sockaddr *)&saddr;
2187 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002188 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002189 if (fd < 0 && errno != EINTR) {
2190 return;
2191 } else if (fd >= 0) {
2192 if (s->do_telnetopt)
2193 tcp_chr_telnet_init(fd);
2194 break;
2195 }
2196 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002197 if (tcp_chr_add_client(chr, fd) < 0)
2198 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002199}
2200
2201static void tcp_chr_close(CharDriverState *chr)
2202{
2203 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002204 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002205 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002206 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002207 }
2208 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002209 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002210 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002211 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002212 g_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302213 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002214}
2215
Kevin Wolf6e1db572011-06-01 13:29:11 +02002216static int qemu_chr_open_socket(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00002217{
2218 CharDriverState *chr = NULL;
2219 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002220 int fd = -1;
2221 int is_listen;
2222 int is_waitconnect;
2223 int do_nodelay;
2224 int is_unix;
2225 int is_telnet;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002226 int ret;
aliguori6f97dba2008-10-31 18:49:55 +00002227
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002228 is_listen = qemu_opt_get_bool(opts, "server", 0);
2229 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2230 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2231 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2232 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002233 if (!is_listen)
2234 is_waitconnect = 0;
2235
Anthony Liguori7267c092011-08-20 22:09:37 -05002236 chr = g_malloc0(sizeof(CharDriverState));
2237 s = g_malloc0(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002238
aliguorif07b6002008-11-11 20:54:09 +00002239 if (is_unix) {
2240 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002241 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002242 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002243 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002244 }
2245 } else {
2246 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002247 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002248 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002249 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002250 }
2251 }
Kevin Wolf6e1db572011-06-01 13:29:11 +02002252 if (fd < 0) {
2253 ret = -errno;
aliguori6f97dba2008-10-31 18:49:55 +00002254 goto fail;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002255 }
aliguori6f97dba2008-10-31 18:49:55 +00002256
2257 if (!is_waitconnect)
2258 socket_set_nonblock(fd);
2259
2260 s->connected = 0;
2261 s->fd = -1;
2262 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002263 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002264 s->is_unix = is_unix;
2265 s->do_nodelay = do_nodelay && !is_unix;
2266
2267 chr->opaque = s;
2268 chr->chr_write = tcp_chr_write;
2269 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002270 chr->get_msgfd = tcp_get_msgfd;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002271 chr->chr_add_client = tcp_chr_add_client;
aliguori6f97dba2008-10-31 18:49:55 +00002272
2273 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002274 s->listen_fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002275 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002276 if (is_telnet)
2277 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002278
aliguori6f97dba2008-10-31 18:49:55 +00002279 } else {
aliguorif07b6002008-11-11 20:54:09 +00002280 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002281 s->fd = fd;
2282 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002283 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002284 }
2285
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002286 /* for "info chardev" monitor command */
Anthony Liguori7267c092011-08-20 22:09:37 -05002287 chr->filename = g_malloc(256);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002288 if (is_unix) {
2289 snprintf(chr->filename, 256, "unix:%s%s",
2290 qemu_opt_get(opts, "path"),
2291 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2292 } else if (is_telnet) {
2293 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2294 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2295 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2296 } else {
2297 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2298 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2299 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2300 }
2301
aliguori6f97dba2008-10-31 18:49:55 +00002302 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002303 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002304 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002305 tcp_chr_accept(chr);
2306 socket_set_nonblock(s->listen_fd);
2307 }
Kevin Wolf6e1db572011-06-01 13:29:11 +02002308
2309 *_chr = chr;
2310 return 0;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002311
aliguori6f97dba2008-10-31 18:49:55 +00002312 fail:
2313 if (fd >= 0)
2314 closesocket(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002315 g_free(s);
2316 g_free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002317 return ret;
aliguori6f97dba2008-10-31 18:49:55 +00002318}
2319
Luiz Capitulino999bd672010-10-22 16:09:05 -02002320/***********************************************************/
2321/* Memory chardev */
2322typedef struct {
2323 size_t outbuf_size;
2324 size_t outbuf_capacity;
2325 uint8_t *outbuf;
2326} MemoryDriver;
2327
2328static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2329{
2330 MemoryDriver *d = chr->opaque;
2331
2332 /* TODO: the QString implementation has the same code, we should
2333 * introduce a generic way to do this in cutils.c */
2334 if (d->outbuf_capacity < d->outbuf_size + len) {
2335 /* grow outbuf */
2336 d->outbuf_capacity += len;
2337 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002338 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002339 }
2340
2341 memcpy(d->outbuf + d->outbuf_size, buf, len);
2342 d->outbuf_size += len;
2343
2344 return len;
2345}
2346
2347void qemu_chr_init_mem(CharDriverState *chr)
2348{
2349 MemoryDriver *d;
2350
Anthony Liguori7267c092011-08-20 22:09:37 -05002351 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002352 d->outbuf_size = 0;
2353 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002354 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002355
2356 memset(chr, 0, sizeof(*chr));
2357 chr->opaque = d;
2358 chr->chr_write = mem_chr_write;
2359}
2360
2361QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2362{
2363 MemoryDriver *d = chr->opaque;
2364 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2365}
2366
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002367/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002368void qemu_chr_close_mem(CharDriverState *chr)
2369{
2370 MemoryDriver *d = chr->opaque;
2371
Anthony Liguori7267c092011-08-20 22:09:37 -05002372 g_free(d->outbuf);
2373 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002374 chr->opaque = NULL;
2375 chr->chr_write = NULL;
2376}
2377
2378size_t qemu_chr_mem_osize(const CharDriverState *chr)
2379{
2380 const MemoryDriver *d = chr->opaque;
2381 return d->outbuf_size;
2382}
2383
Gerd Hoffmann33521632009-12-08 13:11:49 +01002384QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002385{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002386 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002387 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002388 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002389 QemuOpts *opts;
2390
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002391 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002392 if (NULL == opts)
2393 return NULL;
2394
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002395 if (strstart(filename, "mon:", &p)) {
2396 filename = p;
2397 qemu_opt_set(opts, "mux", "on");
2398 }
2399
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002400 if (strcmp(filename, "null") == 0 ||
2401 strcmp(filename, "pty") == 0 ||
2402 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002403 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002404 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002405 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002406 return opts;
2407 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002408 if (strstart(filename, "vc", &p)) {
2409 qemu_opt_set(opts, "backend", "vc");
2410 if (*p == ':') {
2411 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2412 /* pixels */
2413 qemu_opt_set(opts, "width", width);
2414 qemu_opt_set(opts, "height", height);
2415 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2416 /* chars */
2417 qemu_opt_set(opts, "cols", width);
2418 qemu_opt_set(opts, "rows", height);
2419 } else {
2420 goto fail;
2421 }
2422 }
2423 return opts;
2424 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002425 if (strcmp(filename, "con:") == 0) {
2426 qemu_opt_set(opts, "backend", "console");
2427 return opts;
2428 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002429 if (strstart(filename, "COM", NULL)) {
2430 qemu_opt_set(opts, "backend", "serial");
2431 qemu_opt_set(opts, "path", filename);
2432 return opts;
2433 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002434 if (strstart(filename, "file:", &p)) {
2435 qemu_opt_set(opts, "backend", "file");
2436 qemu_opt_set(opts, "path", p);
2437 return opts;
2438 }
2439 if (strstart(filename, "pipe:", &p)) {
2440 qemu_opt_set(opts, "backend", "pipe");
2441 qemu_opt_set(opts, "path", p);
2442 return opts;
2443 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002444 if (strstart(filename, "tcp:", &p) ||
2445 strstart(filename, "telnet:", &p)) {
2446 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2447 host[0] = 0;
2448 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2449 goto fail;
2450 }
2451 qemu_opt_set(opts, "backend", "socket");
2452 qemu_opt_set(opts, "host", host);
2453 qemu_opt_set(opts, "port", port);
2454 if (p[pos] == ',') {
2455 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2456 goto fail;
2457 }
2458 if (strstart(filename, "telnet:", &p))
2459 qemu_opt_set(opts, "telnet", "on");
2460 return opts;
2461 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002462 if (strstart(filename, "udp:", &p)) {
2463 qemu_opt_set(opts, "backend", "udp");
2464 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2465 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002466 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002467 goto fail;
2468 }
2469 }
2470 qemu_opt_set(opts, "host", host);
2471 qemu_opt_set(opts, "port", port);
2472 if (p[pos] == '@') {
2473 p += pos + 1;
2474 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2475 host[0] = 0;
2476 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002477 goto fail;
2478 }
2479 }
2480 qemu_opt_set(opts, "localaddr", host);
2481 qemu_opt_set(opts, "localport", port);
2482 }
2483 return opts;
2484 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002485 if (strstart(filename, "unix:", &p)) {
2486 qemu_opt_set(opts, "backend", "socket");
2487 if (qemu_opts_do_parse(opts, p, "path") != 0)
2488 goto fail;
2489 return opts;
2490 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002491 if (strstart(filename, "/dev/parport", NULL) ||
2492 strstart(filename, "/dev/ppi", NULL)) {
2493 qemu_opt_set(opts, "backend", "parport");
2494 qemu_opt_set(opts, "path", filename);
2495 return opts;
2496 }
2497 if (strstart(filename, "/dev/", NULL)) {
2498 qemu_opt_set(opts, "backend", "tty");
2499 qemu_opt_set(opts, "path", filename);
2500 return opts;
2501 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002502
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002503fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002504 qemu_opts_del(opts);
2505 return NULL;
2506}
2507
2508static const struct {
2509 const char *name;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002510 int (*open)(QemuOpts *opts, CharDriverState **chr);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002511} backend_table[] = {
2512 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002513 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002514 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002515 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002516 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002517#ifdef _WIN32
2518 { .name = "file", .open = qemu_chr_open_win_file_out },
2519 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002520 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002521 { .name = "serial", .open = qemu_chr_open_win },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002522#else
2523 { .name = "file", .open = qemu_chr_open_file_out },
2524 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002525 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002526 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002527#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002528#ifdef CONFIG_BRLAPI
2529 { .name = "braille", .open = chr_baum_init },
2530#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002531#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002532 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2533 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002534 { .name = "tty", .open = qemu_chr_open_tty },
2535#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002536#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2537 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002538 { .name = "parport", .open = qemu_chr_open_pp },
2539#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002540#ifdef CONFIG_SPICE
2541 { .name = "spicevmc", .open = qemu_chr_open_spice },
2542#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002543};
2544
Anthony Liguorif69554b2011-08-15 11:17:37 -05002545CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002546 void (*init)(struct CharDriverState *s))
2547{
2548 CharDriverState *chr;
2549 int i;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002550 int ret;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002551
2552 if (qemu_opts_id(opts) == NULL) {
2553 fprintf(stderr, "chardev: no id specified\n");
2554 return NULL;
2555 }
2556
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002557 if (qemu_opt_get(opts, "backend") == NULL) {
2558 fprintf(stderr, "chardev: \"%s\" missing backend\n",
2559 qemu_opts_id(opts));
2560 return NULL;
2561 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002562 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2563 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2564 break;
2565 }
2566 if (i == ARRAY_SIZE(backend_table)) {
2567 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2568 qemu_opt_get(opts, "backend"));
2569 return NULL;
2570 }
2571
Kevin Wolf6e1db572011-06-01 13:29:11 +02002572 ret = backend_table[i].open(opts, &chr);
2573 if (ret < 0) {
2574 fprintf(stderr, "chardev: opening backend \"%s\" failed: %s\n",
2575 qemu_opt_get(opts, "backend"), strerror(-ret));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002576 return NULL;
2577 }
2578
2579 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05002580 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002581 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002582 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002583
2584 if (qemu_opt_get_bool(opts, "mux", 0)) {
2585 CharDriverState *base = chr;
2586 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05002587 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002588 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2589 chr = qemu_chr_open_mux(base);
2590 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002591 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002592 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002593 } else {
2594 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002595 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002596 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002597 return chr;
2598}
2599
Anthony Liguori27143a42011-08-15 11:17:36 -05002600CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002601{
2602 const char *p;
2603 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002604 QemuOpts *opts;
2605
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002606 if (strstart(filename, "chardev:", &p)) {
2607 return qemu_chr_find(p);
2608 }
2609
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002610 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002611 if (!opts)
2612 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002613
Anthony Liguorif69554b2011-08-15 11:17:37 -05002614 chr = qemu_chr_new_from_opts(opts, init);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002615 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2616 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002617 }
Paolo Bonzini363f8cb2010-12-23 13:42:54 +01002618 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002619 return chr;
2620}
2621
Anthony Liguori15f31512011-08-15 11:17:35 -05002622void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01002623{
2624 if (chr->chr_set_echo) {
2625 chr->chr_set_echo(chr, echo);
2626 }
2627}
2628
Anthony Liguoric9d830e2011-08-15 11:17:32 -05002629void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002630{
2631 if (chr->chr_guest_open) {
2632 chr->chr_guest_open(chr);
2633 }
2634}
2635
Anthony Liguori28178222011-08-15 11:17:33 -05002636void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002637{
2638 if (chr->chr_guest_close) {
2639 chr->chr_guest_close(chr);
2640 }
2641}
2642
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002643void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00002644{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002645 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002646 if (chr->chr_close)
2647 chr->chr_close(chr);
Anthony Liguori7267c092011-08-20 22:09:37 -05002648 g_free(chr->filename);
2649 g_free(chr->label);
2650 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002651}
2652
Luiz Capitulino588b3832009-12-10 17:16:08 -02002653static void qemu_chr_qlist_iter(QObject *obj, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002654{
Luiz Capitulino588b3832009-12-10 17:16:08 -02002655 QDict *chr_dict;
2656 Monitor *mon = opaque;
2657
2658 chr_dict = qobject_to_qdict(obj);
2659 monitor_printf(mon, "%s: filename=%s\n", qdict_get_str(chr_dict, "label"),
2660 qdict_get_str(chr_dict, "filename"));
2661}
2662
2663void qemu_chr_info_print(Monitor *mon, const QObject *ret_data)
2664{
2665 qlist_iter(qobject_to_qlist(ret_data), qemu_chr_qlist_iter, mon);
2666}
2667
Luiz Capitulino588b3832009-12-10 17:16:08 -02002668void qemu_chr_info(Monitor *mon, QObject **ret_data)
2669{
2670 QList *chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002671 CharDriverState *chr;
2672
Luiz Capitulino588b3832009-12-10 17:16:08 -02002673 chr_list = qlist_new();
2674
Blue Swirl72cf2d42009-09-12 07:36:22 +00002675 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulino588b3832009-12-10 17:16:08 -02002676 QObject *obj = qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2677 chr->label, chr->filename);
2678 qlist_append_obj(chr_list, obj);
aliguori6f97dba2008-10-31 18:49:55 +00002679 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002680
2681 *ret_data = QOBJECT(chr_list);
aliguori6f97dba2008-10-31 18:49:55 +00002682}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002683
2684CharDriverState *qemu_chr_find(const char *name)
2685{
2686 CharDriverState *chr;
2687
Blue Swirl72cf2d42009-09-12 07:36:22 +00002688 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002689 if (strcmp(chr->label, name) != 0)
2690 continue;
2691 return chr;
2692 }
2693 return NULL;
2694}