blob: d0c1564306d268c4d488f5e53116e6f6ac115022 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000029#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030030#include "qmp-commands.h"
Corey Minyardcfb429c2014-10-02 11:17:35 -050031#include "qapi/qmp-input-visitor.h"
32#include "qapi/qmp-output-visitor.h"
33#include "qapi-visit.h"
aliguori6f97dba2008-10-31 18:49:55 +000034
35#include <unistd.h>
36#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000037#include <time.h>
38#include <errno.h>
39#include <sys/time.h>
40#include <zlib.h>
41
42#ifndef _WIN32
43#include <sys/times.h>
44#include <sys/wait.h>
45#include <termios.h>
46#include <sys/mman.h>
47#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000049#include <sys/socket.h>
50#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000051#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000053#include <dirent.h>
54#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) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200809 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
810 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200811 g_source_attach(iwp->src, NULL);
812 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200813 g_source_destroy(iwp->src);
814 g_source_unref(iwp->src);
815 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200816 }
817 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530818}
819
820static gboolean io_watch_poll_check(GSource *source)
821{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200822 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530823}
824
825static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
826 gpointer user_data)
827{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200828 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530829}
830
831static void io_watch_poll_finalize(GSource *source)
832{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200833 /* Due to a glib bug, removing the last reference to a source
834 * inside a finalize callback causes recursive locking (and a
835 * deadlock). This is not a problem inside other callbacks,
836 * including dispatch callbacks, so we call io_remove_watch_poll
837 * to remove this source. At this point, iwp->src must
838 * be NULL, or we would leak it.
839 *
840 * This would be solved much more elegantly by child sources,
841 * but we support older glib versions that do not have them.
842 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530843 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200844 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530845}
846
847static GSourceFuncs io_watch_poll_funcs = {
848 .prepare = io_watch_poll_prepare,
849 .check = io_watch_poll_check,
850 .dispatch = io_watch_poll_dispatch,
851 .finalize = io_watch_poll_finalize,
852};
853
854/* Can only be used for read */
855static guint io_add_watch_poll(GIOChannel *channel,
856 IOCanReadHandler *fd_can_read,
857 GIOFunc fd_read,
858 gpointer user_data)
859{
860 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200861 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530862
Paolo Bonzinid185c092013-04-05 17:59:33 +0200863 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530864 iwp->fd_can_read = fd_can_read;
865 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200866 iwp->channel = channel;
867 iwp->fd_read = (GSourceFunc) fd_read;
868 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530869
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200870 tag = g_source_attach(&iwp->parent, NULL);
871 g_source_unref(&iwp->parent);
872 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530873}
874
Paolo Bonzini2b316772013-04-19 17:32:09 +0200875static void io_remove_watch_poll(guint tag)
876{
877 GSource *source;
878 IOWatchPoll *iwp;
879
880 g_return_if_fail (tag > 0);
881
882 source = g_main_context_find_source_by_id(NULL, tag);
883 g_return_if_fail (source != NULL);
884
885 iwp = io_watch_poll_from_source(source);
886 if (iwp->src) {
887 g_source_destroy(iwp->src);
888 g_source_unref(iwp->src);
889 iwp->src = NULL;
890 }
891 g_source_destroy(&iwp->parent);
892}
893
Amit Shah26da70c2013-08-28 15:23:37 +0530894static void remove_fd_in_watch(CharDriverState *chr)
895{
896 if (chr->fd_in_tag) {
897 io_remove_watch_poll(chr->fd_in_tag);
898 chr->fd_in_tag = 0;
899 }
900}
901
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000902#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530903static GIOChannel *io_channel_from_fd(int fd)
904{
905 GIOChannel *chan;
906
907 if (fd == -1) {
908 return NULL;
909 }
910
911 chan = g_io_channel_unix_new(fd);
912
913 g_io_channel_set_encoding(chan, NULL, NULL);
914 g_io_channel_set_buffered(chan, FALSE);
915
916 return chan;
917}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000918#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530919
Anthony Liguori76a96442013-03-05 23:21:21 +0530920static GIOChannel *io_channel_from_socket(int fd)
921{
922 GIOChannel *chan;
923
924 if (fd == -1) {
925 return NULL;
926 }
927
928#ifdef _WIN32
929 chan = g_io_channel_win32_new_socket(fd);
930#else
931 chan = g_io_channel_unix_new(fd);
932#endif
933
934 g_io_channel_set_encoding(chan, NULL, NULL);
935 g_io_channel_set_buffered(chan, FALSE);
936
937 return chan;
938}
939
Anthony Liguori684a0962013-03-29 11:39:50 -0500940static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530941{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200942 size_t offset = 0;
943 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530944
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200945 while (offset < len && status == G_IO_STATUS_NORMAL) {
946 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500947
948 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530949 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500950 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530951 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500952
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200953 if (offset > 0) {
954 return offset;
955 }
956 switch (status) {
957 case G_IO_STATUS_NORMAL:
958 g_assert(len == 0);
959 return 0;
960 case G_IO_STATUS_AGAIN:
961 errno = EAGAIN;
962 return -1;
963 default:
964 break;
965 }
966 errno = EINVAL;
967 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530968}
Anthony Liguori96c63842013-03-05 23:21:18 +0530969
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000970#ifndef _WIN32
971
Anthony Liguoria29753f2013-03-05 23:21:19 +0530972typedef struct FDCharDriver {
973 CharDriverState *chr;
974 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000975 int max_size;
976} FDCharDriver;
977
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200978/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000979static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
980{
981 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530982
Anthony Liguori684a0962013-03-29 11:39:50 -0500983 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530984}
985
986static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
987{
988 CharDriverState *chr = opaque;
989 FDCharDriver *s = chr->opaque;
990 int len;
991 uint8_t buf[READ_BUF_LEN];
992 GIOStatus status;
993 gsize bytes_read;
994
995 len = sizeof(buf);
996 if (len > s->max_size) {
997 len = s->max_size;
998 }
999 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001000 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301001 }
1002
1003 status = g_io_channel_read_chars(chan, (gchar *)buf,
1004 len, &bytes_read, NULL);
1005 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301006 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301007 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1008 return FALSE;
1009 }
1010 if (status == G_IO_STATUS_NORMAL) {
1011 qemu_chr_be_write(chr, buf, bytes_read);
1012 }
1013
1014 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001015}
1016
1017static int fd_chr_read_poll(void *opaque)
1018{
1019 CharDriverState *chr = opaque;
1020 FDCharDriver *s = chr->opaque;
1021
Anthony Liguori909cda12011-08-15 11:17:31 -05001022 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001023 return s->max_size;
1024}
1025
Anthony Liguori23673ca2013-03-05 23:21:23 +05301026static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1027{
1028 FDCharDriver *s = chr->opaque;
1029 return g_io_create_watch(s->fd_out, cond);
1030}
1031
aliguori6f97dba2008-10-31 18:49:55 +00001032static void fd_chr_update_read_handler(CharDriverState *chr)
1033{
1034 FDCharDriver *s = chr->opaque;
1035
Amit Shah26da70c2013-08-28 15:23:37 +05301036 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301037 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301038 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1039 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001040 }
1041}
1042
1043static void fd_chr_close(struct CharDriverState *chr)
1044{
1045 FDCharDriver *s = chr->opaque;
1046
Amit Shah26da70c2013-08-28 15:23:37 +05301047 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301048 if (s->fd_in) {
1049 g_io_channel_unref(s->fd_in);
1050 }
1051 if (s->fd_out) {
1052 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001053 }
1054
Anthony Liguori7267c092011-08-20 22:09:37 -05001055 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001056 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001057}
1058
1059/* open a character device to a unix fd */
1060static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1061{
1062 CharDriverState *chr;
1063 FDCharDriver *s;
1064
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001065 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001066 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +05301067 s->fd_in = io_channel_from_fd(fd_in);
1068 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001069 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301070 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001071 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301072 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001073 chr->chr_write = fd_chr_write;
1074 chr->chr_update_read_handler = fd_chr_update_read_handler;
1075 chr->chr_close = fd_chr_close;
1076
aliguori6f97dba2008-10-31 18:49:55 +00001077 return chr;
1078}
1079
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001080static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001081{
1082 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001083 char filename_in[CHR_MAX_FILENAME_SIZE];
1084 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001085 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001086
1087 if (filename == NULL) {
1088 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +01001089 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001090 }
aliguori6f97dba2008-10-31 18:49:55 +00001091
Corey Minyard9f781162014-10-02 11:17:33 -05001092 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1093 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001094 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1095 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001096 if (fd_in < 0 || fd_out < 0) {
1097 if (fd_in >= 0)
1098 close(fd_in);
1099 if (fd_out >= 0)
1100 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001101 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001102 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001103 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001104 }
aliguori6f97dba2008-10-31 18:49:55 +00001105 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001106 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001107}
1108
aliguori6f97dba2008-10-31 18:49:55 +00001109/* init terminal so that we can grab keys */
1110static struct termios oldtty;
1111static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001112static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001113static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001114static bool stdio_echo_state;
1115
1116static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001117
1118static void term_exit(void)
1119{
1120 tcsetattr (0, TCSANOW, &oldtty);
1121 fcntl(0, F_SETFL, old_fd0_flags);
1122}
1123
Gal Hammere76d4422015-01-07 10:38:35 +02001124static void term_stdio_handler(int sig)
1125{
1126 /* restore echo after resume from suspend. */
1127 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1128}
1129
Paolo Bonzinibb002512010-12-23 13:42:50 +01001130static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001131{
1132 struct termios tty;
1133
Gal Hammere76d4422015-01-07 10:38:35 +02001134 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001135 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001136 if (!echo) {
1137 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001138 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001139 tty.c_oflag |= OPOST;
1140 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1141 tty.c_cflag &= ~(CSIZE|PARENB);
1142 tty.c_cflag |= CS8;
1143 tty.c_cc[VMIN] = 1;
1144 tty.c_cc[VTIME] = 0;
1145 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001146 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001147 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001148
1149 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001150}
1151
1152static void qemu_chr_close_stdio(struct CharDriverState *chr)
1153{
1154 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001155 fd_chr_close(chr);
1156}
1157
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001158static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001159{
1160 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001161 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001162
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001163 if (is_daemonized()) {
1164 error_report("cannot use stdio with -daemonize");
1165 return NULL;
1166 }
Li Liuc88930a2014-09-09 19:19:48 +08001167
1168 if (stdio_in_use) {
1169 error_report("cannot use stdio by multiple character devices");
1170 exit(1);
1171 }
1172
1173 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301174 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001175 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001176 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301177 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001178
Gal Hammere76d4422015-01-07 10:38:35 +02001179 memset(&act, 0, sizeof(act));
1180 act.sa_handler = term_stdio_handler;
1181 sigaction(SIGCONT, &act, NULL);
1182
aliguori6f97dba2008-10-31 18:49:55 +00001183 chr = qemu_chr_open_fd(0, 1);
1184 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001185 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001186 if (opts->has_signal) {
1187 stdio_allow_signal = opts->signal;
1188 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001189 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001190
Markus Armbruster1f514702012-02-07 15:09:08 +01001191 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001192}
1193
aliguori6f97dba2008-10-31 18:49:55 +00001194#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001195 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1196 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001197
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001198#define HAVE_CHARDEV_TTY 1
1199
aliguori6f97dba2008-10-31 18:49:55 +00001200typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301201 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001202 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001203
1204 /* Protected by the CharDriverState chr_write_lock. */
1205 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301206 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001207 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001208} PtyCharDriver;
1209
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001210static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001211static void pty_chr_state(CharDriverState *chr, int connected);
1212
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301213static gboolean pty_chr_timer(gpointer opaque)
1214{
1215 struct CharDriverState *chr = opaque;
1216 PtyCharDriver *s = chr->opaque;
1217
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001218 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001219 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001220 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001221 if (!s->connected) {
1222 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001223 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001224 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001225 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301226 return FALSE;
1227}
1228
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001229/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301230static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1231{
1232 PtyCharDriver *s = chr->opaque;
1233
1234 if (s->timer_tag) {
1235 g_source_remove(s->timer_tag);
1236 s->timer_tag = 0;
1237 }
1238
1239 if (ms == 1000) {
1240 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1241 } else {
1242 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1243 }
1244}
1245
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001246/* Called with chr_write_lock held. */
1247static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001248{
1249 PtyCharDriver *s = chr->opaque;
1250 GPollFD pfd;
1251
1252 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1253 pfd.events = G_IO_OUT;
1254 pfd.revents = 0;
1255 g_poll(&pfd, 1, 0);
1256 if (pfd.revents & G_IO_HUP) {
1257 pty_chr_state(chr, 0);
1258 } else {
1259 pty_chr_state(chr, 1);
1260 }
1261}
1262
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001263static void pty_chr_update_read_handler(CharDriverState *chr)
1264{
1265 qemu_mutex_lock(&chr->chr_write_lock);
1266 pty_chr_update_read_handler_locked(chr);
1267 qemu_mutex_unlock(&chr->chr_write_lock);
1268}
1269
1270/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001271static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1272{
1273 PtyCharDriver *s = chr->opaque;
1274
1275 if (!s->connected) {
1276 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001277 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001278 if (!s->connected) {
1279 return 0;
1280 }
aliguori6f97dba2008-10-31 18:49:55 +00001281 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001282 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001283}
1284
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301285static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1286{
1287 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001288 if (!s->connected) {
1289 return NULL;
1290 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301291 return g_io_create_watch(s->fd, cond);
1292}
1293
aliguori6f97dba2008-10-31 18:49:55 +00001294static int pty_chr_read_poll(void *opaque)
1295{
1296 CharDriverState *chr = opaque;
1297 PtyCharDriver *s = chr->opaque;
1298
Anthony Liguori909cda12011-08-15 11:17:31 -05001299 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001300 return s->read_bytes;
1301}
1302
Anthony Liguori093d3a22013-03-05 23:21:20 +05301303static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001304{
1305 CharDriverState *chr = opaque;
1306 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301307 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301308 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301309 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001310
1311 len = sizeof(buf);
1312 if (len > s->read_bytes)
1313 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001314 if (len == 0) {
1315 return TRUE;
1316 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301317 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1318 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001319 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301320 return FALSE;
1321 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001322 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001323 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001324 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301325 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001326}
1327
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001328static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1329{
1330 CharDriverState *chr = opaque;
1331 PtyCharDriver *s = chr->opaque;
1332
1333 s->open_tag = 0;
1334 qemu_chr_be_generic_open(chr);
1335 return FALSE;
1336}
1337
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001338/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001339static void pty_chr_state(CharDriverState *chr, int connected)
1340{
1341 PtyCharDriver *s = chr->opaque;
1342
1343 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001344 if (s->open_tag) {
1345 g_source_remove(s->open_tag);
1346 s->open_tag = 0;
1347 }
Amit Shah26da70c2013-08-28 15:23:37 +05301348 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001349 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001350 /* (re-)connect poll interval for idle guests: once per second.
1351 * We check more frequently in case the guests sends data to
1352 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301353 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001354 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001355 if (s->timer_tag) {
1356 g_source_remove(s->timer_tag);
1357 s->timer_tag = 0;
1358 }
1359 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001360 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001361 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001362 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001363 }
1364 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301365 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1366 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001367 }
aliguori6f97dba2008-10-31 18:49:55 +00001368 }
1369}
1370
aliguori6f97dba2008-10-31 18:49:55 +00001371static void pty_chr_close(struct CharDriverState *chr)
1372{
1373 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301374 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001375
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001376 qemu_mutex_lock(&chr->chr_write_lock);
1377 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301378 fd = g_io_channel_unix_get_fd(s->fd);
1379 g_io_channel_unref(s->fd);
1380 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301381 if (s->timer_tag) {
1382 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001383 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301384 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001385 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001386 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001387 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001388}
1389
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001390static CharDriverState *qemu_chr_open_pty(const char *id,
1391 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001392{
1393 CharDriverState *chr;
1394 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001395 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001396 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001397
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001398 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1399 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001400 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001401 }
1402
aliguori6f97dba2008-10-31 18:49:55 +00001403 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001404 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001405
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001406 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001407
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001408 chr->filename = g_strdup_printf("pty:%s", pty_name);
1409 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001410 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001411
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001412 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001413 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001414
1415 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001416 chr->opaque = s;
1417 chr->chr_write = pty_chr_write;
1418 chr->chr_update_read_handler = pty_chr_update_read_handler;
1419 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301420 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001421 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001422
Anthony Liguori093d3a22013-03-05 23:21:20 +05301423 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301424 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001425
Markus Armbruster1f514702012-02-07 15:09:08 +01001426 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001427}
1428
1429static void tty_serial_init(int fd, int speed,
1430 int parity, int data_bits, int stop_bits)
1431{
1432 struct termios tty;
1433 speed_t spd;
1434
1435#if 0
1436 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1437 speed, parity, data_bits, stop_bits);
1438#endif
1439 tcgetattr (fd, &tty);
1440
Stefan Weil45eea132009-10-26 16:10:10 +01001441#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1442 speed = speed * 10 / 11;
1443 do {
1444 check_speed(50);
1445 check_speed(75);
1446 check_speed(110);
1447 check_speed(134);
1448 check_speed(150);
1449 check_speed(200);
1450 check_speed(300);
1451 check_speed(600);
1452 check_speed(1200);
1453 check_speed(1800);
1454 check_speed(2400);
1455 check_speed(4800);
1456 check_speed(9600);
1457 check_speed(19200);
1458 check_speed(38400);
1459 /* Non-Posix values follow. They may be unsupported on some systems. */
1460 check_speed(57600);
1461 check_speed(115200);
1462#ifdef B230400
1463 check_speed(230400);
1464#endif
1465#ifdef B460800
1466 check_speed(460800);
1467#endif
1468#ifdef B500000
1469 check_speed(500000);
1470#endif
1471#ifdef B576000
1472 check_speed(576000);
1473#endif
1474#ifdef B921600
1475 check_speed(921600);
1476#endif
1477#ifdef B1000000
1478 check_speed(1000000);
1479#endif
1480#ifdef B1152000
1481 check_speed(1152000);
1482#endif
1483#ifdef B1500000
1484 check_speed(1500000);
1485#endif
1486#ifdef B2000000
1487 check_speed(2000000);
1488#endif
1489#ifdef B2500000
1490 check_speed(2500000);
1491#endif
1492#ifdef B3000000
1493 check_speed(3000000);
1494#endif
1495#ifdef B3500000
1496 check_speed(3500000);
1497#endif
1498#ifdef B4000000
1499 check_speed(4000000);
1500#endif
aliguori6f97dba2008-10-31 18:49:55 +00001501 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001502 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001503
1504 cfsetispeed(&tty, spd);
1505 cfsetospeed(&tty, spd);
1506
1507 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1508 |INLCR|IGNCR|ICRNL|IXON);
1509 tty.c_oflag |= OPOST;
1510 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1511 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1512 switch(data_bits) {
1513 default:
1514 case 8:
1515 tty.c_cflag |= CS8;
1516 break;
1517 case 7:
1518 tty.c_cflag |= CS7;
1519 break;
1520 case 6:
1521 tty.c_cflag |= CS6;
1522 break;
1523 case 5:
1524 tty.c_cflag |= CS5;
1525 break;
1526 }
1527 switch(parity) {
1528 default:
1529 case 'N':
1530 break;
1531 case 'E':
1532 tty.c_cflag |= PARENB;
1533 break;
1534 case 'O':
1535 tty.c_cflag |= PARENB | PARODD;
1536 break;
1537 }
1538 if (stop_bits == 2)
1539 tty.c_cflag |= CSTOPB;
1540
1541 tcsetattr (fd, TCSANOW, &tty);
1542}
1543
1544static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1545{
1546 FDCharDriver *s = chr->opaque;
1547
1548 switch(cmd) {
1549 case CHR_IOCTL_SERIAL_SET_PARAMS:
1550 {
1551 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301552 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1553 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001554 ssp->data_bits, ssp->stop_bits);
1555 }
1556 break;
1557 case CHR_IOCTL_SERIAL_SET_BREAK:
1558 {
1559 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301560 if (enable) {
1561 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1562 }
aliguori6f97dba2008-10-31 18:49:55 +00001563 }
1564 break;
1565 case CHR_IOCTL_SERIAL_GET_TIOCM:
1566 {
1567 int sarg = 0;
1568 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301569 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001570 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001571 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001572 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001573 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001574 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001575 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001576 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001577 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001578 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001579 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001580 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001581 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001582 *targ |= CHR_TIOCM_RTS;
1583 }
1584 break;
1585 case CHR_IOCTL_SERIAL_SET_TIOCM:
1586 {
1587 int sarg = *(int *)arg;
1588 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301589 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001590 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1591 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1592 if (sarg & CHR_TIOCM_CTS)
1593 targ |= TIOCM_CTS;
1594 if (sarg & CHR_TIOCM_CAR)
1595 targ |= TIOCM_CAR;
1596 if (sarg & CHR_TIOCM_DSR)
1597 targ |= TIOCM_DSR;
1598 if (sarg & CHR_TIOCM_RI)
1599 targ |= TIOCM_RI;
1600 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001601 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001602 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001603 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301604 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001605 }
1606 break;
1607 default:
1608 return -ENOTSUP;
1609 }
1610 return 0;
1611}
1612
David Ahern4266a132010-02-10 18:27:17 -07001613static void qemu_chr_close_tty(CharDriverState *chr)
1614{
1615 FDCharDriver *s = chr->opaque;
1616 int fd = -1;
1617
1618 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301619 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001620 }
1621
1622 fd_chr_close(chr);
1623
1624 if (fd >= 0) {
1625 close(fd);
1626 }
1627}
1628
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001629static CharDriverState *qemu_chr_open_tty_fd(int fd)
1630{
1631 CharDriverState *chr;
1632
1633 tty_serial_init(fd, 115200, 'N', 8, 1);
1634 chr = qemu_chr_open_fd(fd, fd);
1635 chr->chr_ioctl = tty_serial_ioctl;
1636 chr->chr_close = qemu_chr_close_tty;
1637 return chr;
1638}
aliguori6f97dba2008-10-31 18:49:55 +00001639#endif /* __linux__ || __sun__ */
1640
1641#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001642
1643#define HAVE_CHARDEV_PARPORT 1
1644
aliguori6f97dba2008-10-31 18:49:55 +00001645typedef struct {
1646 int fd;
1647 int mode;
1648} ParallelCharDriver;
1649
1650static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1651{
1652 if (s->mode != mode) {
1653 int m = mode;
1654 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1655 return 0;
1656 s->mode = mode;
1657 }
1658 return 1;
1659}
1660
1661static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1662{
1663 ParallelCharDriver *drv = chr->opaque;
1664 int fd = drv->fd;
1665 uint8_t b;
1666
1667 switch(cmd) {
1668 case CHR_IOCTL_PP_READ_DATA:
1669 if (ioctl(fd, PPRDATA, &b) < 0)
1670 return -ENOTSUP;
1671 *(uint8_t *)arg = b;
1672 break;
1673 case CHR_IOCTL_PP_WRITE_DATA:
1674 b = *(uint8_t *)arg;
1675 if (ioctl(fd, PPWDATA, &b) < 0)
1676 return -ENOTSUP;
1677 break;
1678 case CHR_IOCTL_PP_READ_CONTROL:
1679 if (ioctl(fd, PPRCONTROL, &b) < 0)
1680 return -ENOTSUP;
1681 /* Linux gives only the lowest bits, and no way to know data
1682 direction! For better compatibility set the fixed upper
1683 bits. */
1684 *(uint8_t *)arg = b | 0xc0;
1685 break;
1686 case CHR_IOCTL_PP_WRITE_CONTROL:
1687 b = *(uint8_t *)arg;
1688 if (ioctl(fd, PPWCONTROL, &b) < 0)
1689 return -ENOTSUP;
1690 break;
1691 case CHR_IOCTL_PP_READ_STATUS:
1692 if (ioctl(fd, PPRSTATUS, &b) < 0)
1693 return -ENOTSUP;
1694 *(uint8_t *)arg = b;
1695 break;
1696 case CHR_IOCTL_PP_DATA_DIR:
1697 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1698 return -ENOTSUP;
1699 break;
1700 case CHR_IOCTL_PP_EPP_READ_ADDR:
1701 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1702 struct ParallelIOArg *parg = arg;
1703 int n = read(fd, parg->buffer, parg->count);
1704 if (n != parg->count) {
1705 return -EIO;
1706 }
1707 }
1708 break;
1709 case CHR_IOCTL_PP_EPP_READ:
1710 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1711 struct ParallelIOArg *parg = arg;
1712 int n = read(fd, parg->buffer, parg->count);
1713 if (n != parg->count) {
1714 return -EIO;
1715 }
1716 }
1717 break;
1718 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1719 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1720 struct ParallelIOArg *parg = arg;
1721 int n = write(fd, parg->buffer, parg->count);
1722 if (n != parg->count) {
1723 return -EIO;
1724 }
1725 }
1726 break;
1727 case CHR_IOCTL_PP_EPP_WRITE:
1728 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1729 struct ParallelIOArg *parg = arg;
1730 int n = write(fd, parg->buffer, parg->count);
1731 if (n != parg->count) {
1732 return -EIO;
1733 }
1734 }
1735 break;
1736 default:
1737 return -ENOTSUP;
1738 }
1739 return 0;
1740}
1741
1742static void pp_close(CharDriverState *chr)
1743{
1744 ParallelCharDriver *drv = chr->opaque;
1745 int fd = drv->fd;
1746
1747 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1748 ioctl(fd, PPRELEASE);
1749 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001750 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001751 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001752}
1753
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001754static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001755{
1756 CharDriverState *chr;
1757 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001758
1759 if (ioctl(fd, PPCLAIM) < 0) {
1760 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001761 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001762 }
1763
Anthony Liguori7267c092011-08-20 22:09:37 -05001764 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001765 drv->fd = fd;
1766 drv->mode = IEEE1284_MODE_COMPAT;
1767
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001768 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001769 chr->chr_write = null_chr_write;
1770 chr->chr_ioctl = pp_ioctl;
1771 chr->chr_close = pp_close;
1772 chr->opaque = drv;
1773
Markus Armbruster1f514702012-02-07 15:09:08 +01001774 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001775}
1776#endif /* __linux__ */
1777
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001778#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001779
1780#define HAVE_CHARDEV_PARPORT 1
1781
blueswir16972f932008-11-22 20:49:12 +00001782static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1783{
Stefan Weile0efb992011-02-23 19:09:16 +01001784 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001785 uint8_t b;
1786
1787 switch(cmd) {
1788 case CHR_IOCTL_PP_READ_DATA:
1789 if (ioctl(fd, PPIGDATA, &b) < 0)
1790 return -ENOTSUP;
1791 *(uint8_t *)arg = b;
1792 break;
1793 case CHR_IOCTL_PP_WRITE_DATA:
1794 b = *(uint8_t *)arg;
1795 if (ioctl(fd, PPISDATA, &b) < 0)
1796 return -ENOTSUP;
1797 break;
1798 case CHR_IOCTL_PP_READ_CONTROL:
1799 if (ioctl(fd, PPIGCTRL, &b) < 0)
1800 return -ENOTSUP;
1801 *(uint8_t *)arg = b;
1802 break;
1803 case CHR_IOCTL_PP_WRITE_CONTROL:
1804 b = *(uint8_t *)arg;
1805 if (ioctl(fd, PPISCTRL, &b) < 0)
1806 return -ENOTSUP;
1807 break;
1808 case CHR_IOCTL_PP_READ_STATUS:
1809 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1810 return -ENOTSUP;
1811 *(uint8_t *)arg = b;
1812 break;
1813 default:
1814 return -ENOTSUP;
1815 }
1816 return 0;
1817}
1818
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001819static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001820{
1821 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001822
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001823 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001824 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001825 chr->chr_write = null_chr_write;
1826 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001827 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001828 return chr;
blueswir16972f932008-11-22 20:49:12 +00001829}
1830#endif
1831
aliguori6f97dba2008-10-31 18:49:55 +00001832#else /* _WIN32 */
1833
1834typedef struct {
1835 int max_size;
1836 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001837 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001838 BOOL fpipe;
1839 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001840
1841 /* Protected by the CharDriverState chr_write_lock. */
1842 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001843} WinCharState;
1844
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001845typedef struct {
1846 HANDLE hStdIn;
1847 HANDLE hInputReadyEvent;
1848 HANDLE hInputDoneEvent;
1849 HANDLE hInputThread;
1850 uint8_t win_stdio_buf;
1851} WinStdioCharState;
1852
aliguori6f97dba2008-10-31 18:49:55 +00001853#define NSENDBUF 2048
1854#define NRECVBUF 2048
1855#define MAXCONNECT 1
1856#define NTIMEOUT 5000
1857
1858static int win_chr_poll(void *opaque);
1859static int win_chr_pipe_poll(void *opaque);
1860
1861static void win_chr_close(CharDriverState *chr)
1862{
1863 WinCharState *s = chr->opaque;
1864
1865 if (s->hsend) {
1866 CloseHandle(s->hsend);
1867 s->hsend = NULL;
1868 }
1869 if (s->hrecv) {
1870 CloseHandle(s->hrecv);
1871 s->hrecv = NULL;
1872 }
1873 if (s->hcom) {
1874 CloseHandle(s->hcom);
1875 s->hcom = NULL;
1876 }
1877 if (s->fpipe)
1878 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1879 else
1880 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301881
Hans de Goedea425d232011-11-19 10:22:43 +01001882 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001883}
1884
1885static int win_chr_init(CharDriverState *chr, const char *filename)
1886{
1887 WinCharState *s = chr->opaque;
1888 COMMCONFIG comcfg;
1889 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1890 COMSTAT comstat;
1891 DWORD size;
1892 DWORD err;
1893
1894 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1895 if (!s->hsend) {
1896 fprintf(stderr, "Failed CreateEvent\n");
1897 goto fail;
1898 }
1899 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1900 if (!s->hrecv) {
1901 fprintf(stderr, "Failed CreateEvent\n");
1902 goto fail;
1903 }
1904
1905 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1906 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1907 if (s->hcom == INVALID_HANDLE_VALUE) {
1908 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1909 s->hcom = NULL;
1910 goto fail;
1911 }
1912
1913 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1914 fprintf(stderr, "Failed SetupComm\n");
1915 goto fail;
1916 }
1917
1918 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1919 size = sizeof(COMMCONFIG);
1920 GetDefaultCommConfig(filename, &comcfg, &size);
1921 comcfg.dcb.DCBlength = sizeof(DCB);
1922 CommConfigDialog(filename, NULL, &comcfg);
1923
1924 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1925 fprintf(stderr, "Failed SetCommState\n");
1926 goto fail;
1927 }
1928
1929 if (!SetCommMask(s->hcom, EV_ERR)) {
1930 fprintf(stderr, "Failed SetCommMask\n");
1931 goto fail;
1932 }
1933
1934 cto.ReadIntervalTimeout = MAXDWORD;
1935 if (!SetCommTimeouts(s->hcom, &cto)) {
1936 fprintf(stderr, "Failed SetCommTimeouts\n");
1937 goto fail;
1938 }
1939
1940 if (!ClearCommError(s->hcom, &err, &comstat)) {
1941 fprintf(stderr, "Failed ClearCommError\n");
1942 goto fail;
1943 }
1944 qemu_add_polling_cb(win_chr_poll, chr);
1945 return 0;
1946
1947 fail:
1948 win_chr_close(chr);
1949 return -1;
1950}
1951
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001952/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001953static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1954{
1955 WinCharState *s = chr->opaque;
1956 DWORD len, ret, size, err;
1957
1958 len = len1;
1959 ZeroMemory(&s->osend, sizeof(s->osend));
1960 s->osend.hEvent = s->hsend;
1961 while (len > 0) {
1962 if (s->hsend)
1963 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1964 else
1965 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1966 if (!ret) {
1967 err = GetLastError();
1968 if (err == ERROR_IO_PENDING) {
1969 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1970 if (ret) {
1971 buf += size;
1972 len -= size;
1973 } else {
1974 break;
1975 }
1976 } else {
1977 break;
1978 }
1979 } else {
1980 buf += size;
1981 len -= size;
1982 }
1983 }
1984 return len1 - len;
1985}
1986
1987static int win_chr_read_poll(CharDriverState *chr)
1988{
1989 WinCharState *s = chr->opaque;
1990
Anthony Liguori909cda12011-08-15 11:17:31 -05001991 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001992 return s->max_size;
1993}
1994
1995static void win_chr_readfile(CharDriverState *chr)
1996{
1997 WinCharState *s = chr->opaque;
1998 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301999 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002000 DWORD size;
2001
2002 ZeroMemory(&s->orecv, sizeof(s->orecv));
2003 s->orecv.hEvent = s->hrecv;
2004 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2005 if (!ret) {
2006 err = GetLastError();
2007 if (err == ERROR_IO_PENDING) {
2008 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2009 }
2010 }
2011
2012 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002013 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002014 }
2015}
2016
2017static void win_chr_read(CharDriverState *chr)
2018{
2019 WinCharState *s = chr->opaque;
2020
2021 if (s->len > s->max_size)
2022 s->len = s->max_size;
2023 if (s->len == 0)
2024 return;
2025
2026 win_chr_readfile(chr);
2027}
2028
2029static int win_chr_poll(void *opaque)
2030{
2031 CharDriverState *chr = opaque;
2032 WinCharState *s = chr->opaque;
2033 COMSTAT status;
2034 DWORD comerr;
2035
2036 ClearCommError(s->hcom, &comerr, &status);
2037 if (status.cbInQue > 0) {
2038 s->len = status.cbInQue;
2039 win_chr_read_poll(chr);
2040 win_chr_read(chr);
2041 return 1;
2042 }
2043 return 0;
2044}
2045
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01002046static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00002047{
2048 CharDriverState *chr;
2049 WinCharState *s;
2050
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002051 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002052 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002053 chr->opaque = s;
2054 chr->chr_write = win_chr_write;
2055 chr->chr_close = win_chr_close;
2056
2057 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002058 g_free(s);
2059 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002060 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002061 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002062 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002063}
2064
2065static int win_chr_pipe_poll(void *opaque)
2066{
2067 CharDriverState *chr = opaque;
2068 WinCharState *s = chr->opaque;
2069 DWORD size;
2070
2071 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2072 if (size > 0) {
2073 s->len = size;
2074 win_chr_read_poll(chr);
2075 win_chr_read(chr);
2076 return 1;
2077 }
2078 return 0;
2079}
2080
2081static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2082{
2083 WinCharState *s = chr->opaque;
2084 OVERLAPPED ov;
2085 int ret;
2086 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002087 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002088
2089 s->fpipe = TRUE;
2090
2091 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2092 if (!s->hsend) {
2093 fprintf(stderr, "Failed CreateEvent\n");
2094 goto fail;
2095 }
2096 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2097 if (!s->hrecv) {
2098 fprintf(stderr, "Failed CreateEvent\n");
2099 goto fail;
2100 }
2101
2102 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2103 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2104 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2105 PIPE_WAIT,
2106 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2107 if (s->hcom == INVALID_HANDLE_VALUE) {
2108 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2109 s->hcom = NULL;
2110 goto fail;
2111 }
2112
2113 ZeroMemory(&ov, sizeof(ov));
2114 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2115 ret = ConnectNamedPipe(s->hcom, &ov);
2116 if (ret) {
2117 fprintf(stderr, "Failed ConnectNamedPipe\n");
2118 goto fail;
2119 }
2120
2121 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2122 if (!ret) {
2123 fprintf(stderr, "Failed GetOverlappedResult\n");
2124 if (ov.hEvent) {
2125 CloseHandle(ov.hEvent);
2126 ov.hEvent = NULL;
2127 }
2128 goto fail;
2129 }
2130
2131 if (ov.hEvent) {
2132 CloseHandle(ov.hEvent);
2133 ov.hEvent = NULL;
2134 }
2135 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2136 return 0;
2137
2138 fail:
2139 win_chr_close(chr);
2140 return -1;
2141}
2142
2143
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002144static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002145{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002146 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002147 CharDriverState *chr;
2148 WinCharState *s;
2149
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002150 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002151 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002152 chr->opaque = s;
2153 chr->chr_write = win_chr_write;
2154 chr->chr_close = win_chr_close;
2155
2156 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002157 g_free(s);
2158 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002159 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002160 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002161 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002162}
2163
Markus Armbruster1f514702012-02-07 15:09:08 +01002164static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002165{
2166 CharDriverState *chr;
2167 WinCharState *s;
2168
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002169 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002170 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002171 s->hcom = fd_out;
2172 chr->opaque = s;
2173 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002174 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002175}
2176
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002177static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002178{
Markus Armbruster1f514702012-02-07 15:09:08 +01002179 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002180}
2181
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002182static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2183{
2184 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2185 DWORD dwSize;
2186 int len1;
2187
2188 len1 = len;
2189
2190 while (len1 > 0) {
2191 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2192 break;
2193 }
2194 buf += dwSize;
2195 len1 -= dwSize;
2196 }
2197
2198 return len - len1;
2199}
2200
2201static void win_stdio_wait_func(void *opaque)
2202{
2203 CharDriverState *chr = opaque;
2204 WinStdioCharState *stdio = chr->opaque;
2205 INPUT_RECORD buf[4];
2206 int ret;
2207 DWORD dwSize;
2208 int i;
2209
Stefan Weildff74242013-12-07 14:48:04 +01002210 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002211
2212 if (!ret) {
2213 /* Avoid error storm */
2214 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2215 return;
2216 }
2217
2218 for (i = 0; i < dwSize; i++) {
2219 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2220
2221 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2222 int j;
2223 if (kev->uChar.AsciiChar != 0) {
2224 for (j = 0; j < kev->wRepeatCount; j++) {
2225 if (qemu_chr_be_can_write(chr)) {
2226 uint8_t c = kev->uChar.AsciiChar;
2227 qemu_chr_be_write(chr, &c, 1);
2228 }
2229 }
2230 }
2231 }
2232 }
2233}
2234
2235static DWORD WINAPI win_stdio_thread(LPVOID param)
2236{
2237 CharDriverState *chr = param;
2238 WinStdioCharState *stdio = chr->opaque;
2239 int ret;
2240 DWORD dwSize;
2241
2242 while (1) {
2243
2244 /* Wait for one byte */
2245 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2246
2247 /* Exit in case of error, continue if nothing read */
2248 if (!ret) {
2249 break;
2250 }
2251 if (!dwSize) {
2252 continue;
2253 }
2254
2255 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2256 if (stdio->win_stdio_buf == '\r') {
2257 continue;
2258 }
2259
2260 /* Signal the main thread and wait until the byte was eaten */
2261 if (!SetEvent(stdio->hInputReadyEvent)) {
2262 break;
2263 }
2264 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2265 != WAIT_OBJECT_0) {
2266 break;
2267 }
2268 }
2269
2270 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2271 return 0;
2272}
2273
2274static void win_stdio_thread_wait_func(void *opaque)
2275{
2276 CharDriverState *chr = opaque;
2277 WinStdioCharState *stdio = chr->opaque;
2278
2279 if (qemu_chr_be_can_write(chr)) {
2280 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2281 }
2282
2283 SetEvent(stdio->hInputDoneEvent);
2284}
2285
2286static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2287{
2288 WinStdioCharState *stdio = chr->opaque;
2289 DWORD dwMode = 0;
2290
2291 GetConsoleMode(stdio->hStdIn, &dwMode);
2292
2293 if (echo) {
2294 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2295 } else {
2296 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2297 }
2298}
2299
2300static void win_stdio_close(CharDriverState *chr)
2301{
2302 WinStdioCharState *stdio = chr->opaque;
2303
2304 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2305 CloseHandle(stdio->hInputReadyEvent);
2306 }
2307 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2308 CloseHandle(stdio->hInputDoneEvent);
2309 }
2310 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2311 TerminateThread(stdio->hInputThread, 0);
2312 }
2313
2314 g_free(chr->opaque);
2315 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002316}
2317
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002318static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002319{
2320 CharDriverState *chr;
2321 WinStdioCharState *stdio;
2322 DWORD dwMode;
2323 int is_console = 0;
2324
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002325 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002326 stdio = g_malloc0(sizeof(WinStdioCharState));
2327
2328 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2329 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2330 fprintf(stderr, "cannot open stdio: invalid handle\n");
2331 exit(1);
2332 }
2333
2334 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2335
2336 chr->opaque = stdio;
2337 chr->chr_write = win_stdio_write;
2338 chr->chr_close = win_stdio_close;
2339
Anthony Liguoried7a1542013-03-05 23:21:17 +05302340 if (is_console) {
2341 if (qemu_add_wait_object(stdio->hStdIn,
2342 win_stdio_wait_func, chr)) {
2343 fprintf(stderr, "qemu_add_wait_object: failed\n");
2344 }
2345 } else {
2346 DWORD dwId;
2347
2348 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2349 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2350 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2351 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002352
Anthony Liguoried7a1542013-03-05 23:21:17 +05302353 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2354 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2355 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2356 fprintf(stderr, "cannot create stdio thread or event\n");
2357 exit(1);
2358 }
2359 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2360 win_stdio_thread_wait_func, chr)) {
2361 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002362 }
2363 }
2364
2365 dwMode |= ENABLE_LINE_INPUT;
2366
Anthony Liguoried7a1542013-03-05 23:21:17 +05302367 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002368 /* set the terminal in raw mode */
2369 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2370 dwMode |= ENABLE_PROCESSED_INPUT;
2371 }
2372
2373 SetConsoleMode(stdio->hStdIn, dwMode);
2374
2375 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2376 qemu_chr_fe_set_echo(chr, false);
2377
Markus Armbruster1f514702012-02-07 15:09:08 +01002378 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002379}
aliguori6f97dba2008-10-31 18:49:55 +00002380#endif /* !_WIN32 */
2381
Anthony Liguori5ab82112013-03-05 23:21:31 +05302382
aliguori6f97dba2008-10-31 18:49:55 +00002383/***********************************************************/
2384/* UDP Net console */
2385
2386typedef struct {
2387 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302388 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302389 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002390 int bufcnt;
2391 int bufptr;
2392 int max_size;
2393} NetCharDriver;
2394
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002395/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002396static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2397{
2398 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302399 gsize bytes_written;
2400 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002401
Anthony Liguori76a96442013-03-05 23:21:21 +05302402 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2403 if (status == G_IO_STATUS_EOF) {
2404 return 0;
2405 } else if (status != G_IO_STATUS_NORMAL) {
2406 return -1;
2407 }
2408
2409 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002410}
2411
2412static int udp_chr_read_poll(void *opaque)
2413{
2414 CharDriverState *chr = opaque;
2415 NetCharDriver *s = chr->opaque;
2416
Anthony Liguori909cda12011-08-15 11:17:31 -05002417 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002418
2419 /* If there were any stray characters in the queue process them
2420 * first
2421 */
2422 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002423 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002424 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002425 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002426 }
2427 return s->max_size;
2428}
2429
Anthony Liguori76a96442013-03-05 23:21:21 +05302430static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002431{
2432 CharDriverState *chr = opaque;
2433 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302434 gsize bytes_read = 0;
2435 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002436
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002437 if (s->max_size == 0) {
2438 return TRUE;
2439 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302440 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2441 &bytes_read, NULL);
2442 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002443 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302444 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302445 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302446 return FALSE;
2447 }
aliguori6f97dba2008-10-31 18:49:55 +00002448
2449 s->bufptr = 0;
2450 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002451 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002452 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002453 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002454 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302455
2456 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002457}
2458
2459static void udp_chr_update_read_handler(CharDriverState *chr)
2460{
2461 NetCharDriver *s = chr->opaque;
2462
Amit Shah26da70c2013-08-28 15:23:37 +05302463 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302464 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302465 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2466 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002467 }
2468}
2469
aliguori819f56b2009-03-28 17:58:14 +00002470static void udp_chr_close(CharDriverState *chr)
2471{
2472 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302473
2474 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302475 if (s->chan) {
2476 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002477 closesocket(s->fd);
2478 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002479 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002480 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002481}
2482
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002483static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002484{
2485 CharDriverState *chr = NULL;
2486 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002487
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002488 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002489 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002490
aliguori6f97dba2008-10-31 18:49:55 +00002491 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302492 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002493 s->bufcnt = 0;
2494 s->bufptr = 0;
2495 chr->opaque = s;
2496 chr->chr_write = udp_chr_write;
2497 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002498 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002499 /* be isn't opened until we get a connection */
2500 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002501 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002502}
aliguori6f97dba2008-10-31 18:49:55 +00002503
aliguori6f97dba2008-10-31 18:49:55 +00002504/***********************************************************/
2505/* TCP Net console */
2506
2507typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302508
2509 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302510 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002511 int fd, listen_fd;
2512 int connected;
2513 int max_size;
2514 int do_telnetopt;
2515 int do_nodelay;
2516 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002517 int *read_msgfds;
2518 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002519 int *write_msgfds;
2520 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002521
2522 SocketAddress *addr;
2523 bool is_listen;
2524 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002525
2526 guint reconnect_timer;
2527 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002528 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002529} TCPCharDriver;
2530
Corey Minyard5dd1f022014-10-02 11:17:37 -05002531static gboolean socket_reconnect_timeout(gpointer opaque);
2532
2533static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2534{
2535 TCPCharDriver *s = chr->opaque;
2536 assert(s->connected == 0);
2537 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2538 socket_reconnect_timeout, chr);
2539}
2540
Corey Minyard5008e5b2014-10-08 07:11:55 -05002541static void check_report_connect_error(CharDriverState *chr,
2542 Error *err)
2543{
2544 TCPCharDriver *s = chr->opaque;
2545
2546 if (!s->connect_err_reported) {
2547 error_report("Unable to connect character device %s: %s",
2548 chr->label, error_get_pretty(err));
2549 s->connect_err_reported = true;
2550 }
2551 qemu_chr_socket_restart_timer(chr);
2552}
2553
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302554static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002555
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002556#ifndef _WIN32
2557static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2558{
2559 TCPCharDriver *s = chr->opaque;
2560 struct msghdr msgh;
2561 struct iovec iov;
2562 int r;
2563
2564 size_t fd_size = s->write_msgfds_num * sizeof(int);
2565 char control[CMSG_SPACE(fd_size)];
2566 struct cmsghdr *cmsg;
2567
2568 memset(&msgh, 0, sizeof(msgh));
2569 memset(control, 0, sizeof(control));
2570
2571 /* set the payload */
2572 iov.iov_base = (uint8_t *) buf;
2573 iov.iov_len = len;
2574
2575 msgh.msg_iov = &iov;
2576 msgh.msg_iovlen = 1;
2577
2578 msgh.msg_control = control;
2579 msgh.msg_controllen = sizeof(control);
2580
2581 cmsg = CMSG_FIRSTHDR(&msgh);
2582
2583 cmsg->cmsg_len = CMSG_LEN(fd_size);
2584 cmsg->cmsg_level = SOL_SOCKET;
2585 cmsg->cmsg_type = SCM_RIGHTS;
2586 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2587
2588 do {
2589 r = sendmsg(s->fd, &msgh, 0);
2590 } while (r < 0 && errno == EINTR);
2591
2592 /* free the written msgfds, no matter what */
2593 if (s->write_msgfds_num) {
2594 g_free(s->write_msgfds);
2595 s->write_msgfds = 0;
2596 s->write_msgfds_num = 0;
2597 }
2598
2599 return r;
2600}
2601#endif
2602
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002603/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002604static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2605{
2606 TCPCharDriver *s = chr->opaque;
2607 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002608#ifndef _WIN32
2609 if (s->is_unix && s->write_msgfds_num) {
2610 return unix_send_msgfds(chr, buf, len);
2611 } else
2612#endif
2613 {
2614 return io_channel_send(s->chan, buf, len);
2615 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002616 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002617 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002618 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002619 }
2620}
2621
2622static int tcp_chr_read_poll(void *opaque)
2623{
2624 CharDriverState *chr = opaque;
2625 TCPCharDriver *s = chr->opaque;
2626 if (!s->connected)
2627 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002628 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002629 return s->max_size;
2630}
2631
2632#define IAC 255
2633#define IAC_BREAK 243
2634static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2635 TCPCharDriver *s,
2636 uint8_t *buf, int *size)
2637{
2638 /* Handle any telnet client's basic IAC options to satisfy char by
2639 * char mode with no echo. All IAC options will be removed from
2640 * the buf and the do_telnetopt variable will be used to track the
2641 * state of the width of the IAC information.
2642 *
2643 * IAC commands come in sets of 3 bytes with the exception of the
2644 * "IAC BREAK" command and the double IAC.
2645 */
2646
2647 int i;
2648 int j = 0;
2649
2650 for (i = 0; i < *size; i++) {
2651 if (s->do_telnetopt > 1) {
2652 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2653 /* Double IAC means send an IAC */
2654 if (j != i)
2655 buf[j] = buf[i];
2656 j++;
2657 s->do_telnetopt = 1;
2658 } else {
2659 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2660 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002661 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002662 s->do_telnetopt++;
2663 }
2664 s->do_telnetopt++;
2665 }
2666 if (s->do_telnetopt >= 4) {
2667 s->do_telnetopt = 1;
2668 }
2669 } else {
2670 if ((unsigned char)buf[i] == IAC) {
2671 s->do_telnetopt = 2;
2672 } else {
2673 if (j != i)
2674 buf[j] = buf[i];
2675 j++;
2676 }
2677 }
2678 }
2679 *size = j;
2680}
2681
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002682static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002683{
2684 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002685 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2686
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002687 assert(num <= TCP_MAX_FDS);
2688
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002689 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002690 int i;
2691
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002692 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2693
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002694 /* Close unused fds */
2695 for (i = to_copy; i < s->read_msgfds_num; i++) {
2696 close(s->read_msgfds[i]);
2697 }
2698
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002699 g_free(s->read_msgfds);
2700 s->read_msgfds = 0;
2701 s->read_msgfds_num = 0;
2702 }
2703
2704 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002705}
2706
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002707static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2708{
2709 TCPCharDriver *s = chr->opaque;
2710
2711 /* clear old pending fd array */
2712 if (s->write_msgfds) {
2713 g_free(s->write_msgfds);
2714 }
2715
2716 if (num) {
2717 s->write_msgfds = g_malloc(num * sizeof(int));
2718 memcpy(s->write_msgfds, fds, num * sizeof(int));
2719 }
2720
2721 s->write_msgfds_num = num;
2722
2723 return 0;
2724}
2725
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002726#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002727static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2728{
2729 TCPCharDriver *s = chr->opaque;
2730 struct cmsghdr *cmsg;
2731
2732 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002733 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002734
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002735 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002736 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002737 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002738 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002739 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002740
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002741 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2742
2743 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002744 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002745 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002746
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002747 /* close and clean read_msgfds */
2748 for (i = 0; i < s->read_msgfds_num; i++) {
2749 close(s->read_msgfds[i]);
2750 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002751
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002752 if (s->read_msgfds_num) {
2753 g_free(s->read_msgfds);
2754 }
2755
2756 s->read_msgfds_num = fd_size / sizeof(int);
2757 s->read_msgfds = g_malloc(fd_size);
2758 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2759
2760 for (i = 0; i < s->read_msgfds_num; i++) {
2761 int fd = s->read_msgfds[i];
2762 if (fd < 0) {
2763 continue;
2764 }
2765
2766 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2767 qemu_set_block(fd);
2768
2769 #ifndef MSG_CMSG_CLOEXEC
2770 qemu_set_cloexec(fd);
2771 #endif
2772 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002773 }
2774}
2775
Mark McLoughlin9977c892009-07-22 09:11:38 +01002776static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2777{
2778 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002779 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002780 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002781 union {
2782 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002783 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002784 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002785 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002786 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002787
2788 iov[0].iov_base = buf;
2789 iov[0].iov_len = len;
2790
2791 msg.msg_iov = iov;
2792 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002793 msg.msg_control = &msg_control;
2794 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002795
Corey Bryant06138652012-08-14 16:43:42 -04002796#ifdef MSG_CMSG_CLOEXEC
2797 flags |= MSG_CMSG_CLOEXEC;
2798#endif
2799 ret = recvmsg(s->fd, &msg, flags);
2800 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002801 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002802 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002803
2804 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002805}
2806#else
2807static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2808{
2809 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002810 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002811}
2812#endif
2813
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302814static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2815{
2816 TCPCharDriver *s = chr->opaque;
2817 return g_io_create_watch(s->chan, cond);
2818}
2819
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002820static void tcp_chr_disconnect(CharDriverState *chr)
2821{
2822 TCPCharDriver *s = chr->opaque;
2823
2824 s->connected = 0;
2825 if (s->listen_chan) {
2826 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2827 tcp_chr_accept, chr);
2828 }
2829 remove_fd_in_watch(chr);
2830 g_io_channel_unref(s->chan);
2831 s->chan = NULL;
2832 closesocket(s->fd);
2833 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002834 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2835 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002836 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002837 if (s->reconnect_time) {
2838 qemu_chr_socket_restart_timer(chr);
2839 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002840}
2841
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302842static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002843{
2844 CharDriverState *chr = opaque;
2845 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302846 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002847 int len, size;
2848
Kirill Batuzov812c1052014-07-01 15:52:32 +04002849 if (cond & G_IO_HUP) {
2850 /* connection closed */
2851 tcp_chr_disconnect(chr);
2852 return TRUE;
2853 }
2854
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302855 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002856 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302857 }
aliguori6f97dba2008-10-31 18:49:55 +00002858 len = sizeof(buf);
2859 if (len > s->max_size)
2860 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002861 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002862 if (size == 0) {
2863 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002864 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002865 } else if (size > 0) {
2866 if (s->do_telnetopt)
2867 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2868 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002869 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002870 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302871
2872 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002873}
2874
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002875static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2876{
2877 TCPCharDriver *s = chr->opaque;
2878 int size;
2879
2880 if (!s->connected) {
2881 return 0;
2882 }
2883
2884 size = tcp_chr_recv(chr, (void *) buf, len);
2885 if (size == 0) {
2886 /* connection closed */
2887 tcp_chr_disconnect(chr);
2888 }
2889
2890 return size;
2891}
2892
Blue Swirl68c18d12010-08-15 09:46:24 +00002893#ifndef _WIN32
2894CharDriverState *qemu_chr_open_eventfd(int eventfd)
2895{
David Marchande9d21c42014-06-11 17:25:16 +02002896 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2897
2898 if (chr) {
2899 chr->avail_connections = 1;
2900 }
2901
2902 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002903}
Blue Swirl68c18d12010-08-15 09:46:24 +00002904#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002905
aliguori6f97dba2008-10-31 18:49:55 +00002906static void tcp_chr_connect(void *opaque)
2907{
2908 CharDriverState *chr = opaque;
2909 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002910 struct sockaddr_storage ss, ps;
2911 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002912
2913 memset(&ss, 0, ss_len);
2914 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2915 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2916 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002917 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2918 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2919 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002920 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002921 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2922 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002923 s->is_listen, s->is_telnet);
2924 }
aliguori6f97dba2008-10-31 18:49:55 +00002925
2926 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302927 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302928 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2929 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002930 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002931 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002932}
2933
Gal Hammerac1b84d2014-02-25 12:12:35 +02002934static void tcp_chr_update_read_handler(CharDriverState *chr)
2935{
2936 TCPCharDriver *s = chr->opaque;
2937
2938 remove_fd_in_watch(chr);
2939 if (s->chan) {
2940 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2941 tcp_chr_read, chr);
2942 }
2943}
2944
aliguori6f97dba2008-10-31 18:49:55 +00002945#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2946static void tcp_chr_telnet_init(int fd)
2947{
2948 char buf[3];
2949 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2950 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2951 send(fd, (char *)buf, 3, 0);
2952 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2953 send(fd, (char *)buf, 3, 0);
2954 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2955 send(fd, (char *)buf, 3, 0);
2956 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2957 send(fd, (char *)buf, 3, 0);
2958}
2959
Daniel P. Berrange13661082011-06-23 13:31:42 +01002960static int tcp_chr_add_client(CharDriverState *chr, int fd)
2961{
2962 TCPCharDriver *s = chr->opaque;
2963 if (s->fd != -1)
2964 return -1;
2965
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002966 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002967 if (s->do_nodelay)
2968 socket_set_nodelay(fd);
2969 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302970 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002971 if (s->listen_tag) {
2972 g_source_remove(s->listen_tag);
2973 s->listen_tag = 0;
2974 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002975 tcp_chr_connect(chr);
2976
2977 return 0;
2978}
2979
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302980static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002981{
2982 CharDriverState *chr = opaque;
2983 TCPCharDriver *s = chr->opaque;
2984 struct sockaddr_in saddr;
2985#ifndef _WIN32
2986 struct sockaddr_un uaddr;
2987#endif
2988 struct sockaddr *addr;
2989 socklen_t len;
2990 int fd;
2991
2992 for(;;) {
2993#ifndef _WIN32
2994 if (s->is_unix) {
2995 len = sizeof(uaddr);
2996 addr = (struct sockaddr *)&uaddr;
2997 } else
2998#endif
2999 {
3000 len = sizeof(saddr);
3001 addr = (struct sockaddr *)&saddr;
3002 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003003 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003004 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003005 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303006 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003007 } else if (fd >= 0) {
3008 if (s->do_telnetopt)
3009 tcp_chr_telnet_init(fd);
3010 break;
3011 }
3012 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003013 if (tcp_chr_add_client(chr, fd) < 0)
3014 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303015
3016 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003017}
3018
3019static void tcp_chr_close(CharDriverState *chr)
3020{
3021 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003022 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003023
Corey Minyard5dd1f022014-10-02 11:17:37 -05003024 if (s->reconnect_timer) {
3025 g_source_remove(s->reconnect_timer);
3026 s->reconnect_timer = 0;
3027 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003028 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003029 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303030 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303031 if (s->chan) {
3032 g_io_channel_unref(s->chan);
3033 }
aliguori6f97dba2008-10-31 18:49:55 +00003034 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003035 }
3036 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303037 if (s->listen_tag) {
3038 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003039 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303040 }
3041 if (s->listen_chan) {
3042 g_io_channel_unref(s->listen_chan);
3043 }
aliguori6f97dba2008-10-31 18:49:55 +00003044 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003045 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003046 if (s->read_msgfds_num) {
3047 for (i = 0; i < s->read_msgfds_num; i++) {
3048 close(s->read_msgfds[i]);
3049 }
3050 g_free(s->read_msgfds);
3051 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003052 if (s->write_msgfds_num) {
3053 g_free(s->write_msgfds);
3054 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003055 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003056 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003057}
3058
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003059static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003060{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003061 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003062
Corey Minyardcfb429c2014-10-02 11:17:35 -05003063 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003064 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303065 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003066 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3067 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003068 } else {
3069 s->connected = 1;
3070 s->fd = fd;
3071 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303072 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003073 tcp_chr_connect(chr);
3074 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003075}
3076
Corey Minyard51795022014-10-08 07:11:56 -05003077static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003078{
3079 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003080 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003081
3082 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003083 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003084 return;
3085 }
3086
Corey Minyard5008e5b2014-10-08 07:11:55 -05003087 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003088 qemu_chr_finish_socket_connection(chr, fd);
3089}
3090
Corey Minyardcfb429c2014-10-02 11:17:35 -05003091static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003092{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003093 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003094 int fd;
3095
Corey Minyardcfb429c2014-10-02 11:17:35 -05003096 if (s->is_listen) {
3097 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003098 } else if (s->reconnect_time) {
3099 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3100 return fd >= 0;
3101 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003102 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003103 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003104 if (fd < 0) {
3105 return false;
3106 }
3107
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003108 qemu_chr_finish_socket_connection(chr, fd);
3109 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003110}
3111
Lei Li51767e72013-01-25 00:03:19 +08003112/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003113/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003114
3115typedef struct {
3116 size_t size;
3117 size_t prod;
3118 size_t cons;
3119 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003120} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003121
Markus Armbruster3949e592013-02-06 21:27:24 +01003122static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003123{
Markus Armbruster3949e592013-02-06 21:27:24 +01003124 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003125
Markus Armbruster5c230102013-02-06 21:27:23 +01003126 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003127}
3128
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003129/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003130static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003131{
Markus Armbruster3949e592013-02-06 21:27:24 +01003132 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003133 int i;
3134
3135 if (!buf || (len < 0)) {
3136 return -1;
3137 }
3138
3139 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003140 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3141 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003142 d->cons = d->prod - d->size;
3143 }
3144 }
3145
3146 return 0;
3147}
3148
Markus Armbruster3949e592013-02-06 21:27:24 +01003149static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003150{
Markus Armbruster3949e592013-02-06 21:27:24 +01003151 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003152 int i;
3153
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003154 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003155 for (i = 0; i < len && d->cons != d->prod; i++) {
3156 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003157 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003158 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003159
3160 return i;
3161}
3162
Markus Armbruster3949e592013-02-06 21:27:24 +01003163static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003164{
Markus Armbruster3949e592013-02-06 21:27:24 +01003165 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003166
3167 g_free(d->cbuf);
3168 g_free(d);
3169 chr->opaque = NULL;
3170}
3171
Markus Armbruster4f573782013-07-26 16:44:32 +02003172static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3173 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003174{
3175 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003176 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003177
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003178 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003179 d = g_malloc(sizeof(*d));
3180
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003181 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003182
3183 /* The size must be power of 2 */
3184 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003185 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003186 goto fail;
3187 }
3188
3189 d->prod = 0;
3190 d->cons = 0;
3191 d->cbuf = g_malloc0(d->size);
3192
3193 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003194 chr->chr_write = ringbuf_chr_write;
3195 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003196
3197 return chr;
3198
3199fail:
3200 g_free(d);
3201 g_free(chr);
3202 return NULL;
3203}
3204
Hani Benhabiles8e597772014-05-27 23:39:30 +01003205bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003206{
Markus Armbruster3949e592013-02-06 21:27:24 +01003207 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003208}
3209
Markus Armbruster3949e592013-02-06 21:27:24 +01003210void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003211 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003212 Error **errp)
3213{
3214 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003215 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003216 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003217 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003218
3219 chr = qemu_chr_find(device);
3220 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003221 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003222 return;
3223 }
3224
Markus Armbruster3949e592013-02-06 21:27:24 +01003225 if (!chr_is_ringbuf(chr)) {
3226 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003227 return;
3228 }
3229
Lei Li1f590cf2013-01-25 00:03:20 +08003230 if (has_format && (format == DATA_FORMAT_BASE64)) {
3231 write_data = g_base64_decode(data, &write_count);
3232 } else {
3233 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003234 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003235 }
3236
Markus Armbruster3949e592013-02-06 21:27:24 +01003237 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003238
Markus Armbruster13289fb2013-02-06 21:27:18 +01003239 if (write_data != (uint8_t *)data) {
3240 g_free((void *)write_data);
3241 }
3242
Lei Li1f590cf2013-01-25 00:03:20 +08003243 if (ret < 0) {
3244 error_setg(errp, "Failed to write to device %s", device);
3245 return;
3246 }
3247}
3248
Markus Armbruster3949e592013-02-06 21:27:24 +01003249char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003250 bool has_format, enum DataFormat format,
3251 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003252{
3253 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003254 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003255 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003256 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003257
3258 chr = qemu_chr_find(device);
3259 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003260 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003261 return NULL;
3262 }
3263
Markus Armbruster3949e592013-02-06 21:27:24 +01003264 if (!chr_is_ringbuf(chr)) {
3265 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003266 return NULL;
3267 }
3268
3269 if (size <= 0) {
3270 error_setg(errp, "size must be greater than zero");
3271 return NULL;
3272 }
3273
Markus Armbruster3949e592013-02-06 21:27:24 +01003274 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003275 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003276 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003277
Markus Armbruster3949e592013-02-06 21:27:24 +01003278 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003279
3280 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003281 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003282 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003283 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003284 /*
3285 * FIXME should read only complete, valid UTF-8 characters up
3286 * to @size bytes. Invalid sequences should be replaced by a
3287 * suitable replacement character. Except when (and only
3288 * when) ring buffer lost characters since last read, initial
3289 * continuation characters should be dropped.
3290 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003291 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003292 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003293 }
3294
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003295 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003296}
3297
Gerd Hoffmann33521632009-12-08 13:11:49 +01003298QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003299{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003300 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003301 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003302 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003303 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003304 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003305
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003306 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003307 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003308 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003309 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003310 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003311
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003312 if (strstart(filename, "mon:", &p)) {
3313 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003314 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003315 if (strcmp(filename, "stdio") == 0) {
3316 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3317 * but pass it to the guest. Handle this only for compat syntax,
3318 * for -chardev syntax we have special option for this.
3319 * This is what -nographic did, redirecting+muxing serial+monitor
3320 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003321 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003322 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003323 }
3324
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003325 if (strcmp(filename, "null") == 0 ||
3326 strcmp(filename, "pty") == 0 ||
3327 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003328 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003329 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003330 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003331 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003332 return opts;
3333 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003334 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003335 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003336 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003337 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003338 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003339 qemu_opt_set(opts, "width", width, &error_abort);
3340 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003341 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003342 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003343 qemu_opt_set(opts, "cols", width, &error_abort);
3344 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003345 } else {
3346 goto fail;
3347 }
3348 }
3349 return opts;
3350 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003351 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003352 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003353 return opts;
3354 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003355 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003356 qemu_opt_set(opts, "backend", "serial", &error_abort);
3357 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003358 return opts;
3359 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003360 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003361 qemu_opt_set(opts, "backend", "file", &error_abort);
3362 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003363 return opts;
3364 }
3365 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003366 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3367 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003368 return opts;
3369 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003370 if (strstart(filename, "tcp:", &p) ||
3371 strstart(filename, "telnet:", &p)) {
3372 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3373 host[0] = 0;
3374 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3375 goto fail;
3376 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003377 qemu_opt_set(opts, "backend", "socket", &error_abort);
3378 qemu_opt_set(opts, "host", host, &error_abort);
3379 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003380 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003381 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3382 if (local_err) {
3383 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003384 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003385 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003386 }
3387 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003388 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003389 return opts;
3390 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003391 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003392 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003393 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3394 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003395 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003396 goto fail;
3397 }
3398 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003399 qemu_opt_set(opts, "host", host, &error_abort);
3400 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003401 if (p[pos] == '@') {
3402 p += pos + 1;
3403 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3404 host[0] = 0;
3405 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003406 goto fail;
3407 }
3408 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003409 qemu_opt_set(opts, "localaddr", host, &error_abort);
3410 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003411 }
3412 return opts;
3413 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003414 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003415 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003416 qemu_opts_do_parse(opts, p, "path", &local_err);
3417 if (local_err) {
3418 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003419 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003420 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003421 return opts;
3422 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003423 if (strstart(filename, "/dev/parport", NULL) ||
3424 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003425 qemu_opt_set(opts, "backend", "parport", &error_abort);
3426 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003427 return opts;
3428 }
3429 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003430 qemu_opt_set(opts, "backend", "tty", &error_abort);
3431 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003432 return opts;
3433 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003434
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003435fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003436 qemu_opts_del(opts);
3437 return NULL;
3438}
3439
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003440static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3441 Error **errp)
3442{
3443 const char *path = qemu_opt_get(opts, "path");
3444
3445 if (path == NULL) {
3446 error_setg(errp, "chardev: file: no filename given");
3447 return;
3448 }
3449 backend->file = g_new0(ChardevFile, 1);
3450 backend->file->out = g_strdup(path);
3451}
3452
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003453static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3454 Error **errp)
3455{
3456 backend->stdio = g_new0(ChardevStdio, 1);
3457 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003458 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003459}
3460
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003461static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3462 Error **errp)
3463{
3464 const char *device = qemu_opt_get(opts, "path");
3465
3466 if (device == NULL) {
3467 error_setg(errp, "chardev: serial/tty: no device path given");
3468 return;
3469 }
3470 backend->serial = g_new0(ChardevHostdev, 1);
3471 backend->serial->device = g_strdup(device);
3472}
3473
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003474static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3475 Error **errp)
3476{
3477 const char *device = qemu_opt_get(opts, "path");
3478
3479 if (device == NULL) {
3480 error_setg(errp, "chardev: parallel: no device path given");
3481 return;
3482 }
3483 backend->parallel = g_new0(ChardevHostdev, 1);
3484 backend->parallel->device = g_strdup(device);
3485}
3486
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003487static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3488 Error **errp)
3489{
3490 const char *device = qemu_opt_get(opts, "path");
3491
3492 if (device == NULL) {
3493 error_setg(errp, "chardev: pipe: no device path given");
3494 return;
3495 }
3496 backend->pipe = g_new0(ChardevHostdev, 1);
3497 backend->pipe->device = g_strdup(device);
3498}
3499
Markus Armbruster4f573782013-07-26 16:44:32 +02003500static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3501 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003502{
3503 int val;
3504
Markus Armbruster3a1da422013-07-26 16:44:34 +02003505 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003506
Markus Armbruster0f953052013-06-27 16:22:07 +02003507 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003508 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003509 backend->ringbuf->has_size = true;
3510 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003511 }
3512}
3513
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003514static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3515 Error **errp)
3516{
3517 const char *chardev = qemu_opt_get(opts, "chardev");
3518
3519 if (chardev == NULL) {
3520 error_setg(errp, "chardev: mux: no chardev given");
3521 return;
3522 }
3523 backend->mux = g_new0(ChardevMux, 1);
3524 backend->mux->chardev = g_strdup(chardev);
3525}
3526
Peter Maydelldafd3252014-09-02 11:24:13 +01003527static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3528 Error **errp)
3529{
3530 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3531 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3532 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3533 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003534 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003535 const char *path = qemu_opt_get(opts, "path");
3536 const char *host = qemu_opt_get(opts, "host");
3537 const char *port = qemu_opt_get(opts, "port");
3538 SocketAddress *addr;
3539
3540 if (!path) {
3541 if (!host) {
3542 error_setg(errp, "chardev: socket: no host given");
3543 return;
3544 }
3545 if (!port) {
3546 error_setg(errp, "chardev: socket: no port given");
3547 return;
3548 }
3549 }
3550
3551 backend->socket = g_new0(ChardevSocket, 1);
3552
3553 backend->socket->has_nodelay = true;
3554 backend->socket->nodelay = do_nodelay;
3555 backend->socket->has_server = true;
3556 backend->socket->server = is_listen;
3557 backend->socket->has_telnet = true;
3558 backend->socket->telnet = is_telnet;
3559 backend->socket->has_wait = true;
3560 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003561 backend->socket->has_reconnect = true;
3562 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003563
3564 addr = g_new0(SocketAddress, 1);
3565 if (path) {
3566 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3567 addr->q_unix = g_new0(UnixSocketAddress, 1);
3568 addr->q_unix->path = g_strdup(path);
3569 } else {
3570 addr->kind = SOCKET_ADDRESS_KIND_INET;
3571 addr->inet = g_new0(InetSocketAddress, 1);
3572 addr->inet->host = g_strdup(host);
3573 addr->inet->port = g_strdup(port);
3574 addr->inet->has_to = qemu_opt_get(opts, "to");
3575 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3576 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3577 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3578 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3579 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3580 }
3581 backend->socket->addr = addr;
3582}
3583
Peter Maydell90a14bf2014-09-02 11:24:15 +01003584static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3585 Error **errp)
3586{
3587 const char *host = qemu_opt_get(opts, "host");
3588 const char *port = qemu_opt_get(opts, "port");
3589 const char *localaddr = qemu_opt_get(opts, "localaddr");
3590 const char *localport = qemu_opt_get(opts, "localport");
3591 bool has_local = false;
3592 SocketAddress *addr;
3593
3594 if (host == NULL || strlen(host) == 0) {
3595 host = "localhost";
3596 }
3597 if (port == NULL || strlen(port) == 0) {
3598 error_setg(errp, "chardev: udp: remote port not specified");
3599 return;
3600 }
3601 if (localport == NULL || strlen(localport) == 0) {
3602 localport = "0";
3603 } else {
3604 has_local = true;
3605 }
3606 if (localaddr == NULL || strlen(localaddr) == 0) {
3607 localaddr = "";
3608 } else {
3609 has_local = true;
3610 }
3611
3612 backend->udp = g_new0(ChardevUdp, 1);
3613
3614 addr = g_new0(SocketAddress, 1);
3615 addr->kind = SOCKET_ADDRESS_KIND_INET;
3616 addr->inet = g_new0(InetSocketAddress, 1);
3617 addr->inet->host = g_strdup(host);
3618 addr->inet->port = g_strdup(port);
3619 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3620 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3621 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3622 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3623 backend->udp->remote = addr;
3624
3625 if (has_local) {
3626 backend->udp->has_local = true;
3627 addr = g_new0(SocketAddress, 1);
3628 addr->kind = SOCKET_ADDRESS_KIND_INET;
3629 addr->inet = g_new0(InetSocketAddress, 1);
3630 addr->inet->host = g_strdup(localaddr);
3631 addr->inet->port = g_strdup(localport);
3632 backend->udp->local = addr;
3633 }
3634}
3635
Anthony Liguorid654f342013-03-05 23:21:28 +05303636typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003637 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003638 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003639 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303640} CharDriver;
3641
3642static GSList *backends;
3643
Peter Maydelle4d50d42014-09-02 11:24:17 +01003644void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003645 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3646{
3647 CharDriver *s;
3648
3649 s = g_malloc0(sizeof(*s));
3650 s->name = g_strdup(name);
3651 s->kind = kind;
3652 s->parse = parse;
3653
3654 backends = g_slist_append(backends, s);
3655}
3656
Anthony Liguorif69554b2011-08-15 11:17:37 -05003657CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003658 void (*init)(struct CharDriverState *s),
3659 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003660{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003661 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303662 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003663 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303664 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003665 ChardevReturn *ret = NULL;
3666 ChardevBackend *backend;
3667 const char *id = qemu_opts_id(opts);
3668 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003669
Peter Maydella61ae7f2014-09-02 11:24:16 +01003670 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003671 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003672 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003673 }
3674
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003675 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003676 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003677 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003678 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003679 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303680 for (i = backends; i; i = i->next) {
3681 cd = i->data;
3682
3683 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003684 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303685 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003686 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303687 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003688 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003689 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003690 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003691 }
3692
Peter Maydella61ae7f2014-09-02 11:24:16 +01003693 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003694
3695 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003696 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003697 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003698
3699 chr = NULL;
3700 backend->kind = cd->kind;
3701 if (cd->parse) {
3702 cd->parse(opts, backend, &local_err);
3703 if (local_err) {
3704 error_propagate(errp, local_err);
3705 goto qapi_out;
3706 }
3707 }
3708 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3709 if (!ret) {
3710 goto qapi_out;
3711 }
3712
3713 if (bid) {
3714 qapi_free_ChardevBackend(backend);
3715 qapi_free_ChardevReturn(ret);
3716 backend = g_new0(ChardevBackend, 1);
3717 backend->mux = g_new0(ChardevMux, 1);
3718 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3719 backend->mux->chardev = g_strdup(bid);
3720 ret = qmp_chardev_add(id, backend, errp);
3721 if (!ret) {
3722 chr = qemu_chr_find(bid);
3723 qemu_chr_delete(chr);
3724 chr = NULL;
3725 goto qapi_out;
3726 }
3727 }
3728
3729 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003730 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003731
3732qapi_out:
3733 qapi_free_ChardevBackend(backend);
3734 qapi_free_ChardevReturn(ret);
3735 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003736 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003737
3738err:
3739 qemu_opts_del(opts);
3740 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003741}
3742
Anthony Liguori27143a42011-08-15 11:17:36 -05003743CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003744{
3745 const char *p;
3746 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003747 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003748 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003749
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003750 if (strstart(filename, "chardev:", &p)) {
3751 return qemu_chr_find(p);
3752 }
3753
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003754 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003755 if (!opts)
3756 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003757
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003758 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003759 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003760 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003761 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003762 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003763 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003764 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003765 }
3766 return chr;
3767}
3768
Anthony Liguori15f31512011-08-15 11:17:35 -05003769void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003770{
3771 if (chr->chr_set_echo) {
3772 chr->chr_set_echo(chr, echo);
3773 }
3774}
3775
Hans de Goede8e25daa2013-03-26 11:07:57 +01003776void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003777{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003778 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003779 return;
3780 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003781 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003782 if (chr->chr_set_fe_open) {
3783 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003784 }
3785}
3786
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003787void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3788{
3789 if (chr->chr_fe_event) {
3790 chr->chr_fe_event(chr, event);
3791 }
3792}
3793
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003794int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3795 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303796{
3797 GSource *src;
3798 guint tag;
3799
3800 if (s->chr_add_watch == NULL) {
3801 return -ENOSYS;
3802 }
3803
3804 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003805 if (!src) {
3806 return -EINVAL;
3807 }
3808
Anthony Liguori23673ca2013-03-05 23:21:23 +05303809 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3810 tag = g_source_attach(src, NULL);
3811 g_source_unref(src);
3812
3813 return tag;
3814}
3815
Hans de Goede44c473d2013-03-27 20:29:39 +01003816int qemu_chr_fe_claim(CharDriverState *s)
3817{
3818 if (s->avail_connections < 1) {
3819 return -1;
3820 }
3821 s->avail_connections--;
3822 return 0;
3823}
3824
3825void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3826{
3827 if (qemu_chr_fe_claim(s) != 0) {
3828 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3829 __func__, s->label);
3830 exit(1);
3831 }
3832}
3833
3834void qemu_chr_fe_release(CharDriverState *s)
3835{
3836 s->avail_connections++;
3837}
3838
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003839void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003840{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003841 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003842 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003843 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003844 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003845 g_free(chr->filename);
3846 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003847 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003848 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003849}
3850
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003851ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003852{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003853 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003854 CharDriverState *chr;
3855
Blue Swirl72cf2d42009-09-12 07:36:22 +00003856 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003857 ChardevInfoList *info = g_malloc0(sizeof(*info));
3858 info->value = g_malloc0(sizeof(*info->value));
3859 info->value->label = g_strdup(chr->label);
3860 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003861 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003862
3863 info->next = chr_list;
3864 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003865 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003866
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003867 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003868}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003869
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003870ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3871{
3872 ChardevBackendInfoList *backend_list = NULL;
3873 CharDriver *c = NULL;
3874 GSList *i = NULL;
3875
3876 for (i = backends; i; i = i->next) {
3877 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3878 c = i->data;
3879 info->value = g_malloc0(sizeof(*info->value));
3880 info->value->name = g_strdup(c->name);
3881
3882 info->next = backend_list;
3883 backend_list = info;
3884 }
3885
3886 return backend_list;
3887}
3888
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003889CharDriverState *qemu_chr_find(const char *name)
3890{
3891 CharDriverState *chr;
3892
Blue Swirl72cf2d42009-09-12 07:36:22 +00003893 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003894 if (strcmp(chr->label, name) != 0)
3895 continue;
3896 return chr;
3897 }
3898 return NULL;
3899}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003900
3901/* Get a character (serial) device interface. */
3902CharDriverState *qemu_char_get_next_serial(void)
3903{
3904 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003905 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003906
3907 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003908
3909 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3910 chr = serial_hds[next_serial++];
3911 qemu_chr_fe_claim_no_fail(chr);
3912 return chr;
3913 }
3914 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003915}
3916
Paolo Bonzini4d454572012-11-26 16:03:42 +01003917QemuOptsList qemu_chardev_opts = {
3918 .name = "chardev",
3919 .implied_opt_name = "backend",
3920 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3921 .desc = {
3922 {
3923 .name = "backend",
3924 .type = QEMU_OPT_STRING,
3925 },{
3926 .name = "path",
3927 .type = QEMU_OPT_STRING,
3928 },{
3929 .name = "host",
3930 .type = QEMU_OPT_STRING,
3931 },{
3932 .name = "port",
3933 .type = QEMU_OPT_STRING,
3934 },{
3935 .name = "localaddr",
3936 .type = QEMU_OPT_STRING,
3937 },{
3938 .name = "localport",
3939 .type = QEMU_OPT_STRING,
3940 },{
3941 .name = "to",
3942 .type = QEMU_OPT_NUMBER,
3943 },{
3944 .name = "ipv4",
3945 .type = QEMU_OPT_BOOL,
3946 },{
3947 .name = "ipv6",
3948 .type = QEMU_OPT_BOOL,
3949 },{
3950 .name = "wait",
3951 .type = QEMU_OPT_BOOL,
3952 },{
3953 .name = "server",
3954 .type = QEMU_OPT_BOOL,
3955 },{
3956 .name = "delay",
3957 .type = QEMU_OPT_BOOL,
3958 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003959 .name = "reconnect",
3960 .type = QEMU_OPT_NUMBER,
3961 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003962 .name = "telnet",
3963 .type = QEMU_OPT_BOOL,
3964 },{
3965 .name = "width",
3966 .type = QEMU_OPT_NUMBER,
3967 },{
3968 .name = "height",
3969 .type = QEMU_OPT_NUMBER,
3970 },{
3971 .name = "cols",
3972 .type = QEMU_OPT_NUMBER,
3973 },{
3974 .name = "rows",
3975 .type = QEMU_OPT_NUMBER,
3976 },{
3977 .name = "mux",
3978 .type = QEMU_OPT_BOOL,
3979 },{
3980 .name = "signal",
3981 .type = QEMU_OPT_BOOL,
3982 },{
3983 .name = "name",
3984 .type = QEMU_OPT_STRING,
3985 },{
3986 .name = "debug",
3987 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003988 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003989 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003990 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003991 },{
3992 .name = "chardev",
3993 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003994 },
3995 { /* end of list */ }
3996 },
3997};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003998
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003999#ifdef _WIN32
4000
4001static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4002{
4003 HANDLE out;
4004
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004005 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004006 error_setg(errp, "input file not supported");
4007 return NULL;
4008 }
4009
4010 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4011 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4012 if (out == INVALID_HANDLE_VALUE) {
4013 error_setg(errp, "open %s failed", file->out);
4014 return NULL;
4015 }
4016 return qemu_chr_open_win_file(out);
4017}
4018
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004019static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4020 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004021{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004022 return qemu_chr_open_win_path(serial->device);
4023}
4024
4025static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4026 Error **errp)
4027{
4028 error_setg(errp, "character device backend type 'parallel' not supported");
4029 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004030}
4031
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004032#else /* WIN32 */
4033
4034static int qmp_chardev_open_file_source(char *src, int flags,
4035 Error **errp)
4036{
4037 int fd = -1;
4038
4039 TFR(fd = qemu_open(src, flags, 0666));
4040 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004041 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004042 }
4043 return fd;
4044}
4045
4046static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4047{
Markus Armbruster5f758362014-05-19 18:57:34 +02004048 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004049
4050 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4051 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004052 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004053 return NULL;
4054 }
4055
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004056 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004057 flags = O_RDONLY;
4058 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004059 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004060 qemu_close(out);
4061 return NULL;
4062 }
4063 }
4064
4065 return qemu_chr_open_fd(in, out);
4066}
4067
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004068static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4069 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004070{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004071#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004072 int fd;
4073
4074 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004075 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004076 return NULL;
4077 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004078 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004079 return qemu_chr_open_tty_fd(fd);
4080#else
4081 error_setg(errp, "character device backend type 'serial' not supported");
4082 return NULL;
4083#endif
4084}
4085
4086static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4087 Error **errp)
4088{
4089#ifdef HAVE_CHARDEV_PARPORT
4090 int fd;
4091
4092 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004093 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004094 return NULL;
4095 }
4096 return qemu_chr_open_pp_fd(fd);
4097#else
4098 error_setg(errp, "character device backend type 'parallel' not supported");
4099 return NULL;
4100#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004101}
4102
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004103#endif /* WIN32 */
4104
Corey Minyard5008e5b2014-10-08 07:11:55 -05004105static void socket_try_connect(CharDriverState *chr)
4106{
4107 Error *err = NULL;
4108
4109 if (!qemu_chr_open_socket_fd(chr, &err)) {
4110 check_report_connect_error(chr, err);
4111 }
4112}
4113
Corey Minyard5dd1f022014-10-02 11:17:37 -05004114static gboolean socket_reconnect_timeout(gpointer opaque)
4115{
4116 CharDriverState *chr = opaque;
4117 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004118
4119 s->reconnect_timer = 0;
4120
4121 if (chr->be_open) {
4122 return false;
4123 }
4124
Corey Minyard5008e5b2014-10-08 07:11:55 -05004125 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004126
4127 return false;
4128}
4129
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004130static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
4131 Error **errp)
4132{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004133 CharDriverState *chr;
4134 TCPCharDriver *s;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004135 SocketAddress *addr = sock->addr;
4136 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4137 bool is_listen = sock->has_server ? sock->server : true;
4138 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4139 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004140 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004141
4142 chr = qemu_chr_alloc();
4143 s = g_malloc0(sizeof(TCPCharDriver));
4144
4145 s->fd = -1;
4146 s->listen_fd = -1;
4147 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004148 s->is_listen = is_listen;
4149 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004150 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004151 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004152
4153 chr->opaque = s;
4154 chr->chr_write = tcp_chr_write;
4155 chr->chr_sync_read = tcp_chr_sync_read;
4156 chr->chr_close = tcp_chr_close;
4157 chr->get_msgfds = tcp_get_msgfds;
4158 chr->set_msgfds = tcp_set_msgfds;
4159 chr->chr_add_client = tcp_chr_add_client;
4160 chr->chr_add_watch = tcp_chr_add_watch;
4161 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4162 /* be isn't opened until we get a connection */
4163 chr->explicit_be_open = true;
4164
4165 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004166 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4167 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004168
4169 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004170 if (is_telnet) {
4171 s->do_telnetopt = 1;
4172 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004173 } else if (reconnect > 0) {
4174 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004175 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004176
Corey Minyard5008e5b2014-10-08 07:11:55 -05004177 if (s->reconnect_time) {
4178 socket_try_connect(chr);
4179 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4180 g_free(s);
4181 g_free(chr->filename);
4182 g_free(chr);
4183 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004184 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004185
4186 if (is_listen && is_waitconnect) {
4187 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4188 chr->filename);
4189 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4190 qemu_set_nonblock(s->listen_fd);
4191 }
4192
4193 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004194}
4195
Lei Li08d0ab32013-05-20 14:51:03 +08004196static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4197 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004198{
4199 int fd;
4200
Lei Li08d0ab32013-05-20 14:51:03 +08004201 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004202 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004203 return NULL;
4204 }
4205 return qemu_chr_open_udp_fd(fd);
4206}
4207
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004208ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4209 Error **errp)
4210{
4211 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004212 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004213
4214 chr = qemu_chr_find(id);
4215 if (chr) {
4216 error_setg(errp, "Chardev '%s' already exists", id);
4217 g_free(ret);
4218 return NULL;
4219 }
4220
4221 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004222 case CHARDEV_BACKEND_KIND_FILE:
4223 chr = qmp_chardev_open_file(backend->file, errp);
4224 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004225 case CHARDEV_BACKEND_KIND_SERIAL:
4226 chr = qmp_chardev_open_serial(backend->serial, errp);
4227 break;
4228 case CHARDEV_BACKEND_KIND_PARALLEL:
4229 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004230 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004231 case CHARDEV_BACKEND_KIND_PIPE:
4232 chr = qemu_chr_open_pipe(backend->pipe);
4233 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004234 case CHARDEV_BACKEND_KIND_SOCKET:
4235 chr = qmp_chardev_open_socket(backend->socket, errp);
4236 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004237 case CHARDEV_BACKEND_KIND_UDP:
4238 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004239 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004240#ifdef HAVE_CHARDEV_TTY
4241 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004242 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004243 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004244#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004245 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004246 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004247 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004248 case CHARDEV_BACKEND_KIND_MUX:
4249 base = qemu_chr_find(backend->mux->chardev);
4250 if (base == NULL) {
4251 error_setg(errp, "mux: base chardev %s not found",
4252 backend->mux->chardev);
4253 break;
4254 }
4255 chr = qemu_chr_open_mux(base);
4256 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004257 case CHARDEV_BACKEND_KIND_MSMOUSE:
4258 chr = qemu_chr_open_msmouse();
4259 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004260#ifdef CONFIG_BRLAPI
4261 case CHARDEV_BACKEND_KIND_BRAILLE:
4262 chr = chr_baum_init();
4263 break;
4264#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004265 case CHARDEV_BACKEND_KIND_TESTDEV:
4266 chr = chr_testdev_init();
4267 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004268 case CHARDEV_BACKEND_KIND_STDIO:
4269 chr = qemu_chr_open_stdio(backend->stdio);
4270 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004271#ifdef _WIN32
4272 case CHARDEV_BACKEND_KIND_CONSOLE:
4273 chr = qemu_chr_open_win_con();
4274 break;
4275#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004276#ifdef CONFIG_SPICE
4277 case CHARDEV_BACKEND_KIND_SPICEVMC:
4278 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4279 break;
4280 case CHARDEV_BACKEND_KIND_SPICEPORT:
4281 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4282 break;
4283#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004284 case CHARDEV_BACKEND_KIND_VC:
4285 chr = vc_init(backend->vc);
4286 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004287 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004288 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004289 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004290 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004291 default:
4292 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4293 break;
4294 }
4295
Markus Armbruster3894c782014-05-19 18:57:36 +02004296 /*
4297 * Character backend open hasn't been fully converted to the Error
4298 * API. Some opens fail without setting an error. Set a generic
4299 * error then.
4300 * TODO full conversion to Error API
4301 */
4302 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004303 error_setg(errp, "Failed to create chardev");
4304 }
4305 if (chr) {
4306 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004307 chr->avail_connections =
4308 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004309 if (!chr->filename) {
4310 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4311 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004312 if (!chr->explicit_be_open) {
4313 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4314 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004315 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4316 return ret;
4317 } else {
4318 g_free(ret);
4319 return NULL;
4320 }
4321}
4322
4323void qmp_chardev_remove(const char *id, Error **errp)
4324{
4325 CharDriverState *chr;
4326
4327 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004328 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004329 error_setg(errp, "Chardev '%s' not found", id);
4330 return;
4331 }
4332 if (chr->chr_can_read || chr->chr_read ||
4333 chr->chr_event || chr->handler_opaque) {
4334 error_setg(errp, "Chardev '%s' is busy", id);
4335 return;
4336 }
4337 qemu_chr_delete(chr);
4338}
Anthony Liguorid654f342013-03-05 23:21:28 +05304339
4340static void register_types(void)
4341{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004342 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4343 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4344 qemu_chr_parse_socket);
4345 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4346 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4347 qemu_chr_parse_ringbuf);
4348 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4349 qemu_chr_parse_file_out);
4350 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4351 qemu_chr_parse_stdio);
4352 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4353 qemu_chr_parse_serial);
4354 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4355 qemu_chr_parse_serial);
4356 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4357 qemu_chr_parse_parallel);
4358 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4359 qemu_chr_parse_parallel);
4360 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4361 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4362 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4363 qemu_chr_parse_pipe);
4364 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004365 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004366 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4367 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004368 /* this must be done after machine init, since we register FEs with muxes
4369 * as part of realize functions like serial_isa_realizefn when -nographic
4370 * is specified
4371 */
4372 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304373}
4374
4375type_init(register_types);