blob: 15c1a1547fd6fb729c4722ac156af9dc4f44c1db [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
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100386static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000387{
388 CharDriverState *chr;
389
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200390 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000391 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500392 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100393 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000394}
395
396/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000397#define MAX_MUX 4
398#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
399#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
400typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100401 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000402 IOReadHandler *chr_read[MAX_MUX];
403 IOEventHandler *chr_event[MAX_MUX];
404 void *ext_opaque[MAX_MUX];
405 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200406 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000407 int mux_cnt;
408 int term_got_escape;
409 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000410 /* Intermediate input buffer allows to catch escape sequences even if the
411 currently active device is not accepting any input - but only until it
412 is full as well. */
413 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
414 int prod[MAX_MUX];
415 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200416 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200417
418 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200419 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200420 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000421} MuxDriver;
422
423
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200424/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000425static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
426{
427 MuxDriver *d = chr->opaque;
428 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200429 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200430 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000431 } else {
432 int i;
433
434 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200435 for (i = 0; i < len; i++) {
436 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000437 char buf1[64];
438 int64_t ti;
439 int secs;
440
Alex Blighbc72ad62013-08-21 16:03:08 +0100441 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200442 if (d->timestamps_start == -1)
443 d->timestamps_start = ti;
444 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000445 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000446 snprintf(buf1, sizeof(buf1),
447 "[%02d:%02d:%02d.%03d] ",
448 secs / 3600,
449 (secs / 60) % 60,
450 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000451 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200452 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200453 d->linestart = 0;
454 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200455 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200456 if (buf[i] == '\n') {
457 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000458 }
459 }
460 }
461 return ret;
462}
463
464static const char * const mux_help[] = {
465 "% h print this help\n\r",
466 "% x exit emulator\n\r",
467 "% s save disk data back to file (if -snapshot)\n\r",
Gonglei86d10322014-11-15 18:06:46 +0800468 "% t toggle console timestamps\n\r",
aliguori6f97dba2008-10-31 18:49:55 +0000469 "% b send break (magic sysrq)\n\r",
470 "% c switch between console and monitor\n\r",
471 "% % sends %\n\r",
472 NULL
473};
474
475int term_escape_char = 0x01; /* ctrl-a is used for escape */
476static void mux_print_help(CharDriverState *chr)
477{
478 int i, j;
479 char ebuf[15] = "Escape-Char";
480 char cbuf[50] = "\n\r";
481
482 if (term_escape_char > 0 && term_escape_char < 26) {
483 snprintf(cbuf, sizeof(cbuf), "\n\r");
484 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
485 } else {
486 snprintf(cbuf, sizeof(cbuf),
487 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
488 term_escape_char);
489 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200490 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000491 for (i = 0; mux_help[i] != NULL; i++) {
492 for (j=0; mux_help[i][j] != '\0'; j++) {
493 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200494 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000495 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200496 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000497 }
498 }
499}
500
aliguori2724b182009-03-05 23:01:47 +0000501static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
502{
503 if (d->chr_event[mux_nr])
504 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
505}
506
aliguori6f97dba2008-10-31 18:49:55 +0000507static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
508{
509 if (d->term_got_escape) {
510 d->term_got_escape = 0;
511 if (ch == term_escape_char)
512 goto send_char;
513 switch(ch) {
514 case '?':
515 case 'h':
516 mux_print_help(chr);
517 break;
518 case 'x':
519 {
520 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200521 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000522 exit(0);
523 break;
524 }
525 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200526 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000527 break;
528 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100529 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000530 break;
531 case 'c':
532 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200533 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
534 d->focus++;
535 if (d->focus >= d->mux_cnt)
536 d->focus = 0;
537 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000538 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200539 case 't':
540 d->timestamps = !d->timestamps;
541 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200542 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200543 break;
aliguori6f97dba2008-10-31 18:49:55 +0000544 }
545 } else if (ch == term_escape_char) {
546 d->term_got_escape = 1;
547 } else {
548 send_char:
549 return 1;
550 }
551 return 0;
552}
553
554static void mux_chr_accept_input(CharDriverState *chr)
555{
aliguori6f97dba2008-10-31 18:49:55 +0000556 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200557 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000558
aliguoria80bf992009-03-05 23:00:02 +0000559 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000560 d->chr_can_read[m] &&
561 d->chr_can_read[m](d->ext_opaque[m])) {
562 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000563 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000564 }
565}
566
567static int mux_chr_can_read(void *opaque)
568{
569 CharDriverState *chr = opaque;
570 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200571 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000572
aliguoria80bf992009-03-05 23:00:02 +0000573 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000574 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000575 if (d->chr_can_read[m])
576 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000577 return 0;
578}
579
580static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
581{
582 CharDriverState *chr = opaque;
583 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200584 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000585 int i;
586
587 mux_chr_accept_input (opaque);
588
589 for(i = 0; i < size; i++)
590 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000591 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000592 d->chr_can_read[m] &&
593 d->chr_can_read[m](d->ext_opaque[m]))
594 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
595 else
aliguoria80bf992009-03-05 23:00:02 +0000596 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000597 }
598}
599
600static void mux_chr_event(void *opaque, int event)
601{
602 CharDriverState *chr = opaque;
603 MuxDriver *d = chr->opaque;
604 int i;
605
606 /* Send the event to all registered listeners */
607 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000608 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000609}
610
611static void mux_chr_update_read_handler(CharDriverState *chr)
612{
613 MuxDriver *d = chr->opaque;
614
615 if (d->mux_cnt >= MAX_MUX) {
616 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
617 return;
618 }
619 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
620 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
621 d->chr_read[d->mux_cnt] = chr->chr_read;
622 d->chr_event[d->mux_cnt] = chr->chr_event;
623 /* Fix up the real driver with mux routines */
624 if (d->mux_cnt == 0) {
625 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
626 mux_chr_event, chr);
627 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200628 if (d->focus != -1) {
629 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200630 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200631 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000632 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200633 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000634}
635
Michael Roth7b7ab182013-07-30 13:04:22 -0500636static bool muxes_realized;
637
638/**
639 * Called after processing of default and command-line-specified
640 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
641 * to a mux chardev. This is done here to ensure that
642 * output/prompts/banners are only displayed for the FE that has
643 * focus when initial command-line processing/machine init is
644 * completed.
645 *
646 * After this point, any new FE attached to any new or existing
647 * mux will receive CHR_EVENT_OPENED notifications for the BE
648 * immediately.
649 */
650static void muxes_realize_done(Notifier *notifier, void *unused)
651{
652 CharDriverState *chr;
653
654 QTAILQ_FOREACH(chr, &chardevs, next) {
655 if (chr->is_mux) {
656 MuxDriver *d = chr->opaque;
657 int i;
658
659 /* send OPENED to all already-attached FEs */
660 for (i = 0; i < d->mux_cnt; i++) {
661 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
662 }
663 /* mark mux as OPENED so any new FEs will immediately receive
664 * OPENED event
665 */
666 qemu_chr_be_generic_open(chr);
667 }
668 }
669 muxes_realized = true;
670}
671
672static Notifier muxes_realize_notify = {
673 .notify = muxes_realize_done,
674};
675
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400676static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
677{
678 MuxDriver *d = s->opaque;
679 return d->drv->chr_add_watch(d->drv, cond);
680}
681
aliguori6f97dba2008-10-31 18:49:55 +0000682static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
683{
684 CharDriverState *chr;
685 MuxDriver *d;
686
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200687 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +0200688 d = g_new0(MuxDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000689
690 chr->opaque = d;
691 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200692 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000693 chr->chr_write = mux_chr_write;
694 chr->chr_update_read_handler = mux_chr_update_read_handler;
695 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100696 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100697 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400698 if (drv->chr_add_watch) {
699 chr->chr_add_watch = mux_chr_add_watch;
700 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500701 /* only default to opened state if we've realized the initial
702 * set of muxes
703 */
704 chr->explicit_be_open = muxes_realized ? 0 : 1;
705 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200706
aliguori6f97dba2008-10-31 18:49:55 +0000707 return chr;
708}
709
710
711#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000712int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000713{
714 int ret, len;
715
716 len = len1;
717 while (len > 0) {
718 ret = send(fd, buf, len, 0);
719 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000720 errno = WSAGetLastError();
721 if (errno != WSAEWOULDBLOCK) {
722 return -1;
723 }
724 } else if (ret == 0) {
725 break;
726 } else {
727 buf += ret;
728 len -= ret;
729 }
730 }
731 return len1 - len;
732}
733
734#else
735
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100736int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000737{
738 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100739 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000740
741 len = len1;
742 while (len > 0) {
743 ret = write(fd, buf, len);
744 if (ret < 0) {
745 if (errno != EINTR && errno != EAGAIN)
746 return -1;
747 } else if (ret == 0) {
748 break;
749 } else {
750 buf += ret;
751 len -= ret;
752 }
753 }
754 return len1 - len;
755}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500756
757int recv_all(int fd, void *_buf, int len1, bool single_read)
758{
759 int ret, len;
760 uint8_t *buf = _buf;
761
762 len = len1;
763 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
764 if (ret < 0) {
765 if (errno != EINTR && errno != EAGAIN) {
766 return -1;
767 }
768 continue;
769 } else {
770 if (single_read) {
771 return ret;
772 }
773 buf += ret;
774 len -= ret;
775 }
776 }
777 return len1 - len;
778}
779
aliguori6f97dba2008-10-31 18:49:55 +0000780#endif /* !_WIN32 */
781
Anthony Liguori96c63842013-03-05 23:21:18 +0530782typedef struct IOWatchPoll
783{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200784 GSource parent;
785
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200786 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530787 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530788
789 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200790 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530791 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530792} IOWatchPoll;
793
Anthony Liguori96c63842013-03-05 23:21:18 +0530794static IOWatchPoll *io_watch_poll_from_source(GSource *source)
795{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200796 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530797}
798
799static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
800{
801 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200802 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200803 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200804 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530805 return FALSE;
806 }
807
Paolo Bonzinid185c092013-04-05 17:59:33 +0200808 if (now_active) {
Nils Carlson4bf1cb02015-07-19 20:39:56 +0000809 iwp->src = g_io_create_watch(iwp->channel,
810 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200811 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200812 g_source_attach(iwp->src, NULL);
813 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200814 g_source_destroy(iwp->src);
815 g_source_unref(iwp->src);
816 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200817 }
818 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530819}
820
821static gboolean io_watch_poll_check(GSource *source)
822{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200823 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530824}
825
826static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
827 gpointer user_data)
828{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200829 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530830}
831
832static void io_watch_poll_finalize(GSource *source)
833{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200834 /* Due to a glib bug, removing the last reference to a source
835 * inside a finalize callback causes recursive locking (and a
836 * deadlock). This is not a problem inside other callbacks,
837 * including dispatch callbacks, so we call io_remove_watch_poll
838 * to remove this source. At this point, iwp->src must
839 * be NULL, or we would leak it.
840 *
841 * This would be solved much more elegantly by child sources,
842 * but we support older glib versions that do not have them.
843 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530844 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200845 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530846}
847
848static GSourceFuncs io_watch_poll_funcs = {
849 .prepare = io_watch_poll_prepare,
850 .check = io_watch_poll_check,
851 .dispatch = io_watch_poll_dispatch,
852 .finalize = io_watch_poll_finalize,
853};
854
855/* Can only be used for read */
856static guint io_add_watch_poll(GIOChannel *channel,
857 IOCanReadHandler *fd_can_read,
858 GIOFunc fd_read,
859 gpointer user_data)
860{
861 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200862 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530863
Paolo Bonzinid185c092013-04-05 17:59:33 +0200864 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530865 iwp->fd_can_read = fd_can_read;
866 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200867 iwp->channel = channel;
868 iwp->fd_read = (GSourceFunc) fd_read;
869 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530870
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200871 tag = g_source_attach(&iwp->parent, NULL);
872 g_source_unref(&iwp->parent);
873 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530874}
875
Paolo Bonzini2b316772013-04-19 17:32:09 +0200876static void io_remove_watch_poll(guint tag)
877{
878 GSource *source;
879 IOWatchPoll *iwp;
880
881 g_return_if_fail (tag > 0);
882
883 source = g_main_context_find_source_by_id(NULL, tag);
884 g_return_if_fail (source != NULL);
885
886 iwp = io_watch_poll_from_source(source);
887 if (iwp->src) {
888 g_source_destroy(iwp->src);
889 g_source_unref(iwp->src);
890 iwp->src = NULL;
891 }
892 g_source_destroy(&iwp->parent);
893}
894
Amit Shah26da70c2013-08-28 15:23:37 +0530895static void remove_fd_in_watch(CharDriverState *chr)
896{
897 if (chr->fd_in_tag) {
898 io_remove_watch_poll(chr->fd_in_tag);
899 chr->fd_in_tag = 0;
900 }
901}
902
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000903#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530904static GIOChannel *io_channel_from_fd(int fd)
905{
906 GIOChannel *chan;
907
908 if (fd == -1) {
909 return NULL;
910 }
911
912 chan = g_io_channel_unix_new(fd);
913
914 g_io_channel_set_encoding(chan, NULL, NULL);
915 g_io_channel_set_buffered(chan, FALSE);
916
917 return chan;
918}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000919#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530920
Anthony Liguori76a96442013-03-05 23:21:21 +0530921static GIOChannel *io_channel_from_socket(int fd)
922{
923 GIOChannel *chan;
924
925 if (fd == -1) {
926 return NULL;
927 }
928
929#ifdef _WIN32
930 chan = g_io_channel_win32_new_socket(fd);
931#else
932 chan = g_io_channel_unix_new(fd);
933#endif
934
935 g_io_channel_set_encoding(chan, NULL, NULL);
936 g_io_channel_set_buffered(chan, FALSE);
937
938 return chan;
939}
940
Anthony Liguori684a0962013-03-29 11:39:50 -0500941static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530942{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200943 size_t offset = 0;
944 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530945
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200946 while (offset < len && status == G_IO_STATUS_NORMAL) {
947 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500948
949 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530950 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500951 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530952 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500953
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200954 if (offset > 0) {
955 return offset;
956 }
957 switch (status) {
958 case G_IO_STATUS_NORMAL:
959 g_assert(len == 0);
960 return 0;
961 case G_IO_STATUS_AGAIN:
962 errno = EAGAIN;
963 return -1;
964 default:
965 break;
966 }
967 errno = EINVAL;
968 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530969}
Anthony Liguori96c63842013-03-05 23:21:18 +0530970
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000971#ifndef _WIN32
972
Anthony Liguoria29753f2013-03-05 23:21:19 +0530973typedef struct FDCharDriver {
974 CharDriverState *chr;
975 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000976 int max_size;
977} FDCharDriver;
978
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200979/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000980static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
981{
982 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530983
Anthony Liguori684a0962013-03-29 11:39:50 -0500984 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530985}
986
987static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
988{
989 CharDriverState *chr = opaque;
990 FDCharDriver *s = chr->opaque;
991 int len;
992 uint8_t buf[READ_BUF_LEN];
993 GIOStatus status;
994 gsize bytes_read;
995
996 len = sizeof(buf);
997 if (len > s->max_size) {
998 len = s->max_size;
999 }
1000 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001001 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301002 }
1003
1004 status = g_io_channel_read_chars(chan, (gchar *)buf,
1005 len, &bytes_read, NULL);
1006 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301007 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301008 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1009 return FALSE;
1010 }
1011 if (status == G_IO_STATUS_NORMAL) {
1012 qemu_chr_be_write(chr, buf, bytes_read);
1013 }
1014
1015 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001016}
1017
1018static int fd_chr_read_poll(void *opaque)
1019{
1020 CharDriverState *chr = opaque;
1021 FDCharDriver *s = chr->opaque;
1022
Anthony Liguori909cda12011-08-15 11:17:31 -05001023 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001024 return s->max_size;
1025}
1026
Anthony Liguori23673ca2013-03-05 23:21:23 +05301027static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1028{
1029 FDCharDriver *s = chr->opaque;
1030 return g_io_create_watch(s->fd_out, cond);
1031}
1032
aliguori6f97dba2008-10-31 18:49:55 +00001033static void fd_chr_update_read_handler(CharDriverState *chr)
1034{
1035 FDCharDriver *s = chr->opaque;
1036
Amit Shah26da70c2013-08-28 15:23:37 +05301037 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301038 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301039 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1040 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001041 }
1042}
1043
1044static void fd_chr_close(struct CharDriverState *chr)
1045{
1046 FDCharDriver *s = chr->opaque;
1047
Amit Shah26da70c2013-08-28 15:23:37 +05301048 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301049 if (s->fd_in) {
1050 g_io_channel_unref(s->fd_in);
1051 }
1052 if (s->fd_out) {
1053 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001054 }
1055
Anthony Liguori7267c092011-08-20 22:09:37 -05001056 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001057 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001058}
1059
1060/* open a character device to a unix fd */
1061static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1062{
1063 CharDriverState *chr;
1064 FDCharDriver *s;
1065
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001066 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02001067 s = g_new0(FDCharDriver, 1);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301068 s->fd_in = io_channel_from_fd(fd_in);
1069 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001070 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301071 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001072 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301073 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001074 chr->chr_write = fd_chr_write;
1075 chr->chr_update_read_handler = fd_chr_update_read_handler;
1076 chr->chr_close = fd_chr_close;
1077
aliguori6f97dba2008-10-31 18:49:55 +00001078 return chr;
1079}
1080
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001081static CharDriverState *qemu_chr_open_pipe(const char *id,
1082 ChardevBackend *backend,
1083 ChardevReturn *ret,
1084 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001085{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001086 ChardevHostdev *opts = backend->pipe;
aliguori6f97dba2008-10-31 18:49:55 +00001087 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001088 char filename_in[CHR_MAX_FILENAME_SIZE];
1089 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001090 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001091
Corey Minyard9f781162014-10-02 11:17:33 -05001092 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1093 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001094 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1095 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001096 if (fd_in < 0 || fd_out < 0) {
1097 if (fd_in >= 0)
1098 close(fd_in);
1099 if (fd_out >= 0)
1100 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001101 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001102 if (fd_in < 0) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001103 error_setg_file_open(errp, errno, filename);
Markus Armbruster1f514702012-02-07 15:09:08 +01001104 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001105 }
aliguori6f97dba2008-10-31 18:49:55 +00001106 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001107 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001108}
1109
aliguori6f97dba2008-10-31 18:49:55 +00001110/* init terminal so that we can grab keys */
1111static struct termios oldtty;
1112static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001113static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001114static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001115static bool stdio_echo_state;
1116
1117static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001118
1119static void term_exit(void)
1120{
1121 tcsetattr (0, TCSANOW, &oldtty);
1122 fcntl(0, F_SETFL, old_fd0_flags);
1123}
1124
Gal Hammere76d4422015-01-07 10:38:35 +02001125static void term_stdio_handler(int sig)
1126{
1127 /* restore echo after resume from suspend. */
1128 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1129}
1130
Paolo Bonzinibb002512010-12-23 13:42:50 +01001131static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001132{
1133 struct termios tty;
1134
Gal Hammere76d4422015-01-07 10:38:35 +02001135 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001136 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001137 if (!echo) {
1138 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001139 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001140 tty.c_oflag |= OPOST;
1141 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1142 tty.c_cflag &= ~(CSIZE|PARENB);
1143 tty.c_cflag |= CS8;
1144 tty.c_cc[VMIN] = 1;
1145 tty.c_cc[VTIME] = 0;
1146 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001147 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001148 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001149
1150 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001151}
1152
1153static void qemu_chr_close_stdio(struct CharDriverState *chr)
1154{
1155 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001156 fd_chr_close(chr);
1157}
1158
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001159static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001160{
1161 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001162 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001163
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001164 if (is_daemonized()) {
1165 error_report("cannot use stdio with -daemonize");
1166 return NULL;
1167 }
Li Liuc88930a2014-09-09 19:19:48 +08001168
1169 if (stdio_in_use) {
1170 error_report("cannot use stdio by multiple character devices");
1171 exit(1);
1172 }
1173
1174 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301175 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001176 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001177 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301178 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001179
Gal Hammere76d4422015-01-07 10:38:35 +02001180 memset(&act, 0, sizeof(act));
1181 act.sa_handler = term_stdio_handler;
1182 sigaction(SIGCONT, &act, NULL);
1183
aliguori6f97dba2008-10-31 18:49:55 +00001184 chr = qemu_chr_open_fd(0, 1);
1185 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001186 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001187 if (opts->has_signal) {
1188 stdio_allow_signal = opts->signal;
1189 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001190 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001191
Markus Armbruster1f514702012-02-07 15:09:08 +01001192 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001193}
1194
aliguori6f97dba2008-10-31 18:49:55 +00001195#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001196 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1197 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001198
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001199#define HAVE_CHARDEV_SERIAL 1
1200#define HAVE_CHARDEV_PTY 1
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001201
aliguori6f97dba2008-10-31 18:49:55 +00001202typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301203 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001204 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001205
1206 /* Protected by the CharDriverState chr_write_lock. */
1207 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301208 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001209 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001210} PtyCharDriver;
1211
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001212static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001213static void pty_chr_state(CharDriverState *chr, int connected);
1214
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301215static gboolean pty_chr_timer(gpointer opaque)
1216{
1217 struct CharDriverState *chr = opaque;
1218 PtyCharDriver *s = chr->opaque;
1219
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001220 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001221 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001222 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001223 if (!s->connected) {
1224 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001225 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001226 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001227 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301228 return FALSE;
1229}
1230
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001231/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301232static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1233{
1234 PtyCharDriver *s = chr->opaque;
1235
1236 if (s->timer_tag) {
1237 g_source_remove(s->timer_tag);
1238 s->timer_tag = 0;
1239 }
1240
1241 if (ms == 1000) {
1242 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1243 } else {
1244 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1245 }
1246}
1247
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001248/* Called with chr_write_lock held. */
1249static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001250{
1251 PtyCharDriver *s = chr->opaque;
1252 GPollFD pfd;
1253
1254 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1255 pfd.events = G_IO_OUT;
1256 pfd.revents = 0;
1257 g_poll(&pfd, 1, 0);
1258 if (pfd.revents & G_IO_HUP) {
1259 pty_chr_state(chr, 0);
1260 } else {
1261 pty_chr_state(chr, 1);
1262 }
1263}
1264
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001265static void pty_chr_update_read_handler(CharDriverState *chr)
1266{
1267 qemu_mutex_lock(&chr->chr_write_lock);
1268 pty_chr_update_read_handler_locked(chr);
1269 qemu_mutex_unlock(&chr->chr_write_lock);
1270}
1271
1272/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001273static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1274{
1275 PtyCharDriver *s = chr->opaque;
1276
1277 if (!s->connected) {
1278 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001279 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001280 if (!s->connected) {
1281 return 0;
1282 }
aliguori6f97dba2008-10-31 18:49:55 +00001283 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001284 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001285}
1286
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301287static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1288{
1289 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001290 if (!s->connected) {
1291 return NULL;
1292 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301293 return g_io_create_watch(s->fd, cond);
1294}
1295
aliguori6f97dba2008-10-31 18:49:55 +00001296static int pty_chr_read_poll(void *opaque)
1297{
1298 CharDriverState *chr = opaque;
1299 PtyCharDriver *s = chr->opaque;
1300
Anthony Liguori909cda12011-08-15 11:17:31 -05001301 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001302 return s->read_bytes;
1303}
1304
Anthony Liguori093d3a22013-03-05 23:21:20 +05301305static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001306{
1307 CharDriverState *chr = opaque;
1308 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301309 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301310 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301311 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001312
1313 len = sizeof(buf);
1314 if (len > s->read_bytes)
1315 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001316 if (len == 0) {
1317 return TRUE;
1318 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301319 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1320 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001321 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301322 return FALSE;
1323 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001324 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001325 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001326 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301327 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001328}
1329
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001330static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1331{
1332 CharDriverState *chr = opaque;
1333 PtyCharDriver *s = chr->opaque;
1334
1335 s->open_tag = 0;
1336 qemu_chr_be_generic_open(chr);
1337 return FALSE;
1338}
1339
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001340/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001341static void pty_chr_state(CharDriverState *chr, int connected)
1342{
1343 PtyCharDriver *s = chr->opaque;
1344
1345 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001346 if (s->open_tag) {
1347 g_source_remove(s->open_tag);
1348 s->open_tag = 0;
1349 }
Amit Shah26da70c2013-08-28 15:23:37 +05301350 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001351 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001352 /* (re-)connect poll interval for idle guests: once per second.
1353 * We check more frequently in case the guests sends data to
1354 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301355 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001356 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001357 if (s->timer_tag) {
1358 g_source_remove(s->timer_tag);
1359 s->timer_tag = 0;
1360 }
1361 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001362 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001363 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001364 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001365 }
1366 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301367 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1368 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001369 }
aliguori6f97dba2008-10-31 18:49:55 +00001370 }
1371}
1372
aliguori6f97dba2008-10-31 18:49:55 +00001373static void pty_chr_close(struct CharDriverState *chr)
1374{
1375 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301376 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001377
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001378 qemu_mutex_lock(&chr->chr_write_lock);
1379 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301380 fd = g_io_channel_unix_get_fd(s->fd);
1381 g_io_channel_unref(s->fd);
1382 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301383 if (s->timer_tag) {
1384 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001385 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301386 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001387 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001388 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001389 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001390}
1391
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001392static CharDriverState *qemu_chr_open_pty(const char *id,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001393 ChardevBackend *backend,
1394 ChardevReturn *ret,
1395 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001396{
1397 CharDriverState *chr;
1398 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001399 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001400 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001401
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001402 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1403 if (master_fd < 0) {
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001404 error_setg_errno(errp, errno, "Failed to create PTY");
Markus Armbruster1f514702012-02-07 15:09:08 +01001405 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001406 }
1407
aliguori6f97dba2008-10-31 18:49:55 +00001408 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001409 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001410
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001411 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001412
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001413 chr->filename = g_strdup_printf("pty:%s", pty_name);
1414 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001415 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001416
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001417 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001418 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001419
Markus Armbruster2d528d42015-09-14 13:54:03 +02001420 s = g_new0(PtyCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001421 chr->opaque = s;
1422 chr->chr_write = pty_chr_write;
1423 chr->chr_update_read_handler = pty_chr_update_read_handler;
1424 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301425 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001426 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001427
Anthony Liguori093d3a22013-03-05 23:21:20 +05301428 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301429 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001430
Markus Armbruster1f514702012-02-07 15:09:08 +01001431 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001432}
1433
1434static void tty_serial_init(int fd, int speed,
1435 int parity, int data_bits, int stop_bits)
1436{
1437 struct termios tty;
1438 speed_t spd;
1439
1440#if 0
1441 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1442 speed, parity, data_bits, stop_bits);
1443#endif
1444 tcgetattr (fd, &tty);
1445
Stefan Weil45eea132009-10-26 16:10:10 +01001446#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1447 speed = speed * 10 / 11;
1448 do {
1449 check_speed(50);
1450 check_speed(75);
1451 check_speed(110);
1452 check_speed(134);
1453 check_speed(150);
1454 check_speed(200);
1455 check_speed(300);
1456 check_speed(600);
1457 check_speed(1200);
1458 check_speed(1800);
1459 check_speed(2400);
1460 check_speed(4800);
1461 check_speed(9600);
1462 check_speed(19200);
1463 check_speed(38400);
1464 /* Non-Posix values follow. They may be unsupported on some systems. */
1465 check_speed(57600);
1466 check_speed(115200);
1467#ifdef B230400
1468 check_speed(230400);
1469#endif
1470#ifdef B460800
1471 check_speed(460800);
1472#endif
1473#ifdef B500000
1474 check_speed(500000);
1475#endif
1476#ifdef B576000
1477 check_speed(576000);
1478#endif
1479#ifdef B921600
1480 check_speed(921600);
1481#endif
1482#ifdef B1000000
1483 check_speed(1000000);
1484#endif
1485#ifdef B1152000
1486 check_speed(1152000);
1487#endif
1488#ifdef B1500000
1489 check_speed(1500000);
1490#endif
1491#ifdef B2000000
1492 check_speed(2000000);
1493#endif
1494#ifdef B2500000
1495 check_speed(2500000);
1496#endif
1497#ifdef B3000000
1498 check_speed(3000000);
1499#endif
1500#ifdef B3500000
1501 check_speed(3500000);
1502#endif
1503#ifdef B4000000
1504 check_speed(4000000);
1505#endif
aliguori6f97dba2008-10-31 18:49:55 +00001506 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001507 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001508
1509 cfsetispeed(&tty, spd);
1510 cfsetospeed(&tty, spd);
1511
1512 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1513 |INLCR|IGNCR|ICRNL|IXON);
1514 tty.c_oflag |= OPOST;
1515 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1516 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1517 switch(data_bits) {
1518 default:
1519 case 8:
1520 tty.c_cflag |= CS8;
1521 break;
1522 case 7:
1523 tty.c_cflag |= CS7;
1524 break;
1525 case 6:
1526 tty.c_cflag |= CS6;
1527 break;
1528 case 5:
1529 tty.c_cflag |= CS5;
1530 break;
1531 }
1532 switch(parity) {
1533 default:
1534 case 'N':
1535 break;
1536 case 'E':
1537 tty.c_cflag |= PARENB;
1538 break;
1539 case 'O':
1540 tty.c_cflag |= PARENB | PARODD;
1541 break;
1542 }
1543 if (stop_bits == 2)
1544 tty.c_cflag |= CSTOPB;
1545
1546 tcsetattr (fd, TCSANOW, &tty);
1547}
1548
1549static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1550{
1551 FDCharDriver *s = chr->opaque;
1552
1553 switch(cmd) {
1554 case CHR_IOCTL_SERIAL_SET_PARAMS:
1555 {
1556 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301557 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1558 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001559 ssp->data_bits, ssp->stop_bits);
1560 }
1561 break;
1562 case CHR_IOCTL_SERIAL_SET_BREAK:
1563 {
1564 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301565 if (enable) {
1566 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1567 }
aliguori6f97dba2008-10-31 18:49:55 +00001568 }
1569 break;
1570 case CHR_IOCTL_SERIAL_GET_TIOCM:
1571 {
1572 int sarg = 0;
1573 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301574 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001575 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001576 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001577 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001578 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001579 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001580 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001581 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001582 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001583 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001584 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001585 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001586 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001587 *targ |= CHR_TIOCM_RTS;
1588 }
1589 break;
1590 case CHR_IOCTL_SERIAL_SET_TIOCM:
1591 {
1592 int sarg = *(int *)arg;
1593 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301594 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001595 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1596 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1597 if (sarg & CHR_TIOCM_CTS)
1598 targ |= TIOCM_CTS;
1599 if (sarg & CHR_TIOCM_CAR)
1600 targ |= TIOCM_CAR;
1601 if (sarg & CHR_TIOCM_DSR)
1602 targ |= TIOCM_DSR;
1603 if (sarg & CHR_TIOCM_RI)
1604 targ |= TIOCM_RI;
1605 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001606 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001607 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001608 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301609 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001610 }
1611 break;
1612 default:
1613 return -ENOTSUP;
1614 }
1615 return 0;
1616}
1617
David Ahern4266a132010-02-10 18:27:17 -07001618static void qemu_chr_close_tty(CharDriverState *chr)
1619{
1620 FDCharDriver *s = chr->opaque;
1621 int fd = -1;
1622
1623 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301624 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001625 }
1626
1627 fd_chr_close(chr);
1628
1629 if (fd >= 0) {
1630 close(fd);
1631 }
1632}
1633
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001634static CharDriverState *qemu_chr_open_tty_fd(int fd)
1635{
1636 CharDriverState *chr;
1637
1638 tty_serial_init(fd, 115200, 'N', 8, 1);
1639 chr = qemu_chr_open_fd(fd, fd);
1640 chr->chr_ioctl = tty_serial_ioctl;
1641 chr->chr_close = qemu_chr_close_tty;
1642 return chr;
1643}
aliguori6f97dba2008-10-31 18:49:55 +00001644#endif /* __linux__ || __sun__ */
1645
1646#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001647
1648#define HAVE_CHARDEV_PARPORT 1
1649
aliguori6f97dba2008-10-31 18:49:55 +00001650typedef struct {
1651 int fd;
1652 int mode;
1653} ParallelCharDriver;
1654
1655static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1656{
1657 if (s->mode != mode) {
1658 int m = mode;
1659 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1660 return 0;
1661 s->mode = mode;
1662 }
1663 return 1;
1664}
1665
1666static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1667{
1668 ParallelCharDriver *drv = chr->opaque;
1669 int fd = drv->fd;
1670 uint8_t b;
1671
1672 switch(cmd) {
1673 case CHR_IOCTL_PP_READ_DATA:
1674 if (ioctl(fd, PPRDATA, &b) < 0)
1675 return -ENOTSUP;
1676 *(uint8_t *)arg = b;
1677 break;
1678 case CHR_IOCTL_PP_WRITE_DATA:
1679 b = *(uint8_t *)arg;
1680 if (ioctl(fd, PPWDATA, &b) < 0)
1681 return -ENOTSUP;
1682 break;
1683 case CHR_IOCTL_PP_READ_CONTROL:
1684 if (ioctl(fd, PPRCONTROL, &b) < 0)
1685 return -ENOTSUP;
1686 /* Linux gives only the lowest bits, and no way to know data
1687 direction! For better compatibility set the fixed upper
1688 bits. */
1689 *(uint8_t *)arg = b | 0xc0;
1690 break;
1691 case CHR_IOCTL_PP_WRITE_CONTROL:
1692 b = *(uint8_t *)arg;
1693 if (ioctl(fd, PPWCONTROL, &b) < 0)
1694 return -ENOTSUP;
1695 break;
1696 case CHR_IOCTL_PP_READ_STATUS:
1697 if (ioctl(fd, PPRSTATUS, &b) < 0)
1698 return -ENOTSUP;
1699 *(uint8_t *)arg = b;
1700 break;
1701 case CHR_IOCTL_PP_DATA_DIR:
1702 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1703 return -ENOTSUP;
1704 break;
1705 case CHR_IOCTL_PP_EPP_READ_ADDR:
1706 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1707 struct ParallelIOArg *parg = arg;
1708 int n = read(fd, parg->buffer, parg->count);
1709 if (n != parg->count) {
1710 return -EIO;
1711 }
1712 }
1713 break;
1714 case CHR_IOCTL_PP_EPP_READ:
1715 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1716 struct ParallelIOArg *parg = arg;
1717 int n = read(fd, parg->buffer, parg->count);
1718 if (n != parg->count) {
1719 return -EIO;
1720 }
1721 }
1722 break;
1723 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1724 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1725 struct ParallelIOArg *parg = arg;
1726 int n = write(fd, parg->buffer, parg->count);
1727 if (n != parg->count) {
1728 return -EIO;
1729 }
1730 }
1731 break;
1732 case CHR_IOCTL_PP_EPP_WRITE:
1733 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1734 struct ParallelIOArg *parg = arg;
1735 int n = write(fd, parg->buffer, parg->count);
1736 if (n != parg->count) {
1737 return -EIO;
1738 }
1739 }
1740 break;
1741 default:
1742 return -ENOTSUP;
1743 }
1744 return 0;
1745}
1746
1747static void pp_close(CharDriverState *chr)
1748{
1749 ParallelCharDriver *drv = chr->opaque;
1750 int fd = drv->fd;
1751
1752 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1753 ioctl(fd, PPRELEASE);
1754 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001755 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001756 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001757}
1758
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001759static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001760{
1761 CharDriverState *chr;
1762 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001763
1764 if (ioctl(fd, PPCLAIM) < 0) {
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001765 error_setg_errno(errp, errno, "not a parallel port");
aliguori6f97dba2008-10-31 18:49:55 +00001766 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001767 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001768 }
1769
Markus Armbruster2d528d42015-09-14 13:54:03 +02001770 drv = g_new0(ParallelCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001771 drv->fd = fd;
1772 drv->mode = IEEE1284_MODE_COMPAT;
1773
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001774 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001775 chr->chr_write = null_chr_write;
1776 chr->chr_ioctl = pp_ioctl;
1777 chr->chr_close = pp_close;
1778 chr->opaque = drv;
1779
Markus Armbruster1f514702012-02-07 15:09:08 +01001780 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001781}
1782#endif /* __linux__ */
1783
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001784#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001785
1786#define HAVE_CHARDEV_PARPORT 1
1787
blueswir16972f932008-11-22 20:49:12 +00001788static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1789{
Stefan Weile0efb992011-02-23 19:09:16 +01001790 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001791 uint8_t b;
1792
1793 switch(cmd) {
1794 case CHR_IOCTL_PP_READ_DATA:
1795 if (ioctl(fd, PPIGDATA, &b) < 0)
1796 return -ENOTSUP;
1797 *(uint8_t *)arg = b;
1798 break;
1799 case CHR_IOCTL_PP_WRITE_DATA:
1800 b = *(uint8_t *)arg;
1801 if (ioctl(fd, PPISDATA, &b) < 0)
1802 return -ENOTSUP;
1803 break;
1804 case CHR_IOCTL_PP_READ_CONTROL:
1805 if (ioctl(fd, PPIGCTRL, &b) < 0)
1806 return -ENOTSUP;
1807 *(uint8_t *)arg = b;
1808 break;
1809 case CHR_IOCTL_PP_WRITE_CONTROL:
1810 b = *(uint8_t *)arg;
1811 if (ioctl(fd, PPISCTRL, &b) < 0)
1812 return -ENOTSUP;
1813 break;
1814 case CHR_IOCTL_PP_READ_STATUS:
1815 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1816 return -ENOTSUP;
1817 *(uint8_t *)arg = b;
1818 break;
1819 default:
1820 return -ENOTSUP;
1821 }
1822 return 0;
1823}
1824
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001825static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
blueswir16972f932008-11-22 20:49:12 +00001826{
1827 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001828
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001829 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001830 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001831 chr->chr_write = null_chr_write;
1832 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001833 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001834 return chr;
blueswir16972f932008-11-22 20:49:12 +00001835}
1836#endif
1837
aliguori6f97dba2008-10-31 18:49:55 +00001838#else /* _WIN32 */
1839
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001840#define HAVE_CHARDEV_SERIAL 1
1841
aliguori6f97dba2008-10-31 18:49:55 +00001842typedef struct {
1843 int max_size;
1844 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001845 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001846 BOOL fpipe;
1847 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001848
1849 /* Protected by the CharDriverState chr_write_lock. */
1850 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001851} WinCharState;
1852
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001853typedef struct {
1854 HANDLE hStdIn;
1855 HANDLE hInputReadyEvent;
1856 HANDLE hInputDoneEvent;
1857 HANDLE hInputThread;
1858 uint8_t win_stdio_buf;
1859} WinStdioCharState;
1860
aliguori6f97dba2008-10-31 18:49:55 +00001861#define NSENDBUF 2048
1862#define NRECVBUF 2048
1863#define MAXCONNECT 1
1864#define NTIMEOUT 5000
1865
1866static int win_chr_poll(void *opaque);
1867static int win_chr_pipe_poll(void *opaque);
1868
1869static void win_chr_close(CharDriverState *chr)
1870{
1871 WinCharState *s = chr->opaque;
1872
1873 if (s->hsend) {
1874 CloseHandle(s->hsend);
1875 s->hsend = NULL;
1876 }
1877 if (s->hrecv) {
1878 CloseHandle(s->hrecv);
1879 s->hrecv = NULL;
1880 }
1881 if (s->hcom) {
1882 CloseHandle(s->hcom);
1883 s->hcom = NULL;
1884 }
1885 if (s->fpipe)
1886 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1887 else
1888 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301889
Hans de Goedea425d232011-11-19 10:22:43 +01001890 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001891}
1892
Paolo Bonzini6511d392015-09-29 15:08:05 +02001893static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001894{
1895 WinCharState *s = chr->opaque;
1896 COMMCONFIG comcfg;
1897 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1898 COMSTAT comstat;
1899 DWORD size;
1900 DWORD err;
1901
1902 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1903 if (!s->hsend) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001904 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001905 goto fail;
1906 }
1907 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1908 if (!s->hrecv) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001909 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001910 goto fail;
1911 }
1912
1913 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1914 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1915 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001916 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00001917 s->hcom = NULL;
1918 goto fail;
1919 }
1920
1921 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001922 error_setg(errp, "Failed SetupComm");
aliguori6f97dba2008-10-31 18:49:55 +00001923 goto fail;
1924 }
1925
1926 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1927 size = sizeof(COMMCONFIG);
1928 GetDefaultCommConfig(filename, &comcfg, &size);
1929 comcfg.dcb.DCBlength = sizeof(DCB);
1930 CommConfigDialog(filename, NULL, &comcfg);
1931
1932 if (!SetCommState(s->hcom, &comcfg.dcb)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001933 error_setg(errp, "Failed SetCommState");
aliguori6f97dba2008-10-31 18:49:55 +00001934 goto fail;
1935 }
1936
1937 if (!SetCommMask(s->hcom, EV_ERR)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001938 error_setg(errp, "Failed SetCommMask");
aliguori6f97dba2008-10-31 18:49:55 +00001939 goto fail;
1940 }
1941
1942 cto.ReadIntervalTimeout = MAXDWORD;
1943 if (!SetCommTimeouts(s->hcom, &cto)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001944 error_setg(errp, "Failed SetCommTimeouts");
aliguori6f97dba2008-10-31 18:49:55 +00001945 goto fail;
1946 }
1947
1948 if (!ClearCommError(s->hcom, &err, &comstat)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001949 error_setg(errp, "Failed ClearCommError");
aliguori6f97dba2008-10-31 18:49:55 +00001950 goto fail;
1951 }
1952 qemu_add_polling_cb(win_chr_poll, chr);
1953 return 0;
1954
1955 fail:
1956 win_chr_close(chr);
1957 return -1;
1958}
1959
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001960/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001961static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1962{
1963 WinCharState *s = chr->opaque;
1964 DWORD len, ret, size, err;
1965
1966 len = len1;
1967 ZeroMemory(&s->osend, sizeof(s->osend));
1968 s->osend.hEvent = s->hsend;
1969 while (len > 0) {
1970 if (s->hsend)
1971 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1972 else
1973 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1974 if (!ret) {
1975 err = GetLastError();
1976 if (err == ERROR_IO_PENDING) {
1977 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1978 if (ret) {
1979 buf += size;
1980 len -= size;
1981 } else {
1982 break;
1983 }
1984 } else {
1985 break;
1986 }
1987 } else {
1988 buf += size;
1989 len -= size;
1990 }
1991 }
1992 return len1 - len;
1993}
1994
1995static int win_chr_read_poll(CharDriverState *chr)
1996{
1997 WinCharState *s = chr->opaque;
1998
Anthony Liguori909cda12011-08-15 11:17:31 -05001999 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002000 return s->max_size;
2001}
2002
2003static void win_chr_readfile(CharDriverState *chr)
2004{
2005 WinCharState *s = chr->opaque;
2006 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302007 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002008 DWORD size;
2009
2010 ZeroMemory(&s->orecv, sizeof(s->orecv));
2011 s->orecv.hEvent = s->hrecv;
2012 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2013 if (!ret) {
2014 err = GetLastError();
2015 if (err == ERROR_IO_PENDING) {
2016 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2017 }
2018 }
2019
2020 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002021 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002022 }
2023}
2024
2025static void win_chr_read(CharDriverState *chr)
2026{
2027 WinCharState *s = chr->opaque;
2028
2029 if (s->len > s->max_size)
2030 s->len = s->max_size;
2031 if (s->len == 0)
2032 return;
2033
2034 win_chr_readfile(chr);
2035}
2036
2037static int win_chr_poll(void *opaque)
2038{
2039 CharDriverState *chr = opaque;
2040 WinCharState *s = chr->opaque;
2041 COMSTAT status;
2042 DWORD comerr;
2043
2044 ClearCommError(s->hcom, &comerr, &status);
2045 if (status.cbInQue > 0) {
2046 s->len = status.cbInQue;
2047 win_chr_read_poll(chr);
2048 win_chr_read(chr);
2049 return 1;
2050 }
2051 return 0;
2052}
2053
Paolo Bonzini6511d392015-09-29 15:08:05 +02002054static CharDriverState *qemu_chr_open_win_path(const char *filename,
2055 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002056{
2057 CharDriverState *chr;
2058 WinCharState *s;
2059
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002060 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002061 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002062 chr->opaque = s;
2063 chr->chr_write = win_chr_write;
2064 chr->chr_close = win_chr_close;
2065
Paolo Bonzini6511d392015-09-29 15:08:05 +02002066 if (win_chr_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002067 g_free(s);
2068 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002069 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002070 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002071 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002072}
2073
2074static int win_chr_pipe_poll(void *opaque)
2075{
2076 CharDriverState *chr = opaque;
2077 WinCharState *s = chr->opaque;
2078 DWORD size;
2079
2080 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2081 if (size > 0) {
2082 s->len = size;
2083 win_chr_read_poll(chr);
2084 win_chr_read(chr);
2085 return 1;
2086 }
2087 return 0;
2088}
2089
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002090static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
2091 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002092{
2093 WinCharState *s = chr->opaque;
2094 OVERLAPPED ov;
2095 int ret;
2096 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002097 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002098
2099 s->fpipe = TRUE;
2100
2101 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2102 if (!s->hsend) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002103 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002104 goto fail;
2105 }
2106 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2107 if (!s->hrecv) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002108 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002109 goto fail;
2110 }
2111
2112 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2113 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2114 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2115 PIPE_WAIT,
2116 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2117 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002118 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00002119 s->hcom = NULL;
2120 goto fail;
2121 }
2122
2123 ZeroMemory(&ov, sizeof(ov));
2124 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2125 ret = ConnectNamedPipe(s->hcom, &ov);
2126 if (ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002127 error_setg(errp, "Failed ConnectNamedPipe");
aliguori6f97dba2008-10-31 18:49:55 +00002128 goto fail;
2129 }
2130
2131 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2132 if (!ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002133 error_setg(errp, "Failed GetOverlappedResult");
aliguori6f97dba2008-10-31 18:49:55 +00002134 if (ov.hEvent) {
2135 CloseHandle(ov.hEvent);
2136 ov.hEvent = NULL;
2137 }
2138 goto fail;
2139 }
2140
2141 if (ov.hEvent) {
2142 CloseHandle(ov.hEvent);
2143 ov.hEvent = NULL;
2144 }
2145 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2146 return 0;
2147
2148 fail:
2149 win_chr_close(chr);
2150 return -1;
2151}
2152
2153
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002154static CharDriverState *qemu_chr_open_pipe(const char *id,
2155 ChardevBackend *backend,
2156 ChardevReturn *ret,
2157 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002158{
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002159 ChardevHostdev *opts = backend->pipe;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002160 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002161 CharDriverState *chr;
2162 WinCharState *s;
2163
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002164 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002165 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002166 chr->opaque = s;
2167 chr->chr_write = win_chr_write;
2168 chr->chr_close = win_chr_close;
2169
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002170 if (win_chr_pipe_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002171 g_free(s);
2172 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002173 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002174 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002175 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002176}
2177
Markus Armbruster1f514702012-02-07 15:09:08 +01002178static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002179{
2180 CharDriverState *chr;
2181 WinCharState *s;
2182
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002183 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002184 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002185 s->hcom = fd_out;
2186 chr->opaque = s;
2187 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002188 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002189}
2190
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002191static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002192{
Markus Armbruster1f514702012-02-07 15:09:08 +01002193 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002194}
2195
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002196static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2197{
2198 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2199 DWORD dwSize;
2200 int len1;
2201
2202 len1 = len;
2203
2204 while (len1 > 0) {
2205 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2206 break;
2207 }
2208 buf += dwSize;
2209 len1 -= dwSize;
2210 }
2211
2212 return len - len1;
2213}
2214
2215static void win_stdio_wait_func(void *opaque)
2216{
2217 CharDriverState *chr = opaque;
2218 WinStdioCharState *stdio = chr->opaque;
2219 INPUT_RECORD buf[4];
2220 int ret;
2221 DWORD dwSize;
2222 int i;
2223
Stefan Weildff74242013-12-07 14:48:04 +01002224 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002225
2226 if (!ret) {
2227 /* Avoid error storm */
2228 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2229 return;
2230 }
2231
2232 for (i = 0; i < dwSize; i++) {
2233 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2234
2235 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2236 int j;
2237 if (kev->uChar.AsciiChar != 0) {
2238 for (j = 0; j < kev->wRepeatCount; j++) {
2239 if (qemu_chr_be_can_write(chr)) {
2240 uint8_t c = kev->uChar.AsciiChar;
2241 qemu_chr_be_write(chr, &c, 1);
2242 }
2243 }
2244 }
2245 }
2246 }
2247}
2248
2249static DWORD WINAPI win_stdio_thread(LPVOID param)
2250{
2251 CharDriverState *chr = param;
2252 WinStdioCharState *stdio = chr->opaque;
2253 int ret;
2254 DWORD dwSize;
2255
2256 while (1) {
2257
2258 /* Wait for one byte */
2259 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2260
2261 /* Exit in case of error, continue if nothing read */
2262 if (!ret) {
2263 break;
2264 }
2265 if (!dwSize) {
2266 continue;
2267 }
2268
2269 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2270 if (stdio->win_stdio_buf == '\r') {
2271 continue;
2272 }
2273
2274 /* Signal the main thread and wait until the byte was eaten */
2275 if (!SetEvent(stdio->hInputReadyEvent)) {
2276 break;
2277 }
2278 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2279 != WAIT_OBJECT_0) {
2280 break;
2281 }
2282 }
2283
2284 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2285 return 0;
2286}
2287
2288static void win_stdio_thread_wait_func(void *opaque)
2289{
2290 CharDriverState *chr = opaque;
2291 WinStdioCharState *stdio = chr->opaque;
2292
2293 if (qemu_chr_be_can_write(chr)) {
2294 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2295 }
2296
2297 SetEvent(stdio->hInputDoneEvent);
2298}
2299
2300static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2301{
2302 WinStdioCharState *stdio = chr->opaque;
2303 DWORD dwMode = 0;
2304
2305 GetConsoleMode(stdio->hStdIn, &dwMode);
2306
2307 if (echo) {
2308 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2309 } else {
2310 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2311 }
2312}
2313
2314static void win_stdio_close(CharDriverState *chr)
2315{
2316 WinStdioCharState *stdio = chr->opaque;
2317
2318 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2319 CloseHandle(stdio->hInputReadyEvent);
2320 }
2321 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2322 CloseHandle(stdio->hInputDoneEvent);
2323 }
2324 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2325 TerminateThread(stdio->hInputThread, 0);
2326 }
2327
2328 g_free(chr->opaque);
2329 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002330}
2331
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002332static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002333{
2334 CharDriverState *chr;
2335 WinStdioCharState *stdio;
2336 DWORD dwMode;
2337 int is_console = 0;
2338
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002339 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002340 stdio = g_new0(WinStdioCharState, 1);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002341
2342 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2343 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2344 fprintf(stderr, "cannot open stdio: invalid handle\n");
2345 exit(1);
2346 }
2347
2348 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2349
2350 chr->opaque = stdio;
2351 chr->chr_write = win_stdio_write;
2352 chr->chr_close = win_stdio_close;
2353
Anthony Liguoried7a1542013-03-05 23:21:17 +05302354 if (is_console) {
2355 if (qemu_add_wait_object(stdio->hStdIn,
2356 win_stdio_wait_func, chr)) {
2357 fprintf(stderr, "qemu_add_wait_object: failed\n");
2358 }
2359 } else {
2360 DWORD dwId;
2361
2362 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2363 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2364 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2365 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002366
Anthony Liguoried7a1542013-03-05 23:21:17 +05302367 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2368 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2369 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2370 fprintf(stderr, "cannot create stdio thread or event\n");
2371 exit(1);
2372 }
2373 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2374 win_stdio_thread_wait_func, chr)) {
2375 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002376 }
2377 }
2378
2379 dwMode |= ENABLE_LINE_INPUT;
2380
Anthony Liguoried7a1542013-03-05 23:21:17 +05302381 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002382 /* set the terminal in raw mode */
2383 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2384 dwMode |= ENABLE_PROCESSED_INPUT;
2385 }
2386
2387 SetConsoleMode(stdio->hStdIn, dwMode);
2388
2389 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2390 qemu_chr_fe_set_echo(chr, false);
2391
Markus Armbruster1f514702012-02-07 15:09:08 +01002392 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002393}
aliguori6f97dba2008-10-31 18:49:55 +00002394#endif /* !_WIN32 */
2395
Anthony Liguori5ab82112013-03-05 23:21:31 +05302396
aliguori6f97dba2008-10-31 18:49:55 +00002397/***********************************************************/
2398/* UDP Net console */
2399
2400typedef struct {
2401 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302402 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302403 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002404 int bufcnt;
2405 int bufptr;
2406 int max_size;
2407} NetCharDriver;
2408
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002409/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002410static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2411{
2412 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302413 gsize bytes_written;
2414 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002415
Anthony Liguori76a96442013-03-05 23:21:21 +05302416 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2417 if (status == G_IO_STATUS_EOF) {
2418 return 0;
2419 } else if (status != G_IO_STATUS_NORMAL) {
2420 return -1;
2421 }
2422
2423 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002424}
2425
2426static int udp_chr_read_poll(void *opaque)
2427{
2428 CharDriverState *chr = opaque;
2429 NetCharDriver *s = chr->opaque;
2430
Anthony Liguori909cda12011-08-15 11:17:31 -05002431 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002432
2433 /* If there were any stray characters in the queue process them
2434 * first
2435 */
2436 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002437 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002438 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002439 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002440 }
2441 return s->max_size;
2442}
2443
Anthony Liguori76a96442013-03-05 23:21:21 +05302444static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002445{
2446 CharDriverState *chr = opaque;
2447 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302448 gsize bytes_read = 0;
2449 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002450
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002451 if (s->max_size == 0) {
2452 return TRUE;
2453 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302454 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2455 &bytes_read, NULL);
2456 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002457 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302458 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302459 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302460 return FALSE;
2461 }
aliguori6f97dba2008-10-31 18:49:55 +00002462
2463 s->bufptr = 0;
2464 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002465 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002466 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002467 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002468 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302469
2470 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002471}
2472
2473static void udp_chr_update_read_handler(CharDriverState *chr)
2474{
2475 NetCharDriver *s = chr->opaque;
2476
Amit Shah26da70c2013-08-28 15:23:37 +05302477 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302478 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302479 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2480 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002481 }
2482}
2483
aliguori819f56b2009-03-28 17:58:14 +00002484static void udp_chr_close(CharDriverState *chr)
2485{
2486 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302487
2488 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302489 if (s->chan) {
2490 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002491 closesocket(s->fd);
2492 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002493 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002494 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002495}
2496
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002497static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002498{
2499 CharDriverState *chr = NULL;
2500 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002501
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002502 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002503 s = g_new0(NetCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002504
aliguori6f97dba2008-10-31 18:49:55 +00002505 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302506 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002507 s->bufcnt = 0;
2508 s->bufptr = 0;
2509 chr->opaque = s;
2510 chr->chr_write = udp_chr_write;
2511 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002512 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002513 /* be isn't opened until we get a connection */
2514 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002515 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002516}
aliguori6f97dba2008-10-31 18:49:55 +00002517
aliguori6f97dba2008-10-31 18:49:55 +00002518/***********************************************************/
2519/* TCP Net console */
2520
2521typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302522
2523 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302524 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002525 int fd, listen_fd;
2526 int connected;
2527 int max_size;
2528 int do_telnetopt;
2529 int do_nodelay;
2530 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002531 int *read_msgfds;
2532 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002533 int *write_msgfds;
2534 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002535
2536 SocketAddress *addr;
2537 bool is_listen;
2538 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002539
2540 guint reconnect_timer;
2541 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002542 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002543} TCPCharDriver;
2544
Corey Minyard5dd1f022014-10-02 11:17:37 -05002545static gboolean socket_reconnect_timeout(gpointer opaque);
2546
2547static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2548{
2549 TCPCharDriver *s = chr->opaque;
2550 assert(s->connected == 0);
2551 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2552 socket_reconnect_timeout, chr);
2553}
2554
Corey Minyard5008e5b2014-10-08 07:11:55 -05002555static void check_report_connect_error(CharDriverState *chr,
2556 Error *err)
2557{
2558 TCPCharDriver *s = chr->opaque;
2559
2560 if (!s->connect_err_reported) {
2561 error_report("Unable to connect character device %s: %s",
2562 chr->label, error_get_pretty(err));
2563 s->connect_err_reported = true;
2564 }
2565 qemu_chr_socket_restart_timer(chr);
2566}
2567
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302568static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002569
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002570#ifndef _WIN32
2571static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2572{
2573 TCPCharDriver *s = chr->opaque;
2574 struct msghdr msgh;
2575 struct iovec iov;
2576 int r;
2577
2578 size_t fd_size = s->write_msgfds_num * sizeof(int);
2579 char control[CMSG_SPACE(fd_size)];
2580 struct cmsghdr *cmsg;
2581
2582 memset(&msgh, 0, sizeof(msgh));
2583 memset(control, 0, sizeof(control));
2584
2585 /* set the payload */
2586 iov.iov_base = (uint8_t *) buf;
2587 iov.iov_len = len;
2588
2589 msgh.msg_iov = &iov;
2590 msgh.msg_iovlen = 1;
2591
2592 msgh.msg_control = control;
2593 msgh.msg_controllen = sizeof(control);
2594
2595 cmsg = CMSG_FIRSTHDR(&msgh);
2596
2597 cmsg->cmsg_len = CMSG_LEN(fd_size);
2598 cmsg->cmsg_level = SOL_SOCKET;
2599 cmsg->cmsg_type = SCM_RIGHTS;
2600 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2601
2602 do {
2603 r = sendmsg(s->fd, &msgh, 0);
2604 } while (r < 0 && errno == EINTR);
2605
2606 /* free the written msgfds, no matter what */
2607 if (s->write_msgfds_num) {
2608 g_free(s->write_msgfds);
2609 s->write_msgfds = 0;
2610 s->write_msgfds_num = 0;
2611 }
2612
2613 return r;
2614}
2615#endif
2616
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002617/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002618static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2619{
2620 TCPCharDriver *s = chr->opaque;
2621 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002622#ifndef _WIN32
2623 if (s->is_unix && s->write_msgfds_num) {
2624 return unix_send_msgfds(chr, buf, len);
2625 } else
2626#endif
2627 {
2628 return io_channel_send(s->chan, buf, len);
2629 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002630 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002631 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002632 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002633 }
2634}
2635
2636static int tcp_chr_read_poll(void *opaque)
2637{
2638 CharDriverState *chr = opaque;
2639 TCPCharDriver *s = chr->opaque;
2640 if (!s->connected)
2641 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002642 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002643 return s->max_size;
2644}
2645
2646#define IAC 255
2647#define IAC_BREAK 243
2648static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2649 TCPCharDriver *s,
2650 uint8_t *buf, int *size)
2651{
2652 /* Handle any telnet client's basic IAC options to satisfy char by
2653 * char mode with no echo. All IAC options will be removed from
2654 * the buf and the do_telnetopt variable will be used to track the
2655 * state of the width of the IAC information.
2656 *
2657 * IAC commands come in sets of 3 bytes with the exception of the
2658 * "IAC BREAK" command and the double IAC.
2659 */
2660
2661 int i;
2662 int j = 0;
2663
2664 for (i = 0; i < *size; i++) {
2665 if (s->do_telnetopt > 1) {
2666 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2667 /* Double IAC means send an IAC */
2668 if (j != i)
2669 buf[j] = buf[i];
2670 j++;
2671 s->do_telnetopt = 1;
2672 } else {
2673 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2674 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002675 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002676 s->do_telnetopt++;
2677 }
2678 s->do_telnetopt++;
2679 }
2680 if (s->do_telnetopt >= 4) {
2681 s->do_telnetopt = 1;
2682 }
2683 } else {
2684 if ((unsigned char)buf[i] == IAC) {
2685 s->do_telnetopt = 2;
2686 } else {
2687 if (j != i)
2688 buf[j] = buf[i];
2689 j++;
2690 }
2691 }
2692 }
2693 *size = j;
2694}
2695
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002696static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002697{
2698 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002699 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2700
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002701 assert(num <= TCP_MAX_FDS);
2702
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002703 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002704 int i;
2705
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002706 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2707
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002708 /* Close unused fds */
2709 for (i = to_copy; i < s->read_msgfds_num; i++) {
2710 close(s->read_msgfds[i]);
2711 }
2712
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002713 g_free(s->read_msgfds);
2714 s->read_msgfds = 0;
2715 s->read_msgfds_num = 0;
2716 }
2717
2718 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002719}
2720
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002721static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2722{
2723 TCPCharDriver *s = chr->opaque;
2724
2725 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002726 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002727
2728 if (num) {
Markus Armbruster2d528d42015-09-14 13:54:03 +02002729 s->write_msgfds = g_new(int, num);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002730 memcpy(s->write_msgfds, fds, num * sizeof(int));
2731 }
2732
2733 s->write_msgfds_num = num;
2734
2735 return 0;
2736}
2737
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002738#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002739static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2740{
2741 TCPCharDriver *s = chr->opaque;
2742 struct cmsghdr *cmsg;
2743
2744 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002745 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002746
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002747 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002748 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002749 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002750 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002751 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002752
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002753 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2754
2755 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002756 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002757 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002758
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002759 /* close and clean read_msgfds */
2760 for (i = 0; i < s->read_msgfds_num; i++) {
2761 close(s->read_msgfds[i]);
2762 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002763
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002764 if (s->read_msgfds_num) {
2765 g_free(s->read_msgfds);
2766 }
2767
2768 s->read_msgfds_num = fd_size / sizeof(int);
2769 s->read_msgfds = g_malloc(fd_size);
2770 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2771
2772 for (i = 0; i < s->read_msgfds_num; i++) {
2773 int fd = s->read_msgfds[i];
2774 if (fd < 0) {
2775 continue;
2776 }
2777
2778 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2779 qemu_set_block(fd);
2780
2781 #ifndef MSG_CMSG_CLOEXEC
2782 qemu_set_cloexec(fd);
2783 #endif
2784 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002785 }
2786}
2787
Mark McLoughlin9977c892009-07-22 09:11:38 +01002788static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2789{
2790 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002791 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002792 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002793 union {
2794 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002795 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002796 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002797 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002798 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002799
2800 iov[0].iov_base = buf;
2801 iov[0].iov_len = len;
2802
2803 msg.msg_iov = iov;
2804 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002805 msg.msg_control = &msg_control;
2806 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002807
Corey Bryant06138652012-08-14 16:43:42 -04002808#ifdef MSG_CMSG_CLOEXEC
2809 flags |= MSG_CMSG_CLOEXEC;
2810#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002811 do {
2812 ret = recvmsg(s->fd, &msg, flags);
2813 } while (ret == -1 && errno == EINTR);
2814
Corey Bryant06138652012-08-14 16:43:42 -04002815 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002816 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002817 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002818
2819 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002820}
2821#else
2822static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2823{
2824 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002825 ssize_t ret;
2826
2827 do {
2828 ret = qemu_recv(s->fd, buf, len, 0);
2829 } while (ret == -1 && socket_error() == EINTR);
2830
2831 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002832}
2833#endif
2834
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302835static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2836{
2837 TCPCharDriver *s = chr->opaque;
2838 return g_io_create_watch(s->chan, cond);
2839}
2840
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002841static void tcp_chr_disconnect(CharDriverState *chr)
2842{
2843 TCPCharDriver *s = chr->opaque;
2844
2845 s->connected = 0;
2846 if (s->listen_chan) {
2847 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2848 tcp_chr_accept, chr);
2849 }
2850 remove_fd_in_watch(chr);
2851 g_io_channel_unref(s->chan);
2852 s->chan = NULL;
2853 closesocket(s->fd);
2854 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002855 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2856 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002857 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002858 if (s->reconnect_time) {
2859 qemu_chr_socket_restart_timer(chr);
2860 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002861}
2862
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302863static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002864{
2865 CharDriverState *chr = opaque;
2866 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302867 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002868 int len, size;
2869
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302870 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002871 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302872 }
aliguori6f97dba2008-10-31 18:49:55 +00002873 len = sizeof(buf);
2874 if (len > s->max_size)
2875 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002876 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002877 if (size == 0 ||
2878 (size < 0 &&
2879 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002880 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002881 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002882 } else if (size > 0) {
2883 if (s->do_telnetopt)
2884 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2885 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002886 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002887 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302888
2889 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002890}
2891
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002892static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2893{
2894 TCPCharDriver *s = chr->opaque;
2895 int size;
2896
2897 if (!s->connected) {
2898 return 0;
2899 }
2900
2901 size = tcp_chr_recv(chr, (void *) buf, len);
2902 if (size == 0) {
2903 /* connection closed */
2904 tcp_chr_disconnect(chr);
2905 }
2906
2907 return size;
2908}
2909
Blue Swirl68c18d12010-08-15 09:46:24 +00002910#ifndef _WIN32
2911CharDriverState *qemu_chr_open_eventfd(int eventfd)
2912{
David Marchande9d21c42014-06-11 17:25:16 +02002913 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2914
2915 if (chr) {
2916 chr->avail_connections = 1;
2917 }
2918
2919 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002920}
Blue Swirl68c18d12010-08-15 09:46:24 +00002921#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002922
aliguori6f97dba2008-10-31 18:49:55 +00002923static void tcp_chr_connect(void *opaque)
2924{
2925 CharDriverState *chr = opaque;
2926 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002927 struct sockaddr_storage ss, ps;
2928 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002929
2930 memset(&ss, 0, ss_len);
2931 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2932 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2933 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002934 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2935 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2936 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002937 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002938 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2939 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002940 s->is_listen, s->is_telnet);
2941 }
aliguori6f97dba2008-10-31 18:49:55 +00002942
2943 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302944 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302945 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2946 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002947 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002948 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002949}
2950
Gal Hammerac1b84d2014-02-25 12:12:35 +02002951static void tcp_chr_update_read_handler(CharDriverState *chr)
2952{
2953 TCPCharDriver *s = chr->opaque;
2954
2955 remove_fd_in_watch(chr);
2956 if (s->chan) {
2957 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2958 tcp_chr_read, chr);
2959 }
2960}
2961
aliguori6f97dba2008-10-31 18:49:55 +00002962#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2963static void tcp_chr_telnet_init(int fd)
2964{
2965 char buf[3];
2966 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2967 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2968 send(fd, (char *)buf, 3, 0);
2969 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2970 send(fd, (char *)buf, 3, 0);
2971 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2972 send(fd, (char *)buf, 3, 0);
2973 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2974 send(fd, (char *)buf, 3, 0);
2975}
2976
Daniel P. Berrange13661082011-06-23 13:31:42 +01002977static int tcp_chr_add_client(CharDriverState *chr, int fd)
2978{
2979 TCPCharDriver *s = chr->opaque;
2980 if (s->fd != -1)
2981 return -1;
2982
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002983 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002984 if (s->do_nodelay)
2985 socket_set_nodelay(fd);
2986 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302987 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002988 if (s->listen_tag) {
2989 g_source_remove(s->listen_tag);
2990 s->listen_tag = 0;
2991 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002992 tcp_chr_connect(chr);
2993
2994 return 0;
2995}
2996
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302997static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002998{
2999 CharDriverState *chr = opaque;
3000 TCPCharDriver *s = chr->opaque;
3001 struct sockaddr_in saddr;
3002#ifndef _WIN32
3003 struct sockaddr_un uaddr;
3004#endif
3005 struct sockaddr *addr;
3006 socklen_t len;
3007 int fd;
3008
3009 for(;;) {
3010#ifndef _WIN32
3011 if (s->is_unix) {
3012 len = sizeof(uaddr);
3013 addr = (struct sockaddr *)&uaddr;
3014 } else
3015#endif
3016 {
3017 len = sizeof(saddr);
3018 addr = (struct sockaddr *)&saddr;
3019 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003020 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003021 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003022 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303023 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003024 } else if (fd >= 0) {
3025 if (s->do_telnetopt)
3026 tcp_chr_telnet_init(fd);
3027 break;
3028 }
3029 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003030 if (tcp_chr_add_client(chr, fd) < 0)
3031 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303032
3033 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003034}
3035
3036static void tcp_chr_close(CharDriverState *chr)
3037{
3038 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003039 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003040
Corey Minyard5dd1f022014-10-02 11:17:37 -05003041 if (s->reconnect_timer) {
3042 g_source_remove(s->reconnect_timer);
3043 s->reconnect_timer = 0;
3044 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003045 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003046 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303047 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303048 if (s->chan) {
3049 g_io_channel_unref(s->chan);
3050 }
aliguori6f97dba2008-10-31 18:49:55 +00003051 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003052 }
3053 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303054 if (s->listen_tag) {
3055 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003056 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303057 }
3058 if (s->listen_chan) {
3059 g_io_channel_unref(s->listen_chan);
3060 }
aliguori6f97dba2008-10-31 18:49:55 +00003061 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003062 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003063 if (s->read_msgfds_num) {
3064 for (i = 0; i < s->read_msgfds_num; i++) {
3065 close(s->read_msgfds[i]);
3066 }
3067 g_free(s->read_msgfds);
3068 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003069 if (s->write_msgfds_num) {
3070 g_free(s->write_msgfds);
3071 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003072 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003073 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003074}
3075
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003076static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003077{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003078 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003079
Corey Minyardcfb429c2014-10-02 11:17:35 -05003080 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003081 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303082 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003083 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3084 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003085 } else {
3086 s->connected = 1;
3087 s->fd = fd;
3088 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303089 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003090 tcp_chr_connect(chr);
3091 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003092}
3093
Corey Minyard51795022014-10-08 07:11:56 -05003094static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003095{
3096 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003097 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003098
3099 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003100 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003101 return;
3102 }
3103
Corey Minyard5008e5b2014-10-08 07:11:55 -05003104 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003105 qemu_chr_finish_socket_connection(chr, fd);
3106}
3107
Corey Minyardcfb429c2014-10-02 11:17:35 -05003108static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003109{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003110 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003111 int fd;
3112
Corey Minyardcfb429c2014-10-02 11:17:35 -05003113 if (s->is_listen) {
3114 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003115 } else if (s->reconnect_time) {
3116 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3117 return fd >= 0;
3118 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003119 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003120 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003121 if (fd < 0) {
3122 return false;
3123 }
3124
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003125 qemu_chr_finish_socket_connection(chr, fd);
3126 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003127}
3128
Lei Li51767e72013-01-25 00:03:19 +08003129/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003130/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003131
3132typedef struct {
3133 size_t size;
3134 size_t prod;
3135 size_t cons;
3136 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003137} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003138
Markus Armbruster3949e592013-02-06 21:27:24 +01003139static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003140{
Markus Armbruster3949e592013-02-06 21:27:24 +01003141 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003142
Markus Armbruster5c230102013-02-06 21:27:23 +01003143 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003144}
3145
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003146/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003147static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003148{
Markus Armbruster3949e592013-02-06 21:27:24 +01003149 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003150 int i;
3151
3152 if (!buf || (len < 0)) {
3153 return -1;
3154 }
3155
3156 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003157 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3158 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003159 d->cons = d->prod - d->size;
3160 }
3161 }
3162
3163 return 0;
3164}
3165
Markus Armbruster3949e592013-02-06 21:27:24 +01003166static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003167{
Markus Armbruster3949e592013-02-06 21:27:24 +01003168 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003169 int i;
3170
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003171 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003172 for (i = 0; i < len && d->cons != d->prod; i++) {
3173 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003174 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003175 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003176
3177 return i;
3178}
3179
Markus Armbruster3949e592013-02-06 21:27:24 +01003180static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003181{
Markus Armbruster3949e592013-02-06 21:27:24 +01003182 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003183
3184 g_free(d->cbuf);
3185 g_free(d);
3186 chr->opaque = NULL;
3187}
3188
Markus Armbruster4f573782013-07-26 16:44:32 +02003189static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3190 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003191{
3192 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003193 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003194
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003195 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003196 d = g_malloc(sizeof(*d));
3197
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003198 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003199
3200 /* The size must be power of 2 */
3201 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003202 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003203 goto fail;
3204 }
3205
3206 d->prod = 0;
3207 d->cons = 0;
3208 d->cbuf = g_malloc0(d->size);
3209
3210 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003211 chr->chr_write = ringbuf_chr_write;
3212 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003213
3214 return chr;
3215
3216fail:
3217 g_free(d);
3218 g_free(chr);
3219 return NULL;
3220}
3221
Hani Benhabiles8e597772014-05-27 23:39:30 +01003222bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003223{
Markus Armbruster3949e592013-02-06 21:27:24 +01003224 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003225}
3226
Markus Armbruster3949e592013-02-06 21:27:24 +01003227void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003228 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003229 Error **errp)
3230{
3231 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003232 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003233 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003234 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003235
3236 chr = qemu_chr_find(device);
3237 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003238 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003239 return;
3240 }
3241
Markus Armbruster3949e592013-02-06 21:27:24 +01003242 if (!chr_is_ringbuf(chr)) {
3243 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003244 return;
3245 }
3246
Lei Li1f590cf2013-01-25 00:03:20 +08003247 if (has_format && (format == DATA_FORMAT_BASE64)) {
3248 write_data = g_base64_decode(data, &write_count);
3249 } else {
3250 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003251 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003252 }
3253
Markus Armbruster3949e592013-02-06 21:27:24 +01003254 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003255
Markus Armbruster13289fb2013-02-06 21:27:18 +01003256 if (write_data != (uint8_t *)data) {
3257 g_free((void *)write_data);
3258 }
3259
Lei Li1f590cf2013-01-25 00:03:20 +08003260 if (ret < 0) {
3261 error_setg(errp, "Failed to write to device %s", device);
3262 return;
3263 }
3264}
3265
Markus Armbruster3949e592013-02-06 21:27:24 +01003266char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003267 bool has_format, enum DataFormat format,
3268 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003269{
3270 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003271 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003272 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003273 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003274
3275 chr = qemu_chr_find(device);
3276 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003277 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003278 return NULL;
3279 }
3280
Markus Armbruster3949e592013-02-06 21:27:24 +01003281 if (!chr_is_ringbuf(chr)) {
3282 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003283 return NULL;
3284 }
3285
3286 if (size <= 0) {
3287 error_setg(errp, "size must be greater than zero");
3288 return NULL;
3289 }
3290
Markus Armbruster3949e592013-02-06 21:27:24 +01003291 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003292 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003293 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003294
Markus Armbruster3949e592013-02-06 21:27:24 +01003295 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003296
3297 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003298 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003299 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003300 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003301 /*
3302 * FIXME should read only complete, valid UTF-8 characters up
3303 * to @size bytes. Invalid sequences should be replaced by a
3304 * suitable replacement character. Except when (and only
3305 * when) ring buffer lost characters since last read, initial
3306 * continuation characters should be dropped.
3307 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003308 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003309 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003310 }
3311
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003312 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003313}
3314
Gerd Hoffmann33521632009-12-08 13:11:49 +01003315QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003316{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003317 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003318 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003319 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003320 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003321 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003322
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003323 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003324 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003325 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003326 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003327 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003328
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003329 if (strstart(filename, "mon:", &p)) {
3330 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003331 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003332 if (strcmp(filename, "stdio") == 0) {
3333 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3334 * but pass it to the guest. Handle this only for compat syntax,
3335 * for -chardev syntax we have special option for this.
3336 * This is what -nographic did, redirecting+muxing serial+monitor
3337 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003338 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003339 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003340 }
3341
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003342 if (strcmp(filename, "null") == 0 ||
3343 strcmp(filename, "pty") == 0 ||
3344 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003345 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003346 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003347 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003348 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003349 return opts;
3350 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003351 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003352 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003353 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003354 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003355 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003356 qemu_opt_set(opts, "width", width, &error_abort);
3357 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003358 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003359 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003360 qemu_opt_set(opts, "cols", width, &error_abort);
3361 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003362 } else {
3363 goto fail;
3364 }
3365 }
3366 return opts;
3367 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003368 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003369 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003370 return opts;
3371 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003372 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003373 qemu_opt_set(opts, "backend", "serial", &error_abort);
3374 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003375 return opts;
3376 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003377 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003378 qemu_opt_set(opts, "backend", "file", &error_abort);
3379 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003380 return opts;
3381 }
3382 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003383 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3384 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003385 return opts;
3386 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003387 if (strstart(filename, "tcp:", &p) ||
3388 strstart(filename, "telnet:", &p)) {
3389 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3390 host[0] = 0;
3391 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3392 goto fail;
3393 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003394 qemu_opt_set(opts, "backend", "socket", &error_abort);
3395 qemu_opt_set(opts, "host", host, &error_abort);
3396 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003397 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003398 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3399 if (local_err) {
3400 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003401 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003402 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003403 }
3404 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003405 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003406 return opts;
3407 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003408 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003409 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003410 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3411 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003412 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003413 goto fail;
3414 }
3415 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003416 qemu_opt_set(opts, "host", host, &error_abort);
3417 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003418 if (p[pos] == '@') {
3419 p += pos + 1;
3420 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3421 host[0] = 0;
3422 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003423 goto fail;
3424 }
3425 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003426 qemu_opt_set(opts, "localaddr", host, &error_abort);
3427 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003428 }
3429 return opts;
3430 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003431 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003432 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003433 qemu_opts_do_parse(opts, p, "path", &local_err);
3434 if (local_err) {
3435 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003436 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003437 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003438 return opts;
3439 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003440 if (strstart(filename, "/dev/parport", NULL) ||
3441 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003442 qemu_opt_set(opts, "backend", "parport", &error_abort);
3443 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003444 return opts;
3445 }
3446 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003447 qemu_opt_set(opts, "backend", "tty", &error_abort);
3448 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003449 return opts;
3450 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003451
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003452fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003453 qemu_opts_del(opts);
3454 return NULL;
3455}
3456
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003457static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3458 Error **errp)
3459{
3460 const char *path = qemu_opt_get(opts, "path");
3461
3462 if (path == NULL) {
3463 error_setg(errp, "chardev: file: no filename given");
3464 return;
3465 }
3466 backend->file = g_new0(ChardevFile, 1);
3467 backend->file->out = g_strdup(path);
3468}
3469
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003470static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3471 Error **errp)
3472{
3473 backend->stdio = g_new0(ChardevStdio, 1);
3474 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003475 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003476}
3477
Paolo Bonzini6511d392015-09-29 15:08:05 +02003478#ifdef HAVE_CHARDEV_SERIAL
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003479static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3480 Error **errp)
3481{
3482 const char *device = qemu_opt_get(opts, "path");
3483
3484 if (device == NULL) {
3485 error_setg(errp, "chardev: serial/tty: no device path given");
3486 return;
3487 }
3488 backend->serial = g_new0(ChardevHostdev, 1);
3489 backend->serial->device = g_strdup(device);
3490}
Paolo Bonzini6511d392015-09-29 15:08:05 +02003491#endif
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003492
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003493#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003494static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3495 Error **errp)
3496{
3497 const char *device = qemu_opt_get(opts, "path");
3498
3499 if (device == NULL) {
3500 error_setg(errp, "chardev: parallel: no device path given");
3501 return;
3502 }
3503 backend->parallel = g_new0(ChardevHostdev, 1);
3504 backend->parallel->device = g_strdup(device);
3505}
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003506#endif
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003507
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003508static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3509 Error **errp)
3510{
3511 const char *device = qemu_opt_get(opts, "path");
3512
3513 if (device == NULL) {
3514 error_setg(errp, "chardev: pipe: no device path given");
3515 return;
3516 }
3517 backend->pipe = g_new0(ChardevHostdev, 1);
3518 backend->pipe->device = g_strdup(device);
3519}
3520
Markus Armbruster4f573782013-07-26 16:44:32 +02003521static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3522 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003523{
3524 int val;
3525
Markus Armbruster3a1da422013-07-26 16:44:34 +02003526 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003527
Markus Armbruster0f953052013-06-27 16:22:07 +02003528 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003529 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003530 backend->ringbuf->has_size = true;
3531 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003532 }
3533}
3534
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003535static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3536 Error **errp)
3537{
3538 const char *chardev = qemu_opt_get(opts, "chardev");
3539
3540 if (chardev == NULL) {
3541 error_setg(errp, "chardev: mux: no chardev given");
3542 return;
3543 }
3544 backend->mux = g_new0(ChardevMux, 1);
3545 backend->mux->chardev = g_strdup(chardev);
3546}
3547
Peter Maydelldafd3252014-09-02 11:24:13 +01003548static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3549 Error **errp)
3550{
3551 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3552 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3553 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3554 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003555 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003556 const char *path = qemu_opt_get(opts, "path");
3557 const char *host = qemu_opt_get(opts, "host");
3558 const char *port = qemu_opt_get(opts, "port");
3559 SocketAddress *addr;
3560
3561 if (!path) {
3562 if (!host) {
3563 error_setg(errp, "chardev: socket: no host given");
3564 return;
3565 }
3566 if (!port) {
3567 error_setg(errp, "chardev: socket: no port given");
3568 return;
3569 }
3570 }
3571
3572 backend->socket = g_new0(ChardevSocket, 1);
3573
3574 backend->socket->has_nodelay = true;
3575 backend->socket->nodelay = do_nodelay;
3576 backend->socket->has_server = true;
3577 backend->socket->server = is_listen;
3578 backend->socket->has_telnet = true;
3579 backend->socket->telnet = is_telnet;
3580 backend->socket->has_wait = true;
3581 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003582 backend->socket->has_reconnect = true;
3583 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003584
3585 addr = g_new0(SocketAddress, 1);
3586 if (path) {
3587 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3588 addr->q_unix = g_new0(UnixSocketAddress, 1);
3589 addr->q_unix->path = g_strdup(path);
3590 } else {
3591 addr->kind = SOCKET_ADDRESS_KIND_INET;
3592 addr->inet = g_new0(InetSocketAddress, 1);
3593 addr->inet->host = g_strdup(host);
3594 addr->inet->port = g_strdup(port);
3595 addr->inet->has_to = qemu_opt_get(opts, "to");
3596 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3597 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3598 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3599 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3600 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3601 }
3602 backend->socket->addr = addr;
3603}
3604
Peter Maydell90a14bf2014-09-02 11:24:15 +01003605static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3606 Error **errp)
3607{
3608 const char *host = qemu_opt_get(opts, "host");
3609 const char *port = qemu_opt_get(opts, "port");
3610 const char *localaddr = qemu_opt_get(opts, "localaddr");
3611 const char *localport = qemu_opt_get(opts, "localport");
3612 bool has_local = false;
3613 SocketAddress *addr;
3614
3615 if (host == NULL || strlen(host) == 0) {
3616 host = "localhost";
3617 }
3618 if (port == NULL || strlen(port) == 0) {
3619 error_setg(errp, "chardev: udp: remote port not specified");
3620 return;
3621 }
3622 if (localport == NULL || strlen(localport) == 0) {
3623 localport = "0";
3624 } else {
3625 has_local = true;
3626 }
3627 if (localaddr == NULL || strlen(localaddr) == 0) {
3628 localaddr = "";
3629 } else {
3630 has_local = true;
3631 }
3632
3633 backend->udp = g_new0(ChardevUdp, 1);
3634
3635 addr = g_new0(SocketAddress, 1);
3636 addr->kind = SOCKET_ADDRESS_KIND_INET;
3637 addr->inet = g_new0(InetSocketAddress, 1);
3638 addr->inet->host = g_strdup(host);
3639 addr->inet->port = g_strdup(port);
3640 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3641 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3642 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3643 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3644 backend->udp->remote = addr;
3645
3646 if (has_local) {
3647 backend->udp->has_local = true;
3648 addr = g_new0(SocketAddress, 1);
3649 addr->kind = SOCKET_ADDRESS_KIND_INET;
3650 addr->inet = g_new0(InetSocketAddress, 1);
3651 addr->inet->host = g_strdup(localaddr);
3652 addr->inet->port = g_strdup(localport);
3653 backend->udp->local = addr;
3654 }
3655}
3656
Anthony Liguorid654f342013-03-05 23:21:28 +05303657typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003658 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003659 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003660 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003661 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3662 ChardevReturn *ret, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303663} CharDriver;
3664
3665static GSList *backends;
3666
Peter Maydelle4d50d42014-09-02 11:24:17 +01003667void register_char_driver(const char *name, ChardevBackendKind kind,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003668 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp),
3669 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3670 ChardevReturn *ret, Error **errp))
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003671{
3672 CharDriver *s;
3673
3674 s = g_malloc0(sizeof(*s));
3675 s->name = g_strdup(name);
3676 s->kind = kind;
3677 s->parse = parse;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003678 s->create = create;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003679
3680 backends = g_slist_append(backends, s);
3681}
3682
Anthony Liguorif69554b2011-08-15 11:17:37 -05003683CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003684 void (*init)(struct CharDriverState *s),
3685 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003686{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003687 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303688 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003689 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303690 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003691 ChardevReturn *ret = NULL;
3692 ChardevBackend *backend;
3693 const char *id = qemu_opts_id(opts);
3694 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003695
Peter Maydella61ae7f2014-09-02 11:24:16 +01003696 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003697 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003698 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003699 }
3700
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003701 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003702 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003703 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003704 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003705 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303706 for (i = backends; i; i = i->next) {
3707 cd = i->data;
3708
3709 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003710 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303711 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003712 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303713 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003714 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003715 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003716 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003717 }
3718
Peter Maydella61ae7f2014-09-02 11:24:16 +01003719 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003720
3721 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003722 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003723 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003724
3725 chr = NULL;
3726 backend->kind = cd->kind;
3727 if (cd->parse) {
3728 cd->parse(opts, backend, &local_err);
3729 if (local_err) {
3730 error_propagate(errp, local_err);
3731 goto qapi_out;
3732 }
3733 }
3734 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3735 if (!ret) {
3736 goto qapi_out;
3737 }
3738
3739 if (bid) {
3740 qapi_free_ChardevBackend(backend);
3741 qapi_free_ChardevReturn(ret);
3742 backend = g_new0(ChardevBackend, 1);
3743 backend->mux = g_new0(ChardevMux, 1);
3744 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3745 backend->mux->chardev = g_strdup(bid);
3746 ret = qmp_chardev_add(id, backend, errp);
3747 if (!ret) {
3748 chr = qemu_chr_find(bid);
3749 qemu_chr_delete(chr);
3750 chr = NULL;
3751 goto qapi_out;
3752 }
3753 }
3754
3755 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003756 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003757
3758qapi_out:
3759 qapi_free_ChardevBackend(backend);
3760 qapi_free_ChardevReturn(ret);
3761 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003762 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003763
3764err:
3765 qemu_opts_del(opts);
3766 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003767}
3768
Anthony Liguori27143a42011-08-15 11:17:36 -05003769CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003770{
3771 const char *p;
3772 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003773 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003774 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003775
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003776 if (strstart(filename, "chardev:", &p)) {
3777 return qemu_chr_find(p);
3778 }
3779
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003780 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003781 if (!opts)
3782 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003783
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003784 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003785 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003786 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003787 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003788 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003789 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003790 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003791 }
3792 return chr;
3793}
3794
Anthony Liguori15f31512011-08-15 11:17:35 -05003795void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003796{
3797 if (chr->chr_set_echo) {
3798 chr->chr_set_echo(chr, echo);
3799 }
3800}
3801
Hans de Goede8e25daa2013-03-26 11:07:57 +01003802void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003803{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003804 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003805 return;
3806 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003807 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003808 if (chr->chr_set_fe_open) {
3809 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003810 }
3811}
3812
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003813void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3814{
3815 if (chr->chr_fe_event) {
3816 chr->chr_fe_event(chr, event);
3817 }
3818}
3819
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003820int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3821 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303822{
3823 GSource *src;
3824 guint tag;
3825
3826 if (s->chr_add_watch == NULL) {
3827 return -ENOSYS;
3828 }
3829
3830 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003831 if (!src) {
3832 return -EINVAL;
3833 }
3834
Anthony Liguori23673ca2013-03-05 23:21:23 +05303835 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3836 tag = g_source_attach(src, NULL);
3837 g_source_unref(src);
3838
3839 return tag;
3840}
3841
Hans de Goede44c473d2013-03-27 20:29:39 +01003842int qemu_chr_fe_claim(CharDriverState *s)
3843{
3844 if (s->avail_connections < 1) {
3845 return -1;
3846 }
3847 s->avail_connections--;
3848 return 0;
3849}
3850
3851void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3852{
3853 if (qemu_chr_fe_claim(s) != 0) {
3854 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3855 __func__, s->label);
3856 exit(1);
3857 }
3858}
3859
3860void qemu_chr_fe_release(CharDriverState *s)
3861{
3862 s->avail_connections++;
3863}
3864
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003865void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003866{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003867 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003868 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003869 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003870 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003871 g_free(chr->filename);
3872 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003873 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003874 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003875}
3876
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003877ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003878{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003879 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003880 CharDriverState *chr;
3881
Blue Swirl72cf2d42009-09-12 07:36:22 +00003882 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003883 ChardevInfoList *info = g_malloc0(sizeof(*info));
3884 info->value = g_malloc0(sizeof(*info->value));
3885 info->value->label = g_strdup(chr->label);
3886 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003887 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003888
3889 info->next = chr_list;
3890 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003891 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003892
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003893 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003894}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003895
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003896ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3897{
3898 ChardevBackendInfoList *backend_list = NULL;
3899 CharDriver *c = NULL;
3900 GSList *i = NULL;
3901
3902 for (i = backends; i; i = i->next) {
3903 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3904 c = i->data;
3905 info->value = g_malloc0(sizeof(*info->value));
3906 info->value->name = g_strdup(c->name);
3907
3908 info->next = backend_list;
3909 backend_list = info;
3910 }
3911
3912 return backend_list;
3913}
3914
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003915CharDriverState *qemu_chr_find(const char *name)
3916{
3917 CharDriverState *chr;
3918
Blue Swirl72cf2d42009-09-12 07:36:22 +00003919 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003920 if (strcmp(chr->label, name) != 0)
3921 continue;
3922 return chr;
3923 }
3924 return NULL;
3925}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003926
3927/* Get a character (serial) device interface. */
3928CharDriverState *qemu_char_get_next_serial(void)
3929{
3930 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003931 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003932
3933 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003934
3935 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3936 chr = serial_hds[next_serial++];
3937 qemu_chr_fe_claim_no_fail(chr);
3938 return chr;
3939 }
3940 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003941}
3942
Paolo Bonzini4d454572012-11-26 16:03:42 +01003943QemuOptsList qemu_chardev_opts = {
3944 .name = "chardev",
3945 .implied_opt_name = "backend",
3946 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3947 .desc = {
3948 {
3949 .name = "backend",
3950 .type = QEMU_OPT_STRING,
3951 },{
3952 .name = "path",
3953 .type = QEMU_OPT_STRING,
3954 },{
3955 .name = "host",
3956 .type = QEMU_OPT_STRING,
3957 },{
3958 .name = "port",
3959 .type = QEMU_OPT_STRING,
3960 },{
3961 .name = "localaddr",
3962 .type = QEMU_OPT_STRING,
3963 },{
3964 .name = "localport",
3965 .type = QEMU_OPT_STRING,
3966 },{
3967 .name = "to",
3968 .type = QEMU_OPT_NUMBER,
3969 },{
3970 .name = "ipv4",
3971 .type = QEMU_OPT_BOOL,
3972 },{
3973 .name = "ipv6",
3974 .type = QEMU_OPT_BOOL,
3975 },{
3976 .name = "wait",
3977 .type = QEMU_OPT_BOOL,
3978 },{
3979 .name = "server",
3980 .type = QEMU_OPT_BOOL,
3981 },{
3982 .name = "delay",
3983 .type = QEMU_OPT_BOOL,
3984 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003985 .name = "reconnect",
3986 .type = QEMU_OPT_NUMBER,
3987 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003988 .name = "telnet",
3989 .type = QEMU_OPT_BOOL,
3990 },{
3991 .name = "width",
3992 .type = QEMU_OPT_NUMBER,
3993 },{
3994 .name = "height",
3995 .type = QEMU_OPT_NUMBER,
3996 },{
3997 .name = "cols",
3998 .type = QEMU_OPT_NUMBER,
3999 },{
4000 .name = "rows",
4001 .type = QEMU_OPT_NUMBER,
4002 },{
4003 .name = "mux",
4004 .type = QEMU_OPT_BOOL,
4005 },{
4006 .name = "signal",
4007 .type = QEMU_OPT_BOOL,
4008 },{
4009 .name = "name",
4010 .type = QEMU_OPT_STRING,
4011 },{
4012 .name = "debug",
4013 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08004014 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01004015 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01004016 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004017 },{
4018 .name = "chardev",
4019 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004020 },
4021 { /* end of list */ }
4022 },
4023};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004024
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004025#ifdef _WIN32
4026
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004027static CharDriverState *qmp_chardev_open_file(const char *id,
4028 ChardevBackend *backend,
4029 ChardevReturn *ret,
4030 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004031{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004032 ChardevFile *file = backend->file;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004033 HANDLE out;
4034
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004035 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004036 error_setg(errp, "input file not supported");
4037 return NULL;
4038 }
4039
4040 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4041 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4042 if (out == INVALID_HANDLE_VALUE) {
4043 error_setg(errp, "open %s failed", file->out);
4044 return NULL;
4045 }
4046 return qemu_chr_open_win_file(out);
4047}
4048
Paolo Bonzini6511d392015-09-29 15:08:05 +02004049static CharDriverState *qmp_chardev_open_serial(const char *id,
4050 ChardevBackend *backend,
4051 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004052 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004053{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004054 ChardevHostdev *serial = backend->serial;
4055 return qemu_chr_open_win_path(serial->device, errp);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004056}
4057
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004058#else /* WIN32 */
4059
4060static int qmp_chardev_open_file_source(char *src, int flags,
4061 Error **errp)
4062{
4063 int fd = -1;
4064
4065 TFR(fd = qemu_open(src, flags, 0666));
4066 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004067 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004068 }
4069 return fd;
4070}
4071
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004072static CharDriverState *qmp_chardev_open_file(const char *id,
4073 ChardevBackend *backend,
4074 ChardevReturn *ret,
4075 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004076{
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004077 ChardevFile *file = backend->file;
Markus Armbruster5f758362014-05-19 18:57:34 +02004078 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004079
4080 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4081 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004082 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004083 return NULL;
4084 }
4085
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004086 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004087 flags = O_RDONLY;
4088 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004089 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004090 qemu_close(out);
4091 return NULL;
4092 }
4093 }
4094
4095 return qemu_chr_open_fd(in, out);
4096}
4097
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004098#ifdef HAVE_CHARDEV_SERIAL
Paolo Bonzini6511d392015-09-29 15:08:05 +02004099static CharDriverState *qmp_chardev_open_serial(const char *id,
4100 ChardevBackend *backend,
4101 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004102 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004103{
Paolo Bonzini6511d392015-09-29 15:08:05 +02004104 ChardevHostdev *serial = backend->serial;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004105 int fd;
4106
4107 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004108 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004109 return NULL;
4110 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004111 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004112 return qemu_chr_open_tty_fd(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004113}
Paolo Bonzini6511d392015-09-29 15:08:05 +02004114#endif
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004115
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004116#ifdef HAVE_CHARDEV_PARPORT
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004117static CharDriverState *qmp_chardev_open_parallel(const char *id,
4118 ChardevBackend *backend,
4119 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004120 Error **errp)
4121{
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004122 ChardevHostdev *parallel = backend->parallel;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004123 int fd;
4124
4125 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004126 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004127 return NULL;
4128 }
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004129 return qemu_chr_open_pp_fd(fd, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004130}
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004131#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004132
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004133#endif /* WIN32 */
4134
Corey Minyard5008e5b2014-10-08 07:11:55 -05004135static void socket_try_connect(CharDriverState *chr)
4136{
4137 Error *err = NULL;
4138
4139 if (!qemu_chr_open_socket_fd(chr, &err)) {
4140 check_report_connect_error(chr, err);
4141 }
4142}
4143
Corey Minyard5dd1f022014-10-02 11:17:37 -05004144static gboolean socket_reconnect_timeout(gpointer opaque)
4145{
4146 CharDriverState *chr = opaque;
4147 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004148
4149 s->reconnect_timer = 0;
4150
4151 if (chr->be_open) {
4152 return false;
4153 }
4154
Corey Minyard5008e5b2014-10-08 07:11:55 -05004155 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004156
4157 return false;
4158}
4159
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004160static CharDriverState *qmp_chardev_open_socket(const char *id,
4161 ChardevBackend *backend,
4162 ChardevReturn *ret,
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004163 Error **errp)
4164{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004165 CharDriverState *chr;
4166 TCPCharDriver *s;
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004167 ChardevSocket *sock = backend->socket;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004168 SocketAddress *addr = sock->addr;
4169 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4170 bool is_listen = sock->has_server ? sock->server : true;
4171 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4172 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004173 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004174
4175 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02004176 s = g_new0(TCPCharDriver, 1);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004177
4178 s->fd = -1;
4179 s->listen_fd = -1;
4180 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004181 s->is_listen = is_listen;
4182 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004183 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004184 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004185
4186 chr->opaque = s;
4187 chr->chr_write = tcp_chr_write;
4188 chr->chr_sync_read = tcp_chr_sync_read;
4189 chr->chr_close = tcp_chr_close;
4190 chr->get_msgfds = tcp_get_msgfds;
4191 chr->set_msgfds = tcp_set_msgfds;
4192 chr->chr_add_client = tcp_chr_add_client;
4193 chr->chr_add_watch = tcp_chr_add_watch;
4194 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4195 /* be isn't opened until we get a connection */
4196 chr->explicit_be_open = true;
4197
4198 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004199 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4200 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004201
4202 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004203 if (is_telnet) {
4204 s->do_telnetopt = 1;
4205 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004206 } else if (reconnect > 0) {
4207 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004208 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004209
Corey Minyard5008e5b2014-10-08 07:11:55 -05004210 if (s->reconnect_time) {
4211 socket_try_connect(chr);
4212 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4213 g_free(s);
4214 g_free(chr->filename);
4215 g_free(chr);
4216 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004217 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004218
4219 if (is_listen && is_waitconnect) {
4220 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4221 chr->filename);
4222 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4223 qemu_set_nonblock(s->listen_fd);
4224 }
4225
4226 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004227}
4228
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004229static CharDriverState *qmp_chardev_open_udp(const char *id,
4230 ChardevBackend *backend,
4231 ChardevReturn *ret,
Lei Li08d0ab32013-05-20 14:51:03 +08004232 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004233{
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004234 ChardevUdp *udp = backend->udp;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004235 int fd;
4236
Lei Li08d0ab32013-05-20 14:51:03 +08004237 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004238 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004239 return NULL;
4240 }
4241 return qemu_chr_open_udp_fd(fd);
4242}
4243
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004244ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4245 Error **errp)
4246{
4247 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004248 CharDriverState *base, *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004249 Error *local_err = NULL;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004250 GSList *i;
4251 CharDriver *cd;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004252
4253 chr = qemu_chr_find(id);
4254 if (chr) {
4255 error_setg(errp, "Chardev '%s' already exists", id);
4256 g_free(ret);
4257 return NULL;
4258 }
4259
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004260 for (i = backends; i; i = i->next) {
4261 cd = i->data;
4262
4263 if (cd->kind == backend->kind && cd->create) {
4264 chr = cd->create(id, backend, ret, &local_err);
4265 if (local_err) {
4266 error_propagate(errp, local_err);
4267 goto out_error;
4268 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004269 break;
4270 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004271 }
4272
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004273 if (chr == NULL) {
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004274 switch (backend->kind) {
4275 case CHARDEV_BACKEND_KIND_FILE:
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004276 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004277 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004278 case CHARDEV_BACKEND_KIND_SERIAL:
Paolo Bonzini6511d392015-09-29 15:08:05 +02004279 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004280 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004281 case CHARDEV_BACKEND_KIND_PARALLEL:
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004282 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004283 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004284 case CHARDEV_BACKEND_KIND_PIPE:
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004285 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004286 break;
4287 case CHARDEV_BACKEND_KIND_SOCKET:
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004288 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004289 break;
4290 case CHARDEV_BACKEND_KIND_UDP:
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004291 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004292 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004293 case CHARDEV_BACKEND_KIND_PTY:
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004294 abort();
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004295 break;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004296 case CHARDEV_BACKEND_KIND_NULL:
4297 chr = qemu_chr_open_null();
4298 break;
4299 case CHARDEV_BACKEND_KIND_MUX:
4300 base = qemu_chr_find(backend->mux->chardev);
4301 if (base == NULL) {
4302 error_setg(&local_err, "mux: base chardev %s not found",
4303 backend->mux->chardev);
4304 break;
4305 }
4306 chr = qemu_chr_open_mux(base);
4307 break;
4308 case CHARDEV_BACKEND_KIND_MSMOUSE:
4309 chr = qemu_chr_open_msmouse();
4310 break;
4311#ifdef CONFIG_BRLAPI
4312 case CHARDEV_BACKEND_KIND_BRAILLE:
4313 chr = chr_baum_init();
4314 break;
4315#endif
4316 case CHARDEV_BACKEND_KIND_TESTDEV:
4317 chr = chr_testdev_init();
4318 break;
4319 case CHARDEV_BACKEND_KIND_STDIO:
4320 chr = qemu_chr_open_stdio(backend->stdio);
4321 break;
4322#ifdef _WIN32
4323 case CHARDEV_BACKEND_KIND_CONSOLE:
4324 chr = qemu_chr_open_win_con();
4325 break;
4326#endif
4327#ifdef CONFIG_SPICE
4328 case CHARDEV_BACKEND_KIND_SPICEVMC:
4329 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4330 break;
4331 case CHARDEV_BACKEND_KIND_SPICEPORT:
4332 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4333 break;
4334#endif
4335 case CHARDEV_BACKEND_KIND_VC:
4336 chr = vc_init(backend->vc);
4337 break;
4338 case CHARDEV_BACKEND_KIND_RINGBUF:
4339 case CHARDEV_BACKEND_KIND_MEMORY:
4340 chr = qemu_chr_open_ringbuf(backend->ringbuf, &local_err);
4341 break;
4342 default:
4343 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4344 goto out_error;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004345 }
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004346
4347 /*
4348 * Character backend open hasn't been fully converted to the Error
4349 * API. Some opens fail without setting an error. Set a generic
4350 * error then.
4351 * TODO full conversion to Error API
4352 */
4353 if (chr == NULL) {
4354 if (local_err) {
4355 error_propagate(errp, local_err);
4356 } else {
4357 error_setg(errp, "Failed to create chardev");
4358 }
4359 goto out_error;
4360 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004361 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004362
4363 chr->label = g_strdup(id);
4364 chr->avail_connections =
4365 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
4366 if (!chr->filename) {
4367 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4368 }
4369 if (!chr->explicit_be_open) {
4370 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4371 }
4372 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4373 return ret;
4374
4375out_error:
4376 g_free(ret);
4377 return NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004378}
4379
4380void qmp_chardev_remove(const char *id, Error **errp)
4381{
4382 CharDriverState *chr;
4383
4384 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004385 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004386 error_setg(errp, "Chardev '%s' not found", id);
4387 return;
4388 }
4389 if (chr->chr_can_read || chr->chr_read ||
4390 chr->chr_event || chr->handler_opaque) {
4391 error_setg(errp, "Chardev '%s' is busy", id);
4392 return;
4393 }
4394 qemu_chr_delete(chr);
4395}
Anthony Liguorid654f342013-03-05 23:21:28 +05304396
4397static void register_types(void)
4398{
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004399 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
4400 NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004401 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004402 qemu_chr_parse_socket, qmp_chardev_open_socket);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004403 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp,
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004404 qmp_chardev_open_udp);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004405 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004406 qemu_chr_parse_ringbuf, NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004407 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004408 qemu_chr_parse_file_out, qmp_chardev_open_file);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004409 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004410 qemu_chr_parse_stdio, NULL);
Paolo Bonzini6511d392015-09-29 15:08:05 +02004411#if defined HAVE_CHARDEV_SERIAL
Peter Maydelle4d50d42014-09-02 11:24:17 +01004412 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004413 qemu_chr_parse_serial, qmp_chardev_open_serial);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004414 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004415 qemu_chr_parse_serial, qmp_chardev_open_serial);
4416#endif
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004417#ifdef HAVE_CHARDEV_PARPORT
Peter Maydelle4d50d42014-09-02 11:24:17 +01004418 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004419 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004420 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004421 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
4422#endif
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004423#ifdef HAVE_CHARDEV_PTY
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004424 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004425 qemu_chr_open_pty);
4426#endif
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004427 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL,
4428 NULL);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004429 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004430 qemu_chr_parse_pipe, qemu_chr_open_pipe);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004431 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux,
4432 NULL);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004433 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004434 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004435 qemu_chr_parse_ringbuf, NULL);
Michael Roth7b7ab182013-07-30 13:04:22 -05004436 /* this must be done after machine init, since we register FEs with muxes
4437 * as part of realize functions like serial_isa_realizefn when -nographic
4438 * is specified
4439 */
4440 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304441}
4442
4443type_init(register_types);