blob: 64ca39761d2fefca66eaa572ed324cab1b16e238 [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
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001171static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001172{
1173 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001174 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001175
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001176 if (is_daemonized()) {
1177 error_report("cannot use stdio with -daemonize");
1178 return NULL;
1179 }
Li Liuc88930a2014-09-09 19:19:48 +08001180
1181 if (stdio_in_use) {
1182 error_report("cannot use stdio by multiple character devices");
1183 exit(1);
1184 }
1185
1186 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301187 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001188 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001189 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301190 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001191
Gal Hammere76d4422015-01-07 10:38:35 +02001192 memset(&act, 0, sizeof(act));
1193 act.sa_handler = term_stdio_handler;
1194 sigaction(SIGCONT, &act, NULL);
1195
aliguori6f97dba2008-10-31 18:49:55 +00001196 chr = qemu_chr_open_fd(0, 1);
1197 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001198 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001199 if (opts->has_signal) {
1200 stdio_allow_signal = opts->signal;
1201 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001202 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001203
Markus Armbruster1f514702012-02-07 15:09:08 +01001204 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001205}
1206
aliguori6f97dba2008-10-31 18:49:55 +00001207#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001208 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1209 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001210
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001211#define HAVE_CHARDEV_SERIAL 1
1212#define HAVE_CHARDEV_PTY 1
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001213
aliguori6f97dba2008-10-31 18:49:55 +00001214typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301215 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001216 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001217
1218 /* Protected by the CharDriverState chr_write_lock. */
1219 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301220 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001221 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001222} PtyCharDriver;
1223
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001224static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001225static void pty_chr_state(CharDriverState *chr, int connected);
1226
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301227static gboolean pty_chr_timer(gpointer opaque)
1228{
1229 struct CharDriverState *chr = opaque;
1230 PtyCharDriver *s = chr->opaque;
1231
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001232 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001233 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001234 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001235 if (!s->connected) {
1236 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001237 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001238 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001239 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301240 return FALSE;
1241}
1242
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001243/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301244static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1245{
1246 PtyCharDriver *s = chr->opaque;
1247
1248 if (s->timer_tag) {
1249 g_source_remove(s->timer_tag);
1250 s->timer_tag = 0;
1251 }
1252
1253 if (ms == 1000) {
1254 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1255 } else {
1256 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1257 }
1258}
1259
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001260/* Called with chr_write_lock held. */
1261static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001262{
1263 PtyCharDriver *s = chr->opaque;
1264 GPollFD pfd;
1265
1266 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1267 pfd.events = G_IO_OUT;
1268 pfd.revents = 0;
1269 g_poll(&pfd, 1, 0);
1270 if (pfd.revents & G_IO_HUP) {
1271 pty_chr_state(chr, 0);
1272 } else {
1273 pty_chr_state(chr, 1);
1274 }
1275}
1276
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001277static void pty_chr_update_read_handler(CharDriverState *chr)
1278{
1279 qemu_mutex_lock(&chr->chr_write_lock);
1280 pty_chr_update_read_handler_locked(chr);
1281 qemu_mutex_unlock(&chr->chr_write_lock);
1282}
1283
1284/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001285static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1286{
1287 PtyCharDriver *s = chr->opaque;
1288
1289 if (!s->connected) {
1290 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001291 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001292 if (!s->connected) {
1293 return 0;
1294 }
aliguori6f97dba2008-10-31 18:49:55 +00001295 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001296 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001297}
1298
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301299static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1300{
1301 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001302 if (!s->connected) {
1303 return NULL;
1304 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301305 return g_io_create_watch(s->fd, cond);
1306}
1307
aliguori6f97dba2008-10-31 18:49:55 +00001308static int pty_chr_read_poll(void *opaque)
1309{
1310 CharDriverState *chr = opaque;
1311 PtyCharDriver *s = chr->opaque;
1312
Anthony Liguori909cda12011-08-15 11:17:31 -05001313 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001314 return s->read_bytes;
1315}
1316
Anthony Liguori093d3a22013-03-05 23:21:20 +05301317static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001318{
1319 CharDriverState *chr = opaque;
1320 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301321 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301322 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301323 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001324
1325 len = sizeof(buf);
1326 if (len > s->read_bytes)
1327 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001328 if (len == 0) {
1329 return TRUE;
1330 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301331 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1332 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001333 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301334 return FALSE;
1335 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001336 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001337 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001338 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301339 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001340}
1341
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001342static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1343{
1344 CharDriverState *chr = opaque;
1345 PtyCharDriver *s = chr->opaque;
1346
1347 s->open_tag = 0;
1348 qemu_chr_be_generic_open(chr);
1349 return FALSE;
1350}
1351
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001352/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001353static void pty_chr_state(CharDriverState *chr, int connected)
1354{
1355 PtyCharDriver *s = chr->opaque;
1356
1357 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001358 if (s->open_tag) {
1359 g_source_remove(s->open_tag);
1360 s->open_tag = 0;
1361 }
Amit Shah26da70c2013-08-28 15:23:37 +05301362 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001363 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001364 /* (re-)connect poll interval for idle guests: once per second.
1365 * We check more frequently in case the guests sends data to
1366 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301367 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001368 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001369 if (s->timer_tag) {
1370 g_source_remove(s->timer_tag);
1371 s->timer_tag = 0;
1372 }
1373 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001374 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001375 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001376 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001377 }
1378 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301379 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1380 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001381 }
aliguori6f97dba2008-10-31 18:49:55 +00001382 }
1383}
1384
aliguori6f97dba2008-10-31 18:49:55 +00001385static void pty_chr_close(struct CharDriverState *chr)
1386{
1387 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301388 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001389
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001390 qemu_mutex_lock(&chr->chr_write_lock);
1391 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301392 fd = g_io_channel_unix_get_fd(s->fd);
1393 g_io_channel_unref(s->fd);
1394 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301395 if (s->timer_tag) {
1396 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001397 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301398 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001399 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001400 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001401 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001402}
1403
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001404static CharDriverState *qemu_chr_open_pty(const char *id,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001405 ChardevBackend *backend,
1406 ChardevReturn *ret,
1407 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001408{
1409 CharDriverState *chr;
1410 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001411 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001412 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001413
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001414 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1415 if (master_fd < 0) {
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001416 error_setg_errno(errp, errno, "Failed to create PTY");
Markus Armbruster1f514702012-02-07 15:09:08 +01001417 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001418 }
1419
aliguori6f97dba2008-10-31 18:49:55 +00001420 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001421 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001422
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001423 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001424
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001425 chr->filename = g_strdup_printf("pty:%s", pty_name);
1426 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001427 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001428
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001429 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001430 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001431
Markus Armbruster2d528d42015-09-14 13:54:03 +02001432 s = g_new0(PtyCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001433 chr->opaque = s;
1434 chr->chr_write = pty_chr_write;
1435 chr->chr_update_read_handler = pty_chr_update_read_handler;
1436 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301437 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001438 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001439
Anthony Liguori093d3a22013-03-05 23:21:20 +05301440 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301441 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001442
Markus Armbruster1f514702012-02-07 15:09:08 +01001443 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001444}
1445
1446static void tty_serial_init(int fd, int speed,
1447 int parity, int data_bits, int stop_bits)
1448{
1449 struct termios tty;
1450 speed_t spd;
1451
1452#if 0
1453 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1454 speed, parity, data_bits, stop_bits);
1455#endif
1456 tcgetattr (fd, &tty);
1457
Stefan Weil45eea132009-10-26 16:10:10 +01001458#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1459 speed = speed * 10 / 11;
1460 do {
1461 check_speed(50);
1462 check_speed(75);
1463 check_speed(110);
1464 check_speed(134);
1465 check_speed(150);
1466 check_speed(200);
1467 check_speed(300);
1468 check_speed(600);
1469 check_speed(1200);
1470 check_speed(1800);
1471 check_speed(2400);
1472 check_speed(4800);
1473 check_speed(9600);
1474 check_speed(19200);
1475 check_speed(38400);
1476 /* Non-Posix values follow. They may be unsupported on some systems. */
1477 check_speed(57600);
1478 check_speed(115200);
1479#ifdef B230400
1480 check_speed(230400);
1481#endif
1482#ifdef B460800
1483 check_speed(460800);
1484#endif
1485#ifdef B500000
1486 check_speed(500000);
1487#endif
1488#ifdef B576000
1489 check_speed(576000);
1490#endif
1491#ifdef B921600
1492 check_speed(921600);
1493#endif
1494#ifdef B1000000
1495 check_speed(1000000);
1496#endif
1497#ifdef B1152000
1498 check_speed(1152000);
1499#endif
1500#ifdef B1500000
1501 check_speed(1500000);
1502#endif
1503#ifdef B2000000
1504 check_speed(2000000);
1505#endif
1506#ifdef B2500000
1507 check_speed(2500000);
1508#endif
1509#ifdef B3000000
1510 check_speed(3000000);
1511#endif
1512#ifdef B3500000
1513 check_speed(3500000);
1514#endif
1515#ifdef B4000000
1516 check_speed(4000000);
1517#endif
aliguori6f97dba2008-10-31 18:49:55 +00001518 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001519 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001520
1521 cfsetispeed(&tty, spd);
1522 cfsetospeed(&tty, spd);
1523
1524 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1525 |INLCR|IGNCR|ICRNL|IXON);
1526 tty.c_oflag |= OPOST;
1527 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1528 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1529 switch(data_bits) {
1530 default:
1531 case 8:
1532 tty.c_cflag |= CS8;
1533 break;
1534 case 7:
1535 tty.c_cflag |= CS7;
1536 break;
1537 case 6:
1538 tty.c_cflag |= CS6;
1539 break;
1540 case 5:
1541 tty.c_cflag |= CS5;
1542 break;
1543 }
1544 switch(parity) {
1545 default:
1546 case 'N':
1547 break;
1548 case 'E':
1549 tty.c_cflag |= PARENB;
1550 break;
1551 case 'O':
1552 tty.c_cflag |= PARENB | PARODD;
1553 break;
1554 }
1555 if (stop_bits == 2)
1556 tty.c_cflag |= CSTOPB;
1557
1558 tcsetattr (fd, TCSANOW, &tty);
1559}
1560
1561static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1562{
1563 FDCharDriver *s = chr->opaque;
1564
1565 switch(cmd) {
1566 case CHR_IOCTL_SERIAL_SET_PARAMS:
1567 {
1568 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301569 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1570 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001571 ssp->data_bits, ssp->stop_bits);
1572 }
1573 break;
1574 case CHR_IOCTL_SERIAL_SET_BREAK:
1575 {
1576 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301577 if (enable) {
1578 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1579 }
aliguori6f97dba2008-10-31 18:49:55 +00001580 }
1581 break;
1582 case CHR_IOCTL_SERIAL_GET_TIOCM:
1583 {
1584 int sarg = 0;
1585 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301586 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001587 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001588 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001589 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001590 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001591 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001592 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001593 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001594 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001595 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001596 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001597 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001598 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001599 *targ |= CHR_TIOCM_RTS;
1600 }
1601 break;
1602 case CHR_IOCTL_SERIAL_SET_TIOCM:
1603 {
1604 int sarg = *(int *)arg;
1605 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301606 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001607 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1608 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1609 if (sarg & CHR_TIOCM_CTS)
1610 targ |= TIOCM_CTS;
1611 if (sarg & CHR_TIOCM_CAR)
1612 targ |= TIOCM_CAR;
1613 if (sarg & CHR_TIOCM_DSR)
1614 targ |= TIOCM_DSR;
1615 if (sarg & CHR_TIOCM_RI)
1616 targ |= TIOCM_RI;
1617 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001618 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001619 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001620 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301621 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001622 }
1623 break;
1624 default:
1625 return -ENOTSUP;
1626 }
1627 return 0;
1628}
1629
David Ahern4266a132010-02-10 18:27:17 -07001630static void qemu_chr_close_tty(CharDriverState *chr)
1631{
1632 FDCharDriver *s = chr->opaque;
1633 int fd = -1;
1634
1635 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301636 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001637 }
1638
1639 fd_chr_close(chr);
1640
1641 if (fd >= 0) {
1642 close(fd);
1643 }
1644}
1645
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001646static CharDriverState *qemu_chr_open_tty_fd(int fd)
1647{
1648 CharDriverState *chr;
1649
1650 tty_serial_init(fd, 115200, 'N', 8, 1);
1651 chr = qemu_chr_open_fd(fd, fd);
1652 chr->chr_ioctl = tty_serial_ioctl;
1653 chr->chr_close = qemu_chr_close_tty;
1654 return chr;
1655}
aliguori6f97dba2008-10-31 18:49:55 +00001656#endif /* __linux__ || __sun__ */
1657
1658#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001659
1660#define HAVE_CHARDEV_PARPORT 1
1661
aliguori6f97dba2008-10-31 18:49:55 +00001662typedef struct {
1663 int fd;
1664 int mode;
1665} ParallelCharDriver;
1666
1667static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1668{
1669 if (s->mode != mode) {
1670 int m = mode;
1671 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1672 return 0;
1673 s->mode = mode;
1674 }
1675 return 1;
1676}
1677
1678static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1679{
1680 ParallelCharDriver *drv = chr->opaque;
1681 int fd = drv->fd;
1682 uint8_t b;
1683
1684 switch(cmd) {
1685 case CHR_IOCTL_PP_READ_DATA:
1686 if (ioctl(fd, PPRDATA, &b) < 0)
1687 return -ENOTSUP;
1688 *(uint8_t *)arg = b;
1689 break;
1690 case CHR_IOCTL_PP_WRITE_DATA:
1691 b = *(uint8_t *)arg;
1692 if (ioctl(fd, PPWDATA, &b) < 0)
1693 return -ENOTSUP;
1694 break;
1695 case CHR_IOCTL_PP_READ_CONTROL:
1696 if (ioctl(fd, PPRCONTROL, &b) < 0)
1697 return -ENOTSUP;
1698 /* Linux gives only the lowest bits, and no way to know data
1699 direction! For better compatibility set the fixed upper
1700 bits. */
1701 *(uint8_t *)arg = b | 0xc0;
1702 break;
1703 case CHR_IOCTL_PP_WRITE_CONTROL:
1704 b = *(uint8_t *)arg;
1705 if (ioctl(fd, PPWCONTROL, &b) < 0)
1706 return -ENOTSUP;
1707 break;
1708 case CHR_IOCTL_PP_READ_STATUS:
1709 if (ioctl(fd, PPRSTATUS, &b) < 0)
1710 return -ENOTSUP;
1711 *(uint8_t *)arg = b;
1712 break;
1713 case CHR_IOCTL_PP_DATA_DIR:
1714 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1715 return -ENOTSUP;
1716 break;
1717 case CHR_IOCTL_PP_EPP_READ_ADDR:
1718 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1719 struct ParallelIOArg *parg = arg;
1720 int n = read(fd, parg->buffer, parg->count);
1721 if (n != parg->count) {
1722 return -EIO;
1723 }
1724 }
1725 break;
1726 case CHR_IOCTL_PP_EPP_READ:
1727 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1728 struct ParallelIOArg *parg = arg;
1729 int n = read(fd, parg->buffer, parg->count);
1730 if (n != parg->count) {
1731 return -EIO;
1732 }
1733 }
1734 break;
1735 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1736 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1737 struct ParallelIOArg *parg = arg;
1738 int n = write(fd, parg->buffer, parg->count);
1739 if (n != parg->count) {
1740 return -EIO;
1741 }
1742 }
1743 break;
1744 case CHR_IOCTL_PP_EPP_WRITE:
1745 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1746 struct ParallelIOArg *parg = arg;
1747 int n = write(fd, parg->buffer, parg->count);
1748 if (n != parg->count) {
1749 return -EIO;
1750 }
1751 }
1752 break;
1753 default:
1754 return -ENOTSUP;
1755 }
1756 return 0;
1757}
1758
1759static void pp_close(CharDriverState *chr)
1760{
1761 ParallelCharDriver *drv = chr->opaque;
1762 int fd = drv->fd;
1763
1764 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1765 ioctl(fd, PPRELEASE);
1766 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001767 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001768 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001769}
1770
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001771static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001772{
1773 CharDriverState *chr;
1774 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001775
1776 if (ioctl(fd, PPCLAIM) < 0) {
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001777 error_setg_errno(errp, errno, "not a parallel port");
aliguori6f97dba2008-10-31 18:49:55 +00001778 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001779 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001780 }
1781
Markus Armbruster2d528d42015-09-14 13:54:03 +02001782 drv = g_new0(ParallelCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001783 drv->fd = fd;
1784 drv->mode = IEEE1284_MODE_COMPAT;
1785
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001786 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001787 chr->chr_write = null_chr_write;
1788 chr->chr_ioctl = pp_ioctl;
1789 chr->chr_close = pp_close;
1790 chr->opaque = drv;
1791
Markus Armbruster1f514702012-02-07 15:09:08 +01001792 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001793}
1794#endif /* __linux__ */
1795
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001796#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001797
1798#define HAVE_CHARDEV_PARPORT 1
1799
blueswir16972f932008-11-22 20:49:12 +00001800static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1801{
Stefan Weile0efb992011-02-23 19:09:16 +01001802 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001803 uint8_t b;
1804
1805 switch(cmd) {
1806 case CHR_IOCTL_PP_READ_DATA:
1807 if (ioctl(fd, PPIGDATA, &b) < 0)
1808 return -ENOTSUP;
1809 *(uint8_t *)arg = b;
1810 break;
1811 case CHR_IOCTL_PP_WRITE_DATA:
1812 b = *(uint8_t *)arg;
1813 if (ioctl(fd, PPISDATA, &b) < 0)
1814 return -ENOTSUP;
1815 break;
1816 case CHR_IOCTL_PP_READ_CONTROL:
1817 if (ioctl(fd, PPIGCTRL, &b) < 0)
1818 return -ENOTSUP;
1819 *(uint8_t *)arg = b;
1820 break;
1821 case CHR_IOCTL_PP_WRITE_CONTROL:
1822 b = *(uint8_t *)arg;
1823 if (ioctl(fd, PPISCTRL, &b) < 0)
1824 return -ENOTSUP;
1825 break;
1826 case CHR_IOCTL_PP_READ_STATUS:
1827 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1828 return -ENOTSUP;
1829 *(uint8_t *)arg = b;
1830 break;
1831 default:
1832 return -ENOTSUP;
1833 }
1834 return 0;
1835}
1836
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001837static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
blueswir16972f932008-11-22 20:49:12 +00001838{
1839 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001840
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001841 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001842 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001843 chr->chr_write = null_chr_write;
1844 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001845 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001846 return chr;
blueswir16972f932008-11-22 20:49:12 +00001847}
1848#endif
1849
aliguori6f97dba2008-10-31 18:49:55 +00001850#else /* _WIN32 */
1851
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001852#define HAVE_CHARDEV_SERIAL 1
1853
aliguori6f97dba2008-10-31 18:49:55 +00001854typedef struct {
1855 int max_size;
1856 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001857 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001858 BOOL fpipe;
1859 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001860
1861 /* Protected by the CharDriverState chr_write_lock. */
1862 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001863} WinCharState;
1864
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001865typedef struct {
1866 HANDLE hStdIn;
1867 HANDLE hInputReadyEvent;
1868 HANDLE hInputDoneEvent;
1869 HANDLE hInputThread;
1870 uint8_t win_stdio_buf;
1871} WinStdioCharState;
1872
aliguori6f97dba2008-10-31 18:49:55 +00001873#define NSENDBUF 2048
1874#define NRECVBUF 2048
1875#define MAXCONNECT 1
1876#define NTIMEOUT 5000
1877
1878static int win_chr_poll(void *opaque);
1879static int win_chr_pipe_poll(void *opaque);
1880
1881static void win_chr_close(CharDriverState *chr)
1882{
1883 WinCharState *s = chr->opaque;
1884
1885 if (s->hsend) {
1886 CloseHandle(s->hsend);
1887 s->hsend = NULL;
1888 }
1889 if (s->hrecv) {
1890 CloseHandle(s->hrecv);
1891 s->hrecv = NULL;
1892 }
1893 if (s->hcom) {
1894 CloseHandle(s->hcom);
1895 s->hcom = NULL;
1896 }
1897 if (s->fpipe)
1898 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1899 else
1900 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301901
Hans de Goedea425d232011-11-19 10:22:43 +01001902 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001903}
1904
Paolo Bonzini6511d392015-09-29 15:08:05 +02001905static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001906{
1907 WinCharState *s = chr->opaque;
1908 COMMCONFIG comcfg;
1909 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1910 COMSTAT comstat;
1911 DWORD size;
1912 DWORD err;
1913
1914 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1915 if (!s->hsend) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001916 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001917 goto fail;
1918 }
1919 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1920 if (!s->hrecv) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001921 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001922 goto fail;
1923 }
1924
1925 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1926 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1927 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001928 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00001929 s->hcom = NULL;
1930 goto fail;
1931 }
1932
1933 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001934 error_setg(errp, "Failed SetupComm");
aliguori6f97dba2008-10-31 18:49:55 +00001935 goto fail;
1936 }
1937
1938 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1939 size = sizeof(COMMCONFIG);
1940 GetDefaultCommConfig(filename, &comcfg, &size);
1941 comcfg.dcb.DCBlength = sizeof(DCB);
1942 CommConfigDialog(filename, NULL, &comcfg);
1943
1944 if (!SetCommState(s->hcom, &comcfg.dcb)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001945 error_setg(errp, "Failed SetCommState");
aliguori6f97dba2008-10-31 18:49:55 +00001946 goto fail;
1947 }
1948
1949 if (!SetCommMask(s->hcom, EV_ERR)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001950 error_setg(errp, "Failed SetCommMask");
aliguori6f97dba2008-10-31 18:49:55 +00001951 goto fail;
1952 }
1953
1954 cto.ReadIntervalTimeout = MAXDWORD;
1955 if (!SetCommTimeouts(s->hcom, &cto)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001956 error_setg(errp, "Failed SetCommTimeouts");
aliguori6f97dba2008-10-31 18:49:55 +00001957 goto fail;
1958 }
1959
1960 if (!ClearCommError(s->hcom, &err, &comstat)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001961 error_setg(errp, "Failed ClearCommError");
aliguori6f97dba2008-10-31 18:49:55 +00001962 goto fail;
1963 }
1964 qemu_add_polling_cb(win_chr_poll, chr);
1965 return 0;
1966
1967 fail:
1968 win_chr_close(chr);
1969 return -1;
1970}
1971
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001972/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001973static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1974{
1975 WinCharState *s = chr->opaque;
1976 DWORD len, ret, size, err;
1977
1978 len = len1;
1979 ZeroMemory(&s->osend, sizeof(s->osend));
1980 s->osend.hEvent = s->hsend;
1981 while (len > 0) {
1982 if (s->hsend)
1983 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1984 else
1985 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1986 if (!ret) {
1987 err = GetLastError();
1988 if (err == ERROR_IO_PENDING) {
1989 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1990 if (ret) {
1991 buf += size;
1992 len -= size;
1993 } else {
1994 break;
1995 }
1996 } else {
1997 break;
1998 }
1999 } else {
2000 buf += size;
2001 len -= size;
2002 }
2003 }
2004 return len1 - len;
2005}
2006
2007static int win_chr_read_poll(CharDriverState *chr)
2008{
2009 WinCharState *s = chr->opaque;
2010
Anthony Liguori909cda12011-08-15 11:17:31 -05002011 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002012 return s->max_size;
2013}
2014
2015static void win_chr_readfile(CharDriverState *chr)
2016{
2017 WinCharState *s = chr->opaque;
2018 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302019 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002020 DWORD size;
2021
2022 ZeroMemory(&s->orecv, sizeof(s->orecv));
2023 s->orecv.hEvent = s->hrecv;
2024 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2025 if (!ret) {
2026 err = GetLastError();
2027 if (err == ERROR_IO_PENDING) {
2028 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2029 }
2030 }
2031
2032 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002033 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002034 }
2035}
2036
2037static void win_chr_read(CharDriverState *chr)
2038{
2039 WinCharState *s = chr->opaque;
2040
2041 if (s->len > s->max_size)
2042 s->len = s->max_size;
2043 if (s->len == 0)
2044 return;
2045
2046 win_chr_readfile(chr);
2047}
2048
2049static int win_chr_poll(void *opaque)
2050{
2051 CharDriverState *chr = opaque;
2052 WinCharState *s = chr->opaque;
2053 COMSTAT status;
2054 DWORD comerr;
2055
2056 ClearCommError(s->hcom, &comerr, &status);
2057 if (status.cbInQue > 0) {
2058 s->len = status.cbInQue;
2059 win_chr_read_poll(chr);
2060 win_chr_read(chr);
2061 return 1;
2062 }
2063 return 0;
2064}
2065
Paolo Bonzini6511d392015-09-29 15:08:05 +02002066static CharDriverState *qemu_chr_open_win_path(const char *filename,
2067 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002068{
2069 CharDriverState *chr;
2070 WinCharState *s;
2071
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002072 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002073 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002074 chr->opaque = s;
2075 chr->chr_write = win_chr_write;
2076 chr->chr_close = win_chr_close;
2077
Paolo Bonzini6511d392015-09-29 15:08:05 +02002078 if (win_chr_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002079 g_free(s);
2080 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002081 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002082 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002083 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002084}
2085
2086static int win_chr_pipe_poll(void *opaque)
2087{
2088 CharDriverState *chr = opaque;
2089 WinCharState *s = chr->opaque;
2090 DWORD size;
2091
2092 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2093 if (size > 0) {
2094 s->len = size;
2095 win_chr_read_poll(chr);
2096 win_chr_read(chr);
2097 return 1;
2098 }
2099 return 0;
2100}
2101
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002102static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
2103 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002104{
2105 WinCharState *s = chr->opaque;
2106 OVERLAPPED ov;
2107 int ret;
2108 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002109 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002110
2111 s->fpipe = TRUE;
2112
2113 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2114 if (!s->hsend) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002115 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002116 goto fail;
2117 }
2118 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2119 if (!s->hrecv) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002120 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002121 goto fail;
2122 }
2123
2124 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2125 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2126 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2127 PIPE_WAIT,
2128 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2129 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002130 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00002131 s->hcom = NULL;
2132 goto fail;
2133 }
2134
2135 ZeroMemory(&ov, sizeof(ov));
2136 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2137 ret = ConnectNamedPipe(s->hcom, &ov);
2138 if (ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002139 error_setg(errp, "Failed ConnectNamedPipe");
aliguori6f97dba2008-10-31 18:49:55 +00002140 goto fail;
2141 }
2142
2143 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2144 if (!ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002145 error_setg(errp, "Failed GetOverlappedResult");
aliguori6f97dba2008-10-31 18:49:55 +00002146 if (ov.hEvent) {
2147 CloseHandle(ov.hEvent);
2148 ov.hEvent = NULL;
2149 }
2150 goto fail;
2151 }
2152
2153 if (ov.hEvent) {
2154 CloseHandle(ov.hEvent);
2155 ov.hEvent = NULL;
2156 }
2157 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2158 return 0;
2159
2160 fail:
2161 win_chr_close(chr);
2162 return -1;
2163}
2164
2165
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002166static CharDriverState *qemu_chr_open_pipe(const char *id,
2167 ChardevBackend *backend,
2168 ChardevReturn *ret,
2169 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002170{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002171 ChardevHostdev *opts = backend->pipe;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002172 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002173 CharDriverState *chr;
2174 WinCharState *s;
2175
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002176 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002177 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002178 chr->opaque = s;
2179 chr->chr_write = win_chr_write;
2180 chr->chr_close = win_chr_close;
2181
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002182 if (win_chr_pipe_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002183 g_free(s);
2184 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002185 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002186 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002187 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002188}
2189
Markus Armbruster1f514702012-02-07 15:09:08 +01002190static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002191{
2192 CharDriverState *chr;
2193 WinCharState *s;
2194
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002195 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002196 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002197 s->hcom = fd_out;
2198 chr->opaque = s;
2199 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002200 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002201}
2202
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002203static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002204{
Markus Armbruster1f514702012-02-07 15:09:08 +01002205 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002206}
2207
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002208static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2209{
2210 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2211 DWORD dwSize;
2212 int len1;
2213
2214 len1 = len;
2215
2216 while (len1 > 0) {
2217 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2218 break;
2219 }
2220 buf += dwSize;
2221 len1 -= dwSize;
2222 }
2223
2224 return len - len1;
2225}
2226
2227static void win_stdio_wait_func(void *opaque)
2228{
2229 CharDriverState *chr = opaque;
2230 WinStdioCharState *stdio = chr->opaque;
2231 INPUT_RECORD buf[4];
2232 int ret;
2233 DWORD dwSize;
2234 int i;
2235
Stefan Weildff74242013-12-07 14:48:04 +01002236 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002237
2238 if (!ret) {
2239 /* Avoid error storm */
2240 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2241 return;
2242 }
2243
2244 for (i = 0; i < dwSize; i++) {
2245 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2246
2247 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2248 int j;
2249 if (kev->uChar.AsciiChar != 0) {
2250 for (j = 0; j < kev->wRepeatCount; j++) {
2251 if (qemu_chr_be_can_write(chr)) {
2252 uint8_t c = kev->uChar.AsciiChar;
2253 qemu_chr_be_write(chr, &c, 1);
2254 }
2255 }
2256 }
2257 }
2258 }
2259}
2260
2261static DWORD WINAPI win_stdio_thread(LPVOID param)
2262{
2263 CharDriverState *chr = param;
2264 WinStdioCharState *stdio = chr->opaque;
2265 int ret;
2266 DWORD dwSize;
2267
2268 while (1) {
2269
2270 /* Wait for one byte */
2271 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2272
2273 /* Exit in case of error, continue if nothing read */
2274 if (!ret) {
2275 break;
2276 }
2277 if (!dwSize) {
2278 continue;
2279 }
2280
2281 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2282 if (stdio->win_stdio_buf == '\r') {
2283 continue;
2284 }
2285
2286 /* Signal the main thread and wait until the byte was eaten */
2287 if (!SetEvent(stdio->hInputReadyEvent)) {
2288 break;
2289 }
2290 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2291 != WAIT_OBJECT_0) {
2292 break;
2293 }
2294 }
2295
2296 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2297 return 0;
2298}
2299
2300static void win_stdio_thread_wait_func(void *opaque)
2301{
2302 CharDriverState *chr = opaque;
2303 WinStdioCharState *stdio = chr->opaque;
2304
2305 if (qemu_chr_be_can_write(chr)) {
2306 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2307 }
2308
2309 SetEvent(stdio->hInputDoneEvent);
2310}
2311
2312static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2313{
2314 WinStdioCharState *stdio = chr->opaque;
2315 DWORD dwMode = 0;
2316
2317 GetConsoleMode(stdio->hStdIn, &dwMode);
2318
2319 if (echo) {
2320 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2321 } else {
2322 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2323 }
2324}
2325
2326static void win_stdio_close(CharDriverState *chr)
2327{
2328 WinStdioCharState *stdio = chr->opaque;
2329
2330 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2331 CloseHandle(stdio->hInputReadyEvent);
2332 }
2333 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2334 CloseHandle(stdio->hInputDoneEvent);
2335 }
2336 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2337 TerminateThread(stdio->hInputThread, 0);
2338 }
2339
2340 g_free(chr->opaque);
2341 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002342}
2343
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002344static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002345{
2346 CharDriverState *chr;
2347 WinStdioCharState *stdio;
2348 DWORD dwMode;
2349 int is_console = 0;
2350
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002351 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002352 stdio = g_new0(WinStdioCharState, 1);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002353
2354 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2355 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2356 fprintf(stderr, "cannot open stdio: invalid handle\n");
2357 exit(1);
2358 }
2359
2360 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2361
2362 chr->opaque = stdio;
2363 chr->chr_write = win_stdio_write;
2364 chr->chr_close = win_stdio_close;
2365
Anthony Liguoried7a1542013-03-05 23:21:17 +05302366 if (is_console) {
2367 if (qemu_add_wait_object(stdio->hStdIn,
2368 win_stdio_wait_func, chr)) {
2369 fprintf(stderr, "qemu_add_wait_object: failed\n");
2370 }
2371 } else {
2372 DWORD dwId;
2373
2374 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2375 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2376 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2377 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002378
Anthony Liguoried7a1542013-03-05 23:21:17 +05302379 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2380 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2381 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2382 fprintf(stderr, "cannot create stdio thread or event\n");
2383 exit(1);
2384 }
2385 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2386 win_stdio_thread_wait_func, chr)) {
2387 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002388 }
2389 }
2390
2391 dwMode |= ENABLE_LINE_INPUT;
2392
Anthony Liguoried7a1542013-03-05 23:21:17 +05302393 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002394 /* set the terminal in raw mode */
2395 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2396 dwMode |= ENABLE_PROCESSED_INPUT;
2397 }
2398
2399 SetConsoleMode(stdio->hStdIn, dwMode);
2400
2401 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2402 qemu_chr_fe_set_echo(chr, false);
2403
Markus Armbruster1f514702012-02-07 15:09:08 +01002404 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002405}
aliguori6f97dba2008-10-31 18:49:55 +00002406#endif /* !_WIN32 */
2407
Anthony Liguori5ab82112013-03-05 23:21:31 +05302408
aliguori6f97dba2008-10-31 18:49:55 +00002409/***********************************************************/
2410/* UDP Net console */
2411
2412typedef struct {
2413 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302414 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302415 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002416 int bufcnt;
2417 int bufptr;
2418 int max_size;
2419} NetCharDriver;
2420
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002421/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002422static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2423{
2424 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302425 gsize bytes_written;
2426 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002427
Anthony Liguori76a96442013-03-05 23:21:21 +05302428 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2429 if (status == G_IO_STATUS_EOF) {
2430 return 0;
2431 } else if (status != G_IO_STATUS_NORMAL) {
2432 return -1;
2433 }
2434
2435 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002436}
2437
2438static int udp_chr_read_poll(void *opaque)
2439{
2440 CharDriverState *chr = opaque;
2441 NetCharDriver *s = chr->opaque;
2442
Anthony Liguori909cda12011-08-15 11:17:31 -05002443 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002444
2445 /* If there were any stray characters in the queue process them
2446 * first
2447 */
2448 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002449 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002450 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002451 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002452 }
2453 return s->max_size;
2454}
2455
Anthony Liguori76a96442013-03-05 23:21:21 +05302456static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002457{
2458 CharDriverState *chr = opaque;
2459 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302460 gsize bytes_read = 0;
2461 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002462
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002463 if (s->max_size == 0) {
2464 return TRUE;
2465 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302466 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2467 &bytes_read, NULL);
2468 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002469 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302470 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302471 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302472 return FALSE;
2473 }
aliguori6f97dba2008-10-31 18:49:55 +00002474
2475 s->bufptr = 0;
2476 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002477 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002478 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002479 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002480 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302481
2482 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002483}
2484
2485static void udp_chr_update_read_handler(CharDriverState *chr)
2486{
2487 NetCharDriver *s = chr->opaque;
2488
Amit Shah26da70c2013-08-28 15:23:37 +05302489 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302490 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302491 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2492 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002493 }
2494}
2495
aliguori819f56b2009-03-28 17:58:14 +00002496static void udp_chr_close(CharDriverState *chr)
2497{
2498 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302499
2500 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302501 if (s->chan) {
2502 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002503 closesocket(s->fd);
2504 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002505 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002506 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002507}
2508
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002509static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002510{
2511 CharDriverState *chr = NULL;
2512 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002513
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002514 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002515 s = g_new0(NetCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002516
aliguori6f97dba2008-10-31 18:49:55 +00002517 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302518 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002519 s->bufcnt = 0;
2520 s->bufptr = 0;
2521 chr->opaque = s;
2522 chr->chr_write = udp_chr_write;
2523 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002524 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002525 /* be isn't opened until we get a connection */
2526 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002527 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002528}
aliguori6f97dba2008-10-31 18:49:55 +00002529
aliguori6f97dba2008-10-31 18:49:55 +00002530/***********************************************************/
2531/* TCP Net console */
2532
2533typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302534
2535 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302536 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002537 int fd, listen_fd;
2538 int connected;
2539 int max_size;
2540 int do_telnetopt;
2541 int do_nodelay;
2542 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002543 int *read_msgfds;
2544 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002545 int *write_msgfds;
2546 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002547
2548 SocketAddress *addr;
2549 bool is_listen;
2550 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002551
2552 guint reconnect_timer;
2553 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002554 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002555} TCPCharDriver;
2556
Corey Minyard5dd1f022014-10-02 11:17:37 -05002557static gboolean socket_reconnect_timeout(gpointer opaque);
2558
2559static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2560{
2561 TCPCharDriver *s = chr->opaque;
2562 assert(s->connected == 0);
2563 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2564 socket_reconnect_timeout, chr);
2565}
2566
Corey Minyard5008e5b2014-10-08 07:11:55 -05002567static void check_report_connect_error(CharDriverState *chr,
2568 Error *err)
2569{
2570 TCPCharDriver *s = chr->opaque;
2571
2572 if (!s->connect_err_reported) {
2573 error_report("Unable to connect character device %s: %s",
2574 chr->label, error_get_pretty(err));
2575 s->connect_err_reported = true;
2576 }
2577 qemu_chr_socket_restart_timer(chr);
2578}
2579
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302580static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002581
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002582#ifndef _WIN32
2583static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2584{
2585 TCPCharDriver *s = chr->opaque;
2586 struct msghdr msgh;
2587 struct iovec iov;
2588 int r;
2589
2590 size_t fd_size = s->write_msgfds_num * sizeof(int);
2591 char control[CMSG_SPACE(fd_size)];
2592 struct cmsghdr *cmsg;
2593
2594 memset(&msgh, 0, sizeof(msgh));
2595 memset(control, 0, sizeof(control));
2596
2597 /* set the payload */
2598 iov.iov_base = (uint8_t *) buf;
2599 iov.iov_len = len;
2600
2601 msgh.msg_iov = &iov;
2602 msgh.msg_iovlen = 1;
2603
2604 msgh.msg_control = control;
2605 msgh.msg_controllen = sizeof(control);
2606
2607 cmsg = CMSG_FIRSTHDR(&msgh);
2608
2609 cmsg->cmsg_len = CMSG_LEN(fd_size);
2610 cmsg->cmsg_level = SOL_SOCKET;
2611 cmsg->cmsg_type = SCM_RIGHTS;
2612 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2613
2614 do {
2615 r = sendmsg(s->fd, &msgh, 0);
2616 } while (r < 0 && errno == EINTR);
2617
2618 /* free the written msgfds, no matter what */
2619 if (s->write_msgfds_num) {
2620 g_free(s->write_msgfds);
2621 s->write_msgfds = 0;
2622 s->write_msgfds_num = 0;
2623 }
2624
2625 return r;
2626}
2627#endif
2628
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002629/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002630static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2631{
2632 TCPCharDriver *s = chr->opaque;
2633 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002634#ifndef _WIN32
2635 if (s->is_unix && s->write_msgfds_num) {
2636 return unix_send_msgfds(chr, buf, len);
2637 } else
2638#endif
2639 {
2640 return io_channel_send(s->chan, buf, len);
2641 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002642 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002643 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002644 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002645 }
2646}
2647
2648static int tcp_chr_read_poll(void *opaque)
2649{
2650 CharDriverState *chr = opaque;
2651 TCPCharDriver *s = chr->opaque;
2652 if (!s->connected)
2653 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002654 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002655 return s->max_size;
2656}
2657
2658#define IAC 255
2659#define IAC_BREAK 243
2660static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2661 TCPCharDriver *s,
2662 uint8_t *buf, int *size)
2663{
2664 /* Handle any telnet client's basic IAC options to satisfy char by
2665 * char mode with no echo. All IAC options will be removed from
2666 * the buf and the do_telnetopt variable will be used to track the
2667 * state of the width of the IAC information.
2668 *
2669 * IAC commands come in sets of 3 bytes with the exception of the
2670 * "IAC BREAK" command and the double IAC.
2671 */
2672
2673 int i;
2674 int j = 0;
2675
2676 for (i = 0; i < *size; i++) {
2677 if (s->do_telnetopt > 1) {
2678 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2679 /* Double IAC means send an IAC */
2680 if (j != i)
2681 buf[j] = buf[i];
2682 j++;
2683 s->do_telnetopt = 1;
2684 } else {
2685 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2686 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002687 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002688 s->do_telnetopt++;
2689 }
2690 s->do_telnetopt++;
2691 }
2692 if (s->do_telnetopt >= 4) {
2693 s->do_telnetopt = 1;
2694 }
2695 } else {
2696 if ((unsigned char)buf[i] == IAC) {
2697 s->do_telnetopt = 2;
2698 } else {
2699 if (j != i)
2700 buf[j] = buf[i];
2701 j++;
2702 }
2703 }
2704 }
2705 *size = j;
2706}
2707
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002708static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002709{
2710 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002711 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2712
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002713 assert(num <= TCP_MAX_FDS);
2714
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002715 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002716 int i;
2717
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002718 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2719
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002720 /* Close unused fds */
2721 for (i = to_copy; i < s->read_msgfds_num; i++) {
2722 close(s->read_msgfds[i]);
2723 }
2724
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002725 g_free(s->read_msgfds);
2726 s->read_msgfds = 0;
2727 s->read_msgfds_num = 0;
2728 }
2729
2730 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002731}
2732
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002733static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2734{
2735 TCPCharDriver *s = chr->opaque;
2736
2737 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002738 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002739
2740 if (num) {
Markus Armbruster2d528d42015-09-14 13:54:03 +02002741 s->write_msgfds = g_new(int, num);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002742 memcpy(s->write_msgfds, fds, num * sizeof(int));
2743 }
2744
2745 s->write_msgfds_num = num;
2746
2747 return 0;
2748}
2749
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002750#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002751static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2752{
2753 TCPCharDriver *s = chr->opaque;
2754 struct cmsghdr *cmsg;
2755
2756 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002757 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002758
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002759 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002760 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002761 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002762 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002763 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002764
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002765 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2766
2767 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002768 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002769 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002770
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002771 /* close and clean read_msgfds */
2772 for (i = 0; i < s->read_msgfds_num; i++) {
2773 close(s->read_msgfds[i]);
2774 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002775
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002776 if (s->read_msgfds_num) {
2777 g_free(s->read_msgfds);
2778 }
2779
2780 s->read_msgfds_num = fd_size / sizeof(int);
2781 s->read_msgfds = g_malloc(fd_size);
2782 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2783
2784 for (i = 0; i < s->read_msgfds_num; i++) {
2785 int fd = s->read_msgfds[i];
2786 if (fd < 0) {
2787 continue;
2788 }
2789
2790 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2791 qemu_set_block(fd);
2792
2793 #ifndef MSG_CMSG_CLOEXEC
2794 qemu_set_cloexec(fd);
2795 #endif
2796 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002797 }
2798}
2799
Mark McLoughlin9977c892009-07-22 09:11:38 +01002800static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2801{
2802 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002803 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002804 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002805 union {
2806 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002807 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002808 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002809 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002810 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002811
2812 iov[0].iov_base = buf;
2813 iov[0].iov_len = len;
2814
2815 msg.msg_iov = iov;
2816 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002817 msg.msg_control = &msg_control;
2818 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002819
Corey Bryant06138652012-08-14 16:43:42 -04002820#ifdef MSG_CMSG_CLOEXEC
2821 flags |= MSG_CMSG_CLOEXEC;
2822#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002823 do {
2824 ret = recvmsg(s->fd, &msg, flags);
2825 } while (ret == -1 && errno == EINTR);
2826
Corey Bryant06138652012-08-14 16:43:42 -04002827 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002828 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002829 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002830
2831 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002832}
2833#else
2834static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2835{
2836 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002837 ssize_t ret;
2838
2839 do {
2840 ret = qemu_recv(s->fd, buf, len, 0);
2841 } while (ret == -1 && socket_error() == EINTR);
2842
2843 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002844}
2845#endif
2846
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302847static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2848{
2849 TCPCharDriver *s = chr->opaque;
2850 return g_io_create_watch(s->chan, cond);
2851}
2852
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002853static void tcp_chr_disconnect(CharDriverState *chr)
2854{
2855 TCPCharDriver *s = chr->opaque;
2856
2857 s->connected = 0;
2858 if (s->listen_chan) {
2859 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2860 tcp_chr_accept, chr);
2861 }
2862 remove_fd_in_watch(chr);
2863 g_io_channel_unref(s->chan);
2864 s->chan = NULL;
2865 closesocket(s->fd);
2866 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002867 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2868 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002869 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002870 if (s->reconnect_time) {
2871 qemu_chr_socket_restart_timer(chr);
2872 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002873}
2874
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302875static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002876{
2877 CharDriverState *chr = opaque;
2878 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302879 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002880 int len, size;
2881
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302882 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002883 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302884 }
aliguori6f97dba2008-10-31 18:49:55 +00002885 len = sizeof(buf);
2886 if (len > s->max_size)
2887 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002888 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002889 if (size == 0 ||
2890 (size < 0 &&
2891 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002892 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002893 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002894 } else if (size > 0) {
2895 if (s->do_telnetopt)
2896 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2897 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002898 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002899 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302900
2901 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002902}
2903
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002904static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2905{
2906 TCPCharDriver *s = chr->opaque;
2907 int size;
2908
2909 if (!s->connected) {
2910 return 0;
2911 }
2912
2913 size = tcp_chr_recv(chr, (void *) buf, len);
2914 if (size == 0) {
2915 /* connection closed */
2916 tcp_chr_disconnect(chr);
2917 }
2918
2919 return size;
2920}
2921
Blue Swirl68c18d12010-08-15 09:46:24 +00002922#ifndef _WIN32
2923CharDriverState *qemu_chr_open_eventfd(int eventfd)
2924{
David Marchande9d21c42014-06-11 17:25:16 +02002925 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2926
2927 if (chr) {
2928 chr->avail_connections = 1;
2929 }
2930
2931 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002932}
Blue Swirl68c18d12010-08-15 09:46:24 +00002933#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002934
aliguori6f97dba2008-10-31 18:49:55 +00002935static void tcp_chr_connect(void *opaque)
2936{
2937 CharDriverState *chr = opaque;
2938 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002939 struct sockaddr_storage ss, ps;
2940 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002941
2942 memset(&ss, 0, ss_len);
2943 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2944 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2945 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002946 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2947 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2948 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002949 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002950 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2951 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002952 s->is_listen, s->is_telnet);
2953 }
aliguori6f97dba2008-10-31 18:49:55 +00002954
2955 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302956 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302957 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2958 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002959 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002960 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002961}
2962
Gal Hammerac1b84d2014-02-25 12:12:35 +02002963static void tcp_chr_update_read_handler(CharDriverState *chr)
2964{
2965 TCPCharDriver *s = chr->opaque;
2966
2967 remove_fd_in_watch(chr);
2968 if (s->chan) {
2969 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2970 tcp_chr_read, chr);
2971 }
2972}
2973
aliguori6f97dba2008-10-31 18:49:55 +00002974#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2975static void tcp_chr_telnet_init(int fd)
2976{
2977 char buf[3];
2978 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2979 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2980 send(fd, (char *)buf, 3, 0);
2981 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2982 send(fd, (char *)buf, 3, 0);
2983 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2984 send(fd, (char *)buf, 3, 0);
2985 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2986 send(fd, (char *)buf, 3, 0);
2987}
2988
Daniel P. Berrange13661082011-06-23 13:31:42 +01002989static int tcp_chr_add_client(CharDriverState *chr, int fd)
2990{
2991 TCPCharDriver *s = chr->opaque;
2992 if (s->fd != -1)
2993 return -1;
2994
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002995 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002996 if (s->do_nodelay)
2997 socket_set_nodelay(fd);
2998 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302999 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003000 if (s->listen_tag) {
3001 g_source_remove(s->listen_tag);
3002 s->listen_tag = 0;
3003 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003004 tcp_chr_connect(chr);
3005
3006 return 0;
3007}
3008
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303009static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00003010{
3011 CharDriverState *chr = opaque;
3012 TCPCharDriver *s = chr->opaque;
3013 struct sockaddr_in saddr;
3014#ifndef _WIN32
3015 struct sockaddr_un uaddr;
3016#endif
3017 struct sockaddr *addr;
3018 socklen_t len;
3019 int fd;
3020
3021 for(;;) {
3022#ifndef _WIN32
3023 if (s->is_unix) {
3024 len = sizeof(uaddr);
3025 addr = (struct sockaddr *)&uaddr;
3026 } else
3027#endif
3028 {
3029 len = sizeof(saddr);
3030 addr = (struct sockaddr *)&saddr;
3031 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003032 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003033 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003034 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303035 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003036 } else if (fd >= 0) {
3037 if (s->do_telnetopt)
3038 tcp_chr_telnet_init(fd);
3039 break;
3040 }
3041 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003042 if (tcp_chr_add_client(chr, fd) < 0)
3043 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303044
3045 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003046}
3047
3048static void tcp_chr_close(CharDriverState *chr)
3049{
3050 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003051 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003052
Corey Minyard5dd1f022014-10-02 11:17:37 -05003053 if (s->reconnect_timer) {
3054 g_source_remove(s->reconnect_timer);
3055 s->reconnect_timer = 0;
3056 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003057 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003058 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303059 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303060 if (s->chan) {
3061 g_io_channel_unref(s->chan);
3062 }
aliguori6f97dba2008-10-31 18:49:55 +00003063 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003064 }
3065 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303066 if (s->listen_tag) {
3067 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003068 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303069 }
3070 if (s->listen_chan) {
3071 g_io_channel_unref(s->listen_chan);
3072 }
aliguori6f97dba2008-10-31 18:49:55 +00003073 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003074 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003075 if (s->read_msgfds_num) {
3076 for (i = 0; i < s->read_msgfds_num; i++) {
3077 close(s->read_msgfds[i]);
3078 }
3079 g_free(s->read_msgfds);
3080 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003081 if (s->write_msgfds_num) {
3082 g_free(s->write_msgfds);
3083 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003084 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003085 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003086}
3087
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003088static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003089{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003090 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003091
Corey Minyardcfb429c2014-10-02 11:17:35 -05003092 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003093 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303094 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003095 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3096 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003097 } else {
3098 s->connected = 1;
3099 s->fd = fd;
3100 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303101 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003102 tcp_chr_connect(chr);
3103 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003104}
3105
Corey Minyard51795022014-10-08 07:11:56 -05003106static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003107{
3108 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003109 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003110
3111 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003112 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003113 return;
3114 }
3115
Corey Minyard5008e5b2014-10-08 07:11:55 -05003116 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003117 qemu_chr_finish_socket_connection(chr, fd);
3118}
3119
Corey Minyardcfb429c2014-10-02 11:17:35 -05003120static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003121{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003122 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003123 int fd;
3124
Corey Minyardcfb429c2014-10-02 11:17:35 -05003125 if (s->is_listen) {
3126 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003127 } else if (s->reconnect_time) {
3128 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3129 return fd >= 0;
3130 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003131 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003132 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003133 if (fd < 0) {
3134 return false;
3135 }
3136
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003137 qemu_chr_finish_socket_connection(chr, fd);
3138 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003139}
3140
Lei Li51767e72013-01-25 00:03:19 +08003141/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003142/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003143
3144typedef struct {
3145 size_t size;
3146 size_t prod;
3147 size_t cons;
3148 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003149} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003150
Markus Armbruster3949e592013-02-06 21:27:24 +01003151static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003152{
Markus Armbruster3949e592013-02-06 21:27:24 +01003153 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003154
Markus Armbruster5c230102013-02-06 21:27:23 +01003155 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003156}
3157
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003158/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003159static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003160{
Markus Armbruster3949e592013-02-06 21:27:24 +01003161 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003162 int i;
3163
3164 if (!buf || (len < 0)) {
3165 return -1;
3166 }
3167
3168 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003169 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3170 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003171 d->cons = d->prod - d->size;
3172 }
3173 }
3174
3175 return 0;
3176}
3177
Markus Armbruster3949e592013-02-06 21:27:24 +01003178static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003179{
Markus Armbruster3949e592013-02-06 21:27:24 +01003180 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003181 int i;
3182
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003183 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003184 for (i = 0; i < len && d->cons != d->prod; i++) {
3185 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003186 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003187 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003188
3189 return i;
3190}
3191
Markus Armbruster3949e592013-02-06 21:27:24 +01003192static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003193{
Markus Armbruster3949e592013-02-06 21:27:24 +01003194 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003195
3196 g_free(d->cbuf);
3197 g_free(d);
3198 chr->opaque = NULL;
3199}
3200
Markus Armbruster4f573782013-07-26 16:44:32 +02003201static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3202 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003203{
3204 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003205 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003206
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003207 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003208 d = g_malloc(sizeof(*d));
3209
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003210 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003211
3212 /* The size must be power of 2 */
3213 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003214 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003215 goto fail;
3216 }
3217
3218 d->prod = 0;
3219 d->cons = 0;
3220 d->cbuf = g_malloc0(d->size);
3221
3222 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003223 chr->chr_write = ringbuf_chr_write;
3224 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003225
3226 return chr;
3227
3228fail:
3229 g_free(d);
3230 g_free(chr);
3231 return NULL;
3232}
3233
Hani Benhabiles8e597772014-05-27 23:39:30 +01003234bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003235{
Markus Armbruster3949e592013-02-06 21:27:24 +01003236 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003237}
3238
Markus Armbruster3949e592013-02-06 21:27:24 +01003239void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003240 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003241 Error **errp)
3242{
3243 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003244 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003245 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003246 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003247
3248 chr = qemu_chr_find(device);
3249 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003250 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003251 return;
3252 }
3253
Markus Armbruster3949e592013-02-06 21:27:24 +01003254 if (!chr_is_ringbuf(chr)) {
3255 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003256 return;
3257 }
3258
Lei Li1f590cf2013-01-25 00:03:20 +08003259 if (has_format && (format == DATA_FORMAT_BASE64)) {
3260 write_data = g_base64_decode(data, &write_count);
3261 } else {
3262 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003263 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003264 }
3265
Markus Armbruster3949e592013-02-06 21:27:24 +01003266 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003267
Markus Armbruster13289fb2013-02-06 21:27:18 +01003268 if (write_data != (uint8_t *)data) {
3269 g_free((void *)write_data);
3270 }
3271
Lei Li1f590cf2013-01-25 00:03:20 +08003272 if (ret < 0) {
3273 error_setg(errp, "Failed to write to device %s", device);
3274 return;
3275 }
3276}
3277
Markus Armbruster3949e592013-02-06 21:27:24 +01003278char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003279 bool has_format, enum DataFormat format,
3280 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003281{
3282 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003283 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003284 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003285 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003286
3287 chr = qemu_chr_find(device);
3288 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003289 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003290 return NULL;
3291 }
3292
Markus Armbruster3949e592013-02-06 21:27:24 +01003293 if (!chr_is_ringbuf(chr)) {
3294 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003295 return NULL;
3296 }
3297
3298 if (size <= 0) {
3299 error_setg(errp, "size must be greater than zero");
3300 return NULL;
3301 }
3302
Markus Armbruster3949e592013-02-06 21:27:24 +01003303 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003304 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003305 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003306
Markus Armbruster3949e592013-02-06 21:27:24 +01003307 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003308
3309 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003310 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003311 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003312 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003313 /*
3314 * FIXME should read only complete, valid UTF-8 characters up
3315 * to @size bytes. Invalid sequences should be replaced by a
3316 * suitable replacement character. Except when (and only
3317 * when) ring buffer lost characters since last read, initial
3318 * continuation characters should be dropped.
3319 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003320 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003321 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003322 }
3323
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003324 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003325}
3326
Gerd Hoffmann33521632009-12-08 13:11:49 +01003327QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003328{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003329 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003330 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003331 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003332 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003333 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003334
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003335 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003336 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003337 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003338 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003339 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003340
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003341 if (strstart(filename, "mon:", &p)) {
3342 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003343 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003344 if (strcmp(filename, "stdio") == 0) {
3345 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3346 * but pass it to the guest. Handle this only for compat syntax,
3347 * for -chardev syntax we have special option for this.
3348 * This is what -nographic did, redirecting+muxing serial+monitor
3349 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003350 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003351 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003352 }
3353
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003354 if (strcmp(filename, "null") == 0 ||
3355 strcmp(filename, "pty") == 0 ||
3356 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003357 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003358 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003359 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003360 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003361 return opts;
3362 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003363 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003364 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003365 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003366 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003367 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003368 qemu_opt_set(opts, "width", width, &error_abort);
3369 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003370 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003371 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003372 qemu_opt_set(opts, "cols", width, &error_abort);
3373 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003374 } else {
3375 goto fail;
3376 }
3377 }
3378 return opts;
3379 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003380 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003381 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003382 return opts;
3383 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003384 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003385 qemu_opt_set(opts, "backend", "serial", &error_abort);
3386 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003387 return opts;
3388 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003389 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003390 qemu_opt_set(opts, "backend", "file", &error_abort);
3391 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003392 return opts;
3393 }
3394 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003395 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3396 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003397 return opts;
3398 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003399 if (strstart(filename, "tcp:", &p) ||
3400 strstart(filename, "telnet:", &p)) {
3401 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3402 host[0] = 0;
3403 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3404 goto fail;
3405 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003406 qemu_opt_set(opts, "backend", "socket", &error_abort);
3407 qemu_opt_set(opts, "host", host, &error_abort);
3408 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003409 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003410 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3411 if (local_err) {
3412 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003413 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003414 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003415 }
3416 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003417 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003418 return opts;
3419 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003420 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003421 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003422 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3423 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003424 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003425 goto fail;
3426 }
3427 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003428 qemu_opt_set(opts, "host", host, &error_abort);
3429 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003430 if (p[pos] == '@') {
3431 p += pos + 1;
3432 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3433 host[0] = 0;
3434 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003435 goto fail;
3436 }
3437 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003438 qemu_opt_set(opts, "localaddr", host, &error_abort);
3439 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003440 }
3441 return opts;
3442 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003443 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003444 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003445 qemu_opts_do_parse(opts, p, "path", &local_err);
3446 if (local_err) {
3447 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003448 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003449 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003450 return opts;
3451 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003452 if (strstart(filename, "/dev/parport", NULL) ||
3453 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003454 qemu_opt_set(opts, "backend", "parport", &error_abort);
3455 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003456 return opts;
3457 }
3458 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003459 qemu_opt_set(opts, "backend", "tty", &error_abort);
3460 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003461 return opts;
3462 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003463
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003464fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003465 qemu_opts_del(opts);
3466 return NULL;
3467}
3468
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003469static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3470 Error **errp)
3471{
3472 const char *path = qemu_opt_get(opts, "path");
3473
3474 if (path == NULL) {
3475 error_setg(errp, "chardev: file: no filename given");
3476 return;
3477 }
3478 backend->file = g_new0(ChardevFile, 1);
3479 backend->file->out = g_strdup(path);
3480}
3481
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003482static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3483 Error **errp)
3484{
3485 backend->stdio = g_new0(ChardevStdio, 1);
3486 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003487 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003488}
3489
Paolo Bonzini6511d392015-09-29 15:08:05 +02003490#ifdef HAVE_CHARDEV_SERIAL
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003491static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3492 Error **errp)
3493{
3494 const char *device = qemu_opt_get(opts, "path");
3495
3496 if (device == NULL) {
3497 error_setg(errp, "chardev: serial/tty: no device path given");
3498 return;
3499 }
3500 backend->serial = g_new0(ChardevHostdev, 1);
3501 backend->serial->device = g_strdup(device);
3502}
Paolo Bonzini6511d392015-09-29 15:08:05 +02003503#endif
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003504
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003505#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003506static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3507 Error **errp)
3508{
3509 const char *device = qemu_opt_get(opts, "path");
3510
3511 if (device == NULL) {
3512 error_setg(errp, "chardev: parallel: no device path given");
3513 return;
3514 }
3515 backend->parallel = g_new0(ChardevHostdev, 1);
3516 backend->parallel->device = g_strdup(device);
3517}
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003518#endif
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003519
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003520static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3521 Error **errp)
3522{
3523 const char *device = qemu_opt_get(opts, "path");
3524
3525 if (device == NULL) {
3526 error_setg(errp, "chardev: pipe: no device path given");
3527 return;
3528 }
3529 backend->pipe = g_new0(ChardevHostdev, 1);
3530 backend->pipe->device = g_strdup(device);
3531}
3532
Markus Armbruster4f573782013-07-26 16:44:32 +02003533static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3534 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003535{
3536 int val;
3537
Markus Armbruster3a1da422013-07-26 16:44:34 +02003538 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003539
Markus Armbruster0f953052013-06-27 16:22:07 +02003540 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003541 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003542 backend->ringbuf->has_size = true;
3543 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003544 }
3545}
3546
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003547static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3548 Error **errp)
3549{
3550 const char *chardev = qemu_opt_get(opts, "chardev");
3551
3552 if (chardev == NULL) {
3553 error_setg(errp, "chardev: mux: no chardev given");
3554 return;
3555 }
3556 backend->mux = g_new0(ChardevMux, 1);
3557 backend->mux->chardev = g_strdup(chardev);
3558}
3559
Peter Maydelldafd3252014-09-02 11:24:13 +01003560static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3561 Error **errp)
3562{
3563 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3564 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3565 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3566 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003567 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003568 const char *path = qemu_opt_get(opts, "path");
3569 const char *host = qemu_opt_get(opts, "host");
3570 const char *port = qemu_opt_get(opts, "port");
3571 SocketAddress *addr;
3572
3573 if (!path) {
3574 if (!host) {
3575 error_setg(errp, "chardev: socket: no host given");
3576 return;
3577 }
3578 if (!port) {
3579 error_setg(errp, "chardev: socket: no port given");
3580 return;
3581 }
3582 }
3583
3584 backend->socket = g_new0(ChardevSocket, 1);
3585
3586 backend->socket->has_nodelay = true;
3587 backend->socket->nodelay = do_nodelay;
3588 backend->socket->has_server = true;
3589 backend->socket->server = is_listen;
3590 backend->socket->has_telnet = true;
3591 backend->socket->telnet = is_telnet;
3592 backend->socket->has_wait = true;
3593 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003594 backend->socket->has_reconnect = true;
3595 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003596
3597 addr = g_new0(SocketAddress, 1);
3598 if (path) {
3599 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3600 addr->q_unix = g_new0(UnixSocketAddress, 1);
3601 addr->q_unix->path = g_strdup(path);
3602 } else {
3603 addr->kind = SOCKET_ADDRESS_KIND_INET;
3604 addr->inet = g_new0(InetSocketAddress, 1);
3605 addr->inet->host = g_strdup(host);
3606 addr->inet->port = g_strdup(port);
3607 addr->inet->has_to = qemu_opt_get(opts, "to");
3608 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3609 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3610 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3611 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3612 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3613 }
3614 backend->socket->addr = addr;
3615}
3616
Peter Maydell90a14bf2014-09-02 11:24:15 +01003617static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3618 Error **errp)
3619{
3620 const char *host = qemu_opt_get(opts, "host");
3621 const char *port = qemu_opt_get(opts, "port");
3622 const char *localaddr = qemu_opt_get(opts, "localaddr");
3623 const char *localport = qemu_opt_get(opts, "localport");
3624 bool has_local = false;
3625 SocketAddress *addr;
3626
3627 if (host == NULL || strlen(host) == 0) {
3628 host = "localhost";
3629 }
3630 if (port == NULL || strlen(port) == 0) {
3631 error_setg(errp, "chardev: udp: remote port not specified");
3632 return;
3633 }
3634 if (localport == NULL || strlen(localport) == 0) {
3635 localport = "0";
3636 } else {
3637 has_local = true;
3638 }
3639 if (localaddr == NULL || strlen(localaddr) == 0) {
3640 localaddr = "";
3641 } else {
3642 has_local = true;
3643 }
3644
3645 backend->udp = g_new0(ChardevUdp, 1);
3646
3647 addr = g_new0(SocketAddress, 1);
3648 addr->kind = SOCKET_ADDRESS_KIND_INET;
3649 addr->inet = g_new0(InetSocketAddress, 1);
3650 addr->inet->host = g_strdup(host);
3651 addr->inet->port = g_strdup(port);
3652 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3653 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3654 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3655 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3656 backend->udp->remote = addr;
3657
3658 if (has_local) {
3659 backend->udp->has_local = true;
3660 addr = g_new0(SocketAddress, 1);
3661 addr->kind = SOCKET_ADDRESS_KIND_INET;
3662 addr->inet = g_new0(InetSocketAddress, 1);
3663 addr->inet->host = g_strdup(localaddr);
3664 addr->inet->port = g_strdup(localport);
3665 backend->udp->local = addr;
3666 }
3667}
3668
Anthony Liguorid654f342013-03-05 23:21:28 +05303669typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003670 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003671 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003672 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003673 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3674 ChardevReturn *ret, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303675} CharDriver;
3676
3677static GSList *backends;
3678
Peter Maydelle4d50d42014-09-02 11:24:17 +01003679void register_char_driver(const char *name, ChardevBackendKind kind,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003680 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp),
3681 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3682 ChardevReturn *ret, Error **errp))
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003683{
3684 CharDriver *s;
3685
3686 s = g_malloc0(sizeof(*s));
3687 s->name = g_strdup(name);
3688 s->kind = kind;
3689 s->parse = parse;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003690 s->create = create;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003691
3692 backends = g_slist_append(backends, s);
3693}
3694
Anthony Liguorif69554b2011-08-15 11:17:37 -05003695CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003696 void (*init)(struct CharDriverState *s),
3697 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003698{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003699 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303700 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003701 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303702 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003703 ChardevReturn *ret = NULL;
3704 ChardevBackend *backend;
3705 const char *id = qemu_opts_id(opts);
3706 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003707
Peter Maydella61ae7f2014-09-02 11:24:16 +01003708 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003709 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003710 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003711 }
3712
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003713 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003714 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003715 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003716 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003717 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303718 for (i = backends; i; i = i->next) {
3719 cd = i->data;
3720
3721 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003722 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303723 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003724 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303725 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003726 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003727 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003728 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003729 }
3730
Peter Maydella61ae7f2014-09-02 11:24:16 +01003731 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003732
3733 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003734 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003735 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003736
3737 chr = NULL;
3738 backend->kind = cd->kind;
3739 if (cd->parse) {
3740 cd->parse(opts, backend, &local_err);
3741 if (local_err) {
3742 error_propagate(errp, local_err);
3743 goto qapi_out;
3744 }
3745 }
3746 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3747 if (!ret) {
3748 goto qapi_out;
3749 }
3750
3751 if (bid) {
3752 qapi_free_ChardevBackend(backend);
3753 qapi_free_ChardevReturn(ret);
3754 backend = g_new0(ChardevBackend, 1);
3755 backend->mux = g_new0(ChardevMux, 1);
3756 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3757 backend->mux->chardev = g_strdup(bid);
3758 ret = qmp_chardev_add(id, backend, errp);
3759 if (!ret) {
3760 chr = qemu_chr_find(bid);
3761 qemu_chr_delete(chr);
3762 chr = NULL;
3763 goto qapi_out;
3764 }
3765 }
3766
3767 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003768 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003769
3770qapi_out:
3771 qapi_free_ChardevBackend(backend);
3772 qapi_free_ChardevReturn(ret);
3773 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003774 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003775
3776err:
3777 qemu_opts_del(opts);
3778 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003779}
3780
Anthony Liguori27143a42011-08-15 11:17:36 -05003781CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003782{
3783 const char *p;
3784 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003785 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003786 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003787
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003788 if (strstart(filename, "chardev:", &p)) {
3789 return qemu_chr_find(p);
3790 }
3791
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003792 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003793 if (!opts)
3794 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003795
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003796 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003797 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003798 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003799 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003800 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003801 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003802 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003803 }
3804 return chr;
3805}
3806
Anthony Liguori15f31512011-08-15 11:17:35 -05003807void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003808{
3809 if (chr->chr_set_echo) {
3810 chr->chr_set_echo(chr, echo);
3811 }
3812}
3813
Hans de Goede8e25daa2013-03-26 11:07:57 +01003814void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003815{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003816 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003817 return;
3818 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003819 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003820 if (chr->chr_set_fe_open) {
3821 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003822 }
3823}
3824
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003825void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3826{
3827 if (chr->chr_fe_event) {
3828 chr->chr_fe_event(chr, event);
3829 }
3830}
3831
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003832int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3833 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303834{
3835 GSource *src;
3836 guint tag;
3837
3838 if (s->chr_add_watch == NULL) {
3839 return -ENOSYS;
3840 }
3841
3842 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003843 if (!src) {
3844 return -EINVAL;
3845 }
3846
Anthony Liguori23673ca2013-03-05 23:21:23 +05303847 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3848 tag = g_source_attach(src, NULL);
3849 g_source_unref(src);
3850
3851 return tag;
3852}
3853
Hans de Goede44c473d2013-03-27 20:29:39 +01003854int qemu_chr_fe_claim(CharDriverState *s)
3855{
3856 if (s->avail_connections < 1) {
3857 return -1;
3858 }
3859 s->avail_connections--;
3860 return 0;
3861}
3862
3863void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3864{
3865 if (qemu_chr_fe_claim(s) != 0) {
3866 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3867 __func__, s->label);
3868 exit(1);
3869 }
3870}
3871
3872void qemu_chr_fe_release(CharDriverState *s)
3873{
3874 s->avail_connections++;
3875}
3876
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003877void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003878{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003879 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003880 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003881 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003882 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003883 g_free(chr->filename);
3884 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003885 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003886 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003887}
3888
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003889ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003890{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003891 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003892 CharDriverState *chr;
3893
Blue Swirl72cf2d42009-09-12 07:36:22 +00003894 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003895 ChardevInfoList *info = g_malloc0(sizeof(*info));
3896 info->value = g_malloc0(sizeof(*info->value));
3897 info->value->label = g_strdup(chr->label);
3898 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003899 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003900
3901 info->next = chr_list;
3902 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003903 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003904
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003905 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003906}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003907
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003908ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3909{
3910 ChardevBackendInfoList *backend_list = NULL;
3911 CharDriver *c = NULL;
3912 GSList *i = NULL;
3913
3914 for (i = backends; i; i = i->next) {
3915 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3916 c = i->data;
3917 info->value = g_malloc0(sizeof(*info->value));
3918 info->value->name = g_strdup(c->name);
3919
3920 info->next = backend_list;
3921 backend_list = info;
3922 }
3923
3924 return backend_list;
3925}
3926
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003927CharDriverState *qemu_chr_find(const char *name)
3928{
3929 CharDriverState *chr;
3930
Blue Swirl72cf2d42009-09-12 07:36:22 +00003931 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003932 if (strcmp(chr->label, name) != 0)
3933 continue;
3934 return chr;
3935 }
3936 return NULL;
3937}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003938
3939/* Get a character (serial) device interface. */
3940CharDriverState *qemu_char_get_next_serial(void)
3941{
3942 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003943 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003944
3945 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003946
3947 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3948 chr = serial_hds[next_serial++];
3949 qemu_chr_fe_claim_no_fail(chr);
3950 return chr;
3951 }
3952 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003953}
3954
Paolo Bonzini4d454572012-11-26 16:03:42 +01003955QemuOptsList qemu_chardev_opts = {
3956 .name = "chardev",
3957 .implied_opt_name = "backend",
3958 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3959 .desc = {
3960 {
3961 .name = "backend",
3962 .type = QEMU_OPT_STRING,
3963 },{
3964 .name = "path",
3965 .type = QEMU_OPT_STRING,
3966 },{
3967 .name = "host",
3968 .type = QEMU_OPT_STRING,
3969 },{
3970 .name = "port",
3971 .type = QEMU_OPT_STRING,
3972 },{
3973 .name = "localaddr",
3974 .type = QEMU_OPT_STRING,
3975 },{
3976 .name = "localport",
3977 .type = QEMU_OPT_STRING,
3978 },{
3979 .name = "to",
3980 .type = QEMU_OPT_NUMBER,
3981 },{
3982 .name = "ipv4",
3983 .type = QEMU_OPT_BOOL,
3984 },{
3985 .name = "ipv6",
3986 .type = QEMU_OPT_BOOL,
3987 },{
3988 .name = "wait",
3989 .type = QEMU_OPT_BOOL,
3990 },{
3991 .name = "server",
3992 .type = QEMU_OPT_BOOL,
3993 },{
3994 .name = "delay",
3995 .type = QEMU_OPT_BOOL,
3996 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003997 .name = "reconnect",
3998 .type = QEMU_OPT_NUMBER,
3999 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004000 .name = "telnet",
4001 .type = QEMU_OPT_BOOL,
4002 },{
4003 .name = "width",
4004 .type = QEMU_OPT_NUMBER,
4005 },{
4006 .name = "height",
4007 .type = QEMU_OPT_NUMBER,
4008 },{
4009 .name = "cols",
4010 .type = QEMU_OPT_NUMBER,
4011 },{
4012 .name = "rows",
4013 .type = QEMU_OPT_NUMBER,
4014 },{
4015 .name = "mux",
4016 .type = QEMU_OPT_BOOL,
4017 },{
4018 .name = "signal",
4019 .type = QEMU_OPT_BOOL,
4020 },{
4021 .name = "name",
4022 .type = QEMU_OPT_STRING,
4023 },{
4024 .name = "debug",
4025 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08004026 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01004027 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01004028 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004029 },{
4030 .name = "chardev",
4031 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004032 },
4033 { /* end of list */ }
4034 },
4035};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004036
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004037#ifdef _WIN32
4038
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004039static CharDriverState *qmp_chardev_open_file(const char *id,
4040 ChardevBackend *backend,
4041 ChardevReturn *ret,
4042 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004043{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004044 ChardevFile *file = backend->file;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004045 HANDLE out;
4046
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004047 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004048 error_setg(errp, "input file not supported");
4049 return NULL;
4050 }
4051
4052 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4053 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4054 if (out == INVALID_HANDLE_VALUE) {
4055 error_setg(errp, "open %s failed", file->out);
4056 return NULL;
4057 }
4058 return qemu_chr_open_win_file(out);
4059}
4060
Paolo Bonzini6511d392015-09-29 15:08:05 +02004061static CharDriverState *qmp_chardev_open_serial(const char *id,
4062 ChardevBackend *backend,
4063 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004064 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004065{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004066 ChardevHostdev *serial = backend->serial;
4067 return qemu_chr_open_win_path(serial->device, errp);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004068}
4069
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004070#else /* WIN32 */
4071
4072static int qmp_chardev_open_file_source(char *src, int flags,
4073 Error **errp)
4074{
4075 int fd = -1;
4076
4077 TFR(fd = qemu_open(src, flags, 0666));
4078 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004079 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004080 }
4081 return fd;
4082}
4083
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004084static CharDriverState *qmp_chardev_open_file(const char *id,
4085 ChardevBackend *backend,
4086 ChardevReturn *ret,
4087 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004088{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004089 ChardevFile *file = backend->file;
Markus Armbruster5f758362014-05-19 18:57:34 +02004090 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004091
4092 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4093 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004094 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004095 return NULL;
4096 }
4097
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004098 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004099 flags = O_RDONLY;
4100 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004101 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004102 qemu_close(out);
4103 return NULL;
4104 }
4105 }
4106
4107 return qemu_chr_open_fd(in, out);
4108}
4109
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004110#ifdef HAVE_CHARDEV_SERIAL
Paolo Bonzini6511d392015-09-29 15:08:05 +02004111static CharDriverState *qmp_chardev_open_serial(const char *id,
4112 ChardevBackend *backend,
4113 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004114 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004115{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004116 ChardevHostdev *serial = backend->serial;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004117 int fd;
4118
4119 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004120 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004121 return NULL;
4122 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004123 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004124 return qemu_chr_open_tty_fd(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004125}
Paolo Bonzini6511d392015-09-29 15:08:05 +02004126#endif
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004127
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004128#ifdef HAVE_CHARDEV_PARPORT
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004129static CharDriverState *qmp_chardev_open_parallel(const char *id,
4130 ChardevBackend *backend,
4131 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004132 Error **errp)
4133{
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004134 ChardevHostdev *parallel = backend->parallel;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004135 int fd;
4136
4137 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004138 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004139 return NULL;
4140 }
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004141 return qemu_chr_open_pp_fd(fd, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004142}
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004143#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004144
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004145#endif /* WIN32 */
4146
Corey Minyard5008e5b2014-10-08 07:11:55 -05004147static void socket_try_connect(CharDriverState *chr)
4148{
4149 Error *err = NULL;
4150
4151 if (!qemu_chr_open_socket_fd(chr, &err)) {
4152 check_report_connect_error(chr, err);
4153 }
4154}
4155
Corey Minyard5dd1f022014-10-02 11:17:37 -05004156static gboolean socket_reconnect_timeout(gpointer opaque)
4157{
4158 CharDriverState *chr = opaque;
4159 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004160
4161 s->reconnect_timer = 0;
4162
4163 if (chr->be_open) {
4164 return false;
4165 }
4166
Corey Minyard5008e5b2014-10-08 07:11:55 -05004167 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004168
4169 return false;
4170}
4171
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004172static CharDriverState *qmp_chardev_open_socket(const char *id,
4173 ChardevBackend *backend,
4174 ChardevReturn *ret,
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004175 Error **errp)
4176{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004177 CharDriverState *chr;
4178 TCPCharDriver *s;
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004179 ChardevSocket *sock = backend->socket;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004180 SocketAddress *addr = sock->addr;
4181 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4182 bool is_listen = sock->has_server ? sock->server : true;
4183 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4184 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004185 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004186
4187 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02004188 s = g_new0(TCPCharDriver, 1);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004189
4190 s->fd = -1;
4191 s->listen_fd = -1;
4192 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004193 s->is_listen = is_listen;
4194 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004195 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004196 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004197
4198 chr->opaque = s;
4199 chr->chr_write = tcp_chr_write;
4200 chr->chr_sync_read = tcp_chr_sync_read;
4201 chr->chr_close = tcp_chr_close;
4202 chr->get_msgfds = tcp_get_msgfds;
4203 chr->set_msgfds = tcp_set_msgfds;
4204 chr->chr_add_client = tcp_chr_add_client;
4205 chr->chr_add_watch = tcp_chr_add_watch;
4206 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4207 /* be isn't opened until we get a connection */
4208 chr->explicit_be_open = true;
4209
4210 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004211 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4212 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004213
4214 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004215 if (is_telnet) {
4216 s->do_telnetopt = 1;
4217 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004218 } else if (reconnect > 0) {
4219 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004220 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004221
Corey Minyard5008e5b2014-10-08 07:11:55 -05004222 if (s->reconnect_time) {
4223 socket_try_connect(chr);
4224 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4225 g_free(s);
4226 g_free(chr->filename);
4227 g_free(chr);
4228 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004229 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004230
4231 if (is_listen && is_waitconnect) {
4232 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4233 chr->filename);
4234 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4235 qemu_set_nonblock(s->listen_fd);
4236 }
4237
4238 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004239}
4240
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004241static CharDriverState *qmp_chardev_open_udp(const char *id,
4242 ChardevBackend *backend,
4243 ChardevReturn *ret,
Lei Li08d0ab32013-05-20 14:51:03 +08004244 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004245{
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004246 ChardevUdp *udp = backend->udp;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004247 int fd;
4248
Lei Li08d0ab32013-05-20 14:51:03 +08004249 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004250 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004251 return NULL;
4252 }
4253 return qemu_chr_open_udp_fd(fd);
4254}
4255
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004256ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4257 Error **errp)
4258{
4259 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004260 CharDriverState *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004261 Error *local_err = NULL;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004262 GSList *i;
4263 CharDriver *cd;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004264
4265 chr = qemu_chr_find(id);
4266 if (chr) {
4267 error_setg(errp, "Chardev '%s' already exists", id);
4268 g_free(ret);
4269 return NULL;
4270 }
4271
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004272 for (i = backends; i; i = i->next) {
4273 cd = i->data;
4274
4275 if (cd->kind == backend->kind && cd->create) {
4276 chr = cd->create(id, backend, ret, &local_err);
4277 if (local_err) {
4278 error_propagate(errp, local_err);
4279 goto out_error;
4280 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004281 break;
4282 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004283 }
4284
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004285 if (chr == NULL) {
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004286 switch (backend->kind) {
4287 case CHARDEV_BACKEND_KIND_FILE:
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004288 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004289 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004290 case CHARDEV_BACKEND_KIND_SERIAL:
Paolo Bonzini6511d392015-09-29 15:08:05 +02004291 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004292 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004293 case CHARDEV_BACKEND_KIND_PARALLEL:
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004294 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004295 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004296 case CHARDEV_BACKEND_KIND_PIPE:
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004297 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004298 break;
4299 case CHARDEV_BACKEND_KIND_SOCKET:
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004300 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004301 break;
4302 case CHARDEV_BACKEND_KIND_UDP:
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004303 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004304 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004305 case CHARDEV_BACKEND_KIND_PTY:
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004306 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004307 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004308 case CHARDEV_BACKEND_KIND_NULL:
Paolo Bonzini0d649922015-09-29 15:24:29 +02004309 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004310 break;
4311 case CHARDEV_BACKEND_KIND_MUX:
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004312 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004313 break;
4314 case CHARDEV_BACKEND_KIND_MSMOUSE:
Paolo Bonzini96d885b2015-09-29 15:29:15 +02004315 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004316 break;
4317#ifdef CONFIG_BRLAPI
4318 case CHARDEV_BACKEND_KIND_BRAILLE:
4319 chr = chr_baum_init();
4320 break;
4321#endif
4322 case CHARDEV_BACKEND_KIND_TESTDEV:
4323 chr = chr_testdev_init();
4324 break;
4325 case CHARDEV_BACKEND_KIND_STDIO:
4326 chr = qemu_chr_open_stdio(backend->stdio);
4327 break;
4328#ifdef _WIN32
4329 case CHARDEV_BACKEND_KIND_CONSOLE:
4330 chr = qemu_chr_open_win_con();
4331 break;
4332#endif
4333#ifdef CONFIG_SPICE
4334 case CHARDEV_BACKEND_KIND_SPICEVMC:
4335 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4336 break;
4337 case CHARDEV_BACKEND_KIND_SPICEPORT:
4338 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4339 break;
4340#endif
4341 case CHARDEV_BACKEND_KIND_VC:
4342 chr = vc_init(backend->vc);
4343 break;
4344 case CHARDEV_BACKEND_KIND_RINGBUF:
4345 case CHARDEV_BACKEND_KIND_MEMORY:
4346 chr = qemu_chr_open_ringbuf(backend->ringbuf, &local_err);
4347 break;
4348 default:
4349 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4350 goto out_error;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004351 }
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004352
4353 /*
4354 * Character backend open hasn't been fully converted to the Error
4355 * API. Some opens fail without setting an error. Set a generic
4356 * error then.
4357 * TODO full conversion to Error API
4358 */
4359 if (chr == NULL) {
4360 if (local_err) {
4361 error_propagate(errp, local_err);
4362 } else {
4363 error_setg(errp, "Failed to create chardev");
4364 }
4365 goto out_error;
4366 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004367 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004368
4369 chr->label = g_strdup(id);
4370 chr->avail_connections =
4371 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
4372 if (!chr->filename) {
4373 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4374 }
4375 if (!chr->explicit_be_open) {
4376 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4377 }
4378 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4379 return ret;
4380
4381out_error:
4382 g_free(ret);
4383 return NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004384}
4385
4386void qmp_chardev_remove(const char *id, Error **errp)
4387{
4388 CharDriverState *chr;
4389
4390 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004391 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004392 error_setg(errp, "Chardev '%s' not found", id);
4393 return;
4394 }
4395 if (chr->chr_can_read || chr->chr_read ||
4396 chr->chr_event || chr->handler_opaque) {
4397 error_setg(errp, "Chardev '%s' is busy", id);
4398 return;
4399 }
4400 qemu_chr_delete(chr);
4401}
Anthony Liguorid654f342013-03-05 23:21:28 +05304402
4403static void register_types(void)
4404{
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004405 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
Paolo Bonzini0d649922015-09-29 15:24:29 +02004406 qemu_chr_open_null);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004407 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004408 qemu_chr_parse_socket, qmp_chardev_open_socket);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004409 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp,
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004410 qmp_chardev_open_udp);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004411 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004412 qemu_chr_parse_ringbuf, NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004413 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004414 qemu_chr_parse_file_out, qmp_chardev_open_file);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004415 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004416 qemu_chr_parse_stdio, NULL);
Paolo Bonzini6511d392015-09-29 15:08:05 +02004417#if defined HAVE_CHARDEV_SERIAL
Peter Maydelle4d50d42014-09-02 11:24:17 +01004418 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004419 qemu_chr_parse_serial, qmp_chardev_open_serial);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004420 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004421 qemu_chr_parse_serial, qmp_chardev_open_serial);
4422#endif
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004423#ifdef HAVE_CHARDEV_PARPORT
Peter Maydelle4d50d42014-09-02 11:24:17 +01004424 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004425 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004426 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004427 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
4428#endif
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004429#ifdef HAVE_CHARDEV_PTY
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004430 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004431 qemu_chr_open_pty);
4432#endif
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004433 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL,
4434 NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004435 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004436 qemu_chr_parse_pipe, qemu_chr_open_pipe);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004437 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux,
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004438 qemu_chr_open_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004439 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004440 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004441 qemu_chr_parse_ringbuf, NULL);
Michael Roth7b7ab182013-07-30 13:04:22 -05004442 /* this must be done after machine init, since we register FEs with muxes
4443 * as part of realize functions like serial_isa_realizefn when -nographic
4444 * is specified
4445 */
4446 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304447}
4448
4449type_init(register_types);