blob: 5bacee8ff6689f1e843185a78b7107401e21d4eb [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
aliguori6f97dba2008-10-31 18:49:55 +0000685static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
686{
687 CharDriverState *chr;
688 MuxDriver *d;
689
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200690 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +0200691 d = g_new0(MuxDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000692
693 chr->opaque = d;
694 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200695 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000696 chr->chr_write = mux_chr_write;
697 chr->chr_update_read_handler = mux_chr_update_read_handler;
698 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100699 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100700 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400701 if (drv->chr_add_watch) {
702 chr->chr_add_watch = mux_chr_add_watch;
703 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500704 /* only default to opened state if we've realized the initial
705 * set of muxes
706 */
707 chr->explicit_be_open = muxes_realized ? 0 : 1;
708 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200709
aliguori6f97dba2008-10-31 18:49:55 +0000710 return chr;
711}
712
713
714#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000715int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000716{
717 int ret, len;
718
719 len = len1;
720 while (len > 0) {
721 ret = send(fd, buf, len, 0);
722 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000723 errno = WSAGetLastError();
724 if (errno != WSAEWOULDBLOCK) {
725 return -1;
726 }
727 } else if (ret == 0) {
728 break;
729 } else {
730 buf += ret;
731 len -= ret;
732 }
733 }
734 return len1 - len;
735}
736
737#else
738
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100739int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000740{
741 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100742 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000743
744 len = len1;
745 while (len > 0) {
746 ret = write(fd, buf, len);
747 if (ret < 0) {
748 if (errno != EINTR && errno != EAGAIN)
749 return -1;
750 } else if (ret == 0) {
751 break;
752 } else {
753 buf += ret;
754 len -= ret;
755 }
756 }
757 return len1 - len;
758}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500759
760int recv_all(int fd, void *_buf, int len1, bool single_read)
761{
762 int ret, len;
763 uint8_t *buf = _buf;
764
765 len = len1;
766 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
767 if (ret < 0) {
768 if (errno != EINTR && errno != EAGAIN) {
769 return -1;
770 }
771 continue;
772 } else {
773 if (single_read) {
774 return ret;
775 }
776 buf += ret;
777 len -= ret;
778 }
779 }
780 return len1 - len;
781}
782
aliguori6f97dba2008-10-31 18:49:55 +0000783#endif /* !_WIN32 */
784
Anthony Liguori96c63842013-03-05 23:21:18 +0530785typedef struct IOWatchPoll
786{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200787 GSource parent;
788
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200789 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530790 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530791
792 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200793 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530794 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530795} IOWatchPoll;
796
Anthony Liguori96c63842013-03-05 23:21:18 +0530797static IOWatchPoll *io_watch_poll_from_source(GSource *source)
798{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200799 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530800}
801
802static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
803{
804 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200805 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200806 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200807 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530808 return FALSE;
809 }
810
Paolo Bonzinid185c092013-04-05 17:59:33 +0200811 if (now_active) {
Nils Carlson4bf1cb02015-07-19 20:39:56 +0000812 iwp->src = g_io_create_watch(iwp->channel,
813 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200814 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200815 g_source_attach(iwp->src, NULL);
816 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200817 g_source_destroy(iwp->src);
818 g_source_unref(iwp->src);
819 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200820 }
821 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530822}
823
824static gboolean io_watch_poll_check(GSource *source)
825{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200826 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530827}
828
829static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
830 gpointer user_data)
831{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200832 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530833}
834
835static void io_watch_poll_finalize(GSource *source)
836{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200837 /* Due to a glib bug, removing the last reference to a source
838 * inside a finalize callback causes recursive locking (and a
839 * deadlock). This is not a problem inside other callbacks,
840 * including dispatch callbacks, so we call io_remove_watch_poll
841 * to remove this source. At this point, iwp->src must
842 * be NULL, or we would leak it.
843 *
844 * This would be solved much more elegantly by child sources,
845 * but we support older glib versions that do not have them.
846 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530847 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200848 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530849}
850
851static GSourceFuncs io_watch_poll_funcs = {
852 .prepare = io_watch_poll_prepare,
853 .check = io_watch_poll_check,
854 .dispatch = io_watch_poll_dispatch,
855 .finalize = io_watch_poll_finalize,
856};
857
858/* Can only be used for read */
859static guint io_add_watch_poll(GIOChannel *channel,
860 IOCanReadHandler *fd_can_read,
861 GIOFunc fd_read,
862 gpointer user_data)
863{
864 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200865 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530866
Paolo Bonzinid185c092013-04-05 17:59:33 +0200867 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530868 iwp->fd_can_read = fd_can_read;
869 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200870 iwp->channel = channel;
871 iwp->fd_read = (GSourceFunc) fd_read;
872 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530873
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200874 tag = g_source_attach(&iwp->parent, NULL);
875 g_source_unref(&iwp->parent);
876 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530877}
878
Paolo Bonzini2b316772013-04-19 17:32:09 +0200879static void io_remove_watch_poll(guint tag)
880{
881 GSource *source;
882 IOWatchPoll *iwp;
883
884 g_return_if_fail (tag > 0);
885
886 source = g_main_context_find_source_by_id(NULL, tag);
887 g_return_if_fail (source != NULL);
888
889 iwp = io_watch_poll_from_source(source);
890 if (iwp->src) {
891 g_source_destroy(iwp->src);
892 g_source_unref(iwp->src);
893 iwp->src = NULL;
894 }
895 g_source_destroy(&iwp->parent);
896}
897
Amit Shah26da70c2013-08-28 15:23:37 +0530898static void remove_fd_in_watch(CharDriverState *chr)
899{
900 if (chr->fd_in_tag) {
901 io_remove_watch_poll(chr->fd_in_tag);
902 chr->fd_in_tag = 0;
903 }
904}
905
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000906#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530907static GIOChannel *io_channel_from_fd(int fd)
908{
909 GIOChannel *chan;
910
911 if (fd == -1) {
912 return NULL;
913 }
914
915 chan = g_io_channel_unix_new(fd);
916
917 g_io_channel_set_encoding(chan, NULL, NULL);
918 g_io_channel_set_buffered(chan, FALSE);
919
920 return chan;
921}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000922#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530923
Anthony Liguori76a96442013-03-05 23:21:21 +0530924static GIOChannel *io_channel_from_socket(int fd)
925{
926 GIOChannel *chan;
927
928 if (fd == -1) {
929 return NULL;
930 }
931
932#ifdef _WIN32
933 chan = g_io_channel_win32_new_socket(fd);
934#else
935 chan = g_io_channel_unix_new(fd);
936#endif
937
938 g_io_channel_set_encoding(chan, NULL, NULL);
939 g_io_channel_set_buffered(chan, FALSE);
940
941 return chan;
942}
943
Anthony Liguori684a0962013-03-29 11:39:50 -0500944static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530945{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200946 size_t offset = 0;
947 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530948
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200949 while (offset < len && status == G_IO_STATUS_NORMAL) {
950 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500951
952 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530953 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500954 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530955 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500956
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200957 if (offset > 0) {
958 return offset;
959 }
960 switch (status) {
961 case G_IO_STATUS_NORMAL:
962 g_assert(len == 0);
963 return 0;
964 case G_IO_STATUS_AGAIN:
965 errno = EAGAIN;
966 return -1;
967 default:
968 break;
969 }
970 errno = EINVAL;
971 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530972}
Anthony Liguori96c63842013-03-05 23:21:18 +0530973
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000974#ifndef _WIN32
975
Anthony Liguoria29753f2013-03-05 23:21:19 +0530976typedef struct FDCharDriver {
977 CharDriverState *chr;
978 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000979 int max_size;
980} FDCharDriver;
981
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200982/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000983static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
984{
985 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530986
Anthony Liguori684a0962013-03-29 11:39:50 -0500987 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530988}
989
990static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
991{
992 CharDriverState *chr = opaque;
993 FDCharDriver *s = chr->opaque;
994 int len;
995 uint8_t buf[READ_BUF_LEN];
996 GIOStatus status;
997 gsize bytes_read;
998
999 len = sizeof(buf);
1000 if (len > s->max_size) {
1001 len = s->max_size;
1002 }
1003 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001004 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301005 }
1006
1007 status = g_io_channel_read_chars(chan, (gchar *)buf,
1008 len, &bytes_read, NULL);
1009 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301010 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301011 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1012 return FALSE;
1013 }
1014 if (status == G_IO_STATUS_NORMAL) {
1015 qemu_chr_be_write(chr, buf, bytes_read);
1016 }
1017
1018 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001019}
1020
1021static int fd_chr_read_poll(void *opaque)
1022{
1023 CharDriverState *chr = opaque;
1024 FDCharDriver *s = chr->opaque;
1025
Anthony Liguori909cda12011-08-15 11:17:31 -05001026 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001027 return s->max_size;
1028}
1029
Anthony Liguori23673ca2013-03-05 23:21:23 +05301030static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1031{
1032 FDCharDriver *s = chr->opaque;
1033 return g_io_create_watch(s->fd_out, cond);
1034}
1035
aliguori6f97dba2008-10-31 18:49:55 +00001036static void fd_chr_update_read_handler(CharDriverState *chr)
1037{
1038 FDCharDriver *s = chr->opaque;
1039
Amit Shah26da70c2013-08-28 15:23:37 +05301040 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301041 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301042 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1043 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001044 }
1045}
1046
1047static void fd_chr_close(struct CharDriverState *chr)
1048{
1049 FDCharDriver *s = chr->opaque;
1050
Amit Shah26da70c2013-08-28 15:23:37 +05301051 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301052 if (s->fd_in) {
1053 g_io_channel_unref(s->fd_in);
1054 }
1055 if (s->fd_out) {
1056 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001057 }
1058
Anthony Liguori7267c092011-08-20 22:09:37 -05001059 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001060 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001061}
1062
1063/* open a character device to a unix fd */
1064static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1065{
1066 CharDriverState *chr;
1067 FDCharDriver *s;
1068
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001069 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02001070 s = g_new0(FDCharDriver, 1);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301071 s->fd_in = io_channel_from_fd(fd_in);
1072 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001073 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301074 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001075 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301076 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001077 chr->chr_write = fd_chr_write;
1078 chr->chr_update_read_handler = fd_chr_update_read_handler;
1079 chr->chr_close = fd_chr_close;
1080
aliguori6f97dba2008-10-31 18:49:55 +00001081 return chr;
1082}
1083
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001084static CharDriverState *qemu_chr_open_pipe(const char *id,
1085 ChardevBackend *backend,
1086 ChardevReturn *ret,
1087 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001088{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001089 ChardevHostdev *opts = backend->pipe;
aliguori6f97dba2008-10-31 18:49:55 +00001090 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001091 char filename_in[CHR_MAX_FILENAME_SIZE];
1092 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001093 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001094
Corey Minyard9f781162014-10-02 11:17:33 -05001095 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1096 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001097 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1098 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001099 if (fd_in < 0 || fd_out < 0) {
1100 if (fd_in >= 0)
1101 close(fd_in);
1102 if (fd_out >= 0)
1103 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001104 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001105 if (fd_in < 0) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001106 error_setg_file_open(errp, errno, filename);
Markus Armbruster1f514702012-02-07 15:09:08 +01001107 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001108 }
aliguori6f97dba2008-10-31 18:49:55 +00001109 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001110 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001111}
1112
aliguori6f97dba2008-10-31 18:49:55 +00001113/* init terminal so that we can grab keys */
1114static struct termios oldtty;
1115static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001116static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001117static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001118static bool stdio_echo_state;
1119
1120static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001121
1122static void term_exit(void)
1123{
1124 tcsetattr (0, TCSANOW, &oldtty);
1125 fcntl(0, F_SETFL, old_fd0_flags);
1126}
1127
Gal Hammere76d4422015-01-07 10:38:35 +02001128static void term_stdio_handler(int sig)
1129{
1130 /* restore echo after resume from suspend. */
1131 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1132}
1133
Paolo Bonzinibb002512010-12-23 13:42:50 +01001134static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001135{
1136 struct termios tty;
1137
Gal Hammere76d4422015-01-07 10:38:35 +02001138 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001139 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001140 if (!echo) {
1141 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001142 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001143 tty.c_oflag |= OPOST;
1144 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1145 tty.c_cflag &= ~(CSIZE|PARENB);
1146 tty.c_cflag |= CS8;
1147 tty.c_cc[VMIN] = 1;
1148 tty.c_cc[VTIME] = 0;
1149 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001150 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001151 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001152
1153 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001154}
1155
1156static void qemu_chr_close_stdio(struct CharDriverState *chr)
1157{
1158 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001159 fd_chr_close(chr);
1160}
1161
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001162static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001163{
1164 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001165 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001166
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001167 if (is_daemonized()) {
1168 error_report("cannot use stdio with -daemonize");
1169 return NULL;
1170 }
Li Liuc88930a2014-09-09 19:19:48 +08001171
1172 if (stdio_in_use) {
1173 error_report("cannot use stdio by multiple character devices");
1174 exit(1);
1175 }
1176
1177 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301178 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001179 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001180 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301181 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001182
Gal Hammere76d4422015-01-07 10:38:35 +02001183 memset(&act, 0, sizeof(act));
1184 act.sa_handler = term_stdio_handler;
1185 sigaction(SIGCONT, &act, NULL);
1186
aliguori6f97dba2008-10-31 18:49:55 +00001187 chr = qemu_chr_open_fd(0, 1);
1188 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001189 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001190 if (opts->has_signal) {
1191 stdio_allow_signal = opts->signal;
1192 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001193 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001194
Markus Armbruster1f514702012-02-07 15:09:08 +01001195 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001196}
1197
aliguori6f97dba2008-10-31 18:49:55 +00001198#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001199 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1200 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001201
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001202#define HAVE_CHARDEV_SERIAL 1
1203#define HAVE_CHARDEV_PTY 1
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001204
aliguori6f97dba2008-10-31 18:49:55 +00001205typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301206 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001207 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001208
1209 /* Protected by the CharDriverState chr_write_lock. */
1210 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301211 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001212 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001213} PtyCharDriver;
1214
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001215static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001216static void pty_chr_state(CharDriverState *chr, int connected);
1217
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301218static gboolean pty_chr_timer(gpointer opaque)
1219{
1220 struct CharDriverState *chr = opaque;
1221 PtyCharDriver *s = chr->opaque;
1222
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001223 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001224 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001225 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001226 if (!s->connected) {
1227 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001228 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001229 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001230 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301231 return FALSE;
1232}
1233
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001234/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301235static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1236{
1237 PtyCharDriver *s = chr->opaque;
1238
1239 if (s->timer_tag) {
1240 g_source_remove(s->timer_tag);
1241 s->timer_tag = 0;
1242 }
1243
1244 if (ms == 1000) {
1245 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1246 } else {
1247 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1248 }
1249}
1250
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001251/* Called with chr_write_lock held. */
1252static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001253{
1254 PtyCharDriver *s = chr->opaque;
1255 GPollFD pfd;
1256
1257 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1258 pfd.events = G_IO_OUT;
1259 pfd.revents = 0;
1260 g_poll(&pfd, 1, 0);
1261 if (pfd.revents & G_IO_HUP) {
1262 pty_chr_state(chr, 0);
1263 } else {
1264 pty_chr_state(chr, 1);
1265 }
1266}
1267
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001268static void pty_chr_update_read_handler(CharDriverState *chr)
1269{
1270 qemu_mutex_lock(&chr->chr_write_lock);
1271 pty_chr_update_read_handler_locked(chr);
1272 qemu_mutex_unlock(&chr->chr_write_lock);
1273}
1274
1275/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001276static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1277{
1278 PtyCharDriver *s = chr->opaque;
1279
1280 if (!s->connected) {
1281 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001282 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001283 if (!s->connected) {
1284 return 0;
1285 }
aliguori6f97dba2008-10-31 18:49:55 +00001286 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001287 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001288}
1289
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301290static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1291{
1292 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001293 if (!s->connected) {
1294 return NULL;
1295 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301296 return g_io_create_watch(s->fd, cond);
1297}
1298
aliguori6f97dba2008-10-31 18:49:55 +00001299static int pty_chr_read_poll(void *opaque)
1300{
1301 CharDriverState *chr = opaque;
1302 PtyCharDriver *s = chr->opaque;
1303
Anthony Liguori909cda12011-08-15 11:17:31 -05001304 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001305 return s->read_bytes;
1306}
1307
Anthony Liguori093d3a22013-03-05 23:21:20 +05301308static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001309{
1310 CharDriverState *chr = opaque;
1311 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301312 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301313 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301314 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001315
1316 len = sizeof(buf);
1317 if (len > s->read_bytes)
1318 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001319 if (len == 0) {
1320 return TRUE;
1321 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301322 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1323 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001324 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301325 return FALSE;
1326 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001327 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001328 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001329 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301330 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001331}
1332
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001333static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1334{
1335 CharDriverState *chr = opaque;
1336 PtyCharDriver *s = chr->opaque;
1337
1338 s->open_tag = 0;
1339 qemu_chr_be_generic_open(chr);
1340 return FALSE;
1341}
1342
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001343/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001344static void pty_chr_state(CharDriverState *chr, int connected)
1345{
1346 PtyCharDriver *s = chr->opaque;
1347
1348 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001349 if (s->open_tag) {
1350 g_source_remove(s->open_tag);
1351 s->open_tag = 0;
1352 }
Amit Shah26da70c2013-08-28 15:23:37 +05301353 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001354 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001355 /* (re-)connect poll interval for idle guests: once per second.
1356 * We check more frequently in case the guests sends data to
1357 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301358 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001359 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001360 if (s->timer_tag) {
1361 g_source_remove(s->timer_tag);
1362 s->timer_tag = 0;
1363 }
1364 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001365 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001366 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001367 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001368 }
1369 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301370 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1371 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001372 }
aliguori6f97dba2008-10-31 18:49:55 +00001373 }
1374}
1375
aliguori6f97dba2008-10-31 18:49:55 +00001376static void pty_chr_close(struct CharDriverState *chr)
1377{
1378 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301379 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001380
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001381 qemu_mutex_lock(&chr->chr_write_lock);
1382 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301383 fd = g_io_channel_unix_get_fd(s->fd);
1384 g_io_channel_unref(s->fd);
1385 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301386 if (s->timer_tag) {
1387 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001388 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301389 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001390 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001391 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001392 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001393}
1394
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001395static CharDriverState *qemu_chr_open_pty(const char *id,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001396 ChardevBackend *backend,
1397 ChardevReturn *ret,
1398 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001399{
1400 CharDriverState *chr;
1401 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001402 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001403 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001404
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001405 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1406 if (master_fd < 0) {
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001407 error_setg_errno(errp, errno, "Failed to create PTY");
Markus Armbruster1f514702012-02-07 15:09:08 +01001408 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001409 }
1410
aliguori6f97dba2008-10-31 18:49:55 +00001411 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001412 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001413
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001414 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001415
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001416 chr->filename = g_strdup_printf("pty:%s", pty_name);
1417 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001418 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001419
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001420 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001421 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001422
Markus Armbruster2d528d42015-09-14 13:54:03 +02001423 s = g_new0(PtyCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001424 chr->opaque = s;
1425 chr->chr_write = pty_chr_write;
1426 chr->chr_update_read_handler = pty_chr_update_read_handler;
1427 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301428 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001429 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001430
Anthony Liguori093d3a22013-03-05 23:21:20 +05301431 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301432 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001433
Markus Armbruster1f514702012-02-07 15:09:08 +01001434 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001435}
1436
1437static void tty_serial_init(int fd, int speed,
1438 int parity, int data_bits, int stop_bits)
1439{
1440 struct termios tty;
1441 speed_t spd;
1442
1443#if 0
1444 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1445 speed, parity, data_bits, stop_bits);
1446#endif
1447 tcgetattr (fd, &tty);
1448
Stefan Weil45eea132009-10-26 16:10:10 +01001449#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1450 speed = speed * 10 / 11;
1451 do {
1452 check_speed(50);
1453 check_speed(75);
1454 check_speed(110);
1455 check_speed(134);
1456 check_speed(150);
1457 check_speed(200);
1458 check_speed(300);
1459 check_speed(600);
1460 check_speed(1200);
1461 check_speed(1800);
1462 check_speed(2400);
1463 check_speed(4800);
1464 check_speed(9600);
1465 check_speed(19200);
1466 check_speed(38400);
1467 /* Non-Posix values follow. They may be unsupported on some systems. */
1468 check_speed(57600);
1469 check_speed(115200);
1470#ifdef B230400
1471 check_speed(230400);
1472#endif
1473#ifdef B460800
1474 check_speed(460800);
1475#endif
1476#ifdef B500000
1477 check_speed(500000);
1478#endif
1479#ifdef B576000
1480 check_speed(576000);
1481#endif
1482#ifdef B921600
1483 check_speed(921600);
1484#endif
1485#ifdef B1000000
1486 check_speed(1000000);
1487#endif
1488#ifdef B1152000
1489 check_speed(1152000);
1490#endif
1491#ifdef B1500000
1492 check_speed(1500000);
1493#endif
1494#ifdef B2000000
1495 check_speed(2000000);
1496#endif
1497#ifdef B2500000
1498 check_speed(2500000);
1499#endif
1500#ifdef B3000000
1501 check_speed(3000000);
1502#endif
1503#ifdef B3500000
1504 check_speed(3500000);
1505#endif
1506#ifdef B4000000
1507 check_speed(4000000);
1508#endif
aliguori6f97dba2008-10-31 18:49:55 +00001509 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001510 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001511
1512 cfsetispeed(&tty, spd);
1513 cfsetospeed(&tty, spd);
1514
1515 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1516 |INLCR|IGNCR|ICRNL|IXON);
1517 tty.c_oflag |= OPOST;
1518 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1519 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1520 switch(data_bits) {
1521 default:
1522 case 8:
1523 tty.c_cflag |= CS8;
1524 break;
1525 case 7:
1526 tty.c_cflag |= CS7;
1527 break;
1528 case 6:
1529 tty.c_cflag |= CS6;
1530 break;
1531 case 5:
1532 tty.c_cflag |= CS5;
1533 break;
1534 }
1535 switch(parity) {
1536 default:
1537 case 'N':
1538 break;
1539 case 'E':
1540 tty.c_cflag |= PARENB;
1541 break;
1542 case 'O':
1543 tty.c_cflag |= PARENB | PARODD;
1544 break;
1545 }
1546 if (stop_bits == 2)
1547 tty.c_cflag |= CSTOPB;
1548
1549 tcsetattr (fd, TCSANOW, &tty);
1550}
1551
1552static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1553{
1554 FDCharDriver *s = chr->opaque;
1555
1556 switch(cmd) {
1557 case CHR_IOCTL_SERIAL_SET_PARAMS:
1558 {
1559 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301560 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1561 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001562 ssp->data_bits, ssp->stop_bits);
1563 }
1564 break;
1565 case CHR_IOCTL_SERIAL_SET_BREAK:
1566 {
1567 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301568 if (enable) {
1569 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1570 }
aliguori6f97dba2008-10-31 18:49:55 +00001571 }
1572 break;
1573 case CHR_IOCTL_SERIAL_GET_TIOCM:
1574 {
1575 int sarg = 0;
1576 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301577 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001578 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001579 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001580 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001581 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001582 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001583 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001584 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001585 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001586 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001587 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001588 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001589 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001590 *targ |= CHR_TIOCM_RTS;
1591 }
1592 break;
1593 case CHR_IOCTL_SERIAL_SET_TIOCM:
1594 {
1595 int sarg = *(int *)arg;
1596 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301597 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001598 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1599 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1600 if (sarg & CHR_TIOCM_CTS)
1601 targ |= TIOCM_CTS;
1602 if (sarg & CHR_TIOCM_CAR)
1603 targ |= TIOCM_CAR;
1604 if (sarg & CHR_TIOCM_DSR)
1605 targ |= TIOCM_DSR;
1606 if (sarg & CHR_TIOCM_RI)
1607 targ |= TIOCM_RI;
1608 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001609 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001610 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001611 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301612 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001613 }
1614 break;
1615 default:
1616 return -ENOTSUP;
1617 }
1618 return 0;
1619}
1620
David Ahern4266a132010-02-10 18:27:17 -07001621static void qemu_chr_close_tty(CharDriverState *chr)
1622{
1623 FDCharDriver *s = chr->opaque;
1624 int fd = -1;
1625
1626 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301627 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001628 }
1629
1630 fd_chr_close(chr);
1631
1632 if (fd >= 0) {
1633 close(fd);
1634 }
1635}
1636
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001637static CharDriverState *qemu_chr_open_tty_fd(int fd)
1638{
1639 CharDriverState *chr;
1640
1641 tty_serial_init(fd, 115200, 'N', 8, 1);
1642 chr = qemu_chr_open_fd(fd, fd);
1643 chr->chr_ioctl = tty_serial_ioctl;
1644 chr->chr_close = qemu_chr_close_tty;
1645 return chr;
1646}
aliguori6f97dba2008-10-31 18:49:55 +00001647#endif /* __linux__ || __sun__ */
1648
1649#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001650
1651#define HAVE_CHARDEV_PARPORT 1
1652
aliguori6f97dba2008-10-31 18:49:55 +00001653typedef struct {
1654 int fd;
1655 int mode;
1656} ParallelCharDriver;
1657
1658static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1659{
1660 if (s->mode != mode) {
1661 int m = mode;
1662 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1663 return 0;
1664 s->mode = mode;
1665 }
1666 return 1;
1667}
1668
1669static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1670{
1671 ParallelCharDriver *drv = chr->opaque;
1672 int fd = drv->fd;
1673 uint8_t b;
1674
1675 switch(cmd) {
1676 case CHR_IOCTL_PP_READ_DATA:
1677 if (ioctl(fd, PPRDATA, &b) < 0)
1678 return -ENOTSUP;
1679 *(uint8_t *)arg = b;
1680 break;
1681 case CHR_IOCTL_PP_WRITE_DATA:
1682 b = *(uint8_t *)arg;
1683 if (ioctl(fd, PPWDATA, &b) < 0)
1684 return -ENOTSUP;
1685 break;
1686 case CHR_IOCTL_PP_READ_CONTROL:
1687 if (ioctl(fd, PPRCONTROL, &b) < 0)
1688 return -ENOTSUP;
1689 /* Linux gives only the lowest bits, and no way to know data
1690 direction! For better compatibility set the fixed upper
1691 bits. */
1692 *(uint8_t *)arg = b | 0xc0;
1693 break;
1694 case CHR_IOCTL_PP_WRITE_CONTROL:
1695 b = *(uint8_t *)arg;
1696 if (ioctl(fd, PPWCONTROL, &b) < 0)
1697 return -ENOTSUP;
1698 break;
1699 case CHR_IOCTL_PP_READ_STATUS:
1700 if (ioctl(fd, PPRSTATUS, &b) < 0)
1701 return -ENOTSUP;
1702 *(uint8_t *)arg = b;
1703 break;
1704 case CHR_IOCTL_PP_DATA_DIR:
1705 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1706 return -ENOTSUP;
1707 break;
1708 case CHR_IOCTL_PP_EPP_READ_ADDR:
1709 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1710 struct ParallelIOArg *parg = arg;
1711 int n = read(fd, parg->buffer, parg->count);
1712 if (n != parg->count) {
1713 return -EIO;
1714 }
1715 }
1716 break;
1717 case CHR_IOCTL_PP_EPP_READ:
1718 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
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_WRITE_ADDR:
1727 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1728 struct ParallelIOArg *parg = arg;
1729 int n = write(fd, parg->buffer, parg->count);
1730 if (n != parg->count) {
1731 return -EIO;
1732 }
1733 }
1734 break;
1735 case CHR_IOCTL_PP_EPP_WRITE:
1736 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
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 default:
1745 return -ENOTSUP;
1746 }
1747 return 0;
1748}
1749
1750static void pp_close(CharDriverState *chr)
1751{
1752 ParallelCharDriver *drv = chr->opaque;
1753 int fd = drv->fd;
1754
1755 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1756 ioctl(fd, PPRELEASE);
1757 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001758 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001759 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001760}
1761
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001762static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001763{
1764 CharDriverState *chr;
1765 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001766
1767 if (ioctl(fd, PPCLAIM) < 0) {
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001768 error_setg_errno(errp, errno, "not a parallel port");
aliguori6f97dba2008-10-31 18:49:55 +00001769 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001770 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001771 }
1772
Markus Armbruster2d528d42015-09-14 13:54:03 +02001773 drv = g_new0(ParallelCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001774 drv->fd = fd;
1775 drv->mode = IEEE1284_MODE_COMPAT;
1776
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001777 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001778 chr->chr_write = null_chr_write;
1779 chr->chr_ioctl = pp_ioctl;
1780 chr->chr_close = pp_close;
1781 chr->opaque = drv;
1782
Markus Armbruster1f514702012-02-07 15:09:08 +01001783 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001784}
1785#endif /* __linux__ */
1786
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001787#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001788
1789#define HAVE_CHARDEV_PARPORT 1
1790
blueswir16972f932008-11-22 20:49:12 +00001791static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1792{
Stefan Weile0efb992011-02-23 19:09:16 +01001793 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001794 uint8_t b;
1795
1796 switch(cmd) {
1797 case CHR_IOCTL_PP_READ_DATA:
1798 if (ioctl(fd, PPIGDATA, &b) < 0)
1799 return -ENOTSUP;
1800 *(uint8_t *)arg = b;
1801 break;
1802 case CHR_IOCTL_PP_WRITE_DATA:
1803 b = *(uint8_t *)arg;
1804 if (ioctl(fd, PPISDATA, &b) < 0)
1805 return -ENOTSUP;
1806 break;
1807 case CHR_IOCTL_PP_READ_CONTROL:
1808 if (ioctl(fd, PPIGCTRL, &b) < 0)
1809 return -ENOTSUP;
1810 *(uint8_t *)arg = b;
1811 break;
1812 case CHR_IOCTL_PP_WRITE_CONTROL:
1813 b = *(uint8_t *)arg;
1814 if (ioctl(fd, PPISCTRL, &b) < 0)
1815 return -ENOTSUP;
1816 break;
1817 case CHR_IOCTL_PP_READ_STATUS:
1818 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1819 return -ENOTSUP;
1820 *(uint8_t *)arg = b;
1821 break;
1822 default:
1823 return -ENOTSUP;
1824 }
1825 return 0;
1826}
1827
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001828static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
blueswir16972f932008-11-22 20:49:12 +00001829{
1830 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001831
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001832 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001833 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001834 chr->chr_write = null_chr_write;
1835 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001836 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001837 return chr;
blueswir16972f932008-11-22 20:49:12 +00001838}
1839#endif
1840
aliguori6f97dba2008-10-31 18:49:55 +00001841#else /* _WIN32 */
1842
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001843#define HAVE_CHARDEV_SERIAL 1
1844
aliguori6f97dba2008-10-31 18:49:55 +00001845typedef struct {
1846 int max_size;
1847 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001848 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001849 BOOL fpipe;
1850 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001851
1852 /* Protected by the CharDriverState chr_write_lock. */
1853 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001854} WinCharState;
1855
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001856typedef struct {
1857 HANDLE hStdIn;
1858 HANDLE hInputReadyEvent;
1859 HANDLE hInputDoneEvent;
1860 HANDLE hInputThread;
1861 uint8_t win_stdio_buf;
1862} WinStdioCharState;
1863
aliguori6f97dba2008-10-31 18:49:55 +00001864#define NSENDBUF 2048
1865#define NRECVBUF 2048
1866#define MAXCONNECT 1
1867#define NTIMEOUT 5000
1868
1869static int win_chr_poll(void *opaque);
1870static int win_chr_pipe_poll(void *opaque);
1871
1872static void win_chr_close(CharDriverState *chr)
1873{
1874 WinCharState *s = chr->opaque;
1875
1876 if (s->hsend) {
1877 CloseHandle(s->hsend);
1878 s->hsend = NULL;
1879 }
1880 if (s->hrecv) {
1881 CloseHandle(s->hrecv);
1882 s->hrecv = NULL;
1883 }
1884 if (s->hcom) {
1885 CloseHandle(s->hcom);
1886 s->hcom = NULL;
1887 }
1888 if (s->fpipe)
1889 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1890 else
1891 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301892
Hans de Goedea425d232011-11-19 10:22:43 +01001893 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001894}
1895
Paolo Bonzini6511d392015-09-29 15:08:05 +02001896static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001897{
1898 WinCharState *s = chr->opaque;
1899 COMMCONFIG comcfg;
1900 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1901 COMSTAT comstat;
1902 DWORD size;
1903 DWORD err;
1904
1905 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1906 if (!s->hsend) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001907 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001908 goto fail;
1909 }
1910 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1911 if (!s->hrecv) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001912 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001913 goto fail;
1914 }
1915
1916 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1917 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1918 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001919 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00001920 s->hcom = NULL;
1921 goto fail;
1922 }
1923
1924 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001925 error_setg(errp, "Failed SetupComm");
aliguori6f97dba2008-10-31 18:49:55 +00001926 goto fail;
1927 }
1928
1929 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1930 size = sizeof(COMMCONFIG);
1931 GetDefaultCommConfig(filename, &comcfg, &size);
1932 comcfg.dcb.DCBlength = sizeof(DCB);
1933 CommConfigDialog(filename, NULL, &comcfg);
1934
1935 if (!SetCommState(s->hcom, &comcfg.dcb)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001936 error_setg(errp, "Failed SetCommState");
aliguori6f97dba2008-10-31 18:49:55 +00001937 goto fail;
1938 }
1939
1940 if (!SetCommMask(s->hcom, EV_ERR)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001941 error_setg(errp, "Failed SetCommMask");
aliguori6f97dba2008-10-31 18:49:55 +00001942 goto fail;
1943 }
1944
1945 cto.ReadIntervalTimeout = MAXDWORD;
1946 if (!SetCommTimeouts(s->hcom, &cto)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001947 error_setg(errp, "Failed SetCommTimeouts");
aliguori6f97dba2008-10-31 18:49:55 +00001948 goto fail;
1949 }
1950
1951 if (!ClearCommError(s->hcom, &err, &comstat)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001952 error_setg(errp, "Failed ClearCommError");
aliguori6f97dba2008-10-31 18:49:55 +00001953 goto fail;
1954 }
1955 qemu_add_polling_cb(win_chr_poll, chr);
1956 return 0;
1957
1958 fail:
1959 win_chr_close(chr);
1960 return -1;
1961}
1962
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001963/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001964static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1965{
1966 WinCharState *s = chr->opaque;
1967 DWORD len, ret, size, err;
1968
1969 len = len1;
1970 ZeroMemory(&s->osend, sizeof(s->osend));
1971 s->osend.hEvent = s->hsend;
1972 while (len > 0) {
1973 if (s->hsend)
1974 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1975 else
1976 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1977 if (!ret) {
1978 err = GetLastError();
1979 if (err == ERROR_IO_PENDING) {
1980 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1981 if (ret) {
1982 buf += size;
1983 len -= size;
1984 } else {
1985 break;
1986 }
1987 } else {
1988 break;
1989 }
1990 } else {
1991 buf += size;
1992 len -= size;
1993 }
1994 }
1995 return len1 - len;
1996}
1997
1998static int win_chr_read_poll(CharDriverState *chr)
1999{
2000 WinCharState *s = chr->opaque;
2001
Anthony Liguori909cda12011-08-15 11:17:31 -05002002 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002003 return s->max_size;
2004}
2005
2006static void win_chr_readfile(CharDriverState *chr)
2007{
2008 WinCharState *s = chr->opaque;
2009 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302010 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002011 DWORD size;
2012
2013 ZeroMemory(&s->orecv, sizeof(s->orecv));
2014 s->orecv.hEvent = s->hrecv;
2015 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2016 if (!ret) {
2017 err = GetLastError();
2018 if (err == ERROR_IO_PENDING) {
2019 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2020 }
2021 }
2022
2023 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002024 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002025 }
2026}
2027
2028static void win_chr_read(CharDriverState *chr)
2029{
2030 WinCharState *s = chr->opaque;
2031
2032 if (s->len > s->max_size)
2033 s->len = s->max_size;
2034 if (s->len == 0)
2035 return;
2036
2037 win_chr_readfile(chr);
2038}
2039
2040static int win_chr_poll(void *opaque)
2041{
2042 CharDriverState *chr = opaque;
2043 WinCharState *s = chr->opaque;
2044 COMSTAT status;
2045 DWORD comerr;
2046
2047 ClearCommError(s->hcom, &comerr, &status);
2048 if (status.cbInQue > 0) {
2049 s->len = status.cbInQue;
2050 win_chr_read_poll(chr);
2051 win_chr_read(chr);
2052 return 1;
2053 }
2054 return 0;
2055}
2056
Paolo Bonzini6511d392015-09-29 15:08:05 +02002057static CharDriverState *qemu_chr_open_win_path(const char *filename,
2058 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002059{
2060 CharDriverState *chr;
2061 WinCharState *s;
2062
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002063 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002064 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002065 chr->opaque = s;
2066 chr->chr_write = win_chr_write;
2067 chr->chr_close = win_chr_close;
2068
Paolo Bonzini6511d392015-09-29 15:08:05 +02002069 if (win_chr_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002070 g_free(s);
2071 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002072 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002073 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002074 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002075}
2076
2077static int win_chr_pipe_poll(void *opaque)
2078{
2079 CharDriverState *chr = opaque;
2080 WinCharState *s = chr->opaque;
2081 DWORD size;
2082
2083 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2084 if (size > 0) {
2085 s->len = size;
2086 win_chr_read_poll(chr);
2087 win_chr_read(chr);
2088 return 1;
2089 }
2090 return 0;
2091}
2092
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002093static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
2094 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002095{
2096 WinCharState *s = chr->opaque;
2097 OVERLAPPED ov;
2098 int ret;
2099 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002100 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002101
2102 s->fpipe = TRUE;
2103
2104 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2105 if (!s->hsend) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002106 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002107 goto fail;
2108 }
2109 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2110 if (!s->hrecv) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002111 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002112 goto fail;
2113 }
2114
2115 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2116 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2117 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2118 PIPE_WAIT,
2119 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2120 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002121 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00002122 s->hcom = NULL;
2123 goto fail;
2124 }
2125
2126 ZeroMemory(&ov, sizeof(ov));
2127 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2128 ret = ConnectNamedPipe(s->hcom, &ov);
2129 if (ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002130 error_setg(errp, "Failed ConnectNamedPipe");
aliguori6f97dba2008-10-31 18:49:55 +00002131 goto fail;
2132 }
2133
2134 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2135 if (!ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002136 error_setg(errp, "Failed GetOverlappedResult");
aliguori6f97dba2008-10-31 18:49:55 +00002137 if (ov.hEvent) {
2138 CloseHandle(ov.hEvent);
2139 ov.hEvent = NULL;
2140 }
2141 goto fail;
2142 }
2143
2144 if (ov.hEvent) {
2145 CloseHandle(ov.hEvent);
2146 ov.hEvent = NULL;
2147 }
2148 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2149 return 0;
2150
2151 fail:
2152 win_chr_close(chr);
2153 return -1;
2154}
2155
2156
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002157static CharDriverState *qemu_chr_open_pipe(const char *id,
2158 ChardevBackend *backend,
2159 ChardevReturn *ret,
2160 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002161{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002162 ChardevHostdev *opts = backend->pipe;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002163 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002164 CharDriverState *chr;
2165 WinCharState *s;
2166
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002167 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002168 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002169 chr->opaque = s;
2170 chr->chr_write = win_chr_write;
2171 chr->chr_close = win_chr_close;
2172
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002173 if (win_chr_pipe_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002174 g_free(s);
2175 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002176 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002177 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002178 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002179}
2180
Markus Armbruster1f514702012-02-07 15:09:08 +01002181static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002182{
2183 CharDriverState *chr;
2184 WinCharState *s;
2185
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002186 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002187 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002188 s->hcom = fd_out;
2189 chr->opaque = s;
2190 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002191 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002192}
2193
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002194static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002195{
Markus Armbruster1f514702012-02-07 15:09:08 +01002196 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002197}
2198
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002199static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2200{
2201 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2202 DWORD dwSize;
2203 int len1;
2204
2205 len1 = len;
2206
2207 while (len1 > 0) {
2208 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2209 break;
2210 }
2211 buf += dwSize;
2212 len1 -= dwSize;
2213 }
2214
2215 return len - len1;
2216}
2217
2218static void win_stdio_wait_func(void *opaque)
2219{
2220 CharDriverState *chr = opaque;
2221 WinStdioCharState *stdio = chr->opaque;
2222 INPUT_RECORD buf[4];
2223 int ret;
2224 DWORD dwSize;
2225 int i;
2226
Stefan Weildff74242013-12-07 14:48:04 +01002227 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002228
2229 if (!ret) {
2230 /* Avoid error storm */
2231 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2232 return;
2233 }
2234
2235 for (i = 0; i < dwSize; i++) {
2236 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2237
2238 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2239 int j;
2240 if (kev->uChar.AsciiChar != 0) {
2241 for (j = 0; j < kev->wRepeatCount; j++) {
2242 if (qemu_chr_be_can_write(chr)) {
2243 uint8_t c = kev->uChar.AsciiChar;
2244 qemu_chr_be_write(chr, &c, 1);
2245 }
2246 }
2247 }
2248 }
2249 }
2250}
2251
2252static DWORD WINAPI win_stdio_thread(LPVOID param)
2253{
2254 CharDriverState *chr = param;
2255 WinStdioCharState *stdio = chr->opaque;
2256 int ret;
2257 DWORD dwSize;
2258
2259 while (1) {
2260
2261 /* Wait for one byte */
2262 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2263
2264 /* Exit in case of error, continue if nothing read */
2265 if (!ret) {
2266 break;
2267 }
2268 if (!dwSize) {
2269 continue;
2270 }
2271
2272 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2273 if (stdio->win_stdio_buf == '\r') {
2274 continue;
2275 }
2276
2277 /* Signal the main thread and wait until the byte was eaten */
2278 if (!SetEvent(stdio->hInputReadyEvent)) {
2279 break;
2280 }
2281 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2282 != WAIT_OBJECT_0) {
2283 break;
2284 }
2285 }
2286
2287 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2288 return 0;
2289}
2290
2291static void win_stdio_thread_wait_func(void *opaque)
2292{
2293 CharDriverState *chr = opaque;
2294 WinStdioCharState *stdio = chr->opaque;
2295
2296 if (qemu_chr_be_can_write(chr)) {
2297 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2298 }
2299
2300 SetEvent(stdio->hInputDoneEvent);
2301}
2302
2303static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2304{
2305 WinStdioCharState *stdio = chr->opaque;
2306 DWORD dwMode = 0;
2307
2308 GetConsoleMode(stdio->hStdIn, &dwMode);
2309
2310 if (echo) {
2311 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2312 } else {
2313 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2314 }
2315}
2316
2317static void win_stdio_close(CharDriverState *chr)
2318{
2319 WinStdioCharState *stdio = chr->opaque;
2320
2321 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2322 CloseHandle(stdio->hInputReadyEvent);
2323 }
2324 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2325 CloseHandle(stdio->hInputDoneEvent);
2326 }
2327 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2328 TerminateThread(stdio->hInputThread, 0);
2329 }
2330
2331 g_free(chr->opaque);
2332 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002333}
2334
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002335static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002336{
2337 CharDriverState *chr;
2338 WinStdioCharState *stdio;
2339 DWORD dwMode;
2340 int is_console = 0;
2341
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002342 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002343 stdio = g_new0(WinStdioCharState, 1);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002344
2345 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2346 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2347 fprintf(stderr, "cannot open stdio: invalid handle\n");
2348 exit(1);
2349 }
2350
2351 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2352
2353 chr->opaque = stdio;
2354 chr->chr_write = win_stdio_write;
2355 chr->chr_close = win_stdio_close;
2356
Anthony Liguoried7a1542013-03-05 23:21:17 +05302357 if (is_console) {
2358 if (qemu_add_wait_object(stdio->hStdIn,
2359 win_stdio_wait_func, chr)) {
2360 fprintf(stderr, "qemu_add_wait_object: failed\n");
2361 }
2362 } else {
2363 DWORD dwId;
2364
2365 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2366 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2367 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2368 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002369
Anthony Liguoried7a1542013-03-05 23:21:17 +05302370 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2371 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2372 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2373 fprintf(stderr, "cannot create stdio thread or event\n");
2374 exit(1);
2375 }
2376 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2377 win_stdio_thread_wait_func, chr)) {
2378 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002379 }
2380 }
2381
2382 dwMode |= ENABLE_LINE_INPUT;
2383
Anthony Liguoried7a1542013-03-05 23:21:17 +05302384 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002385 /* set the terminal in raw mode */
2386 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2387 dwMode |= ENABLE_PROCESSED_INPUT;
2388 }
2389
2390 SetConsoleMode(stdio->hStdIn, dwMode);
2391
2392 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2393 qemu_chr_fe_set_echo(chr, false);
2394
Markus Armbruster1f514702012-02-07 15:09:08 +01002395 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002396}
aliguori6f97dba2008-10-31 18:49:55 +00002397#endif /* !_WIN32 */
2398
Anthony Liguori5ab82112013-03-05 23:21:31 +05302399
aliguori6f97dba2008-10-31 18:49:55 +00002400/***********************************************************/
2401/* UDP Net console */
2402
2403typedef struct {
2404 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302405 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302406 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002407 int bufcnt;
2408 int bufptr;
2409 int max_size;
2410} NetCharDriver;
2411
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002412/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002413static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2414{
2415 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302416 gsize bytes_written;
2417 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002418
Anthony Liguori76a96442013-03-05 23:21:21 +05302419 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2420 if (status == G_IO_STATUS_EOF) {
2421 return 0;
2422 } else if (status != G_IO_STATUS_NORMAL) {
2423 return -1;
2424 }
2425
2426 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002427}
2428
2429static int udp_chr_read_poll(void *opaque)
2430{
2431 CharDriverState *chr = opaque;
2432 NetCharDriver *s = chr->opaque;
2433
Anthony Liguori909cda12011-08-15 11:17:31 -05002434 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002435
2436 /* If there were any stray characters in the queue process them
2437 * first
2438 */
2439 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002440 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002441 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002442 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002443 }
2444 return s->max_size;
2445}
2446
Anthony Liguori76a96442013-03-05 23:21:21 +05302447static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002448{
2449 CharDriverState *chr = opaque;
2450 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302451 gsize bytes_read = 0;
2452 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002453
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002454 if (s->max_size == 0) {
2455 return TRUE;
2456 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302457 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2458 &bytes_read, NULL);
2459 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002460 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302461 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302462 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302463 return FALSE;
2464 }
aliguori6f97dba2008-10-31 18:49:55 +00002465
2466 s->bufptr = 0;
2467 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002468 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002469 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002470 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002471 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302472
2473 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002474}
2475
2476static void udp_chr_update_read_handler(CharDriverState *chr)
2477{
2478 NetCharDriver *s = chr->opaque;
2479
Amit Shah26da70c2013-08-28 15:23:37 +05302480 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302481 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302482 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2483 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002484 }
2485}
2486
aliguori819f56b2009-03-28 17:58:14 +00002487static void udp_chr_close(CharDriverState *chr)
2488{
2489 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302490
2491 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302492 if (s->chan) {
2493 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002494 closesocket(s->fd);
2495 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002496 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002497 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002498}
2499
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002500static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002501{
2502 CharDriverState *chr = NULL;
2503 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002504
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002505 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002506 s = g_new0(NetCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002507
aliguori6f97dba2008-10-31 18:49:55 +00002508 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302509 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002510 s->bufcnt = 0;
2511 s->bufptr = 0;
2512 chr->opaque = s;
2513 chr->chr_write = udp_chr_write;
2514 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002515 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002516 /* be isn't opened until we get a connection */
2517 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002518 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002519}
aliguori6f97dba2008-10-31 18:49:55 +00002520
aliguori6f97dba2008-10-31 18:49:55 +00002521/***********************************************************/
2522/* TCP Net console */
2523
2524typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302525
2526 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302527 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002528 int fd, listen_fd;
2529 int connected;
2530 int max_size;
2531 int do_telnetopt;
2532 int do_nodelay;
2533 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002534 int *read_msgfds;
2535 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002536 int *write_msgfds;
2537 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002538
2539 SocketAddress *addr;
2540 bool is_listen;
2541 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002542
2543 guint reconnect_timer;
2544 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002545 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002546} TCPCharDriver;
2547
Corey Minyard5dd1f022014-10-02 11:17:37 -05002548static gboolean socket_reconnect_timeout(gpointer opaque);
2549
2550static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2551{
2552 TCPCharDriver *s = chr->opaque;
2553 assert(s->connected == 0);
2554 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2555 socket_reconnect_timeout, chr);
2556}
2557
Corey Minyard5008e5b2014-10-08 07:11:55 -05002558static void check_report_connect_error(CharDriverState *chr,
2559 Error *err)
2560{
2561 TCPCharDriver *s = chr->opaque;
2562
2563 if (!s->connect_err_reported) {
2564 error_report("Unable to connect character device %s: %s",
2565 chr->label, error_get_pretty(err));
2566 s->connect_err_reported = true;
2567 }
2568 qemu_chr_socket_restart_timer(chr);
2569}
2570
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302571static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002572
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002573#ifndef _WIN32
2574static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2575{
2576 TCPCharDriver *s = chr->opaque;
2577 struct msghdr msgh;
2578 struct iovec iov;
2579 int r;
2580
2581 size_t fd_size = s->write_msgfds_num * sizeof(int);
2582 char control[CMSG_SPACE(fd_size)];
2583 struct cmsghdr *cmsg;
2584
2585 memset(&msgh, 0, sizeof(msgh));
2586 memset(control, 0, sizeof(control));
2587
2588 /* set the payload */
2589 iov.iov_base = (uint8_t *) buf;
2590 iov.iov_len = len;
2591
2592 msgh.msg_iov = &iov;
2593 msgh.msg_iovlen = 1;
2594
2595 msgh.msg_control = control;
2596 msgh.msg_controllen = sizeof(control);
2597
2598 cmsg = CMSG_FIRSTHDR(&msgh);
2599
2600 cmsg->cmsg_len = CMSG_LEN(fd_size);
2601 cmsg->cmsg_level = SOL_SOCKET;
2602 cmsg->cmsg_type = SCM_RIGHTS;
2603 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2604
2605 do {
2606 r = sendmsg(s->fd, &msgh, 0);
2607 } while (r < 0 && errno == EINTR);
2608
2609 /* free the written msgfds, no matter what */
2610 if (s->write_msgfds_num) {
2611 g_free(s->write_msgfds);
2612 s->write_msgfds = 0;
2613 s->write_msgfds_num = 0;
2614 }
2615
2616 return r;
2617}
2618#endif
2619
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002620/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002621static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2622{
2623 TCPCharDriver *s = chr->opaque;
2624 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002625#ifndef _WIN32
2626 if (s->is_unix && s->write_msgfds_num) {
2627 return unix_send_msgfds(chr, buf, len);
2628 } else
2629#endif
2630 {
2631 return io_channel_send(s->chan, buf, len);
2632 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002633 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002634 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002635 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002636 }
2637}
2638
2639static int tcp_chr_read_poll(void *opaque)
2640{
2641 CharDriverState *chr = opaque;
2642 TCPCharDriver *s = chr->opaque;
2643 if (!s->connected)
2644 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002645 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002646 return s->max_size;
2647}
2648
2649#define IAC 255
2650#define IAC_BREAK 243
2651static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2652 TCPCharDriver *s,
2653 uint8_t *buf, int *size)
2654{
2655 /* Handle any telnet client's basic IAC options to satisfy char by
2656 * char mode with no echo. All IAC options will be removed from
2657 * the buf and the do_telnetopt variable will be used to track the
2658 * state of the width of the IAC information.
2659 *
2660 * IAC commands come in sets of 3 bytes with the exception of the
2661 * "IAC BREAK" command and the double IAC.
2662 */
2663
2664 int i;
2665 int j = 0;
2666
2667 for (i = 0; i < *size; i++) {
2668 if (s->do_telnetopt > 1) {
2669 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2670 /* Double IAC means send an IAC */
2671 if (j != i)
2672 buf[j] = buf[i];
2673 j++;
2674 s->do_telnetopt = 1;
2675 } else {
2676 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2677 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002678 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002679 s->do_telnetopt++;
2680 }
2681 s->do_telnetopt++;
2682 }
2683 if (s->do_telnetopt >= 4) {
2684 s->do_telnetopt = 1;
2685 }
2686 } else {
2687 if ((unsigned char)buf[i] == IAC) {
2688 s->do_telnetopt = 2;
2689 } else {
2690 if (j != i)
2691 buf[j] = buf[i];
2692 j++;
2693 }
2694 }
2695 }
2696 *size = j;
2697}
2698
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002699static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002700{
2701 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002702 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2703
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002704 assert(num <= TCP_MAX_FDS);
2705
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002706 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002707 int i;
2708
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002709 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2710
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002711 /* Close unused fds */
2712 for (i = to_copy; i < s->read_msgfds_num; i++) {
2713 close(s->read_msgfds[i]);
2714 }
2715
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002716 g_free(s->read_msgfds);
2717 s->read_msgfds = 0;
2718 s->read_msgfds_num = 0;
2719 }
2720
2721 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002722}
2723
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002724static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2725{
2726 TCPCharDriver *s = chr->opaque;
2727
2728 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002729 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002730
2731 if (num) {
Markus Armbruster2d528d42015-09-14 13:54:03 +02002732 s->write_msgfds = g_new(int, num);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002733 memcpy(s->write_msgfds, fds, num * sizeof(int));
2734 }
2735
2736 s->write_msgfds_num = num;
2737
2738 return 0;
2739}
2740
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002741#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002742static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2743{
2744 TCPCharDriver *s = chr->opaque;
2745 struct cmsghdr *cmsg;
2746
2747 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002748 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002749
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002750 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002751 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002752 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002753 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002754 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002755
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002756 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2757
2758 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002759 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002760 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002761
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002762 /* close and clean read_msgfds */
2763 for (i = 0; i < s->read_msgfds_num; i++) {
2764 close(s->read_msgfds[i]);
2765 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002766
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002767 if (s->read_msgfds_num) {
2768 g_free(s->read_msgfds);
2769 }
2770
2771 s->read_msgfds_num = fd_size / sizeof(int);
2772 s->read_msgfds = g_malloc(fd_size);
2773 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2774
2775 for (i = 0; i < s->read_msgfds_num; i++) {
2776 int fd = s->read_msgfds[i];
2777 if (fd < 0) {
2778 continue;
2779 }
2780
2781 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2782 qemu_set_block(fd);
2783
2784 #ifndef MSG_CMSG_CLOEXEC
2785 qemu_set_cloexec(fd);
2786 #endif
2787 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002788 }
2789}
2790
Mark McLoughlin9977c892009-07-22 09:11:38 +01002791static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2792{
2793 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002794 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002795 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002796 union {
2797 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002798 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002799 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002800 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002801 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002802
2803 iov[0].iov_base = buf;
2804 iov[0].iov_len = len;
2805
2806 msg.msg_iov = iov;
2807 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002808 msg.msg_control = &msg_control;
2809 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002810
Corey Bryant06138652012-08-14 16:43:42 -04002811#ifdef MSG_CMSG_CLOEXEC
2812 flags |= MSG_CMSG_CLOEXEC;
2813#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002814 do {
2815 ret = recvmsg(s->fd, &msg, flags);
2816 } while (ret == -1 && errno == EINTR);
2817
Corey Bryant06138652012-08-14 16:43:42 -04002818 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002819 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002820 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002821
2822 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002823}
2824#else
2825static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2826{
2827 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002828 ssize_t ret;
2829
2830 do {
2831 ret = qemu_recv(s->fd, buf, len, 0);
2832 } while (ret == -1 && socket_error() == EINTR);
2833
2834 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002835}
2836#endif
2837
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302838static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2839{
2840 TCPCharDriver *s = chr->opaque;
2841 return g_io_create_watch(s->chan, cond);
2842}
2843
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002844static void tcp_chr_disconnect(CharDriverState *chr)
2845{
2846 TCPCharDriver *s = chr->opaque;
2847
2848 s->connected = 0;
2849 if (s->listen_chan) {
2850 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2851 tcp_chr_accept, chr);
2852 }
2853 remove_fd_in_watch(chr);
2854 g_io_channel_unref(s->chan);
2855 s->chan = NULL;
2856 closesocket(s->fd);
2857 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002858 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2859 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002860 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002861 if (s->reconnect_time) {
2862 qemu_chr_socket_restart_timer(chr);
2863 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002864}
2865
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302866static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002867{
2868 CharDriverState *chr = opaque;
2869 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302870 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002871 int len, size;
2872
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302873 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002874 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302875 }
aliguori6f97dba2008-10-31 18:49:55 +00002876 len = sizeof(buf);
2877 if (len > s->max_size)
2878 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002879 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002880 if (size == 0 ||
2881 (size < 0 &&
2882 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002883 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002884 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002885 } else if (size > 0) {
2886 if (s->do_telnetopt)
2887 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2888 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002889 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002890 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302891
2892 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002893}
2894
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002895static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2896{
2897 TCPCharDriver *s = chr->opaque;
2898 int size;
2899
2900 if (!s->connected) {
2901 return 0;
2902 }
2903
2904 size = tcp_chr_recv(chr, (void *) buf, len);
2905 if (size == 0) {
2906 /* connection closed */
2907 tcp_chr_disconnect(chr);
2908 }
2909
2910 return size;
2911}
2912
Blue Swirl68c18d12010-08-15 09:46:24 +00002913#ifndef _WIN32
2914CharDriverState *qemu_chr_open_eventfd(int eventfd)
2915{
David Marchande9d21c42014-06-11 17:25:16 +02002916 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2917
2918 if (chr) {
2919 chr->avail_connections = 1;
2920 }
2921
2922 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002923}
Blue Swirl68c18d12010-08-15 09:46:24 +00002924#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002925
aliguori6f97dba2008-10-31 18:49:55 +00002926static void tcp_chr_connect(void *opaque)
2927{
2928 CharDriverState *chr = opaque;
2929 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002930 struct sockaddr_storage ss, ps;
2931 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002932
2933 memset(&ss, 0, ss_len);
2934 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2935 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2936 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002937 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2938 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2939 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002940 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002941 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2942 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002943 s->is_listen, s->is_telnet);
2944 }
aliguori6f97dba2008-10-31 18:49:55 +00002945
2946 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302947 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302948 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2949 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002950 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002951 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002952}
2953
Gal Hammerac1b84d2014-02-25 12:12:35 +02002954static void tcp_chr_update_read_handler(CharDriverState *chr)
2955{
2956 TCPCharDriver *s = chr->opaque;
2957
2958 remove_fd_in_watch(chr);
2959 if (s->chan) {
2960 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2961 tcp_chr_read, chr);
2962 }
2963}
2964
aliguori6f97dba2008-10-31 18:49:55 +00002965#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2966static void tcp_chr_telnet_init(int fd)
2967{
2968 char buf[3];
2969 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2970 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2971 send(fd, (char *)buf, 3, 0);
2972 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2973 send(fd, (char *)buf, 3, 0);
2974 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2975 send(fd, (char *)buf, 3, 0);
2976 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2977 send(fd, (char *)buf, 3, 0);
2978}
2979
Daniel P. Berrange13661082011-06-23 13:31:42 +01002980static int tcp_chr_add_client(CharDriverState *chr, int fd)
2981{
2982 TCPCharDriver *s = chr->opaque;
2983 if (s->fd != -1)
2984 return -1;
2985
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002986 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002987 if (s->do_nodelay)
2988 socket_set_nodelay(fd);
2989 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302990 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002991 if (s->listen_tag) {
2992 g_source_remove(s->listen_tag);
2993 s->listen_tag = 0;
2994 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002995 tcp_chr_connect(chr);
2996
2997 return 0;
2998}
2999
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303000static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00003001{
3002 CharDriverState *chr = opaque;
3003 TCPCharDriver *s = chr->opaque;
3004 struct sockaddr_in saddr;
3005#ifndef _WIN32
3006 struct sockaddr_un uaddr;
3007#endif
3008 struct sockaddr *addr;
3009 socklen_t len;
3010 int fd;
3011
3012 for(;;) {
3013#ifndef _WIN32
3014 if (s->is_unix) {
3015 len = sizeof(uaddr);
3016 addr = (struct sockaddr *)&uaddr;
3017 } else
3018#endif
3019 {
3020 len = sizeof(saddr);
3021 addr = (struct sockaddr *)&saddr;
3022 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003023 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003024 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003025 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303026 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003027 } else if (fd >= 0) {
3028 if (s->do_telnetopt)
3029 tcp_chr_telnet_init(fd);
3030 break;
3031 }
3032 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003033 if (tcp_chr_add_client(chr, fd) < 0)
3034 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303035
3036 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003037}
3038
3039static void tcp_chr_close(CharDriverState *chr)
3040{
3041 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003042 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003043
Corey Minyard5dd1f022014-10-02 11:17:37 -05003044 if (s->reconnect_timer) {
3045 g_source_remove(s->reconnect_timer);
3046 s->reconnect_timer = 0;
3047 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003048 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003049 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303050 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303051 if (s->chan) {
3052 g_io_channel_unref(s->chan);
3053 }
aliguori6f97dba2008-10-31 18:49:55 +00003054 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003055 }
3056 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303057 if (s->listen_tag) {
3058 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003059 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303060 }
3061 if (s->listen_chan) {
3062 g_io_channel_unref(s->listen_chan);
3063 }
aliguori6f97dba2008-10-31 18:49:55 +00003064 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003065 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003066 if (s->read_msgfds_num) {
3067 for (i = 0; i < s->read_msgfds_num; i++) {
3068 close(s->read_msgfds[i]);
3069 }
3070 g_free(s->read_msgfds);
3071 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003072 if (s->write_msgfds_num) {
3073 g_free(s->write_msgfds);
3074 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003075 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003076 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003077}
3078
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003079static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003080{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003081 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003082
Corey Minyardcfb429c2014-10-02 11:17:35 -05003083 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003084 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303085 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003086 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3087 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003088 } else {
3089 s->connected = 1;
3090 s->fd = fd;
3091 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303092 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003093 tcp_chr_connect(chr);
3094 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003095}
3096
Corey Minyard51795022014-10-08 07:11:56 -05003097static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003098{
3099 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003100 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003101
3102 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003103 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003104 return;
3105 }
3106
Corey Minyard5008e5b2014-10-08 07:11:55 -05003107 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003108 qemu_chr_finish_socket_connection(chr, fd);
3109}
3110
Corey Minyardcfb429c2014-10-02 11:17:35 -05003111static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003112{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003113 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003114 int fd;
3115
Corey Minyardcfb429c2014-10-02 11:17:35 -05003116 if (s->is_listen) {
3117 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003118 } else if (s->reconnect_time) {
3119 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3120 return fd >= 0;
3121 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003122 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003123 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003124 if (fd < 0) {
3125 return false;
3126 }
3127
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003128 qemu_chr_finish_socket_connection(chr, fd);
3129 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003130}
3131
Lei Li51767e72013-01-25 00:03:19 +08003132/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003133/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003134
3135typedef struct {
3136 size_t size;
3137 size_t prod;
3138 size_t cons;
3139 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003140} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003141
Markus Armbruster3949e592013-02-06 21:27:24 +01003142static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003143{
Markus Armbruster3949e592013-02-06 21:27:24 +01003144 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003145
Markus Armbruster5c230102013-02-06 21:27:23 +01003146 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003147}
3148
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003149/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003150static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003151{
Markus Armbruster3949e592013-02-06 21:27:24 +01003152 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003153 int i;
3154
3155 if (!buf || (len < 0)) {
3156 return -1;
3157 }
3158
3159 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003160 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3161 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003162 d->cons = d->prod - d->size;
3163 }
3164 }
3165
3166 return 0;
3167}
3168
Markus Armbruster3949e592013-02-06 21:27:24 +01003169static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003170{
Markus Armbruster3949e592013-02-06 21:27:24 +01003171 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003172 int i;
3173
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003174 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003175 for (i = 0; i < len && d->cons != d->prod; i++) {
3176 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003177 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003178 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003179
3180 return i;
3181}
3182
Markus Armbruster3949e592013-02-06 21:27:24 +01003183static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003184{
Markus Armbruster3949e592013-02-06 21:27:24 +01003185 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003186
3187 g_free(d->cbuf);
3188 g_free(d);
3189 chr->opaque = NULL;
3190}
3191
Markus Armbruster4f573782013-07-26 16:44:32 +02003192static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3193 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003194{
3195 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003196 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003197
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003198 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003199 d = g_malloc(sizeof(*d));
3200
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003201 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003202
3203 /* The size must be power of 2 */
3204 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003205 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003206 goto fail;
3207 }
3208
3209 d->prod = 0;
3210 d->cons = 0;
3211 d->cbuf = g_malloc0(d->size);
3212
3213 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003214 chr->chr_write = ringbuf_chr_write;
3215 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003216
3217 return chr;
3218
3219fail:
3220 g_free(d);
3221 g_free(chr);
3222 return NULL;
3223}
3224
Hani Benhabiles8e597772014-05-27 23:39:30 +01003225bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003226{
Markus Armbruster3949e592013-02-06 21:27:24 +01003227 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003228}
3229
Markus Armbruster3949e592013-02-06 21:27:24 +01003230void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003231 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003232 Error **errp)
3233{
3234 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003235 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003236 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003237 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003238
3239 chr = qemu_chr_find(device);
3240 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003241 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003242 return;
3243 }
3244
Markus Armbruster3949e592013-02-06 21:27:24 +01003245 if (!chr_is_ringbuf(chr)) {
3246 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003247 return;
3248 }
3249
Lei Li1f590cf2013-01-25 00:03:20 +08003250 if (has_format && (format == DATA_FORMAT_BASE64)) {
3251 write_data = g_base64_decode(data, &write_count);
3252 } else {
3253 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003254 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003255 }
3256
Markus Armbruster3949e592013-02-06 21:27:24 +01003257 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003258
Markus Armbruster13289fb2013-02-06 21:27:18 +01003259 if (write_data != (uint8_t *)data) {
3260 g_free((void *)write_data);
3261 }
3262
Lei Li1f590cf2013-01-25 00:03:20 +08003263 if (ret < 0) {
3264 error_setg(errp, "Failed to write to device %s", device);
3265 return;
3266 }
3267}
3268
Markus Armbruster3949e592013-02-06 21:27:24 +01003269char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003270 bool has_format, enum DataFormat format,
3271 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003272{
3273 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003274 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003275 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003276 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003277
3278 chr = qemu_chr_find(device);
3279 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003280 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003281 return NULL;
3282 }
3283
Markus Armbruster3949e592013-02-06 21:27:24 +01003284 if (!chr_is_ringbuf(chr)) {
3285 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003286 return NULL;
3287 }
3288
3289 if (size <= 0) {
3290 error_setg(errp, "size must be greater than zero");
3291 return NULL;
3292 }
3293
Markus Armbruster3949e592013-02-06 21:27:24 +01003294 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003295 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003296 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003297
Markus Armbruster3949e592013-02-06 21:27:24 +01003298 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003299
3300 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003301 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003302 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003303 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003304 /*
3305 * FIXME should read only complete, valid UTF-8 characters up
3306 * to @size bytes. Invalid sequences should be replaced by a
3307 * suitable replacement character. Except when (and only
3308 * when) ring buffer lost characters since last read, initial
3309 * continuation characters should be dropped.
3310 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003311 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003312 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003313 }
3314
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003315 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003316}
3317
Gerd Hoffmann33521632009-12-08 13:11:49 +01003318QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003319{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003320 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003321 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003322 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003323 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003324 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003325
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003326 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003327 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003328 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003329 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003330 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003331
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003332 if (strstart(filename, "mon:", &p)) {
3333 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003334 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003335 if (strcmp(filename, "stdio") == 0) {
3336 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3337 * but pass it to the guest. Handle this only for compat syntax,
3338 * for -chardev syntax we have special option for this.
3339 * This is what -nographic did, redirecting+muxing serial+monitor
3340 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003341 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003342 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003343 }
3344
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003345 if (strcmp(filename, "null") == 0 ||
3346 strcmp(filename, "pty") == 0 ||
3347 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003348 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003349 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003350 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003351 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003352 return opts;
3353 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003354 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003355 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003356 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003357 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003358 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003359 qemu_opt_set(opts, "width", width, &error_abort);
3360 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003361 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003362 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003363 qemu_opt_set(opts, "cols", width, &error_abort);
3364 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003365 } else {
3366 goto fail;
3367 }
3368 }
3369 return opts;
3370 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003371 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003372 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003373 return opts;
3374 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003375 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003376 qemu_opt_set(opts, "backend", "serial", &error_abort);
3377 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003378 return opts;
3379 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003380 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003381 qemu_opt_set(opts, "backend", "file", &error_abort);
3382 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003383 return opts;
3384 }
3385 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003386 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3387 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003388 return opts;
3389 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003390 if (strstart(filename, "tcp:", &p) ||
3391 strstart(filename, "telnet:", &p)) {
3392 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3393 host[0] = 0;
3394 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3395 goto fail;
3396 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003397 qemu_opt_set(opts, "backend", "socket", &error_abort);
3398 qemu_opt_set(opts, "host", host, &error_abort);
3399 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003400 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003401 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3402 if (local_err) {
3403 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003404 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003405 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003406 }
3407 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003408 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003409 return opts;
3410 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003411 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003412 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003413 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3414 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003415 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003416 goto fail;
3417 }
3418 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003419 qemu_opt_set(opts, "host", host, &error_abort);
3420 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003421 if (p[pos] == '@') {
3422 p += pos + 1;
3423 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3424 host[0] = 0;
3425 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003426 goto fail;
3427 }
3428 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003429 qemu_opt_set(opts, "localaddr", host, &error_abort);
3430 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003431 }
3432 return opts;
3433 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003434 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003435 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003436 qemu_opts_do_parse(opts, p, "path", &local_err);
3437 if (local_err) {
3438 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003439 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003440 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003441 return opts;
3442 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003443 if (strstart(filename, "/dev/parport", NULL) ||
3444 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003445 qemu_opt_set(opts, "backend", "parport", &error_abort);
3446 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003447 return opts;
3448 }
3449 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003450 qemu_opt_set(opts, "backend", "tty", &error_abort);
3451 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003452 return opts;
3453 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003454
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003455fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003456 qemu_opts_del(opts);
3457 return NULL;
3458}
3459
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003460static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3461 Error **errp)
3462{
3463 const char *path = qemu_opt_get(opts, "path");
3464
3465 if (path == NULL) {
3466 error_setg(errp, "chardev: file: no filename given");
3467 return;
3468 }
3469 backend->file = g_new0(ChardevFile, 1);
3470 backend->file->out = g_strdup(path);
3471}
3472
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003473static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3474 Error **errp)
3475{
3476 backend->stdio = g_new0(ChardevStdio, 1);
3477 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003478 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003479}
3480
Paolo Bonzini6511d392015-09-29 15:08:05 +02003481#ifdef HAVE_CHARDEV_SERIAL
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003482static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3483 Error **errp)
3484{
3485 const char *device = qemu_opt_get(opts, "path");
3486
3487 if (device == NULL) {
3488 error_setg(errp, "chardev: serial/tty: no device path given");
3489 return;
3490 }
3491 backend->serial = g_new0(ChardevHostdev, 1);
3492 backend->serial->device = g_strdup(device);
3493}
Paolo Bonzini6511d392015-09-29 15:08:05 +02003494#endif
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003495
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003496#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003497static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3498 Error **errp)
3499{
3500 const char *device = qemu_opt_get(opts, "path");
3501
3502 if (device == NULL) {
3503 error_setg(errp, "chardev: parallel: no device path given");
3504 return;
3505 }
3506 backend->parallel = g_new0(ChardevHostdev, 1);
3507 backend->parallel->device = g_strdup(device);
3508}
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003509#endif
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003510
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003511static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3512 Error **errp)
3513{
3514 const char *device = qemu_opt_get(opts, "path");
3515
3516 if (device == NULL) {
3517 error_setg(errp, "chardev: pipe: no device path given");
3518 return;
3519 }
3520 backend->pipe = g_new0(ChardevHostdev, 1);
3521 backend->pipe->device = g_strdup(device);
3522}
3523
Markus Armbruster4f573782013-07-26 16:44:32 +02003524static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3525 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003526{
3527 int val;
3528
Markus Armbruster3a1da422013-07-26 16:44:34 +02003529 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003530
Markus Armbruster0f953052013-06-27 16:22:07 +02003531 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003532 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003533 backend->ringbuf->has_size = true;
3534 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003535 }
3536}
3537
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003538static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3539 Error **errp)
3540{
3541 const char *chardev = qemu_opt_get(opts, "chardev");
3542
3543 if (chardev == NULL) {
3544 error_setg(errp, "chardev: mux: no chardev given");
3545 return;
3546 }
3547 backend->mux = g_new0(ChardevMux, 1);
3548 backend->mux->chardev = g_strdup(chardev);
3549}
3550
Peter Maydelldafd3252014-09-02 11:24:13 +01003551static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3552 Error **errp)
3553{
3554 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3555 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3556 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3557 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003558 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003559 const char *path = qemu_opt_get(opts, "path");
3560 const char *host = qemu_opt_get(opts, "host");
3561 const char *port = qemu_opt_get(opts, "port");
3562 SocketAddress *addr;
3563
3564 if (!path) {
3565 if (!host) {
3566 error_setg(errp, "chardev: socket: no host given");
3567 return;
3568 }
3569 if (!port) {
3570 error_setg(errp, "chardev: socket: no port given");
3571 return;
3572 }
3573 }
3574
3575 backend->socket = g_new0(ChardevSocket, 1);
3576
3577 backend->socket->has_nodelay = true;
3578 backend->socket->nodelay = do_nodelay;
3579 backend->socket->has_server = true;
3580 backend->socket->server = is_listen;
3581 backend->socket->has_telnet = true;
3582 backend->socket->telnet = is_telnet;
3583 backend->socket->has_wait = true;
3584 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003585 backend->socket->has_reconnect = true;
3586 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003587
3588 addr = g_new0(SocketAddress, 1);
3589 if (path) {
3590 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3591 addr->q_unix = g_new0(UnixSocketAddress, 1);
3592 addr->q_unix->path = g_strdup(path);
3593 } else {
3594 addr->kind = SOCKET_ADDRESS_KIND_INET;
3595 addr->inet = g_new0(InetSocketAddress, 1);
3596 addr->inet->host = g_strdup(host);
3597 addr->inet->port = g_strdup(port);
3598 addr->inet->has_to = qemu_opt_get(opts, "to");
3599 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3600 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3601 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3602 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3603 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3604 }
3605 backend->socket->addr = addr;
3606}
3607
Peter Maydell90a14bf2014-09-02 11:24:15 +01003608static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3609 Error **errp)
3610{
3611 const char *host = qemu_opt_get(opts, "host");
3612 const char *port = qemu_opt_get(opts, "port");
3613 const char *localaddr = qemu_opt_get(opts, "localaddr");
3614 const char *localport = qemu_opt_get(opts, "localport");
3615 bool has_local = false;
3616 SocketAddress *addr;
3617
3618 if (host == NULL || strlen(host) == 0) {
3619 host = "localhost";
3620 }
3621 if (port == NULL || strlen(port) == 0) {
3622 error_setg(errp, "chardev: udp: remote port not specified");
3623 return;
3624 }
3625 if (localport == NULL || strlen(localport) == 0) {
3626 localport = "0";
3627 } else {
3628 has_local = true;
3629 }
3630 if (localaddr == NULL || strlen(localaddr) == 0) {
3631 localaddr = "";
3632 } else {
3633 has_local = true;
3634 }
3635
3636 backend->udp = g_new0(ChardevUdp, 1);
3637
3638 addr = g_new0(SocketAddress, 1);
3639 addr->kind = SOCKET_ADDRESS_KIND_INET;
3640 addr->inet = g_new0(InetSocketAddress, 1);
3641 addr->inet->host = g_strdup(host);
3642 addr->inet->port = g_strdup(port);
3643 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3644 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3645 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3646 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3647 backend->udp->remote = addr;
3648
3649 if (has_local) {
3650 backend->udp->has_local = true;
3651 addr = g_new0(SocketAddress, 1);
3652 addr->kind = SOCKET_ADDRESS_KIND_INET;
3653 addr->inet = g_new0(InetSocketAddress, 1);
3654 addr->inet->host = g_strdup(localaddr);
3655 addr->inet->port = g_strdup(localport);
3656 backend->udp->local = addr;
3657 }
3658}
3659
Anthony Liguorid654f342013-03-05 23:21:28 +05303660typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003661 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003662 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003663 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003664 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3665 ChardevReturn *ret, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303666} CharDriver;
3667
3668static GSList *backends;
3669
Peter Maydelle4d50d42014-09-02 11:24:17 +01003670void register_char_driver(const char *name, ChardevBackendKind kind,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003671 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp),
3672 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3673 ChardevReturn *ret, Error **errp))
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003674{
3675 CharDriver *s;
3676
3677 s = g_malloc0(sizeof(*s));
3678 s->name = g_strdup(name);
3679 s->kind = kind;
3680 s->parse = parse;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003681 s->create = create;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003682
3683 backends = g_slist_append(backends, s);
3684}
3685
Anthony Liguorif69554b2011-08-15 11:17:37 -05003686CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003687 void (*init)(struct CharDriverState *s),
3688 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003689{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003690 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303691 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003692 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303693 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003694 ChardevReturn *ret = NULL;
3695 ChardevBackend *backend;
3696 const char *id = qemu_opts_id(opts);
3697 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003698
Peter Maydella61ae7f2014-09-02 11:24:16 +01003699 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003700 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003701 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003702 }
3703
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003704 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003705 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003706 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003707 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003708 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303709 for (i = backends; i; i = i->next) {
3710 cd = i->data;
3711
3712 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003713 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303714 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003715 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303716 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003717 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003718 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003719 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003720 }
3721
Peter Maydella61ae7f2014-09-02 11:24:16 +01003722 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003723
3724 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003725 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003726 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003727
3728 chr = NULL;
3729 backend->kind = cd->kind;
3730 if (cd->parse) {
3731 cd->parse(opts, backend, &local_err);
3732 if (local_err) {
3733 error_propagate(errp, local_err);
3734 goto qapi_out;
3735 }
3736 }
3737 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3738 if (!ret) {
3739 goto qapi_out;
3740 }
3741
3742 if (bid) {
3743 qapi_free_ChardevBackend(backend);
3744 qapi_free_ChardevReturn(ret);
3745 backend = g_new0(ChardevBackend, 1);
3746 backend->mux = g_new0(ChardevMux, 1);
3747 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3748 backend->mux->chardev = g_strdup(bid);
3749 ret = qmp_chardev_add(id, backend, errp);
3750 if (!ret) {
3751 chr = qemu_chr_find(bid);
3752 qemu_chr_delete(chr);
3753 chr = NULL;
3754 goto qapi_out;
3755 }
3756 }
3757
3758 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003759 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003760
3761qapi_out:
3762 qapi_free_ChardevBackend(backend);
3763 qapi_free_ChardevReturn(ret);
3764 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003765 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003766
3767err:
3768 qemu_opts_del(opts);
3769 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003770}
3771
Anthony Liguori27143a42011-08-15 11:17:36 -05003772CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003773{
3774 const char *p;
3775 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003776 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003777 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003778
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003779 if (strstart(filename, "chardev:", &p)) {
3780 return qemu_chr_find(p);
3781 }
3782
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003783 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003784 if (!opts)
3785 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003786
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003787 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003788 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003789 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003790 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003791 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003792 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003793 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003794 }
3795 return chr;
3796}
3797
Anthony Liguori15f31512011-08-15 11:17:35 -05003798void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003799{
3800 if (chr->chr_set_echo) {
3801 chr->chr_set_echo(chr, echo);
3802 }
3803}
3804
Hans de Goede8e25daa2013-03-26 11:07:57 +01003805void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003806{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003807 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003808 return;
3809 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003810 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003811 if (chr->chr_set_fe_open) {
3812 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003813 }
3814}
3815
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003816void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3817{
3818 if (chr->chr_fe_event) {
3819 chr->chr_fe_event(chr, event);
3820 }
3821}
3822
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003823int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3824 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303825{
3826 GSource *src;
3827 guint tag;
3828
3829 if (s->chr_add_watch == NULL) {
3830 return -ENOSYS;
3831 }
3832
3833 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003834 if (!src) {
3835 return -EINVAL;
3836 }
3837
Anthony Liguori23673ca2013-03-05 23:21:23 +05303838 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3839 tag = g_source_attach(src, NULL);
3840 g_source_unref(src);
3841
3842 return tag;
3843}
3844
Hans de Goede44c473d2013-03-27 20:29:39 +01003845int qemu_chr_fe_claim(CharDriverState *s)
3846{
3847 if (s->avail_connections < 1) {
3848 return -1;
3849 }
3850 s->avail_connections--;
3851 return 0;
3852}
3853
3854void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3855{
3856 if (qemu_chr_fe_claim(s) != 0) {
3857 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3858 __func__, s->label);
3859 exit(1);
3860 }
3861}
3862
3863void qemu_chr_fe_release(CharDriverState *s)
3864{
3865 s->avail_connections++;
3866}
3867
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003868void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003869{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003870 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003871 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003872 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003873 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003874 g_free(chr->filename);
3875 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003876 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003877 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003878}
3879
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003880ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003881{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003882 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003883 CharDriverState *chr;
3884
Blue Swirl72cf2d42009-09-12 07:36:22 +00003885 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003886 ChardevInfoList *info = g_malloc0(sizeof(*info));
3887 info->value = g_malloc0(sizeof(*info->value));
3888 info->value->label = g_strdup(chr->label);
3889 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003890 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003891
3892 info->next = chr_list;
3893 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003894 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003895
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003896 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003897}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003898
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003899ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3900{
3901 ChardevBackendInfoList *backend_list = NULL;
3902 CharDriver *c = NULL;
3903 GSList *i = NULL;
3904
3905 for (i = backends; i; i = i->next) {
3906 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3907 c = i->data;
3908 info->value = g_malloc0(sizeof(*info->value));
3909 info->value->name = g_strdup(c->name);
3910
3911 info->next = backend_list;
3912 backend_list = info;
3913 }
3914
3915 return backend_list;
3916}
3917
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003918CharDriverState *qemu_chr_find(const char *name)
3919{
3920 CharDriverState *chr;
3921
Blue Swirl72cf2d42009-09-12 07:36:22 +00003922 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003923 if (strcmp(chr->label, name) != 0)
3924 continue;
3925 return chr;
3926 }
3927 return NULL;
3928}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003929
3930/* Get a character (serial) device interface. */
3931CharDriverState *qemu_char_get_next_serial(void)
3932{
3933 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003934 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003935
3936 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003937
3938 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3939 chr = serial_hds[next_serial++];
3940 qemu_chr_fe_claim_no_fail(chr);
3941 return chr;
3942 }
3943 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003944}
3945
Paolo Bonzini4d454572012-11-26 16:03:42 +01003946QemuOptsList qemu_chardev_opts = {
3947 .name = "chardev",
3948 .implied_opt_name = "backend",
3949 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3950 .desc = {
3951 {
3952 .name = "backend",
3953 .type = QEMU_OPT_STRING,
3954 },{
3955 .name = "path",
3956 .type = QEMU_OPT_STRING,
3957 },{
3958 .name = "host",
3959 .type = QEMU_OPT_STRING,
3960 },{
3961 .name = "port",
3962 .type = QEMU_OPT_STRING,
3963 },{
3964 .name = "localaddr",
3965 .type = QEMU_OPT_STRING,
3966 },{
3967 .name = "localport",
3968 .type = QEMU_OPT_STRING,
3969 },{
3970 .name = "to",
3971 .type = QEMU_OPT_NUMBER,
3972 },{
3973 .name = "ipv4",
3974 .type = QEMU_OPT_BOOL,
3975 },{
3976 .name = "ipv6",
3977 .type = QEMU_OPT_BOOL,
3978 },{
3979 .name = "wait",
3980 .type = QEMU_OPT_BOOL,
3981 },{
3982 .name = "server",
3983 .type = QEMU_OPT_BOOL,
3984 },{
3985 .name = "delay",
3986 .type = QEMU_OPT_BOOL,
3987 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003988 .name = "reconnect",
3989 .type = QEMU_OPT_NUMBER,
3990 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003991 .name = "telnet",
3992 .type = QEMU_OPT_BOOL,
3993 },{
3994 .name = "width",
3995 .type = QEMU_OPT_NUMBER,
3996 },{
3997 .name = "height",
3998 .type = QEMU_OPT_NUMBER,
3999 },{
4000 .name = "cols",
4001 .type = QEMU_OPT_NUMBER,
4002 },{
4003 .name = "rows",
4004 .type = QEMU_OPT_NUMBER,
4005 },{
4006 .name = "mux",
4007 .type = QEMU_OPT_BOOL,
4008 },{
4009 .name = "signal",
4010 .type = QEMU_OPT_BOOL,
4011 },{
4012 .name = "name",
4013 .type = QEMU_OPT_STRING,
4014 },{
4015 .name = "debug",
4016 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08004017 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01004018 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01004019 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004020 },{
4021 .name = "chardev",
4022 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004023 },
4024 { /* end of list */ }
4025 },
4026};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004027
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004028#ifdef _WIN32
4029
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004030static CharDriverState *qmp_chardev_open_file(const char *id,
4031 ChardevBackend *backend,
4032 ChardevReturn *ret,
4033 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004034{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004035 ChardevFile *file = backend->file;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004036 HANDLE out;
4037
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004038 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004039 error_setg(errp, "input file not supported");
4040 return NULL;
4041 }
4042
4043 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4044 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4045 if (out == INVALID_HANDLE_VALUE) {
4046 error_setg(errp, "open %s failed", file->out);
4047 return NULL;
4048 }
4049 return qemu_chr_open_win_file(out);
4050}
4051
Paolo Bonzini6511d392015-09-29 15:08:05 +02004052static CharDriverState *qmp_chardev_open_serial(const char *id,
4053 ChardevBackend *backend,
4054 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004055 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004056{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004057 ChardevHostdev *serial = backend->serial;
4058 return qemu_chr_open_win_path(serial->device, errp);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004059}
4060
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004061#else /* WIN32 */
4062
4063static int qmp_chardev_open_file_source(char *src, int flags,
4064 Error **errp)
4065{
4066 int fd = -1;
4067
4068 TFR(fd = qemu_open(src, flags, 0666));
4069 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004070 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004071 }
4072 return fd;
4073}
4074
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004075static CharDriverState *qmp_chardev_open_file(const char *id,
4076 ChardevBackend *backend,
4077 ChardevReturn *ret,
4078 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004079{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004080 ChardevFile *file = backend->file;
Markus Armbruster5f758362014-05-19 18:57:34 +02004081 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004082
4083 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4084 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004085 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004086 return NULL;
4087 }
4088
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004089 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004090 flags = O_RDONLY;
4091 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004092 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004093 qemu_close(out);
4094 return NULL;
4095 }
4096 }
4097
4098 return qemu_chr_open_fd(in, out);
4099}
4100
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004101#ifdef HAVE_CHARDEV_SERIAL
Paolo Bonzini6511d392015-09-29 15:08:05 +02004102static CharDriverState *qmp_chardev_open_serial(const char *id,
4103 ChardevBackend *backend,
4104 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004105 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004106{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004107 ChardevHostdev *serial = backend->serial;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004108 int fd;
4109
4110 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004111 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004112 return NULL;
4113 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004114 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004115 return qemu_chr_open_tty_fd(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004116}
Paolo Bonzini6511d392015-09-29 15:08:05 +02004117#endif
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004118
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004119#ifdef HAVE_CHARDEV_PARPORT
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004120static CharDriverState *qmp_chardev_open_parallel(const char *id,
4121 ChardevBackend *backend,
4122 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004123 Error **errp)
4124{
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004125 ChardevHostdev *parallel = backend->parallel;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004126 int fd;
4127
4128 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004129 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004130 return NULL;
4131 }
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004132 return qemu_chr_open_pp_fd(fd, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004133}
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004134#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004135
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004136#endif /* WIN32 */
4137
Corey Minyard5008e5b2014-10-08 07:11:55 -05004138static void socket_try_connect(CharDriverState *chr)
4139{
4140 Error *err = NULL;
4141
4142 if (!qemu_chr_open_socket_fd(chr, &err)) {
4143 check_report_connect_error(chr, err);
4144 }
4145}
4146
Corey Minyard5dd1f022014-10-02 11:17:37 -05004147static gboolean socket_reconnect_timeout(gpointer opaque)
4148{
4149 CharDriverState *chr = opaque;
4150 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004151
4152 s->reconnect_timer = 0;
4153
4154 if (chr->be_open) {
4155 return false;
4156 }
4157
Corey Minyard5008e5b2014-10-08 07:11:55 -05004158 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004159
4160 return false;
4161}
4162
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004163static CharDriverState *qmp_chardev_open_socket(const char *id,
4164 ChardevBackend *backend,
4165 ChardevReturn *ret,
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004166 Error **errp)
4167{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004168 CharDriverState *chr;
4169 TCPCharDriver *s;
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004170 ChardevSocket *sock = backend->socket;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004171 SocketAddress *addr = sock->addr;
4172 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4173 bool is_listen = sock->has_server ? sock->server : true;
4174 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4175 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004176 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004177
4178 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02004179 s = g_new0(TCPCharDriver, 1);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004180
4181 s->fd = -1;
4182 s->listen_fd = -1;
4183 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004184 s->is_listen = is_listen;
4185 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004186 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004187 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004188
4189 chr->opaque = s;
4190 chr->chr_write = tcp_chr_write;
4191 chr->chr_sync_read = tcp_chr_sync_read;
4192 chr->chr_close = tcp_chr_close;
4193 chr->get_msgfds = tcp_get_msgfds;
4194 chr->set_msgfds = tcp_set_msgfds;
4195 chr->chr_add_client = tcp_chr_add_client;
4196 chr->chr_add_watch = tcp_chr_add_watch;
4197 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4198 /* be isn't opened until we get a connection */
4199 chr->explicit_be_open = true;
4200
4201 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004202 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4203 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004204
4205 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004206 if (is_telnet) {
4207 s->do_telnetopt = 1;
4208 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004209 } else if (reconnect > 0) {
4210 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004211 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004212
Corey Minyard5008e5b2014-10-08 07:11:55 -05004213 if (s->reconnect_time) {
4214 socket_try_connect(chr);
4215 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4216 g_free(s);
4217 g_free(chr->filename);
4218 g_free(chr);
4219 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004220 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004221
4222 if (is_listen && is_waitconnect) {
4223 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4224 chr->filename);
4225 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4226 qemu_set_nonblock(s->listen_fd);
4227 }
4228
4229 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004230}
4231
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004232static CharDriverState *qmp_chardev_open_udp(const char *id,
4233 ChardevBackend *backend,
4234 ChardevReturn *ret,
Lei Li08d0ab32013-05-20 14:51:03 +08004235 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004236{
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004237 ChardevUdp *udp = backend->udp;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004238 int fd;
4239
Lei Li08d0ab32013-05-20 14:51:03 +08004240 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004241 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004242 return NULL;
4243 }
4244 return qemu_chr_open_udp_fd(fd);
4245}
4246
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004247ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4248 Error **errp)
4249{
4250 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004251 CharDriverState *base, *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004252 Error *local_err = NULL;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004253 GSList *i;
4254 CharDriver *cd;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004255
4256 chr = qemu_chr_find(id);
4257 if (chr) {
4258 error_setg(errp, "Chardev '%s' already exists", id);
4259 g_free(ret);
4260 return NULL;
4261 }
4262
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004263 for (i = backends; i; i = i->next) {
4264 cd = i->data;
4265
4266 if (cd->kind == backend->kind && cd->create) {
4267 chr = cd->create(id, backend, ret, &local_err);
4268 if (local_err) {
4269 error_propagate(errp, local_err);
4270 goto out_error;
4271 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004272 break;
4273 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004274 }
4275
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004276 if (chr == NULL) {
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004277 switch (backend->kind) {
4278 case CHARDEV_BACKEND_KIND_FILE:
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004279 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004280 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004281 case CHARDEV_BACKEND_KIND_SERIAL:
Paolo Bonzini6511d392015-09-29 15:08:05 +02004282 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004283 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004284 case CHARDEV_BACKEND_KIND_PARALLEL:
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004285 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004286 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004287 case CHARDEV_BACKEND_KIND_PIPE:
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004288 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004289 break;
4290 case CHARDEV_BACKEND_KIND_SOCKET:
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004291 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004292 break;
4293 case CHARDEV_BACKEND_KIND_UDP:
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004294 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004295 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004296 case CHARDEV_BACKEND_KIND_PTY:
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004297 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004298 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004299 case CHARDEV_BACKEND_KIND_NULL:
Paolo Bonzini0d649922015-09-29 15:24:29 +02004300 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004301 break;
4302 case CHARDEV_BACKEND_KIND_MUX:
4303 base = qemu_chr_find(backend->mux->chardev);
4304 if (base == NULL) {
4305 error_setg(&local_err, "mux: base chardev %s not found",
4306 backend->mux->chardev);
4307 break;
4308 }
4309 chr = qemu_chr_open_mux(base);
4310 break;
4311 case CHARDEV_BACKEND_KIND_MSMOUSE:
4312 chr = qemu_chr_open_msmouse();
4313 break;
4314#ifdef CONFIG_BRLAPI
4315 case CHARDEV_BACKEND_KIND_BRAILLE:
4316 chr = chr_baum_init();
4317 break;
4318#endif
4319 case CHARDEV_BACKEND_KIND_TESTDEV:
4320 chr = chr_testdev_init();
4321 break;
4322 case CHARDEV_BACKEND_KIND_STDIO:
4323 chr = qemu_chr_open_stdio(backend->stdio);
4324 break;
4325#ifdef _WIN32
4326 case CHARDEV_BACKEND_KIND_CONSOLE:
4327 chr = qemu_chr_open_win_con();
4328 break;
4329#endif
4330#ifdef CONFIG_SPICE
4331 case CHARDEV_BACKEND_KIND_SPICEVMC:
4332 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4333 break;
4334 case CHARDEV_BACKEND_KIND_SPICEPORT:
4335 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4336 break;
4337#endif
4338 case CHARDEV_BACKEND_KIND_VC:
4339 chr = vc_init(backend->vc);
4340 break;
4341 case CHARDEV_BACKEND_KIND_RINGBUF:
4342 case CHARDEV_BACKEND_KIND_MEMORY:
4343 chr = qemu_chr_open_ringbuf(backend->ringbuf, &local_err);
4344 break;
4345 default:
4346 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4347 goto out_error;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004348 }
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004349
4350 /*
4351 * Character backend open hasn't been fully converted to the Error
4352 * API. Some opens fail without setting an error. Set a generic
4353 * error then.
4354 * TODO full conversion to Error API
4355 */
4356 if (chr == NULL) {
4357 if (local_err) {
4358 error_propagate(errp, local_err);
4359 } else {
4360 error_setg(errp, "Failed to create chardev");
4361 }
4362 goto out_error;
4363 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004364 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004365
4366 chr->label = g_strdup(id);
4367 chr->avail_connections =
4368 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
4369 if (!chr->filename) {
4370 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4371 }
4372 if (!chr->explicit_be_open) {
4373 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4374 }
4375 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4376 return ret;
4377
4378out_error:
4379 g_free(ret);
4380 return NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004381}
4382
4383void qmp_chardev_remove(const char *id, Error **errp)
4384{
4385 CharDriverState *chr;
4386
4387 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004388 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004389 error_setg(errp, "Chardev '%s' not found", id);
4390 return;
4391 }
4392 if (chr->chr_can_read || chr->chr_read ||
4393 chr->chr_event || chr->handler_opaque) {
4394 error_setg(errp, "Chardev '%s' is busy", id);
4395 return;
4396 }
4397 qemu_chr_delete(chr);
4398}
Anthony Liguorid654f342013-03-05 23:21:28 +05304399
4400static void register_types(void)
4401{
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004402 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
Paolo Bonzini0d649922015-09-29 15:24:29 +02004403 qemu_chr_open_null);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004404 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004405 qemu_chr_parse_socket, qmp_chardev_open_socket);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004406 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp,
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004407 qmp_chardev_open_udp);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004408 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004409 qemu_chr_parse_ringbuf, NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004410 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004411 qemu_chr_parse_file_out, qmp_chardev_open_file);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004412 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004413 qemu_chr_parse_stdio, NULL);
Paolo Bonzini6511d392015-09-29 15:08:05 +02004414#if defined HAVE_CHARDEV_SERIAL
Peter Maydelle4d50d42014-09-02 11:24:17 +01004415 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004416 qemu_chr_parse_serial, qmp_chardev_open_serial);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004417 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004418 qemu_chr_parse_serial, qmp_chardev_open_serial);
4419#endif
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004420#ifdef HAVE_CHARDEV_PARPORT
Peter Maydelle4d50d42014-09-02 11:24:17 +01004421 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004422 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004423 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004424 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
4425#endif
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004426#ifdef HAVE_CHARDEV_PTY
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004427 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004428 qemu_chr_open_pty);
4429#endif
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004430 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL,
4431 NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004432 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004433 qemu_chr_parse_pipe, qemu_chr_open_pipe);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004434 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux,
4435 NULL);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004436 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004437 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004438 qemu_chr_parse_ringbuf, NULL);
Michael Roth7b7ab182013-07-30 13:04:22 -05004439 /* this must be done after machine init, since we register FEs with muxes
4440 * as part of realize functions like serial_isa_realizefn when -nographic
4441 * is specified
4442 */
4443 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304444}
4445
4446type_init(register_types);