blob: 6e897dab2aa3ade75f258229072251b78b99d50d [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__GLIBC__)
57#include <pty.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040058#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir1c5e97232009-03-07 20:06:23 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000062#endif
Michael Tokarev3294ce12012-06-02 23:43:33 +040063#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64#include <dev/ppbus/ppi.h>
65#include <dev/ppbus/ppbconf.h>
66#elif defined(__DragonFly__)
67#include <dev/misc/ppi/ppi.h>
68#include <bus/ppbus/ppbconf.h>
69#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010070#else
aliguori6f97dba2008-10-31 18:49:55 +000071#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000072#include <pty.h>
73
74#include <linux/ppdev.h>
75#include <linux/parport.h>
76#endif
77#ifdef __sun__
78#include <sys/stat.h>
79#include <sys/ethernet.h>
80#include <sys/sockio.h>
81#include <netinet/arp.h>
82#include <netinet/in.h>
83#include <netinet/in_systm.h>
84#include <netinet/ip.h>
85#include <netinet/ip_icmp.h> // must come after ip.h
86#include <netinet/udp.h>
87#include <netinet/tcp.h>
88#include <net/if.h>
89#include <syslog.h>
90#include <stropts.h>
91#endif
92#endif
93#endif
94
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010095#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020096#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000097
Amit Shah9bd78542009-11-03 19:59:54 +053098#define READ_BUF_LEN 4096
99
aliguori6f97dba2008-10-31 18:49:55 +0000100/***********************************************************/
101/* character device */
102
Blue Swirl72cf2d42009-09-12 07:36:22 +0000103static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000105
Hans de Goedea425d232011-11-19 10:22:43 +0100106void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000107{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200108 /* Keep track if the char device is open */
109 switch (event) {
110 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100111 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200112 break;
113 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100114 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200115 break;
116 }
117
aliguori6f97dba2008-10-31 18:49:55 +0000118 if (!s->chr_event)
119 return;
120 s->chr_event(s->handler_opaque, event);
121}
122
Hans de Goedefee204f2013-03-26 11:07:54 +0100123static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000124{
125 CharDriverState *s = opaque;
Hans de Goedea425d232011-11-19 10:22:43 +0100126 qemu_chr_be_event(s, CHR_EVENT_OPENED);
Anthony Liguori9f939df2013-03-05 23:21:27 +0530127 s->idle_tag = 0;
128 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +0000129}
130
Hans de Goedefee204f2013-03-26 11:07:54 +0100131void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000132{
Anthony Liguori9f939df2013-03-05 23:21:27 +0530133 if (s->idle_tag == 0) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100134 s->idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000135 }
136}
137
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500138int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000139{
140 return s->chr_write(s, buf, len);
141}
142
Anthony Liguoricd187202013-03-26 10:04:17 -0500143int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
144{
145 int offset = 0;
146 int res;
147
148 while (offset < len) {
149 do {
150 res = s->chr_write(s, buf + offset, len - offset);
151 if (res == -1 && errno == EAGAIN) {
152 g_usleep(100);
153 }
154 } while (res == -1 && errno == EAGAIN);
155
156 if (res == 0) {
157 break;
158 }
159
160 if (res < 0) {
161 return res;
162 }
163
164 offset += res;
165 }
166
167 return offset;
168}
169
Anthony Liguori41084f12011-08-15 11:17:34 -0500170int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000171{
172 if (!s->chr_ioctl)
173 return -ENOTSUP;
174 return s->chr_ioctl(s, cmd, arg);
175}
176
Anthony Liguori909cda12011-08-15 11:17:31 -0500177int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000178{
179 if (!s->chr_can_read)
180 return 0;
181 return s->chr_can_read(s->handler_opaque);
182}
183
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500184void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000185{
Stefan Weilac310732012-04-19 22:27:14 +0200186 if (s->chr_read) {
187 s->chr_read(s->handler_opaque, buf, len);
188 }
aliguori6f97dba2008-10-31 18:49:55 +0000189}
190
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500191int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100192{
193 return s->get_msgfd ? s->get_msgfd(s) : -1;
194}
195
Daniel P. Berrange13661082011-06-23 13:31:42 +0100196int qemu_chr_add_client(CharDriverState *s, int fd)
197{
198 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
199}
200
aliguori6f97dba2008-10-31 18:49:55 +0000201void qemu_chr_accept_input(CharDriverState *s)
202{
203 if (s->chr_accept_input)
204 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100205 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000206}
207
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500208void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000209{
Amit Shah9bd78542009-11-03 19:59:54 +0530210 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000211 va_list ap;
212 va_start(ap, fmt);
213 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500214 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000215 va_end(ap);
216}
217
aliguori6f97dba2008-10-31 18:49:55 +0000218void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100219 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000220 IOReadHandler *fd_read,
221 IOEventHandler *fd_event,
222 void *opaque)
223{
Hans de Goede19083222013-03-26 11:07:56 +0100224 int fe_open;
225
Amit Shahda7d9982011-04-25 15:18:22 +0530226 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100227 fe_open = 0;
228 } else {
229 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530230 }
aliguori6f97dba2008-10-31 18:49:55 +0000231 s->chr_can_read = fd_can_read;
232 s->chr_read = fd_read;
233 s->chr_event = fd_event;
234 s->handler_opaque = opaque;
235 if (s->chr_update_read_handler)
236 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200237
Hans de Goede19083222013-03-26 11:07:56 +0100238 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100239 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100240 }
241
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200242 /* We're connecting to an already opened device, so let's make sure we
243 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100244 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100245 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200246 }
aliguori6f97dba2008-10-31 18:49:55 +0000247}
248
249static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
250{
251 return len;
252}
253
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100254static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000255{
256 CharDriverState *chr;
257
Anthony Liguori7267c092011-08-20 22:09:37 -0500258 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000259 chr->chr_write = null_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +0100260 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000261}
262
263/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000264#define MAX_MUX 4
265#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
266#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
267typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100268 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000269 IOReadHandler *chr_read[MAX_MUX];
270 IOEventHandler *chr_event[MAX_MUX];
271 void *ext_opaque[MAX_MUX];
272 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200273 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000274 int mux_cnt;
275 int term_got_escape;
276 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000277 /* Intermediate input buffer allows to catch escape sequences even if the
278 currently active device is not accepting any input - but only until it
279 is full as well. */
280 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
281 int prod[MAX_MUX];
282 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200283 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200284 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200285 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000286} MuxDriver;
287
288
289static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
290{
291 MuxDriver *d = chr->opaque;
292 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200293 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000294 ret = d->drv->chr_write(d->drv, buf, len);
295 } else {
296 int i;
297
298 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200299 for (i = 0; i < len; i++) {
300 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000301 char buf1[64];
302 int64_t ti;
303 int secs;
304
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100305 ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200306 if (d->timestamps_start == -1)
307 d->timestamps_start = ti;
308 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000309 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000310 snprintf(buf1, sizeof(buf1),
311 "[%02d:%02d:%02d.%03d] ",
312 secs / 3600,
313 (secs / 60) % 60,
314 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000315 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000316 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200317 d->linestart = 0;
318 }
319 ret += d->drv->chr_write(d->drv, buf+i, 1);
320 if (buf[i] == '\n') {
321 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000322 }
323 }
324 }
325 return ret;
326}
327
328static const char * const mux_help[] = {
329 "% h print this help\n\r",
330 "% x exit emulator\n\r",
331 "% s save disk data back to file (if -snapshot)\n\r",
332 "% t toggle console timestamps\n\r"
333 "% b send break (magic sysrq)\n\r",
334 "% c switch between console and monitor\n\r",
335 "% % sends %\n\r",
336 NULL
337};
338
339int term_escape_char = 0x01; /* ctrl-a is used for escape */
340static void mux_print_help(CharDriverState *chr)
341{
342 int i, j;
343 char ebuf[15] = "Escape-Char";
344 char cbuf[50] = "\n\r";
345
346 if (term_escape_char > 0 && term_escape_char < 26) {
347 snprintf(cbuf, sizeof(cbuf), "\n\r");
348 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
349 } else {
350 snprintf(cbuf, sizeof(cbuf),
351 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
352 term_escape_char);
353 }
354 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
355 for (i = 0; mux_help[i] != NULL; i++) {
356 for (j=0; mux_help[i][j] != '\0'; j++) {
357 if (mux_help[i][j] == '%')
358 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
359 else
360 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
361 }
362 }
363}
364
aliguori2724b182009-03-05 23:01:47 +0000365static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
366{
367 if (d->chr_event[mux_nr])
368 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
369}
370
aliguori6f97dba2008-10-31 18:49:55 +0000371static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
372{
373 if (d->term_got_escape) {
374 d->term_got_escape = 0;
375 if (ch == term_escape_char)
376 goto send_char;
377 switch(ch) {
378 case '?':
379 case 'h':
380 mux_print_help(chr);
381 break;
382 case 'x':
383 {
384 const char *term = "QEMU: Terminated\n\r";
385 chr->chr_write(chr,(uint8_t *)term,strlen(term));
386 exit(0);
387 break;
388 }
389 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200390 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000391 break;
392 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100393 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000394 break;
395 case 'c':
396 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200397 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
398 d->focus++;
399 if (d->focus >= d->mux_cnt)
400 d->focus = 0;
401 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000402 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200403 case 't':
404 d->timestamps = !d->timestamps;
405 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200406 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200407 break;
aliguori6f97dba2008-10-31 18:49:55 +0000408 }
409 } else if (ch == term_escape_char) {
410 d->term_got_escape = 1;
411 } else {
412 send_char:
413 return 1;
414 }
415 return 0;
416}
417
418static void mux_chr_accept_input(CharDriverState *chr)
419{
aliguori6f97dba2008-10-31 18:49:55 +0000420 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200421 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000422
aliguoria80bf992009-03-05 23:00:02 +0000423 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000424 d->chr_can_read[m] &&
425 d->chr_can_read[m](d->ext_opaque[m])) {
426 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000427 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000428 }
429}
430
431static int mux_chr_can_read(void *opaque)
432{
433 CharDriverState *chr = opaque;
434 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200435 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000436
aliguoria80bf992009-03-05 23:00:02 +0000437 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000438 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000439 if (d->chr_can_read[m])
440 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000441 return 0;
442}
443
444static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
445{
446 CharDriverState *chr = opaque;
447 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200448 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000449 int i;
450
451 mux_chr_accept_input (opaque);
452
453 for(i = 0; i < size; i++)
454 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000455 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000456 d->chr_can_read[m] &&
457 d->chr_can_read[m](d->ext_opaque[m]))
458 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
459 else
aliguoria80bf992009-03-05 23:00:02 +0000460 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000461 }
462}
463
464static void mux_chr_event(void *opaque, int event)
465{
466 CharDriverState *chr = opaque;
467 MuxDriver *d = chr->opaque;
468 int i;
469
470 /* Send the event to all registered listeners */
471 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000472 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000473}
474
475static void mux_chr_update_read_handler(CharDriverState *chr)
476{
477 MuxDriver *d = chr->opaque;
478
479 if (d->mux_cnt >= MAX_MUX) {
480 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
481 return;
482 }
483 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
484 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
485 d->chr_read[d->mux_cnt] = chr->chr_read;
486 d->chr_event[d->mux_cnt] = chr->chr_event;
487 /* Fix up the real driver with mux routines */
488 if (d->mux_cnt == 0) {
489 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
490 mux_chr_event, chr);
491 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200492 if (d->focus != -1) {
493 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200494 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200495 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000496 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200497 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000498}
499
500static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
501{
502 CharDriverState *chr;
503 MuxDriver *d;
504
Anthony Liguori7267c092011-08-20 22:09:37 -0500505 chr = g_malloc0(sizeof(CharDriverState));
506 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000507
508 chr->opaque = d;
509 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200510 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000511 chr->chr_write = mux_chr_write;
512 chr->chr_update_read_handler = mux_chr_update_read_handler;
513 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100514 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100515 chr->chr_set_fe_open = NULL;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200516
517 /* Muxes are always open on creation */
Hans de Goedefee204f2013-03-26 11:07:54 +0100518 qemu_chr_be_generic_open(chr);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200519
aliguori6f97dba2008-10-31 18:49:55 +0000520 return chr;
521}
522
523
524#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000525int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000526{
527 int ret, len;
528
529 len = len1;
530 while (len > 0) {
531 ret = send(fd, buf, len, 0);
532 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000533 errno = WSAGetLastError();
534 if (errno != WSAEWOULDBLOCK) {
535 return -1;
536 }
537 } else if (ret == 0) {
538 break;
539 } else {
540 buf += ret;
541 len -= ret;
542 }
543 }
544 return len1 - len;
545}
546
547#else
548
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100549int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000550{
551 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100552 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000553
554 len = len1;
555 while (len > 0) {
556 ret = write(fd, buf, len);
557 if (ret < 0) {
558 if (errno != EINTR && errno != EAGAIN)
559 return -1;
560 } else if (ret == 0) {
561 break;
562 } else {
563 buf += ret;
564 len -= ret;
565 }
566 }
567 return len1 - len;
568}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500569
570int recv_all(int fd, void *_buf, int len1, bool single_read)
571{
572 int ret, len;
573 uint8_t *buf = _buf;
574
575 len = len1;
576 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
577 if (ret < 0) {
578 if (errno != EINTR && errno != EAGAIN) {
579 return -1;
580 }
581 continue;
582 } else {
583 if (single_read) {
584 return ret;
585 }
586 buf += ret;
587 len -= ret;
588 }
589 }
590 return len1 - len;
591}
592
aliguori6f97dba2008-10-31 18:49:55 +0000593#endif /* !_WIN32 */
594
Anthony Liguori96c63842013-03-05 23:21:18 +0530595typedef struct IOWatchPoll
596{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200597 GSource parent;
598
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200599 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530600 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530601
602 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200603 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530604 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530605} IOWatchPoll;
606
Anthony Liguori96c63842013-03-05 23:21:18 +0530607static IOWatchPoll *io_watch_poll_from_source(GSource *source)
608{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200609 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530610}
611
612static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
613{
614 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200615 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200616 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200617 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530618 return FALSE;
619 }
620
Paolo Bonzinid185c092013-04-05 17:59:33 +0200621 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200622 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
623 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200624 g_source_attach(iwp->src, NULL);
625 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200626 g_source_destroy(iwp->src);
627 g_source_unref(iwp->src);
628 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200629 }
630 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530631}
632
633static gboolean io_watch_poll_check(GSource *source)
634{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200635 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530636}
637
638static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
639 gpointer user_data)
640{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200641 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530642}
643
644static void io_watch_poll_finalize(GSource *source)
645{
646 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini910b6362013-04-19 17:32:06 +0200647 if (iwp->src) {
648 g_source_destroy(iwp->src);
649 g_source_unref(iwp->src);
650 iwp->src = NULL;
651 }
Anthony Liguori96c63842013-03-05 23:21:18 +0530652}
653
654static GSourceFuncs io_watch_poll_funcs = {
655 .prepare = io_watch_poll_prepare,
656 .check = io_watch_poll_check,
657 .dispatch = io_watch_poll_dispatch,
658 .finalize = io_watch_poll_finalize,
659};
660
661/* Can only be used for read */
662static guint io_add_watch_poll(GIOChannel *channel,
663 IOCanReadHandler *fd_can_read,
664 GIOFunc fd_read,
665 gpointer user_data)
666{
667 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200668 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530669
Paolo Bonzinid185c092013-04-05 17:59:33 +0200670 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530671 iwp->fd_can_read = fd_can_read;
672 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200673 iwp->channel = channel;
674 iwp->fd_read = (GSourceFunc) fd_read;
675 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530676
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200677 tag = g_source_attach(&iwp->parent, NULL);
678 g_source_unref(&iwp->parent);
679 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530680}
681
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000682#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530683static GIOChannel *io_channel_from_fd(int fd)
684{
685 GIOChannel *chan;
686
687 if (fd == -1) {
688 return NULL;
689 }
690
691 chan = g_io_channel_unix_new(fd);
692
693 g_io_channel_set_encoding(chan, NULL, NULL);
694 g_io_channel_set_buffered(chan, FALSE);
695
696 return chan;
697}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000698#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530699
Anthony Liguori76a96442013-03-05 23:21:21 +0530700static GIOChannel *io_channel_from_socket(int fd)
701{
702 GIOChannel *chan;
703
704 if (fd == -1) {
705 return NULL;
706 }
707
708#ifdef _WIN32
709 chan = g_io_channel_win32_new_socket(fd);
710#else
711 chan = g_io_channel_unix_new(fd);
712#endif
713
714 g_io_channel_set_encoding(chan, NULL, NULL);
715 g_io_channel_set_buffered(chan, FALSE);
716
717 return chan;
718}
719
Anthony Liguori684a0962013-03-29 11:39:50 -0500720static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530721{
722 GIOStatus status;
Anthony Liguori684a0962013-03-29 11:39:50 -0500723 size_t offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530724
Anthony Liguori684a0962013-03-29 11:39:50 -0500725 offset = 0;
726 while (offset < len) {
727 gsize bytes_written;
728
729 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530730 &bytes_written, NULL);
731 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530732 if (status == G_IO_STATUS_AGAIN) {
Anthony Liguori684a0962013-03-29 11:39:50 -0500733 /* If we've written any data, return a partial write. */
734 if (offset) {
735 break;
736 }
Anthony Liguori23673ca2013-03-05 23:21:23 +0530737 errno = EAGAIN;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530738 } else {
739 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530740 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500741
742 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530743 } else if (status == G_IO_STATUS_EOF) {
744 break;
Anthony Liguori96c63842013-03-05 23:21:18 +0530745 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500746
747 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530748 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500749
750 return offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530751}
Anthony Liguori96c63842013-03-05 23:21:18 +0530752
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000753#ifndef _WIN32
754
Anthony Liguoria29753f2013-03-05 23:21:19 +0530755typedef struct FDCharDriver {
756 CharDriverState *chr;
757 GIOChannel *fd_in, *fd_out;
758 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000759 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530760 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000761} FDCharDriver;
762
aliguori6f97dba2008-10-31 18:49:55 +0000763static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
764{
765 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530766
Anthony Liguori684a0962013-03-29 11:39:50 -0500767 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530768}
769
770static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
771{
772 CharDriverState *chr = opaque;
773 FDCharDriver *s = chr->opaque;
774 int len;
775 uint8_t buf[READ_BUF_LEN];
776 GIOStatus status;
777 gsize bytes_read;
778
779 len = sizeof(buf);
780 if (len > s->max_size) {
781 len = s->max_size;
782 }
783 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200784 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530785 }
786
787 status = g_io_channel_read_chars(chan, (gchar *)buf,
788 len, &bytes_read, NULL);
789 if (status == G_IO_STATUS_EOF) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200790 if (s->fd_in_tag) {
791 g_source_remove(s->fd_in_tag);
792 s->fd_in_tag = 0;
793 }
Anthony Liguoria29753f2013-03-05 23:21:19 +0530794 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
795 return FALSE;
796 }
797 if (status == G_IO_STATUS_NORMAL) {
798 qemu_chr_be_write(chr, buf, bytes_read);
799 }
800
801 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000802}
803
804static int fd_chr_read_poll(void *opaque)
805{
806 CharDriverState *chr = opaque;
807 FDCharDriver *s = chr->opaque;
808
Anthony Liguori909cda12011-08-15 11:17:31 -0500809 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000810 return s->max_size;
811}
812
Anthony Liguori23673ca2013-03-05 23:21:23 +0530813static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
814{
815 FDCharDriver *s = chr->opaque;
816 return g_io_create_watch(s->fd_out, cond);
817}
818
aliguori6f97dba2008-10-31 18:49:55 +0000819static void fd_chr_update_read_handler(CharDriverState *chr)
820{
821 FDCharDriver *s = chr->opaque;
822
Anthony Liguoria29753f2013-03-05 23:21:19 +0530823 if (s->fd_in_tag) {
824 g_source_remove(s->fd_in_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +0200825 s->fd_in_tag = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530826 }
827
828 if (s->fd_in) {
829 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 +0000830 }
831}
832
833static void fd_chr_close(struct CharDriverState *chr)
834{
835 FDCharDriver *s = chr->opaque;
836
Anthony Liguoria29753f2013-03-05 23:21:19 +0530837 if (s->fd_in_tag) {
838 g_source_remove(s->fd_in_tag);
839 s->fd_in_tag = 0;
840 }
841
842 if (s->fd_in) {
843 g_io_channel_unref(s->fd_in);
844 }
845 if (s->fd_out) {
846 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000847 }
848
Anthony Liguori7267c092011-08-20 22:09:37 -0500849 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100850 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000851}
852
853/* open a character device to a unix fd */
854static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
855{
856 CharDriverState *chr;
857 FDCharDriver *s;
858
Anthony Liguori7267c092011-08-20 22:09:37 -0500859 chr = g_malloc0(sizeof(CharDriverState));
860 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530861 s->fd_in = io_channel_from_fd(fd_in);
862 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530863 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530864 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000865 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530866 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000867 chr->chr_write = fd_chr_write;
868 chr->chr_update_read_handler = fd_chr_update_read_handler;
869 chr->chr_close = fd_chr_close;
870
Hans de Goedefee204f2013-03-26 11:07:54 +0100871 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000872
873 return chr;
874}
875
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100876static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000877{
878 int fd_in, fd_out;
879 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100880 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200881
882 if (filename == NULL) {
883 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100884 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200885 }
aliguori6f97dba2008-10-31 18:49:55 +0000886
887 snprintf(filename_in, 256, "%s.in", filename);
888 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100889 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
890 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000891 if (fd_in < 0 || fd_out < 0) {
892 if (fd_in >= 0)
893 close(fd_in);
894 if (fd_out >= 0)
895 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100896 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100897 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100898 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100899 }
aliguori6f97dba2008-10-31 18:49:55 +0000900 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100901 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000902}
903
aliguori6f97dba2008-10-31 18:49:55 +0000904/* init terminal so that we can grab keys */
905static struct termios oldtty;
906static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100907static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000908
909static void term_exit(void)
910{
911 tcsetattr (0, TCSANOW, &oldtty);
912 fcntl(0, F_SETFL, old_fd0_flags);
913}
914
Paolo Bonzinibb002512010-12-23 13:42:50 +0100915static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000916{
917 struct termios tty;
918
Paolo Bonzini03693642010-12-23 13:42:49 +0100919 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100920 if (!echo) {
921 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000922 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100923 tty.c_oflag |= OPOST;
924 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
925 tty.c_cflag &= ~(CSIZE|PARENB);
926 tty.c_cflag |= CS8;
927 tty.c_cc[VMIN] = 1;
928 tty.c_cc[VTIME] = 0;
929 }
aliguori6f97dba2008-10-31 18:49:55 +0000930 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100931 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000932 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000933
934 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000935}
936
937static void qemu_chr_close_stdio(struct CharDriverState *chr)
938{
939 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000940 fd_chr_close(chr);
941}
942
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100943static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000944{
945 CharDriverState *chr;
946
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400947 if (is_daemonized()) {
948 error_report("cannot use stdio with -daemonize");
949 return NULL;
950 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530951 old_fd0_flags = fcntl(0, F_GETFL);
952 tcgetattr (0, &oldtty);
953 fcntl(0, F_SETFL, O_NONBLOCK);
954 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100955
aliguori6f97dba2008-10-31 18:49:55 +0000956 chr = qemu_chr_open_fd(0, 1);
957 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100958 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100959 stdio_allow_signal = display_type != DT_NOGRAPHIC;
960 if (opts->has_signal) {
961 stdio_allow_signal = opts->signal;
962 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500963 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000964
Markus Armbruster1f514702012-02-07 15:09:08 +0100965 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000966}
967
968#ifdef __sun__
969/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000970static int openpty(int *amaster, int *aslave, char *name,
971 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000972{
973 const char *slave;
974 int mfd = -1, sfd = -1;
975
976 *amaster = *aslave = -1;
977
978 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
979 if (mfd < 0)
980 goto err;
981
982 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
983 goto err;
984
985 if ((slave = ptsname(mfd)) == NULL)
986 goto err;
987
988 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
989 goto err;
990
991 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
992 (termp != NULL && tcgetattr(sfd, termp) < 0))
993 goto err;
994
995 if (amaster)
996 *amaster = mfd;
997 if (aslave)
998 *aslave = sfd;
999 if (winp)
1000 ioctl(sfd, TIOCSWINSZ, winp);
1001
1002 return 0;
1003
1004err:
1005 if (sfd != -1)
1006 close(sfd);
1007 close(mfd);
1008 return -1;
1009}
1010
blueswir13f4cb3d2009-04-13 16:31:01 +00001011static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +00001012{
1013 termios_p->c_iflag &=
1014 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
1015 termios_p->c_oflag &= ~OPOST;
1016 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1017 termios_p->c_cflag &= ~(CSIZE|PARENB);
1018 termios_p->c_cflag |= CS8;
1019
1020 termios_p->c_cc[VMIN] = 0;
1021 termios_p->c_cc[VTIME] = 0;
1022}
1023#endif
1024
1025#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001026 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1027 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001028
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001029#define HAVE_CHARDEV_TTY 1
1030
aliguori6f97dba2008-10-31 18:49:55 +00001031typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301032 GIOChannel *fd;
1033 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001034 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001035 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301036 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001037} PtyCharDriver;
1038
1039static void pty_chr_update_read_handler(CharDriverState *chr);
1040static void pty_chr_state(CharDriverState *chr, int connected);
1041
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301042static gboolean pty_chr_timer(gpointer opaque)
1043{
1044 struct CharDriverState *chr = opaque;
1045 PtyCharDriver *s = chr->opaque;
1046
1047 if (s->connected) {
1048 goto out;
1049 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301050
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;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001112 if (len == 0) {
1113 return TRUE;
1114 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301115 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1116 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001117 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301118 return FALSE;
1119 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001120 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001121 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001122 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301123 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001124}
1125
1126static void pty_chr_update_read_handler(CharDriverState *chr)
1127{
1128 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001129 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001130
Paolo Bonzini85a67692013-04-19 17:32:07 +02001131 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1132 pfd.events = G_IO_OUT;
1133 pfd.revents = 0;
1134 g_poll(&pfd, 1, 0);
1135 if (pfd.revents & G_IO_HUP) {
1136 pty_chr_state(chr, 0);
1137 } else {
1138 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301139 }
aliguori6f97dba2008-10-31 18:49:55 +00001140}
1141
1142static void pty_chr_state(CharDriverState *chr, int connected)
1143{
1144 PtyCharDriver *s = chr->opaque;
1145
1146 if (!connected) {
Paolo Bonzini910b6362013-04-19 17:32:06 +02001147 if (s->fd_tag) {
1148 g_source_remove(s->fd_tag);
1149 s->fd_tag = 0;
1150 }
aliguori6f97dba2008-10-31 18:49:55 +00001151 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001152 /* (re-)connect poll interval for idle guests: once per second.
1153 * We check more frequently in case the guests sends data to
1154 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301155 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001156 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001157 if (s->timer_tag) {
1158 g_source_remove(s->timer_tag);
1159 s->timer_tag = 0;
1160 }
1161 if (!s->connected) {
Hans de Goedefee204f2013-03-26 11:07:54 +01001162 qemu_chr_be_generic_open(chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001163 s->connected = 1;
1164 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
1165 }
aliguori6f97dba2008-10-31 18:49:55 +00001166 }
1167}
1168
aliguori6f97dba2008-10-31 18:49:55 +00001169
1170static void pty_chr_close(struct CharDriverState *chr)
1171{
1172 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301173 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001174
Anthony Liguori093d3a22013-03-05 23:21:20 +05301175 if (s->fd_tag) {
1176 g_source_remove(s->fd_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001177 s->fd_tag = 0;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301178 }
1179 fd = g_io_channel_unix_get_fd(s->fd);
1180 g_io_channel_unref(s->fd);
1181 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301182 if (s->timer_tag) {
1183 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001184 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301185 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001186 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001187 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001188}
1189
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001190static CharDriverState *qemu_chr_open_pty(const char *id,
1191 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001192{
1193 CharDriverState *chr;
1194 PtyCharDriver *s;
1195 struct termios tty;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001196 int master_fd, slave_fd;
blueswir1c5e97232009-03-07 20:06:23 +00001197#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001198 char pty_name[PATH_MAX];
1199#define q_ptsname(x) pty_name
1200#else
1201 char *pty_name = NULL;
1202#define q_ptsname(x) ptsname(x)
1203#endif
1204
Markus Armbrustera4e26042011-11-11 10:40:05 +01001205 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001206 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001207 }
1208
1209 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001210 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001211 cfmakeraw(&tty);
1212 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1213 close(slave_fd);
1214
Markus Armbrustera4e26042011-11-11 10:40:05 +01001215 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001216
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001217 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1218 ret->pty = g_strdup(q_ptsname(master_fd));
1219 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001220
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001221 fprintf(stderr, "char device redirected to %s (label %s)\n",
1222 q_ptsname(master_fd), id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001223
1224 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001225 chr->opaque = s;
1226 chr->chr_write = pty_chr_write;
1227 chr->chr_update_read_handler = pty_chr_update_read_handler;
1228 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301229 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001230
Anthony Liguori093d3a22013-03-05 23:21:20 +05301231 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301232 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001233
Markus Armbruster1f514702012-02-07 15:09:08 +01001234 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001235}
1236
1237static void tty_serial_init(int fd, int speed,
1238 int parity, int data_bits, int stop_bits)
1239{
1240 struct termios tty;
1241 speed_t spd;
1242
1243#if 0
1244 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1245 speed, parity, data_bits, stop_bits);
1246#endif
1247 tcgetattr (fd, &tty);
1248
Stefan Weil45eea132009-10-26 16:10:10 +01001249#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1250 speed = speed * 10 / 11;
1251 do {
1252 check_speed(50);
1253 check_speed(75);
1254 check_speed(110);
1255 check_speed(134);
1256 check_speed(150);
1257 check_speed(200);
1258 check_speed(300);
1259 check_speed(600);
1260 check_speed(1200);
1261 check_speed(1800);
1262 check_speed(2400);
1263 check_speed(4800);
1264 check_speed(9600);
1265 check_speed(19200);
1266 check_speed(38400);
1267 /* Non-Posix values follow. They may be unsupported on some systems. */
1268 check_speed(57600);
1269 check_speed(115200);
1270#ifdef B230400
1271 check_speed(230400);
1272#endif
1273#ifdef B460800
1274 check_speed(460800);
1275#endif
1276#ifdef B500000
1277 check_speed(500000);
1278#endif
1279#ifdef B576000
1280 check_speed(576000);
1281#endif
1282#ifdef B921600
1283 check_speed(921600);
1284#endif
1285#ifdef B1000000
1286 check_speed(1000000);
1287#endif
1288#ifdef B1152000
1289 check_speed(1152000);
1290#endif
1291#ifdef B1500000
1292 check_speed(1500000);
1293#endif
1294#ifdef B2000000
1295 check_speed(2000000);
1296#endif
1297#ifdef B2500000
1298 check_speed(2500000);
1299#endif
1300#ifdef B3000000
1301 check_speed(3000000);
1302#endif
1303#ifdef B3500000
1304 check_speed(3500000);
1305#endif
1306#ifdef B4000000
1307 check_speed(4000000);
1308#endif
aliguori6f97dba2008-10-31 18:49:55 +00001309 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001310 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001311
1312 cfsetispeed(&tty, spd);
1313 cfsetospeed(&tty, spd);
1314
1315 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1316 |INLCR|IGNCR|ICRNL|IXON);
1317 tty.c_oflag |= OPOST;
1318 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1319 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1320 switch(data_bits) {
1321 default:
1322 case 8:
1323 tty.c_cflag |= CS8;
1324 break;
1325 case 7:
1326 tty.c_cflag |= CS7;
1327 break;
1328 case 6:
1329 tty.c_cflag |= CS6;
1330 break;
1331 case 5:
1332 tty.c_cflag |= CS5;
1333 break;
1334 }
1335 switch(parity) {
1336 default:
1337 case 'N':
1338 break;
1339 case 'E':
1340 tty.c_cflag |= PARENB;
1341 break;
1342 case 'O':
1343 tty.c_cflag |= PARENB | PARODD;
1344 break;
1345 }
1346 if (stop_bits == 2)
1347 tty.c_cflag |= CSTOPB;
1348
1349 tcsetattr (fd, TCSANOW, &tty);
1350}
1351
1352static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1353{
1354 FDCharDriver *s = chr->opaque;
1355
1356 switch(cmd) {
1357 case CHR_IOCTL_SERIAL_SET_PARAMS:
1358 {
1359 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301360 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1361 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001362 ssp->data_bits, ssp->stop_bits);
1363 }
1364 break;
1365 case CHR_IOCTL_SERIAL_SET_BREAK:
1366 {
1367 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301368 if (enable) {
1369 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1370 }
aliguori6f97dba2008-10-31 18:49:55 +00001371 }
1372 break;
1373 case CHR_IOCTL_SERIAL_GET_TIOCM:
1374 {
1375 int sarg = 0;
1376 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301377 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001378 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001379 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001380 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001381 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001382 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001383 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001384 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001385 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001386 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001387 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001388 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001389 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001390 *targ |= CHR_TIOCM_RTS;
1391 }
1392 break;
1393 case CHR_IOCTL_SERIAL_SET_TIOCM:
1394 {
1395 int sarg = *(int *)arg;
1396 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301397 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001398 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1399 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1400 if (sarg & CHR_TIOCM_CTS)
1401 targ |= TIOCM_CTS;
1402 if (sarg & CHR_TIOCM_CAR)
1403 targ |= TIOCM_CAR;
1404 if (sarg & CHR_TIOCM_DSR)
1405 targ |= TIOCM_DSR;
1406 if (sarg & CHR_TIOCM_RI)
1407 targ |= TIOCM_RI;
1408 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001409 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001410 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001411 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301412 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001413 }
1414 break;
1415 default:
1416 return -ENOTSUP;
1417 }
1418 return 0;
1419}
1420
David Ahern4266a132010-02-10 18:27:17 -07001421static void qemu_chr_close_tty(CharDriverState *chr)
1422{
1423 FDCharDriver *s = chr->opaque;
1424 int fd = -1;
1425
1426 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301427 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001428 }
1429
1430 fd_chr_close(chr);
1431
1432 if (fd >= 0) {
1433 close(fd);
1434 }
1435}
1436
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001437static CharDriverState *qemu_chr_open_tty_fd(int fd)
1438{
1439 CharDriverState *chr;
1440
1441 tty_serial_init(fd, 115200, 'N', 8, 1);
1442 chr = qemu_chr_open_fd(fd, fd);
1443 chr->chr_ioctl = tty_serial_ioctl;
1444 chr->chr_close = qemu_chr_close_tty;
1445 return chr;
1446}
aliguori6f97dba2008-10-31 18:49:55 +00001447#endif /* __linux__ || __sun__ */
1448
1449#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001450
1451#define HAVE_CHARDEV_PARPORT 1
1452
aliguori6f97dba2008-10-31 18:49:55 +00001453typedef struct {
1454 int fd;
1455 int mode;
1456} ParallelCharDriver;
1457
1458static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1459{
1460 if (s->mode != mode) {
1461 int m = mode;
1462 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1463 return 0;
1464 s->mode = mode;
1465 }
1466 return 1;
1467}
1468
1469static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1470{
1471 ParallelCharDriver *drv = chr->opaque;
1472 int fd = drv->fd;
1473 uint8_t b;
1474
1475 switch(cmd) {
1476 case CHR_IOCTL_PP_READ_DATA:
1477 if (ioctl(fd, PPRDATA, &b) < 0)
1478 return -ENOTSUP;
1479 *(uint8_t *)arg = b;
1480 break;
1481 case CHR_IOCTL_PP_WRITE_DATA:
1482 b = *(uint8_t *)arg;
1483 if (ioctl(fd, PPWDATA, &b) < 0)
1484 return -ENOTSUP;
1485 break;
1486 case CHR_IOCTL_PP_READ_CONTROL:
1487 if (ioctl(fd, PPRCONTROL, &b) < 0)
1488 return -ENOTSUP;
1489 /* Linux gives only the lowest bits, and no way to know data
1490 direction! For better compatibility set the fixed upper
1491 bits. */
1492 *(uint8_t *)arg = b | 0xc0;
1493 break;
1494 case CHR_IOCTL_PP_WRITE_CONTROL:
1495 b = *(uint8_t *)arg;
1496 if (ioctl(fd, PPWCONTROL, &b) < 0)
1497 return -ENOTSUP;
1498 break;
1499 case CHR_IOCTL_PP_READ_STATUS:
1500 if (ioctl(fd, PPRSTATUS, &b) < 0)
1501 return -ENOTSUP;
1502 *(uint8_t *)arg = b;
1503 break;
1504 case CHR_IOCTL_PP_DATA_DIR:
1505 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1506 return -ENOTSUP;
1507 break;
1508 case CHR_IOCTL_PP_EPP_READ_ADDR:
1509 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1510 struct ParallelIOArg *parg = arg;
1511 int n = read(fd, parg->buffer, parg->count);
1512 if (n != parg->count) {
1513 return -EIO;
1514 }
1515 }
1516 break;
1517 case CHR_IOCTL_PP_EPP_READ:
1518 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1519 struct ParallelIOArg *parg = arg;
1520 int n = read(fd, parg->buffer, parg->count);
1521 if (n != parg->count) {
1522 return -EIO;
1523 }
1524 }
1525 break;
1526 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1527 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1528 struct ParallelIOArg *parg = arg;
1529 int n = write(fd, parg->buffer, parg->count);
1530 if (n != parg->count) {
1531 return -EIO;
1532 }
1533 }
1534 break;
1535 case CHR_IOCTL_PP_EPP_WRITE:
1536 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1537 struct ParallelIOArg *parg = arg;
1538 int n = write(fd, parg->buffer, parg->count);
1539 if (n != parg->count) {
1540 return -EIO;
1541 }
1542 }
1543 break;
1544 default:
1545 return -ENOTSUP;
1546 }
1547 return 0;
1548}
1549
1550static void pp_close(CharDriverState *chr)
1551{
1552 ParallelCharDriver *drv = chr->opaque;
1553 int fd = drv->fd;
1554
1555 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1556 ioctl(fd, PPRELEASE);
1557 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001558 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001559 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001560}
1561
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001562static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001563{
1564 CharDriverState *chr;
1565 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001566
1567 if (ioctl(fd, PPCLAIM) < 0) {
1568 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001569 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001570 }
1571
Anthony Liguori7267c092011-08-20 22:09:37 -05001572 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001573 drv->fd = fd;
1574 drv->mode = IEEE1284_MODE_COMPAT;
1575
Anthony Liguori7267c092011-08-20 22:09:37 -05001576 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001577 chr->chr_write = null_chr_write;
1578 chr->chr_ioctl = pp_ioctl;
1579 chr->chr_close = pp_close;
1580 chr->opaque = drv;
1581
Hans de Goedefee204f2013-03-26 11:07:54 +01001582 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001583
Markus Armbruster1f514702012-02-07 15:09:08 +01001584 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001585}
1586#endif /* __linux__ */
1587
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001588#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001589
1590#define HAVE_CHARDEV_PARPORT 1
1591
blueswir16972f932008-11-22 20:49:12 +00001592static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1593{
Stefan Weile0efb992011-02-23 19:09:16 +01001594 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001595 uint8_t b;
1596
1597 switch(cmd) {
1598 case CHR_IOCTL_PP_READ_DATA:
1599 if (ioctl(fd, PPIGDATA, &b) < 0)
1600 return -ENOTSUP;
1601 *(uint8_t *)arg = b;
1602 break;
1603 case CHR_IOCTL_PP_WRITE_DATA:
1604 b = *(uint8_t *)arg;
1605 if (ioctl(fd, PPISDATA, &b) < 0)
1606 return -ENOTSUP;
1607 break;
1608 case CHR_IOCTL_PP_READ_CONTROL:
1609 if (ioctl(fd, PPIGCTRL, &b) < 0)
1610 return -ENOTSUP;
1611 *(uint8_t *)arg = b;
1612 break;
1613 case CHR_IOCTL_PP_WRITE_CONTROL:
1614 b = *(uint8_t *)arg;
1615 if (ioctl(fd, PPISCTRL, &b) < 0)
1616 return -ENOTSUP;
1617 break;
1618 case CHR_IOCTL_PP_READ_STATUS:
1619 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1620 return -ENOTSUP;
1621 *(uint8_t *)arg = b;
1622 break;
1623 default:
1624 return -ENOTSUP;
1625 }
1626 return 0;
1627}
1628
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001629static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001630{
1631 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001632
Anthony Liguori7267c092011-08-20 22:09:37 -05001633 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001634 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001635 chr->chr_write = null_chr_write;
1636 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001637 return chr;
blueswir16972f932008-11-22 20:49:12 +00001638}
1639#endif
1640
aliguori6f97dba2008-10-31 18:49:55 +00001641#else /* _WIN32 */
1642
1643typedef struct {
1644 int max_size;
1645 HANDLE hcom, hrecv, hsend;
1646 OVERLAPPED orecv, osend;
1647 BOOL fpipe;
1648 DWORD len;
1649} WinCharState;
1650
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001651typedef struct {
1652 HANDLE hStdIn;
1653 HANDLE hInputReadyEvent;
1654 HANDLE hInputDoneEvent;
1655 HANDLE hInputThread;
1656 uint8_t win_stdio_buf;
1657} WinStdioCharState;
1658
aliguori6f97dba2008-10-31 18:49:55 +00001659#define NSENDBUF 2048
1660#define NRECVBUF 2048
1661#define MAXCONNECT 1
1662#define NTIMEOUT 5000
1663
1664static int win_chr_poll(void *opaque);
1665static int win_chr_pipe_poll(void *opaque);
1666
1667static void win_chr_close(CharDriverState *chr)
1668{
1669 WinCharState *s = chr->opaque;
1670
1671 if (s->hsend) {
1672 CloseHandle(s->hsend);
1673 s->hsend = NULL;
1674 }
1675 if (s->hrecv) {
1676 CloseHandle(s->hrecv);
1677 s->hrecv = NULL;
1678 }
1679 if (s->hcom) {
1680 CloseHandle(s->hcom);
1681 s->hcom = NULL;
1682 }
1683 if (s->fpipe)
1684 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1685 else
1686 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301687
Hans de Goedea425d232011-11-19 10:22:43 +01001688 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001689}
1690
1691static int win_chr_init(CharDriverState *chr, const char *filename)
1692{
1693 WinCharState *s = chr->opaque;
1694 COMMCONFIG comcfg;
1695 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1696 COMSTAT comstat;
1697 DWORD size;
1698 DWORD err;
1699
1700 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1701 if (!s->hsend) {
1702 fprintf(stderr, "Failed CreateEvent\n");
1703 goto fail;
1704 }
1705 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1706 if (!s->hrecv) {
1707 fprintf(stderr, "Failed CreateEvent\n");
1708 goto fail;
1709 }
1710
1711 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1712 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1713 if (s->hcom == INVALID_HANDLE_VALUE) {
1714 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1715 s->hcom = NULL;
1716 goto fail;
1717 }
1718
1719 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1720 fprintf(stderr, "Failed SetupComm\n");
1721 goto fail;
1722 }
1723
1724 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1725 size = sizeof(COMMCONFIG);
1726 GetDefaultCommConfig(filename, &comcfg, &size);
1727 comcfg.dcb.DCBlength = sizeof(DCB);
1728 CommConfigDialog(filename, NULL, &comcfg);
1729
1730 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1731 fprintf(stderr, "Failed SetCommState\n");
1732 goto fail;
1733 }
1734
1735 if (!SetCommMask(s->hcom, EV_ERR)) {
1736 fprintf(stderr, "Failed SetCommMask\n");
1737 goto fail;
1738 }
1739
1740 cto.ReadIntervalTimeout = MAXDWORD;
1741 if (!SetCommTimeouts(s->hcom, &cto)) {
1742 fprintf(stderr, "Failed SetCommTimeouts\n");
1743 goto fail;
1744 }
1745
1746 if (!ClearCommError(s->hcom, &err, &comstat)) {
1747 fprintf(stderr, "Failed ClearCommError\n");
1748 goto fail;
1749 }
1750 qemu_add_polling_cb(win_chr_poll, chr);
1751 return 0;
1752
1753 fail:
1754 win_chr_close(chr);
1755 return -1;
1756}
1757
1758static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1759{
1760 WinCharState *s = chr->opaque;
1761 DWORD len, ret, size, err;
1762
1763 len = len1;
1764 ZeroMemory(&s->osend, sizeof(s->osend));
1765 s->osend.hEvent = s->hsend;
1766 while (len > 0) {
1767 if (s->hsend)
1768 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1769 else
1770 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1771 if (!ret) {
1772 err = GetLastError();
1773 if (err == ERROR_IO_PENDING) {
1774 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1775 if (ret) {
1776 buf += size;
1777 len -= size;
1778 } else {
1779 break;
1780 }
1781 } else {
1782 break;
1783 }
1784 } else {
1785 buf += size;
1786 len -= size;
1787 }
1788 }
1789 return len1 - len;
1790}
1791
1792static int win_chr_read_poll(CharDriverState *chr)
1793{
1794 WinCharState *s = chr->opaque;
1795
Anthony Liguori909cda12011-08-15 11:17:31 -05001796 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001797 return s->max_size;
1798}
1799
1800static void win_chr_readfile(CharDriverState *chr)
1801{
1802 WinCharState *s = chr->opaque;
1803 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301804 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001805 DWORD size;
1806
1807 ZeroMemory(&s->orecv, sizeof(s->orecv));
1808 s->orecv.hEvent = s->hrecv;
1809 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1810 if (!ret) {
1811 err = GetLastError();
1812 if (err == ERROR_IO_PENDING) {
1813 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1814 }
1815 }
1816
1817 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001818 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001819 }
1820}
1821
1822static void win_chr_read(CharDriverState *chr)
1823{
1824 WinCharState *s = chr->opaque;
1825
1826 if (s->len > s->max_size)
1827 s->len = s->max_size;
1828 if (s->len == 0)
1829 return;
1830
1831 win_chr_readfile(chr);
1832}
1833
1834static int win_chr_poll(void *opaque)
1835{
1836 CharDriverState *chr = opaque;
1837 WinCharState *s = chr->opaque;
1838 COMSTAT status;
1839 DWORD comerr;
1840
1841 ClearCommError(s->hcom, &comerr, &status);
1842 if (status.cbInQue > 0) {
1843 s->len = status.cbInQue;
1844 win_chr_read_poll(chr);
1845 win_chr_read(chr);
1846 return 1;
1847 }
1848 return 0;
1849}
1850
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001851static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001852{
1853 CharDriverState *chr;
1854 WinCharState *s;
1855
Anthony Liguori7267c092011-08-20 22:09:37 -05001856 chr = g_malloc0(sizeof(CharDriverState));
1857 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001858 chr->opaque = s;
1859 chr->chr_write = win_chr_write;
1860 chr->chr_close = win_chr_close;
1861
1862 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001863 g_free(s);
1864 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001865 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001866 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001867 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001868 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001869}
1870
1871static int win_chr_pipe_poll(void *opaque)
1872{
1873 CharDriverState *chr = opaque;
1874 WinCharState *s = chr->opaque;
1875 DWORD size;
1876
1877 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1878 if (size > 0) {
1879 s->len = size;
1880 win_chr_read_poll(chr);
1881 win_chr_read(chr);
1882 return 1;
1883 }
1884 return 0;
1885}
1886
1887static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1888{
1889 WinCharState *s = chr->opaque;
1890 OVERLAPPED ov;
1891 int ret;
1892 DWORD size;
1893 char openname[256];
1894
1895 s->fpipe = TRUE;
1896
1897 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1898 if (!s->hsend) {
1899 fprintf(stderr, "Failed CreateEvent\n");
1900 goto fail;
1901 }
1902 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1903 if (!s->hrecv) {
1904 fprintf(stderr, "Failed CreateEvent\n");
1905 goto fail;
1906 }
1907
1908 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1909 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1910 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1911 PIPE_WAIT,
1912 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1913 if (s->hcom == INVALID_HANDLE_VALUE) {
1914 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1915 s->hcom = NULL;
1916 goto fail;
1917 }
1918
1919 ZeroMemory(&ov, sizeof(ov));
1920 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1921 ret = ConnectNamedPipe(s->hcom, &ov);
1922 if (ret) {
1923 fprintf(stderr, "Failed ConnectNamedPipe\n");
1924 goto fail;
1925 }
1926
1927 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1928 if (!ret) {
1929 fprintf(stderr, "Failed GetOverlappedResult\n");
1930 if (ov.hEvent) {
1931 CloseHandle(ov.hEvent);
1932 ov.hEvent = NULL;
1933 }
1934 goto fail;
1935 }
1936
1937 if (ov.hEvent) {
1938 CloseHandle(ov.hEvent);
1939 ov.hEvent = NULL;
1940 }
1941 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1942 return 0;
1943
1944 fail:
1945 win_chr_close(chr);
1946 return -1;
1947}
1948
1949
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001950static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001951{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001952 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001953 CharDriverState *chr;
1954 WinCharState *s;
1955
Anthony Liguori7267c092011-08-20 22:09:37 -05001956 chr = g_malloc0(sizeof(CharDriverState));
1957 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001958 chr->opaque = s;
1959 chr->chr_write = win_chr_write;
1960 chr->chr_close = win_chr_close;
1961
1962 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001963 g_free(s);
1964 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001965 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001966 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001967 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001968 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001969}
1970
Markus Armbruster1f514702012-02-07 15:09:08 +01001971static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001972{
1973 CharDriverState *chr;
1974 WinCharState *s;
1975
Anthony Liguori7267c092011-08-20 22:09:37 -05001976 chr = g_malloc0(sizeof(CharDriverState));
1977 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001978 s->hcom = fd_out;
1979 chr->opaque = s;
1980 chr->chr_write = win_chr_write;
Hans de Goedefee204f2013-03-26 11:07:54 +01001981 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001982 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001983}
1984
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001985static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001986{
Markus Armbruster1f514702012-02-07 15:09:08 +01001987 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001988}
1989
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001990static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1991{
1992 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1993 DWORD dwSize;
1994 int len1;
1995
1996 len1 = len;
1997
1998 while (len1 > 0) {
1999 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2000 break;
2001 }
2002 buf += dwSize;
2003 len1 -= dwSize;
2004 }
2005
2006 return len - len1;
2007}
2008
2009static void win_stdio_wait_func(void *opaque)
2010{
2011 CharDriverState *chr = opaque;
2012 WinStdioCharState *stdio = chr->opaque;
2013 INPUT_RECORD buf[4];
2014 int ret;
2015 DWORD dwSize;
2016 int i;
2017
2018 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2019 &dwSize);
2020
2021 if (!ret) {
2022 /* Avoid error storm */
2023 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2024 return;
2025 }
2026
2027 for (i = 0; i < dwSize; i++) {
2028 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2029
2030 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2031 int j;
2032 if (kev->uChar.AsciiChar != 0) {
2033 for (j = 0; j < kev->wRepeatCount; j++) {
2034 if (qemu_chr_be_can_write(chr)) {
2035 uint8_t c = kev->uChar.AsciiChar;
2036 qemu_chr_be_write(chr, &c, 1);
2037 }
2038 }
2039 }
2040 }
2041 }
2042}
2043
2044static DWORD WINAPI win_stdio_thread(LPVOID param)
2045{
2046 CharDriverState *chr = param;
2047 WinStdioCharState *stdio = chr->opaque;
2048 int ret;
2049 DWORD dwSize;
2050
2051 while (1) {
2052
2053 /* Wait for one byte */
2054 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2055
2056 /* Exit in case of error, continue if nothing read */
2057 if (!ret) {
2058 break;
2059 }
2060 if (!dwSize) {
2061 continue;
2062 }
2063
2064 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2065 if (stdio->win_stdio_buf == '\r') {
2066 continue;
2067 }
2068
2069 /* Signal the main thread and wait until the byte was eaten */
2070 if (!SetEvent(stdio->hInputReadyEvent)) {
2071 break;
2072 }
2073 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2074 != WAIT_OBJECT_0) {
2075 break;
2076 }
2077 }
2078
2079 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2080 return 0;
2081}
2082
2083static void win_stdio_thread_wait_func(void *opaque)
2084{
2085 CharDriverState *chr = opaque;
2086 WinStdioCharState *stdio = chr->opaque;
2087
2088 if (qemu_chr_be_can_write(chr)) {
2089 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2090 }
2091
2092 SetEvent(stdio->hInputDoneEvent);
2093}
2094
2095static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2096{
2097 WinStdioCharState *stdio = chr->opaque;
2098 DWORD dwMode = 0;
2099
2100 GetConsoleMode(stdio->hStdIn, &dwMode);
2101
2102 if (echo) {
2103 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2104 } else {
2105 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2106 }
2107}
2108
2109static void win_stdio_close(CharDriverState *chr)
2110{
2111 WinStdioCharState *stdio = chr->opaque;
2112
2113 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2114 CloseHandle(stdio->hInputReadyEvent);
2115 }
2116 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2117 CloseHandle(stdio->hInputDoneEvent);
2118 }
2119 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2120 TerminateThread(stdio->hInputThread, 0);
2121 }
2122
2123 g_free(chr->opaque);
2124 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002125}
2126
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002127static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002128{
2129 CharDriverState *chr;
2130 WinStdioCharState *stdio;
2131 DWORD dwMode;
2132 int is_console = 0;
2133
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002134 chr = g_malloc0(sizeof(CharDriverState));
2135 stdio = g_malloc0(sizeof(WinStdioCharState));
2136
2137 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2138 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2139 fprintf(stderr, "cannot open stdio: invalid handle\n");
2140 exit(1);
2141 }
2142
2143 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2144
2145 chr->opaque = stdio;
2146 chr->chr_write = win_stdio_write;
2147 chr->chr_close = win_stdio_close;
2148
Anthony Liguoried7a1542013-03-05 23:21:17 +05302149 if (is_console) {
2150 if (qemu_add_wait_object(stdio->hStdIn,
2151 win_stdio_wait_func, chr)) {
2152 fprintf(stderr, "qemu_add_wait_object: failed\n");
2153 }
2154 } else {
2155 DWORD dwId;
2156
2157 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2158 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2159 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2160 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002161
Anthony Liguoried7a1542013-03-05 23:21:17 +05302162 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2163 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2164 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2165 fprintf(stderr, "cannot create stdio thread or event\n");
2166 exit(1);
2167 }
2168 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2169 win_stdio_thread_wait_func, chr)) {
2170 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002171 }
2172 }
2173
2174 dwMode |= ENABLE_LINE_INPUT;
2175
Anthony Liguoried7a1542013-03-05 23:21:17 +05302176 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002177 /* set the terminal in raw mode */
2178 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2179 dwMode |= ENABLE_PROCESSED_INPUT;
2180 }
2181
2182 SetConsoleMode(stdio->hStdIn, dwMode);
2183
2184 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2185 qemu_chr_fe_set_echo(chr, false);
2186
Markus Armbruster1f514702012-02-07 15:09:08 +01002187 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002188}
aliguori6f97dba2008-10-31 18:49:55 +00002189#endif /* !_WIN32 */
2190
Anthony Liguori5ab82112013-03-05 23:21:31 +05302191
aliguori6f97dba2008-10-31 18:49:55 +00002192/***********************************************************/
2193/* UDP Net console */
2194
2195typedef struct {
2196 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302197 GIOChannel *chan;
2198 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302199 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002200 int bufcnt;
2201 int bufptr;
2202 int max_size;
2203} NetCharDriver;
2204
2205static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2206{
2207 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302208 gsize bytes_written;
2209 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002210
Anthony Liguori76a96442013-03-05 23:21:21 +05302211 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2212 if (status == G_IO_STATUS_EOF) {
2213 return 0;
2214 } else if (status != G_IO_STATUS_NORMAL) {
2215 return -1;
2216 }
2217
2218 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002219}
2220
2221static int udp_chr_read_poll(void *opaque)
2222{
2223 CharDriverState *chr = opaque;
2224 NetCharDriver *s = chr->opaque;
2225
Anthony Liguori909cda12011-08-15 11:17:31 -05002226 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002227
2228 /* If there were any stray characters in the queue process them
2229 * first
2230 */
2231 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002232 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002233 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002234 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002235 }
2236 return s->max_size;
2237}
2238
Anthony Liguori76a96442013-03-05 23:21:21 +05302239static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002240{
2241 CharDriverState *chr = opaque;
2242 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302243 gsize bytes_read = 0;
2244 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002245
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002246 if (s->max_size == 0) {
2247 return TRUE;
2248 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302249 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2250 &bytes_read, NULL);
2251 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002252 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302253 if (status != G_IO_STATUS_NORMAL) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002254 if (s->tag) {
2255 g_source_remove(s->tag);
2256 s->tag = 0;
2257 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302258 return FALSE;
2259 }
aliguori6f97dba2008-10-31 18:49:55 +00002260
2261 s->bufptr = 0;
2262 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002263 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002264 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002265 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002266 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302267
2268 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002269}
2270
2271static void udp_chr_update_read_handler(CharDriverState *chr)
2272{
2273 NetCharDriver *s = chr->opaque;
2274
Anthony Liguori76a96442013-03-05 23:21:21 +05302275 if (s->tag) {
2276 g_source_remove(s->tag);
2277 s->tag = 0;
2278 }
2279
2280 if (s->chan) {
2281 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002282 }
2283}
2284
aliguori819f56b2009-03-28 17:58:14 +00002285static void udp_chr_close(CharDriverState *chr)
2286{
2287 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302288 if (s->tag) {
2289 g_source_remove(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002290 s->tag = 0;
Anthony Liguori76a96442013-03-05 23:21:21 +05302291 }
2292 if (s->chan) {
2293 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002294 closesocket(s->fd);
2295 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002296 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002297 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002298}
2299
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002300static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002301{
2302 CharDriverState *chr = NULL;
2303 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002304
Anthony Liguori7267c092011-08-20 22:09:37 -05002305 chr = g_malloc0(sizeof(CharDriverState));
2306 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002307
aliguori6f97dba2008-10-31 18:49:55 +00002308 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302309 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002310 s->bufcnt = 0;
2311 s->bufptr = 0;
2312 chr->opaque = s;
2313 chr->chr_write = udp_chr_write;
2314 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002315 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002316 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002317}
aliguori6f97dba2008-10-31 18:49:55 +00002318
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002319static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2320{
2321 Error *local_err = NULL;
2322 int fd = -1;
2323
2324 fd = inet_dgram_opts(opts, &local_err);
2325 if (fd < 0) {
2326 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002327 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002328 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002329}
2330
2331/***********************************************************/
2332/* TCP Net console */
2333
2334typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302335
2336 GIOChannel *chan, *listen_chan;
2337 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002338 int fd, listen_fd;
2339 int connected;
2340 int max_size;
2341 int do_telnetopt;
2342 int do_nodelay;
2343 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002344 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002345} TCPCharDriver;
2346
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302347static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002348
2349static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2350{
2351 TCPCharDriver *s = chr->opaque;
2352 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002353 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002354 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002355 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002356 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002357 }
2358}
2359
2360static int tcp_chr_read_poll(void *opaque)
2361{
2362 CharDriverState *chr = opaque;
2363 TCPCharDriver *s = chr->opaque;
2364 if (!s->connected)
2365 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002366 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002367 return s->max_size;
2368}
2369
2370#define IAC 255
2371#define IAC_BREAK 243
2372static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2373 TCPCharDriver *s,
2374 uint8_t *buf, int *size)
2375{
2376 /* Handle any telnet client's basic IAC options to satisfy char by
2377 * char mode with no echo. All IAC options will be removed from
2378 * the buf and the do_telnetopt variable will be used to track the
2379 * state of the width of the IAC information.
2380 *
2381 * IAC commands come in sets of 3 bytes with the exception of the
2382 * "IAC BREAK" command and the double IAC.
2383 */
2384
2385 int i;
2386 int j = 0;
2387
2388 for (i = 0; i < *size; i++) {
2389 if (s->do_telnetopt > 1) {
2390 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2391 /* Double IAC means send an IAC */
2392 if (j != i)
2393 buf[j] = buf[i];
2394 j++;
2395 s->do_telnetopt = 1;
2396 } else {
2397 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2398 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002399 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002400 s->do_telnetopt++;
2401 }
2402 s->do_telnetopt++;
2403 }
2404 if (s->do_telnetopt >= 4) {
2405 s->do_telnetopt = 1;
2406 }
2407 } else {
2408 if ((unsigned char)buf[i] == IAC) {
2409 s->do_telnetopt = 2;
2410 } else {
2411 if (j != i)
2412 buf[j] = buf[i];
2413 j++;
2414 }
2415 }
2416 }
2417 *size = j;
2418}
2419
Mark McLoughlin7d174052009-07-22 09:11:39 +01002420static int tcp_get_msgfd(CharDriverState *chr)
2421{
2422 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002423 int fd = s->msgfd;
2424 s->msgfd = -1;
2425 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002426}
2427
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002428#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002429static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2430{
2431 TCPCharDriver *s = chr->opaque;
2432 struct cmsghdr *cmsg;
2433
2434 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2435 int fd;
2436
2437 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2438 cmsg->cmsg_level != SOL_SOCKET ||
2439 cmsg->cmsg_type != SCM_RIGHTS)
2440 continue;
2441
2442 fd = *((int *)CMSG_DATA(cmsg));
2443 if (fd < 0)
2444 continue;
2445
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002446 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2447 qemu_set_block(fd);
2448
Corey Bryant06138652012-08-14 16:43:42 -04002449#ifndef MSG_CMSG_CLOEXEC
2450 qemu_set_cloexec(fd);
2451#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002452 if (s->msgfd != -1)
2453 close(s->msgfd);
2454 s->msgfd = fd;
2455 }
2456}
2457
Mark McLoughlin9977c892009-07-22 09:11:38 +01002458static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2459{
2460 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002461 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002462 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002463 union {
2464 struct cmsghdr cmsg;
2465 char control[CMSG_SPACE(sizeof(int))];
2466 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002467 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002468 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002469
2470 iov[0].iov_base = buf;
2471 iov[0].iov_len = len;
2472
2473 msg.msg_iov = iov;
2474 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002475 msg.msg_control = &msg_control;
2476 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002477
Corey Bryant06138652012-08-14 16:43:42 -04002478#ifdef MSG_CMSG_CLOEXEC
2479 flags |= MSG_CMSG_CLOEXEC;
2480#endif
2481 ret = recvmsg(s->fd, &msg, flags);
2482 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002483 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002484 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002485
2486 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002487}
2488#else
2489static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2490{
2491 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002492 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002493}
2494#endif
2495
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302496static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2497{
2498 TCPCharDriver *s = chr->opaque;
2499 return g_io_create_watch(s->chan, cond);
2500}
2501
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302502static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002503{
2504 CharDriverState *chr = opaque;
2505 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302506 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002507 int len, size;
2508
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302509 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002510 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302511 }
aliguori6f97dba2008-10-31 18:49:55 +00002512 len = sizeof(buf);
2513 if (len > s->max_size)
2514 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002515 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002516 if (size == 0) {
2517 /* connection closed */
2518 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302519 if (s->listen_chan) {
2520 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002521 }
Paolo Bonzini910b6362013-04-19 17:32:06 +02002522 if (s->tag) {
2523 g_source_remove(s->tag);
2524 s->tag = 0;
2525 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302526 g_io_channel_unref(s->chan);
2527 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002528 closesocket(s->fd);
2529 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002530 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002531 } else if (size > 0) {
2532 if (s->do_telnetopt)
2533 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2534 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002535 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002536 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302537
2538 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002539}
2540
Blue Swirl68c18d12010-08-15 09:46:24 +00002541#ifndef _WIN32
2542CharDriverState *qemu_chr_open_eventfd(int eventfd)
2543{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002544 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002545}
Blue Swirl68c18d12010-08-15 09:46:24 +00002546#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002547
aliguori6f97dba2008-10-31 18:49:55 +00002548static void tcp_chr_connect(void *opaque)
2549{
2550 CharDriverState *chr = opaque;
2551 TCPCharDriver *s = chr->opaque;
2552
2553 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302554 if (s->chan) {
2555 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002556 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002557 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002558}
2559
2560#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2561static void tcp_chr_telnet_init(int fd)
2562{
2563 char buf[3];
2564 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2565 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2566 send(fd, (char *)buf, 3, 0);
2567 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2568 send(fd, (char *)buf, 3, 0);
2569 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2570 send(fd, (char *)buf, 3, 0);
2571 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2572 send(fd, (char *)buf, 3, 0);
2573}
2574
Daniel P. Berrange13661082011-06-23 13:31:42 +01002575static int tcp_chr_add_client(CharDriverState *chr, int fd)
2576{
2577 TCPCharDriver *s = chr->opaque;
2578 if (s->fd != -1)
2579 return -1;
2580
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002581 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002582 if (s->do_nodelay)
2583 socket_set_nodelay(fd);
2584 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302585 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002586 if (s->listen_tag) {
2587 g_source_remove(s->listen_tag);
2588 s->listen_tag = 0;
2589 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002590 tcp_chr_connect(chr);
2591
2592 return 0;
2593}
2594
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302595static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002596{
2597 CharDriverState *chr = opaque;
2598 TCPCharDriver *s = chr->opaque;
2599 struct sockaddr_in saddr;
2600#ifndef _WIN32
2601 struct sockaddr_un uaddr;
2602#endif
2603 struct sockaddr *addr;
2604 socklen_t len;
2605 int fd;
2606
2607 for(;;) {
2608#ifndef _WIN32
2609 if (s->is_unix) {
2610 len = sizeof(uaddr);
2611 addr = (struct sockaddr *)&uaddr;
2612 } else
2613#endif
2614 {
2615 len = sizeof(saddr);
2616 addr = (struct sockaddr *)&saddr;
2617 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002618 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002619 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302620 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002621 } else if (fd >= 0) {
2622 if (s->do_telnetopt)
2623 tcp_chr_telnet_init(fd);
2624 break;
2625 }
2626 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002627 if (tcp_chr_add_client(chr, fd) < 0)
2628 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302629
2630 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002631}
2632
2633static void tcp_chr_close(CharDriverState *chr)
2634{
2635 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002636 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302637 if (s->tag) {
2638 g_source_remove(s->tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002639 s->tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302640 }
2641 if (s->chan) {
2642 g_io_channel_unref(s->chan);
2643 }
aliguori6f97dba2008-10-31 18:49:55 +00002644 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002645 }
2646 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302647 if (s->listen_tag) {
2648 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002649 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302650 }
2651 if (s->listen_chan) {
2652 g_io_channel_unref(s->listen_chan);
2653 }
aliguori6f97dba2008-10-31 18:49:55 +00002654 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002655 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002656 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002657 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002658}
2659
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002660static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2661 bool is_listen, bool is_telnet,
2662 bool is_waitconnect,
2663 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002664{
2665 CharDriverState *chr = NULL;
2666 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002667 char host[NI_MAXHOST], serv[NI_MAXSERV];
2668 const char *left = "", *right = "";
2669 struct sockaddr_storage ss;
2670 socklen_t ss_len = sizeof(ss);
2671
2672 memset(&ss, 0, ss_len);
2673 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2674 error_setg(errp, "getsockname: %s", strerror(errno));
2675 return NULL;
2676 }
2677
2678 chr = g_malloc0(sizeof(CharDriverState));
2679 s = g_malloc0(sizeof(TCPCharDriver));
2680
2681 s->connected = 0;
2682 s->fd = -1;
2683 s->listen_fd = -1;
2684 s->msgfd = -1;
2685
2686 chr->filename = g_malloc(256);
2687 switch (ss.ss_family) {
2688#ifndef _WIN32
2689 case AF_UNIX:
2690 s->is_unix = 1;
2691 snprintf(chr->filename, 256, "unix:%s%s",
2692 ((struct sockaddr_un *)(&ss))->sun_path,
2693 is_listen ? ",server" : "");
2694 break;
2695#endif
2696 case AF_INET6:
2697 left = "[";
2698 right = "]";
2699 /* fall through */
2700 case AF_INET:
2701 s->do_nodelay = do_nodelay;
2702 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2703 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002704 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002705 is_telnet ? "telnet" : "tcp",
2706 left, host, right, serv,
2707 is_listen ? ",server" : "");
2708 break;
2709 }
2710
2711 chr->opaque = s;
2712 chr->chr_write = tcp_chr_write;
2713 chr->chr_close = tcp_chr_close;
2714 chr->get_msgfd = tcp_get_msgfd;
2715 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302716 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002717
2718 if (is_listen) {
2719 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302720 s->listen_chan = io_channel_from_socket(s->listen_fd);
2721 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002722 if (is_telnet) {
2723 s->do_telnetopt = 1;
2724 }
2725 } else {
2726 s->connected = 1;
2727 s->fd = fd;
2728 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302729 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002730 tcp_chr_connect(chr);
2731 }
2732
2733 if (is_listen && is_waitconnect) {
2734 printf("QEMU waiting for connection on: %s\n",
2735 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302736 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002737 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002738 }
2739 return chr;
2740}
2741
2742static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2743{
2744 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002745 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002746 int fd = -1;
2747 int is_listen;
2748 int is_waitconnect;
2749 int do_nodelay;
2750 int is_unix;
2751 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002752
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002753 is_listen = qemu_opt_get_bool(opts, "server", 0);
2754 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2755 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2756 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2757 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002758 if (!is_listen)
2759 is_waitconnect = 0;
2760
aliguorif07b6002008-11-11 20:54:09 +00002761 if (is_unix) {
2762 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002763 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002764 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002765 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002766 }
2767 } else {
2768 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002769 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002770 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002771 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002772 }
2773 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002774 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002775 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002776 }
aliguori6f97dba2008-10-31 18:49:55 +00002777
2778 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002779 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002780
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002781 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2782 is_waitconnect, &local_err);
2783 if (error_is_set(&local_err)) {
2784 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002785 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002786 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002787
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002788
aliguori6f97dba2008-10-31 18:49:55 +00002789 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002790 if (local_err) {
2791 qerror_report_err(local_err);
2792 error_free(local_err);
2793 }
2794 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002795 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002796 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002797 if (chr) {
2798 g_free(chr->opaque);
2799 g_free(chr);
2800 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002801 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002802}
2803
Lei Li51767e72013-01-25 00:03:19 +08002804/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002805/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002806
2807typedef struct {
2808 size_t size;
2809 size_t prod;
2810 size_t cons;
2811 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002812} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002813
Markus Armbruster3949e592013-02-06 21:27:24 +01002814static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002815{
Markus Armbruster3949e592013-02-06 21:27:24 +01002816 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002817
Markus Armbruster5c230102013-02-06 21:27:23 +01002818 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002819}
2820
Markus Armbruster3949e592013-02-06 21:27:24 +01002821static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002822{
Markus Armbruster3949e592013-02-06 21:27:24 +01002823 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002824 int i;
2825
2826 if (!buf || (len < 0)) {
2827 return -1;
2828 }
2829
2830 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002831 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2832 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002833 d->cons = d->prod - d->size;
2834 }
2835 }
2836
2837 return 0;
2838}
2839
Markus Armbruster3949e592013-02-06 21:27:24 +01002840static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002841{
Markus Armbruster3949e592013-02-06 21:27:24 +01002842 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002843 int i;
2844
Markus Armbruster5c230102013-02-06 21:27:23 +01002845 for (i = 0; i < len && d->cons != d->prod; i++) {
2846 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002847 }
2848
2849 return i;
2850}
2851
Markus Armbruster3949e592013-02-06 21:27:24 +01002852static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002853{
Markus Armbruster3949e592013-02-06 21:27:24 +01002854 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002855
2856 g_free(d->cbuf);
2857 g_free(d);
2858 chr->opaque = NULL;
2859}
2860
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002861static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2862 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002863{
2864 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002865 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002866
2867 chr = g_malloc0(sizeof(CharDriverState));
2868 d = g_malloc(sizeof(*d));
2869
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002870 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002871
2872 /* The size must be power of 2 */
2873 if (d->size & (d->size - 1)) {
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002874 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002875 goto fail;
2876 }
2877
2878 d->prod = 0;
2879 d->cons = 0;
2880 d->cbuf = g_malloc0(d->size);
2881
2882 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002883 chr->chr_write = ringbuf_chr_write;
2884 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002885
2886 return chr;
2887
2888fail:
2889 g_free(d);
2890 g_free(chr);
2891 return NULL;
2892}
2893
Markus Armbruster3949e592013-02-06 21:27:24 +01002894static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002895{
Markus Armbruster3949e592013-02-06 21:27:24 +01002896 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002897}
2898
Markus Armbruster3949e592013-02-06 21:27:24 +01002899void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002900 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002901 Error **errp)
2902{
2903 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002904 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002905 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002906 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002907
2908 chr = qemu_chr_find(device);
2909 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002910 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002911 return;
2912 }
2913
Markus Armbruster3949e592013-02-06 21:27:24 +01002914 if (!chr_is_ringbuf(chr)) {
2915 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002916 return;
2917 }
2918
Lei Li1f590cf2013-01-25 00:03:20 +08002919 if (has_format && (format == DATA_FORMAT_BASE64)) {
2920 write_data = g_base64_decode(data, &write_count);
2921 } else {
2922 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002923 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002924 }
2925
Markus Armbruster3949e592013-02-06 21:27:24 +01002926 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002927
Markus Armbruster13289fb2013-02-06 21:27:18 +01002928 if (write_data != (uint8_t *)data) {
2929 g_free((void *)write_data);
2930 }
2931
Lei Li1f590cf2013-01-25 00:03:20 +08002932 if (ret < 0) {
2933 error_setg(errp, "Failed to write to device %s", device);
2934 return;
2935 }
2936}
2937
Markus Armbruster3949e592013-02-06 21:27:24 +01002938char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002939 bool has_format, enum DataFormat format,
2940 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002941{
2942 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002943 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002944 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002945 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002946
2947 chr = qemu_chr_find(device);
2948 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002949 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002950 return NULL;
2951 }
2952
Markus Armbruster3949e592013-02-06 21:27:24 +01002953 if (!chr_is_ringbuf(chr)) {
2954 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002955 return NULL;
2956 }
2957
2958 if (size <= 0) {
2959 error_setg(errp, "size must be greater than zero");
2960 return NULL;
2961 }
2962
Markus Armbruster3949e592013-02-06 21:27:24 +01002963 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002964 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002965 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002966
Markus Armbruster3949e592013-02-06 21:27:24 +01002967 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002968
2969 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002970 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002971 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002972 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002973 /*
2974 * FIXME should read only complete, valid UTF-8 characters up
2975 * to @size bytes. Invalid sequences should be replaced by a
2976 * suitable replacement character. Except when (and only
2977 * when) ring buffer lost characters since last read, initial
2978 * continuation characters should be dropped.
2979 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002980 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002981 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002982 }
2983
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002984 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002985}
2986
Gerd Hoffmann33521632009-12-08 13:11:49 +01002987QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002988{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002989 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002990 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002991 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002992 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002993 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002994
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002995 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2996 if (error_is_set(&local_err)) {
2997 qerror_report_err(local_err);
2998 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002999 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003000 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003001
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003002 if (strstart(filename, "mon:", &p)) {
3003 filename = p;
3004 qemu_opt_set(opts, "mux", "on");
3005 }
3006
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003007 if (strcmp(filename, "null") == 0 ||
3008 strcmp(filename, "pty") == 0 ||
3009 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003010 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003011 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003012 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003013 return opts;
3014 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003015 if (strstart(filename, "vc", &p)) {
3016 qemu_opt_set(opts, "backend", "vc");
3017 if (*p == ':') {
3018 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
3019 /* pixels */
3020 qemu_opt_set(opts, "width", width);
3021 qemu_opt_set(opts, "height", height);
3022 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
3023 /* chars */
3024 qemu_opt_set(opts, "cols", width);
3025 qemu_opt_set(opts, "rows", height);
3026 } else {
3027 goto fail;
3028 }
3029 }
3030 return opts;
3031 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003032 if (strcmp(filename, "con:") == 0) {
3033 qemu_opt_set(opts, "backend", "console");
3034 return opts;
3035 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003036 if (strstart(filename, "COM", NULL)) {
3037 qemu_opt_set(opts, "backend", "serial");
3038 qemu_opt_set(opts, "path", filename);
3039 return opts;
3040 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003041 if (strstart(filename, "file:", &p)) {
3042 qemu_opt_set(opts, "backend", "file");
3043 qemu_opt_set(opts, "path", p);
3044 return opts;
3045 }
3046 if (strstart(filename, "pipe:", &p)) {
3047 qemu_opt_set(opts, "backend", "pipe");
3048 qemu_opt_set(opts, "path", p);
3049 return opts;
3050 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003051 if (strstart(filename, "tcp:", &p) ||
3052 strstart(filename, "telnet:", &p)) {
3053 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3054 host[0] = 0;
3055 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3056 goto fail;
3057 }
3058 qemu_opt_set(opts, "backend", "socket");
3059 qemu_opt_set(opts, "host", host);
3060 qemu_opt_set(opts, "port", port);
3061 if (p[pos] == ',') {
3062 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3063 goto fail;
3064 }
3065 if (strstart(filename, "telnet:", &p))
3066 qemu_opt_set(opts, "telnet", "on");
3067 return opts;
3068 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003069 if (strstart(filename, "udp:", &p)) {
3070 qemu_opt_set(opts, "backend", "udp");
3071 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3072 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003073 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003074 goto fail;
3075 }
3076 }
3077 qemu_opt_set(opts, "host", host);
3078 qemu_opt_set(opts, "port", port);
3079 if (p[pos] == '@') {
3080 p += pos + 1;
3081 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3082 host[0] = 0;
3083 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003084 goto fail;
3085 }
3086 }
3087 qemu_opt_set(opts, "localaddr", host);
3088 qemu_opt_set(opts, "localport", port);
3089 }
3090 return opts;
3091 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003092 if (strstart(filename, "unix:", &p)) {
3093 qemu_opt_set(opts, "backend", "socket");
3094 if (qemu_opts_do_parse(opts, p, "path") != 0)
3095 goto fail;
3096 return opts;
3097 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003098 if (strstart(filename, "/dev/parport", NULL) ||
3099 strstart(filename, "/dev/ppi", NULL)) {
3100 qemu_opt_set(opts, "backend", "parport");
3101 qemu_opt_set(opts, "path", filename);
3102 return opts;
3103 }
3104 if (strstart(filename, "/dev/", NULL)) {
3105 qemu_opt_set(opts, "backend", "tty");
3106 qemu_opt_set(opts, "path", filename);
3107 return opts;
3108 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003109
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003110fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003111 qemu_opts_del(opts);
3112 return NULL;
3113}
3114
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003115static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3116 Error **errp)
3117{
3118 const char *path = qemu_opt_get(opts, "path");
3119
3120 if (path == NULL) {
3121 error_setg(errp, "chardev: file: no filename given");
3122 return;
3123 }
3124 backend->file = g_new0(ChardevFile, 1);
3125 backend->file->out = g_strdup(path);
3126}
3127
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003128static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3129 Error **errp)
3130{
3131 backend->stdio = g_new0(ChardevStdio, 1);
3132 backend->stdio->has_signal = true;
3133 backend->stdio->signal =
3134 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3135}
3136
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003137static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3138 Error **errp)
3139{
3140 const char *device = qemu_opt_get(opts, "path");
3141
3142 if (device == NULL) {
3143 error_setg(errp, "chardev: serial/tty: no device path given");
3144 return;
3145 }
3146 backend->serial = g_new0(ChardevHostdev, 1);
3147 backend->serial->device = g_strdup(device);
3148}
3149
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003150static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3151 Error **errp)
3152{
3153 const char *device = qemu_opt_get(opts, "path");
3154
3155 if (device == NULL) {
3156 error_setg(errp, "chardev: parallel: no device path given");
3157 return;
3158 }
3159 backend->parallel = g_new0(ChardevHostdev, 1);
3160 backend->parallel->device = g_strdup(device);
3161}
3162
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003163static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3164 Error **errp)
3165{
3166 const char *device = qemu_opt_get(opts, "path");
3167
3168 if (device == NULL) {
3169 error_setg(errp, "chardev: pipe: no device path given");
3170 return;
3171 }
3172 backend->pipe = g_new0(ChardevHostdev, 1);
3173 backend->pipe->device = g_strdup(device);
3174}
3175
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003176static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3177 Error **errp)
3178{
3179 int val;
3180
3181 backend->memory = g_new0(ChardevRingbuf, 1);
3182
3183 val = qemu_opt_get_number(opts, "size", 0);
3184 if (val != 0) {
3185 backend->memory->has_size = true;
3186 backend->memory->size = val;
3187 }
3188}
3189
Anthony Liguorid654f342013-03-05 23:21:28 +05303190typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003191 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003192 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003193 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003194 /* new, qapi-based */
3195 int kind;
3196 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303197} CharDriver;
3198
3199static GSList *backends;
3200
3201void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3202{
3203 CharDriver *s;
3204
3205 s = g_malloc0(sizeof(*s));
3206 s->name = g_strdup(name);
3207 s->open = open;
3208
3209 backends = g_slist_append(backends, s);
3210}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003211
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003212void register_char_driver_qapi(const char *name, int kind,
3213 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3214{
3215 CharDriver *s;
3216
3217 s = g_malloc0(sizeof(*s));
3218 s->name = g_strdup(name);
3219 s->kind = kind;
3220 s->parse = parse;
3221
3222 backends = g_slist_append(backends, s);
3223}
3224
Anthony Liguorif69554b2011-08-15 11:17:37 -05003225CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003226 void (*init)(struct CharDriverState *s),
3227 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003228{
Anthony Liguorid654f342013-03-05 23:21:28 +05303229 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003230 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303231 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003232
3233 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003234 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003235 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003236 }
3237
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003238 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003239 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003240 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003241 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003242 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303243 for (i = backends; i; i = i->next) {
3244 cd = i->data;
3245
3246 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003247 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303248 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003249 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303250 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003251 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003252 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303253 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003254 }
3255
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003256 if (!cd->open) {
3257 /* using new, qapi init */
3258 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3259 ChardevReturn *ret = NULL;
3260 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003261 const char *bid = NULL;
3262
3263 if (qemu_opt_get_bool(opts, "mux", 0)) {
3264 bid = g_strdup_printf("%s-base", id);
3265 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003266
3267 chr = NULL;
3268 backend->kind = cd->kind;
3269 if (cd->parse) {
3270 cd->parse(opts, backend, errp);
3271 if (error_is_set(errp)) {
3272 goto qapi_out;
3273 }
3274 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003275 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003276 if (error_is_set(errp)) {
3277 goto qapi_out;
3278 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003279
3280 if (bid) {
3281 qapi_free_ChardevBackend(backend);
3282 qapi_free_ChardevReturn(ret);
3283 backend = g_new0(ChardevBackend, 1);
3284 backend->mux = g_new0(ChardevMux, 1);
3285 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3286 backend->mux->chardev = g_strdup(bid);
3287 ret = qmp_chardev_add(id, backend, errp);
3288 if (error_is_set(errp)) {
3289 goto qapi_out;
3290 }
3291 }
3292
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003293 chr = qemu_chr_find(id);
3294
3295 qapi_out:
3296 qapi_free_ChardevBackend(backend);
3297 qapi_free_ChardevReturn(ret);
3298 return chr;
3299 }
3300
Anthony Liguorid654f342013-03-05 23:21:28 +05303301 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003302 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003303 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003304 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003305 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003306 }
3307
3308 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003309 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003310 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003311 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003312
3313 if (qemu_opt_get_bool(opts, "mux", 0)) {
3314 CharDriverState *base = chr;
3315 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003316 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003317 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3318 chr = qemu_chr_open_mux(base);
3319 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003320 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003321 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003322 } else {
3323 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003324 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003325 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003326 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003327 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003328
3329err:
3330 qemu_opts_del(opts);
3331 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003332}
3333
Anthony Liguori27143a42011-08-15 11:17:36 -05003334CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003335{
3336 const char *p;
3337 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003338 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003339 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003340
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003341 if (strstart(filename, "chardev:", &p)) {
3342 return qemu_chr_find(p);
3343 }
3344
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003345 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003346 if (!opts)
3347 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003348
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003349 chr = qemu_chr_new_from_opts(opts, init, &err);
3350 if (error_is_set(&err)) {
3351 fprintf(stderr, "%s\n", error_get_pretty(err));
3352 error_free(err);
3353 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003354 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003355 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003356 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003357 }
3358 return chr;
3359}
3360
Anthony Liguori15f31512011-08-15 11:17:35 -05003361void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003362{
3363 if (chr->chr_set_echo) {
3364 chr->chr_set_echo(chr, echo);
3365 }
3366}
3367
Hans de Goede8e25daa2013-03-26 11:07:57 +01003368void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003369{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003370 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003371 return;
3372 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003373 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003374 if (chr->chr_set_fe_open) {
3375 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003376 }
3377}
3378
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003379int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3380 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303381{
3382 GSource *src;
3383 guint tag;
3384
3385 if (s->chr_add_watch == NULL) {
3386 return -ENOSYS;
3387 }
3388
3389 src = s->chr_add_watch(s, cond);
3390 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3391 tag = g_source_attach(src, NULL);
3392 g_source_unref(src);
3393
3394 return tag;
3395}
3396
Hans de Goede44c473d2013-03-27 20:29:39 +01003397int qemu_chr_fe_claim(CharDriverState *s)
3398{
3399 if (s->avail_connections < 1) {
3400 return -1;
3401 }
3402 s->avail_connections--;
3403 return 0;
3404}
3405
3406void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3407{
3408 if (qemu_chr_fe_claim(s) != 0) {
3409 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3410 __func__, s->label);
3411 exit(1);
3412 }
3413}
3414
3415void qemu_chr_fe_release(CharDriverState *s)
3416{
3417 s->avail_connections++;
3418}
3419
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003420void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003421{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003422 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003423 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003424 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003425 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003426 g_free(chr->filename);
3427 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003428 if (chr->opts) {
3429 qemu_opts_del(chr->opts);
3430 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003431 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003432}
3433
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003434ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003435{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003436 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003437 CharDriverState *chr;
3438
Blue Swirl72cf2d42009-09-12 07:36:22 +00003439 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003440 ChardevInfoList *info = g_malloc0(sizeof(*info));
3441 info->value = g_malloc0(sizeof(*info->value));
3442 info->value->label = g_strdup(chr->label);
3443 info->value->filename = g_strdup(chr->filename);
3444
3445 info->next = chr_list;
3446 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003447 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003448
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003449 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003450}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003451
3452CharDriverState *qemu_chr_find(const char *name)
3453{
3454 CharDriverState *chr;
3455
Blue Swirl72cf2d42009-09-12 07:36:22 +00003456 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003457 if (strcmp(chr->label, name) != 0)
3458 continue;
3459 return chr;
3460 }
3461 return NULL;
3462}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003463
3464/* Get a character (serial) device interface. */
3465CharDriverState *qemu_char_get_next_serial(void)
3466{
3467 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003468 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003469
3470 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003471
3472 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3473 chr = serial_hds[next_serial++];
3474 qemu_chr_fe_claim_no_fail(chr);
3475 return chr;
3476 }
3477 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003478}
3479
Paolo Bonzini4d454572012-11-26 16:03:42 +01003480QemuOptsList qemu_chardev_opts = {
3481 .name = "chardev",
3482 .implied_opt_name = "backend",
3483 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3484 .desc = {
3485 {
3486 .name = "backend",
3487 .type = QEMU_OPT_STRING,
3488 },{
3489 .name = "path",
3490 .type = QEMU_OPT_STRING,
3491 },{
3492 .name = "host",
3493 .type = QEMU_OPT_STRING,
3494 },{
3495 .name = "port",
3496 .type = QEMU_OPT_STRING,
3497 },{
3498 .name = "localaddr",
3499 .type = QEMU_OPT_STRING,
3500 },{
3501 .name = "localport",
3502 .type = QEMU_OPT_STRING,
3503 },{
3504 .name = "to",
3505 .type = QEMU_OPT_NUMBER,
3506 },{
3507 .name = "ipv4",
3508 .type = QEMU_OPT_BOOL,
3509 },{
3510 .name = "ipv6",
3511 .type = QEMU_OPT_BOOL,
3512 },{
3513 .name = "wait",
3514 .type = QEMU_OPT_BOOL,
3515 },{
3516 .name = "server",
3517 .type = QEMU_OPT_BOOL,
3518 },{
3519 .name = "delay",
3520 .type = QEMU_OPT_BOOL,
3521 },{
3522 .name = "telnet",
3523 .type = QEMU_OPT_BOOL,
3524 },{
3525 .name = "width",
3526 .type = QEMU_OPT_NUMBER,
3527 },{
3528 .name = "height",
3529 .type = QEMU_OPT_NUMBER,
3530 },{
3531 .name = "cols",
3532 .type = QEMU_OPT_NUMBER,
3533 },{
3534 .name = "rows",
3535 .type = QEMU_OPT_NUMBER,
3536 },{
3537 .name = "mux",
3538 .type = QEMU_OPT_BOOL,
3539 },{
3540 .name = "signal",
3541 .type = QEMU_OPT_BOOL,
3542 },{
3543 .name = "name",
3544 .type = QEMU_OPT_STRING,
3545 },{
3546 .name = "debug",
3547 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003548 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003549 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003550 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003551 },
3552 { /* end of list */ }
3553 },
3554};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003555
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003556#ifdef _WIN32
3557
3558static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3559{
3560 HANDLE out;
3561
3562 if (file->in) {
3563 error_setg(errp, "input file not supported");
3564 return NULL;
3565 }
3566
3567 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3568 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3569 if (out == INVALID_HANDLE_VALUE) {
3570 error_setg(errp, "open %s failed", file->out);
3571 return NULL;
3572 }
3573 return qemu_chr_open_win_file(out);
3574}
3575
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003576static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3577 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003578{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003579 return qemu_chr_open_win_path(serial->device);
3580}
3581
3582static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3583 Error **errp)
3584{
3585 error_setg(errp, "character device backend type 'parallel' not supported");
3586 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003587}
3588
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003589#else /* WIN32 */
3590
3591static int qmp_chardev_open_file_source(char *src, int flags,
3592 Error **errp)
3593{
3594 int fd = -1;
3595
3596 TFR(fd = qemu_open(src, flags, 0666));
3597 if (fd == -1) {
3598 error_setg(errp, "open %s: %s", src, strerror(errno));
3599 }
3600 return fd;
3601}
3602
3603static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3604{
3605 int flags, in = -1, out = -1;
3606
3607 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3608 out = qmp_chardev_open_file_source(file->out, flags, errp);
3609 if (error_is_set(errp)) {
3610 return NULL;
3611 }
3612
3613 if (file->in) {
3614 flags = O_RDONLY;
3615 in = qmp_chardev_open_file_source(file->in, flags, errp);
3616 if (error_is_set(errp)) {
3617 qemu_close(out);
3618 return NULL;
3619 }
3620 }
3621
3622 return qemu_chr_open_fd(in, out);
3623}
3624
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003625static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3626 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003627{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003628#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003629 int fd;
3630
3631 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3632 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003633 return NULL;
3634 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003635 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003636 return qemu_chr_open_tty_fd(fd);
3637#else
3638 error_setg(errp, "character device backend type 'serial' not supported");
3639 return NULL;
3640#endif
3641}
3642
3643static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3644 Error **errp)
3645{
3646#ifdef HAVE_CHARDEV_PARPORT
3647 int fd;
3648
3649 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3650 if (error_is_set(errp)) {
3651 return NULL;
3652 }
3653 return qemu_chr_open_pp_fd(fd);
3654#else
3655 error_setg(errp, "character device backend type 'parallel' not supported");
3656 return NULL;
3657#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003658}
3659
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003660#endif /* WIN32 */
3661
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003662static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3663 Error **errp)
3664{
3665 SocketAddress *addr = sock->addr;
3666 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3667 bool is_listen = sock->has_server ? sock->server : true;
3668 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3669 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3670 int fd;
3671
3672 if (is_listen) {
3673 fd = socket_listen(addr, errp);
3674 } else {
3675 fd = socket_connect(addr, errp, NULL, NULL);
3676 }
3677 if (error_is_set(errp)) {
3678 return NULL;
3679 }
3680 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3681 is_telnet, is_waitconnect, errp);
3682}
3683
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003684static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3685 Error **errp)
3686{
3687 int fd;
3688
3689 fd = socket_dgram(dgram->remote, dgram->local, errp);
3690 if (error_is_set(errp)) {
3691 return NULL;
3692 }
3693 return qemu_chr_open_udp_fd(fd);
3694}
3695
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003696ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3697 Error **errp)
3698{
3699 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003700 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003701
3702 chr = qemu_chr_find(id);
3703 if (chr) {
3704 error_setg(errp, "Chardev '%s' already exists", id);
3705 g_free(ret);
3706 return NULL;
3707 }
3708
3709 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003710 case CHARDEV_BACKEND_KIND_FILE:
3711 chr = qmp_chardev_open_file(backend->file, errp);
3712 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003713 case CHARDEV_BACKEND_KIND_SERIAL:
3714 chr = qmp_chardev_open_serial(backend->serial, errp);
3715 break;
3716 case CHARDEV_BACKEND_KIND_PARALLEL:
3717 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003718 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003719 case CHARDEV_BACKEND_KIND_PIPE:
3720 chr = qemu_chr_open_pipe(backend->pipe);
3721 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003722 case CHARDEV_BACKEND_KIND_SOCKET:
3723 chr = qmp_chardev_open_socket(backend->socket, errp);
3724 break;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003725 case CHARDEV_BACKEND_KIND_DGRAM:
3726 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3727 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003728#ifdef HAVE_CHARDEV_TTY
3729 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003730 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003731 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003732#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003733 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003734 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003735 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003736 case CHARDEV_BACKEND_KIND_MUX:
3737 base = qemu_chr_find(backend->mux->chardev);
3738 if (base == NULL) {
3739 error_setg(errp, "mux: base chardev %s not found",
3740 backend->mux->chardev);
3741 break;
3742 }
3743 chr = qemu_chr_open_mux(base);
3744 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003745 case CHARDEV_BACKEND_KIND_MSMOUSE:
3746 chr = qemu_chr_open_msmouse();
3747 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003748#ifdef CONFIG_BRLAPI
3749 case CHARDEV_BACKEND_KIND_BRAILLE:
3750 chr = chr_baum_init();
3751 break;
3752#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003753 case CHARDEV_BACKEND_KIND_STDIO:
3754 chr = qemu_chr_open_stdio(backend->stdio);
3755 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003756#ifdef _WIN32
3757 case CHARDEV_BACKEND_KIND_CONSOLE:
3758 chr = qemu_chr_open_win_con();
3759 break;
3760#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003761#ifdef CONFIG_SPICE
3762 case CHARDEV_BACKEND_KIND_SPICEVMC:
3763 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3764 break;
3765 case CHARDEV_BACKEND_KIND_SPICEPORT:
3766 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3767 break;
3768#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003769 case CHARDEV_BACKEND_KIND_VC:
3770 chr = vc_init(backend->vc);
3771 break;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003772 case CHARDEV_BACKEND_KIND_MEMORY:
3773 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3774 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003775 default:
3776 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3777 break;
3778 }
3779
3780 if (chr == NULL && !error_is_set(errp)) {
3781 error_setg(errp, "Failed to create chardev");
3782 }
3783 if (chr) {
3784 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003785 chr->avail_connections =
3786 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003787 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3788 return ret;
3789 } else {
3790 g_free(ret);
3791 return NULL;
3792 }
3793}
3794
3795void qmp_chardev_remove(const char *id, Error **errp)
3796{
3797 CharDriverState *chr;
3798
3799 chr = qemu_chr_find(id);
3800 if (NULL == chr) {
3801 error_setg(errp, "Chardev '%s' not found", id);
3802 return;
3803 }
3804 if (chr->chr_can_read || chr->chr_read ||
3805 chr->chr_event || chr->handler_opaque) {
3806 error_setg(errp, "Chardev '%s' is busy", id);
3807 return;
3808 }
3809 qemu_chr_delete(chr);
3810}
Anthony Liguorid654f342013-03-05 23:21:28 +05303811
3812static void register_types(void)
3813{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003814 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303815 register_char_driver("socket", qemu_chr_open_socket);
3816 register_char_driver("udp", qemu_chr_open_udp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003817 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3818 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003819 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3820 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003821 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3822 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003823 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3824 qemu_chr_parse_serial);
3825 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3826 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003827 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3828 qemu_chr_parse_parallel);
3829 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3830 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003831 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003832 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003833 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3834 qemu_chr_parse_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303835}
3836
3837type_init(register_types);