blob: b9bef443de265a97a7f7e6af8a30b71dc1959bb5 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
aliguori6f97dba2008-10-31 18:49:55 +000032
33#include <unistd.h>
34#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000035#include <time.h>
36#include <errno.h>
37#include <sys/time.h>
38#include <zlib.h>
39
40#ifndef _WIN32
41#include <sys/times.h>
42#include <sys/wait.h>
43#include <termios.h>
44#include <sys/mman.h>
45#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000047#include <sys/socket.h>
48#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <dirent.h>
52#include <netdb.h>
53#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000055#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57#include <dev/ppbus/ppi.h>
58#include <dev/ppbus/ppbconf.h>
59#elif defined(__DragonFly__)
60#include <dev/misc/ppi/ppi.h>
61#include <bus/ppbus/ppbconf.h>
62#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010063#else
aliguori6f97dba2008-10-31 18:49:55 +000064#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000065#include <linux/ppdev.h>
66#include <linux/parport.h>
67#endif
68#ifdef __sun__
69#include <sys/stat.h>
70#include <sys/ethernet.h>
71#include <sys/sockio.h>
72#include <netinet/arp.h>
73#include <netinet/in.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_icmp.h> // must come after ip.h
77#include <netinet/udp.h>
78#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000079#endif
80#endif
81#endif
82
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010083#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020084#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000085
Amit Shah9bd78542009-11-03 19:59:54 +053086#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030087#define READ_RETRIES 10
Amit Shah9bd78542009-11-03 19:59:54 +053088
aliguori6f97dba2008-10-31 18:49:55 +000089/***********************************************************/
90/* character device */
91
Blue Swirl72cf2d42009-09-12 07:36:22 +000092static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
93 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +000094
Hans de Goedea425d232011-11-19 10:22:43 +010095void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +000096{
Alexander Graf73cdf3f2010-04-01 18:42:39 +020097 /* Keep track if the char device is open */
98 switch (event) {
99 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100100 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200101 break;
102 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100103 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200104 break;
105 }
106
aliguori6f97dba2008-10-31 18:49:55 +0000107 if (!s->chr_event)
108 return;
109 s->chr_event(s->handler_opaque, event);
110}
111
Hans de Goedefee204f2013-03-26 11:07:54 +0100112void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000113{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500114 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000115}
116
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500117int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000118{
119 return s->chr_write(s, buf, len);
120}
121
Anthony Liguoricd187202013-03-26 10:04:17 -0500122int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
123{
124 int offset = 0;
125 int res;
126
127 while (offset < len) {
128 do {
129 res = s->chr_write(s, buf + offset, len - offset);
130 if (res == -1 && errno == EAGAIN) {
131 g_usleep(100);
132 }
133 } while (res == -1 && errno == EAGAIN);
134
135 if (res == 0) {
136 break;
137 }
138
139 if (res < 0) {
140 return res;
141 }
142
143 offset += res;
144 }
145
146 return offset;
147}
148
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300149int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
150{
151 int offset = 0, counter = 10;
152 int res;
153
154 if (!s->chr_sync_read) {
155 return 0;
156 }
157
158 while (offset < len) {
159 do {
160 res = s->chr_sync_read(s, buf + offset, len - offset);
161 if (res == -1 && errno == EAGAIN) {
162 g_usleep(100);
163 }
164 } while (res == -1 && errno == EAGAIN);
165
166 if (res == 0) {
167 break;
168 }
169
170 if (res < 0) {
171 return res;
172 }
173
174 offset += res;
175
176 if (!counter--) {
177 break;
178 }
179 }
180
181 return offset;
182}
183
Anthony Liguori41084f12011-08-15 11:17:34 -0500184int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000185{
186 if (!s->chr_ioctl)
187 return -ENOTSUP;
188 return s->chr_ioctl(s, cmd, arg);
189}
190
Anthony Liguori909cda12011-08-15 11:17:31 -0500191int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000192{
193 if (!s->chr_can_read)
194 return 0;
195 return s->chr_can_read(s->handler_opaque);
196}
197
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500198void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000199{
Stefan Weilac310732012-04-19 22:27:14 +0200200 if (s->chr_read) {
201 s->chr_read(s->handler_opaque, buf, len);
202 }
aliguori6f97dba2008-10-31 18:49:55 +0000203}
204
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500205int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100206{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300207 int fd;
208 return (qemu_chr_fe_get_msgfds(s, &fd, 1) >= 0) ? fd : -1;
209}
210
211int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
212{
213 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100214}
215
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300216int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
217{
218 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
219}
220
Daniel P. Berrange13661082011-06-23 13:31:42 +0100221int qemu_chr_add_client(CharDriverState *s, int fd)
222{
223 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
224}
225
aliguori6f97dba2008-10-31 18:49:55 +0000226void qemu_chr_accept_input(CharDriverState *s)
227{
228 if (s->chr_accept_input)
229 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100230 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000231}
232
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500233void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000234{
Amit Shah9bd78542009-11-03 19:59:54 +0530235 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000236 va_list ap;
237 va_start(ap, fmt);
238 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500239 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000240 va_end(ap);
241}
242
Amit Shah386a5a12013-08-28 15:24:05 +0530243static void remove_fd_in_watch(CharDriverState *chr);
244
aliguori6f97dba2008-10-31 18:49:55 +0000245void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100246 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000247 IOReadHandler *fd_read,
248 IOEventHandler *fd_event,
249 void *opaque)
250{
Hans de Goede19083222013-03-26 11:07:56 +0100251 int fe_open;
252
Amit Shahda7d9982011-04-25 15:18:22 +0530253 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100254 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530255 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100256 } else {
257 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530258 }
aliguori6f97dba2008-10-31 18:49:55 +0000259 s->chr_can_read = fd_can_read;
260 s->chr_read = fd_read;
261 s->chr_event = fd_event;
262 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200263 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000264 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200265
Hans de Goede19083222013-03-26 11:07:56 +0100266 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100267 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100268 }
269
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200270 /* We're connecting to an already opened device, so let's make sure we
271 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100272 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100273 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200274 }
aliguori6f97dba2008-10-31 18:49:55 +0000275}
276
277static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
278{
279 return len;
280}
281
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100282static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000283{
284 CharDriverState *chr;
285
Anthony Liguori7267c092011-08-20 22:09:37 -0500286 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000287 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500288 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100289 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000290}
291
292/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000293#define MAX_MUX 4
294#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
295#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
296typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100297 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000298 IOReadHandler *chr_read[MAX_MUX];
299 IOEventHandler *chr_event[MAX_MUX];
300 void *ext_opaque[MAX_MUX];
301 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200302 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000303 int mux_cnt;
304 int term_got_escape;
305 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000306 /* Intermediate input buffer allows to catch escape sequences even if the
307 currently active device is not accepting any input - but only until it
308 is full as well. */
309 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
310 int prod[MAX_MUX];
311 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200312 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200313 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200314 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000315} MuxDriver;
316
317
318static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
319{
320 MuxDriver *d = chr->opaque;
321 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200322 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000323 ret = d->drv->chr_write(d->drv, buf, len);
324 } else {
325 int i;
326
327 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200328 for (i = 0; i < len; i++) {
329 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000330 char buf1[64];
331 int64_t ti;
332 int secs;
333
Alex Blighbc72ad62013-08-21 16:03:08 +0100334 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200335 if (d->timestamps_start == -1)
336 d->timestamps_start = ti;
337 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000338 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000339 snprintf(buf1, sizeof(buf1),
340 "[%02d:%02d:%02d.%03d] ",
341 secs / 3600,
342 (secs / 60) % 60,
343 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000344 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000345 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200346 d->linestart = 0;
347 }
348 ret += d->drv->chr_write(d->drv, buf+i, 1);
349 if (buf[i] == '\n') {
350 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000351 }
352 }
353 }
354 return ret;
355}
356
357static const char * const mux_help[] = {
358 "% h print this help\n\r",
359 "% x exit emulator\n\r",
360 "% s save disk data back to file (if -snapshot)\n\r",
361 "% t toggle console timestamps\n\r"
362 "% b send break (magic sysrq)\n\r",
363 "% c switch between console and monitor\n\r",
364 "% % sends %\n\r",
365 NULL
366};
367
368int term_escape_char = 0x01; /* ctrl-a is used for escape */
369static void mux_print_help(CharDriverState *chr)
370{
371 int i, j;
372 char ebuf[15] = "Escape-Char";
373 char cbuf[50] = "\n\r";
374
375 if (term_escape_char > 0 && term_escape_char < 26) {
376 snprintf(cbuf, sizeof(cbuf), "\n\r");
377 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
378 } else {
379 snprintf(cbuf, sizeof(cbuf),
380 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
381 term_escape_char);
382 }
383 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
384 for (i = 0; mux_help[i] != NULL; i++) {
385 for (j=0; mux_help[i][j] != '\0'; j++) {
386 if (mux_help[i][j] == '%')
387 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
388 else
389 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
390 }
391 }
392}
393
aliguori2724b182009-03-05 23:01:47 +0000394static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
395{
396 if (d->chr_event[mux_nr])
397 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
398}
399
aliguori6f97dba2008-10-31 18:49:55 +0000400static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
401{
402 if (d->term_got_escape) {
403 d->term_got_escape = 0;
404 if (ch == term_escape_char)
405 goto send_char;
406 switch(ch) {
407 case '?':
408 case 'h':
409 mux_print_help(chr);
410 break;
411 case 'x':
412 {
413 const char *term = "QEMU: Terminated\n\r";
414 chr->chr_write(chr,(uint8_t *)term,strlen(term));
415 exit(0);
416 break;
417 }
418 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200419 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000420 break;
421 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100422 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000423 break;
424 case 'c':
425 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200426 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
427 d->focus++;
428 if (d->focus >= d->mux_cnt)
429 d->focus = 0;
430 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000431 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200432 case 't':
433 d->timestamps = !d->timestamps;
434 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200435 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200436 break;
aliguori6f97dba2008-10-31 18:49:55 +0000437 }
438 } else if (ch == term_escape_char) {
439 d->term_got_escape = 1;
440 } else {
441 send_char:
442 return 1;
443 }
444 return 0;
445}
446
447static void mux_chr_accept_input(CharDriverState *chr)
448{
aliguori6f97dba2008-10-31 18:49:55 +0000449 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200450 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000451
aliguoria80bf992009-03-05 23:00:02 +0000452 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000453 d->chr_can_read[m] &&
454 d->chr_can_read[m](d->ext_opaque[m])) {
455 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000456 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000457 }
458}
459
460static int mux_chr_can_read(void *opaque)
461{
462 CharDriverState *chr = opaque;
463 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200464 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000465
aliguoria80bf992009-03-05 23:00:02 +0000466 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000467 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000468 if (d->chr_can_read[m])
469 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000470 return 0;
471}
472
473static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
474{
475 CharDriverState *chr = opaque;
476 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200477 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000478 int i;
479
480 mux_chr_accept_input (opaque);
481
482 for(i = 0; i < size; i++)
483 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000484 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000485 d->chr_can_read[m] &&
486 d->chr_can_read[m](d->ext_opaque[m]))
487 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
488 else
aliguoria80bf992009-03-05 23:00:02 +0000489 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000490 }
491}
492
493static void mux_chr_event(void *opaque, int event)
494{
495 CharDriverState *chr = opaque;
496 MuxDriver *d = chr->opaque;
497 int i;
498
499 /* Send the event to all registered listeners */
500 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000501 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000502}
503
504static void mux_chr_update_read_handler(CharDriverState *chr)
505{
506 MuxDriver *d = chr->opaque;
507
508 if (d->mux_cnt >= MAX_MUX) {
509 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
510 return;
511 }
512 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
513 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
514 d->chr_read[d->mux_cnt] = chr->chr_read;
515 d->chr_event[d->mux_cnt] = chr->chr_event;
516 /* Fix up the real driver with mux routines */
517 if (d->mux_cnt == 0) {
518 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
519 mux_chr_event, chr);
520 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200521 if (d->focus != -1) {
522 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200523 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200524 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000525 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200526 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000527}
528
Michael Roth7b7ab182013-07-30 13:04:22 -0500529static bool muxes_realized;
530
531/**
532 * Called after processing of default and command-line-specified
533 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
534 * to a mux chardev. This is done here to ensure that
535 * output/prompts/banners are only displayed for the FE that has
536 * focus when initial command-line processing/machine init is
537 * completed.
538 *
539 * After this point, any new FE attached to any new or existing
540 * mux will receive CHR_EVENT_OPENED notifications for the BE
541 * immediately.
542 */
543static void muxes_realize_done(Notifier *notifier, void *unused)
544{
545 CharDriverState *chr;
546
547 QTAILQ_FOREACH(chr, &chardevs, next) {
548 if (chr->is_mux) {
549 MuxDriver *d = chr->opaque;
550 int i;
551
552 /* send OPENED to all already-attached FEs */
553 for (i = 0; i < d->mux_cnt; i++) {
554 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
555 }
556 /* mark mux as OPENED so any new FEs will immediately receive
557 * OPENED event
558 */
559 qemu_chr_be_generic_open(chr);
560 }
561 }
562 muxes_realized = true;
563}
564
565static Notifier muxes_realize_notify = {
566 .notify = muxes_realize_done,
567};
568
aliguori6f97dba2008-10-31 18:49:55 +0000569static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
570{
571 CharDriverState *chr;
572 MuxDriver *d;
573
Anthony Liguori7267c092011-08-20 22:09:37 -0500574 chr = g_malloc0(sizeof(CharDriverState));
575 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000576
577 chr->opaque = d;
578 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200579 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000580 chr->chr_write = mux_chr_write;
581 chr->chr_update_read_handler = mux_chr_update_read_handler;
582 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100583 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100584 chr->chr_set_fe_open = NULL;
Michael Roth7b7ab182013-07-30 13:04:22 -0500585 /* only default to opened state if we've realized the initial
586 * set of muxes
587 */
588 chr->explicit_be_open = muxes_realized ? 0 : 1;
589 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200590
aliguori6f97dba2008-10-31 18:49:55 +0000591 return chr;
592}
593
594
595#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000596int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000597{
598 int ret, len;
599
600 len = len1;
601 while (len > 0) {
602 ret = send(fd, buf, len, 0);
603 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000604 errno = WSAGetLastError();
605 if (errno != WSAEWOULDBLOCK) {
606 return -1;
607 }
608 } else if (ret == 0) {
609 break;
610 } else {
611 buf += ret;
612 len -= ret;
613 }
614 }
615 return len1 - len;
616}
617
618#else
619
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100620int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000621{
622 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100623 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000624
625 len = len1;
626 while (len > 0) {
627 ret = write(fd, buf, len);
628 if (ret < 0) {
629 if (errno != EINTR && errno != EAGAIN)
630 return -1;
631 } else if (ret == 0) {
632 break;
633 } else {
634 buf += ret;
635 len -= ret;
636 }
637 }
638 return len1 - len;
639}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500640
641int recv_all(int fd, void *_buf, int len1, bool single_read)
642{
643 int ret, len;
644 uint8_t *buf = _buf;
645
646 len = len1;
647 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
648 if (ret < 0) {
649 if (errno != EINTR && errno != EAGAIN) {
650 return -1;
651 }
652 continue;
653 } else {
654 if (single_read) {
655 return ret;
656 }
657 buf += ret;
658 len -= ret;
659 }
660 }
661 return len1 - len;
662}
663
aliguori6f97dba2008-10-31 18:49:55 +0000664#endif /* !_WIN32 */
665
Anthony Liguori96c63842013-03-05 23:21:18 +0530666typedef struct IOWatchPoll
667{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200668 GSource parent;
669
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200670 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530671 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530672
673 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200674 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530675 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530676} IOWatchPoll;
677
Anthony Liguori96c63842013-03-05 23:21:18 +0530678static IOWatchPoll *io_watch_poll_from_source(GSource *source)
679{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200680 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530681}
682
683static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
684{
685 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200686 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200687 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200688 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530689 return FALSE;
690 }
691
Paolo Bonzinid185c092013-04-05 17:59:33 +0200692 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200693 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
694 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200695 g_source_attach(iwp->src, NULL);
696 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200697 g_source_destroy(iwp->src);
698 g_source_unref(iwp->src);
699 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200700 }
701 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530702}
703
704static gboolean io_watch_poll_check(GSource *source)
705{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200706 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530707}
708
709static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
710 gpointer user_data)
711{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200712 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530713}
714
715static void io_watch_poll_finalize(GSource *source)
716{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200717 /* Due to a glib bug, removing the last reference to a source
718 * inside a finalize callback causes recursive locking (and a
719 * deadlock). This is not a problem inside other callbacks,
720 * including dispatch callbacks, so we call io_remove_watch_poll
721 * to remove this source. At this point, iwp->src must
722 * be NULL, or we would leak it.
723 *
724 * This would be solved much more elegantly by child sources,
725 * but we support older glib versions that do not have them.
726 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530727 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200728 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530729}
730
731static GSourceFuncs io_watch_poll_funcs = {
732 .prepare = io_watch_poll_prepare,
733 .check = io_watch_poll_check,
734 .dispatch = io_watch_poll_dispatch,
735 .finalize = io_watch_poll_finalize,
736};
737
738/* Can only be used for read */
739static guint io_add_watch_poll(GIOChannel *channel,
740 IOCanReadHandler *fd_can_read,
741 GIOFunc fd_read,
742 gpointer user_data)
743{
744 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200745 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530746
Paolo Bonzinid185c092013-04-05 17:59:33 +0200747 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530748 iwp->fd_can_read = fd_can_read;
749 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200750 iwp->channel = channel;
751 iwp->fd_read = (GSourceFunc) fd_read;
752 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530753
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200754 tag = g_source_attach(&iwp->parent, NULL);
755 g_source_unref(&iwp->parent);
756 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530757}
758
Paolo Bonzini2b316772013-04-19 17:32:09 +0200759static void io_remove_watch_poll(guint tag)
760{
761 GSource *source;
762 IOWatchPoll *iwp;
763
764 g_return_if_fail (tag > 0);
765
766 source = g_main_context_find_source_by_id(NULL, tag);
767 g_return_if_fail (source != NULL);
768
769 iwp = io_watch_poll_from_source(source);
770 if (iwp->src) {
771 g_source_destroy(iwp->src);
772 g_source_unref(iwp->src);
773 iwp->src = NULL;
774 }
775 g_source_destroy(&iwp->parent);
776}
777
Amit Shah26da70c2013-08-28 15:23:37 +0530778static void remove_fd_in_watch(CharDriverState *chr)
779{
780 if (chr->fd_in_tag) {
781 io_remove_watch_poll(chr->fd_in_tag);
782 chr->fd_in_tag = 0;
783 }
784}
785
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000786#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530787static GIOChannel *io_channel_from_fd(int fd)
788{
789 GIOChannel *chan;
790
791 if (fd == -1) {
792 return NULL;
793 }
794
795 chan = g_io_channel_unix_new(fd);
796
797 g_io_channel_set_encoding(chan, NULL, NULL);
798 g_io_channel_set_buffered(chan, FALSE);
799
800 return chan;
801}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000802#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530803
Anthony Liguori76a96442013-03-05 23:21:21 +0530804static GIOChannel *io_channel_from_socket(int fd)
805{
806 GIOChannel *chan;
807
808 if (fd == -1) {
809 return NULL;
810 }
811
812#ifdef _WIN32
813 chan = g_io_channel_win32_new_socket(fd);
814#else
815 chan = g_io_channel_unix_new(fd);
816#endif
817
818 g_io_channel_set_encoding(chan, NULL, NULL);
819 g_io_channel_set_buffered(chan, FALSE);
820
821 return chan;
822}
823
Anthony Liguori684a0962013-03-29 11:39:50 -0500824static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530825{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200826 size_t offset = 0;
827 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530828
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200829 while (offset < len && status == G_IO_STATUS_NORMAL) {
830 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500831
832 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530833 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500834 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530835 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500836
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200837 if (offset > 0) {
838 return offset;
839 }
840 switch (status) {
841 case G_IO_STATUS_NORMAL:
842 g_assert(len == 0);
843 return 0;
844 case G_IO_STATUS_AGAIN:
845 errno = EAGAIN;
846 return -1;
847 default:
848 break;
849 }
850 errno = EINVAL;
851 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530852}
Anthony Liguori96c63842013-03-05 23:21:18 +0530853
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000854#ifndef _WIN32
855
Anthony Liguoria29753f2013-03-05 23:21:19 +0530856typedef struct FDCharDriver {
857 CharDriverState *chr;
858 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000859 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530860 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000861} FDCharDriver;
862
aliguori6f97dba2008-10-31 18:49:55 +0000863static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
864{
865 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530866
Anthony Liguori684a0962013-03-29 11:39:50 -0500867 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530868}
869
870static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
871{
872 CharDriverState *chr = opaque;
873 FDCharDriver *s = chr->opaque;
874 int len;
875 uint8_t buf[READ_BUF_LEN];
876 GIOStatus status;
877 gsize bytes_read;
878
879 len = sizeof(buf);
880 if (len > s->max_size) {
881 len = s->max_size;
882 }
883 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200884 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530885 }
886
887 status = g_io_channel_read_chars(chan, (gchar *)buf,
888 len, &bytes_read, NULL);
889 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530890 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530891 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
892 return FALSE;
893 }
894 if (status == G_IO_STATUS_NORMAL) {
895 qemu_chr_be_write(chr, buf, bytes_read);
896 }
897
898 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +0000899}
900
901static int fd_chr_read_poll(void *opaque)
902{
903 CharDriverState *chr = opaque;
904 FDCharDriver *s = chr->opaque;
905
Anthony Liguori909cda12011-08-15 11:17:31 -0500906 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000907 return s->max_size;
908}
909
Anthony Liguori23673ca2013-03-05 23:21:23 +0530910static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
911{
912 FDCharDriver *s = chr->opaque;
913 return g_io_create_watch(s->fd_out, cond);
914}
915
aliguori6f97dba2008-10-31 18:49:55 +0000916static void fd_chr_update_read_handler(CharDriverState *chr)
917{
918 FDCharDriver *s = chr->opaque;
919
Amit Shah26da70c2013-08-28 15:23:37 +0530920 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530921 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +0530922 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
923 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +0000924 }
925}
926
927static void fd_chr_close(struct CharDriverState *chr)
928{
929 FDCharDriver *s = chr->opaque;
930
Amit Shah26da70c2013-08-28 15:23:37 +0530931 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530932 if (s->fd_in) {
933 g_io_channel_unref(s->fd_in);
934 }
935 if (s->fd_out) {
936 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000937 }
938
Anthony Liguori7267c092011-08-20 22:09:37 -0500939 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +0100940 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000941}
942
943/* open a character device to a unix fd */
944static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
945{
946 CharDriverState *chr;
947 FDCharDriver *s;
948
Anthony Liguori7267c092011-08-20 22:09:37 -0500949 chr = g_malloc0(sizeof(CharDriverState));
950 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +0530951 s->fd_in = io_channel_from_fd(fd_in);
952 s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori23673ca2013-03-05 23:21:23 +0530953 fcntl(fd_out, F_SETFL, O_NONBLOCK);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530954 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +0000955 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +0530956 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +0000957 chr->chr_write = fd_chr_write;
958 chr->chr_update_read_handler = fd_chr_update_read_handler;
959 chr->chr_close = fd_chr_close;
960
aliguori6f97dba2008-10-31 18:49:55 +0000961 return chr;
962}
963
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100964static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000965{
966 int fd_in, fd_out;
967 char filename_in[256], filename_out[256];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +0100968 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200969
970 if (filename == NULL) {
971 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +0100972 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200973 }
aliguori6f97dba2008-10-31 18:49:55 +0000974
975 snprintf(filename_in, 256, "%s.in", filename);
976 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100977 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
978 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000979 if (fd_in < 0 || fd_out < 0) {
980 if (fd_in >= 0)
981 close(fd_in);
982 if (fd_out >= 0)
983 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +0100984 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100985 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +0100986 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +0100987 }
aliguori6f97dba2008-10-31 18:49:55 +0000988 }
Markus Armbruster1f514702012-02-07 15:09:08 +0100989 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +0000990}
991
aliguori6f97dba2008-10-31 18:49:55 +0000992/* init terminal so that we can grab keys */
993static struct termios oldtty;
994static int old_fd0_flags;
Paolo Bonzinibb002512010-12-23 13:42:50 +0100995static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +0000996
997static void term_exit(void)
998{
999 tcsetattr (0, TCSANOW, &oldtty);
1000 fcntl(0, F_SETFL, old_fd0_flags);
1001}
1002
Paolo Bonzinibb002512010-12-23 13:42:50 +01001003static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001004{
1005 struct termios tty;
1006
Paolo Bonzini03693642010-12-23 13:42:49 +01001007 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001008 if (!echo) {
1009 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001010 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001011 tty.c_oflag |= OPOST;
1012 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1013 tty.c_cflag &= ~(CSIZE|PARENB);
1014 tty.c_cflag |= CS8;
1015 tty.c_cc[VMIN] = 1;
1016 tty.c_cc[VTIME] = 0;
1017 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001018 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001019 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001020
1021 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001022}
1023
1024static void qemu_chr_close_stdio(struct CharDriverState *chr)
1025{
1026 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001027 fd_chr_close(chr);
1028}
1029
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001030static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001031{
1032 CharDriverState *chr;
1033
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001034 if (is_daemonized()) {
1035 error_report("cannot use stdio with -daemonize");
1036 return NULL;
1037 }
Anthony Liguoried7a1542013-03-05 23:21:17 +05301038 old_fd0_flags = fcntl(0, F_GETFL);
1039 tcgetattr (0, &oldtty);
1040 fcntl(0, F_SETFL, O_NONBLOCK);
1041 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001042
aliguori6f97dba2008-10-31 18:49:55 +00001043 chr = qemu_chr_open_fd(0, 1);
1044 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001045 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001046 if (opts->has_signal) {
1047 stdio_allow_signal = opts->signal;
1048 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001049 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001050
Markus Armbruster1f514702012-02-07 15:09:08 +01001051 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001052}
1053
aliguori6f97dba2008-10-31 18:49:55 +00001054#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001055 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1056 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001057
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001058#define HAVE_CHARDEV_TTY 1
1059
aliguori6f97dba2008-10-31 18:49:55 +00001060typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301061 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001062 int connected;
aliguori6f97dba2008-10-31 18:49:55 +00001063 int read_bytes;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301064 guint timer_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001065} PtyCharDriver;
1066
1067static void pty_chr_update_read_handler(CharDriverState *chr);
1068static void pty_chr_state(CharDriverState *chr, int connected);
1069
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301070static gboolean pty_chr_timer(gpointer opaque)
1071{
1072 struct CharDriverState *chr = opaque;
1073 PtyCharDriver *s = chr->opaque;
1074
Hans de Goede79f20072013-04-25 13:53:02 +02001075 s->timer_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001076 if (!s->connected) {
1077 /* Next poll ... */
1078 pty_chr_update_read_handler(chr);
1079 }
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301080 return FALSE;
1081}
1082
1083static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1084{
1085 PtyCharDriver *s = chr->opaque;
1086
1087 if (s->timer_tag) {
1088 g_source_remove(s->timer_tag);
1089 s->timer_tag = 0;
1090 }
1091
1092 if (ms == 1000) {
1093 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1094 } else {
1095 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1096 }
1097}
1098
aliguori6f97dba2008-10-31 18:49:55 +00001099static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1100{
1101 PtyCharDriver *s = chr->opaque;
1102
1103 if (!s->connected) {
1104 /* guest sends data, check for (re-)connect */
1105 pty_chr_update_read_handler(chr);
1106 return 0;
1107 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001108 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001109}
1110
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301111static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1112{
1113 PtyCharDriver *s = chr->opaque;
1114 return g_io_create_watch(s->fd, cond);
1115}
1116
aliguori6f97dba2008-10-31 18:49:55 +00001117static int pty_chr_read_poll(void *opaque)
1118{
1119 CharDriverState *chr = opaque;
1120 PtyCharDriver *s = chr->opaque;
1121
Anthony Liguori909cda12011-08-15 11:17:31 -05001122 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001123 return s->read_bytes;
1124}
1125
Anthony Liguori093d3a22013-03-05 23:21:20 +05301126static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001127{
1128 CharDriverState *chr = opaque;
1129 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301130 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301131 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301132 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001133
1134 len = sizeof(buf);
1135 if (len > s->read_bytes)
1136 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001137 if (len == 0) {
1138 return TRUE;
1139 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301140 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1141 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001142 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301143 return FALSE;
1144 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001145 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001146 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001147 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301148 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001149}
1150
1151static void pty_chr_update_read_handler(CharDriverState *chr)
1152{
1153 PtyCharDriver *s = chr->opaque;
Paolo Bonzini85a67692013-04-19 17:32:07 +02001154 GPollFD pfd;
aliguori6f97dba2008-10-31 18:49:55 +00001155
Paolo Bonzini85a67692013-04-19 17:32:07 +02001156 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1157 pfd.events = G_IO_OUT;
1158 pfd.revents = 0;
1159 g_poll(&pfd, 1, 0);
1160 if (pfd.revents & G_IO_HUP) {
1161 pty_chr_state(chr, 0);
1162 } else {
1163 pty_chr_state(chr, 1);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301164 }
aliguori6f97dba2008-10-31 18:49:55 +00001165}
1166
1167static void pty_chr_state(CharDriverState *chr, int connected)
1168{
1169 PtyCharDriver *s = chr->opaque;
1170
1171 if (!connected) {
Amit Shah26da70c2013-08-28 15:23:37 +05301172 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001173 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001174 /* (re-)connect poll interval for idle guests: once per second.
1175 * We check more frequently in case the guests sends data to
1176 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301177 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001178 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001179 if (s->timer_tag) {
1180 g_source_remove(s->timer_tag);
1181 s->timer_tag = 0;
1182 }
1183 if (!s->connected) {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001184 s->connected = 1;
James Hogan3a3567d2013-08-08 12:09:38 +01001185 qemu_chr_be_generic_open(chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001186 }
1187 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301188 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1189 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001190 }
aliguori6f97dba2008-10-31 18:49:55 +00001191 }
1192}
1193
aliguori6f97dba2008-10-31 18:49:55 +00001194static void pty_chr_close(struct CharDriverState *chr)
1195{
1196 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301197 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001198
Amit Shah26da70c2013-08-28 15:23:37 +05301199 remove_fd_in_watch(chr);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301200 fd = g_io_channel_unix_get_fd(s->fd);
1201 g_io_channel_unref(s->fd);
1202 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301203 if (s->timer_tag) {
1204 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001205 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301206 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001207 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001208 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001209}
1210
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001211static CharDriverState *qemu_chr_open_pty(const char *id,
1212 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001213{
1214 CharDriverState *chr;
1215 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001216 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001217 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001218
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001219 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1220 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001221 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001222 }
1223
aliguori6f97dba2008-10-31 18:49:55 +00001224 close(slave_fd);
1225
Markus Armbrustera4e26042011-11-11 10:40:05 +01001226 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001227
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001228 chr->filename = g_strdup_printf("pty:%s", pty_name);
1229 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001230 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001231
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001232 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001233 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001234
1235 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001236 chr->opaque = s;
1237 chr->chr_write = pty_chr_write;
1238 chr->chr_update_read_handler = pty_chr_update_read_handler;
1239 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301240 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001241 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001242
Anthony Liguori093d3a22013-03-05 23:21:20 +05301243 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301244 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001245
Markus Armbruster1f514702012-02-07 15:09:08 +01001246 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001247}
1248
1249static void tty_serial_init(int fd, int speed,
1250 int parity, int data_bits, int stop_bits)
1251{
1252 struct termios tty;
1253 speed_t spd;
1254
1255#if 0
1256 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1257 speed, parity, data_bits, stop_bits);
1258#endif
1259 tcgetattr (fd, &tty);
1260
Stefan Weil45eea132009-10-26 16:10:10 +01001261#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1262 speed = speed * 10 / 11;
1263 do {
1264 check_speed(50);
1265 check_speed(75);
1266 check_speed(110);
1267 check_speed(134);
1268 check_speed(150);
1269 check_speed(200);
1270 check_speed(300);
1271 check_speed(600);
1272 check_speed(1200);
1273 check_speed(1800);
1274 check_speed(2400);
1275 check_speed(4800);
1276 check_speed(9600);
1277 check_speed(19200);
1278 check_speed(38400);
1279 /* Non-Posix values follow. They may be unsupported on some systems. */
1280 check_speed(57600);
1281 check_speed(115200);
1282#ifdef B230400
1283 check_speed(230400);
1284#endif
1285#ifdef B460800
1286 check_speed(460800);
1287#endif
1288#ifdef B500000
1289 check_speed(500000);
1290#endif
1291#ifdef B576000
1292 check_speed(576000);
1293#endif
1294#ifdef B921600
1295 check_speed(921600);
1296#endif
1297#ifdef B1000000
1298 check_speed(1000000);
1299#endif
1300#ifdef B1152000
1301 check_speed(1152000);
1302#endif
1303#ifdef B1500000
1304 check_speed(1500000);
1305#endif
1306#ifdef B2000000
1307 check_speed(2000000);
1308#endif
1309#ifdef B2500000
1310 check_speed(2500000);
1311#endif
1312#ifdef B3000000
1313 check_speed(3000000);
1314#endif
1315#ifdef B3500000
1316 check_speed(3500000);
1317#endif
1318#ifdef B4000000
1319 check_speed(4000000);
1320#endif
aliguori6f97dba2008-10-31 18:49:55 +00001321 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001322 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001323
1324 cfsetispeed(&tty, spd);
1325 cfsetospeed(&tty, spd);
1326
1327 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1328 |INLCR|IGNCR|ICRNL|IXON);
1329 tty.c_oflag |= OPOST;
1330 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1331 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1332 switch(data_bits) {
1333 default:
1334 case 8:
1335 tty.c_cflag |= CS8;
1336 break;
1337 case 7:
1338 tty.c_cflag |= CS7;
1339 break;
1340 case 6:
1341 tty.c_cflag |= CS6;
1342 break;
1343 case 5:
1344 tty.c_cflag |= CS5;
1345 break;
1346 }
1347 switch(parity) {
1348 default:
1349 case 'N':
1350 break;
1351 case 'E':
1352 tty.c_cflag |= PARENB;
1353 break;
1354 case 'O':
1355 tty.c_cflag |= PARENB | PARODD;
1356 break;
1357 }
1358 if (stop_bits == 2)
1359 tty.c_cflag |= CSTOPB;
1360
1361 tcsetattr (fd, TCSANOW, &tty);
1362}
1363
1364static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1365{
1366 FDCharDriver *s = chr->opaque;
1367
1368 switch(cmd) {
1369 case CHR_IOCTL_SERIAL_SET_PARAMS:
1370 {
1371 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301372 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1373 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001374 ssp->data_bits, ssp->stop_bits);
1375 }
1376 break;
1377 case CHR_IOCTL_SERIAL_SET_BREAK:
1378 {
1379 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301380 if (enable) {
1381 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1382 }
aliguori6f97dba2008-10-31 18:49:55 +00001383 }
1384 break;
1385 case CHR_IOCTL_SERIAL_GET_TIOCM:
1386 {
1387 int sarg = 0;
1388 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301389 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001390 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001391 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001392 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001393 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001394 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001395 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001396 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001397 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001398 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001399 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001400 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001401 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001402 *targ |= CHR_TIOCM_RTS;
1403 }
1404 break;
1405 case CHR_IOCTL_SERIAL_SET_TIOCM:
1406 {
1407 int sarg = *(int *)arg;
1408 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301409 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001410 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1411 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1412 if (sarg & CHR_TIOCM_CTS)
1413 targ |= TIOCM_CTS;
1414 if (sarg & CHR_TIOCM_CAR)
1415 targ |= TIOCM_CAR;
1416 if (sarg & CHR_TIOCM_DSR)
1417 targ |= TIOCM_DSR;
1418 if (sarg & CHR_TIOCM_RI)
1419 targ |= TIOCM_RI;
1420 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001421 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001422 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001423 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301424 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001425 }
1426 break;
1427 default:
1428 return -ENOTSUP;
1429 }
1430 return 0;
1431}
1432
David Ahern4266a132010-02-10 18:27:17 -07001433static void qemu_chr_close_tty(CharDriverState *chr)
1434{
1435 FDCharDriver *s = chr->opaque;
1436 int fd = -1;
1437
1438 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301439 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001440 }
1441
1442 fd_chr_close(chr);
1443
1444 if (fd >= 0) {
1445 close(fd);
1446 }
1447}
1448
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001449static CharDriverState *qemu_chr_open_tty_fd(int fd)
1450{
1451 CharDriverState *chr;
1452
1453 tty_serial_init(fd, 115200, 'N', 8, 1);
1454 chr = qemu_chr_open_fd(fd, fd);
1455 chr->chr_ioctl = tty_serial_ioctl;
1456 chr->chr_close = qemu_chr_close_tty;
1457 return chr;
1458}
aliguori6f97dba2008-10-31 18:49:55 +00001459#endif /* __linux__ || __sun__ */
1460
1461#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001462
1463#define HAVE_CHARDEV_PARPORT 1
1464
aliguori6f97dba2008-10-31 18:49:55 +00001465typedef struct {
1466 int fd;
1467 int mode;
1468} ParallelCharDriver;
1469
1470static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1471{
1472 if (s->mode != mode) {
1473 int m = mode;
1474 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1475 return 0;
1476 s->mode = mode;
1477 }
1478 return 1;
1479}
1480
1481static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1482{
1483 ParallelCharDriver *drv = chr->opaque;
1484 int fd = drv->fd;
1485 uint8_t b;
1486
1487 switch(cmd) {
1488 case CHR_IOCTL_PP_READ_DATA:
1489 if (ioctl(fd, PPRDATA, &b) < 0)
1490 return -ENOTSUP;
1491 *(uint8_t *)arg = b;
1492 break;
1493 case CHR_IOCTL_PP_WRITE_DATA:
1494 b = *(uint8_t *)arg;
1495 if (ioctl(fd, PPWDATA, &b) < 0)
1496 return -ENOTSUP;
1497 break;
1498 case CHR_IOCTL_PP_READ_CONTROL:
1499 if (ioctl(fd, PPRCONTROL, &b) < 0)
1500 return -ENOTSUP;
1501 /* Linux gives only the lowest bits, and no way to know data
1502 direction! For better compatibility set the fixed upper
1503 bits. */
1504 *(uint8_t *)arg = b | 0xc0;
1505 break;
1506 case CHR_IOCTL_PP_WRITE_CONTROL:
1507 b = *(uint8_t *)arg;
1508 if (ioctl(fd, PPWCONTROL, &b) < 0)
1509 return -ENOTSUP;
1510 break;
1511 case CHR_IOCTL_PP_READ_STATUS:
1512 if (ioctl(fd, PPRSTATUS, &b) < 0)
1513 return -ENOTSUP;
1514 *(uint8_t *)arg = b;
1515 break;
1516 case CHR_IOCTL_PP_DATA_DIR:
1517 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1518 return -ENOTSUP;
1519 break;
1520 case CHR_IOCTL_PP_EPP_READ_ADDR:
1521 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1522 struct ParallelIOArg *parg = arg;
1523 int n = read(fd, parg->buffer, parg->count);
1524 if (n != parg->count) {
1525 return -EIO;
1526 }
1527 }
1528 break;
1529 case CHR_IOCTL_PP_EPP_READ:
1530 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1531 struct ParallelIOArg *parg = arg;
1532 int n = read(fd, parg->buffer, parg->count);
1533 if (n != parg->count) {
1534 return -EIO;
1535 }
1536 }
1537 break;
1538 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1539 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1540 struct ParallelIOArg *parg = arg;
1541 int n = write(fd, parg->buffer, parg->count);
1542 if (n != parg->count) {
1543 return -EIO;
1544 }
1545 }
1546 break;
1547 case CHR_IOCTL_PP_EPP_WRITE:
1548 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1549 struct ParallelIOArg *parg = arg;
1550 int n = write(fd, parg->buffer, parg->count);
1551 if (n != parg->count) {
1552 return -EIO;
1553 }
1554 }
1555 break;
1556 default:
1557 return -ENOTSUP;
1558 }
1559 return 0;
1560}
1561
1562static void pp_close(CharDriverState *chr)
1563{
1564 ParallelCharDriver *drv = chr->opaque;
1565 int fd = drv->fd;
1566
1567 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1568 ioctl(fd, PPRELEASE);
1569 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001570 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001571 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001572}
1573
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001574static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001575{
1576 CharDriverState *chr;
1577 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001578
1579 if (ioctl(fd, PPCLAIM) < 0) {
1580 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001581 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001582 }
1583
Anthony Liguori7267c092011-08-20 22:09:37 -05001584 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001585 drv->fd = fd;
1586 drv->mode = IEEE1284_MODE_COMPAT;
1587
Anthony Liguori7267c092011-08-20 22:09:37 -05001588 chr = g_malloc0(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001589 chr->chr_write = null_chr_write;
1590 chr->chr_ioctl = pp_ioctl;
1591 chr->chr_close = pp_close;
1592 chr->opaque = drv;
1593
Markus Armbruster1f514702012-02-07 15:09:08 +01001594 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001595}
1596#endif /* __linux__ */
1597
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001598#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001599
1600#define HAVE_CHARDEV_PARPORT 1
1601
blueswir16972f932008-11-22 20:49:12 +00001602static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1603{
Stefan Weile0efb992011-02-23 19:09:16 +01001604 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001605 uint8_t b;
1606
1607 switch(cmd) {
1608 case CHR_IOCTL_PP_READ_DATA:
1609 if (ioctl(fd, PPIGDATA, &b) < 0)
1610 return -ENOTSUP;
1611 *(uint8_t *)arg = b;
1612 break;
1613 case CHR_IOCTL_PP_WRITE_DATA:
1614 b = *(uint8_t *)arg;
1615 if (ioctl(fd, PPISDATA, &b) < 0)
1616 return -ENOTSUP;
1617 break;
1618 case CHR_IOCTL_PP_READ_CONTROL:
1619 if (ioctl(fd, PPIGCTRL, &b) < 0)
1620 return -ENOTSUP;
1621 *(uint8_t *)arg = b;
1622 break;
1623 case CHR_IOCTL_PP_WRITE_CONTROL:
1624 b = *(uint8_t *)arg;
1625 if (ioctl(fd, PPISCTRL, &b) < 0)
1626 return -ENOTSUP;
1627 break;
1628 case CHR_IOCTL_PP_READ_STATUS:
1629 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1630 return -ENOTSUP;
1631 *(uint8_t *)arg = b;
1632 break;
1633 default:
1634 return -ENOTSUP;
1635 }
1636 return 0;
1637}
1638
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001639static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001640{
1641 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001642
Anthony Liguori7267c092011-08-20 22:09:37 -05001643 chr = g_malloc0(sizeof(CharDriverState));
Stefan Weile0efb992011-02-23 19:09:16 +01001644 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001645 chr->chr_write = null_chr_write;
1646 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001647 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001648 return chr;
blueswir16972f932008-11-22 20:49:12 +00001649}
1650#endif
1651
aliguori6f97dba2008-10-31 18:49:55 +00001652#else /* _WIN32 */
1653
1654typedef struct {
1655 int max_size;
1656 HANDLE hcom, hrecv, hsend;
1657 OVERLAPPED orecv, osend;
1658 BOOL fpipe;
1659 DWORD len;
1660} WinCharState;
1661
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001662typedef struct {
1663 HANDLE hStdIn;
1664 HANDLE hInputReadyEvent;
1665 HANDLE hInputDoneEvent;
1666 HANDLE hInputThread;
1667 uint8_t win_stdio_buf;
1668} WinStdioCharState;
1669
aliguori6f97dba2008-10-31 18:49:55 +00001670#define NSENDBUF 2048
1671#define NRECVBUF 2048
1672#define MAXCONNECT 1
1673#define NTIMEOUT 5000
1674
1675static int win_chr_poll(void *opaque);
1676static int win_chr_pipe_poll(void *opaque);
1677
1678static void win_chr_close(CharDriverState *chr)
1679{
1680 WinCharState *s = chr->opaque;
1681
1682 if (s->hsend) {
1683 CloseHandle(s->hsend);
1684 s->hsend = NULL;
1685 }
1686 if (s->hrecv) {
1687 CloseHandle(s->hrecv);
1688 s->hrecv = NULL;
1689 }
1690 if (s->hcom) {
1691 CloseHandle(s->hcom);
1692 s->hcom = NULL;
1693 }
1694 if (s->fpipe)
1695 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1696 else
1697 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301698
Hans de Goedea425d232011-11-19 10:22:43 +01001699 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001700}
1701
1702static int win_chr_init(CharDriverState *chr, const char *filename)
1703{
1704 WinCharState *s = chr->opaque;
1705 COMMCONFIG comcfg;
1706 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1707 COMSTAT comstat;
1708 DWORD size;
1709 DWORD err;
1710
1711 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1712 if (!s->hsend) {
1713 fprintf(stderr, "Failed CreateEvent\n");
1714 goto fail;
1715 }
1716 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1717 if (!s->hrecv) {
1718 fprintf(stderr, "Failed CreateEvent\n");
1719 goto fail;
1720 }
1721
1722 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1723 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1724 if (s->hcom == INVALID_HANDLE_VALUE) {
1725 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1726 s->hcom = NULL;
1727 goto fail;
1728 }
1729
1730 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1731 fprintf(stderr, "Failed SetupComm\n");
1732 goto fail;
1733 }
1734
1735 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1736 size = sizeof(COMMCONFIG);
1737 GetDefaultCommConfig(filename, &comcfg, &size);
1738 comcfg.dcb.DCBlength = sizeof(DCB);
1739 CommConfigDialog(filename, NULL, &comcfg);
1740
1741 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1742 fprintf(stderr, "Failed SetCommState\n");
1743 goto fail;
1744 }
1745
1746 if (!SetCommMask(s->hcom, EV_ERR)) {
1747 fprintf(stderr, "Failed SetCommMask\n");
1748 goto fail;
1749 }
1750
1751 cto.ReadIntervalTimeout = MAXDWORD;
1752 if (!SetCommTimeouts(s->hcom, &cto)) {
1753 fprintf(stderr, "Failed SetCommTimeouts\n");
1754 goto fail;
1755 }
1756
1757 if (!ClearCommError(s->hcom, &err, &comstat)) {
1758 fprintf(stderr, "Failed ClearCommError\n");
1759 goto fail;
1760 }
1761 qemu_add_polling_cb(win_chr_poll, chr);
1762 return 0;
1763
1764 fail:
1765 win_chr_close(chr);
1766 return -1;
1767}
1768
1769static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1770{
1771 WinCharState *s = chr->opaque;
1772 DWORD len, ret, size, err;
1773
1774 len = len1;
1775 ZeroMemory(&s->osend, sizeof(s->osend));
1776 s->osend.hEvent = s->hsend;
1777 while (len > 0) {
1778 if (s->hsend)
1779 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1780 else
1781 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1782 if (!ret) {
1783 err = GetLastError();
1784 if (err == ERROR_IO_PENDING) {
1785 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1786 if (ret) {
1787 buf += size;
1788 len -= size;
1789 } else {
1790 break;
1791 }
1792 } else {
1793 break;
1794 }
1795 } else {
1796 buf += size;
1797 len -= size;
1798 }
1799 }
1800 return len1 - len;
1801}
1802
1803static int win_chr_read_poll(CharDriverState *chr)
1804{
1805 WinCharState *s = chr->opaque;
1806
Anthony Liguori909cda12011-08-15 11:17:31 -05001807 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001808 return s->max_size;
1809}
1810
1811static void win_chr_readfile(CharDriverState *chr)
1812{
1813 WinCharState *s = chr->opaque;
1814 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301815 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001816 DWORD size;
1817
1818 ZeroMemory(&s->orecv, sizeof(s->orecv));
1819 s->orecv.hEvent = s->hrecv;
1820 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1821 if (!ret) {
1822 err = GetLastError();
1823 if (err == ERROR_IO_PENDING) {
1824 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1825 }
1826 }
1827
1828 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001829 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001830 }
1831}
1832
1833static void win_chr_read(CharDriverState *chr)
1834{
1835 WinCharState *s = chr->opaque;
1836
1837 if (s->len > s->max_size)
1838 s->len = s->max_size;
1839 if (s->len == 0)
1840 return;
1841
1842 win_chr_readfile(chr);
1843}
1844
1845static int win_chr_poll(void *opaque)
1846{
1847 CharDriverState *chr = opaque;
1848 WinCharState *s = chr->opaque;
1849 COMSTAT status;
1850 DWORD comerr;
1851
1852 ClearCommError(s->hcom, &comerr, &status);
1853 if (status.cbInQue > 0) {
1854 s->len = status.cbInQue;
1855 win_chr_read_poll(chr);
1856 win_chr_read(chr);
1857 return 1;
1858 }
1859 return 0;
1860}
1861
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001862static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00001863{
1864 CharDriverState *chr;
1865 WinCharState *s;
1866
Anthony Liguori7267c092011-08-20 22:09:37 -05001867 chr = g_malloc0(sizeof(CharDriverState));
1868 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001869 chr->opaque = s;
1870 chr->chr_write = win_chr_write;
1871 chr->chr_close = win_chr_close;
1872
1873 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001874 g_free(s);
1875 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001876 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001877 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001878 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001879}
1880
1881static int win_chr_pipe_poll(void *opaque)
1882{
1883 CharDriverState *chr = opaque;
1884 WinCharState *s = chr->opaque;
1885 DWORD size;
1886
1887 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1888 if (size > 0) {
1889 s->len = size;
1890 win_chr_read_poll(chr);
1891 win_chr_read(chr);
1892 return 1;
1893 }
1894 return 0;
1895}
1896
1897static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1898{
1899 WinCharState *s = chr->opaque;
1900 OVERLAPPED ov;
1901 int ret;
1902 DWORD size;
1903 char openname[256];
1904
1905 s->fpipe = TRUE;
1906
1907 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1908 if (!s->hsend) {
1909 fprintf(stderr, "Failed CreateEvent\n");
1910 goto fail;
1911 }
1912 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1913 if (!s->hrecv) {
1914 fprintf(stderr, "Failed CreateEvent\n");
1915 goto fail;
1916 }
1917
1918 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1919 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1920 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1921 PIPE_WAIT,
1922 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1923 if (s->hcom == INVALID_HANDLE_VALUE) {
1924 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1925 s->hcom = NULL;
1926 goto fail;
1927 }
1928
1929 ZeroMemory(&ov, sizeof(ov));
1930 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1931 ret = ConnectNamedPipe(s->hcom, &ov);
1932 if (ret) {
1933 fprintf(stderr, "Failed ConnectNamedPipe\n");
1934 goto fail;
1935 }
1936
1937 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1938 if (!ret) {
1939 fprintf(stderr, "Failed GetOverlappedResult\n");
1940 if (ov.hEvent) {
1941 CloseHandle(ov.hEvent);
1942 ov.hEvent = NULL;
1943 }
1944 goto fail;
1945 }
1946
1947 if (ov.hEvent) {
1948 CloseHandle(ov.hEvent);
1949 ov.hEvent = NULL;
1950 }
1951 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1952 return 0;
1953
1954 fail:
1955 win_chr_close(chr);
1956 return -1;
1957}
1958
1959
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001960static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001961{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001962 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00001963 CharDriverState *chr;
1964 WinCharState *s;
1965
Anthony Liguori7267c092011-08-20 22:09:37 -05001966 chr = g_malloc0(sizeof(CharDriverState));
1967 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001968 chr->opaque = s;
1969 chr->chr_write = win_chr_write;
1970 chr->chr_close = win_chr_close;
1971
1972 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02001973 g_free(s);
1974 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001975 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001976 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001977 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001978}
1979
Markus Armbruster1f514702012-02-07 15:09:08 +01001980static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00001981{
1982 CharDriverState *chr;
1983 WinCharState *s;
1984
Anthony Liguori7267c092011-08-20 22:09:37 -05001985 chr = g_malloc0(sizeof(CharDriverState));
1986 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001987 s->hcom = fd_out;
1988 chr->opaque = s;
1989 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01001990 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001991}
1992
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01001993static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00001994{
Markus Armbruster1f514702012-02-07 15:09:08 +01001995 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00001996}
1997
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001998static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
1999{
2000 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2001 DWORD dwSize;
2002 int len1;
2003
2004 len1 = len;
2005
2006 while (len1 > 0) {
2007 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2008 break;
2009 }
2010 buf += dwSize;
2011 len1 -= dwSize;
2012 }
2013
2014 return len - len1;
2015}
2016
2017static void win_stdio_wait_func(void *opaque)
2018{
2019 CharDriverState *chr = opaque;
2020 WinStdioCharState *stdio = chr->opaque;
2021 INPUT_RECORD buf[4];
2022 int ret;
2023 DWORD dwSize;
2024 int i;
2025
Stefan Weildff74242013-12-07 14:48:04 +01002026 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002027
2028 if (!ret) {
2029 /* Avoid error storm */
2030 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2031 return;
2032 }
2033
2034 for (i = 0; i < dwSize; i++) {
2035 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2036
2037 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2038 int j;
2039 if (kev->uChar.AsciiChar != 0) {
2040 for (j = 0; j < kev->wRepeatCount; j++) {
2041 if (qemu_chr_be_can_write(chr)) {
2042 uint8_t c = kev->uChar.AsciiChar;
2043 qemu_chr_be_write(chr, &c, 1);
2044 }
2045 }
2046 }
2047 }
2048 }
2049}
2050
2051static DWORD WINAPI win_stdio_thread(LPVOID param)
2052{
2053 CharDriverState *chr = param;
2054 WinStdioCharState *stdio = chr->opaque;
2055 int ret;
2056 DWORD dwSize;
2057
2058 while (1) {
2059
2060 /* Wait for one byte */
2061 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2062
2063 /* Exit in case of error, continue if nothing read */
2064 if (!ret) {
2065 break;
2066 }
2067 if (!dwSize) {
2068 continue;
2069 }
2070
2071 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2072 if (stdio->win_stdio_buf == '\r') {
2073 continue;
2074 }
2075
2076 /* Signal the main thread and wait until the byte was eaten */
2077 if (!SetEvent(stdio->hInputReadyEvent)) {
2078 break;
2079 }
2080 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2081 != WAIT_OBJECT_0) {
2082 break;
2083 }
2084 }
2085
2086 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2087 return 0;
2088}
2089
2090static void win_stdio_thread_wait_func(void *opaque)
2091{
2092 CharDriverState *chr = opaque;
2093 WinStdioCharState *stdio = chr->opaque;
2094
2095 if (qemu_chr_be_can_write(chr)) {
2096 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2097 }
2098
2099 SetEvent(stdio->hInputDoneEvent);
2100}
2101
2102static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2103{
2104 WinStdioCharState *stdio = chr->opaque;
2105 DWORD dwMode = 0;
2106
2107 GetConsoleMode(stdio->hStdIn, &dwMode);
2108
2109 if (echo) {
2110 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2111 } else {
2112 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2113 }
2114}
2115
2116static void win_stdio_close(CharDriverState *chr)
2117{
2118 WinStdioCharState *stdio = chr->opaque;
2119
2120 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2121 CloseHandle(stdio->hInputReadyEvent);
2122 }
2123 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2124 CloseHandle(stdio->hInputDoneEvent);
2125 }
2126 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2127 TerminateThread(stdio->hInputThread, 0);
2128 }
2129
2130 g_free(chr->opaque);
2131 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002132}
2133
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002134static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002135{
2136 CharDriverState *chr;
2137 WinStdioCharState *stdio;
2138 DWORD dwMode;
2139 int is_console = 0;
2140
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002141 chr = g_malloc0(sizeof(CharDriverState));
2142 stdio = g_malloc0(sizeof(WinStdioCharState));
2143
2144 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2145 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2146 fprintf(stderr, "cannot open stdio: invalid handle\n");
2147 exit(1);
2148 }
2149
2150 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2151
2152 chr->opaque = stdio;
2153 chr->chr_write = win_stdio_write;
2154 chr->chr_close = win_stdio_close;
2155
Anthony Liguoried7a1542013-03-05 23:21:17 +05302156 if (is_console) {
2157 if (qemu_add_wait_object(stdio->hStdIn,
2158 win_stdio_wait_func, chr)) {
2159 fprintf(stderr, "qemu_add_wait_object: failed\n");
2160 }
2161 } else {
2162 DWORD dwId;
2163
2164 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2165 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2166 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2167 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002168
Anthony Liguoried7a1542013-03-05 23:21:17 +05302169 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2170 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2171 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2172 fprintf(stderr, "cannot create stdio thread or event\n");
2173 exit(1);
2174 }
2175 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2176 win_stdio_thread_wait_func, chr)) {
2177 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002178 }
2179 }
2180
2181 dwMode |= ENABLE_LINE_INPUT;
2182
Anthony Liguoried7a1542013-03-05 23:21:17 +05302183 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002184 /* set the terminal in raw mode */
2185 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2186 dwMode |= ENABLE_PROCESSED_INPUT;
2187 }
2188
2189 SetConsoleMode(stdio->hStdIn, dwMode);
2190
2191 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2192 qemu_chr_fe_set_echo(chr, false);
2193
Markus Armbruster1f514702012-02-07 15:09:08 +01002194 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002195}
aliguori6f97dba2008-10-31 18:49:55 +00002196#endif /* !_WIN32 */
2197
Anthony Liguori5ab82112013-03-05 23:21:31 +05302198
aliguori6f97dba2008-10-31 18:49:55 +00002199/***********************************************************/
2200/* UDP Net console */
2201
2202typedef struct {
2203 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302204 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302205 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002206 int bufcnt;
2207 int bufptr;
2208 int max_size;
2209} NetCharDriver;
2210
2211static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2212{
2213 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302214 gsize bytes_written;
2215 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002216
Anthony Liguori76a96442013-03-05 23:21:21 +05302217 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2218 if (status == G_IO_STATUS_EOF) {
2219 return 0;
2220 } else if (status != G_IO_STATUS_NORMAL) {
2221 return -1;
2222 }
2223
2224 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002225}
2226
2227static int udp_chr_read_poll(void *opaque)
2228{
2229 CharDriverState *chr = opaque;
2230 NetCharDriver *s = chr->opaque;
2231
Anthony Liguori909cda12011-08-15 11:17:31 -05002232 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002233
2234 /* If there were any stray characters in the queue process them
2235 * first
2236 */
2237 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002238 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002239 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002240 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002241 }
2242 return s->max_size;
2243}
2244
Anthony Liguori76a96442013-03-05 23:21:21 +05302245static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002246{
2247 CharDriverState *chr = opaque;
2248 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302249 gsize bytes_read = 0;
2250 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002251
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002252 if (s->max_size == 0) {
2253 return TRUE;
2254 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302255 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2256 &bytes_read, NULL);
2257 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002258 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302259 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302260 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302261 return FALSE;
2262 }
aliguori6f97dba2008-10-31 18:49:55 +00002263
2264 s->bufptr = 0;
2265 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002266 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002267 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002268 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002269 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302270
2271 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002272}
2273
2274static void udp_chr_update_read_handler(CharDriverState *chr)
2275{
2276 NetCharDriver *s = chr->opaque;
2277
Amit Shah26da70c2013-08-28 15:23:37 +05302278 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302279 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302280 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2281 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002282 }
2283}
2284
aliguori819f56b2009-03-28 17:58:14 +00002285static void udp_chr_close(CharDriverState *chr)
2286{
2287 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302288
2289 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302290 if (s->chan) {
2291 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002292 closesocket(s->fd);
2293 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002294 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002295 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002296}
2297
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002298static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002299{
2300 CharDriverState *chr = NULL;
2301 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002302
Anthony Liguori7267c092011-08-20 22:09:37 -05002303 chr = g_malloc0(sizeof(CharDriverState));
2304 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002305
aliguori6f97dba2008-10-31 18:49:55 +00002306 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302307 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002308 s->bufcnt = 0;
2309 s->bufptr = 0;
2310 chr->opaque = s;
2311 chr->chr_write = udp_chr_write;
2312 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002313 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002314 /* be isn't opened until we get a connection */
2315 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002316 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002317}
aliguori6f97dba2008-10-31 18:49:55 +00002318
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002319static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
2320{
2321 Error *local_err = NULL;
2322 int fd = -1;
2323
2324 fd = inet_dgram_opts(opts, &local_err);
2325 if (fd < 0) {
Gerd Hoffmann58a37142013-06-24 08:39:55 +02002326 qerror_report_err(local_err);
2327 error_free(local_err);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002328 return NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002329 }
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002330 return qemu_chr_open_udp_fd(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002331}
2332
2333/***********************************************************/
2334/* TCP Net console */
2335
2336typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302337
2338 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302339 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002340 int fd, listen_fd;
2341 int connected;
2342 int max_size;
2343 int do_telnetopt;
2344 int do_nodelay;
2345 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002346 int *read_msgfds;
2347 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002348 int *write_msgfds;
2349 int write_msgfds_num;
aliguori6f97dba2008-10-31 18:49:55 +00002350} TCPCharDriver;
2351
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302352static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002353
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002354#ifndef _WIN32
2355static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2356{
2357 TCPCharDriver *s = chr->opaque;
2358 struct msghdr msgh;
2359 struct iovec iov;
2360 int r;
2361
2362 size_t fd_size = s->write_msgfds_num * sizeof(int);
2363 char control[CMSG_SPACE(fd_size)];
2364 struct cmsghdr *cmsg;
2365
2366 memset(&msgh, 0, sizeof(msgh));
2367 memset(control, 0, sizeof(control));
2368
2369 /* set the payload */
2370 iov.iov_base = (uint8_t *) buf;
2371 iov.iov_len = len;
2372
2373 msgh.msg_iov = &iov;
2374 msgh.msg_iovlen = 1;
2375
2376 msgh.msg_control = control;
2377 msgh.msg_controllen = sizeof(control);
2378
2379 cmsg = CMSG_FIRSTHDR(&msgh);
2380
2381 cmsg->cmsg_len = CMSG_LEN(fd_size);
2382 cmsg->cmsg_level = SOL_SOCKET;
2383 cmsg->cmsg_type = SCM_RIGHTS;
2384 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2385
2386 do {
2387 r = sendmsg(s->fd, &msgh, 0);
2388 } while (r < 0 && errno == EINTR);
2389
2390 /* free the written msgfds, no matter what */
2391 if (s->write_msgfds_num) {
2392 g_free(s->write_msgfds);
2393 s->write_msgfds = 0;
2394 s->write_msgfds_num = 0;
2395 }
2396
2397 return r;
2398}
2399#endif
2400
aliguori6f97dba2008-10-31 18:49:55 +00002401static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2402{
2403 TCPCharDriver *s = chr->opaque;
2404 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002405#ifndef _WIN32
2406 if (s->is_unix && s->write_msgfds_num) {
2407 return unix_send_msgfds(chr, buf, len);
2408 } else
2409#endif
2410 {
2411 return io_channel_send(s->chan, buf, len);
2412 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002413 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002414 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002415 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002416 }
2417}
2418
2419static int tcp_chr_read_poll(void *opaque)
2420{
2421 CharDriverState *chr = opaque;
2422 TCPCharDriver *s = chr->opaque;
2423 if (!s->connected)
2424 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002425 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002426 return s->max_size;
2427}
2428
2429#define IAC 255
2430#define IAC_BREAK 243
2431static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2432 TCPCharDriver *s,
2433 uint8_t *buf, int *size)
2434{
2435 /* Handle any telnet client's basic IAC options to satisfy char by
2436 * char mode with no echo. All IAC options will be removed from
2437 * the buf and the do_telnetopt variable will be used to track the
2438 * state of the width of the IAC information.
2439 *
2440 * IAC commands come in sets of 3 bytes with the exception of the
2441 * "IAC BREAK" command and the double IAC.
2442 */
2443
2444 int i;
2445 int j = 0;
2446
2447 for (i = 0; i < *size; i++) {
2448 if (s->do_telnetopt > 1) {
2449 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2450 /* Double IAC means send an IAC */
2451 if (j != i)
2452 buf[j] = buf[i];
2453 j++;
2454 s->do_telnetopt = 1;
2455 } else {
2456 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2457 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002458 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002459 s->do_telnetopt++;
2460 }
2461 s->do_telnetopt++;
2462 }
2463 if (s->do_telnetopt >= 4) {
2464 s->do_telnetopt = 1;
2465 }
2466 } else {
2467 if ((unsigned char)buf[i] == IAC) {
2468 s->do_telnetopt = 2;
2469 } else {
2470 if (j != i)
2471 buf[j] = buf[i];
2472 j++;
2473 }
2474 }
2475 }
2476 *size = j;
2477}
2478
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002479static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002480{
2481 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002482 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2483
2484 if (to_copy) {
2485 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2486
2487 g_free(s->read_msgfds);
2488 s->read_msgfds = 0;
2489 s->read_msgfds_num = 0;
2490 }
2491
2492 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002493}
2494
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002495static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2496{
2497 TCPCharDriver *s = chr->opaque;
2498
2499 /* clear old pending fd array */
2500 if (s->write_msgfds) {
2501 g_free(s->write_msgfds);
2502 }
2503
2504 if (num) {
2505 s->write_msgfds = g_malloc(num * sizeof(int));
2506 memcpy(s->write_msgfds, fds, num * sizeof(int));
2507 }
2508
2509 s->write_msgfds_num = num;
2510
2511 return 0;
2512}
2513
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002514#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002515static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2516{
2517 TCPCharDriver *s = chr->opaque;
2518 struct cmsghdr *cmsg;
2519
2520 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002521 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002522
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002523 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002524 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002525 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002526 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002527 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002528
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002529 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2530
2531 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002532 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002533 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002534
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002535 /* close and clean read_msgfds */
2536 for (i = 0; i < s->read_msgfds_num; i++) {
2537 close(s->read_msgfds[i]);
2538 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002539
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002540 if (s->read_msgfds_num) {
2541 g_free(s->read_msgfds);
2542 }
2543
2544 s->read_msgfds_num = fd_size / sizeof(int);
2545 s->read_msgfds = g_malloc(fd_size);
2546 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2547
2548 for (i = 0; i < s->read_msgfds_num; i++) {
2549 int fd = s->read_msgfds[i];
2550 if (fd < 0) {
2551 continue;
2552 }
2553
2554 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2555 qemu_set_block(fd);
2556
2557 #ifndef MSG_CMSG_CLOEXEC
2558 qemu_set_cloexec(fd);
2559 #endif
2560 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002561 }
2562}
2563
Mark McLoughlin9977c892009-07-22 09:11:38 +01002564static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2565{
2566 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002567 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002568 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002569 union {
2570 struct cmsghdr cmsg;
2571 char control[CMSG_SPACE(sizeof(int))];
2572 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002573 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002574 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002575
2576 iov[0].iov_base = buf;
2577 iov[0].iov_len = len;
2578
2579 msg.msg_iov = iov;
2580 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002581 msg.msg_control = &msg_control;
2582 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002583
Corey Bryant06138652012-08-14 16:43:42 -04002584#ifdef MSG_CMSG_CLOEXEC
2585 flags |= MSG_CMSG_CLOEXEC;
2586#endif
2587 ret = recvmsg(s->fd, &msg, flags);
2588 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002589 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002590 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002591
2592 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002593}
2594#else
2595static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2596{
2597 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002598 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002599}
2600#endif
2601
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302602static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2603{
2604 TCPCharDriver *s = chr->opaque;
2605 return g_io_create_watch(s->chan, cond);
2606}
2607
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002608static void tcp_chr_disconnect(CharDriverState *chr)
2609{
2610 TCPCharDriver *s = chr->opaque;
2611
2612 s->connected = 0;
2613 if (s->listen_chan) {
2614 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2615 tcp_chr_accept, chr);
2616 }
2617 remove_fd_in_watch(chr);
2618 g_io_channel_unref(s->chan);
2619 s->chan = NULL;
2620 closesocket(s->fd);
2621 s->fd = -1;
2622 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2623}
2624
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302625static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002626{
2627 CharDriverState *chr = opaque;
2628 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302629 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002630 int len, size;
2631
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302632 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002633 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302634 }
aliguori6f97dba2008-10-31 18:49:55 +00002635 len = sizeof(buf);
2636 if (len > s->max_size)
2637 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002638 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002639 if (size == 0) {
2640 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002641 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002642 } else if (size > 0) {
2643 if (s->do_telnetopt)
2644 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2645 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002646 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002647 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302648
2649 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002650}
2651
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002652static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2653{
2654 TCPCharDriver *s = chr->opaque;
2655 int size;
2656
2657 if (!s->connected) {
2658 return 0;
2659 }
2660
2661 size = tcp_chr_recv(chr, (void *) buf, len);
2662 if (size == 0) {
2663 /* connection closed */
2664 tcp_chr_disconnect(chr);
2665 }
2666
2667 return size;
2668}
2669
Blue Swirl68c18d12010-08-15 09:46:24 +00002670#ifndef _WIN32
2671CharDriverState *qemu_chr_open_eventfd(int eventfd)
2672{
David Marchande9d21c42014-06-11 17:25:16 +02002673 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2674
2675 if (chr) {
2676 chr->avail_connections = 1;
2677 }
2678
2679 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002680}
Blue Swirl68c18d12010-08-15 09:46:24 +00002681#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002682
aliguori6f97dba2008-10-31 18:49:55 +00002683static void tcp_chr_connect(void *opaque)
2684{
2685 CharDriverState *chr = opaque;
2686 TCPCharDriver *s = chr->opaque;
2687
2688 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302689 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302690 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2691 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002692 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002693 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002694}
2695
Gal Hammerac1b84d2014-02-25 12:12:35 +02002696static void tcp_chr_update_read_handler(CharDriverState *chr)
2697{
2698 TCPCharDriver *s = chr->opaque;
2699
2700 remove_fd_in_watch(chr);
2701 if (s->chan) {
2702 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2703 tcp_chr_read, chr);
2704 }
2705}
2706
aliguori6f97dba2008-10-31 18:49:55 +00002707#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2708static void tcp_chr_telnet_init(int fd)
2709{
2710 char buf[3];
2711 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2712 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2713 send(fd, (char *)buf, 3, 0);
2714 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2715 send(fd, (char *)buf, 3, 0);
2716 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2717 send(fd, (char *)buf, 3, 0);
2718 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2719 send(fd, (char *)buf, 3, 0);
2720}
2721
Daniel P. Berrange13661082011-06-23 13:31:42 +01002722static int tcp_chr_add_client(CharDriverState *chr, int fd)
2723{
2724 TCPCharDriver *s = chr->opaque;
2725 if (s->fd != -1)
2726 return -1;
2727
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002728 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002729 if (s->do_nodelay)
2730 socket_set_nodelay(fd);
2731 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302732 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002733 if (s->listen_tag) {
2734 g_source_remove(s->listen_tag);
2735 s->listen_tag = 0;
2736 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002737 tcp_chr_connect(chr);
2738
2739 return 0;
2740}
2741
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302742static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002743{
2744 CharDriverState *chr = opaque;
2745 TCPCharDriver *s = chr->opaque;
2746 struct sockaddr_in saddr;
2747#ifndef _WIN32
2748 struct sockaddr_un uaddr;
2749#endif
2750 struct sockaddr *addr;
2751 socklen_t len;
2752 int fd;
2753
2754 for(;;) {
2755#ifndef _WIN32
2756 if (s->is_unix) {
2757 len = sizeof(uaddr);
2758 addr = (struct sockaddr *)&uaddr;
2759 } else
2760#endif
2761 {
2762 len = sizeof(saddr);
2763 addr = (struct sockaddr *)&saddr;
2764 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002765 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002766 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002767 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302768 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002769 } else if (fd >= 0) {
2770 if (s->do_telnetopt)
2771 tcp_chr_telnet_init(fd);
2772 break;
2773 }
2774 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002775 if (tcp_chr_add_client(chr, fd) < 0)
2776 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302777
2778 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002779}
2780
2781static void tcp_chr_close(CharDriverState *chr)
2782{
2783 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002784 int i;
aliguori819f56b2009-03-28 17:58:14 +00002785 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302786 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302787 if (s->chan) {
2788 g_io_channel_unref(s->chan);
2789 }
aliguori6f97dba2008-10-31 18:49:55 +00002790 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002791 }
2792 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302793 if (s->listen_tag) {
2794 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002795 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302796 }
2797 if (s->listen_chan) {
2798 g_io_channel_unref(s->listen_chan);
2799 }
aliguori6f97dba2008-10-31 18:49:55 +00002800 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002801 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002802 if (s->read_msgfds_num) {
2803 for (i = 0; i < s->read_msgfds_num; i++) {
2804 close(s->read_msgfds[i]);
2805 }
2806 g_free(s->read_msgfds);
2807 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002808 if (s->write_msgfds_num) {
2809 g_free(s->write_msgfds);
2810 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002811 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002812 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002813}
2814
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002815static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
2816 bool is_listen, bool is_telnet,
2817 bool is_waitconnect,
2818 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002819{
2820 CharDriverState *chr = NULL;
2821 TCPCharDriver *s = NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002822 char host[NI_MAXHOST], serv[NI_MAXSERV];
2823 const char *left = "", *right = "";
2824 struct sockaddr_storage ss;
2825 socklen_t ss_len = sizeof(ss);
2826
2827 memset(&ss, 0, ss_len);
2828 if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02002829 error_setg_errno(errp, errno, "getsockname");
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002830 return NULL;
2831 }
2832
2833 chr = g_malloc0(sizeof(CharDriverState));
2834 s = g_malloc0(sizeof(TCPCharDriver));
2835
2836 s->connected = 0;
2837 s->fd = -1;
2838 s->listen_fd = -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002839 s->read_msgfds = 0;
2840 s->read_msgfds_num = 0;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002841 s->write_msgfds = 0;
2842 s->write_msgfds_num = 0;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002843
2844 chr->filename = g_malloc(256);
2845 switch (ss.ss_family) {
2846#ifndef _WIN32
2847 case AF_UNIX:
2848 s->is_unix = 1;
2849 snprintf(chr->filename, 256, "unix:%s%s",
2850 ((struct sockaddr_un *)(&ss))->sun_path,
2851 is_listen ? ",server" : "");
2852 break;
2853#endif
2854 case AF_INET6:
2855 left = "[";
2856 right = "]";
2857 /* fall through */
2858 case AF_INET:
2859 s->do_nodelay = do_nodelay;
2860 getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
2861 serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
Igor Mitsyankoe5545852013-03-10 17:58:05 +04002862 snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002863 is_telnet ? "telnet" : "tcp",
2864 left, host, right, serv,
2865 is_listen ? ",server" : "");
2866 break;
2867 }
2868
2869 chr->opaque = s;
2870 chr->chr_write = tcp_chr_write;
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002871 chr->chr_sync_read = tcp_chr_sync_read;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002872 chr->chr_close = tcp_chr_close;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002873 chr->get_msgfds = tcp_get_msgfds;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002874 chr->set_msgfds = tcp_set_msgfds;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002875 chr->chr_add_client = tcp_chr_add_client;
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302876 chr->chr_add_watch = tcp_chr_add_watch;
Gal Hammerac1b84d2014-02-25 12:12:35 +02002877 chr->chr_update_read_handler = tcp_chr_update_read_handler;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002878 /* be isn't opened until we get a connection */
2879 chr->explicit_be_open = true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002880
2881 if (is_listen) {
2882 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302883 s->listen_chan = io_channel_from_socket(s->listen_fd);
2884 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002885 if (is_telnet) {
2886 s->do_telnetopt = 1;
2887 }
2888 } else {
2889 s->connected = 1;
2890 s->fd = fd;
2891 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302892 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002893 tcp_chr_connect(chr);
2894 }
2895
2896 if (is_listen && is_waitconnect) {
Gerd Hoffmannfdca2122013-06-24 08:39:49 +02002897 fprintf(stderr, "QEMU waiting for connection on: %s\n",
2898 chr->filename);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302899 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002900 qemu_set_nonblock(s->listen_fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002901 }
2902 return chr;
2903}
2904
2905static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2906{
2907 CharDriverState *chr = NULL;
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002908 Error *local_err = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002909 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002910
liguange990a392013-06-18 11:45:35 +08002911 bool is_listen = qemu_opt_get_bool(opts, "server", false);
2912 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
2913 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
2914 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
2915 bool is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002916
aliguorif07b6002008-11-11 20:54:09 +00002917 if (is_unix) {
2918 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002919 fd = unix_listen_opts(opts, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002920 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002921 fd = unix_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002922 }
2923 } else {
2924 if (is_listen) {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002925 fd = inet_listen_opts(opts, 0, &local_err);
aliguorif07b6002008-11-11 20:54:09 +00002926 } else {
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002927 fd = inet_connect_opts(opts, &local_err, NULL, NULL);
aliguorif07b6002008-11-11 20:54:09 +00002928 }
2929 }
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002930 if (fd < 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002931 goto fail;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01002932 }
aliguori6f97dba2008-10-31 18:49:55 +00002933
2934 if (!is_waitconnect)
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002935 qemu_set_nonblock(fd);
aliguori6f97dba2008-10-31 18:49:55 +00002936
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002937 chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
2938 is_waitconnect, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002939 if (local_err) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002940 goto fail;
aliguori6f97dba2008-10-31 18:49:55 +00002941 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002942 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002943
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002944
aliguori6f97dba2008-10-31 18:49:55 +00002945 fail:
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002946 if (local_err) {
2947 qerror_report_err(local_err);
2948 error_free(local_err);
2949 }
2950 if (fd >= 0) {
aliguori6f97dba2008-10-31 18:49:55 +00002951 closesocket(fd);
Paolo Bonzini87d5f242012-10-02 09:16:49 +02002952 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01002953 if (chr) {
2954 g_free(chr->opaque);
2955 g_free(chr);
2956 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002957 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002958}
2959
Lei Li51767e72013-01-25 00:03:19 +08002960/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01002961/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08002962
2963typedef struct {
2964 size_t size;
2965 size_t prod;
2966 size_t cons;
2967 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01002968} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08002969
Markus Armbruster3949e592013-02-06 21:27:24 +01002970static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08002971{
Markus Armbruster3949e592013-02-06 21:27:24 +01002972 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002973
Markus Armbruster5c230102013-02-06 21:27:23 +01002974 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08002975}
2976
Markus Armbruster3949e592013-02-06 21:27:24 +01002977static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002978{
Markus Armbruster3949e592013-02-06 21:27:24 +01002979 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002980 int i;
2981
2982 if (!buf || (len < 0)) {
2983 return -1;
2984 }
2985
2986 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01002987 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
2988 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08002989 d->cons = d->prod - d->size;
2990 }
2991 }
2992
2993 return 0;
2994}
2995
Markus Armbruster3949e592013-02-06 21:27:24 +01002996static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08002997{
Markus Armbruster3949e592013-02-06 21:27:24 +01002998 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08002999 int i;
3000
Markus Armbruster5c230102013-02-06 21:27:23 +01003001 for (i = 0; i < len && d->cons != d->prod; i++) {
3002 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003003 }
3004
3005 return i;
3006}
3007
Markus Armbruster3949e592013-02-06 21:27:24 +01003008static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003009{
Markus Armbruster3949e592013-02-06 21:27:24 +01003010 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003011
3012 g_free(d->cbuf);
3013 g_free(d);
3014 chr->opaque = NULL;
3015}
3016
Markus Armbruster4f573782013-07-26 16:44:32 +02003017static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3018 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003019{
3020 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003021 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003022
3023 chr = g_malloc0(sizeof(CharDriverState));
3024 d = g_malloc(sizeof(*d));
3025
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003026 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003027
3028 /* The size must be power of 2 */
3029 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003030 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003031 goto fail;
3032 }
3033
3034 d->prod = 0;
3035 d->cons = 0;
3036 d->cbuf = g_malloc0(d->size);
3037
3038 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003039 chr->chr_write = ringbuf_chr_write;
3040 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003041
3042 return chr;
3043
3044fail:
3045 g_free(d);
3046 g_free(chr);
3047 return NULL;
3048}
3049
Hani Benhabiles8e597772014-05-27 23:39:30 +01003050bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003051{
Markus Armbruster3949e592013-02-06 21:27:24 +01003052 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003053}
3054
Markus Armbruster3949e592013-02-06 21:27:24 +01003055void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003056 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003057 Error **errp)
3058{
3059 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003060 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003061 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003062 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003063
3064 chr = qemu_chr_find(device);
3065 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003066 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003067 return;
3068 }
3069
Markus Armbruster3949e592013-02-06 21:27:24 +01003070 if (!chr_is_ringbuf(chr)) {
3071 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003072 return;
3073 }
3074
Lei Li1f590cf2013-01-25 00:03:20 +08003075 if (has_format && (format == DATA_FORMAT_BASE64)) {
3076 write_data = g_base64_decode(data, &write_count);
3077 } else {
3078 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003079 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003080 }
3081
Markus Armbruster3949e592013-02-06 21:27:24 +01003082 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003083
Markus Armbruster13289fb2013-02-06 21:27:18 +01003084 if (write_data != (uint8_t *)data) {
3085 g_free((void *)write_data);
3086 }
3087
Lei Li1f590cf2013-01-25 00:03:20 +08003088 if (ret < 0) {
3089 error_setg(errp, "Failed to write to device %s", device);
3090 return;
3091 }
3092}
3093
Markus Armbruster3949e592013-02-06 21:27:24 +01003094char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003095 bool has_format, enum DataFormat format,
3096 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003097{
3098 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003099 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003100 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003101 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003102
3103 chr = qemu_chr_find(device);
3104 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003105 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003106 return NULL;
3107 }
3108
Markus Armbruster3949e592013-02-06 21:27:24 +01003109 if (!chr_is_ringbuf(chr)) {
3110 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003111 return NULL;
3112 }
3113
3114 if (size <= 0) {
3115 error_setg(errp, "size must be greater than zero");
3116 return NULL;
3117 }
3118
Markus Armbruster3949e592013-02-06 21:27:24 +01003119 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003120 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003121 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003122
Markus Armbruster3949e592013-02-06 21:27:24 +01003123 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003124
3125 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003126 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003127 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003128 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003129 /*
3130 * FIXME should read only complete, valid UTF-8 characters up
3131 * to @size bytes. Invalid sequences should be replaced by a
3132 * suitable replacement character. Except when (and only
3133 * when) ring buffer lost characters since last read, initial
3134 * continuation characters should be dropped.
3135 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003136 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003137 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003138 }
3139
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003140 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003141}
3142
Gerd Hoffmann33521632009-12-08 13:11:49 +01003143QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003144{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003145 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003146 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003147 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003148 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003149 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003150
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003151 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003152 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003153 qerror_report_err(local_err);
3154 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003155 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003156 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003157
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003158 if (strstart(filename, "mon:", &p)) {
3159 filename = p;
3160 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003161 if (strcmp(filename, "stdio") == 0) {
3162 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3163 * but pass it to the guest. Handle this only for compat syntax,
3164 * for -chardev syntax we have special option for this.
3165 * This is what -nographic did, redirecting+muxing serial+monitor
3166 * to stdio causing Ctrl+C to be passed to guest. */
3167 qemu_opt_set(opts, "signal", "off");
3168 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003169 }
3170
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003171 if (strcmp(filename, "null") == 0 ||
3172 strcmp(filename, "pty") == 0 ||
3173 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003174 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003175 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003176 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003177 return opts;
3178 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003179 if (strstart(filename, "vc", &p)) {
3180 qemu_opt_set(opts, "backend", "vc");
3181 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003182 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003183 /* pixels */
3184 qemu_opt_set(opts, "width", width);
3185 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003186 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003187 /* chars */
3188 qemu_opt_set(opts, "cols", width);
3189 qemu_opt_set(opts, "rows", height);
3190 } else {
3191 goto fail;
3192 }
3193 }
3194 return opts;
3195 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003196 if (strcmp(filename, "con:") == 0) {
3197 qemu_opt_set(opts, "backend", "console");
3198 return opts;
3199 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003200 if (strstart(filename, "COM", NULL)) {
3201 qemu_opt_set(opts, "backend", "serial");
3202 qemu_opt_set(opts, "path", filename);
3203 return opts;
3204 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003205 if (strstart(filename, "file:", &p)) {
3206 qemu_opt_set(opts, "backend", "file");
3207 qemu_opt_set(opts, "path", p);
3208 return opts;
3209 }
3210 if (strstart(filename, "pipe:", &p)) {
3211 qemu_opt_set(opts, "backend", "pipe");
3212 qemu_opt_set(opts, "path", p);
3213 return opts;
3214 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003215 if (strstart(filename, "tcp:", &p) ||
3216 strstart(filename, "telnet:", &p)) {
3217 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3218 host[0] = 0;
3219 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3220 goto fail;
3221 }
3222 qemu_opt_set(opts, "backend", "socket");
3223 qemu_opt_set(opts, "host", host);
3224 qemu_opt_set(opts, "port", port);
3225 if (p[pos] == ',') {
3226 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3227 goto fail;
3228 }
3229 if (strstart(filename, "telnet:", &p))
3230 qemu_opt_set(opts, "telnet", "on");
3231 return opts;
3232 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003233 if (strstart(filename, "udp:", &p)) {
3234 qemu_opt_set(opts, "backend", "udp");
3235 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3236 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003237 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003238 goto fail;
3239 }
3240 }
3241 qemu_opt_set(opts, "host", host);
3242 qemu_opt_set(opts, "port", port);
3243 if (p[pos] == '@') {
3244 p += pos + 1;
3245 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3246 host[0] = 0;
3247 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003248 goto fail;
3249 }
3250 }
3251 qemu_opt_set(opts, "localaddr", host);
3252 qemu_opt_set(opts, "localport", port);
3253 }
3254 return opts;
3255 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003256 if (strstart(filename, "unix:", &p)) {
3257 qemu_opt_set(opts, "backend", "socket");
3258 if (qemu_opts_do_parse(opts, p, "path") != 0)
3259 goto fail;
3260 return opts;
3261 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003262 if (strstart(filename, "/dev/parport", NULL) ||
3263 strstart(filename, "/dev/ppi", NULL)) {
3264 qemu_opt_set(opts, "backend", "parport");
3265 qemu_opt_set(opts, "path", filename);
3266 return opts;
3267 }
3268 if (strstart(filename, "/dev/", NULL)) {
3269 qemu_opt_set(opts, "backend", "tty");
3270 qemu_opt_set(opts, "path", filename);
3271 return opts;
3272 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003273
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003274fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003275 qemu_opts_del(opts);
3276 return NULL;
3277}
3278
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003279static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3280 Error **errp)
3281{
3282 const char *path = qemu_opt_get(opts, "path");
3283
3284 if (path == NULL) {
3285 error_setg(errp, "chardev: file: no filename given");
3286 return;
3287 }
3288 backend->file = g_new0(ChardevFile, 1);
3289 backend->file->out = g_strdup(path);
3290}
3291
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003292static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3293 Error **errp)
3294{
3295 backend->stdio = g_new0(ChardevStdio, 1);
3296 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003297 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003298}
3299
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003300static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3301 Error **errp)
3302{
3303 const char *device = qemu_opt_get(opts, "path");
3304
3305 if (device == NULL) {
3306 error_setg(errp, "chardev: serial/tty: no device path given");
3307 return;
3308 }
3309 backend->serial = g_new0(ChardevHostdev, 1);
3310 backend->serial->device = g_strdup(device);
3311}
3312
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003313static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3314 Error **errp)
3315{
3316 const char *device = qemu_opt_get(opts, "path");
3317
3318 if (device == NULL) {
3319 error_setg(errp, "chardev: parallel: no device path given");
3320 return;
3321 }
3322 backend->parallel = g_new0(ChardevHostdev, 1);
3323 backend->parallel->device = g_strdup(device);
3324}
3325
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003326static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3327 Error **errp)
3328{
3329 const char *device = qemu_opt_get(opts, "path");
3330
3331 if (device == NULL) {
3332 error_setg(errp, "chardev: pipe: no device path given");
3333 return;
3334 }
3335 backend->pipe = g_new0(ChardevHostdev, 1);
3336 backend->pipe->device = g_strdup(device);
3337}
3338
Markus Armbruster4f573782013-07-26 16:44:32 +02003339static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3340 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003341{
3342 int val;
3343
Markus Armbruster3a1da422013-07-26 16:44:34 +02003344 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003345
Markus Armbruster0f953052013-06-27 16:22:07 +02003346 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003347 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003348 backend->ringbuf->has_size = true;
3349 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003350 }
3351}
3352
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003353static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3354 Error **errp)
3355{
3356 const char *chardev = qemu_opt_get(opts, "chardev");
3357
3358 if (chardev == NULL) {
3359 error_setg(errp, "chardev: mux: no chardev given");
3360 return;
3361 }
3362 backend->mux = g_new0(ChardevMux, 1);
3363 backend->mux->chardev = g_strdup(chardev);
3364}
3365
Anthony Liguorid654f342013-03-05 23:21:28 +05303366typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003367 const char *name;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003368 /* old, pre qapi */
Markus Armbruster1f514702012-02-07 15:09:08 +01003369 CharDriverState *(*open)(QemuOpts *opts);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003370 /* new, qapi-based */
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003371 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003372 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303373} CharDriver;
3374
3375static GSList *backends;
3376
3377void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
3378{
3379 CharDriver *s;
3380
3381 s = g_malloc0(sizeof(*s));
3382 s->name = g_strdup(name);
3383 s->open = open;
3384
3385 backends = g_slist_append(backends, s);
3386}
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003387
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003388void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003389 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3390{
3391 CharDriver *s;
3392
3393 s = g_malloc0(sizeof(*s));
3394 s->name = g_strdup(name);
3395 s->kind = kind;
3396 s->parse = parse;
3397
3398 backends = g_slist_append(backends, s);
3399}
3400
Anthony Liguorif69554b2011-08-15 11:17:37 -05003401CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003402 void (*init)(struct CharDriverState *s),
3403 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003404{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003405 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303406 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003407 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303408 GSList *i;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003409
3410 if (qemu_opts_id(opts) == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003411 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003412 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003413 }
3414
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003415 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003416 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003417 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003418 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003419 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303420 for (i = backends; i; i = i->next) {
3421 cd = i->data;
3422
3423 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003424 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303425 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003426 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303427 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003428 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003429 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003430 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003431 }
3432
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003433 if (!cd->open) {
3434 /* using new, qapi init */
3435 ChardevBackend *backend = g_new0(ChardevBackend, 1);
3436 ChardevReturn *ret = NULL;
3437 const char *id = qemu_opts_id(opts);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003438 char *bid = NULL;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003439
3440 if (qemu_opt_get_bool(opts, "mux", 0)) {
3441 bid = g_strdup_printf("%s-base", id);
3442 }
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003443
3444 chr = NULL;
3445 backend->kind = cd->kind;
3446 if (cd->parse) {
Markus Armbruster0aff6372014-05-19 18:57:35 +02003447 cd->parse(opts, backend, &local_err);
3448 if (local_err) {
3449 error_propagate(errp, local_err);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003450 goto qapi_out;
3451 }
3452 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003453 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003454 if (!ret) {
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003455 goto qapi_out;
3456 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003457
3458 if (bid) {
3459 qapi_free_ChardevBackend(backend);
3460 qapi_free_ChardevReturn(ret);
3461 backend = g_new0(ChardevBackend, 1);
3462 backend->mux = g_new0(ChardevMux, 1);
3463 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3464 backend->mux->chardev = g_strdup(bid);
3465 ret = qmp_chardev_add(id, backend, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003466 if (!ret) {
Gerd Hoffmannee6ee832013-09-13 12:48:47 +02003467 chr = qemu_chr_find(bid);
3468 qemu_chr_delete(chr);
3469 chr = NULL;
3470 goto qapi_out;
3471 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003472 }
3473
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003474 chr = qemu_chr_find(id);
Markus Armbruster2ea3e2c2013-06-27 15:25:12 +02003475 chr->opts = opts;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003476
3477 qapi_out:
3478 qapi_free_ChardevBackend(backend);
3479 qapi_free_ChardevReturn(ret);
Gerd Hoffmanndc2c4ec2013-06-24 08:39:53 +02003480 g_free(bid);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003481 return chr;
3482 }
3483
Anthony Liguorid654f342013-03-05 23:21:28 +05303484 chr = cd->open(opts);
Markus Armbruster1f514702012-02-07 15:09:08 +01003485 if (!chr) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003486 error_setg(errp, "chardev: opening backend \"%s\" failed",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003487 qemu_opt_get(opts, "backend"));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003488 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003489 }
3490
3491 if (!chr->filename)
Anthony Liguori7267c092011-08-20 22:09:37 -05003492 chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003493 chr->init = init;
Michael Rothbd5c51e2013-06-07 15:19:53 -05003494 /* if we didn't create the chardev via qmp_chardev_add, we
3495 * need to send the OPENED event here
3496 */
3497 if (!chr->explicit_be_open) {
3498 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
3499 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00003500 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003501
3502 if (qemu_opt_get_bool(opts, "mux", 0)) {
3503 CharDriverState *base = chr;
3504 int len = strlen(qemu_opts_id(opts)) + 6;
Anthony Liguori7267c092011-08-20 22:09:37 -05003505 base->label = g_malloc(len);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003506 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
3507 chr = qemu_chr_open_mux(base);
3508 chr->filename = base->filename;
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003509 chr->avail_connections = MAX_MUX;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003510 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Kusanagi Kouichid5b27162011-04-26 19:19:26 +09003511 } else {
3512 chr->avail_connections = 1;
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003513 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003514 chr->label = g_strdup(qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003515 chr->opts = opts;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003516 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003517
3518err:
3519 qemu_opts_del(opts);
3520 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003521}
3522
Anthony Liguori27143a42011-08-15 11:17:36 -05003523CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003524{
3525 const char *p;
3526 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003527 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003528 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003529
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003530 if (strstart(filename, "chardev:", &p)) {
3531 return qemu_chr_find(p);
3532 }
3533
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003534 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003535 if (!opts)
3536 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003537
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003538 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003539 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003540 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003541 error_free(err);
3542 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003543 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003544 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003545 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003546 }
3547 return chr;
3548}
3549
Anthony Liguori15f31512011-08-15 11:17:35 -05003550void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003551{
3552 if (chr->chr_set_echo) {
3553 chr->chr_set_echo(chr, echo);
3554 }
3555}
3556
Hans de Goede8e25daa2013-03-26 11:07:57 +01003557void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003558{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003559 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003560 return;
3561 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003562 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003563 if (chr->chr_set_fe_open) {
3564 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003565 }
3566}
3567
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003568void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3569{
3570 if (chr->chr_fe_event) {
3571 chr->chr_fe_event(chr, event);
3572 }
3573}
3574
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003575int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3576 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303577{
3578 GSource *src;
3579 guint tag;
3580
3581 if (s->chr_add_watch == NULL) {
3582 return -ENOSYS;
3583 }
3584
3585 src = s->chr_add_watch(s, cond);
3586 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3587 tag = g_source_attach(src, NULL);
3588 g_source_unref(src);
3589
3590 return tag;
3591}
3592
Hans de Goede44c473d2013-03-27 20:29:39 +01003593int qemu_chr_fe_claim(CharDriverState *s)
3594{
3595 if (s->avail_connections < 1) {
3596 return -1;
3597 }
3598 s->avail_connections--;
3599 return 0;
3600}
3601
3602void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3603{
3604 if (qemu_chr_fe_claim(s) != 0) {
3605 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3606 __func__, s->label);
3607 exit(1);
3608 }
3609}
3610
3611void qemu_chr_fe_release(CharDriverState *s)
3612{
3613 s->avail_connections++;
3614}
3615
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003616void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003617{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003618 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003619 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003620 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003621 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003622 g_free(chr->filename);
3623 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003624 if (chr->opts) {
3625 qemu_opts_del(chr->opts);
3626 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003627 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003628}
3629
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003630ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003631{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003632 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003633 CharDriverState *chr;
3634
Blue Swirl72cf2d42009-09-12 07:36:22 +00003635 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003636 ChardevInfoList *info = g_malloc0(sizeof(*info));
3637 info->value = g_malloc0(sizeof(*info->value));
3638 info->value->label = g_strdup(chr->label);
3639 info->value->filename = g_strdup(chr->filename);
3640
3641 info->next = chr_list;
3642 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003643 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003644
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003645 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003646}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003647
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003648ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3649{
3650 ChardevBackendInfoList *backend_list = NULL;
3651 CharDriver *c = NULL;
3652 GSList *i = NULL;
3653
3654 for (i = backends; i; i = i->next) {
3655 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3656 c = i->data;
3657 info->value = g_malloc0(sizeof(*info->value));
3658 info->value->name = g_strdup(c->name);
3659
3660 info->next = backend_list;
3661 backend_list = info;
3662 }
3663
3664 return backend_list;
3665}
3666
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003667CharDriverState *qemu_chr_find(const char *name)
3668{
3669 CharDriverState *chr;
3670
Blue Swirl72cf2d42009-09-12 07:36:22 +00003671 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003672 if (strcmp(chr->label, name) != 0)
3673 continue;
3674 return chr;
3675 }
3676 return NULL;
3677}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003678
3679/* Get a character (serial) device interface. */
3680CharDriverState *qemu_char_get_next_serial(void)
3681{
3682 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003683 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003684
3685 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003686
3687 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3688 chr = serial_hds[next_serial++];
3689 qemu_chr_fe_claim_no_fail(chr);
3690 return chr;
3691 }
3692 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003693}
3694
Paolo Bonzini4d454572012-11-26 16:03:42 +01003695QemuOptsList qemu_chardev_opts = {
3696 .name = "chardev",
3697 .implied_opt_name = "backend",
3698 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3699 .desc = {
3700 {
3701 .name = "backend",
3702 .type = QEMU_OPT_STRING,
3703 },{
3704 .name = "path",
3705 .type = QEMU_OPT_STRING,
3706 },{
3707 .name = "host",
3708 .type = QEMU_OPT_STRING,
3709 },{
3710 .name = "port",
3711 .type = QEMU_OPT_STRING,
3712 },{
3713 .name = "localaddr",
3714 .type = QEMU_OPT_STRING,
3715 },{
3716 .name = "localport",
3717 .type = QEMU_OPT_STRING,
3718 },{
3719 .name = "to",
3720 .type = QEMU_OPT_NUMBER,
3721 },{
3722 .name = "ipv4",
3723 .type = QEMU_OPT_BOOL,
3724 },{
3725 .name = "ipv6",
3726 .type = QEMU_OPT_BOOL,
3727 },{
3728 .name = "wait",
3729 .type = QEMU_OPT_BOOL,
3730 },{
3731 .name = "server",
3732 .type = QEMU_OPT_BOOL,
3733 },{
3734 .name = "delay",
3735 .type = QEMU_OPT_BOOL,
3736 },{
3737 .name = "telnet",
3738 .type = QEMU_OPT_BOOL,
3739 },{
3740 .name = "width",
3741 .type = QEMU_OPT_NUMBER,
3742 },{
3743 .name = "height",
3744 .type = QEMU_OPT_NUMBER,
3745 },{
3746 .name = "cols",
3747 .type = QEMU_OPT_NUMBER,
3748 },{
3749 .name = "rows",
3750 .type = QEMU_OPT_NUMBER,
3751 },{
3752 .name = "mux",
3753 .type = QEMU_OPT_BOOL,
3754 },{
3755 .name = "signal",
3756 .type = QEMU_OPT_BOOL,
3757 },{
3758 .name = "name",
3759 .type = QEMU_OPT_STRING,
3760 },{
3761 .name = "debug",
3762 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003763 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003764 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003765 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003766 },{
3767 .name = "chardev",
3768 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003769 },
3770 { /* end of list */ }
3771 },
3772};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003773
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003774#ifdef _WIN32
3775
3776static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3777{
3778 HANDLE out;
3779
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003780 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003781 error_setg(errp, "input file not supported");
3782 return NULL;
3783 }
3784
3785 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3786 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3787 if (out == INVALID_HANDLE_VALUE) {
3788 error_setg(errp, "open %s failed", file->out);
3789 return NULL;
3790 }
3791 return qemu_chr_open_win_file(out);
3792}
3793
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003794static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3795 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003796{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003797 return qemu_chr_open_win_path(serial->device);
3798}
3799
3800static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3801 Error **errp)
3802{
3803 error_setg(errp, "character device backend type 'parallel' not supported");
3804 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003805}
3806
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003807#else /* WIN32 */
3808
3809static int qmp_chardev_open_file_source(char *src, int flags,
3810 Error **errp)
3811{
3812 int fd = -1;
3813
3814 TFR(fd = qemu_open(src, flags, 0666));
3815 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02003816 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003817 }
3818 return fd;
3819}
3820
3821static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3822{
Markus Armbruster5f758362014-05-19 18:57:34 +02003823 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003824
3825 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
3826 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003827 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003828 return NULL;
3829 }
3830
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003831 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003832 flags = O_RDONLY;
3833 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003834 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003835 qemu_close(out);
3836 return NULL;
3837 }
3838 }
3839
3840 return qemu_chr_open_fd(in, out);
3841}
3842
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003843static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3844 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003845{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003846#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003847 int fd;
3848
3849 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003850 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003851 return NULL;
3852 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003853 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003854 return qemu_chr_open_tty_fd(fd);
3855#else
3856 error_setg(errp, "character device backend type 'serial' not supported");
3857 return NULL;
3858#endif
3859}
3860
3861static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3862 Error **errp)
3863{
3864#ifdef HAVE_CHARDEV_PARPORT
3865 int fd;
3866
3867 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003868 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003869 return NULL;
3870 }
3871 return qemu_chr_open_pp_fd(fd);
3872#else
3873 error_setg(errp, "character device backend type 'parallel' not supported");
3874 return NULL;
3875#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003876}
3877
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003878#endif /* WIN32 */
3879
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003880static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
3881 Error **errp)
3882{
3883 SocketAddress *addr = sock->addr;
3884 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
3885 bool is_listen = sock->has_server ? sock->server : true;
3886 bool is_telnet = sock->has_telnet ? sock->telnet : false;
3887 bool is_waitconnect = sock->has_wait ? sock->wait : false;
3888 int fd;
3889
3890 if (is_listen) {
3891 fd = socket_listen(addr, errp);
3892 } else {
3893 fd = socket_connect(addr, errp, NULL, NULL);
3894 }
Markus Armbruster5f758362014-05-19 18:57:34 +02003895 if (fd < 0) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003896 return NULL;
3897 }
3898 return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
3899 is_telnet, is_waitconnect, errp);
3900}
3901
Lei Li08d0ab32013-05-20 14:51:03 +08003902static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
3903 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003904{
3905 int fd;
3906
Lei Li08d0ab32013-05-20 14:51:03 +08003907 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02003908 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003909 return NULL;
3910 }
3911 return qemu_chr_open_udp_fd(fd);
3912}
3913
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003914ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
3915 Error **errp)
3916{
3917 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003918 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003919
3920 chr = qemu_chr_find(id);
3921 if (chr) {
3922 error_setg(errp, "Chardev '%s' already exists", id);
3923 g_free(ret);
3924 return NULL;
3925 }
3926
3927 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003928 case CHARDEV_BACKEND_KIND_FILE:
3929 chr = qmp_chardev_open_file(backend->file, errp);
3930 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003931 case CHARDEV_BACKEND_KIND_SERIAL:
3932 chr = qmp_chardev_open_serial(backend->serial, errp);
3933 break;
3934 case CHARDEV_BACKEND_KIND_PARALLEL:
3935 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003936 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003937 case CHARDEV_BACKEND_KIND_PIPE:
3938 chr = qemu_chr_open_pipe(backend->pipe);
3939 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003940 case CHARDEV_BACKEND_KIND_SOCKET:
3941 chr = qmp_chardev_open_socket(backend->socket, errp);
3942 break;
Lei Li08d0ab32013-05-20 14:51:03 +08003943 case CHARDEV_BACKEND_KIND_UDP:
3944 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01003945 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003946#ifdef HAVE_CHARDEV_TTY
3947 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01003948 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003949 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01003950#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003951 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01003952 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003953 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01003954 case CHARDEV_BACKEND_KIND_MUX:
3955 base = qemu_chr_find(backend->mux->chardev);
3956 if (base == NULL) {
3957 error_setg(errp, "mux: base chardev %s not found",
3958 backend->mux->chardev);
3959 break;
3960 }
3961 chr = qemu_chr_open_mux(base);
3962 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01003963 case CHARDEV_BACKEND_KIND_MSMOUSE:
3964 chr = qemu_chr_open_msmouse();
3965 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01003966#ifdef CONFIG_BRLAPI
3967 case CHARDEV_BACKEND_KIND_BRAILLE:
3968 chr = chr_baum_init();
3969 break;
3970#endif
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003971 case CHARDEV_BACKEND_KIND_STDIO:
3972 chr = qemu_chr_open_stdio(backend->stdio);
3973 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01003974#ifdef _WIN32
3975 case CHARDEV_BACKEND_KIND_CONSOLE:
3976 chr = qemu_chr_open_win_con();
3977 break;
3978#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01003979#ifdef CONFIG_SPICE
3980 case CHARDEV_BACKEND_KIND_SPICEVMC:
3981 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
3982 break;
3983 case CHARDEV_BACKEND_KIND_SPICEPORT:
3984 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
3985 break;
3986#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01003987 case CHARDEV_BACKEND_KIND_VC:
3988 chr = vc_init(backend->vc);
3989 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02003990 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003991 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02003992 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003993 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003994 default:
3995 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
3996 break;
3997 }
3998
Markus Armbruster3894c782014-05-19 18:57:36 +02003999 /*
4000 * Character backend open hasn't been fully converted to the Error
4001 * API. Some opens fail without setting an error. Set a generic
4002 * error then.
4003 * TODO full conversion to Error API
4004 */
4005 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004006 error_setg(errp, "Failed to create chardev");
4007 }
4008 if (chr) {
4009 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004010 chr->avail_connections =
4011 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004012 if (!chr->filename) {
4013 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4014 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004015 if (!chr->explicit_be_open) {
4016 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4017 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004018 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4019 return ret;
4020 } else {
4021 g_free(ret);
4022 return NULL;
4023 }
4024}
4025
4026void qmp_chardev_remove(const char *id, Error **errp)
4027{
4028 CharDriverState *chr;
4029
4030 chr = qemu_chr_find(id);
4031 if (NULL == chr) {
4032 error_setg(errp, "Chardev '%s' not found", id);
4033 return;
4034 }
4035 if (chr->chr_can_read || chr->chr_read ||
4036 chr->chr_event || chr->handler_opaque) {
4037 error_setg(errp, "Chardev '%s' is busy", id);
4038 return;
4039 }
4040 qemu_chr_delete(chr);
4041}
Anthony Liguorid654f342013-03-05 23:21:28 +05304042
4043static void register_types(void)
4044{
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004045 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
Anthony Liguorid654f342013-03-05 23:21:28 +05304046 register_char_driver("socket", qemu_chr_open_socket);
4047 register_char_driver("udp", qemu_chr_open_udp);
Markus Armbruster3a1da422013-07-26 16:44:34 +02004048 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Markus Armbruster4f573782013-07-26 16:44:32 +02004049 qemu_chr_parse_ringbuf);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01004050 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
4051 qemu_chr_parse_file_out);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004052 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
4053 qemu_chr_parse_stdio);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01004054 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
4055 qemu_chr_parse_serial);
4056 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
4057 qemu_chr_parse_serial);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01004058 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4059 qemu_chr_parse_parallel);
4060 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4061 qemu_chr_parse_parallel);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004062 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004063 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004064 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
4065 qemu_chr_parse_pipe);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004066 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
4067 qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004068 /* Bug-compatibility: */
4069 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
4070 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004071 /* this must be done after machine init, since we register FEs with muxes
4072 * as part of realize functions like serial_isa_realizefn when -nographic
4073 * is specified
4074 */
4075 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304076}
4077
4078type_init(register_types);