blob: bc53d09a9bab90b70588b5299d46c707b5402ad0 [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"
aliguori376253e2009-03-05 23:01:23 +000025#include "monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
aliguori6f97dba2008-10-31 18:49:55 +000027#include "sysemu.h"
28#include "qemu-timer.h"
29#include "qemu-char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
31#include "hw/baum.h"
aurel32aa71cf82009-02-08 15:53:20 +000032#include "hw/msmouse.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030033#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000034
35#include <unistd.h>
36#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000037#include <time.h>
38#include <errno.h>
39#include <sys/time.h>
40#include <zlib.h>
41
42#ifndef _WIN32
43#include <sys/times.h>
44#include <sys/wait.h>
45#include <termios.h>
46#include <sys/mman.h>
47#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000049#include <sys/socket.h>
50#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000051#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000053#include <dirent.h>
54#include <netdb.h>
55#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020056#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000057#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010058#if defined(__GLIBC__)
59#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040060#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000061#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000062#else
63#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000064#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040065#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66#include <dev/ppbus/ppi.h>
67#include <dev/ppbus/ppbconf.h>
68#elif defined(__DragonFly__)
69#include <dev/misc/ppi/ppi.h>
70#include <bus/ppbus/ppbconf.h>
71#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010072#else
aliguori6f97dba2008-10-31 18:49:55 +000073#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000074#include <pty.h>
75
76#include <linux/ppdev.h>
77#include <linux/parport.h>
78#endif
79#ifdef __sun__
80#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
83#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
93#endif
94#endif
95#endif
96
97#include "qemu_socket.h"
Alon Levycbcc6332011-01-19 10:49:50 +020098#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000099
Amit Shah9bd78542009-11-03 19:59:54 +0530100#define READ_BUF_LEN 4096
101
aliguori6f97dba2008-10-31 18:49:55 +0000102/***********************************************************/
103/* character device */
104
Blue Swirl72cf2d42009-09-12 07:36:22 +0000105static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000107
Hans de Goedea425d232011-11-19 10:22:43 +0100108void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000109{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200110 /* Keep track if the char device is open */
111 switch (event) {
112 case CHR_EVENT_OPENED:
113 s->opened = 1;
114 break;
115 case CHR_EVENT_CLOSED:
116 s->opened = 0;
117 break;
118 }
119
aliguori6f97dba2008-10-31 18:49:55 +0000120 if (!s->chr_event)
121 return;
122 s->chr_event(s->handler_opaque, event);
123}
124
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200125static void qemu_chr_fire_open_event(void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000126{
127 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100128 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200129 qemu_free_timer(s->open_timer);
130 s->open_timer = NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000131}
132
Amit Shah127338e2009-11-03 19:59:56 +0530133void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000134{
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200135 if (s->open_timer == NULL) {
Jan Kiszka06dec082012-11-22 18:30:02 +0100136 s->open_timer = qemu_new_timer_ms(rt_clock,
Jan Kiszkaac4119c2012-10-12 09:52:49 +0200137 qemu_chr_fire_open_event, s);
Jan Kiszka06dec082012-11-22 18:30:02 +0100138 qemu_mod_timer(s->open_timer, qemu_get_clock_ms(rt_clock) - 1);
aliguori6f97dba2008-10-31 18:49:55 +0000139 }
140}
141
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500142int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000143{
144 return s->chr_write(s, buf, len);
145}
146
Anthony Liguori41084f12011-08-15 11:17:34 -0500147int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000148{
149 if (!s->chr_ioctl)
150 return -ENOTSUP;
151 return s->chr_ioctl(s, cmd, arg);
152}
153
Anthony Liguori909cda12011-08-15 11:17:31 -0500154int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000155{
156 if (!s->chr_can_read)
157 return 0;
158 return s->chr_can_read(s->handler_opaque);
159}
160
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500161void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000162{
Stefan Weilac310732012-04-19 22:27:14 +0200163 if (s->chr_read) {
164 s->chr_read(s->handler_opaque, buf, len);
165 }
aliguori6f97dba2008-10-31 18:49:55 +0000166}
167
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500168int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100169{
170 return s->get_msgfd ? s->get_msgfd(s) : -1;
171}
172
Daniel P. Berrange13661082011-06-23 13:31:42 +0100173int qemu_chr_add_client(CharDriverState *s, int fd)
174{
175 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
176}
177
aliguori6f97dba2008-10-31 18:49:55 +0000178void qemu_chr_accept_input(CharDriverState *s)
179{
180 if (s->chr_accept_input)
181 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100182 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000183}
184
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500185void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000186{
Amit Shah9bd78542009-11-03 19:59:54 +0530187 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000188 va_list ap;
189 va_start(ap, fmt);
190 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500191 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000192 va_end(ap);
193}
194
aliguori6f97dba2008-10-31 18:49:55 +0000195void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100196 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000197 IOReadHandler *fd_read,
198 IOEventHandler *fd_event,
199 void *opaque)
200{
Amit Shahda7d9982011-04-25 15:18:22 +0530201 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530202 /* chr driver being released. */
Kusanagi Kouichid5b27162011-04-26 19:19:26 +0900203 ++s->avail_connections;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530204 }
aliguori6f97dba2008-10-31 18:49:55 +0000205 s->chr_can_read = fd_can_read;
206 s->chr_read = fd_read;
207 s->chr_event = fd_event;
208 s->handler_opaque = opaque;
209 if (s->chr_update_read_handler)
210 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200211
212 /* We're connecting to an already opened device, so let's make sure we
213 also get the open event */
214 if (s->opened) {
215 qemu_chr_generic_open(s);
216 }
aliguori6f97dba2008-10-31 18:49:55 +0000217}
218
219static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
220{
221 return len;
222}
223
Markus Armbruster1f514702012-02-07 15:09:08 +0100224static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000225{
226 CharDriverState *chr;
227
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000229 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100230 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000231}
232
233/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000234#define MAX_MUX 4
235#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
236#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
237typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100238 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000239 IOReadHandler *chr_read[MAX_MUX];
240 IOEventHandler *chr_event[MAX_MUX];
241 void *ext_opaque[MAX_MUX];
242 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200243 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000244 int mux_cnt;
245 int term_got_escape;
246 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000247 /* Intermediate input buffer allows to catch escape sequences even if the
248 currently active device is not accepting any input - but only until it
249 is full as well. */
250 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
251 int prod[MAX_MUX];
252 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200253 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200254 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200255 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000256} MuxDriver;
257
258
259static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
260{
261 MuxDriver *d = chr->opaque;
262 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200263 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000264 ret = d->drv->chr_write(d->drv, buf, len);
265 } else {
266 int i;
267
268 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200269 for (i = 0; i < len; i++) {
270 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000271 char buf1[64];
272 int64_t ti;
273 int secs;
274
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100275 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200276 if (d->timestamps_start == -1)
277 d->timestamps_start = ti;
278 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000279 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000280 snprintf(buf1, sizeof(buf1),
281 "[%02d:%02d:%02d.%03d] ",
282 secs / 3600,
283 (secs / 60) % 60,
284 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000285 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000286 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200287 d->linestart = 0;
288 }
289 ret += d->drv->chr_write(d->drv, buf+i, 1);
290 if (buf[i] == '\n') {
291 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000292 }
293 }
294 }
295 return ret;
296}
297
298static const char * const mux_help[] = {
299 "% h print this help\n\r",
300 "% x exit emulator\n\r",
301 "% s save disk data back to file (if -snapshot)\n\r",
302 "% t toggle console timestamps\n\r"
303 "% b send break (magic sysrq)\n\r",
304 "% c switch between console and monitor\n\r",
305 "% % sends %\n\r",
306 NULL
307};
308
309int term_escape_char = 0x01; /* ctrl-a is used for escape */
310static void mux_print_help(CharDriverState *chr)
311{
312 int i, j;
313 char ebuf[15] = "Escape-Char";
314 char cbuf[50] = "\n\r";
315
316 if (term_escape_char > 0 && term_escape_char < 26) {
317 snprintf(cbuf, sizeof(cbuf), "\n\r");
318 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
319 } else {
320 snprintf(cbuf, sizeof(cbuf),
321 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
322 term_escape_char);
323 }
324 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
325 for (i = 0; mux_help[i] != NULL; i++) {
326 for (j=0; mux_help[i][j] != '\0'; j++) {
327 if (mux_help[i][j] == '%')
328 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
329 else
330 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
331 }
332 }
333}
334
aliguori2724b182009-03-05 23:01:47 +0000335static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
336{
337 if (d->chr_event[mux_nr])
338 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
339}
340
aliguori6f97dba2008-10-31 18:49:55 +0000341static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
342{
343 if (d->term_got_escape) {
344 d->term_got_escape = 0;
345 if (ch == term_escape_char)
346 goto send_char;
347 switch(ch) {
348 case '?':
349 case 'h':
350 mux_print_help(chr);
351 break;
352 case 'x':
353 {
354 const char *term = "QEMU: Terminated\n\r";
355 chr->chr_write(chr,(uint8_t *)term,strlen(term));
356 exit(0);
357 break;
358 }
359 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200360 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000361 break;
362 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100363 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000364 break;
365 case 'c':
366 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
368 d->focus++;
369 if (d->focus >= d->mux_cnt)
370 d->focus = 0;
371 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000372 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200373 case 't':
374 d->timestamps = !d->timestamps;
375 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200376 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200377 break;
aliguori6f97dba2008-10-31 18:49:55 +0000378 }
379 } else if (ch == term_escape_char) {
380 d->term_got_escape = 1;
381 } else {
382 send_char:
383 return 1;
384 }
385 return 0;
386}
387
388static void mux_chr_accept_input(CharDriverState *chr)
389{
aliguori6f97dba2008-10-31 18:49:55 +0000390 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200391 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000392
aliguoria80bf992009-03-05 23:00:02 +0000393 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000394 d->chr_can_read[m] &&
395 d->chr_can_read[m](d->ext_opaque[m])) {
396 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000397 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000398 }
399}
400
401static int mux_chr_can_read(void *opaque)
402{
403 CharDriverState *chr = opaque;
404 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200405 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000406
aliguoria80bf992009-03-05 23:00:02 +0000407 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000408 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000409 if (d->chr_can_read[m])
410 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000411 return 0;
412}
413
414static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
415{
416 CharDriverState *chr = opaque;
417 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200418 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000419 int i;
420
421 mux_chr_accept_input (opaque);
422
423 for(i = 0; i < size; i++)
424 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000425 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000426 d->chr_can_read[m] &&
427 d->chr_can_read[m](d->ext_opaque[m]))
428 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
429 else
aliguoria80bf992009-03-05 23:00:02 +0000430 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000431 }
432}
433
434static void mux_chr_event(void *opaque, int event)
435{
436 CharDriverState *chr = opaque;
437 MuxDriver *d = chr->opaque;
438 int i;
439
440 /* Send the event to all registered listeners */
441 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000442 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000443}
444
445static void mux_chr_update_read_handler(CharDriverState *chr)
446{
447 MuxDriver *d = chr->opaque;
448
449 if (d->mux_cnt >= MAX_MUX) {
450 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
451 return;
452 }
453 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
454 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
455 d->chr_read[d->mux_cnt] = chr->chr_read;
456 d->chr_event[d->mux_cnt] = chr->chr_event;
457 /* Fix up the real driver with mux routines */
458 if (d->mux_cnt == 0) {
459 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
460 mux_chr_event, chr);
461 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200462 if (d->focus != -1) {
463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200464 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200465 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000466 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200467 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000468}
469
470static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
471{
472 CharDriverState *chr;
473 MuxDriver *d;
474
Anthony Liguori7267c092011-08-20 22:09:37 -0500475 chr = g_malloc0(sizeof(CharDriverState));
476 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000477
478 chr->opaque = d;
479 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200480 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000481 chr->chr_write = mux_chr_write;
482 chr->chr_update_read_handler = mux_chr_update_read_handler;
483 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100484 /* Frontend guest-open / -close notification is not support with muxes */
485 chr->chr_guest_open = NULL;
486 chr->chr_guest_close = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200487
488 /* Muxes are always open on creation */
489 qemu_chr_generic_open(chr);
490
aliguori6f97dba2008-10-31 18:49:55 +0000491 return chr;
492}
493
494
495#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000496int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000497{
498 int ret, len;
499
500 len = len1;
501 while (len > 0) {
502 ret = send(fd, buf, len, 0);
503 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000504 errno = WSAGetLastError();
505 if (errno != WSAEWOULDBLOCK) {
506 return -1;
507 }
508 } else if (ret == 0) {
509 break;
510 } else {
511 buf += ret;
512 len -= ret;
513 }
514 }
515 return len1 - len;
516}
517
518#else
519
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100520int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000521{
522 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100523 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000524
525 len = len1;
526 while (len > 0) {
527 ret = write(fd, buf, len);
528 if (ret < 0) {
529 if (errno != EINTR && errno != EAGAIN)
530 return -1;
531 } else if (ret == 0) {
532 break;
533 } else {
534 buf += ret;
535 len -= ret;
536 }
537 }
538 return len1 - len;
539}
aliguori6f97dba2008-10-31 18:49:55 +0000540#endif /* !_WIN32 */
541
Fabien Chouteaudb418a02011-10-06 16:37:51 +0200542#define STDIO_MAX_CLIENTS 1
543static int stdio_nb_clients;
544
aliguori6f97dba2008-10-31 18:49:55 +0000545#ifndef _WIN32
546
547typedef struct {
548 int fd_in, fd_out;
549 int max_size;
550} FDCharDriver;
551
aliguori6f97dba2008-10-31 18:49:55 +0000552
553static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
554{
555 FDCharDriver *s = chr->opaque;
556 return send_all(s->fd_out, buf, len);
557}
558
559static int fd_chr_read_poll(void *opaque)
560{
561 CharDriverState *chr = opaque;
562 FDCharDriver *s = chr->opaque;
563
Anthony Liguori909cda12011-08-15 11:17:31 -0500564 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000565 return s->max_size;
566}
567
568static void fd_chr_read(void *opaque)
569{
570 CharDriverState *chr = opaque;
571 FDCharDriver *s = chr->opaque;
572 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530573 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000574
575 len = sizeof(buf);
576 if (len > s->max_size)
577 len = s->max_size;
578 if (len == 0)
579 return;
580 size = read(s->fd_in, buf, len);
581 if (size == 0) {
582 /* FD has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100584 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000585 return;
586 }
587 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500588 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000589 }
590}
591
592static void fd_chr_update_read_handler(CharDriverState *chr)
593{
594 FDCharDriver *s = chr->opaque;
595
596 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500597 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000598 } else {
599 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
600 fd_chr_read, NULL, chr);
601 }
602 }
603}
604
605static void fd_chr_close(struct CharDriverState *chr)
606{
607 FDCharDriver *s = chr->opaque;
608
609 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500610 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000611 } else {
612 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
613 }
614 }
615
Anthony Liguori7267c092011-08-20 22:09:37 -0500616 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100617 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000618}
619
620/* open a character device to a unix fd */
621static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
622{
623 CharDriverState *chr;
624 FDCharDriver *s;
625
Anthony Liguori7267c092011-08-20 22:09:37 -0500626 chr = g_malloc0(sizeof(CharDriverState));
627 s = g_malloc0(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000628 s->fd_in = fd_in;
629 s->fd_out = fd_out;
630 chr->opaque = s;
631 chr->chr_write = fd_chr_write;
632 chr->chr_update_read_handler = fd_chr_update_read_handler;
633 chr->chr_close = fd_chr_close;
634
Amit Shah127338e2009-11-03 19:59:56 +0530635 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000636
637 return chr;
638}
639
Markus Armbruster1f514702012-02-07 15:09:08 +0100640static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000641{
642 int fd_out;
643
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100644 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200645 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100646 if (fd_out < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100647 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100648 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100649 return qemu_chr_open_fd(-1, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000650}
651
Markus Armbruster1f514702012-02-07 15:09:08 +0100652static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000653{
654 int fd_in, fd_out;
655 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200656 const char *filename = qemu_opt_get(opts, "path");
657
658 if (filename == NULL) {
659 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100660 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200661 }
aliguori6f97dba2008-10-31 18:49:55 +0000662
663 snprintf(filename_in, 256, "%s.in", filename);
664 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100665 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
666 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000667 if (fd_in < 0 || fd_out < 0) {
668 if (fd_in >= 0)
669 close(fd_in);
670 if (fd_out >= 0)
671 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100672 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100673 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100674 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100675 }
aliguori6f97dba2008-10-31 18:49:55 +0000676 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100677 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000678}
679
680
681/* for STDIO, we handle the case where several clients use it
682 (nographic mode) */
683
684#define TERM_FIFO_MAX_SIZE 1
685
686static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
687static int term_fifo_size;
688
689static int stdio_read_poll(void *opaque)
690{
691 CharDriverState *chr = opaque;
692
693 /* try to flush the queue if needed */
Anthony Liguori909cda12011-08-15 11:17:31 -0500694 if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500695 qemu_chr_be_write(chr, term_fifo, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000696 term_fifo_size = 0;
697 }
698 /* see if we can absorb more chars */
699 if (term_fifo_size == 0)
700 return 1;
701 else
702 return 0;
703}
704
705static void stdio_read(void *opaque)
706{
707 int size;
708 uint8_t buf[1];
709 CharDriverState *chr = opaque;
710
711 size = read(0, buf, 1);
712 if (size == 0) {
713 /* stdin has been closed. Remove it from the active list. */
714 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Hans de Goedea425d232011-11-19 10:22:43 +0100715 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000716 return;
717 }
718 if (size > 0) {
Anthony Liguori909cda12011-08-15 11:17:31 -0500719 if (qemu_chr_be_can_write(chr) > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500720 qemu_chr_be_write(chr, buf, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000721 } else if (term_fifo_size == 0) {
722 term_fifo[term_fifo_size++] = buf[0];
723 }
724 }
725}
726
727/* init terminal so that we can grab keys */
728static struct termios oldtty;
729static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100730static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000731
732static void term_exit(void)
733{
734 tcsetattr (0, TCSANOW, &oldtty);
735 fcntl(0, F_SETFL, old_fd0_flags);
736}
737
Paolo Bonzinibb002512010-12-23 13:42:50 +0100738static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000739{
740 struct termios tty;
741
Paolo Bonzini03693642010-12-23 13:42:49 +0100742 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100743 if (!echo) {
744 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000745 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100746 tty.c_oflag |= OPOST;
747 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
748 tty.c_cflag &= ~(CSIZE|PARENB);
749 tty.c_cflag |= CS8;
750 tty.c_cc[VMIN] = 1;
751 tty.c_cc[VTIME] = 0;
752 }
aliguori6f97dba2008-10-31 18:49:55 +0000753 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100754 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000755 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000756
757 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000758}
759
760static void qemu_chr_close_stdio(struct CharDriverState *chr)
761{
762 term_exit();
763 stdio_nb_clients--;
764 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
765 fd_chr_close(chr);
766}
767
Markus Armbruster1f514702012-02-07 15:09:08 +0100768static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000769{
770 CharDriverState *chr;
771
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100772 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100773 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100774 }
Paolo Bonzini03693642010-12-23 13:42:49 +0100775 if (stdio_nb_clients == 0) {
776 old_fd0_flags = fcntl(0, F_GETFL);
777 tcgetattr (0, &oldtty);
778 fcntl(0, F_SETFL, O_NONBLOCK);
779 atexit(term_exit);
780 }
781
aliguori6f97dba2008-10-31 18:49:55 +0000782 chr = qemu_chr_open_fd(0, 1);
783 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100784 chr->chr_set_echo = qemu_chr_set_echo_stdio;
aliguori6f97dba2008-10-31 18:49:55 +0000785 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
786 stdio_nb_clients++;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100787 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
788 display_type != DT_NOGRAPHIC);
Anthony Liguori15f31512011-08-15 11:17:35 -0500789 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000790
Markus Armbruster1f514702012-02-07 15:09:08 +0100791 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000792}
793
794#ifdef __sun__
795/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000796static int openpty(int *amaster, int *aslave, char *name,
797 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000798{
799 const char *slave;
800 int mfd = -1, sfd = -1;
801
802 *amaster = *aslave = -1;
803
804 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
805 if (mfd < 0)
806 goto err;
807
808 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
809 goto err;
810
811 if ((slave = ptsname(mfd)) == NULL)
812 goto err;
813
814 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
815 goto err;
816
817 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
818 (termp != NULL && tcgetattr(sfd, termp) < 0))
819 goto err;
820
821 if (amaster)
822 *amaster = mfd;
823 if (aslave)
824 *aslave = sfd;
825 if (winp)
826 ioctl(sfd, TIOCSWINSZ, winp);
827
828 return 0;
829
830err:
831 if (sfd != -1)
832 close(sfd);
833 close(mfd);
834 return -1;
835}
836
blueswir13f4cb3d2009-04-13 16:31:01 +0000837static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000838{
839 termios_p->c_iflag &=
840 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
841 termios_p->c_oflag &= ~OPOST;
842 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
843 termios_p->c_cflag &= ~(CSIZE|PARENB);
844 termios_p->c_cflag |= CS8;
845
846 termios_p->c_cc[VMIN] = 0;
847 termios_p->c_cc[VTIME] = 0;
848}
849#endif
850
851#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100852 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
853 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000854
855typedef struct {
856 int fd;
857 int connected;
858 int polling;
859 int read_bytes;
860 QEMUTimer *timer;
861} PtyCharDriver;
862
863static void pty_chr_update_read_handler(CharDriverState *chr);
864static void pty_chr_state(CharDriverState *chr, int connected);
865
866static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
867{
868 PtyCharDriver *s = chr->opaque;
869
870 if (!s->connected) {
871 /* guest sends data, check for (re-)connect */
872 pty_chr_update_read_handler(chr);
873 return 0;
874 }
875 return send_all(s->fd, buf, len);
876}
877
878static int pty_chr_read_poll(void *opaque)
879{
880 CharDriverState *chr = opaque;
881 PtyCharDriver *s = chr->opaque;
882
Anthony Liguori909cda12011-08-15 11:17:31 -0500883 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000884 return s->read_bytes;
885}
886
887static void pty_chr_read(void *opaque)
888{
889 CharDriverState *chr = opaque;
890 PtyCharDriver *s = chr->opaque;
891 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530892 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000893
894 len = sizeof(buf);
895 if (len > s->read_bytes)
896 len = s->read_bytes;
897 if (len == 0)
898 return;
899 size = read(s->fd, buf, len);
900 if ((size == -1 && errno == EIO) ||
901 (size == 0)) {
902 pty_chr_state(chr, 0);
903 return;
904 }
905 if (size > 0) {
906 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500907 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +0000908 }
909}
910
911static void pty_chr_update_read_handler(CharDriverState *chr)
912{
913 PtyCharDriver *s = chr->opaque;
914
915 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
916 pty_chr_read, NULL, chr);
917 s->polling = 1;
918 /*
919 * Short timeout here: just need wait long enougth that qemu makes
920 * it through the poll loop once. When reconnected we want a
921 * short timeout so we notice it almost instantly. Otherwise
922 * read() gives us -EIO instantly, making pty_chr_state() reset the
923 * timeout to the normal (much longer) poll interval before the
924 * timer triggers.
925 */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100926 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
aliguori6f97dba2008-10-31 18:49:55 +0000927}
928
929static void pty_chr_state(CharDriverState *chr, int connected)
930{
931 PtyCharDriver *s = chr->opaque;
932
933 if (!connected) {
934 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
935 s->connected = 0;
936 s->polling = 0;
937 /* (re-)connect poll interval for idle guests: once per second.
938 * We check more frequently in case the guests sends data to
939 * the virtual device linked to our pty. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100940 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
aliguori6f97dba2008-10-31 18:49:55 +0000941 } else {
942 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530943 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000944 s->connected = 1;
945 }
946}
947
948static void pty_chr_timer(void *opaque)
949{
950 struct CharDriverState *chr = opaque;
951 PtyCharDriver *s = chr->opaque;
952
953 if (s->connected)
954 return;
955 if (s->polling) {
956 /* If we arrive here without polling being cleared due
957 * read returning -EIO, then we are (re-)connected */
958 pty_chr_state(chr, 1);
959 return;
960 }
961
962 /* Next poll ... */
963 pty_chr_update_read_handler(chr);
964}
965
966static void pty_chr_close(struct CharDriverState *chr)
967{
968 PtyCharDriver *s = chr->opaque;
969
970 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
971 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000972 qemu_del_timer(s->timer);
973 qemu_free_timer(s->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -0500974 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100975 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000976}
977
Markus Armbruster1f514702012-02-07 15:09:08 +0100978static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000979{
980 CharDriverState *chr;
981 PtyCharDriver *s;
982 struct termios tty;
Markus Armbrustera4e26042011-11-11 10:40:05 +0100983 int master_fd, slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000984#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000985 char pty_name[PATH_MAX];
986#define q_ptsname(x) pty_name
987#else
988 char *pty_name = NULL;
989#define q_ptsname(x) ptsname(x)
990#endif
991
Markus Armbrustera4e26042011-11-11 10:40:05 +0100992 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100993 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000994 }
995
996 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +0000997 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000998 cfmakeraw(&tty);
999 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1000 close(slave_fd);
1001
Markus Armbrustera4e26042011-11-11 10:40:05 +01001002 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001003
Markus Armbrustera4e26042011-11-11 10:40:05 +01001004 len = strlen(q_ptsname(master_fd)) + 5;
1005 chr->filename = g_malloc(len);
1006 snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
1007 qemu_opt_set(opts, "path", q_ptsname(master_fd));
1008 fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
1009
1010 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001011 chr->opaque = s;
1012 chr->chr_write = pty_chr_write;
1013 chr->chr_update_read_handler = pty_chr_update_read_handler;
1014 chr->chr_close = pty_chr_close;
1015
Markus Armbrustera4e26042011-11-11 10:40:05 +01001016 s->fd = master_fd;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001017 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001018
Markus Armbruster1f514702012-02-07 15:09:08 +01001019 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001020}
1021
1022static void tty_serial_init(int fd, int speed,
1023 int parity, int data_bits, int stop_bits)
1024{
1025 struct termios tty;
1026 speed_t spd;
1027
1028#if 0
1029 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1030 speed, parity, data_bits, stop_bits);
1031#endif
1032 tcgetattr (fd, &tty);
1033
Stefan Weil45eea132009-10-26 16:10:10 +01001034#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1035 speed = speed * 10 / 11;
1036 do {
1037 check_speed(50);
1038 check_speed(75);
1039 check_speed(110);
1040 check_speed(134);
1041 check_speed(150);
1042 check_speed(200);
1043 check_speed(300);
1044 check_speed(600);
1045 check_speed(1200);
1046 check_speed(1800);
1047 check_speed(2400);
1048 check_speed(4800);
1049 check_speed(9600);
1050 check_speed(19200);
1051 check_speed(38400);
1052 /* Non-Posix values follow. They may be unsupported on some systems. */
1053 check_speed(57600);
1054 check_speed(115200);
1055#ifdef B230400
1056 check_speed(230400);
1057#endif
1058#ifdef B460800
1059 check_speed(460800);
1060#endif
1061#ifdef B500000
1062 check_speed(500000);
1063#endif
1064#ifdef B576000
1065 check_speed(576000);
1066#endif
1067#ifdef B921600
1068 check_speed(921600);
1069#endif
1070#ifdef B1000000
1071 check_speed(1000000);
1072#endif
1073#ifdef B1152000
1074 check_speed(1152000);
1075#endif
1076#ifdef B1500000
1077 check_speed(1500000);
1078#endif
1079#ifdef B2000000
1080 check_speed(2000000);
1081#endif
1082#ifdef B2500000
1083 check_speed(2500000);
1084#endif
1085#ifdef B3000000
1086 check_speed(3000000);
1087#endif
1088#ifdef B3500000
1089 check_speed(3500000);
1090#endif
1091#ifdef B4000000
1092 check_speed(4000000);
1093#endif
aliguori6f97dba2008-10-31 18:49:55 +00001094 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001095 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001096
1097 cfsetispeed(&tty, spd);
1098 cfsetospeed(&tty, spd);
1099
1100 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1101 |INLCR|IGNCR|ICRNL|IXON);
1102 tty.c_oflag |= OPOST;
1103 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1104 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1105 switch(data_bits) {
1106 default:
1107 case 8:
1108 tty.c_cflag |= CS8;
1109 break;
1110 case 7:
1111 tty.c_cflag |= CS7;
1112 break;
1113 case 6:
1114 tty.c_cflag |= CS6;
1115 break;
1116 case 5:
1117 tty.c_cflag |= CS5;
1118 break;
1119 }
1120 switch(parity) {
1121 default:
1122 case 'N':
1123 break;
1124 case 'E':
1125 tty.c_cflag |= PARENB;
1126 break;
1127 case 'O':
1128 tty.c_cflag |= PARENB | PARODD;
1129 break;
1130 }
1131 if (stop_bits == 2)
1132 tty.c_cflag |= CSTOPB;
1133
1134 tcsetattr (fd, TCSANOW, &tty);
1135}
1136
1137static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1138{
1139 FDCharDriver *s = chr->opaque;
1140
1141 switch(cmd) {
1142 case CHR_IOCTL_SERIAL_SET_PARAMS:
1143 {
1144 QEMUSerialSetParams *ssp = arg;
1145 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1146 ssp->data_bits, ssp->stop_bits);
1147 }
1148 break;
1149 case CHR_IOCTL_SERIAL_SET_BREAK:
1150 {
1151 int enable = *(int *)arg;
1152 if (enable)
1153 tcsendbreak(s->fd_in, 1);
1154 }
1155 break;
1156 case CHR_IOCTL_SERIAL_GET_TIOCM:
1157 {
1158 int sarg = 0;
1159 int *targ = (int *)arg;
1160 ioctl(s->fd_in, TIOCMGET, &sarg);
1161 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001162 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001163 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001164 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001165 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001166 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001167 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001168 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001169 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001170 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001171 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001172 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001173 *targ |= CHR_TIOCM_RTS;
1174 }
1175 break;
1176 case CHR_IOCTL_SERIAL_SET_TIOCM:
1177 {
1178 int sarg = *(int *)arg;
1179 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001180 ioctl(s->fd_in, TIOCMGET, &targ);
1181 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1182 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1183 if (sarg & CHR_TIOCM_CTS)
1184 targ |= TIOCM_CTS;
1185 if (sarg & CHR_TIOCM_CAR)
1186 targ |= TIOCM_CAR;
1187 if (sarg & CHR_TIOCM_DSR)
1188 targ |= TIOCM_DSR;
1189 if (sarg & CHR_TIOCM_RI)
1190 targ |= TIOCM_RI;
1191 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001192 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001193 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001194 targ |= TIOCM_RTS;
1195 ioctl(s->fd_in, TIOCMSET, &targ);
1196 }
1197 break;
1198 default:
1199 return -ENOTSUP;
1200 }
1201 return 0;
1202}
1203
David Ahern4266a132010-02-10 18:27:17 -07001204static void qemu_chr_close_tty(CharDriverState *chr)
1205{
1206 FDCharDriver *s = chr->opaque;
1207 int fd = -1;
1208
1209 if (s) {
1210 fd = s->fd_in;
1211 }
1212
1213 fd_chr_close(chr);
1214
1215 if (fd >= 0) {
1216 close(fd);
1217 }
1218}
1219
Markus Armbruster1f514702012-02-07 15:09:08 +01001220static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001221{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001222 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001223 CharDriverState *chr;
1224 int fd;
1225
Markus Armbrusterb181e042012-02-07 15:09:09 +01001226 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001227 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001228 return NULL;
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001229 }
aliguori6f97dba2008-10-31 18:49:55 +00001230 tty_serial_init(fd, 115200, 'N', 8, 1);
1231 chr = qemu_chr_open_fd(fd, fd);
aliguori6f97dba2008-10-31 18:49:55 +00001232 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001233 chr->chr_close = qemu_chr_close_tty;
Markus Armbruster1f514702012-02-07 15:09:08 +01001234 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001235}
1236#else /* ! __linux__ && ! __sun__ */
Markus Armbruster1f514702012-02-07 15:09:08 +01001237static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001238{
Markus Armbruster1f514702012-02-07 15:09:08 +01001239 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001240}
1241#endif /* __linux__ || __sun__ */
1242
1243#if defined(__linux__)
1244typedef struct {
1245 int fd;
1246 int mode;
1247} ParallelCharDriver;
1248
1249static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1250{
1251 if (s->mode != mode) {
1252 int m = mode;
1253 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1254 return 0;
1255 s->mode = mode;
1256 }
1257 return 1;
1258}
1259
1260static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1261{
1262 ParallelCharDriver *drv = chr->opaque;
1263 int fd = drv->fd;
1264 uint8_t b;
1265
1266 switch(cmd) {
1267 case CHR_IOCTL_PP_READ_DATA:
1268 if (ioctl(fd, PPRDATA, &b) < 0)
1269 return -ENOTSUP;
1270 *(uint8_t *)arg = b;
1271 break;
1272 case CHR_IOCTL_PP_WRITE_DATA:
1273 b = *(uint8_t *)arg;
1274 if (ioctl(fd, PPWDATA, &b) < 0)
1275 return -ENOTSUP;
1276 break;
1277 case CHR_IOCTL_PP_READ_CONTROL:
1278 if (ioctl(fd, PPRCONTROL, &b) < 0)
1279 return -ENOTSUP;
1280 /* Linux gives only the lowest bits, and no way to know data
1281 direction! For better compatibility set the fixed upper
1282 bits. */
1283 *(uint8_t *)arg = b | 0xc0;
1284 break;
1285 case CHR_IOCTL_PP_WRITE_CONTROL:
1286 b = *(uint8_t *)arg;
1287 if (ioctl(fd, PPWCONTROL, &b) < 0)
1288 return -ENOTSUP;
1289 break;
1290 case CHR_IOCTL_PP_READ_STATUS:
1291 if (ioctl(fd, PPRSTATUS, &b) < 0)
1292 return -ENOTSUP;
1293 *(uint8_t *)arg = b;
1294 break;
1295 case CHR_IOCTL_PP_DATA_DIR:
1296 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1297 return -ENOTSUP;
1298 break;
1299 case CHR_IOCTL_PP_EPP_READ_ADDR:
1300 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1301 struct ParallelIOArg *parg = arg;
1302 int n = read(fd, parg->buffer, parg->count);
1303 if (n != parg->count) {
1304 return -EIO;
1305 }
1306 }
1307 break;
1308 case CHR_IOCTL_PP_EPP_READ:
1309 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1310 struct ParallelIOArg *parg = arg;
1311 int n = read(fd, parg->buffer, parg->count);
1312 if (n != parg->count) {
1313 return -EIO;
1314 }
1315 }
1316 break;
1317 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1318 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1319 struct ParallelIOArg *parg = arg;
1320 int n = write(fd, parg->buffer, parg->count);
1321 if (n != parg->count) {
1322 return -EIO;
1323 }
1324 }
1325 break;
1326 case CHR_IOCTL_PP_EPP_WRITE:
1327 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1328 struct ParallelIOArg *parg = arg;
1329 int n = write(fd, parg->buffer, parg->count);
1330 if (n != parg->count) {
1331 return -EIO;
1332 }
1333 }
1334 break;
1335 default:
1336 return -ENOTSUP;
1337 }
1338 return 0;
1339}
1340
1341static void pp_close(CharDriverState *chr)
1342{
1343 ParallelCharDriver *drv = chr->opaque;
1344 int fd = drv->fd;
1345
1346 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1347 ioctl(fd, PPRELEASE);
1348 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001349 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001350 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001351}
1352
Markus Armbruster1f514702012-02-07 15:09:08 +01001353static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001354{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001355 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001356 CharDriverState *chr;
1357 ParallelCharDriver *drv;
1358 int fd;
1359
Markus Armbrusterb181e042012-02-07 15:09:09 +01001360 TFR(fd = qemu_open(filename, O_RDWR));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001361 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001362 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001363 }
aliguori6f97dba2008-10-31 18:49:55 +00001364
1365 if (ioctl(fd, PPCLAIM) < 0) {
1366 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001367 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001368 }
1369
Anthony Liguori7267c092011-08-20 22:09:37 -05001370 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001371 drv->fd = fd;
1372 drv->mode = IEEE1284_MODE_COMPAT;
1373
Anthony Liguori7267c092011-08-20 22:09:37 -05001374 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001375 chr->chr_write = null_chr_write;
1376 chr->chr_ioctl = pp_ioctl;
1377 chr->chr_close = pp_close;
1378 chr->opaque = drv;
1379
Amit Shah127338e2009-11-03 19:59:56 +05301380 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001381
Markus Armbruster1f514702012-02-07 15:09:08 +01001382 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001383}
1384#endif /* __linux__ */
1385
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001386#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001387static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1388{
Stefan Weile0efb992011-02-23 19:09:16 +01001389 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001390 uint8_t b;
1391
1392 switch(cmd) {
1393 case CHR_IOCTL_PP_READ_DATA:
1394 if (ioctl(fd, PPIGDATA, &b) < 0)
1395 return -ENOTSUP;
1396 *(uint8_t *)arg = b;
1397 break;
1398 case CHR_IOCTL_PP_WRITE_DATA:
1399 b = *(uint8_t *)arg;
1400 if (ioctl(fd, PPISDATA, &b) < 0)
1401 return -ENOTSUP;
1402 break;
1403 case CHR_IOCTL_PP_READ_CONTROL:
1404 if (ioctl(fd, PPIGCTRL, &b) < 0)
1405 return -ENOTSUP;
1406 *(uint8_t *)arg = b;
1407 break;
1408 case CHR_IOCTL_PP_WRITE_CONTROL:
1409 b = *(uint8_t *)arg;
1410 if (ioctl(fd, PPISCTRL, &b) < 0)
1411 return -ENOTSUP;
1412 break;
1413 case CHR_IOCTL_PP_READ_STATUS:
1414 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1415 return -ENOTSUP;
1416 *(uint8_t *)arg = b;
1417 break;
1418 default:
1419 return -ENOTSUP;
1420 }
1421 return 0;
1422}
1423
Markus Armbruster1f514702012-02-07 15:09:08 +01001424static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001425{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001426 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001427 CharDriverState *chr;
1428 int fd;
1429
Markus Armbrusterb181e042012-02-07 15:09:09 +01001430 fd = qemu_open(filename, O_RDWR);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001431 if (fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001432 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001433 }
blueswir16972f932008-11-22 20:49:12 +00001434
Anthony Liguori7267c092011-08-20 22:09:37 -05001435 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001436 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001437 chr->chr_write = null_chr_write;
1438 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001439 return chr;
blueswir16972f932008-11-22 20:49:12 +00001440}
1441#endif
1442
aliguori6f97dba2008-10-31 18:49:55 +00001443#else /* _WIN32 */
1444
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001445static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1446
aliguori6f97dba2008-10-31 18:49:55 +00001447typedef struct {
1448 int max_size;
1449 HANDLE hcom, hrecv, hsend;
1450 OVERLAPPED orecv, osend;
1451 BOOL fpipe;
1452 DWORD len;
1453} WinCharState;
1454
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001455typedef struct {
1456 HANDLE hStdIn;
1457 HANDLE hInputReadyEvent;
1458 HANDLE hInputDoneEvent;
1459 HANDLE hInputThread;
1460 uint8_t win_stdio_buf;
1461} WinStdioCharState;
1462
aliguori6f97dba2008-10-31 18:49:55 +00001463#define NSENDBUF 2048
1464#define NRECVBUF 2048
1465#define MAXCONNECT 1
1466#define NTIMEOUT 5000
1467
1468static int win_chr_poll(void *opaque);
1469static int win_chr_pipe_poll(void *opaque);
1470
1471static void win_chr_close(CharDriverState *chr)
1472{
1473 WinCharState *s = chr->opaque;
1474
1475 if (s->hsend) {
1476 CloseHandle(s->hsend);
1477 s->hsend = NULL;
1478 }
1479 if (s->hrecv) {
1480 CloseHandle(s->hrecv);
1481 s->hrecv = NULL;
1482 }
1483 if (s->hcom) {
1484 CloseHandle(s->hcom);
1485 s->hcom = NULL;
1486 }
1487 if (s->fpipe)
1488 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1489 else
1490 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301491
Hans de Goedea425d232011-11-19 10:22:43 +01001492 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001493}
1494
1495static int win_chr_init(CharDriverState *chr, const char *filename)
1496{
1497 WinCharState *s = chr->opaque;
1498 COMMCONFIG comcfg;
1499 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1500 COMSTAT comstat;
1501 DWORD size;
1502 DWORD err;
1503
1504 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1505 if (!s->hsend) {
1506 fprintf(stderr, "Failed CreateEvent\n");
1507 goto fail;
1508 }
1509 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1510 if (!s->hrecv) {
1511 fprintf(stderr, "Failed CreateEvent\n");
1512 goto fail;
1513 }
1514
1515 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1516 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1517 if (s->hcom == INVALID_HANDLE_VALUE) {
1518 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1519 s->hcom = NULL;
1520 goto fail;
1521 }
1522
1523 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1524 fprintf(stderr, "Failed SetupComm\n");
1525 goto fail;
1526 }
1527
1528 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1529 size = sizeof(COMMCONFIG);
1530 GetDefaultCommConfig(filename, &comcfg, &size);
1531 comcfg.dcb.DCBlength = sizeof(DCB);
1532 CommConfigDialog(filename, NULL, &comcfg);
1533
1534 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1535 fprintf(stderr, "Failed SetCommState\n");
1536 goto fail;
1537 }
1538
1539 if (!SetCommMask(s->hcom, EV_ERR)) {
1540 fprintf(stderr, "Failed SetCommMask\n");
1541 goto fail;
1542 }
1543
1544 cto.ReadIntervalTimeout = MAXDWORD;
1545 if (!SetCommTimeouts(s->hcom, &cto)) {
1546 fprintf(stderr, "Failed SetCommTimeouts\n");
1547 goto fail;
1548 }
1549
1550 if (!ClearCommError(s->hcom, &err, &comstat)) {
1551 fprintf(stderr, "Failed ClearCommError\n");
1552 goto fail;
1553 }
1554 qemu_add_polling_cb(win_chr_poll, chr);
1555 return 0;
1556
1557 fail:
1558 win_chr_close(chr);
1559 return -1;
1560}
1561
1562static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1563{
1564 WinCharState *s = chr->opaque;
1565 DWORD len, ret, size, err;
1566
1567 len = len1;
1568 ZeroMemory(&s->osend, sizeof(s->osend));
1569 s->osend.hEvent = s->hsend;
1570 while (len > 0) {
1571 if (s->hsend)
1572 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1573 else
1574 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1575 if (!ret) {
1576 err = GetLastError();
1577 if (err == ERROR_IO_PENDING) {
1578 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1579 if (ret) {
1580 buf += size;
1581 len -= size;
1582 } else {
1583 break;
1584 }
1585 } else {
1586 break;
1587 }
1588 } else {
1589 buf += size;
1590 len -= size;
1591 }
1592 }
1593 return len1 - len;
1594}
1595
1596static int win_chr_read_poll(CharDriverState *chr)
1597{
1598 WinCharState *s = chr->opaque;
1599
Anthony Liguori909cda12011-08-15 11:17:31 -05001600 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001601 return s->max_size;
1602}
1603
1604static void win_chr_readfile(CharDriverState *chr)
1605{
1606 WinCharState *s = chr->opaque;
1607 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301608 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001609 DWORD size;
1610
1611 ZeroMemory(&s->orecv, sizeof(s->orecv));
1612 s->orecv.hEvent = s->hrecv;
1613 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1614 if (!ret) {
1615 err = GetLastError();
1616 if (err == ERROR_IO_PENDING) {
1617 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1618 }
1619 }
1620
1621 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001622 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001623 }
1624}
1625
1626static void win_chr_read(CharDriverState *chr)
1627{
1628 WinCharState *s = chr->opaque;
1629
1630 if (s->len > s->max_size)
1631 s->len = s->max_size;
1632 if (s->len == 0)
1633 return;
1634
1635 win_chr_readfile(chr);
1636}
1637
1638static int win_chr_poll(void *opaque)
1639{
1640 CharDriverState *chr = opaque;
1641 WinCharState *s = chr->opaque;
1642 COMSTAT status;
1643 DWORD comerr;
1644
1645 ClearCommError(s->hcom, &comerr, &status);
1646 if (status.cbInQue > 0) {
1647 s->len = status.cbInQue;
1648 win_chr_read_poll(chr);
1649 win_chr_read(chr);
1650 return 1;
1651 }
1652 return 0;
1653}
1654
Markus Armbruster1f514702012-02-07 15:09:08 +01001655static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001656{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001657 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001658 CharDriverState *chr;
1659 WinCharState *s;
1660
Anthony Liguori7267c092011-08-20 22:09:37 -05001661 chr = g_malloc0(sizeof(CharDriverState));
1662 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001663 chr->opaque = s;
1664 chr->chr_write = win_chr_write;
1665 chr->chr_close = win_chr_close;
1666
1667 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001668 g_free(s);
1669 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001670 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001671 }
Amit Shah127338e2009-11-03 19:59:56 +05301672 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001673 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001674}
1675
1676static int win_chr_pipe_poll(void *opaque)
1677{
1678 CharDriverState *chr = opaque;
1679 WinCharState *s = chr->opaque;
1680 DWORD size;
1681
1682 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1683 if (size > 0) {
1684 s->len = size;
1685 win_chr_read_poll(chr);
1686 win_chr_read(chr);
1687 return 1;
1688 }
1689 return 0;
1690}
1691
1692static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1693{
1694 WinCharState *s = chr->opaque;
1695 OVERLAPPED ov;
1696 int ret;
1697 DWORD size;
1698 char openname[256];
1699
1700 s->fpipe = TRUE;
1701
1702 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1703 if (!s->hsend) {
1704 fprintf(stderr, "Failed CreateEvent\n");
1705 goto fail;
1706 }
1707 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1708 if (!s->hrecv) {
1709 fprintf(stderr, "Failed CreateEvent\n");
1710 goto fail;
1711 }
1712
1713 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1714 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1715 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1716 PIPE_WAIT,
1717 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1718 if (s->hcom == INVALID_HANDLE_VALUE) {
1719 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1720 s->hcom = NULL;
1721 goto fail;
1722 }
1723
1724 ZeroMemory(&ov, sizeof(ov));
1725 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1726 ret = ConnectNamedPipe(s->hcom, &ov);
1727 if (ret) {
1728 fprintf(stderr, "Failed ConnectNamedPipe\n");
1729 goto fail;
1730 }
1731
1732 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1733 if (!ret) {
1734 fprintf(stderr, "Failed GetOverlappedResult\n");
1735 if (ov.hEvent) {
1736 CloseHandle(ov.hEvent);
1737 ov.hEvent = NULL;
1738 }
1739 goto fail;
1740 }
1741
1742 if (ov.hEvent) {
1743 CloseHandle(ov.hEvent);
1744 ov.hEvent = NULL;
1745 }
1746 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1747 return 0;
1748
1749 fail:
1750 win_chr_close(chr);
1751 return -1;
1752}
1753
1754
Markus Armbruster1f514702012-02-07 15:09:08 +01001755static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001756{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001757 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001758 CharDriverState *chr;
1759 WinCharState *s;
1760
Anthony Liguori7267c092011-08-20 22:09:37 -05001761 chr = g_malloc0(sizeof(CharDriverState));
1762 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001763 chr->opaque = s;
1764 chr->chr_write = win_chr_write;
1765 chr->chr_close = win_chr_close;
1766
1767 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001768 g_free(s);
1769 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001770 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001771 }
Amit Shah127338e2009-11-03 19:59:56 +05301772 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001773 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001774}
1775
Markus Armbruster1f514702012-02-07 15:09:08 +01001776static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001777{
1778 CharDriverState *chr;
1779 WinCharState *s;
1780
Anthony Liguori7267c092011-08-20 22:09:37 -05001781 chr = g_malloc0(sizeof(CharDriverState));
1782 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001783 s->hcom = fd_out;
1784 chr->opaque = s;
1785 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301786 qemu_chr_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001787 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001788}
1789
Markus Armbruster1f514702012-02-07 15:09:08 +01001790static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001791{
Markus Armbruster1f514702012-02-07 15:09:08 +01001792 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001793}
1794
Markus Armbruster1f514702012-02-07 15:09:08 +01001795static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001796{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001797 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001798 HANDLE fd_out;
1799
1800 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1801 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001802 if (fd_out == INVALID_HANDLE_VALUE) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001803 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001804 }
aliguori6f97dba2008-10-31 18:49:55 +00001805
Markus Armbruster1f514702012-02-07 15:09:08 +01001806 return qemu_chr_open_win_file(fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001807}
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001808
1809static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1810{
1811 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1812 DWORD dwSize;
1813 int len1;
1814
1815 len1 = len;
1816
1817 while (len1 > 0) {
1818 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1819 break;
1820 }
1821 buf += dwSize;
1822 len1 -= dwSize;
1823 }
1824
1825 return len - len1;
1826}
1827
1828static void win_stdio_wait_func(void *opaque)
1829{
1830 CharDriverState *chr = opaque;
1831 WinStdioCharState *stdio = chr->opaque;
1832 INPUT_RECORD buf[4];
1833 int ret;
1834 DWORD dwSize;
1835 int i;
1836
1837 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
1838 &dwSize);
1839
1840 if (!ret) {
1841 /* Avoid error storm */
1842 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
1843 return;
1844 }
1845
1846 for (i = 0; i < dwSize; i++) {
1847 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
1848
1849 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
1850 int j;
1851 if (kev->uChar.AsciiChar != 0) {
1852 for (j = 0; j < kev->wRepeatCount; j++) {
1853 if (qemu_chr_be_can_write(chr)) {
1854 uint8_t c = kev->uChar.AsciiChar;
1855 qemu_chr_be_write(chr, &c, 1);
1856 }
1857 }
1858 }
1859 }
1860 }
1861}
1862
1863static DWORD WINAPI win_stdio_thread(LPVOID param)
1864{
1865 CharDriverState *chr = param;
1866 WinStdioCharState *stdio = chr->opaque;
1867 int ret;
1868 DWORD dwSize;
1869
1870 while (1) {
1871
1872 /* Wait for one byte */
1873 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
1874
1875 /* Exit in case of error, continue if nothing read */
1876 if (!ret) {
1877 break;
1878 }
1879 if (!dwSize) {
1880 continue;
1881 }
1882
1883 /* Some terminal emulator returns \r\n for Enter, just pass \n */
1884 if (stdio->win_stdio_buf == '\r') {
1885 continue;
1886 }
1887
1888 /* Signal the main thread and wait until the byte was eaten */
1889 if (!SetEvent(stdio->hInputReadyEvent)) {
1890 break;
1891 }
1892 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
1893 != WAIT_OBJECT_0) {
1894 break;
1895 }
1896 }
1897
1898 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
1899 return 0;
1900}
1901
1902static void win_stdio_thread_wait_func(void *opaque)
1903{
1904 CharDriverState *chr = opaque;
1905 WinStdioCharState *stdio = chr->opaque;
1906
1907 if (qemu_chr_be_can_write(chr)) {
1908 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
1909 }
1910
1911 SetEvent(stdio->hInputDoneEvent);
1912}
1913
1914static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
1915{
1916 WinStdioCharState *stdio = chr->opaque;
1917 DWORD dwMode = 0;
1918
1919 GetConsoleMode(stdio->hStdIn, &dwMode);
1920
1921 if (echo) {
1922 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
1923 } else {
1924 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
1925 }
1926}
1927
1928static void win_stdio_close(CharDriverState *chr)
1929{
1930 WinStdioCharState *stdio = chr->opaque;
1931
1932 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
1933 CloseHandle(stdio->hInputReadyEvent);
1934 }
1935 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
1936 CloseHandle(stdio->hInputDoneEvent);
1937 }
1938 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
1939 TerminateThread(stdio->hInputThread, 0);
1940 }
1941
1942 g_free(chr->opaque);
1943 g_free(chr);
1944 stdio_nb_clients--;
1945}
1946
Markus Armbruster1f514702012-02-07 15:09:08 +01001947static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001948{
1949 CharDriverState *chr;
1950 WinStdioCharState *stdio;
1951 DWORD dwMode;
1952 int is_console = 0;
1953
1954 if (stdio_nb_clients >= STDIO_MAX_CLIENTS
1955 || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001956 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001957 }
1958
1959 chr = g_malloc0(sizeof(CharDriverState));
1960 stdio = g_malloc0(sizeof(WinStdioCharState));
1961
1962 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1963 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
1964 fprintf(stderr, "cannot open stdio: invalid handle\n");
1965 exit(1);
1966 }
1967
1968 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
1969
1970 chr->opaque = stdio;
1971 chr->chr_write = win_stdio_write;
1972 chr->chr_close = win_stdio_close;
1973
1974 if (stdio_nb_clients == 0) {
1975 if (is_console) {
1976 if (qemu_add_wait_object(stdio->hStdIn,
1977 win_stdio_wait_func, chr)) {
1978 fprintf(stderr, "qemu_add_wait_object: failed\n");
1979 }
1980 } else {
1981 DWORD dwId;
1982
1983 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1984 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1985 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
1986 chr, 0, &dwId);
1987
1988 if (stdio->hInputThread == INVALID_HANDLE_VALUE
1989 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
1990 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
1991 fprintf(stderr, "cannot create stdio thread or event\n");
1992 exit(1);
1993 }
1994 if (qemu_add_wait_object(stdio->hInputReadyEvent,
1995 win_stdio_thread_wait_func, chr)) {
1996 fprintf(stderr, "qemu_add_wait_object: failed\n");
1997 }
1998 }
1999 }
2000
2001 dwMode |= ENABLE_LINE_INPUT;
2002
2003 stdio_clients[stdio_nb_clients++] = chr;
2004 if (stdio_nb_clients == 1 && is_console) {
2005 /* set the terminal in raw mode */
2006 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2007 dwMode |= ENABLE_PROCESSED_INPUT;
2008 }
2009
2010 SetConsoleMode(stdio->hStdIn, dwMode);
2011
2012 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2013 qemu_chr_fe_set_echo(chr, false);
2014
Markus Armbruster1f514702012-02-07 15:09:08 +01002015 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002016}
aliguori6f97dba2008-10-31 18:49:55 +00002017#endif /* !_WIN32 */
2018
2019/***********************************************************/
2020/* UDP Net console */
2021
2022typedef struct {
2023 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05302024 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002025 int bufcnt;
2026 int bufptr;
2027 int max_size;
2028} NetCharDriver;
2029
2030static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2031{
2032 NetCharDriver *s = chr->opaque;
2033
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002034 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00002035}
2036
2037static int udp_chr_read_poll(void *opaque)
2038{
2039 CharDriverState *chr = opaque;
2040 NetCharDriver *s = chr->opaque;
2041
Anthony Liguori909cda12011-08-15 11:17:31 -05002042 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002043
2044 /* If there were any stray characters in the queue process them
2045 * first
2046 */
2047 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002048 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002049 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002050 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002051 }
2052 return s->max_size;
2053}
2054
2055static void udp_chr_read(void *opaque)
2056{
2057 CharDriverState *chr = opaque;
2058 NetCharDriver *s = chr->opaque;
2059
2060 if (s->max_size == 0)
2061 return;
Blue Swirl00aa0042011-07-23 20:04:29 +00002062 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00002063 s->bufptr = s->bufcnt;
2064 if (s->bufcnt <= 0)
2065 return;
2066
2067 s->bufptr = 0;
2068 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002069 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002070 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002071 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002072 }
2073}
2074
2075static void udp_chr_update_read_handler(CharDriverState *chr)
2076{
2077 NetCharDriver *s = chr->opaque;
2078
2079 if (s->fd >= 0) {
2080 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2081 udp_chr_read, NULL, chr);
2082 }
2083}
2084
aliguori819f56b2009-03-28 17:58:14 +00002085static void udp_chr_close(CharDriverState *chr)
2086{
2087 NetCharDriver *s = chr->opaque;
2088 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002089 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori819f56b2009-03-28 17:58:14 +00002090 closesocket(s->fd);
2091 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002092 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002093 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002094}
2095
Markus Armbruster1f514702012-02-07 15:09:08 +01002096static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002097{
2098 CharDriverState *chr = NULL;
2099 NetCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002100 Error *local_err = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002101 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002102
Anthony Liguori7267c092011-08-20 22:09:37 -05002103 chr = g_malloc0(sizeof(CharDriverState));
2104 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002105
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002106 fd = inet_dgram_opts(opts, &local_err);
aliguori6f97dba2008-10-31 18:49:55 +00002107 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002108 goto return_err;
2109 }
2110
2111 s->fd = fd;
2112 s->bufcnt = 0;
2113 s->bufptr = 0;
2114 chr->opaque = s;
2115 chr->chr_write = udp_chr_write;
2116 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002117 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002118 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002119
2120return_err:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002121 if (local_err) {
2122 qerror_report_err(local_err);
2123 error_free(local_err);
2124 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002125 g_free(chr);
2126 g_free(s);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002127 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002128 closesocket(fd);
Kevin Wolf6e1db572011-06-01 13:29:11 +02002129 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002130 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002131}
2132
2133/***********************************************************/
2134/* TCP Net console */
2135
2136typedef struct {
2137 int fd, listen_fd;
2138 int connected;
2139 int max_size;
2140 int do_telnetopt;
2141 int do_nodelay;
2142 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002143 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002144} TCPCharDriver;
2145
2146static void tcp_chr_accept(void *opaque);
2147
2148static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2149{
2150 TCPCharDriver *s = chr->opaque;
2151 if (s->connected) {
2152 return send_all(s->fd, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002153 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002154 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002155 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002156 }
2157}
2158
2159static int tcp_chr_read_poll(void *opaque)
2160{
2161 CharDriverState *chr = opaque;
2162 TCPCharDriver *s = chr->opaque;
2163 if (!s->connected)
2164 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002165 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002166 return s->max_size;
2167}
2168
2169#define IAC 255
2170#define IAC_BREAK 243
2171static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2172 TCPCharDriver *s,
2173 uint8_t *buf, int *size)
2174{
2175 /* Handle any telnet client's basic IAC options to satisfy char by
2176 * char mode with no echo. All IAC options will be removed from
2177 * the buf and the do_telnetopt variable will be used to track the
2178 * state of the width of the IAC information.
2179 *
2180 * IAC commands come in sets of 3 bytes with the exception of the
2181 * "IAC BREAK" command and the double IAC.
2182 */
2183
2184 int i;
2185 int j = 0;
2186
2187 for (i = 0; i < *size; i++) {
2188 if (s->do_telnetopt > 1) {
2189 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2190 /* Double IAC means send an IAC */
2191 if (j != i)
2192 buf[j] = buf[i];
2193 j++;
2194 s->do_telnetopt = 1;
2195 } else {
2196 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2197 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002198 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002199 s->do_telnetopt++;
2200 }
2201 s->do_telnetopt++;
2202 }
2203 if (s->do_telnetopt >= 4) {
2204 s->do_telnetopt = 1;
2205 }
2206 } else {
2207 if ((unsigned char)buf[i] == IAC) {
2208 s->do_telnetopt = 2;
2209 } else {
2210 if (j != i)
2211 buf[j] = buf[i];
2212 j++;
2213 }
2214 }
2215 }
2216 *size = j;
2217}
2218
Mark McLoughlin7d174052009-07-22 09:11:39 +01002219static int tcp_get_msgfd(CharDriverState *chr)
2220{
2221 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002222 int fd = s->msgfd;
2223 s->msgfd = -1;
2224 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002225}
2226
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002227#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002228static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2229{
2230 TCPCharDriver *s = chr->opaque;
2231 struct cmsghdr *cmsg;
2232
2233 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2234 int fd;
2235
2236 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2237 cmsg->cmsg_level != SOL_SOCKET ||
2238 cmsg->cmsg_type != SCM_RIGHTS)
2239 continue;
2240
2241 fd = *((int *)CMSG_DATA(cmsg));
2242 if (fd < 0)
2243 continue;
2244
Corey Bryant06138652012-08-14 16:43:42 -04002245#ifndef MSG_CMSG_CLOEXEC
2246 qemu_set_cloexec(fd);
2247#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002248 if (s->msgfd != -1)
2249 close(s->msgfd);
2250 s->msgfd = fd;
2251 }
2252}
2253
Mark McLoughlin9977c892009-07-22 09:11:38 +01002254static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2255{
2256 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002257 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002258 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002259 union {
2260 struct cmsghdr cmsg;
2261 char control[CMSG_SPACE(sizeof(int))];
2262 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002263 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002264 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002265
2266 iov[0].iov_base = buf;
2267 iov[0].iov_len = len;
2268
2269 msg.msg_iov = iov;
2270 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002271 msg.msg_control = &msg_control;
2272 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002273
Corey Bryant06138652012-08-14 16:43:42 -04002274#ifdef MSG_CMSG_CLOEXEC
2275 flags |= MSG_CMSG_CLOEXEC;
2276#endif
2277 ret = recvmsg(s->fd, &msg, flags);
2278 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002279 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002280 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002281
2282 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002283}
2284#else
2285static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2286{
2287 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002288 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002289}
2290#endif
2291
aliguori6f97dba2008-10-31 18:49:55 +00002292static void tcp_chr_read(void *opaque)
2293{
2294 CharDriverState *chr = opaque;
2295 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302296 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002297 int len, size;
2298
2299 if (!s->connected || s->max_size <= 0)
2300 return;
2301 len = sizeof(buf);
2302 if (len > s->max_size)
2303 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002304 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002305 if (size == 0) {
2306 /* connection closed */
2307 s->connected = 0;
2308 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002309 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002310 }
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002311 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002312 closesocket(s->fd);
2313 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002314 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002315 } else if (size > 0) {
2316 if (s->do_telnetopt)
2317 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2318 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002319 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002320 }
2321}
2322
Blue Swirl68c18d12010-08-15 09:46:24 +00002323#ifndef _WIN32
2324CharDriverState *qemu_chr_open_eventfd(int eventfd)
2325{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002326 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002327}
Blue Swirl68c18d12010-08-15 09:46:24 +00002328#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002329
aliguori6f97dba2008-10-31 18:49:55 +00002330static void tcp_chr_connect(void *opaque)
2331{
2332 CharDriverState *chr = opaque;
2333 TCPCharDriver *s = chr->opaque;
2334
2335 s->connected = 1;
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002336 if (s->fd >= 0) {
2337 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2338 tcp_chr_read, NULL, chr);
2339 }
Amit Shah127338e2009-11-03 19:59:56 +05302340 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002341}
2342
2343#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2344static void tcp_chr_telnet_init(int fd)
2345{
2346 char buf[3];
2347 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2348 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2349 send(fd, (char *)buf, 3, 0);
2350 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2351 send(fd, (char *)buf, 3, 0);
2352 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2353 send(fd, (char *)buf, 3, 0);
2354 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2355 send(fd, (char *)buf, 3, 0);
2356}
2357
2358static void socket_set_nodelay(int fd)
2359{
2360 int val = 1;
2361 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2362}
2363
Daniel P. Berrange13661082011-06-23 13:31:42 +01002364static int tcp_chr_add_client(CharDriverState *chr, int fd)
2365{
2366 TCPCharDriver *s = chr->opaque;
2367 if (s->fd != -1)
2368 return -1;
2369
2370 socket_set_nonblock(fd);
2371 if (s->do_nodelay)
2372 socket_set_nodelay(fd);
2373 s->fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002374 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002375 tcp_chr_connect(chr);
2376
2377 return 0;
2378}
2379
aliguori6f97dba2008-10-31 18:49:55 +00002380static void tcp_chr_accept(void *opaque)
2381{
2382 CharDriverState *chr = opaque;
2383 TCPCharDriver *s = chr->opaque;
2384 struct sockaddr_in saddr;
2385#ifndef _WIN32
2386 struct sockaddr_un uaddr;
2387#endif
2388 struct sockaddr *addr;
2389 socklen_t len;
2390 int fd;
2391
2392 for(;;) {
2393#ifndef _WIN32
2394 if (s->is_unix) {
2395 len = sizeof(uaddr);
2396 addr = (struct sockaddr *)&uaddr;
2397 } else
2398#endif
2399 {
2400 len = sizeof(saddr);
2401 addr = (struct sockaddr *)&saddr;
2402 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002403 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002404 if (fd < 0 && errno != EINTR) {
2405 return;
2406 } else if (fd >= 0) {
2407 if (s->do_telnetopt)
2408 tcp_chr_telnet_init(fd);
2409 break;
2410 }
2411 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002412 if (tcp_chr_add_client(chr, fd) < 0)
2413 close(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002414}
2415
2416static void tcp_chr_close(CharDriverState *chr)
2417{
2418 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002419 if (s->fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002420 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002421 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002422 }
2423 if (s->listen_fd >= 0) {
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002424 qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002425 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002426 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002427 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002428 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002429}
2430
Markus Armbruster1f514702012-02-07 15:09:08 +01002431static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002432{
2433 CharDriverState *chr = NULL;
2434 TCPCharDriver *s = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002435 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002436 int fd = -1;
2437 int is_listen;
2438 int is_waitconnect;
2439 int do_nodelay;
2440 int is_unix;
2441 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002442
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002443 is_listen = qemu_opt_get_bool(opts, "server", 0);
2444 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2445 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2446 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2447 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002448 if (!is_listen)
2449 is_waitconnect = 0;
2450
Anthony Liguori7267c092011-08-20 22:09:37 -05002451 chr = g_malloc0(sizeof(CharDriverState));
2452 s = g_malloc0(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002453
aliguorif07b6002008-11-11 20:54:09 +00002454 if (is_unix) {
2455 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002456 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002457 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002458 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002459 }
2460 } else {
2461 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002462 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002463 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002464 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002465 }
2466 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002467 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002468 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002469 }
aliguori6f97dba2008-10-31 18:49:55 +00002470
2471 if (!is_waitconnect)
2472 socket_set_nonblock(fd);
2473
2474 s->connected = 0;
2475 s->fd = -1;
2476 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002477 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002478 s->is_unix = is_unix;
2479 s->do_nodelay = do_nodelay && !is_unix;
2480
2481 chr->opaque = s;
2482 chr->chr_write = tcp_chr_write;
2483 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002484 chr->get_msgfd = tcp_get_msgfd;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002485 chr->chr_add_client = tcp_chr_add_client;
aliguori6f97dba2008-10-31 18:49:55 +00002486
2487 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002488 s->listen_fd = fd;
Marcelo Tosatti069c1592011-09-16 18:19:55 -03002489 qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002490 if (is_telnet)
2491 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002492
aliguori6f97dba2008-10-31 18:49:55 +00002493 } else {
aliguorif07b6002008-11-11 20:54:09 +00002494 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002495 s->fd = fd;
2496 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002497 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002498 }
2499
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002500 /* for "info chardev" monitor command */
Anthony Liguori7267c092011-08-20 22:09:37 -05002501 chr->filename = g_malloc(256);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002502 if (is_unix) {
2503 snprintf(chr->filename, 256, "unix:%s%s",
2504 qemu_opt_get(opts, "path"),
2505 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2506 } else if (is_telnet) {
2507 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2508 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2509 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2510 } else {
2511 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2512 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2513 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2514 }
2515
aliguori6f97dba2008-10-31 18:49:55 +00002516 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002517 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002518 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002519 tcp_chr_accept(chr);
2520 socket_set_nonblock(s->listen_fd);
2521 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002522 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002523
aliguori6f97dba2008-10-31 18:49:55 +00002524 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002525 if (local_err) {
2526 qerror_report_err(local_err);
2527 error_free(local_err);
2528 }
2529 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002530 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002531 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002532 g_free(s);
2533 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002534 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002535}
2536
Luiz Capitulino999bd672010-10-22 16:09:05 -02002537/***********************************************************/
2538/* Memory chardev */
2539typedef struct {
2540 size_t outbuf_size;
2541 size_t outbuf_capacity;
2542 uint8_t *outbuf;
2543} MemoryDriver;
2544
2545static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2546{
2547 MemoryDriver *d = chr->opaque;
2548
2549 /* TODO: the QString implementation has the same code, we should
2550 * introduce a generic way to do this in cutils.c */
2551 if (d->outbuf_capacity < d->outbuf_size + len) {
2552 /* grow outbuf */
2553 d->outbuf_capacity += len;
2554 d->outbuf_capacity *= 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05002555 d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002556 }
2557
2558 memcpy(d->outbuf + d->outbuf_size, buf, len);
2559 d->outbuf_size += len;
2560
2561 return len;
2562}
2563
2564void qemu_chr_init_mem(CharDriverState *chr)
2565{
2566 MemoryDriver *d;
2567
Anthony Liguori7267c092011-08-20 22:09:37 -05002568 d = g_malloc(sizeof(*d));
Luiz Capitulino999bd672010-10-22 16:09:05 -02002569 d->outbuf_size = 0;
2570 d->outbuf_capacity = 4096;
Anthony Liguori7267c092011-08-20 22:09:37 -05002571 d->outbuf = g_malloc0(d->outbuf_capacity);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002572
2573 memset(chr, 0, sizeof(*chr));
2574 chr->opaque = d;
2575 chr->chr_write = mem_chr_write;
2576}
2577
2578QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2579{
2580 MemoryDriver *d = chr->opaque;
2581 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2582}
2583
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002584/* NOTE: this driver can not be closed with qemu_chr_delete()! */
Luiz Capitulino999bd672010-10-22 16:09:05 -02002585void qemu_chr_close_mem(CharDriverState *chr)
2586{
2587 MemoryDriver *d = chr->opaque;
2588
Anthony Liguori7267c092011-08-20 22:09:37 -05002589 g_free(d->outbuf);
2590 g_free(chr->opaque);
Luiz Capitulino999bd672010-10-22 16:09:05 -02002591 chr->opaque = NULL;
2592 chr->chr_write = NULL;
2593}
2594
2595size_t qemu_chr_mem_osize(const CharDriverState *chr)
2596{
2597 const MemoryDriver *d = chr->opaque;
2598 return d->outbuf_size;
2599}
2600
Gerd Hoffmann33521632009-12-08 13:11:49 +01002601QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002602{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002603 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002604 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002605 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002606 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002607 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002608
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002609 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2610 if (error_is_set(&local_err)) {
2611 qerror_report_err(local_err);
2612 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002613 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002614 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002615
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002616 if (strstart(filename, "mon:", &p)) {
2617 filename = p;
2618 qemu_opt_set(opts, "mux", "on");
2619 }
2620
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002621 if (strcmp(filename, "null") == 0 ||
2622 strcmp(filename, "pty") == 0 ||
2623 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002624 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002625 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002626 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002627 return opts;
2628 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002629 if (strstart(filename, "vc", &p)) {
2630 qemu_opt_set(opts, "backend", "vc");
2631 if (*p == ':') {
2632 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2633 /* pixels */
2634 qemu_opt_set(opts, "width", width);
2635 qemu_opt_set(opts, "height", height);
2636 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2637 /* chars */
2638 qemu_opt_set(opts, "cols", width);
2639 qemu_opt_set(opts, "rows", height);
2640 } else {
2641 goto fail;
2642 }
2643 }
2644 return opts;
2645 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002646 if (strcmp(filename, "con:") == 0) {
2647 qemu_opt_set(opts, "backend", "console");
2648 return opts;
2649 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002650 if (strstart(filename, "COM", NULL)) {
2651 qemu_opt_set(opts, "backend", "serial");
2652 qemu_opt_set(opts, "path", filename);
2653 return opts;
2654 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002655 if (strstart(filename, "file:", &p)) {
2656 qemu_opt_set(opts, "backend", "file");
2657 qemu_opt_set(opts, "path", p);
2658 return opts;
2659 }
2660 if (strstart(filename, "pipe:", &p)) {
2661 qemu_opt_set(opts, "backend", "pipe");
2662 qemu_opt_set(opts, "path", p);
2663 return opts;
2664 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002665 if (strstart(filename, "tcp:", &p) ||
2666 strstart(filename, "telnet:", &p)) {
2667 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2668 host[0] = 0;
2669 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2670 goto fail;
2671 }
2672 qemu_opt_set(opts, "backend", "socket");
2673 qemu_opt_set(opts, "host", host);
2674 qemu_opt_set(opts, "port", port);
2675 if (p[pos] == ',') {
2676 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2677 goto fail;
2678 }
2679 if (strstart(filename, "telnet:", &p))
2680 qemu_opt_set(opts, "telnet", "on");
2681 return opts;
2682 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002683 if (strstart(filename, "udp:", &p)) {
2684 qemu_opt_set(opts, "backend", "udp");
2685 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2686 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01002687 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002688 goto fail;
2689 }
2690 }
2691 qemu_opt_set(opts, "host", host);
2692 qemu_opt_set(opts, "port", port);
2693 if (p[pos] == '@') {
2694 p += pos + 1;
2695 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2696 host[0] = 0;
2697 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002698 goto fail;
2699 }
2700 }
2701 qemu_opt_set(opts, "localaddr", host);
2702 qemu_opt_set(opts, "localport", port);
2703 }
2704 return opts;
2705 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002706 if (strstart(filename, "unix:", &p)) {
2707 qemu_opt_set(opts, "backend", "socket");
2708 if (qemu_opts_do_parse(opts, p, "path") != 0)
2709 goto fail;
2710 return opts;
2711 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002712 if (strstart(filename, "/dev/parport", NULL) ||
2713 strstart(filename, "/dev/ppi", NULL)) {
2714 qemu_opt_set(opts, "backend", "parport");
2715 qemu_opt_set(opts, "path", filename);
2716 return opts;
2717 }
2718 if (strstart(filename, "/dev/", NULL)) {
2719 qemu_opt_set(opts, "backend", "tty");
2720 qemu_opt_set(opts, "path", filename);
2721 return opts;
2722 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002723
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002724fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002725 qemu_opts_del(opts);
2726 return NULL;
2727}
2728
2729static const struct {
2730 const char *name;
Markus Armbruster1f514702012-02-07 15:09:08 +01002731 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002732} backend_table[] = {
2733 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002734 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002735 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002736 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002737 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002738#ifdef _WIN32
2739 { .name = "file", .open = qemu_chr_open_win_file_out },
2740 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002741 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002742 { .name = "serial", .open = qemu_chr_open_win },
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002743 { .name = "stdio", .open = qemu_chr_open_win_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002744#else
2745 { .name = "file", .open = qemu_chr_open_file_out },
2746 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002747 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002748 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002749#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002750#ifdef CONFIG_BRLAPI
2751 { .name = "braille", .open = chr_baum_init },
2752#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002753#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002754 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2755 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002756 { .name = "tty", .open = qemu_chr_open_tty },
2757#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002758#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2759 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002760 { .name = "parport", .open = qemu_chr_open_pp },
2761#endif
Alon Levycbcc6332011-01-19 10:49:50 +02002762#ifdef CONFIG_SPICE
2763 { .name = "spicevmc", .open = qemu_chr_open_spice },
2764#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002765};
2766
Anthony Liguorif69554b2011-08-15 11:17:37 -05002767CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002768 void (*init)(struct CharDriverState *s))
2769{
2770 CharDriverState *chr;
2771 int i;
2772
2773 if (qemu_opts_id(opts) == NULL) {
2774 fprintf(stderr, "chardev: no id specified\n");
2775 return NULL;
2776 }
2777
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00002778 if (qemu_opt_get(opts, "backend") == NULL) {
2779 fprintf(stderr, "chardev: \"%s\" missing backend\n",
2780 qemu_opts_id(opts));
2781 return NULL;
2782 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002783 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2784 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2785 break;
2786 }
2787 if (i == ARRAY_SIZE(backend_table)) {
2788 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2789 qemu_opt_get(opts, "backend"));
2790 return NULL;
2791 }
2792
Markus Armbruster1f514702012-02-07 15:09:08 +01002793 chr = backend_table[i].open(opts);
2794 if (!chr) {
2795 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2796 qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002797 return NULL;
2798 }
2799
2800 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05002801 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002802 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002803 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002804
2805 if (qemu_opt_get_bool(opts, "mux", 0)) {
2806 CharDriverState *base = chr;
2807 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05002808 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002809 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2810 chr = qemu_chr_open_mux(base);
2811 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002812 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002813 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09002814 } else {
2815 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002816 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002817 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002818 return chr;
2819}
2820
Anthony Liguori27143a42011-08-15 11:17:36 -05002821CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002822{
2823 const char *p;
2824 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002825 QemuOpts *opts;
2826
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002827 if (strstart(filename, "chardev:", &p)) {
2828 return qemu_chr_find(p);
2829 }
2830
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002831 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002832 if (!opts)
2833 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002834
Anthony Liguorif69554b2011-08-15 11:17:37 -05002835 chr = qemu_chr_new_from_opts(opts, init);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002836 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2837 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002838 }
Paolo Bonzini363f8cb2010-12-23 13:42:54 +01002839 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +00002840 return chr;
2841}
2842
Anthony Liguori15f31512011-08-15 11:17:35 -05002843void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01002844{
2845 if (chr->chr_set_echo) {
2846 chr->chr_set_echo(chr, echo);
2847 }
2848}
2849
Anthony Liguoric9d830e2011-08-15 11:17:32 -05002850void qemu_chr_fe_open(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002851{
2852 if (chr->chr_guest_open) {
2853 chr->chr_guest_open(chr);
2854 }
2855}
2856
Anthony Liguori28178222011-08-15 11:17:33 -05002857void qemu_chr_fe_close(struct CharDriverState *chr)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01002858{
2859 if (chr->chr_guest_close) {
2860 chr->chr_guest_close(chr);
2861 }
2862}
2863
Anthony Liguori70f24fb2011-08-15 11:17:38 -05002864void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00002865{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002866 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002867 if (chr->chr_close)
2868 chr->chr_close(chr);
Anthony Liguori7267c092011-08-20 22:09:37 -05002869 g_free(chr->filename);
2870 g_free(chr->label);
2871 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002872}
2873
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002874ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002875{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002876 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002877 CharDriverState *chr;
2878
Blue Swirl72cf2d42009-09-12 07:36:22 +00002879 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002880 ChardevInfoList *info = g_malloc0(sizeof(*info));
2881 info->value = g_malloc0(sizeof(*info->value));
2882 info->value->label = g_strdup(chr->label);
2883 info->value->filename = g_strdup(chr->filename);
2884
2885 info->next = chr_list;
2886 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00002887 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002888
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002889 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002890}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002891
2892CharDriverState *qemu_chr_find(const char *name)
2893{
2894 CharDriverState *chr;
2895
Blue Swirl72cf2d42009-09-12 07:36:22 +00002896 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002897 if (strcmp(chr->label, name) != 0)
2898 continue;
2899 return chr;
2900 }
2901 return NULL;
2902}
Anthony Liguori0beb4942011-12-22 15:29:25 -06002903
2904/* Get a character (serial) device interface. */
2905CharDriverState *qemu_char_get_next_serial(void)
2906{
2907 static int next_serial;
2908
2909 /* FIXME: This function needs to go away: use chardev properties! */
2910 return serial_hds[next_serial++];
2911}
2912