blob: ef84b5368160edcb710a41ad0b3085597c057978 [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;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530976 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000977} FDCharDriver;
978
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200979/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000980static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
981{
982 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530983
Anthony Liguori684a0962013-03-29 11:39:50 -0500984 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530985}
986
987static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
988{
989 CharDriverState *chr = opaque;
990 FDCharDriver *s = chr->opaque;
991 int len;
992 uint8_t buf[READ_BUF_LEN];
993 GIOStatus status;
994 gsize bytes_read;
995
996 len = sizeof(buf);
997 if (len > s->max_size) {
998 len = s->max_size;
999 }
1000 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001001 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301002 }
1003
1004 status = g_io_channel_read_chars(chan, (gchar *)buf,
1005 len, &bytes_read, NULL);
1006 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301007 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301008 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1009 return FALSE;
1010 }
1011 if (status == G_IO_STATUS_NORMAL) {
1012 qemu_chr_be_write(chr, buf, bytes_read);
1013 }
1014
1015 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001016}
1017
1018static int fd_chr_read_poll(void *opaque)
1019{
1020 CharDriverState *chr = opaque;
1021 FDCharDriver *s = chr->opaque;
1022
Anthony Liguori909cda12011-08-15 11:17:31 -05001023 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001024 return s->max_size;
1025}
1026
Anthony Liguori23673ca2013-03-05 23:21:23 +05301027static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1028{
1029 FDCharDriver *s = chr->opaque;
1030 return g_io_create_watch(s->fd_out, cond);
1031}
1032
aliguori6f97dba2008-10-31 18:49:55 +00001033static void fd_chr_update_read_handler(CharDriverState *chr)
1034{
1035 FDCharDriver *s = chr->opaque;
1036
Amit Shah26da70c2013-08-28 15:23:37 +05301037 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301038 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301039 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1040 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001041 }
1042}
1043
1044static void fd_chr_close(struct CharDriverState *chr)
1045{
1046 FDCharDriver *s = chr->opaque;
1047
Amit Shah26da70c2013-08-28 15:23:37 +05301048 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301049 if (s->fd_in) {
1050 g_io_channel_unref(s->fd_in);
1051 }
1052 if (s->fd_out) {
1053 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001054 }
1055
Anthony Liguori7267c092011-08-20 22:09:37 -05001056 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001057 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001058}
1059
1060/* open a character device to a unix fd */
1061static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1062{
1063 CharDriverState *chr;
1064 FDCharDriver *s;
1065
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001066 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001067 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +05301068 s->fd_in = io_channel_from_fd(fd_in);
1069 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001070 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301071 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001072 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301073 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001074 chr->chr_write = fd_chr_write;
1075 chr->chr_update_read_handler = fd_chr_update_read_handler;
1076 chr->chr_close = fd_chr_close;
1077
aliguori6f97dba2008-10-31 18:49:55 +00001078 return chr;
1079}
1080
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001081static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001082{
1083 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001084 char filename_in[CHR_MAX_FILENAME_SIZE];
1085 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001086 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001087
1088 if (filename == NULL) {
1089 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +01001090 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001091 }
aliguori6f97dba2008-10-31 18:49:55 +00001092
Corey Minyard9f781162014-10-02 11:17:33 -05001093 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1094 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001095 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1096 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001097 if (fd_in < 0 || fd_out < 0) {
1098 if (fd_in >= 0)
1099 close(fd_in);
1100 if (fd_out >= 0)
1101 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001102 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001103 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001104 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001105 }
aliguori6f97dba2008-10-31 18:49:55 +00001106 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001107 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001108}
1109
aliguori6f97dba2008-10-31 18:49:55 +00001110/* init terminal so that we can grab keys */
1111static struct termios oldtty;
1112static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001113static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001114static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001115
1116static void term_exit(void)
1117{
1118 tcsetattr (0, TCSANOW, &oldtty);
1119 fcntl(0, F_SETFL, old_fd0_flags);
1120}
1121
Paolo Bonzinibb002512010-12-23 13:42:50 +01001122static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001123{
1124 struct termios tty;
1125
Paolo Bonzini03693642010-12-23 13:42:49 +01001126 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001127 if (!echo) {
1128 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001129 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001130 tty.c_oflag |= OPOST;
1131 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1132 tty.c_cflag &= ~(CSIZE|PARENB);
1133 tty.c_cflag |= CS8;
1134 tty.c_cc[VMIN] = 1;
1135 tty.c_cc[VTIME] = 0;
1136 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001137 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001138 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001139
1140 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001141}
1142
1143static void qemu_chr_close_stdio(struct CharDriverState *chr)
1144{
1145 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001146 fd_chr_close(chr);
1147}
1148
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001149static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001150{
1151 CharDriverState *chr;
1152
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001153 if (is_daemonized()) {
1154 error_report("cannot use stdio with -daemonize");
1155 return NULL;
1156 }
Li Liuc88930a2014-09-09 19:19:48 +08001157
1158 if (stdio_in_use) {
1159 error_report("cannot use stdio by multiple character devices");
1160 exit(1);
1161 }
1162
1163 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301164 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001165 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001166 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301167 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001168
aliguori6f97dba2008-10-31 18:49:55 +00001169 chr = qemu_chr_open_fd(0, 1);
1170 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001171 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001172 if (opts->has_signal) {
1173 stdio_allow_signal = opts->signal;
1174 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001175 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001176
Markus Armbruster1f514702012-02-07 15:09:08 +01001177 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001178}
1179
aliguori6f97dba2008-10-31 18:49:55 +00001180#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001181 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1182 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001183
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001184#define HAVE_CHARDEV_TTY 1
1185
aliguori6f97dba2008-10-31 18:49:55 +00001186typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301187 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001188 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001189
1190 /* Protected by the CharDriverState chr_write_lock. */
1191 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301192 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001193 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001194} PtyCharDriver;
1195
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001196static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001197static void pty_chr_state(CharDriverState *chr, int connected);
1198
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301199static gboolean pty_chr_timer(gpointer opaque)
1200{
1201 struct CharDriverState *chr = opaque;
1202 PtyCharDriver *s = chr->opaque;
1203
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001204 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001205 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001206 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001207 if (!s->connected) {
1208 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001209 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001210 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001211 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301212 return FALSE;
1213}
1214
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001215/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301216static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1217{
1218 PtyCharDriver *s = chr->opaque;
1219
1220 if (s->timer_tag) {
1221 g_source_remove(s->timer_tag);
1222 s->timer_tag = 0;
1223 }
1224
1225 if (ms == 1000) {
1226 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1227 } else {
1228 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1229 }
1230}
1231
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001232/* Called with chr_write_lock held. */
1233static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001234{
1235 PtyCharDriver *s = chr->opaque;
1236 GPollFD pfd;
1237
1238 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1239 pfd.events = G_IO_OUT;
1240 pfd.revents = 0;
1241 g_poll(&pfd, 1, 0);
1242 if (pfd.revents & G_IO_HUP) {
1243 pty_chr_state(chr, 0);
1244 } else {
1245 pty_chr_state(chr, 1);
1246 }
1247}
1248
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001249static void pty_chr_update_read_handler(CharDriverState *chr)
1250{
1251 qemu_mutex_lock(&chr->chr_write_lock);
1252 pty_chr_update_read_handler_locked(chr);
1253 qemu_mutex_unlock(&chr->chr_write_lock);
1254}
1255
1256/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001257static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1258{
1259 PtyCharDriver *s = chr->opaque;
1260
1261 if (!s->connected) {
1262 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001263 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001264 if (!s->connected) {
1265 return 0;
1266 }
aliguori6f97dba2008-10-31 18:49:55 +00001267 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001268 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001269}
1270
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301271static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1272{
1273 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001274 if (!s->connected) {
1275 return NULL;
1276 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301277 return g_io_create_watch(s->fd, cond);
1278}
1279
aliguori6f97dba2008-10-31 18:49:55 +00001280static int pty_chr_read_poll(void *opaque)
1281{
1282 CharDriverState *chr = opaque;
1283 PtyCharDriver *s = chr->opaque;
1284
Anthony Liguori909cda12011-08-15 11:17:31 -05001285 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001286 return s->read_bytes;
1287}
1288
Anthony Liguori093d3a22013-03-05 23:21:20 +05301289static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001290{
1291 CharDriverState *chr = opaque;
1292 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301293 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301294 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301295 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001296
1297 len = sizeof(buf);
1298 if (len > s->read_bytes)
1299 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001300 if (len == 0) {
1301 return TRUE;
1302 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301303 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1304 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001305 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301306 return FALSE;
1307 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001308 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001309 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001310 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301311 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001312}
1313
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001314static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1315{
1316 CharDriverState *chr = opaque;
1317 PtyCharDriver *s = chr->opaque;
1318
1319 s->open_tag = 0;
1320 qemu_chr_be_generic_open(chr);
1321 return FALSE;
1322}
1323
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001324/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001325static void pty_chr_state(CharDriverState *chr, int connected)
1326{
1327 PtyCharDriver *s = chr->opaque;
1328
1329 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001330 if (s->open_tag) {
1331 g_source_remove(s->open_tag);
1332 s->open_tag = 0;
1333 }
Amit Shah26da70c2013-08-28 15:23:37 +05301334 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001335 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001336 /* (re-)connect poll interval for idle guests: once per second.
1337 * We check more frequently in case the guests sends data to
1338 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301339 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001340 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001341 if (s->timer_tag) {
1342 g_source_remove(s->timer_tag);
1343 s->timer_tag = 0;
1344 }
1345 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001346 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001347 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001348 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001349 }
1350 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301351 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1352 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001353 }
aliguori6f97dba2008-10-31 18:49:55 +00001354 }
1355}
1356
aliguori6f97dba2008-10-31 18:49:55 +00001357static void pty_chr_close(struct CharDriverState *chr)
1358{
1359 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301360 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001361
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001362 qemu_mutex_lock(&chr->chr_write_lock);
1363 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301364 fd = g_io_channel_unix_get_fd(s->fd);
1365 g_io_channel_unref(s->fd);
1366 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301367 if (s->timer_tag) {
1368 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001369 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301370 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001371 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001372 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001373 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001374}
1375
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001376static CharDriverState *qemu_chr_open_pty(const char *id,
1377 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001378{
1379 CharDriverState *chr;
1380 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001381 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001382 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001383
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001384 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1385 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001386 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001387 }
1388
aliguori6f97dba2008-10-31 18:49:55 +00001389 close(slave_fd);
1390
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001391 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001392
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001393 chr->filename = g_strdup_printf("pty:%s", pty_name);
1394 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001395 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001396
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001397 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001398 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001399
1400 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001401 chr->opaque = s;
1402 chr->chr_write = pty_chr_write;
1403 chr->chr_update_read_handler = pty_chr_update_read_handler;
1404 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301405 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001406 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001407
Anthony Liguori093d3a22013-03-05 23:21:20 +05301408 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301409 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001410
Markus Armbruster1f514702012-02-07 15:09:08 +01001411 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001412}
1413
1414static void tty_serial_init(int fd, int speed,
1415 int parity, int data_bits, int stop_bits)
1416{
1417 struct termios tty;
1418 speed_t spd;
1419
1420#if 0
1421 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1422 speed, parity, data_bits, stop_bits);
1423#endif
1424 tcgetattr (fd, &tty);
1425
Stefan Weil45eea132009-10-26 16:10:10 +01001426#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1427 speed = speed * 10 / 11;
1428 do {
1429 check_speed(50);
1430 check_speed(75);
1431 check_speed(110);
1432 check_speed(134);
1433 check_speed(150);
1434 check_speed(200);
1435 check_speed(300);
1436 check_speed(600);
1437 check_speed(1200);
1438 check_speed(1800);
1439 check_speed(2400);
1440 check_speed(4800);
1441 check_speed(9600);
1442 check_speed(19200);
1443 check_speed(38400);
1444 /* Non-Posix values follow. They may be unsupported on some systems. */
1445 check_speed(57600);
1446 check_speed(115200);
1447#ifdef B230400
1448 check_speed(230400);
1449#endif
1450#ifdef B460800
1451 check_speed(460800);
1452#endif
1453#ifdef B500000
1454 check_speed(500000);
1455#endif
1456#ifdef B576000
1457 check_speed(576000);
1458#endif
1459#ifdef B921600
1460 check_speed(921600);
1461#endif
1462#ifdef B1000000
1463 check_speed(1000000);
1464#endif
1465#ifdef B1152000
1466 check_speed(1152000);
1467#endif
1468#ifdef B1500000
1469 check_speed(1500000);
1470#endif
1471#ifdef B2000000
1472 check_speed(2000000);
1473#endif
1474#ifdef B2500000
1475 check_speed(2500000);
1476#endif
1477#ifdef B3000000
1478 check_speed(3000000);
1479#endif
1480#ifdef B3500000
1481 check_speed(3500000);
1482#endif
1483#ifdef B4000000
1484 check_speed(4000000);
1485#endif
aliguori6f97dba2008-10-31 18:49:55 +00001486 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001487 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001488
1489 cfsetispeed(&tty, spd);
1490 cfsetospeed(&tty, spd);
1491
1492 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1493 |INLCR|IGNCR|ICRNL|IXON);
1494 tty.c_oflag |= OPOST;
1495 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1496 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1497 switch(data_bits) {
1498 default:
1499 case 8:
1500 tty.c_cflag |= CS8;
1501 break;
1502 case 7:
1503 tty.c_cflag |= CS7;
1504 break;
1505 case 6:
1506 tty.c_cflag |= CS6;
1507 break;
1508 case 5:
1509 tty.c_cflag |= CS5;
1510 break;
1511 }
1512 switch(parity) {
1513 default:
1514 case 'N':
1515 break;
1516 case 'E':
1517 tty.c_cflag |= PARENB;
1518 break;
1519 case 'O':
1520 tty.c_cflag |= PARENB | PARODD;
1521 break;
1522 }
1523 if (stop_bits == 2)
1524 tty.c_cflag |= CSTOPB;
1525
1526 tcsetattr (fd, TCSANOW, &tty);
1527}
1528
1529static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1530{
1531 FDCharDriver *s = chr->opaque;
1532
1533 switch(cmd) {
1534 case CHR_IOCTL_SERIAL_SET_PARAMS:
1535 {
1536 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301537 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1538 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001539 ssp->data_bits, ssp->stop_bits);
1540 }
1541 break;
1542 case CHR_IOCTL_SERIAL_SET_BREAK:
1543 {
1544 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301545 if (enable) {
1546 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1547 }
aliguori6f97dba2008-10-31 18:49:55 +00001548 }
1549 break;
1550 case CHR_IOCTL_SERIAL_GET_TIOCM:
1551 {
1552 int sarg = 0;
1553 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301554 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001555 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001556 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001557 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001558 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001559 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001560 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001561 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001562 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001563 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001564 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001565 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001566 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001567 *targ |= CHR_TIOCM_RTS;
1568 }
1569 break;
1570 case CHR_IOCTL_SERIAL_SET_TIOCM:
1571 {
1572 int sarg = *(int *)arg;
1573 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301574 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001575 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1576 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1577 if (sarg & CHR_TIOCM_CTS)
1578 targ |= TIOCM_CTS;
1579 if (sarg & CHR_TIOCM_CAR)
1580 targ |= TIOCM_CAR;
1581 if (sarg & CHR_TIOCM_DSR)
1582 targ |= TIOCM_DSR;
1583 if (sarg & CHR_TIOCM_RI)
1584 targ |= TIOCM_RI;
1585 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001586 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001587 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001588 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301589 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001590 }
1591 break;
1592 default:
1593 return -ENOTSUP;
1594 }
1595 return 0;
1596}
1597
David Ahern4266a132010-02-10 18:27:17 -07001598static void qemu_chr_close_tty(CharDriverState *chr)
1599{
1600 FDCharDriver *s = chr->opaque;
1601 int fd = -1;
1602
1603 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301604 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001605 }
1606
1607 fd_chr_close(chr);
1608
1609 if (fd >= 0) {
1610 close(fd);
1611 }
1612}
1613
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001614static CharDriverState *qemu_chr_open_tty_fd(int fd)
1615{
1616 CharDriverState *chr;
1617
1618 tty_serial_init(fd, 115200, 'N', 8, 1);
1619 chr = qemu_chr_open_fd(fd, fd);
1620 chr->chr_ioctl = tty_serial_ioctl;
1621 chr->chr_close = qemu_chr_close_tty;
1622 return chr;
1623}
aliguori6f97dba2008-10-31 18:49:55 +00001624#endif /* __linux__ || __sun__ */
1625
1626#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001627
1628#define HAVE_CHARDEV_PARPORT 1
1629
aliguori6f97dba2008-10-31 18:49:55 +00001630typedef struct {
1631 int fd;
1632 int mode;
1633} ParallelCharDriver;
1634
1635static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1636{
1637 if (s->mode != mode) {
1638 int m = mode;
1639 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1640 return 0;
1641 s->mode = mode;
1642 }
1643 return 1;
1644}
1645
1646static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1647{
1648 ParallelCharDriver *drv = chr->opaque;
1649 int fd = drv->fd;
1650 uint8_t b;
1651
1652 switch(cmd) {
1653 case CHR_IOCTL_PP_READ_DATA:
1654 if (ioctl(fd, PPRDATA, &b) < 0)
1655 return -ENOTSUP;
1656 *(uint8_t *)arg = b;
1657 break;
1658 case CHR_IOCTL_PP_WRITE_DATA:
1659 b = *(uint8_t *)arg;
1660 if (ioctl(fd, PPWDATA, &b) < 0)
1661 return -ENOTSUP;
1662 break;
1663 case CHR_IOCTL_PP_READ_CONTROL:
1664 if (ioctl(fd, PPRCONTROL, &b) < 0)
1665 return -ENOTSUP;
1666 /* Linux gives only the lowest bits, and no way to know data
1667 direction! For better compatibility set the fixed upper
1668 bits. */
1669 *(uint8_t *)arg = b | 0xc0;
1670 break;
1671 case CHR_IOCTL_PP_WRITE_CONTROL:
1672 b = *(uint8_t *)arg;
1673 if (ioctl(fd, PPWCONTROL, &b) < 0)
1674 return -ENOTSUP;
1675 break;
1676 case CHR_IOCTL_PP_READ_STATUS:
1677 if (ioctl(fd, PPRSTATUS, &b) < 0)
1678 return -ENOTSUP;
1679 *(uint8_t *)arg = b;
1680 break;
1681 case CHR_IOCTL_PP_DATA_DIR:
1682 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1683 return -ENOTSUP;
1684 break;
1685 case CHR_IOCTL_PP_EPP_READ_ADDR:
1686 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1687 struct ParallelIOArg *parg = arg;
1688 int n = read(fd, parg->buffer, parg->count);
1689 if (n != parg->count) {
1690 return -EIO;
1691 }
1692 }
1693 break;
1694 case CHR_IOCTL_PP_EPP_READ:
1695 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1696 struct ParallelIOArg *parg = arg;
1697 int n = read(fd, parg->buffer, parg->count);
1698 if (n != parg->count) {
1699 return -EIO;
1700 }
1701 }
1702 break;
1703 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1704 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1705 struct ParallelIOArg *parg = arg;
1706 int n = write(fd, parg->buffer, parg->count);
1707 if (n != parg->count) {
1708 return -EIO;
1709 }
1710 }
1711 break;
1712 case CHR_IOCTL_PP_EPP_WRITE:
1713 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1714 struct ParallelIOArg *parg = arg;
1715 int n = write(fd, parg->buffer, parg->count);
1716 if (n != parg->count) {
1717 return -EIO;
1718 }
1719 }
1720 break;
1721 default:
1722 return -ENOTSUP;
1723 }
1724 return 0;
1725}
1726
1727static void pp_close(CharDriverState *chr)
1728{
1729 ParallelCharDriver *drv = chr->opaque;
1730 int fd = drv->fd;
1731
1732 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1733 ioctl(fd, PPRELEASE);
1734 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001735 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001736 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001737}
1738
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001739static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001740{
1741 CharDriverState *chr;
1742 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001743
1744 if (ioctl(fd, PPCLAIM) < 0) {
1745 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001746 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001747 }
1748
Anthony Liguori7267c092011-08-20 22:09:37 -05001749 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001750 drv->fd = fd;
1751 drv->mode = IEEE1284_MODE_COMPAT;
1752
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001753 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001754 chr->chr_write = null_chr_write;
1755 chr->chr_ioctl = pp_ioctl;
1756 chr->chr_close = pp_close;
1757 chr->opaque = drv;
1758
Markus Armbruster1f514702012-02-07 15:09:08 +01001759 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001760}
1761#endif /* __linux__ */
1762
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001763#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001764
1765#define HAVE_CHARDEV_PARPORT 1
1766
blueswir16972f932008-11-22 20:49:12 +00001767static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1768{
Stefan Weile0efb992011-02-23 19:09:16 +01001769 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001770 uint8_t b;
1771
1772 switch(cmd) {
1773 case CHR_IOCTL_PP_READ_DATA:
1774 if (ioctl(fd, PPIGDATA, &b) < 0)
1775 return -ENOTSUP;
1776 *(uint8_t *)arg = b;
1777 break;
1778 case CHR_IOCTL_PP_WRITE_DATA:
1779 b = *(uint8_t *)arg;
1780 if (ioctl(fd, PPISDATA, &b) < 0)
1781 return -ENOTSUP;
1782 break;
1783 case CHR_IOCTL_PP_READ_CONTROL:
1784 if (ioctl(fd, PPIGCTRL, &b) < 0)
1785 return -ENOTSUP;
1786 *(uint8_t *)arg = b;
1787 break;
1788 case CHR_IOCTL_PP_WRITE_CONTROL:
1789 b = *(uint8_t *)arg;
1790 if (ioctl(fd, PPISCTRL, &b) < 0)
1791 return -ENOTSUP;
1792 break;
1793 case CHR_IOCTL_PP_READ_STATUS:
1794 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1795 return -ENOTSUP;
1796 *(uint8_t *)arg = b;
1797 break;
1798 default:
1799 return -ENOTSUP;
1800 }
1801 return 0;
1802}
1803
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001804static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001805{
1806 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001807
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001808 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001809 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001810 chr->chr_write = null_chr_write;
1811 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001812 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001813 return chr;
blueswir16972f932008-11-22 20:49:12 +00001814}
1815#endif
1816
aliguori6f97dba2008-10-31 18:49:55 +00001817#else /* _WIN32 */
1818
1819typedef struct {
1820 int max_size;
1821 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001822 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001823 BOOL fpipe;
1824 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001825
1826 /* Protected by the CharDriverState chr_write_lock. */
1827 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001828} WinCharState;
1829
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001830typedef struct {
1831 HANDLE hStdIn;
1832 HANDLE hInputReadyEvent;
1833 HANDLE hInputDoneEvent;
1834 HANDLE hInputThread;
1835 uint8_t win_stdio_buf;
1836} WinStdioCharState;
1837
aliguori6f97dba2008-10-31 18:49:55 +00001838#define NSENDBUF 2048
1839#define NRECVBUF 2048
1840#define MAXCONNECT 1
1841#define NTIMEOUT 5000
1842
1843static int win_chr_poll(void *opaque);
1844static int win_chr_pipe_poll(void *opaque);
1845
1846static void win_chr_close(CharDriverState *chr)
1847{
1848 WinCharState *s = chr->opaque;
1849
1850 if (s->hsend) {
1851 CloseHandle(s->hsend);
1852 s->hsend = NULL;
1853 }
1854 if (s->hrecv) {
1855 CloseHandle(s->hrecv);
1856 s->hrecv = NULL;
1857 }
1858 if (s->hcom) {
1859 CloseHandle(s->hcom);
1860 s->hcom = NULL;
1861 }
1862 if (s->fpipe)
1863 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1864 else
1865 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301866
Hans de Goedea425d232011-11-19 10:22:43 +01001867 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001868}
1869
1870static int win_chr_init(CharDriverState *chr, const char *filename)
1871{
1872 WinCharState *s = chr->opaque;
1873 COMMCONFIG comcfg;
1874 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1875 COMSTAT comstat;
1876 DWORD size;
1877 DWORD err;
1878
1879 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1880 if (!s->hsend) {
1881 fprintf(stderr, "Failed CreateEvent\n");
1882 goto fail;
1883 }
1884 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1885 if (!s->hrecv) {
1886 fprintf(stderr, "Failed CreateEvent\n");
1887 goto fail;
1888 }
1889
1890 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1891 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1892 if (s->hcom == INVALID_HANDLE_VALUE) {
1893 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1894 s->hcom = NULL;
1895 goto fail;
1896 }
1897
1898 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1899 fprintf(stderr, "Failed SetupComm\n");
1900 goto fail;
1901 }
1902
1903 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1904 size = sizeof(COMMCONFIG);
1905 GetDefaultCommConfig(filename, &comcfg, &size);
1906 comcfg.dcb.DCBlength = sizeof(DCB);
1907 CommConfigDialog(filename, NULL, &comcfg);
1908
1909 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1910 fprintf(stderr, "Failed SetCommState\n");
1911 goto fail;
1912 }
1913
1914 if (!SetCommMask(s->hcom, EV_ERR)) {
1915 fprintf(stderr, "Failed SetCommMask\n");
1916 goto fail;
1917 }
1918
1919 cto.ReadIntervalTimeout = MAXDWORD;
1920 if (!SetCommTimeouts(s->hcom, &cto)) {
1921 fprintf(stderr, "Failed SetCommTimeouts\n");
1922 goto fail;
1923 }
1924
1925 if (!ClearCommError(s->hcom, &err, &comstat)) {
1926 fprintf(stderr, "Failed ClearCommError\n");
1927 goto fail;
1928 }
1929 qemu_add_polling_cb(win_chr_poll, chr);
1930 return 0;
1931
1932 fail:
1933 win_chr_close(chr);
1934 return -1;
1935}
1936
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001937/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001938static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1939{
1940 WinCharState *s = chr->opaque;
1941 DWORD len, ret, size, err;
1942
1943 len = len1;
1944 ZeroMemory(&s->osend, sizeof(s->osend));
1945 s->osend.hEvent = s->hsend;
1946 while (len > 0) {
1947 if (s->hsend)
1948 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1949 else
1950 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1951 if (!ret) {
1952 err = GetLastError();
1953 if (err == ERROR_IO_PENDING) {
1954 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1955 if (ret) {
1956 buf += size;
1957 len -= size;
1958 } else {
1959 break;
1960 }
1961 } else {
1962 break;
1963 }
1964 } else {
1965 buf += size;
1966 len -= size;
1967 }
1968 }
1969 return len1 - len;
1970}
1971
1972static int win_chr_read_poll(CharDriverState *chr)
1973{
1974 WinCharState *s = chr->opaque;
1975
Anthony Liguori909cda12011-08-15 11:17:31 -05001976 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001977 return s->max_size;
1978}
1979
1980static void win_chr_readfile(CharDriverState *chr)
1981{
1982 WinCharState *s = chr->opaque;
1983 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301984 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001985 DWORD size;
1986
1987 ZeroMemory(&s->orecv, sizeof(s->orecv));
1988 s->orecv.hEvent = s->hrecv;
1989 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1990 if (!ret) {
1991 err = GetLastError();
1992 if (err == ERROR_IO_PENDING) {
1993 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1994 }
1995 }
1996
1997 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001998 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001999 }
2000}
2001
2002static void win_chr_read(CharDriverState *chr)
2003{
2004 WinCharState *s = chr->opaque;
2005
2006 if (s->len > s->max_size)
2007 s->len = s->max_size;
2008 if (s->len == 0)
2009 return;
2010
2011 win_chr_readfile(chr);
2012}
2013
2014static int win_chr_poll(void *opaque)
2015{
2016 CharDriverState *chr = opaque;
2017 WinCharState *s = chr->opaque;
2018 COMSTAT status;
2019 DWORD comerr;
2020
2021 ClearCommError(s->hcom, &comerr, &status);
2022 if (status.cbInQue > 0) {
2023 s->len = status.cbInQue;
2024 win_chr_read_poll(chr);
2025 win_chr_read(chr);
2026 return 1;
2027 }
2028 return 0;
2029}
2030
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01002031static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00002032{
2033 CharDriverState *chr;
2034 WinCharState *s;
2035
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002036 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002037 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002038 chr->opaque = s;
2039 chr->chr_write = win_chr_write;
2040 chr->chr_close = win_chr_close;
2041
2042 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002043 g_free(s);
2044 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002045 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002046 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002047 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002048}
2049
2050static int win_chr_pipe_poll(void *opaque)
2051{
2052 CharDriverState *chr = opaque;
2053 WinCharState *s = chr->opaque;
2054 DWORD size;
2055
2056 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2057 if (size > 0) {
2058 s->len = size;
2059 win_chr_read_poll(chr);
2060 win_chr_read(chr);
2061 return 1;
2062 }
2063 return 0;
2064}
2065
2066static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2067{
2068 WinCharState *s = chr->opaque;
2069 OVERLAPPED ov;
2070 int ret;
2071 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002072 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002073
2074 s->fpipe = TRUE;
2075
2076 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2077 if (!s->hsend) {
2078 fprintf(stderr, "Failed CreateEvent\n");
2079 goto fail;
2080 }
2081 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2082 if (!s->hrecv) {
2083 fprintf(stderr, "Failed CreateEvent\n");
2084 goto fail;
2085 }
2086
2087 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2088 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2089 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2090 PIPE_WAIT,
2091 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2092 if (s->hcom == INVALID_HANDLE_VALUE) {
2093 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2094 s->hcom = NULL;
2095 goto fail;
2096 }
2097
2098 ZeroMemory(&ov, sizeof(ov));
2099 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2100 ret = ConnectNamedPipe(s->hcom, &ov);
2101 if (ret) {
2102 fprintf(stderr, "Failed ConnectNamedPipe\n");
2103 goto fail;
2104 }
2105
2106 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2107 if (!ret) {
2108 fprintf(stderr, "Failed GetOverlappedResult\n");
2109 if (ov.hEvent) {
2110 CloseHandle(ov.hEvent);
2111 ov.hEvent = NULL;
2112 }
2113 goto fail;
2114 }
2115
2116 if (ov.hEvent) {
2117 CloseHandle(ov.hEvent);
2118 ov.hEvent = NULL;
2119 }
2120 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2121 return 0;
2122
2123 fail:
2124 win_chr_close(chr);
2125 return -1;
2126}
2127
2128
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002129static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002130{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002131 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002132 CharDriverState *chr;
2133 WinCharState *s;
2134
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002135 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002136 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002137 chr->opaque = s;
2138 chr->chr_write = win_chr_write;
2139 chr->chr_close = win_chr_close;
2140
2141 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002142 g_free(s);
2143 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002144 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002145 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002146 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002147}
2148
Markus Armbruster1f514702012-02-07 15:09:08 +01002149static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002150{
2151 CharDriverState *chr;
2152 WinCharState *s;
2153
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002154 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002155 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002156 s->hcom = fd_out;
2157 chr->opaque = s;
2158 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002159 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002160}
2161
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002162static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002163{
Markus Armbruster1f514702012-02-07 15:09:08 +01002164 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002165}
2166
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002167static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2168{
2169 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2170 DWORD dwSize;
2171 int len1;
2172
2173 len1 = len;
2174
2175 while (len1 > 0) {
2176 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2177 break;
2178 }
2179 buf += dwSize;
2180 len1 -= dwSize;
2181 }
2182
2183 return len - len1;
2184}
2185
2186static void win_stdio_wait_func(void *opaque)
2187{
2188 CharDriverState *chr = opaque;
2189 WinStdioCharState *stdio = chr->opaque;
2190 INPUT_RECORD buf[4];
2191 int ret;
2192 DWORD dwSize;
2193 int i;
2194
Stefan Weildff74242013-12-07 14:48:04 +01002195 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002196
2197 if (!ret) {
2198 /* Avoid error storm */
2199 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2200 return;
2201 }
2202
2203 for (i = 0; i < dwSize; i++) {
2204 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2205
2206 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2207 int j;
2208 if (kev->uChar.AsciiChar != 0) {
2209 for (j = 0; j < kev->wRepeatCount; j++) {
2210 if (qemu_chr_be_can_write(chr)) {
2211 uint8_t c = kev->uChar.AsciiChar;
2212 qemu_chr_be_write(chr, &c, 1);
2213 }
2214 }
2215 }
2216 }
2217 }
2218}
2219
2220static DWORD WINAPI win_stdio_thread(LPVOID param)
2221{
2222 CharDriverState *chr = param;
2223 WinStdioCharState *stdio = chr->opaque;
2224 int ret;
2225 DWORD dwSize;
2226
2227 while (1) {
2228
2229 /* Wait for one byte */
2230 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2231
2232 /* Exit in case of error, continue if nothing read */
2233 if (!ret) {
2234 break;
2235 }
2236 if (!dwSize) {
2237 continue;
2238 }
2239
2240 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2241 if (stdio->win_stdio_buf == '\r') {
2242 continue;
2243 }
2244
2245 /* Signal the main thread and wait until the byte was eaten */
2246 if (!SetEvent(stdio->hInputReadyEvent)) {
2247 break;
2248 }
2249 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2250 != WAIT_OBJECT_0) {
2251 break;
2252 }
2253 }
2254
2255 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2256 return 0;
2257}
2258
2259static void win_stdio_thread_wait_func(void *opaque)
2260{
2261 CharDriverState *chr = opaque;
2262 WinStdioCharState *stdio = chr->opaque;
2263
2264 if (qemu_chr_be_can_write(chr)) {
2265 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2266 }
2267
2268 SetEvent(stdio->hInputDoneEvent);
2269}
2270
2271static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2272{
2273 WinStdioCharState *stdio = chr->opaque;
2274 DWORD dwMode = 0;
2275
2276 GetConsoleMode(stdio->hStdIn, &dwMode);
2277
2278 if (echo) {
2279 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2280 } else {
2281 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2282 }
2283}
2284
2285static void win_stdio_close(CharDriverState *chr)
2286{
2287 WinStdioCharState *stdio = chr->opaque;
2288
2289 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2290 CloseHandle(stdio->hInputReadyEvent);
2291 }
2292 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2293 CloseHandle(stdio->hInputDoneEvent);
2294 }
2295 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2296 TerminateThread(stdio->hInputThread, 0);
2297 }
2298
2299 g_free(chr->opaque);
2300 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002301}
2302
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002303static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002304{
2305 CharDriverState *chr;
2306 WinStdioCharState *stdio;
2307 DWORD dwMode;
2308 int is_console = 0;
2309
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002310 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002311 stdio = g_malloc0(sizeof(WinStdioCharState));
2312
2313 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2314 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2315 fprintf(stderr, "cannot open stdio: invalid handle\n");
2316 exit(1);
2317 }
2318
2319 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2320
2321 chr->opaque = stdio;
2322 chr->chr_write = win_stdio_write;
2323 chr->chr_close = win_stdio_close;
2324
Anthony Liguoried7a1542013-03-05 23:21:17 +05302325 if (is_console) {
2326 if (qemu_add_wait_object(stdio->hStdIn,
2327 win_stdio_wait_func, chr)) {
2328 fprintf(stderr, "qemu_add_wait_object: failed\n");
2329 }
2330 } else {
2331 DWORD dwId;
2332
2333 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2334 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2335 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2336 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002337
Anthony Liguoried7a1542013-03-05 23:21:17 +05302338 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2339 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2340 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2341 fprintf(stderr, "cannot create stdio thread or event\n");
2342 exit(1);
2343 }
2344 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2345 win_stdio_thread_wait_func, chr)) {
2346 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002347 }
2348 }
2349
2350 dwMode |= ENABLE_LINE_INPUT;
2351
Anthony Liguoried7a1542013-03-05 23:21:17 +05302352 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002353 /* set the terminal in raw mode */
2354 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2355 dwMode |= ENABLE_PROCESSED_INPUT;
2356 }
2357
2358 SetConsoleMode(stdio->hStdIn, dwMode);
2359
2360 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2361 qemu_chr_fe_set_echo(chr, false);
2362
Markus Armbruster1f514702012-02-07 15:09:08 +01002363 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002364}
aliguori6f97dba2008-10-31 18:49:55 +00002365#endif /* !_WIN32 */
2366
Anthony Liguori5ab82112013-03-05 23:21:31 +05302367
aliguori6f97dba2008-10-31 18:49:55 +00002368/***********************************************************/
2369/* UDP Net console */
2370
2371typedef struct {
2372 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302373 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302374 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002375 int bufcnt;
2376 int bufptr;
2377 int max_size;
2378} NetCharDriver;
2379
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002380/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002381static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2382{
2383 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302384 gsize bytes_written;
2385 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002386
Anthony Liguori76a96442013-03-05 23:21:21 +05302387 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2388 if (status == G_IO_STATUS_EOF) {
2389 return 0;
2390 } else if (status != G_IO_STATUS_NORMAL) {
2391 return -1;
2392 }
2393
2394 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002395}
2396
2397static int udp_chr_read_poll(void *opaque)
2398{
2399 CharDriverState *chr = opaque;
2400 NetCharDriver *s = chr->opaque;
2401
Anthony Liguori909cda12011-08-15 11:17:31 -05002402 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002403
2404 /* If there were any stray characters in the queue process them
2405 * first
2406 */
2407 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002408 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002409 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002410 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002411 }
2412 return s->max_size;
2413}
2414
Anthony Liguori76a96442013-03-05 23:21:21 +05302415static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002416{
2417 CharDriverState *chr = opaque;
2418 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302419 gsize bytes_read = 0;
2420 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002421
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002422 if (s->max_size == 0) {
2423 return TRUE;
2424 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302425 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2426 &bytes_read, NULL);
2427 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002428 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302429 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302430 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302431 return FALSE;
2432 }
aliguori6f97dba2008-10-31 18:49:55 +00002433
2434 s->bufptr = 0;
2435 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002436 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002437 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002438 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002439 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302440
2441 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002442}
2443
2444static void udp_chr_update_read_handler(CharDriverState *chr)
2445{
2446 NetCharDriver *s = chr->opaque;
2447
Amit Shah26da70c2013-08-28 15:23:37 +05302448 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302449 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302450 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2451 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002452 }
2453}
2454
aliguori819f56b2009-03-28 17:58:14 +00002455static void udp_chr_close(CharDriverState *chr)
2456{
2457 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302458
2459 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302460 if (s->chan) {
2461 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002462 closesocket(s->fd);
2463 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002464 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002465 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002466}
2467
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002468static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002469{
2470 CharDriverState *chr = NULL;
2471 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002472
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002473 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002474 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002475
aliguori6f97dba2008-10-31 18:49:55 +00002476 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302477 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002478 s->bufcnt = 0;
2479 s->bufptr = 0;
2480 chr->opaque = s;
2481 chr->chr_write = udp_chr_write;
2482 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002483 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002484 /* be isn't opened until we get a connection */
2485 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002486 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002487}
aliguori6f97dba2008-10-31 18:49:55 +00002488
aliguori6f97dba2008-10-31 18:49:55 +00002489/***********************************************************/
2490/* TCP Net console */
2491
2492typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302493
2494 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302495 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002496 int fd, listen_fd;
2497 int connected;
2498 int max_size;
2499 int do_telnetopt;
2500 int do_nodelay;
2501 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002502 int *read_msgfds;
2503 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002504 int *write_msgfds;
2505 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002506
2507 SocketAddress *addr;
2508 bool is_listen;
2509 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002510
2511 guint reconnect_timer;
2512 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002513 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002514} TCPCharDriver;
2515
Corey Minyard5dd1f022014-10-02 11:17:37 -05002516static gboolean socket_reconnect_timeout(gpointer opaque);
2517
2518static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2519{
2520 TCPCharDriver *s = chr->opaque;
2521 assert(s->connected == 0);
2522 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2523 socket_reconnect_timeout, chr);
2524}
2525
Corey Minyard5008e5b2014-10-08 07:11:55 -05002526static void check_report_connect_error(CharDriverState *chr,
2527 Error *err)
2528{
2529 TCPCharDriver *s = chr->opaque;
2530
2531 if (!s->connect_err_reported) {
2532 error_report("Unable to connect character device %s: %s",
2533 chr->label, error_get_pretty(err));
2534 s->connect_err_reported = true;
2535 }
2536 qemu_chr_socket_restart_timer(chr);
2537}
2538
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302539static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002540
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002541#ifndef _WIN32
2542static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2543{
2544 TCPCharDriver *s = chr->opaque;
2545 struct msghdr msgh;
2546 struct iovec iov;
2547 int r;
2548
2549 size_t fd_size = s->write_msgfds_num * sizeof(int);
2550 char control[CMSG_SPACE(fd_size)];
2551 struct cmsghdr *cmsg;
2552
2553 memset(&msgh, 0, sizeof(msgh));
2554 memset(control, 0, sizeof(control));
2555
2556 /* set the payload */
2557 iov.iov_base = (uint8_t *) buf;
2558 iov.iov_len = len;
2559
2560 msgh.msg_iov = &iov;
2561 msgh.msg_iovlen = 1;
2562
2563 msgh.msg_control = control;
2564 msgh.msg_controllen = sizeof(control);
2565
2566 cmsg = CMSG_FIRSTHDR(&msgh);
2567
2568 cmsg->cmsg_len = CMSG_LEN(fd_size);
2569 cmsg->cmsg_level = SOL_SOCKET;
2570 cmsg->cmsg_type = SCM_RIGHTS;
2571 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2572
2573 do {
2574 r = sendmsg(s->fd, &msgh, 0);
2575 } while (r < 0 && errno == EINTR);
2576
2577 /* free the written msgfds, no matter what */
2578 if (s->write_msgfds_num) {
2579 g_free(s->write_msgfds);
2580 s->write_msgfds = 0;
2581 s->write_msgfds_num = 0;
2582 }
2583
2584 return r;
2585}
2586#endif
2587
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002588/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002589static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2590{
2591 TCPCharDriver *s = chr->opaque;
2592 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002593#ifndef _WIN32
2594 if (s->is_unix && s->write_msgfds_num) {
2595 return unix_send_msgfds(chr, buf, len);
2596 } else
2597#endif
2598 {
2599 return io_channel_send(s->chan, buf, len);
2600 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002601 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002602 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002603 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002604 }
2605}
2606
2607static int tcp_chr_read_poll(void *opaque)
2608{
2609 CharDriverState *chr = opaque;
2610 TCPCharDriver *s = chr->opaque;
2611 if (!s->connected)
2612 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002613 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002614 return s->max_size;
2615}
2616
2617#define IAC 255
2618#define IAC_BREAK 243
2619static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2620 TCPCharDriver *s,
2621 uint8_t *buf, int *size)
2622{
2623 /* Handle any telnet client's basic IAC options to satisfy char by
2624 * char mode with no echo. All IAC options will be removed from
2625 * the buf and the do_telnetopt variable will be used to track the
2626 * state of the width of the IAC information.
2627 *
2628 * IAC commands come in sets of 3 bytes with the exception of the
2629 * "IAC BREAK" command and the double IAC.
2630 */
2631
2632 int i;
2633 int j = 0;
2634
2635 for (i = 0; i < *size; i++) {
2636 if (s->do_telnetopt > 1) {
2637 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2638 /* Double IAC means send an IAC */
2639 if (j != i)
2640 buf[j] = buf[i];
2641 j++;
2642 s->do_telnetopt = 1;
2643 } else {
2644 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2645 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002646 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002647 s->do_telnetopt++;
2648 }
2649 s->do_telnetopt++;
2650 }
2651 if (s->do_telnetopt >= 4) {
2652 s->do_telnetopt = 1;
2653 }
2654 } else {
2655 if ((unsigned char)buf[i] == IAC) {
2656 s->do_telnetopt = 2;
2657 } else {
2658 if (j != i)
2659 buf[j] = buf[i];
2660 j++;
2661 }
2662 }
2663 }
2664 *size = j;
2665}
2666
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002667static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002668{
2669 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002670 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2671
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002672 assert(num <= TCP_MAX_FDS);
2673
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002674 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002675 int i;
2676
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002677 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2678
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002679 /* Close unused fds */
2680 for (i = to_copy; i < s->read_msgfds_num; i++) {
2681 close(s->read_msgfds[i]);
2682 }
2683
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002684 g_free(s->read_msgfds);
2685 s->read_msgfds = 0;
2686 s->read_msgfds_num = 0;
2687 }
2688
2689 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002690}
2691
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002692static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2693{
2694 TCPCharDriver *s = chr->opaque;
2695
2696 /* clear old pending fd array */
2697 if (s->write_msgfds) {
2698 g_free(s->write_msgfds);
2699 }
2700
2701 if (num) {
2702 s->write_msgfds = g_malloc(num * sizeof(int));
2703 memcpy(s->write_msgfds, fds, num * sizeof(int));
2704 }
2705
2706 s->write_msgfds_num = num;
2707
2708 return 0;
2709}
2710
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002711#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002712static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2713{
2714 TCPCharDriver *s = chr->opaque;
2715 struct cmsghdr *cmsg;
2716
2717 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002718 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002719
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002720 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002721 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002722 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002723 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002724 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002725
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002726 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2727
2728 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002729 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002730 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002731
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002732 /* close and clean read_msgfds */
2733 for (i = 0; i < s->read_msgfds_num; i++) {
2734 close(s->read_msgfds[i]);
2735 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002736
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002737 if (s->read_msgfds_num) {
2738 g_free(s->read_msgfds);
2739 }
2740
2741 s->read_msgfds_num = fd_size / sizeof(int);
2742 s->read_msgfds = g_malloc(fd_size);
2743 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2744
2745 for (i = 0; i < s->read_msgfds_num; i++) {
2746 int fd = s->read_msgfds[i];
2747 if (fd < 0) {
2748 continue;
2749 }
2750
2751 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2752 qemu_set_block(fd);
2753
2754 #ifndef MSG_CMSG_CLOEXEC
2755 qemu_set_cloexec(fd);
2756 #endif
2757 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002758 }
2759}
2760
Mark McLoughlin9977c892009-07-22 09:11:38 +01002761static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2762{
2763 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002764 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002765 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002766 union {
2767 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002768 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002769 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002770 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002771 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002772
2773 iov[0].iov_base = buf;
2774 iov[0].iov_len = len;
2775
2776 msg.msg_iov = iov;
2777 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002778 msg.msg_control = &msg_control;
2779 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002780
Corey Bryant06138652012-08-14 16:43:42 -04002781#ifdef MSG_CMSG_CLOEXEC
2782 flags |= MSG_CMSG_CLOEXEC;
2783#endif
2784 ret = recvmsg(s->fd, &msg, flags);
2785 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002786 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002787 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002788
2789 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002790}
2791#else
2792static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2793{
2794 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002795 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002796}
2797#endif
2798
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302799static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2800{
2801 TCPCharDriver *s = chr->opaque;
2802 return g_io_create_watch(s->chan, cond);
2803}
2804
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002805static void tcp_chr_disconnect(CharDriverState *chr)
2806{
2807 TCPCharDriver *s = chr->opaque;
2808
2809 s->connected = 0;
2810 if (s->listen_chan) {
2811 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2812 tcp_chr_accept, chr);
2813 }
2814 remove_fd_in_watch(chr);
2815 g_io_channel_unref(s->chan);
2816 s->chan = NULL;
2817 closesocket(s->fd);
2818 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002819 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2820 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002821 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002822 if (s->reconnect_time) {
2823 qemu_chr_socket_restart_timer(chr);
2824 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002825}
2826
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302827static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002828{
2829 CharDriverState *chr = opaque;
2830 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302831 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002832 int len, size;
2833
Kirill Batuzov812c1052014-07-01 15:52:32 +04002834 if (cond & G_IO_HUP) {
2835 /* connection closed */
2836 tcp_chr_disconnect(chr);
2837 return TRUE;
2838 }
2839
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302840 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002841 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302842 }
aliguori6f97dba2008-10-31 18:49:55 +00002843 len = sizeof(buf);
2844 if (len > s->max_size)
2845 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002846 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002847 if (size == 0) {
2848 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002849 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002850 } else if (size > 0) {
2851 if (s->do_telnetopt)
2852 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2853 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002854 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002855 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302856
2857 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002858}
2859
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002860static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2861{
2862 TCPCharDriver *s = chr->opaque;
2863 int size;
2864
2865 if (!s->connected) {
2866 return 0;
2867 }
2868
2869 size = tcp_chr_recv(chr, (void *) buf, len);
2870 if (size == 0) {
2871 /* connection closed */
2872 tcp_chr_disconnect(chr);
2873 }
2874
2875 return size;
2876}
2877
Blue Swirl68c18d12010-08-15 09:46:24 +00002878#ifndef _WIN32
2879CharDriverState *qemu_chr_open_eventfd(int eventfd)
2880{
David Marchande9d21c42014-06-11 17:25:16 +02002881 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2882
2883 if (chr) {
2884 chr->avail_connections = 1;
2885 }
2886
2887 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002888}
Blue Swirl68c18d12010-08-15 09:46:24 +00002889#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002890
aliguori6f97dba2008-10-31 18:49:55 +00002891static void tcp_chr_connect(void *opaque)
2892{
2893 CharDriverState *chr = opaque;
2894 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002895 struct sockaddr_storage ss, ps;
2896 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002897
2898 memset(&ss, 0, ss_len);
2899 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2900 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2901 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002902 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2903 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2904 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002905 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002906 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2907 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002908 s->is_listen, s->is_telnet);
2909 }
aliguori6f97dba2008-10-31 18:49:55 +00002910
2911 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302912 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302913 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2914 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002915 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002916 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002917}
2918
Gal Hammerac1b84d2014-02-25 12:12:35 +02002919static void tcp_chr_update_read_handler(CharDriverState *chr)
2920{
2921 TCPCharDriver *s = chr->opaque;
2922
2923 remove_fd_in_watch(chr);
2924 if (s->chan) {
2925 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2926 tcp_chr_read, chr);
2927 }
2928}
2929
aliguori6f97dba2008-10-31 18:49:55 +00002930#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2931static void tcp_chr_telnet_init(int fd)
2932{
2933 char buf[3];
2934 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2935 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2936 send(fd, (char *)buf, 3, 0);
2937 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2938 send(fd, (char *)buf, 3, 0);
2939 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2940 send(fd, (char *)buf, 3, 0);
2941 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2942 send(fd, (char *)buf, 3, 0);
2943}
2944
Daniel P. Berrange13661082011-06-23 13:31:42 +01002945static int tcp_chr_add_client(CharDriverState *chr, int fd)
2946{
2947 TCPCharDriver *s = chr->opaque;
2948 if (s->fd != -1)
2949 return -1;
2950
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002951 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002952 if (s->do_nodelay)
2953 socket_set_nodelay(fd);
2954 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302955 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002956 if (s->listen_tag) {
2957 g_source_remove(s->listen_tag);
2958 s->listen_tag = 0;
2959 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002960 tcp_chr_connect(chr);
2961
2962 return 0;
2963}
2964
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302965static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002966{
2967 CharDriverState *chr = opaque;
2968 TCPCharDriver *s = chr->opaque;
2969 struct sockaddr_in saddr;
2970#ifndef _WIN32
2971 struct sockaddr_un uaddr;
2972#endif
2973 struct sockaddr *addr;
2974 socklen_t len;
2975 int fd;
2976
2977 for(;;) {
2978#ifndef _WIN32
2979 if (s->is_unix) {
2980 len = sizeof(uaddr);
2981 addr = (struct sockaddr *)&uaddr;
2982 } else
2983#endif
2984 {
2985 len = sizeof(saddr);
2986 addr = (struct sockaddr *)&saddr;
2987 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002988 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002989 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002990 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302991 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002992 } else if (fd >= 0) {
2993 if (s->do_telnetopt)
2994 tcp_chr_telnet_init(fd);
2995 break;
2996 }
2997 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002998 if (tcp_chr_add_client(chr, fd) < 0)
2999 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303000
3001 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003002}
3003
3004static void tcp_chr_close(CharDriverState *chr)
3005{
3006 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003007 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003008
Corey Minyard5dd1f022014-10-02 11:17:37 -05003009 if (s->reconnect_timer) {
3010 g_source_remove(s->reconnect_timer);
3011 s->reconnect_timer = 0;
3012 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003013 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003014 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303015 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303016 if (s->chan) {
3017 g_io_channel_unref(s->chan);
3018 }
aliguori6f97dba2008-10-31 18:49:55 +00003019 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003020 }
3021 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303022 if (s->listen_tag) {
3023 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003024 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303025 }
3026 if (s->listen_chan) {
3027 g_io_channel_unref(s->listen_chan);
3028 }
aliguori6f97dba2008-10-31 18:49:55 +00003029 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003030 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003031 if (s->read_msgfds_num) {
3032 for (i = 0; i < s->read_msgfds_num; i++) {
3033 close(s->read_msgfds[i]);
3034 }
3035 g_free(s->read_msgfds);
3036 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003037 if (s->write_msgfds_num) {
3038 g_free(s->write_msgfds);
3039 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003040 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003041 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003042}
3043
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003044static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003045{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003046 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003047
Corey Minyardcfb429c2014-10-02 11:17:35 -05003048 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003049 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303050 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003051 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3052 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003053 } else {
3054 s->connected = 1;
3055 s->fd = fd;
3056 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303057 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003058 tcp_chr_connect(chr);
3059 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003060}
3061
Corey Minyard51795022014-10-08 07:11:56 -05003062static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003063{
3064 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003065 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003066
3067 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003068 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003069 return;
3070 }
3071
Corey Minyard5008e5b2014-10-08 07:11:55 -05003072 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003073 qemu_chr_finish_socket_connection(chr, fd);
3074}
3075
Corey Minyardcfb429c2014-10-02 11:17:35 -05003076static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003077{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003078 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003079 int fd;
3080
Corey Minyardcfb429c2014-10-02 11:17:35 -05003081 if (s->is_listen) {
3082 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003083 } else if (s->reconnect_time) {
3084 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3085 return fd >= 0;
3086 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003087 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003088 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003089 if (fd < 0) {
3090 return false;
3091 }
3092
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003093 qemu_chr_finish_socket_connection(chr, fd);
3094 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003095}
3096
Lei Li51767e72013-01-25 00:03:19 +08003097/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003098/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003099
3100typedef struct {
3101 size_t size;
3102 size_t prod;
3103 size_t cons;
3104 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003105} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003106
Markus Armbruster3949e592013-02-06 21:27:24 +01003107static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003108{
Markus Armbruster3949e592013-02-06 21:27:24 +01003109 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003110
Markus Armbruster5c230102013-02-06 21:27:23 +01003111 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003112}
3113
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003114/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003115static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003116{
Markus Armbruster3949e592013-02-06 21:27:24 +01003117 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003118 int i;
3119
3120 if (!buf || (len < 0)) {
3121 return -1;
3122 }
3123
3124 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003125 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3126 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003127 d->cons = d->prod - d->size;
3128 }
3129 }
3130
3131 return 0;
3132}
3133
Markus Armbruster3949e592013-02-06 21:27:24 +01003134static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003135{
Markus Armbruster3949e592013-02-06 21:27:24 +01003136 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003137 int i;
3138
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003139 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003140 for (i = 0; i < len && d->cons != d->prod; i++) {
3141 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003142 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003143 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003144
3145 return i;
3146}
3147
Markus Armbruster3949e592013-02-06 21:27:24 +01003148static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003149{
Markus Armbruster3949e592013-02-06 21:27:24 +01003150 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003151
3152 g_free(d->cbuf);
3153 g_free(d);
3154 chr->opaque = NULL;
3155}
3156
Markus Armbruster4f573782013-07-26 16:44:32 +02003157static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3158 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003159{
3160 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003161 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003162
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003163 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003164 d = g_malloc(sizeof(*d));
3165
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003166 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003167
3168 /* The size must be power of 2 */
3169 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003170 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003171 goto fail;
3172 }
3173
3174 d->prod = 0;
3175 d->cons = 0;
3176 d->cbuf = g_malloc0(d->size);
3177
3178 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003179 chr->chr_write = ringbuf_chr_write;
3180 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003181
3182 return chr;
3183
3184fail:
3185 g_free(d);
3186 g_free(chr);
3187 return NULL;
3188}
3189
Hani Benhabiles8e597772014-05-27 23:39:30 +01003190bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003191{
Markus Armbruster3949e592013-02-06 21:27:24 +01003192 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003193}
3194
Markus Armbruster3949e592013-02-06 21:27:24 +01003195void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003196 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003197 Error **errp)
3198{
3199 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003200 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003201 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003202 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003203
3204 chr = qemu_chr_find(device);
3205 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003206 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003207 return;
3208 }
3209
Markus Armbruster3949e592013-02-06 21:27:24 +01003210 if (!chr_is_ringbuf(chr)) {
3211 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003212 return;
3213 }
3214
Lei Li1f590cf2013-01-25 00:03:20 +08003215 if (has_format && (format == DATA_FORMAT_BASE64)) {
3216 write_data = g_base64_decode(data, &write_count);
3217 } else {
3218 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003219 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003220 }
3221
Markus Armbruster3949e592013-02-06 21:27:24 +01003222 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003223
Markus Armbruster13289fb2013-02-06 21:27:18 +01003224 if (write_data != (uint8_t *)data) {
3225 g_free((void *)write_data);
3226 }
3227
Lei Li1f590cf2013-01-25 00:03:20 +08003228 if (ret < 0) {
3229 error_setg(errp, "Failed to write to device %s", device);
3230 return;
3231 }
3232}
3233
Markus Armbruster3949e592013-02-06 21:27:24 +01003234char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003235 bool has_format, enum DataFormat format,
3236 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003237{
3238 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003239 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003240 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003241 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003242
3243 chr = qemu_chr_find(device);
3244 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003245 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003246 return NULL;
3247 }
3248
Markus Armbruster3949e592013-02-06 21:27:24 +01003249 if (!chr_is_ringbuf(chr)) {
3250 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003251 return NULL;
3252 }
3253
3254 if (size <= 0) {
3255 error_setg(errp, "size must be greater than zero");
3256 return NULL;
3257 }
3258
Markus Armbruster3949e592013-02-06 21:27:24 +01003259 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003260 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003261 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003262
Markus Armbruster3949e592013-02-06 21:27:24 +01003263 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003264
3265 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003266 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003267 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003268 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003269 /*
3270 * FIXME should read only complete, valid UTF-8 characters up
3271 * to @size bytes. Invalid sequences should be replaced by a
3272 * suitable replacement character. Except when (and only
3273 * when) ring buffer lost characters since last read, initial
3274 * continuation characters should be dropped.
3275 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003276 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003277 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003278 }
3279
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003280 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003281}
3282
Gerd Hoffmann33521632009-12-08 13:11:49 +01003283QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003284{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003285 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003286 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003287 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003288 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003289 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003290
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003291 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003292 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003293 qerror_report_err(local_err);
3294 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003295 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003296 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003297
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003298 if (strstart(filename, "mon:", &p)) {
3299 filename = p;
3300 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003301 if (strcmp(filename, "stdio") == 0) {
3302 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3303 * but pass it to the guest. Handle this only for compat syntax,
3304 * for -chardev syntax we have special option for this.
3305 * This is what -nographic did, redirecting+muxing serial+monitor
3306 * to stdio causing Ctrl+C to be passed to guest. */
3307 qemu_opt_set(opts, "signal", "off");
3308 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003309 }
3310
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003311 if (strcmp(filename, "null") == 0 ||
3312 strcmp(filename, "pty") == 0 ||
3313 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003314 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003315 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003316 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003317 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003318 return opts;
3319 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003320 if (strstart(filename, "vc", &p)) {
3321 qemu_opt_set(opts, "backend", "vc");
3322 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003323 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003324 /* pixels */
3325 qemu_opt_set(opts, "width", width);
3326 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003327 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003328 /* chars */
3329 qemu_opt_set(opts, "cols", width);
3330 qemu_opt_set(opts, "rows", height);
3331 } else {
3332 goto fail;
3333 }
3334 }
3335 return opts;
3336 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003337 if (strcmp(filename, "con:") == 0) {
3338 qemu_opt_set(opts, "backend", "console");
3339 return opts;
3340 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003341 if (strstart(filename, "COM", NULL)) {
3342 qemu_opt_set(opts, "backend", "serial");
3343 qemu_opt_set(opts, "path", filename);
3344 return opts;
3345 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003346 if (strstart(filename, "file:", &p)) {
3347 qemu_opt_set(opts, "backend", "file");
3348 qemu_opt_set(opts, "path", p);
3349 return opts;
3350 }
3351 if (strstart(filename, "pipe:", &p)) {
3352 qemu_opt_set(opts, "backend", "pipe");
3353 qemu_opt_set(opts, "path", p);
3354 return opts;
3355 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003356 if (strstart(filename, "tcp:", &p) ||
3357 strstart(filename, "telnet:", &p)) {
3358 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3359 host[0] = 0;
3360 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3361 goto fail;
3362 }
3363 qemu_opt_set(opts, "backend", "socket");
3364 qemu_opt_set(opts, "host", host);
3365 qemu_opt_set(opts, "port", port);
3366 if (p[pos] == ',') {
3367 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3368 goto fail;
3369 }
3370 if (strstart(filename, "telnet:", &p))
3371 qemu_opt_set(opts, "telnet", "on");
3372 return opts;
3373 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003374 if (strstart(filename, "udp:", &p)) {
3375 qemu_opt_set(opts, "backend", "udp");
3376 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3377 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003378 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003379 goto fail;
3380 }
3381 }
3382 qemu_opt_set(opts, "host", host);
3383 qemu_opt_set(opts, "port", port);
3384 if (p[pos] == '@') {
3385 p += pos + 1;
3386 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3387 host[0] = 0;
3388 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003389 goto fail;
3390 }
3391 }
3392 qemu_opt_set(opts, "localaddr", host);
3393 qemu_opt_set(opts, "localport", port);
3394 }
3395 return opts;
3396 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003397 if (strstart(filename, "unix:", &p)) {
3398 qemu_opt_set(opts, "backend", "socket");
3399 if (qemu_opts_do_parse(opts, p, "path") != 0)
3400 goto fail;
3401 return opts;
3402 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003403 if (strstart(filename, "/dev/parport", NULL) ||
3404 strstart(filename, "/dev/ppi", NULL)) {
3405 qemu_opt_set(opts, "backend", "parport");
3406 qemu_opt_set(opts, "path", filename);
3407 return opts;
3408 }
3409 if (strstart(filename, "/dev/", NULL)) {
3410 qemu_opt_set(opts, "backend", "tty");
3411 qemu_opt_set(opts, "path", filename);
3412 return opts;
3413 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003414
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003415fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003416 qemu_opts_del(opts);
3417 return NULL;
3418}
3419
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003420static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3421 Error **errp)
3422{
3423 const char *path = qemu_opt_get(opts, "path");
3424
3425 if (path == NULL) {
3426 error_setg(errp, "chardev: file: no filename given");
3427 return;
3428 }
3429 backend->file = g_new0(ChardevFile, 1);
3430 backend->file->out = g_strdup(path);
3431}
3432
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003433static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3434 Error **errp)
3435{
3436 backend->stdio = g_new0(ChardevStdio, 1);
3437 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003438 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003439}
3440
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003441static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3442 Error **errp)
3443{
3444 const char *device = qemu_opt_get(opts, "path");
3445
3446 if (device == NULL) {
3447 error_setg(errp, "chardev: serial/tty: no device path given");
3448 return;
3449 }
3450 backend->serial = g_new0(ChardevHostdev, 1);
3451 backend->serial->device = g_strdup(device);
3452}
3453
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003454static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3455 Error **errp)
3456{
3457 const char *device = qemu_opt_get(opts, "path");
3458
3459 if (device == NULL) {
3460 error_setg(errp, "chardev: parallel: no device path given");
3461 return;
3462 }
3463 backend->parallel = g_new0(ChardevHostdev, 1);
3464 backend->parallel->device = g_strdup(device);
3465}
3466
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003467static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3468 Error **errp)
3469{
3470 const char *device = qemu_opt_get(opts, "path");
3471
3472 if (device == NULL) {
3473 error_setg(errp, "chardev: pipe: no device path given");
3474 return;
3475 }
3476 backend->pipe = g_new0(ChardevHostdev, 1);
3477 backend->pipe->device = g_strdup(device);
3478}
3479
Markus Armbruster4f573782013-07-26 16:44:32 +02003480static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3481 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003482{
3483 int val;
3484
Markus Armbruster3a1da422013-07-26 16:44:34 +02003485 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003486
Markus Armbruster0f953052013-06-27 16:22:07 +02003487 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003488 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003489 backend->ringbuf->has_size = true;
3490 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003491 }
3492}
3493
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003494static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3495 Error **errp)
3496{
3497 const char *chardev = qemu_opt_get(opts, "chardev");
3498
3499 if (chardev == NULL) {
3500 error_setg(errp, "chardev: mux: no chardev given");
3501 return;
3502 }
3503 backend->mux = g_new0(ChardevMux, 1);
3504 backend->mux->chardev = g_strdup(chardev);
3505}
3506
Peter Maydelldafd3252014-09-02 11:24:13 +01003507static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3508 Error **errp)
3509{
3510 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3511 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3512 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3513 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003514 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003515 const char *path = qemu_opt_get(opts, "path");
3516 const char *host = qemu_opt_get(opts, "host");
3517 const char *port = qemu_opt_get(opts, "port");
3518 SocketAddress *addr;
3519
3520 if (!path) {
3521 if (!host) {
3522 error_setg(errp, "chardev: socket: no host given");
3523 return;
3524 }
3525 if (!port) {
3526 error_setg(errp, "chardev: socket: no port given");
3527 return;
3528 }
3529 }
3530
3531 backend->socket = g_new0(ChardevSocket, 1);
3532
3533 backend->socket->has_nodelay = true;
3534 backend->socket->nodelay = do_nodelay;
3535 backend->socket->has_server = true;
3536 backend->socket->server = is_listen;
3537 backend->socket->has_telnet = true;
3538 backend->socket->telnet = is_telnet;
3539 backend->socket->has_wait = true;
3540 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003541 backend->socket->has_reconnect = true;
3542 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003543
3544 addr = g_new0(SocketAddress, 1);
3545 if (path) {
3546 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3547 addr->q_unix = g_new0(UnixSocketAddress, 1);
3548 addr->q_unix->path = g_strdup(path);
3549 } else {
3550 addr->kind = SOCKET_ADDRESS_KIND_INET;
3551 addr->inet = g_new0(InetSocketAddress, 1);
3552 addr->inet->host = g_strdup(host);
3553 addr->inet->port = g_strdup(port);
3554 addr->inet->has_to = qemu_opt_get(opts, "to");
3555 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3556 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3557 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3558 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3559 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3560 }
3561 backend->socket->addr = addr;
3562}
3563
Peter Maydell90a14bf2014-09-02 11:24:15 +01003564static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3565 Error **errp)
3566{
3567 const char *host = qemu_opt_get(opts, "host");
3568 const char *port = qemu_opt_get(opts, "port");
3569 const char *localaddr = qemu_opt_get(opts, "localaddr");
3570 const char *localport = qemu_opt_get(opts, "localport");
3571 bool has_local = false;
3572 SocketAddress *addr;
3573
3574 if (host == NULL || strlen(host) == 0) {
3575 host = "localhost";
3576 }
3577 if (port == NULL || strlen(port) == 0) {
3578 error_setg(errp, "chardev: udp: remote port not specified");
3579 return;
3580 }
3581 if (localport == NULL || strlen(localport) == 0) {
3582 localport = "0";
3583 } else {
3584 has_local = true;
3585 }
3586 if (localaddr == NULL || strlen(localaddr) == 0) {
3587 localaddr = "";
3588 } else {
3589 has_local = true;
3590 }
3591
3592 backend->udp = g_new0(ChardevUdp, 1);
3593
3594 addr = g_new0(SocketAddress, 1);
3595 addr->kind = SOCKET_ADDRESS_KIND_INET;
3596 addr->inet = g_new0(InetSocketAddress, 1);
3597 addr->inet->host = g_strdup(host);
3598 addr->inet->port = g_strdup(port);
3599 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3600 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3601 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3602 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3603 backend->udp->remote = addr;
3604
3605 if (has_local) {
3606 backend->udp->has_local = true;
3607 addr = g_new0(SocketAddress, 1);
3608 addr->kind = SOCKET_ADDRESS_KIND_INET;
3609 addr->inet = g_new0(InetSocketAddress, 1);
3610 addr->inet->host = g_strdup(localaddr);
3611 addr->inet->port = g_strdup(localport);
3612 backend->udp->local = addr;
3613 }
3614}
3615
Anthony Liguorid654f342013-03-05 23:21:28 +05303616typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003617 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003618 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003619 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303620} CharDriver;
3621
3622static GSList *backends;
3623
Peter Maydelle4d50d42014-09-02 11:24:17 +01003624void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003625 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3626{
3627 CharDriver *s;
3628
3629 s = g_malloc0(sizeof(*s));
3630 s->name = g_strdup(name);
3631 s->kind = kind;
3632 s->parse = parse;
3633
3634 backends = g_slist_append(backends, s);
3635}
3636
Anthony Liguorif69554b2011-08-15 11:17:37 -05003637CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003638 void (*init)(struct CharDriverState *s),
3639 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003640{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003641 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303642 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003643 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303644 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003645 ChardevReturn *ret = NULL;
3646 ChardevBackend *backend;
3647 const char *id = qemu_opts_id(opts);
3648 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003649
Peter Maydella61ae7f2014-09-02 11:24:16 +01003650 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003651 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003652 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003653 }
3654
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003655 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003656 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003657 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003658 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003659 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303660 for (i = backends; i; i = i->next) {
3661 cd = i->data;
3662
3663 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003664 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303665 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003666 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303667 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003668 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003669 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003670 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003671 }
3672
Peter Maydella61ae7f2014-09-02 11:24:16 +01003673 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003674
3675 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003676 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003677 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003678
3679 chr = NULL;
3680 backend->kind = cd->kind;
3681 if (cd->parse) {
3682 cd->parse(opts, backend, &local_err);
3683 if (local_err) {
3684 error_propagate(errp, local_err);
3685 goto qapi_out;
3686 }
3687 }
3688 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3689 if (!ret) {
3690 goto qapi_out;
3691 }
3692
3693 if (bid) {
3694 qapi_free_ChardevBackend(backend);
3695 qapi_free_ChardevReturn(ret);
3696 backend = g_new0(ChardevBackend, 1);
3697 backend->mux = g_new0(ChardevMux, 1);
3698 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3699 backend->mux->chardev = g_strdup(bid);
3700 ret = qmp_chardev_add(id, backend, errp);
3701 if (!ret) {
3702 chr = qemu_chr_find(bid);
3703 qemu_chr_delete(chr);
3704 chr = NULL;
3705 goto qapi_out;
3706 }
3707 }
3708
3709 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003710 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003711
3712qapi_out:
3713 qapi_free_ChardevBackend(backend);
3714 qapi_free_ChardevReturn(ret);
3715 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003716 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003717
3718err:
3719 qemu_opts_del(opts);
3720 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003721}
3722
Anthony Liguori27143a42011-08-15 11:17:36 -05003723CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003724{
3725 const char *p;
3726 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003727 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003728 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003729
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003730 if (strstart(filename, "chardev:", &p)) {
3731 return qemu_chr_find(p);
3732 }
3733
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003734 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003735 if (!opts)
3736 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003737
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003738 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003739 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003740 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003741 error_free(err);
3742 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003743 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003744 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003745 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003746 }
3747 return chr;
3748}
3749
Anthony Liguori15f31512011-08-15 11:17:35 -05003750void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003751{
3752 if (chr->chr_set_echo) {
3753 chr->chr_set_echo(chr, echo);
3754 }
3755}
3756
Hans de Goede8e25daa2013-03-26 11:07:57 +01003757void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003758{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003759 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003760 return;
3761 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003762 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003763 if (chr->chr_set_fe_open) {
3764 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003765 }
3766}
3767
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003768void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3769{
3770 if (chr->chr_fe_event) {
3771 chr->chr_fe_event(chr, event);
3772 }
3773}
3774
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003775int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3776 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303777{
3778 GSource *src;
3779 guint tag;
3780
3781 if (s->chr_add_watch == NULL) {
3782 return -ENOSYS;
3783 }
3784
3785 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003786 if (!src) {
3787 return -EINVAL;
3788 }
3789
Anthony Liguori23673ca2013-03-05 23:21:23 +05303790 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3791 tag = g_source_attach(src, NULL);
3792 g_source_unref(src);
3793
3794 return tag;
3795}
3796
Hans de Goede44c473d2013-03-27 20:29:39 +01003797int qemu_chr_fe_claim(CharDriverState *s)
3798{
3799 if (s->avail_connections < 1) {
3800 return -1;
3801 }
3802 s->avail_connections--;
3803 return 0;
3804}
3805
3806void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3807{
3808 if (qemu_chr_fe_claim(s) != 0) {
3809 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3810 __func__, s->label);
3811 exit(1);
3812 }
3813}
3814
3815void qemu_chr_fe_release(CharDriverState *s)
3816{
3817 s->avail_connections++;
3818}
3819
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003820void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003821{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003822 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003823 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003824 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003825 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003826 g_free(chr->filename);
3827 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003828 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003829 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003830}
3831
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003832ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003833{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003834 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003835 CharDriverState *chr;
3836
Blue Swirl72cf2d42009-09-12 07:36:22 +00003837 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003838 ChardevInfoList *info = g_malloc0(sizeof(*info));
3839 info->value = g_malloc0(sizeof(*info->value));
3840 info->value->label = g_strdup(chr->label);
3841 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003842 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003843
3844 info->next = chr_list;
3845 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003846 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003847
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003848 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003849}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003850
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003851ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3852{
3853 ChardevBackendInfoList *backend_list = NULL;
3854 CharDriver *c = NULL;
3855 GSList *i = NULL;
3856
3857 for (i = backends; i; i = i->next) {
3858 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3859 c = i->data;
3860 info->value = g_malloc0(sizeof(*info->value));
3861 info->value->name = g_strdup(c->name);
3862
3863 info->next = backend_list;
3864 backend_list = info;
3865 }
3866
3867 return backend_list;
3868}
3869
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003870CharDriverState *qemu_chr_find(const char *name)
3871{
3872 CharDriverState *chr;
3873
Blue Swirl72cf2d42009-09-12 07:36:22 +00003874 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003875 if (strcmp(chr->label, name) != 0)
3876 continue;
3877 return chr;
3878 }
3879 return NULL;
3880}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003881
3882/* Get a character (serial) device interface. */
3883CharDriverState *qemu_char_get_next_serial(void)
3884{
3885 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003886 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003887
3888 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003889
3890 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3891 chr = serial_hds[next_serial++];
3892 qemu_chr_fe_claim_no_fail(chr);
3893 return chr;
3894 }
3895 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003896}
3897
Paolo Bonzini4d454572012-11-26 16:03:42 +01003898QemuOptsList qemu_chardev_opts = {
3899 .name = "chardev",
3900 .implied_opt_name = "backend",
3901 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3902 .desc = {
3903 {
3904 .name = "backend",
3905 .type = QEMU_OPT_STRING,
3906 },{
3907 .name = "path",
3908 .type = QEMU_OPT_STRING,
3909 },{
3910 .name = "host",
3911 .type = QEMU_OPT_STRING,
3912 },{
3913 .name = "port",
3914 .type = QEMU_OPT_STRING,
3915 },{
3916 .name = "localaddr",
3917 .type = QEMU_OPT_STRING,
3918 },{
3919 .name = "localport",
3920 .type = QEMU_OPT_STRING,
3921 },{
3922 .name = "to",
3923 .type = QEMU_OPT_NUMBER,
3924 },{
3925 .name = "ipv4",
3926 .type = QEMU_OPT_BOOL,
3927 },{
3928 .name = "ipv6",
3929 .type = QEMU_OPT_BOOL,
3930 },{
3931 .name = "wait",
3932 .type = QEMU_OPT_BOOL,
3933 },{
3934 .name = "server",
3935 .type = QEMU_OPT_BOOL,
3936 },{
3937 .name = "delay",
3938 .type = QEMU_OPT_BOOL,
3939 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003940 .name = "reconnect",
3941 .type = QEMU_OPT_NUMBER,
3942 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003943 .name = "telnet",
3944 .type = QEMU_OPT_BOOL,
3945 },{
3946 .name = "width",
3947 .type = QEMU_OPT_NUMBER,
3948 },{
3949 .name = "height",
3950 .type = QEMU_OPT_NUMBER,
3951 },{
3952 .name = "cols",
3953 .type = QEMU_OPT_NUMBER,
3954 },{
3955 .name = "rows",
3956 .type = QEMU_OPT_NUMBER,
3957 },{
3958 .name = "mux",
3959 .type = QEMU_OPT_BOOL,
3960 },{
3961 .name = "signal",
3962 .type = QEMU_OPT_BOOL,
3963 },{
3964 .name = "name",
3965 .type = QEMU_OPT_STRING,
3966 },{
3967 .name = "debug",
3968 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003969 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003970 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003971 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003972 },{
3973 .name = "chardev",
3974 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003975 },
3976 { /* end of list */ }
3977 },
3978};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003979
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003980#ifdef _WIN32
3981
3982static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3983{
3984 HANDLE out;
3985
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003986 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003987 error_setg(errp, "input file not supported");
3988 return NULL;
3989 }
3990
3991 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3992 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3993 if (out == INVALID_HANDLE_VALUE) {
3994 error_setg(errp, "open %s failed", file->out);
3995 return NULL;
3996 }
3997 return qemu_chr_open_win_file(out);
3998}
3999
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004000static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4001 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004002{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004003 return qemu_chr_open_win_path(serial->device);
4004}
4005
4006static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4007 Error **errp)
4008{
4009 error_setg(errp, "character device backend type 'parallel' not supported");
4010 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004011}
4012
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004013#else /* WIN32 */
4014
4015static int qmp_chardev_open_file_source(char *src, int flags,
4016 Error **errp)
4017{
4018 int fd = -1;
4019
4020 TFR(fd = qemu_open(src, flags, 0666));
4021 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004022 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004023 }
4024 return fd;
4025}
4026
4027static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4028{
Markus Armbruster5f758362014-05-19 18:57:34 +02004029 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004030
4031 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4032 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004033 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004034 return NULL;
4035 }
4036
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004037 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004038 flags = O_RDONLY;
4039 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004040 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004041 qemu_close(out);
4042 return NULL;
4043 }
4044 }
4045
4046 return qemu_chr_open_fd(in, out);
4047}
4048
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004049static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4050 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004051{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004052#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004053 int fd;
4054
4055 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004056 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004057 return NULL;
4058 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004059 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004060 return qemu_chr_open_tty_fd(fd);
4061#else
4062 error_setg(errp, "character device backend type 'serial' not supported");
4063 return NULL;
4064#endif
4065}
4066
4067static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4068 Error **errp)
4069{
4070#ifdef HAVE_CHARDEV_PARPORT
4071 int fd;
4072
4073 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004074 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004075 return NULL;
4076 }
4077 return qemu_chr_open_pp_fd(fd);
4078#else
4079 error_setg(errp, "character device backend type 'parallel' not supported");
4080 return NULL;
4081#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004082}
4083
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004084#endif /* WIN32 */
4085
Corey Minyard5008e5b2014-10-08 07:11:55 -05004086static void socket_try_connect(CharDriverState *chr)
4087{
4088 Error *err = NULL;
4089
4090 if (!qemu_chr_open_socket_fd(chr, &err)) {
4091 check_report_connect_error(chr, err);
4092 }
4093}
4094
Corey Minyard5dd1f022014-10-02 11:17:37 -05004095static gboolean socket_reconnect_timeout(gpointer opaque)
4096{
4097 CharDriverState *chr = opaque;
4098 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004099
4100 s->reconnect_timer = 0;
4101
4102 if (chr->be_open) {
4103 return false;
4104 }
4105
Corey Minyard5008e5b2014-10-08 07:11:55 -05004106 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004107
4108 return false;
4109}
4110
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004111static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
4112 Error **errp)
4113{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004114 CharDriverState *chr;
4115 TCPCharDriver *s;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004116 SocketAddress *addr = sock->addr;
4117 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4118 bool is_listen = sock->has_server ? sock->server : true;
4119 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4120 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004121 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004122
4123 chr = qemu_chr_alloc();
4124 s = g_malloc0(sizeof(TCPCharDriver));
4125
4126 s->fd = -1;
4127 s->listen_fd = -1;
4128 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004129 s->is_listen = is_listen;
4130 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004131 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004132 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004133
4134 chr->opaque = s;
4135 chr->chr_write = tcp_chr_write;
4136 chr->chr_sync_read = tcp_chr_sync_read;
4137 chr->chr_close = tcp_chr_close;
4138 chr->get_msgfds = tcp_get_msgfds;
4139 chr->set_msgfds = tcp_set_msgfds;
4140 chr->chr_add_client = tcp_chr_add_client;
4141 chr->chr_add_watch = tcp_chr_add_watch;
4142 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4143 /* be isn't opened until we get a connection */
4144 chr->explicit_be_open = true;
4145
4146 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004147 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4148 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004149
4150 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004151 if (is_telnet) {
4152 s->do_telnetopt = 1;
4153 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004154 } else if (reconnect > 0) {
4155 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004156 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004157
Corey Minyard5008e5b2014-10-08 07:11:55 -05004158 if (s->reconnect_time) {
4159 socket_try_connect(chr);
4160 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4161 g_free(s);
4162 g_free(chr->filename);
4163 g_free(chr);
4164 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004165 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004166
4167 if (is_listen && is_waitconnect) {
4168 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4169 chr->filename);
4170 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4171 qemu_set_nonblock(s->listen_fd);
4172 }
4173
4174 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004175}
4176
Lei Li08d0ab32013-05-20 14:51:03 +08004177static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4178 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004179{
4180 int fd;
4181
Lei Li08d0ab32013-05-20 14:51:03 +08004182 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004183 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004184 return NULL;
4185 }
4186 return qemu_chr_open_udp_fd(fd);
4187}
4188
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004189ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4190 Error **errp)
4191{
4192 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004193 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004194
4195 chr = qemu_chr_find(id);
4196 if (chr) {
4197 error_setg(errp, "Chardev '%s' already exists", id);
4198 g_free(ret);
4199 return NULL;
4200 }
4201
4202 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004203 case CHARDEV_BACKEND_KIND_FILE:
4204 chr = qmp_chardev_open_file(backend->file, errp);
4205 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004206 case CHARDEV_BACKEND_KIND_SERIAL:
4207 chr = qmp_chardev_open_serial(backend->serial, errp);
4208 break;
4209 case CHARDEV_BACKEND_KIND_PARALLEL:
4210 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004211 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004212 case CHARDEV_BACKEND_KIND_PIPE:
4213 chr = qemu_chr_open_pipe(backend->pipe);
4214 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004215 case CHARDEV_BACKEND_KIND_SOCKET:
4216 chr = qmp_chardev_open_socket(backend->socket, errp);
4217 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004218 case CHARDEV_BACKEND_KIND_UDP:
4219 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004220 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004221#ifdef HAVE_CHARDEV_TTY
4222 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004223 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004224 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004225#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004226 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004227 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004228 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004229 case CHARDEV_BACKEND_KIND_MUX:
4230 base = qemu_chr_find(backend->mux->chardev);
4231 if (base == NULL) {
4232 error_setg(errp, "mux: base chardev %s not found",
4233 backend->mux->chardev);
4234 break;
4235 }
4236 chr = qemu_chr_open_mux(base);
4237 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004238 case CHARDEV_BACKEND_KIND_MSMOUSE:
4239 chr = qemu_chr_open_msmouse();
4240 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004241#ifdef CONFIG_BRLAPI
4242 case CHARDEV_BACKEND_KIND_BRAILLE:
4243 chr = chr_baum_init();
4244 break;
4245#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004246 case CHARDEV_BACKEND_KIND_TESTDEV:
4247 chr = chr_testdev_init();
4248 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004249 case CHARDEV_BACKEND_KIND_STDIO:
4250 chr = qemu_chr_open_stdio(backend->stdio);
4251 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004252#ifdef _WIN32
4253 case CHARDEV_BACKEND_KIND_CONSOLE:
4254 chr = qemu_chr_open_win_con();
4255 break;
4256#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004257#ifdef CONFIG_SPICE
4258 case CHARDEV_BACKEND_KIND_SPICEVMC:
4259 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4260 break;
4261 case CHARDEV_BACKEND_KIND_SPICEPORT:
4262 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4263 break;
4264#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004265 case CHARDEV_BACKEND_KIND_VC:
4266 chr = vc_init(backend->vc);
4267 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004268 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004269 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004270 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004271 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004272 default:
4273 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4274 break;
4275 }
4276
Markus Armbruster3894c782014-05-19 18:57:36 +02004277 /*
4278 * Character backend open hasn't been fully converted to the Error
4279 * API. Some opens fail without setting an error. Set a generic
4280 * error then.
4281 * TODO full conversion to Error API
4282 */
4283 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004284 error_setg(errp, "Failed to create chardev");
4285 }
4286 if (chr) {
4287 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004288 chr->avail_connections =
4289 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004290 if (!chr->filename) {
4291 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4292 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004293 if (!chr->explicit_be_open) {
4294 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4295 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004296 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4297 return ret;
4298 } else {
4299 g_free(ret);
4300 return NULL;
4301 }
4302}
4303
4304void qmp_chardev_remove(const char *id, Error **errp)
4305{
4306 CharDriverState *chr;
4307
4308 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004309 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004310 error_setg(errp, "Chardev '%s' not found", id);
4311 return;
4312 }
4313 if (chr->chr_can_read || chr->chr_read ||
4314 chr->chr_event || chr->handler_opaque) {
4315 error_setg(errp, "Chardev '%s' is busy", id);
4316 return;
4317 }
4318 qemu_chr_delete(chr);
4319}
Anthony Liguorid654f342013-03-05 23:21:28 +05304320
4321static void register_types(void)
4322{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004323 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4324 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4325 qemu_chr_parse_socket);
4326 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4327 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4328 qemu_chr_parse_ringbuf);
4329 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4330 qemu_chr_parse_file_out);
4331 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4332 qemu_chr_parse_stdio);
4333 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4334 qemu_chr_parse_serial);
4335 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4336 qemu_chr_parse_serial);
4337 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4338 qemu_chr_parse_parallel);
4339 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4340 qemu_chr_parse_parallel);
4341 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4342 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4343 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4344 qemu_chr_parse_pipe);
4345 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004346 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004347 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4348 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004349 /* this must be done after machine init, since we register FEs with muxes
4350 * as part of realize functions like serial_isa_realizefn when -nographic
4351 * is specified
4352 */
4353 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304354}
4355
4356type_init(register_types);