blob: c71805ad8d5740c47fa657e4a54079f2f22917a0 [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
Amit Shah9bd78542009-11-03 19:59:54 +053091
aliguori6f97dba2008-10-31 18:49:55 +000092/***********************************************************/
Corey Minyardcfb429c2014-10-02 11:17:35 -050093/* Socket address helpers */
94static void qapi_copy_SocketAddress(SocketAddress **p_dest,
95 SocketAddress *src)
96{
97 QmpOutputVisitor *qov;
98 QmpInputVisitor *qiv;
99 Visitor *ov, *iv;
100 QObject *obj;
101
102 *p_dest = NULL;
103
104 qov = qmp_output_visitor_new();
105 ov = qmp_output_get_visitor(qov);
106 visit_type_SocketAddress(ov, &src, NULL, &error_abort);
107 obj = qmp_output_get_qobject(qov);
108 qmp_output_visitor_cleanup(qov);
109 if (!obj) {
110 return;
111 }
112
113 qiv = qmp_input_visitor_new(obj);
114 iv = qmp_input_get_visitor(qiv);
115 visit_type_SocketAddress(iv, p_dest, NULL, &error_abort);
116 qmp_input_visitor_cleanup(qiv);
117 qobject_decref(obj);
118}
119
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500120static int SocketAddress_to_str(char *dest, int max_len,
121 const char *prefix, SocketAddress *addr,
122 bool is_listen, bool is_telnet)
123{
124 switch (addr->kind) {
125 case SOCKET_ADDRESS_KIND_INET:
126 return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix,
127 is_telnet ? "telnet" : "tcp", addr->inet->host,
128 addr->inet->port, is_listen ? ",server" : "");
129 break;
130 case SOCKET_ADDRESS_KIND_UNIX:
131 return snprintf(dest, max_len, "%sunix:%s%s", prefix,
132 addr->q_unix->path, is_listen ? ",server" : "");
133 break;
134 case SOCKET_ADDRESS_KIND_FD:
135 return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->fd->str,
136 is_listen ? ",server" : "");
137 break;
138 default:
139 abort();
140 }
141}
142
143static int sockaddr_to_str(char *dest, int max_len,
144 struct sockaddr_storage *ss, socklen_t ss_len,
Corey Minyard01ca5192014-10-02 11:17:38 -0500145 struct sockaddr_storage *ps, socklen_t ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500146 bool is_listen, bool is_telnet)
147{
Corey Minyard01ca5192014-10-02 11:17:38 -0500148 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
149 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500150 const char *left = "", *right = "";
151
152 switch (ss->ss_family) {
153#ifndef _WIN32
154 case AF_UNIX:
155 return snprintf(dest, max_len, "unix:%s%s",
156 ((struct sockaddr_un *)(ss))->sun_path,
157 is_listen ? ",server" : "");
158#endif
159 case AF_INET6:
160 left = "[";
161 right = "]";
162 /* fall through */
163 case AF_INET:
Corey Minyard01ca5192014-10-02 11:17:38 -0500164 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
165 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
166 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
167 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
168 return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500169 is_telnet ? "telnet" : "tcp",
Corey Minyard01ca5192014-10-02 11:17:38 -0500170 left, shost, right, sserv,
171 is_listen ? ",server" : "",
172 left, phost, right, pserv);
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500173
174 default:
175 return snprintf(dest, max_len, "unknown");
176 }
177}
178
Corey Minyardcfb429c2014-10-02 11:17:35 -0500179/***********************************************************/
aliguori6f97dba2008-10-31 18:49:55 +0000180/* character device */
181
Blue Swirl72cf2d42009-09-12 07:36:22 +0000182static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
183 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000184
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200185CharDriverState *qemu_chr_alloc(void)
186{
187 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +0200188 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200189 return chr;
190}
191
Hans de Goedea425d232011-11-19 10:22:43 +0100192void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000193{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200194 /* Keep track if the char device is open */
195 switch (event) {
196 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100197 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200198 break;
199 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100200 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200201 break;
202 }
203
aliguori6f97dba2008-10-31 18:49:55 +0000204 if (!s->chr_event)
205 return;
206 s->chr_event(s->handler_opaque, event);
207}
208
Hans de Goedefee204f2013-03-26 11:07:54 +0100209void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000210{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500211 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000212}
213
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500214int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000215{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200216 int ret;
217
218 qemu_mutex_lock(&s->chr_write_lock);
219 ret = s->chr_write(s, buf, len);
220 qemu_mutex_unlock(&s->chr_write_lock);
221 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000222}
223
Anthony Liguoricd187202013-03-26 10:04:17 -0500224int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
225{
226 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200227 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500228
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200229 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500230 while (offset < len) {
231 do {
232 res = s->chr_write(s, buf + offset, len - offset);
233 if (res == -1 && errno == EAGAIN) {
234 g_usleep(100);
235 }
236 } while (res == -1 && errno == EAGAIN);
237
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200238 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500239 break;
240 }
241
Anthony Liguoricd187202013-03-26 10:04:17 -0500242 offset += res;
243 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200244 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500245
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200246 if (res < 0) {
247 return res;
248 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500249 return offset;
250}
251
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300252int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
253{
254 int offset = 0, counter = 10;
255 int res;
256
257 if (!s->chr_sync_read) {
258 return 0;
259 }
260
261 while (offset < len) {
262 do {
263 res = s->chr_sync_read(s, buf + offset, len - offset);
264 if (res == -1 && errno == EAGAIN) {
265 g_usleep(100);
266 }
267 } while (res == -1 && errno == EAGAIN);
268
269 if (res == 0) {
270 break;
271 }
272
273 if (res < 0) {
274 return res;
275 }
276
277 offset += res;
278
279 if (!counter--) {
280 break;
281 }
282 }
283
284 return offset;
285}
286
Anthony Liguori41084f12011-08-15 11:17:34 -0500287int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000288{
289 if (!s->chr_ioctl)
290 return -ENOTSUP;
291 return s->chr_ioctl(s, cmd, arg);
292}
293
Anthony Liguori909cda12011-08-15 11:17:31 -0500294int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000295{
296 if (!s->chr_can_read)
297 return 0;
298 return s->chr_can_read(s->handler_opaque);
299}
300
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500301void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000302{
Stefan Weilac310732012-04-19 22:27:14 +0200303 if (s->chr_read) {
304 s->chr_read(s->handler_opaque, buf, len);
305 }
aliguori6f97dba2008-10-31 18:49:55 +0000306}
307
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500308int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100309{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300310 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800311 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300312}
313
314int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
315{
316 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100317}
318
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300319int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
320{
321 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
322}
323
Daniel P. Berrange13661082011-06-23 13:31:42 +0100324int qemu_chr_add_client(CharDriverState *s, int fd)
325{
326 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
327}
328
aliguori6f97dba2008-10-31 18:49:55 +0000329void qemu_chr_accept_input(CharDriverState *s)
330{
331 if (s->chr_accept_input)
332 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100333 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000334}
335
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500336void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000337{
Amit Shah9bd78542009-11-03 19:59:54 +0530338 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000339 va_list ap;
340 va_start(ap, fmt);
341 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500342 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000343 va_end(ap);
344}
345
Amit Shah386a5a12013-08-28 15:24:05 +0530346static void remove_fd_in_watch(CharDriverState *chr);
347
aliguori6f97dba2008-10-31 18:49:55 +0000348void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100349 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000350 IOReadHandler *fd_read,
351 IOEventHandler *fd_event,
352 void *opaque)
353{
Hans de Goede19083222013-03-26 11:07:56 +0100354 int fe_open;
355
Amit Shahda7d9982011-04-25 15:18:22 +0530356 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100357 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530358 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100359 } else {
360 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530361 }
aliguori6f97dba2008-10-31 18:49:55 +0000362 s->chr_can_read = fd_can_read;
363 s->chr_read = fd_read;
364 s->chr_event = fd_event;
365 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200366 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000367 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200368
Hans de Goede19083222013-03-26 11:07:56 +0100369 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100370 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100371 }
372
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200373 /* We're connecting to an already opened device, so let's make sure we
374 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100375 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100376 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200377 }
aliguori6f97dba2008-10-31 18:49:55 +0000378}
379
380static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
381{
382 return len;
383}
384
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100385static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000386{
387 CharDriverState *chr;
388
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200389 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000390 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500391 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100392 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000393}
394
395/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000396#define MAX_MUX 4
397#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
398#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
399typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100400 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000401 IOReadHandler *chr_read[MAX_MUX];
402 IOEventHandler *chr_event[MAX_MUX];
403 void *ext_opaque[MAX_MUX];
404 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200405 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000406 int mux_cnt;
407 int term_got_escape;
408 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000409 /* Intermediate input buffer allows to catch escape sequences even if the
410 currently active device is not accepting any input - but only until it
411 is full as well. */
412 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
413 int prod[MAX_MUX];
414 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200415 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200416
417 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200418 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200419 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000420} MuxDriver;
421
422
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200423/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000424static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
425{
426 MuxDriver *d = chr->opaque;
427 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200428 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200429 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000430 } else {
431 int i;
432
433 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200434 for (i = 0; i < len; i++) {
435 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000436 char buf1[64];
437 int64_t ti;
438 int secs;
439
Alex Blighbc72ad62013-08-21 16:03:08 +0100440 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200441 if (d->timestamps_start == -1)
442 d->timestamps_start = ti;
443 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000444 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000445 snprintf(buf1, sizeof(buf1),
446 "[%02d:%02d:%02d.%03d] ",
447 secs / 3600,
448 (secs / 60) % 60,
449 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000450 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200451 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200452 d->linestart = 0;
453 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200454 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200455 if (buf[i] == '\n') {
456 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000457 }
458 }
459 }
460 return ret;
461}
462
463static const char * const mux_help[] = {
464 "% h print this help\n\r",
465 "% x exit emulator\n\r",
466 "% s save disk data back to file (if -snapshot)\n\r",
467 "% t toggle console timestamps\n\r"
468 "% b send break (magic sysrq)\n\r",
469 "% c switch between console and monitor\n\r",
470 "% % sends %\n\r",
471 NULL
472};
473
474int term_escape_char = 0x01; /* ctrl-a is used for escape */
475static void mux_print_help(CharDriverState *chr)
476{
477 int i, j;
478 char ebuf[15] = "Escape-Char";
479 char cbuf[50] = "\n\r";
480
481 if (term_escape_char > 0 && term_escape_char < 26) {
482 snprintf(cbuf, sizeof(cbuf), "\n\r");
483 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
484 } else {
485 snprintf(cbuf, sizeof(cbuf),
486 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
487 term_escape_char);
488 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200489 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000490 for (i = 0; mux_help[i] != NULL; i++) {
491 for (j=0; mux_help[i][j] != '\0'; j++) {
492 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200493 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000494 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200495 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000496 }
497 }
498}
499
aliguori2724b182009-03-05 23:01:47 +0000500static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
501{
502 if (d->chr_event[mux_nr])
503 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
504}
505
aliguori6f97dba2008-10-31 18:49:55 +0000506static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
507{
508 if (d->term_got_escape) {
509 d->term_got_escape = 0;
510 if (ch == term_escape_char)
511 goto send_char;
512 switch(ch) {
513 case '?':
514 case 'h':
515 mux_print_help(chr);
516 break;
517 case 'x':
518 {
519 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200520 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000521 exit(0);
522 break;
523 }
524 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200525 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000526 break;
527 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100528 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000529 break;
530 case 'c':
531 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200532 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
533 d->focus++;
534 if (d->focus >= d->mux_cnt)
535 d->focus = 0;
536 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000537 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200538 case 't':
539 d->timestamps = !d->timestamps;
540 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200541 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200542 break;
aliguori6f97dba2008-10-31 18:49:55 +0000543 }
544 } else if (ch == term_escape_char) {
545 d->term_got_escape = 1;
546 } else {
547 send_char:
548 return 1;
549 }
550 return 0;
551}
552
553static void mux_chr_accept_input(CharDriverState *chr)
554{
aliguori6f97dba2008-10-31 18:49:55 +0000555 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200556 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000557
aliguoria80bf992009-03-05 23:00:02 +0000558 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000559 d->chr_can_read[m] &&
560 d->chr_can_read[m](d->ext_opaque[m])) {
561 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000562 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000563 }
564}
565
566static int mux_chr_can_read(void *opaque)
567{
568 CharDriverState *chr = opaque;
569 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200570 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000571
aliguoria80bf992009-03-05 23:00:02 +0000572 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000573 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000574 if (d->chr_can_read[m])
575 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000576 return 0;
577}
578
579static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
580{
581 CharDriverState *chr = opaque;
582 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200583 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000584 int i;
585
586 mux_chr_accept_input (opaque);
587
588 for(i = 0; i < size; i++)
589 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000590 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000591 d->chr_can_read[m] &&
592 d->chr_can_read[m](d->ext_opaque[m]))
593 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
594 else
aliguoria80bf992009-03-05 23:00:02 +0000595 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000596 }
597}
598
599static void mux_chr_event(void *opaque, int event)
600{
601 CharDriverState *chr = opaque;
602 MuxDriver *d = chr->opaque;
603 int i;
604
605 /* Send the event to all registered listeners */
606 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000607 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000608}
609
610static void mux_chr_update_read_handler(CharDriverState *chr)
611{
612 MuxDriver *d = chr->opaque;
613
614 if (d->mux_cnt >= MAX_MUX) {
615 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
616 return;
617 }
618 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
619 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
620 d->chr_read[d->mux_cnt] = chr->chr_read;
621 d->chr_event[d->mux_cnt] = chr->chr_event;
622 /* Fix up the real driver with mux routines */
623 if (d->mux_cnt == 0) {
624 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
625 mux_chr_event, chr);
626 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200627 if (d->focus != -1) {
628 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200629 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200630 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000631 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200632 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000633}
634
Michael Roth7b7ab182013-07-30 13:04:22 -0500635static bool muxes_realized;
636
637/**
638 * Called after processing of default and command-line-specified
639 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
640 * to a mux chardev. This is done here to ensure that
641 * output/prompts/banners are only displayed for the FE that has
642 * focus when initial command-line processing/machine init is
643 * completed.
644 *
645 * After this point, any new FE attached to any new or existing
646 * mux will receive CHR_EVENT_OPENED notifications for the BE
647 * immediately.
648 */
649static void muxes_realize_done(Notifier *notifier, void *unused)
650{
651 CharDriverState *chr;
652
653 QTAILQ_FOREACH(chr, &chardevs, next) {
654 if (chr->is_mux) {
655 MuxDriver *d = chr->opaque;
656 int i;
657
658 /* send OPENED to all already-attached FEs */
659 for (i = 0; i < d->mux_cnt; i++) {
660 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
661 }
662 /* mark mux as OPENED so any new FEs will immediately receive
663 * OPENED event
664 */
665 qemu_chr_be_generic_open(chr);
666 }
667 }
668 muxes_realized = true;
669}
670
671static Notifier muxes_realize_notify = {
672 .notify = muxes_realize_done,
673};
674
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400675static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
676{
677 MuxDriver *d = s->opaque;
678 return d->drv->chr_add_watch(d->drv, cond);
679}
680
aliguori6f97dba2008-10-31 18:49:55 +0000681static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
682{
683 CharDriverState *chr;
684 MuxDriver *d;
685
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200686 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500687 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000688
689 chr->opaque = d;
690 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200691 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000692 chr->chr_write = mux_chr_write;
693 chr->chr_update_read_handler = mux_chr_update_read_handler;
694 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100695 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100696 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400697 if (drv->chr_add_watch) {
698 chr->chr_add_watch = mux_chr_add_watch;
699 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500700 /* only default to opened state if we've realized the initial
701 * set of muxes
702 */
703 chr->explicit_be_open = muxes_realized ? 0 : 1;
704 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200705
aliguori6f97dba2008-10-31 18:49:55 +0000706 return chr;
707}
708
709
710#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000711int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000712{
713 int ret, len;
714
715 len = len1;
716 while (len > 0) {
717 ret = send(fd, buf, len, 0);
718 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000719 errno = WSAGetLastError();
720 if (errno != WSAEWOULDBLOCK) {
721 return -1;
722 }
723 } else if (ret == 0) {
724 break;
725 } else {
726 buf += ret;
727 len -= ret;
728 }
729 }
730 return len1 - len;
731}
732
733#else
734
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100735int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000736{
737 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100738 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000739
740 len = len1;
741 while (len > 0) {
742 ret = write(fd, buf, len);
743 if (ret < 0) {
744 if (errno != EINTR && errno != EAGAIN)
745 return -1;
746 } else if (ret == 0) {
747 break;
748 } else {
749 buf += ret;
750 len -= ret;
751 }
752 }
753 return len1 - len;
754}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500755
756int recv_all(int fd, void *_buf, int len1, bool single_read)
757{
758 int ret, len;
759 uint8_t *buf = _buf;
760
761 len = len1;
762 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
763 if (ret < 0) {
764 if (errno != EINTR && errno != EAGAIN) {
765 return -1;
766 }
767 continue;
768 } else {
769 if (single_read) {
770 return ret;
771 }
772 buf += ret;
773 len -= ret;
774 }
775 }
776 return len1 - len;
777}
778
aliguori6f97dba2008-10-31 18:49:55 +0000779#endif /* !_WIN32 */
780
Anthony Liguori96c63842013-03-05 23:21:18 +0530781typedef struct IOWatchPoll
782{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200783 GSource parent;
784
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200785 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530786 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530787
788 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200789 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530790 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530791} IOWatchPoll;
792
Anthony Liguori96c63842013-03-05 23:21:18 +0530793static IOWatchPoll *io_watch_poll_from_source(GSource *source)
794{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200795 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530796}
797
798static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
799{
800 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200801 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200802 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200803 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530804 return FALSE;
805 }
806
Paolo Bonzinid185c092013-04-05 17:59:33 +0200807 if (now_active) {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200808 iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
809 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200810 g_source_attach(iwp->src, NULL);
811 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200812 g_source_destroy(iwp->src);
813 g_source_unref(iwp->src);
814 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200815 }
816 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530817}
818
819static gboolean io_watch_poll_check(GSource *source)
820{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200821 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530822}
823
824static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
825 gpointer user_data)
826{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200827 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530828}
829
830static void io_watch_poll_finalize(GSource *source)
831{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200832 /* Due to a glib bug, removing the last reference to a source
833 * inside a finalize callback causes recursive locking (and a
834 * deadlock). This is not a problem inside other callbacks,
835 * including dispatch callbacks, so we call io_remove_watch_poll
836 * to remove this source. At this point, iwp->src must
837 * be NULL, or we would leak it.
838 *
839 * This would be solved much more elegantly by child sources,
840 * but we support older glib versions that do not have them.
841 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530842 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200843 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530844}
845
846static GSourceFuncs io_watch_poll_funcs = {
847 .prepare = io_watch_poll_prepare,
848 .check = io_watch_poll_check,
849 .dispatch = io_watch_poll_dispatch,
850 .finalize = io_watch_poll_finalize,
851};
852
853/* Can only be used for read */
854static guint io_add_watch_poll(GIOChannel *channel,
855 IOCanReadHandler *fd_can_read,
856 GIOFunc fd_read,
857 gpointer user_data)
858{
859 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200860 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530861
Paolo Bonzinid185c092013-04-05 17:59:33 +0200862 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530863 iwp->fd_can_read = fd_can_read;
864 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200865 iwp->channel = channel;
866 iwp->fd_read = (GSourceFunc) fd_read;
867 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530868
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200869 tag = g_source_attach(&iwp->parent, NULL);
870 g_source_unref(&iwp->parent);
871 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530872}
873
Paolo Bonzini2b316772013-04-19 17:32:09 +0200874static void io_remove_watch_poll(guint tag)
875{
876 GSource *source;
877 IOWatchPoll *iwp;
878
879 g_return_if_fail (tag > 0);
880
881 source = g_main_context_find_source_by_id(NULL, tag);
882 g_return_if_fail (source != NULL);
883
884 iwp = io_watch_poll_from_source(source);
885 if (iwp->src) {
886 g_source_destroy(iwp->src);
887 g_source_unref(iwp->src);
888 iwp->src = NULL;
889 }
890 g_source_destroy(&iwp->parent);
891}
892
Amit Shah26da70c2013-08-28 15:23:37 +0530893static void remove_fd_in_watch(CharDriverState *chr)
894{
895 if (chr->fd_in_tag) {
896 io_remove_watch_poll(chr->fd_in_tag);
897 chr->fd_in_tag = 0;
898 }
899}
900
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000901#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530902static GIOChannel *io_channel_from_fd(int fd)
903{
904 GIOChannel *chan;
905
906 if (fd == -1) {
907 return NULL;
908 }
909
910 chan = g_io_channel_unix_new(fd);
911
912 g_io_channel_set_encoding(chan, NULL, NULL);
913 g_io_channel_set_buffered(chan, FALSE);
914
915 return chan;
916}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000917#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530918
Anthony Liguori76a96442013-03-05 23:21:21 +0530919static GIOChannel *io_channel_from_socket(int fd)
920{
921 GIOChannel *chan;
922
923 if (fd == -1) {
924 return NULL;
925 }
926
927#ifdef _WIN32
928 chan = g_io_channel_win32_new_socket(fd);
929#else
930 chan = g_io_channel_unix_new(fd);
931#endif
932
933 g_io_channel_set_encoding(chan, NULL, NULL);
934 g_io_channel_set_buffered(chan, FALSE);
935
936 return chan;
937}
938
Anthony Liguori684a0962013-03-29 11:39:50 -0500939static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530940{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200941 size_t offset = 0;
942 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530943
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200944 while (offset < len && status == G_IO_STATUS_NORMAL) {
945 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500946
947 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530948 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500949 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530950 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500951
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200952 if (offset > 0) {
953 return offset;
954 }
955 switch (status) {
956 case G_IO_STATUS_NORMAL:
957 g_assert(len == 0);
958 return 0;
959 case G_IO_STATUS_AGAIN:
960 errno = EAGAIN;
961 return -1;
962 default:
963 break;
964 }
965 errno = EINVAL;
966 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530967}
Anthony Liguori96c63842013-03-05 23:21:18 +0530968
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000969#ifndef _WIN32
970
Anthony Liguoria29753f2013-03-05 23:21:19 +0530971typedef struct FDCharDriver {
972 CharDriverState *chr;
973 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000974 int max_size;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530975 QTAILQ_ENTRY(FDCharDriver) node;
aliguori6f97dba2008-10-31 18:49:55 +0000976} FDCharDriver;
977
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200978/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000979static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
980{
981 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530982
Anthony Liguori684a0962013-03-29 11:39:50 -0500983 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530984}
985
986static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
987{
988 CharDriverState *chr = opaque;
989 FDCharDriver *s = chr->opaque;
990 int len;
991 uint8_t buf[READ_BUF_LEN];
992 GIOStatus status;
993 gsize bytes_read;
994
995 len = sizeof(buf);
996 if (len > s->max_size) {
997 len = s->max_size;
998 }
999 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001000 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301001 }
1002
1003 status = g_io_channel_read_chars(chan, (gchar *)buf,
1004 len, &bytes_read, NULL);
1005 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301006 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301007 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1008 return FALSE;
1009 }
1010 if (status == G_IO_STATUS_NORMAL) {
1011 qemu_chr_be_write(chr, buf, bytes_read);
1012 }
1013
1014 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001015}
1016
1017static int fd_chr_read_poll(void *opaque)
1018{
1019 CharDriverState *chr = opaque;
1020 FDCharDriver *s = chr->opaque;
1021
Anthony Liguori909cda12011-08-15 11:17:31 -05001022 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001023 return s->max_size;
1024}
1025
Anthony Liguori23673ca2013-03-05 23:21:23 +05301026static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1027{
1028 FDCharDriver *s = chr->opaque;
1029 return g_io_create_watch(s->fd_out, cond);
1030}
1031
aliguori6f97dba2008-10-31 18:49:55 +00001032static void fd_chr_update_read_handler(CharDriverState *chr)
1033{
1034 FDCharDriver *s = chr->opaque;
1035
Amit Shah26da70c2013-08-28 15:23:37 +05301036 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301037 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301038 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1039 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001040 }
1041}
1042
1043static void fd_chr_close(struct CharDriverState *chr)
1044{
1045 FDCharDriver *s = chr->opaque;
1046
Amit Shah26da70c2013-08-28 15:23:37 +05301047 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301048 if (s->fd_in) {
1049 g_io_channel_unref(s->fd_in);
1050 }
1051 if (s->fd_out) {
1052 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001053 }
1054
Anthony Liguori7267c092011-08-20 22:09:37 -05001055 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001056 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001057}
1058
1059/* open a character device to a unix fd */
1060static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1061{
1062 CharDriverState *chr;
1063 FDCharDriver *s;
1064
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001065 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001066 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +05301067 s->fd_in = io_channel_from_fd(fd_in);
1068 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001069 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301070 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001071 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301072 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001073 chr->chr_write = fd_chr_write;
1074 chr->chr_update_read_handler = fd_chr_update_read_handler;
1075 chr->chr_close = fd_chr_close;
1076
aliguori6f97dba2008-10-31 18:49:55 +00001077 return chr;
1078}
1079
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001080static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001081{
1082 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001083 char filename_in[CHR_MAX_FILENAME_SIZE];
1084 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001085 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001086
1087 if (filename == NULL) {
1088 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +01001089 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001090 }
aliguori6f97dba2008-10-31 18:49:55 +00001091
Corey Minyard9f781162014-10-02 11:17:33 -05001092 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1093 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001094 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1095 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001096 if (fd_in < 0 || fd_out < 0) {
1097 if (fd_in >= 0)
1098 close(fd_in);
1099 if (fd_out >= 0)
1100 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001101 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001102 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001103 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001104 }
aliguori6f97dba2008-10-31 18:49:55 +00001105 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001106 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001107}
1108
aliguori6f97dba2008-10-31 18:49:55 +00001109/* init terminal so that we can grab keys */
1110static struct termios oldtty;
1111static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001112static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001113static bool stdio_allow_signal;
aliguori6f97dba2008-10-31 18:49:55 +00001114
1115static void term_exit(void)
1116{
1117 tcsetattr (0, TCSANOW, &oldtty);
1118 fcntl(0, F_SETFL, old_fd0_flags);
1119}
1120
Paolo Bonzinibb002512010-12-23 13:42:50 +01001121static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001122{
1123 struct termios tty;
1124
Paolo Bonzini03693642010-12-23 13:42:49 +01001125 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001126 if (!echo) {
1127 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001128 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001129 tty.c_oflag |= OPOST;
1130 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1131 tty.c_cflag &= ~(CSIZE|PARENB);
1132 tty.c_cflag |= CS8;
1133 tty.c_cc[VMIN] = 1;
1134 tty.c_cc[VTIME] = 0;
1135 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001136 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001137 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001138
1139 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001140}
1141
1142static void qemu_chr_close_stdio(struct CharDriverState *chr)
1143{
1144 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001145 fd_chr_close(chr);
1146}
1147
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001148static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001149{
1150 CharDriverState *chr;
1151
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001152 if (is_daemonized()) {
1153 error_report("cannot use stdio with -daemonize");
1154 return NULL;
1155 }
Li Liuc88930a2014-09-09 19:19:48 +08001156
1157 if (stdio_in_use) {
1158 error_report("cannot use stdio by multiple character devices");
1159 exit(1);
1160 }
1161
1162 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301163 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001164 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001165 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301166 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001167
aliguori6f97dba2008-10-31 18:49:55 +00001168 chr = qemu_chr_open_fd(0, 1);
1169 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001170 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001171 if (opts->has_signal) {
1172 stdio_allow_signal = opts->signal;
1173 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001174 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001175
Markus Armbruster1f514702012-02-07 15:09:08 +01001176 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001177}
1178
aliguori6f97dba2008-10-31 18:49:55 +00001179#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001180 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1181 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001182
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001183#define HAVE_CHARDEV_TTY 1
1184
aliguori6f97dba2008-10-31 18:49:55 +00001185typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301186 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001187 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001188
1189 /* Protected by the CharDriverState chr_write_lock. */
1190 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301191 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001192 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001193} PtyCharDriver;
1194
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001195static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001196static void pty_chr_state(CharDriverState *chr, int connected);
1197
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301198static gboolean pty_chr_timer(gpointer opaque)
1199{
1200 struct CharDriverState *chr = opaque;
1201 PtyCharDriver *s = chr->opaque;
1202
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001203 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001204 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001205 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001206 if (!s->connected) {
1207 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001208 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001209 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001210 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301211 return FALSE;
1212}
1213
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001214/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301215static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1216{
1217 PtyCharDriver *s = chr->opaque;
1218
1219 if (s->timer_tag) {
1220 g_source_remove(s->timer_tag);
1221 s->timer_tag = 0;
1222 }
1223
1224 if (ms == 1000) {
1225 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1226 } else {
1227 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1228 }
1229}
1230
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001231/* Called with chr_write_lock held. */
1232static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001233{
1234 PtyCharDriver *s = chr->opaque;
1235 GPollFD pfd;
1236
1237 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1238 pfd.events = G_IO_OUT;
1239 pfd.revents = 0;
1240 g_poll(&pfd, 1, 0);
1241 if (pfd.revents & G_IO_HUP) {
1242 pty_chr_state(chr, 0);
1243 } else {
1244 pty_chr_state(chr, 1);
1245 }
1246}
1247
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001248static void pty_chr_update_read_handler(CharDriverState *chr)
1249{
1250 qemu_mutex_lock(&chr->chr_write_lock);
1251 pty_chr_update_read_handler_locked(chr);
1252 qemu_mutex_unlock(&chr->chr_write_lock);
1253}
1254
1255/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001256static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1257{
1258 PtyCharDriver *s = chr->opaque;
1259
1260 if (!s->connected) {
1261 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001262 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001263 if (!s->connected) {
1264 return 0;
1265 }
aliguori6f97dba2008-10-31 18:49:55 +00001266 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001267 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001268}
1269
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301270static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1271{
1272 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001273 if (!s->connected) {
1274 return NULL;
1275 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301276 return g_io_create_watch(s->fd, cond);
1277}
1278
aliguori6f97dba2008-10-31 18:49:55 +00001279static int pty_chr_read_poll(void *opaque)
1280{
1281 CharDriverState *chr = opaque;
1282 PtyCharDriver *s = chr->opaque;
1283
Anthony Liguori909cda12011-08-15 11:17:31 -05001284 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001285 return s->read_bytes;
1286}
1287
Anthony Liguori093d3a22013-03-05 23:21:20 +05301288static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001289{
1290 CharDriverState *chr = opaque;
1291 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301292 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301293 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301294 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001295
1296 len = sizeof(buf);
1297 if (len > s->read_bytes)
1298 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001299 if (len == 0) {
1300 return TRUE;
1301 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301302 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1303 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001304 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301305 return FALSE;
1306 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001307 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001308 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001309 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301310 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001311}
1312
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001313static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1314{
1315 CharDriverState *chr = opaque;
1316 PtyCharDriver *s = chr->opaque;
1317
1318 s->open_tag = 0;
1319 qemu_chr_be_generic_open(chr);
1320 return FALSE;
1321}
1322
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001323/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001324static void pty_chr_state(CharDriverState *chr, int connected)
1325{
1326 PtyCharDriver *s = chr->opaque;
1327
1328 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001329 if (s->open_tag) {
1330 g_source_remove(s->open_tag);
1331 s->open_tag = 0;
1332 }
Amit Shah26da70c2013-08-28 15:23:37 +05301333 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001334 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001335 /* (re-)connect poll interval for idle guests: once per second.
1336 * We check more frequently in case the guests sends data to
1337 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301338 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001339 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001340 if (s->timer_tag) {
1341 g_source_remove(s->timer_tag);
1342 s->timer_tag = 0;
1343 }
1344 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001345 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001346 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001347 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001348 }
1349 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301350 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1351 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001352 }
aliguori6f97dba2008-10-31 18:49:55 +00001353 }
1354}
1355
aliguori6f97dba2008-10-31 18:49:55 +00001356static void pty_chr_close(struct CharDriverState *chr)
1357{
1358 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301359 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001360
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001361 qemu_mutex_lock(&chr->chr_write_lock);
1362 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301363 fd = g_io_channel_unix_get_fd(s->fd);
1364 g_io_channel_unref(s->fd);
1365 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301366 if (s->timer_tag) {
1367 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001368 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301369 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001370 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001371 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001372 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001373}
1374
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001375static CharDriverState *qemu_chr_open_pty(const char *id,
1376 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001377{
1378 CharDriverState *chr;
1379 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001380 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001381 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001382
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001383 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1384 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001385 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001386 }
1387
aliguori6f97dba2008-10-31 18:49:55 +00001388 close(slave_fd);
1389
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001390 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001391
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001392 chr->filename = g_strdup_printf("pty:%s", pty_name);
1393 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001394 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001395
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001396 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001397 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001398
1399 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001400 chr->opaque = s;
1401 chr->chr_write = pty_chr_write;
1402 chr->chr_update_read_handler = pty_chr_update_read_handler;
1403 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301404 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001405 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001406
Anthony Liguori093d3a22013-03-05 23:21:20 +05301407 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301408 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001409
Markus Armbruster1f514702012-02-07 15:09:08 +01001410 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001411}
1412
1413static void tty_serial_init(int fd, int speed,
1414 int parity, int data_bits, int stop_bits)
1415{
1416 struct termios tty;
1417 speed_t spd;
1418
1419#if 0
1420 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1421 speed, parity, data_bits, stop_bits);
1422#endif
1423 tcgetattr (fd, &tty);
1424
Stefan Weil45eea132009-10-26 16:10:10 +01001425#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1426 speed = speed * 10 / 11;
1427 do {
1428 check_speed(50);
1429 check_speed(75);
1430 check_speed(110);
1431 check_speed(134);
1432 check_speed(150);
1433 check_speed(200);
1434 check_speed(300);
1435 check_speed(600);
1436 check_speed(1200);
1437 check_speed(1800);
1438 check_speed(2400);
1439 check_speed(4800);
1440 check_speed(9600);
1441 check_speed(19200);
1442 check_speed(38400);
1443 /* Non-Posix values follow. They may be unsupported on some systems. */
1444 check_speed(57600);
1445 check_speed(115200);
1446#ifdef B230400
1447 check_speed(230400);
1448#endif
1449#ifdef B460800
1450 check_speed(460800);
1451#endif
1452#ifdef B500000
1453 check_speed(500000);
1454#endif
1455#ifdef B576000
1456 check_speed(576000);
1457#endif
1458#ifdef B921600
1459 check_speed(921600);
1460#endif
1461#ifdef B1000000
1462 check_speed(1000000);
1463#endif
1464#ifdef B1152000
1465 check_speed(1152000);
1466#endif
1467#ifdef B1500000
1468 check_speed(1500000);
1469#endif
1470#ifdef B2000000
1471 check_speed(2000000);
1472#endif
1473#ifdef B2500000
1474 check_speed(2500000);
1475#endif
1476#ifdef B3000000
1477 check_speed(3000000);
1478#endif
1479#ifdef B3500000
1480 check_speed(3500000);
1481#endif
1482#ifdef B4000000
1483 check_speed(4000000);
1484#endif
aliguori6f97dba2008-10-31 18:49:55 +00001485 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001486 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001487
1488 cfsetispeed(&tty, spd);
1489 cfsetospeed(&tty, spd);
1490
1491 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1492 |INLCR|IGNCR|ICRNL|IXON);
1493 tty.c_oflag |= OPOST;
1494 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1495 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1496 switch(data_bits) {
1497 default:
1498 case 8:
1499 tty.c_cflag |= CS8;
1500 break;
1501 case 7:
1502 tty.c_cflag |= CS7;
1503 break;
1504 case 6:
1505 tty.c_cflag |= CS6;
1506 break;
1507 case 5:
1508 tty.c_cflag |= CS5;
1509 break;
1510 }
1511 switch(parity) {
1512 default:
1513 case 'N':
1514 break;
1515 case 'E':
1516 tty.c_cflag |= PARENB;
1517 break;
1518 case 'O':
1519 tty.c_cflag |= PARENB | PARODD;
1520 break;
1521 }
1522 if (stop_bits == 2)
1523 tty.c_cflag |= CSTOPB;
1524
1525 tcsetattr (fd, TCSANOW, &tty);
1526}
1527
1528static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1529{
1530 FDCharDriver *s = chr->opaque;
1531
1532 switch(cmd) {
1533 case CHR_IOCTL_SERIAL_SET_PARAMS:
1534 {
1535 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301536 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1537 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001538 ssp->data_bits, ssp->stop_bits);
1539 }
1540 break;
1541 case CHR_IOCTL_SERIAL_SET_BREAK:
1542 {
1543 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301544 if (enable) {
1545 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1546 }
aliguori6f97dba2008-10-31 18:49:55 +00001547 }
1548 break;
1549 case CHR_IOCTL_SERIAL_GET_TIOCM:
1550 {
1551 int sarg = 0;
1552 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301553 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001554 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001555 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001556 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001557 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001558 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001559 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001560 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001561 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001562 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001563 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001564 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001565 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001566 *targ |= CHR_TIOCM_RTS;
1567 }
1568 break;
1569 case CHR_IOCTL_SERIAL_SET_TIOCM:
1570 {
1571 int sarg = *(int *)arg;
1572 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301573 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001574 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1575 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1576 if (sarg & CHR_TIOCM_CTS)
1577 targ |= TIOCM_CTS;
1578 if (sarg & CHR_TIOCM_CAR)
1579 targ |= TIOCM_CAR;
1580 if (sarg & CHR_TIOCM_DSR)
1581 targ |= TIOCM_DSR;
1582 if (sarg & CHR_TIOCM_RI)
1583 targ |= TIOCM_RI;
1584 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001585 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001586 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001587 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301588 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001589 }
1590 break;
1591 default:
1592 return -ENOTSUP;
1593 }
1594 return 0;
1595}
1596
David Ahern4266a132010-02-10 18:27:17 -07001597static void qemu_chr_close_tty(CharDriverState *chr)
1598{
1599 FDCharDriver *s = chr->opaque;
1600 int fd = -1;
1601
1602 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301603 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001604 }
1605
1606 fd_chr_close(chr);
1607
1608 if (fd >= 0) {
1609 close(fd);
1610 }
1611}
1612
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001613static CharDriverState *qemu_chr_open_tty_fd(int fd)
1614{
1615 CharDriverState *chr;
1616
1617 tty_serial_init(fd, 115200, 'N', 8, 1);
1618 chr = qemu_chr_open_fd(fd, fd);
1619 chr->chr_ioctl = tty_serial_ioctl;
1620 chr->chr_close = qemu_chr_close_tty;
1621 return chr;
1622}
aliguori6f97dba2008-10-31 18:49:55 +00001623#endif /* __linux__ || __sun__ */
1624
1625#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001626
1627#define HAVE_CHARDEV_PARPORT 1
1628
aliguori6f97dba2008-10-31 18:49:55 +00001629typedef struct {
1630 int fd;
1631 int mode;
1632} ParallelCharDriver;
1633
1634static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1635{
1636 if (s->mode != mode) {
1637 int m = mode;
1638 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1639 return 0;
1640 s->mode = mode;
1641 }
1642 return 1;
1643}
1644
1645static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1646{
1647 ParallelCharDriver *drv = chr->opaque;
1648 int fd = drv->fd;
1649 uint8_t b;
1650
1651 switch(cmd) {
1652 case CHR_IOCTL_PP_READ_DATA:
1653 if (ioctl(fd, PPRDATA, &b) < 0)
1654 return -ENOTSUP;
1655 *(uint8_t *)arg = b;
1656 break;
1657 case CHR_IOCTL_PP_WRITE_DATA:
1658 b = *(uint8_t *)arg;
1659 if (ioctl(fd, PPWDATA, &b) < 0)
1660 return -ENOTSUP;
1661 break;
1662 case CHR_IOCTL_PP_READ_CONTROL:
1663 if (ioctl(fd, PPRCONTROL, &b) < 0)
1664 return -ENOTSUP;
1665 /* Linux gives only the lowest bits, and no way to know data
1666 direction! For better compatibility set the fixed upper
1667 bits. */
1668 *(uint8_t *)arg = b | 0xc0;
1669 break;
1670 case CHR_IOCTL_PP_WRITE_CONTROL:
1671 b = *(uint8_t *)arg;
1672 if (ioctl(fd, PPWCONTROL, &b) < 0)
1673 return -ENOTSUP;
1674 break;
1675 case CHR_IOCTL_PP_READ_STATUS:
1676 if (ioctl(fd, PPRSTATUS, &b) < 0)
1677 return -ENOTSUP;
1678 *(uint8_t *)arg = b;
1679 break;
1680 case CHR_IOCTL_PP_DATA_DIR:
1681 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1682 return -ENOTSUP;
1683 break;
1684 case CHR_IOCTL_PP_EPP_READ_ADDR:
1685 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1686 struct ParallelIOArg *parg = arg;
1687 int n = read(fd, parg->buffer, parg->count);
1688 if (n != parg->count) {
1689 return -EIO;
1690 }
1691 }
1692 break;
1693 case CHR_IOCTL_PP_EPP_READ:
1694 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1695 struct ParallelIOArg *parg = arg;
1696 int n = read(fd, parg->buffer, parg->count);
1697 if (n != parg->count) {
1698 return -EIO;
1699 }
1700 }
1701 break;
1702 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1703 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1704 struct ParallelIOArg *parg = arg;
1705 int n = write(fd, parg->buffer, parg->count);
1706 if (n != parg->count) {
1707 return -EIO;
1708 }
1709 }
1710 break;
1711 case CHR_IOCTL_PP_EPP_WRITE:
1712 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1713 struct ParallelIOArg *parg = arg;
1714 int n = write(fd, parg->buffer, parg->count);
1715 if (n != parg->count) {
1716 return -EIO;
1717 }
1718 }
1719 break;
1720 default:
1721 return -ENOTSUP;
1722 }
1723 return 0;
1724}
1725
1726static void pp_close(CharDriverState *chr)
1727{
1728 ParallelCharDriver *drv = chr->opaque;
1729 int fd = drv->fd;
1730
1731 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1732 ioctl(fd, PPRELEASE);
1733 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001734 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001735 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001736}
1737
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001738static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001739{
1740 CharDriverState *chr;
1741 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001742
1743 if (ioctl(fd, PPCLAIM) < 0) {
1744 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001745 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001746 }
1747
Anthony Liguori7267c092011-08-20 22:09:37 -05001748 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001749 drv->fd = fd;
1750 drv->mode = IEEE1284_MODE_COMPAT;
1751
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001752 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001753 chr->chr_write = null_chr_write;
1754 chr->chr_ioctl = pp_ioctl;
1755 chr->chr_close = pp_close;
1756 chr->opaque = drv;
1757
Markus Armbruster1f514702012-02-07 15:09:08 +01001758 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001759}
1760#endif /* __linux__ */
1761
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001762#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001763
1764#define HAVE_CHARDEV_PARPORT 1
1765
blueswir16972f932008-11-22 20:49:12 +00001766static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1767{
Stefan Weile0efb992011-02-23 19:09:16 +01001768 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001769 uint8_t b;
1770
1771 switch(cmd) {
1772 case CHR_IOCTL_PP_READ_DATA:
1773 if (ioctl(fd, PPIGDATA, &b) < 0)
1774 return -ENOTSUP;
1775 *(uint8_t *)arg = b;
1776 break;
1777 case CHR_IOCTL_PP_WRITE_DATA:
1778 b = *(uint8_t *)arg;
1779 if (ioctl(fd, PPISDATA, &b) < 0)
1780 return -ENOTSUP;
1781 break;
1782 case CHR_IOCTL_PP_READ_CONTROL:
1783 if (ioctl(fd, PPIGCTRL, &b) < 0)
1784 return -ENOTSUP;
1785 *(uint8_t *)arg = b;
1786 break;
1787 case CHR_IOCTL_PP_WRITE_CONTROL:
1788 b = *(uint8_t *)arg;
1789 if (ioctl(fd, PPISCTRL, &b) < 0)
1790 return -ENOTSUP;
1791 break;
1792 case CHR_IOCTL_PP_READ_STATUS:
1793 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1794 return -ENOTSUP;
1795 *(uint8_t *)arg = b;
1796 break;
1797 default:
1798 return -ENOTSUP;
1799 }
1800 return 0;
1801}
1802
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001803static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001804{
1805 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001806
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001807 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001808 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001809 chr->chr_write = null_chr_write;
1810 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001811 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001812 return chr;
blueswir16972f932008-11-22 20:49:12 +00001813}
1814#endif
1815
aliguori6f97dba2008-10-31 18:49:55 +00001816#else /* _WIN32 */
1817
1818typedef struct {
1819 int max_size;
1820 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001821 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001822 BOOL fpipe;
1823 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001824
1825 /* Protected by the CharDriverState chr_write_lock. */
1826 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001827} WinCharState;
1828
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001829typedef struct {
1830 HANDLE hStdIn;
1831 HANDLE hInputReadyEvent;
1832 HANDLE hInputDoneEvent;
1833 HANDLE hInputThread;
1834 uint8_t win_stdio_buf;
1835} WinStdioCharState;
1836
aliguori6f97dba2008-10-31 18:49:55 +00001837#define NSENDBUF 2048
1838#define NRECVBUF 2048
1839#define MAXCONNECT 1
1840#define NTIMEOUT 5000
1841
1842static int win_chr_poll(void *opaque);
1843static int win_chr_pipe_poll(void *opaque);
1844
1845static void win_chr_close(CharDriverState *chr)
1846{
1847 WinCharState *s = chr->opaque;
1848
1849 if (s->hsend) {
1850 CloseHandle(s->hsend);
1851 s->hsend = NULL;
1852 }
1853 if (s->hrecv) {
1854 CloseHandle(s->hrecv);
1855 s->hrecv = NULL;
1856 }
1857 if (s->hcom) {
1858 CloseHandle(s->hcom);
1859 s->hcom = NULL;
1860 }
1861 if (s->fpipe)
1862 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1863 else
1864 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301865
Hans de Goedea425d232011-11-19 10:22:43 +01001866 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001867}
1868
1869static int win_chr_init(CharDriverState *chr, const char *filename)
1870{
1871 WinCharState *s = chr->opaque;
1872 COMMCONFIG comcfg;
1873 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1874 COMSTAT comstat;
1875 DWORD size;
1876 DWORD err;
1877
1878 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1879 if (!s->hsend) {
1880 fprintf(stderr, "Failed CreateEvent\n");
1881 goto fail;
1882 }
1883 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1884 if (!s->hrecv) {
1885 fprintf(stderr, "Failed CreateEvent\n");
1886 goto fail;
1887 }
1888
1889 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1890 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1891 if (s->hcom == INVALID_HANDLE_VALUE) {
1892 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1893 s->hcom = NULL;
1894 goto fail;
1895 }
1896
1897 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1898 fprintf(stderr, "Failed SetupComm\n");
1899 goto fail;
1900 }
1901
1902 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1903 size = sizeof(COMMCONFIG);
1904 GetDefaultCommConfig(filename, &comcfg, &size);
1905 comcfg.dcb.DCBlength = sizeof(DCB);
1906 CommConfigDialog(filename, NULL, &comcfg);
1907
1908 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1909 fprintf(stderr, "Failed SetCommState\n");
1910 goto fail;
1911 }
1912
1913 if (!SetCommMask(s->hcom, EV_ERR)) {
1914 fprintf(stderr, "Failed SetCommMask\n");
1915 goto fail;
1916 }
1917
1918 cto.ReadIntervalTimeout = MAXDWORD;
1919 if (!SetCommTimeouts(s->hcom, &cto)) {
1920 fprintf(stderr, "Failed SetCommTimeouts\n");
1921 goto fail;
1922 }
1923
1924 if (!ClearCommError(s->hcom, &err, &comstat)) {
1925 fprintf(stderr, "Failed ClearCommError\n");
1926 goto fail;
1927 }
1928 qemu_add_polling_cb(win_chr_poll, chr);
1929 return 0;
1930
1931 fail:
1932 win_chr_close(chr);
1933 return -1;
1934}
1935
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001936/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001937static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1938{
1939 WinCharState *s = chr->opaque;
1940 DWORD len, ret, size, err;
1941
1942 len = len1;
1943 ZeroMemory(&s->osend, sizeof(s->osend));
1944 s->osend.hEvent = s->hsend;
1945 while (len > 0) {
1946 if (s->hsend)
1947 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1948 else
1949 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1950 if (!ret) {
1951 err = GetLastError();
1952 if (err == ERROR_IO_PENDING) {
1953 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1954 if (ret) {
1955 buf += size;
1956 len -= size;
1957 } else {
1958 break;
1959 }
1960 } else {
1961 break;
1962 }
1963 } else {
1964 buf += size;
1965 len -= size;
1966 }
1967 }
1968 return len1 - len;
1969}
1970
1971static int win_chr_read_poll(CharDriverState *chr)
1972{
1973 WinCharState *s = chr->opaque;
1974
Anthony Liguori909cda12011-08-15 11:17:31 -05001975 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001976 return s->max_size;
1977}
1978
1979static void win_chr_readfile(CharDriverState *chr)
1980{
1981 WinCharState *s = chr->opaque;
1982 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301983 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001984 DWORD size;
1985
1986 ZeroMemory(&s->orecv, sizeof(s->orecv));
1987 s->orecv.hEvent = s->hrecv;
1988 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1989 if (!ret) {
1990 err = GetLastError();
1991 if (err == ERROR_IO_PENDING) {
1992 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1993 }
1994 }
1995
1996 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001997 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001998 }
1999}
2000
2001static void win_chr_read(CharDriverState *chr)
2002{
2003 WinCharState *s = chr->opaque;
2004
2005 if (s->len > s->max_size)
2006 s->len = s->max_size;
2007 if (s->len == 0)
2008 return;
2009
2010 win_chr_readfile(chr);
2011}
2012
2013static int win_chr_poll(void *opaque)
2014{
2015 CharDriverState *chr = opaque;
2016 WinCharState *s = chr->opaque;
2017 COMSTAT status;
2018 DWORD comerr;
2019
2020 ClearCommError(s->hcom, &comerr, &status);
2021 if (status.cbInQue > 0) {
2022 s->len = status.cbInQue;
2023 win_chr_read_poll(chr);
2024 win_chr_read(chr);
2025 return 1;
2026 }
2027 return 0;
2028}
2029
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01002030static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00002031{
2032 CharDriverState *chr;
2033 WinCharState *s;
2034
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002035 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002036 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002037 chr->opaque = s;
2038 chr->chr_write = win_chr_write;
2039 chr->chr_close = win_chr_close;
2040
2041 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002042 g_free(s);
2043 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002044 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002045 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002046 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002047}
2048
2049static int win_chr_pipe_poll(void *opaque)
2050{
2051 CharDriverState *chr = opaque;
2052 WinCharState *s = chr->opaque;
2053 DWORD size;
2054
2055 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2056 if (size > 0) {
2057 s->len = size;
2058 win_chr_read_poll(chr);
2059 win_chr_read(chr);
2060 return 1;
2061 }
2062 return 0;
2063}
2064
2065static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2066{
2067 WinCharState *s = chr->opaque;
2068 OVERLAPPED ov;
2069 int ret;
2070 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002071 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002072
2073 s->fpipe = TRUE;
2074
2075 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2076 if (!s->hsend) {
2077 fprintf(stderr, "Failed CreateEvent\n");
2078 goto fail;
2079 }
2080 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2081 if (!s->hrecv) {
2082 fprintf(stderr, "Failed CreateEvent\n");
2083 goto fail;
2084 }
2085
2086 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2087 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2088 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2089 PIPE_WAIT,
2090 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2091 if (s->hcom == INVALID_HANDLE_VALUE) {
2092 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2093 s->hcom = NULL;
2094 goto fail;
2095 }
2096
2097 ZeroMemory(&ov, sizeof(ov));
2098 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2099 ret = ConnectNamedPipe(s->hcom, &ov);
2100 if (ret) {
2101 fprintf(stderr, "Failed ConnectNamedPipe\n");
2102 goto fail;
2103 }
2104
2105 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2106 if (!ret) {
2107 fprintf(stderr, "Failed GetOverlappedResult\n");
2108 if (ov.hEvent) {
2109 CloseHandle(ov.hEvent);
2110 ov.hEvent = NULL;
2111 }
2112 goto fail;
2113 }
2114
2115 if (ov.hEvent) {
2116 CloseHandle(ov.hEvent);
2117 ov.hEvent = NULL;
2118 }
2119 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2120 return 0;
2121
2122 fail:
2123 win_chr_close(chr);
2124 return -1;
2125}
2126
2127
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002128static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002129{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002130 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002131 CharDriverState *chr;
2132 WinCharState *s;
2133
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002134 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002135 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002136 chr->opaque = s;
2137 chr->chr_write = win_chr_write;
2138 chr->chr_close = win_chr_close;
2139
2140 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002141 g_free(s);
2142 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002143 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002144 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002145 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002146}
2147
Markus Armbruster1f514702012-02-07 15:09:08 +01002148static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002149{
2150 CharDriverState *chr;
2151 WinCharState *s;
2152
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002153 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002154 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002155 s->hcom = fd_out;
2156 chr->opaque = s;
2157 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002158 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002159}
2160
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002161static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002162{
Markus Armbruster1f514702012-02-07 15:09:08 +01002163 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002164}
2165
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002166static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2167{
2168 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2169 DWORD dwSize;
2170 int len1;
2171
2172 len1 = len;
2173
2174 while (len1 > 0) {
2175 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2176 break;
2177 }
2178 buf += dwSize;
2179 len1 -= dwSize;
2180 }
2181
2182 return len - len1;
2183}
2184
2185static void win_stdio_wait_func(void *opaque)
2186{
2187 CharDriverState *chr = opaque;
2188 WinStdioCharState *stdio = chr->opaque;
2189 INPUT_RECORD buf[4];
2190 int ret;
2191 DWORD dwSize;
2192 int i;
2193
Stefan Weildff74242013-12-07 14:48:04 +01002194 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002195
2196 if (!ret) {
2197 /* Avoid error storm */
2198 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2199 return;
2200 }
2201
2202 for (i = 0; i < dwSize; i++) {
2203 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2204
2205 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2206 int j;
2207 if (kev->uChar.AsciiChar != 0) {
2208 for (j = 0; j < kev->wRepeatCount; j++) {
2209 if (qemu_chr_be_can_write(chr)) {
2210 uint8_t c = kev->uChar.AsciiChar;
2211 qemu_chr_be_write(chr, &c, 1);
2212 }
2213 }
2214 }
2215 }
2216 }
2217}
2218
2219static DWORD WINAPI win_stdio_thread(LPVOID param)
2220{
2221 CharDriverState *chr = param;
2222 WinStdioCharState *stdio = chr->opaque;
2223 int ret;
2224 DWORD dwSize;
2225
2226 while (1) {
2227
2228 /* Wait for one byte */
2229 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2230
2231 /* Exit in case of error, continue if nothing read */
2232 if (!ret) {
2233 break;
2234 }
2235 if (!dwSize) {
2236 continue;
2237 }
2238
2239 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2240 if (stdio->win_stdio_buf == '\r') {
2241 continue;
2242 }
2243
2244 /* Signal the main thread and wait until the byte was eaten */
2245 if (!SetEvent(stdio->hInputReadyEvent)) {
2246 break;
2247 }
2248 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2249 != WAIT_OBJECT_0) {
2250 break;
2251 }
2252 }
2253
2254 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2255 return 0;
2256}
2257
2258static void win_stdio_thread_wait_func(void *opaque)
2259{
2260 CharDriverState *chr = opaque;
2261 WinStdioCharState *stdio = chr->opaque;
2262
2263 if (qemu_chr_be_can_write(chr)) {
2264 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2265 }
2266
2267 SetEvent(stdio->hInputDoneEvent);
2268}
2269
2270static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2271{
2272 WinStdioCharState *stdio = chr->opaque;
2273 DWORD dwMode = 0;
2274
2275 GetConsoleMode(stdio->hStdIn, &dwMode);
2276
2277 if (echo) {
2278 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2279 } else {
2280 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2281 }
2282}
2283
2284static void win_stdio_close(CharDriverState *chr)
2285{
2286 WinStdioCharState *stdio = chr->opaque;
2287
2288 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2289 CloseHandle(stdio->hInputReadyEvent);
2290 }
2291 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2292 CloseHandle(stdio->hInputDoneEvent);
2293 }
2294 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2295 TerminateThread(stdio->hInputThread, 0);
2296 }
2297
2298 g_free(chr->opaque);
2299 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002300}
2301
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002302static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002303{
2304 CharDriverState *chr;
2305 WinStdioCharState *stdio;
2306 DWORD dwMode;
2307 int is_console = 0;
2308
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002309 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002310 stdio = g_malloc0(sizeof(WinStdioCharState));
2311
2312 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2313 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2314 fprintf(stderr, "cannot open stdio: invalid handle\n");
2315 exit(1);
2316 }
2317
2318 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2319
2320 chr->opaque = stdio;
2321 chr->chr_write = win_stdio_write;
2322 chr->chr_close = win_stdio_close;
2323
Anthony Liguoried7a1542013-03-05 23:21:17 +05302324 if (is_console) {
2325 if (qemu_add_wait_object(stdio->hStdIn,
2326 win_stdio_wait_func, chr)) {
2327 fprintf(stderr, "qemu_add_wait_object: failed\n");
2328 }
2329 } else {
2330 DWORD dwId;
2331
2332 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2333 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2334 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2335 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002336
Anthony Liguoried7a1542013-03-05 23:21:17 +05302337 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2338 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2339 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2340 fprintf(stderr, "cannot create stdio thread or event\n");
2341 exit(1);
2342 }
2343 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2344 win_stdio_thread_wait_func, chr)) {
2345 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002346 }
2347 }
2348
2349 dwMode |= ENABLE_LINE_INPUT;
2350
Anthony Liguoried7a1542013-03-05 23:21:17 +05302351 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002352 /* set the terminal in raw mode */
2353 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2354 dwMode |= ENABLE_PROCESSED_INPUT;
2355 }
2356
2357 SetConsoleMode(stdio->hStdIn, dwMode);
2358
2359 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2360 qemu_chr_fe_set_echo(chr, false);
2361
Markus Armbruster1f514702012-02-07 15:09:08 +01002362 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002363}
aliguori6f97dba2008-10-31 18:49:55 +00002364#endif /* !_WIN32 */
2365
Anthony Liguori5ab82112013-03-05 23:21:31 +05302366
aliguori6f97dba2008-10-31 18:49:55 +00002367/***********************************************************/
2368/* UDP Net console */
2369
2370typedef struct {
2371 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302372 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302373 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002374 int bufcnt;
2375 int bufptr;
2376 int max_size;
2377} NetCharDriver;
2378
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002379/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002380static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2381{
2382 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302383 gsize bytes_written;
2384 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002385
Anthony Liguori76a96442013-03-05 23:21:21 +05302386 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2387 if (status == G_IO_STATUS_EOF) {
2388 return 0;
2389 } else if (status != G_IO_STATUS_NORMAL) {
2390 return -1;
2391 }
2392
2393 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002394}
2395
2396static int udp_chr_read_poll(void *opaque)
2397{
2398 CharDriverState *chr = opaque;
2399 NetCharDriver *s = chr->opaque;
2400
Anthony Liguori909cda12011-08-15 11:17:31 -05002401 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002402
2403 /* If there were any stray characters in the queue process them
2404 * first
2405 */
2406 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002407 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002408 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002409 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002410 }
2411 return s->max_size;
2412}
2413
Anthony Liguori76a96442013-03-05 23:21:21 +05302414static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002415{
2416 CharDriverState *chr = opaque;
2417 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302418 gsize bytes_read = 0;
2419 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002420
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002421 if (s->max_size == 0) {
2422 return TRUE;
2423 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302424 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2425 &bytes_read, NULL);
2426 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002427 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302428 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302429 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302430 return FALSE;
2431 }
aliguori6f97dba2008-10-31 18:49:55 +00002432
2433 s->bufptr = 0;
2434 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002435 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002436 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002437 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002438 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302439
2440 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002441}
2442
2443static void udp_chr_update_read_handler(CharDriverState *chr)
2444{
2445 NetCharDriver *s = chr->opaque;
2446
Amit Shah26da70c2013-08-28 15:23:37 +05302447 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302448 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302449 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2450 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002451 }
2452}
2453
aliguori819f56b2009-03-28 17:58:14 +00002454static void udp_chr_close(CharDriverState *chr)
2455{
2456 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302457
2458 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302459 if (s->chan) {
2460 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002461 closesocket(s->fd);
2462 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002463 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002464 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002465}
2466
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002467static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002468{
2469 CharDriverState *chr = NULL;
2470 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002471
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002472 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002473 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002474
aliguori6f97dba2008-10-31 18:49:55 +00002475 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302476 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002477 s->bufcnt = 0;
2478 s->bufptr = 0;
2479 chr->opaque = s;
2480 chr->chr_write = udp_chr_write;
2481 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002482 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002483 /* be isn't opened until we get a connection */
2484 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002485 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002486}
aliguori6f97dba2008-10-31 18:49:55 +00002487
aliguori6f97dba2008-10-31 18:49:55 +00002488/***********************************************************/
2489/* TCP Net console */
2490
2491typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302492
2493 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302494 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002495 int fd, listen_fd;
2496 int connected;
2497 int max_size;
2498 int do_telnetopt;
2499 int do_nodelay;
2500 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002501 int *read_msgfds;
2502 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002503 int *write_msgfds;
2504 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002505
2506 SocketAddress *addr;
2507 bool is_listen;
2508 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002509
2510 guint reconnect_timer;
2511 int64_t reconnect_time;
aliguori6f97dba2008-10-31 18:49:55 +00002512} TCPCharDriver;
2513
Corey Minyard5dd1f022014-10-02 11:17:37 -05002514static gboolean socket_reconnect_timeout(gpointer opaque);
2515
2516static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2517{
2518 TCPCharDriver *s = chr->opaque;
2519 assert(s->connected == 0);
2520 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2521 socket_reconnect_timeout, chr);
2522}
2523
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302524static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002525
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002526#ifndef _WIN32
2527static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2528{
2529 TCPCharDriver *s = chr->opaque;
2530 struct msghdr msgh;
2531 struct iovec iov;
2532 int r;
2533
2534 size_t fd_size = s->write_msgfds_num * sizeof(int);
2535 char control[CMSG_SPACE(fd_size)];
2536 struct cmsghdr *cmsg;
2537
2538 memset(&msgh, 0, sizeof(msgh));
2539 memset(control, 0, sizeof(control));
2540
2541 /* set the payload */
2542 iov.iov_base = (uint8_t *) buf;
2543 iov.iov_len = len;
2544
2545 msgh.msg_iov = &iov;
2546 msgh.msg_iovlen = 1;
2547
2548 msgh.msg_control = control;
2549 msgh.msg_controllen = sizeof(control);
2550
2551 cmsg = CMSG_FIRSTHDR(&msgh);
2552
2553 cmsg->cmsg_len = CMSG_LEN(fd_size);
2554 cmsg->cmsg_level = SOL_SOCKET;
2555 cmsg->cmsg_type = SCM_RIGHTS;
2556 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2557
2558 do {
2559 r = sendmsg(s->fd, &msgh, 0);
2560 } while (r < 0 && errno == EINTR);
2561
2562 /* free the written msgfds, no matter what */
2563 if (s->write_msgfds_num) {
2564 g_free(s->write_msgfds);
2565 s->write_msgfds = 0;
2566 s->write_msgfds_num = 0;
2567 }
2568
2569 return r;
2570}
2571#endif
2572
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002573/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002574static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2575{
2576 TCPCharDriver *s = chr->opaque;
2577 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002578#ifndef _WIN32
2579 if (s->is_unix && s->write_msgfds_num) {
2580 return unix_send_msgfds(chr, buf, len);
2581 } else
2582#endif
2583 {
2584 return io_channel_send(s->chan, buf, len);
2585 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002586 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002587 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002588 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002589 }
2590}
2591
2592static int tcp_chr_read_poll(void *opaque)
2593{
2594 CharDriverState *chr = opaque;
2595 TCPCharDriver *s = chr->opaque;
2596 if (!s->connected)
2597 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002598 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002599 return s->max_size;
2600}
2601
2602#define IAC 255
2603#define IAC_BREAK 243
2604static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2605 TCPCharDriver *s,
2606 uint8_t *buf, int *size)
2607{
2608 /* Handle any telnet client's basic IAC options to satisfy char by
2609 * char mode with no echo. All IAC options will be removed from
2610 * the buf and the do_telnetopt variable will be used to track the
2611 * state of the width of the IAC information.
2612 *
2613 * IAC commands come in sets of 3 bytes with the exception of the
2614 * "IAC BREAK" command and the double IAC.
2615 */
2616
2617 int i;
2618 int j = 0;
2619
2620 for (i = 0; i < *size; i++) {
2621 if (s->do_telnetopt > 1) {
2622 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2623 /* Double IAC means send an IAC */
2624 if (j != i)
2625 buf[j] = buf[i];
2626 j++;
2627 s->do_telnetopt = 1;
2628 } else {
2629 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2630 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002631 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002632 s->do_telnetopt++;
2633 }
2634 s->do_telnetopt++;
2635 }
2636 if (s->do_telnetopt >= 4) {
2637 s->do_telnetopt = 1;
2638 }
2639 } else {
2640 if ((unsigned char)buf[i] == IAC) {
2641 s->do_telnetopt = 2;
2642 } else {
2643 if (j != i)
2644 buf[j] = buf[i];
2645 j++;
2646 }
2647 }
2648 }
2649 *size = j;
2650}
2651
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002652static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002653{
2654 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002655 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2656
2657 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002658 int i;
2659
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002660 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2661
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002662 /* Close unused fds */
2663 for (i = to_copy; i < s->read_msgfds_num; i++) {
2664 close(s->read_msgfds[i]);
2665 }
2666
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002667 g_free(s->read_msgfds);
2668 s->read_msgfds = 0;
2669 s->read_msgfds_num = 0;
2670 }
2671
2672 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002673}
2674
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002675static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2676{
2677 TCPCharDriver *s = chr->opaque;
2678
2679 /* clear old pending fd array */
2680 if (s->write_msgfds) {
2681 g_free(s->write_msgfds);
2682 }
2683
2684 if (num) {
2685 s->write_msgfds = g_malloc(num * sizeof(int));
2686 memcpy(s->write_msgfds, fds, num * sizeof(int));
2687 }
2688
2689 s->write_msgfds_num = num;
2690
2691 return 0;
2692}
2693
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002694#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002695static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2696{
2697 TCPCharDriver *s = chr->opaque;
2698 struct cmsghdr *cmsg;
2699
2700 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002701 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002702
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002703 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002704 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002705 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002706 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002707 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002708
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002709 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2710
2711 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002712 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002713 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002714
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002715 /* close and clean read_msgfds */
2716 for (i = 0; i < s->read_msgfds_num; i++) {
2717 close(s->read_msgfds[i]);
2718 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002719
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002720 if (s->read_msgfds_num) {
2721 g_free(s->read_msgfds);
2722 }
2723
2724 s->read_msgfds_num = fd_size / sizeof(int);
2725 s->read_msgfds = g_malloc(fd_size);
2726 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2727
2728 for (i = 0; i < s->read_msgfds_num; i++) {
2729 int fd = s->read_msgfds[i];
2730 if (fd < 0) {
2731 continue;
2732 }
2733
2734 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2735 qemu_set_block(fd);
2736
2737 #ifndef MSG_CMSG_CLOEXEC
2738 qemu_set_cloexec(fd);
2739 #endif
2740 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002741 }
2742}
2743
Mark McLoughlin9977c892009-07-22 09:11:38 +01002744static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2745{
2746 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002747 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002748 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002749 union {
2750 struct cmsghdr cmsg;
2751 char control[CMSG_SPACE(sizeof(int))];
2752 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002753 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002754 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002755
2756 iov[0].iov_base = buf;
2757 iov[0].iov_len = len;
2758
2759 msg.msg_iov = iov;
2760 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002761 msg.msg_control = &msg_control;
2762 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002763
Corey Bryant06138652012-08-14 16:43:42 -04002764#ifdef MSG_CMSG_CLOEXEC
2765 flags |= MSG_CMSG_CLOEXEC;
2766#endif
2767 ret = recvmsg(s->fd, &msg, flags);
2768 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002769 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002770 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002771
2772 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002773}
2774#else
2775static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2776{
2777 TCPCharDriver *s = chr->opaque;
Blue Swirl00aa0042011-07-23 20:04:29 +00002778 return qemu_recv(s->fd, buf, len, 0);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002779}
2780#endif
2781
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302782static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2783{
2784 TCPCharDriver *s = chr->opaque;
2785 return g_io_create_watch(s->chan, cond);
2786}
2787
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002788static void tcp_chr_disconnect(CharDriverState *chr)
2789{
2790 TCPCharDriver *s = chr->opaque;
2791
2792 s->connected = 0;
2793 if (s->listen_chan) {
2794 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2795 tcp_chr_accept, chr);
2796 }
2797 remove_fd_in_watch(chr);
2798 g_io_channel_unref(s->chan);
2799 s->chan = NULL;
2800 closesocket(s->fd);
2801 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002802 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2803 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002804 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002805 if (s->reconnect_time) {
2806 qemu_chr_socket_restart_timer(chr);
2807 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002808}
2809
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302810static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002811{
2812 CharDriverState *chr = opaque;
2813 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302814 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002815 int len, size;
2816
Kirill Batuzov812c1052014-07-01 15:52:32 +04002817 if (cond & G_IO_HUP) {
2818 /* connection closed */
2819 tcp_chr_disconnect(chr);
2820 return TRUE;
2821 }
2822
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302823 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002824 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302825 }
aliguori6f97dba2008-10-31 18:49:55 +00002826 len = sizeof(buf);
2827 if (len > s->max_size)
2828 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002829 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002830 if (size == 0) {
2831 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002832 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002833 } else if (size > 0) {
2834 if (s->do_telnetopt)
2835 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2836 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002837 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002838 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302839
2840 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002841}
2842
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002843static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2844{
2845 TCPCharDriver *s = chr->opaque;
2846 int size;
2847
2848 if (!s->connected) {
2849 return 0;
2850 }
2851
2852 size = tcp_chr_recv(chr, (void *) buf, len);
2853 if (size == 0) {
2854 /* connection closed */
2855 tcp_chr_disconnect(chr);
2856 }
2857
2858 return size;
2859}
2860
Blue Swirl68c18d12010-08-15 09:46:24 +00002861#ifndef _WIN32
2862CharDriverState *qemu_chr_open_eventfd(int eventfd)
2863{
David Marchande9d21c42014-06-11 17:25:16 +02002864 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2865
2866 if (chr) {
2867 chr->avail_connections = 1;
2868 }
2869
2870 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002871}
Blue Swirl68c18d12010-08-15 09:46:24 +00002872#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002873
aliguori6f97dba2008-10-31 18:49:55 +00002874static void tcp_chr_connect(void *opaque)
2875{
2876 CharDriverState *chr = opaque;
2877 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002878 struct sockaddr_storage ss, ps;
2879 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002880
2881 memset(&ss, 0, ss_len);
2882 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2883 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2884 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002885 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2886 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2887 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002888 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002889 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2890 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002891 s->is_listen, s->is_telnet);
2892 }
aliguori6f97dba2008-10-31 18:49:55 +00002893
2894 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302895 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302896 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2897 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002898 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002899 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002900}
2901
Gal Hammerac1b84d2014-02-25 12:12:35 +02002902static void tcp_chr_update_read_handler(CharDriverState *chr)
2903{
2904 TCPCharDriver *s = chr->opaque;
2905
2906 remove_fd_in_watch(chr);
2907 if (s->chan) {
2908 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2909 tcp_chr_read, chr);
2910 }
2911}
2912
aliguori6f97dba2008-10-31 18:49:55 +00002913#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2914static void tcp_chr_telnet_init(int fd)
2915{
2916 char buf[3];
2917 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2918 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2919 send(fd, (char *)buf, 3, 0);
2920 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2921 send(fd, (char *)buf, 3, 0);
2922 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2923 send(fd, (char *)buf, 3, 0);
2924 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2925 send(fd, (char *)buf, 3, 0);
2926}
2927
Daniel P. Berrange13661082011-06-23 13:31:42 +01002928static int tcp_chr_add_client(CharDriverState *chr, int fd)
2929{
2930 TCPCharDriver *s = chr->opaque;
2931 if (s->fd != -1)
2932 return -1;
2933
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002934 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002935 if (s->do_nodelay)
2936 socket_set_nodelay(fd);
2937 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302938 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002939 if (s->listen_tag) {
2940 g_source_remove(s->listen_tag);
2941 s->listen_tag = 0;
2942 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002943 tcp_chr_connect(chr);
2944
2945 return 0;
2946}
2947
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302948static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002949{
2950 CharDriverState *chr = opaque;
2951 TCPCharDriver *s = chr->opaque;
2952 struct sockaddr_in saddr;
2953#ifndef _WIN32
2954 struct sockaddr_un uaddr;
2955#endif
2956 struct sockaddr *addr;
2957 socklen_t len;
2958 int fd;
2959
2960 for(;;) {
2961#ifndef _WIN32
2962 if (s->is_unix) {
2963 len = sizeof(uaddr);
2964 addr = (struct sockaddr *)&uaddr;
2965 } else
2966#endif
2967 {
2968 len = sizeof(saddr);
2969 addr = (struct sockaddr *)&saddr;
2970 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002971 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002972 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02002973 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302974 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00002975 } else if (fd >= 0) {
2976 if (s->do_telnetopt)
2977 tcp_chr_telnet_init(fd);
2978 break;
2979 }
2980 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002981 if (tcp_chr_add_client(chr, fd) < 0)
2982 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302983
2984 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002985}
2986
2987static void tcp_chr_close(CharDriverState *chr)
2988{
2989 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002990 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002991
Corey Minyard5dd1f022014-10-02 11:17:37 -05002992 if (s->reconnect_timer) {
2993 g_source_remove(s->reconnect_timer);
2994 s->reconnect_timer = 0;
2995 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05002996 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00002997 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05302998 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302999 if (s->chan) {
3000 g_io_channel_unref(s->chan);
3001 }
aliguori6f97dba2008-10-31 18:49:55 +00003002 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003003 }
3004 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303005 if (s->listen_tag) {
3006 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003007 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303008 }
3009 if (s->listen_chan) {
3010 g_io_channel_unref(s->listen_chan);
3011 }
aliguori6f97dba2008-10-31 18:49:55 +00003012 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003013 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003014 if (s->read_msgfds_num) {
3015 for (i = 0; i < s->read_msgfds_num; i++) {
3016 close(s->read_msgfds[i]);
3017 }
3018 g_free(s->read_msgfds);
3019 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003020 if (s->write_msgfds_num) {
3021 g_free(s->write_msgfds);
3022 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003023 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003024 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003025}
3026
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003027static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003028{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003029 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003030
Corey Minyardcfb429c2014-10-02 11:17:35 -05003031 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003032 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303033 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003034 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3035 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003036 } else {
3037 s->connected = 1;
3038 s->fd = fd;
3039 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303040 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003041 tcp_chr_connect(chr);
3042 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003043}
3044
Corey Minyard51795022014-10-08 07:11:56 -05003045static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003046{
3047 CharDriverState *chr = opaque;
3048
3049 if (fd < 0) {
Corey Minyard51795022014-10-08 07:11:56 -05003050 error_report("Unable to connect to char device %s: %s",
3051 chr->label, error_get_pretty(err));
Corey Minyard5dd1f022014-10-02 11:17:37 -05003052 qemu_chr_socket_restart_timer(chr);
3053 return;
3054 }
3055
3056 qemu_chr_finish_socket_connection(chr, fd);
3057}
3058
Corey Minyardcfb429c2014-10-02 11:17:35 -05003059static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003060{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003061 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003062 int fd;
3063
Corey Minyardcfb429c2014-10-02 11:17:35 -05003064 if (s->is_listen) {
3065 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003066 } else if (s->reconnect_time) {
3067 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3068 return fd >= 0;
3069 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003070 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003071 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003072 if (fd < 0) {
3073 return false;
3074 }
3075
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003076 qemu_chr_finish_socket_connection(chr, fd);
3077 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003078}
3079
Lei Li51767e72013-01-25 00:03:19 +08003080/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003081/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003082
3083typedef struct {
3084 size_t size;
3085 size_t prod;
3086 size_t cons;
3087 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003088} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003089
Markus Armbruster3949e592013-02-06 21:27:24 +01003090static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003091{
Markus Armbruster3949e592013-02-06 21:27:24 +01003092 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003093
Markus Armbruster5c230102013-02-06 21:27:23 +01003094 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003095}
3096
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003097/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003098static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003099{
Markus Armbruster3949e592013-02-06 21:27:24 +01003100 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003101 int i;
3102
3103 if (!buf || (len < 0)) {
3104 return -1;
3105 }
3106
3107 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003108 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3109 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003110 d->cons = d->prod - d->size;
3111 }
3112 }
3113
3114 return 0;
3115}
3116
Markus Armbruster3949e592013-02-06 21:27:24 +01003117static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003118{
Markus Armbruster3949e592013-02-06 21:27:24 +01003119 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003120 int i;
3121
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003122 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003123 for (i = 0; i < len && d->cons != d->prod; i++) {
3124 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003125 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003126 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003127
3128 return i;
3129}
3130
Markus Armbruster3949e592013-02-06 21:27:24 +01003131static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003132{
Markus Armbruster3949e592013-02-06 21:27:24 +01003133 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003134
3135 g_free(d->cbuf);
3136 g_free(d);
3137 chr->opaque = NULL;
3138}
3139
Markus Armbruster4f573782013-07-26 16:44:32 +02003140static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3141 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003142{
3143 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003144 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003145
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003146 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003147 d = g_malloc(sizeof(*d));
3148
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003149 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003150
3151 /* The size must be power of 2 */
3152 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003153 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003154 goto fail;
3155 }
3156
3157 d->prod = 0;
3158 d->cons = 0;
3159 d->cbuf = g_malloc0(d->size);
3160
3161 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003162 chr->chr_write = ringbuf_chr_write;
3163 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003164
3165 return chr;
3166
3167fail:
3168 g_free(d);
3169 g_free(chr);
3170 return NULL;
3171}
3172
Hani Benhabiles8e597772014-05-27 23:39:30 +01003173bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003174{
Markus Armbruster3949e592013-02-06 21:27:24 +01003175 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003176}
3177
Markus Armbruster3949e592013-02-06 21:27:24 +01003178void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003179 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003180 Error **errp)
3181{
3182 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003183 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003184 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003185 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003186
3187 chr = qemu_chr_find(device);
3188 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003189 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003190 return;
3191 }
3192
Markus Armbruster3949e592013-02-06 21:27:24 +01003193 if (!chr_is_ringbuf(chr)) {
3194 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003195 return;
3196 }
3197
Lei Li1f590cf2013-01-25 00:03:20 +08003198 if (has_format && (format == DATA_FORMAT_BASE64)) {
3199 write_data = g_base64_decode(data, &write_count);
3200 } else {
3201 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003202 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003203 }
3204
Markus Armbruster3949e592013-02-06 21:27:24 +01003205 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003206
Markus Armbruster13289fb2013-02-06 21:27:18 +01003207 if (write_data != (uint8_t *)data) {
3208 g_free((void *)write_data);
3209 }
3210
Lei Li1f590cf2013-01-25 00:03:20 +08003211 if (ret < 0) {
3212 error_setg(errp, "Failed to write to device %s", device);
3213 return;
3214 }
3215}
3216
Markus Armbruster3949e592013-02-06 21:27:24 +01003217char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003218 bool has_format, enum DataFormat format,
3219 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003220{
3221 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003222 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003223 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003224 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003225
3226 chr = qemu_chr_find(device);
3227 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003228 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003229 return NULL;
3230 }
3231
Markus Armbruster3949e592013-02-06 21:27:24 +01003232 if (!chr_is_ringbuf(chr)) {
3233 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003234 return NULL;
3235 }
3236
3237 if (size <= 0) {
3238 error_setg(errp, "size must be greater than zero");
3239 return NULL;
3240 }
3241
Markus Armbruster3949e592013-02-06 21:27:24 +01003242 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003243 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003244 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003245
Markus Armbruster3949e592013-02-06 21:27:24 +01003246 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003247
3248 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003249 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003250 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003251 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003252 /*
3253 * FIXME should read only complete, valid UTF-8 characters up
3254 * to @size bytes. Invalid sequences should be replaced by a
3255 * suitable replacement character. Except when (and only
3256 * when) ring buffer lost characters since last read, initial
3257 * continuation characters should be dropped.
3258 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003259 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003260 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003261 }
3262
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003263 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003264}
3265
Gerd Hoffmann33521632009-12-08 13:11:49 +01003266QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003267{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003268 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003269 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003270 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003271 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003272 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003273
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003274 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003275 if (local_err) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003276 qerror_report_err(local_err);
3277 error_free(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003278 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003279 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003280
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003281 if (strstart(filename, "mon:", &p)) {
3282 filename = p;
3283 qemu_opt_set(opts, "mux", "on");
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003284 if (strcmp(filename, "stdio") == 0) {
3285 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3286 * but pass it to the guest. Handle this only for compat syntax,
3287 * for -chardev syntax we have special option for this.
3288 * This is what -nographic did, redirecting+muxing serial+monitor
3289 * to stdio causing Ctrl+C to be passed to guest. */
3290 qemu_opt_set(opts, "signal", "off");
3291 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003292 }
3293
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003294 if (strcmp(filename, "null") == 0 ||
3295 strcmp(filename, "pty") == 0 ||
3296 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003297 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003298 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003299 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02003300 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003301 return opts;
3302 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003303 if (strstart(filename, "vc", &p)) {
3304 qemu_opt_set(opts, "backend", "vc");
3305 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003306 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003307 /* pixels */
3308 qemu_opt_set(opts, "width", width);
3309 qemu_opt_set(opts, "height", height);
Stefan Weil49aa4052013-09-30 23:04:49 +02003310 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003311 /* chars */
3312 qemu_opt_set(opts, "cols", width);
3313 qemu_opt_set(opts, "rows", height);
3314 } else {
3315 goto fail;
3316 }
3317 }
3318 return opts;
3319 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003320 if (strcmp(filename, "con:") == 0) {
3321 qemu_opt_set(opts, "backend", "console");
3322 return opts;
3323 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003324 if (strstart(filename, "COM", NULL)) {
3325 qemu_opt_set(opts, "backend", "serial");
3326 qemu_opt_set(opts, "path", filename);
3327 return opts;
3328 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003329 if (strstart(filename, "file:", &p)) {
3330 qemu_opt_set(opts, "backend", "file");
3331 qemu_opt_set(opts, "path", p);
3332 return opts;
3333 }
3334 if (strstart(filename, "pipe:", &p)) {
3335 qemu_opt_set(opts, "backend", "pipe");
3336 qemu_opt_set(opts, "path", p);
3337 return opts;
3338 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003339 if (strstart(filename, "tcp:", &p) ||
3340 strstart(filename, "telnet:", &p)) {
3341 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3342 host[0] = 0;
3343 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3344 goto fail;
3345 }
3346 qemu_opt_set(opts, "backend", "socket");
3347 qemu_opt_set(opts, "host", host);
3348 qemu_opt_set(opts, "port", port);
3349 if (p[pos] == ',') {
3350 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
3351 goto fail;
3352 }
3353 if (strstart(filename, "telnet:", &p))
3354 qemu_opt_set(opts, "telnet", "on");
3355 return opts;
3356 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003357 if (strstart(filename, "udp:", &p)) {
3358 qemu_opt_set(opts, "backend", "udp");
3359 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3360 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003361 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003362 goto fail;
3363 }
3364 }
3365 qemu_opt_set(opts, "host", host);
3366 qemu_opt_set(opts, "port", port);
3367 if (p[pos] == '@') {
3368 p += pos + 1;
3369 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3370 host[0] = 0;
3371 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003372 goto fail;
3373 }
3374 }
3375 qemu_opt_set(opts, "localaddr", host);
3376 qemu_opt_set(opts, "localport", port);
3377 }
3378 return opts;
3379 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003380 if (strstart(filename, "unix:", &p)) {
3381 qemu_opt_set(opts, "backend", "socket");
3382 if (qemu_opts_do_parse(opts, p, "path") != 0)
3383 goto fail;
3384 return opts;
3385 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003386 if (strstart(filename, "/dev/parport", NULL) ||
3387 strstart(filename, "/dev/ppi", NULL)) {
3388 qemu_opt_set(opts, "backend", "parport");
3389 qemu_opt_set(opts, "path", filename);
3390 return opts;
3391 }
3392 if (strstart(filename, "/dev/", NULL)) {
3393 qemu_opt_set(opts, "backend", "tty");
3394 qemu_opt_set(opts, "path", filename);
3395 return opts;
3396 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003397
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003398fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003399 qemu_opts_del(opts);
3400 return NULL;
3401}
3402
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003403static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3404 Error **errp)
3405{
3406 const char *path = qemu_opt_get(opts, "path");
3407
3408 if (path == NULL) {
3409 error_setg(errp, "chardev: file: no filename given");
3410 return;
3411 }
3412 backend->file = g_new0(ChardevFile, 1);
3413 backend->file->out = g_strdup(path);
3414}
3415
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003416static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3417 Error **errp)
3418{
3419 backend->stdio = g_new0(ChardevStdio, 1);
3420 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003421 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003422}
3423
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003424static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3425 Error **errp)
3426{
3427 const char *device = qemu_opt_get(opts, "path");
3428
3429 if (device == NULL) {
3430 error_setg(errp, "chardev: serial/tty: no device path given");
3431 return;
3432 }
3433 backend->serial = g_new0(ChardevHostdev, 1);
3434 backend->serial->device = g_strdup(device);
3435}
3436
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003437static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3438 Error **errp)
3439{
3440 const char *device = qemu_opt_get(opts, "path");
3441
3442 if (device == NULL) {
3443 error_setg(errp, "chardev: parallel: no device path given");
3444 return;
3445 }
3446 backend->parallel = g_new0(ChardevHostdev, 1);
3447 backend->parallel->device = g_strdup(device);
3448}
3449
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003450static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3451 Error **errp)
3452{
3453 const char *device = qemu_opt_get(opts, "path");
3454
3455 if (device == NULL) {
3456 error_setg(errp, "chardev: pipe: no device path given");
3457 return;
3458 }
3459 backend->pipe = g_new0(ChardevHostdev, 1);
3460 backend->pipe->device = g_strdup(device);
3461}
3462
Markus Armbruster4f573782013-07-26 16:44:32 +02003463static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3464 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003465{
3466 int val;
3467
Markus Armbruster3a1da422013-07-26 16:44:34 +02003468 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003469
Markus Armbruster0f953052013-06-27 16:22:07 +02003470 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003471 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003472 backend->ringbuf->has_size = true;
3473 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003474 }
3475}
3476
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003477static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3478 Error **errp)
3479{
3480 const char *chardev = qemu_opt_get(opts, "chardev");
3481
3482 if (chardev == NULL) {
3483 error_setg(errp, "chardev: mux: no chardev given");
3484 return;
3485 }
3486 backend->mux = g_new0(ChardevMux, 1);
3487 backend->mux->chardev = g_strdup(chardev);
3488}
3489
Peter Maydelldafd3252014-09-02 11:24:13 +01003490static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3491 Error **errp)
3492{
3493 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3494 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3495 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3496 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003497 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003498 const char *path = qemu_opt_get(opts, "path");
3499 const char *host = qemu_opt_get(opts, "host");
3500 const char *port = qemu_opt_get(opts, "port");
3501 SocketAddress *addr;
3502
3503 if (!path) {
3504 if (!host) {
3505 error_setg(errp, "chardev: socket: no host given");
3506 return;
3507 }
3508 if (!port) {
3509 error_setg(errp, "chardev: socket: no port given");
3510 return;
3511 }
3512 }
3513
3514 backend->socket = g_new0(ChardevSocket, 1);
3515
3516 backend->socket->has_nodelay = true;
3517 backend->socket->nodelay = do_nodelay;
3518 backend->socket->has_server = true;
3519 backend->socket->server = is_listen;
3520 backend->socket->has_telnet = true;
3521 backend->socket->telnet = is_telnet;
3522 backend->socket->has_wait = true;
3523 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003524 backend->socket->has_reconnect = true;
3525 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003526
3527 addr = g_new0(SocketAddress, 1);
3528 if (path) {
3529 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3530 addr->q_unix = g_new0(UnixSocketAddress, 1);
3531 addr->q_unix->path = g_strdup(path);
3532 } else {
3533 addr->kind = SOCKET_ADDRESS_KIND_INET;
3534 addr->inet = g_new0(InetSocketAddress, 1);
3535 addr->inet->host = g_strdup(host);
3536 addr->inet->port = g_strdup(port);
3537 addr->inet->has_to = qemu_opt_get(opts, "to");
3538 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3539 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3540 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3541 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3542 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3543 }
3544 backend->socket->addr = addr;
3545}
3546
Peter Maydell90a14bf2014-09-02 11:24:15 +01003547static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3548 Error **errp)
3549{
3550 const char *host = qemu_opt_get(opts, "host");
3551 const char *port = qemu_opt_get(opts, "port");
3552 const char *localaddr = qemu_opt_get(opts, "localaddr");
3553 const char *localport = qemu_opt_get(opts, "localport");
3554 bool has_local = false;
3555 SocketAddress *addr;
3556
3557 if (host == NULL || strlen(host) == 0) {
3558 host = "localhost";
3559 }
3560 if (port == NULL || strlen(port) == 0) {
3561 error_setg(errp, "chardev: udp: remote port not specified");
3562 return;
3563 }
3564 if (localport == NULL || strlen(localport) == 0) {
3565 localport = "0";
3566 } else {
3567 has_local = true;
3568 }
3569 if (localaddr == NULL || strlen(localaddr) == 0) {
3570 localaddr = "";
3571 } else {
3572 has_local = true;
3573 }
3574
3575 backend->udp = g_new0(ChardevUdp, 1);
3576
3577 addr = g_new0(SocketAddress, 1);
3578 addr->kind = SOCKET_ADDRESS_KIND_INET;
3579 addr->inet = g_new0(InetSocketAddress, 1);
3580 addr->inet->host = g_strdup(host);
3581 addr->inet->port = g_strdup(port);
3582 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3583 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3584 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3585 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3586 backend->udp->remote = addr;
3587
3588 if (has_local) {
3589 backend->udp->has_local = true;
3590 addr = g_new0(SocketAddress, 1);
3591 addr->kind = SOCKET_ADDRESS_KIND_INET;
3592 addr->inet = g_new0(InetSocketAddress, 1);
3593 addr->inet->host = g_strdup(localaddr);
3594 addr->inet->port = g_strdup(localport);
3595 backend->udp->local = addr;
3596 }
3597}
3598
Anthony Liguorid654f342013-03-05 23:21:28 +05303599typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003600 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003601 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003602 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303603} CharDriver;
3604
3605static GSList *backends;
3606
Peter Maydelle4d50d42014-09-02 11:24:17 +01003607void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003608 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3609{
3610 CharDriver *s;
3611
3612 s = g_malloc0(sizeof(*s));
3613 s->name = g_strdup(name);
3614 s->kind = kind;
3615 s->parse = parse;
3616
3617 backends = g_slist_append(backends, s);
3618}
3619
Anthony Liguorif69554b2011-08-15 11:17:37 -05003620CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003621 void (*init)(struct CharDriverState *s),
3622 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003623{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003624 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303625 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003626 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303627 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003628 ChardevReturn *ret = NULL;
3629 ChardevBackend *backend;
3630 const char *id = qemu_opts_id(opts);
3631 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003632
Peter Maydella61ae7f2014-09-02 11:24:16 +01003633 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003634 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003635 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003636 }
3637
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003638 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003639 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003640 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003641 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003642 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303643 for (i = backends; i; i = i->next) {
3644 cd = i->data;
3645
3646 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003647 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303648 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003649 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303650 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003651 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003652 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003653 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003654 }
3655
Peter Maydella61ae7f2014-09-02 11:24:16 +01003656 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003657
3658 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003659 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003660 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003661
3662 chr = NULL;
3663 backend->kind = cd->kind;
3664 if (cd->parse) {
3665 cd->parse(opts, backend, &local_err);
3666 if (local_err) {
3667 error_propagate(errp, local_err);
3668 goto qapi_out;
3669 }
3670 }
3671 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3672 if (!ret) {
3673 goto qapi_out;
3674 }
3675
3676 if (bid) {
3677 qapi_free_ChardevBackend(backend);
3678 qapi_free_ChardevReturn(ret);
3679 backend = g_new0(ChardevBackend, 1);
3680 backend->mux = g_new0(ChardevMux, 1);
3681 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3682 backend->mux->chardev = g_strdup(bid);
3683 ret = qmp_chardev_add(id, backend, errp);
3684 if (!ret) {
3685 chr = qemu_chr_find(bid);
3686 qemu_chr_delete(chr);
3687 chr = NULL;
3688 goto qapi_out;
3689 }
3690 }
3691
3692 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003693 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003694
3695qapi_out:
3696 qapi_free_ChardevBackend(backend);
3697 qapi_free_ChardevReturn(ret);
3698 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003699 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003700
3701err:
3702 qemu_opts_del(opts);
3703 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003704}
3705
Anthony Liguori27143a42011-08-15 11:17:36 -05003706CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003707{
3708 const char *p;
3709 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003710 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003711 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003712
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003713 if (strstart(filename, "chardev:", &p)) {
3714 return qemu_chr_find(p);
3715 }
3716
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003717 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003718 if (!opts)
3719 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003720
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003721 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003722 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04003723 error_report("%s", error_get_pretty(err));
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003724 error_free(err);
3725 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003726 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003727 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003728 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003729 }
3730 return chr;
3731}
3732
Anthony Liguori15f31512011-08-15 11:17:35 -05003733void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003734{
3735 if (chr->chr_set_echo) {
3736 chr->chr_set_echo(chr, echo);
3737 }
3738}
3739
Hans de Goede8e25daa2013-03-26 11:07:57 +01003740void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003741{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003742 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003743 return;
3744 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003745 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003746 if (chr->chr_set_fe_open) {
3747 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003748 }
3749}
3750
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003751void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3752{
3753 if (chr->chr_fe_event) {
3754 chr->chr_fe_event(chr, event);
3755 }
3756}
3757
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003758int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3759 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303760{
3761 GSource *src;
3762 guint tag;
3763
3764 if (s->chr_add_watch == NULL) {
3765 return -ENOSYS;
3766 }
3767
3768 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003769 if (!src) {
3770 return -EINVAL;
3771 }
3772
Anthony Liguori23673ca2013-03-05 23:21:23 +05303773 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3774 tag = g_source_attach(src, NULL);
3775 g_source_unref(src);
3776
3777 return tag;
3778}
3779
Hans de Goede44c473d2013-03-27 20:29:39 +01003780int qemu_chr_fe_claim(CharDriverState *s)
3781{
3782 if (s->avail_connections < 1) {
3783 return -1;
3784 }
3785 s->avail_connections--;
3786 return 0;
3787}
3788
3789void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3790{
3791 if (qemu_chr_fe_claim(s) != 0) {
3792 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3793 __func__, s->label);
3794 exit(1);
3795 }
3796}
3797
3798void qemu_chr_fe_release(CharDriverState *s)
3799{
3800 s->avail_connections++;
3801}
3802
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003803void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003804{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003805 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003806 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003807 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003808 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003809 g_free(chr->filename);
3810 g_free(chr->label);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003811 if (chr->opts) {
3812 qemu_opts_del(chr->opts);
3813 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003814 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003815}
3816
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003817ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003818{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003819 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003820 CharDriverState *chr;
3821
Blue Swirl72cf2d42009-09-12 07:36:22 +00003822 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003823 ChardevInfoList *info = g_malloc0(sizeof(*info));
3824 info->value = g_malloc0(sizeof(*info->value));
3825 info->value->label = g_strdup(chr->label);
3826 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003827 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003828
3829 info->next = chr_list;
3830 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003831 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003832
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003833 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003834}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003835
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003836ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3837{
3838 ChardevBackendInfoList *backend_list = NULL;
3839 CharDriver *c = NULL;
3840 GSList *i = NULL;
3841
3842 for (i = backends; i; i = i->next) {
3843 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3844 c = i->data;
3845 info->value = g_malloc0(sizeof(*info->value));
3846 info->value->name = g_strdup(c->name);
3847
3848 info->next = backend_list;
3849 backend_list = info;
3850 }
3851
3852 return backend_list;
3853}
3854
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003855CharDriverState *qemu_chr_find(const char *name)
3856{
3857 CharDriverState *chr;
3858
Blue Swirl72cf2d42009-09-12 07:36:22 +00003859 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003860 if (strcmp(chr->label, name) != 0)
3861 continue;
3862 return chr;
3863 }
3864 return NULL;
3865}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003866
3867/* Get a character (serial) device interface. */
3868CharDriverState *qemu_char_get_next_serial(void)
3869{
3870 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003871 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003872
3873 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003874
3875 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3876 chr = serial_hds[next_serial++];
3877 qemu_chr_fe_claim_no_fail(chr);
3878 return chr;
3879 }
3880 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003881}
3882
Paolo Bonzini4d454572012-11-26 16:03:42 +01003883QemuOptsList qemu_chardev_opts = {
3884 .name = "chardev",
3885 .implied_opt_name = "backend",
3886 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3887 .desc = {
3888 {
3889 .name = "backend",
3890 .type = QEMU_OPT_STRING,
3891 },{
3892 .name = "path",
3893 .type = QEMU_OPT_STRING,
3894 },{
3895 .name = "host",
3896 .type = QEMU_OPT_STRING,
3897 },{
3898 .name = "port",
3899 .type = QEMU_OPT_STRING,
3900 },{
3901 .name = "localaddr",
3902 .type = QEMU_OPT_STRING,
3903 },{
3904 .name = "localport",
3905 .type = QEMU_OPT_STRING,
3906 },{
3907 .name = "to",
3908 .type = QEMU_OPT_NUMBER,
3909 },{
3910 .name = "ipv4",
3911 .type = QEMU_OPT_BOOL,
3912 },{
3913 .name = "ipv6",
3914 .type = QEMU_OPT_BOOL,
3915 },{
3916 .name = "wait",
3917 .type = QEMU_OPT_BOOL,
3918 },{
3919 .name = "server",
3920 .type = QEMU_OPT_BOOL,
3921 },{
3922 .name = "delay",
3923 .type = QEMU_OPT_BOOL,
3924 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003925 .name = "reconnect",
3926 .type = QEMU_OPT_NUMBER,
3927 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003928 .name = "telnet",
3929 .type = QEMU_OPT_BOOL,
3930 },{
3931 .name = "width",
3932 .type = QEMU_OPT_NUMBER,
3933 },{
3934 .name = "height",
3935 .type = QEMU_OPT_NUMBER,
3936 },{
3937 .name = "cols",
3938 .type = QEMU_OPT_NUMBER,
3939 },{
3940 .name = "rows",
3941 .type = QEMU_OPT_NUMBER,
3942 },{
3943 .name = "mux",
3944 .type = QEMU_OPT_BOOL,
3945 },{
3946 .name = "signal",
3947 .type = QEMU_OPT_BOOL,
3948 },{
3949 .name = "name",
3950 .type = QEMU_OPT_STRING,
3951 },{
3952 .name = "debug",
3953 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003954 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003955 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003956 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003957 },{
3958 .name = "chardev",
3959 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003960 },
3961 { /* end of list */ }
3962 },
3963};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01003964
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003965#ifdef _WIN32
3966
3967static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
3968{
3969 HANDLE out;
3970
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02003971 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003972 error_setg(errp, "input file not supported");
3973 return NULL;
3974 }
3975
3976 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3977 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3978 if (out == INVALID_HANDLE_VALUE) {
3979 error_setg(errp, "open %s failed", file->out);
3980 return NULL;
3981 }
3982 return qemu_chr_open_win_file(out);
3983}
3984
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003985static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
3986 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003987{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01003988 return qemu_chr_open_win_path(serial->device);
3989}
3990
3991static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
3992 Error **errp)
3993{
3994 error_setg(errp, "character device backend type 'parallel' not supported");
3995 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01003996}
3997
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01003998#else /* WIN32 */
3999
4000static int qmp_chardev_open_file_source(char *src, int flags,
4001 Error **errp)
4002{
4003 int fd = -1;
4004
4005 TFR(fd = qemu_open(src, flags, 0666));
4006 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004007 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004008 }
4009 return fd;
4010}
4011
4012static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4013{
Markus Armbruster5f758362014-05-19 18:57:34 +02004014 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004015
4016 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4017 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004018 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004019 return NULL;
4020 }
4021
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004022 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004023 flags = O_RDONLY;
4024 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004025 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004026 qemu_close(out);
4027 return NULL;
4028 }
4029 }
4030
4031 return qemu_chr_open_fd(in, out);
4032}
4033
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004034static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4035 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004036{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004037#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004038 int fd;
4039
4040 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004041 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004042 return NULL;
4043 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004044 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004045 return qemu_chr_open_tty_fd(fd);
4046#else
4047 error_setg(errp, "character device backend type 'serial' not supported");
4048 return NULL;
4049#endif
4050}
4051
4052static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4053 Error **errp)
4054{
4055#ifdef HAVE_CHARDEV_PARPORT
4056 int fd;
4057
4058 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004059 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004060 return NULL;
4061 }
4062 return qemu_chr_open_pp_fd(fd);
4063#else
4064 error_setg(errp, "character device backend type 'parallel' not supported");
4065 return NULL;
4066#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004067}
4068
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004069#endif /* WIN32 */
4070
Corey Minyard5dd1f022014-10-02 11:17:37 -05004071static gboolean socket_reconnect_timeout(gpointer opaque)
4072{
4073 CharDriverState *chr = opaque;
4074 TCPCharDriver *s = chr->opaque;
4075 Error *err;
4076
4077 s->reconnect_timer = 0;
4078
4079 if (chr->be_open) {
4080 return false;
4081 }
4082
4083 if (!qemu_chr_open_socket_fd(chr, &err)) {
Corey Minyard51795022014-10-08 07:11:56 -05004084 error_report("Unable to connect to char device %s: %s\n",
4085 chr->label, error_get_pretty(err));
Corey Minyard5dd1f022014-10-02 11:17:37 -05004086 qemu_chr_socket_restart_timer(chr);
4087 }
4088
4089 return false;
4090}
4091
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004092static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
4093 Error **errp)
4094{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004095 CharDriverState *chr;
4096 TCPCharDriver *s;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004097 SocketAddress *addr = sock->addr;
4098 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4099 bool is_listen = sock->has_server ? sock->server : true;
4100 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4101 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004102 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004103
4104 chr = qemu_chr_alloc();
4105 s = g_malloc0(sizeof(TCPCharDriver));
4106
4107 s->fd = -1;
4108 s->listen_fd = -1;
4109 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004110 s->is_listen = is_listen;
4111 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004112 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004113 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004114
4115 chr->opaque = s;
4116 chr->chr_write = tcp_chr_write;
4117 chr->chr_sync_read = tcp_chr_sync_read;
4118 chr->chr_close = tcp_chr_close;
4119 chr->get_msgfds = tcp_get_msgfds;
4120 chr->set_msgfds = tcp_set_msgfds;
4121 chr->chr_add_client = tcp_chr_add_client;
4122 chr->chr_add_watch = tcp_chr_add_watch;
4123 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4124 /* be isn't opened until we get a connection */
4125 chr->explicit_be_open = true;
4126
4127 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004128 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4129 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004130
4131 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004132 if (is_telnet) {
4133 s->do_telnetopt = 1;
4134 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004135 } else if (reconnect > 0) {
4136 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004137 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004138
Corey Minyardcfb429c2014-10-02 11:17:35 -05004139 if (!qemu_chr_open_socket_fd(chr, errp)) {
Corey Minyard5dd1f022014-10-02 11:17:37 -05004140 if (s->reconnect_time) {
4141 qemu_chr_socket_restart_timer(chr);
4142 } else {
4143 g_free(s);
4144 g_free(chr->filename);
4145 g_free(chr);
4146 return NULL;
4147 }
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004148 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004149
4150 if (is_listen && is_waitconnect) {
4151 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4152 chr->filename);
4153 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4154 qemu_set_nonblock(s->listen_fd);
4155 }
4156
4157 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004158}
4159
Lei Li08d0ab32013-05-20 14:51:03 +08004160static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4161 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004162{
4163 int fd;
4164
Lei Li08d0ab32013-05-20 14:51:03 +08004165 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004166 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004167 return NULL;
4168 }
4169 return qemu_chr_open_udp_fd(fd);
4170}
4171
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004172ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4173 Error **errp)
4174{
4175 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004176 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004177
4178 chr = qemu_chr_find(id);
4179 if (chr) {
4180 error_setg(errp, "Chardev '%s' already exists", id);
4181 g_free(ret);
4182 return NULL;
4183 }
4184
4185 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004186 case CHARDEV_BACKEND_KIND_FILE:
4187 chr = qmp_chardev_open_file(backend->file, errp);
4188 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004189 case CHARDEV_BACKEND_KIND_SERIAL:
4190 chr = qmp_chardev_open_serial(backend->serial, errp);
4191 break;
4192 case CHARDEV_BACKEND_KIND_PARALLEL:
4193 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004194 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004195 case CHARDEV_BACKEND_KIND_PIPE:
4196 chr = qemu_chr_open_pipe(backend->pipe);
4197 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004198 case CHARDEV_BACKEND_KIND_SOCKET:
4199 chr = qmp_chardev_open_socket(backend->socket, errp);
4200 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004201 case CHARDEV_BACKEND_KIND_UDP:
4202 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004203 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004204#ifdef HAVE_CHARDEV_TTY
4205 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004206 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004207 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004208#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004209 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004210 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004211 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004212 case CHARDEV_BACKEND_KIND_MUX:
4213 base = qemu_chr_find(backend->mux->chardev);
4214 if (base == NULL) {
4215 error_setg(errp, "mux: base chardev %s not found",
4216 backend->mux->chardev);
4217 break;
4218 }
4219 chr = qemu_chr_open_mux(base);
4220 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004221 case CHARDEV_BACKEND_KIND_MSMOUSE:
4222 chr = qemu_chr_open_msmouse();
4223 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004224#ifdef CONFIG_BRLAPI
4225 case CHARDEV_BACKEND_KIND_BRAILLE:
4226 chr = chr_baum_init();
4227 break;
4228#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004229 case CHARDEV_BACKEND_KIND_TESTDEV:
4230 chr = chr_testdev_init();
4231 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004232 case CHARDEV_BACKEND_KIND_STDIO:
4233 chr = qemu_chr_open_stdio(backend->stdio);
4234 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004235#ifdef _WIN32
4236 case CHARDEV_BACKEND_KIND_CONSOLE:
4237 chr = qemu_chr_open_win_con();
4238 break;
4239#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004240#ifdef CONFIG_SPICE
4241 case CHARDEV_BACKEND_KIND_SPICEVMC:
4242 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4243 break;
4244 case CHARDEV_BACKEND_KIND_SPICEPORT:
4245 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4246 break;
4247#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004248 case CHARDEV_BACKEND_KIND_VC:
4249 chr = vc_init(backend->vc);
4250 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004251 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004252 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004253 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004254 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004255 default:
4256 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4257 break;
4258 }
4259
Markus Armbruster3894c782014-05-19 18:57:36 +02004260 /*
4261 * Character backend open hasn't been fully converted to the Error
4262 * API. Some opens fail without setting an error. Set a generic
4263 * error then.
4264 * TODO full conversion to Error API
4265 */
4266 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004267 error_setg(errp, "Failed to create chardev");
4268 }
4269 if (chr) {
4270 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004271 chr->avail_connections =
4272 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004273 if (!chr->filename) {
4274 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4275 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004276 if (!chr->explicit_be_open) {
4277 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4278 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004279 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4280 return ret;
4281 } else {
4282 g_free(ret);
4283 return NULL;
4284 }
4285}
4286
4287void qmp_chardev_remove(const char *id, Error **errp)
4288{
4289 CharDriverState *chr;
4290
4291 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004292 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004293 error_setg(errp, "Chardev '%s' not found", id);
4294 return;
4295 }
4296 if (chr->chr_can_read || chr->chr_read ||
4297 chr->chr_event || chr->handler_opaque) {
4298 error_setg(errp, "Chardev '%s' is busy", id);
4299 return;
4300 }
4301 qemu_chr_delete(chr);
4302}
Anthony Liguorid654f342013-03-05 23:21:28 +05304303
4304static void register_types(void)
4305{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004306 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4307 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4308 qemu_chr_parse_socket);
4309 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4310 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4311 qemu_chr_parse_ringbuf);
4312 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4313 qemu_chr_parse_file_out);
4314 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4315 qemu_chr_parse_stdio);
4316 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4317 qemu_chr_parse_serial);
4318 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4319 qemu_chr_parse_serial);
4320 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4321 qemu_chr_parse_parallel);
4322 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4323 qemu_chr_parse_parallel);
4324 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4325 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4326 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4327 qemu_chr_parse_pipe);
4328 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004329 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004330 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4331 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004332 /* this must be done after machine init, since we register FEs with muxes
4333 * as part of realize functions like serial_isa_realizefn when -nographic
4334 * is specified
4335 */
4336 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304337}
4338
4339type_init(register_types);