blob: dd410ce40fc82161a6022e1c674404db76474679 [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 Bonzini927d4872012-12-17 18:20:05 +010029#include "char/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
Anthony Liguori96c63842013-03-05 23:21:18 +0530599 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530600
601 IOCanReadHandler *fd_can_read;
602 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530603} IOWatchPoll;
604
Anthony Liguori96c63842013-03-05 23:21:18 +0530605static IOWatchPoll *io_watch_poll_from_source(GSource *source)
606{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200607 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530608}
609
610static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
611{
612 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200613 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
614 bool was_active = g_source_get_context(iwp->src) != NULL;
615 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530616 return FALSE;
617 }
618
Paolo Bonzinid185c092013-04-05 17:59:33 +0200619 if (now_active) {
620 g_source_attach(iwp->src, NULL);
621 } else {
622 g_source_remove(g_source_get_id(iwp->src));
623 }
624 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530625}
626
627static gboolean io_watch_poll_check(GSource *source)
628{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200629 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530630}
631
632static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
633 gpointer user_data)
634{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200635 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530636}
637
638static void io_watch_poll_finalize(GSource *source)
639{
640 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200641 g_source_unref(iwp->src);
Anthony Liguori96c63842013-03-05 23:21:18 +0530642}
643
644static GSourceFuncs io_watch_poll_funcs = {
645 .prepare = io_watch_poll_prepare,
646 .check = io_watch_poll_check,
647 .dispatch = io_watch_poll_dispatch,
648 .finalize = io_watch_poll_finalize,
649};
650
651/* Can only be used for read */
652static guint io_add_watch_poll(GIOChannel *channel,
653 IOCanReadHandler *fd_can_read,
654 GIOFunc fd_read,
655 gpointer user_data)
656{
657 IOWatchPoll *iwp;
Anthony Liguori96c63842013-03-05 23:21:18 +0530658
Paolo Bonzinid185c092013-04-05 17:59:33 +0200659 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530660 iwp->fd_can_read = fd_can_read;
661 iwp->opaque = user_data;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200662 iwp->src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
663 g_source_set_callback(iwp->src, (GSourceFunc)fd_read, user_data, NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530664
Paolo Bonzinid185c092013-04-05 17:59:33 +0200665 return g_source_attach(&iwp->parent, NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530666}
667
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000668#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530669static GIOChannel *io_channel_from_fd(int fd)
670{
671 GIOChannel *chan;
672
673 if (fd == -1) {
674 return NULL;
675 }
676
677 chan = g_io_channel_unix_new(fd);
678
679 g_io_channel_set_encoding(chan, NULL, NULL);
680 g_io_channel_set_buffered(chan, FALSE);
681
682 return chan;
683}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000684#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530685
Anthony Liguori76a96442013-03-05 23:21:21 +0530686static GIOChannel *io_channel_from_socket(int fd)
687{
688 GIOChannel *chan;
689
690 if (fd == -1) {
691 return NULL;
692 }
693
694#ifdef _WIN32
695 chan = g_io_channel_win32_new_socket(fd);
696#else
697 chan = g_io_channel_unix_new(fd);
698#endif
699
700 g_io_channel_set_encoding(chan, NULL, NULL);
701 g_io_channel_set_buffered(chan, FALSE);
702
703 return chan;
704}
705
Anthony Liguori684a0962013-03-29 11:39:50 -0500706static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530707{
708 GIOStatus status;
Anthony Liguori684a0962013-03-29 11:39:50 -0500709 size_t offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530710
Anthony Liguori684a0962013-03-29 11:39:50 -0500711 offset = 0;
712 while (offset < len) {
713 gsize bytes_written;
714
715 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530716 &bytes_written, NULL);
717 if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori23673ca2013-03-05 23:21:23 +0530718 if (status == G_IO_STATUS_AGAIN) {
Anthony Liguori684a0962013-03-29 11:39:50 -0500719 /* If we've written any data, return a partial write. */
720 if (offset) {
721 break;
722 }
Anthony Liguori23673ca2013-03-05 23:21:23 +0530723 errno = EAGAIN;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530724 } else {
725 errno = EINVAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530726 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500727
728 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530729 } else if (status == G_IO_STATUS_EOF) {
730 break;
Anthony Liguori96c63842013-03-05 23:21:18 +0530731 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500732
733 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530734 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500735
736 return offset;
Anthony Liguori96c63842013-03-05 23:21:18 +0530737}
Anthony Liguori96c63842013-03-05 23:21:18 +0530738
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000739#ifndef _WIN32
740
Anthony Liguoria29753f2013-03-05 23:21:19 +0530741typedef struct FDCharDriver {
742 CharDriverState *chr;
743 GIOChannel *fd_in, *fd_out;
744 guint fd_in_tag;
aliguori6f97dba2008-10-31 18:49:55 +0000745 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530746 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000747} FDCharDriver;
748
aliguori6f97dba2008-10-31 18:49:55 +0000749static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
750{
751 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530752
Anthony Liguori684a0962013-03-29 11:39:50 -0500753 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530754}
755
756static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
757{
758 CharDriverState *chr = opaque;
759 FDCharDriver *s = chr->opaque;
760 int len;
761 uint8_t buf[READ_BUF_LEN];
762 GIOStatus status;
763 gsize bytes_read;
764
765 len = sizeof(buf);
766 if (len > s->max_size) {
767 len = s->max_size;
768 }
769 if (len == 0) {
770 return FALSE;
771 }
772
773 status = g_io_channel_read_chars(chan, (gchar *)buf,
774 len, &bytes_read, NULL);
775 if (status == G_IO_STATUS_EOF) {
776 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
777 return FALSE;
778 }
779 if (status == G_IO_STATUS_NORMAL) {
780 qemu_chr_be_write(chr, buf, bytes_read);
781 }
782
783 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000784}
785
786static int fd_chr_read_poll(void *opaque)
787{
788 CharDriverState *chr = opaque;
789 FDCharDriver *s = chr->opaque;
790
Anthony Liguori909cda12011-08-15 11:17:31 -0500791 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000792 return s->max_size;
793}
794
Anthony Liguori23673ca2013-03-05 23:21:23 +0530795static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
796{
797 FDCharDriver *s = chr->opaque;
798 return g_io_create_watch(s->fd_out, cond);
799}
800
aliguori6f97dba2008-10-31 18:49:55 +0000801static void fd_chr_update_read_handler(CharDriverState *chr)
802{
803 FDCharDriver *s = chr->opaque;
804
Anthony Liguoria29753f2013-03-05 23:21:19 +0530805 if (s->fd_in_tag) {
806 g_source_remove(s->fd_in_tag);
807 }
808
809 if (s->fd_in) {
810 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 +0000811 }
812}
813
814static void fd_chr_close(struct CharDriverState *chr)
815{
816 FDCharDriver *s = chr->opaque;
817
Anthony Liguoria29753f2013-03-05 23:21:19 +0530818 if (s->fd_in_tag) {
819 g_source_remove(s->fd_in_tag);
820 s->fd_in_tag = 0;
821 }
822
823 if (s->fd_in) {
824 g_io_channel_unref(s->fd_in);
825 }
826 if (s->fd_out) {
827 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000828 }
829
Anthony Liguori7267c092011-08-20 22:09:37 -0500830 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100831 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000832}
833
834/* open a character device to a unix fd */
835static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
836{
837 CharDriverState *chr;
838 FDCharDriver *s;
839
Anthony Liguori7267c092011-08-20 22:09:37 -0500840 chr = g_malloc0(sizeof(CharDriverState));
841 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530842 s->fd_in = io_channel_from_fd(fd_in);
843 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530844 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530845 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000846 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530847 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000848 chr->chr_write = fd_chr_write;
849 chr->chr_update_read_handler = fd_chr_update_read_handler;
850 chr->chr_close = fd_chr_close;
851
Hans de Goedefee204f2013-03-26 11:07:54 +0100852 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000853
854 return chr;
855}
856
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100857static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000858{
859 int fd_in, fd_out;
860 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100861 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200862
863 if (filename == NULL) {
864 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100865 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200866 }
aliguori6f97dba2008-10-31 18:49:55 +0000867
868 snprintf(filename_in, 256, "%s.in", filename);
869 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100870 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
871 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000872 if (fd_in < 0 || fd_out < 0) {
873 if (fd_in >= 0)
874 close(fd_in);
875 if (fd_out >= 0)
876 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100877 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100878 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100879 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100880 }
aliguori6f97dba2008-10-31 18:49:55 +0000881 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100882 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000883}
884
aliguori6f97dba2008-10-31 18:49:55 +0000885/* init terminal so that we can grab keys */
886static struct termios oldtty;
887static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100888static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000889
890static void term_exit(void)
891{
892 tcsetattr (0, TCSANOW, &oldtty);
893 fcntl(0, F_SETFL, old_fd0_flags);
894}
895
Paolo Bonzinibb002512010-12-23 13:42:50 +0100896static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +0000897{
898 struct termios tty;
899
Paolo Bonzini03693642010-12-23 13:42:49 +0100900 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100901 if (!echo) {
902 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +0000903 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +0100904 tty.c_oflag |= OPOST;
905 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
906 tty.c_cflag &= ~(CSIZE|PARENB);
907 tty.c_cflag |= CS8;
908 tty.c_cc[VMIN] = 1;
909 tty.c_cc[VTIME] = 0;
910 }
aliguori6f97dba2008-10-31 18:49:55 +0000911 /* if graphical mode, we allow Ctrl-C handling */
Paolo Bonzinibb002512010-12-23 13:42:50 +0100912 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +0000913 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +0000914
915 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000916}
917
918static void qemu_chr_close_stdio(struct CharDriverState *chr)
919{
920 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +0000921 fd_chr_close(chr);
922}
923
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100924static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000925{
926 CharDriverState *chr;
927
Michael Tokarevab51b1d2012-12-30 12:48:14 +0400928 if (is_daemonized()) {
929 error_report("cannot use stdio with -daemonize");
930 return NULL;
931 }
Anthony Liguoried7a1542013-03-05 23:21:17 +0530932 old_fd0_flags = fcntl(0, F_GETFL);
933 tcgetattr (0, &oldtty);
934 fcntl(0, F_SETFL, O_NONBLOCK);
935 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +0100936
aliguori6f97dba2008-10-31 18:49:55 +0000937 chr = qemu_chr_open_fd(0, 1);
938 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100939 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +0100940 stdio_allow_signal = display_type != DT_NOGRAPHIC;
941 if (opts->has_signal) {
942 stdio_allow_signal = opts->signal;
943 }
Anthony Liguori15f31512011-08-15 11:17:35 -0500944 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +0000945
Markus Armbruster1f514702012-02-07 15:09:08 +0100946 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000947}
948
949#ifdef __sun__
950/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000951static int openpty(int *amaster, int *aslave, char *name,
952 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000953{
954 const char *slave;
955 int mfd = -1, sfd = -1;
956
957 *amaster = *aslave = -1;
958
959 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
960 if (mfd < 0)
961 goto err;
962
963 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
964 goto err;
965
966 if ((slave = ptsname(mfd)) == NULL)
967 goto err;
968
969 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
970 goto err;
971
972 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
973 (termp != NULL && tcgetattr(sfd, termp) < 0))
974 goto err;
975
976 if (amaster)
977 *amaster = mfd;
978 if (aslave)
979 *aslave = sfd;
980 if (winp)
981 ioctl(sfd, TIOCSWINSZ, winp);
982
983 return 0;
984
985err:
986 if (sfd != -1)
987 close(sfd);
988 close(mfd);
989 return -1;
990}
991
blueswir13f4cb3d2009-04-13 16:31:01 +0000992static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000993{
994 termios_p->c_iflag &=
995 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
996 termios_p->c_oflag &= ~OPOST;
997 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
998 termios_p->c_cflag &= ~(CSIZE|PARENB);
999 termios_p->c_cflag |= CS8;
1000
1001 termios_p->c_cc[VMIN] = 0;
1002 termios_p->c_cc[VTIME] = 0;
1003}
1004#endif
1005
1006#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001007 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1008 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001009
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001010#define HAVE_CHARDEV_TTY 1
1011
aliguori6f97dba2008-10-31 18:49:55 +00001012typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301013 GIOChannel *fd;
1014 guint fd_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001015 int connected;
1016 int polling;
1017 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301018 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001019} PtyCharDriver;
1020
1021static void pty_chr_update_read_handler(CharDriverState *chr);
1022static void pty_chr_state(CharDriverState *chr, int connected);
1023
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301024static gboolean pty_chr_timer(gpointer opaque)
1025{
1026 struct CharDriverState *chr = opaque;
1027 PtyCharDriver *s = chr->opaque;
1028
1029 if (s->connected) {
1030 goto out;
1031 }
1032 if (s->polling) {
1033 /* If we arrive here without polling being cleared due
1034 * read returning -EIO, then we are (re-)connected */
1035 pty_chr_state(chr, 1);
1036 goto out;
1037 }
1038
1039 /* Next poll ... */
1040 pty_chr_update_read_handler(chr);
1041
1042out:
1043 return FALSE;
1044}
1045
1046static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1047{
1048 PtyCharDriver *s = chr->opaque;
1049
1050 if (s->timer_tag) {
1051 g_source_remove(s->timer_tag);
1052 s->timer_tag = 0;
1053 }
1054
1055 if (ms == 1000) {
1056 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1057 } else {
1058 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1059 }
1060}
1061
aliguori6f97dba2008-10-31 18:49:55 +00001062static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1063{
1064 PtyCharDriver *s = chr->opaque;
1065
1066 if (!s->connected) {
1067 /* guest sends data, check for (re-)connect */
1068 pty_chr_update_read_handler(chr);
1069 return 0;
1070 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001071 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001072}
1073
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301074static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1075{
1076 PtyCharDriver *s = chr->opaque;
1077 return g_io_create_watch(s->fd, cond);
1078}
1079
aliguori6f97dba2008-10-31 18:49:55 +00001080static int pty_chr_read_poll(void *opaque)
1081{
1082 CharDriverState *chr = opaque;
1083 PtyCharDriver *s = chr->opaque;
1084
Anthony Liguori909cda12011-08-15 11:17:31 -05001085 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001086 return s->read_bytes;
1087}
1088
Anthony Liguori093d3a22013-03-05 23:21:20 +05301089static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001090{
1091 CharDriverState *chr = opaque;
1092 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301093 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301094 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301095 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001096
1097 len = sizeof(buf);
1098 if (len > s->read_bytes)
1099 len = s->read_bytes;
1100 if (len == 0)
Anthony Liguori093d3a22013-03-05 23:21:20 +05301101 return FALSE;
1102 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1103 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001104 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301105 return FALSE;
1106 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001107 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001108 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001109 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301110 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001111}
1112
1113static void pty_chr_update_read_handler(CharDriverState *chr)
1114{
1115 PtyCharDriver *s = chr->opaque;
1116
Anthony Liguori093d3a22013-03-05 23:21:20 +05301117 if (s->fd_tag) {
1118 g_source_remove(s->fd_tag);
1119 }
1120
1121 s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001122 s->polling = 1;
1123 /*
1124 * Short timeout here: just need wait long enougth that qemu makes
1125 * it through the poll loop once. When reconnected we want a
1126 * short timeout so we notice it almost instantly. Otherwise
1127 * read() gives us -EIO instantly, making pty_chr_state() reset the
1128 * timeout to the normal (much longer) poll interval before the
1129 * timer triggers.
1130 */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301131 pty_chr_rearm_timer(chr, 10);
aliguori6f97dba2008-10-31 18:49:55 +00001132}
1133
1134static void pty_chr_state(CharDriverState *chr, int connected)
1135{
1136 PtyCharDriver *s = chr->opaque;
1137
1138 if (!connected) {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301139 g_source_remove(s->fd_tag);
1140 s->fd_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001141 s->connected = 0;
1142 s->polling = 0;
1143 /* (re-)connect poll interval for idle guests: once per second.
1144 * We check more frequently in case the guests sends data to
1145 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301146 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001147 } else {
1148 if (!s->connected)
Hans de Goedefee204f2013-03-26 11:07:54 +01001149 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001150 s->connected = 1;
1151 }
1152}
1153
aliguori6f97dba2008-10-31 18:49:55 +00001154
1155static void pty_chr_close(struct CharDriverState *chr)
1156{
1157 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301158 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001159
Anthony Liguori093d3a22013-03-05 23:21:20 +05301160 if (s->fd_tag) {
1161 g_source_remove(s->fd_tag);
1162 }
1163 fd = g_io_channel_unix_get_fd(s->fd);
1164 g_io_channel_unref(s->fd);
1165 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301166 if (s->timer_tag) {
1167 g_source_remove(s->timer_tag);
1168 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001169 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001170 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001171}
1172
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001173static CharDriverState *qemu_chr_open_pty(const char *id,
1174 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001175{
1176 CharDriverState *chr;
1177 PtyCharDriver *s;
1178 struct termios tty;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001179 int master_fd, slave_fd;
blueswir1c5e97232009-03-07 20:06:23 +00001180#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +00001181 char pty_name[PATH_MAX];
1182#define q_ptsname(x) pty_name
1183#else
1184 char *pty_name = NULL;
1185#define q_ptsname(x) ptsname(x)
1186#endif
1187
Markus Armbrustera4e26042011-11-11 10:40:05 +01001188 if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001189 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001190 }
1191
1192 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +00001193 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001194 cfmakeraw(&tty);
1195 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1196 close(slave_fd);
1197
Markus Armbrustera4e26042011-11-11 10:40:05 +01001198 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001199
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001200 chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd));
1201 ret->pty = g_strdup(q_ptsname(master_fd));
1202 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001203
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001204 fprintf(stderr, "char device redirected to %s (label %s)\n",
1205 q_ptsname(master_fd), id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001206
1207 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001208 chr->opaque = s;
1209 chr->chr_write = pty_chr_write;
1210 chr->chr_update_read_handler = pty_chr_update_read_handler;
1211 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301212 chr->chr_add_watch = pty_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001213
Anthony Liguori093d3a22013-03-05 23:21:20 +05301214 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301215 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001216
Markus Armbruster1f514702012-02-07 15:09:08 +01001217 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001218}
1219
1220static void tty_serial_init(int fd, int speed,
1221 int parity, int data_bits, int stop_bits)
1222{
1223 struct termios tty;
1224 speed_t spd;
1225
1226#if 0
1227 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1228 speed, parity, data_bits, stop_bits);
1229#endif
1230 tcgetattr (fd, &tty);
1231
Stefan Weil45eea132009-10-26 16:10:10 +01001232#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1233 speed = speed * 10 / 11;
1234 do {
1235 check_speed(50);
1236 check_speed(75);
1237 check_speed(110);
1238 check_speed(134);
1239 check_speed(150);
1240 check_speed(200);
1241 check_speed(300);
1242 check_speed(600);
1243 check_speed(1200);
1244 check_speed(1800);
1245 check_speed(2400);
1246 check_speed(4800);
1247 check_speed(9600);
1248 check_speed(19200);
1249 check_speed(38400);
1250 /* Non-Posix values follow. They may be unsupported on some systems. */
1251 check_speed(57600);
1252 check_speed(115200);
1253#ifdef B230400
1254 check_speed(230400);
1255#endif
1256#ifdef B460800
1257 check_speed(460800);
1258#endif
1259#ifdef B500000
1260 check_speed(500000);
1261#endif
1262#ifdef B576000
1263 check_speed(576000);
1264#endif
1265#ifdef B921600
1266 check_speed(921600);
1267#endif
1268#ifdef B1000000
1269 check_speed(1000000);
1270#endif
1271#ifdef B1152000
1272 check_speed(1152000);
1273#endif
1274#ifdef B1500000
1275 check_speed(1500000);
1276#endif
1277#ifdef B2000000
1278 check_speed(2000000);
1279#endif
1280#ifdef B2500000
1281 check_speed(2500000);
1282#endif
1283#ifdef B3000000
1284 check_speed(3000000);
1285#endif
1286#ifdef B3500000
1287 check_speed(3500000);
1288#endif
1289#ifdef B4000000
1290 check_speed(4000000);
1291#endif
aliguori6f97dba2008-10-31 18:49:55 +00001292 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001293 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001294
1295 cfsetispeed(&tty, spd);
1296 cfsetospeed(&tty, spd);
1297
1298 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1299 |INLCR|IGNCR|ICRNL|IXON);
1300 tty.c_oflag |= OPOST;
1301 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1302 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1303 switch(data_bits) {
1304 default:
1305 case 8:
1306 tty.c_cflag |= CS8;
1307 break;
1308 case 7:
1309 tty.c_cflag |= CS7;
1310 break;
1311 case 6:
1312 tty.c_cflag |= CS6;
1313 break;
1314 case 5:
1315 tty.c_cflag |= CS5;
1316 break;
1317 }
1318 switch(parity) {
1319 default:
1320 case 'N':
1321 break;
1322 case 'E':
1323 tty.c_cflag |= PARENB;
1324 break;
1325 case 'O':
1326 tty.c_cflag |= PARENB | PARODD;
1327 break;
1328 }
1329 if (stop_bits == 2)
1330 tty.c_cflag |= CSTOPB;
1331
1332 tcsetattr (fd, TCSANOW, &tty);
1333}
1334
1335static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1336{
1337 FDCharDriver *s = chr->opaque;
1338
1339 switch(cmd) {
1340 case CHR_IOCTL_SERIAL_SET_PARAMS:
1341 {
1342 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301343 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1344 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001345 ssp->data_bits, ssp->stop_bits);
1346 }
1347 break;
1348 case CHR_IOCTL_SERIAL_SET_BREAK:
1349 {
1350 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301351 if (enable) {
1352 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1353 }
aliguori6f97dba2008-10-31 18:49:55 +00001354 }
1355 break;
1356 case CHR_IOCTL_SERIAL_GET_TIOCM:
1357 {
1358 int sarg = 0;
1359 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301360 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001361 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001362 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001363 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001364 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001365 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001366 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001367 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001368 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001369 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001370 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001371 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001372 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001373 *targ |= CHR_TIOCM_RTS;
1374 }
1375 break;
1376 case CHR_IOCTL_SERIAL_SET_TIOCM:
1377 {
1378 int sarg = *(int *)arg;
1379 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301380 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001381 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1382 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1383 if (sarg & CHR_TIOCM_CTS)
1384 targ |= TIOCM_CTS;
1385 if (sarg & CHR_TIOCM_CAR)
1386 targ |= TIOCM_CAR;
1387 if (sarg & CHR_TIOCM_DSR)
1388 targ |= TIOCM_DSR;
1389 if (sarg & CHR_TIOCM_RI)
1390 targ |= TIOCM_RI;
1391 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001392 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001393 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001394 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301395 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001396 }
1397 break;
1398 default:
1399 return -ENOTSUP;
1400 }
1401 return 0;
1402}
1403
David Ahern4266a132010-02-10 18:27:17 -07001404static void qemu_chr_close_tty(CharDriverState *chr)
1405{
1406 FDCharDriver *s = chr->opaque;
1407 int fd = -1;
1408
1409 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301410 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001411 }
1412
1413 fd_chr_close(chr);
1414
1415 if (fd >= 0) {
1416 close(fd);
1417 }
1418}
1419
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001420static CharDriverState *qemu_chr_open_tty_fd(int fd)
1421{
1422 CharDriverState *chr;
1423
1424 tty_serial_init(fd, 115200, 'N', 8, 1);
1425 chr = qemu_chr_open_fd(fd, fd);
1426 chr->chr_ioctl = tty_serial_ioctl;
1427 chr->chr_close = qemu_chr_close_tty;
1428 return chr;
1429}
aliguori6f97dba2008-10-31 18:49:55 +00001430#endif /* __linux__ || __sun__ */
1431
1432#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001433
1434#define HAVE_CHARDEV_PARPORT 1
1435
aliguori6f97dba2008-10-31 18:49:55 +00001436typedef struct {
1437 int fd;
1438 int mode;
1439} ParallelCharDriver;
1440
1441static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1442{
1443 if (s->mode != mode) {
1444 int m = mode;
1445 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1446 return 0;
1447 s->mode = mode;
1448 }
1449 return 1;
1450}
1451
1452static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1453{
1454 ParallelCharDriver *drv = chr->opaque;
1455 int fd = drv->fd;
1456 uint8_t b;
1457
1458 switch(cmd) {
1459 case CHR_IOCTL_PP_READ_DATA:
1460 if (ioctl(fd, PPRDATA, &b) < 0)
1461 return -ENOTSUP;
1462 *(uint8_t *)arg = b;
1463 break;
1464 case CHR_IOCTL_PP_WRITE_DATA:
1465 b = *(uint8_t *)arg;
1466 if (ioctl(fd, PPWDATA, &b) < 0)
1467 return -ENOTSUP;
1468 break;
1469 case CHR_IOCTL_PP_READ_CONTROL:
1470 if (ioctl(fd, PPRCONTROL, &b) < 0)
1471 return -ENOTSUP;
1472 /* Linux gives only the lowest bits, and no way to know data
1473 direction! For better compatibility set the fixed upper
1474 bits. */
1475 *(uint8_t *)arg = b | 0xc0;
1476 break;
1477 case CHR_IOCTL_PP_WRITE_CONTROL:
1478 b = *(uint8_t *)arg;
1479 if (ioctl(fd, PPWCONTROL, &b) < 0)
1480 return -ENOTSUP;
1481 break;
1482 case CHR_IOCTL_PP_READ_STATUS:
1483 if (ioctl(fd, PPRSTATUS, &b) < 0)
1484 return -ENOTSUP;
1485 *(uint8_t *)arg = b;
1486 break;
1487 case CHR_IOCTL_PP_DATA_DIR:
1488 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1489 return -ENOTSUP;
1490 break;
1491 case CHR_IOCTL_PP_EPP_READ_ADDR:
1492 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1493 struct ParallelIOArg *parg = arg;
1494 int n = read(fd, parg->buffer, parg->count);
1495 if (n != parg->count) {
1496 return -EIO;
1497 }
1498 }
1499 break;
1500 case CHR_IOCTL_PP_EPP_READ:
1501 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1502 struct ParallelIOArg *parg = arg;
1503 int n = read(fd, parg->buffer, parg->count);
1504 if (n != parg->count) {
1505 return -EIO;
1506 }
1507 }
1508 break;
1509 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1510 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1511 struct ParallelIOArg *parg = arg;
1512 int n = write(fd, parg->buffer, parg->count);
1513 if (n != parg->count) {
1514 return -EIO;
1515 }
1516 }
1517 break;
1518 case CHR_IOCTL_PP_EPP_WRITE:
1519 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1520 struct ParallelIOArg *parg = arg;
1521 int n = write(fd, parg->buffer, parg->count);
1522 if (n != parg->count) {
1523 return -EIO;
1524 }
1525 }
1526 break;
1527 default:
1528 return -ENOTSUP;
1529 }
1530 return 0;
1531}
1532
1533static void pp_close(CharDriverState *chr)
1534{
1535 ParallelCharDriver *drv = chr->opaque;
1536 int fd = drv->fd;
1537
1538 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1539 ioctl(fd, PPRELEASE);
1540 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001541 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001542 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001543}
1544
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001545static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001546{
1547 CharDriverState *chr;
1548 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001549
1550 if (ioctl(fd, PPCLAIM) < 0) {
1551 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001552 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001553 }
1554
Anthony Liguori7267c092011-08-20 22:09:37 -05001555 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001556 drv->fd = fd;
1557 drv->mode = IEEE1284_MODE_COMPAT;
1558
Anthony Liguori7267c092011-08-20 22:09:37 -05001559 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001560 chr->chr_write = null_chr_write;
1561 chr->chr_ioctl = pp_ioctl;
1562 chr->chr_close = pp_close;
1563 chr->opaque = drv;
1564
Hans de Goedefee204f2013-03-26 11:07:54 +01001565 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001566
Markus Armbruster1f514702012-02-07 15:09:08 +01001567 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001568}
1569#endif /* __linux__ */
1570
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001571#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001572
1573#define HAVE_CHARDEV_PARPORT 1
1574
blueswir16972f932008-11-22 20:49:12 +00001575static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1576{
Stefan Weile0efb992011-02-23 19:09:16 +01001577 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001578 uint8_t b;
1579
1580 switch(cmd) {
1581 case CHR_IOCTL_PP_READ_DATA:
1582 if (ioctl(fd, PPIGDATA, &b) < 0)
1583 return -ENOTSUP;
1584 *(uint8_t *)arg = b;
1585 break;
1586 case CHR_IOCTL_PP_WRITE_DATA:
1587 b = *(uint8_t *)arg;
1588 if (ioctl(fd, PPISDATA, &b) < 0)
1589 return -ENOTSUP;
1590 break;
1591 case CHR_IOCTL_PP_READ_CONTROL:
1592 if (ioctl(fd, PPIGCTRL, &b) < 0)
1593 return -ENOTSUP;
1594 *(uint8_t *)arg = b;
1595 break;
1596 case CHR_IOCTL_PP_WRITE_CONTROL:
1597 b = *(uint8_t *)arg;
1598 if (ioctl(fd, PPISCTRL, &b) < 0)
1599 return -ENOTSUP;
1600 break;
1601 case CHR_IOCTL_PP_READ_STATUS:
1602 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1603 return -ENOTSUP;
1604 *(uint8_t *)arg = b;
1605 break;
1606 default:
1607 return -ENOTSUP;
1608 }
1609 return 0;
1610}
1611
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001612static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001613{
1614 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001615
Anthony Liguori7267c092011-08-20 22:09:37 -05001616 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001617 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001618 chr->chr_write = null_chr_write;
1619 chr->chr_ioctl = pp_ioctl;
Markus Armbruster1f514702012-02-07 15:09:08 +01001620 return chr;
blueswir16972f932008-11-22 20:49:12 +00001621}
1622#endif
1623
aliguori6f97dba2008-10-31 18:49:55 +00001624#else /* _WIN32 */
1625
1626typedef struct {
1627 int max_size;
1628 HANDLE hcom, hrecv, hsend;
1629 OVERLAPPED orecv, osend;
1630 BOOL fpipe;
1631 DWORD len;
1632} WinCharState;
1633
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001634typedef struct {
1635 HANDLE hStdIn;
1636 HANDLE hInputReadyEvent;
1637 HANDLE hInputDoneEvent;
1638 HANDLE hInputThread;
1639 uint8_t win_stdio_buf;
1640} WinStdioCharState;
1641
aliguori6f97dba2008-10-31 18:49:55 +00001642#define NSENDBUF 2048
1643#define NRECVBUF 2048
1644#define MAXCONNECT 1
1645#define NTIMEOUT 5000
1646
1647static int win_chr_poll(void *opaque);
1648static int win_chr_pipe_poll(void *opaque);
1649
1650static void win_chr_close(CharDriverState *chr)
1651{
1652 WinCharState *s = chr->opaque;
1653
1654 if (s->hsend) {
1655 CloseHandle(s->hsend);
1656 s->hsend = NULL;
1657 }
1658 if (s->hrecv) {
1659 CloseHandle(s->hrecv);
1660 s->hrecv = NULL;
1661 }
1662 if (s->hcom) {
1663 CloseHandle(s->hcom);
1664 s->hcom = NULL;
1665 }
1666 if (s->fpipe)
1667 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1668 else
1669 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301670
Hans de Goedea425d232011-11-19 10:22:43 +01001671 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001672}
1673
1674static int win_chr_init(CharDriverState *chr, const char *filename)
1675{
1676 WinCharState *s = chr->opaque;
1677 COMMCONFIG comcfg;
1678 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1679 COMSTAT comstat;
1680 DWORD size;
1681 DWORD err;
1682
1683 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1684 if (!s->hsend) {
1685 fprintf(stderr, "Failed CreateEvent\n");
1686 goto fail;
1687 }
1688 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1689 if (!s->hrecv) {
1690 fprintf(stderr, "Failed CreateEvent\n");
1691 goto fail;
1692 }
1693
1694 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1695 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1696 if (s->hcom == INVALID_HANDLE_VALUE) {
1697 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1698 s->hcom = NULL;
1699 goto fail;
1700 }
1701
1702 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1703 fprintf(stderr, "Failed SetupComm\n");
1704 goto fail;
1705 }
1706
1707 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1708 size = sizeof(COMMCONFIG);
1709 GetDefaultCommConfig(filename, &comcfg, &size);
1710 comcfg.dcb.DCBlength = sizeof(DCB);
1711 CommConfigDialog(filename, NULL, &comcfg);
1712
1713 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1714 fprintf(stderr, "Failed SetCommState\n");
1715 goto fail;
1716 }
1717
1718 if (!SetCommMask(s->hcom, EV_ERR)) {
1719 fprintf(stderr, "Failed SetCommMask\n");
1720 goto fail;
1721 }
1722
1723 cto.ReadIntervalTimeout = MAXDWORD;
1724 if (!SetCommTimeouts(s->hcom, &cto)) {
1725 fprintf(stderr, "Failed SetCommTimeouts\n");
1726 goto fail;
1727 }
1728
1729 if (!ClearCommError(s->hcom, &err, &comstat)) {
1730 fprintf(stderr, "Failed ClearCommError\n");
1731 goto fail;
1732 }
1733 qemu_add_polling_cb(win_chr_poll, chr);
1734 return 0;
1735
1736 fail:
1737 win_chr_close(chr);
1738 return -1;
1739}
1740
1741static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1742{
1743 WinCharState *s = chr->opaque;
1744 DWORD len, ret, size, err;
1745
1746 len = len1;
1747 ZeroMemory(&s->osend, sizeof(s->osend));
1748 s->osend.hEvent = s->hsend;
1749 while (len > 0) {
1750 if (s->hsend)
1751 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1752 else
1753 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1754 if (!ret) {
1755 err = GetLastError();
1756 if (err == ERROR_IO_PENDING) {
1757 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1758 if (ret) {
1759 buf += size;
1760 len -= size;
1761 } else {
1762 break;
1763 }
1764 } else {
1765 break;
1766 }
1767 } else {
1768 buf += size;
1769 len -= size;
1770 }
1771 }
1772 return len1 - len;
1773}
1774
1775static int win_chr_read_poll(CharDriverState *chr)
1776{
1777 WinCharState *s = chr->opaque;
1778
Anthony Liguori909cda12011-08-15 11:17:31 -05001779 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001780 return s->max_size;
1781}
1782
1783static void win_chr_readfile(CharDriverState *chr)
1784{
1785 WinCharState *s = chr->opaque;
1786 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301787 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001788 DWORD size;
1789
1790 ZeroMemory(&s->orecv, sizeof(s->orecv));
1791 s->orecv.hEvent = s->hrecv;
1792 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1793 if (!ret) {
1794 err = GetLastError();
1795 if (err == ERROR_IO_PENDING) {
1796 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1797 }
1798 }
1799
1800 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001801 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001802 }
1803}
1804
1805static void win_chr_read(CharDriverState *chr)
1806{
1807 WinCharState *s = chr->opaque;
1808
1809 if (s->len > s->max_size)
1810 s->len = s->max_size;
1811 if (s->len == 0)
1812 return;
1813
1814 win_chr_readfile(chr);
1815}
1816
1817static int win_chr_poll(void *opaque)
1818{
1819 CharDriverState *chr = opaque;
1820 WinCharState *s = chr->opaque;
1821 COMSTAT status;
1822 DWORD comerr;
1823
1824 ClearCommError(s->hcom, &comerr, &status);
1825 if (status.cbInQue > 0) {
1826 s->len = status.cbInQue;
1827 win_chr_read_poll(chr);
1828 win_chr_read(chr);
1829 return 1;
1830 }
1831 return 0;
1832}
1833
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001834static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001835{
1836 CharDriverState *chr;
1837 WinCharState *s;
1838
Anthony Liguori7267c092011-08-20 22:09:37 -05001839 chr = g_malloc0(sizeof(CharDriverState));
1840 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001841 chr->opaque = s;
1842 chr->chr_write = win_chr_write;
1843 chr->chr_close = win_chr_close;
1844
1845 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001846 g_free(s);
1847 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001848 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001849 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001850 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001851 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001852}
1853
1854static int win_chr_pipe_poll(void *opaque)
1855{
1856 CharDriverState *chr = opaque;
1857 WinCharState *s = chr->opaque;
1858 DWORD size;
1859
1860 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1861 if (size > 0) {
1862 s->len = size;
1863 win_chr_read_poll(chr);
1864 win_chr_read(chr);
1865 return 1;
1866 }
1867 return 0;
1868}
1869
1870static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1871{
1872 WinCharState *s = chr->opaque;
1873 OVERLAPPED ov;
1874 int ret;
1875 DWORD size;
1876 char openname[256];
1877
1878 s->fpipe = TRUE;
1879
1880 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1881 if (!s->hsend) {
1882 fprintf(stderr, "Failed CreateEvent\n");
1883 goto fail;
1884 }
1885 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1886 if (!s->hrecv) {
1887 fprintf(stderr, "Failed CreateEvent\n");
1888 goto fail;
1889 }
1890
1891 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1892 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1893 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1894 PIPE_WAIT,
1895 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1896 if (s->hcom == INVALID_HANDLE_VALUE) {
1897 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1898 s->hcom = NULL;
1899 goto fail;
1900 }
1901
1902 ZeroMemory(&ov, sizeof(ov));
1903 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1904 ret = ConnectNamedPipe(s->hcom, &ov);
1905 if (ret) {
1906 fprintf(stderr, "Failed ConnectNamedPipe\n");
1907 goto fail;
1908 }
1909
1910 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1911 if (!ret) {
1912 fprintf(stderr, "Failed GetOverlappedResult\n");
1913 if (ov.hEvent) {
1914 CloseHandle(ov.hEvent);
1915 ov.hEvent = NULL;
1916 }
1917 goto fail;
1918 }
1919
1920 if (ov.hEvent) {
1921 CloseHandle(ov.hEvent);
1922 ov.hEvent = NULL;
1923 }
1924 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1925 return 0;
1926
1927 fail:
1928 win_chr_close(chr);
1929 return -1;
1930}
1931
1932
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001933static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001934{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001935 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001936 CharDriverState *chr;
1937 WinCharState *s;
1938
Anthony Liguori7267c092011-08-20 22:09:37 -05001939 chr = g_malloc0(sizeof(CharDriverState));
1940 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001941 chr->opaque = s;
1942 chr->chr_write = win_chr_write;
1943 chr->chr_close = win_chr_close;
1944
1945 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001946 g_free(s);
1947 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001948 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001949 }
Hans de Goedefee204f2013-03-26 11:07:54 +01001950 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001951 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001952}
1953
Markus Armbruster1f514702012-02-07 15:09:08 +01001954static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001955{
1956 CharDriverState *chr;
1957 WinCharState *s;
1958
Anthony Liguori7267c092011-08-20 22:09:37 -05001959 chr = g_malloc0(sizeof(CharDriverState));
1960 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001961 s->hcom = fd_out;
1962 chr->opaque = s;
1963 chr->chr_write = win_chr_write;
Hans de Goedefee204f2013-03-26 11:07:54 +01001964 qemu_chr_be_generic_open(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001965 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001966}
1967
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001968static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001969{
Markus Armbruster1f514702012-02-07 15:09:08 +01001970 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001971}
1972
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001973static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1974{
1975 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1976 DWORD dwSize;
1977 int len1;
1978
1979 len1 = len;
1980
1981 while (len1 > 0) {
1982 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
1983 break;
1984 }
1985 buf += dwSize;
1986 len1 -= dwSize;
1987 }
1988
1989 return len - len1;
1990}
1991
1992static void win_stdio_wait_func(void *opaque)
1993{
1994 CharDriverState *chr = opaque;
1995 WinStdioCharState *stdio = chr->opaque;
1996 INPUT_RECORD buf[4];
1997 int ret;
1998 DWORD dwSize;
1999 int i;
2000
2001 ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
2002 &dwSize);
2003
2004 if (!ret) {
2005 /* Avoid error storm */
2006 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2007 return;
2008 }
2009
2010 for (i = 0; i < dwSize; i++) {
2011 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2012
2013 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2014 int j;
2015 if (kev->uChar.AsciiChar != 0) {
2016 for (j = 0; j < kev->wRepeatCount; j++) {
2017 if (qemu_chr_be_can_write(chr)) {
2018 uint8_t c = kev->uChar.AsciiChar;
2019 qemu_chr_be_write(chr, &c, 1);
2020 }
2021 }
2022 }
2023 }
2024 }
2025}
2026
2027static DWORD WINAPI win_stdio_thread(LPVOID param)
2028{
2029 CharDriverState *chr = param;
2030 WinStdioCharState *stdio = chr->opaque;
2031 int ret;
2032 DWORD dwSize;
2033
2034 while (1) {
2035
2036 /* Wait for one byte */
2037 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2038
2039 /* Exit in case of error, continue if nothing read */
2040 if (!ret) {
2041 break;
2042 }
2043 if (!dwSize) {
2044 continue;
2045 }
2046
2047 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2048 if (stdio->win_stdio_buf == '\r') {
2049 continue;
2050 }
2051
2052 /* Signal the main thread and wait until the byte was eaten */
2053 if (!SetEvent(stdio->hInputReadyEvent)) {
2054 break;
2055 }
2056 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2057 != WAIT_OBJECT_0) {
2058 break;
2059 }
2060 }
2061
2062 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2063 return 0;
2064}
2065
2066static void win_stdio_thread_wait_func(void *opaque)
2067{
2068 CharDriverState *chr = opaque;
2069 WinStdioCharState *stdio = chr->opaque;
2070
2071 if (qemu_chr_be_can_write(chr)) {
2072 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2073 }
2074
2075 SetEvent(stdio->hInputDoneEvent);
2076}
2077
2078static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2079{
2080 WinStdioCharState *stdio = chr->opaque;
2081 DWORD dwMode = 0;
2082
2083 GetConsoleMode(stdio->hStdIn, &dwMode);
2084
2085 if (echo) {
2086 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2087 } else {
2088 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2089 }
2090}
2091
2092static void win_stdio_close(CharDriverState *chr)
2093{
2094 WinStdioCharState *stdio = chr->opaque;
2095
2096 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2097 CloseHandle(stdio->hInputReadyEvent);
2098 }
2099 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2100 CloseHandle(stdio->hInputDoneEvent);
2101 }
2102 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2103 TerminateThread(stdio->hInputThread, 0);
2104 }
2105
2106 g_free(chr->opaque);
2107 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002108}
2109
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002110static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002111{
2112 CharDriverState *chr;
2113 WinStdioCharState *stdio;
2114 DWORD dwMode;
2115 int is_console = 0;
2116
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002117 chr = g_malloc0(sizeof(CharDriverState));
2118 stdio = g_malloc0(sizeof(WinStdioCharState));
2119
2120 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2121 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2122 fprintf(stderr, "cannot open stdio: invalid handle\n");
2123 exit(1);
2124 }
2125
2126 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2127
2128 chr->opaque = stdio;
2129 chr->chr_write = win_stdio_write;
2130 chr->chr_close = win_stdio_close;
2131
Anthony Liguoried7a1542013-03-05 23:21:17 +05302132 if (is_console) {
2133 if (qemu_add_wait_object(stdio->hStdIn,
2134 win_stdio_wait_func, chr)) {
2135 fprintf(stderr, "qemu_add_wait_object: failed\n");
2136 }
2137 } else {
2138 DWORD dwId;
2139
2140 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2141 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2142 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2143 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002144
Anthony Liguoried7a1542013-03-05 23:21:17 +05302145 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2146 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2147 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2148 fprintf(stderr, "cannot create stdio thread or event\n");
2149 exit(1);
2150 }
2151 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2152 win_stdio_thread_wait_func, chr)) {
2153 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002154 }
2155 }
2156
2157 dwMode |= ENABLE_LINE_INPUT;
2158
Anthony Liguoried7a1542013-03-05 23:21:17 +05302159 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002160 /* set the terminal in raw mode */
2161 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2162 dwMode |= ENABLE_PROCESSED_INPUT;
2163 }
2164
2165 SetConsoleMode(stdio->hStdIn, dwMode);
2166
2167 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2168 qemu_chr_fe_set_echo(chr, false);
2169
Markus Armbruster1f514702012-02-07 15:09:08 +01002170 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002171}
aliguori6f97dba2008-10-31 18:49:55 +00002172#endif /* !_WIN32 */
2173
Anthony Liguori5ab82112013-03-05 23:21:31 +05302174
aliguori6f97dba2008-10-31 18:49:55 +00002175/***********************************************************/
2176/* UDP Net console */
2177
2178typedef struct {
2179 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302180 GIOChannel *chan;
2181 guint tag;
Amit Shah9bd78542009-11-03 19:59:54 +05302182 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002183 int bufcnt;
2184 int bufptr;
2185 int max_size;
2186} NetCharDriver;
2187
2188static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2189{
2190 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302191 gsize bytes_written;
2192 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002193
Anthony Liguori76a96442013-03-05 23:21:21 +05302194 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2195 if (status == G_IO_STATUS_EOF) {
2196 return 0;
2197 } else if (status != G_IO_STATUS_NORMAL) {
2198 return -1;
2199 }
2200
2201 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002202}
2203
2204static int udp_chr_read_poll(void *opaque)
2205{
2206 CharDriverState *chr = opaque;
2207 NetCharDriver *s = chr->opaque;
2208
Anthony Liguori909cda12011-08-15 11:17:31 -05002209 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002210
2211 /* If there were any stray characters in the queue process them
2212 * first
2213 */
2214 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002215 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002216 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002217 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002218 }
2219 return s->max_size;
2220}
2221
Anthony Liguori76a96442013-03-05 23:21:21 +05302222static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002223{
2224 CharDriverState *chr = opaque;
2225 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302226 gsize bytes_read = 0;
2227 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002228
2229 if (s->max_size == 0)
Anthony Liguori76a96442013-03-05 23:21:21 +05302230 return FALSE;
2231 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2232 &bytes_read, NULL);
2233 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002234 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302235 if (status != G_IO_STATUS_NORMAL) {
2236 return FALSE;
2237 }
aliguori6f97dba2008-10-31 18:49:55 +00002238
2239 s->bufptr = 0;
2240 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002241 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002242 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002243 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002244 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302245
2246 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002247}
2248
2249static void udp_chr_update_read_handler(CharDriverState *chr)
2250{
2251 NetCharDriver *s = chr->opaque;
2252
Anthony Liguori76a96442013-03-05 23:21:21 +05302253 if (s->tag) {
2254 g_source_remove(s->tag);
2255 s->tag = 0;
2256 }
2257
2258 if (s->chan) {
2259 s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002260 }
2261}
2262
aliguori819f56b2009-03-28 17:58:14 +00002263static void udp_chr_close(CharDriverState *chr)
2264{
2265 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302266 if (s->tag) {
2267 g_source_remove(s->tag);
2268 }
2269 if (s->chan) {
2270 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002271 closesocket(s->fd);
2272 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002273 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002274 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002275}
2276
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002277static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002278{
2279 CharDriverState *chr = NULL;
2280 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002281
Anthony Liguori7267c092011-08-20 22:09:37 -05002282 chr = g_malloc0(sizeof(CharDriverState));
2283 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002284
aliguori6f97dba2008-10-31 18:49:55 +00002285 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302286 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002287 s->bufcnt = 0;
2288 s->bufptr = 0;
2289 chr->opaque = s;
2290 chr->chr_write = udp_chr_write;
2291 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002292 chr->chr_close = udp_chr_close;
Markus Armbruster1f514702012-02-07 15:09:08 +01002293 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002294}
aliguori6f97dba2008-10-31 18:49:55 +00002295
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002296static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2297{
2298 Error *local_err = NULL;
2299 int fd = -1;
2300
2301 fd = inet_dgram_opts(opts, &local_err);
2302 if (fd < 0) {
2303 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002304 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002305 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002306}
2307
2308/***********************************************************/
2309/* TCP Net console */
2310
2311typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302312
2313 GIOChannel *chan, *listen_chan;
2314 guint tag, listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002315 int fd, listen_fd;
2316 int connected;
2317 int max_size;
2318 int do_telnetopt;
2319 int do_nodelay;
2320 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002321 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002322} TCPCharDriver;
2323
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302324static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002325
2326static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2327{
2328 TCPCharDriver *s = chr->opaque;
2329 if (s->connected) {
Anthony Liguori684a0962013-03-29 11:39:50 -05002330 return io_channel_send(s->chan, buf, len);
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002331 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002332 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002333 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002334 }
2335}
2336
2337static int tcp_chr_read_poll(void *opaque)
2338{
2339 CharDriverState *chr = opaque;
2340 TCPCharDriver *s = chr->opaque;
2341 if (!s->connected)
2342 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002343 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002344 return s->max_size;
2345}
2346
2347#define IAC 255
2348#define IAC_BREAK 243
2349static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2350 TCPCharDriver *s,
2351 uint8_t *buf, int *size)
2352{
2353 /* Handle any telnet client's basic IAC options to satisfy char by
2354 * char mode with no echo. All IAC options will be removed from
2355 * the buf and the do_telnetopt variable will be used to track the
2356 * state of the width of the IAC information.
2357 *
2358 * IAC commands come in sets of 3 bytes with the exception of the
2359 * "IAC BREAK" command and the double IAC.
2360 */
2361
2362 int i;
2363 int j = 0;
2364
2365 for (i = 0; i < *size; i++) {
2366 if (s->do_telnetopt > 1) {
2367 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2368 /* Double IAC means send an IAC */
2369 if (j != i)
2370 buf[j] = buf[i];
2371 j++;
2372 s->do_telnetopt = 1;
2373 } else {
2374 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2375 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002376 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002377 s->do_telnetopt++;
2378 }
2379 s->do_telnetopt++;
2380 }
2381 if (s->do_telnetopt >= 4) {
2382 s->do_telnetopt = 1;
2383 }
2384 } else {
2385 if ((unsigned char)buf[i] == IAC) {
2386 s->do_telnetopt = 2;
2387 } else {
2388 if (j != i)
2389 buf[j] = buf[i];
2390 j++;
2391 }
2392 }
2393 }
2394 *size = j;
2395}
2396
Mark McLoughlin7d174052009-07-22 09:11:39 +01002397static int tcp_get_msgfd(CharDriverState *chr)
2398{
2399 TCPCharDriver *s = chr->opaque;
Paolo Bonzinie53f27b2010-04-16 17:25:23 +02002400 int fd = s->msgfd;
2401 s->msgfd = -1;
2402 return fd;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002403}
2404
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002405#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002406static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2407{
2408 TCPCharDriver *s = chr->opaque;
2409 struct cmsghdr *cmsg;
2410
2411 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2412 int fd;
2413
2414 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2415 cmsg->cmsg_level != SOL_SOCKET ||
2416 cmsg->cmsg_type != SCM_RIGHTS)
2417 continue;
2418
2419 fd = *((int *)CMSG_DATA(cmsg));
2420 if (fd < 0)
2421 continue;
2422
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002423 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2424 qemu_set_block(fd);
2425
Corey Bryant06138652012-08-14 16:43:42 -04002426#ifndef MSG_CMSG_CLOEXEC
2427 qemu_set_cloexec(fd);
2428#endif
Mark McLoughlin7d174052009-07-22 09:11:39 +01002429 if (s->msgfd != -1)
2430 close(s->msgfd);
2431 s->msgfd = fd;
2432 }
2433}
2434
Mark McLoughlin9977c892009-07-22 09:11:38 +01002435static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2436{
2437 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002438 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002439 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002440 union {
2441 struct cmsghdr cmsg;
2442 char control[CMSG_SPACE(sizeof(int))];
2443 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002444 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002445 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002446
2447 iov[0].iov_base = buf;
2448 iov[0].iov_len = len;
2449
2450 msg.msg_iov = iov;
2451 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002452 msg.msg_control = &msg_control;
2453 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002454
Corey Bryant06138652012-08-14 16:43:42 -04002455#ifdef MSG_CMSG_CLOEXEC
2456 flags |= MSG_CMSG_CLOEXEC;
2457#endif
2458 ret = recvmsg(s->fd, &msg, flags);
2459 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002460 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002461 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002462
2463 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002464}
2465#else
2466static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2467{
2468 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002469 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002470}
2471#endif
2472
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302473static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2474{
2475 TCPCharDriver *s = chr->opaque;
2476 return g_io_create_watch(s->chan, cond);
2477}
2478
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302479static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002480{
2481 CharDriverState *chr = opaque;
2482 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302483 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002484 int len, size;
2485
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302486 if (!s->connected || s->max_size <= 0) {
2487 return FALSE;
2488 }
aliguori6f97dba2008-10-31 18:49:55 +00002489 len = sizeof(buf);
2490 if (len > s->max_size)
2491 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002492 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002493 if (size == 0) {
2494 /* connection closed */
2495 s->connected = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302496 if (s->listen_chan) {
2497 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002498 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302499 g_source_remove(s->tag);
2500 s->tag = 0;
2501 g_io_channel_unref(s->chan);
2502 s->chan = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002503 closesocket(s->fd);
2504 s->fd = -1;
Hans de Goedea425d232011-11-19 10:22:43 +01002505 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002506 } else if (size > 0) {
2507 if (s->do_telnetopt)
2508 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2509 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002510 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002511 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302512
2513 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002514}
2515
Blue Swirl68c18d12010-08-15 09:46:24 +00002516#ifndef _WIN32
2517CharDriverState *qemu_chr_open_eventfd(int eventfd)
2518{
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002519 return qemu_chr_open_fd(eventfd, eventfd);
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002520}
Blue Swirl68c18d12010-08-15 09:46:24 +00002521#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002522
aliguori6f97dba2008-10-31 18:49:55 +00002523static void tcp_chr_connect(void *opaque)
2524{
2525 CharDriverState *chr = opaque;
2526 TCPCharDriver *s = chr->opaque;
2527
2528 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302529 if (s->chan) {
2530 s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002531 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002532 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002533}
2534
2535#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2536static void tcp_chr_telnet_init(int fd)
2537{
2538 char buf[3];
2539 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2540 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2541 send(fd, (char *)buf, 3, 0);
2542 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2543 send(fd, (char *)buf, 3, 0);
2544 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2545 send(fd, (char *)buf, 3, 0);
2546 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2547 send(fd, (char *)buf, 3, 0);
2548}
2549
Daniel P. Berrange13661082011-06-23 13:31:42 +01002550static int tcp_chr_add_client(CharDriverState *chr, int fd)
2551{
2552 TCPCharDriver *s = chr->opaque;
2553 if (s->fd != -1)
2554 return -1;
2555
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002556 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002557 if (s->do_nodelay)
2558 socket_set_nodelay(fd);
2559 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302560 s->chan = io_channel_from_socket(fd);
2561 g_source_remove(s->listen_tag);
2562 s->listen_tag = 0;
Daniel P. Berrange13661082011-06-23 13:31:42 +01002563 tcp_chr_connect(chr);
2564
2565 return 0;
2566}
2567
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302568static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002569{
2570 CharDriverState *chr = opaque;
2571 TCPCharDriver *s = chr->opaque;
2572 struct sockaddr_in saddr;
2573#ifndef _WIN32
2574 struct sockaddr_un uaddr;
2575#endif
2576 struct sockaddr *addr;
2577 socklen_t len;
2578 int fd;
2579
2580 for(;;) {
2581#ifndef _WIN32
2582 if (s->is_unix) {
2583 len = sizeof(uaddr);
2584 addr = (struct sockaddr *)&uaddr;
2585 } else
2586#endif
2587 {
2588 len = sizeof(saddr);
2589 addr = (struct sockaddr *)&saddr;
2590 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002591 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002592 if (fd < 0 && errno != EINTR) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302593 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002594 } else if (fd >= 0) {
2595 if (s->do_telnetopt)
2596 tcp_chr_telnet_init(fd);
2597 break;
2598 }
2599 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002600 if (tcp_chr_add_client(chr, fd) < 0)
2601 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302602
2603 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002604}
2605
2606static void tcp_chr_close(CharDriverState *chr)
2607{
2608 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002609 if (s->fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302610 if (s->tag) {
2611 g_source_remove(s->tag);
2612 }
2613 if (s->chan) {
2614 g_io_channel_unref(s->chan);
2615 }
aliguori6f97dba2008-10-31 18:49:55 +00002616 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002617 }
2618 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302619 if (s->listen_tag) {
2620 g_source_remove(s->listen_tag);
2621 }
2622 if (s->listen_chan) {
2623 g_io_channel_unref(s->listen_chan);
2624 }
aliguori6f97dba2008-10-31 18:49:55 +00002625 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002626 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002627 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002628 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002629}
2630
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002631static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2632 bool is_listen, bool is_telnet,
2633 bool is_waitconnect,
2634 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002635{
2636 CharDriverState *chr = NULL;
2637 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002638 char host[NI_MAXHOST], serv[NI_MAXSERV];
2639 const char *left = "", *right = "";
2640 struct sockaddr_storage ss;
2641 socklen_t ss_len = sizeof(ss);
2642
2643 memset(&ss, 0, ss_len);
2644 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2645 error_setg(errp, "getsockname: %s", strerror(errno));
2646 return NULL;
2647 }
2648
2649 chr = g_malloc0(sizeof(CharDriverState));
2650 s = g_malloc0(sizeof(TCPCharDriver));
2651
2652 s->connected = 0;
2653 s->fd = -1;
2654 s->listen_fd = -1;
2655 s->msgfd = -1;
2656
2657 chr->filename = g_malloc(256);
2658 switch (ss.ss_family) {
2659#ifndef _WIN32
2660 case AF_UNIX:
2661 s->is_unix = 1;
2662 snprintf(chr->filename, 256, "unix:%s%s",
2663 ((struct sockaddr_un *)(&ss))->sun_path,
2664 is_listen ? ",server" : "");
2665 break;
2666#endif
2667 case AF_INET6:
2668 left = "[";
2669 right = "]";
2670 /* fall through */
2671 case AF_INET:
2672 s->do_nodelay = do_nodelay;
2673 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2674 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002675 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002676 is_telnet ? "telnet" : "tcp",
2677 left, host, right, serv,
2678 is_listen ? ",server" : "");
2679 break;
2680 }
2681
2682 chr->opaque = s;
2683 chr->chr_write = tcp_chr_write;
2684 chr->chr_close = tcp_chr_close;
2685 chr->get_msgfd = tcp_get_msgfd;
2686 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302687 chr->chr_add_watch = tcp_chr_add_watch;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002688
2689 if (is_listen) {
2690 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302691 s->listen_chan = io_channel_from_socket(s->listen_fd);
2692 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002693 if (is_telnet) {
2694 s->do_telnetopt = 1;
2695 }
2696 } else {
2697 s->connected = 1;
2698 s->fd = fd;
2699 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302700 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002701 tcp_chr_connect(chr);
2702 }
2703
2704 if (is_listen && is_waitconnect) {
2705 printf("QEMU waiting for connection on: %s\n",
2706 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302707 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002708 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002709 }
2710 return chr;
2711}
2712
2713static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2714{
2715 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002716 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002717 int fd = -1;
2718 int is_listen;
2719 int is_waitconnect;
2720 int do_nodelay;
2721 int is_unix;
2722 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002723
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002724 is_listen = qemu_opt_get_bool(opts, "server", 0);
2725 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2726 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2727 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2728 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002729 if (!is_listen)
2730 is_waitconnect = 0;
2731
aliguorif07b6002008-11-11 20:54:09 +00002732 if (is_unix) {
2733 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002734 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002735 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002736 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002737 }
2738 } else {
2739 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002740 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002741 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002742 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002743 }
2744 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002745 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002746 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002747 }
aliguori6f97dba2008-10-31 18:49:55 +00002748
2749 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002750 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002751
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002752 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2753 is_waitconnect, &local_err);
2754 if (error_is_set(&local_err)) {
2755 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002756 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002757 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002758
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002759
aliguori6f97dba2008-10-31 18:49:55 +00002760 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002761 if (local_err) {
2762 qerror_report_err(local_err);
2763 error_free(local_err);
2764 }
2765 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002766 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002767 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002768 if (chr) {
2769 g_free(chr->opaque);
2770 g_free(chr);
2771 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002772 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002773}
2774
Lei Li51767e72013-01-25 00:03:19 +08002775/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002776/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002777
2778typedef struct {
2779 size_t size;
2780 size_t prod;
2781 size_t cons;
2782 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002783} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002784
Markus Armbruster3949e592013-02-06 21:27:24 +01002785static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002786{
Markus Armbruster3949e592013-02-06 21:27:24 +01002787 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002788
Markus Armbruster5c230102013-02-06 21:27:23 +01002789 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002790}
2791
Markus Armbruster3949e592013-02-06 21:27:24 +01002792static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002793{
Markus Armbruster3949e592013-02-06 21:27:24 +01002794 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002795 int i;
2796
2797 if (!buf || (len < 0)) {
2798 return -1;
2799 }
2800
2801 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002802 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2803 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002804 d->cons = d->prod - d->size;
2805 }
2806 }
2807
2808 return 0;
2809}
2810
Markus Armbruster3949e592013-02-06 21:27:24 +01002811static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002812{
Markus Armbruster3949e592013-02-06 21:27:24 +01002813 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002814 int i;
2815
Markus Armbruster5c230102013-02-06 21:27:23 +01002816 for (i = 0; i < len && d->cons != d->prod; i++) {
2817 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08002818 }
2819
2820 return i;
2821}
2822
Markus Armbruster3949e592013-02-06 21:27:24 +01002823static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002824{
Markus Armbruster3949e592013-02-06 21:27:24 +01002825 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002826
2827 g_free(d->cbuf);
2828 g_free(d);
2829 chr->opaque = NULL;
2830}
2831
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002832static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
2833 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08002834{
2835 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01002836 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08002837
2838 chr = g_malloc0(sizeof(CharDriverState));
2839 d = g_malloc(sizeof(*d));
2840
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002841 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08002842
2843 /* The size must be power of 2 */
2844 if (d->size & (d->size - 1)) {
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01002845 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08002846 goto fail;
2847 }
2848
2849 d->prod = 0;
2850 d->cons = 0;
2851 d->cbuf = g_malloc0(d->size);
2852
2853 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01002854 chr->chr_write = ringbuf_chr_write;
2855 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08002856
2857 return chr;
2858
2859fail:
2860 g_free(d);
2861 g_free(chr);
2862 return NULL;
2863}
2864
Markus Armbruster3949e592013-02-06 21:27:24 +01002865static bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08002866{
Markus Armbruster3949e592013-02-06 21:27:24 +01002867 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08002868}
2869
Markus Armbruster3949e592013-02-06 21:27:24 +01002870void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01002871 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08002872 Error **errp)
2873{
2874 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002875 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08002876 int ret;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002877 size_t write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08002878
2879 chr = qemu_chr_find(device);
2880 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002881 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002882 return;
2883 }
2884
Markus Armbruster3949e592013-02-06 21:27:24 +01002885 if (!chr_is_ringbuf(chr)) {
2886 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08002887 return;
2888 }
2889
Lei Li1f590cf2013-01-25 00:03:20 +08002890 if (has_format && (format == DATA_FORMAT_BASE64)) {
2891 write_data = g_base64_decode(data, &write_count);
2892 } else {
2893 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01002894 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08002895 }
2896
Markus Armbruster3949e592013-02-06 21:27:24 +01002897 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08002898
Markus Armbruster13289fb2013-02-06 21:27:18 +01002899 if (write_data != (uint8_t *)data) {
2900 g_free((void *)write_data);
2901 }
2902
Lei Li1f590cf2013-01-25 00:03:20 +08002903 if (ret < 0) {
2904 error_setg(errp, "Failed to write to device %s", device);
2905 return;
2906 }
2907}
2908
Markus Armbruster3949e592013-02-06 21:27:24 +01002909char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002910 bool has_format, enum DataFormat format,
2911 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08002912{
2913 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01002914 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002915 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002916 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08002917
2918 chr = qemu_chr_find(device);
2919 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01002920 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08002921 return NULL;
2922 }
2923
Markus Armbruster3949e592013-02-06 21:27:24 +01002924 if (!chr_is_ringbuf(chr)) {
2925 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08002926 return NULL;
2927 }
2928
2929 if (size <= 0) {
2930 error_setg(errp, "size must be greater than zero");
2931 return NULL;
2932 }
2933
Markus Armbruster3949e592013-02-06 21:27:24 +01002934 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08002935 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002936 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08002937
Markus Armbruster3949e592013-02-06 21:27:24 +01002938 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08002939
2940 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002941 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01002942 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08002943 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01002944 /*
2945 * FIXME should read only complete, valid UTF-8 characters up
2946 * to @size bytes. Invalid sequences should be replaced by a
2947 * suitable replacement character. Except when (and only
2948 * when) ring buffer lost characters since last read, initial
2949 * continuation characters should be dropped.
2950 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01002951 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002952 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08002953 }
2954
Markus Armbruster3ab651f2013-02-06 21:27:15 +01002955 return data;
Lei Li49b6d722013-01-25 00:03:21 +08002956}
2957
Gerd Hoffmann33521632009-12-08 13:11:49 +01002958QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002959{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002960 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002961 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002962 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002963 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002964 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002965
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002966 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
2967 if (error_is_set(&local_err)) {
2968 qerror_report_err(local_err);
2969 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002970 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03002971 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002972
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002973 if (strstart(filename, "mon:", &p)) {
2974 filename = p;
2975 qemu_opt_set(opts, "mux", "on");
2976 }
2977
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002978 if (strcmp(filename, "null") == 0 ||
2979 strcmp(filename, "pty") == 0 ||
2980 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002981 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002982 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002983 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002984 return opts;
2985 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002986 if (strstart(filename, "vc", &p)) {
2987 qemu_opt_set(opts, "backend", "vc");
2988 if (*p == ':') {
2989 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2990 /* pixels */
2991 qemu_opt_set(opts, "width", width);
2992 qemu_opt_set(opts, "height", height);
2993 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2994 /* chars */
2995 qemu_opt_set(opts, "cols", width);
2996 qemu_opt_set(opts, "rows", height);
2997 } else {
2998 goto fail;
2999 }
3000 }
3001 return opts;
3002 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003003 if (strcmp(filename, "con:") == 0) {
3004 qemu_opt_set(opts, "backend", "console");
3005 return opts;
3006 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003007 if (strstart(filename, "COM", NULL)) {
3008 qemu_opt_set(opts, "backend", "serial");
3009 qemu_opt_set(opts, "path", filename);
3010 return opts;
3011 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003012 if (strstart(filename, "file:", &p)) {
3013 qemu_opt_set(opts, "backend", "file");
3014 qemu_opt_set(opts, "path", p);
3015 return opts;
3016 }
3017 if (strstart(filename, "pipe:", &p)) {
3018 qemu_opt_set(opts, "backend", "pipe");
3019 qemu_opt_set(opts, "path", p);
3020 return opts;
3021 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003022 if (strstart(filename, "tcp:", &p) ||
3023 strstart(filename, "telnet:", &p)) {
3024 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3025 host[0] = 0;
3026 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3027 goto fail;
3028 }
3029 qemu_opt_set(opts, "backend", "socket");
3030 qemu_opt_set(opts, "host", host);
3031 qemu_opt_set(opts, "port", port);
3032 if (p[pos] == ',') {
3033 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3034 goto fail;
3035 }
3036 if (strstart(filename, "telnet:", &p))
3037 qemu_opt_set(opts, "telnet", "on");
3038 return opts;
3039 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003040 if (strstart(filename, "udp:", &p)) {
3041 qemu_opt_set(opts, "backend", "udp");
3042 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3043 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003044 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003045 goto fail;
3046 }
3047 }
3048 qemu_opt_set(opts, "host", host);
3049 qemu_opt_set(opts, "port", port);
3050 if (p[pos] == '@') {
3051 p += pos + 1;
3052 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3053 host[0] = 0;
3054 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003055 goto fail;
3056 }
3057 }
3058 qemu_opt_set(opts, "localaddr", host);
3059 qemu_opt_set(opts, "localport", port);
3060 }
3061 return opts;
3062 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003063 if (strstart(filename, "unix:", &p)) {
3064 qemu_opt_set(opts, "backend", "socket");
3065 if (qemu_opts_do_parse(opts, p, "path") != 0)
3066 goto fail;
3067 return opts;
3068 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003069 if (strstart(filename, "/dev/parport", NULL) ||
3070 strstart(filename, "/dev/ppi", NULL)) {
3071 qemu_opt_set(opts, "backend", "parport");
3072 qemu_opt_set(opts, "path", filename);
3073 return opts;
3074 }
3075 if (strstart(filename, "/dev/", NULL)) {
3076 qemu_opt_set(opts, "backend", "tty");
3077 qemu_opt_set(opts, "path", filename);
3078 return opts;
3079 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003080
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003081fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003082 qemu_opts_del(opts);
3083 return NULL;
3084}
3085
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003086static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3087 Error **errp)
3088{
3089 const char *path = qemu_opt_get(opts, "path");
3090
3091 if (path == NULL) {
3092 error_setg(errp, "chardev: file: no filename given");
3093 return;
3094 }
3095 backend->file = g_new0(ChardevFile, 1);
3096 backend->file->out = g_strdup(path);
3097}
3098
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003099static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3100 Error **errp)
3101{
3102 backend->stdio = g_new0(ChardevStdio, 1);
3103 backend->stdio->has_signal = true;
3104 backend->stdio->signal =
3105 qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC);
3106}
3107
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003108static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3109 Error **errp)
3110{
3111 const char *device = qemu_opt_get(opts, "path");
3112
3113 if (device == NULL) {
3114 error_setg(errp, "chardev: serial/tty: no device path given");
3115 return;
3116 }
3117 backend->serial = g_new0(ChardevHostdev, 1);
3118 backend->serial->device = g_strdup(device);
3119}
3120
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003121static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3122 Error **errp)
3123{
3124 const char *device = qemu_opt_get(opts, "path");
3125
3126 if (device == NULL) {
3127 error_setg(errp, "chardev: parallel: no device path given");
3128 return;
3129 }
3130 backend->parallel = g_new0(ChardevHostdev, 1);
3131 backend->parallel->device = g_strdup(device);
3132}
3133
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003134static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3135 Error **errp)
3136{
3137 const char *device = qemu_opt_get(opts, "path");
3138
3139 if (device == NULL) {
3140 error_setg(errp, "chardev: pipe: no device path given");
3141 return;
3142 }
3143 backend->pipe = g_new0(ChardevHostdev, 1);
3144 backend->pipe->device = g_strdup(device);
3145}
3146
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003147static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3148 Error **errp)
3149{
3150 int val;
3151
3152 backend->memory = g_new0(ChardevRingbuf, 1);
3153
3154 val = qemu_opt_get_number(opts, "size", 0);
3155 if (val != 0) {
3156 backend->memory->has_size = true;
3157 backend->memory->size = val;
3158 }
3159}
3160
Anthony Liguorid654f342013-03-05 23:21:28 +05303161typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003162 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003163 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003164 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003165 /* new, qapi-based */
3166 int kind;
3167 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303168} CharDriver;
3169
3170static GSList *backends;
3171
3172void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3173{
3174 CharDriver *s;
3175
3176 s = g_malloc0(sizeof(*s));
3177 s->name = g_strdup(name);
3178 s->open = open;
3179
3180 backends = g_slist_append(backends, s);
3181}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003182
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003183void register_char_driver_qapi(const char *name, int kind,
3184 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3185{
3186 CharDriver *s;
3187
3188 s = g_malloc0(sizeof(*s));
3189 s->name = g_strdup(name);
3190 s->kind = kind;
3191 s->parse = parse;
3192
3193 backends = g_slist_append(backends, s);
3194}
3195
Anthony Liguorif69554b2011-08-15 11:17:37 -05003196CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003197 void (*init)(struct CharDriverState *s),
3198 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003199{
Anthony Liguorid654f342013-03-05 23:21:28 +05303200 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003201 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303202 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003203
3204 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003205 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003206 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003207 }
3208
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003209 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003210 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003211 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003212 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003213 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303214 for (i = backends; i; i = i->next) {
3215 cd = i->data;
3216
3217 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003218 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303219 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003220 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303221 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003222 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003223 qemu_opt_get(opts, "backend"));
Anthony Liguorid654f342013-03-05 23:21:28 +05303224 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003225 }
3226
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003227 if (!cd->open) {
3228 /* using new, qapi init */
3229 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3230 ChardevReturn *ret = NULL;
3231 const char *id = qemu_opts_id(opts);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003232 const char *bid = NULL;
3233
3234 if (qemu_opt_get_bool(opts, "mux", 0)) {
3235 bid = g_strdup_printf("%s-base", id);
3236 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003237
3238 chr = NULL;
3239 backend->kind = cd->kind;
3240 if (cd->parse) {
3241 cd->parse(opts, backend, errp);
3242 if (error_is_set(errp)) {
3243 goto qapi_out;
3244 }
3245 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003246 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003247 if (error_is_set(errp)) {
3248 goto qapi_out;
3249 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003250
3251 if (bid) {
3252 qapi_free_ChardevBackend(backend);
3253 qapi_free_ChardevReturn(ret);
3254 backend = g_new0(ChardevBackend, 1);
3255 backend->mux = g_new0(ChardevMux, 1);
3256 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3257 backend->mux->chardev = g_strdup(bid);
3258 ret = qmp_chardev_add(id, backend, errp);
3259 if (error_is_set(errp)) {
3260 goto qapi_out;
3261 }
3262 }
3263
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003264 chr = qemu_chr_find(id);
3265
3266 qapi_out:
3267 qapi_free_ChardevBackend(backend);
3268 qapi_free_ChardevReturn(ret);
3269 return chr;
3270 }
3271
Anthony Liguorid654f342013-03-05 23:21:28 +05303272 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003273 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003274 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003275 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003276 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003277 }
3278
3279 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003280 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003281 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003282 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003283
3284 if (qemu_opt_get_bool(opts, "mux", 0)) {
3285 CharDriverState *base = chr;
3286 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003287 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003288 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3289 chr = qemu_chr_open_mux(base);
3290 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003291 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003292 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003293 } else {
3294 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003295 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003296 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003297 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003298 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003299
3300err:
3301 qemu_opts_del(opts);
3302 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003303}
3304
Anthony Liguori27143a42011-08-15 11:17:36 -05003305CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003306{
3307 const char *p;
3308 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003309 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003310 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003311
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003312 if (strstart(filename, "chardev:", &p)) {
3313 return qemu_chr_find(p);
3314 }
3315
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003316 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003317 if (!opts)
3318 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003319
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003320 chr = qemu_chr_new_from_opts(opts, init, &err);
3321 if (error_is_set(&err)) {
3322 fprintf(stderr, "%s\n", error_get_pretty(err));
3323 error_free(err);
3324 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003325 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003326 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003327 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003328 }
3329 return chr;
3330}
3331
Anthony Liguori15f31512011-08-15 11:17:35 -05003332void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003333{
3334 if (chr->chr_set_echo) {
3335 chr->chr_set_echo(chr, echo);
3336 }
3337}
3338
Hans de Goede8e25daa2013-03-26 11:07:57 +01003339void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003340{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003341 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003342 return;
3343 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003344 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003345 if (chr->chr_set_fe_open) {
3346 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003347 }
3348}
3349
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003350int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3351 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303352{
3353 GSource *src;
3354 guint tag;
3355
3356 if (s->chr_add_watch == NULL) {
3357 return -ENOSYS;
3358 }
3359
3360 src = s->chr_add_watch(s, cond);
3361 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3362 tag = g_source_attach(src, NULL);
3363 g_source_unref(src);
3364
3365 return tag;
3366}
3367
Hans de Goede44c473d2013-03-27 20:29:39 +01003368int qemu_chr_fe_claim(CharDriverState *s)
3369{
3370 if (s->avail_connections < 1) {
3371 return -1;
3372 }
3373 s->avail_connections--;
3374 return 0;
3375}
3376
3377void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3378{
3379 if (qemu_chr_fe_claim(s) != 0) {
3380 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3381 __func__, s->label);
3382 exit(1);
3383 }
3384}
3385
3386void qemu_chr_fe_release(CharDriverState *s)
3387{
3388 s->avail_connections++;
3389}
3390
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003391void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003392{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003393 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003394 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003395 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003396 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003397 g_free(chr->filename);
3398 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003399 if (chr->opts) {
3400 qemu_opts_del(chr->opts);
3401 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003402 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003403}
3404
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003405ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003406{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003407 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003408 CharDriverState *chr;
3409
Blue Swirl72cf2d42009-09-12 07:36:22 +00003410 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003411 ChardevInfoList *info = g_malloc0(sizeof(*info));
3412 info->value = g_malloc0(sizeof(*info->value));
3413 info->value->label = g_strdup(chr->label);
3414 info->value->filename = g_strdup(chr->filename);
3415
3416 info->next = chr_list;
3417 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003418 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003419
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003420 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003421}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003422
3423CharDriverState *qemu_chr_find(const char *name)
3424{
3425 CharDriverState *chr;
3426
Blue Swirl72cf2d42009-09-12 07:36:22 +00003427 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003428 if (strcmp(chr->label, name) != 0)
3429 continue;
3430 return chr;
3431 }
3432 return NULL;
3433}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003434
3435/* Get a character (serial) device interface. */
3436CharDriverState *qemu_char_get_next_serial(void)
3437{
3438 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003439 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003440
3441 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003442
3443 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3444 chr = serial_hds[next_serial++];
3445 qemu_chr_fe_claim_no_fail(chr);
3446 return chr;
3447 }
3448 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003449}
3450
Paolo Bonzini4d454572012-11-26 16:03:42 +01003451QemuOptsList qemu_chardev_opts = {
3452 .name = "chardev",
3453 .implied_opt_name = "backend",
3454 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3455 .desc = {
3456 {
3457 .name = "backend",
3458 .type = QEMU_OPT_STRING,
3459 },{
3460 .name = "path",
3461 .type = QEMU_OPT_STRING,
3462 },{
3463 .name = "host",
3464 .type = QEMU_OPT_STRING,
3465 },{
3466 .name = "port",
3467 .type = QEMU_OPT_STRING,
3468 },{
3469 .name = "localaddr",
3470 .type = QEMU_OPT_STRING,
3471 },{
3472 .name = "localport",
3473 .type = QEMU_OPT_STRING,
3474 },{
3475 .name = "to",
3476 .type = QEMU_OPT_NUMBER,
3477 },{
3478 .name = "ipv4",
3479 .type = QEMU_OPT_BOOL,
3480 },{
3481 .name = "ipv6",
3482 .type = QEMU_OPT_BOOL,
3483 },{
3484 .name = "wait",
3485 .type = QEMU_OPT_BOOL,
3486 },{
3487 .name = "server",
3488 .type = QEMU_OPT_BOOL,
3489 },{
3490 .name = "delay",
3491 .type = QEMU_OPT_BOOL,
3492 },{
3493 .name = "telnet",
3494 .type = QEMU_OPT_BOOL,
3495 },{
3496 .name = "width",
3497 .type = QEMU_OPT_NUMBER,
3498 },{
3499 .name = "height",
3500 .type = QEMU_OPT_NUMBER,
3501 },{
3502 .name = "cols",
3503 .type = QEMU_OPT_NUMBER,
3504 },{
3505 .name = "rows",
3506 .type = QEMU_OPT_NUMBER,
3507 },{
3508 .name = "mux",
3509 .type = QEMU_OPT_BOOL,
3510 },{
3511 .name = "signal",
3512 .type = QEMU_OPT_BOOL,
3513 },{
3514 .name = "name",
3515 .type = QEMU_OPT_STRING,
3516 },{
3517 .name = "debug",
3518 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003519 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003520 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003521 .type = QEMU_OPT_SIZE,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003522 },
3523 { /* end of list */ }
3524 },
3525};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003526
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003527#ifdef _WIN32
3528
3529static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3530{
3531 HANDLE out;
3532
3533 if (file->in) {
3534 error_setg(errp, "input file not supported");
3535 return NULL;
3536 }
3537
3538 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3539 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3540 if (out == INVALID_HANDLE_VALUE) {
3541 error_setg(errp, "open %s failed", file->out);
3542 return NULL;
3543 }
3544 return qemu_chr_open_win_file(out);
3545}
3546
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003547static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3548 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003549{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003550 return qemu_chr_open_win_path(serial->device);
3551}
3552
3553static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3554 Error **errp)
3555{
3556 error_setg(errp, "character device backend type 'parallel' not supported");
3557 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003558}
3559
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003560#else /* WIN32 */
3561
3562static int qmp_chardev_open_file_source(char *src, int flags,
3563 Error **errp)
3564{
3565 int fd = -1;
3566
3567 TFR(fd = qemu_open(src, flags, 0666));
3568 if (fd == -1) {
3569 error_setg(errp, "open %s: %s", src, strerror(errno));
3570 }
3571 return fd;
3572}
3573
3574static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3575{
3576 int flags, in = -1, out = -1;
3577
3578 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3579 out = qmp_chardev_open_file_source(file->out, flags, errp);
3580 if (error_is_set(errp)) {
3581 return NULL;
3582 }
3583
3584 if (file->in) {
3585 flags = O_RDONLY;
3586 in = qmp_chardev_open_file_source(file->in, flags, errp);
3587 if (error_is_set(errp)) {
3588 qemu_close(out);
3589 return NULL;
3590 }
3591 }
3592
3593 return qemu_chr_open_fd(in, out);
3594}
3595
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003596static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3597 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003598{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003599#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003600 int fd;
3601
3602 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
3603 if (error_is_set(errp)) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003604 return NULL;
3605 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003606 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003607 return qemu_chr_open_tty_fd(fd);
3608#else
3609 error_setg(errp, "character device backend type 'serial' not supported");
3610 return NULL;
3611#endif
3612}
3613
3614static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3615 Error **errp)
3616{
3617#ifdef HAVE_CHARDEV_PARPORT
3618 int fd;
3619
3620 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
3621 if (error_is_set(errp)) {
3622 return NULL;
3623 }
3624 return qemu_chr_open_pp_fd(fd);
3625#else
3626 error_setg(errp, "character device backend type 'parallel' not supported");
3627 return NULL;
3628#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003629}
3630
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003631#endif /* WIN32 */
3632
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003633static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3634 Error **errp)
3635{
3636 SocketAddress *addr = sock->addr;
3637 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3638 bool is_listen = sock->has_server ? sock->server : true;
3639 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3640 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3641 int fd;
3642
3643 if (is_listen) {
3644 fd = socket_listen(addr, errp);
3645 } else {
3646 fd = socket_connect(addr, errp, NULL, NULL);
3647 }
3648 if (error_is_set(errp)) {
3649 return NULL;
3650 }
3651 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3652 is_telnet, is_waitconnect, errp);
3653}
3654
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003655static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
3656 Error **errp)
3657{
3658 int fd;
3659
3660 fd = socket_dgram(dgram->remote, dgram->local, errp);
3661 if (error_is_set(errp)) {
3662 return NULL;
3663 }
3664 return qemu_chr_open_udp_fd(fd);
3665}
3666
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003667ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3668 Error **errp)
3669{
3670 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003671 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003672
3673 chr = qemu_chr_find(id);
3674 if (chr) {
3675 error_setg(errp, "Chardev '%s' already exists", id);
3676 g_free(ret);
3677 return NULL;
3678 }
3679
3680 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003681 case CHARDEV_BACKEND_KIND_FILE:
3682 chr = qmp_chardev_open_file(backend->file, errp);
3683 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003684 case CHARDEV_BACKEND_KIND_SERIAL:
3685 chr = qmp_chardev_open_serial(backend->serial, errp);
3686 break;
3687 case CHARDEV_BACKEND_KIND_PARALLEL:
3688 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003689 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003690 case CHARDEV_BACKEND_KIND_PIPE:
3691 chr = qemu_chr_open_pipe(backend->pipe);
3692 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003693 case CHARDEV_BACKEND_KIND_SOCKET:
3694 chr = qmp_chardev_open_socket(backend->socket, errp);
3695 break;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003696 case CHARDEV_BACKEND_KIND_DGRAM:
3697 chr = qmp_chardev_open_dgram(backend->dgram, errp);
3698 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003699#ifdef HAVE_CHARDEV_TTY
3700 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003701 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003702 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003703#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003704 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003705 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003706 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003707 case CHARDEV_BACKEND_KIND_MUX:
3708 base = qemu_chr_find(backend->mux->chardev);
3709 if (base == NULL) {
3710 error_setg(errp, "mux: base chardev %s not found",
3711 backend->mux->chardev);
3712 break;
3713 }
3714 chr = qemu_chr_open_mux(base);
3715 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003716 case CHARDEV_BACKEND_KIND_MSMOUSE:
3717 chr = qemu_chr_open_msmouse();
3718 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003719#ifdef CONFIG_BRLAPI
3720 case CHARDEV_BACKEND_KIND_BRAILLE:
3721 chr = chr_baum_init();
3722 break;
3723#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003724 case CHARDEV_BACKEND_KIND_STDIO:
3725 chr = qemu_chr_open_stdio(backend->stdio);
3726 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003727#ifdef _WIN32
3728 case CHARDEV_BACKEND_KIND_CONSOLE:
3729 chr = qemu_chr_open_win_con();
3730 break;
3731#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003732#ifdef CONFIG_SPICE
3733 case CHARDEV_BACKEND_KIND_SPICEVMC:
3734 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3735 break;
3736 case CHARDEV_BACKEND_KIND_SPICEPORT:
3737 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3738 break;
3739#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003740 case CHARDEV_BACKEND_KIND_VC:
3741 chr = vc_init(backend->vc);
3742 break;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003743 case CHARDEV_BACKEND_KIND_MEMORY:
3744 chr = qemu_chr_open_ringbuf(backend->memory, errp);
3745 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003746 default:
3747 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3748 break;
3749 }
3750
3751 if (chr == NULL && !error_is_set(errp)) {
3752 error_setg(errp, "Failed to create chardev");
3753 }
3754 if (chr) {
3755 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003756 chr->avail_connections =
3757 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003758 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
3759 return ret;
3760 } else {
3761 g_free(ret);
3762 return NULL;
3763 }
3764}
3765
3766void qmp_chardev_remove(const char *id, Error **errp)
3767{
3768 CharDriverState *chr;
3769
3770 chr = qemu_chr_find(id);
3771 if (NULL == chr) {
3772 error_setg(errp, "Chardev '%s' not found", id);
3773 return;
3774 }
3775 if (chr->chr_can_read || chr->chr_read ||
3776 chr->chr_event || chr->handler_opaque) {
3777 error_setg(errp, "Chardev '%s' is busy", id);
3778 return;
3779 }
3780 qemu_chr_delete(chr);
3781}
Anthony Liguorid654f342013-03-05 23:21:28 +05303782
3783static void register_types(void)
3784{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003785 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05303786 register_char_driver("socket", qemu_chr_open_socket);
3787 register_char_driver("udp", qemu_chr_open_udp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003788 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
3789 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003790 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
3791 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003792 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
3793 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003794 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
3795 qemu_chr_parse_serial);
3796 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
3797 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003798 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
3799 qemu_chr_parse_parallel);
3800 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
3801 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003802 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003803 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003804 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
3805 qemu_chr_parse_pipe);
Anthony Liguorid654f342013-03-05 23:21:28 +05303806}
3807
3808type_init(register_types);