blob: 552a4988c8c681ef1b46b449f87919a04c595e21 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__GLIBC__)
57#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040058#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000062#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040063#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64#include <dev/ppbus/ppi.h>
65#include <dev/ppbus/ppbconf.h>
66#elif defined(__DragonFly__)
67#include <dev/misc/ppi/ppi.h>
68#include <bus/ppbus/ppbconf.h>
69#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010070#else
aliguori6f97dba2008-10-31 18:49:55 +000071#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000072#include <pty.h>
73
74#include <linux/ppdev.h>
75#include <linux/parport.h>
76#endif
77#ifdef __sun__
78#include <sys/stat.h>
79#include <sys/ethernet.h>
80#include <sys/sockio.h>
81#include <netinet/arp.h>
82#include <netinet/in.h>
83#include <netinet/in_systm.h>
84#include <netinet/ip.h>
85#include <netinet/ip_icmp.h> // must come after ip.h
86#include <netinet/udp.h>
87#include <netinet/tcp.h>
88#include <net/if.h>
89#include <syslog.h>
90#include <stropts.h>
91#endif
92#endif
93#endif
94
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010095#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020096#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000097
Amit Shah9bd78542009-11-03 19:59:54 +053098#define READ_BUF_LEN 4096
99
aliguori6f97dba2008-10-31 18:49:55 +0000100/***********************************************************/
101/* character device */
102
Blue Swirl72cf2d42009-09-12 07:36:22 +0000103static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000105
Hans de Goedea425d232011-11-19 10:22:43 +0100106void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000107{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200108 /* Keep track if the char device is open */
109 switch (event) {
110 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100111 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200112 break;
113 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100114 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200115 break;
116 }
117
aliguori6f97dba2008-10-31 18:49:55 +0000118 if (!s->chr_event)
119 return;
120 s->chr_event(s->handler_opaque, event);
121}
122
Hans de Goedefee204f2013-03-26 11:07:54 +0100123static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000124{
125 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100126 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Anthony Liguori9f939df2013-03-05 23:21:27 +0530127 s->idle_tag = 0;
128 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +0000129}
130
Hans de Goedefee204f2013-03-26 11:07:54 +0100131void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000132{
Anthony Liguori9f939df2013-03-05 23:21:27 +0530133 if (s->idle_tag == 0) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100134 s->idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000135 }
136}
137
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500138int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000139{
140 return s->chr_write(s, buf, len);
141}
142
Anthony Liguoricd187202013-03-26 10:04:17 -0500143int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
144{
145 int offset = 0;
146 int res;
147
148 while (offset < len) {
149 do {
150 res = s->chr_write(s, buf + offset, len - offset);
151 if (res == -1 && errno == EAGAIN) {
152 g_usleep(100);
153 }
154 } while (res == -1 && errno == EAGAIN);
155
156 if (res == 0) {
157 break;
158 }
159
160 if (res < 0) {
161 return res;
162 }
163
164 offset += res;
165 }
166
167 return offset;
168}
169
Anthony Liguori41084f12011-08-15 11:17:34 -0500170int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000171{
172 if (!s->chr_ioctl)
173 return -ENOTSUP;
174 return s->chr_ioctl(s, cmd, arg);
175}
176
Anthony Liguori909cda12011-08-15 11:17:31 -0500177int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000178{
179 if (!s->chr_can_read)
180 return 0;
181 return s->chr_can_read(s->handler_opaque);
182}
183
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500184void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000185{
Stefan Weilac310732012-04-19 22:27:14 +0200186 if (s->chr_read) {
187 s->chr_read(s->handler_opaque, buf, len);
188 }
aliguori6f97dba2008-10-31 18:49:55 +0000189}
190
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500191int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100192{
193 return s->get_msgfd ? s->get_msgfd(s) : -1;
194}
195
Daniel P. Berrange13661082011-06-23 13:31:42 +0100196int qemu_chr_add_client(CharDriverState *s, int fd)
197{
198 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
199}
200
aliguori6f97dba2008-10-31 18:49:55 +0000201void qemu_chr_accept_input(CharDriverState *s)
202{
203 if (s->chr_accept_input)
204 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100205 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000206}
207
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500208void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000209{
Amit Shah9bd78542009-11-03 19:59:54 +0530210 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000211 va_list ap;
212 va_start(ap, fmt);
213 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500214 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000215 va_end(ap);
216}
217
aliguori6f97dba2008-10-31 18:49:55 +0000218void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100219 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000220 IOReadHandler *fd_read,
221 IOEventHandler *fd_event,
222 void *opaque)
223{
Hans de Goede19083222013-03-26 11:07:56 +0100224 int fe_open;
225
Amit Shahda7d9982011-04-25 15:18:22 +0530226 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100227 fe_open = 0;
228 } else {
229 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530230 }
aliguori6f97dba2008-10-31 18:49:55 +0000231 s->chr_can_read = fd_can_read;
232 s->chr_read = fd_read;
233 s->chr_event = fd_event;
234 s->handler_opaque = opaque;
235 if (s->chr_update_read_handler)
236 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200237
Hans de Goede19083222013-03-26 11:07:56 +0100238 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100239 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100240 }
241
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200242 /* We're connecting to an already opened device, so let's make sure we
243 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100244 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100245 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200246 }
aliguori6f97dba2008-10-31 18:49:55 +0000247}
248
249static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
250{
251 return len;
252}
253
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100254static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000255{
256 CharDriverState *chr;
257
Anthony Liguori7267c092011-08-20 22:09:37 -0500258 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000259 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100260 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000261}
262
263/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000264#define MAX_MUX 4
265#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
266#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
267typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100268 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000269 IOReadHandler *chr_read[MAX_MUX];
270 IOEventHandler *chr_event[MAX_MUX];
271 void *ext_opaque[MAX_MUX];
272 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200273 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000274 int mux_cnt;
275 int term_got_escape;
276 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000277 /* Intermediate input buffer allows to catch escape sequences even if the
278 currently active device is not accepting any input - but only until it
279 is full as well. */
280 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
281 int prod[MAX_MUX];
282 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200283 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200284 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200285 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000286} MuxDriver;
287
288
289static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
290{
291 MuxDriver *d = chr->opaque;
292 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200293 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000294 ret = d->drv->chr_write(d->drv, buf, len);
295 } else {
296 int i;
297
298 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200299 for (i = 0; i < len; i++) {
300 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000301 char buf1[64];
302 int64_t ti;
303 int secs;
304
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100305 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200306 if (d->timestamps_start == -1)
307 d->timestamps_start = ti;
308 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000309 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000310 snprintf(buf1, sizeof(buf1),
311 "[%02d:%02d:%02d.%03d] ",
312 secs / 3600,
313 (secs / 60) % 60,
314 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000315 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000316 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200317 d->linestart = 0;
318 }
319 ret += d->drv->chr_write(d->drv, buf+i, 1);
320 if (buf[i] == '\n') {
321 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000322 }
323 }
324 }
325 return ret;
326}
327
328static const char * const mux_help[] = {
329 "% h print this help\n\r",
330 "% x exit emulator\n\r",
331 "% s save disk data back to file (if -snapshot)\n\r",
332 "% t toggle console timestamps\n\r"
333 "% b send break (magic sysrq)\n\r",
334 "% c switch between console and monitor\n\r",
335 "% % sends %\n\r",
336 NULL
337};
338
339int term_escape_char = 0x01; /* ctrl-a is used for escape */
340static void mux_print_help(CharDriverState *chr)
341{
342 int i, j;
343 char ebuf[15] = "Escape-Char";
344 char cbuf[50] = "\n\r";
345
346 if (term_escape_char > 0 && term_escape_char < 26) {
347 snprintf(cbuf, sizeof(cbuf), "\n\r");
348 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
349 } else {
350 snprintf(cbuf, sizeof(cbuf),
351 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
352 term_escape_char);
353 }
354 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
355 for (i = 0; mux_help[i] != NULL; i++) {
356 for (j=0; mux_help[i][j] != '\0'; j++) {
357 if (mux_help[i][j] == '%')
358 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
359 else
360 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
361 }
362 }
363}
364
aliguori2724b182009-03-05 23:01:47 +0000365static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
366{
367 if (d->chr_event[mux_nr])
368 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
369}
370
aliguori6f97dba2008-10-31 18:49:55 +0000371static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
372{
373 if (d->term_got_escape) {
374 d->term_got_escape = 0;
375 if (ch == term_escape_char)
376 goto send_char;
377 switch(ch) {
378 case '?':
379 case 'h':
380 mux_print_help(chr);
381 break;
382 case 'x':
383 {
384 const char *term = "QEMU: Terminated\n\r";
385 chr->chr_write(chr,(uint8_t *)term,strlen(term));
386 exit(0);
387 break;
388 }
389 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200390 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000391 break;
392 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100393 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000394 break;
395 case 'c':
396 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200397 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
398 d->focus++;
399 if (d->focus >= d->mux_cnt)
400 d->focus = 0;
401 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000402 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200403 case 't':
404 d->timestamps = !d->timestamps;
405 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200406 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200407 break;
aliguori6f97dba2008-10-31 18:49:55 +0000408 }
409 } else if (ch == term_escape_char) {
410 d->term_got_escape = 1;
411 } else {
412 send_char:
413 return 1;
414 }
415 return 0;
416}
417
418static void mux_chr_accept_input(CharDriverState *chr)
419{
aliguori6f97dba2008-10-31 18:49:55 +0000420 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200421 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000422
aliguoria80bf992009-03-05 23:00:02 +0000423 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000424 d->chr_can_read[m] &&
425 d->chr_can_read[m](d->ext_opaque[m])) {
426 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000427 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000428 }
429}
430
431static int mux_chr_can_read(void *opaque)
432{
433 CharDriverState *chr = opaque;
434 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200435 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000436
aliguoria80bf992009-03-05 23:00:02 +0000437 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000438 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000439 if (d->chr_can_read[m])
440 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000441 return 0;
442}
443
444static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
445{
446 CharDriverState *chr = opaque;
447 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200448 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000449 int i;
450
451 mux_chr_accept_input (opaque);
452
453 for(i = 0; i < size; i++)
454 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000455 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000456 d->chr_can_read[m] &&
457 d->chr_can_read[m](d->ext_opaque[m]))
458 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
459 else
aliguoria80bf992009-03-05 23:00:02 +0000460 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000461 }
462}
463
464static void mux_chr_event(void *opaque, int event)
465{
466 CharDriverState *chr = opaque;
467 MuxDriver *d = chr->opaque;
468 int i;
469
470 /* Send the event to all registered listeners */
471 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000472 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000473}
474
475static void mux_chr_update_read_handler(CharDriverState *chr)
476{
477 MuxDriver *d = chr->opaque;
478
479 if (d->mux_cnt >= MAX_MUX) {
480 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
481 return;
482 }
483 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
484 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
485 d->chr_read[d->mux_cnt] = chr->chr_read;
486 d->chr_event[d->mux_cnt] = chr->chr_event;
487 /* Fix up the real driver with mux routines */
488 if (d->mux_cnt == 0) {
489 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
490 mux_chr_event, chr);
491 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200492 if (d->focus != -1) {
493 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200494 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200495 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000496 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200497 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000498}
499
500static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
501{
502 CharDriverState *chr;
503 MuxDriver *d;
504
Anthony Liguori7267c092011-08-20 22:09:37 -0500505 chr = g_malloc0(sizeof(CharDriverState));
506 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000507
508 chr->opaque = d;
509 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200510 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000511 chr->chr_write = mux_chr_write;
512 chr->chr_update_read_handler = mux_chr_update_read_handler;
513 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100514 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100515 chr->chr_set_fe_open = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200516
517 /* Muxes are always open on creation */
Hans de Goedefee204f2013-03-26 11:07:54 +0100518 qemu_chr_be_generic_open(chr);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200519
aliguori6f97dba2008-10-31 18:49:55 +0000520 return chr;
521}
522
523
524#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000525int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000526{
527 int ret, len;
528
529 len = len1;
530 while (len > 0) {
531 ret = send(fd, buf, len, 0);
532 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000533 errno = WSAGetLastError();
534 if (errno != WSAEWOULDBLOCK) {
535 return -1;
536 }
537 } else if (ret == 0) {
538 break;
539 } else {
540 buf += ret;
541 len -= ret;
542 }
543 }
544 return len1 - len;
545}
546
547#else
548
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100549int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000550{
551 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100552 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000553
554 len = len1;
555 while (len > 0) {
556 ret = write(fd, buf, len);
557 if (ret < 0) {
558 if (errno != EINTR && errno != EAGAIN)
559 return -1;
560 } else if (ret == 0) {
561 break;
562 } else {
563 buf += ret;
564 len -= ret;
565 }
566 }
567 return len1 - len;
568}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500569
570int recv_all(int fd, void *_buf, int len1, bool single_read)
571{
572 int ret, len;
573 uint8_t *buf = _buf;
574
575 len = len1;
576 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
577 if (ret < 0) {
578 if (errno != EINTR && errno != EAGAIN) {
579 return -1;
580 }
581 continue;
582 } else {
583 if (single_read) {
584 return ret;
585 }
586 buf += ret;
587 len -= ret;
588 }
589 }
590 return len1 - len;
591}
592
aliguori6f97dba2008-10-31 18:49:55 +0000593#endif /* !_WIN32 */
594
Anthony Liguori96c63842013-03-05 23:21:18 +0530595typedef struct IOWatchPoll
596{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200597 GSource parent;
598
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200599 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530600 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530601
602 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200603 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530604 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530605} IOWatchPoll;
606
Anthony Liguori96c63842013-03-05 23:21:18 +0530607static IOWatchPoll *io_watch_poll_from_source(GSource *source)
608{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200609 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530610}
611
612static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
613{
614 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200615 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200616 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200617 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530618 return FALSE;
619 }
620
Paolo Bonzinid185c092013-04-05 17:59:33 +0200621 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200622 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
623 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200624 g_source_attach(iwp->src, NULL);
625 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200626 g_source_destroy(iwp->src);
627 g_source_unref(iwp->src);
628 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200629 }
630 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530631}
632
633static gboolean io_watch_poll_check(GSource *source)
634{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200635 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530636}
637
638static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
639 gpointer user_data)
640{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200641 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530642}
643
644static void io_watch_poll_finalize(GSource *source)
645{
646 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini910b6362013-04-19 17:32:06 +0200647 if (iwp->src) {
648 g_source_destroy(iwp->src);
649 g_source_unref(iwp->src);
650 iwp->src = NULL;
651 }
Anthony Liguori96c63842013-03-05 23:21:18 +0530652}
653
654static GSourceFuncs io_watch_poll_funcs = {
655 .prepare = io_watch_poll_prepare,
656 .check = io_watch_poll_check,
657 .dispatch = io_watch_poll_dispatch,
658 .finalize = io_watch_poll_finalize,
659};
660
661/* Can only be used for read */
662static guint io_add_watch_poll(GIOChannel *channel,
663 IOCanReadHandler *fd_can_read,
664 GIOFunc fd_read,
665 gpointer user_data)
666{
667 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200668 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530669
Paolo Bonzinid185c092013-04-05 17:59:33 +0200670 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530671 iwp->fd_can_read = fd_can_read;
672 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200673 iwp->channel = channel;
674 iwp->fd_read = (GSourceFunc) fd_read;
675 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530676
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200677 tag = g_source_attach(&iwp->parent, NULL);
678 g_source_unref(&iwp->parent);
679 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530680}
681
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000682#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530683static GIOChannel *io_channel_from_fd(int fd)
684{
685 GIOChannel *chan;
686
687 if (fd == -1) {
688 return NULL;
689 }
690
691 chan = g_io_channel_unix_new(fd);
692
693 g_io_channel_set_encoding(chan, NULL, NULL);
694 g_io_channel_set_buffered(chan, FALSE);
695
696 return chan;
697}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000698#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530699
Anthony Liguori76a96442013-03-05 23:21:21 +0530700static GIOChannel *io_channel_from_socket(int fd)
701{
702 GIOChannel *chan;
703
704 if (fd == -1) {
705 return NULL;
706 }
707
708#ifdef _WIN32
709 chan = g_io_channel_win32_new_socket(fd);
710#else
711 chan = g_io_channel_unix_new(fd);
712#endif
713
714 g_io_channel_set_encoding(chan, NULL, NULL);
715 g_io_channel_set_buffered(chan, FALSE);
716
717 return chan;
718}
719
Anthony Liguori684a0962013-03-29 11:39:50 -0500720static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530721{
722 GIOStatus status;
Anthony Liguori684a0962013-03-29 11:39:50 -0500723 size_t offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530724
Anthony Liguori684a0962013-03-29 11:39:50 -0500725 offset = 0;
726 while (offset < len) {
727 gsize bytes_written;
728
729 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530730 &bytes_written, NULL);
731 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530732 if (status == G_IO_STATUS_AGAIN) {
Anthony Liguori684a0962013-03-29 11:39:50 -0500733 /* If we've written any data, return a partial write. */
734 if (offset) {
735 break;
736 }
Anthony Liguori23673ca2013-03-05 23:21:23 +0530737 errno = EAGAIN;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530738 } else {
739 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530740 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500741
742 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530743 } else if (status == G_IO_STATUS_EOF) {
744 break;
Anthony Liguori96c63842013-03-05 23:21:18 +0530745 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500746
747 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530748 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500749
750 return offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530751}
Anthony Liguori96c63842013-03-05 23:21:18 +0530752
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000753#ifndef _WIN32
754
Anthony Liguoria29753f2013-03-05 23:21:19 +0530755typedef struct FDCharDriver {
756 CharDriverState *chr;
757 GIOChannel *fd_in, *fd_out;
758 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000759 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530760 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000761} FDCharDriver;
762
aliguori6f97dba2008-10-31 18:49:55 +0000763static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
764{
765 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530766
Anthony Liguori684a0962013-03-29 11:39:50 -0500767 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530768}
769
770static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
771{
772 CharDriverState *chr = opaque;
773 FDCharDriver *s = chr->opaque;
774 int len;
775 uint8_t buf[READ_BUF_LEN];
776 GIOStatus status;
777 gsize bytes_read;
778
779 len = sizeof(buf);
780 if (len > s->max_size) {
781 len = s->max_size;
782 }
783 if (len == 0) {
784 return FALSE;
785 }
786
787 status = g_io_channel_read_chars(chan, (gchar *)buf,
788 len, &bytes_read, NULL);
789 if (status == G_IO_STATUS_EOF) {
790 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
791 return FALSE;
792 }
793 if (status == G_IO_STATUS_NORMAL) {
794 qemu_chr_be_write(chr, buf, bytes_read);
795 }
796
797 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000798}
799
800static int fd_chr_read_poll(void *opaque)
801{
802 CharDriverState *chr = opaque;
803 FDCharDriver *s = chr->opaque;
804
Anthony Liguori909cda12011-08-15 11:17:31 -0500805 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000806 return s->max_size;
807}
808
Anthony Liguori23673ca2013-03-05 23:21:23 +0530809static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
810{
811 FDCharDriver *s = chr->opaque;
812 return g_io_create_watch(s->fd_out, cond);
813}
814
aliguori6f97dba2008-10-31 18:49:55 +0000815static void fd_chr_update_read_handler(CharDriverState *chr)
816{
817 FDCharDriver *s = chr->opaque;
818
Anthony Liguoria29753f2013-03-05 23:21:19 +0530819 if (s->fd_in_tag) {
820 g_source_remove(s->fd_in_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +0200821 s->fd_in_tag = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530822 }
823
824 if (s->fd_in) {
825 s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000826 }
827}
828
829static void fd_chr_close(struct CharDriverState *chr)
830{
831 FDCharDriver *s = chr->opaque;
832
Anthony Liguoria29753f2013-03-05 23:21:19 +0530833 if (s->fd_in_tag) {
834 g_source_remove(s->fd_in_tag);
835 s->fd_in_tag = 0;
836 }
837
838 if (s->fd_in) {
839 g_io_channel_unref(s->fd_in);
840 }
841 if (s->fd_out) {
842 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000843 }
844
Anthony Liguori7267c092011-08-20 22:09:37 -0500845 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100846 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000847}
848
849/* open a character device to a unix fd */
850static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
851{
852 CharDriverState *chr;
853 FDCharDriver *s;
854
Anthony Liguori7267c092011-08-20 22:09:37 -0500855 chr = g_malloc0(sizeof(CharDriverState));
856 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530857 s->fd_in = io_channel_from_fd(fd_in);
858 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530859 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530860 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000861 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530862 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000863 chr->chr_write = fd_chr_write;
864 chr->chr_update_read_handler = fd_chr_update_read_handler;
865 chr->chr_close = fd_chr_close;
866
Hans de Goedefee204f2013-03-26 11:07:54 +0100867 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000868
869 return chr;
870}
871
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100872static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000873{
874 int fd_in, fd_out;
875 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100876 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200877
878 if (filename == NULL) {
879 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100880 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200881 }
aliguori6f97dba2008-10-31 18:49:55 +0000882
883 snprintf(filename_in, 256, "%s.in", filename);
884 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100885 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
886 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000887 if (fd_in < 0 || fd_out < 0) {
888 if (fd_in >= 0)
889 close(fd_in);
890 if (fd_out >= 0)
891 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100892 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100893 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100894 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100895 }
aliguori6f97dba2008-10-31 18:49:55 +0000896 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100897 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000898}
899
aliguori6f97dba2008-10-31 18:49:55 +0000900/* init terminal so that we can grab keys */
901static struct termios oldtty;
902static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100903static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000904
905static void term_exit(void)
906{
907 tcsetattr (0, TCSANOW, &oldtty);
908 fcntl(0, F_SETFL, old_fd0_flags);
909}
910
Paolo Bonzinibb002512010-12-23 13:42:50 +0100911static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000912{
913 struct termios tty;
914
Paolo Bonzini03693642010-12-23 13:42:49 +0100915 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100916 if (!echo) {
917 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000918 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100919 tty.c_oflag |= OPOST;
920 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
921 tty.c_cflag &= ~(CSIZE|PARENB);
922 tty.c_cflag |= CS8;
923 tty.c_cc[VMIN] = 1;
924 tty.c_cc[VTIME] = 0;
925 }
aliguori6f97dba2008-10-31 18:49:55 +0000926 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100927 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000928 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000929
930 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000931}
932
933static void qemu_chr_close_stdio(struct CharDriverState *chr)
934{
935 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000936 fd_chr_close(chr);
937}
938
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100939static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000940{
941 CharDriverState *chr;
942
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400943 if (is_daemonized()) {
944 error_report("cannot use stdio with -daemonize");
945 return NULL;
946 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530947 old_fd0_flags = fcntl(0, F_GETFL);
948 tcgetattr (0, &oldtty);
949 fcntl(0, F_SETFL, O_NONBLOCK);
950 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100951
aliguori6f97dba2008-10-31 18:49:55 +0000952 chr = qemu_chr_open_fd(0, 1);
953 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100954 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100955 stdio_allow_signal = display_type != DT_NOGRAPHIC;
956 if (opts->has_signal) {
957 stdio_allow_signal = opts->signal;
958 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500959 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000960
Markus Armbruster1f514702012-02-07 15:09:08 +0100961 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000962}
963
964#ifdef __sun__
965/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000966static int openpty(int *amaster, int *aslave, char *name,
967 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000968{
969 const char *slave;
970 int mfd = -1, sfd = -1;
971
972 *amaster = *aslave = -1;
973
974 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
975 if (mfd < 0)
976 goto err;
977
978 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
979 goto err;
980
981 if ((slave = ptsname(mfd)) == NULL)
982 goto err;
983
984 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
985 goto err;
986
987 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
988 (termp != NULL && tcgetattr(sfd, termp) < 0))
989 goto err;
990
991 if (amaster)
992 *amaster = mfd;
993 if (aslave)
994 *aslave = sfd;
995 if (winp)
996 ioctl(sfd, TIOCSWINSZ, winp);
997
998 return 0;
999
1000err:
1001 if (sfd != -1)
1002 close(sfd);
1003 close(mfd);
1004 return -1;
1005}
1006
blueswir13f4cb3d2009-04-13 16:31:01 +00001007static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +00001008{
1009 termios_p->c_iflag &=
1010 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
1011 termios_p->c_oflag &= ~OPOST;
1012 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1013 termios_p->c_cflag &= ~(CSIZE|PARENB);
1014 termios_p->c_cflag |= CS8;
1015
1016 termios_p->c_cc[VMIN] = 0;
1017 termios_p->c_cc[VTIME] = 0;
1018}
1019#endif
1020
1021#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001022 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1023 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001024
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001025#define HAVE_CHARDEV_TTY 1
1026
aliguori6f97dba2008-10-31 18:49:55 +00001027typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301028 GIOChannel *fd;
1029 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001030 int connected;
1031 int polling;
1032 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301033 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001034} PtyCharDriver;
1035
1036static void pty_chr_update_read_handler(CharDriverState *chr);
1037static void pty_chr_state(CharDriverState *chr, int connected);
1038
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301039static gboolean pty_chr_timer(gpointer opaque)
1040{
1041 struct CharDriverState *chr = opaque;
1042 PtyCharDriver *s = chr->opaque;
1043
1044 if (s->connected) {
1045 goto out;
1046 }
1047 if (s->polling) {
1048 /* If we arrive here without polling being cleared due
1049 * read returning -EIO, then we are (re-)connected */
1050 pty_chr_state(chr, 1);
1051 goto out;
1052 }
1053
1054 /* Next poll ... */
1055 pty_chr_update_read_handler(chr);
1056
1057out:
1058 return FALSE;
1059}
1060
1061static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1062{
1063 PtyCharDriver *s = chr->opaque;
1064
1065 if (s->timer_tag) {
1066 g_source_remove(s->timer_tag);
1067 s->timer_tag = 0;
1068 }
1069
1070 if (ms == 1000) {
1071 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1072 } else {
1073 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1074 }
1075}
1076
aliguori6f97dba2008-10-31 18:49:55 +00001077static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1078{
1079 PtyCharDriver *s = chr->opaque;
1080
1081 if (!s->connected) {
1082 /* guest sends data, check for (re-)connect */
1083 pty_chr_update_read_handler(chr);
1084 return 0;
1085 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001086 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001087}
1088
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301089static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1090{
1091 PtyCharDriver *s = chr->opaque;
1092 return g_io_create_watch(s->fd, cond);
1093}
1094
aliguori6f97dba2008-10-31 18:49:55 +00001095static int pty_chr_read_poll(void *opaque)
1096{
1097 CharDriverState *chr = opaque;
1098 PtyCharDriver *s = chr->opaque;
1099
Anthony Liguori909cda12011-08-15 11:17:31 -05001100 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001101 return s->read_bytes;
1102}
1103
Anthony Liguori093d3a22013-03-05 23:21:20 +05301104static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001105{
1106 CharDriverState *chr = opaque;
1107 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301108 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301109 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301110 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001111
1112 len = sizeof(buf);
1113 if (len > s->read_bytes)
1114 len = s->read_bytes;
1115 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301116 return FALSE;
1117 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1118 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001119 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301120 return FALSE;
1121 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001122 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001123 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001124 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301125 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001126}
1127
1128static void pty_chr_update_read_handler(CharDriverState *chr)
1129{
1130 PtyCharDriver *s = chr->opaque;
1131
Anthony Liguori093d3a22013-03-05 23:21:20 +05301132 if (s->fd_tag) {
1133 g_source_remove(s->fd_tag);
1134 }
1135
1136 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001137 s->polling = 1;
1138 /*
1139 * Short timeout here: just need wait long enougth that qemu makes
1140 * it through the poll loop once. When reconnected we want a
1141 * short timeout so we notice it almost instantly. Otherwise
1142 * read() gives us -EIO instantly, making pty_chr_state() reset the
1143 * timeout to the normal (much longer) poll interval before the
1144 * timer triggers.
1145 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301146 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001147}
1148
1149static void pty_chr_state(CharDriverState *chr, int connected)
1150{
1151 PtyCharDriver *s = chr->opaque;
1152
1153 if (!connected) {
Paolo Bonzini910b6362013-04-19 17:32:06 +02001154 if (s->fd_tag) {
1155 g_source_remove(s->fd_tag);
1156 s->fd_tag = 0;
1157 }
aliguori6f97dba2008-10-31 18:49:55 +00001158 s->connected = 0;
1159 s->polling = 0;
1160 /* (re-)connect poll interval for idle guests: once per second.
1161 * We check more frequently in case the guests sends data to
1162 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301163 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001164 } else {
1165 if (!s->connected)
Hans de Goedefee204f2013-03-26 11:07:54 +01001166 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001167 s->connected = 1;
1168 }
1169}
1170
aliguori6f97dba2008-10-31 18:49:55 +00001171
1172static void pty_chr_close(struct CharDriverState *chr)
1173{
1174 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301175 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001176
Anthony Liguori093d3a22013-03-05 23:21:20 +05301177 if (s->fd_tag) {
1178 g_source_remove(s->fd_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001179 s->fd_tag = 0;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301180 }
1181 fd = g_io_channel_unix_get_fd(s->fd);
1182 g_io_channel_unref(s->fd);
1183 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301184 if (s->timer_tag) {
1185 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001186 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301187 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001188 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001189 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001190}
1191
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001192static CharDriverState *qemu_chr_open_pty(const char *id,
1193 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001194{
1195 CharDriverState *chr;
1196 PtyCharDriver *s;
1197 struct termios tty;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001198 int master_fd, slave_fd;
blueswir1c5e97232009-03-07 20:06:23 +00001199#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001200 char pty_name[PATH_MAX];
1201#define q_ptsname(x) pty_name
1202#else
1203 char *pty_name = NULL;
1204#define q_ptsname(x) ptsname(x)
1205#endif
1206
Markus Armbrustera4e26042011-11-11 10:40:05 +01001207 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001208 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001209 }
1210
1211 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001212 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001213 cfmakeraw(&tty);
1214 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1215 close(slave_fd);
1216
Markus Armbrustera4e26042011-11-11 10:40:05 +01001217 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001218
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001219 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1220 ret->pty = g_strdup(q_ptsname(master_fd));
1221 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001222
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001223 fprintf(stderr, "char device redirected to %s (label %s)\n",
1224 q_ptsname(master_fd), id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001225
1226 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001227 chr->opaque = s;
1228 chr->chr_write = pty_chr_write;
1229 chr->chr_update_read_handler = pty_chr_update_read_handler;
1230 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301231 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001232
Anthony Liguori093d3a22013-03-05 23:21:20 +05301233 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301234 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001235
Markus Armbruster1f514702012-02-07 15:09:08 +01001236 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001237}
1238
1239static void tty_serial_init(int fd, int speed,
1240 int parity, int data_bits, int stop_bits)
1241{
1242 struct termios tty;
1243 speed_t spd;
1244
1245#if 0
1246 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1247 speed, parity, data_bits, stop_bits);
1248#endif
1249 tcgetattr (fd, &tty);
1250
Stefan Weil45eea132009-10-26 16:10:10 +01001251#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1252 speed = speed * 10 / 11;
1253 do {
1254 check_speed(50);
1255 check_speed(75);
1256 check_speed(110);
1257 check_speed(134);
1258 check_speed(150);
1259 check_speed(200);
1260 check_speed(300);
1261 check_speed(600);
1262 check_speed(1200);
1263 check_speed(1800);
1264 check_speed(2400);
1265 check_speed(4800);
1266 check_speed(9600);
1267 check_speed(19200);
1268 check_speed(38400);
1269 /* Non-Posix values follow. They may be unsupported on some systems. */
1270 check_speed(57600);
1271 check_speed(115200);
1272#ifdef B230400
1273 check_speed(230400);
1274#endif
1275#ifdef B460800
1276 check_speed(460800);
1277#endif
1278#ifdef B500000
1279 check_speed(500000);
1280#endif
1281#ifdef B576000
1282 check_speed(576000);
1283#endif
1284#ifdef B921600
1285 check_speed(921600);
1286#endif
1287#ifdef B1000000
1288 check_speed(1000000);
1289#endif
1290#ifdef B1152000
1291 check_speed(1152000);
1292#endif
1293#ifdef B1500000
1294 check_speed(1500000);
1295#endif
1296#ifdef B2000000
1297 check_speed(2000000);
1298#endif
1299#ifdef B2500000
1300 check_speed(2500000);
1301#endif
1302#ifdef B3000000
1303 check_speed(3000000);
1304#endif
1305#ifdef B3500000
1306 check_speed(3500000);
1307#endif
1308#ifdef B4000000
1309 check_speed(4000000);
1310#endif
aliguori6f97dba2008-10-31 18:49:55 +00001311 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001312 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001313
1314 cfsetispeed(&tty, spd);
1315 cfsetospeed(&tty, spd);
1316
1317 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1318 |INLCR|IGNCR|ICRNL|IXON);
1319 tty.c_oflag |= OPOST;
1320 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1321 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1322 switch(data_bits) {
1323 default:
1324 case 8:
1325 tty.c_cflag |= CS8;
1326 break;
1327 case 7:
1328 tty.c_cflag |= CS7;
1329 break;
1330 case 6:
1331 tty.c_cflag |= CS6;
1332 break;
1333 case 5:
1334 tty.c_cflag |= CS5;
1335 break;
1336 }
1337 switch(parity) {
1338 default:
1339 case 'N':
1340 break;
1341 case 'E':
1342 tty.c_cflag |= PARENB;
1343 break;
1344 case 'O':
1345 tty.c_cflag |= PARENB | PARODD;
1346 break;
1347 }
1348 if (stop_bits == 2)
1349 tty.c_cflag |= CSTOPB;
1350
1351 tcsetattr (fd, TCSANOW, &tty);
1352}
1353
1354static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1355{
1356 FDCharDriver *s = chr->opaque;
1357
1358 switch(cmd) {
1359 case CHR_IOCTL_SERIAL_SET_PARAMS:
1360 {
1361 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301362 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1363 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001364 ssp->data_bits, ssp->stop_bits);
1365 }
1366 break;
1367 case CHR_IOCTL_SERIAL_SET_BREAK:
1368 {
1369 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301370 if (enable) {
1371 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1372 }
aliguori6f97dba2008-10-31 18:49:55 +00001373 }
1374 break;
1375 case CHR_IOCTL_SERIAL_GET_TIOCM:
1376 {
1377 int sarg = 0;
1378 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301379 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001380 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001381 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001382 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001383 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001384 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001385 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001386 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001387 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001388 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001389 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001390 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001391 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001392 *targ |= CHR_TIOCM_RTS;
1393 }
1394 break;
1395 case CHR_IOCTL_SERIAL_SET_TIOCM:
1396 {
1397 int sarg = *(int *)arg;
1398 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301399 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001400 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1401 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1402 if (sarg & CHR_TIOCM_CTS)
1403 targ |= TIOCM_CTS;
1404 if (sarg & CHR_TIOCM_CAR)
1405 targ |= TIOCM_CAR;
1406 if (sarg & CHR_TIOCM_DSR)
1407 targ |= TIOCM_DSR;
1408 if (sarg & CHR_TIOCM_RI)
1409 targ |= TIOCM_RI;
1410 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001411 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001412 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001413 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301414 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001415 }
1416 break;
1417 default:
1418 return -ENOTSUP;
1419 }
1420 return 0;
1421}
1422
David Ahern4266a132010-02-10 18:27:17 -07001423static void qemu_chr_close_tty(CharDriverState *chr)
1424{
1425 FDCharDriver *s = chr->opaque;
1426 int fd = -1;
1427
1428 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301429 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001430 }
1431
1432 fd_chr_close(chr);
1433
1434 if (fd >= 0) {
1435 close(fd);
1436 }
1437}
1438
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001439static CharDriverState *qemu_chr_open_tty_fd(int fd)
1440{
1441 CharDriverState *chr;
1442
1443 tty_serial_init(fd, 115200, 'N', 8, 1);
1444 chr = qemu_chr_open_fd(fd, fd);
1445 chr->chr_ioctl = tty_serial_ioctl;
1446 chr->chr_close = qemu_chr_close_tty;
1447 return chr;
1448}
aliguori6f97dba2008-10-31 18:49:55 +00001449#endif /* __linux__ || __sun__ */
1450
1451#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001452
1453#define HAVE_CHARDEV_PARPORT 1
1454
aliguori6f97dba2008-10-31 18:49:55 +00001455typedef struct {
1456 int fd;
1457 int mode;
1458} ParallelCharDriver;
1459
1460static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1461{
1462 if (s->mode != mode) {
1463 int m = mode;
1464 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1465 return 0;
1466 s->mode = mode;
1467 }
1468 return 1;
1469}
1470
1471static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1472{
1473 ParallelCharDriver *drv = chr->opaque;
1474 int fd = drv->fd;
1475 uint8_t b;
1476
1477 switch(cmd) {
1478 case CHR_IOCTL_PP_READ_DATA:
1479 if (ioctl(fd, PPRDATA, &b) < 0)
1480 return -ENOTSUP;
1481 *(uint8_t *)arg = b;
1482 break;
1483 case CHR_IOCTL_PP_WRITE_DATA:
1484 b = *(uint8_t *)arg;
1485 if (ioctl(fd, PPWDATA, &b) < 0)
1486 return -ENOTSUP;
1487 break;
1488 case CHR_IOCTL_PP_READ_CONTROL:
1489 if (ioctl(fd, PPRCONTROL, &b) < 0)
1490 return -ENOTSUP;
1491 /* Linux gives only the lowest bits, and no way to know data
1492 direction! For better compatibility set the fixed upper
1493 bits. */
1494 *(uint8_t *)arg = b | 0xc0;
1495 break;
1496 case CHR_IOCTL_PP_WRITE_CONTROL:
1497 b = *(uint8_t *)arg;
1498 if (ioctl(fd, PPWCONTROL, &b) < 0)
1499 return -ENOTSUP;
1500 break;
1501 case CHR_IOCTL_PP_READ_STATUS:
1502 if (ioctl(fd, PPRSTATUS, &b) < 0)
1503 return -ENOTSUP;
1504 *(uint8_t *)arg = b;
1505 break;
1506 case CHR_IOCTL_PP_DATA_DIR:
1507 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1508 return -ENOTSUP;
1509 break;
1510 case CHR_IOCTL_PP_EPP_READ_ADDR:
1511 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1512 struct ParallelIOArg *parg = arg;
1513 int n = read(fd, parg->buffer, parg->count);
1514 if (n != parg->count) {
1515 return -EIO;
1516 }
1517 }
1518 break;
1519 case CHR_IOCTL_PP_EPP_READ:
1520 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1521 struct ParallelIOArg *parg = arg;
1522 int n = read(fd, parg->buffer, parg->count);
1523 if (n != parg->count) {
1524 return -EIO;
1525 }
1526 }
1527 break;
1528 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1529 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1530 struct ParallelIOArg *parg = arg;
1531 int n = write(fd, parg->buffer, parg->count);
1532 if (n != parg->count) {
1533 return -EIO;
1534 }
1535 }
1536 break;
1537 case CHR_IOCTL_PP_EPP_WRITE:
1538 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1539 struct ParallelIOArg *parg = arg;
1540 int n = write(fd, parg->buffer, parg->count);
1541 if (n != parg->count) {
1542 return -EIO;
1543 }
1544 }
1545 break;
1546 default:
1547 return -ENOTSUP;
1548 }
1549 return 0;
1550}
1551
1552static void pp_close(CharDriverState *chr)
1553{
1554 ParallelCharDriver *drv = chr->opaque;
1555 int fd = drv->fd;
1556
1557 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1558 ioctl(fd, PPRELEASE);
1559 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001560 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001561 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001562}
1563
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001564static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001565{
1566 CharDriverState *chr;
1567 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001568
1569 if (ioctl(fd, PPCLAIM) < 0) {
1570 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001571 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001572 }
1573
Anthony Liguori7267c092011-08-20 22:09:37 -05001574 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001575 drv->fd = fd;
1576 drv->mode = IEEE1284_MODE_COMPAT;
1577
Anthony Liguori7267c092011-08-20 22:09:37 -05001578 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001579 chr->chr_write = null_chr_write;
1580 chr->chr_ioctl = pp_ioctl;
1581 chr->chr_close = pp_close;
1582 chr->opaque = drv;
1583
Hans de Goedefee204f2013-03-26 11:07:54 +01001584 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001585
Markus Armbruster1f514702012-02-07 15:09:08 +01001586 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001587}
1588#endif /* __linux__ */
1589
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001590#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001591
1592#define HAVE_CHARDEV_PARPORT 1
1593
blueswir16972f932008-11-22 20:49:12 +00001594static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1595{
Stefan Weile0efb992011-02-23 19:09:16 +01001596 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001597 uint8_t b;
1598
1599 switch(cmd) {
1600 case CHR_IOCTL_PP_READ_DATA:
1601 if (ioctl(fd, PPIGDATA, &b) < 0)
1602 return -ENOTSUP;
1603 *(uint8_t *)arg = b;
1604 break;
1605 case CHR_IOCTL_PP_WRITE_DATA:
1606 b = *(uint8_t *)arg;
1607 if (ioctl(fd, PPISDATA, &b) < 0)
1608 return -ENOTSUP;
1609 break;
1610 case CHR_IOCTL_PP_READ_CONTROL:
1611 if (ioctl(fd, PPIGCTRL, &b) < 0)
1612 return -ENOTSUP;
1613 *(uint8_t *)arg = b;
1614 break;
1615 case CHR_IOCTL_PP_WRITE_CONTROL:
1616 b = *(uint8_t *)arg;
1617 if (ioctl(fd, PPISCTRL, &b) < 0)
1618 return -ENOTSUP;
1619 break;
1620 case CHR_IOCTL_PP_READ_STATUS:
1621 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1622 return -ENOTSUP;
1623 *(uint8_t *)arg = b;
1624 break;
1625 default:
1626 return -ENOTSUP;
1627 }
1628 return 0;
1629}
1630
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001631static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001632{
1633 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001634
Anthony Liguori7267c092011-08-20 22:09:37 -05001635 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001636 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001637 chr->chr_write = null_chr_write;
1638 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001639 return chr;
blueswir16972f932008-11-22 20:49:12 +00001640}
1641#endif
1642
aliguori6f97dba2008-10-31 18:49:55 +00001643#else /* _WIN32 */
1644
1645typedef struct {
1646 int max_size;
1647 HANDLE hcom, hrecv, hsend;
1648 OVERLAPPED orecv, osend;
1649 BOOL fpipe;
1650 DWORD len;
1651} WinCharState;
1652
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001653typedef struct {
1654 HANDLE hStdIn;
1655 HANDLE hInputReadyEvent;
1656 HANDLE hInputDoneEvent;
1657 HANDLE hInputThread;
1658 uint8_t win_stdio_buf;
1659} WinStdioCharState;
1660
aliguori6f97dba2008-10-31 18:49:55 +00001661#define NSENDBUF 2048
1662#define NRECVBUF 2048
1663#define MAXCONNECT 1
1664#define NTIMEOUT 5000
1665
1666static int win_chr_poll(void *opaque);
1667static int win_chr_pipe_poll(void *opaque);
1668
1669static void win_chr_close(CharDriverState *chr)
1670{
1671 WinCharState *s = chr->opaque;
1672
1673 if (s->hsend) {
1674 CloseHandle(s->hsend);
1675 s->hsend = NULL;
1676 }
1677 if (s->hrecv) {
1678 CloseHandle(s->hrecv);
1679 s->hrecv = NULL;
1680 }
1681 if (s->hcom) {
1682 CloseHandle(s->hcom);
1683 s->hcom = NULL;
1684 }
1685 if (s->fpipe)
1686 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1687 else
1688 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301689
Hans de Goedea425d232011-11-19 10:22:43 +01001690 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001691}
1692
1693static int win_chr_init(CharDriverState *chr, const char *filename)
1694{
1695 WinCharState *s = chr->opaque;
1696 COMMCONFIG comcfg;
1697 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1698 COMSTAT comstat;
1699 DWORD size;
1700 DWORD err;
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 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1714 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1715 if (s->hcom == INVALID_HANDLE_VALUE) {
1716 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1717 s->hcom = NULL;
1718 goto fail;
1719 }
1720
1721 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1722 fprintf(stderr, "Failed SetupComm\n");
1723 goto fail;
1724 }
1725
1726 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1727 size = sizeof(COMMCONFIG);
1728 GetDefaultCommConfig(filename, &comcfg, &size);
1729 comcfg.dcb.DCBlength = sizeof(DCB);
1730 CommConfigDialog(filename, NULL, &comcfg);
1731
1732 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1733 fprintf(stderr, "Failed SetCommState\n");
1734 goto fail;
1735 }
1736
1737 if (!SetCommMask(s->hcom, EV_ERR)) {
1738 fprintf(stderr, "Failed SetCommMask\n");
1739 goto fail;
1740 }
1741
1742 cto.ReadIntervalTimeout = MAXDWORD;
1743 if (!SetCommTimeouts(s->hcom, &cto)) {
1744 fprintf(stderr, "Failed SetCommTimeouts\n");
1745 goto fail;
1746 }
1747
1748 if (!ClearCommError(s->hcom, &err, &comstat)) {
1749 fprintf(stderr, "Failed ClearCommError\n");
1750 goto fail;
1751 }
1752 qemu_add_polling_cb(win_chr_poll, chr);
1753 return 0;
1754
1755 fail:
1756 win_chr_close(chr);
1757 return -1;
1758}
1759
1760static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1761{
1762 WinCharState *s = chr->opaque;
1763 DWORD len, ret, size, err;
1764
1765 len = len1;
1766 ZeroMemory(&s->osend, sizeof(s->osend));
1767 s->osend.hEvent = s->hsend;
1768 while (len > 0) {
1769 if (s->hsend)
1770 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1771 else
1772 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1773 if (!ret) {
1774 err = GetLastError();
1775 if (err == ERROR_IO_PENDING) {
1776 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1777 if (ret) {
1778 buf += size;
1779 len -= size;
1780 } else {
1781 break;
1782 }
1783 } else {
1784 break;
1785 }
1786 } else {
1787 buf += size;
1788 len -= size;
1789 }
1790 }
1791 return len1 - len;
1792}
1793
1794static int win_chr_read_poll(CharDriverState *chr)
1795{
1796 WinCharState *s = chr->opaque;
1797
Anthony Liguori909cda12011-08-15 11:17:31 -05001798 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001799 return s->max_size;
1800}
1801
1802static void win_chr_readfile(CharDriverState *chr)
1803{
1804 WinCharState *s = chr->opaque;
1805 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301806 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001807 DWORD size;
1808
1809 ZeroMemory(&s->orecv, sizeof(s->orecv));
1810 s->orecv.hEvent = s->hrecv;
1811 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1812 if (!ret) {
1813 err = GetLastError();
1814 if (err == ERROR_IO_PENDING) {
1815 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1816 }
1817 }
1818
1819 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001820 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001821 }
1822}
1823
1824static void win_chr_read(CharDriverState *chr)
1825{
1826 WinCharState *s = chr->opaque;
1827
1828 if (s->len > s->max_size)
1829 s->len = s->max_size;
1830 if (s->len == 0)
1831 return;
1832
1833 win_chr_readfile(chr);
1834}
1835
1836static int win_chr_poll(void *opaque)
1837{
1838 CharDriverState *chr = opaque;
1839 WinCharState *s = chr->opaque;
1840 COMSTAT status;
1841 DWORD comerr;
1842
1843 ClearCommError(s->hcom, &comerr, &status);
1844 if (status.cbInQue > 0) {
1845 s->len = status.cbInQue;
1846 win_chr_read_poll(chr);
1847 win_chr_read(chr);
1848 return 1;
1849 }
1850 return 0;
1851}
1852
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001853static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001854{
1855 CharDriverState *chr;
1856 WinCharState *s;
1857
Anthony Liguori7267c092011-08-20 22:09:37 -05001858 chr = g_malloc0(sizeof(CharDriverState));
1859 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001860 chr->opaque = s;
1861 chr->chr_write = win_chr_write;
1862 chr->chr_close = win_chr_close;
1863
1864 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001865 g_free(s);
1866 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001867 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001868 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001869 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001870 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001871}
1872
1873static int win_chr_pipe_poll(void *opaque)
1874{
1875 CharDriverState *chr = opaque;
1876 WinCharState *s = chr->opaque;
1877 DWORD size;
1878
1879 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1880 if (size > 0) {
1881 s->len = size;
1882 win_chr_read_poll(chr);
1883 win_chr_read(chr);
1884 return 1;
1885 }
1886 return 0;
1887}
1888
1889static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1890{
1891 WinCharState *s = chr->opaque;
1892 OVERLAPPED ov;
1893 int ret;
1894 DWORD size;
1895 char openname[256];
1896
1897 s->fpipe = TRUE;
1898
1899 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1900 if (!s->hsend) {
1901 fprintf(stderr, "Failed CreateEvent\n");
1902 goto fail;
1903 }
1904 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1905 if (!s->hrecv) {
1906 fprintf(stderr, "Failed CreateEvent\n");
1907 goto fail;
1908 }
1909
1910 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1911 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1912 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1913 PIPE_WAIT,
1914 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1915 if (s->hcom == INVALID_HANDLE_VALUE) {
1916 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1917 s->hcom = NULL;
1918 goto fail;
1919 }
1920
1921 ZeroMemory(&ov, sizeof(ov));
1922 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1923 ret = ConnectNamedPipe(s->hcom, &ov);
1924 if (ret) {
1925 fprintf(stderr, "Failed ConnectNamedPipe\n");
1926 goto fail;
1927 }
1928
1929 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1930 if (!ret) {
1931 fprintf(stderr, "Failed GetOverlappedResult\n");
1932 if (ov.hEvent) {
1933 CloseHandle(ov.hEvent);
1934 ov.hEvent = NULL;
1935 }
1936 goto fail;
1937 }
1938
1939 if (ov.hEvent) {
1940 CloseHandle(ov.hEvent);
1941 ov.hEvent = NULL;
1942 }
1943 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1944 return 0;
1945
1946 fail:
1947 win_chr_close(chr);
1948 return -1;
1949}
1950
1951
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001952static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001953{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001954 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001955 CharDriverState *chr;
1956 WinCharState *s;
1957
Anthony Liguori7267c092011-08-20 22:09:37 -05001958 chr = g_malloc0(sizeof(CharDriverState));
1959 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001960 chr->opaque = s;
1961 chr->chr_write = win_chr_write;
1962 chr->chr_close = win_chr_close;
1963
1964 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001965 g_free(s);
1966 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001967 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001968 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001969 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001970 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001971}
1972
Markus Armbruster1f514702012-02-07 15:09:08 +01001973static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001974{
1975 CharDriverState *chr;
1976 WinCharState *s;
1977
Anthony Liguori7267c092011-08-20 22:09:37 -05001978 chr = g_malloc0(sizeof(CharDriverState));
1979 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001980 s->hcom = fd_out;
1981 chr->opaque = s;
1982 chr->chr_write = win_chr_write;
Hans de Goedefee204f2013-03-26 11:07:54 +01001983 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001984 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001985}
1986
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001987static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001988{
Markus Armbruster1f514702012-02-07 15:09:08 +01001989 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001990}
1991
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001992static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1993{
1994 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1995 DWORD dwSize;
1996 int len1;
1997
1998 len1 = len;
1999
2000 while (len1 > 0) {
2001 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2002 break;
2003 }
2004 buf += dwSize;
2005 len1 -= dwSize;
2006 }
2007
2008 return len - len1;
2009}
2010
2011static void win_stdio_wait_func(void *opaque)
2012{
2013 CharDriverState *chr = opaque;
2014 WinStdioCharState *stdio = chr->opaque;
2015 INPUT_RECORD buf[4];
2016 int ret;
2017 DWORD dwSize;
2018 int i;
2019
2020 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2021 &dwSize);
2022
2023 if (!ret) {
2024 /* Avoid error storm */
2025 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2026 return;
2027 }
2028
2029 for (i = 0; i < dwSize; i++) {
2030 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2031
2032 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2033 int j;
2034 if (kev->uChar.AsciiChar != 0) {
2035 for (j = 0; j < kev->wRepeatCount; j++) {
2036 if (qemu_chr_be_can_write(chr)) {
2037 uint8_t c = kev->uChar.AsciiChar;
2038 qemu_chr_be_write(chr, &c, 1);
2039 }
2040 }
2041 }
2042 }
2043 }
2044}
2045
2046static DWORD WINAPI win_stdio_thread(LPVOID param)
2047{
2048 CharDriverState *chr = param;
2049 WinStdioCharState *stdio = chr->opaque;
2050 int ret;
2051 DWORD dwSize;
2052
2053 while (1) {
2054
2055 /* Wait for one byte */
2056 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2057
2058 /* Exit in case of error, continue if nothing read */
2059 if (!ret) {
2060 break;
2061 }
2062 if (!dwSize) {
2063 continue;
2064 }
2065
2066 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2067 if (stdio->win_stdio_buf == '\r') {
2068 continue;
2069 }
2070
2071 /* Signal the main thread and wait until the byte was eaten */
2072 if (!SetEvent(stdio->hInputReadyEvent)) {
2073 break;
2074 }
2075 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2076 != WAIT_OBJECT_0) {
2077 break;
2078 }
2079 }
2080
2081 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2082 return 0;
2083}
2084
2085static void win_stdio_thread_wait_func(void *opaque)
2086{
2087 CharDriverState *chr = opaque;
2088 WinStdioCharState *stdio = chr->opaque;
2089
2090 if (qemu_chr_be_can_write(chr)) {
2091 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2092 }
2093
2094 SetEvent(stdio->hInputDoneEvent);
2095}
2096
2097static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2098{
2099 WinStdioCharState *stdio = chr->opaque;
2100 DWORD dwMode = 0;
2101
2102 GetConsoleMode(stdio->hStdIn, &dwMode);
2103
2104 if (echo) {
2105 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2106 } else {
2107 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2108 }
2109}
2110
2111static void win_stdio_close(CharDriverState *chr)
2112{
2113 WinStdioCharState *stdio = chr->opaque;
2114
2115 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2116 CloseHandle(stdio->hInputReadyEvent);
2117 }
2118 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2119 CloseHandle(stdio->hInputDoneEvent);
2120 }
2121 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2122 TerminateThread(stdio->hInputThread, 0);
2123 }
2124
2125 g_free(chr->opaque);
2126 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002127}
2128
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002129static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002130{
2131 CharDriverState *chr;
2132 WinStdioCharState *stdio;
2133 DWORD dwMode;
2134 int is_console = 0;
2135
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002136 chr = g_malloc0(sizeof(CharDriverState));
2137 stdio = g_malloc0(sizeof(WinStdioCharState));
2138
2139 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2140 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2141 fprintf(stderr, "cannot open stdio: invalid handle\n");
2142 exit(1);
2143 }
2144
2145 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2146
2147 chr->opaque = stdio;
2148 chr->chr_write = win_stdio_write;
2149 chr->chr_close = win_stdio_close;
2150
Anthony Liguoried7a1542013-03-05 23:21:17 +05302151 if (is_console) {
2152 if (qemu_add_wait_object(stdio->hStdIn,
2153 win_stdio_wait_func, chr)) {
2154 fprintf(stderr, "qemu_add_wait_object: failed\n");
2155 }
2156 } else {
2157 DWORD dwId;
2158
2159 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2160 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2161 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2162 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002163
Anthony Liguoried7a1542013-03-05 23:21:17 +05302164 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2165 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2166 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2167 fprintf(stderr, "cannot create stdio thread or event\n");
2168 exit(1);
2169 }
2170 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2171 win_stdio_thread_wait_func, chr)) {
2172 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002173 }
2174 }
2175
2176 dwMode |= ENABLE_LINE_INPUT;
2177
Anthony Liguoried7a1542013-03-05 23:21:17 +05302178 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002179 /* set the terminal in raw mode */
2180 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2181 dwMode |= ENABLE_PROCESSED_INPUT;
2182 }
2183
2184 SetConsoleMode(stdio->hStdIn, dwMode);
2185
2186 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2187 qemu_chr_fe_set_echo(chr, false);
2188
Markus Armbruster1f514702012-02-07 15:09:08 +01002189 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002190}
aliguori6f97dba2008-10-31 18:49:55 +00002191#endif /* !_WIN32 */
2192
Anthony Liguori5ab82112013-03-05 23:21:31 +05302193
aliguori6f97dba2008-10-31 18:49:55 +00002194/***********************************************************/
2195/* UDP Net console */
2196
2197typedef struct {
2198 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302199 GIOChannel *chan;
2200 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302201 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002202 int bufcnt;
2203 int bufptr;
2204 int max_size;
2205} NetCharDriver;
2206
2207static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2208{
2209 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302210 gsize bytes_written;
2211 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002212
Anthony Liguori76a96442013-03-05 23:21:21 +05302213 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2214 if (status == G_IO_STATUS_EOF) {
2215 return 0;
2216 } else if (status != G_IO_STATUS_NORMAL) {
2217 return -1;
2218 }
2219
2220 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002221}
2222
2223static int udp_chr_read_poll(void *opaque)
2224{
2225 CharDriverState *chr = opaque;
2226 NetCharDriver *s = chr->opaque;
2227
Anthony Liguori909cda12011-08-15 11:17:31 -05002228 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002229
2230 /* If there were any stray characters in the queue process them
2231 * first
2232 */
2233 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002234 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002235 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002236 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002237 }
2238 return s->max_size;
2239}
2240
Anthony Liguori76a96442013-03-05 23:21:21 +05302241static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002242{
2243 CharDriverState *chr = opaque;
2244 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302245 gsize bytes_read = 0;
2246 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002247
2248 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302249 return FALSE;
2250 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2251 &bytes_read, NULL);
2252 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002253 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302254 if (status != G_IO_STATUS_NORMAL) {
2255 return FALSE;
2256 }
aliguori6f97dba2008-10-31 18:49:55 +00002257
2258 s->bufptr = 0;
2259 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002260 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002261 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002262 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002263 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302264
2265 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002266}
2267
2268static void udp_chr_update_read_handler(CharDriverState *chr)
2269{
2270 NetCharDriver *s = chr->opaque;
2271
Anthony Liguori76a96442013-03-05 23:21:21 +05302272 if (s->tag) {
2273 g_source_remove(s->tag);
2274 s->tag = 0;
2275 }
2276
2277 if (s->chan) {
2278 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002279 }
2280}
2281
aliguori819f56b2009-03-28 17:58:14 +00002282static void udp_chr_close(CharDriverState *chr)
2283{
2284 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302285 if (s->tag) {
2286 g_source_remove(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002287 s->tag = 0;
Anthony Liguori76a96442013-03-05 23:21:21 +05302288 }
2289 if (s->chan) {
2290 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002291 closesocket(s->fd);
2292 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002293 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002294 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002295}
2296
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002297static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002298{
2299 CharDriverState *chr = NULL;
2300 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002301
Anthony Liguori7267c092011-08-20 22:09:37 -05002302 chr = g_malloc0(sizeof(CharDriverState));
2303 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002304
aliguori6f97dba2008-10-31 18:49:55 +00002305 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302306 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002307 s->bufcnt = 0;
2308 s->bufptr = 0;
2309 chr->opaque = s;
2310 chr->chr_write = udp_chr_write;
2311 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002312 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002313 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002314}
aliguori6f97dba2008-10-31 18:49:55 +00002315
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002316static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2317{
2318 Error *local_err = NULL;
2319 int fd = -1;
2320
2321 fd = inet_dgram_opts(opts, &local_err);
2322 if (fd < 0) {
2323 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002324 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002325 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002326}
2327
2328/***********************************************************/
2329/* TCP Net console */
2330
2331typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302332
2333 GIOChannel *chan, *listen_chan;
2334 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002335 int fd, listen_fd;
2336 int connected;
2337 int max_size;
2338 int do_telnetopt;
2339 int do_nodelay;
2340 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002341 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002342} TCPCharDriver;
2343
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302344static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002345
2346static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2347{
2348 TCPCharDriver *s = chr->opaque;
2349 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002350 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002351 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002352 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002353 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002354 }
2355}
2356
2357static int tcp_chr_read_poll(void *opaque)
2358{
2359 CharDriverState *chr = opaque;
2360 TCPCharDriver *s = chr->opaque;
2361 if (!s->connected)
2362 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002363 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002364 return s->max_size;
2365}
2366
2367#define IAC 255
2368#define IAC_BREAK 243
2369static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2370 TCPCharDriver *s,
2371 uint8_t *buf, int *size)
2372{
2373 /* Handle any telnet client's basic IAC options to satisfy char by
2374 * char mode with no echo. All IAC options will be removed from
2375 * the buf and the do_telnetopt variable will be used to track the
2376 * state of the width of the IAC information.
2377 *
2378 * IAC commands come in sets of 3 bytes with the exception of the
2379 * "IAC BREAK" command and the double IAC.
2380 */
2381
2382 int i;
2383 int j = 0;
2384
2385 for (i = 0; i < *size; i++) {
2386 if (s->do_telnetopt > 1) {
2387 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2388 /* Double IAC means send an IAC */
2389 if (j != i)
2390 buf[j] = buf[i];
2391 j++;
2392 s->do_telnetopt = 1;
2393 } else {
2394 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2395 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002396 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002397 s->do_telnetopt++;
2398 }
2399 s->do_telnetopt++;
2400 }
2401 if (s->do_telnetopt >= 4) {
2402 s->do_telnetopt = 1;
2403 }
2404 } else {
2405 if ((unsigned char)buf[i] == IAC) {
2406 s->do_telnetopt = 2;
2407 } else {
2408 if (j != i)
2409 buf[j] = buf[i];
2410 j++;
2411 }
2412 }
2413 }
2414 *size = j;
2415}
2416
Mark McLoughlin7d174052009-07-22 09:11:39 +01002417static int tcp_get_msgfd(CharDriverState *chr)
2418{
2419 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002420 int fd = s->msgfd;
2421 s->msgfd = -1;
2422 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002423}
2424
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002425#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002426static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2427{
2428 TCPCharDriver *s = chr->opaque;
2429 struct cmsghdr *cmsg;
2430
2431 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2432 int fd;
2433
2434 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2435 cmsg->cmsg_level != SOL_SOCKET ||
2436 cmsg->cmsg_type != SCM_RIGHTS)
2437 continue;
2438
2439 fd = *((int *)CMSG_DATA(cmsg));
2440 if (fd < 0)
2441 continue;
2442
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002443 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2444 qemu_set_block(fd);
2445
Corey Bryant06138652012-08-14 16:43:42 -04002446#ifndef MSG_CMSG_CLOEXEC
2447 qemu_set_cloexec(fd);
2448#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002449 if (s->msgfd != -1)
2450 close(s->msgfd);
2451 s->msgfd = fd;
2452 }
2453}
2454
Mark McLoughlin9977c892009-07-22 09:11:38 +01002455static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2456{
2457 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002458 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002459 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002460 union {
2461 struct cmsghdr cmsg;
2462 char control[CMSG_SPACE(sizeof(int))];
2463 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002464 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002465 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002466
2467 iov[0].iov_base = buf;
2468 iov[0].iov_len = len;
2469
2470 msg.msg_iov = iov;
2471 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002472 msg.msg_control = &msg_control;
2473 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002474
Corey Bryant06138652012-08-14 16:43:42 -04002475#ifdef MSG_CMSG_CLOEXEC
2476 flags |= MSG_CMSG_CLOEXEC;
2477#endif
2478 ret = recvmsg(s->fd, &msg, flags);
2479 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002480 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002481 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002482
2483 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002484}
2485#else
2486static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2487{
2488 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002489 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002490}
2491#endif
2492
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302493static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2494{
2495 TCPCharDriver *s = chr->opaque;
2496 return g_io_create_watch(s->chan, cond);
2497}
2498
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302499static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002500{
2501 CharDriverState *chr = opaque;
2502 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302503 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002504 int len, size;
2505
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302506 if (!s->connected || s->max_size <= 0) {
2507 return FALSE;
2508 }
aliguori6f97dba2008-10-31 18:49:55 +00002509 len = sizeof(buf);
2510 if (len > s->max_size)
2511 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002512 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002513 if (size == 0) {
2514 /* connection closed */
2515 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302516 if (s->listen_chan) {
2517 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002518 }
Paolo Bonzini910b6362013-04-19 17:32:06 +02002519 if (s->tag) {
2520 g_source_remove(s->tag);
2521 s->tag = 0;
2522 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302523 g_io_channel_unref(s->chan);
2524 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002525 closesocket(s->fd);
2526 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002527 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002528 } else if (size > 0) {
2529 if (s->do_telnetopt)
2530 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2531 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002532 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002533 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302534
2535 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002536}
2537
Blue Swirl68c18d12010-08-15 09:46:24 +00002538#ifndef _WIN32
2539CharDriverState *qemu_chr_open_eventfd(int eventfd)
2540{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002541 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002542}
Blue Swirl68c18d12010-08-15 09:46:24 +00002543#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002544
aliguori6f97dba2008-10-31 18:49:55 +00002545static void tcp_chr_connect(void *opaque)
2546{
2547 CharDriverState *chr = opaque;
2548 TCPCharDriver *s = chr->opaque;
2549
2550 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302551 if (s->chan) {
2552 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002553 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002554 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002555}
2556
2557#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2558static void tcp_chr_telnet_init(int fd)
2559{
2560 char buf[3];
2561 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2562 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2563 send(fd, (char *)buf, 3, 0);
2564 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2565 send(fd, (char *)buf, 3, 0);
2566 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2567 send(fd, (char *)buf, 3, 0);
2568 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2569 send(fd, (char *)buf, 3, 0);
2570}
2571
Daniel P. Berrange13661082011-06-23 13:31:42 +01002572static int tcp_chr_add_client(CharDriverState *chr, int fd)
2573{
2574 TCPCharDriver *s = chr->opaque;
2575 if (s->fd != -1)
2576 return -1;
2577
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002578 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002579 if (s->do_nodelay)
2580 socket_set_nodelay(fd);
2581 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302582 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002583 if (s->listen_tag) {
2584 g_source_remove(s->listen_tag);
2585 s->listen_tag = 0;
2586 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002587 tcp_chr_connect(chr);
2588
2589 return 0;
2590}
2591
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302592static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002593{
2594 CharDriverState *chr = opaque;
2595 TCPCharDriver *s = chr->opaque;
2596 struct sockaddr_in saddr;
2597#ifndef _WIN32
2598 struct sockaddr_un uaddr;
2599#endif
2600 struct sockaddr *addr;
2601 socklen_t len;
2602 int fd;
2603
2604 for(;;) {
2605#ifndef _WIN32
2606 if (s->is_unix) {
2607 len = sizeof(uaddr);
2608 addr = (struct sockaddr *)&uaddr;
2609 } else
2610#endif
2611 {
2612 len = sizeof(saddr);
2613 addr = (struct sockaddr *)&saddr;
2614 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002615 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002616 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302617 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002618 } else if (fd >= 0) {
2619 if (s->do_telnetopt)
2620 tcp_chr_telnet_init(fd);
2621 break;
2622 }
2623 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002624 if (tcp_chr_add_client(chr, fd) < 0)
2625 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302626
2627 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002628}
2629
2630static void tcp_chr_close(CharDriverState *chr)
2631{
2632 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002633 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302634 if (s->tag) {
2635 g_source_remove(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002636 s->tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302637 }
2638 if (s->chan) {
2639 g_io_channel_unref(s->chan);
2640 }
aliguori6f97dba2008-10-31 18:49:55 +00002641 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002642 }
2643 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302644 if (s->listen_tag) {
2645 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002646 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302647 }
2648 if (s->listen_chan) {
2649 g_io_channel_unref(s->listen_chan);
2650 }
aliguori6f97dba2008-10-31 18:49:55 +00002651 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002652 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002653 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002654 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002655}
2656
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002657static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2658 bool is_listen, bool is_telnet,
2659 bool is_waitconnect,
2660 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002661{
2662 CharDriverState *chr = NULL;
2663 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002664 char host[NI_MAXHOST], serv[NI_MAXSERV];
2665 const char *left = "", *right = "";
2666 struct sockaddr_storage ss;
2667 socklen_t ss_len = sizeof(ss);
2668
2669 memset(&ss, 0, ss_len);
2670 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2671 error_setg(errp, "getsockname: %s", strerror(errno));
2672 return NULL;
2673 }
2674
2675 chr = g_malloc0(sizeof(CharDriverState));
2676 s = g_malloc0(sizeof(TCPCharDriver));
2677
2678 s->connected = 0;
2679 s->fd = -1;
2680 s->listen_fd = -1;
2681 s->msgfd = -1;
2682
2683 chr->filename = g_malloc(256);
2684 switch (ss.ss_family) {
2685#ifndef _WIN32
2686 case AF_UNIX:
2687 s->is_unix = 1;
2688 snprintf(chr->filename, 256, "unix:%s%s",
2689 ((struct sockaddr_un *)(&ss))->sun_path,
2690 is_listen ? ",server" : "");
2691 break;
2692#endif
2693 case AF_INET6:
2694 left = "[";
2695 right = "]";
2696 /* fall through */
2697 case AF_INET:
2698 s->do_nodelay = do_nodelay;
2699 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2700 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002701 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002702 is_telnet ? "telnet" : "tcp",
2703 left, host, right, serv,
2704 is_listen ? ",server" : "");
2705 break;
2706 }
2707
2708 chr->opaque = s;
2709 chr->chr_write = tcp_chr_write;
2710 chr->chr_close = tcp_chr_close;
2711 chr->get_msgfd = tcp_get_msgfd;
2712 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302713 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002714
2715 if (is_listen) {
2716 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302717 s->listen_chan = io_channel_from_socket(s->listen_fd);
2718 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002719 if (is_telnet) {
2720 s->do_telnetopt = 1;
2721 }
2722 } else {
2723 s->connected = 1;
2724 s->fd = fd;
2725 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302726 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002727 tcp_chr_connect(chr);
2728 }
2729
2730 if (is_listen && is_waitconnect) {
2731 printf("QEMU waiting for connection on: %s\n",
2732 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302733 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002734 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002735 }
2736 return chr;
2737}
2738
2739static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2740{
2741 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002742 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002743 int fd = -1;
2744 int is_listen;
2745 int is_waitconnect;
2746 int do_nodelay;
2747 int is_unix;
2748 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002749
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002750 is_listen = qemu_opt_get_bool(opts, "server", 0);
2751 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2752 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2753 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2754 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002755 if (!is_listen)
2756 is_waitconnect = 0;
2757
aliguorif07b6002008-11-11 20:54:09 +00002758 if (is_unix) {
2759 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002760 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002761 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002762 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002763 }
2764 } else {
2765 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002766 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002767 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002768 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002769 }
2770 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002771 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002772 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002773 }
aliguori6f97dba2008-10-31 18:49:55 +00002774
2775 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002776 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002777
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002778 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2779 is_waitconnect, &local_err);
2780 if (error_is_set(&local_err)) {
2781 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002782 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002783 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002784
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002785
aliguori6f97dba2008-10-31 18:49:55 +00002786 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002787 if (local_err) {
2788 qerror_report_err(local_err);
2789 error_free(local_err);
2790 }
2791 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002792 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002793 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002794 if (chr) {
2795 g_free(chr->opaque);
2796 g_free(chr);
2797 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002798 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002799}
2800
Lei Li51767e72013-01-25 00:03:19 +08002801/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002802/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002803
2804typedef struct {
2805 size_t size;
2806 size_t prod;
2807 size_t cons;
2808 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002809} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002810
Markus Armbruster3949e592013-02-06 21:27:24 +01002811static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002812{
Markus Armbruster3949e592013-02-06 21:27:24 +01002813 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002814
Markus Armbruster5c230102013-02-06 21:27:23 +01002815 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002816}
2817
Markus Armbruster3949e592013-02-06 21:27:24 +01002818static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002819{
Markus Armbruster3949e592013-02-06 21:27:24 +01002820 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002821 int i;
2822
2823 if (!buf || (len < 0)) {
2824 return -1;
2825 }
2826
2827 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002828 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2829 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002830 d->cons = d->prod - d->size;
2831 }
2832 }
2833
2834 return 0;
2835}
2836
Markus Armbruster3949e592013-02-06 21:27:24 +01002837static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002838{
Markus Armbruster3949e592013-02-06 21:27:24 +01002839 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002840 int i;
2841
Markus Armbruster5c230102013-02-06 21:27:23 +01002842 for (i = 0; i < len && d->cons != d->prod; i++) {
2843 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002844 }
2845
2846 return i;
2847}
2848
Markus Armbruster3949e592013-02-06 21:27:24 +01002849static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002850{
Markus Armbruster3949e592013-02-06 21:27:24 +01002851 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002852
2853 g_free(d->cbuf);
2854 g_free(d);
2855 chr->opaque = NULL;
2856}
2857
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002858static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2859 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002860{
2861 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002862 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002863
2864 chr = g_malloc0(sizeof(CharDriverState));
2865 d = g_malloc(sizeof(*d));
2866
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002867 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002868
2869 /* The size must be power of 2 */
2870 if (d->size & (d->size - 1)) {
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002871 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002872 goto fail;
2873 }
2874
2875 d->prod = 0;
2876 d->cons = 0;
2877 d->cbuf = g_malloc0(d->size);
2878
2879 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002880 chr->chr_write = ringbuf_chr_write;
2881 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002882
2883 return chr;
2884
2885fail:
2886 g_free(d);
2887 g_free(chr);
2888 return NULL;
2889}
2890
Markus Armbruster3949e592013-02-06 21:27:24 +01002891static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002892{
Markus Armbruster3949e592013-02-06 21:27:24 +01002893 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002894}
2895
Markus Armbruster3949e592013-02-06 21:27:24 +01002896void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002897 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002898 Error **errp)
2899{
2900 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002901 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002902 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002903 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002904
2905 chr = qemu_chr_find(device);
2906 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002907 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002908 return;
2909 }
2910
Markus Armbruster3949e592013-02-06 21:27:24 +01002911 if (!chr_is_ringbuf(chr)) {
2912 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002913 return;
2914 }
2915
Lei Li1f590cf2013-01-25 00:03:20 +08002916 if (has_format && (format == DATA_FORMAT_BASE64)) {
2917 write_data = g_base64_decode(data, &write_count);
2918 } else {
2919 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002920 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002921 }
2922
Markus Armbruster3949e592013-02-06 21:27:24 +01002923 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002924
Markus Armbruster13289fb2013-02-06 21:27:18 +01002925 if (write_data != (uint8_t *)data) {
2926 g_free((void *)write_data);
2927 }
2928
Lei Li1f590cf2013-01-25 00:03:20 +08002929 if (ret < 0) {
2930 error_setg(errp, "Failed to write to device %s", device);
2931 return;
2932 }
2933}
2934
Markus Armbruster3949e592013-02-06 21:27:24 +01002935char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002936 bool has_format, enum DataFormat format,
2937 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002938{
2939 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002940 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002941 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002942 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002943
2944 chr = qemu_chr_find(device);
2945 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002946 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002947 return NULL;
2948 }
2949
Markus Armbruster3949e592013-02-06 21:27:24 +01002950 if (!chr_is_ringbuf(chr)) {
2951 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002952 return NULL;
2953 }
2954
2955 if (size <= 0) {
2956 error_setg(errp, "size must be greater than zero");
2957 return NULL;
2958 }
2959
Markus Armbruster3949e592013-02-06 21:27:24 +01002960 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002961 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002962 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002963
Markus Armbruster3949e592013-02-06 21:27:24 +01002964 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002965
2966 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002967 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002968 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002969 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002970 /*
2971 * FIXME should read only complete, valid UTF-8 characters up
2972 * to @size bytes. Invalid sequences should be replaced by a
2973 * suitable replacement character. Except when (and only
2974 * when) ring buffer lost characters since last read, initial
2975 * continuation characters should be dropped.
2976 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002977 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002978 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002979 }
2980
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002981 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002982}
2983
Gerd Hoffmann33521632009-12-08 13:11:49 +01002984QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002985{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002986 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002987 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002988 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002989 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002990 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002991
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002992 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2993 if (error_is_set(&local_err)) {
2994 qerror_report_err(local_err);
2995 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002996 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002997 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002998
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002999 if (strstart(filename, "mon:", &p)) {
3000 filename = p;
3001 qemu_opt_set(opts, "mux", "on");
3002 }
3003
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003004 if (strcmp(filename, "null") == 0 ||
3005 strcmp(filename, "pty") == 0 ||
3006 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003007 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003008 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003009 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003010 return opts;
3011 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003012 if (strstart(filename, "vc", &p)) {
3013 qemu_opt_set(opts, "backend", "vc");
3014 if (*p == ':') {
3015 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3016 /* pixels */
3017 qemu_opt_set(opts, "width", width);
3018 qemu_opt_set(opts, "height", height);
3019 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3020 /* chars */
3021 qemu_opt_set(opts, "cols", width);
3022 qemu_opt_set(opts, "rows", height);
3023 } else {
3024 goto fail;
3025 }
3026 }
3027 return opts;
3028 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003029 if (strcmp(filename, "con:") == 0) {
3030 qemu_opt_set(opts, "backend", "console");
3031 return opts;
3032 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003033 if (strstart(filename, "COM", NULL)) {
3034 qemu_opt_set(opts, "backend", "serial");
3035 qemu_opt_set(opts, "path", filename);
3036 return opts;
3037 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003038 if (strstart(filename, "file:", &p)) {
3039 qemu_opt_set(opts, "backend", "file");
3040 qemu_opt_set(opts, "path", p);
3041 return opts;
3042 }
3043 if (strstart(filename, "pipe:", &p)) {
3044 qemu_opt_set(opts, "backend", "pipe");
3045 qemu_opt_set(opts, "path", p);
3046 return opts;
3047 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003048 if (strstart(filename, "tcp:", &p) ||
3049 strstart(filename, "telnet:", &p)) {
3050 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3051 host[0] = 0;
3052 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3053 goto fail;
3054 }
3055 qemu_opt_set(opts, "backend", "socket");
3056 qemu_opt_set(opts, "host", host);
3057 qemu_opt_set(opts, "port", port);
3058 if (p[pos] == ',') {
3059 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3060 goto fail;
3061 }
3062 if (strstart(filename, "telnet:", &p))
3063 qemu_opt_set(opts, "telnet", "on");
3064 return opts;
3065 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003066 if (strstart(filename, "udp:", &p)) {
3067 qemu_opt_set(opts, "backend", "udp");
3068 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3069 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003070 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003071 goto fail;
3072 }
3073 }
3074 qemu_opt_set(opts, "host", host);
3075 qemu_opt_set(opts, "port", port);
3076 if (p[pos] == '@') {
3077 p += pos + 1;
3078 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3079 host[0] = 0;
3080 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003081 goto fail;
3082 }
3083 }
3084 qemu_opt_set(opts, "localaddr", host);
3085 qemu_opt_set(opts, "localport", port);
3086 }
3087 return opts;
3088 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003089 if (strstart(filename, "unix:", &p)) {
3090 qemu_opt_set(opts, "backend", "socket");
3091 if (qemu_opts_do_parse(opts, p, "path") != 0)
3092 goto fail;
3093 return opts;
3094 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003095 if (strstart(filename, "/dev/parport", NULL) ||
3096 strstart(filename, "/dev/ppi", NULL)) {
3097 qemu_opt_set(opts, "backend", "parport");
3098 qemu_opt_set(opts, "path", filename);
3099 return opts;
3100 }
3101 if (strstart(filename, "/dev/", NULL)) {
3102 qemu_opt_set(opts, "backend", "tty");
3103 qemu_opt_set(opts, "path", filename);
3104 return opts;
3105 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003106
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003107fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003108 qemu_opts_del(opts);
3109 return NULL;
3110}
3111
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003112static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3113 Error **errp)
3114{
3115 const char *path = qemu_opt_get(opts, "path");
3116
3117 if (path == NULL) {
3118 error_setg(errp, "chardev: file: no filename given");
3119 return;
3120 }
3121 backend->file = g_new0(ChardevFile, 1);
3122 backend->file->out = g_strdup(path);
3123}
3124
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003125static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3126 Error **errp)
3127{
3128 backend->stdio = g_new0(ChardevStdio, 1);
3129 backend->stdio->has_signal = true;
3130 backend->stdio->signal =
3131 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3132}
3133
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003134static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3135 Error **errp)
3136{
3137 const char *device = qemu_opt_get(opts, "path");
3138
3139 if (device == NULL) {
3140 error_setg(errp, "chardev: serial/tty: no device path given");
3141 return;
3142 }
3143 backend->serial = g_new0(ChardevHostdev, 1);
3144 backend->serial->device = g_strdup(device);
3145}
3146
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003147static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3148 Error **errp)
3149{
3150 const char *device = qemu_opt_get(opts, "path");
3151
3152 if (device == NULL) {
3153 error_setg(errp, "chardev: parallel: no device path given");
3154 return;
3155 }
3156 backend->parallel = g_new0(ChardevHostdev, 1);
3157 backend->parallel->device = g_strdup(device);
3158}
3159
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003160static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3161 Error **errp)
3162{
3163 const char *device = qemu_opt_get(opts, "path");
3164
3165 if (device == NULL) {
3166 error_setg(errp, "chardev: pipe: no device path given");
3167 return;
3168 }
3169 backend->pipe = g_new0(ChardevHostdev, 1);
3170 backend->pipe->device = g_strdup(device);
3171}
3172
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003173static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3174 Error **errp)
3175{
3176 int val;
3177
3178 backend->memory = g_new0(ChardevRingbuf, 1);
3179
3180 val = qemu_opt_get_number(opts, "size", 0);
3181 if (val != 0) {
3182 backend->memory->has_size = true;
3183 backend->memory->size = val;
3184 }
3185}
3186
Anthony Liguorid654f342013-03-05 23:21:28 +05303187typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003188 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003189 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003190 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003191 /* new, qapi-based */
3192 int kind;
3193 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303194} CharDriver;
3195
3196static GSList *backends;
3197
3198void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3199{
3200 CharDriver *s;
3201
3202 s = g_malloc0(sizeof(*s));
3203 s->name = g_strdup(name);
3204 s->open = open;
3205
3206 backends = g_slist_append(backends, s);
3207}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003208
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003209void register_char_driver_qapi(const char *name, int kind,
3210 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3211{
3212 CharDriver *s;
3213
3214 s = g_malloc0(sizeof(*s));
3215 s->name = g_strdup(name);
3216 s->kind = kind;
3217 s->parse = parse;
3218
3219 backends = g_slist_append(backends, s);
3220}
3221
Anthony Liguorif69554b2011-08-15 11:17:37 -05003222CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003223 void (*init)(struct CharDriverState *s),
3224 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003225{
Anthony Liguorid654f342013-03-05 23:21:28 +05303226 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003227 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303228 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003229
3230 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003231 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003232 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003233 }
3234
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003235 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003236 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003237 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003238 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003239 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303240 for (i = backends; i; i = i->next) {
3241 cd = i->data;
3242
3243 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003244 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303245 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003246 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303247 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003248 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003249 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303250 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003251 }
3252
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003253 if (!cd->open) {
3254 /* using new, qapi init */
3255 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3256 ChardevReturn *ret = NULL;
3257 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003258 const char *bid = NULL;
3259
3260 if (qemu_opt_get_bool(opts, "mux", 0)) {
3261 bid = g_strdup_printf("%s-base", id);
3262 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003263
3264 chr = NULL;
3265 backend->kind = cd->kind;
3266 if (cd->parse) {
3267 cd->parse(opts, backend, errp);
3268 if (error_is_set(errp)) {
3269 goto qapi_out;
3270 }
3271 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003272 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003273 if (error_is_set(errp)) {
3274 goto qapi_out;
3275 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003276
3277 if (bid) {
3278 qapi_free_ChardevBackend(backend);
3279 qapi_free_ChardevReturn(ret);
3280 backend = g_new0(ChardevBackend, 1);
3281 backend->mux = g_new0(ChardevMux, 1);
3282 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3283 backend->mux->chardev = g_strdup(bid);
3284 ret = qmp_chardev_add(id, backend, errp);
3285 if (error_is_set(errp)) {
3286 goto qapi_out;
3287 }
3288 }
3289
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003290 chr = qemu_chr_find(id);
3291
3292 qapi_out:
3293 qapi_free_ChardevBackend(backend);
3294 qapi_free_ChardevReturn(ret);
3295 return chr;
3296 }
3297
Anthony Liguorid654f342013-03-05 23:21:28 +05303298 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003299 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003300 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003301 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003302 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003303 }
3304
3305 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003306 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003307 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003308 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003309
3310 if (qemu_opt_get_bool(opts, "mux", 0)) {
3311 CharDriverState *base = chr;
3312 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003313 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003314 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3315 chr = qemu_chr_open_mux(base);
3316 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003317 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003318 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003319 } else {
3320 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003321 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003322 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003323 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003324 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003325
3326err:
3327 qemu_opts_del(opts);
3328 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003329}
3330
Anthony Liguori27143a42011-08-15 11:17:36 -05003331CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003332{
3333 const char *p;
3334 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003335 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003336 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003337
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003338 if (strstart(filename, "chardev:", &p)) {
3339 return qemu_chr_find(p);
3340 }
3341
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003342 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003343 if (!opts)
3344 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003345
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003346 chr = qemu_chr_new_from_opts(opts, init, &err);
3347 if (error_is_set(&err)) {
3348 fprintf(stderr, "%s\n", error_get_pretty(err));
3349 error_free(err);
3350 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003351 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003352 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003353 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003354 }
3355 return chr;
3356}
3357
Anthony Liguori15f31512011-08-15 11:17:35 -05003358void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003359{
3360 if (chr->chr_set_echo) {
3361 chr->chr_set_echo(chr, echo);
3362 }
3363}
3364
Hans de Goede8e25daa2013-03-26 11:07:57 +01003365void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003366{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003367 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003368 return;
3369 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003370 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003371 if (chr->chr_set_fe_open) {
3372 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003373 }
3374}
3375
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003376int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3377 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303378{
3379 GSource *src;
3380 guint tag;
3381
3382 if (s->chr_add_watch == NULL) {
3383 return -ENOSYS;
3384 }
3385
3386 src = s->chr_add_watch(s, cond);
3387 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3388 tag = g_source_attach(src, NULL);
3389 g_source_unref(src);
3390
3391 return tag;
3392}
3393
Hans de Goede44c473d2013-03-27 20:29:39 +01003394int qemu_chr_fe_claim(CharDriverState *s)
3395{
3396 if (s->avail_connections < 1) {
3397 return -1;
3398 }
3399 s->avail_connections--;
3400 return 0;
3401}
3402
3403void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3404{
3405 if (qemu_chr_fe_claim(s) != 0) {
3406 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3407 __func__, s->label);
3408 exit(1);
3409 }
3410}
3411
3412void qemu_chr_fe_release(CharDriverState *s)
3413{
3414 s->avail_connections++;
3415}
3416
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003417void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003418{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003419 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003420 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003421 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003422 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003423 g_free(chr->filename);
3424 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003425 if (chr->opts) {
3426 qemu_opts_del(chr->opts);
3427 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003428 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003429}
3430
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003431ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003432{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003433 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003434 CharDriverState *chr;
3435
Blue Swirl72cf2d42009-09-12 07:36:22 +00003436 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003437 ChardevInfoList *info = g_malloc0(sizeof(*info));
3438 info->value = g_malloc0(sizeof(*info->value));
3439 info->value->label = g_strdup(chr->label);
3440 info->value->filename = g_strdup(chr->filename);
3441
3442 info->next = chr_list;
3443 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003444 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003445
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003446 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003447}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003448
3449CharDriverState *qemu_chr_find(const char *name)
3450{
3451 CharDriverState *chr;
3452
Blue Swirl72cf2d42009-09-12 07:36:22 +00003453 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003454 if (strcmp(chr->label, name) != 0)
3455 continue;
3456 return chr;
3457 }
3458 return NULL;
3459}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003460
3461/* Get a character (serial) device interface. */
3462CharDriverState *qemu_char_get_next_serial(void)
3463{
3464 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003465 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003466
3467 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003468
3469 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3470 chr = serial_hds[next_serial++];
3471 qemu_chr_fe_claim_no_fail(chr);
3472 return chr;
3473 }
3474 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003475}
3476
Paolo Bonzini4d454572012-11-26 16:03:42 +01003477QemuOptsList qemu_chardev_opts = {
3478 .name = "chardev",
3479 .implied_opt_name = "backend",
3480 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3481 .desc = {
3482 {
3483 .name = "backend",
3484 .type = QEMU_OPT_STRING,
3485 },{
3486 .name = "path",
3487 .type = QEMU_OPT_STRING,
3488 },{
3489 .name = "host",
3490 .type = QEMU_OPT_STRING,
3491 },{
3492 .name = "port",
3493 .type = QEMU_OPT_STRING,
3494 },{
3495 .name = "localaddr",
3496 .type = QEMU_OPT_STRING,
3497 },{
3498 .name = "localport",
3499 .type = QEMU_OPT_STRING,
3500 },{
3501 .name = "to",
3502 .type = QEMU_OPT_NUMBER,
3503 },{
3504 .name = "ipv4",
3505 .type = QEMU_OPT_BOOL,
3506 },{
3507 .name = "ipv6",
3508 .type = QEMU_OPT_BOOL,
3509 },{
3510 .name = "wait",
3511 .type = QEMU_OPT_BOOL,
3512 },{
3513 .name = "server",
3514 .type = QEMU_OPT_BOOL,
3515 },{
3516 .name = "delay",
3517 .type = QEMU_OPT_BOOL,
3518 },{
3519 .name = "telnet",
3520 .type = QEMU_OPT_BOOL,
3521 },{
3522 .name = "width",
3523 .type = QEMU_OPT_NUMBER,
3524 },{
3525 .name = "height",
3526 .type = QEMU_OPT_NUMBER,
3527 },{
3528 .name = "cols",
3529 .type = QEMU_OPT_NUMBER,
3530 },{
3531 .name = "rows",
3532 .type = QEMU_OPT_NUMBER,
3533 },{
3534 .name = "mux",
3535 .type = QEMU_OPT_BOOL,
3536 },{
3537 .name = "signal",
3538 .type = QEMU_OPT_BOOL,
3539 },{
3540 .name = "name",
3541 .type = QEMU_OPT_STRING,
3542 },{
3543 .name = "debug",
3544 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003545 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003546 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003547 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003548 },
3549 { /* end of list */ }
3550 },
3551};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003552
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003553#ifdef _WIN32
3554
3555static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3556{
3557 HANDLE out;
3558
3559 if (file->in) {
3560 error_setg(errp, "input file not supported");
3561 return NULL;
3562 }
3563
3564 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3565 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3566 if (out == INVALID_HANDLE_VALUE) {
3567 error_setg(errp, "open %s failed", file->out);
3568 return NULL;
3569 }
3570 return qemu_chr_open_win_file(out);
3571}
3572
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003573static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3574 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003575{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003576 return qemu_chr_open_win_path(serial->device);
3577}
3578
3579static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3580 Error **errp)
3581{
3582 error_setg(errp, "character device backend type 'parallel' not supported");
3583 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003584}
3585
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003586#else /* WIN32 */
3587
3588static int qmp_chardev_open_file_source(char *src, int flags,
3589 Error **errp)
3590{
3591 int fd = -1;
3592
3593 TFR(fd = qemu_open(src, flags, 0666));
3594 if (fd == -1) {
3595 error_setg(errp, "open %s: %s", src, strerror(errno));
3596 }
3597 return fd;
3598}
3599
3600static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3601{
3602 int flags, in = -1, out = -1;
3603
3604 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3605 out = qmp_chardev_open_file_source(file->out, flags, errp);
3606 if (error_is_set(errp)) {
3607 return NULL;
3608 }
3609
3610 if (file->in) {
3611 flags = O_RDONLY;
3612 in = qmp_chardev_open_file_source(file->in, flags, errp);
3613 if (error_is_set(errp)) {
3614 qemu_close(out);
3615 return NULL;
3616 }
3617 }
3618
3619 return qemu_chr_open_fd(in, out);
3620}
3621
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003622static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3623 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003624{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003625#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003626 int fd;
3627
3628 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3629 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003630 return NULL;
3631 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003632 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003633 return qemu_chr_open_tty_fd(fd);
3634#else
3635 error_setg(errp, "character device backend type 'serial' not supported");
3636 return NULL;
3637#endif
3638}
3639
3640static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3641 Error **errp)
3642{
3643#ifdef HAVE_CHARDEV_PARPORT
3644 int fd;
3645
3646 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3647 if (error_is_set(errp)) {
3648 return NULL;
3649 }
3650 return qemu_chr_open_pp_fd(fd);
3651#else
3652 error_setg(errp, "character device backend type 'parallel' not supported");
3653 return NULL;
3654#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003655}
3656
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003657#endif /* WIN32 */
3658
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003659static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3660 Error **errp)
3661{
3662 SocketAddress *addr = sock->addr;
3663 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3664 bool is_listen = sock->has_server ? sock->server : true;
3665 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3666 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3667 int fd;
3668
3669 if (is_listen) {
3670 fd = socket_listen(addr, errp);
3671 } else {
3672 fd = socket_connect(addr, errp, NULL, NULL);
3673 }
3674 if (error_is_set(errp)) {
3675 return NULL;
3676 }
3677 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3678 is_telnet, is_waitconnect, errp);
3679}
3680
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003681static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3682 Error **errp)
3683{
3684 int fd;
3685
3686 fd = socket_dgram(dgram->remote, dgram->local, errp);
3687 if (error_is_set(errp)) {
3688 return NULL;
3689 }
3690 return qemu_chr_open_udp_fd(fd);
3691}
3692
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003693ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3694 Error **errp)
3695{
3696 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003697 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003698
3699 chr = qemu_chr_find(id);
3700 if (chr) {
3701 error_setg(errp, "Chardev '%s' already exists", id);
3702 g_free(ret);
3703 return NULL;
3704 }
3705
3706 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003707 case CHARDEV_BACKEND_KIND_FILE:
3708 chr = qmp_chardev_open_file(backend->file, errp);
3709 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003710 case CHARDEV_BACKEND_KIND_SERIAL:
3711 chr = qmp_chardev_open_serial(backend->serial, errp);
3712 break;
3713 case CHARDEV_BACKEND_KIND_PARALLEL:
3714 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003715 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003716 case CHARDEV_BACKEND_KIND_PIPE:
3717 chr = qemu_chr_open_pipe(backend->pipe);
3718 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003719 case CHARDEV_BACKEND_KIND_SOCKET:
3720 chr = qmp_chardev_open_socket(backend->socket, errp);
3721 break;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003722 case CHARDEV_BACKEND_KIND_DGRAM:
3723 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3724 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003725#ifdef HAVE_CHARDEV_TTY
3726 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003727 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003728 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003729#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003730 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003731 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003732 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003733 case CHARDEV_BACKEND_KIND_MUX:
3734 base = qemu_chr_find(backend->mux->chardev);
3735 if (base == NULL) {
3736 error_setg(errp, "mux: base chardev %s not found",
3737 backend->mux->chardev);
3738 break;
3739 }
3740 chr = qemu_chr_open_mux(base);
3741 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003742 case CHARDEV_BACKEND_KIND_MSMOUSE:
3743 chr = qemu_chr_open_msmouse();
3744 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003745#ifdef CONFIG_BRLAPI
3746 case CHARDEV_BACKEND_KIND_BRAILLE:
3747 chr = chr_baum_init();
3748 break;
3749#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003750 case CHARDEV_BACKEND_KIND_STDIO:
3751 chr = qemu_chr_open_stdio(backend->stdio);
3752 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003753#ifdef _WIN32
3754 case CHARDEV_BACKEND_KIND_CONSOLE:
3755 chr = qemu_chr_open_win_con();
3756 break;
3757#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003758#ifdef CONFIG_SPICE
3759 case CHARDEV_BACKEND_KIND_SPICEVMC:
3760 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3761 break;
3762 case CHARDEV_BACKEND_KIND_SPICEPORT:
3763 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3764 break;
3765#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003766 case CHARDEV_BACKEND_KIND_VC:
3767 chr = vc_init(backend->vc);
3768 break;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003769 case CHARDEV_BACKEND_KIND_MEMORY:
3770 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3771 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003772 default:
3773 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3774 break;
3775 }
3776
3777 if (chr == NULL && !error_is_set(errp)) {
3778 error_setg(errp, "Failed to create chardev");
3779 }
3780 if (chr) {
3781 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003782 chr->avail_connections =
3783 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003784 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3785 return ret;
3786 } else {
3787 g_free(ret);
3788 return NULL;
3789 }
3790}
3791
3792void qmp_chardev_remove(const char *id, Error **errp)
3793{
3794 CharDriverState *chr;
3795
3796 chr = qemu_chr_find(id);
3797 if (NULL == chr) {
3798 error_setg(errp, "Chardev '%s' not found", id);
3799 return;
3800 }
3801 if (chr->chr_can_read || chr->chr_read ||
3802 chr->chr_event || chr->handler_opaque) {
3803 error_setg(errp, "Chardev '%s' is busy", id);
3804 return;
3805 }
3806 qemu_chr_delete(chr);
3807}
Anthony Liguorid654f342013-03-05 23:21:28 +05303808
3809static void register_types(void)
3810{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003811 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303812 register_char_driver("socket", qemu_chr_open_socket);
3813 register_char_driver("udp", qemu_chr_open_udp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003814 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3815 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003816 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3817 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003818 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3819 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003820 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3821 qemu_chr_parse_serial);
3822 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3823 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003824 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3825 qemu_chr_parse_parallel);
3826 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3827 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003828 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003829 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003830 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3831 qemu_chr_parse_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303832}
3833
3834type_init(register_types);