blob: dcf706592b3db8da21b1ba66d3bfb271fb5d0466 [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
142int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
143{
144 return s->chr_write(s, buf, len);
145}
146
147int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
148{
149 if (!s->chr_ioctl)
150 return -ENOTSUP;
151 return s->chr_ioctl(s, cmd, arg);
152}
153
154int qemu_chr_can_read(CharDriverState *s)
155{
156 if (!s->chr_can_read)
157 return 0;
158 return s->chr_can_read(s->handler_opaque);
159}
160
161void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
162{
163 s->chr_read(s->handler_opaque, buf, len);
164}
165
Mark McLoughlin7d174052009-07-22 09:11:39 +0100166int qemu_chr_get_msgfd(CharDriverState *s)
167{
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
182void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
183{
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);
188 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
189 va_end(ap);
190}
191
192void qemu_chr_send_event(CharDriverState *s, int event)
193{
194 if (s->chr_send_event)
195 s->chr_send_event(s, event);
196}
197
198void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100199 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000200 IOReadHandler *fd_read,
201 IOEventHandler *fd_event,
202 void *opaque)
203{
Amit Shahda7d9982011-04-25 15:18:22 +0530204 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530205 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900206 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530207 }
aliguori6f97dba2008-10-31 18:49:55 +0000208 s->chr_can_read = fd_can_read;
209 s->chr_read = fd_read;
210 s->chr_event = fd_event;
211 s->handler_opaque = opaque;
212 if (s->chr_update_read_handler)
213 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200214
215 /* We're connecting to an already opened device, so let's make sure we
216 also get the open event */
217 if (s->opened) {
218 qemu_chr_generic_open(s);
219 }
aliguori6f97dba2008-10-31 18:49:55 +0000220}
221
222static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
223{
224 return len;
225}
226
Kevin Wolf6e1db572011-06-01 13:29:11 +0200227static int qemu_chr_open_null(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000228{
229 CharDriverState *chr;
230
231 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000232 chr->chr_write = null_chr_write;
Kevin Wolf6e1db572011-06-01 13:29:11 +0200233
234 *_chr= chr;
235 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000236}
237
238/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000239#define MAX_MUX 4
240#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
241#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
242typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100243 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000244 IOReadHandler *chr_read[MAX_MUX];
245 IOEventHandler *chr_event[MAX_MUX];
246 void *ext_opaque[MAX_MUX];
247 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200248 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000249 int mux_cnt;
250 int term_got_escape;
251 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000252 /* Intermediate input buffer allows to catch escape sequences even if the
253 currently active device is not accepting any input - but only until it
254 is full as well. */
255 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
256 int prod[MAX_MUX];
257 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200258 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200259 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200260 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000261} MuxDriver;
262
263
264static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
265{
266 MuxDriver *d = chr->opaque;
267 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200268 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000269 ret = d->drv->chr_write(d->drv, buf, len);
270 } else {
271 int i;
272
273 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200274 for (i = 0; i < len; i++) {
275 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000276 char buf1[64];
277 int64_t ti;
278 int secs;
279
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100280 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200281 if (d->timestamps_start == -1)
282 d->timestamps_start = ti;
283 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000284 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000285 snprintf(buf1, sizeof(buf1),
286 "[%02d:%02d:%02d.%03d] ",
287 secs / 3600,
288 (secs / 60) % 60,
289 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000290 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000291 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200292 d->linestart = 0;
293 }
294 ret += d->drv->chr_write(d->drv, buf+i, 1);
295 if (buf[i] == '\n') {
296 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000297 }
298 }
299 }
300 return ret;
301}
302
303static const char * const mux_help[] = {
304 "% h print this help\n\r",
305 "% x exit emulator\n\r",
306 "% s save disk data back to file (if -snapshot)\n\r",
307 "% t toggle console timestamps\n\r"
308 "% b send break (magic sysrq)\n\r",
309 "% c switch between console and monitor\n\r",
310 "% % sends %\n\r",
311 NULL
312};
313
314int term_escape_char = 0x01; /* ctrl-a is used for escape */
315static void mux_print_help(CharDriverState *chr)
316{
317 int i, j;
318 char ebuf[15] = "Escape-Char";
319 char cbuf[50] = "\n\r";
320
321 if (term_escape_char > 0 && term_escape_char < 26) {
322 snprintf(cbuf, sizeof(cbuf), "\n\r");
323 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
324 } else {
325 snprintf(cbuf, sizeof(cbuf),
326 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
327 term_escape_char);
328 }
329 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
330 for (i = 0; mux_help[i] != NULL; i++) {
331 for (j=0; mux_help[i][j] != '\0'; j++) {
332 if (mux_help[i][j] == '%')
333 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
334 else
335 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
336 }
337 }
338}
339
aliguori2724b182009-03-05 23:01:47 +0000340static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
341{
342 if (d->chr_event[mux_nr])
343 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
344}
345
aliguori6f97dba2008-10-31 18:49:55 +0000346static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
347{
348 if (d->term_got_escape) {
349 d->term_got_escape = 0;
350 if (ch == term_escape_char)
351 goto send_char;
352 switch(ch) {
353 case '?':
354 case 'h':
355 mux_print_help(chr);
356 break;
357 case 'x':
358 {
359 const char *term = "QEMU: Terminated\n\r";
360 chr->chr_write(chr,(uint8_t *)term,strlen(term));
361 exit(0);
362 break;
363 }
364 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200365 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000366 break;
367 case 'b':
368 qemu_chr_event(chr, CHR_EVENT_BREAK);
369 break;
370 case 'c':
371 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200372 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
373 d->focus++;
374 if (d->focus >= d->mux_cnt)
375 d->focus = 0;
376 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000377 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200378 case 't':
379 d->timestamps = !d->timestamps;
380 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200381 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200382 break;
aliguori6f97dba2008-10-31 18:49:55 +0000383 }
384 } else if (ch == term_escape_char) {
385 d->term_got_escape = 1;
386 } else {
387 send_char:
388 return 1;
389 }
390 return 0;
391}
392
393static void mux_chr_accept_input(CharDriverState *chr)
394{
aliguori6f97dba2008-10-31 18:49:55 +0000395 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200396 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000397
aliguoria80bf992009-03-05 23:00:02 +0000398 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000399 d->chr_can_read[m] &&
400 d->chr_can_read[m](d->ext_opaque[m])) {
401 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000402 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000403 }
404}
405
406static int mux_chr_can_read(void *opaque)
407{
408 CharDriverState *chr = opaque;
409 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200410 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000411
aliguoria80bf992009-03-05 23:00:02 +0000412 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000413 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000414 if (d->chr_can_read[m])
415 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000416 return 0;
417}
418
419static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
420{
421 CharDriverState *chr = opaque;
422 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200423 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000424 int i;
425
426 mux_chr_accept_input (opaque);
427
428 for(i = 0; i < size; i++)
429 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000430 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000431 d->chr_can_read[m] &&
432 d->chr_can_read[m](d->ext_opaque[m]))
433 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
434 else
aliguoria80bf992009-03-05 23:00:02 +0000435 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000436 }
437}
438
439static void mux_chr_event(void *opaque, int event)
440{
441 CharDriverState *chr = opaque;
442 MuxDriver *d = chr->opaque;
443 int i;
444
445 /* Send the event to all registered listeners */
446 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000447 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000448}
449
450static void mux_chr_update_read_handler(CharDriverState *chr)
451{
452 MuxDriver *d = chr->opaque;
453
454 if (d->mux_cnt >= MAX_MUX) {
455 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
456 return;
457 }
458 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
459 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
460 d->chr_read[d->mux_cnt] = chr->chr_read;
461 d->chr_event[d->mux_cnt] = chr->chr_event;
462 /* Fix up the real driver with mux routines */
463 if (d->mux_cnt == 0) {
464 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
465 mux_chr_event, chr);
466 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200467 if (d->focus != -1) {
468 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200469 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200470 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000471 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200472 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000473}
474
475static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
476{
477 CharDriverState *chr;
478 MuxDriver *d;
479
480 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000481 d = qemu_mallocz(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000482
483 chr->opaque = d;
484 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200485 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000486 chr->chr_write = mux_chr_write;
487 chr->chr_update_read_handler = mux_chr_update_read_handler;
488 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100489 /* Frontend guest-open / -close notification is not support with muxes */
490 chr->chr_guest_open = NULL;
491 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200492
493 /* Muxes are always open on creation */
494 qemu_chr_generic_open(chr);
495
aliguori6f97dba2008-10-31 18:49:55 +0000496 return chr;
497}
498
499
500#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000501int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000502{
503 int ret, len;
504
505 len = len1;
506 while (len > 0) {
507 ret = send(fd, buf, len, 0);
508 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000509 errno = WSAGetLastError();
510 if (errno != WSAEWOULDBLOCK) {
511 return -1;
512 }
513 } else if (ret == 0) {
514 break;
515 } else {
516 buf += ret;
517 len -= ret;
518 }
519 }
520 return len1 - len;
521}
522
523#else
524
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100525int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000526{
527 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100528 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000529
530 len = len1;
531 while (len > 0) {
532 ret = write(fd, buf, len);
533 if (ret < 0) {
534 if (errno != EINTR && errno != EAGAIN)
535 return -1;
536 } else if (ret == 0) {
537 break;
538 } else {
539 buf += ret;
540 len -= ret;
541 }
542 }
543 return len1 - len;
544}
aliguori6f97dba2008-10-31 18:49:55 +0000545#endif /* !_WIN32 */
546
547#ifndef _WIN32
548
549typedef struct {
550 int fd_in, fd_out;
551 int max_size;
552} FDCharDriver;
553
554#define STDIO_MAX_CLIENTS 1
555static int stdio_nb_clients = 0;
556
557static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
558{
559 FDCharDriver *s = chr->opaque;
560 return send_all(s->fd_out, buf, len);
561}
562
563static int fd_chr_read_poll(void *opaque)
564{
565 CharDriverState *chr = opaque;
566 FDCharDriver *s = chr->opaque;
567
568 s->max_size = qemu_chr_can_read(chr);
569 return s->max_size;
570}
571
572static void fd_chr_read(void *opaque)
573{
574 CharDriverState *chr = opaque;
575 FDCharDriver *s = chr->opaque;
576 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530577 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000578
579 len = sizeof(buf);
580 if (len > s->max_size)
581 len = s->max_size;
582 if (len == 0)
583 return;
584 size = read(s->fd_in, buf, len);
585 if (size == 0) {
586 /* FD has been closed. Remove it from the active list. */
587 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530588 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000589 return;
590 }
591 if (size > 0) {
592 qemu_chr_read(chr, buf, size);
593 }
594}
595
596static void fd_chr_update_read_handler(CharDriverState *chr)
597{
598 FDCharDriver *s = chr->opaque;
599
600 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500601 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000602 } else {
603 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
604 fd_chr_read, NULL, chr);
605 }
606 }
607}
608
609static void fd_chr_close(struct CharDriverState *chr)
610{
611 FDCharDriver *s = chr->opaque;
612
613 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500614 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000615 } else {
616 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
617 }
618 }
619
620 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530621 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000622}
623
624/* open a character device to a unix fd */
625static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
626{
627 CharDriverState *chr;
628 FDCharDriver *s;
629
630 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000631 s = qemu_mallocz(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000632 s->fd_in = fd_in;
633 s->fd_out = fd_out;
634 chr->opaque = s;
635 chr->chr_write = fd_chr_write;
636 chr->chr_update_read_handler = fd_chr_update_read_handler;
637 chr->chr_close = fd_chr_close;
638
Amit Shah127338e2009-11-03 19:59:56 +0530639 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000640
641 return chr;
642}
643
Kevin Wolf6e1db572011-06-01 13:29:11 +0200644static int qemu_chr_open_file_out(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000645{
646 int fd_out;
647
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100648 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200649 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Kevin Wolf6e1db572011-06-01 13:29:11 +0200650 if (fd_out < 0) {
651 return -errno;
652 }
653
654 *_chr = qemu_chr_open_fd(-1, fd_out);
655 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000656}
657
Kevin Wolf6e1db572011-06-01 13:29:11 +0200658static int qemu_chr_open_pipe(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000659{
660 int fd_in, fd_out;
661 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200662 const char *filename = qemu_opt_get(opts, "path");
663
664 if (filename == NULL) {
665 fprintf(stderr, "chardev: pipe: no filename given\n");
Kevin Wolf6e1db572011-06-01 13:29:11 +0200666 return -EINVAL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200667 }
aliguori6f97dba2008-10-31 18:49:55 +0000668
669 snprintf(filename_in, 256, "%s.in", filename);
670 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100671 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
672 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000673 if (fd_in < 0 || fd_out < 0) {
674 if (fd_in >= 0)
675 close(fd_in);
676 if (fd_out >= 0)
677 close(fd_out);
Kevin Wolf6e1db572011-06-01 13:29:11 +0200678 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
679 if (fd_in < 0) {
680 return -errno;
681 }
aliguori6f97dba2008-10-31 18:49:55 +0000682 }
Kevin Wolf6e1db572011-06-01 13:29:11 +0200683
684 *_chr = qemu_chr_open_fd(fd_in, fd_out);
685 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000686}
687
688
689/* for STDIO, we handle the case where several clients use it
690 (nographic mode) */
691
692#define TERM_FIFO_MAX_SIZE 1
693
694static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
695static int term_fifo_size;
696
697static int stdio_read_poll(void *opaque)
698{
699 CharDriverState *chr = opaque;
700
701 /* try to flush the queue if needed */
702 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
703 qemu_chr_read(chr, term_fifo, 1);
704 term_fifo_size = 0;
705 }
706 /* see if we can absorb more chars */
707 if (term_fifo_size == 0)
708 return 1;
709 else
710 return 0;
711}
712
713static void stdio_read(void *opaque)
714{
715 int size;
716 uint8_t buf[1];
717 CharDriverState *chr = opaque;
718
719 size = read(0, buf, 1);
720 if (size == 0) {
721 /* stdin has been closed. Remove it from the active list. */
722 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530723 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000724 return;
725 }
726 if (size > 0) {
727 if (qemu_chr_can_read(chr) > 0) {
728 qemu_chr_read(chr, buf, 1);
729 } else if (term_fifo_size == 0) {
730 term_fifo[term_fifo_size++] = buf[0];
731 }
732 }
733}
734
735/* init terminal so that we can grab keys */
736static struct termios oldtty;
737static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100738static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000739
740static void term_exit(void)
741{
742 tcsetattr (0, TCSANOW, &oldtty);
743 fcntl(0, F_SETFL, old_fd0_flags);
744}
745
Paolo Bonzinibb002512010-12-23 13:42:50 +0100746static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000747{
748 struct termios tty;
749
Paolo Bonzini03693642010-12-23 13:42:49 +0100750 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100751 if (!echo) {
752 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000753 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100754 tty.c_oflag |= OPOST;
755 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
756 tty.c_cflag &= ~(CSIZE|PARENB);
757 tty.c_cflag |= CS8;
758 tty.c_cc[VMIN] = 1;
759 tty.c_cc[VTIME] = 0;
760 }
aliguori6f97dba2008-10-31 18:49:55 +0000761 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100762 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000763 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000764
765 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000766}
767
768static void qemu_chr_close_stdio(struct CharDriverState *chr)
769{
770 term_exit();
771 stdio_nb_clients--;
772 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
773 fd_chr_close(chr);
774}
775
Kevin Wolf6e1db572011-06-01 13:29:11 +0200776static int qemu_chr_open_stdio(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000777{
778 CharDriverState *chr;
779
Kevin Wolf6e1db572011-06-01 13:29:11 +0200780 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
781 return -EBUSY;
782 }
783
Paolo Bonzini03693642010-12-23 13:42:49 +0100784 if (stdio_nb_clients == 0) {
785 old_fd0_flags = fcntl(0, F_GETFL);
786 tcgetattr (0, &oldtty);
787 fcntl(0, F_SETFL, O_NONBLOCK);
788 atexit(term_exit);
789 }
790
aliguori6f97dba2008-10-31 18:49:55 +0000791 chr = qemu_chr_open_fd(0, 1);
792 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100793 chr->chr_set_echo = qemu_chr_set_echo_stdio;
aliguori6f97dba2008-10-31 18:49:55 +0000794 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
795 stdio_nb_clients++;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100796 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
797 display_type != DT_NOGRAPHIC);
798 qemu_chr_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000799
Kevin Wolf6e1db572011-06-01 13:29:11 +0200800 *_chr = chr;
801 return 0;
aliguori6f97dba2008-10-31 18:49:55 +0000802}
803
804#ifdef __sun__
805/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000806static int openpty(int *amaster, int *aslave, char *name,
807 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000808{
809 const char *slave;
810 int mfd = -1, sfd = -1;
811
812 *amaster = *aslave = -1;
813
814 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
815 if (mfd < 0)
816 goto err;
817
818 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
819 goto err;
820
821 if ((slave = ptsname(mfd)) == NULL)
822 goto err;
823
824 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
825 goto err;
826
827 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
828 (termp != NULL && tcgetattr(sfd, termp) < 0))
829 goto err;
830
831 if (amaster)
832 *amaster = mfd;
833 if (aslave)
834 *aslave = sfd;
835 if (winp)
836 ioctl(sfd, TIOCSWINSZ, winp);
837
838 return 0;
839
840err:
841 if (sfd != -1)
842 close(sfd);
843 close(mfd);
844 return -1;
845}
846
blueswir13f4cb3d2009-04-13 16:31:01 +0000847static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000848{
849 termios_p->c_iflag &=
850 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
851 termios_p->c_oflag &= ~OPOST;
852 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
853 termios_p->c_cflag &= ~(CSIZE|PARENB);
854 termios_p->c_cflag |= CS8;
855
856 termios_p->c_cc[VMIN] = 0;
857 termios_p->c_cc[VTIME] = 0;
858}
859#endif
860
861#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100862 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
863 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000864
865typedef struct {
866 int fd;
867 int connected;
868 int polling;
869 int read_bytes;
870 QEMUTimer *timer;
871} PtyCharDriver;
872
873static void pty_chr_update_read_handler(CharDriverState *chr);
874static void pty_chr_state(CharDriverState *chr, int connected);
875
876static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
877{
878 PtyCharDriver *s = chr->opaque;
879
880 if (!s->connected) {
881 /* guest sends data, check for (re-)connect */
882 pty_chr_update_read_handler(chr);
883 return 0;
884 }
885 return send_all(s->fd, buf, len);
886}
887
888static int pty_chr_read_poll(void *opaque)
889{
890 CharDriverState *chr = opaque;
891 PtyCharDriver *s = chr->opaque;
892
893 s->read_bytes = qemu_chr_can_read(chr);
894 return s->read_bytes;
895}
896
897static void pty_chr_read(void *opaque)
898{
899 CharDriverState *chr = opaque;
900 PtyCharDriver *s = chr->opaque;
901 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530902 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000903
904 len = sizeof(buf);
905 if (len > s->read_bytes)
906 len = s->read_bytes;
907 if (len == 0)
908 return;
909 size = read(s->fd, buf, len);
910 if ((size == -1 && errno == EIO) ||
911 (size == 0)) {
912 pty_chr_state(chr, 0);
913 return;
914 }
915 if (size > 0) {
916 pty_chr_state(chr, 1);
917 qemu_chr_read(chr, buf, size);
918 }
919}
920
921static void pty_chr_update_read_handler(CharDriverState *chr)
922{
923 PtyCharDriver *s = chr->opaque;
924
925 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
926 pty_chr_read, NULL, chr);
927 s->polling = 1;
928 /*
929 * Short timeout here: just need wait long enougth that qemu makes
930 * it through the poll loop once. When reconnected we want a
931 * short timeout so we notice it almost instantly. Otherwise
932 * read() gives us -EIO instantly, making pty_chr_state() reset the
933 * timeout to the normal (much longer) poll interval before the
934 * timer triggers.
935 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100936 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +0000937}
938
939static void pty_chr_state(CharDriverState *chr, int connected)
940{
941 PtyCharDriver *s = chr->opaque;
942
943 if (!connected) {
944 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
945 s->connected = 0;
946 s->polling = 0;
947 /* (re-)connect poll interval for idle guests: once per second.
948 * We check more frequently in case the guests sends data to
949 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100950 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +0000951 } else {
952 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530953 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000954 s->connected = 1;
955 }
956}
957
958static void pty_chr_timer(void *opaque)
959{
960 struct CharDriverState *chr = opaque;
961 PtyCharDriver *s = chr->opaque;
962
963 if (s->connected)
964 return;
965 if (s->polling) {
966 /* If we arrive here without polling being cleared due
967 * read returning -EIO, then we are (re-)connected */
968 pty_chr_state(chr, 1);
969 return;
970 }
971
972 /* Next poll ... */
973 pty_chr_update_read_handler(chr);
974}
975
976static void pty_chr_close(struct CharDriverState *chr)
977{
978 PtyCharDriver *s = chr->opaque;
979
980 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
981 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000982 qemu_del_timer(s->timer);
983 qemu_free_timer(s->timer);
aliguori6f97dba2008-10-31 18:49:55 +0000984 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530985 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000986}
987
Kevin Wolf6e1db572011-06-01 13:29:11 +0200988static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +0000989{
990 CharDriverState *chr;
991 PtyCharDriver *s;
992 struct termios tty;
993 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000994#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000995 char pty_name[PATH_MAX];
996#define q_ptsname(x) pty_name
997#else
998 char *pty_name = NULL;
999#define q_ptsname(x) ptsname(x)
1000#endif
1001
1002 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001003 s = qemu_mallocz(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001004
1005 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Kevin Wolf6e1db572011-06-01 13:29:11 +02001006 return -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001007 }
1008
1009 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001010 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001011 cfmakeraw(&tty);
1012 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1013 close(slave_fd);
1014
1015 len = strlen(q_ptsname(s->fd)) + 5;
1016 chr->filename = qemu_malloc(len);
1017 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02001018 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +00001019 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
1020
1021 chr->opaque = s;
1022 chr->chr_write = pty_chr_write;
1023 chr->chr_update_read_handler = pty_chr_update_read_handler;
1024 chr->chr_close = pty_chr_close;
1025
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001026 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001027
Kevin Wolf6e1db572011-06-01 13:29:11 +02001028 *_chr = chr;
1029 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001030}
1031
1032static void tty_serial_init(int fd, int speed,
1033 int parity, int data_bits, int stop_bits)
1034{
1035 struct termios tty;
1036 speed_t spd;
1037
1038#if 0
1039 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1040 speed, parity, data_bits, stop_bits);
1041#endif
1042 tcgetattr (fd, &tty);
1043
Stefan Weil45eea132009-10-26 16:10:10 +01001044#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1045 speed = speed * 10 / 11;
1046 do {
1047 check_speed(50);
1048 check_speed(75);
1049 check_speed(110);
1050 check_speed(134);
1051 check_speed(150);
1052 check_speed(200);
1053 check_speed(300);
1054 check_speed(600);
1055 check_speed(1200);
1056 check_speed(1800);
1057 check_speed(2400);
1058 check_speed(4800);
1059 check_speed(9600);
1060 check_speed(19200);
1061 check_speed(38400);
1062 /* Non-Posix values follow. They may be unsupported on some systems. */
1063 check_speed(57600);
1064 check_speed(115200);
1065#ifdef B230400
1066 check_speed(230400);
1067#endif
1068#ifdef B460800
1069 check_speed(460800);
1070#endif
1071#ifdef B500000
1072 check_speed(500000);
1073#endif
1074#ifdef B576000
1075 check_speed(576000);
1076#endif
1077#ifdef B921600
1078 check_speed(921600);
1079#endif
1080#ifdef B1000000
1081 check_speed(1000000);
1082#endif
1083#ifdef B1152000
1084 check_speed(1152000);
1085#endif
1086#ifdef B1500000
1087 check_speed(1500000);
1088#endif
1089#ifdef B2000000
1090 check_speed(2000000);
1091#endif
1092#ifdef B2500000
1093 check_speed(2500000);
1094#endif
1095#ifdef B3000000
1096 check_speed(3000000);
1097#endif
1098#ifdef B3500000
1099 check_speed(3500000);
1100#endif
1101#ifdef B4000000
1102 check_speed(4000000);
1103#endif
aliguori6f97dba2008-10-31 18:49:55 +00001104 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001105 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001106
1107 cfsetispeed(&tty, spd);
1108 cfsetospeed(&tty, spd);
1109
1110 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1111 |INLCR|IGNCR|ICRNL|IXON);
1112 tty.c_oflag |= OPOST;
1113 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1114 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1115 switch(data_bits) {
1116 default:
1117 case 8:
1118 tty.c_cflag |= CS8;
1119 break;
1120 case 7:
1121 tty.c_cflag |= CS7;
1122 break;
1123 case 6:
1124 tty.c_cflag |= CS6;
1125 break;
1126 case 5:
1127 tty.c_cflag |= CS5;
1128 break;
1129 }
1130 switch(parity) {
1131 default:
1132 case 'N':
1133 break;
1134 case 'E':
1135 tty.c_cflag |= PARENB;
1136 break;
1137 case 'O':
1138 tty.c_cflag |= PARENB | PARODD;
1139 break;
1140 }
1141 if (stop_bits == 2)
1142 tty.c_cflag |= CSTOPB;
1143
1144 tcsetattr (fd, TCSANOW, &tty);
1145}
1146
1147static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1148{
1149 FDCharDriver *s = chr->opaque;
1150
1151 switch(cmd) {
1152 case CHR_IOCTL_SERIAL_SET_PARAMS:
1153 {
1154 QEMUSerialSetParams *ssp = arg;
1155 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1156 ssp->data_bits, ssp->stop_bits);
1157 }
1158 break;
1159 case CHR_IOCTL_SERIAL_SET_BREAK:
1160 {
1161 int enable = *(int *)arg;
1162 if (enable)
1163 tcsendbreak(s->fd_in, 1);
1164 }
1165 break;
1166 case CHR_IOCTL_SERIAL_GET_TIOCM:
1167 {
1168 int sarg = 0;
1169 int *targ = (int *)arg;
1170 ioctl(s->fd_in, TIOCMGET, &sarg);
1171 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001172 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001173 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001174 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001175 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001176 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001177 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001178 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001179 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001180 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001181 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001182 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001183 *targ |= CHR_TIOCM_RTS;
1184 }
1185 break;
1186 case CHR_IOCTL_SERIAL_SET_TIOCM:
1187 {
1188 int sarg = *(int *)arg;
1189 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001190 ioctl(s->fd_in, TIOCMGET, &targ);
1191 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1192 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1193 if (sarg & CHR_TIOCM_CTS)
1194 targ |= TIOCM_CTS;
1195 if (sarg & CHR_TIOCM_CAR)
1196 targ |= TIOCM_CAR;
1197 if (sarg & CHR_TIOCM_DSR)
1198 targ |= TIOCM_DSR;
1199 if (sarg & CHR_TIOCM_RI)
1200 targ |= TIOCM_RI;
1201 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001202 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001203 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001204 targ |= TIOCM_RTS;
1205 ioctl(s->fd_in, TIOCMSET, &targ);
1206 }
1207 break;
1208 default:
1209 return -ENOTSUP;
1210 }
1211 return 0;
1212}
1213
David Ahern4266a132010-02-10 18:27:17 -07001214static void qemu_chr_close_tty(CharDriverState *chr)
1215{
1216 FDCharDriver *s = chr->opaque;
1217 int fd = -1;
1218
1219 if (s) {
1220 fd = s->fd_in;
1221 }
1222
1223 fd_chr_close(chr);
1224
1225 if (fd >= 0) {
1226 close(fd);
1227 }
1228}
1229
Kevin Wolf6e1db572011-06-01 13:29:11 +02001230static int qemu_chr_open_tty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001231{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001232 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001233 CharDriverState *chr;
1234 int fd;
1235
Kevin Wolf6e1db572011-06-01 13:29:11 +02001236 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001237 if (fd < 0) {
Kevin Wolf6e1db572011-06-01 13:29:11 +02001238 return -errno;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001239 }
aliguori6f97dba2008-10-31 18:49:55 +00001240 tty_serial_init(fd, 115200, 'N', 8, 1);
1241 chr = qemu_chr_open_fd(fd, fd);
aliguori6f97dba2008-10-31 18:49:55 +00001242 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001243 chr->chr_close = qemu_chr_close_tty;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001244
1245 *_chr = chr;
1246 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001247}
1248#else /* ! __linux__ && ! __sun__ */
Kevin Wolf6e1db572011-06-01 13:29:11 +02001249static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001250{
Kevin Wolf6e1db572011-06-01 13:29:11 +02001251 return -ENOTSUP;
aliguori6f97dba2008-10-31 18:49:55 +00001252}
1253#endif /* __linux__ || __sun__ */
1254
1255#if defined(__linux__)
1256typedef struct {
1257 int fd;
1258 int mode;
1259} ParallelCharDriver;
1260
1261static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1262{
1263 if (s->mode != mode) {
1264 int m = mode;
1265 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1266 return 0;
1267 s->mode = mode;
1268 }
1269 return 1;
1270}
1271
1272static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1273{
1274 ParallelCharDriver *drv = chr->opaque;
1275 int fd = drv->fd;
1276 uint8_t b;
1277
1278 switch(cmd) {
1279 case CHR_IOCTL_PP_READ_DATA:
1280 if (ioctl(fd, PPRDATA, &b) < 0)
1281 return -ENOTSUP;
1282 *(uint8_t *)arg = b;
1283 break;
1284 case CHR_IOCTL_PP_WRITE_DATA:
1285 b = *(uint8_t *)arg;
1286 if (ioctl(fd, PPWDATA, &b) < 0)
1287 return -ENOTSUP;
1288 break;
1289 case CHR_IOCTL_PP_READ_CONTROL:
1290 if (ioctl(fd, PPRCONTROL, &b) < 0)
1291 return -ENOTSUP;
1292 /* Linux gives only the lowest bits, and no way to know data
1293 direction! For better compatibility set the fixed upper
1294 bits. */
1295 *(uint8_t *)arg = b | 0xc0;
1296 break;
1297 case CHR_IOCTL_PP_WRITE_CONTROL:
1298 b = *(uint8_t *)arg;
1299 if (ioctl(fd, PPWCONTROL, &b) < 0)
1300 return -ENOTSUP;
1301 break;
1302 case CHR_IOCTL_PP_READ_STATUS:
1303 if (ioctl(fd, PPRSTATUS, &b) < 0)
1304 return -ENOTSUP;
1305 *(uint8_t *)arg = b;
1306 break;
1307 case CHR_IOCTL_PP_DATA_DIR:
1308 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1309 return -ENOTSUP;
1310 break;
1311 case CHR_IOCTL_PP_EPP_READ_ADDR:
1312 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1313 struct ParallelIOArg *parg = arg;
1314 int n = read(fd, parg->buffer, parg->count);
1315 if (n != parg->count) {
1316 return -EIO;
1317 }
1318 }
1319 break;
1320 case CHR_IOCTL_PP_EPP_READ:
1321 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1322 struct ParallelIOArg *parg = arg;
1323 int n = read(fd, parg->buffer, parg->count);
1324 if (n != parg->count) {
1325 return -EIO;
1326 }
1327 }
1328 break;
1329 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1330 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1331 struct ParallelIOArg *parg = arg;
1332 int n = write(fd, parg->buffer, parg->count);
1333 if (n != parg->count) {
1334 return -EIO;
1335 }
1336 }
1337 break;
1338 case CHR_IOCTL_PP_EPP_WRITE:
1339 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1340 struct ParallelIOArg *parg = arg;
1341 int n = write(fd, parg->buffer, parg->count);
1342 if (n != parg->count) {
1343 return -EIO;
1344 }
1345 }
1346 break;
1347 default:
1348 return -ENOTSUP;
1349 }
1350 return 0;
1351}
1352
1353static void pp_close(CharDriverState *chr)
1354{
1355 ParallelCharDriver *drv = chr->opaque;
1356 int fd = drv->fd;
1357
1358 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1359 ioctl(fd, PPRELEASE);
1360 close(fd);
1361 qemu_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301362 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001363}
1364
Kevin Wolf6e1db572011-06-01 13:29:11 +02001365static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001366{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001367 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001368 CharDriverState *chr;
1369 ParallelCharDriver *drv;
1370 int fd;
1371
1372 TFR(fd = open(filename, O_RDWR));
Kevin Wolf6e1db572011-06-01 13:29:11 +02001373 if (fd < 0) {
1374 return -errno;
1375 }
aliguori6f97dba2008-10-31 18:49:55 +00001376
1377 if (ioctl(fd, PPCLAIM) < 0) {
1378 close(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001379 return -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001380 }
1381
1382 drv = qemu_mallocz(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001383 drv->fd = fd;
1384 drv->mode = IEEE1284_MODE_COMPAT;
1385
1386 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001387 chr->chr_write = null_chr_write;
1388 chr->chr_ioctl = pp_ioctl;
1389 chr->chr_close = pp_close;
1390 chr->opaque = drv;
1391
Amit Shah127338e2009-11-03 19:59:56 +05301392 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001393
Kevin Wolf6e1db572011-06-01 13:29:11 +02001394 *_chr = chr;
1395 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001396}
1397#endif /* __linux__ */
1398
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001399#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001400static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1401{
Stefan Weile0efb992011-02-23 19:09:16 +01001402 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001403 uint8_t b;
1404
1405 switch(cmd) {
1406 case CHR_IOCTL_PP_READ_DATA:
1407 if (ioctl(fd, PPIGDATA, &b) < 0)
1408 return -ENOTSUP;
1409 *(uint8_t *)arg = b;
1410 break;
1411 case CHR_IOCTL_PP_WRITE_DATA:
1412 b = *(uint8_t *)arg;
1413 if (ioctl(fd, PPISDATA, &b) < 0)
1414 return -ENOTSUP;
1415 break;
1416 case CHR_IOCTL_PP_READ_CONTROL:
1417 if (ioctl(fd, PPIGCTRL, &b) < 0)
1418 return -ENOTSUP;
1419 *(uint8_t *)arg = b;
1420 break;
1421 case CHR_IOCTL_PP_WRITE_CONTROL:
1422 b = *(uint8_t *)arg;
1423 if (ioctl(fd, PPISCTRL, &b) < 0)
1424 return -ENOTSUP;
1425 break;
1426 case CHR_IOCTL_PP_READ_STATUS:
1427 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1428 return -ENOTSUP;
1429 *(uint8_t *)arg = b;
1430 break;
1431 default:
1432 return -ENOTSUP;
1433 }
1434 return 0;
1435}
1436
Kevin Wolf6e1db572011-06-01 13:29:11 +02001437static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
blueswir16972f932008-11-22 20:49:12 +00001438{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001439 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001440 CharDriverState *chr;
1441 int fd;
1442
Kevin Wolf6e1db572011-06-01 13:29:11 +02001443 fd = qemu_open(filename, O_RDWR);
1444 if (fd < 0) {
1445 return -errno;
1446 }
blueswir16972f932008-11-22 20:49:12 +00001447
1448 chr = qemu_mallocz(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001449 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001450 chr->chr_write = null_chr_write;
1451 chr->chr_ioctl = pp_ioctl;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001452
1453 *_chr = chr;
1454 return 0;
blueswir16972f932008-11-22 20:49:12 +00001455}
1456#endif
1457
aliguori6f97dba2008-10-31 18:49:55 +00001458#else /* _WIN32 */
1459
1460typedef struct {
1461 int max_size;
1462 HANDLE hcom, hrecv, hsend;
1463 OVERLAPPED orecv, osend;
1464 BOOL fpipe;
1465 DWORD len;
1466} WinCharState;
1467
1468#define NSENDBUF 2048
1469#define NRECVBUF 2048
1470#define MAXCONNECT 1
1471#define NTIMEOUT 5000
1472
1473static int win_chr_poll(void *opaque);
1474static int win_chr_pipe_poll(void *opaque);
1475
1476static void win_chr_close(CharDriverState *chr)
1477{
1478 WinCharState *s = chr->opaque;
1479
1480 if (s->hsend) {
1481 CloseHandle(s->hsend);
1482 s->hsend = NULL;
1483 }
1484 if (s->hrecv) {
1485 CloseHandle(s->hrecv);
1486 s->hrecv = NULL;
1487 }
1488 if (s->hcom) {
1489 CloseHandle(s->hcom);
1490 s->hcom = NULL;
1491 }
1492 if (s->fpipe)
1493 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1494 else
1495 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301496
1497 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001498}
1499
1500static int win_chr_init(CharDriverState *chr, const char *filename)
1501{
1502 WinCharState *s = chr->opaque;
1503 COMMCONFIG comcfg;
1504 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1505 COMSTAT comstat;
1506 DWORD size;
1507 DWORD err;
1508
1509 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1510 if (!s->hsend) {
1511 fprintf(stderr, "Failed CreateEvent\n");
1512 goto fail;
1513 }
1514 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1515 if (!s->hrecv) {
1516 fprintf(stderr, "Failed CreateEvent\n");
1517 goto fail;
1518 }
1519
1520 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1521 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1522 if (s->hcom == INVALID_HANDLE_VALUE) {
1523 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1524 s->hcom = NULL;
1525 goto fail;
1526 }
1527
1528 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1529 fprintf(stderr, "Failed SetupComm\n");
1530 goto fail;
1531 }
1532
1533 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1534 size = sizeof(COMMCONFIG);
1535 GetDefaultCommConfig(filename, &comcfg, &size);
1536 comcfg.dcb.DCBlength = sizeof(DCB);
1537 CommConfigDialog(filename, NULL, &comcfg);
1538
1539 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1540 fprintf(stderr, "Failed SetCommState\n");
1541 goto fail;
1542 }
1543
1544 if (!SetCommMask(s->hcom, EV_ERR)) {
1545 fprintf(stderr, "Failed SetCommMask\n");
1546 goto fail;
1547 }
1548
1549 cto.ReadIntervalTimeout = MAXDWORD;
1550 if (!SetCommTimeouts(s->hcom, &cto)) {
1551 fprintf(stderr, "Failed SetCommTimeouts\n");
1552 goto fail;
1553 }
1554
1555 if (!ClearCommError(s->hcom, &err, &comstat)) {
1556 fprintf(stderr, "Failed ClearCommError\n");
1557 goto fail;
1558 }
1559 qemu_add_polling_cb(win_chr_poll, chr);
1560 return 0;
1561
1562 fail:
1563 win_chr_close(chr);
1564 return -1;
1565}
1566
1567static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1568{
1569 WinCharState *s = chr->opaque;
1570 DWORD len, ret, size, err;
1571
1572 len = len1;
1573 ZeroMemory(&s->osend, sizeof(s->osend));
1574 s->osend.hEvent = s->hsend;
1575 while (len > 0) {
1576 if (s->hsend)
1577 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1578 else
1579 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1580 if (!ret) {
1581 err = GetLastError();
1582 if (err == ERROR_IO_PENDING) {
1583 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1584 if (ret) {
1585 buf += size;
1586 len -= size;
1587 } else {
1588 break;
1589 }
1590 } else {
1591 break;
1592 }
1593 } else {
1594 buf += size;
1595 len -= size;
1596 }
1597 }
1598 return len1 - len;
1599}
1600
1601static int win_chr_read_poll(CharDriverState *chr)
1602{
1603 WinCharState *s = chr->opaque;
1604
1605 s->max_size = qemu_chr_can_read(chr);
1606 return s->max_size;
1607}
1608
1609static void win_chr_readfile(CharDriverState *chr)
1610{
1611 WinCharState *s = chr->opaque;
1612 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301613 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001614 DWORD size;
1615
1616 ZeroMemory(&s->orecv, sizeof(s->orecv));
1617 s->orecv.hEvent = s->hrecv;
1618 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1619 if (!ret) {
1620 err = GetLastError();
1621 if (err == ERROR_IO_PENDING) {
1622 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1623 }
1624 }
1625
1626 if (size > 0) {
1627 qemu_chr_read(chr, buf, size);
1628 }
1629}
1630
1631static void win_chr_read(CharDriverState *chr)
1632{
1633 WinCharState *s = chr->opaque;
1634
1635 if (s->len > s->max_size)
1636 s->len = s->max_size;
1637 if (s->len == 0)
1638 return;
1639
1640 win_chr_readfile(chr);
1641}
1642
1643static int win_chr_poll(void *opaque)
1644{
1645 CharDriverState *chr = opaque;
1646 WinCharState *s = chr->opaque;
1647 COMSTAT status;
1648 DWORD comerr;
1649
1650 ClearCommError(s->hcom, &comerr, &status);
1651 if (status.cbInQue > 0) {
1652 s->len = status.cbInQue;
1653 win_chr_read_poll(chr);
1654 win_chr_read(chr);
1655 return 1;
1656 }
1657 return 0;
1658}
1659
Kevin Wolf6e1db572011-06-01 13:29:11 +02001660static int qemu_chr_open_win(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001661{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001662 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001663 CharDriverState *chr;
1664 WinCharState *s;
1665
1666 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001667 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001668 chr->opaque = s;
1669 chr->chr_write = win_chr_write;
1670 chr->chr_close = win_chr_close;
1671
1672 if (win_chr_init(chr, filename) < 0) {
1673 free(s);
1674 free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001675 return -EIO;
aliguori6f97dba2008-10-31 18:49:55 +00001676 }
Amit Shah127338e2009-11-03 19:59:56 +05301677 qemu_chr_generic_open(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001678
1679 *_chr = chr;
1680 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001681}
1682
1683static int win_chr_pipe_poll(void *opaque)
1684{
1685 CharDriverState *chr = opaque;
1686 WinCharState *s = chr->opaque;
1687 DWORD size;
1688
1689 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1690 if (size > 0) {
1691 s->len = size;
1692 win_chr_read_poll(chr);
1693 win_chr_read(chr);
1694 return 1;
1695 }
1696 return 0;
1697}
1698
1699static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1700{
1701 WinCharState *s = chr->opaque;
1702 OVERLAPPED ov;
1703 int ret;
1704 DWORD size;
1705 char openname[256];
1706
1707 s->fpipe = TRUE;
1708
1709 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1710 if (!s->hsend) {
1711 fprintf(stderr, "Failed CreateEvent\n");
1712 goto fail;
1713 }
1714 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1715 if (!s->hrecv) {
1716 fprintf(stderr, "Failed CreateEvent\n");
1717 goto fail;
1718 }
1719
1720 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1721 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1722 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1723 PIPE_WAIT,
1724 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1725 if (s->hcom == INVALID_HANDLE_VALUE) {
1726 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1727 s->hcom = NULL;
1728 goto fail;
1729 }
1730
1731 ZeroMemory(&ov, sizeof(ov));
1732 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1733 ret = ConnectNamedPipe(s->hcom, &ov);
1734 if (ret) {
1735 fprintf(stderr, "Failed ConnectNamedPipe\n");
1736 goto fail;
1737 }
1738
1739 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1740 if (!ret) {
1741 fprintf(stderr, "Failed GetOverlappedResult\n");
1742 if (ov.hEvent) {
1743 CloseHandle(ov.hEvent);
1744 ov.hEvent = NULL;
1745 }
1746 goto fail;
1747 }
1748
1749 if (ov.hEvent) {
1750 CloseHandle(ov.hEvent);
1751 ov.hEvent = NULL;
1752 }
1753 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1754 return 0;
1755
1756 fail:
1757 win_chr_close(chr);
1758 return -1;
1759}
1760
1761
Kevin Wolf6e1db572011-06-01 13:29:11 +02001762static int qemu_chr_open_win_pipe(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001763{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001764 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001765 CharDriverState *chr;
1766 WinCharState *s;
1767
1768 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001769 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001770 chr->opaque = s;
1771 chr->chr_write = win_chr_write;
1772 chr->chr_close = win_chr_close;
1773
1774 if (win_chr_pipe_init(chr, filename) < 0) {
1775 free(s);
1776 free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001777 return -EIO;
aliguori6f97dba2008-10-31 18:49:55 +00001778 }
Amit Shah127338e2009-11-03 19:59:56 +05301779 qemu_chr_generic_open(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001780
1781 *_chr = chr;
1782 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001783}
1784
1785static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1786{
1787 CharDriverState *chr;
1788 WinCharState *s;
1789
1790 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001791 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001792 s->hcom = fd_out;
1793 chr->opaque = s;
1794 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301795 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001796 return chr;
1797}
1798
Kevin Wolf6e1db572011-06-01 13:29:11 +02001799static int qemu_chr_open_win_con(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001800{
Kevin Wolf6e1db572011-06-01 13:29:11 +02001801 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE), chr);
aliguori6f97dba2008-10-31 18:49:55 +00001802}
1803
Kevin Wolf6e1db572011-06-01 13:29:11 +02001804static int qemu_chr_open_win_file_out(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001805{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001806 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001807 HANDLE fd_out;
1808
1809 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1810 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001811 if (fd_out == INVALID_HANDLE_VALUE) {
1812 return -EIO;
1813 }
aliguori6f97dba2008-10-31 18:49:55 +00001814
Kevin Wolf6e1db572011-06-01 13:29:11 +02001815 return qemu_chr_open_win_file(fd_out, _chr);
aliguori6f97dba2008-10-31 18:49:55 +00001816}
1817#endif /* !_WIN32 */
1818
1819/***********************************************************/
1820/* UDP Net console */
1821
1822typedef struct {
1823 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05301824 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001825 int bufcnt;
1826 int bufptr;
1827 int max_size;
1828} NetCharDriver;
1829
1830static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1831{
1832 NetCharDriver *s = chr->opaque;
1833
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001834 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00001835}
1836
1837static int udp_chr_read_poll(void *opaque)
1838{
1839 CharDriverState *chr = opaque;
1840 NetCharDriver *s = chr->opaque;
1841
1842 s->max_size = qemu_chr_can_read(chr);
1843
1844 /* If there were any stray characters in the queue process them
1845 * first
1846 */
1847 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1848 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1849 s->bufptr++;
1850 s->max_size = qemu_chr_can_read(chr);
1851 }
1852 return s->max_size;
1853}
1854
1855static void udp_chr_read(void *opaque)
1856{
1857 CharDriverState *chr = opaque;
1858 NetCharDriver *s = chr->opaque;
1859
1860 if (s->max_size == 0)
1861 return;
Blue Swirlc5b76b32009-06-13 08:44:31 +00001862 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00001863 s->bufptr = s->bufcnt;
1864 if (s->bufcnt <= 0)
1865 return;
1866
1867 s->bufptr = 0;
1868 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1869 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1870 s->bufptr++;
1871 s->max_size = qemu_chr_can_read(chr);
1872 }
1873}
1874
1875static void udp_chr_update_read_handler(CharDriverState *chr)
1876{
1877 NetCharDriver *s = chr->opaque;
1878
1879 if (s->fd >= 0) {
1880 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1881 udp_chr_read, NULL, chr);
1882 }
1883}
1884
aliguori819f56b2009-03-28 17:58:14 +00001885static void udp_chr_close(CharDriverState *chr)
1886{
1887 NetCharDriver *s = chr->opaque;
1888 if (s->fd >= 0) {
1889 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1890 closesocket(s->fd);
1891 }
1892 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05301893 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00001894}
1895
Kevin Wolf6e1db572011-06-01 13:29:11 +02001896static int qemu_chr_open_udp(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00001897{
1898 CharDriverState *chr = NULL;
1899 NetCharDriver *s = NULL;
1900 int fd = -1;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001901 int ret;
aliguori6f97dba2008-10-31 18:49:55 +00001902
1903 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001904 s = qemu_mallocz(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001905
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001906 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00001907 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001908 fprintf(stderr, "inet_dgram_opts failed\n");
Kevin Wolf6e1db572011-06-01 13:29:11 +02001909 ret = -errno;
aliguori6f97dba2008-10-31 18:49:55 +00001910 goto return_err;
1911 }
1912
1913 s->fd = fd;
1914 s->bufcnt = 0;
1915 s->bufptr = 0;
1916 chr->opaque = s;
1917 chr->chr_write = udp_chr_write;
1918 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00001919 chr->chr_close = udp_chr_close;
Kevin Wolf6e1db572011-06-01 13:29:11 +02001920
1921 *_chr = chr;
1922 return 0;
aliguori6f97dba2008-10-31 18:49:55 +00001923
1924return_err:
Kevin Wolf6e1db572011-06-01 13:29:11 +02001925 qemu_free(chr);
1926 qemu_free(s);
1927 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00001928 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02001929 }
1930 return ret;
aliguori6f97dba2008-10-31 18:49:55 +00001931}
1932
1933/***********************************************************/
1934/* TCP Net console */
1935
1936typedef struct {
1937 int fd, listen_fd;
1938 int connected;
1939 int max_size;
1940 int do_telnetopt;
1941 int do_nodelay;
1942 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001943 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00001944} TCPCharDriver;
1945
1946static void tcp_chr_accept(void *opaque);
1947
1948static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1949{
1950 TCPCharDriver *s = chr->opaque;
1951 if (s->connected) {
1952 return send_all(s->fd, buf, len);
1953 } else {
1954 /* XXX: indicate an error ? */
1955 return len;
1956 }
1957}
1958
1959static int tcp_chr_read_poll(void *opaque)
1960{
1961 CharDriverState *chr = opaque;
1962 TCPCharDriver *s = chr->opaque;
1963 if (!s->connected)
1964 return 0;
1965 s->max_size = qemu_chr_can_read(chr);
1966 return s->max_size;
1967}
1968
1969#define IAC 255
1970#define IAC_BREAK 243
1971static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1972 TCPCharDriver *s,
1973 uint8_t *buf, int *size)
1974{
1975 /* Handle any telnet client's basic IAC options to satisfy char by
1976 * char mode with no echo. All IAC options will be removed from
1977 * the buf and the do_telnetopt variable will be used to track the
1978 * state of the width of the IAC information.
1979 *
1980 * IAC commands come in sets of 3 bytes with the exception of the
1981 * "IAC BREAK" command and the double IAC.
1982 */
1983
1984 int i;
1985 int j = 0;
1986
1987 for (i = 0; i < *size; i++) {
1988 if (s->do_telnetopt > 1) {
1989 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1990 /* Double IAC means send an IAC */
1991 if (j != i)
1992 buf[j] = buf[i];
1993 j++;
1994 s->do_telnetopt = 1;
1995 } else {
1996 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1997 /* Handle IAC break commands by sending a serial break */
1998 qemu_chr_event(chr, CHR_EVENT_BREAK);
1999 s->do_telnetopt++;
2000 }
2001 s->do_telnetopt++;
2002 }
2003 if (s->do_telnetopt >= 4) {
2004 s->do_telnetopt = 1;
2005 }
2006 } else {
2007 if ((unsigned char)buf[i] == IAC) {
2008 s->do_telnetopt = 2;
2009 } else {
2010 if (j != i)
2011 buf[j] = buf[i];
2012 j++;
2013 }
2014 }
2015 }
2016 *size = j;
2017}
2018
Mark McLoughlin7d174052009-07-22 09:11:39 +01002019static int tcp_get_msgfd(CharDriverState *chr)
2020{
2021 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002022 int fd = s->msgfd;
2023 s->msgfd = -1;
2024 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002025}
2026
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002027#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002028static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2029{
2030 TCPCharDriver *s = chr->opaque;
2031 struct cmsghdr *cmsg;
2032
2033 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2034 int fd;
2035
2036 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2037 cmsg->cmsg_level != SOL_SOCKET ||
2038 cmsg->cmsg_type != SCM_RIGHTS)
2039 continue;
2040
2041 fd = *((int *)CMSG_DATA(cmsg));
2042 if (fd < 0)
2043 continue;
2044
2045 if (s->msgfd != -1)
2046 close(s->msgfd);
2047 s->msgfd = fd;
2048 }
2049}
2050
Mark McLoughlin9977c892009-07-22 09:11:38 +01002051static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2052{
2053 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002054 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002055 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002056 union {
2057 struct cmsghdr cmsg;
2058 char control[CMSG_SPACE(sizeof(int))];
2059 } msg_control;
2060 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002061
2062 iov[0].iov_base = buf;
2063 iov[0].iov_len = len;
2064
2065 msg.msg_iov = iov;
2066 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002067 msg.msg_control = &msg_control;
2068 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002069
Mark McLoughlin7d174052009-07-22 09:11:39 +01002070 ret = recvmsg(s->fd, &msg, 0);
2071 if (ret > 0 && s->is_unix)
2072 unix_process_msgfd(chr, &msg);
2073
2074 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002075}
2076#else
2077static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2078{
2079 TCPCharDriver *s = chr->opaque;
2080 return recv(s->fd, buf, len, 0);
2081}
2082#endif
2083
aliguori6f97dba2008-10-31 18:49:55 +00002084static void tcp_chr_read(void *opaque)
2085{
2086 CharDriverState *chr = opaque;
2087 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302088 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002089 int len, size;
2090
2091 if (!s->connected || s->max_size <= 0)
2092 return;
2093 len = sizeof(buf);
2094 if (len > s->max_size)
2095 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002096 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002097 if (size == 0) {
2098 /* connection closed */
2099 s->connected = 0;
2100 if (s->listen_fd >= 0) {
2101 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2102 }
2103 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2104 closesocket(s->fd);
2105 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302106 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002107 } else if (size > 0) {
2108 if (s->do_telnetopt)
2109 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2110 if (size > 0)
2111 qemu_chr_read(chr, buf, size);
2112 }
2113}
2114
Blue Swirl68c18d12010-08-15 09:46:24 +00002115#ifndef _WIN32
2116CharDriverState *qemu_chr_open_eventfd(int eventfd)
2117{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002118 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002119}
Blue Swirl68c18d12010-08-15 09:46:24 +00002120#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002121
aliguori6f97dba2008-10-31 18:49:55 +00002122static void tcp_chr_connect(void *opaque)
2123{
2124 CharDriverState *chr = opaque;
2125 TCPCharDriver *s = chr->opaque;
2126
2127 s->connected = 1;
2128 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2129 tcp_chr_read, NULL, chr);
Amit Shah127338e2009-11-03 19:59:56 +05302130 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002131}
2132
2133#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2134static void tcp_chr_telnet_init(int fd)
2135{
2136 char buf[3];
2137 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2138 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2139 send(fd, (char *)buf, 3, 0);
2140 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2141 send(fd, (char *)buf, 3, 0);
2142 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2143 send(fd, (char *)buf, 3, 0);
2144 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2145 send(fd, (char *)buf, 3, 0);
2146}
2147
2148static void socket_set_nodelay(int fd)
2149{
2150 int val = 1;
2151 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2152}
2153
Daniel P. Berrange13661082011-06-23 13:31:42 +01002154static int tcp_chr_add_client(CharDriverState *chr, int fd)
2155{
2156 TCPCharDriver *s = chr->opaque;
2157 if (s->fd != -1)
2158 return -1;
2159
2160 socket_set_nonblock(fd);
2161 if (s->do_nodelay)
2162 socket_set_nodelay(fd);
2163 s->fd = fd;
2164 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2165 tcp_chr_connect(chr);
2166
2167 return 0;
2168}
2169
aliguori6f97dba2008-10-31 18:49:55 +00002170static void tcp_chr_accept(void *opaque)
2171{
2172 CharDriverState *chr = opaque;
2173 TCPCharDriver *s = chr->opaque;
2174 struct sockaddr_in saddr;
2175#ifndef _WIN32
2176 struct sockaddr_un uaddr;
2177#endif
2178 struct sockaddr *addr;
2179 socklen_t len;
2180 int fd;
2181
2182 for(;;) {
2183#ifndef _WIN32
2184 if (s->is_unix) {
2185 len = sizeof(uaddr);
2186 addr = (struct sockaddr *)&uaddr;
2187 } else
2188#endif
2189 {
2190 len = sizeof(saddr);
2191 addr = (struct sockaddr *)&saddr;
2192 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002193 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002194 if (fd < 0 && errno != EINTR) {
2195 return;
2196 } else if (fd >= 0) {
2197 if (s->do_telnetopt)
2198 tcp_chr_telnet_init(fd);
2199 break;
2200 }
2201 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002202 if (tcp_chr_add_client(chr, fd) < 0)
2203 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002204}
2205
2206static void tcp_chr_close(CharDriverState *chr)
2207{
2208 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002209 if (s->fd >= 0) {
2210 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002211 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002212 }
2213 if (s->listen_fd >= 0) {
2214 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002215 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002216 }
aliguori6f97dba2008-10-31 18:49:55 +00002217 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302218 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002219}
2220
Kevin Wolf6e1db572011-06-01 13:29:11 +02002221static int qemu_chr_open_socket(QemuOpts *opts, CharDriverState **_chr)
aliguori6f97dba2008-10-31 18:49:55 +00002222{
2223 CharDriverState *chr = NULL;
2224 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002225 int fd = -1;
2226 int is_listen;
2227 int is_waitconnect;
2228 int do_nodelay;
2229 int is_unix;
2230 int is_telnet;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002231 int ret;
aliguori6f97dba2008-10-31 18:49:55 +00002232
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002233 is_listen = qemu_opt_get_bool(opts, "server", 0);
2234 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2235 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2236 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2237 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002238 if (!is_listen)
2239 is_waitconnect = 0;
2240
2241 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00002242 s = qemu_mallocz(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002243
aliguorif07b6002008-11-11 20:54:09 +00002244 if (is_unix) {
2245 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002246 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002247 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002248 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002249 }
2250 } else {
2251 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002252 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002253 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002254 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002255 }
2256 }
Kevin Wolf6e1db572011-06-01 13:29:11 +02002257 if (fd < 0) {
2258 ret = -errno;
aliguori6f97dba2008-10-31 18:49:55 +00002259 goto fail;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002260 }
aliguori6f97dba2008-10-31 18:49:55 +00002261
2262 if (!is_waitconnect)
2263 socket_set_nonblock(fd);
2264
2265 s->connected = 0;
2266 s->fd = -1;
2267 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002268 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002269 s->is_unix = is_unix;
2270 s->do_nodelay = do_nodelay && !is_unix;
2271
2272 chr->opaque = s;
2273 chr->chr_write = tcp_chr_write;
2274 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002275 chr->get_msgfd = tcp_get_msgfd;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002276 chr->chr_add_client = tcp_chr_add_client;
aliguori6f97dba2008-10-31 18:49:55 +00002277
2278 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002279 s->listen_fd = fd;
2280 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2281 if (is_telnet)
2282 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002283
aliguori6f97dba2008-10-31 18:49:55 +00002284 } else {
aliguorif07b6002008-11-11 20:54:09 +00002285 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002286 s->fd = fd;
2287 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002288 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002289 }
2290
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002291 /* for "info chardev" monitor command */
2292 chr->filename = qemu_malloc(256);
2293 if (is_unix) {
2294 snprintf(chr->filename, 256, "unix:%s%s",
2295 qemu_opt_get(opts, "path"),
2296 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2297 } else if (is_telnet) {
2298 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2299 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2300 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2301 } else {
2302 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2303 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2304 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2305 }
2306
aliguori6f97dba2008-10-31 18:49:55 +00002307 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002308 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002309 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002310 tcp_chr_accept(chr);
2311 socket_set_nonblock(s->listen_fd);
2312 }
Kevin Wolf6e1db572011-06-01 13:29:11 +02002313
2314 *_chr = chr;
2315 return 0;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002316
aliguori6f97dba2008-10-31 18:49:55 +00002317 fail:
2318 if (fd >= 0)
2319 closesocket(fd);
2320 qemu_free(s);
2321 qemu_free(chr);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002322 return ret;
aliguori6f97dba2008-10-31 18:49:55 +00002323}
2324
Luiz Capitulino999bd672010-10-22 16:09:05 -02002325/***********************************************************/
2326/* Memory chardev */
2327typedef struct {
2328 size_t outbuf_size;
2329 size_t outbuf_capacity;
2330 uint8_t *outbuf;
2331} MemoryDriver;
2332
2333static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2334{
2335 MemoryDriver *d = chr->opaque;
2336
2337 /* TODO: the QString implementation has the same code, we should
2338 * introduce a generic way to do this in cutils.c */
2339 if (d->outbuf_capacity < d->outbuf_size + len) {
2340 /* grow outbuf */
2341 d->outbuf_capacity += len;
2342 d->outbuf_capacity *= 2;
2343 d->outbuf = qemu_realloc(d->outbuf, d->outbuf_capacity);
2344 }
2345
2346 memcpy(d->outbuf + d->outbuf_size, buf, len);
2347 d->outbuf_size += len;
2348
2349 return len;
2350}
2351
2352void qemu_chr_init_mem(CharDriverState *chr)
2353{
2354 MemoryDriver *d;
2355
2356 d = qemu_malloc(sizeof(*d));
2357 d->outbuf_size = 0;
2358 d->outbuf_capacity = 4096;
2359 d->outbuf = qemu_mallocz(d->outbuf_capacity);
2360
2361 memset(chr, 0, sizeof(*chr));
2362 chr->opaque = d;
2363 chr->chr_write = mem_chr_write;
2364}
2365
2366QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2367{
2368 MemoryDriver *d = chr->opaque;
2369 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2370}
2371
2372/* NOTE: this driver can not be closed with qemu_chr_close()! */
2373void qemu_chr_close_mem(CharDriverState *chr)
2374{
2375 MemoryDriver *d = chr->opaque;
2376
2377 qemu_free(d->outbuf);
2378 qemu_free(chr->opaque);
2379 chr->opaque = NULL;
2380 chr->chr_write = NULL;
2381}
2382
2383size_t qemu_chr_mem_osize(const CharDriverState *chr)
2384{
2385 const MemoryDriver *d = chr->opaque;
2386 return d->outbuf_size;
2387}
2388
Gerd Hoffmann33521632009-12-08 13:11:49 +01002389QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002390{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002391 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002392 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002393 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002394 QemuOpts *opts;
2395
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002396 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002397 if (NULL == opts)
2398 return NULL;
2399
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002400 if (strstart(filename, "mon:", &p)) {
2401 filename = p;
2402 qemu_opt_set(opts, "mux", "on");
2403 }
2404
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002405 if (strcmp(filename, "null") == 0 ||
2406 strcmp(filename, "pty") == 0 ||
2407 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002408 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002409 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002410 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002411 return opts;
2412 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002413 if (strstart(filename, "vc", &p)) {
2414 qemu_opt_set(opts, "backend", "vc");
2415 if (*p == ':') {
2416 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2417 /* pixels */
2418 qemu_opt_set(opts, "width", width);
2419 qemu_opt_set(opts, "height", height);
2420 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2421 /* chars */
2422 qemu_opt_set(opts, "cols", width);
2423 qemu_opt_set(opts, "rows", height);
2424 } else {
2425 goto fail;
2426 }
2427 }
2428 return opts;
2429 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002430 if (strcmp(filename, "con:") == 0) {
2431 qemu_opt_set(opts, "backend", "console");
2432 return opts;
2433 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002434 if (strstart(filename, "COM", NULL)) {
2435 qemu_opt_set(opts, "backend", "serial");
2436 qemu_opt_set(opts, "path", filename);
2437 return opts;
2438 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002439 if (strstart(filename, "file:", &p)) {
2440 qemu_opt_set(opts, "backend", "file");
2441 qemu_opt_set(opts, "path", p);
2442 return opts;
2443 }
2444 if (strstart(filename, "pipe:", &p)) {
2445 qemu_opt_set(opts, "backend", "pipe");
2446 qemu_opt_set(opts, "path", p);
2447 return opts;
2448 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002449 if (strstart(filename, "tcp:", &p) ||
2450 strstart(filename, "telnet:", &p)) {
2451 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2452 host[0] = 0;
2453 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2454 goto fail;
2455 }
2456 qemu_opt_set(opts, "backend", "socket");
2457 qemu_opt_set(opts, "host", host);
2458 qemu_opt_set(opts, "port", port);
2459 if (p[pos] == ',') {
2460 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2461 goto fail;
2462 }
2463 if (strstart(filename, "telnet:", &p))
2464 qemu_opt_set(opts, "telnet", "on");
2465 return opts;
2466 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002467 if (strstart(filename, "udp:", &p)) {
2468 qemu_opt_set(opts, "backend", "udp");
2469 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2470 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002471 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002472 goto fail;
2473 }
2474 }
2475 qemu_opt_set(opts, "host", host);
2476 qemu_opt_set(opts, "port", port);
2477 if (p[pos] == '@') {
2478 p += pos + 1;
2479 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2480 host[0] = 0;
2481 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002482 goto fail;
2483 }
2484 }
2485 qemu_opt_set(opts, "localaddr", host);
2486 qemu_opt_set(opts, "localport", port);
2487 }
2488 return opts;
2489 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002490 if (strstart(filename, "unix:", &p)) {
2491 qemu_opt_set(opts, "backend", "socket");
2492 if (qemu_opts_do_parse(opts, p, "path") != 0)
2493 goto fail;
2494 return opts;
2495 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002496 if (strstart(filename, "/dev/parport", NULL) ||
2497 strstart(filename, "/dev/ppi", NULL)) {
2498 qemu_opt_set(opts, "backend", "parport");
2499 qemu_opt_set(opts, "path", filename);
2500 return opts;
2501 }
2502 if (strstart(filename, "/dev/", NULL)) {
2503 qemu_opt_set(opts, "backend", "tty");
2504 qemu_opt_set(opts, "path", filename);
2505 return opts;
2506 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002507
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002508fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002509 qemu_opts_del(opts);
2510 return NULL;
2511}
2512
2513static const struct {
2514 const char *name;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002515 int (*open)(QemuOpts *opts, CharDriverState **chr);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002516} backend_table[] = {
2517 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002518 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002519 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002520 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002521 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002522#ifdef _WIN32
2523 { .name = "file", .open = qemu_chr_open_win_file_out },
2524 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002525 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002526 { .name = "serial", .open = qemu_chr_open_win },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002527#else
2528 { .name = "file", .open = qemu_chr_open_file_out },
2529 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002530 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002531 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002532#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002533#ifdef CONFIG_BRLAPI
2534 { .name = "braille", .open = chr_baum_init },
2535#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002536#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002537 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2538 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002539 { .name = "tty", .open = qemu_chr_open_tty },
2540#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002541#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2542 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002543 { .name = "parport", .open = qemu_chr_open_pp },
2544#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002545#ifdef CONFIG_SPICE
2546 { .name = "spicevmc", .open = qemu_chr_open_spice },
2547#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002548};
2549
2550CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2551 void (*init)(struct CharDriverState *s))
2552{
2553 CharDriverState *chr;
2554 int i;
Kevin Wolf6e1db572011-06-01 13:29:11 +02002555 int ret;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002556
2557 if (qemu_opts_id(opts) == NULL) {
2558 fprintf(stderr, "chardev: no id specified\n");
2559 return NULL;
2560 }
2561
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002562 if (qemu_opt_get(opts, "backend") == NULL) {
2563 fprintf(stderr, "chardev: \"%s\" missing backend\n",
2564 qemu_opts_id(opts));
2565 return NULL;
2566 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002567 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2568 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2569 break;
2570 }
2571 if (i == ARRAY_SIZE(backend_table)) {
2572 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2573 qemu_opt_get(opts, "backend"));
2574 return NULL;
2575 }
2576
Kevin Wolf6e1db572011-06-01 13:29:11 +02002577 ret = backend_table[i].open(opts, &chr);
2578 if (ret < 0) {
2579 fprintf(stderr, "chardev: opening backend \"%s\" failed: %s\n",
2580 qemu_opt_get(opts, "backend"), strerror(-ret));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002581 return NULL;
2582 }
2583
2584 if (!chr->filename)
2585 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2586 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002587 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002588
2589 if (qemu_opt_get_bool(opts, "mux", 0)) {
2590 CharDriverState *base = chr;
2591 int len = strlen(qemu_opts_id(opts)) + 6;
2592 base->label = qemu_malloc(len);
2593 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2594 chr = qemu_chr_open_mux(base);
2595 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002596 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002597 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002598 } else {
2599 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002600 }
2601 chr->label = qemu_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002602 return chr;
2603}
2604
aurel32ceecf1d2009-01-18 14:08:04 +00002605CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002606{
2607 const char *p;
2608 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002609 QemuOpts *opts;
2610
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002611 if (strstart(filename, "chardev:", &p)) {
2612 return qemu_chr_find(p);
2613 }
2614
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002615 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002616 if (!opts)
2617 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002618
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002619 chr = qemu_chr_open_opts(opts, init);
2620 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2621 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002622 }
Paolo Bonzini363f8cb2010-12-23 13:42:54 +01002623 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002624 return chr;
2625}
2626
Paolo Bonzinic48855e2010-12-23 13:42:48 +01002627void qemu_chr_set_echo(struct CharDriverState *chr, bool echo)
2628{
2629 if (chr->chr_set_echo) {
2630 chr->chr_set_echo(chr, echo);
2631 }
2632}
2633
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002634void qemu_chr_guest_open(struct CharDriverState *chr)
2635{
2636 if (chr->chr_guest_open) {
2637 chr->chr_guest_open(chr);
2638 }
2639}
2640
2641void qemu_chr_guest_close(struct CharDriverState *chr)
2642{
2643 if (chr->chr_guest_close) {
2644 chr->chr_guest_close(chr);
2645 }
2646}
2647
aliguori6f97dba2008-10-31 18:49:55 +00002648void qemu_chr_close(CharDriverState *chr)
2649{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002650 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002651 if (chr->chr_close)
2652 chr->chr_close(chr);
2653 qemu_free(chr->filename);
2654 qemu_free(chr->label);
2655 qemu_free(chr);
2656}
2657
Luiz Capitulino588b3832009-12-10 17:16:08 -02002658static void qemu_chr_qlist_iter(QObject *obj, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002659{
Luiz Capitulino588b3832009-12-10 17:16:08 -02002660 QDict *chr_dict;
2661 Monitor *mon = opaque;
2662
2663 chr_dict = qobject_to_qdict(obj);
2664 monitor_printf(mon, "%s: filename=%s\n", qdict_get_str(chr_dict, "label"),
2665 qdict_get_str(chr_dict, "filename"));
2666}
2667
2668void qemu_chr_info_print(Monitor *mon, const QObject *ret_data)
2669{
2670 qlist_iter(qobject_to_qlist(ret_data), qemu_chr_qlist_iter, mon);
2671}
2672
Luiz Capitulino588b3832009-12-10 17:16:08 -02002673void qemu_chr_info(Monitor *mon, QObject **ret_data)
2674{
2675 QList *chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002676 CharDriverState *chr;
2677
Luiz Capitulino588b3832009-12-10 17:16:08 -02002678 chr_list = qlist_new();
2679
Blue Swirl72cf2d42009-09-12 07:36:22 +00002680 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulino588b3832009-12-10 17:16:08 -02002681 QObject *obj = qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2682 chr->label, chr->filename);
2683 qlist_append_obj(chr_list, obj);
aliguori6f97dba2008-10-31 18:49:55 +00002684 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002685
2686 *ret_data = QOBJECT(chr_list);
aliguori6f97dba2008-10-31 18:49:55 +00002687}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002688
2689CharDriverState *qemu_chr_find(const char *name)
2690{
2691 CharDriverState *chr;
2692
Blue Swirl72cf2d42009-09-12 07:36:22 +00002693 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002694 if (strcmp(chr->label, name) != 0)
2695 continue;
2696 return chr;
2697 }
2698 return NULL;
2699}