blob: 728ed9b4778aaf9c57219399bc169551fa3985da [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 Bonzini1e885b22013-04-08 15:03:15 +0200647 g_source_destroy(iwp->src);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200648 g_source_unref(iwp->src);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200649 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530650}
651
652static GSourceFuncs io_watch_poll_funcs = {
653 .prepare = io_watch_poll_prepare,
654 .check = io_watch_poll_check,
655 .dispatch = io_watch_poll_dispatch,
656 .finalize = io_watch_poll_finalize,
657};
658
659/* Can only be used for read */
660static guint io_add_watch_poll(GIOChannel *channel,
661 IOCanReadHandler *fd_can_read,
662 GIOFunc fd_read,
663 gpointer user_data)
664{
665 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200666 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530667
Paolo Bonzinid185c092013-04-05 17:59:33 +0200668 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530669 iwp->fd_can_read = fd_can_read;
670 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200671 iwp->channel = channel;
672 iwp->fd_read = (GSourceFunc) fd_read;
673 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530674
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200675 tag = g_source_attach(&iwp->parent, NULL);
676 g_source_unref(&iwp->parent);
677 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530678}
679
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000680#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530681static GIOChannel *io_channel_from_fd(int fd)
682{
683 GIOChannel *chan;
684
685 if (fd == -1) {
686 return NULL;
687 }
688
689 chan = g_io_channel_unix_new(fd);
690
691 g_io_channel_set_encoding(chan, NULL, NULL);
692 g_io_channel_set_buffered(chan, FALSE);
693
694 return chan;
695}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000696#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530697
Anthony Liguori76a96442013-03-05 23:21:21 +0530698static GIOChannel *io_channel_from_socket(int fd)
699{
700 GIOChannel *chan;
701
702 if (fd == -1) {
703 return NULL;
704 }
705
706#ifdef _WIN32
707 chan = g_io_channel_win32_new_socket(fd);
708#else
709 chan = g_io_channel_unix_new(fd);
710#endif
711
712 g_io_channel_set_encoding(chan, NULL, NULL);
713 g_io_channel_set_buffered(chan, FALSE);
714
715 return chan;
716}
717
Anthony Liguori684a0962013-03-29 11:39:50 -0500718static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530719{
720 GIOStatus status;
Anthony Liguori684a0962013-03-29 11:39:50 -0500721 size_t offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530722
Anthony Liguori684a0962013-03-29 11:39:50 -0500723 offset = 0;
724 while (offset < len) {
725 gsize bytes_written;
726
727 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530728 &bytes_written, NULL);
729 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530730 if (status == G_IO_STATUS_AGAIN) {
Anthony Liguori684a0962013-03-29 11:39:50 -0500731 /* If we've written any data, return a partial write. */
732 if (offset) {
733 break;
734 }
Anthony Liguori23673ca2013-03-05 23:21:23 +0530735 errno = EAGAIN;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530736 } else {
737 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530738 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500739
740 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530741 } else if (status == G_IO_STATUS_EOF) {
742 break;
Anthony Liguori96c63842013-03-05 23:21:18 +0530743 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500744
745 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530746 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500747
748 return offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530749}
Anthony Liguori96c63842013-03-05 23:21:18 +0530750
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000751#ifndef _WIN32
752
Anthony Liguoria29753f2013-03-05 23:21:19 +0530753typedef struct FDCharDriver {
754 CharDriverState *chr;
755 GIOChannel *fd_in, *fd_out;
756 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000757 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530758 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000759} FDCharDriver;
760
aliguori6f97dba2008-10-31 18:49:55 +0000761static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
762{
763 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530764
Anthony Liguori684a0962013-03-29 11:39:50 -0500765 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530766}
767
768static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
769{
770 CharDriverState *chr = opaque;
771 FDCharDriver *s = chr->opaque;
772 int len;
773 uint8_t buf[READ_BUF_LEN];
774 GIOStatus status;
775 gsize bytes_read;
776
777 len = sizeof(buf);
778 if (len > s->max_size) {
779 len = s->max_size;
780 }
781 if (len == 0) {
782 return FALSE;
783 }
784
785 status = g_io_channel_read_chars(chan, (gchar *)buf,
786 len, &bytes_read, NULL);
787 if (status == G_IO_STATUS_EOF) {
788 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
789 return FALSE;
790 }
791 if (status == G_IO_STATUS_NORMAL) {
792 qemu_chr_be_write(chr, buf, bytes_read);
793 }
794
795 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000796}
797
798static int fd_chr_read_poll(void *opaque)
799{
800 CharDriverState *chr = opaque;
801 FDCharDriver *s = chr->opaque;
802
Anthony Liguori909cda12011-08-15 11:17:31 -0500803 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000804 return s->max_size;
805}
806
Anthony Liguori23673ca2013-03-05 23:21:23 +0530807static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
808{
809 FDCharDriver *s = chr->opaque;
810 return g_io_create_watch(s->fd_out, cond);
811}
812
aliguori6f97dba2008-10-31 18:49:55 +0000813static void fd_chr_update_read_handler(CharDriverState *chr)
814{
815 FDCharDriver *s = chr->opaque;
816
Anthony Liguoria29753f2013-03-05 23:21:19 +0530817 if (s->fd_in_tag) {
818 g_source_remove(s->fd_in_tag);
819 }
820
821 if (s->fd_in) {
822 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 +0000823 }
824}
825
826static void fd_chr_close(struct CharDriverState *chr)
827{
828 FDCharDriver *s = chr->opaque;
829
Anthony Liguoria29753f2013-03-05 23:21:19 +0530830 if (s->fd_in_tag) {
831 g_source_remove(s->fd_in_tag);
832 s->fd_in_tag = 0;
833 }
834
835 if (s->fd_in) {
836 g_io_channel_unref(s->fd_in);
837 }
838 if (s->fd_out) {
839 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000840 }
841
Anthony Liguori7267c092011-08-20 22:09:37 -0500842 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100843 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000844}
845
846/* open a character device to a unix fd */
847static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
848{
849 CharDriverState *chr;
850 FDCharDriver *s;
851
Anthony Liguori7267c092011-08-20 22:09:37 -0500852 chr = g_malloc0(sizeof(CharDriverState));
853 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530854 s->fd_in = io_channel_from_fd(fd_in);
855 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530856 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530857 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000858 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530859 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000860 chr->chr_write = fd_chr_write;
861 chr->chr_update_read_handler = fd_chr_update_read_handler;
862 chr->chr_close = fd_chr_close;
863
Hans de Goedefee204f2013-03-26 11:07:54 +0100864 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000865
866 return chr;
867}
868
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100869static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000870{
871 int fd_in, fd_out;
872 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100873 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200874
875 if (filename == NULL) {
876 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100877 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200878 }
aliguori6f97dba2008-10-31 18:49:55 +0000879
880 snprintf(filename_in, 256, "%s.in", filename);
881 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100882 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
883 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000884 if (fd_in < 0 || fd_out < 0) {
885 if (fd_in >= 0)
886 close(fd_in);
887 if (fd_out >= 0)
888 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100889 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100890 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100891 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100892 }
aliguori6f97dba2008-10-31 18:49:55 +0000893 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100894 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000895}
896
aliguori6f97dba2008-10-31 18:49:55 +0000897/* init terminal so that we can grab keys */
898static struct termios oldtty;
899static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100900static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000901
902static void term_exit(void)
903{
904 tcsetattr (0, TCSANOW, &oldtty);
905 fcntl(0, F_SETFL, old_fd0_flags);
906}
907
Paolo Bonzinibb002512010-12-23 13:42:50 +0100908static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000909{
910 struct termios tty;
911
Paolo Bonzini03693642010-12-23 13:42:49 +0100912 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100913 if (!echo) {
914 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000915 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100916 tty.c_oflag |= OPOST;
917 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
918 tty.c_cflag &= ~(CSIZE|PARENB);
919 tty.c_cflag |= CS8;
920 tty.c_cc[VMIN] = 1;
921 tty.c_cc[VTIME] = 0;
922 }
aliguori6f97dba2008-10-31 18:49:55 +0000923 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100924 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000925 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000926
927 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000928}
929
930static void qemu_chr_close_stdio(struct CharDriverState *chr)
931{
932 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000933 fd_chr_close(chr);
934}
935
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100936static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000937{
938 CharDriverState *chr;
939
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400940 if (is_daemonized()) {
941 error_report("cannot use stdio with -daemonize");
942 return NULL;
943 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530944 old_fd0_flags = fcntl(0, F_GETFL);
945 tcgetattr (0, &oldtty);
946 fcntl(0, F_SETFL, O_NONBLOCK);
947 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100948
aliguori6f97dba2008-10-31 18:49:55 +0000949 chr = qemu_chr_open_fd(0, 1);
950 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100951 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100952 stdio_allow_signal = display_type != DT_NOGRAPHIC;
953 if (opts->has_signal) {
954 stdio_allow_signal = opts->signal;
955 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500956 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000957
Markus Armbruster1f514702012-02-07 15:09:08 +0100958 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000959}
960
961#ifdef __sun__
962/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000963static int openpty(int *amaster, int *aslave, char *name,
964 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000965{
966 const char *slave;
967 int mfd = -1, sfd = -1;
968
969 *amaster = *aslave = -1;
970
971 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
972 if (mfd < 0)
973 goto err;
974
975 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
976 goto err;
977
978 if ((slave = ptsname(mfd)) == NULL)
979 goto err;
980
981 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
982 goto err;
983
984 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
985 (termp != NULL && tcgetattr(sfd, termp) < 0))
986 goto err;
987
988 if (amaster)
989 *amaster = mfd;
990 if (aslave)
991 *aslave = sfd;
992 if (winp)
993 ioctl(sfd, TIOCSWINSZ, winp);
994
995 return 0;
996
997err:
998 if (sfd != -1)
999 close(sfd);
1000 close(mfd);
1001 return -1;
1002}
1003
blueswir13f4cb3d2009-04-13 16:31:01 +00001004static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +00001005{
1006 termios_p->c_iflag &=
1007 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
1008 termios_p->c_oflag &= ~OPOST;
1009 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1010 termios_p->c_cflag &= ~(CSIZE|PARENB);
1011 termios_p->c_cflag |= CS8;
1012
1013 termios_p->c_cc[VMIN] = 0;
1014 termios_p->c_cc[VTIME] = 0;
1015}
1016#endif
1017
1018#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001019 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1020 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001021
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001022#define HAVE_CHARDEV_TTY 1
1023
aliguori6f97dba2008-10-31 18:49:55 +00001024typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301025 GIOChannel *fd;
1026 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001027 int connected;
1028 int polling;
1029 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301030 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001031} PtyCharDriver;
1032
1033static void pty_chr_update_read_handler(CharDriverState *chr);
1034static void pty_chr_state(CharDriverState *chr, int connected);
1035
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301036static gboolean pty_chr_timer(gpointer opaque)
1037{
1038 struct CharDriverState *chr = opaque;
1039 PtyCharDriver *s = chr->opaque;
1040
1041 if (s->connected) {
1042 goto out;
1043 }
1044 if (s->polling) {
1045 /* If we arrive here without polling being cleared due
1046 * read returning -EIO, then we are (re-)connected */
1047 pty_chr_state(chr, 1);
1048 goto out;
1049 }
1050
1051 /* Next poll ... */
1052 pty_chr_update_read_handler(chr);
1053
1054out:
1055 return FALSE;
1056}
1057
1058static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1059{
1060 PtyCharDriver *s = chr->opaque;
1061
1062 if (s->timer_tag) {
1063 g_source_remove(s->timer_tag);
1064 s->timer_tag = 0;
1065 }
1066
1067 if (ms == 1000) {
1068 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1069 } else {
1070 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1071 }
1072}
1073
aliguori6f97dba2008-10-31 18:49:55 +00001074static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1075{
1076 PtyCharDriver *s = chr->opaque;
1077
1078 if (!s->connected) {
1079 /* guest sends data, check for (re-)connect */
1080 pty_chr_update_read_handler(chr);
1081 return 0;
1082 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001083 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001084}
1085
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301086static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1087{
1088 PtyCharDriver *s = chr->opaque;
1089 return g_io_create_watch(s->fd, cond);
1090}
1091
aliguori6f97dba2008-10-31 18:49:55 +00001092static int pty_chr_read_poll(void *opaque)
1093{
1094 CharDriverState *chr = opaque;
1095 PtyCharDriver *s = chr->opaque;
1096
Anthony Liguori909cda12011-08-15 11:17:31 -05001097 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001098 return s->read_bytes;
1099}
1100
Anthony Liguori093d3a22013-03-05 23:21:20 +05301101static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001102{
1103 CharDriverState *chr = opaque;
1104 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301105 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301106 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301107 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001108
1109 len = sizeof(buf);
1110 if (len > s->read_bytes)
1111 len = s->read_bytes;
1112 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301113 return FALSE;
1114 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1115 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001116 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301117 return FALSE;
1118 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001119 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001120 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001121 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301122 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001123}
1124
1125static void pty_chr_update_read_handler(CharDriverState *chr)
1126{
1127 PtyCharDriver *s = chr->opaque;
1128
Anthony Liguori093d3a22013-03-05 23:21:20 +05301129 if (s->fd_tag) {
1130 g_source_remove(s->fd_tag);
1131 }
1132
1133 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001134 s->polling = 1;
1135 /*
1136 * Short timeout here: just need wait long enougth that qemu makes
1137 * it through the poll loop once. When reconnected we want a
1138 * short timeout so we notice it almost instantly. Otherwise
1139 * read() gives us -EIO instantly, making pty_chr_state() reset the
1140 * timeout to the normal (much longer) poll interval before the
1141 * timer triggers.
1142 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301143 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001144}
1145
1146static void pty_chr_state(CharDriverState *chr, int connected)
1147{
1148 PtyCharDriver *s = chr->opaque;
1149
1150 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301151 g_source_remove(s->fd_tag);
1152 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001153 s->connected = 0;
1154 s->polling = 0;
1155 /* (re-)connect poll interval for idle guests: once per second.
1156 * We check more frequently in case the guests sends data to
1157 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301158 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001159 } else {
1160 if (!s->connected)
Hans de Goedefee204f2013-03-26 11:07:54 +01001161 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001162 s->connected = 1;
1163 }
1164}
1165
aliguori6f97dba2008-10-31 18:49:55 +00001166
1167static void pty_chr_close(struct CharDriverState *chr)
1168{
1169 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301170 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001171
Anthony Liguori093d3a22013-03-05 23:21:20 +05301172 if (s->fd_tag) {
1173 g_source_remove(s->fd_tag);
1174 }
1175 fd = g_io_channel_unix_get_fd(s->fd);
1176 g_io_channel_unref(s->fd);
1177 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301178 if (s->timer_tag) {
1179 g_source_remove(s->timer_tag);
1180 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001181 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001182 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001183}
1184
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001185static CharDriverState *qemu_chr_open_pty(const char *id,
1186 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001187{
1188 CharDriverState *chr;
1189 PtyCharDriver *s;
1190 struct termios tty;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001191 int master_fd, slave_fd;
blueswir1c5e97232009-03-07 20:06:23 +00001192#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001193 char pty_name[PATH_MAX];
1194#define q_ptsname(x) pty_name
1195#else
1196 char *pty_name = NULL;
1197#define q_ptsname(x) ptsname(x)
1198#endif
1199
Markus Armbrustera4e26042011-11-11 10:40:05 +01001200 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001201 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001202 }
1203
1204 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001205 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001206 cfmakeraw(&tty);
1207 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1208 close(slave_fd);
1209
Markus Armbrustera4e26042011-11-11 10:40:05 +01001210 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001211
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001212 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1213 ret->pty = g_strdup(q_ptsname(master_fd));
1214 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001215
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001216 fprintf(stderr, "char device redirected to %s (label %s)\n",
1217 q_ptsname(master_fd), id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001218
1219 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001220 chr->opaque = s;
1221 chr->chr_write = pty_chr_write;
1222 chr->chr_update_read_handler = pty_chr_update_read_handler;
1223 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301224 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001225
Anthony Liguori093d3a22013-03-05 23:21:20 +05301226 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301227 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001228
Markus Armbruster1f514702012-02-07 15:09:08 +01001229 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001230}
1231
1232static void tty_serial_init(int fd, int speed,
1233 int parity, int data_bits, int stop_bits)
1234{
1235 struct termios tty;
1236 speed_t spd;
1237
1238#if 0
1239 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1240 speed, parity, data_bits, stop_bits);
1241#endif
1242 tcgetattr (fd, &tty);
1243
Stefan Weil45eea132009-10-26 16:10:10 +01001244#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1245 speed = speed * 10 / 11;
1246 do {
1247 check_speed(50);
1248 check_speed(75);
1249 check_speed(110);
1250 check_speed(134);
1251 check_speed(150);
1252 check_speed(200);
1253 check_speed(300);
1254 check_speed(600);
1255 check_speed(1200);
1256 check_speed(1800);
1257 check_speed(2400);
1258 check_speed(4800);
1259 check_speed(9600);
1260 check_speed(19200);
1261 check_speed(38400);
1262 /* Non-Posix values follow. They may be unsupported on some systems. */
1263 check_speed(57600);
1264 check_speed(115200);
1265#ifdef B230400
1266 check_speed(230400);
1267#endif
1268#ifdef B460800
1269 check_speed(460800);
1270#endif
1271#ifdef B500000
1272 check_speed(500000);
1273#endif
1274#ifdef B576000
1275 check_speed(576000);
1276#endif
1277#ifdef B921600
1278 check_speed(921600);
1279#endif
1280#ifdef B1000000
1281 check_speed(1000000);
1282#endif
1283#ifdef B1152000
1284 check_speed(1152000);
1285#endif
1286#ifdef B1500000
1287 check_speed(1500000);
1288#endif
1289#ifdef B2000000
1290 check_speed(2000000);
1291#endif
1292#ifdef B2500000
1293 check_speed(2500000);
1294#endif
1295#ifdef B3000000
1296 check_speed(3000000);
1297#endif
1298#ifdef B3500000
1299 check_speed(3500000);
1300#endif
1301#ifdef B4000000
1302 check_speed(4000000);
1303#endif
aliguori6f97dba2008-10-31 18:49:55 +00001304 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001305 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001306
1307 cfsetispeed(&tty, spd);
1308 cfsetospeed(&tty, spd);
1309
1310 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1311 |INLCR|IGNCR|ICRNL|IXON);
1312 tty.c_oflag |= OPOST;
1313 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1314 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1315 switch(data_bits) {
1316 default:
1317 case 8:
1318 tty.c_cflag |= CS8;
1319 break;
1320 case 7:
1321 tty.c_cflag |= CS7;
1322 break;
1323 case 6:
1324 tty.c_cflag |= CS6;
1325 break;
1326 case 5:
1327 tty.c_cflag |= CS5;
1328 break;
1329 }
1330 switch(parity) {
1331 default:
1332 case 'N':
1333 break;
1334 case 'E':
1335 tty.c_cflag |= PARENB;
1336 break;
1337 case 'O':
1338 tty.c_cflag |= PARENB | PARODD;
1339 break;
1340 }
1341 if (stop_bits == 2)
1342 tty.c_cflag |= CSTOPB;
1343
1344 tcsetattr (fd, TCSANOW, &tty);
1345}
1346
1347static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1348{
1349 FDCharDriver *s = chr->opaque;
1350
1351 switch(cmd) {
1352 case CHR_IOCTL_SERIAL_SET_PARAMS:
1353 {
1354 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301355 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1356 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001357 ssp->data_bits, ssp->stop_bits);
1358 }
1359 break;
1360 case CHR_IOCTL_SERIAL_SET_BREAK:
1361 {
1362 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301363 if (enable) {
1364 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1365 }
aliguori6f97dba2008-10-31 18:49:55 +00001366 }
1367 break;
1368 case CHR_IOCTL_SERIAL_GET_TIOCM:
1369 {
1370 int sarg = 0;
1371 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301372 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001373 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001374 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001375 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001376 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001377 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001378 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001379 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001380 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001381 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001382 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001383 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001384 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001385 *targ |= CHR_TIOCM_RTS;
1386 }
1387 break;
1388 case CHR_IOCTL_SERIAL_SET_TIOCM:
1389 {
1390 int sarg = *(int *)arg;
1391 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301392 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001393 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1394 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1395 if (sarg & CHR_TIOCM_CTS)
1396 targ |= TIOCM_CTS;
1397 if (sarg & CHR_TIOCM_CAR)
1398 targ |= TIOCM_CAR;
1399 if (sarg & CHR_TIOCM_DSR)
1400 targ |= TIOCM_DSR;
1401 if (sarg & CHR_TIOCM_RI)
1402 targ |= TIOCM_RI;
1403 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001404 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001405 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001406 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301407 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001408 }
1409 break;
1410 default:
1411 return -ENOTSUP;
1412 }
1413 return 0;
1414}
1415
David Ahern4266a132010-02-10 18:27:17 -07001416static void qemu_chr_close_tty(CharDriverState *chr)
1417{
1418 FDCharDriver *s = chr->opaque;
1419 int fd = -1;
1420
1421 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301422 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001423 }
1424
1425 fd_chr_close(chr);
1426
1427 if (fd >= 0) {
1428 close(fd);
1429 }
1430}
1431
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001432static CharDriverState *qemu_chr_open_tty_fd(int fd)
1433{
1434 CharDriverState *chr;
1435
1436 tty_serial_init(fd, 115200, 'N', 8, 1);
1437 chr = qemu_chr_open_fd(fd, fd);
1438 chr->chr_ioctl = tty_serial_ioctl;
1439 chr->chr_close = qemu_chr_close_tty;
1440 return chr;
1441}
aliguori6f97dba2008-10-31 18:49:55 +00001442#endif /* __linux__ || __sun__ */
1443
1444#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001445
1446#define HAVE_CHARDEV_PARPORT 1
1447
aliguori6f97dba2008-10-31 18:49:55 +00001448typedef struct {
1449 int fd;
1450 int mode;
1451} ParallelCharDriver;
1452
1453static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1454{
1455 if (s->mode != mode) {
1456 int m = mode;
1457 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1458 return 0;
1459 s->mode = mode;
1460 }
1461 return 1;
1462}
1463
1464static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1465{
1466 ParallelCharDriver *drv = chr->opaque;
1467 int fd = drv->fd;
1468 uint8_t b;
1469
1470 switch(cmd) {
1471 case CHR_IOCTL_PP_READ_DATA:
1472 if (ioctl(fd, PPRDATA, &b) < 0)
1473 return -ENOTSUP;
1474 *(uint8_t *)arg = b;
1475 break;
1476 case CHR_IOCTL_PP_WRITE_DATA:
1477 b = *(uint8_t *)arg;
1478 if (ioctl(fd, PPWDATA, &b) < 0)
1479 return -ENOTSUP;
1480 break;
1481 case CHR_IOCTL_PP_READ_CONTROL:
1482 if (ioctl(fd, PPRCONTROL, &b) < 0)
1483 return -ENOTSUP;
1484 /* Linux gives only the lowest bits, and no way to know data
1485 direction! For better compatibility set the fixed upper
1486 bits. */
1487 *(uint8_t *)arg = b | 0xc0;
1488 break;
1489 case CHR_IOCTL_PP_WRITE_CONTROL:
1490 b = *(uint8_t *)arg;
1491 if (ioctl(fd, PPWCONTROL, &b) < 0)
1492 return -ENOTSUP;
1493 break;
1494 case CHR_IOCTL_PP_READ_STATUS:
1495 if (ioctl(fd, PPRSTATUS, &b) < 0)
1496 return -ENOTSUP;
1497 *(uint8_t *)arg = b;
1498 break;
1499 case CHR_IOCTL_PP_DATA_DIR:
1500 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1501 return -ENOTSUP;
1502 break;
1503 case CHR_IOCTL_PP_EPP_READ_ADDR:
1504 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1505 struct ParallelIOArg *parg = arg;
1506 int n = read(fd, parg->buffer, parg->count);
1507 if (n != parg->count) {
1508 return -EIO;
1509 }
1510 }
1511 break;
1512 case CHR_IOCTL_PP_EPP_READ:
1513 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1514 struct ParallelIOArg *parg = arg;
1515 int n = read(fd, parg->buffer, parg->count);
1516 if (n != parg->count) {
1517 return -EIO;
1518 }
1519 }
1520 break;
1521 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1522 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1523 struct ParallelIOArg *parg = arg;
1524 int n = write(fd, parg->buffer, parg->count);
1525 if (n != parg->count) {
1526 return -EIO;
1527 }
1528 }
1529 break;
1530 case CHR_IOCTL_PP_EPP_WRITE:
1531 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1532 struct ParallelIOArg *parg = arg;
1533 int n = write(fd, parg->buffer, parg->count);
1534 if (n != parg->count) {
1535 return -EIO;
1536 }
1537 }
1538 break;
1539 default:
1540 return -ENOTSUP;
1541 }
1542 return 0;
1543}
1544
1545static void pp_close(CharDriverState *chr)
1546{
1547 ParallelCharDriver *drv = chr->opaque;
1548 int fd = drv->fd;
1549
1550 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1551 ioctl(fd, PPRELEASE);
1552 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001553 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001554 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001555}
1556
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001557static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001558{
1559 CharDriverState *chr;
1560 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001561
1562 if (ioctl(fd, PPCLAIM) < 0) {
1563 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001564 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001565 }
1566
Anthony Liguori7267c092011-08-20 22:09:37 -05001567 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001568 drv->fd = fd;
1569 drv->mode = IEEE1284_MODE_COMPAT;
1570
Anthony Liguori7267c092011-08-20 22:09:37 -05001571 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001572 chr->chr_write = null_chr_write;
1573 chr->chr_ioctl = pp_ioctl;
1574 chr->chr_close = pp_close;
1575 chr->opaque = drv;
1576
Hans de Goedefee204f2013-03-26 11:07:54 +01001577 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001578
Markus Armbruster1f514702012-02-07 15:09:08 +01001579 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001580}
1581#endif /* __linux__ */
1582
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001583#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001584
1585#define HAVE_CHARDEV_PARPORT 1
1586
blueswir16972f932008-11-22 20:49:12 +00001587static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1588{
Stefan Weile0efb992011-02-23 19:09:16 +01001589 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001590 uint8_t b;
1591
1592 switch(cmd) {
1593 case CHR_IOCTL_PP_READ_DATA:
1594 if (ioctl(fd, PPIGDATA, &b) < 0)
1595 return -ENOTSUP;
1596 *(uint8_t *)arg = b;
1597 break;
1598 case CHR_IOCTL_PP_WRITE_DATA:
1599 b = *(uint8_t *)arg;
1600 if (ioctl(fd, PPISDATA, &b) < 0)
1601 return -ENOTSUP;
1602 break;
1603 case CHR_IOCTL_PP_READ_CONTROL:
1604 if (ioctl(fd, PPIGCTRL, &b) < 0)
1605 return -ENOTSUP;
1606 *(uint8_t *)arg = b;
1607 break;
1608 case CHR_IOCTL_PP_WRITE_CONTROL:
1609 b = *(uint8_t *)arg;
1610 if (ioctl(fd, PPISCTRL, &b) < 0)
1611 return -ENOTSUP;
1612 break;
1613 case CHR_IOCTL_PP_READ_STATUS:
1614 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1615 return -ENOTSUP;
1616 *(uint8_t *)arg = b;
1617 break;
1618 default:
1619 return -ENOTSUP;
1620 }
1621 return 0;
1622}
1623
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001624static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001625{
1626 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001627
Anthony Liguori7267c092011-08-20 22:09:37 -05001628 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001629 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001630 chr->chr_write = null_chr_write;
1631 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001632 return chr;
blueswir16972f932008-11-22 20:49:12 +00001633}
1634#endif
1635
aliguori6f97dba2008-10-31 18:49:55 +00001636#else /* _WIN32 */
1637
1638typedef struct {
1639 int max_size;
1640 HANDLE hcom, hrecv, hsend;
1641 OVERLAPPED orecv, osend;
1642 BOOL fpipe;
1643 DWORD len;
1644} WinCharState;
1645
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001646typedef struct {
1647 HANDLE hStdIn;
1648 HANDLE hInputReadyEvent;
1649 HANDLE hInputDoneEvent;
1650 HANDLE hInputThread;
1651 uint8_t win_stdio_buf;
1652} WinStdioCharState;
1653
aliguori6f97dba2008-10-31 18:49:55 +00001654#define NSENDBUF 2048
1655#define NRECVBUF 2048
1656#define MAXCONNECT 1
1657#define NTIMEOUT 5000
1658
1659static int win_chr_poll(void *opaque);
1660static int win_chr_pipe_poll(void *opaque);
1661
1662static void win_chr_close(CharDriverState *chr)
1663{
1664 WinCharState *s = chr->opaque;
1665
1666 if (s->hsend) {
1667 CloseHandle(s->hsend);
1668 s->hsend = NULL;
1669 }
1670 if (s->hrecv) {
1671 CloseHandle(s->hrecv);
1672 s->hrecv = NULL;
1673 }
1674 if (s->hcom) {
1675 CloseHandle(s->hcom);
1676 s->hcom = NULL;
1677 }
1678 if (s->fpipe)
1679 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1680 else
1681 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301682
Hans de Goedea425d232011-11-19 10:22:43 +01001683 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001684}
1685
1686static int win_chr_init(CharDriverState *chr, const char *filename)
1687{
1688 WinCharState *s = chr->opaque;
1689 COMMCONFIG comcfg;
1690 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1691 COMSTAT comstat;
1692 DWORD size;
1693 DWORD err;
1694
1695 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1696 if (!s->hsend) {
1697 fprintf(stderr, "Failed CreateEvent\n");
1698 goto fail;
1699 }
1700 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1701 if (!s->hrecv) {
1702 fprintf(stderr, "Failed CreateEvent\n");
1703 goto fail;
1704 }
1705
1706 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1707 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1708 if (s->hcom == INVALID_HANDLE_VALUE) {
1709 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1710 s->hcom = NULL;
1711 goto fail;
1712 }
1713
1714 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1715 fprintf(stderr, "Failed SetupComm\n");
1716 goto fail;
1717 }
1718
1719 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1720 size = sizeof(COMMCONFIG);
1721 GetDefaultCommConfig(filename, &comcfg, &size);
1722 comcfg.dcb.DCBlength = sizeof(DCB);
1723 CommConfigDialog(filename, NULL, &comcfg);
1724
1725 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1726 fprintf(stderr, "Failed SetCommState\n");
1727 goto fail;
1728 }
1729
1730 if (!SetCommMask(s->hcom, EV_ERR)) {
1731 fprintf(stderr, "Failed SetCommMask\n");
1732 goto fail;
1733 }
1734
1735 cto.ReadIntervalTimeout = MAXDWORD;
1736 if (!SetCommTimeouts(s->hcom, &cto)) {
1737 fprintf(stderr, "Failed SetCommTimeouts\n");
1738 goto fail;
1739 }
1740
1741 if (!ClearCommError(s->hcom, &err, &comstat)) {
1742 fprintf(stderr, "Failed ClearCommError\n");
1743 goto fail;
1744 }
1745 qemu_add_polling_cb(win_chr_poll, chr);
1746 return 0;
1747
1748 fail:
1749 win_chr_close(chr);
1750 return -1;
1751}
1752
1753static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1754{
1755 WinCharState *s = chr->opaque;
1756 DWORD len, ret, size, err;
1757
1758 len = len1;
1759 ZeroMemory(&s->osend, sizeof(s->osend));
1760 s->osend.hEvent = s->hsend;
1761 while (len > 0) {
1762 if (s->hsend)
1763 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1764 else
1765 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1766 if (!ret) {
1767 err = GetLastError();
1768 if (err == ERROR_IO_PENDING) {
1769 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1770 if (ret) {
1771 buf += size;
1772 len -= size;
1773 } else {
1774 break;
1775 }
1776 } else {
1777 break;
1778 }
1779 } else {
1780 buf += size;
1781 len -= size;
1782 }
1783 }
1784 return len1 - len;
1785}
1786
1787static int win_chr_read_poll(CharDriverState *chr)
1788{
1789 WinCharState *s = chr->opaque;
1790
Anthony Liguori909cda12011-08-15 11:17:31 -05001791 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001792 return s->max_size;
1793}
1794
1795static void win_chr_readfile(CharDriverState *chr)
1796{
1797 WinCharState *s = chr->opaque;
1798 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301799 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001800 DWORD size;
1801
1802 ZeroMemory(&s->orecv, sizeof(s->orecv));
1803 s->orecv.hEvent = s->hrecv;
1804 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1805 if (!ret) {
1806 err = GetLastError();
1807 if (err == ERROR_IO_PENDING) {
1808 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1809 }
1810 }
1811
1812 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001813 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001814 }
1815}
1816
1817static void win_chr_read(CharDriverState *chr)
1818{
1819 WinCharState *s = chr->opaque;
1820
1821 if (s->len > s->max_size)
1822 s->len = s->max_size;
1823 if (s->len == 0)
1824 return;
1825
1826 win_chr_readfile(chr);
1827}
1828
1829static int win_chr_poll(void *opaque)
1830{
1831 CharDriverState *chr = opaque;
1832 WinCharState *s = chr->opaque;
1833 COMSTAT status;
1834 DWORD comerr;
1835
1836 ClearCommError(s->hcom, &comerr, &status);
1837 if (status.cbInQue > 0) {
1838 s->len = status.cbInQue;
1839 win_chr_read_poll(chr);
1840 win_chr_read(chr);
1841 return 1;
1842 }
1843 return 0;
1844}
1845
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001846static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001847{
1848 CharDriverState *chr;
1849 WinCharState *s;
1850
Anthony Liguori7267c092011-08-20 22:09:37 -05001851 chr = g_malloc0(sizeof(CharDriverState));
1852 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001853 chr->opaque = s;
1854 chr->chr_write = win_chr_write;
1855 chr->chr_close = win_chr_close;
1856
1857 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001858 g_free(s);
1859 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001860 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001861 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001862 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001863 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001864}
1865
1866static int win_chr_pipe_poll(void *opaque)
1867{
1868 CharDriverState *chr = opaque;
1869 WinCharState *s = chr->opaque;
1870 DWORD size;
1871
1872 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1873 if (size > 0) {
1874 s->len = size;
1875 win_chr_read_poll(chr);
1876 win_chr_read(chr);
1877 return 1;
1878 }
1879 return 0;
1880}
1881
1882static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1883{
1884 WinCharState *s = chr->opaque;
1885 OVERLAPPED ov;
1886 int ret;
1887 DWORD size;
1888 char openname[256];
1889
1890 s->fpipe = TRUE;
1891
1892 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1893 if (!s->hsend) {
1894 fprintf(stderr, "Failed CreateEvent\n");
1895 goto fail;
1896 }
1897 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1898 if (!s->hrecv) {
1899 fprintf(stderr, "Failed CreateEvent\n");
1900 goto fail;
1901 }
1902
1903 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1904 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1905 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1906 PIPE_WAIT,
1907 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1908 if (s->hcom == INVALID_HANDLE_VALUE) {
1909 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1910 s->hcom = NULL;
1911 goto fail;
1912 }
1913
1914 ZeroMemory(&ov, sizeof(ov));
1915 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1916 ret = ConnectNamedPipe(s->hcom, &ov);
1917 if (ret) {
1918 fprintf(stderr, "Failed ConnectNamedPipe\n");
1919 goto fail;
1920 }
1921
1922 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1923 if (!ret) {
1924 fprintf(stderr, "Failed GetOverlappedResult\n");
1925 if (ov.hEvent) {
1926 CloseHandle(ov.hEvent);
1927 ov.hEvent = NULL;
1928 }
1929 goto fail;
1930 }
1931
1932 if (ov.hEvent) {
1933 CloseHandle(ov.hEvent);
1934 ov.hEvent = NULL;
1935 }
1936 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1937 return 0;
1938
1939 fail:
1940 win_chr_close(chr);
1941 return -1;
1942}
1943
1944
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001945static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001946{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001947 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001948 CharDriverState *chr;
1949 WinCharState *s;
1950
Anthony Liguori7267c092011-08-20 22:09:37 -05001951 chr = g_malloc0(sizeof(CharDriverState));
1952 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001953 chr->opaque = s;
1954 chr->chr_write = win_chr_write;
1955 chr->chr_close = win_chr_close;
1956
1957 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001958 g_free(s);
1959 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001960 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001961 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001962 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001963 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001964}
1965
Markus Armbruster1f514702012-02-07 15:09:08 +01001966static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001967{
1968 CharDriverState *chr;
1969 WinCharState *s;
1970
Anthony Liguori7267c092011-08-20 22:09:37 -05001971 chr = g_malloc0(sizeof(CharDriverState));
1972 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001973 s->hcom = fd_out;
1974 chr->opaque = s;
1975 chr->chr_write = win_chr_write;
Hans de Goedefee204f2013-03-26 11:07:54 +01001976 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001977 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001978}
1979
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001980static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001981{
Markus Armbruster1f514702012-02-07 15:09:08 +01001982 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001983}
1984
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001985static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1986{
1987 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1988 DWORD dwSize;
1989 int len1;
1990
1991 len1 = len;
1992
1993 while (len1 > 0) {
1994 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1995 break;
1996 }
1997 buf += dwSize;
1998 len1 -= dwSize;
1999 }
2000
2001 return len - len1;
2002}
2003
2004static void win_stdio_wait_func(void *opaque)
2005{
2006 CharDriverState *chr = opaque;
2007 WinStdioCharState *stdio = chr->opaque;
2008 INPUT_RECORD buf[4];
2009 int ret;
2010 DWORD dwSize;
2011 int i;
2012
2013 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2014 &dwSize);
2015
2016 if (!ret) {
2017 /* Avoid error storm */
2018 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2019 return;
2020 }
2021
2022 for (i = 0; i < dwSize; i++) {
2023 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2024
2025 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2026 int j;
2027 if (kev->uChar.AsciiChar != 0) {
2028 for (j = 0; j < kev->wRepeatCount; j++) {
2029 if (qemu_chr_be_can_write(chr)) {
2030 uint8_t c = kev->uChar.AsciiChar;
2031 qemu_chr_be_write(chr, &c, 1);
2032 }
2033 }
2034 }
2035 }
2036 }
2037}
2038
2039static DWORD WINAPI win_stdio_thread(LPVOID param)
2040{
2041 CharDriverState *chr = param;
2042 WinStdioCharState *stdio = chr->opaque;
2043 int ret;
2044 DWORD dwSize;
2045
2046 while (1) {
2047
2048 /* Wait for one byte */
2049 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2050
2051 /* Exit in case of error, continue if nothing read */
2052 if (!ret) {
2053 break;
2054 }
2055 if (!dwSize) {
2056 continue;
2057 }
2058
2059 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2060 if (stdio->win_stdio_buf == '\r') {
2061 continue;
2062 }
2063
2064 /* Signal the main thread and wait until the byte was eaten */
2065 if (!SetEvent(stdio->hInputReadyEvent)) {
2066 break;
2067 }
2068 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2069 != WAIT_OBJECT_0) {
2070 break;
2071 }
2072 }
2073
2074 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2075 return 0;
2076}
2077
2078static void win_stdio_thread_wait_func(void *opaque)
2079{
2080 CharDriverState *chr = opaque;
2081 WinStdioCharState *stdio = chr->opaque;
2082
2083 if (qemu_chr_be_can_write(chr)) {
2084 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2085 }
2086
2087 SetEvent(stdio->hInputDoneEvent);
2088}
2089
2090static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2091{
2092 WinStdioCharState *stdio = chr->opaque;
2093 DWORD dwMode = 0;
2094
2095 GetConsoleMode(stdio->hStdIn, &dwMode);
2096
2097 if (echo) {
2098 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2099 } else {
2100 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2101 }
2102}
2103
2104static void win_stdio_close(CharDriverState *chr)
2105{
2106 WinStdioCharState *stdio = chr->opaque;
2107
2108 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2109 CloseHandle(stdio->hInputReadyEvent);
2110 }
2111 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2112 CloseHandle(stdio->hInputDoneEvent);
2113 }
2114 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2115 TerminateThread(stdio->hInputThread, 0);
2116 }
2117
2118 g_free(chr->opaque);
2119 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002120}
2121
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002122static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002123{
2124 CharDriverState *chr;
2125 WinStdioCharState *stdio;
2126 DWORD dwMode;
2127 int is_console = 0;
2128
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002129 chr = g_malloc0(sizeof(CharDriverState));
2130 stdio = g_malloc0(sizeof(WinStdioCharState));
2131
2132 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2133 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2134 fprintf(stderr, "cannot open stdio: invalid handle\n");
2135 exit(1);
2136 }
2137
2138 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2139
2140 chr->opaque = stdio;
2141 chr->chr_write = win_stdio_write;
2142 chr->chr_close = win_stdio_close;
2143
Anthony Liguoried7a1542013-03-05 23:21:17 +05302144 if (is_console) {
2145 if (qemu_add_wait_object(stdio->hStdIn,
2146 win_stdio_wait_func, chr)) {
2147 fprintf(stderr, "qemu_add_wait_object: failed\n");
2148 }
2149 } else {
2150 DWORD dwId;
2151
2152 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2153 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2154 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2155 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002156
Anthony Liguoried7a1542013-03-05 23:21:17 +05302157 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2158 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2159 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2160 fprintf(stderr, "cannot create stdio thread or event\n");
2161 exit(1);
2162 }
2163 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2164 win_stdio_thread_wait_func, chr)) {
2165 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002166 }
2167 }
2168
2169 dwMode |= ENABLE_LINE_INPUT;
2170
Anthony Liguoried7a1542013-03-05 23:21:17 +05302171 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002172 /* set the terminal in raw mode */
2173 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2174 dwMode |= ENABLE_PROCESSED_INPUT;
2175 }
2176
2177 SetConsoleMode(stdio->hStdIn, dwMode);
2178
2179 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2180 qemu_chr_fe_set_echo(chr, false);
2181
Markus Armbruster1f514702012-02-07 15:09:08 +01002182 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002183}
aliguori6f97dba2008-10-31 18:49:55 +00002184#endif /* !_WIN32 */
2185
Anthony Liguori5ab82112013-03-05 23:21:31 +05302186
aliguori6f97dba2008-10-31 18:49:55 +00002187/***********************************************************/
2188/* UDP Net console */
2189
2190typedef struct {
2191 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302192 GIOChannel *chan;
2193 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302194 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002195 int bufcnt;
2196 int bufptr;
2197 int max_size;
2198} NetCharDriver;
2199
2200static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2201{
2202 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302203 gsize bytes_written;
2204 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002205
Anthony Liguori76a96442013-03-05 23:21:21 +05302206 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2207 if (status == G_IO_STATUS_EOF) {
2208 return 0;
2209 } else if (status != G_IO_STATUS_NORMAL) {
2210 return -1;
2211 }
2212
2213 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002214}
2215
2216static int udp_chr_read_poll(void *opaque)
2217{
2218 CharDriverState *chr = opaque;
2219 NetCharDriver *s = chr->opaque;
2220
Anthony Liguori909cda12011-08-15 11:17:31 -05002221 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002222
2223 /* If there were any stray characters in the queue process them
2224 * first
2225 */
2226 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002227 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002228 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002229 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002230 }
2231 return s->max_size;
2232}
2233
Anthony Liguori76a96442013-03-05 23:21:21 +05302234static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002235{
2236 CharDriverState *chr = opaque;
2237 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302238 gsize bytes_read = 0;
2239 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002240
2241 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302242 return FALSE;
2243 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2244 &bytes_read, NULL);
2245 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002246 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302247 if (status != G_IO_STATUS_NORMAL) {
2248 return FALSE;
2249 }
aliguori6f97dba2008-10-31 18:49:55 +00002250
2251 s->bufptr = 0;
2252 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002253 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002254 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002255 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002256 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302257
2258 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002259}
2260
2261static void udp_chr_update_read_handler(CharDriverState *chr)
2262{
2263 NetCharDriver *s = chr->opaque;
2264
Anthony Liguori76a96442013-03-05 23:21:21 +05302265 if (s->tag) {
2266 g_source_remove(s->tag);
2267 s->tag = 0;
2268 }
2269
2270 if (s->chan) {
2271 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002272 }
2273}
2274
aliguori819f56b2009-03-28 17:58:14 +00002275static void udp_chr_close(CharDriverState *chr)
2276{
2277 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302278 if (s->tag) {
2279 g_source_remove(s->tag);
2280 }
2281 if (s->chan) {
2282 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002283 closesocket(s->fd);
2284 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002285 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002286 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002287}
2288
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002289static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002290{
2291 CharDriverState *chr = NULL;
2292 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002293
Anthony Liguori7267c092011-08-20 22:09:37 -05002294 chr = g_malloc0(sizeof(CharDriverState));
2295 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002296
aliguori6f97dba2008-10-31 18:49:55 +00002297 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302298 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002299 s->bufcnt = 0;
2300 s->bufptr = 0;
2301 chr->opaque = s;
2302 chr->chr_write = udp_chr_write;
2303 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002304 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002305 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002306}
aliguori6f97dba2008-10-31 18:49:55 +00002307
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002308static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2309{
2310 Error *local_err = NULL;
2311 int fd = -1;
2312
2313 fd = inet_dgram_opts(opts, &local_err);
2314 if (fd < 0) {
2315 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002316 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002317 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002318}
2319
2320/***********************************************************/
2321/* TCP Net console */
2322
2323typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302324
2325 GIOChannel *chan, *listen_chan;
2326 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002327 int fd, listen_fd;
2328 int connected;
2329 int max_size;
2330 int do_telnetopt;
2331 int do_nodelay;
2332 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002333 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002334} TCPCharDriver;
2335
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302336static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002337
2338static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2339{
2340 TCPCharDriver *s = chr->opaque;
2341 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002342 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002343 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002344 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002345 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002346 }
2347}
2348
2349static int tcp_chr_read_poll(void *opaque)
2350{
2351 CharDriverState *chr = opaque;
2352 TCPCharDriver *s = chr->opaque;
2353 if (!s->connected)
2354 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002355 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002356 return s->max_size;
2357}
2358
2359#define IAC 255
2360#define IAC_BREAK 243
2361static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2362 TCPCharDriver *s,
2363 uint8_t *buf, int *size)
2364{
2365 /* Handle any telnet client's basic IAC options to satisfy char by
2366 * char mode with no echo. All IAC options will be removed from
2367 * the buf and the do_telnetopt variable will be used to track the
2368 * state of the width of the IAC information.
2369 *
2370 * IAC commands come in sets of 3 bytes with the exception of the
2371 * "IAC BREAK" command and the double IAC.
2372 */
2373
2374 int i;
2375 int j = 0;
2376
2377 for (i = 0; i < *size; i++) {
2378 if (s->do_telnetopt > 1) {
2379 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2380 /* Double IAC means send an IAC */
2381 if (j != i)
2382 buf[j] = buf[i];
2383 j++;
2384 s->do_telnetopt = 1;
2385 } else {
2386 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2387 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002388 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002389 s->do_telnetopt++;
2390 }
2391 s->do_telnetopt++;
2392 }
2393 if (s->do_telnetopt >= 4) {
2394 s->do_telnetopt = 1;
2395 }
2396 } else {
2397 if ((unsigned char)buf[i] == IAC) {
2398 s->do_telnetopt = 2;
2399 } else {
2400 if (j != i)
2401 buf[j] = buf[i];
2402 j++;
2403 }
2404 }
2405 }
2406 *size = j;
2407}
2408
Mark McLoughlin7d174052009-07-22 09:11:39 +01002409static int tcp_get_msgfd(CharDriverState *chr)
2410{
2411 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002412 int fd = s->msgfd;
2413 s->msgfd = -1;
2414 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002415}
2416
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002417#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002418static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2419{
2420 TCPCharDriver *s = chr->opaque;
2421 struct cmsghdr *cmsg;
2422
2423 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2424 int fd;
2425
2426 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2427 cmsg->cmsg_level != SOL_SOCKET ||
2428 cmsg->cmsg_type != SCM_RIGHTS)
2429 continue;
2430
2431 fd = *((int *)CMSG_DATA(cmsg));
2432 if (fd < 0)
2433 continue;
2434
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002435 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2436 qemu_set_block(fd);
2437
Corey Bryant06138652012-08-14 16:43:42 -04002438#ifndef MSG_CMSG_CLOEXEC
2439 qemu_set_cloexec(fd);
2440#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002441 if (s->msgfd != -1)
2442 close(s->msgfd);
2443 s->msgfd = fd;
2444 }
2445}
2446
Mark McLoughlin9977c892009-07-22 09:11:38 +01002447static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2448{
2449 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002450 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002451 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002452 union {
2453 struct cmsghdr cmsg;
2454 char control[CMSG_SPACE(sizeof(int))];
2455 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002456 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002457 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002458
2459 iov[0].iov_base = buf;
2460 iov[0].iov_len = len;
2461
2462 msg.msg_iov = iov;
2463 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002464 msg.msg_control = &msg_control;
2465 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002466
Corey Bryant06138652012-08-14 16:43:42 -04002467#ifdef MSG_CMSG_CLOEXEC
2468 flags |= MSG_CMSG_CLOEXEC;
2469#endif
2470 ret = recvmsg(s->fd, &msg, flags);
2471 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002472 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002473 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002474
2475 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002476}
2477#else
2478static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2479{
2480 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002481 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002482}
2483#endif
2484
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302485static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2486{
2487 TCPCharDriver *s = chr->opaque;
2488 return g_io_create_watch(s->chan, cond);
2489}
2490
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302491static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002492{
2493 CharDriverState *chr = opaque;
2494 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302495 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002496 int len, size;
2497
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302498 if (!s->connected || s->max_size <= 0) {
2499 return FALSE;
2500 }
aliguori6f97dba2008-10-31 18:49:55 +00002501 len = sizeof(buf);
2502 if (len > s->max_size)
2503 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002504 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002505 if (size == 0) {
2506 /* connection closed */
2507 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302508 if (s->listen_chan) {
2509 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002510 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302511 g_source_remove(s->tag);
2512 s->tag = 0;
2513 g_io_channel_unref(s->chan);
2514 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002515 closesocket(s->fd);
2516 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002517 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002518 } else if (size > 0) {
2519 if (s->do_telnetopt)
2520 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2521 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002522 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002523 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302524
2525 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002526}
2527
Blue Swirl68c18d12010-08-15 09:46:24 +00002528#ifndef _WIN32
2529CharDriverState *qemu_chr_open_eventfd(int eventfd)
2530{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002531 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002532}
Blue Swirl68c18d12010-08-15 09:46:24 +00002533#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002534
aliguori6f97dba2008-10-31 18:49:55 +00002535static void tcp_chr_connect(void *opaque)
2536{
2537 CharDriverState *chr = opaque;
2538 TCPCharDriver *s = chr->opaque;
2539
2540 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302541 if (s->chan) {
2542 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002543 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002544 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002545}
2546
2547#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2548static void tcp_chr_telnet_init(int fd)
2549{
2550 char buf[3];
2551 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2552 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2553 send(fd, (char *)buf, 3, 0);
2554 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2555 send(fd, (char *)buf, 3, 0);
2556 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2557 send(fd, (char *)buf, 3, 0);
2558 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2559 send(fd, (char *)buf, 3, 0);
2560}
2561
Daniel P. Berrange13661082011-06-23 13:31:42 +01002562static int tcp_chr_add_client(CharDriverState *chr, int fd)
2563{
2564 TCPCharDriver *s = chr->opaque;
2565 if (s->fd != -1)
2566 return -1;
2567
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002568 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002569 if (s->do_nodelay)
2570 socket_set_nodelay(fd);
2571 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302572 s->chan = io_channel_from_socket(fd);
2573 g_source_remove(s->listen_tag);
2574 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002575 tcp_chr_connect(chr);
2576
2577 return 0;
2578}
2579
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302580static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002581{
2582 CharDriverState *chr = opaque;
2583 TCPCharDriver *s = chr->opaque;
2584 struct sockaddr_in saddr;
2585#ifndef _WIN32
2586 struct sockaddr_un uaddr;
2587#endif
2588 struct sockaddr *addr;
2589 socklen_t len;
2590 int fd;
2591
2592 for(;;) {
2593#ifndef _WIN32
2594 if (s->is_unix) {
2595 len = sizeof(uaddr);
2596 addr = (struct sockaddr *)&uaddr;
2597 } else
2598#endif
2599 {
2600 len = sizeof(saddr);
2601 addr = (struct sockaddr *)&saddr;
2602 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002603 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002604 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302605 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002606 } else if (fd >= 0) {
2607 if (s->do_telnetopt)
2608 tcp_chr_telnet_init(fd);
2609 break;
2610 }
2611 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002612 if (tcp_chr_add_client(chr, fd) < 0)
2613 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302614
2615 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002616}
2617
2618static void tcp_chr_close(CharDriverState *chr)
2619{
2620 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002621 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302622 if (s->tag) {
2623 g_source_remove(s->tag);
2624 }
2625 if (s->chan) {
2626 g_io_channel_unref(s->chan);
2627 }
aliguori6f97dba2008-10-31 18:49:55 +00002628 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002629 }
2630 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302631 if (s->listen_tag) {
2632 g_source_remove(s->listen_tag);
2633 }
2634 if (s->listen_chan) {
2635 g_io_channel_unref(s->listen_chan);
2636 }
aliguori6f97dba2008-10-31 18:49:55 +00002637 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002638 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002639 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002640 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002641}
2642
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002643static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2644 bool is_listen, bool is_telnet,
2645 bool is_waitconnect,
2646 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002647{
2648 CharDriverState *chr = NULL;
2649 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002650 char host[NI_MAXHOST], serv[NI_MAXSERV];
2651 const char *left = "", *right = "";
2652 struct sockaddr_storage ss;
2653 socklen_t ss_len = sizeof(ss);
2654
2655 memset(&ss, 0, ss_len);
2656 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2657 error_setg(errp, "getsockname: %s", strerror(errno));
2658 return NULL;
2659 }
2660
2661 chr = g_malloc0(sizeof(CharDriverState));
2662 s = g_malloc0(sizeof(TCPCharDriver));
2663
2664 s->connected = 0;
2665 s->fd = -1;
2666 s->listen_fd = -1;
2667 s->msgfd = -1;
2668
2669 chr->filename = g_malloc(256);
2670 switch (ss.ss_family) {
2671#ifndef _WIN32
2672 case AF_UNIX:
2673 s->is_unix = 1;
2674 snprintf(chr->filename, 256, "unix:%s%s",
2675 ((struct sockaddr_un *)(&ss))->sun_path,
2676 is_listen ? ",server" : "");
2677 break;
2678#endif
2679 case AF_INET6:
2680 left = "[";
2681 right = "]";
2682 /* fall through */
2683 case AF_INET:
2684 s->do_nodelay = do_nodelay;
2685 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2686 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002687 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002688 is_telnet ? "telnet" : "tcp",
2689 left, host, right, serv,
2690 is_listen ? ",server" : "");
2691 break;
2692 }
2693
2694 chr->opaque = s;
2695 chr->chr_write = tcp_chr_write;
2696 chr->chr_close = tcp_chr_close;
2697 chr->get_msgfd = tcp_get_msgfd;
2698 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302699 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002700
2701 if (is_listen) {
2702 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302703 s->listen_chan = io_channel_from_socket(s->listen_fd);
2704 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002705 if (is_telnet) {
2706 s->do_telnetopt = 1;
2707 }
2708 } else {
2709 s->connected = 1;
2710 s->fd = fd;
2711 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302712 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002713 tcp_chr_connect(chr);
2714 }
2715
2716 if (is_listen && is_waitconnect) {
2717 printf("QEMU waiting for connection on: %s\n",
2718 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302719 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002720 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002721 }
2722 return chr;
2723}
2724
2725static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2726{
2727 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002728 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002729 int fd = -1;
2730 int is_listen;
2731 int is_waitconnect;
2732 int do_nodelay;
2733 int is_unix;
2734 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002735
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002736 is_listen = qemu_opt_get_bool(opts, "server", 0);
2737 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2738 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2739 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2740 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002741 if (!is_listen)
2742 is_waitconnect = 0;
2743
aliguorif07b6002008-11-11 20:54:09 +00002744 if (is_unix) {
2745 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002746 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002747 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002748 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002749 }
2750 } else {
2751 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002752 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002753 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002754 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002755 }
2756 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002757 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002758 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002759 }
aliguori6f97dba2008-10-31 18:49:55 +00002760
2761 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002762 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002763
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002764 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2765 is_waitconnect, &local_err);
2766 if (error_is_set(&local_err)) {
2767 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002768 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002769 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002770
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002771
aliguori6f97dba2008-10-31 18:49:55 +00002772 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002773 if (local_err) {
2774 qerror_report_err(local_err);
2775 error_free(local_err);
2776 }
2777 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002778 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002779 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002780 if (chr) {
2781 g_free(chr->opaque);
2782 g_free(chr);
2783 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002784 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002785}
2786
Lei Li51767e72013-01-25 00:03:19 +08002787/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002788/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002789
2790typedef struct {
2791 size_t size;
2792 size_t prod;
2793 size_t cons;
2794 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002795} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002796
Markus Armbruster3949e592013-02-06 21:27:24 +01002797static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002798{
Markus Armbruster3949e592013-02-06 21:27:24 +01002799 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002800
Markus Armbruster5c230102013-02-06 21:27:23 +01002801 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002802}
2803
Markus Armbruster3949e592013-02-06 21:27:24 +01002804static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002805{
Markus Armbruster3949e592013-02-06 21:27:24 +01002806 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002807 int i;
2808
2809 if (!buf || (len < 0)) {
2810 return -1;
2811 }
2812
2813 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002814 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2815 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002816 d->cons = d->prod - d->size;
2817 }
2818 }
2819
2820 return 0;
2821}
2822
Markus Armbruster3949e592013-02-06 21:27:24 +01002823static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002824{
Markus Armbruster3949e592013-02-06 21:27:24 +01002825 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002826 int i;
2827
Markus Armbruster5c230102013-02-06 21:27:23 +01002828 for (i = 0; i < len && d->cons != d->prod; i++) {
2829 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002830 }
2831
2832 return i;
2833}
2834
Markus Armbruster3949e592013-02-06 21:27:24 +01002835static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002836{
Markus Armbruster3949e592013-02-06 21:27:24 +01002837 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002838
2839 g_free(d->cbuf);
2840 g_free(d);
2841 chr->opaque = NULL;
2842}
2843
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002844static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2845 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002846{
2847 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002848 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002849
2850 chr = g_malloc0(sizeof(CharDriverState));
2851 d = g_malloc(sizeof(*d));
2852
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002853 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002854
2855 /* The size must be power of 2 */
2856 if (d->size & (d->size - 1)) {
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002857 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002858 goto fail;
2859 }
2860
2861 d->prod = 0;
2862 d->cons = 0;
2863 d->cbuf = g_malloc0(d->size);
2864
2865 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002866 chr->chr_write = ringbuf_chr_write;
2867 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002868
2869 return chr;
2870
2871fail:
2872 g_free(d);
2873 g_free(chr);
2874 return NULL;
2875}
2876
Markus Armbruster3949e592013-02-06 21:27:24 +01002877static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002878{
Markus Armbruster3949e592013-02-06 21:27:24 +01002879 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002880}
2881
Markus Armbruster3949e592013-02-06 21:27:24 +01002882void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002883 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002884 Error **errp)
2885{
2886 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002887 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002888 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002889 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002890
2891 chr = qemu_chr_find(device);
2892 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002893 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002894 return;
2895 }
2896
Markus Armbruster3949e592013-02-06 21:27:24 +01002897 if (!chr_is_ringbuf(chr)) {
2898 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002899 return;
2900 }
2901
Lei Li1f590cf2013-01-25 00:03:20 +08002902 if (has_format && (format == DATA_FORMAT_BASE64)) {
2903 write_data = g_base64_decode(data, &write_count);
2904 } else {
2905 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002906 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002907 }
2908
Markus Armbruster3949e592013-02-06 21:27:24 +01002909 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002910
Markus Armbruster13289fb2013-02-06 21:27:18 +01002911 if (write_data != (uint8_t *)data) {
2912 g_free((void *)write_data);
2913 }
2914
Lei Li1f590cf2013-01-25 00:03:20 +08002915 if (ret < 0) {
2916 error_setg(errp, "Failed to write to device %s", device);
2917 return;
2918 }
2919}
2920
Markus Armbruster3949e592013-02-06 21:27:24 +01002921char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002922 bool has_format, enum DataFormat format,
2923 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002924{
2925 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002926 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002927 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002928 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002929
2930 chr = qemu_chr_find(device);
2931 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002932 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002933 return NULL;
2934 }
2935
Markus Armbruster3949e592013-02-06 21:27:24 +01002936 if (!chr_is_ringbuf(chr)) {
2937 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002938 return NULL;
2939 }
2940
2941 if (size <= 0) {
2942 error_setg(errp, "size must be greater than zero");
2943 return NULL;
2944 }
2945
Markus Armbruster3949e592013-02-06 21:27:24 +01002946 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002947 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002948 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002949
Markus Armbruster3949e592013-02-06 21:27:24 +01002950 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002951
2952 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002953 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002954 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002955 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002956 /*
2957 * FIXME should read only complete, valid UTF-8 characters up
2958 * to @size bytes. Invalid sequences should be replaced by a
2959 * suitable replacement character. Except when (and only
2960 * when) ring buffer lost characters since last read, initial
2961 * continuation characters should be dropped.
2962 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002963 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002964 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002965 }
2966
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002967 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002968}
2969
Gerd Hoffmann33521632009-12-08 13:11:49 +01002970QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002971{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002972 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002973 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002974 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002975 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002976 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002977
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002978 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2979 if (error_is_set(&local_err)) {
2980 qerror_report_err(local_err);
2981 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002982 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002983 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002984
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002985 if (strstart(filename, "mon:", &p)) {
2986 filename = p;
2987 qemu_opt_set(opts, "mux", "on");
2988 }
2989
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002990 if (strcmp(filename, "null") == 0 ||
2991 strcmp(filename, "pty") == 0 ||
2992 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002993 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002994 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002995 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002996 return opts;
2997 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002998 if (strstart(filename, "vc", &p)) {
2999 qemu_opt_set(opts, "backend", "vc");
3000 if (*p == ':') {
3001 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3002 /* pixels */
3003 qemu_opt_set(opts, "width", width);
3004 qemu_opt_set(opts, "height", height);
3005 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3006 /* chars */
3007 qemu_opt_set(opts, "cols", width);
3008 qemu_opt_set(opts, "rows", height);
3009 } else {
3010 goto fail;
3011 }
3012 }
3013 return opts;
3014 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003015 if (strcmp(filename, "con:") == 0) {
3016 qemu_opt_set(opts, "backend", "console");
3017 return opts;
3018 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003019 if (strstart(filename, "COM", NULL)) {
3020 qemu_opt_set(opts, "backend", "serial");
3021 qemu_opt_set(opts, "path", filename);
3022 return opts;
3023 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003024 if (strstart(filename, "file:", &p)) {
3025 qemu_opt_set(opts, "backend", "file");
3026 qemu_opt_set(opts, "path", p);
3027 return opts;
3028 }
3029 if (strstart(filename, "pipe:", &p)) {
3030 qemu_opt_set(opts, "backend", "pipe");
3031 qemu_opt_set(opts, "path", p);
3032 return opts;
3033 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003034 if (strstart(filename, "tcp:", &p) ||
3035 strstart(filename, "telnet:", &p)) {
3036 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3037 host[0] = 0;
3038 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3039 goto fail;
3040 }
3041 qemu_opt_set(opts, "backend", "socket");
3042 qemu_opt_set(opts, "host", host);
3043 qemu_opt_set(opts, "port", port);
3044 if (p[pos] == ',') {
3045 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3046 goto fail;
3047 }
3048 if (strstart(filename, "telnet:", &p))
3049 qemu_opt_set(opts, "telnet", "on");
3050 return opts;
3051 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003052 if (strstart(filename, "udp:", &p)) {
3053 qemu_opt_set(opts, "backend", "udp");
3054 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3055 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003056 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003057 goto fail;
3058 }
3059 }
3060 qemu_opt_set(opts, "host", host);
3061 qemu_opt_set(opts, "port", port);
3062 if (p[pos] == '@') {
3063 p += pos + 1;
3064 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3065 host[0] = 0;
3066 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003067 goto fail;
3068 }
3069 }
3070 qemu_opt_set(opts, "localaddr", host);
3071 qemu_opt_set(opts, "localport", port);
3072 }
3073 return opts;
3074 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003075 if (strstart(filename, "unix:", &p)) {
3076 qemu_opt_set(opts, "backend", "socket");
3077 if (qemu_opts_do_parse(opts, p, "path") != 0)
3078 goto fail;
3079 return opts;
3080 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003081 if (strstart(filename, "/dev/parport", NULL) ||
3082 strstart(filename, "/dev/ppi", NULL)) {
3083 qemu_opt_set(opts, "backend", "parport");
3084 qemu_opt_set(opts, "path", filename);
3085 return opts;
3086 }
3087 if (strstart(filename, "/dev/", NULL)) {
3088 qemu_opt_set(opts, "backend", "tty");
3089 qemu_opt_set(opts, "path", filename);
3090 return opts;
3091 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003092
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003093fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003094 qemu_opts_del(opts);
3095 return NULL;
3096}
3097
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003098static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3099 Error **errp)
3100{
3101 const char *path = qemu_opt_get(opts, "path");
3102
3103 if (path == NULL) {
3104 error_setg(errp, "chardev: file: no filename given");
3105 return;
3106 }
3107 backend->file = g_new0(ChardevFile, 1);
3108 backend->file->out = g_strdup(path);
3109}
3110
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003111static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3112 Error **errp)
3113{
3114 backend->stdio = g_new0(ChardevStdio, 1);
3115 backend->stdio->has_signal = true;
3116 backend->stdio->signal =
3117 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3118}
3119
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003120static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3121 Error **errp)
3122{
3123 const char *device = qemu_opt_get(opts, "path");
3124
3125 if (device == NULL) {
3126 error_setg(errp, "chardev: serial/tty: no device path given");
3127 return;
3128 }
3129 backend->serial = g_new0(ChardevHostdev, 1);
3130 backend->serial->device = g_strdup(device);
3131}
3132
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003133static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3134 Error **errp)
3135{
3136 const char *device = qemu_opt_get(opts, "path");
3137
3138 if (device == NULL) {
3139 error_setg(errp, "chardev: parallel: no device path given");
3140 return;
3141 }
3142 backend->parallel = g_new0(ChardevHostdev, 1);
3143 backend->parallel->device = g_strdup(device);
3144}
3145
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003146static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3147 Error **errp)
3148{
3149 const char *device = qemu_opt_get(opts, "path");
3150
3151 if (device == NULL) {
3152 error_setg(errp, "chardev: pipe: no device path given");
3153 return;
3154 }
3155 backend->pipe = g_new0(ChardevHostdev, 1);
3156 backend->pipe->device = g_strdup(device);
3157}
3158
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003159static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3160 Error **errp)
3161{
3162 int val;
3163
3164 backend->memory = g_new0(ChardevRingbuf, 1);
3165
3166 val = qemu_opt_get_number(opts, "size", 0);
3167 if (val != 0) {
3168 backend->memory->has_size = true;
3169 backend->memory->size = val;
3170 }
3171}
3172
Anthony Liguorid654f342013-03-05 23:21:28 +05303173typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003174 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003175 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003176 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003177 /* new, qapi-based */
3178 int kind;
3179 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303180} CharDriver;
3181
3182static GSList *backends;
3183
3184void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3185{
3186 CharDriver *s;
3187
3188 s = g_malloc0(sizeof(*s));
3189 s->name = g_strdup(name);
3190 s->open = open;
3191
3192 backends = g_slist_append(backends, s);
3193}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003194
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003195void register_char_driver_qapi(const char *name, int kind,
3196 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3197{
3198 CharDriver *s;
3199
3200 s = g_malloc0(sizeof(*s));
3201 s->name = g_strdup(name);
3202 s->kind = kind;
3203 s->parse = parse;
3204
3205 backends = g_slist_append(backends, s);
3206}
3207
Anthony Liguorif69554b2011-08-15 11:17:37 -05003208CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003209 void (*init)(struct CharDriverState *s),
3210 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003211{
Anthony Liguorid654f342013-03-05 23:21:28 +05303212 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003213 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303214 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003215
3216 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003217 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003218 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003219 }
3220
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003221 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003222 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003223 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003224 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003225 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303226 for (i = backends; i; i = i->next) {
3227 cd = i->data;
3228
3229 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303231 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003232 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303233 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003234 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003235 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303236 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003237 }
3238
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003239 if (!cd->open) {
3240 /* using new, qapi init */
3241 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3242 ChardevReturn *ret = NULL;
3243 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003244 const char *bid = NULL;
3245
3246 if (qemu_opt_get_bool(opts, "mux", 0)) {
3247 bid = g_strdup_printf("%s-base", id);
3248 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003249
3250 chr = NULL;
3251 backend->kind = cd->kind;
3252 if (cd->parse) {
3253 cd->parse(opts, backend, errp);
3254 if (error_is_set(errp)) {
3255 goto qapi_out;
3256 }
3257 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003258 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003259 if (error_is_set(errp)) {
3260 goto qapi_out;
3261 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003262
3263 if (bid) {
3264 qapi_free_ChardevBackend(backend);
3265 qapi_free_ChardevReturn(ret);
3266 backend = g_new0(ChardevBackend, 1);
3267 backend->mux = g_new0(ChardevMux, 1);
3268 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3269 backend->mux->chardev = g_strdup(bid);
3270 ret = qmp_chardev_add(id, backend, errp);
3271 if (error_is_set(errp)) {
3272 goto qapi_out;
3273 }
3274 }
3275
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003276 chr = qemu_chr_find(id);
3277
3278 qapi_out:
3279 qapi_free_ChardevBackend(backend);
3280 qapi_free_ChardevReturn(ret);
3281 return chr;
3282 }
3283
Anthony Liguorid654f342013-03-05 23:21:28 +05303284 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003285 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003286 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003287 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003288 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003289 }
3290
3291 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003292 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003293 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003294 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003295
3296 if (qemu_opt_get_bool(opts, "mux", 0)) {
3297 CharDriverState *base = chr;
3298 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003299 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003300 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3301 chr = qemu_chr_open_mux(base);
3302 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003303 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003304 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003305 } else {
3306 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003307 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003308 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003309 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003310 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003311
3312err:
3313 qemu_opts_del(opts);
3314 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003315}
3316
Anthony Liguori27143a42011-08-15 11:17:36 -05003317CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003318{
3319 const char *p;
3320 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003321 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003322 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003323
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003324 if (strstart(filename, "chardev:", &p)) {
3325 return qemu_chr_find(p);
3326 }
3327
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003328 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003329 if (!opts)
3330 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003331
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003332 chr = qemu_chr_new_from_opts(opts, init, &err);
3333 if (error_is_set(&err)) {
3334 fprintf(stderr, "%s\n", error_get_pretty(err));
3335 error_free(err);
3336 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003337 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003338 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003339 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003340 }
3341 return chr;
3342}
3343
Anthony Liguori15f31512011-08-15 11:17:35 -05003344void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003345{
3346 if (chr->chr_set_echo) {
3347 chr->chr_set_echo(chr, echo);
3348 }
3349}
3350
Hans de Goede8e25daa2013-03-26 11:07:57 +01003351void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003352{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003353 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003354 return;
3355 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003356 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003357 if (chr->chr_set_fe_open) {
3358 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003359 }
3360}
3361
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003362int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3363 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303364{
3365 GSource *src;
3366 guint tag;
3367
3368 if (s->chr_add_watch == NULL) {
3369 return -ENOSYS;
3370 }
3371
3372 src = s->chr_add_watch(s, cond);
3373 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3374 tag = g_source_attach(src, NULL);
3375 g_source_unref(src);
3376
3377 return tag;
3378}
3379
Hans de Goede44c473d2013-03-27 20:29:39 +01003380int qemu_chr_fe_claim(CharDriverState *s)
3381{
3382 if (s->avail_connections < 1) {
3383 return -1;
3384 }
3385 s->avail_connections--;
3386 return 0;
3387}
3388
3389void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3390{
3391 if (qemu_chr_fe_claim(s) != 0) {
3392 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3393 __func__, s->label);
3394 exit(1);
3395 }
3396}
3397
3398void qemu_chr_fe_release(CharDriverState *s)
3399{
3400 s->avail_connections++;
3401}
3402
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003403void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003404{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003405 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003406 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003407 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003408 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003409 g_free(chr->filename);
3410 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003411 if (chr->opts) {
3412 qemu_opts_del(chr->opts);
3413 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003414 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003415}
3416
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003417ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003418{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003419 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003420 CharDriverState *chr;
3421
Blue Swirl72cf2d42009-09-12 07:36:22 +00003422 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003423 ChardevInfoList *info = g_malloc0(sizeof(*info));
3424 info->value = g_malloc0(sizeof(*info->value));
3425 info->value->label = g_strdup(chr->label);
3426 info->value->filename = g_strdup(chr->filename);
3427
3428 info->next = chr_list;
3429 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003430 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003431
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003432 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003433}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003434
3435CharDriverState *qemu_chr_find(const char *name)
3436{
3437 CharDriverState *chr;
3438
Blue Swirl72cf2d42009-09-12 07:36:22 +00003439 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003440 if (strcmp(chr->label, name) != 0)
3441 continue;
3442 return chr;
3443 }
3444 return NULL;
3445}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003446
3447/* Get a character (serial) device interface. */
3448CharDriverState *qemu_char_get_next_serial(void)
3449{
3450 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003451 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003452
3453 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003454
3455 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3456 chr = serial_hds[next_serial++];
3457 qemu_chr_fe_claim_no_fail(chr);
3458 return chr;
3459 }
3460 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003461}
3462
Paolo Bonzini4d454572012-11-26 16:03:42 +01003463QemuOptsList qemu_chardev_opts = {
3464 .name = "chardev",
3465 .implied_opt_name = "backend",
3466 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3467 .desc = {
3468 {
3469 .name = "backend",
3470 .type = QEMU_OPT_STRING,
3471 },{
3472 .name = "path",
3473 .type = QEMU_OPT_STRING,
3474 },{
3475 .name = "host",
3476 .type = QEMU_OPT_STRING,
3477 },{
3478 .name = "port",
3479 .type = QEMU_OPT_STRING,
3480 },{
3481 .name = "localaddr",
3482 .type = QEMU_OPT_STRING,
3483 },{
3484 .name = "localport",
3485 .type = QEMU_OPT_STRING,
3486 },{
3487 .name = "to",
3488 .type = QEMU_OPT_NUMBER,
3489 },{
3490 .name = "ipv4",
3491 .type = QEMU_OPT_BOOL,
3492 },{
3493 .name = "ipv6",
3494 .type = QEMU_OPT_BOOL,
3495 },{
3496 .name = "wait",
3497 .type = QEMU_OPT_BOOL,
3498 },{
3499 .name = "server",
3500 .type = QEMU_OPT_BOOL,
3501 },{
3502 .name = "delay",
3503 .type = QEMU_OPT_BOOL,
3504 },{
3505 .name = "telnet",
3506 .type = QEMU_OPT_BOOL,
3507 },{
3508 .name = "width",
3509 .type = QEMU_OPT_NUMBER,
3510 },{
3511 .name = "height",
3512 .type = QEMU_OPT_NUMBER,
3513 },{
3514 .name = "cols",
3515 .type = QEMU_OPT_NUMBER,
3516 },{
3517 .name = "rows",
3518 .type = QEMU_OPT_NUMBER,
3519 },{
3520 .name = "mux",
3521 .type = QEMU_OPT_BOOL,
3522 },{
3523 .name = "signal",
3524 .type = QEMU_OPT_BOOL,
3525 },{
3526 .name = "name",
3527 .type = QEMU_OPT_STRING,
3528 },{
3529 .name = "debug",
3530 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003531 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003532 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003533 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003534 },
3535 { /* end of list */ }
3536 },
3537};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003538
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003539#ifdef _WIN32
3540
3541static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3542{
3543 HANDLE out;
3544
3545 if (file->in) {
3546 error_setg(errp, "input file not supported");
3547 return NULL;
3548 }
3549
3550 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3551 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3552 if (out == INVALID_HANDLE_VALUE) {
3553 error_setg(errp, "open %s failed", file->out);
3554 return NULL;
3555 }
3556 return qemu_chr_open_win_file(out);
3557}
3558
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003559static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3560 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003561{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003562 return qemu_chr_open_win_path(serial->device);
3563}
3564
3565static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3566 Error **errp)
3567{
3568 error_setg(errp, "character device backend type 'parallel' not supported");
3569 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003570}
3571
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003572#else /* WIN32 */
3573
3574static int qmp_chardev_open_file_source(char *src, int flags,
3575 Error **errp)
3576{
3577 int fd = -1;
3578
3579 TFR(fd = qemu_open(src, flags, 0666));
3580 if (fd == -1) {
3581 error_setg(errp, "open %s: %s", src, strerror(errno));
3582 }
3583 return fd;
3584}
3585
3586static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3587{
3588 int flags, in = -1, out = -1;
3589
3590 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3591 out = qmp_chardev_open_file_source(file->out, flags, errp);
3592 if (error_is_set(errp)) {
3593 return NULL;
3594 }
3595
3596 if (file->in) {
3597 flags = O_RDONLY;
3598 in = qmp_chardev_open_file_source(file->in, flags, errp);
3599 if (error_is_set(errp)) {
3600 qemu_close(out);
3601 return NULL;
3602 }
3603 }
3604
3605 return qemu_chr_open_fd(in, out);
3606}
3607
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003608static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3609 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003610{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003611#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003612 int fd;
3613
3614 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3615 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003616 return NULL;
3617 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003618 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003619 return qemu_chr_open_tty_fd(fd);
3620#else
3621 error_setg(errp, "character device backend type 'serial' not supported");
3622 return NULL;
3623#endif
3624}
3625
3626static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3627 Error **errp)
3628{
3629#ifdef HAVE_CHARDEV_PARPORT
3630 int fd;
3631
3632 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3633 if (error_is_set(errp)) {
3634 return NULL;
3635 }
3636 return qemu_chr_open_pp_fd(fd);
3637#else
3638 error_setg(errp, "character device backend type 'parallel' not supported");
3639 return NULL;
3640#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003641}
3642
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003643#endif /* WIN32 */
3644
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003645static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3646 Error **errp)
3647{
3648 SocketAddress *addr = sock->addr;
3649 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3650 bool is_listen = sock->has_server ? sock->server : true;
3651 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3652 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3653 int fd;
3654
3655 if (is_listen) {
3656 fd = socket_listen(addr, errp);
3657 } else {
3658 fd = socket_connect(addr, errp, NULL, NULL);
3659 }
3660 if (error_is_set(errp)) {
3661 return NULL;
3662 }
3663 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3664 is_telnet, is_waitconnect, errp);
3665}
3666
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003667static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3668 Error **errp)
3669{
3670 int fd;
3671
3672 fd = socket_dgram(dgram->remote, dgram->local, errp);
3673 if (error_is_set(errp)) {
3674 return NULL;
3675 }
3676 return qemu_chr_open_udp_fd(fd);
3677}
3678
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003679ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3680 Error **errp)
3681{
3682 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003683 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003684
3685 chr = qemu_chr_find(id);
3686 if (chr) {
3687 error_setg(errp, "Chardev '%s' already exists", id);
3688 g_free(ret);
3689 return NULL;
3690 }
3691
3692 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003693 case CHARDEV_BACKEND_KIND_FILE:
3694 chr = qmp_chardev_open_file(backend->file, errp);
3695 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003696 case CHARDEV_BACKEND_KIND_SERIAL:
3697 chr = qmp_chardev_open_serial(backend->serial, errp);
3698 break;
3699 case CHARDEV_BACKEND_KIND_PARALLEL:
3700 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003701 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003702 case CHARDEV_BACKEND_KIND_PIPE:
3703 chr = qemu_chr_open_pipe(backend->pipe);
3704 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003705 case CHARDEV_BACKEND_KIND_SOCKET:
3706 chr = qmp_chardev_open_socket(backend->socket, errp);
3707 break;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003708 case CHARDEV_BACKEND_KIND_DGRAM:
3709 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3710 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003711#ifdef HAVE_CHARDEV_TTY
3712 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003713 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003714 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003715#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003716 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003717 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003718 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003719 case CHARDEV_BACKEND_KIND_MUX:
3720 base = qemu_chr_find(backend->mux->chardev);
3721 if (base == NULL) {
3722 error_setg(errp, "mux: base chardev %s not found",
3723 backend->mux->chardev);
3724 break;
3725 }
3726 chr = qemu_chr_open_mux(base);
3727 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003728 case CHARDEV_BACKEND_KIND_MSMOUSE:
3729 chr = qemu_chr_open_msmouse();
3730 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003731#ifdef CONFIG_BRLAPI
3732 case CHARDEV_BACKEND_KIND_BRAILLE:
3733 chr = chr_baum_init();
3734 break;
3735#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003736 case CHARDEV_BACKEND_KIND_STDIO:
3737 chr = qemu_chr_open_stdio(backend->stdio);
3738 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003739#ifdef _WIN32
3740 case CHARDEV_BACKEND_KIND_CONSOLE:
3741 chr = qemu_chr_open_win_con();
3742 break;
3743#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003744#ifdef CONFIG_SPICE
3745 case CHARDEV_BACKEND_KIND_SPICEVMC:
3746 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3747 break;
3748 case CHARDEV_BACKEND_KIND_SPICEPORT:
3749 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3750 break;
3751#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003752 case CHARDEV_BACKEND_KIND_VC:
3753 chr = vc_init(backend->vc);
3754 break;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003755 case CHARDEV_BACKEND_KIND_MEMORY:
3756 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3757 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003758 default:
3759 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3760 break;
3761 }
3762
3763 if (chr == NULL && !error_is_set(errp)) {
3764 error_setg(errp, "Failed to create chardev");
3765 }
3766 if (chr) {
3767 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003768 chr->avail_connections =
3769 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003770 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3771 return ret;
3772 } else {
3773 g_free(ret);
3774 return NULL;
3775 }
3776}
3777
3778void qmp_chardev_remove(const char *id, Error **errp)
3779{
3780 CharDriverState *chr;
3781
3782 chr = qemu_chr_find(id);
3783 if (NULL == chr) {
3784 error_setg(errp, "Chardev '%s' not found", id);
3785 return;
3786 }
3787 if (chr->chr_can_read || chr->chr_read ||
3788 chr->chr_event || chr->handler_opaque) {
3789 error_setg(errp, "Chardev '%s' is busy", id);
3790 return;
3791 }
3792 qemu_chr_delete(chr);
3793}
Anthony Liguorid654f342013-03-05 23:21:28 +05303794
3795static void register_types(void)
3796{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003797 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303798 register_char_driver("socket", qemu_chr_open_socket);
3799 register_char_driver("udp", qemu_chr_open_udp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003800 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3801 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003802 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3803 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003804 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3805 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003806 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3807 qemu_chr_parse_serial);
3808 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3809 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003810 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3811 qemu_chr_parse_parallel);
3812 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3813 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003814 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003815 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003816 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3817 qemu_chr_parse_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303818}
3819
3820type_init(register_types);