blob: dd83203fa20e301309d36ffa497b8475db581345 [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();
Anthony Liguori7267c092011-08-20 22:09:37 -0500688 d = g_malloc0(sizeof(MuxDriver));
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();
Anthony Liguori7267c092011-08-20 22:09:37 -05001067 s = g_malloc0(sizeof(FDCharDriver));
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
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001081static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001082{
1083 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001084 char filename_in[CHR_MAX_FILENAME_SIZE];
1085 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001086 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001087
1088 if (filename == NULL) {
1089 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +01001090 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001091 }
aliguori6f97dba2008-10-31 18:49:55 +00001092
Corey Minyard9f781162014-10-02 11:17:33 -05001093 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1094 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001095 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1096 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001097 if (fd_in < 0 || fd_out < 0) {
1098 if (fd_in >= 0)
1099 close(fd_in);
1100 if (fd_out >= 0)
1101 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001102 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001103 if (fd_in < 0) {
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
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001199#define HAVE_CHARDEV_TTY 1
1200
aliguori6f97dba2008-10-31 18:49:55 +00001201typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301202 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001203 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001204
1205 /* Protected by the CharDriverState chr_write_lock. */
1206 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301207 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001208 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001209} PtyCharDriver;
1210
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001211static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001212static void pty_chr_state(CharDriverState *chr, int connected);
1213
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301214static gboolean pty_chr_timer(gpointer opaque)
1215{
1216 struct CharDriverState *chr = opaque;
1217 PtyCharDriver *s = chr->opaque;
1218
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001219 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001220 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001221 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001222 if (!s->connected) {
1223 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001224 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001225 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001226 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301227 return FALSE;
1228}
1229
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001230/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301231static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1232{
1233 PtyCharDriver *s = chr->opaque;
1234
1235 if (s->timer_tag) {
1236 g_source_remove(s->timer_tag);
1237 s->timer_tag = 0;
1238 }
1239
1240 if (ms == 1000) {
1241 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1242 } else {
1243 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1244 }
1245}
1246
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001247/* Called with chr_write_lock held. */
1248static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001249{
1250 PtyCharDriver *s = chr->opaque;
1251 GPollFD pfd;
1252
1253 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1254 pfd.events = G_IO_OUT;
1255 pfd.revents = 0;
1256 g_poll(&pfd, 1, 0);
1257 if (pfd.revents & G_IO_HUP) {
1258 pty_chr_state(chr, 0);
1259 } else {
1260 pty_chr_state(chr, 1);
1261 }
1262}
1263
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001264static void pty_chr_update_read_handler(CharDriverState *chr)
1265{
1266 qemu_mutex_lock(&chr->chr_write_lock);
1267 pty_chr_update_read_handler_locked(chr);
1268 qemu_mutex_unlock(&chr->chr_write_lock);
1269}
1270
1271/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001272static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1273{
1274 PtyCharDriver *s = chr->opaque;
1275
1276 if (!s->connected) {
1277 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001278 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001279 if (!s->connected) {
1280 return 0;
1281 }
aliguori6f97dba2008-10-31 18:49:55 +00001282 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001283 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001284}
1285
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301286static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1287{
1288 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001289 if (!s->connected) {
1290 return NULL;
1291 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301292 return g_io_create_watch(s->fd, cond);
1293}
1294
aliguori6f97dba2008-10-31 18:49:55 +00001295static int pty_chr_read_poll(void *opaque)
1296{
1297 CharDriverState *chr = opaque;
1298 PtyCharDriver *s = chr->opaque;
1299
Anthony Liguori909cda12011-08-15 11:17:31 -05001300 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001301 return s->read_bytes;
1302}
1303
Anthony Liguori093d3a22013-03-05 23:21:20 +05301304static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001305{
1306 CharDriverState *chr = opaque;
1307 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301308 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301309 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301310 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001311
1312 len = sizeof(buf);
1313 if (len > s->read_bytes)
1314 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001315 if (len == 0) {
1316 return TRUE;
1317 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301318 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1319 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001320 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301321 return FALSE;
1322 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001323 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001324 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001325 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301326 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001327}
1328
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001329static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1330{
1331 CharDriverState *chr = opaque;
1332 PtyCharDriver *s = chr->opaque;
1333
1334 s->open_tag = 0;
1335 qemu_chr_be_generic_open(chr);
1336 return FALSE;
1337}
1338
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001339/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001340static void pty_chr_state(CharDriverState *chr, int connected)
1341{
1342 PtyCharDriver *s = chr->opaque;
1343
1344 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001345 if (s->open_tag) {
1346 g_source_remove(s->open_tag);
1347 s->open_tag = 0;
1348 }
Amit Shah26da70c2013-08-28 15:23:37 +05301349 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001350 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001351 /* (re-)connect poll interval for idle guests: once per second.
1352 * We check more frequently in case the guests sends data to
1353 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301354 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001355 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001356 if (s->timer_tag) {
1357 g_source_remove(s->timer_tag);
1358 s->timer_tag = 0;
1359 }
1360 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001361 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001362 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001363 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001364 }
1365 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301366 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1367 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001368 }
aliguori6f97dba2008-10-31 18:49:55 +00001369 }
1370}
1371
aliguori6f97dba2008-10-31 18:49:55 +00001372static void pty_chr_close(struct CharDriverState *chr)
1373{
1374 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301375 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001376
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001377 qemu_mutex_lock(&chr->chr_write_lock);
1378 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301379 fd = g_io_channel_unix_get_fd(s->fd);
1380 g_io_channel_unref(s->fd);
1381 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301382 if (s->timer_tag) {
1383 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001384 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301385 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001386 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001387 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001388 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001389}
1390
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001391static CharDriverState *qemu_chr_open_pty(const char *id,
1392 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001393{
1394 CharDriverState *chr;
1395 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001396 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001397 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001398
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001399 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1400 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001401 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001402 }
1403
aliguori6f97dba2008-10-31 18:49:55 +00001404 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001405 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001406
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001407 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001408
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001409 chr->filename = g_strdup_printf("pty:%s", pty_name);
1410 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001411 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001412
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001413 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001414 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001415
1416 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001417 chr->opaque = s;
1418 chr->chr_write = pty_chr_write;
1419 chr->chr_update_read_handler = pty_chr_update_read_handler;
1420 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301421 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001422 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001423
Anthony Liguori093d3a22013-03-05 23:21:20 +05301424 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301425 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001426
Markus Armbruster1f514702012-02-07 15:09:08 +01001427 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001428}
1429
1430static void tty_serial_init(int fd, int speed,
1431 int parity, int data_bits, int stop_bits)
1432{
1433 struct termios tty;
1434 speed_t spd;
1435
1436#if 0
1437 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1438 speed, parity, data_bits, stop_bits);
1439#endif
1440 tcgetattr (fd, &tty);
1441
Stefan Weil45eea132009-10-26 16:10:10 +01001442#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1443 speed = speed * 10 / 11;
1444 do {
1445 check_speed(50);
1446 check_speed(75);
1447 check_speed(110);
1448 check_speed(134);
1449 check_speed(150);
1450 check_speed(200);
1451 check_speed(300);
1452 check_speed(600);
1453 check_speed(1200);
1454 check_speed(1800);
1455 check_speed(2400);
1456 check_speed(4800);
1457 check_speed(9600);
1458 check_speed(19200);
1459 check_speed(38400);
1460 /* Non-Posix values follow. They may be unsupported on some systems. */
1461 check_speed(57600);
1462 check_speed(115200);
1463#ifdef B230400
1464 check_speed(230400);
1465#endif
1466#ifdef B460800
1467 check_speed(460800);
1468#endif
1469#ifdef B500000
1470 check_speed(500000);
1471#endif
1472#ifdef B576000
1473 check_speed(576000);
1474#endif
1475#ifdef B921600
1476 check_speed(921600);
1477#endif
1478#ifdef B1000000
1479 check_speed(1000000);
1480#endif
1481#ifdef B1152000
1482 check_speed(1152000);
1483#endif
1484#ifdef B1500000
1485 check_speed(1500000);
1486#endif
1487#ifdef B2000000
1488 check_speed(2000000);
1489#endif
1490#ifdef B2500000
1491 check_speed(2500000);
1492#endif
1493#ifdef B3000000
1494 check_speed(3000000);
1495#endif
1496#ifdef B3500000
1497 check_speed(3500000);
1498#endif
1499#ifdef B4000000
1500 check_speed(4000000);
1501#endif
aliguori6f97dba2008-10-31 18:49:55 +00001502 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001503 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001504
1505 cfsetispeed(&tty, spd);
1506 cfsetospeed(&tty, spd);
1507
1508 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1509 |INLCR|IGNCR|ICRNL|IXON);
1510 tty.c_oflag |= OPOST;
1511 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1512 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1513 switch(data_bits) {
1514 default:
1515 case 8:
1516 tty.c_cflag |= CS8;
1517 break;
1518 case 7:
1519 tty.c_cflag |= CS7;
1520 break;
1521 case 6:
1522 tty.c_cflag |= CS6;
1523 break;
1524 case 5:
1525 tty.c_cflag |= CS5;
1526 break;
1527 }
1528 switch(parity) {
1529 default:
1530 case 'N':
1531 break;
1532 case 'E':
1533 tty.c_cflag |= PARENB;
1534 break;
1535 case 'O':
1536 tty.c_cflag |= PARENB | PARODD;
1537 break;
1538 }
1539 if (stop_bits == 2)
1540 tty.c_cflag |= CSTOPB;
1541
1542 tcsetattr (fd, TCSANOW, &tty);
1543}
1544
1545static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1546{
1547 FDCharDriver *s = chr->opaque;
1548
1549 switch(cmd) {
1550 case CHR_IOCTL_SERIAL_SET_PARAMS:
1551 {
1552 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301553 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1554 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001555 ssp->data_bits, ssp->stop_bits);
1556 }
1557 break;
1558 case CHR_IOCTL_SERIAL_SET_BREAK:
1559 {
1560 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301561 if (enable) {
1562 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1563 }
aliguori6f97dba2008-10-31 18:49:55 +00001564 }
1565 break;
1566 case CHR_IOCTL_SERIAL_GET_TIOCM:
1567 {
1568 int sarg = 0;
1569 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301570 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001571 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001572 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001573 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001574 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001575 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001576 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001577 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001578 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001579 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001580 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001581 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001582 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001583 *targ |= CHR_TIOCM_RTS;
1584 }
1585 break;
1586 case CHR_IOCTL_SERIAL_SET_TIOCM:
1587 {
1588 int sarg = *(int *)arg;
1589 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301590 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001591 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1592 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1593 if (sarg & CHR_TIOCM_CTS)
1594 targ |= TIOCM_CTS;
1595 if (sarg & CHR_TIOCM_CAR)
1596 targ |= TIOCM_CAR;
1597 if (sarg & CHR_TIOCM_DSR)
1598 targ |= TIOCM_DSR;
1599 if (sarg & CHR_TIOCM_RI)
1600 targ |= TIOCM_RI;
1601 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001602 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001603 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001604 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301605 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001606 }
1607 break;
1608 default:
1609 return -ENOTSUP;
1610 }
1611 return 0;
1612}
1613
David Ahern4266a132010-02-10 18:27:17 -07001614static void qemu_chr_close_tty(CharDriverState *chr)
1615{
1616 FDCharDriver *s = chr->opaque;
1617 int fd = -1;
1618
1619 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301620 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001621 }
1622
1623 fd_chr_close(chr);
1624
1625 if (fd >= 0) {
1626 close(fd);
1627 }
1628}
1629
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001630static CharDriverState *qemu_chr_open_tty_fd(int fd)
1631{
1632 CharDriverState *chr;
1633
1634 tty_serial_init(fd, 115200, 'N', 8, 1);
1635 chr = qemu_chr_open_fd(fd, fd);
1636 chr->chr_ioctl = tty_serial_ioctl;
1637 chr->chr_close = qemu_chr_close_tty;
1638 return chr;
1639}
aliguori6f97dba2008-10-31 18:49:55 +00001640#endif /* __linux__ || __sun__ */
1641
1642#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001643
1644#define HAVE_CHARDEV_PARPORT 1
1645
aliguori6f97dba2008-10-31 18:49:55 +00001646typedef struct {
1647 int fd;
1648 int mode;
1649} ParallelCharDriver;
1650
1651static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1652{
1653 if (s->mode != mode) {
1654 int m = mode;
1655 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1656 return 0;
1657 s->mode = mode;
1658 }
1659 return 1;
1660}
1661
1662static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1663{
1664 ParallelCharDriver *drv = chr->opaque;
1665 int fd = drv->fd;
1666 uint8_t b;
1667
1668 switch(cmd) {
1669 case CHR_IOCTL_PP_READ_DATA:
1670 if (ioctl(fd, PPRDATA, &b) < 0)
1671 return -ENOTSUP;
1672 *(uint8_t *)arg = b;
1673 break;
1674 case CHR_IOCTL_PP_WRITE_DATA:
1675 b = *(uint8_t *)arg;
1676 if (ioctl(fd, PPWDATA, &b) < 0)
1677 return -ENOTSUP;
1678 break;
1679 case CHR_IOCTL_PP_READ_CONTROL:
1680 if (ioctl(fd, PPRCONTROL, &b) < 0)
1681 return -ENOTSUP;
1682 /* Linux gives only the lowest bits, and no way to know data
1683 direction! For better compatibility set the fixed upper
1684 bits. */
1685 *(uint8_t *)arg = b | 0xc0;
1686 break;
1687 case CHR_IOCTL_PP_WRITE_CONTROL:
1688 b = *(uint8_t *)arg;
1689 if (ioctl(fd, PPWCONTROL, &b) < 0)
1690 return -ENOTSUP;
1691 break;
1692 case CHR_IOCTL_PP_READ_STATUS:
1693 if (ioctl(fd, PPRSTATUS, &b) < 0)
1694 return -ENOTSUP;
1695 *(uint8_t *)arg = b;
1696 break;
1697 case CHR_IOCTL_PP_DATA_DIR:
1698 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1699 return -ENOTSUP;
1700 break;
1701 case CHR_IOCTL_PP_EPP_READ_ADDR:
1702 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1703 struct ParallelIOArg *parg = arg;
1704 int n = read(fd, parg->buffer, parg->count);
1705 if (n != parg->count) {
1706 return -EIO;
1707 }
1708 }
1709 break;
1710 case CHR_IOCTL_PP_EPP_READ:
1711 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1712 struct ParallelIOArg *parg = arg;
1713 int n = read(fd, parg->buffer, parg->count);
1714 if (n != parg->count) {
1715 return -EIO;
1716 }
1717 }
1718 break;
1719 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1720 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1721 struct ParallelIOArg *parg = arg;
1722 int n = write(fd, parg->buffer, parg->count);
1723 if (n != parg->count) {
1724 return -EIO;
1725 }
1726 }
1727 break;
1728 case CHR_IOCTL_PP_EPP_WRITE:
1729 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1730 struct ParallelIOArg *parg = arg;
1731 int n = write(fd, parg->buffer, parg->count);
1732 if (n != parg->count) {
1733 return -EIO;
1734 }
1735 }
1736 break;
1737 default:
1738 return -ENOTSUP;
1739 }
1740 return 0;
1741}
1742
1743static void pp_close(CharDriverState *chr)
1744{
1745 ParallelCharDriver *drv = chr->opaque;
1746 int fd = drv->fd;
1747
1748 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1749 ioctl(fd, PPRELEASE);
1750 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001751 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001752 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001753}
1754
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001755static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001756{
1757 CharDriverState *chr;
1758 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001759
1760 if (ioctl(fd, PPCLAIM) < 0) {
1761 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001762 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001763 }
1764
Anthony Liguori7267c092011-08-20 22:09:37 -05001765 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001766 drv->fd = fd;
1767 drv->mode = IEEE1284_MODE_COMPAT;
1768
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001769 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001770 chr->chr_write = null_chr_write;
1771 chr->chr_ioctl = pp_ioctl;
1772 chr->chr_close = pp_close;
1773 chr->opaque = drv;
1774
Markus Armbruster1f514702012-02-07 15:09:08 +01001775 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001776}
1777#endif /* __linux__ */
1778
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001779#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001780
1781#define HAVE_CHARDEV_PARPORT 1
1782
blueswir16972f932008-11-22 20:49:12 +00001783static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1784{
Stefan Weile0efb992011-02-23 19:09:16 +01001785 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001786 uint8_t b;
1787
1788 switch(cmd) {
1789 case CHR_IOCTL_PP_READ_DATA:
1790 if (ioctl(fd, PPIGDATA, &b) < 0)
1791 return -ENOTSUP;
1792 *(uint8_t *)arg = b;
1793 break;
1794 case CHR_IOCTL_PP_WRITE_DATA:
1795 b = *(uint8_t *)arg;
1796 if (ioctl(fd, PPISDATA, &b) < 0)
1797 return -ENOTSUP;
1798 break;
1799 case CHR_IOCTL_PP_READ_CONTROL:
1800 if (ioctl(fd, PPIGCTRL, &b) < 0)
1801 return -ENOTSUP;
1802 *(uint8_t *)arg = b;
1803 break;
1804 case CHR_IOCTL_PP_WRITE_CONTROL:
1805 b = *(uint8_t *)arg;
1806 if (ioctl(fd, PPISCTRL, &b) < 0)
1807 return -ENOTSUP;
1808 break;
1809 case CHR_IOCTL_PP_READ_STATUS:
1810 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1811 return -ENOTSUP;
1812 *(uint8_t *)arg = b;
1813 break;
1814 default:
1815 return -ENOTSUP;
1816 }
1817 return 0;
1818}
1819
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001820static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001821{
1822 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001823
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001824 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001825 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001826 chr->chr_write = null_chr_write;
1827 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001828 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001829 return chr;
blueswir16972f932008-11-22 20:49:12 +00001830}
1831#endif
1832
aliguori6f97dba2008-10-31 18:49:55 +00001833#else /* _WIN32 */
1834
1835typedef struct {
1836 int max_size;
1837 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001838 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001839 BOOL fpipe;
1840 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001841
1842 /* Protected by the CharDriverState chr_write_lock. */
1843 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001844} WinCharState;
1845
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001846typedef struct {
1847 HANDLE hStdIn;
1848 HANDLE hInputReadyEvent;
1849 HANDLE hInputDoneEvent;
1850 HANDLE hInputThread;
1851 uint8_t win_stdio_buf;
1852} WinStdioCharState;
1853
aliguori6f97dba2008-10-31 18:49:55 +00001854#define NSENDBUF 2048
1855#define NRECVBUF 2048
1856#define MAXCONNECT 1
1857#define NTIMEOUT 5000
1858
1859static int win_chr_poll(void *opaque);
1860static int win_chr_pipe_poll(void *opaque);
1861
1862static void win_chr_close(CharDriverState *chr)
1863{
1864 WinCharState *s = chr->opaque;
1865
1866 if (s->hsend) {
1867 CloseHandle(s->hsend);
1868 s->hsend = NULL;
1869 }
1870 if (s->hrecv) {
1871 CloseHandle(s->hrecv);
1872 s->hrecv = NULL;
1873 }
1874 if (s->hcom) {
1875 CloseHandle(s->hcom);
1876 s->hcom = NULL;
1877 }
1878 if (s->fpipe)
1879 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1880 else
1881 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301882
Hans de Goedea425d232011-11-19 10:22:43 +01001883 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001884}
1885
1886static int win_chr_init(CharDriverState *chr, const char *filename)
1887{
1888 WinCharState *s = chr->opaque;
1889 COMMCONFIG comcfg;
1890 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1891 COMSTAT comstat;
1892 DWORD size;
1893 DWORD err;
1894
1895 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1896 if (!s->hsend) {
1897 fprintf(stderr, "Failed CreateEvent\n");
1898 goto fail;
1899 }
1900 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1901 if (!s->hrecv) {
1902 fprintf(stderr, "Failed CreateEvent\n");
1903 goto fail;
1904 }
1905
1906 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1907 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1908 if (s->hcom == INVALID_HANDLE_VALUE) {
1909 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1910 s->hcom = NULL;
1911 goto fail;
1912 }
1913
1914 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1915 fprintf(stderr, "Failed SetupComm\n");
1916 goto fail;
1917 }
1918
1919 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1920 size = sizeof(COMMCONFIG);
1921 GetDefaultCommConfig(filename, &comcfg, &size);
1922 comcfg.dcb.DCBlength = sizeof(DCB);
1923 CommConfigDialog(filename, NULL, &comcfg);
1924
1925 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1926 fprintf(stderr, "Failed SetCommState\n");
1927 goto fail;
1928 }
1929
1930 if (!SetCommMask(s->hcom, EV_ERR)) {
1931 fprintf(stderr, "Failed SetCommMask\n");
1932 goto fail;
1933 }
1934
1935 cto.ReadIntervalTimeout = MAXDWORD;
1936 if (!SetCommTimeouts(s->hcom, &cto)) {
1937 fprintf(stderr, "Failed SetCommTimeouts\n");
1938 goto fail;
1939 }
1940
1941 if (!ClearCommError(s->hcom, &err, &comstat)) {
1942 fprintf(stderr, "Failed ClearCommError\n");
1943 goto fail;
1944 }
1945 qemu_add_polling_cb(win_chr_poll, chr);
1946 return 0;
1947
1948 fail:
1949 win_chr_close(chr);
1950 return -1;
1951}
1952
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001953/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001954static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1955{
1956 WinCharState *s = chr->opaque;
1957 DWORD len, ret, size, err;
1958
1959 len = len1;
1960 ZeroMemory(&s->osend, sizeof(s->osend));
1961 s->osend.hEvent = s->hsend;
1962 while (len > 0) {
1963 if (s->hsend)
1964 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1965 else
1966 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1967 if (!ret) {
1968 err = GetLastError();
1969 if (err == ERROR_IO_PENDING) {
1970 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1971 if (ret) {
1972 buf += size;
1973 len -= size;
1974 } else {
1975 break;
1976 }
1977 } else {
1978 break;
1979 }
1980 } else {
1981 buf += size;
1982 len -= size;
1983 }
1984 }
1985 return len1 - len;
1986}
1987
1988static int win_chr_read_poll(CharDriverState *chr)
1989{
1990 WinCharState *s = chr->opaque;
1991
Anthony Liguori909cda12011-08-15 11:17:31 -05001992 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001993 return s->max_size;
1994}
1995
1996static void win_chr_readfile(CharDriverState *chr)
1997{
1998 WinCharState *s = chr->opaque;
1999 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302000 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002001 DWORD size;
2002
2003 ZeroMemory(&s->orecv, sizeof(s->orecv));
2004 s->orecv.hEvent = s->hrecv;
2005 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2006 if (!ret) {
2007 err = GetLastError();
2008 if (err == ERROR_IO_PENDING) {
2009 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2010 }
2011 }
2012
2013 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002014 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002015 }
2016}
2017
2018static void win_chr_read(CharDriverState *chr)
2019{
2020 WinCharState *s = chr->opaque;
2021
2022 if (s->len > s->max_size)
2023 s->len = s->max_size;
2024 if (s->len == 0)
2025 return;
2026
2027 win_chr_readfile(chr);
2028}
2029
2030static int win_chr_poll(void *opaque)
2031{
2032 CharDriverState *chr = opaque;
2033 WinCharState *s = chr->opaque;
2034 COMSTAT status;
2035 DWORD comerr;
2036
2037 ClearCommError(s->hcom, &comerr, &status);
2038 if (status.cbInQue > 0) {
2039 s->len = status.cbInQue;
2040 win_chr_read_poll(chr);
2041 win_chr_read(chr);
2042 return 1;
2043 }
2044 return 0;
2045}
2046
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01002047static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00002048{
2049 CharDriverState *chr;
2050 WinCharState *s;
2051
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002052 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002053 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002054 chr->opaque = s;
2055 chr->chr_write = win_chr_write;
2056 chr->chr_close = win_chr_close;
2057
2058 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002059 g_free(s);
2060 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002061 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002062 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002063 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002064}
2065
2066static int win_chr_pipe_poll(void *opaque)
2067{
2068 CharDriverState *chr = opaque;
2069 WinCharState *s = chr->opaque;
2070 DWORD size;
2071
2072 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2073 if (size > 0) {
2074 s->len = size;
2075 win_chr_read_poll(chr);
2076 win_chr_read(chr);
2077 return 1;
2078 }
2079 return 0;
2080}
2081
2082static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2083{
2084 WinCharState *s = chr->opaque;
2085 OVERLAPPED ov;
2086 int ret;
2087 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002088 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002089
2090 s->fpipe = TRUE;
2091
2092 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2093 if (!s->hsend) {
2094 fprintf(stderr, "Failed CreateEvent\n");
2095 goto fail;
2096 }
2097 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2098 if (!s->hrecv) {
2099 fprintf(stderr, "Failed CreateEvent\n");
2100 goto fail;
2101 }
2102
2103 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2104 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2105 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2106 PIPE_WAIT,
2107 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2108 if (s->hcom == INVALID_HANDLE_VALUE) {
2109 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2110 s->hcom = NULL;
2111 goto fail;
2112 }
2113
2114 ZeroMemory(&ov, sizeof(ov));
2115 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2116 ret = ConnectNamedPipe(s->hcom, &ov);
2117 if (ret) {
2118 fprintf(stderr, "Failed ConnectNamedPipe\n");
2119 goto fail;
2120 }
2121
2122 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2123 if (!ret) {
2124 fprintf(stderr, "Failed GetOverlappedResult\n");
2125 if (ov.hEvent) {
2126 CloseHandle(ov.hEvent);
2127 ov.hEvent = NULL;
2128 }
2129 goto fail;
2130 }
2131
2132 if (ov.hEvent) {
2133 CloseHandle(ov.hEvent);
2134 ov.hEvent = NULL;
2135 }
2136 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2137 return 0;
2138
2139 fail:
2140 win_chr_close(chr);
2141 return -1;
2142}
2143
2144
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002145static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002146{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002147 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002148 CharDriverState *chr;
2149 WinCharState *s;
2150
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002151 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002152 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002153 chr->opaque = s;
2154 chr->chr_write = win_chr_write;
2155 chr->chr_close = win_chr_close;
2156
2157 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002158 g_free(s);
2159 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002160 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002161 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002162 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002163}
2164
Markus Armbruster1f514702012-02-07 15:09:08 +01002165static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002166{
2167 CharDriverState *chr;
2168 WinCharState *s;
2169
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002170 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002171 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002172 s->hcom = fd_out;
2173 chr->opaque = s;
2174 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002175 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002176}
2177
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002178static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002179{
Markus Armbruster1f514702012-02-07 15:09:08 +01002180 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002181}
2182
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002183static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2184{
2185 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2186 DWORD dwSize;
2187 int len1;
2188
2189 len1 = len;
2190
2191 while (len1 > 0) {
2192 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2193 break;
2194 }
2195 buf += dwSize;
2196 len1 -= dwSize;
2197 }
2198
2199 return len - len1;
2200}
2201
2202static void win_stdio_wait_func(void *opaque)
2203{
2204 CharDriverState *chr = opaque;
2205 WinStdioCharState *stdio = chr->opaque;
2206 INPUT_RECORD buf[4];
2207 int ret;
2208 DWORD dwSize;
2209 int i;
2210
Stefan Weildff74242013-12-07 14:48:04 +01002211 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002212
2213 if (!ret) {
2214 /* Avoid error storm */
2215 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2216 return;
2217 }
2218
2219 for (i = 0; i < dwSize; i++) {
2220 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2221
2222 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2223 int j;
2224 if (kev->uChar.AsciiChar != 0) {
2225 for (j = 0; j < kev->wRepeatCount; j++) {
2226 if (qemu_chr_be_can_write(chr)) {
2227 uint8_t c = kev->uChar.AsciiChar;
2228 qemu_chr_be_write(chr, &c, 1);
2229 }
2230 }
2231 }
2232 }
2233 }
2234}
2235
2236static DWORD WINAPI win_stdio_thread(LPVOID param)
2237{
2238 CharDriverState *chr = param;
2239 WinStdioCharState *stdio = chr->opaque;
2240 int ret;
2241 DWORD dwSize;
2242
2243 while (1) {
2244
2245 /* Wait for one byte */
2246 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2247
2248 /* Exit in case of error, continue if nothing read */
2249 if (!ret) {
2250 break;
2251 }
2252 if (!dwSize) {
2253 continue;
2254 }
2255
2256 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2257 if (stdio->win_stdio_buf == '\r') {
2258 continue;
2259 }
2260
2261 /* Signal the main thread and wait until the byte was eaten */
2262 if (!SetEvent(stdio->hInputReadyEvent)) {
2263 break;
2264 }
2265 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2266 != WAIT_OBJECT_0) {
2267 break;
2268 }
2269 }
2270
2271 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2272 return 0;
2273}
2274
2275static void win_stdio_thread_wait_func(void *opaque)
2276{
2277 CharDriverState *chr = opaque;
2278 WinStdioCharState *stdio = chr->opaque;
2279
2280 if (qemu_chr_be_can_write(chr)) {
2281 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2282 }
2283
2284 SetEvent(stdio->hInputDoneEvent);
2285}
2286
2287static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2288{
2289 WinStdioCharState *stdio = chr->opaque;
2290 DWORD dwMode = 0;
2291
2292 GetConsoleMode(stdio->hStdIn, &dwMode);
2293
2294 if (echo) {
2295 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2296 } else {
2297 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2298 }
2299}
2300
2301static void win_stdio_close(CharDriverState *chr)
2302{
2303 WinStdioCharState *stdio = chr->opaque;
2304
2305 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2306 CloseHandle(stdio->hInputReadyEvent);
2307 }
2308 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2309 CloseHandle(stdio->hInputDoneEvent);
2310 }
2311 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2312 TerminateThread(stdio->hInputThread, 0);
2313 }
2314
2315 g_free(chr->opaque);
2316 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002317}
2318
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002319static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002320{
2321 CharDriverState *chr;
2322 WinStdioCharState *stdio;
2323 DWORD dwMode;
2324 int is_console = 0;
2325
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002326 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002327 stdio = g_malloc0(sizeof(WinStdioCharState));
2328
2329 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2330 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2331 fprintf(stderr, "cannot open stdio: invalid handle\n");
2332 exit(1);
2333 }
2334
2335 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2336
2337 chr->opaque = stdio;
2338 chr->chr_write = win_stdio_write;
2339 chr->chr_close = win_stdio_close;
2340
Anthony Liguoried7a1542013-03-05 23:21:17 +05302341 if (is_console) {
2342 if (qemu_add_wait_object(stdio->hStdIn,
2343 win_stdio_wait_func, chr)) {
2344 fprintf(stderr, "qemu_add_wait_object: failed\n");
2345 }
2346 } else {
2347 DWORD dwId;
2348
2349 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2350 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2351 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2352 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002353
Anthony Liguoried7a1542013-03-05 23:21:17 +05302354 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2355 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2356 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2357 fprintf(stderr, "cannot create stdio thread or event\n");
2358 exit(1);
2359 }
2360 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2361 win_stdio_thread_wait_func, chr)) {
2362 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002363 }
2364 }
2365
2366 dwMode |= ENABLE_LINE_INPUT;
2367
Anthony Liguoried7a1542013-03-05 23:21:17 +05302368 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002369 /* set the terminal in raw mode */
2370 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2371 dwMode |= ENABLE_PROCESSED_INPUT;
2372 }
2373
2374 SetConsoleMode(stdio->hStdIn, dwMode);
2375
2376 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2377 qemu_chr_fe_set_echo(chr, false);
2378
Markus Armbruster1f514702012-02-07 15:09:08 +01002379 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002380}
aliguori6f97dba2008-10-31 18:49:55 +00002381#endif /* !_WIN32 */
2382
Anthony Liguori5ab82112013-03-05 23:21:31 +05302383
aliguori6f97dba2008-10-31 18:49:55 +00002384/***********************************************************/
2385/* UDP Net console */
2386
2387typedef struct {
2388 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302389 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302390 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002391 int bufcnt;
2392 int bufptr;
2393 int max_size;
2394} NetCharDriver;
2395
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002396/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002397static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2398{
2399 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302400 gsize bytes_written;
2401 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002402
Anthony Liguori76a96442013-03-05 23:21:21 +05302403 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2404 if (status == G_IO_STATUS_EOF) {
2405 return 0;
2406 } else if (status != G_IO_STATUS_NORMAL) {
2407 return -1;
2408 }
2409
2410 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002411}
2412
2413static int udp_chr_read_poll(void *opaque)
2414{
2415 CharDriverState *chr = opaque;
2416 NetCharDriver *s = chr->opaque;
2417
Anthony Liguori909cda12011-08-15 11:17:31 -05002418 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002419
2420 /* If there were any stray characters in the queue process them
2421 * first
2422 */
2423 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002424 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002425 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002426 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002427 }
2428 return s->max_size;
2429}
2430
Anthony Liguori76a96442013-03-05 23:21:21 +05302431static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002432{
2433 CharDriverState *chr = opaque;
2434 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302435 gsize bytes_read = 0;
2436 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002437
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002438 if (s->max_size == 0) {
2439 return TRUE;
2440 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302441 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2442 &bytes_read, NULL);
2443 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002444 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302445 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302446 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302447 return FALSE;
2448 }
aliguori6f97dba2008-10-31 18:49:55 +00002449
2450 s->bufptr = 0;
2451 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002452 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002453 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002454 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002455 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302456
2457 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002458}
2459
2460static void udp_chr_update_read_handler(CharDriverState *chr)
2461{
2462 NetCharDriver *s = chr->opaque;
2463
Amit Shah26da70c2013-08-28 15:23:37 +05302464 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302465 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302466 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2467 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002468 }
2469}
2470
aliguori819f56b2009-03-28 17:58:14 +00002471static void udp_chr_close(CharDriverState *chr)
2472{
2473 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302474
2475 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302476 if (s->chan) {
2477 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002478 closesocket(s->fd);
2479 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002480 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002481 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002482}
2483
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002484static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002485{
2486 CharDriverState *chr = NULL;
2487 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002488
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002489 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002490 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002491
aliguori6f97dba2008-10-31 18:49:55 +00002492 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302493 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002494 s->bufcnt = 0;
2495 s->bufptr = 0;
2496 chr->opaque = s;
2497 chr->chr_write = udp_chr_write;
2498 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002499 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002500 /* be isn't opened until we get a connection */
2501 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002502 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002503}
aliguori6f97dba2008-10-31 18:49:55 +00002504
aliguori6f97dba2008-10-31 18:49:55 +00002505/***********************************************************/
2506/* TCP Net console */
2507
2508typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302509
2510 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302511 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002512 int fd, listen_fd;
2513 int connected;
2514 int max_size;
2515 int do_telnetopt;
2516 int do_nodelay;
2517 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002518 int *read_msgfds;
2519 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002520 int *write_msgfds;
2521 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002522
2523 SocketAddress *addr;
2524 bool is_listen;
2525 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002526
2527 guint reconnect_timer;
2528 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002529 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002530} TCPCharDriver;
2531
Corey Minyard5dd1f022014-10-02 11:17:37 -05002532static gboolean socket_reconnect_timeout(gpointer opaque);
2533
2534static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2535{
2536 TCPCharDriver *s = chr->opaque;
2537 assert(s->connected == 0);
2538 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2539 socket_reconnect_timeout, chr);
2540}
2541
Corey Minyard5008e5b2014-10-08 07:11:55 -05002542static void check_report_connect_error(CharDriverState *chr,
2543 Error *err)
2544{
2545 TCPCharDriver *s = chr->opaque;
2546
2547 if (!s->connect_err_reported) {
2548 error_report("Unable to connect character device %s: %s",
2549 chr->label, error_get_pretty(err));
2550 s->connect_err_reported = true;
2551 }
2552 qemu_chr_socket_restart_timer(chr);
2553}
2554
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302555static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002556
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002557#ifndef _WIN32
2558static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2559{
2560 TCPCharDriver *s = chr->opaque;
2561 struct msghdr msgh;
2562 struct iovec iov;
2563 int r;
2564
2565 size_t fd_size = s->write_msgfds_num * sizeof(int);
2566 char control[CMSG_SPACE(fd_size)];
2567 struct cmsghdr *cmsg;
2568
2569 memset(&msgh, 0, sizeof(msgh));
2570 memset(control, 0, sizeof(control));
2571
2572 /* set the payload */
2573 iov.iov_base = (uint8_t *) buf;
2574 iov.iov_len = len;
2575
2576 msgh.msg_iov = &iov;
2577 msgh.msg_iovlen = 1;
2578
2579 msgh.msg_control = control;
2580 msgh.msg_controllen = sizeof(control);
2581
2582 cmsg = CMSG_FIRSTHDR(&msgh);
2583
2584 cmsg->cmsg_len = CMSG_LEN(fd_size);
2585 cmsg->cmsg_level = SOL_SOCKET;
2586 cmsg->cmsg_type = SCM_RIGHTS;
2587 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2588
2589 do {
2590 r = sendmsg(s->fd, &msgh, 0);
2591 } while (r < 0 && errno == EINTR);
2592
2593 /* free the written msgfds, no matter what */
2594 if (s->write_msgfds_num) {
2595 g_free(s->write_msgfds);
2596 s->write_msgfds = 0;
2597 s->write_msgfds_num = 0;
2598 }
2599
2600 return r;
2601}
2602#endif
2603
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002604/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002605static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2606{
2607 TCPCharDriver *s = chr->opaque;
2608 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002609#ifndef _WIN32
2610 if (s->is_unix && s->write_msgfds_num) {
2611 return unix_send_msgfds(chr, buf, len);
2612 } else
2613#endif
2614 {
2615 return io_channel_send(s->chan, buf, len);
2616 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002617 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002618 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002619 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002620 }
2621}
2622
2623static int tcp_chr_read_poll(void *opaque)
2624{
2625 CharDriverState *chr = opaque;
2626 TCPCharDriver *s = chr->opaque;
2627 if (!s->connected)
2628 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002629 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002630 return s->max_size;
2631}
2632
2633#define IAC 255
2634#define IAC_BREAK 243
2635static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2636 TCPCharDriver *s,
2637 uint8_t *buf, int *size)
2638{
2639 /* Handle any telnet client's basic IAC options to satisfy char by
2640 * char mode with no echo. All IAC options will be removed from
2641 * the buf and the do_telnetopt variable will be used to track the
2642 * state of the width of the IAC information.
2643 *
2644 * IAC commands come in sets of 3 bytes with the exception of the
2645 * "IAC BREAK" command and the double IAC.
2646 */
2647
2648 int i;
2649 int j = 0;
2650
2651 for (i = 0; i < *size; i++) {
2652 if (s->do_telnetopt > 1) {
2653 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2654 /* Double IAC means send an IAC */
2655 if (j != i)
2656 buf[j] = buf[i];
2657 j++;
2658 s->do_telnetopt = 1;
2659 } else {
2660 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2661 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002662 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002663 s->do_telnetopt++;
2664 }
2665 s->do_telnetopt++;
2666 }
2667 if (s->do_telnetopt >= 4) {
2668 s->do_telnetopt = 1;
2669 }
2670 } else {
2671 if ((unsigned char)buf[i] == IAC) {
2672 s->do_telnetopt = 2;
2673 } else {
2674 if (j != i)
2675 buf[j] = buf[i];
2676 j++;
2677 }
2678 }
2679 }
2680 *size = j;
2681}
2682
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002683static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002684{
2685 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002686 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2687
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002688 assert(num <= TCP_MAX_FDS);
2689
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002690 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002691 int i;
2692
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002693 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2694
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002695 /* Close unused fds */
2696 for (i = to_copy; i < s->read_msgfds_num; i++) {
2697 close(s->read_msgfds[i]);
2698 }
2699
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002700 g_free(s->read_msgfds);
2701 s->read_msgfds = 0;
2702 s->read_msgfds_num = 0;
2703 }
2704
2705 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002706}
2707
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002708static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2709{
2710 TCPCharDriver *s = chr->opaque;
2711
2712 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002713 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002714
2715 if (num) {
2716 s->write_msgfds = g_malloc(num * sizeof(int));
2717 memcpy(s->write_msgfds, fds, num * sizeof(int));
2718 }
2719
2720 s->write_msgfds_num = num;
2721
2722 return 0;
2723}
2724
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002725#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002726static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2727{
2728 TCPCharDriver *s = chr->opaque;
2729 struct cmsghdr *cmsg;
2730
2731 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002732 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002733
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002734 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002735 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002736 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002737 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002738 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002739
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002740 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2741
2742 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002743 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002744 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002745
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002746 /* close and clean read_msgfds */
2747 for (i = 0; i < s->read_msgfds_num; i++) {
2748 close(s->read_msgfds[i]);
2749 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002750
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002751 if (s->read_msgfds_num) {
2752 g_free(s->read_msgfds);
2753 }
2754
2755 s->read_msgfds_num = fd_size / sizeof(int);
2756 s->read_msgfds = g_malloc(fd_size);
2757 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2758
2759 for (i = 0; i < s->read_msgfds_num; i++) {
2760 int fd = s->read_msgfds[i];
2761 if (fd < 0) {
2762 continue;
2763 }
2764
2765 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2766 qemu_set_block(fd);
2767
2768 #ifndef MSG_CMSG_CLOEXEC
2769 qemu_set_cloexec(fd);
2770 #endif
2771 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002772 }
2773}
2774
Mark McLoughlin9977c892009-07-22 09:11:38 +01002775static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2776{
2777 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002778 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002779 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002780 union {
2781 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002782 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002783 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002784 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002785 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002786
2787 iov[0].iov_base = buf;
2788 iov[0].iov_len = len;
2789
2790 msg.msg_iov = iov;
2791 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002792 msg.msg_control = &msg_control;
2793 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002794
Corey Bryant06138652012-08-14 16:43:42 -04002795#ifdef MSG_CMSG_CLOEXEC
2796 flags |= MSG_CMSG_CLOEXEC;
2797#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002798 do {
2799 ret = recvmsg(s->fd, &msg, flags);
2800 } while (ret == -1 && errno == EINTR);
2801
Corey Bryant06138652012-08-14 16:43:42 -04002802 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002803 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002804 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002805
2806 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002807}
2808#else
2809static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2810{
2811 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002812 ssize_t ret;
2813
2814 do {
2815 ret = qemu_recv(s->fd, buf, len, 0);
2816 } while (ret == -1 && socket_error() == EINTR);
2817
2818 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002819}
2820#endif
2821
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302822static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2823{
2824 TCPCharDriver *s = chr->opaque;
2825 return g_io_create_watch(s->chan, cond);
2826}
2827
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002828static void tcp_chr_disconnect(CharDriverState *chr)
2829{
2830 TCPCharDriver *s = chr->opaque;
2831
2832 s->connected = 0;
2833 if (s->listen_chan) {
2834 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2835 tcp_chr_accept, chr);
2836 }
2837 remove_fd_in_watch(chr);
2838 g_io_channel_unref(s->chan);
2839 s->chan = NULL;
2840 closesocket(s->fd);
2841 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002842 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2843 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002844 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002845 if (s->reconnect_time) {
2846 qemu_chr_socket_restart_timer(chr);
2847 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002848}
2849
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302850static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002851{
2852 CharDriverState *chr = opaque;
2853 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302854 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002855 int len, size;
2856
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302857 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002858 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302859 }
aliguori6f97dba2008-10-31 18:49:55 +00002860 len = sizeof(buf);
2861 if (len > s->max_size)
2862 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002863 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002864 if (size == 0 ||
2865 (size < 0 &&
2866 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002867 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002868 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002869 } else if (size > 0) {
2870 if (s->do_telnetopt)
2871 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2872 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002873 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002874 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302875
2876 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002877}
2878
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002879static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2880{
2881 TCPCharDriver *s = chr->opaque;
2882 int size;
2883
2884 if (!s->connected) {
2885 return 0;
2886 }
2887
2888 size = tcp_chr_recv(chr, (void *) buf, len);
2889 if (size == 0) {
2890 /* connection closed */
2891 tcp_chr_disconnect(chr);
2892 }
2893
2894 return size;
2895}
2896
Blue Swirl68c18d12010-08-15 09:46:24 +00002897#ifndef _WIN32
2898CharDriverState *qemu_chr_open_eventfd(int eventfd)
2899{
David Marchande9d21c42014-06-11 17:25:16 +02002900 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2901
2902 if (chr) {
2903 chr->avail_connections = 1;
2904 }
2905
2906 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002907}
Blue Swirl68c18d12010-08-15 09:46:24 +00002908#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002909
aliguori6f97dba2008-10-31 18:49:55 +00002910static void tcp_chr_connect(void *opaque)
2911{
2912 CharDriverState *chr = opaque;
2913 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002914 struct sockaddr_storage ss, ps;
2915 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002916
2917 memset(&ss, 0, ss_len);
2918 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2919 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2920 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002921 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2922 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2923 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002924 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002925 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2926 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002927 s->is_listen, s->is_telnet);
2928 }
aliguori6f97dba2008-10-31 18:49:55 +00002929
2930 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302931 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302932 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2933 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002934 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002935 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002936}
2937
Gal Hammerac1b84d2014-02-25 12:12:35 +02002938static void tcp_chr_update_read_handler(CharDriverState *chr)
2939{
2940 TCPCharDriver *s = chr->opaque;
2941
2942 remove_fd_in_watch(chr);
2943 if (s->chan) {
2944 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2945 tcp_chr_read, chr);
2946 }
2947}
2948
aliguori6f97dba2008-10-31 18:49:55 +00002949#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2950static void tcp_chr_telnet_init(int fd)
2951{
2952 char buf[3];
2953 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2954 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2955 send(fd, (char *)buf, 3, 0);
2956 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2957 send(fd, (char *)buf, 3, 0);
2958 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2959 send(fd, (char *)buf, 3, 0);
2960 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2961 send(fd, (char *)buf, 3, 0);
2962}
2963
Daniel P. Berrange13661082011-06-23 13:31:42 +01002964static int tcp_chr_add_client(CharDriverState *chr, int fd)
2965{
2966 TCPCharDriver *s = chr->opaque;
2967 if (s->fd != -1)
2968 return -1;
2969
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002970 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002971 if (s->do_nodelay)
2972 socket_set_nodelay(fd);
2973 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302974 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002975 if (s->listen_tag) {
2976 g_source_remove(s->listen_tag);
2977 s->listen_tag = 0;
2978 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002979 tcp_chr_connect(chr);
2980
2981 return 0;
2982}
2983
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302984static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002985{
2986 CharDriverState *chr = opaque;
2987 TCPCharDriver *s = chr->opaque;
2988 struct sockaddr_in saddr;
2989#ifndef _WIN32
2990 struct sockaddr_un uaddr;
2991#endif
2992 struct sockaddr *addr;
2993 socklen_t len;
2994 int fd;
2995
2996 for(;;) {
2997#ifndef _WIN32
2998 if (s->is_unix) {
2999 len = sizeof(uaddr);
3000 addr = (struct sockaddr *)&uaddr;
3001 } else
3002#endif
3003 {
3004 len = sizeof(saddr);
3005 addr = (struct sockaddr *)&saddr;
3006 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003007 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003008 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003009 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303010 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003011 } else if (fd >= 0) {
3012 if (s->do_telnetopt)
3013 tcp_chr_telnet_init(fd);
3014 break;
3015 }
3016 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003017 if (tcp_chr_add_client(chr, fd) < 0)
3018 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303019
3020 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003021}
3022
3023static void tcp_chr_close(CharDriverState *chr)
3024{
3025 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003026 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003027
Corey Minyard5dd1f022014-10-02 11:17:37 -05003028 if (s->reconnect_timer) {
3029 g_source_remove(s->reconnect_timer);
3030 s->reconnect_timer = 0;
3031 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003032 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003033 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303034 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303035 if (s->chan) {
3036 g_io_channel_unref(s->chan);
3037 }
aliguori6f97dba2008-10-31 18:49:55 +00003038 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003039 }
3040 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303041 if (s->listen_tag) {
3042 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003043 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303044 }
3045 if (s->listen_chan) {
3046 g_io_channel_unref(s->listen_chan);
3047 }
aliguori6f97dba2008-10-31 18:49:55 +00003048 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003049 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003050 if (s->read_msgfds_num) {
3051 for (i = 0; i < s->read_msgfds_num; i++) {
3052 close(s->read_msgfds[i]);
3053 }
3054 g_free(s->read_msgfds);
3055 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003056 if (s->write_msgfds_num) {
3057 g_free(s->write_msgfds);
3058 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003059 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003060 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003061}
3062
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003063static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003064{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003065 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003066
Corey Minyardcfb429c2014-10-02 11:17:35 -05003067 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003068 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303069 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003070 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3071 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003072 } else {
3073 s->connected = 1;
3074 s->fd = fd;
3075 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303076 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003077 tcp_chr_connect(chr);
3078 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003079}
3080
Corey Minyard51795022014-10-08 07:11:56 -05003081static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003082{
3083 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003084 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003085
3086 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003087 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003088 return;
3089 }
3090
Corey Minyard5008e5b2014-10-08 07:11:55 -05003091 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003092 qemu_chr_finish_socket_connection(chr, fd);
3093}
3094
Corey Minyardcfb429c2014-10-02 11:17:35 -05003095static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003096{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003097 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003098 int fd;
3099
Corey Minyardcfb429c2014-10-02 11:17:35 -05003100 if (s->is_listen) {
3101 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003102 } else if (s->reconnect_time) {
3103 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3104 return fd >= 0;
3105 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003106 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003107 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003108 if (fd < 0) {
3109 return false;
3110 }
3111
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003112 qemu_chr_finish_socket_connection(chr, fd);
3113 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003114}
3115
Lei Li51767e72013-01-25 00:03:19 +08003116/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003117/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003118
3119typedef struct {
3120 size_t size;
3121 size_t prod;
3122 size_t cons;
3123 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003124} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003125
Markus Armbruster3949e592013-02-06 21:27:24 +01003126static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003127{
Markus Armbruster3949e592013-02-06 21:27:24 +01003128 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003129
Markus Armbruster5c230102013-02-06 21:27:23 +01003130 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003131}
3132
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003133/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003134static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003135{
Markus Armbruster3949e592013-02-06 21:27:24 +01003136 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003137 int i;
3138
3139 if (!buf || (len < 0)) {
3140 return -1;
3141 }
3142
3143 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003144 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3145 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003146 d->cons = d->prod - d->size;
3147 }
3148 }
3149
3150 return 0;
3151}
3152
Markus Armbruster3949e592013-02-06 21:27:24 +01003153static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003154{
Markus Armbruster3949e592013-02-06 21:27:24 +01003155 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003156 int i;
3157
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003158 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003159 for (i = 0; i < len && d->cons != d->prod; i++) {
3160 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003161 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003162 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003163
3164 return i;
3165}
3166
Markus Armbruster3949e592013-02-06 21:27:24 +01003167static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003168{
Markus Armbruster3949e592013-02-06 21:27:24 +01003169 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003170
3171 g_free(d->cbuf);
3172 g_free(d);
3173 chr->opaque = NULL;
3174}
3175
Markus Armbruster4f573782013-07-26 16:44:32 +02003176static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3177 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003178{
3179 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003180 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003181
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003182 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003183 d = g_malloc(sizeof(*d));
3184
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003185 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003186
3187 /* The size must be power of 2 */
3188 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003189 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003190 goto fail;
3191 }
3192
3193 d->prod = 0;
3194 d->cons = 0;
3195 d->cbuf = g_malloc0(d->size);
3196
3197 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003198 chr->chr_write = ringbuf_chr_write;
3199 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003200
3201 return chr;
3202
3203fail:
3204 g_free(d);
3205 g_free(chr);
3206 return NULL;
3207}
3208
Hani Benhabiles8e597772014-05-27 23:39:30 +01003209bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003210{
Markus Armbruster3949e592013-02-06 21:27:24 +01003211 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003212}
3213
Markus Armbruster3949e592013-02-06 21:27:24 +01003214void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003215 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003216 Error **errp)
3217{
3218 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003219 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003220 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003221 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003222
3223 chr = qemu_chr_find(device);
3224 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003225 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003226 return;
3227 }
3228
Markus Armbruster3949e592013-02-06 21:27:24 +01003229 if (!chr_is_ringbuf(chr)) {
3230 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003231 return;
3232 }
3233
Lei Li1f590cf2013-01-25 00:03:20 +08003234 if (has_format && (format == DATA_FORMAT_BASE64)) {
3235 write_data = g_base64_decode(data, &write_count);
3236 } else {
3237 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003238 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003239 }
3240
Markus Armbruster3949e592013-02-06 21:27:24 +01003241 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003242
Markus Armbruster13289fb2013-02-06 21:27:18 +01003243 if (write_data != (uint8_t *)data) {
3244 g_free((void *)write_data);
3245 }
3246
Lei Li1f590cf2013-01-25 00:03:20 +08003247 if (ret < 0) {
3248 error_setg(errp, "Failed to write to device %s", device);
3249 return;
3250 }
3251}
3252
Markus Armbruster3949e592013-02-06 21:27:24 +01003253char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003254 bool has_format, enum DataFormat format,
3255 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003256{
3257 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003258 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003259 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003260 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003261
3262 chr = qemu_chr_find(device);
3263 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003264 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003265 return NULL;
3266 }
3267
Markus Armbruster3949e592013-02-06 21:27:24 +01003268 if (!chr_is_ringbuf(chr)) {
3269 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003270 return NULL;
3271 }
3272
3273 if (size <= 0) {
3274 error_setg(errp, "size must be greater than zero");
3275 return NULL;
3276 }
3277
Markus Armbruster3949e592013-02-06 21:27:24 +01003278 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003279 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003280 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003281
Markus Armbruster3949e592013-02-06 21:27:24 +01003282 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003283
3284 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003285 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003286 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003287 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003288 /*
3289 * FIXME should read only complete, valid UTF-8 characters up
3290 * to @size bytes. Invalid sequences should be replaced by a
3291 * suitable replacement character. Except when (and only
3292 * when) ring buffer lost characters since last read, initial
3293 * continuation characters should be dropped.
3294 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003295 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003296 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003297 }
3298
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003299 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003300}
3301
Gerd Hoffmann33521632009-12-08 13:11:49 +01003302QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003303{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003304 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003305 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003306 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003307 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003308 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003309
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003310 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003311 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003312 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003313 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003314 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003315
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003316 if (strstart(filename, "mon:", &p)) {
3317 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003318 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003319 if (strcmp(filename, "stdio") == 0) {
3320 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3321 * but pass it to the guest. Handle this only for compat syntax,
3322 * for -chardev syntax we have special option for this.
3323 * This is what -nographic did, redirecting+muxing serial+monitor
3324 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003325 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003326 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003327 }
3328
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003329 if (strcmp(filename, "null") == 0 ||
3330 strcmp(filename, "pty") == 0 ||
3331 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003332 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003333 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003334 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003335 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003336 return opts;
3337 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003338 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003339 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003340 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003341 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003342 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003343 qemu_opt_set(opts, "width", width, &error_abort);
3344 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003345 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003346 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003347 qemu_opt_set(opts, "cols", width, &error_abort);
3348 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003349 } else {
3350 goto fail;
3351 }
3352 }
3353 return opts;
3354 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003355 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003356 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003357 return opts;
3358 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003359 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003360 qemu_opt_set(opts, "backend", "serial", &error_abort);
3361 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003362 return opts;
3363 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003364 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003365 qemu_opt_set(opts, "backend", "file", &error_abort);
3366 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003367 return opts;
3368 }
3369 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003370 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3371 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003372 return opts;
3373 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003374 if (strstart(filename, "tcp:", &p) ||
3375 strstart(filename, "telnet:", &p)) {
3376 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3377 host[0] = 0;
3378 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3379 goto fail;
3380 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003381 qemu_opt_set(opts, "backend", "socket", &error_abort);
3382 qemu_opt_set(opts, "host", host, &error_abort);
3383 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003384 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003385 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3386 if (local_err) {
3387 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003388 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003389 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003390 }
3391 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003392 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003393 return opts;
3394 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003395 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003396 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003397 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3398 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003399 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003400 goto fail;
3401 }
3402 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003403 qemu_opt_set(opts, "host", host, &error_abort);
3404 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003405 if (p[pos] == '@') {
3406 p += pos + 1;
3407 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3408 host[0] = 0;
3409 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003410 goto fail;
3411 }
3412 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003413 qemu_opt_set(opts, "localaddr", host, &error_abort);
3414 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003415 }
3416 return opts;
3417 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003418 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003419 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003420 qemu_opts_do_parse(opts, p, "path", &local_err);
3421 if (local_err) {
3422 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003423 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003424 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003425 return opts;
3426 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003427 if (strstart(filename, "/dev/parport", NULL) ||
3428 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003429 qemu_opt_set(opts, "backend", "parport", &error_abort);
3430 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003431 return opts;
3432 }
3433 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003434 qemu_opt_set(opts, "backend", "tty", &error_abort);
3435 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003436 return opts;
3437 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003438
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003439fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003440 qemu_opts_del(opts);
3441 return NULL;
3442}
3443
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003444static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3445 Error **errp)
3446{
3447 const char *path = qemu_opt_get(opts, "path");
3448
3449 if (path == NULL) {
3450 error_setg(errp, "chardev: file: no filename given");
3451 return;
3452 }
3453 backend->file = g_new0(ChardevFile, 1);
3454 backend->file->out = g_strdup(path);
3455}
3456
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003457static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3458 Error **errp)
3459{
3460 backend->stdio = g_new0(ChardevStdio, 1);
3461 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003462 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003463}
3464
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003465static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3466 Error **errp)
3467{
3468 const char *device = qemu_opt_get(opts, "path");
3469
3470 if (device == NULL) {
3471 error_setg(errp, "chardev: serial/tty: no device path given");
3472 return;
3473 }
3474 backend->serial = g_new0(ChardevHostdev, 1);
3475 backend->serial->device = g_strdup(device);
3476}
3477
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003478static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3479 Error **errp)
3480{
3481 const char *device = qemu_opt_get(opts, "path");
3482
3483 if (device == NULL) {
3484 error_setg(errp, "chardev: parallel: no device path given");
3485 return;
3486 }
3487 backend->parallel = g_new0(ChardevHostdev, 1);
3488 backend->parallel->device = g_strdup(device);
3489}
3490
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003491static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3492 Error **errp)
3493{
3494 const char *device = qemu_opt_get(opts, "path");
3495
3496 if (device == NULL) {
3497 error_setg(errp, "chardev: pipe: no device path given");
3498 return;
3499 }
3500 backend->pipe = g_new0(ChardevHostdev, 1);
3501 backend->pipe->device = g_strdup(device);
3502}
3503
Markus Armbruster4f573782013-07-26 16:44:32 +02003504static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3505 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003506{
3507 int val;
3508
Markus Armbruster3a1da422013-07-26 16:44:34 +02003509 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003510
Markus Armbruster0f953052013-06-27 16:22:07 +02003511 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003512 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003513 backend->ringbuf->has_size = true;
3514 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003515 }
3516}
3517
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003518static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3519 Error **errp)
3520{
3521 const char *chardev = qemu_opt_get(opts, "chardev");
3522
3523 if (chardev == NULL) {
3524 error_setg(errp, "chardev: mux: no chardev given");
3525 return;
3526 }
3527 backend->mux = g_new0(ChardevMux, 1);
3528 backend->mux->chardev = g_strdup(chardev);
3529}
3530
Peter Maydelldafd3252014-09-02 11:24:13 +01003531static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3532 Error **errp)
3533{
3534 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3535 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3536 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3537 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003538 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003539 const char *path = qemu_opt_get(opts, "path");
3540 const char *host = qemu_opt_get(opts, "host");
3541 const char *port = qemu_opt_get(opts, "port");
3542 SocketAddress *addr;
3543
3544 if (!path) {
3545 if (!host) {
3546 error_setg(errp, "chardev: socket: no host given");
3547 return;
3548 }
3549 if (!port) {
3550 error_setg(errp, "chardev: socket: no port given");
3551 return;
3552 }
3553 }
3554
3555 backend->socket = g_new0(ChardevSocket, 1);
3556
3557 backend->socket->has_nodelay = true;
3558 backend->socket->nodelay = do_nodelay;
3559 backend->socket->has_server = true;
3560 backend->socket->server = is_listen;
3561 backend->socket->has_telnet = true;
3562 backend->socket->telnet = is_telnet;
3563 backend->socket->has_wait = true;
3564 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003565 backend->socket->has_reconnect = true;
3566 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003567
3568 addr = g_new0(SocketAddress, 1);
3569 if (path) {
3570 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3571 addr->q_unix = g_new0(UnixSocketAddress, 1);
3572 addr->q_unix->path = g_strdup(path);
3573 } else {
3574 addr->kind = SOCKET_ADDRESS_KIND_INET;
3575 addr->inet = g_new0(InetSocketAddress, 1);
3576 addr->inet->host = g_strdup(host);
3577 addr->inet->port = g_strdup(port);
3578 addr->inet->has_to = qemu_opt_get(opts, "to");
3579 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3580 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3581 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3582 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3583 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3584 }
3585 backend->socket->addr = addr;
3586}
3587
Peter Maydell90a14bf2014-09-02 11:24:15 +01003588static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3589 Error **errp)
3590{
3591 const char *host = qemu_opt_get(opts, "host");
3592 const char *port = qemu_opt_get(opts, "port");
3593 const char *localaddr = qemu_opt_get(opts, "localaddr");
3594 const char *localport = qemu_opt_get(opts, "localport");
3595 bool has_local = false;
3596 SocketAddress *addr;
3597
3598 if (host == NULL || strlen(host) == 0) {
3599 host = "localhost";
3600 }
3601 if (port == NULL || strlen(port) == 0) {
3602 error_setg(errp, "chardev: udp: remote port not specified");
3603 return;
3604 }
3605 if (localport == NULL || strlen(localport) == 0) {
3606 localport = "0";
3607 } else {
3608 has_local = true;
3609 }
3610 if (localaddr == NULL || strlen(localaddr) == 0) {
3611 localaddr = "";
3612 } else {
3613 has_local = true;
3614 }
3615
3616 backend->udp = g_new0(ChardevUdp, 1);
3617
3618 addr = g_new0(SocketAddress, 1);
3619 addr->kind = SOCKET_ADDRESS_KIND_INET;
3620 addr->inet = g_new0(InetSocketAddress, 1);
3621 addr->inet->host = g_strdup(host);
3622 addr->inet->port = g_strdup(port);
3623 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3624 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3625 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3626 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3627 backend->udp->remote = addr;
3628
3629 if (has_local) {
3630 backend->udp->has_local = true;
3631 addr = g_new0(SocketAddress, 1);
3632 addr->kind = SOCKET_ADDRESS_KIND_INET;
3633 addr->inet = g_new0(InetSocketAddress, 1);
3634 addr->inet->host = g_strdup(localaddr);
3635 addr->inet->port = g_strdup(localport);
3636 backend->udp->local = addr;
3637 }
3638}
3639
Anthony Liguorid654f342013-03-05 23:21:28 +05303640typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003641 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003642 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003643 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303644} CharDriver;
3645
3646static GSList *backends;
3647
Peter Maydelle4d50d42014-09-02 11:24:17 +01003648void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003649 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3650{
3651 CharDriver *s;
3652
3653 s = g_malloc0(sizeof(*s));
3654 s->name = g_strdup(name);
3655 s->kind = kind;
3656 s->parse = parse;
3657
3658 backends = g_slist_append(backends, s);
3659}
3660
Anthony Liguorif69554b2011-08-15 11:17:37 -05003661CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003662 void (*init)(struct CharDriverState *s),
3663 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003664{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003665 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303666 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003667 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303668 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003669 ChardevReturn *ret = NULL;
3670 ChardevBackend *backend;
3671 const char *id = qemu_opts_id(opts);
3672 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003673
Peter Maydella61ae7f2014-09-02 11:24:16 +01003674 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003675 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003676 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003677 }
3678
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003679 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003680 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003681 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003682 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003683 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303684 for (i = backends; i; i = i->next) {
3685 cd = i->data;
3686
3687 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003688 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303689 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003690 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303691 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003692 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003693 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003694 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003695 }
3696
Peter Maydella61ae7f2014-09-02 11:24:16 +01003697 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003698
3699 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003700 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003701 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003702
3703 chr = NULL;
3704 backend->kind = cd->kind;
3705 if (cd->parse) {
3706 cd->parse(opts, backend, &local_err);
3707 if (local_err) {
3708 error_propagate(errp, local_err);
3709 goto qapi_out;
3710 }
3711 }
3712 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3713 if (!ret) {
3714 goto qapi_out;
3715 }
3716
3717 if (bid) {
3718 qapi_free_ChardevBackend(backend);
3719 qapi_free_ChardevReturn(ret);
3720 backend = g_new0(ChardevBackend, 1);
3721 backend->mux = g_new0(ChardevMux, 1);
3722 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3723 backend->mux->chardev = g_strdup(bid);
3724 ret = qmp_chardev_add(id, backend, errp);
3725 if (!ret) {
3726 chr = qemu_chr_find(bid);
3727 qemu_chr_delete(chr);
3728 chr = NULL;
3729 goto qapi_out;
3730 }
3731 }
3732
3733 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003734 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003735
3736qapi_out:
3737 qapi_free_ChardevBackend(backend);
3738 qapi_free_ChardevReturn(ret);
3739 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003740 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003741
3742err:
3743 qemu_opts_del(opts);
3744 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003745}
3746
Anthony Liguori27143a42011-08-15 11:17:36 -05003747CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003748{
3749 const char *p;
3750 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003751 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003752 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003753
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003754 if (strstart(filename, "chardev:", &p)) {
3755 return qemu_chr_find(p);
3756 }
3757
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003758 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003759 if (!opts)
3760 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003761
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003762 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003763 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003764 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003765 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003766 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003767 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003768 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003769 }
3770 return chr;
3771}
3772
Anthony Liguori15f31512011-08-15 11:17:35 -05003773void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003774{
3775 if (chr->chr_set_echo) {
3776 chr->chr_set_echo(chr, echo);
3777 }
3778}
3779
Hans de Goede8e25daa2013-03-26 11:07:57 +01003780void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003781{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003782 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003783 return;
3784 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003785 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003786 if (chr->chr_set_fe_open) {
3787 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003788 }
3789}
3790
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003791void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3792{
3793 if (chr->chr_fe_event) {
3794 chr->chr_fe_event(chr, event);
3795 }
3796}
3797
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003798int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3799 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303800{
3801 GSource *src;
3802 guint tag;
3803
3804 if (s->chr_add_watch == NULL) {
3805 return -ENOSYS;
3806 }
3807
3808 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003809 if (!src) {
3810 return -EINVAL;
3811 }
3812
Anthony Liguori23673ca2013-03-05 23:21:23 +05303813 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3814 tag = g_source_attach(src, NULL);
3815 g_source_unref(src);
3816
3817 return tag;
3818}
3819
Hans de Goede44c473d2013-03-27 20:29:39 +01003820int qemu_chr_fe_claim(CharDriverState *s)
3821{
3822 if (s->avail_connections < 1) {
3823 return -1;
3824 }
3825 s->avail_connections--;
3826 return 0;
3827}
3828
3829void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3830{
3831 if (qemu_chr_fe_claim(s) != 0) {
3832 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3833 __func__, s->label);
3834 exit(1);
3835 }
3836}
3837
3838void qemu_chr_fe_release(CharDriverState *s)
3839{
3840 s->avail_connections++;
3841}
3842
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003843void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003844{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003845 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003846 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003847 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003848 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003849 g_free(chr->filename);
3850 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003851 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003852 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003853}
3854
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003855ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003856{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003857 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003858 CharDriverState *chr;
3859
Blue Swirl72cf2d42009-09-12 07:36:22 +00003860 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003861 ChardevInfoList *info = g_malloc0(sizeof(*info));
3862 info->value = g_malloc0(sizeof(*info->value));
3863 info->value->label = g_strdup(chr->label);
3864 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003865 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003866
3867 info->next = chr_list;
3868 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003869 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003870
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003871 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003872}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003873
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003874ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3875{
3876 ChardevBackendInfoList *backend_list = NULL;
3877 CharDriver *c = NULL;
3878 GSList *i = NULL;
3879
3880 for (i = backends; i; i = i->next) {
3881 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3882 c = i->data;
3883 info->value = g_malloc0(sizeof(*info->value));
3884 info->value->name = g_strdup(c->name);
3885
3886 info->next = backend_list;
3887 backend_list = info;
3888 }
3889
3890 return backend_list;
3891}
3892
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003893CharDriverState *qemu_chr_find(const char *name)
3894{
3895 CharDriverState *chr;
3896
Blue Swirl72cf2d42009-09-12 07:36:22 +00003897 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003898 if (strcmp(chr->label, name) != 0)
3899 continue;
3900 return chr;
3901 }
3902 return NULL;
3903}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003904
3905/* Get a character (serial) device interface. */
3906CharDriverState *qemu_char_get_next_serial(void)
3907{
3908 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003909 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003910
3911 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003912
3913 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3914 chr = serial_hds[next_serial++];
3915 qemu_chr_fe_claim_no_fail(chr);
3916 return chr;
3917 }
3918 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003919}
3920
Paolo Bonzini4d454572012-11-26 16:03:42 +01003921QemuOptsList qemu_chardev_opts = {
3922 .name = "chardev",
3923 .implied_opt_name = "backend",
3924 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3925 .desc = {
3926 {
3927 .name = "backend",
3928 .type = QEMU_OPT_STRING,
3929 },{
3930 .name = "path",
3931 .type = QEMU_OPT_STRING,
3932 },{
3933 .name = "host",
3934 .type = QEMU_OPT_STRING,
3935 },{
3936 .name = "port",
3937 .type = QEMU_OPT_STRING,
3938 },{
3939 .name = "localaddr",
3940 .type = QEMU_OPT_STRING,
3941 },{
3942 .name = "localport",
3943 .type = QEMU_OPT_STRING,
3944 },{
3945 .name = "to",
3946 .type = QEMU_OPT_NUMBER,
3947 },{
3948 .name = "ipv4",
3949 .type = QEMU_OPT_BOOL,
3950 },{
3951 .name = "ipv6",
3952 .type = QEMU_OPT_BOOL,
3953 },{
3954 .name = "wait",
3955 .type = QEMU_OPT_BOOL,
3956 },{
3957 .name = "server",
3958 .type = QEMU_OPT_BOOL,
3959 },{
3960 .name = "delay",
3961 .type = QEMU_OPT_BOOL,
3962 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003963 .name = "reconnect",
3964 .type = QEMU_OPT_NUMBER,
3965 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003966 .name = "telnet",
3967 .type = QEMU_OPT_BOOL,
3968 },{
3969 .name = "width",
3970 .type = QEMU_OPT_NUMBER,
3971 },{
3972 .name = "height",
3973 .type = QEMU_OPT_NUMBER,
3974 },{
3975 .name = "cols",
3976 .type = QEMU_OPT_NUMBER,
3977 },{
3978 .name = "rows",
3979 .type = QEMU_OPT_NUMBER,
3980 },{
3981 .name = "mux",
3982 .type = QEMU_OPT_BOOL,
3983 },{
3984 .name = "signal",
3985 .type = QEMU_OPT_BOOL,
3986 },{
3987 .name = "name",
3988 .type = QEMU_OPT_STRING,
3989 },{
3990 .name = "debug",
3991 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003992 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003993 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003994 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003995 },{
3996 .name = "chardev",
3997 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003998 },
3999 { /* end of list */ }
4000 },
4001};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004002
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004003#ifdef _WIN32
4004
4005static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4006{
4007 HANDLE out;
4008
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004009 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004010 error_setg(errp, "input file not supported");
4011 return NULL;
4012 }
4013
4014 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4015 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4016 if (out == INVALID_HANDLE_VALUE) {
4017 error_setg(errp, "open %s failed", file->out);
4018 return NULL;
4019 }
4020 return qemu_chr_open_win_file(out);
4021}
4022
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004023static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4024 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004025{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004026 return qemu_chr_open_win_path(serial->device);
4027}
4028
4029static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4030 Error **errp)
4031{
4032 error_setg(errp, "character device backend type 'parallel' not supported");
4033 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004034}
4035
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004036#else /* WIN32 */
4037
4038static int qmp_chardev_open_file_source(char *src, int flags,
4039 Error **errp)
4040{
4041 int fd = -1;
4042
4043 TFR(fd = qemu_open(src, flags, 0666));
4044 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004045 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004046 }
4047 return fd;
4048}
4049
4050static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4051{
Markus Armbruster5f758362014-05-19 18:57:34 +02004052 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004053
4054 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4055 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004056 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004057 return NULL;
4058 }
4059
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004060 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004061 flags = O_RDONLY;
4062 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004063 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004064 qemu_close(out);
4065 return NULL;
4066 }
4067 }
4068
4069 return qemu_chr_open_fd(in, out);
4070}
4071
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004072static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4073 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004074{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004075#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004076 int fd;
4077
4078 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004079 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004080 return NULL;
4081 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004082 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004083 return qemu_chr_open_tty_fd(fd);
4084#else
4085 error_setg(errp, "character device backend type 'serial' not supported");
4086 return NULL;
4087#endif
4088}
4089
4090static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4091 Error **errp)
4092{
4093#ifdef HAVE_CHARDEV_PARPORT
4094 int fd;
4095
4096 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004097 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004098 return NULL;
4099 }
4100 return qemu_chr_open_pp_fd(fd);
4101#else
4102 error_setg(errp, "character device backend type 'parallel' not supported");
4103 return NULL;
4104#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004105}
4106
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004107#endif /* WIN32 */
4108
Corey Minyard5008e5b2014-10-08 07:11:55 -05004109static void socket_try_connect(CharDriverState *chr)
4110{
4111 Error *err = NULL;
4112
4113 if (!qemu_chr_open_socket_fd(chr, &err)) {
4114 check_report_connect_error(chr, err);
4115 }
4116}
4117
Corey Minyard5dd1f022014-10-02 11:17:37 -05004118static gboolean socket_reconnect_timeout(gpointer opaque)
4119{
4120 CharDriverState *chr = opaque;
4121 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004122
4123 s->reconnect_timer = 0;
4124
4125 if (chr->be_open) {
4126 return false;
4127 }
4128
Corey Minyard5008e5b2014-10-08 07:11:55 -05004129 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004130
4131 return false;
4132}
4133
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004134static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
4135 Error **errp)
4136{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004137 CharDriverState *chr;
4138 TCPCharDriver *s;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004139 SocketAddress *addr = sock->addr;
4140 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4141 bool is_listen = sock->has_server ? sock->server : true;
4142 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4143 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004144 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004145
4146 chr = qemu_chr_alloc();
4147 s = g_malloc0(sizeof(TCPCharDriver));
4148
4149 s->fd = -1;
4150 s->listen_fd = -1;
4151 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004152 s->is_listen = is_listen;
4153 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004154 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004155 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004156
4157 chr->opaque = s;
4158 chr->chr_write = tcp_chr_write;
4159 chr->chr_sync_read = tcp_chr_sync_read;
4160 chr->chr_close = tcp_chr_close;
4161 chr->get_msgfds = tcp_get_msgfds;
4162 chr->set_msgfds = tcp_set_msgfds;
4163 chr->chr_add_client = tcp_chr_add_client;
4164 chr->chr_add_watch = tcp_chr_add_watch;
4165 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4166 /* be isn't opened until we get a connection */
4167 chr->explicit_be_open = true;
4168
4169 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004170 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4171 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004172
4173 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004174 if (is_telnet) {
4175 s->do_telnetopt = 1;
4176 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004177 } else if (reconnect > 0) {
4178 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004179 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004180
Corey Minyard5008e5b2014-10-08 07:11:55 -05004181 if (s->reconnect_time) {
4182 socket_try_connect(chr);
4183 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4184 g_free(s);
4185 g_free(chr->filename);
4186 g_free(chr);
4187 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004188 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004189
4190 if (is_listen && is_waitconnect) {
4191 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4192 chr->filename);
4193 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4194 qemu_set_nonblock(s->listen_fd);
4195 }
4196
4197 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004198}
4199
Lei Li08d0ab32013-05-20 14:51:03 +08004200static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4201 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004202{
4203 int fd;
4204
Lei Li08d0ab32013-05-20 14:51:03 +08004205 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004206 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004207 return NULL;
4208 }
4209 return qemu_chr_open_udp_fd(fd);
4210}
4211
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004212ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4213 Error **errp)
4214{
4215 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004216 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004217
4218 chr = qemu_chr_find(id);
4219 if (chr) {
4220 error_setg(errp, "Chardev '%s' already exists", id);
4221 g_free(ret);
4222 return NULL;
4223 }
4224
4225 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004226 case CHARDEV_BACKEND_KIND_FILE:
4227 chr = qmp_chardev_open_file(backend->file, errp);
4228 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004229 case CHARDEV_BACKEND_KIND_SERIAL:
4230 chr = qmp_chardev_open_serial(backend->serial, errp);
4231 break;
4232 case CHARDEV_BACKEND_KIND_PARALLEL:
4233 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004234 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004235 case CHARDEV_BACKEND_KIND_PIPE:
4236 chr = qemu_chr_open_pipe(backend->pipe);
4237 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004238 case CHARDEV_BACKEND_KIND_SOCKET:
4239 chr = qmp_chardev_open_socket(backend->socket, errp);
4240 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004241 case CHARDEV_BACKEND_KIND_UDP:
4242 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004243 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004244#ifdef HAVE_CHARDEV_TTY
4245 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004246 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004247 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004248#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004249 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004250 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004251 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004252 case CHARDEV_BACKEND_KIND_MUX:
4253 base = qemu_chr_find(backend->mux->chardev);
4254 if (base == NULL) {
4255 error_setg(errp, "mux: base chardev %s not found",
4256 backend->mux->chardev);
4257 break;
4258 }
4259 chr = qemu_chr_open_mux(base);
4260 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004261 case CHARDEV_BACKEND_KIND_MSMOUSE:
4262 chr = qemu_chr_open_msmouse();
4263 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004264#ifdef CONFIG_BRLAPI
4265 case CHARDEV_BACKEND_KIND_BRAILLE:
4266 chr = chr_baum_init();
4267 break;
4268#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004269 case CHARDEV_BACKEND_KIND_TESTDEV:
4270 chr = chr_testdev_init();
4271 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004272 case CHARDEV_BACKEND_KIND_STDIO:
4273 chr = qemu_chr_open_stdio(backend->stdio);
4274 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004275#ifdef _WIN32
4276 case CHARDEV_BACKEND_KIND_CONSOLE:
4277 chr = qemu_chr_open_win_con();
4278 break;
4279#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004280#ifdef CONFIG_SPICE
4281 case CHARDEV_BACKEND_KIND_SPICEVMC:
4282 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4283 break;
4284 case CHARDEV_BACKEND_KIND_SPICEPORT:
4285 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4286 break;
4287#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004288 case CHARDEV_BACKEND_KIND_VC:
4289 chr = vc_init(backend->vc);
4290 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004291 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004292 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004293 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004294 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004295 default:
4296 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4297 break;
4298 }
4299
Markus Armbruster3894c782014-05-19 18:57:36 +02004300 /*
4301 * Character backend open hasn't been fully converted to the Error
4302 * API. Some opens fail without setting an error. Set a generic
4303 * error then.
4304 * TODO full conversion to Error API
4305 */
4306 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004307 error_setg(errp, "Failed to create chardev");
4308 }
4309 if (chr) {
4310 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004311 chr->avail_connections =
4312 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004313 if (!chr->filename) {
4314 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4315 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004316 if (!chr->explicit_be_open) {
4317 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4318 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004319 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4320 return ret;
4321 } else {
4322 g_free(ret);
4323 return NULL;
4324 }
4325}
4326
4327void qmp_chardev_remove(const char *id, Error **errp)
4328{
4329 CharDriverState *chr;
4330
4331 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004332 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004333 error_setg(errp, "Chardev '%s' not found", id);
4334 return;
4335 }
4336 if (chr->chr_can_read || chr->chr_read ||
4337 chr->chr_event || chr->handler_opaque) {
4338 error_setg(errp, "Chardev '%s' is busy", id);
4339 return;
4340 }
4341 qemu_chr_delete(chr);
4342}
Anthony Liguorid654f342013-03-05 23:21:28 +05304343
4344static void register_types(void)
4345{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004346 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4347 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4348 qemu_chr_parse_socket);
4349 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4350 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4351 qemu_chr_parse_ringbuf);
4352 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4353 qemu_chr_parse_file_out);
4354 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4355 qemu_chr_parse_stdio);
4356 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4357 qemu_chr_parse_serial);
4358 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4359 qemu_chr_parse_serial);
4360 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4361 qemu_chr_parse_parallel);
4362 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4363 qemu_chr_parse_parallel);
4364 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4365 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4366 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4367 qemu_chr_parse_pipe);
4368 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004369 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004370 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4371 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004372 /* this must be done after machine init, since we register FEs with muxes
4373 * as part of realize functions like serial_isa_realizefn when -nographic
4374 * is specified
4375 */
4376 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304377}
4378
4379type_init(register_types);