blob: faeffd469846f150d7cfc5975b7fc30a5bc865f8 [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 Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010027#include "qemu/error-report.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"
Corey Minyardcfb429c2014-10-02 11:17:35 -050032#include "qapi/qmp-input-visitor.h"
33#include "qapi/qmp-output-visitor.h"
34#include "qapi-visit.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
36#include <unistd.h>
37#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000038#include <time.h>
39#include <errno.h>
40#include <sys/time.h>
41#include <zlib.h>
42
43#ifndef _WIN32
44#include <sys/times.h>
45#include <sys/wait.h>
46#include <termios.h>
47#include <sys/mman.h>
48#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <sys/socket.h>
51#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000054#include <netdb.h>
55#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020056#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000057#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040058#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
59#include <dev/ppbus/ppi.h>
60#include <dev/ppbus/ppbconf.h>
61#elif defined(__DragonFly__)
62#include <dev/misc/ppi/ppi.h>
63#include <bus/ppbus/ppbconf.h>
64#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010065#else
aliguori6f97dba2008-10-31 18:49:55 +000066#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000067#include <linux/ppdev.h>
68#include <linux/parport.h>
69#endif
70#ifdef __sun__
71#include <sys/stat.h>
72#include <sys/ethernet.h>
73#include <sys/sockio.h>
74#include <netinet/arp.h>
75#include <netinet/in.h>
76#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#include <netinet/ip_icmp.h> // must come after ip.h
79#include <netinet/udp.h>
80#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000081#endif
82#endif
83#endif
84
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010085#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020086#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000087
Amit Shah9bd78542009-11-03 19:59:54 +053088#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030089#define READ_RETRIES 10
Corey Minyard9f781162014-10-02 11:17:33 -050090#define CHR_MAX_FILENAME_SIZE 256
Michael S. Tsirkinc4095722014-11-02 18:48:32 +020091#define TCP_MAX_FDS 16
Amit Shah9bd78542009-11-03 19:59:54 +053092
aliguori6f97dba2008-10-31 18:49:55 +000093/***********************************************************/
Corey Minyardcfb429c2014-10-02 11:17:35 -050094/* Socket address helpers */
95static void qapi_copy_SocketAddress(SocketAddress **p_dest,
96 SocketAddress *src)
97{
98 QmpOutputVisitor *qov;
99 QmpInputVisitor *qiv;
100 Visitor *ov, *iv;
101 QObject *obj;
102
103 *p_dest = NULL;
104
105 qov = qmp_output_visitor_new();
106 ov = qmp_output_get_visitor(qov);
107 visit_type_SocketAddress(ov, &src, NULL, &error_abort);
108 obj = qmp_output_get_qobject(qov);
109 qmp_output_visitor_cleanup(qov);
110 if (!obj) {
111 return;
112 }
113
114 qiv = qmp_input_visitor_new(obj);
115 iv = qmp_input_get_visitor(qiv);
116 visit_type_SocketAddress(iv, p_dest, NULL, &error_abort);
117 qmp_input_visitor_cleanup(qiv);
118 qobject_decref(obj);
119}
120
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500121static int SocketAddress_to_str(char *dest, int max_len,
122 const char *prefix, SocketAddress *addr,
123 bool is_listen, bool is_telnet)
124{
125 switch (addr->kind) {
126 case SOCKET_ADDRESS_KIND_INET:
127 return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix,
128 is_telnet ? "telnet" : "tcp", addr->inet->host,
129 addr->inet->port, is_listen ? ",server" : "");
130 break;
131 case SOCKET_ADDRESS_KIND_UNIX:
132 return snprintf(dest, max_len, "%sunix:%s%s", prefix,
133 addr->q_unix->path, is_listen ? ",server" : "");
134 break;
135 case SOCKET_ADDRESS_KIND_FD:
136 return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->fd->str,
137 is_listen ? ",server" : "");
138 break;
139 default:
140 abort();
141 }
142}
143
144static int sockaddr_to_str(char *dest, int max_len,
145 struct sockaddr_storage *ss, socklen_t ss_len,
Corey Minyard01ca5192014-10-02 11:17:38 -0500146 struct sockaddr_storage *ps, socklen_t ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500147 bool is_listen, bool is_telnet)
148{
Corey Minyard01ca5192014-10-02 11:17:38 -0500149 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
150 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500151 const char *left = "", *right = "";
152
153 switch (ss->ss_family) {
154#ifndef _WIN32
155 case AF_UNIX:
156 return snprintf(dest, max_len, "unix:%s%s",
157 ((struct sockaddr_un *)(ss))->sun_path,
158 is_listen ? ",server" : "");
159#endif
160 case AF_INET6:
161 left = "[";
162 right = "]";
163 /* fall through */
164 case AF_INET:
Corey Minyard01ca5192014-10-02 11:17:38 -0500165 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
166 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
167 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
168 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
169 return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500170 is_telnet ? "telnet" : "tcp",
Corey Minyard01ca5192014-10-02 11:17:38 -0500171 left, shost, right, sserv,
172 is_listen ? ",server" : "",
173 left, phost, right, pserv);
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500174
175 default:
176 return snprintf(dest, max_len, "unknown");
177 }
178}
179
Corey Minyardcfb429c2014-10-02 11:17:35 -0500180/***********************************************************/
aliguori6f97dba2008-10-31 18:49:55 +0000181/* character device */
182
Blue Swirl72cf2d42009-09-12 07:36:22 +0000183static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
184 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000185
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200186CharDriverState *qemu_chr_alloc(void)
187{
188 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +0200189 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200190 return chr;
191}
192
Hans de Goedea425d232011-11-19 10:22:43 +0100193void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000194{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200195 /* Keep track if the char device is open */
196 switch (event) {
197 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100198 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200199 break;
200 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100201 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200202 break;
203 }
204
aliguori6f97dba2008-10-31 18:49:55 +0000205 if (!s->chr_event)
206 return;
207 s->chr_event(s->handler_opaque, event);
208}
209
Hans de Goedefee204f2013-03-26 11:07:54 +0100210void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000211{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500212 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000213}
214
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500215int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000216{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200217 int ret;
218
219 qemu_mutex_lock(&s->chr_write_lock);
220 ret = s->chr_write(s, buf, len);
221 qemu_mutex_unlock(&s->chr_write_lock);
222 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000223}
224
Anthony Liguoricd187202013-03-26 10:04:17 -0500225int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
226{
227 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200228 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500229
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200230 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500231 while (offset < len) {
232 do {
233 res = s->chr_write(s, buf + offset, len - offset);
234 if (res == -1 && errno == EAGAIN) {
235 g_usleep(100);
236 }
237 } while (res == -1 && errno == EAGAIN);
238
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200239 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500240 break;
241 }
242
Anthony Liguoricd187202013-03-26 10:04:17 -0500243 offset += res;
244 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200245 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500246
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200247 if (res < 0) {
248 return res;
249 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500250 return offset;
251}
252
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300253int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
254{
255 int offset = 0, counter = 10;
256 int res;
257
258 if (!s->chr_sync_read) {
259 return 0;
260 }
261
262 while (offset < len) {
263 do {
264 res = s->chr_sync_read(s, buf + offset, len - offset);
265 if (res == -1 && errno == EAGAIN) {
266 g_usleep(100);
267 }
268 } while (res == -1 && errno == EAGAIN);
269
270 if (res == 0) {
271 break;
272 }
273
274 if (res < 0) {
275 return res;
276 }
277
278 offset += res;
279
280 if (!counter--) {
281 break;
282 }
283 }
284
285 return offset;
286}
287
Anthony Liguori41084f12011-08-15 11:17:34 -0500288int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000289{
290 if (!s->chr_ioctl)
291 return -ENOTSUP;
292 return s->chr_ioctl(s, cmd, arg);
293}
294
Anthony Liguori909cda12011-08-15 11:17:31 -0500295int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000296{
297 if (!s->chr_can_read)
298 return 0;
299 return s->chr_can_read(s->handler_opaque);
300}
301
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500302void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000303{
Stefan Weilac310732012-04-19 22:27:14 +0200304 if (s->chr_read) {
305 s->chr_read(s->handler_opaque, buf, len);
306 }
aliguori6f97dba2008-10-31 18:49:55 +0000307}
308
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500309int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100310{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300311 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800312 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300313}
314
315int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
316{
317 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100318}
319
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300320int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
321{
322 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
323}
324
Daniel P. Berrange13661082011-06-23 13:31:42 +0100325int qemu_chr_add_client(CharDriverState *s, int fd)
326{
327 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
328}
329
aliguori6f97dba2008-10-31 18:49:55 +0000330void qemu_chr_accept_input(CharDriverState *s)
331{
332 if (s->chr_accept_input)
333 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100334 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000335}
336
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500337void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000338{
Amit Shah9bd78542009-11-03 19:59:54 +0530339 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000340 va_list ap;
341 va_start(ap, fmt);
342 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500343 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000344 va_end(ap);
345}
346
Amit Shah386a5a12013-08-28 15:24:05 +0530347static void remove_fd_in_watch(CharDriverState *chr);
348
aliguori6f97dba2008-10-31 18:49:55 +0000349void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100350 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000351 IOReadHandler *fd_read,
352 IOEventHandler *fd_event,
353 void *opaque)
354{
Hans de Goede19083222013-03-26 11:07:56 +0100355 int fe_open;
356
Amit Shahda7d9982011-04-25 15:18:22 +0530357 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100358 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530359 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100360 } else {
361 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530362 }
aliguori6f97dba2008-10-31 18:49:55 +0000363 s->chr_can_read = fd_can_read;
364 s->chr_read = fd_read;
365 s->chr_event = fd_event;
366 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200367 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000368 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200369
Hans de Goede19083222013-03-26 11:07:56 +0100370 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100371 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100372 }
373
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200374 /* We're connecting to an already opened device, so let's make sure we
375 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100376 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100377 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200378 }
aliguori6f97dba2008-10-31 18:49:55 +0000379}
380
381static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
382{
383 return len;
384}
385
Paolo Bonzini0d649922015-09-29 15:24:29 +0200386static CharDriverState *qemu_chr_open_null(const char *id,
387 ChardevBackend *backend,
388 ChardevReturn *ret,
389 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000390{
391 CharDriverState *chr;
392
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200393 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000394 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500395 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100396 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000397}
398
399/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000400#define MAX_MUX 4
401#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
402#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
403typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100404 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000405 IOReadHandler *chr_read[MAX_MUX];
406 IOEventHandler *chr_event[MAX_MUX];
407 void *ext_opaque[MAX_MUX];
408 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200409 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000410 int mux_cnt;
411 int term_got_escape;
412 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000413 /* Intermediate input buffer allows to catch escape sequences even if the
414 currently active device is not accepting any input - but only until it
415 is full as well. */
416 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
417 int prod[MAX_MUX];
418 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200419 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200420
421 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200422 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200423 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000424} MuxDriver;
425
426
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200427/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000428static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
429{
430 MuxDriver *d = chr->opaque;
431 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200432 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200433 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000434 } else {
435 int i;
436
437 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200438 for (i = 0; i < len; i++) {
439 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000440 char buf1[64];
441 int64_t ti;
442 int secs;
443
Alex Blighbc72ad62013-08-21 16:03:08 +0100444 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200445 if (d->timestamps_start == -1)
446 d->timestamps_start = ti;
447 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000448 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000449 snprintf(buf1, sizeof(buf1),
450 "[%02d:%02d:%02d.%03d] ",
451 secs / 3600,
452 (secs / 60) % 60,
453 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000454 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200455 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200456 d->linestart = 0;
457 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200458 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200459 if (buf[i] == '\n') {
460 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000461 }
462 }
463 }
464 return ret;
465}
466
467static const char * const mux_help[] = {
468 "% h print this help\n\r",
469 "% x exit emulator\n\r",
470 "% s save disk data back to file (if -snapshot)\n\r",
Gonglei86d10322014-11-15 18:06:46 +0800471 "% t toggle console timestamps\n\r",
aliguori6f97dba2008-10-31 18:49:55 +0000472 "% b send break (magic sysrq)\n\r",
473 "% c switch between console and monitor\n\r",
474 "% % sends %\n\r",
475 NULL
476};
477
478int term_escape_char = 0x01; /* ctrl-a is used for escape */
479static void mux_print_help(CharDriverState *chr)
480{
481 int i, j;
482 char ebuf[15] = "Escape-Char";
483 char cbuf[50] = "\n\r";
484
485 if (term_escape_char > 0 && term_escape_char < 26) {
486 snprintf(cbuf, sizeof(cbuf), "\n\r");
487 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
488 } else {
489 snprintf(cbuf, sizeof(cbuf),
490 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
491 term_escape_char);
492 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200493 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000494 for (i = 0; mux_help[i] != NULL; i++) {
495 for (j=0; mux_help[i][j] != '\0'; j++) {
496 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200497 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000498 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200499 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000500 }
501 }
502}
503
aliguori2724b182009-03-05 23:01:47 +0000504static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
505{
506 if (d->chr_event[mux_nr])
507 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
508}
509
aliguori6f97dba2008-10-31 18:49:55 +0000510static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
511{
512 if (d->term_got_escape) {
513 d->term_got_escape = 0;
514 if (ch == term_escape_char)
515 goto send_char;
516 switch(ch) {
517 case '?':
518 case 'h':
519 mux_print_help(chr);
520 break;
521 case 'x':
522 {
523 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200524 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000525 exit(0);
526 break;
527 }
528 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200529 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000530 break;
531 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100532 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000533 break;
534 case 'c':
535 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200536 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
537 d->focus++;
538 if (d->focus >= d->mux_cnt)
539 d->focus = 0;
540 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000541 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200542 case 't':
543 d->timestamps = !d->timestamps;
544 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200545 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200546 break;
aliguori6f97dba2008-10-31 18:49:55 +0000547 }
548 } else if (ch == term_escape_char) {
549 d->term_got_escape = 1;
550 } else {
551 send_char:
552 return 1;
553 }
554 return 0;
555}
556
557static void mux_chr_accept_input(CharDriverState *chr)
558{
aliguori6f97dba2008-10-31 18:49:55 +0000559 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200560 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000561
aliguoria80bf992009-03-05 23:00:02 +0000562 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000563 d->chr_can_read[m] &&
564 d->chr_can_read[m](d->ext_opaque[m])) {
565 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000566 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000567 }
568}
569
570static int mux_chr_can_read(void *opaque)
571{
572 CharDriverState *chr = opaque;
573 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200574 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000575
aliguoria80bf992009-03-05 23:00:02 +0000576 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000577 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000578 if (d->chr_can_read[m])
579 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000580 return 0;
581}
582
583static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
584{
585 CharDriverState *chr = opaque;
586 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200587 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000588 int i;
589
590 mux_chr_accept_input (opaque);
591
592 for(i = 0; i < size; i++)
593 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000594 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000595 d->chr_can_read[m] &&
596 d->chr_can_read[m](d->ext_opaque[m]))
597 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
598 else
aliguoria80bf992009-03-05 23:00:02 +0000599 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000600 }
601}
602
603static void mux_chr_event(void *opaque, int event)
604{
605 CharDriverState *chr = opaque;
606 MuxDriver *d = chr->opaque;
607 int i;
608
609 /* Send the event to all registered listeners */
610 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000611 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000612}
613
614static void mux_chr_update_read_handler(CharDriverState *chr)
615{
616 MuxDriver *d = chr->opaque;
617
618 if (d->mux_cnt >= MAX_MUX) {
619 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
620 return;
621 }
622 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
623 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
624 d->chr_read[d->mux_cnt] = chr->chr_read;
625 d->chr_event[d->mux_cnt] = chr->chr_event;
626 /* Fix up the real driver with mux routines */
627 if (d->mux_cnt == 0) {
628 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
629 mux_chr_event, chr);
630 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200631 if (d->focus != -1) {
632 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200633 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200634 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000635 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200636 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000637}
638
Michael Roth7b7ab182013-07-30 13:04:22 -0500639static bool muxes_realized;
640
641/**
642 * Called after processing of default and command-line-specified
643 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
644 * to a mux chardev. This is done here to ensure that
645 * output/prompts/banners are only displayed for the FE that has
646 * focus when initial command-line processing/machine init is
647 * completed.
648 *
649 * After this point, any new FE attached to any new or existing
650 * mux will receive CHR_EVENT_OPENED notifications for the BE
651 * immediately.
652 */
653static void muxes_realize_done(Notifier *notifier, void *unused)
654{
655 CharDriverState *chr;
656
657 QTAILQ_FOREACH(chr, &chardevs, next) {
658 if (chr->is_mux) {
659 MuxDriver *d = chr->opaque;
660 int i;
661
662 /* send OPENED to all already-attached FEs */
663 for (i = 0; i < d->mux_cnt; i++) {
664 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
665 }
666 /* mark mux as OPENED so any new FEs will immediately receive
667 * OPENED event
668 */
669 qemu_chr_be_generic_open(chr);
670 }
671 }
672 muxes_realized = true;
673}
674
675static Notifier muxes_realize_notify = {
676 .notify = muxes_realize_done,
677};
678
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400679static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
680{
681 MuxDriver *d = s->opaque;
682 return d->drv->chr_add_watch(d->drv, cond);
683}
684
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200685static CharDriverState *qemu_chr_open_mux(const char *id,
686 ChardevBackend *backend,
687 ChardevReturn *ret, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000688{
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200689 ChardevMux *mux = backend->mux;
690 CharDriverState *chr, *drv;
aliguori6f97dba2008-10-31 18:49:55 +0000691 MuxDriver *d;
692
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200693 drv = qemu_chr_find(mux->chardev);
694 if (drv == NULL) {
695 error_setg(errp, "mux: base chardev %s not found", mux->chardev);
696 return NULL;
697 }
698
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200699 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +0200700 d = g_new0(MuxDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000701
702 chr->opaque = d;
703 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200704 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000705 chr->chr_write = mux_chr_write;
706 chr->chr_update_read_handler = mux_chr_update_read_handler;
707 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100708 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100709 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400710 if (drv->chr_add_watch) {
711 chr->chr_add_watch = mux_chr_add_watch;
712 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500713 /* only default to opened state if we've realized the initial
714 * set of muxes
715 */
716 chr->explicit_be_open = muxes_realized ? 0 : 1;
717 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200718
aliguori6f97dba2008-10-31 18:49:55 +0000719 return chr;
720}
721
722
723#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000724int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000725{
726 int ret, len;
727
728 len = len1;
729 while (len > 0) {
730 ret = send(fd, buf, len, 0);
731 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000732 errno = WSAGetLastError();
733 if (errno != WSAEWOULDBLOCK) {
734 return -1;
735 }
736 } else if (ret == 0) {
737 break;
738 } else {
739 buf += ret;
740 len -= ret;
741 }
742 }
743 return len1 - len;
744}
745
746#else
747
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100748int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000749{
750 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100751 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000752
753 len = len1;
754 while (len > 0) {
755 ret = write(fd, buf, len);
756 if (ret < 0) {
757 if (errno != EINTR && errno != EAGAIN)
758 return -1;
759 } else if (ret == 0) {
760 break;
761 } else {
762 buf += ret;
763 len -= ret;
764 }
765 }
766 return len1 - len;
767}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500768
769int recv_all(int fd, void *_buf, int len1, bool single_read)
770{
771 int ret, len;
772 uint8_t *buf = _buf;
773
774 len = len1;
775 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
776 if (ret < 0) {
777 if (errno != EINTR && errno != EAGAIN) {
778 return -1;
779 }
780 continue;
781 } else {
782 if (single_read) {
783 return ret;
784 }
785 buf += ret;
786 len -= ret;
787 }
788 }
789 return len1 - len;
790}
791
aliguori6f97dba2008-10-31 18:49:55 +0000792#endif /* !_WIN32 */
793
Anthony Liguori96c63842013-03-05 23:21:18 +0530794typedef struct IOWatchPoll
795{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200796 GSource parent;
797
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200798 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530799 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530800
801 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200802 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530803 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530804} IOWatchPoll;
805
Anthony Liguori96c63842013-03-05 23:21:18 +0530806static IOWatchPoll *io_watch_poll_from_source(GSource *source)
807{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200808 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530809}
810
811static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
812{
813 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200814 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200815 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200816 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530817 return FALSE;
818 }
819
Paolo Bonzinid185c092013-04-05 17:59:33 +0200820 if (now_active) {
Nils Carlson4bf1cb02015-07-19 20:39:56 +0000821 iwp->src = g_io_create_watch(iwp->channel,
822 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200823 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200824 g_source_attach(iwp->src, NULL);
825 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200826 g_source_destroy(iwp->src);
827 g_source_unref(iwp->src);
828 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200829 }
830 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530831}
832
833static gboolean io_watch_poll_check(GSource *source)
834{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200835 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530836}
837
838static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
839 gpointer user_data)
840{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200841 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530842}
843
844static void io_watch_poll_finalize(GSource *source)
845{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200846 /* Due to a glib bug, removing the last reference to a source
847 * inside a finalize callback causes recursive locking (and a
848 * deadlock). This is not a problem inside other callbacks,
849 * including dispatch callbacks, so we call io_remove_watch_poll
850 * to remove this source. At this point, iwp->src must
851 * be NULL, or we would leak it.
852 *
853 * This would be solved much more elegantly by child sources,
854 * but we support older glib versions that do not have them.
855 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530856 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200857 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530858}
859
860static GSourceFuncs io_watch_poll_funcs = {
861 .prepare = io_watch_poll_prepare,
862 .check = io_watch_poll_check,
863 .dispatch = io_watch_poll_dispatch,
864 .finalize = io_watch_poll_finalize,
865};
866
867/* Can only be used for read */
868static guint io_add_watch_poll(GIOChannel *channel,
869 IOCanReadHandler *fd_can_read,
870 GIOFunc fd_read,
871 gpointer user_data)
872{
873 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200874 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530875
Paolo Bonzinid185c092013-04-05 17:59:33 +0200876 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530877 iwp->fd_can_read = fd_can_read;
878 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200879 iwp->channel = channel;
880 iwp->fd_read = (GSourceFunc) fd_read;
881 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530882
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200883 tag = g_source_attach(&iwp->parent, NULL);
884 g_source_unref(&iwp->parent);
885 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530886}
887
Paolo Bonzini2b316772013-04-19 17:32:09 +0200888static void io_remove_watch_poll(guint tag)
889{
890 GSource *source;
891 IOWatchPoll *iwp;
892
893 g_return_if_fail (tag > 0);
894
895 source = g_main_context_find_source_by_id(NULL, tag);
896 g_return_if_fail (source != NULL);
897
898 iwp = io_watch_poll_from_source(source);
899 if (iwp->src) {
900 g_source_destroy(iwp->src);
901 g_source_unref(iwp->src);
902 iwp->src = NULL;
903 }
904 g_source_destroy(&iwp->parent);
905}
906
Amit Shah26da70c2013-08-28 15:23:37 +0530907static void remove_fd_in_watch(CharDriverState *chr)
908{
909 if (chr->fd_in_tag) {
910 io_remove_watch_poll(chr->fd_in_tag);
911 chr->fd_in_tag = 0;
912 }
913}
914
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000915#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530916static GIOChannel *io_channel_from_fd(int fd)
917{
918 GIOChannel *chan;
919
920 if (fd == -1) {
921 return NULL;
922 }
923
924 chan = g_io_channel_unix_new(fd);
925
926 g_io_channel_set_encoding(chan, NULL, NULL);
927 g_io_channel_set_buffered(chan, FALSE);
928
929 return chan;
930}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000931#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530932
Anthony Liguori76a96442013-03-05 23:21:21 +0530933static GIOChannel *io_channel_from_socket(int fd)
934{
935 GIOChannel *chan;
936
937 if (fd == -1) {
938 return NULL;
939 }
940
941#ifdef _WIN32
942 chan = g_io_channel_win32_new_socket(fd);
943#else
944 chan = g_io_channel_unix_new(fd);
945#endif
946
947 g_io_channel_set_encoding(chan, NULL, NULL);
948 g_io_channel_set_buffered(chan, FALSE);
949
950 return chan;
951}
952
Anthony Liguori684a0962013-03-29 11:39:50 -0500953static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530954{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200955 size_t offset = 0;
956 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530957
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200958 while (offset < len && status == G_IO_STATUS_NORMAL) {
959 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500960
961 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530962 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500963 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530964 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500965
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200966 if (offset > 0) {
967 return offset;
968 }
969 switch (status) {
970 case G_IO_STATUS_NORMAL:
971 g_assert(len == 0);
972 return 0;
973 case G_IO_STATUS_AGAIN:
974 errno = EAGAIN;
975 return -1;
976 default:
977 break;
978 }
979 errno = EINVAL;
980 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530981}
Anthony Liguori96c63842013-03-05 23:21:18 +0530982
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000983#ifndef _WIN32
984
Anthony Liguoria29753f2013-03-05 23:21:19 +0530985typedef struct FDCharDriver {
986 CharDriverState *chr;
987 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000988 int max_size;
989} FDCharDriver;
990
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200991/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000992static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
993{
994 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530995
Anthony Liguori684a0962013-03-29 11:39:50 -0500996 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530997}
998
999static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
1000{
1001 CharDriverState *chr = opaque;
1002 FDCharDriver *s = chr->opaque;
1003 int len;
1004 uint8_t buf[READ_BUF_LEN];
1005 GIOStatus status;
1006 gsize bytes_read;
1007
1008 len = sizeof(buf);
1009 if (len > s->max_size) {
1010 len = s->max_size;
1011 }
1012 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001013 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301014 }
1015
1016 status = g_io_channel_read_chars(chan, (gchar *)buf,
1017 len, &bytes_read, NULL);
1018 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301019 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301020 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1021 return FALSE;
1022 }
1023 if (status == G_IO_STATUS_NORMAL) {
1024 qemu_chr_be_write(chr, buf, bytes_read);
1025 }
1026
1027 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001028}
1029
1030static int fd_chr_read_poll(void *opaque)
1031{
1032 CharDriverState *chr = opaque;
1033 FDCharDriver *s = chr->opaque;
1034
Anthony Liguori909cda12011-08-15 11:17:31 -05001035 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001036 return s->max_size;
1037}
1038
Anthony Liguori23673ca2013-03-05 23:21:23 +05301039static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1040{
1041 FDCharDriver *s = chr->opaque;
1042 return g_io_create_watch(s->fd_out, cond);
1043}
1044
aliguori6f97dba2008-10-31 18:49:55 +00001045static void fd_chr_update_read_handler(CharDriverState *chr)
1046{
1047 FDCharDriver *s = chr->opaque;
1048
Amit Shah26da70c2013-08-28 15:23:37 +05301049 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301050 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301051 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1052 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001053 }
1054}
1055
1056static void fd_chr_close(struct CharDriverState *chr)
1057{
1058 FDCharDriver *s = chr->opaque;
1059
Amit Shah26da70c2013-08-28 15:23:37 +05301060 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301061 if (s->fd_in) {
1062 g_io_channel_unref(s->fd_in);
1063 }
1064 if (s->fd_out) {
1065 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001066 }
1067
Anthony Liguori7267c092011-08-20 22:09:37 -05001068 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001069 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001070}
1071
1072/* open a character device to a unix fd */
1073static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1074{
1075 CharDriverState *chr;
1076 FDCharDriver *s;
1077
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001078 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02001079 s = g_new0(FDCharDriver, 1);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301080 s->fd_in = io_channel_from_fd(fd_in);
1081 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001082 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301083 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001084 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301085 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001086 chr->chr_write = fd_chr_write;
1087 chr->chr_update_read_handler = fd_chr_update_read_handler;
1088 chr->chr_close = fd_chr_close;
1089
aliguori6f97dba2008-10-31 18:49:55 +00001090 return chr;
1091}
1092
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001093static CharDriverState *qemu_chr_open_pipe(const char *id,
1094 ChardevBackend *backend,
1095 ChardevReturn *ret,
1096 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001097{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001098 ChardevHostdev *opts = backend->pipe;
aliguori6f97dba2008-10-31 18:49:55 +00001099 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001100 char filename_in[CHR_MAX_FILENAME_SIZE];
1101 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001102 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001103
Corey Minyard9f781162014-10-02 11:17:33 -05001104 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1105 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001106 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1107 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001108 if (fd_in < 0 || fd_out < 0) {
1109 if (fd_in >= 0)
1110 close(fd_in);
1111 if (fd_out >= 0)
1112 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001113 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001114 if (fd_in < 0) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001115 error_setg_file_open(errp, errno, filename);
Markus Armbruster1f514702012-02-07 15:09:08 +01001116 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001117 }
aliguori6f97dba2008-10-31 18:49:55 +00001118 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001119 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001120}
1121
aliguori6f97dba2008-10-31 18:49:55 +00001122/* init terminal so that we can grab keys */
1123static struct termios oldtty;
1124static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001125static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001126static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001127static bool stdio_echo_state;
1128
1129static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001130
1131static void term_exit(void)
1132{
1133 tcsetattr (0, TCSANOW, &oldtty);
1134 fcntl(0, F_SETFL, old_fd0_flags);
1135}
1136
Gal Hammere76d4422015-01-07 10:38:35 +02001137static void term_stdio_handler(int sig)
1138{
1139 /* restore echo after resume from suspend. */
1140 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1141}
1142
Paolo Bonzinibb002512010-12-23 13:42:50 +01001143static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001144{
1145 struct termios tty;
1146
Gal Hammere76d4422015-01-07 10:38:35 +02001147 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001148 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001149 if (!echo) {
1150 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001151 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001152 tty.c_oflag |= OPOST;
1153 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1154 tty.c_cflag &= ~(CSIZE|PARENB);
1155 tty.c_cflag |= CS8;
1156 tty.c_cc[VMIN] = 1;
1157 tty.c_cc[VTIME] = 0;
1158 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001159 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001160 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001161
1162 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001163}
1164
1165static void qemu_chr_close_stdio(struct CharDriverState *chr)
1166{
1167 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001168 fd_chr_close(chr);
1169}
1170
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001171static CharDriverState *qemu_chr_open_stdio(const char *id,
1172 ChardevBackend *backend,
1173 ChardevReturn *ret,
1174 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001175{
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001176 ChardevStdio *opts = backend->stdio;
aliguori6f97dba2008-10-31 18:49:55 +00001177 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001178 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001179
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001180 if (is_daemonized()) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001181 error_setg(errp, "cannot use stdio with -daemonize");
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001182 return NULL;
1183 }
Li Liuc88930a2014-09-09 19:19:48 +08001184
1185 if (stdio_in_use) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001186 error_setg(errp, "cannot use stdio by multiple character devices");
1187 return NULL;
Li Liuc88930a2014-09-09 19:19:48 +08001188 }
1189
1190 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301191 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001192 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001193 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301194 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001195
Gal Hammere76d4422015-01-07 10:38:35 +02001196 memset(&act, 0, sizeof(act));
1197 act.sa_handler = term_stdio_handler;
1198 sigaction(SIGCONT, &act, NULL);
1199
aliguori6f97dba2008-10-31 18:49:55 +00001200 chr = qemu_chr_open_fd(0, 1);
1201 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001202 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001203 if (opts->has_signal) {
1204 stdio_allow_signal = opts->signal;
1205 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001206 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001207
Markus Armbruster1f514702012-02-07 15:09:08 +01001208 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001209}
1210
aliguori6f97dba2008-10-31 18:49:55 +00001211#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001212 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1213 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001214
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001215#define HAVE_CHARDEV_SERIAL 1
1216#define HAVE_CHARDEV_PTY 1
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001217
aliguori6f97dba2008-10-31 18:49:55 +00001218typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301219 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001220 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001221
1222 /* Protected by the CharDriverState chr_write_lock. */
1223 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301224 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001225 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001226} PtyCharDriver;
1227
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001228static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001229static void pty_chr_state(CharDriverState *chr, int connected);
1230
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301231static gboolean pty_chr_timer(gpointer opaque)
1232{
1233 struct CharDriverState *chr = opaque;
1234 PtyCharDriver *s = chr->opaque;
1235
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001236 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001237 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001238 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001239 if (!s->connected) {
1240 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001241 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001242 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001243 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301244 return FALSE;
1245}
1246
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001247/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301248static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1249{
1250 PtyCharDriver *s = chr->opaque;
1251
1252 if (s->timer_tag) {
1253 g_source_remove(s->timer_tag);
1254 s->timer_tag = 0;
1255 }
1256
1257 if (ms == 1000) {
1258 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1259 } else {
1260 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1261 }
1262}
1263
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001264/* Called with chr_write_lock held. */
1265static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001266{
1267 PtyCharDriver *s = chr->opaque;
1268 GPollFD pfd;
1269
1270 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1271 pfd.events = G_IO_OUT;
1272 pfd.revents = 0;
1273 g_poll(&pfd, 1, 0);
1274 if (pfd.revents & G_IO_HUP) {
1275 pty_chr_state(chr, 0);
1276 } else {
1277 pty_chr_state(chr, 1);
1278 }
1279}
1280
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001281static void pty_chr_update_read_handler(CharDriverState *chr)
1282{
1283 qemu_mutex_lock(&chr->chr_write_lock);
1284 pty_chr_update_read_handler_locked(chr);
1285 qemu_mutex_unlock(&chr->chr_write_lock);
1286}
1287
1288/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001289static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1290{
1291 PtyCharDriver *s = chr->opaque;
1292
1293 if (!s->connected) {
1294 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001295 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001296 if (!s->connected) {
1297 return 0;
1298 }
aliguori6f97dba2008-10-31 18:49:55 +00001299 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001300 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001301}
1302
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301303static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1304{
1305 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001306 if (!s->connected) {
1307 return NULL;
1308 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301309 return g_io_create_watch(s->fd, cond);
1310}
1311
aliguori6f97dba2008-10-31 18:49:55 +00001312static int pty_chr_read_poll(void *opaque)
1313{
1314 CharDriverState *chr = opaque;
1315 PtyCharDriver *s = chr->opaque;
1316
Anthony Liguori909cda12011-08-15 11:17:31 -05001317 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001318 return s->read_bytes;
1319}
1320
Anthony Liguori093d3a22013-03-05 23:21:20 +05301321static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001322{
1323 CharDriverState *chr = opaque;
1324 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301325 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301326 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301327 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001328
1329 len = sizeof(buf);
1330 if (len > s->read_bytes)
1331 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001332 if (len == 0) {
1333 return TRUE;
1334 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301335 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1336 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001337 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301338 return FALSE;
1339 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001340 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001341 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001342 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301343 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001344}
1345
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001346static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1347{
1348 CharDriverState *chr = opaque;
1349 PtyCharDriver *s = chr->opaque;
1350
1351 s->open_tag = 0;
1352 qemu_chr_be_generic_open(chr);
1353 return FALSE;
1354}
1355
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001356/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001357static void pty_chr_state(CharDriverState *chr, int connected)
1358{
1359 PtyCharDriver *s = chr->opaque;
1360
1361 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001362 if (s->open_tag) {
1363 g_source_remove(s->open_tag);
1364 s->open_tag = 0;
1365 }
Amit Shah26da70c2013-08-28 15:23:37 +05301366 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001367 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001368 /* (re-)connect poll interval for idle guests: once per second.
1369 * We check more frequently in case the guests sends data to
1370 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301371 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001372 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001373 if (s->timer_tag) {
1374 g_source_remove(s->timer_tag);
1375 s->timer_tag = 0;
1376 }
1377 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001378 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001379 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001380 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001381 }
1382 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301383 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1384 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001385 }
aliguori6f97dba2008-10-31 18:49:55 +00001386 }
1387}
1388
aliguori6f97dba2008-10-31 18:49:55 +00001389static void pty_chr_close(struct CharDriverState *chr)
1390{
1391 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301392 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001393
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001394 qemu_mutex_lock(&chr->chr_write_lock);
1395 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301396 fd = g_io_channel_unix_get_fd(s->fd);
1397 g_io_channel_unref(s->fd);
1398 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301399 if (s->timer_tag) {
1400 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001401 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301402 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001403 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001404 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001405 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001406}
1407
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001408static CharDriverState *qemu_chr_open_pty(const char *id,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001409 ChardevBackend *backend,
1410 ChardevReturn *ret,
1411 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001412{
1413 CharDriverState *chr;
1414 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001415 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001416 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001417
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001418 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1419 if (master_fd < 0) {
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001420 error_setg_errno(errp, errno, "Failed to create PTY");
Markus Armbruster1f514702012-02-07 15:09:08 +01001421 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001422 }
1423
aliguori6f97dba2008-10-31 18:49:55 +00001424 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001425 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001426
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001427 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001428
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001429 chr->filename = g_strdup_printf("pty:%s", pty_name);
1430 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001431 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001432
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001433 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001434 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001435
Markus Armbruster2d528d42015-09-14 13:54:03 +02001436 s = g_new0(PtyCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001437 chr->opaque = s;
1438 chr->chr_write = pty_chr_write;
1439 chr->chr_update_read_handler = pty_chr_update_read_handler;
1440 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301441 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001442 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001443
Anthony Liguori093d3a22013-03-05 23:21:20 +05301444 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301445 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001446
Markus Armbruster1f514702012-02-07 15:09:08 +01001447 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001448}
1449
1450static void tty_serial_init(int fd, int speed,
1451 int parity, int data_bits, int stop_bits)
1452{
1453 struct termios tty;
1454 speed_t spd;
1455
1456#if 0
1457 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1458 speed, parity, data_bits, stop_bits);
1459#endif
1460 tcgetattr (fd, &tty);
1461
Stefan Weil45eea132009-10-26 16:10:10 +01001462#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1463 speed = speed * 10 / 11;
1464 do {
1465 check_speed(50);
1466 check_speed(75);
1467 check_speed(110);
1468 check_speed(134);
1469 check_speed(150);
1470 check_speed(200);
1471 check_speed(300);
1472 check_speed(600);
1473 check_speed(1200);
1474 check_speed(1800);
1475 check_speed(2400);
1476 check_speed(4800);
1477 check_speed(9600);
1478 check_speed(19200);
1479 check_speed(38400);
1480 /* Non-Posix values follow. They may be unsupported on some systems. */
1481 check_speed(57600);
1482 check_speed(115200);
1483#ifdef B230400
1484 check_speed(230400);
1485#endif
1486#ifdef B460800
1487 check_speed(460800);
1488#endif
1489#ifdef B500000
1490 check_speed(500000);
1491#endif
1492#ifdef B576000
1493 check_speed(576000);
1494#endif
1495#ifdef B921600
1496 check_speed(921600);
1497#endif
1498#ifdef B1000000
1499 check_speed(1000000);
1500#endif
1501#ifdef B1152000
1502 check_speed(1152000);
1503#endif
1504#ifdef B1500000
1505 check_speed(1500000);
1506#endif
1507#ifdef B2000000
1508 check_speed(2000000);
1509#endif
1510#ifdef B2500000
1511 check_speed(2500000);
1512#endif
1513#ifdef B3000000
1514 check_speed(3000000);
1515#endif
1516#ifdef B3500000
1517 check_speed(3500000);
1518#endif
1519#ifdef B4000000
1520 check_speed(4000000);
1521#endif
aliguori6f97dba2008-10-31 18:49:55 +00001522 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001523 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001524
1525 cfsetispeed(&tty, spd);
1526 cfsetospeed(&tty, spd);
1527
1528 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1529 |INLCR|IGNCR|ICRNL|IXON);
1530 tty.c_oflag |= OPOST;
1531 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1532 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1533 switch(data_bits) {
1534 default:
1535 case 8:
1536 tty.c_cflag |= CS8;
1537 break;
1538 case 7:
1539 tty.c_cflag |= CS7;
1540 break;
1541 case 6:
1542 tty.c_cflag |= CS6;
1543 break;
1544 case 5:
1545 tty.c_cflag |= CS5;
1546 break;
1547 }
1548 switch(parity) {
1549 default:
1550 case 'N':
1551 break;
1552 case 'E':
1553 tty.c_cflag |= PARENB;
1554 break;
1555 case 'O':
1556 tty.c_cflag |= PARENB | PARODD;
1557 break;
1558 }
1559 if (stop_bits == 2)
1560 tty.c_cflag |= CSTOPB;
1561
1562 tcsetattr (fd, TCSANOW, &tty);
1563}
1564
1565static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1566{
1567 FDCharDriver *s = chr->opaque;
1568
1569 switch(cmd) {
1570 case CHR_IOCTL_SERIAL_SET_PARAMS:
1571 {
1572 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301573 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1574 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001575 ssp->data_bits, ssp->stop_bits);
1576 }
1577 break;
1578 case CHR_IOCTL_SERIAL_SET_BREAK:
1579 {
1580 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301581 if (enable) {
1582 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1583 }
aliguori6f97dba2008-10-31 18:49:55 +00001584 }
1585 break;
1586 case CHR_IOCTL_SERIAL_GET_TIOCM:
1587 {
1588 int sarg = 0;
1589 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301590 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001591 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001592 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001593 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001594 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001595 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001596 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001597 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001598 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001599 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001600 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001601 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001602 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001603 *targ |= CHR_TIOCM_RTS;
1604 }
1605 break;
1606 case CHR_IOCTL_SERIAL_SET_TIOCM:
1607 {
1608 int sarg = *(int *)arg;
1609 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301610 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001611 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1612 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1613 if (sarg & CHR_TIOCM_CTS)
1614 targ |= TIOCM_CTS;
1615 if (sarg & CHR_TIOCM_CAR)
1616 targ |= TIOCM_CAR;
1617 if (sarg & CHR_TIOCM_DSR)
1618 targ |= TIOCM_DSR;
1619 if (sarg & CHR_TIOCM_RI)
1620 targ |= TIOCM_RI;
1621 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001622 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001623 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001624 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301625 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001626 }
1627 break;
1628 default:
1629 return -ENOTSUP;
1630 }
1631 return 0;
1632}
1633
David Ahern4266a132010-02-10 18:27:17 -07001634static void qemu_chr_close_tty(CharDriverState *chr)
1635{
1636 FDCharDriver *s = chr->opaque;
1637 int fd = -1;
1638
1639 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301640 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001641 }
1642
1643 fd_chr_close(chr);
1644
1645 if (fd >= 0) {
1646 close(fd);
1647 }
1648}
1649
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001650static CharDriverState *qemu_chr_open_tty_fd(int fd)
1651{
1652 CharDriverState *chr;
1653
1654 tty_serial_init(fd, 115200, 'N', 8, 1);
1655 chr = qemu_chr_open_fd(fd, fd);
1656 chr->chr_ioctl = tty_serial_ioctl;
1657 chr->chr_close = qemu_chr_close_tty;
1658 return chr;
1659}
aliguori6f97dba2008-10-31 18:49:55 +00001660#endif /* __linux__ || __sun__ */
1661
1662#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001663
1664#define HAVE_CHARDEV_PARPORT 1
1665
aliguori6f97dba2008-10-31 18:49:55 +00001666typedef struct {
1667 int fd;
1668 int mode;
1669} ParallelCharDriver;
1670
1671static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1672{
1673 if (s->mode != mode) {
1674 int m = mode;
1675 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1676 return 0;
1677 s->mode = mode;
1678 }
1679 return 1;
1680}
1681
1682static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1683{
1684 ParallelCharDriver *drv = chr->opaque;
1685 int fd = drv->fd;
1686 uint8_t b;
1687
1688 switch(cmd) {
1689 case CHR_IOCTL_PP_READ_DATA:
1690 if (ioctl(fd, PPRDATA, &b) < 0)
1691 return -ENOTSUP;
1692 *(uint8_t *)arg = b;
1693 break;
1694 case CHR_IOCTL_PP_WRITE_DATA:
1695 b = *(uint8_t *)arg;
1696 if (ioctl(fd, PPWDATA, &b) < 0)
1697 return -ENOTSUP;
1698 break;
1699 case CHR_IOCTL_PP_READ_CONTROL:
1700 if (ioctl(fd, PPRCONTROL, &b) < 0)
1701 return -ENOTSUP;
1702 /* Linux gives only the lowest bits, and no way to know data
1703 direction! For better compatibility set the fixed upper
1704 bits. */
1705 *(uint8_t *)arg = b | 0xc0;
1706 break;
1707 case CHR_IOCTL_PP_WRITE_CONTROL:
1708 b = *(uint8_t *)arg;
1709 if (ioctl(fd, PPWCONTROL, &b) < 0)
1710 return -ENOTSUP;
1711 break;
1712 case CHR_IOCTL_PP_READ_STATUS:
1713 if (ioctl(fd, PPRSTATUS, &b) < 0)
1714 return -ENOTSUP;
1715 *(uint8_t *)arg = b;
1716 break;
1717 case CHR_IOCTL_PP_DATA_DIR:
1718 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1719 return -ENOTSUP;
1720 break;
1721 case CHR_IOCTL_PP_EPP_READ_ADDR:
1722 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1723 struct ParallelIOArg *parg = arg;
1724 int n = read(fd, parg->buffer, parg->count);
1725 if (n != parg->count) {
1726 return -EIO;
1727 }
1728 }
1729 break;
1730 case CHR_IOCTL_PP_EPP_READ:
1731 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1732 struct ParallelIOArg *parg = arg;
1733 int n = read(fd, parg->buffer, parg->count);
1734 if (n != parg->count) {
1735 return -EIO;
1736 }
1737 }
1738 break;
1739 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1740 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1741 struct ParallelIOArg *parg = arg;
1742 int n = write(fd, parg->buffer, parg->count);
1743 if (n != parg->count) {
1744 return -EIO;
1745 }
1746 }
1747 break;
1748 case CHR_IOCTL_PP_EPP_WRITE:
1749 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1750 struct ParallelIOArg *parg = arg;
1751 int n = write(fd, parg->buffer, parg->count);
1752 if (n != parg->count) {
1753 return -EIO;
1754 }
1755 }
1756 break;
1757 default:
1758 return -ENOTSUP;
1759 }
1760 return 0;
1761}
1762
1763static void pp_close(CharDriverState *chr)
1764{
1765 ParallelCharDriver *drv = chr->opaque;
1766 int fd = drv->fd;
1767
1768 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1769 ioctl(fd, PPRELEASE);
1770 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001771 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001772 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001773}
1774
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001775static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001776{
1777 CharDriverState *chr;
1778 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001779
1780 if (ioctl(fd, PPCLAIM) < 0) {
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001781 error_setg_errno(errp, errno, "not a parallel port");
aliguori6f97dba2008-10-31 18:49:55 +00001782 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001783 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001784 }
1785
Markus Armbruster2d528d42015-09-14 13:54:03 +02001786 drv = g_new0(ParallelCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001787 drv->fd = fd;
1788 drv->mode = IEEE1284_MODE_COMPAT;
1789
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001790 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001791 chr->chr_write = null_chr_write;
1792 chr->chr_ioctl = pp_ioctl;
1793 chr->chr_close = pp_close;
1794 chr->opaque = drv;
1795
Markus Armbruster1f514702012-02-07 15:09:08 +01001796 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001797}
1798#endif /* __linux__ */
1799
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001800#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001801
1802#define HAVE_CHARDEV_PARPORT 1
1803
blueswir16972f932008-11-22 20:49:12 +00001804static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1805{
Stefan Weile0efb992011-02-23 19:09:16 +01001806 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001807 uint8_t b;
1808
1809 switch(cmd) {
1810 case CHR_IOCTL_PP_READ_DATA:
1811 if (ioctl(fd, PPIGDATA, &b) < 0)
1812 return -ENOTSUP;
1813 *(uint8_t *)arg = b;
1814 break;
1815 case CHR_IOCTL_PP_WRITE_DATA:
1816 b = *(uint8_t *)arg;
1817 if (ioctl(fd, PPISDATA, &b) < 0)
1818 return -ENOTSUP;
1819 break;
1820 case CHR_IOCTL_PP_READ_CONTROL:
1821 if (ioctl(fd, PPIGCTRL, &b) < 0)
1822 return -ENOTSUP;
1823 *(uint8_t *)arg = b;
1824 break;
1825 case CHR_IOCTL_PP_WRITE_CONTROL:
1826 b = *(uint8_t *)arg;
1827 if (ioctl(fd, PPISCTRL, &b) < 0)
1828 return -ENOTSUP;
1829 break;
1830 case CHR_IOCTL_PP_READ_STATUS:
1831 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1832 return -ENOTSUP;
1833 *(uint8_t *)arg = b;
1834 break;
1835 default:
1836 return -ENOTSUP;
1837 }
1838 return 0;
1839}
1840
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001841static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
blueswir16972f932008-11-22 20:49:12 +00001842{
1843 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001844
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001845 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001846 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001847 chr->chr_write = null_chr_write;
1848 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001849 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001850 return chr;
blueswir16972f932008-11-22 20:49:12 +00001851}
1852#endif
1853
aliguori6f97dba2008-10-31 18:49:55 +00001854#else /* _WIN32 */
1855
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001856#define HAVE_CHARDEV_SERIAL 1
1857
aliguori6f97dba2008-10-31 18:49:55 +00001858typedef struct {
1859 int max_size;
1860 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001861 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001862 BOOL fpipe;
1863 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001864
1865 /* Protected by the CharDriverState chr_write_lock. */
1866 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001867} WinCharState;
1868
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001869typedef struct {
1870 HANDLE hStdIn;
1871 HANDLE hInputReadyEvent;
1872 HANDLE hInputDoneEvent;
1873 HANDLE hInputThread;
1874 uint8_t win_stdio_buf;
1875} WinStdioCharState;
1876
aliguori6f97dba2008-10-31 18:49:55 +00001877#define NSENDBUF 2048
1878#define NRECVBUF 2048
1879#define MAXCONNECT 1
1880#define NTIMEOUT 5000
1881
1882static int win_chr_poll(void *opaque);
1883static int win_chr_pipe_poll(void *opaque);
1884
1885static void win_chr_close(CharDriverState *chr)
1886{
1887 WinCharState *s = chr->opaque;
1888
1889 if (s->hsend) {
1890 CloseHandle(s->hsend);
1891 s->hsend = NULL;
1892 }
1893 if (s->hrecv) {
1894 CloseHandle(s->hrecv);
1895 s->hrecv = NULL;
1896 }
1897 if (s->hcom) {
1898 CloseHandle(s->hcom);
1899 s->hcom = NULL;
1900 }
1901 if (s->fpipe)
1902 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1903 else
1904 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301905
Hans de Goedea425d232011-11-19 10:22:43 +01001906 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001907}
1908
Paolo Bonzini6511d392015-09-29 15:08:05 +02001909static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001910{
1911 WinCharState *s = chr->opaque;
1912 COMMCONFIG comcfg;
1913 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1914 COMSTAT comstat;
1915 DWORD size;
1916 DWORD err;
1917
1918 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1919 if (!s->hsend) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001920 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001921 goto fail;
1922 }
1923 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1924 if (!s->hrecv) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001925 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001926 goto fail;
1927 }
1928
1929 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1930 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1931 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001932 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00001933 s->hcom = NULL;
1934 goto fail;
1935 }
1936
1937 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001938 error_setg(errp, "Failed SetupComm");
aliguori6f97dba2008-10-31 18:49:55 +00001939 goto fail;
1940 }
1941
1942 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1943 size = sizeof(COMMCONFIG);
1944 GetDefaultCommConfig(filename, &comcfg, &size);
1945 comcfg.dcb.DCBlength = sizeof(DCB);
1946 CommConfigDialog(filename, NULL, &comcfg);
1947
1948 if (!SetCommState(s->hcom, &comcfg.dcb)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001949 error_setg(errp, "Failed SetCommState");
aliguori6f97dba2008-10-31 18:49:55 +00001950 goto fail;
1951 }
1952
1953 if (!SetCommMask(s->hcom, EV_ERR)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001954 error_setg(errp, "Failed SetCommMask");
aliguori6f97dba2008-10-31 18:49:55 +00001955 goto fail;
1956 }
1957
1958 cto.ReadIntervalTimeout = MAXDWORD;
1959 if (!SetCommTimeouts(s->hcom, &cto)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001960 error_setg(errp, "Failed SetCommTimeouts");
aliguori6f97dba2008-10-31 18:49:55 +00001961 goto fail;
1962 }
1963
1964 if (!ClearCommError(s->hcom, &err, &comstat)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001965 error_setg(errp, "Failed ClearCommError");
aliguori6f97dba2008-10-31 18:49:55 +00001966 goto fail;
1967 }
1968 qemu_add_polling_cb(win_chr_poll, chr);
1969 return 0;
1970
1971 fail:
1972 win_chr_close(chr);
1973 return -1;
1974}
1975
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001976/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001977static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1978{
1979 WinCharState *s = chr->opaque;
1980 DWORD len, ret, size, err;
1981
1982 len = len1;
1983 ZeroMemory(&s->osend, sizeof(s->osend));
1984 s->osend.hEvent = s->hsend;
1985 while (len > 0) {
1986 if (s->hsend)
1987 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1988 else
1989 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1990 if (!ret) {
1991 err = GetLastError();
1992 if (err == ERROR_IO_PENDING) {
1993 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1994 if (ret) {
1995 buf += size;
1996 len -= size;
1997 } else {
1998 break;
1999 }
2000 } else {
2001 break;
2002 }
2003 } else {
2004 buf += size;
2005 len -= size;
2006 }
2007 }
2008 return len1 - len;
2009}
2010
2011static int win_chr_read_poll(CharDriverState *chr)
2012{
2013 WinCharState *s = chr->opaque;
2014
Anthony Liguori909cda12011-08-15 11:17:31 -05002015 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002016 return s->max_size;
2017}
2018
2019static void win_chr_readfile(CharDriverState *chr)
2020{
2021 WinCharState *s = chr->opaque;
2022 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302023 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002024 DWORD size;
2025
2026 ZeroMemory(&s->orecv, sizeof(s->orecv));
2027 s->orecv.hEvent = s->hrecv;
2028 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2029 if (!ret) {
2030 err = GetLastError();
2031 if (err == ERROR_IO_PENDING) {
2032 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2033 }
2034 }
2035
2036 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002037 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002038 }
2039}
2040
2041static void win_chr_read(CharDriverState *chr)
2042{
2043 WinCharState *s = chr->opaque;
2044
2045 if (s->len > s->max_size)
2046 s->len = s->max_size;
2047 if (s->len == 0)
2048 return;
2049
2050 win_chr_readfile(chr);
2051}
2052
2053static int win_chr_poll(void *opaque)
2054{
2055 CharDriverState *chr = opaque;
2056 WinCharState *s = chr->opaque;
2057 COMSTAT status;
2058 DWORD comerr;
2059
2060 ClearCommError(s->hcom, &comerr, &status);
2061 if (status.cbInQue > 0) {
2062 s->len = status.cbInQue;
2063 win_chr_read_poll(chr);
2064 win_chr_read(chr);
2065 return 1;
2066 }
2067 return 0;
2068}
2069
Paolo Bonzini6511d392015-09-29 15:08:05 +02002070static CharDriverState *qemu_chr_open_win_path(const char *filename,
2071 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002072{
2073 CharDriverState *chr;
2074 WinCharState *s;
2075
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002076 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002077 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002078 chr->opaque = s;
2079 chr->chr_write = win_chr_write;
2080 chr->chr_close = win_chr_close;
2081
Paolo Bonzini6511d392015-09-29 15:08:05 +02002082 if (win_chr_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002083 g_free(s);
2084 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002085 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002086 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002087 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002088}
2089
2090static int win_chr_pipe_poll(void *opaque)
2091{
2092 CharDriverState *chr = opaque;
2093 WinCharState *s = chr->opaque;
2094 DWORD size;
2095
2096 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2097 if (size > 0) {
2098 s->len = size;
2099 win_chr_read_poll(chr);
2100 win_chr_read(chr);
2101 return 1;
2102 }
2103 return 0;
2104}
2105
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002106static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
2107 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002108{
2109 WinCharState *s = chr->opaque;
2110 OVERLAPPED ov;
2111 int ret;
2112 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002113 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002114
2115 s->fpipe = TRUE;
2116
2117 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2118 if (!s->hsend) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002119 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002120 goto fail;
2121 }
2122 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2123 if (!s->hrecv) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002124 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002125 goto fail;
2126 }
2127
2128 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2129 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2130 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2131 PIPE_WAIT,
2132 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2133 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002134 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00002135 s->hcom = NULL;
2136 goto fail;
2137 }
2138
2139 ZeroMemory(&ov, sizeof(ov));
2140 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2141 ret = ConnectNamedPipe(s->hcom, &ov);
2142 if (ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002143 error_setg(errp, "Failed ConnectNamedPipe");
aliguori6f97dba2008-10-31 18:49:55 +00002144 goto fail;
2145 }
2146
2147 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2148 if (!ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002149 error_setg(errp, "Failed GetOverlappedResult");
aliguori6f97dba2008-10-31 18:49:55 +00002150 if (ov.hEvent) {
2151 CloseHandle(ov.hEvent);
2152 ov.hEvent = NULL;
2153 }
2154 goto fail;
2155 }
2156
2157 if (ov.hEvent) {
2158 CloseHandle(ov.hEvent);
2159 ov.hEvent = NULL;
2160 }
2161 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2162 return 0;
2163
2164 fail:
2165 win_chr_close(chr);
2166 return -1;
2167}
2168
2169
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002170static CharDriverState *qemu_chr_open_pipe(const char *id,
2171 ChardevBackend *backend,
2172 ChardevReturn *ret,
2173 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002174{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002175 ChardevHostdev *opts = backend->pipe;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002176 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002177 CharDriverState *chr;
2178 WinCharState *s;
2179
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002180 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002181 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002182 chr->opaque = s;
2183 chr->chr_write = win_chr_write;
2184 chr->chr_close = win_chr_close;
2185
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002186 if (win_chr_pipe_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002187 g_free(s);
2188 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002189 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002190 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002191 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002192}
2193
Markus Armbruster1f514702012-02-07 15:09:08 +01002194static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002195{
2196 CharDriverState *chr;
2197 WinCharState *s;
2198
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002199 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002200 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002201 s->hcom = fd_out;
2202 chr->opaque = s;
2203 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002204 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002205}
2206
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002207static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002208{
Markus Armbruster1f514702012-02-07 15:09:08 +01002209 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002210}
2211
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002212static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2213{
2214 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2215 DWORD dwSize;
2216 int len1;
2217
2218 len1 = len;
2219
2220 while (len1 > 0) {
2221 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2222 break;
2223 }
2224 buf += dwSize;
2225 len1 -= dwSize;
2226 }
2227
2228 return len - len1;
2229}
2230
2231static void win_stdio_wait_func(void *opaque)
2232{
2233 CharDriverState *chr = opaque;
2234 WinStdioCharState *stdio = chr->opaque;
2235 INPUT_RECORD buf[4];
2236 int ret;
2237 DWORD dwSize;
2238 int i;
2239
Stefan Weildff74242013-12-07 14:48:04 +01002240 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002241
2242 if (!ret) {
2243 /* Avoid error storm */
2244 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2245 return;
2246 }
2247
2248 for (i = 0; i < dwSize; i++) {
2249 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2250
2251 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2252 int j;
2253 if (kev->uChar.AsciiChar != 0) {
2254 for (j = 0; j < kev->wRepeatCount; j++) {
2255 if (qemu_chr_be_can_write(chr)) {
2256 uint8_t c = kev->uChar.AsciiChar;
2257 qemu_chr_be_write(chr, &c, 1);
2258 }
2259 }
2260 }
2261 }
2262 }
2263}
2264
2265static DWORD WINAPI win_stdio_thread(LPVOID param)
2266{
2267 CharDriverState *chr = param;
2268 WinStdioCharState *stdio = chr->opaque;
2269 int ret;
2270 DWORD dwSize;
2271
2272 while (1) {
2273
2274 /* Wait for one byte */
2275 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2276
2277 /* Exit in case of error, continue if nothing read */
2278 if (!ret) {
2279 break;
2280 }
2281 if (!dwSize) {
2282 continue;
2283 }
2284
2285 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2286 if (stdio->win_stdio_buf == '\r') {
2287 continue;
2288 }
2289
2290 /* Signal the main thread and wait until the byte was eaten */
2291 if (!SetEvent(stdio->hInputReadyEvent)) {
2292 break;
2293 }
2294 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2295 != WAIT_OBJECT_0) {
2296 break;
2297 }
2298 }
2299
2300 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2301 return 0;
2302}
2303
2304static void win_stdio_thread_wait_func(void *opaque)
2305{
2306 CharDriverState *chr = opaque;
2307 WinStdioCharState *stdio = chr->opaque;
2308
2309 if (qemu_chr_be_can_write(chr)) {
2310 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2311 }
2312
2313 SetEvent(stdio->hInputDoneEvent);
2314}
2315
2316static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2317{
2318 WinStdioCharState *stdio = chr->opaque;
2319 DWORD dwMode = 0;
2320
2321 GetConsoleMode(stdio->hStdIn, &dwMode);
2322
2323 if (echo) {
2324 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2325 } else {
2326 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2327 }
2328}
2329
2330static void win_stdio_close(CharDriverState *chr)
2331{
2332 WinStdioCharState *stdio = chr->opaque;
2333
2334 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2335 CloseHandle(stdio->hInputReadyEvent);
2336 }
2337 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2338 CloseHandle(stdio->hInputDoneEvent);
2339 }
2340 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2341 TerminateThread(stdio->hInputThread, 0);
2342 }
2343
2344 g_free(chr->opaque);
2345 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002346}
2347
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002348static CharDriverState *qemu_chr_open_stdio(const char *id,
2349 ChardevBackend *backend,
2350 ChardevReturn *ret,
2351 Error **errp)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002352{
2353 CharDriverState *chr;
2354 WinStdioCharState *stdio;
2355 DWORD dwMode;
2356 int is_console = 0;
2357
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002358 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002359 stdio = g_new0(WinStdioCharState, 1);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002360
2361 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2362 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002363 error_setg(errp, "cannot open stdio: invalid handle");
2364 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002365 }
2366
2367 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2368
2369 chr->opaque = stdio;
2370 chr->chr_write = win_stdio_write;
2371 chr->chr_close = win_stdio_close;
2372
Anthony Liguoried7a1542013-03-05 23:21:17 +05302373 if (is_console) {
2374 if (qemu_add_wait_object(stdio->hStdIn,
2375 win_stdio_wait_func, chr)) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002376 error_setg(errp, "qemu_add_wait_object: failed");
2377 goto err1;
Anthony Liguoried7a1542013-03-05 23:21:17 +05302378 }
2379 } else {
2380 DWORD dwId;
2381
2382 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2383 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002384 if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
Anthony Liguoried7a1542013-03-05 23:21:17 +05302385 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002386 error_setg(errp, "cannot create event");
2387 goto err2;
Anthony Liguoried7a1542013-03-05 23:21:17 +05302388 }
2389 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2390 win_stdio_thread_wait_func, chr)) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002391 error_setg(errp, "qemu_add_wait_object: failed");
2392 goto err2;
2393 }
2394 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2395 chr, 0, &dwId);
2396
2397 if (stdio->hInputThread == INVALID_HANDLE_VALUE) {
2398 error_setg(errp, "cannot create stdio thread");
2399 goto err3;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002400 }
2401 }
2402
2403 dwMode |= ENABLE_LINE_INPUT;
2404
Anthony Liguoried7a1542013-03-05 23:21:17 +05302405 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002406 /* set the terminal in raw mode */
2407 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2408 dwMode |= ENABLE_PROCESSED_INPUT;
2409 }
2410
2411 SetConsoleMode(stdio->hStdIn, dwMode);
2412
2413 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2414 qemu_chr_fe_set_echo(chr, false);
2415
Markus Armbruster1f514702012-02-07 15:09:08 +01002416 return chr;
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002417
2418err3:
2419 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2420err2:
2421 CloseHandle(stdio->hInputReadyEvent);
2422 CloseHandle(stdio->hInputDoneEvent);
2423err1:
2424 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2425 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002426}
aliguori6f97dba2008-10-31 18:49:55 +00002427#endif /* !_WIN32 */
2428
Anthony Liguori5ab82112013-03-05 23:21:31 +05302429
aliguori6f97dba2008-10-31 18:49:55 +00002430/***********************************************************/
2431/* UDP Net console */
2432
2433typedef struct {
2434 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302435 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302436 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002437 int bufcnt;
2438 int bufptr;
2439 int max_size;
2440} NetCharDriver;
2441
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002442/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002443static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2444{
2445 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302446 gsize bytes_written;
2447 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002448
Anthony Liguori76a96442013-03-05 23:21:21 +05302449 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2450 if (status == G_IO_STATUS_EOF) {
2451 return 0;
2452 } else if (status != G_IO_STATUS_NORMAL) {
2453 return -1;
2454 }
2455
2456 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002457}
2458
2459static int udp_chr_read_poll(void *opaque)
2460{
2461 CharDriverState *chr = opaque;
2462 NetCharDriver *s = chr->opaque;
2463
Anthony Liguori909cda12011-08-15 11:17:31 -05002464 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002465
2466 /* If there were any stray characters in the queue process them
2467 * first
2468 */
2469 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002470 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002471 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002472 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002473 }
2474 return s->max_size;
2475}
2476
Anthony Liguori76a96442013-03-05 23:21:21 +05302477static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002478{
2479 CharDriverState *chr = opaque;
2480 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302481 gsize bytes_read = 0;
2482 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002483
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002484 if (s->max_size == 0) {
2485 return TRUE;
2486 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302487 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2488 &bytes_read, NULL);
2489 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002490 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302491 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302492 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302493 return FALSE;
2494 }
aliguori6f97dba2008-10-31 18:49:55 +00002495
2496 s->bufptr = 0;
2497 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002498 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002499 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002500 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002501 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302502
2503 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002504}
2505
2506static void udp_chr_update_read_handler(CharDriverState *chr)
2507{
2508 NetCharDriver *s = chr->opaque;
2509
Amit Shah26da70c2013-08-28 15:23:37 +05302510 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302511 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302512 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2513 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002514 }
2515}
2516
aliguori819f56b2009-03-28 17:58:14 +00002517static void udp_chr_close(CharDriverState *chr)
2518{
2519 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302520
2521 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302522 if (s->chan) {
2523 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002524 closesocket(s->fd);
2525 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002526 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002527 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002528}
2529
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002530static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002531{
2532 CharDriverState *chr = NULL;
2533 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002534
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002535 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002536 s = g_new0(NetCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002537
aliguori6f97dba2008-10-31 18:49:55 +00002538 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302539 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002540 s->bufcnt = 0;
2541 s->bufptr = 0;
2542 chr->opaque = s;
2543 chr->chr_write = udp_chr_write;
2544 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002545 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002546 /* be isn't opened until we get a connection */
2547 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002548 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002549}
aliguori6f97dba2008-10-31 18:49:55 +00002550
aliguori6f97dba2008-10-31 18:49:55 +00002551/***********************************************************/
2552/* TCP Net console */
2553
2554typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302555
2556 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302557 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002558 int fd, listen_fd;
2559 int connected;
2560 int max_size;
2561 int do_telnetopt;
2562 int do_nodelay;
2563 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002564 int *read_msgfds;
2565 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002566 int *write_msgfds;
2567 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002568
2569 SocketAddress *addr;
2570 bool is_listen;
2571 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002572
2573 guint reconnect_timer;
2574 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002575 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002576} TCPCharDriver;
2577
Corey Minyard5dd1f022014-10-02 11:17:37 -05002578static gboolean socket_reconnect_timeout(gpointer opaque);
2579
2580static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2581{
2582 TCPCharDriver *s = chr->opaque;
2583 assert(s->connected == 0);
2584 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2585 socket_reconnect_timeout, chr);
2586}
2587
Corey Minyard5008e5b2014-10-08 07:11:55 -05002588static void check_report_connect_error(CharDriverState *chr,
2589 Error *err)
2590{
2591 TCPCharDriver *s = chr->opaque;
2592
2593 if (!s->connect_err_reported) {
2594 error_report("Unable to connect character device %s: %s",
2595 chr->label, error_get_pretty(err));
2596 s->connect_err_reported = true;
2597 }
2598 qemu_chr_socket_restart_timer(chr);
2599}
2600
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302601static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002602
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002603#ifndef _WIN32
2604static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2605{
2606 TCPCharDriver *s = chr->opaque;
2607 struct msghdr msgh;
2608 struct iovec iov;
2609 int r;
2610
2611 size_t fd_size = s->write_msgfds_num * sizeof(int);
2612 char control[CMSG_SPACE(fd_size)];
2613 struct cmsghdr *cmsg;
2614
2615 memset(&msgh, 0, sizeof(msgh));
2616 memset(control, 0, sizeof(control));
2617
2618 /* set the payload */
2619 iov.iov_base = (uint8_t *) buf;
2620 iov.iov_len = len;
2621
2622 msgh.msg_iov = &iov;
2623 msgh.msg_iovlen = 1;
2624
2625 msgh.msg_control = control;
2626 msgh.msg_controllen = sizeof(control);
2627
2628 cmsg = CMSG_FIRSTHDR(&msgh);
2629
2630 cmsg->cmsg_len = CMSG_LEN(fd_size);
2631 cmsg->cmsg_level = SOL_SOCKET;
2632 cmsg->cmsg_type = SCM_RIGHTS;
2633 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2634
2635 do {
2636 r = sendmsg(s->fd, &msgh, 0);
2637 } while (r < 0 && errno == EINTR);
2638
2639 /* free the written msgfds, no matter what */
2640 if (s->write_msgfds_num) {
2641 g_free(s->write_msgfds);
2642 s->write_msgfds = 0;
2643 s->write_msgfds_num = 0;
2644 }
2645
2646 return r;
2647}
2648#endif
2649
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002650/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002651static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2652{
2653 TCPCharDriver *s = chr->opaque;
2654 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002655#ifndef _WIN32
2656 if (s->is_unix && s->write_msgfds_num) {
2657 return unix_send_msgfds(chr, buf, len);
2658 } else
2659#endif
2660 {
2661 return io_channel_send(s->chan, buf, len);
2662 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002663 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002664 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002665 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002666 }
2667}
2668
2669static int tcp_chr_read_poll(void *opaque)
2670{
2671 CharDriverState *chr = opaque;
2672 TCPCharDriver *s = chr->opaque;
2673 if (!s->connected)
2674 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002675 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002676 return s->max_size;
2677}
2678
2679#define IAC 255
2680#define IAC_BREAK 243
2681static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2682 TCPCharDriver *s,
2683 uint8_t *buf, int *size)
2684{
2685 /* Handle any telnet client's basic IAC options to satisfy char by
2686 * char mode with no echo. All IAC options will be removed from
2687 * the buf and the do_telnetopt variable will be used to track the
2688 * state of the width of the IAC information.
2689 *
2690 * IAC commands come in sets of 3 bytes with the exception of the
2691 * "IAC BREAK" command and the double IAC.
2692 */
2693
2694 int i;
2695 int j = 0;
2696
2697 for (i = 0; i < *size; i++) {
2698 if (s->do_telnetopt > 1) {
2699 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2700 /* Double IAC means send an IAC */
2701 if (j != i)
2702 buf[j] = buf[i];
2703 j++;
2704 s->do_telnetopt = 1;
2705 } else {
2706 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2707 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002708 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002709 s->do_telnetopt++;
2710 }
2711 s->do_telnetopt++;
2712 }
2713 if (s->do_telnetopt >= 4) {
2714 s->do_telnetopt = 1;
2715 }
2716 } else {
2717 if ((unsigned char)buf[i] == IAC) {
2718 s->do_telnetopt = 2;
2719 } else {
2720 if (j != i)
2721 buf[j] = buf[i];
2722 j++;
2723 }
2724 }
2725 }
2726 *size = j;
2727}
2728
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002729static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002730{
2731 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002732 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2733
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002734 assert(num <= TCP_MAX_FDS);
2735
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002736 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002737 int i;
2738
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002739 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2740
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002741 /* Close unused fds */
2742 for (i = to_copy; i < s->read_msgfds_num; i++) {
2743 close(s->read_msgfds[i]);
2744 }
2745
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002746 g_free(s->read_msgfds);
2747 s->read_msgfds = 0;
2748 s->read_msgfds_num = 0;
2749 }
2750
2751 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002752}
2753
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002754static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2755{
2756 TCPCharDriver *s = chr->opaque;
2757
2758 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002759 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002760
2761 if (num) {
Markus Armbruster2d528d42015-09-14 13:54:03 +02002762 s->write_msgfds = g_new(int, num);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002763 memcpy(s->write_msgfds, fds, num * sizeof(int));
2764 }
2765
2766 s->write_msgfds_num = num;
2767
2768 return 0;
2769}
2770
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002771#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002772static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2773{
2774 TCPCharDriver *s = chr->opaque;
2775 struct cmsghdr *cmsg;
2776
2777 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002778 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002779
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002780 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002781 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002782 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002783 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002784 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002785
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002786 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2787
2788 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002789 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002790 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002791
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002792 /* close and clean read_msgfds */
2793 for (i = 0; i < s->read_msgfds_num; i++) {
2794 close(s->read_msgfds[i]);
2795 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002796
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002797 if (s->read_msgfds_num) {
2798 g_free(s->read_msgfds);
2799 }
2800
2801 s->read_msgfds_num = fd_size / sizeof(int);
2802 s->read_msgfds = g_malloc(fd_size);
2803 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2804
2805 for (i = 0; i < s->read_msgfds_num; i++) {
2806 int fd = s->read_msgfds[i];
2807 if (fd < 0) {
2808 continue;
2809 }
2810
2811 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2812 qemu_set_block(fd);
2813
2814 #ifndef MSG_CMSG_CLOEXEC
2815 qemu_set_cloexec(fd);
2816 #endif
2817 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002818 }
2819}
2820
Mark McLoughlin9977c892009-07-22 09:11:38 +01002821static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2822{
2823 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002824 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002825 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002826 union {
2827 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002828 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002829 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002830 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002831 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002832
2833 iov[0].iov_base = buf;
2834 iov[0].iov_len = len;
2835
2836 msg.msg_iov = iov;
2837 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002838 msg.msg_control = &msg_control;
2839 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002840
Corey Bryant06138652012-08-14 16:43:42 -04002841#ifdef MSG_CMSG_CLOEXEC
2842 flags |= MSG_CMSG_CLOEXEC;
2843#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002844 do {
2845 ret = recvmsg(s->fd, &msg, flags);
2846 } while (ret == -1 && errno == EINTR);
2847
Corey Bryant06138652012-08-14 16:43:42 -04002848 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002849 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002850 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002851
2852 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002853}
2854#else
2855static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2856{
2857 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002858 ssize_t ret;
2859
2860 do {
2861 ret = qemu_recv(s->fd, buf, len, 0);
2862 } while (ret == -1 && socket_error() == EINTR);
2863
2864 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002865}
2866#endif
2867
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302868static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2869{
2870 TCPCharDriver *s = chr->opaque;
2871 return g_io_create_watch(s->chan, cond);
2872}
2873
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002874static void tcp_chr_disconnect(CharDriverState *chr)
2875{
2876 TCPCharDriver *s = chr->opaque;
2877
2878 s->connected = 0;
2879 if (s->listen_chan) {
2880 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2881 tcp_chr_accept, chr);
2882 }
2883 remove_fd_in_watch(chr);
2884 g_io_channel_unref(s->chan);
2885 s->chan = NULL;
2886 closesocket(s->fd);
2887 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002888 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2889 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002890 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002891 if (s->reconnect_time) {
2892 qemu_chr_socket_restart_timer(chr);
2893 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002894}
2895
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302896static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002897{
2898 CharDriverState *chr = opaque;
2899 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302900 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002901 int len, size;
2902
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302903 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002904 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302905 }
aliguori6f97dba2008-10-31 18:49:55 +00002906 len = sizeof(buf);
2907 if (len > s->max_size)
2908 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002909 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002910 if (size == 0 ||
2911 (size < 0 &&
2912 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002913 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002914 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002915 } else if (size > 0) {
2916 if (s->do_telnetopt)
2917 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2918 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002919 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002920 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302921
2922 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002923}
2924
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002925static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2926{
2927 TCPCharDriver *s = chr->opaque;
2928 int size;
2929
2930 if (!s->connected) {
2931 return 0;
2932 }
2933
2934 size = tcp_chr_recv(chr, (void *) buf, len);
2935 if (size == 0) {
2936 /* connection closed */
2937 tcp_chr_disconnect(chr);
2938 }
2939
2940 return size;
2941}
2942
Blue Swirl68c18d12010-08-15 09:46:24 +00002943#ifndef _WIN32
2944CharDriverState *qemu_chr_open_eventfd(int eventfd)
2945{
David Marchande9d21c42014-06-11 17:25:16 +02002946 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2947
2948 if (chr) {
2949 chr->avail_connections = 1;
2950 }
2951
2952 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002953}
Blue Swirl68c18d12010-08-15 09:46:24 +00002954#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002955
aliguori6f97dba2008-10-31 18:49:55 +00002956static void tcp_chr_connect(void *opaque)
2957{
2958 CharDriverState *chr = opaque;
2959 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002960 struct sockaddr_storage ss, ps;
2961 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002962
2963 memset(&ss, 0, ss_len);
2964 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2965 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2966 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002967 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2968 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2969 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002970 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002971 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2972 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002973 s->is_listen, s->is_telnet);
2974 }
aliguori6f97dba2008-10-31 18:49:55 +00002975
2976 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302977 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302978 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2979 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002980 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002981 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002982}
2983
Gal Hammerac1b84d2014-02-25 12:12:35 +02002984static void tcp_chr_update_read_handler(CharDriverState *chr)
2985{
2986 TCPCharDriver *s = chr->opaque;
2987
2988 remove_fd_in_watch(chr);
2989 if (s->chan) {
2990 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2991 tcp_chr_read, chr);
2992 }
2993}
2994
aliguori6f97dba2008-10-31 18:49:55 +00002995#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2996static void tcp_chr_telnet_init(int fd)
2997{
2998 char buf[3];
2999 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3000 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3001 send(fd, (char *)buf, 3, 0);
3002 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3003 send(fd, (char *)buf, 3, 0);
3004 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3005 send(fd, (char *)buf, 3, 0);
3006 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3007 send(fd, (char *)buf, 3, 0);
3008}
3009
Daniel P. Berrange13661082011-06-23 13:31:42 +01003010static int tcp_chr_add_client(CharDriverState *chr, int fd)
3011{
3012 TCPCharDriver *s = chr->opaque;
3013 if (s->fd != -1)
3014 return -1;
3015
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003016 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01003017 if (s->do_nodelay)
3018 socket_set_nodelay(fd);
3019 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303020 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003021 if (s->listen_tag) {
3022 g_source_remove(s->listen_tag);
3023 s->listen_tag = 0;
3024 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003025 tcp_chr_connect(chr);
3026
3027 return 0;
3028}
3029
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303030static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00003031{
3032 CharDriverState *chr = opaque;
3033 TCPCharDriver *s = chr->opaque;
3034 struct sockaddr_in saddr;
3035#ifndef _WIN32
3036 struct sockaddr_un uaddr;
3037#endif
3038 struct sockaddr *addr;
3039 socklen_t len;
3040 int fd;
3041
3042 for(;;) {
3043#ifndef _WIN32
3044 if (s->is_unix) {
3045 len = sizeof(uaddr);
3046 addr = (struct sockaddr *)&uaddr;
3047 } else
3048#endif
3049 {
3050 len = sizeof(saddr);
3051 addr = (struct sockaddr *)&saddr;
3052 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003053 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003054 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003055 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303056 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003057 } else if (fd >= 0) {
3058 if (s->do_telnetopt)
3059 tcp_chr_telnet_init(fd);
3060 break;
3061 }
3062 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003063 if (tcp_chr_add_client(chr, fd) < 0)
3064 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303065
3066 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003067}
3068
3069static void tcp_chr_close(CharDriverState *chr)
3070{
3071 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003072 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003073
Corey Minyard5dd1f022014-10-02 11:17:37 -05003074 if (s->reconnect_timer) {
3075 g_source_remove(s->reconnect_timer);
3076 s->reconnect_timer = 0;
3077 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003078 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003079 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303080 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303081 if (s->chan) {
3082 g_io_channel_unref(s->chan);
3083 }
aliguori6f97dba2008-10-31 18:49:55 +00003084 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003085 }
3086 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303087 if (s->listen_tag) {
3088 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003089 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303090 }
3091 if (s->listen_chan) {
3092 g_io_channel_unref(s->listen_chan);
3093 }
aliguori6f97dba2008-10-31 18:49:55 +00003094 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003095 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003096 if (s->read_msgfds_num) {
3097 for (i = 0; i < s->read_msgfds_num; i++) {
3098 close(s->read_msgfds[i]);
3099 }
3100 g_free(s->read_msgfds);
3101 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003102 if (s->write_msgfds_num) {
3103 g_free(s->write_msgfds);
3104 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003105 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003106 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003107}
3108
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003109static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003110{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003111 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003112
Corey Minyardcfb429c2014-10-02 11:17:35 -05003113 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003114 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303115 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003116 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3117 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003118 } else {
3119 s->connected = 1;
3120 s->fd = fd;
3121 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303122 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003123 tcp_chr_connect(chr);
3124 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003125}
3126
Corey Minyard51795022014-10-08 07:11:56 -05003127static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003128{
3129 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003130 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003131
3132 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003133 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003134 return;
3135 }
3136
Corey Minyard5008e5b2014-10-08 07:11:55 -05003137 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003138 qemu_chr_finish_socket_connection(chr, fd);
3139}
3140
Corey Minyardcfb429c2014-10-02 11:17:35 -05003141static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003142{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003143 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003144 int fd;
3145
Corey Minyardcfb429c2014-10-02 11:17:35 -05003146 if (s->is_listen) {
3147 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003148 } else if (s->reconnect_time) {
3149 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3150 return fd >= 0;
3151 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003152 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003153 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003154 if (fd < 0) {
3155 return false;
3156 }
3157
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003158 qemu_chr_finish_socket_connection(chr, fd);
3159 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003160}
3161
Lei Li51767e72013-01-25 00:03:19 +08003162/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003163/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003164
3165typedef struct {
3166 size_t size;
3167 size_t prod;
3168 size_t cons;
3169 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003170} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003171
Markus Armbruster3949e592013-02-06 21:27:24 +01003172static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003173{
Markus Armbruster3949e592013-02-06 21:27:24 +01003174 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003175
Markus Armbruster5c230102013-02-06 21:27:23 +01003176 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003177}
3178
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003179/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003180static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003181{
Markus Armbruster3949e592013-02-06 21:27:24 +01003182 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003183 int i;
3184
3185 if (!buf || (len < 0)) {
3186 return -1;
3187 }
3188
3189 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003190 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3191 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003192 d->cons = d->prod - d->size;
3193 }
3194 }
3195
3196 return 0;
3197}
3198
Markus Armbruster3949e592013-02-06 21:27:24 +01003199static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003200{
Markus Armbruster3949e592013-02-06 21:27:24 +01003201 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003202 int i;
3203
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003204 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003205 for (i = 0; i < len && d->cons != d->prod; i++) {
3206 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003207 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003208 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003209
3210 return i;
3211}
3212
Markus Armbruster3949e592013-02-06 21:27:24 +01003213static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003214{
Markus Armbruster3949e592013-02-06 21:27:24 +01003215 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003216
3217 g_free(d->cbuf);
3218 g_free(d);
3219 chr->opaque = NULL;
3220}
3221
Markus Armbruster4f573782013-07-26 16:44:32 +02003222static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3223 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003224{
3225 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003226 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003227
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003228 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003229 d = g_malloc(sizeof(*d));
3230
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003231 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003232
3233 /* The size must be power of 2 */
3234 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003235 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003236 goto fail;
3237 }
3238
3239 d->prod = 0;
3240 d->cons = 0;
3241 d->cbuf = g_malloc0(d->size);
3242
3243 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003244 chr->chr_write = ringbuf_chr_write;
3245 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003246
3247 return chr;
3248
3249fail:
3250 g_free(d);
3251 g_free(chr);
3252 return NULL;
3253}
3254
Hani Benhabiles8e597772014-05-27 23:39:30 +01003255bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003256{
Markus Armbruster3949e592013-02-06 21:27:24 +01003257 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003258}
3259
Markus Armbruster3949e592013-02-06 21:27:24 +01003260void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003261 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003262 Error **errp)
3263{
3264 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003265 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003266 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003267 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003268
3269 chr = qemu_chr_find(device);
3270 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003271 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003272 return;
3273 }
3274
Markus Armbruster3949e592013-02-06 21:27:24 +01003275 if (!chr_is_ringbuf(chr)) {
3276 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003277 return;
3278 }
3279
Lei Li1f590cf2013-01-25 00:03:20 +08003280 if (has_format && (format == DATA_FORMAT_BASE64)) {
3281 write_data = g_base64_decode(data, &write_count);
3282 } else {
3283 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003284 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003285 }
3286
Markus Armbruster3949e592013-02-06 21:27:24 +01003287 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003288
Markus Armbruster13289fb2013-02-06 21:27:18 +01003289 if (write_data != (uint8_t *)data) {
3290 g_free((void *)write_data);
3291 }
3292
Lei Li1f590cf2013-01-25 00:03:20 +08003293 if (ret < 0) {
3294 error_setg(errp, "Failed to write to device %s", device);
3295 return;
3296 }
3297}
3298
Markus Armbruster3949e592013-02-06 21:27:24 +01003299char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003300 bool has_format, enum DataFormat format,
3301 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003302{
3303 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003304 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003305 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003306 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003307
3308 chr = qemu_chr_find(device);
3309 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003310 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003311 return NULL;
3312 }
3313
Markus Armbruster3949e592013-02-06 21:27:24 +01003314 if (!chr_is_ringbuf(chr)) {
3315 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003316 return NULL;
3317 }
3318
3319 if (size <= 0) {
3320 error_setg(errp, "size must be greater than zero");
3321 return NULL;
3322 }
3323
Markus Armbruster3949e592013-02-06 21:27:24 +01003324 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003325 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003326 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003327
Markus Armbruster3949e592013-02-06 21:27:24 +01003328 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003329
3330 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003331 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003332 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003333 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003334 /*
3335 * FIXME should read only complete, valid UTF-8 characters up
3336 * to @size bytes. Invalid sequences should be replaced by a
3337 * suitable replacement character. Except when (and only
3338 * when) ring buffer lost characters since last read, initial
3339 * continuation characters should be dropped.
3340 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003341 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003342 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003343 }
3344
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003345 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003346}
3347
Gerd Hoffmann33521632009-12-08 13:11:49 +01003348QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003349{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003350 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003351 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003352 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003353 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003354 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003355
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003356 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003357 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003358 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003359 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003360 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003361
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003362 if (strstart(filename, "mon:", &p)) {
3363 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003364 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003365 if (strcmp(filename, "stdio") == 0) {
3366 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3367 * but pass it to the guest. Handle this only for compat syntax,
3368 * for -chardev syntax we have special option for this.
3369 * This is what -nographic did, redirecting+muxing serial+monitor
3370 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003371 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003372 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003373 }
3374
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003375 if (strcmp(filename, "null") == 0 ||
3376 strcmp(filename, "pty") == 0 ||
3377 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003378 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003379 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003380 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003381 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003382 return opts;
3383 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003384 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003385 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003386 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003387 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003388 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003389 qemu_opt_set(opts, "width", width, &error_abort);
3390 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003391 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003392 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003393 qemu_opt_set(opts, "cols", width, &error_abort);
3394 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003395 } else {
3396 goto fail;
3397 }
3398 }
3399 return opts;
3400 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003401 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003402 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003403 return opts;
3404 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003405 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003406 qemu_opt_set(opts, "backend", "serial", &error_abort);
3407 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003408 return opts;
3409 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003410 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003411 qemu_opt_set(opts, "backend", "file", &error_abort);
3412 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003413 return opts;
3414 }
3415 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003416 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3417 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003418 return opts;
3419 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003420 if (strstart(filename, "tcp:", &p) ||
3421 strstart(filename, "telnet:", &p)) {
3422 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3423 host[0] = 0;
3424 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3425 goto fail;
3426 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003427 qemu_opt_set(opts, "backend", "socket", &error_abort);
3428 qemu_opt_set(opts, "host", host, &error_abort);
3429 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003430 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003431 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3432 if (local_err) {
3433 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003434 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003435 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003436 }
3437 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003438 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003439 return opts;
3440 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003441 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003442 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003443 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3444 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003445 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003446 goto fail;
3447 }
3448 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003449 qemu_opt_set(opts, "host", host, &error_abort);
3450 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003451 if (p[pos] == '@') {
3452 p += pos + 1;
3453 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3454 host[0] = 0;
3455 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003456 goto fail;
3457 }
3458 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003459 qemu_opt_set(opts, "localaddr", host, &error_abort);
3460 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003461 }
3462 return opts;
3463 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003464 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003465 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003466 qemu_opts_do_parse(opts, p, "path", &local_err);
3467 if (local_err) {
3468 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003469 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003470 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003471 return opts;
3472 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003473 if (strstart(filename, "/dev/parport", NULL) ||
3474 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003475 qemu_opt_set(opts, "backend", "parport", &error_abort);
3476 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003477 return opts;
3478 }
3479 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003480 qemu_opt_set(opts, "backend", "tty", &error_abort);
3481 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003482 return opts;
3483 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003484
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003485fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003486 qemu_opts_del(opts);
3487 return NULL;
3488}
3489
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003490static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3491 Error **errp)
3492{
3493 const char *path = qemu_opt_get(opts, "path");
3494
3495 if (path == NULL) {
3496 error_setg(errp, "chardev: file: no filename given");
3497 return;
3498 }
3499 backend->file = g_new0(ChardevFile, 1);
3500 backend->file->out = g_strdup(path);
3501}
3502
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003503static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3504 Error **errp)
3505{
3506 backend->stdio = g_new0(ChardevStdio, 1);
3507 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003508 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003509}
3510
Paolo Bonzini6511d392015-09-29 15:08:05 +02003511#ifdef HAVE_CHARDEV_SERIAL
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003512static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3513 Error **errp)
3514{
3515 const char *device = qemu_opt_get(opts, "path");
3516
3517 if (device == NULL) {
3518 error_setg(errp, "chardev: serial/tty: no device path given");
3519 return;
3520 }
3521 backend->serial = g_new0(ChardevHostdev, 1);
3522 backend->serial->device = g_strdup(device);
3523}
Paolo Bonzini6511d392015-09-29 15:08:05 +02003524#endif
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003525
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003526#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003527static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3528 Error **errp)
3529{
3530 const char *device = qemu_opt_get(opts, "path");
3531
3532 if (device == NULL) {
3533 error_setg(errp, "chardev: parallel: no device path given");
3534 return;
3535 }
3536 backend->parallel = g_new0(ChardevHostdev, 1);
3537 backend->parallel->device = g_strdup(device);
3538}
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003539#endif
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003540
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003541static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3542 Error **errp)
3543{
3544 const char *device = qemu_opt_get(opts, "path");
3545
3546 if (device == NULL) {
3547 error_setg(errp, "chardev: pipe: no device path given");
3548 return;
3549 }
3550 backend->pipe = g_new0(ChardevHostdev, 1);
3551 backend->pipe->device = g_strdup(device);
3552}
3553
Markus Armbruster4f573782013-07-26 16:44:32 +02003554static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3555 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003556{
3557 int val;
3558
Markus Armbruster3a1da422013-07-26 16:44:34 +02003559 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003560
Markus Armbruster0f953052013-06-27 16:22:07 +02003561 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003562 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003563 backend->ringbuf->has_size = true;
3564 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003565 }
3566}
3567
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003568static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3569 Error **errp)
3570{
3571 const char *chardev = qemu_opt_get(opts, "chardev");
3572
3573 if (chardev == NULL) {
3574 error_setg(errp, "chardev: mux: no chardev given");
3575 return;
3576 }
3577 backend->mux = g_new0(ChardevMux, 1);
3578 backend->mux->chardev = g_strdup(chardev);
3579}
3580
Peter Maydelldafd3252014-09-02 11:24:13 +01003581static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3582 Error **errp)
3583{
3584 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3585 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3586 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3587 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003588 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003589 const char *path = qemu_opt_get(opts, "path");
3590 const char *host = qemu_opt_get(opts, "host");
3591 const char *port = qemu_opt_get(opts, "port");
3592 SocketAddress *addr;
3593
3594 if (!path) {
3595 if (!host) {
3596 error_setg(errp, "chardev: socket: no host given");
3597 return;
3598 }
3599 if (!port) {
3600 error_setg(errp, "chardev: socket: no port given");
3601 return;
3602 }
3603 }
3604
3605 backend->socket = g_new0(ChardevSocket, 1);
3606
3607 backend->socket->has_nodelay = true;
3608 backend->socket->nodelay = do_nodelay;
3609 backend->socket->has_server = true;
3610 backend->socket->server = is_listen;
3611 backend->socket->has_telnet = true;
3612 backend->socket->telnet = is_telnet;
3613 backend->socket->has_wait = true;
3614 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003615 backend->socket->has_reconnect = true;
3616 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003617
3618 addr = g_new0(SocketAddress, 1);
3619 if (path) {
3620 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3621 addr->q_unix = g_new0(UnixSocketAddress, 1);
3622 addr->q_unix->path = g_strdup(path);
3623 } else {
3624 addr->kind = SOCKET_ADDRESS_KIND_INET;
3625 addr->inet = g_new0(InetSocketAddress, 1);
3626 addr->inet->host = g_strdup(host);
3627 addr->inet->port = g_strdup(port);
3628 addr->inet->has_to = qemu_opt_get(opts, "to");
3629 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3630 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3631 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3632 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3633 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3634 }
3635 backend->socket->addr = addr;
3636}
3637
Peter Maydell90a14bf2014-09-02 11:24:15 +01003638static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3639 Error **errp)
3640{
3641 const char *host = qemu_opt_get(opts, "host");
3642 const char *port = qemu_opt_get(opts, "port");
3643 const char *localaddr = qemu_opt_get(opts, "localaddr");
3644 const char *localport = qemu_opt_get(opts, "localport");
3645 bool has_local = false;
3646 SocketAddress *addr;
3647
3648 if (host == NULL || strlen(host) == 0) {
3649 host = "localhost";
3650 }
3651 if (port == NULL || strlen(port) == 0) {
3652 error_setg(errp, "chardev: udp: remote port not specified");
3653 return;
3654 }
3655 if (localport == NULL || strlen(localport) == 0) {
3656 localport = "0";
3657 } else {
3658 has_local = true;
3659 }
3660 if (localaddr == NULL || strlen(localaddr) == 0) {
3661 localaddr = "";
3662 } else {
3663 has_local = true;
3664 }
3665
3666 backend->udp = g_new0(ChardevUdp, 1);
3667
3668 addr = g_new0(SocketAddress, 1);
3669 addr->kind = SOCKET_ADDRESS_KIND_INET;
3670 addr->inet = g_new0(InetSocketAddress, 1);
3671 addr->inet->host = g_strdup(host);
3672 addr->inet->port = g_strdup(port);
3673 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3674 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3675 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3676 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3677 backend->udp->remote = addr;
3678
3679 if (has_local) {
3680 backend->udp->has_local = true;
3681 addr = g_new0(SocketAddress, 1);
3682 addr->kind = SOCKET_ADDRESS_KIND_INET;
3683 addr->inet = g_new0(InetSocketAddress, 1);
3684 addr->inet->host = g_strdup(localaddr);
3685 addr->inet->port = g_strdup(localport);
3686 backend->udp->local = addr;
3687 }
3688}
3689
Anthony Liguorid654f342013-03-05 23:21:28 +05303690typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003691 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003692 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003693 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003694 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3695 ChardevReturn *ret, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303696} CharDriver;
3697
3698static GSList *backends;
3699
Peter Maydelle4d50d42014-09-02 11:24:17 +01003700void register_char_driver(const char *name, ChardevBackendKind kind,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003701 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp),
3702 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3703 ChardevReturn *ret, Error **errp))
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003704{
3705 CharDriver *s;
3706
3707 s = g_malloc0(sizeof(*s));
3708 s->name = g_strdup(name);
3709 s->kind = kind;
3710 s->parse = parse;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003711 s->create = create;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003712
3713 backends = g_slist_append(backends, s);
3714}
3715
Anthony Liguorif69554b2011-08-15 11:17:37 -05003716CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003717 void (*init)(struct CharDriverState *s),
3718 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003719{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003720 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303721 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003722 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303723 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003724 ChardevReturn *ret = NULL;
3725 ChardevBackend *backend;
3726 const char *id = qemu_opts_id(opts);
3727 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003728
Peter Maydella61ae7f2014-09-02 11:24:16 +01003729 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003730 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003731 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003732 }
3733
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003734 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003735 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003736 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003737 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003738 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303739 for (i = backends; i; i = i->next) {
3740 cd = i->data;
3741
3742 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003743 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303744 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003745 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303746 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003747 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003748 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003749 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003750 }
3751
Peter Maydella61ae7f2014-09-02 11:24:16 +01003752 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003753
3754 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003755 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003756 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003757
3758 chr = NULL;
3759 backend->kind = cd->kind;
3760 if (cd->parse) {
3761 cd->parse(opts, backend, &local_err);
3762 if (local_err) {
3763 error_propagate(errp, local_err);
3764 goto qapi_out;
3765 }
3766 }
3767 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3768 if (!ret) {
3769 goto qapi_out;
3770 }
3771
3772 if (bid) {
3773 qapi_free_ChardevBackend(backend);
3774 qapi_free_ChardevReturn(ret);
3775 backend = g_new0(ChardevBackend, 1);
3776 backend->mux = g_new0(ChardevMux, 1);
3777 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3778 backend->mux->chardev = g_strdup(bid);
3779 ret = qmp_chardev_add(id, backend, errp);
3780 if (!ret) {
3781 chr = qemu_chr_find(bid);
3782 qemu_chr_delete(chr);
3783 chr = NULL;
3784 goto qapi_out;
3785 }
3786 }
3787
3788 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003789 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003790
3791qapi_out:
3792 qapi_free_ChardevBackend(backend);
3793 qapi_free_ChardevReturn(ret);
3794 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003795 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003796
3797err:
3798 qemu_opts_del(opts);
3799 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003800}
3801
Anthony Liguori27143a42011-08-15 11:17:36 -05003802CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003803{
3804 const char *p;
3805 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003806 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003807 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003808
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003809 if (strstart(filename, "chardev:", &p)) {
3810 return qemu_chr_find(p);
3811 }
3812
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003813 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003814 if (!opts)
3815 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003816
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003817 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003818 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003819 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003820 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003821 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003822 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003823 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003824 }
3825 return chr;
3826}
3827
Anthony Liguori15f31512011-08-15 11:17:35 -05003828void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003829{
3830 if (chr->chr_set_echo) {
3831 chr->chr_set_echo(chr, echo);
3832 }
3833}
3834
Hans de Goede8e25daa2013-03-26 11:07:57 +01003835void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003836{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003837 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003838 return;
3839 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003840 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003841 if (chr->chr_set_fe_open) {
3842 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003843 }
3844}
3845
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003846void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3847{
3848 if (chr->chr_fe_event) {
3849 chr->chr_fe_event(chr, event);
3850 }
3851}
3852
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003853int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3854 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303855{
3856 GSource *src;
3857 guint tag;
3858
3859 if (s->chr_add_watch == NULL) {
3860 return -ENOSYS;
3861 }
3862
3863 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003864 if (!src) {
3865 return -EINVAL;
3866 }
3867
Anthony Liguori23673ca2013-03-05 23:21:23 +05303868 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3869 tag = g_source_attach(src, NULL);
3870 g_source_unref(src);
3871
3872 return tag;
3873}
3874
Hans de Goede44c473d2013-03-27 20:29:39 +01003875int qemu_chr_fe_claim(CharDriverState *s)
3876{
3877 if (s->avail_connections < 1) {
3878 return -1;
3879 }
3880 s->avail_connections--;
3881 return 0;
3882}
3883
3884void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3885{
3886 if (qemu_chr_fe_claim(s) != 0) {
3887 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3888 __func__, s->label);
3889 exit(1);
3890 }
3891}
3892
3893void qemu_chr_fe_release(CharDriverState *s)
3894{
3895 s->avail_connections++;
3896}
3897
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003898void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003899{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003900 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003901 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003902 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003903 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003904 g_free(chr->filename);
3905 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003906 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003907 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003908}
3909
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003910ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003911{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003912 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003913 CharDriverState *chr;
3914
Blue Swirl72cf2d42009-09-12 07:36:22 +00003915 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003916 ChardevInfoList *info = g_malloc0(sizeof(*info));
3917 info->value = g_malloc0(sizeof(*info->value));
3918 info->value->label = g_strdup(chr->label);
3919 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003920 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003921
3922 info->next = chr_list;
3923 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003924 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003925
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003926 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003927}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003928
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003929ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3930{
3931 ChardevBackendInfoList *backend_list = NULL;
3932 CharDriver *c = NULL;
3933 GSList *i = NULL;
3934
3935 for (i = backends; i; i = i->next) {
3936 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3937 c = i->data;
3938 info->value = g_malloc0(sizeof(*info->value));
3939 info->value->name = g_strdup(c->name);
3940
3941 info->next = backend_list;
3942 backend_list = info;
3943 }
3944
3945 return backend_list;
3946}
3947
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003948CharDriverState *qemu_chr_find(const char *name)
3949{
3950 CharDriverState *chr;
3951
Blue Swirl72cf2d42009-09-12 07:36:22 +00003952 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003953 if (strcmp(chr->label, name) != 0)
3954 continue;
3955 return chr;
3956 }
3957 return NULL;
3958}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003959
3960/* Get a character (serial) device interface. */
3961CharDriverState *qemu_char_get_next_serial(void)
3962{
3963 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003964 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003965
3966 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003967
3968 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3969 chr = serial_hds[next_serial++];
3970 qemu_chr_fe_claim_no_fail(chr);
3971 return chr;
3972 }
3973 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003974}
3975
Paolo Bonzini4d454572012-11-26 16:03:42 +01003976QemuOptsList qemu_chardev_opts = {
3977 .name = "chardev",
3978 .implied_opt_name = "backend",
3979 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3980 .desc = {
3981 {
3982 .name = "backend",
3983 .type = QEMU_OPT_STRING,
3984 },{
3985 .name = "path",
3986 .type = QEMU_OPT_STRING,
3987 },{
3988 .name = "host",
3989 .type = QEMU_OPT_STRING,
3990 },{
3991 .name = "port",
3992 .type = QEMU_OPT_STRING,
3993 },{
3994 .name = "localaddr",
3995 .type = QEMU_OPT_STRING,
3996 },{
3997 .name = "localport",
3998 .type = QEMU_OPT_STRING,
3999 },{
4000 .name = "to",
4001 .type = QEMU_OPT_NUMBER,
4002 },{
4003 .name = "ipv4",
4004 .type = QEMU_OPT_BOOL,
4005 },{
4006 .name = "ipv6",
4007 .type = QEMU_OPT_BOOL,
4008 },{
4009 .name = "wait",
4010 .type = QEMU_OPT_BOOL,
4011 },{
4012 .name = "server",
4013 .type = QEMU_OPT_BOOL,
4014 },{
4015 .name = "delay",
4016 .type = QEMU_OPT_BOOL,
4017 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05004018 .name = "reconnect",
4019 .type = QEMU_OPT_NUMBER,
4020 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004021 .name = "telnet",
4022 .type = QEMU_OPT_BOOL,
4023 },{
4024 .name = "width",
4025 .type = QEMU_OPT_NUMBER,
4026 },{
4027 .name = "height",
4028 .type = QEMU_OPT_NUMBER,
4029 },{
4030 .name = "cols",
4031 .type = QEMU_OPT_NUMBER,
4032 },{
4033 .name = "rows",
4034 .type = QEMU_OPT_NUMBER,
4035 },{
4036 .name = "mux",
4037 .type = QEMU_OPT_BOOL,
4038 },{
4039 .name = "signal",
4040 .type = QEMU_OPT_BOOL,
4041 },{
4042 .name = "name",
4043 .type = QEMU_OPT_STRING,
4044 },{
4045 .name = "debug",
4046 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08004047 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01004048 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01004049 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004050 },{
4051 .name = "chardev",
4052 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004053 },
4054 { /* end of list */ }
4055 },
4056};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004057
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004058#ifdef _WIN32
4059
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004060static CharDriverState *qmp_chardev_open_file(const char *id,
4061 ChardevBackend *backend,
4062 ChardevReturn *ret,
4063 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004064{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004065 ChardevFile *file = backend->file;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004066 HANDLE out;
4067
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004068 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004069 error_setg(errp, "input file not supported");
4070 return NULL;
4071 }
4072
4073 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4074 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4075 if (out == INVALID_HANDLE_VALUE) {
4076 error_setg(errp, "open %s failed", file->out);
4077 return NULL;
4078 }
4079 return qemu_chr_open_win_file(out);
4080}
4081
Paolo Bonzini6511d392015-09-29 15:08:05 +02004082static CharDriverState *qmp_chardev_open_serial(const char *id,
4083 ChardevBackend *backend,
4084 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004085 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004086{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004087 ChardevHostdev *serial = backend->serial;
4088 return qemu_chr_open_win_path(serial->device, errp);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004089}
4090
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004091#else /* WIN32 */
4092
4093static int qmp_chardev_open_file_source(char *src, int flags,
4094 Error **errp)
4095{
4096 int fd = -1;
4097
4098 TFR(fd = qemu_open(src, flags, 0666));
4099 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004100 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004101 }
4102 return fd;
4103}
4104
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004105static CharDriverState *qmp_chardev_open_file(const char *id,
4106 ChardevBackend *backend,
4107 ChardevReturn *ret,
4108 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004109{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004110 ChardevFile *file = backend->file;
Markus Armbruster5f758362014-05-19 18:57:34 +02004111 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004112
4113 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4114 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004115 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004116 return NULL;
4117 }
4118
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004119 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004120 flags = O_RDONLY;
4121 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004122 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004123 qemu_close(out);
4124 return NULL;
4125 }
4126 }
4127
4128 return qemu_chr_open_fd(in, out);
4129}
4130
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004131#ifdef HAVE_CHARDEV_SERIAL
Paolo Bonzini6511d392015-09-29 15:08:05 +02004132static CharDriverState *qmp_chardev_open_serial(const char *id,
4133 ChardevBackend *backend,
4134 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004135 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004136{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004137 ChardevHostdev *serial = backend->serial;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004138 int fd;
4139
4140 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004141 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004142 return NULL;
4143 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004144 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004145 return qemu_chr_open_tty_fd(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004146}
Paolo Bonzini6511d392015-09-29 15:08:05 +02004147#endif
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004148
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004149#ifdef HAVE_CHARDEV_PARPORT
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004150static CharDriverState *qmp_chardev_open_parallel(const char *id,
4151 ChardevBackend *backend,
4152 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004153 Error **errp)
4154{
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004155 ChardevHostdev *parallel = backend->parallel;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004156 int fd;
4157
4158 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004159 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004160 return NULL;
4161 }
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004162 return qemu_chr_open_pp_fd(fd, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004163}
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004164#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004165
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004166#endif /* WIN32 */
4167
Corey Minyard5008e5b2014-10-08 07:11:55 -05004168static void socket_try_connect(CharDriverState *chr)
4169{
4170 Error *err = NULL;
4171
4172 if (!qemu_chr_open_socket_fd(chr, &err)) {
4173 check_report_connect_error(chr, err);
4174 }
4175}
4176
Corey Minyard5dd1f022014-10-02 11:17:37 -05004177static gboolean socket_reconnect_timeout(gpointer opaque)
4178{
4179 CharDriverState *chr = opaque;
4180 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004181
4182 s->reconnect_timer = 0;
4183
4184 if (chr->be_open) {
4185 return false;
4186 }
4187
Corey Minyard5008e5b2014-10-08 07:11:55 -05004188 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004189
4190 return false;
4191}
4192
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004193static CharDriverState *qmp_chardev_open_socket(const char *id,
4194 ChardevBackend *backend,
4195 ChardevReturn *ret,
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004196 Error **errp)
4197{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004198 CharDriverState *chr;
4199 TCPCharDriver *s;
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004200 ChardevSocket *sock = backend->socket;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004201 SocketAddress *addr = sock->addr;
4202 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4203 bool is_listen = sock->has_server ? sock->server : true;
4204 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4205 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004206 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004207
4208 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02004209 s = g_new0(TCPCharDriver, 1);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004210
4211 s->fd = -1;
4212 s->listen_fd = -1;
4213 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004214 s->is_listen = is_listen;
4215 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004216 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004217 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004218
4219 chr->opaque = s;
4220 chr->chr_write = tcp_chr_write;
4221 chr->chr_sync_read = tcp_chr_sync_read;
4222 chr->chr_close = tcp_chr_close;
4223 chr->get_msgfds = tcp_get_msgfds;
4224 chr->set_msgfds = tcp_set_msgfds;
4225 chr->chr_add_client = tcp_chr_add_client;
4226 chr->chr_add_watch = tcp_chr_add_watch;
4227 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4228 /* be isn't opened until we get a connection */
4229 chr->explicit_be_open = true;
4230
4231 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004232 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4233 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004234
4235 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004236 if (is_telnet) {
4237 s->do_telnetopt = 1;
4238 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004239 } else if (reconnect > 0) {
4240 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004241 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004242
Corey Minyard5008e5b2014-10-08 07:11:55 -05004243 if (s->reconnect_time) {
4244 socket_try_connect(chr);
4245 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4246 g_free(s);
4247 g_free(chr->filename);
4248 g_free(chr);
4249 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004250 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004251
4252 if (is_listen && is_waitconnect) {
4253 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4254 chr->filename);
4255 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4256 qemu_set_nonblock(s->listen_fd);
4257 }
4258
4259 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004260}
4261
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004262static CharDriverState *qmp_chardev_open_udp(const char *id,
4263 ChardevBackend *backend,
4264 ChardevReturn *ret,
Lei Li08d0ab32013-05-20 14:51:03 +08004265 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004266{
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004267 ChardevUdp *udp = backend->udp;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004268 int fd;
4269
Lei Li08d0ab32013-05-20 14:51:03 +08004270 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004271 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004272 return NULL;
4273 }
4274 return qemu_chr_open_udp_fd(fd);
4275}
4276
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004277ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4278 Error **errp)
4279{
4280 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004281 CharDriverState *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004282 Error *local_err = NULL;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004283 GSList *i;
4284 CharDriver *cd;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004285
4286 chr = qemu_chr_find(id);
4287 if (chr) {
4288 error_setg(errp, "Chardev '%s' already exists", id);
4289 g_free(ret);
4290 return NULL;
4291 }
4292
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004293 for (i = backends; i; i = i->next) {
4294 cd = i->data;
4295
4296 if (cd->kind == backend->kind && cd->create) {
4297 chr = cd->create(id, backend, ret, &local_err);
4298 if (local_err) {
4299 error_propagate(errp, local_err);
4300 goto out_error;
4301 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004302 break;
4303 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004304 }
4305
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004306 if (chr == NULL) {
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004307 switch (backend->kind) {
4308 case CHARDEV_BACKEND_KIND_FILE:
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004309 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004310 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004311 case CHARDEV_BACKEND_KIND_SERIAL:
Paolo Bonzini6511d392015-09-29 15:08:05 +02004312 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004313 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004314 case CHARDEV_BACKEND_KIND_PARALLEL:
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004315 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004316 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004317 case CHARDEV_BACKEND_KIND_PIPE:
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004318 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004319 break;
4320 case CHARDEV_BACKEND_KIND_SOCKET:
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004321 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004322 break;
4323 case CHARDEV_BACKEND_KIND_UDP:
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004324 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004325 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004326 case CHARDEV_BACKEND_KIND_PTY:
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004327 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004328 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004329 case CHARDEV_BACKEND_KIND_NULL:
Paolo Bonzini0d649922015-09-29 15:24:29 +02004330 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004331 break;
4332 case CHARDEV_BACKEND_KIND_MUX:
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004333 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004334 break;
4335 case CHARDEV_BACKEND_KIND_MSMOUSE:
Paolo Bonzini96d885b2015-09-29 15:29:15 +02004336 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004337 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004338 case CHARDEV_BACKEND_KIND_BRAILLE:
Paolo Bonzinie47666b2015-09-29 15:31:26 +02004339 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004340 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004341 case CHARDEV_BACKEND_KIND_TESTDEV:
Paolo Bonzini04987902015-09-29 15:33:42 +02004342 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004343 break;
4344 case CHARDEV_BACKEND_KIND_STDIO:
Paolo Bonzini8c84b252015-09-29 15:40:28 +02004345 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004346 break;
4347#ifdef _WIN32
4348 case CHARDEV_BACKEND_KIND_CONSOLE:
4349 chr = qemu_chr_open_win_con();
4350 break;
4351#endif
4352#ifdef CONFIG_SPICE
4353 case CHARDEV_BACKEND_KIND_SPICEVMC:
4354 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4355 break;
4356 case CHARDEV_BACKEND_KIND_SPICEPORT:
4357 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4358 break;
4359#endif
4360 case CHARDEV_BACKEND_KIND_VC:
4361 chr = vc_init(backend->vc);
4362 break;
4363 case CHARDEV_BACKEND_KIND_RINGBUF:
4364 case CHARDEV_BACKEND_KIND_MEMORY:
4365 chr = qemu_chr_open_ringbuf(backend->ringbuf, &local_err);
4366 break;
4367 default:
4368 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4369 goto out_error;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004370 }
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004371
4372 /*
4373 * Character backend open hasn't been fully converted to the Error
4374 * API. Some opens fail without setting an error. Set a generic
4375 * error then.
4376 * TODO full conversion to Error API
4377 */
4378 if (chr == NULL) {
4379 if (local_err) {
4380 error_propagate(errp, local_err);
4381 } else {
4382 error_setg(errp, "Failed to create chardev");
4383 }
4384 goto out_error;
4385 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004386 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004387
4388 chr->label = g_strdup(id);
4389 chr->avail_connections =
4390 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
4391 if (!chr->filename) {
4392 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4393 }
4394 if (!chr->explicit_be_open) {
4395 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4396 }
4397 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4398 return ret;
4399
4400out_error:
4401 g_free(ret);
4402 return NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004403}
4404
4405void qmp_chardev_remove(const char *id, Error **errp)
4406{
4407 CharDriverState *chr;
4408
4409 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004410 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004411 error_setg(errp, "Chardev '%s' not found", id);
4412 return;
4413 }
4414 if (chr->chr_can_read || chr->chr_read ||
4415 chr->chr_event || chr->handler_opaque) {
4416 error_setg(errp, "Chardev '%s' is busy", id);
4417 return;
4418 }
4419 qemu_chr_delete(chr);
4420}
Anthony Liguorid654f342013-03-05 23:21:28 +05304421
4422static void register_types(void)
4423{
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004424 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
Paolo Bonzini0d649922015-09-29 15:24:29 +02004425 qemu_chr_open_null);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004426 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004427 qemu_chr_parse_socket, qmp_chardev_open_socket);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004428 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp,
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004429 qmp_chardev_open_udp);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004430 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004431 qemu_chr_parse_ringbuf, NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004432 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004433 qemu_chr_parse_file_out, qmp_chardev_open_file);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004434 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
Paolo Bonzini8c84b252015-09-29 15:40:28 +02004435 qemu_chr_parse_stdio, qemu_chr_open_stdio);
Paolo Bonzini6511d392015-09-29 15:08:05 +02004436#if defined HAVE_CHARDEV_SERIAL
Peter Maydelle4d50d42014-09-02 11:24:17 +01004437 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004438 qemu_chr_parse_serial, qmp_chardev_open_serial);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004439 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004440 qemu_chr_parse_serial, qmp_chardev_open_serial);
4441#endif
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004442#ifdef HAVE_CHARDEV_PARPORT
Peter Maydelle4d50d42014-09-02 11:24:17 +01004443 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004444 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004445 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004446 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
4447#endif
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004448#ifdef HAVE_CHARDEV_PTY
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004449 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004450 qemu_chr_open_pty);
4451#endif
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004452 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL,
4453 NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004454 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004455 qemu_chr_parse_pipe, qemu_chr_open_pipe);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004456 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux,
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004457 qemu_chr_open_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004458 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004459 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004460 qemu_chr_parse_ringbuf, NULL);
Michael Roth7b7ab182013-07-30 13:04:22 -05004461 /* this must be done after machine init, since we register FEs with muxes
4462 * as part of realize functions like serial_isa_realizefn when -nographic
4463 * is specified
4464 */
4465 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304466}
4467
4468type_init(register_types);