blob: d956f8db6c12a198ca6300507d7975deb20f1458 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010027#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
Corey Minyardcfb429c2014-10-02 11:17:35 -050032#include "qapi/qmp-input-visitor.h"
33#include "qapi/qmp-output-visitor.h"
34#include "qapi-visit.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
36#include <unistd.h>
37#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000038#include <time.h>
39#include <errno.h>
40#include <sys/time.h>
41#include <zlib.h>
42
43#ifndef _WIN32
44#include <sys/times.h>
45#include <sys/wait.h>
46#include <termios.h>
47#include <sys/mman.h>
48#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <sys/socket.h>
51#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000054#include <dirent.h>
55#include <netdb.h>
56#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020057#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000058#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040059#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
60#include <dev/ppbus/ppi.h>
61#include <dev/ppbus/ppbconf.h>
62#elif defined(__DragonFly__)
63#include <dev/misc/ppi/ppi.h>
64#include <bus/ppbus/ppbconf.h>
65#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010066#else
aliguori6f97dba2008-10-31 18:49:55 +000067#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000068#include <linux/ppdev.h>
69#include <linux/parport.h>
70#endif
71#ifdef __sun__
72#include <sys/stat.h>
73#include <sys/ethernet.h>
74#include <sys/sockio.h>
75#include <netinet/arp.h>
76#include <netinet/in.h>
77#include <netinet/in_systm.h>
78#include <netinet/ip.h>
79#include <netinet/ip_icmp.h> // must come after ip.h
80#include <netinet/udp.h>
81#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000082#endif
83#endif
84#endif
85
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010086#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020087#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000088
Amit Shah9bd78542009-11-03 19:59:54 +053089#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030090#define READ_RETRIES 10
Corey Minyard9f781162014-10-02 11:17:33 -050091#define CHR_MAX_FILENAME_SIZE 256
Michael S. Tsirkinc4095722014-11-02 18:48:32 +020092#define TCP_MAX_FDS 16
Amit Shah9bd78542009-11-03 19:59:54 +053093
aliguori6f97dba2008-10-31 18:49:55 +000094/***********************************************************/
Corey Minyardcfb429c2014-10-02 11:17:35 -050095/* Socket address helpers */
96static void qapi_copy_SocketAddress(SocketAddress **p_dest,
97 SocketAddress *src)
98{
99 QmpOutputVisitor *qov;
100 QmpInputVisitor *qiv;
101 Visitor *ov, *iv;
102 QObject *obj;
103
104 *p_dest = NULL;
105
106 qov = qmp_output_visitor_new();
107 ov = qmp_output_get_visitor(qov);
108 visit_type_SocketAddress(ov, &src, NULL, &error_abort);
109 obj = qmp_output_get_qobject(qov);
110 qmp_output_visitor_cleanup(qov);
111 if (!obj) {
112 return;
113 }
114
115 qiv = qmp_input_visitor_new(obj);
116 iv = qmp_input_get_visitor(qiv);
117 visit_type_SocketAddress(iv, p_dest, NULL, &error_abort);
118 qmp_input_visitor_cleanup(qiv);
119 qobject_decref(obj);
120}
121
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500122static int SocketAddress_to_str(char *dest, int max_len,
123 const char *prefix, SocketAddress *addr,
124 bool is_listen, bool is_telnet)
125{
126 switch (addr->kind) {
127 case SOCKET_ADDRESS_KIND_INET:
128 return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix,
129 is_telnet ? "telnet" : "tcp", addr->inet->host,
130 addr->inet->port, is_listen ? ",server" : "");
131 break;
132 case SOCKET_ADDRESS_KIND_UNIX:
133 return snprintf(dest, max_len, "%sunix:%s%s", prefix,
134 addr->q_unix->path, is_listen ? ",server" : "");
135 break;
136 case SOCKET_ADDRESS_KIND_FD:
137 return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->fd->str,
138 is_listen ? ",server" : "");
139 break;
140 default:
141 abort();
142 }
143}
144
145static int sockaddr_to_str(char *dest, int max_len,
146 struct sockaddr_storage *ss, socklen_t ss_len,
Corey Minyard01ca5192014-10-02 11:17:38 -0500147 struct sockaddr_storage *ps, socklen_t ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500148 bool is_listen, bool is_telnet)
149{
Corey Minyard01ca5192014-10-02 11:17:38 -0500150 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
151 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500152 const char *left = "", *right = "";
153
154 switch (ss->ss_family) {
155#ifndef _WIN32
156 case AF_UNIX:
157 return snprintf(dest, max_len, "unix:%s%s",
158 ((struct sockaddr_un *)(ss))->sun_path,
159 is_listen ? ",server" : "");
160#endif
161 case AF_INET6:
162 left = "[";
163 right = "]";
164 /* fall through */
165 case AF_INET:
Corey Minyard01ca5192014-10-02 11:17:38 -0500166 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
167 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
168 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
169 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
170 return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500171 is_telnet ? "telnet" : "tcp",
Corey Minyard01ca5192014-10-02 11:17:38 -0500172 left, shost, right, sserv,
173 is_listen ? ",server" : "",
174 left, phost, right, pserv);
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500175
176 default:
177 return snprintf(dest, max_len, "unknown");
178 }
179}
180
Corey Minyardcfb429c2014-10-02 11:17:35 -0500181/***********************************************************/
aliguori6f97dba2008-10-31 18:49:55 +0000182/* character device */
183
Blue Swirl72cf2d42009-09-12 07:36:22 +0000184static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
185 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000186
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200187CharDriverState *qemu_chr_alloc(void)
188{
189 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +0200190 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200191 return chr;
192}
193
Hans de Goedea425d232011-11-19 10:22:43 +0100194void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000195{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200196 /* Keep track if the char device is open */
197 switch (event) {
198 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100199 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200200 break;
201 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100202 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200203 break;
204 }
205
aliguori6f97dba2008-10-31 18:49:55 +0000206 if (!s->chr_event)
207 return;
208 s->chr_event(s->handler_opaque, event);
209}
210
Hans de Goedefee204f2013-03-26 11:07:54 +0100211void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000212{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500213 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000214}
215
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500216int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000217{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200218 int ret;
219
220 qemu_mutex_lock(&s->chr_write_lock);
221 ret = s->chr_write(s, buf, len);
222 qemu_mutex_unlock(&s->chr_write_lock);
223 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000224}
225
Anthony Liguoricd187202013-03-26 10:04:17 -0500226int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
227{
228 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200229 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500230
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200231 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500232 while (offset < len) {
233 do {
234 res = s->chr_write(s, buf + offset, len - offset);
235 if (res == -1 && errno == EAGAIN) {
236 g_usleep(100);
237 }
238 } while (res == -1 && errno == EAGAIN);
239
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200240 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500241 break;
242 }
243
Anthony Liguoricd187202013-03-26 10:04:17 -0500244 offset += res;
245 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200246 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500247
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200248 if (res < 0) {
249 return res;
250 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500251 return offset;
252}
253
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300254int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
255{
256 int offset = 0, counter = 10;
257 int res;
258
259 if (!s->chr_sync_read) {
260 return 0;
261 }
262
263 while (offset < len) {
264 do {
265 res = s->chr_sync_read(s, buf + offset, len - offset);
266 if (res == -1 && errno == EAGAIN) {
267 g_usleep(100);
268 }
269 } while (res == -1 && errno == EAGAIN);
270
271 if (res == 0) {
272 break;
273 }
274
275 if (res < 0) {
276 return res;
277 }
278
279 offset += res;
280
281 if (!counter--) {
282 break;
283 }
284 }
285
286 return offset;
287}
288
Anthony Liguori41084f12011-08-15 11:17:34 -0500289int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000290{
291 if (!s->chr_ioctl)
292 return -ENOTSUP;
293 return s->chr_ioctl(s, cmd, arg);
294}
295
Anthony Liguori909cda12011-08-15 11:17:31 -0500296int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000297{
298 if (!s->chr_can_read)
299 return 0;
300 return s->chr_can_read(s->handler_opaque);
301}
302
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500303void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000304{
Stefan Weilac310732012-04-19 22:27:14 +0200305 if (s->chr_read) {
306 s->chr_read(s->handler_opaque, buf, len);
307 }
aliguori6f97dba2008-10-31 18:49:55 +0000308}
309
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500310int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100311{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300312 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800313 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300314}
315
316int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
317{
318 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100319}
320
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300321int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
322{
323 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
324}
325
Daniel P. Berrange13661082011-06-23 13:31:42 +0100326int qemu_chr_add_client(CharDriverState *s, int fd)
327{
328 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
329}
330
aliguori6f97dba2008-10-31 18:49:55 +0000331void qemu_chr_accept_input(CharDriverState *s)
332{
333 if (s->chr_accept_input)
334 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100335 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000336}
337
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500338void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000339{
Amit Shah9bd78542009-11-03 19:59:54 +0530340 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000341 va_list ap;
342 va_start(ap, fmt);
343 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500344 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000345 va_end(ap);
346}
347
Amit Shah386a5a12013-08-28 15:24:05 +0530348static void remove_fd_in_watch(CharDriverState *chr);
349
aliguori6f97dba2008-10-31 18:49:55 +0000350void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100351 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000352 IOReadHandler *fd_read,
353 IOEventHandler *fd_event,
354 void *opaque)
355{
Hans de Goede19083222013-03-26 11:07:56 +0100356 int fe_open;
357
Amit Shahda7d9982011-04-25 15:18:22 +0530358 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100359 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530360 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100361 } else {
362 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530363 }
aliguori6f97dba2008-10-31 18:49:55 +0000364 s->chr_can_read = fd_can_read;
365 s->chr_read = fd_read;
366 s->chr_event = fd_event;
367 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200368 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000369 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200370
Hans de Goede19083222013-03-26 11:07:56 +0100371 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100372 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100373 }
374
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200375 /* We're connecting to an already opened device, so let's make sure we
376 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100377 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100378 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200379 }
aliguori6f97dba2008-10-31 18:49:55 +0000380}
381
382static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
383{
384 return len;
385}
386
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +0100387static CharDriverState *qemu_chr_open_null(void)
aliguori6f97dba2008-10-31 18:49:55 +0000388{
389 CharDriverState *chr;
390
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200391 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000392 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500393 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100394 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000395}
396
397/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000398#define MAX_MUX 4
399#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
400#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
401typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100402 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000403 IOReadHandler *chr_read[MAX_MUX];
404 IOEventHandler *chr_event[MAX_MUX];
405 void *ext_opaque[MAX_MUX];
406 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200407 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000408 int mux_cnt;
409 int term_got_escape;
410 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000411 /* Intermediate input buffer allows to catch escape sequences even if the
412 currently active device is not accepting any input - but only until it
413 is full as well. */
414 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
415 int prod[MAX_MUX];
416 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200417 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200418
419 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200420 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200421 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000422} MuxDriver;
423
424
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200425/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000426static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
427{
428 MuxDriver *d = chr->opaque;
429 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200430 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200431 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000432 } else {
433 int i;
434
435 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200436 for (i = 0; i < len; i++) {
437 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000438 char buf1[64];
439 int64_t ti;
440 int secs;
441
Alex Blighbc72ad62013-08-21 16:03:08 +0100442 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200443 if (d->timestamps_start == -1)
444 d->timestamps_start = ti;
445 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000446 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000447 snprintf(buf1, sizeof(buf1),
448 "[%02d:%02d:%02d.%03d] ",
449 secs / 3600,
450 (secs / 60) % 60,
451 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000452 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200453 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200454 d->linestart = 0;
455 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200456 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200457 if (buf[i] == '\n') {
458 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000459 }
460 }
461 }
462 return ret;
463}
464
465static const char * const mux_help[] = {
466 "% h print this help\n\r",
467 "% x exit emulator\n\r",
468 "% s save disk data back to file (if -snapshot)\n\r",
Gonglei86d10322014-11-15 18:06:46 +0800469 "% t toggle console timestamps\n\r",
aliguori6f97dba2008-10-31 18:49:55 +0000470 "% b send break (magic sysrq)\n\r",
471 "% c switch between console and monitor\n\r",
472 "% % sends %\n\r",
473 NULL
474};
475
476int term_escape_char = 0x01; /* ctrl-a is used for escape */
477static void mux_print_help(CharDriverState *chr)
478{
479 int i, j;
480 char ebuf[15] = "Escape-Char";
481 char cbuf[50] = "\n\r";
482
483 if (term_escape_char > 0 && term_escape_char < 26) {
484 snprintf(cbuf, sizeof(cbuf), "\n\r");
485 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
486 } else {
487 snprintf(cbuf, sizeof(cbuf),
488 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
489 term_escape_char);
490 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200491 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000492 for (i = 0; mux_help[i] != NULL; i++) {
493 for (j=0; mux_help[i][j] != '\0'; j++) {
494 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200495 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000496 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200497 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000498 }
499 }
500}
501
aliguori2724b182009-03-05 23:01:47 +0000502static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
503{
504 if (d->chr_event[mux_nr])
505 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
506}
507
aliguori6f97dba2008-10-31 18:49:55 +0000508static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
509{
510 if (d->term_got_escape) {
511 d->term_got_escape = 0;
512 if (ch == term_escape_char)
513 goto send_char;
514 switch(ch) {
515 case '?':
516 case 'h':
517 mux_print_help(chr);
518 break;
519 case 'x':
520 {
521 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200522 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000523 exit(0);
524 break;
525 }
526 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200527 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000528 break;
529 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100530 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000531 break;
532 case 'c':
533 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200534 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
535 d->focus++;
536 if (d->focus >= d->mux_cnt)
537 d->focus = 0;
538 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000539 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200540 case 't':
541 d->timestamps = !d->timestamps;
542 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200543 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200544 break;
aliguori6f97dba2008-10-31 18:49:55 +0000545 }
546 } else if (ch == term_escape_char) {
547 d->term_got_escape = 1;
548 } else {
549 send_char:
550 return 1;
551 }
552 return 0;
553}
554
555static void mux_chr_accept_input(CharDriverState *chr)
556{
aliguori6f97dba2008-10-31 18:49:55 +0000557 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200558 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000559
aliguoria80bf992009-03-05 23:00:02 +0000560 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000561 d->chr_can_read[m] &&
562 d->chr_can_read[m](d->ext_opaque[m])) {
563 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000564 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000565 }
566}
567
568static int mux_chr_can_read(void *opaque)
569{
570 CharDriverState *chr = opaque;
571 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200572 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000573
aliguoria80bf992009-03-05 23:00:02 +0000574 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000575 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000576 if (d->chr_can_read[m])
577 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000578 return 0;
579}
580
581static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
582{
583 CharDriverState *chr = opaque;
584 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200585 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000586 int i;
587
588 mux_chr_accept_input (opaque);
589
590 for(i = 0; i < size; i++)
591 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000592 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000593 d->chr_can_read[m] &&
594 d->chr_can_read[m](d->ext_opaque[m]))
595 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
596 else
aliguoria80bf992009-03-05 23:00:02 +0000597 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000598 }
599}
600
601static void mux_chr_event(void *opaque, int event)
602{
603 CharDriverState *chr = opaque;
604 MuxDriver *d = chr->opaque;
605 int i;
606
607 /* Send the event to all registered listeners */
608 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000609 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000610}
611
612static void mux_chr_update_read_handler(CharDriverState *chr)
613{
614 MuxDriver *d = chr->opaque;
615
616 if (d->mux_cnt >= MAX_MUX) {
617 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
618 return;
619 }
620 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
621 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
622 d->chr_read[d->mux_cnt] = chr->chr_read;
623 d->chr_event[d->mux_cnt] = chr->chr_event;
624 /* Fix up the real driver with mux routines */
625 if (d->mux_cnt == 0) {
626 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
627 mux_chr_event, chr);
628 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200629 if (d->focus != -1) {
630 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200631 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200632 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000633 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200634 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000635}
636
Michael Roth7b7ab182013-07-30 13:04:22 -0500637static bool muxes_realized;
638
639/**
640 * Called after processing of default and command-line-specified
641 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
642 * to a mux chardev. This is done here to ensure that
643 * output/prompts/banners are only displayed for the FE that has
644 * focus when initial command-line processing/machine init is
645 * completed.
646 *
647 * After this point, any new FE attached to any new or existing
648 * mux will receive CHR_EVENT_OPENED notifications for the BE
649 * immediately.
650 */
651static void muxes_realize_done(Notifier *notifier, void *unused)
652{
653 CharDriverState *chr;
654
655 QTAILQ_FOREACH(chr, &chardevs, next) {
656 if (chr->is_mux) {
657 MuxDriver *d = chr->opaque;
658 int i;
659
660 /* send OPENED to all already-attached FEs */
661 for (i = 0; i < d->mux_cnt; i++) {
662 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
663 }
664 /* mark mux as OPENED so any new FEs will immediately receive
665 * OPENED event
666 */
667 qemu_chr_be_generic_open(chr);
668 }
669 }
670 muxes_realized = true;
671}
672
673static Notifier muxes_realize_notify = {
674 .notify = muxes_realize_done,
675};
676
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400677static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
678{
679 MuxDriver *d = s->opaque;
680 return d->drv->chr_add_watch(d->drv, cond);
681}
682
aliguori6f97dba2008-10-31 18:49:55 +0000683static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
684{
685 CharDriverState *chr;
686 MuxDriver *d;
687
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200688 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -0500689 d = g_malloc0(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000690
691 chr->opaque = d;
692 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200693 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000694 chr->chr_write = mux_chr_write;
695 chr->chr_update_read_handler = mux_chr_update_read_handler;
696 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100697 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100698 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400699 if (drv->chr_add_watch) {
700 chr->chr_add_watch = mux_chr_add_watch;
701 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500702 /* only default to opened state if we've realized the initial
703 * set of muxes
704 */
705 chr->explicit_be_open = muxes_realized ? 0 : 1;
706 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200707
aliguori6f97dba2008-10-31 18:49:55 +0000708 return chr;
709}
710
711
712#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000713int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000714{
715 int ret, len;
716
717 len = len1;
718 while (len > 0) {
719 ret = send(fd, buf, len, 0);
720 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000721 errno = WSAGetLastError();
722 if (errno != WSAEWOULDBLOCK) {
723 return -1;
724 }
725 } else if (ret == 0) {
726 break;
727 } else {
728 buf += ret;
729 len -= ret;
730 }
731 }
732 return len1 - len;
733}
734
735#else
736
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100737int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000738{
739 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100740 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000741
742 len = len1;
743 while (len > 0) {
744 ret = write(fd, buf, len);
745 if (ret < 0) {
746 if (errno != EINTR && errno != EAGAIN)
747 return -1;
748 } else if (ret == 0) {
749 break;
750 } else {
751 buf += ret;
752 len -= ret;
753 }
754 }
755 return len1 - len;
756}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500757
758int recv_all(int fd, void *_buf, int len1, bool single_read)
759{
760 int ret, len;
761 uint8_t *buf = _buf;
762
763 len = len1;
764 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
765 if (ret < 0) {
766 if (errno != EINTR && errno != EAGAIN) {
767 return -1;
768 }
769 continue;
770 } else {
771 if (single_read) {
772 return ret;
773 }
774 buf += ret;
775 len -= ret;
776 }
777 }
778 return len1 - len;
779}
780
aliguori6f97dba2008-10-31 18:49:55 +0000781#endif /* !_WIN32 */
782
Anthony Liguori96c63842013-03-05 23:21:18 +0530783typedef struct IOWatchPoll
784{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200785 GSource parent;
786
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200787 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530788 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530789
790 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200791 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530792 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530793} IOWatchPoll;
794
Anthony Liguori96c63842013-03-05 23:21:18 +0530795static IOWatchPoll *io_watch_poll_from_source(GSource *source)
796{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200797 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530798}
799
800static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
801{
802 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200803 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200804 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200805 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530806 return FALSE;
807 }
808
Paolo Bonzinid185c092013-04-05 17:59:33 +0200809 if (now_active) {
Nils Carlson4bf1cb02015-07-19 20:39:56 +0000810 iwp->src = g_io_create_watch(iwp->channel,
811 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200812 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200813 g_source_attach(iwp->src, NULL);
814 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200815 g_source_destroy(iwp->src);
816 g_source_unref(iwp->src);
817 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200818 }
819 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530820}
821
822static gboolean io_watch_poll_check(GSource *source)
823{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200824 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530825}
826
827static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
828 gpointer user_data)
829{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200830 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530831}
832
833static void io_watch_poll_finalize(GSource *source)
834{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200835 /* Due to a glib bug, removing the last reference to a source
836 * inside a finalize callback causes recursive locking (and a
837 * deadlock). This is not a problem inside other callbacks,
838 * including dispatch callbacks, so we call io_remove_watch_poll
839 * to remove this source. At this point, iwp->src must
840 * be NULL, or we would leak it.
841 *
842 * This would be solved much more elegantly by child sources,
843 * but we support older glib versions that do not have them.
844 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530845 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200846 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530847}
848
849static GSourceFuncs io_watch_poll_funcs = {
850 .prepare = io_watch_poll_prepare,
851 .check = io_watch_poll_check,
852 .dispatch = io_watch_poll_dispatch,
853 .finalize = io_watch_poll_finalize,
854};
855
856/* Can only be used for read */
857static guint io_add_watch_poll(GIOChannel *channel,
858 IOCanReadHandler *fd_can_read,
859 GIOFunc fd_read,
860 gpointer user_data)
861{
862 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200863 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530864
Paolo Bonzinid185c092013-04-05 17:59:33 +0200865 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530866 iwp->fd_can_read = fd_can_read;
867 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200868 iwp->channel = channel;
869 iwp->fd_read = (GSourceFunc) fd_read;
870 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530871
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200872 tag = g_source_attach(&iwp->parent, NULL);
873 g_source_unref(&iwp->parent);
874 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530875}
876
Paolo Bonzini2b316772013-04-19 17:32:09 +0200877static void io_remove_watch_poll(guint tag)
878{
879 GSource *source;
880 IOWatchPoll *iwp;
881
882 g_return_if_fail (tag > 0);
883
884 source = g_main_context_find_source_by_id(NULL, tag);
885 g_return_if_fail (source != NULL);
886
887 iwp = io_watch_poll_from_source(source);
888 if (iwp->src) {
889 g_source_destroy(iwp->src);
890 g_source_unref(iwp->src);
891 iwp->src = NULL;
892 }
893 g_source_destroy(&iwp->parent);
894}
895
Amit Shah26da70c2013-08-28 15:23:37 +0530896static void remove_fd_in_watch(CharDriverState *chr)
897{
898 if (chr->fd_in_tag) {
899 io_remove_watch_poll(chr->fd_in_tag);
900 chr->fd_in_tag = 0;
901 }
902}
903
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000904#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530905static GIOChannel *io_channel_from_fd(int fd)
906{
907 GIOChannel *chan;
908
909 if (fd == -1) {
910 return NULL;
911 }
912
913 chan = g_io_channel_unix_new(fd);
914
915 g_io_channel_set_encoding(chan, NULL, NULL);
916 g_io_channel_set_buffered(chan, FALSE);
917
918 return chan;
919}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000920#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530921
Anthony Liguori76a96442013-03-05 23:21:21 +0530922static GIOChannel *io_channel_from_socket(int fd)
923{
924 GIOChannel *chan;
925
926 if (fd == -1) {
927 return NULL;
928 }
929
930#ifdef _WIN32
931 chan = g_io_channel_win32_new_socket(fd);
932#else
933 chan = g_io_channel_unix_new(fd);
934#endif
935
936 g_io_channel_set_encoding(chan, NULL, NULL);
937 g_io_channel_set_buffered(chan, FALSE);
938
939 return chan;
940}
941
Anthony Liguori684a0962013-03-29 11:39:50 -0500942static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530943{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200944 size_t offset = 0;
945 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530946
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200947 while (offset < len && status == G_IO_STATUS_NORMAL) {
948 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500949
950 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530951 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500952 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530953 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500954
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200955 if (offset > 0) {
956 return offset;
957 }
958 switch (status) {
959 case G_IO_STATUS_NORMAL:
960 g_assert(len == 0);
961 return 0;
962 case G_IO_STATUS_AGAIN:
963 errno = EAGAIN;
964 return -1;
965 default:
966 break;
967 }
968 errno = EINVAL;
969 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530970}
Anthony Liguori96c63842013-03-05 23:21:18 +0530971
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000972#ifndef _WIN32
973
Anthony Liguoria29753f2013-03-05 23:21:19 +0530974typedef struct FDCharDriver {
975 CharDriverState *chr;
976 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000977 int max_size;
978} FDCharDriver;
979
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200980/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000981static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
982{
983 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530984
Anthony Liguori684a0962013-03-29 11:39:50 -0500985 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530986}
987
988static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
989{
990 CharDriverState *chr = opaque;
991 FDCharDriver *s = chr->opaque;
992 int len;
993 uint8_t buf[READ_BUF_LEN];
994 GIOStatus status;
995 gsize bytes_read;
996
997 len = sizeof(buf);
998 if (len > s->max_size) {
999 len = s->max_size;
1000 }
1001 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001002 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301003 }
1004
1005 status = g_io_channel_read_chars(chan, (gchar *)buf,
1006 len, &bytes_read, NULL);
1007 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +05301008 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301009 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1010 return FALSE;
1011 }
1012 if (status == G_IO_STATUS_NORMAL) {
1013 qemu_chr_be_write(chr, buf, bytes_read);
1014 }
1015
1016 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001017}
1018
1019static int fd_chr_read_poll(void *opaque)
1020{
1021 CharDriverState *chr = opaque;
1022 FDCharDriver *s = chr->opaque;
1023
Anthony Liguori909cda12011-08-15 11:17:31 -05001024 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001025 return s->max_size;
1026}
1027
Anthony Liguori23673ca2013-03-05 23:21:23 +05301028static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1029{
1030 FDCharDriver *s = chr->opaque;
1031 return g_io_create_watch(s->fd_out, cond);
1032}
1033
aliguori6f97dba2008-10-31 18:49:55 +00001034static void fd_chr_update_read_handler(CharDriverState *chr)
1035{
1036 FDCharDriver *s = chr->opaque;
1037
Amit Shah26da70c2013-08-28 15:23:37 +05301038 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301039 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301040 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1041 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001042 }
1043}
1044
1045static void fd_chr_close(struct CharDriverState *chr)
1046{
1047 FDCharDriver *s = chr->opaque;
1048
Amit Shah26da70c2013-08-28 15:23:37 +05301049 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301050 if (s->fd_in) {
1051 g_io_channel_unref(s->fd_in);
1052 }
1053 if (s->fd_out) {
1054 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001055 }
1056
Anthony Liguori7267c092011-08-20 22:09:37 -05001057 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001058 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001059}
1060
1061/* open a character device to a unix fd */
1062static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1063{
1064 CharDriverState *chr;
1065 FDCharDriver *s;
1066
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001067 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05001068 s = g_malloc0(sizeof(FDCharDriver));
Anthony Liguoria29753f2013-03-05 23:21:19 +05301069 s->fd_in = io_channel_from_fd(fd_in);
1070 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001071 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301072 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001073 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301074 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001075 chr->chr_write = fd_chr_write;
1076 chr->chr_update_read_handler = fd_chr_update_read_handler;
1077 chr->chr_close = fd_chr_close;
1078
aliguori6f97dba2008-10-31 18:49:55 +00001079 return chr;
1080}
1081
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001082static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001083{
1084 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001085 char filename_in[CHR_MAX_FILENAME_SIZE];
1086 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001087 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001088
1089 if (filename == NULL) {
1090 fprintf(stderr, "chardev: pipe: no filename given\n");
Markus Armbruster1f514702012-02-07 15:09:08 +01001091 return NULL;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001092 }
aliguori6f97dba2008-10-31 18:49:55 +00001093
Corey Minyard9f781162014-10-02 11:17:33 -05001094 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1095 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001096 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1097 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001098 if (fd_in < 0 || fd_out < 0) {
1099 if (fd_in >= 0)
1100 close(fd_in);
1101 if (fd_out >= 0)
1102 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001103 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001104 if (fd_in < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001105 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001106 }
aliguori6f97dba2008-10-31 18:49:55 +00001107 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001108 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001109}
1110
aliguori6f97dba2008-10-31 18:49:55 +00001111/* init terminal so that we can grab keys */
1112static struct termios oldtty;
1113static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001114static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001115static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001116static bool stdio_echo_state;
1117
1118static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001119
1120static void term_exit(void)
1121{
1122 tcsetattr (0, TCSANOW, &oldtty);
1123 fcntl(0, F_SETFL, old_fd0_flags);
1124}
1125
Gal Hammere76d4422015-01-07 10:38:35 +02001126static void term_stdio_handler(int sig)
1127{
1128 /* restore echo after resume from suspend. */
1129 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1130}
1131
Paolo Bonzinibb002512010-12-23 13:42:50 +01001132static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001133{
1134 struct termios tty;
1135
Gal Hammere76d4422015-01-07 10:38:35 +02001136 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001137 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001138 if (!echo) {
1139 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001140 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001141 tty.c_oflag |= OPOST;
1142 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1143 tty.c_cflag &= ~(CSIZE|PARENB);
1144 tty.c_cflag |= CS8;
1145 tty.c_cc[VMIN] = 1;
1146 tty.c_cc[VTIME] = 0;
1147 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001148 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001149 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001150
1151 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001152}
1153
1154static void qemu_chr_close_stdio(struct CharDriverState *chr)
1155{
1156 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001157 fd_chr_close(chr);
1158}
1159
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001160static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001161{
1162 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001163 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001164
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001165 if (is_daemonized()) {
1166 error_report("cannot use stdio with -daemonize");
1167 return NULL;
1168 }
Li Liuc88930a2014-09-09 19:19:48 +08001169
1170 if (stdio_in_use) {
1171 error_report("cannot use stdio by multiple character devices");
1172 exit(1);
1173 }
1174
1175 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301176 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001177 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001178 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301179 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001180
Gal Hammere76d4422015-01-07 10:38:35 +02001181 memset(&act, 0, sizeof(act));
1182 act.sa_handler = term_stdio_handler;
1183 sigaction(SIGCONT, &act, NULL);
1184
aliguori6f97dba2008-10-31 18:49:55 +00001185 chr = qemu_chr_open_fd(0, 1);
1186 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001187 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001188 if (opts->has_signal) {
1189 stdio_allow_signal = opts->signal;
1190 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001191 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001192
Markus Armbruster1f514702012-02-07 15:09:08 +01001193 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001194}
1195
aliguori6f97dba2008-10-31 18:49:55 +00001196#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001197 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1198 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001199
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001200#define HAVE_CHARDEV_TTY 1
1201
aliguori6f97dba2008-10-31 18:49:55 +00001202typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301203 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001204 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001205
1206 /* Protected by the CharDriverState chr_write_lock. */
1207 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301208 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001209 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001210} PtyCharDriver;
1211
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001212static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001213static void pty_chr_state(CharDriverState *chr, int connected);
1214
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301215static gboolean pty_chr_timer(gpointer opaque)
1216{
1217 struct CharDriverState *chr = opaque;
1218 PtyCharDriver *s = chr->opaque;
1219
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001220 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001221 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001222 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001223 if (!s->connected) {
1224 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001225 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001226 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001227 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301228 return FALSE;
1229}
1230
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001231/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301232static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1233{
1234 PtyCharDriver *s = chr->opaque;
1235
1236 if (s->timer_tag) {
1237 g_source_remove(s->timer_tag);
1238 s->timer_tag = 0;
1239 }
1240
1241 if (ms == 1000) {
1242 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1243 } else {
1244 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1245 }
1246}
1247
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001248/* Called with chr_write_lock held. */
1249static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001250{
1251 PtyCharDriver *s = chr->opaque;
1252 GPollFD pfd;
1253
1254 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1255 pfd.events = G_IO_OUT;
1256 pfd.revents = 0;
1257 g_poll(&pfd, 1, 0);
1258 if (pfd.revents & G_IO_HUP) {
1259 pty_chr_state(chr, 0);
1260 } else {
1261 pty_chr_state(chr, 1);
1262 }
1263}
1264
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001265static void pty_chr_update_read_handler(CharDriverState *chr)
1266{
1267 qemu_mutex_lock(&chr->chr_write_lock);
1268 pty_chr_update_read_handler_locked(chr);
1269 qemu_mutex_unlock(&chr->chr_write_lock);
1270}
1271
1272/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001273static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1274{
1275 PtyCharDriver *s = chr->opaque;
1276
1277 if (!s->connected) {
1278 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001279 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001280 if (!s->connected) {
1281 return 0;
1282 }
aliguori6f97dba2008-10-31 18:49:55 +00001283 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001284 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001285}
1286
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301287static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1288{
1289 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001290 if (!s->connected) {
1291 return NULL;
1292 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301293 return g_io_create_watch(s->fd, cond);
1294}
1295
aliguori6f97dba2008-10-31 18:49:55 +00001296static int pty_chr_read_poll(void *opaque)
1297{
1298 CharDriverState *chr = opaque;
1299 PtyCharDriver *s = chr->opaque;
1300
Anthony Liguori909cda12011-08-15 11:17:31 -05001301 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001302 return s->read_bytes;
1303}
1304
Anthony Liguori093d3a22013-03-05 23:21:20 +05301305static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001306{
1307 CharDriverState *chr = opaque;
1308 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301309 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301310 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301311 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001312
1313 len = sizeof(buf);
1314 if (len > s->read_bytes)
1315 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001316 if (len == 0) {
1317 return TRUE;
1318 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301319 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1320 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001321 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301322 return FALSE;
1323 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001324 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001325 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001326 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301327 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001328}
1329
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001330static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1331{
1332 CharDriverState *chr = opaque;
1333 PtyCharDriver *s = chr->opaque;
1334
1335 s->open_tag = 0;
1336 qemu_chr_be_generic_open(chr);
1337 return FALSE;
1338}
1339
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001340/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001341static void pty_chr_state(CharDriverState *chr, int connected)
1342{
1343 PtyCharDriver *s = chr->opaque;
1344
1345 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001346 if (s->open_tag) {
1347 g_source_remove(s->open_tag);
1348 s->open_tag = 0;
1349 }
Amit Shah26da70c2013-08-28 15:23:37 +05301350 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001351 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001352 /* (re-)connect poll interval for idle guests: once per second.
1353 * We check more frequently in case the guests sends data to
1354 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301355 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001356 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001357 if (s->timer_tag) {
1358 g_source_remove(s->timer_tag);
1359 s->timer_tag = 0;
1360 }
1361 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001362 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001363 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001364 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001365 }
1366 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301367 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1368 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001369 }
aliguori6f97dba2008-10-31 18:49:55 +00001370 }
1371}
1372
aliguori6f97dba2008-10-31 18:49:55 +00001373static void pty_chr_close(struct CharDriverState *chr)
1374{
1375 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301376 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001377
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001378 qemu_mutex_lock(&chr->chr_write_lock);
1379 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301380 fd = g_io_channel_unix_get_fd(s->fd);
1381 g_io_channel_unref(s->fd);
1382 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301383 if (s->timer_tag) {
1384 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001385 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301386 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001387 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001388 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001389 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001390}
1391
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001392static CharDriverState *qemu_chr_open_pty(const char *id,
1393 ChardevReturn *ret)
aliguori6f97dba2008-10-31 18:49:55 +00001394{
1395 CharDriverState *chr;
1396 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001397 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001398 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001399
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001400 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1401 if (master_fd < 0) {
Markus Armbruster1f514702012-02-07 15:09:08 +01001402 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001403 }
1404
aliguori6f97dba2008-10-31 18:49:55 +00001405 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001406 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001407
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001408 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001409
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001410 chr->filename = g_strdup_printf("pty:%s", pty_name);
1411 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001412 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001413
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001414 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001415 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001416
1417 s = g_malloc0(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001418 chr->opaque = s;
1419 chr->chr_write = pty_chr_write;
1420 chr->chr_update_read_handler = pty_chr_update_read_handler;
1421 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301422 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001423 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001424
Anthony Liguori093d3a22013-03-05 23:21:20 +05301425 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301426 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001427
Markus Armbruster1f514702012-02-07 15:09:08 +01001428 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001429}
1430
1431static void tty_serial_init(int fd, int speed,
1432 int parity, int data_bits, int stop_bits)
1433{
1434 struct termios tty;
1435 speed_t spd;
1436
1437#if 0
1438 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1439 speed, parity, data_bits, stop_bits);
1440#endif
1441 tcgetattr (fd, &tty);
1442
Stefan Weil45eea132009-10-26 16:10:10 +01001443#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1444 speed = speed * 10 / 11;
1445 do {
1446 check_speed(50);
1447 check_speed(75);
1448 check_speed(110);
1449 check_speed(134);
1450 check_speed(150);
1451 check_speed(200);
1452 check_speed(300);
1453 check_speed(600);
1454 check_speed(1200);
1455 check_speed(1800);
1456 check_speed(2400);
1457 check_speed(4800);
1458 check_speed(9600);
1459 check_speed(19200);
1460 check_speed(38400);
1461 /* Non-Posix values follow. They may be unsupported on some systems. */
1462 check_speed(57600);
1463 check_speed(115200);
1464#ifdef B230400
1465 check_speed(230400);
1466#endif
1467#ifdef B460800
1468 check_speed(460800);
1469#endif
1470#ifdef B500000
1471 check_speed(500000);
1472#endif
1473#ifdef B576000
1474 check_speed(576000);
1475#endif
1476#ifdef B921600
1477 check_speed(921600);
1478#endif
1479#ifdef B1000000
1480 check_speed(1000000);
1481#endif
1482#ifdef B1152000
1483 check_speed(1152000);
1484#endif
1485#ifdef B1500000
1486 check_speed(1500000);
1487#endif
1488#ifdef B2000000
1489 check_speed(2000000);
1490#endif
1491#ifdef B2500000
1492 check_speed(2500000);
1493#endif
1494#ifdef B3000000
1495 check_speed(3000000);
1496#endif
1497#ifdef B3500000
1498 check_speed(3500000);
1499#endif
1500#ifdef B4000000
1501 check_speed(4000000);
1502#endif
aliguori6f97dba2008-10-31 18:49:55 +00001503 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001504 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001505
1506 cfsetispeed(&tty, spd);
1507 cfsetospeed(&tty, spd);
1508
1509 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1510 |INLCR|IGNCR|ICRNL|IXON);
1511 tty.c_oflag |= OPOST;
1512 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1513 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1514 switch(data_bits) {
1515 default:
1516 case 8:
1517 tty.c_cflag |= CS8;
1518 break;
1519 case 7:
1520 tty.c_cflag |= CS7;
1521 break;
1522 case 6:
1523 tty.c_cflag |= CS6;
1524 break;
1525 case 5:
1526 tty.c_cflag |= CS5;
1527 break;
1528 }
1529 switch(parity) {
1530 default:
1531 case 'N':
1532 break;
1533 case 'E':
1534 tty.c_cflag |= PARENB;
1535 break;
1536 case 'O':
1537 tty.c_cflag |= PARENB | PARODD;
1538 break;
1539 }
1540 if (stop_bits == 2)
1541 tty.c_cflag |= CSTOPB;
1542
1543 tcsetattr (fd, TCSANOW, &tty);
1544}
1545
1546static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1547{
1548 FDCharDriver *s = chr->opaque;
1549
1550 switch(cmd) {
1551 case CHR_IOCTL_SERIAL_SET_PARAMS:
1552 {
1553 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301554 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1555 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001556 ssp->data_bits, ssp->stop_bits);
1557 }
1558 break;
1559 case CHR_IOCTL_SERIAL_SET_BREAK:
1560 {
1561 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301562 if (enable) {
1563 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1564 }
aliguori6f97dba2008-10-31 18:49:55 +00001565 }
1566 break;
1567 case CHR_IOCTL_SERIAL_GET_TIOCM:
1568 {
1569 int sarg = 0;
1570 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301571 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001572 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001573 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001574 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001575 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001576 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001577 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001578 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001579 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001580 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001581 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001582 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001583 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001584 *targ |= CHR_TIOCM_RTS;
1585 }
1586 break;
1587 case CHR_IOCTL_SERIAL_SET_TIOCM:
1588 {
1589 int sarg = *(int *)arg;
1590 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301591 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001592 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1593 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1594 if (sarg & CHR_TIOCM_CTS)
1595 targ |= TIOCM_CTS;
1596 if (sarg & CHR_TIOCM_CAR)
1597 targ |= TIOCM_CAR;
1598 if (sarg & CHR_TIOCM_DSR)
1599 targ |= TIOCM_DSR;
1600 if (sarg & CHR_TIOCM_RI)
1601 targ |= TIOCM_RI;
1602 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001603 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001604 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001605 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301606 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001607 }
1608 break;
1609 default:
1610 return -ENOTSUP;
1611 }
1612 return 0;
1613}
1614
David Ahern4266a132010-02-10 18:27:17 -07001615static void qemu_chr_close_tty(CharDriverState *chr)
1616{
1617 FDCharDriver *s = chr->opaque;
1618 int fd = -1;
1619
1620 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301621 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001622 }
1623
1624 fd_chr_close(chr);
1625
1626 if (fd >= 0) {
1627 close(fd);
1628 }
1629}
1630
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001631static CharDriverState *qemu_chr_open_tty_fd(int fd)
1632{
1633 CharDriverState *chr;
1634
1635 tty_serial_init(fd, 115200, 'N', 8, 1);
1636 chr = qemu_chr_open_fd(fd, fd);
1637 chr->chr_ioctl = tty_serial_ioctl;
1638 chr->chr_close = qemu_chr_close_tty;
1639 return chr;
1640}
aliguori6f97dba2008-10-31 18:49:55 +00001641#endif /* __linux__ || __sun__ */
1642
1643#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001644
1645#define HAVE_CHARDEV_PARPORT 1
1646
aliguori6f97dba2008-10-31 18:49:55 +00001647typedef struct {
1648 int fd;
1649 int mode;
1650} ParallelCharDriver;
1651
1652static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1653{
1654 if (s->mode != mode) {
1655 int m = mode;
1656 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1657 return 0;
1658 s->mode = mode;
1659 }
1660 return 1;
1661}
1662
1663static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1664{
1665 ParallelCharDriver *drv = chr->opaque;
1666 int fd = drv->fd;
1667 uint8_t b;
1668
1669 switch(cmd) {
1670 case CHR_IOCTL_PP_READ_DATA:
1671 if (ioctl(fd, PPRDATA, &b) < 0)
1672 return -ENOTSUP;
1673 *(uint8_t *)arg = b;
1674 break;
1675 case CHR_IOCTL_PP_WRITE_DATA:
1676 b = *(uint8_t *)arg;
1677 if (ioctl(fd, PPWDATA, &b) < 0)
1678 return -ENOTSUP;
1679 break;
1680 case CHR_IOCTL_PP_READ_CONTROL:
1681 if (ioctl(fd, PPRCONTROL, &b) < 0)
1682 return -ENOTSUP;
1683 /* Linux gives only the lowest bits, and no way to know data
1684 direction! For better compatibility set the fixed upper
1685 bits. */
1686 *(uint8_t *)arg = b | 0xc0;
1687 break;
1688 case CHR_IOCTL_PP_WRITE_CONTROL:
1689 b = *(uint8_t *)arg;
1690 if (ioctl(fd, PPWCONTROL, &b) < 0)
1691 return -ENOTSUP;
1692 break;
1693 case CHR_IOCTL_PP_READ_STATUS:
1694 if (ioctl(fd, PPRSTATUS, &b) < 0)
1695 return -ENOTSUP;
1696 *(uint8_t *)arg = b;
1697 break;
1698 case CHR_IOCTL_PP_DATA_DIR:
1699 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1700 return -ENOTSUP;
1701 break;
1702 case CHR_IOCTL_PP_EPP_READ_ADDR:
1703 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1704 struct ParallelIOArg *parg = arg;
1705 int n = read(fd, parg->buffer, parg->count);
1706 if (n != parg->count) {
1707 return -EIO;
1708 }
1709 }
1710 break;
1711 case CHR_IOCTL_PP_EPP_READ:
1712 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1713 struct ParallelIOArg *parg = arg;
1714 int n = read(fd, parg->buffer, parg->count);
1715 if (n != parg->count) {
1716 return -EIO;
1717 }
1718 }
1719 break;
1720 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1721 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1722 struct ParallelIOArg *parg = arg;
1723 int n = write(fd, parg->buffer, parg->count);
1724 if (n != parg->count) {
1725 return -EIO;
1726 }
1727 }
1728 break;
1729 case CHR_IOCTL_PP_EPP_WRITE:
1730 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1731 struct ParallelIOArg *parg = arg;
1732 int n = write(fd, parg->buffer, parg->count);
1733 if (n != parg->count) {
1734 return -EIO;
1735 }
1736 }
1737 break;
1738 default:
1739 return -ENOTSUP;
1740 }
1741 return 0;
1742}
1743
1744static void pp_close(CharDriverState *chr)
1745{
1746 ParallelCharDriver *drv = chr->opaque;
1747 int fd = drv->fd;
1748
1749 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1750 ioctl(fd, PPRELEASE);
1751 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001752 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001753 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001754}
1755
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001756static CharDriverState *qemu_chr_open_pp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00001757{
1758 CharDriverState *chr;
1759 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001760
1761 if (ioctl(fd, PPCLAIM) < 0) {
1762 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001763 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001764 }
1765
Anthony Liguori7267c092011-08-20 22:09:37 -05001766 drv = g_malloc0(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001767 drv->fd = fd;
1768 drv->mode = IEEE1284_MODE_COMPAT;
1769
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001770 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001771 chr->chr_write = null_chr_write;
1772 chr->chr_ioctl = pp_ioctl;
1773 chr->chr_close = pp_close;
1774 chr->opaque = drv;
1775
Markus Armbruster1f514702012-02-07 15:09:08 +01001776 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001777}
1778#endif /* __linux__ */
1779
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001780#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001781
1782#define HAVE_CHARDEV_PARPORT 1
1783
blueswir16972f932008-11-22 20:49:12 +00001784static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1785{
Stefan Weile0efb992011-02-23 19:09:16 +01001786 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001787 uint8_t b;
1788
1789 switch(cmd) {
1790 case CHR_IOCTL_PP_READ_DATA:
1791 if (ioctl(fd, PPIGDATA, &b) < 0)
1792 return -ENOTSUP;
1793 *(uint8_t *)arg = b;
1794 break;
1795 case CHR_IOCTL_PP_WRITE_DATA:
1796 b = *(uint8_t *)arg;
1797 if (ioctl(fd, PPISDATA, &b) < 0)
1798 return -ENOTSUP;
1799 break;
1800 case CHR_IOCTL_PP_READ_CONTROL:
1801 if (ioctl(fd, PPIGCTRL, &b) < 0)
1802 return -ENOTSUP;
1803 *(uint8_t *)arg = b;
1804 break;
1805 case CHR_IOCTL_PP_WRITE_CONTROL:
1806 b = *(uint8_t *)arg;
1807 if (ioctl(fd, PPISCTRL, &b) < 0)
1808 return -ENOTSUP;
1809 break;
1810 case CHR_IOCTL_PP_READ_STATUS:
1811 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1812 return -ENOTSUP;
1813 *(uint8_t *)arg = b;
1814 break;
1815 default:
1816 return -ENOTSUP;
1817 }
1818 return 0;
1819}
1820
Gerd Hoffmann88a946d2013-01-10 14:20:58 +01001821static CharDriverState *qemu_chr_open_pp_fd(int fd)
blueswir16972f932008-11-22 20:49:12 +00001822{
1823 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001824
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001825 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001826 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001827 chr->chr_write = null_chr_write;
1828 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001829 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001830 return chr;
blueswir16972f932008-11-22 20:49:12 +00001831}
1832#endif
1833
aliguori6f97dba2008-10-31 18:49:55 +00001834#else /* _WIN32 */
1835
1836typedef struct {
1837 int max_size;
1838 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001839 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001840 BOOL fpipe;
1841 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001842
1843 /* Protected by the CharDriverState chr_write_lock. */
1844 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001845} WinCharState;
1846
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001847typedef struct {
1848 HANDLE hStdIn;
1849 HANDLE hInputReadyEvent;
1850 HANDLE hInputDoneEvent;
1851 HANDLE hInputThread;
1852 uint8_t win_stdio_buf;
1853} WinStdioCharState;
1854
aliguori6f97dba2008-10-31 18:49:55 +00001855#define NSENDBUF 2048
1856#define NRECVBUF 2048
1857#define MAXCONNECT 1
1858#define NTIMEOUT 5000
1859
1860static int win_chr_poll(void *opaque);
1861static int win_chr_pipe_poll(void *opaque);
1862
1863static void win_chr_close(CharDriverState *chr)
1864{
1865 WinCharState *s = chr->opaque;
1866
1867 if (s->hsend) {
1868 CloseHandle(s->hsend);
1869 s->hsend = NULL;
1870 }
1871 if (s->hrecv) {
1872 CloseHandle(s->hrecv);
1873 s->hrecv = NULL;
1874 }
1875 if (s->hcom) {
1876 CloseHandle(s->hcom);
1877 s->hcom = NULL;
1878 }
1879 if (s->fpipe)
1880 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1881 else
1882 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301883
Hans de Goedea425d232011-11-19 10:22:43 +01001884 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001885}
1886
1887static int win_chr_init(CharDriverState *chr, const char *filename)
1888{
1889 WinCharState *s = chr->opaque;
1890 COMMCONFIG comcfg;
1891 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1892 COMSTAT comstat;
1893 DWORD size;
1894 DWORD err;
1895
1896 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1897 if (!s->hsend) {
1898 fprintf(stderr, "Failed CreateEvent\n");
1899 goto fail;
1900 }
1901 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1902 if (!s->hrecv) {
1903 fprintf(stderr, "Failed CreateEvent\n");
1904 goto fail;
1905 }
1906
1907 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1908 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1909 if (s->hcom == INVALID_HANDLE_VALUE) {
1910 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1911 s->hcom = NULL;
1912 goto fail;
1913 }
1914
1915 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1916 fprintf(stderr, "Failed SetupComm\n");
1917 goto fail;
1918 }
1919
1920 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1921 size = sizeof(COMMCONFIG);
1922 GetDefaultCommConfig(filename, &comcfg, &size);
1923 comcfg.dcb.DCBlength = sizeof(DCB);
1924 CommConfigDialog(filename, NULL, &comcfg);
1925
1926 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1927 fprintf(stderr, "Failed SetCommState\n");
1928 goto fail;
1929 }
1930
1931 if (!SetCommMask(s->hcom, EV_ERR)) {
1932 fprintf(stderr, "Failed SetCommMask\n");
1933 goto fail;
1934 }
1935
1936 cto.ReadIntervalTimeout = MAXDWORD;
1937 if (!SetCommTimeouts(s->hcom, &cto)) {
1938 fprintf(stderr, "Failed SetCommTimeouts\n");
1939 goto fail;
1940 }
1941
1942 if (!ClearCommError(s->hcom, &err, &comstat)) {
1943 fprintf(stderr, "Failed ClearCommError\n");
1944 goto fail;
1945 }
1946 qemu_add_polling_cb(win_chr_poll, chr);
1947 return 0;
1948
1949 fail:
1950 win_chr_close(chr);
1951 return -1;
1952}
1953
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001954/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001955static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1956{
1957 WinCharState *s = chr->opaque;
1958 DWORD len, ret, size, err;
1959
1960 len = len1;
1961 ZeroMemory(&s->osend, sizeof(s->osend));
1962 s->osend.hEvent = s->hsend;
1963 while (len > 0) {
1964 if (s->hsend)
1965 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1966 else
1967 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1968 if (!ret) {
1969 err = GetLastError();
1970 if (err == ERROR_IO_PENDING) {
1971 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1972 if (ret) {
1973 buf += size;
1974 len -= size;
1975 } else {
1976 break;
1977 }
1978 } else {
1979 break;
1980 }
1981 } else {
1982 buf += size;
1983 len -= size;
1984 }
1985 }
1986 return len1 - len;
1987}
1988
1989static int win_chr_read_poll(CharDriverState *chr)
1990{
1991 WinCharState *s = chr->opaque;
1992
Anthony Liguori909cda12011-08-15 11:17:31 -05001993 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001994 return s->max_size;
1995}
1996
1997static void win_chr_readfile(CharDriverState *chr)
1998{
1999 WinCharState *s = chr->opaque;
2000 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302001 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002002 DWORD size;
2003
2004 ZeroMemory(&s->orecv, sizeof(s->orecv));
2005 s->orecv.hEvent = s->hrecv;
2006 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2007 if (!ret) {
2008 err = GetLastError();
2009 if (err == ERROR_IO_PENDING) {
2010 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2011 }
2012 }
2013
2014 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002015 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002016 }
2017}
2018
2019static void win_chr_read(CharDriverState *chr)
2020{
2021 WinCharState *s = chr->opaque;
2022
2023 if (s->len > s->max_size)
2024 s->len = s->max_size;
2025 if (s->len == 0)
2026 return;
2027
2028 win_chr_readfile(chr);
2029}
2030
2031static int win_chr_poll(void *opaque)
2032{
2033 CharDriverState *chr = opaque;
2034 WinCharState *s = chr->opaque;
2035 COMSTAT status;
2036 DWORD comerr;
2037
2038 ClearCommError(s->hcom, &comerr, &status);
2039 if (status.cbInQue > 0) {
2040 s->len = status.cbInQue;
2041 win_chr_read_poll(chr);
2042 win_chr_read(chr);
2043 return 1;
2044 }
2045 return 0;
2046}
2047
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01002048static CharDriverState *qemu_chr_open_win_path(const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +00002049{
2050 CharDriverState *chr;
2051 WinCharState *s;
2052
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002053 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002054 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002055 chr->opaque = s;
2056 chr->chr_write = win_chr_write;
2057 chr->chr_close = win_chr_close;
2058
2059 if (win_chr_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002060 g_free(s);
2061 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002062 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002063 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002064 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002065}
2066
2067static int win_chr_pipe_poll(void *opaque)
2068{
2069 CharDriverState *chr = opaque;
2070 WinCharState *s = chr->opaque;
2071 DWORD size;
2072
2073 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2074 if (size > 0) {
2075 s->len = size;
2076 win_chr_read_poll(chr);
2077 win_chr_read(chr);
2078 return 1;
2079 }
2080 return 0;
2081}
2082
2083static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2084{
2085 WinCharState *s = chr->opaque;
2086 OVERLAPPED ov;
2087 int ret;
2088 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002089 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002090
2091 s->fpipe = TRUE;
2092
2093 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2094 if (!s->hsend) {
2095 fprintf(stderr, "Failed CreateEvent\n");
2096 goto fail;
2097 }
2098 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2099 if (!s->hrecv) {
2100 fprintf(stderr, "Failed CreateEvent\n");
2101 goto fail;
2102 }
2103
2104 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2105 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2106 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2107 PIPE_WAIT,
2108 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2109 if (s->hcom == INVALID_HANDLE_VALUE) {
2110 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2111 s->hcom = NULL;
2112 goto fail;
2113 }
2114
2115 ZeroMemory(&ov, sizeof(ov));
2116 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2117 ret = ConnectNamedPipe(s->hcom, &ov);
2118 if (ret) {
2119 fprintf(stderr, "Failed ConnectNamedPipe\n");
2120 goto fail;
2121 }
2122
2123 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2124 if (!ret) {
2125 fprintf(stderr, "Failed GetOverlappedResult\n");
2126 if (ov.hEvent) {
2127 CloseHandle(ov.hEvent);
2128 ov.hEvent = NULL;
2129 }
2130 goto fail;
2131 }
2132
2133 if (ov.hEvent) {
2134 CloseHandle(ov.hEvent);
2135 ov.hEvent = NULL;
2136 }
2137 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2138 return 0;
2139
2140 fail:
2141 win_chr_close(chr);
2142 return -1;
2143}
2144
2145
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002146static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002147{
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002148 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002149 CharDriverState *chr;
2150 WinCharState *s;
2151
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002152 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002153 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002154 chr->opaque = s;
2155 chr->chr_write = win_chr_write;
2156 chr->chr_close = win_chr_close;
2157
2158 if (win_chr_pipe_init(chr, filename) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002159 g_free(s);
2160 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002161 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002162 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002163 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002164}
2165
Markus Armbruster1f514702012-02-07 15:09:08 +01002166static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002167{
2168 CharDriverState *chr;
2169 WinCharState *s;
2170
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002171 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002172 s = g_malloc0(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00002173 s->hcom = fd_out;
2174 chr->opaque = s;
2175 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002176 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002177}
2178
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01002179static CharDriverState *qemu_chr_open_win_con(void)
aliguori6f97dba2008-10-31 18:49:55 +00002180{
Markus Armbruster1f514702012-02-07 15:09:08 +01002181 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002182}
2183
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002184static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2185{
2186 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2187 DWORD dwSize;
2188 int len1;
2189
2190 len1 = len;
2191
2192 while (len1 > 0) {
2193 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2194 break;
2195 }
2196 buf += dwSize;
2197 len1 -= dwSize;
2198 }
2199
2200 return len - len1;
2201}
2202
2203static void win_stdio_wait_func(void *opaque)
2204{
2205 CharDriverState *chr = opaque;
2206 WinStdioCharState *stdio = chr->opaque;
2207 INPUT_RECORD buf[4];
2208 int ret;
2209 DWORD dwSize;
2210 int i;
2211
Stefan Weildff74242013-12-07 14:48:04 +01002212 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002213
2214 if (!ret) {
2215 /* Avoid error storm */
2216 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2217 return;
2218 }
2219
2220 for (i = 0; i < dwSize; i++) {
2221 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2222
2223 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2224 int j;
2225 if (kev->uChar.AsciiChar != 0) {
2226 for (j = 0; j < kev->wRepeatCount; j++) {
2227 if (qemu_chr_be_can_write(chr)) {
2228 uint8_t c = kev->uChar.AsciiChar;
2229 qemu_chr_be_write(chr, &c, 1);
2230 }
2231 }
2232 }
2233 }
2234 }
2235}
2236
2237static DWORD WINAPI win_stdio_thread(LPVOID param)
2238{
2239 CharDriverState *chr = param;
2240 WinStdioCharState *stdio = chr->opaque;
2241 int ret;
2242 DWORD dwSize;
2243
2244 while (1) {
2245
2246 /* Wait for one byte */
2247 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2248
2249 /* Exit in case of error, continue if nothing read */
2250 if (!ret) {
2251 break;
2252 }
2253 if (!dwSize) {
2254 continue;
2255 }
2256
2257 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2258 if (stdio->win_stdio_buf == '\r') {
2259 continue;
2260 }
2261
2262 /* Signal the main thread and wait until the byte was eaten */
2263 if (!SetEvent(stdio->hInputReadyEvent)) {
2264 break;
2265 }
2266 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2267 != WAIT_OBJECT_0) {
2268 break;
2269 }
2270 }
2271
2272 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2273 return 0;
2274}
2275
2276static void win_stdio_thread_wait_func(void *opaque)
2277{
2278 CharDriverState *chr = opaque;
2279 WinStdioCharState *stdio = chr->opaque;
2280
2281 if (qemu_chr_be_can_write(chr)) {
2282 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2283 }
2284
2285 SetEvent(stdio->hInputDoneEvent);
2286}
2287
2288static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2289{
2290 WinStdioCharState *stdio = chr->opaque;
2291 DWORD dwMode = 0;
2292
2293 GetConsoleMode(stdio->hStdIn, &dwMode);
2294
2295 if (echo) {
2296 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2297 } else {
2298 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2299 }
2300}
2301
2302static void win_stdio_close(CharDriverState *chr)
2303{
2304 WinStdioCharState *stdio = chr->opaque;
2305
2306 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2307 CloseHandle(stdio->hInputReadyEvent);
2308 }
2309 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2310 CloseHandle(stdio->hInputDoneEvent);
2311 }
2312 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2313 TerminateThread(stdio->hInputThread, 0);
2314 }
2315
2316 g_free(chr->opaque);
2317 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002318}
2319
Gerd Hoffmann7c358032013-02-21 12:34:58 +01002320static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002321{
2322 CharDriverState *chr;
2323 WinStdioCharState *stdio;
2324 DWORD dwMode;
2325 int is_console = 0;
2326
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002327 chr = qemu_chr_alloc();
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002328 stdio = g_malloc0(sizeof(WinStdioCharState));
2329
2330 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2331 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
2332 fprintf(stderr, "cannot open stdio: invalid handle\n");
2333 exit(1);
2334 }
2335
2336 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2337
2338 chr->opaque = stdio;
2339 chr->chr_write = win_stdio_write;
2340 chr->chr_close = win_stdio_close;
2341
Anthony Liguoried7a1542013-03-05 23:21:17 +05302342 if (is_console) {
2343 if (qemu_add_wait_object(stdio->hStdIn,
2344 win_stdio_wait_func, chr)) {
2345 fprintf(stderr, "qemu_add_wait_object: failed\n");
2346 }
2347 } else {
2348 DWORD dwId;
2349
2350 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2351 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2352 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2353 chr, 0, &dwId);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002354
Anthony Liguoried7a1542013-03-05 23:21:17 +05302355 if (stdio->hInputThread == INVALID_HANDLE_VALUE
2356 || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
2357 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
2358 fprintf(stderr, "cannot create stdio thread or event\n");
2359 exit(1);
2360 }
2361 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2362 win_stdio_thread_wait_func, chr)) {
2363 fprintf(stderr, "qemu_add_wait_object: failed\n");
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002364 }
2365 }
2366
2367 dwMode |= ENABLE_LINE_INPUT;
2368
Anthony Liguoried7a1542013-03-05 23:21:17 +05302369 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002370 /* set the terminal in raw mode */
2371 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2372 dwMode |= ENABLE_PROCESSED_INPUT;
2373 }
2374
2375 SetConsoleMode(stdio->hStdIn, dwMode);
2376
2377 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2378 qemu_chr_fe_set_echo(chr, false);
2379
Markus Armbruster1f514702012-02-07 15:09:08 +01002380 return chr;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002381}
aliguori6f97dba2008-10-31 18:49:55 +00002382#endif /* !_WIN32 */
2383
Anthony Liguori5ab82112013-03-05 23:21:31 +05302384
aliguori6f97dba2008-10-31 18:49:55 +00002385/***********************************************************/
2386/* UDP Net console */
2387
2388typedef struct {
2389 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302390 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302391 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002392 int bufcnt;
2393 int bufptr;
2394 int max_size;
2395} NetCharDriver;
2396
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002397/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002398static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2399{
2400 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302401 gsize bytes_written;
2402 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002403
Anthony Liguori76a96442013-03-05 23:21:21 +05302404 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2405 if (status == G_IO_STATUS_EOF) {
2406 return 0;
2407 } else if (status != G_IO_STATUS_NORMAL) {
2408 return -1;
2409 }
2410
2411 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002412}
2413
2414static int udp_chr_read_poll(void *opaque)
2415{
2416 CharDriverState *chr = opaque;
2417 NetCharDriver *s = chr->opaque;
2418
Anthony Liguori909cda12011-08-15 11:17:31 -05002419 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002420
2421 /* If there were any stray characters in the queue process them
2422 * first
2423 */
2424 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002425 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002426 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002427 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002428 }
2429 return s->max_size;
2430}
2431
Anthony Liguori76a96442013-03-05 23:21:21 +05302432static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002433{
2434 CharDriverState *chr = opaque;
2435 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302436 gsize bytes_read = 0;
2437 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002438
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002439 if (s->max_size == 0) {
2440 return TRUE;
2441 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302442 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2443 &bytes_read, NULL);
2444 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002445 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302446 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302447 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302448 return FALSE;
2449 }
aliguori6f97dba2008-10-31 18:49:55 +00002450
2451 s->bufptr = 0;
2452 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002453 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002454 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002455 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002456 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302457
2458 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002459}
2460
2461static void udp_chr_update_read_handler(CharDriverState *chr)
2462{
2463 NetCharDriver *s = chr->opaque;
2464
Amit Shah26da70c2013-08-28 15:23:37 +05302465 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302466 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302467 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2468 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002469 }
2470}
2471
aliguori819f56b2009-03-28 17:58:14 +00002472static void udp_chr_close(CharDriverState *chr)
2473{
2474 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302475
2476 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302477 if (s->chan) {
2478 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002479 closesocket(s->fd);
2480 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002481 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002482 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002483}
2484
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002485static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002486{
2487 CharDriverState *chr = NULL;
2488 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002489
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002490 chr = qemu_chr_alloc();
Anthony Liguori7267c092011-08-20 22:09:37 -05002491 s = g_malloc0(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002492
aliguori6f97dba2008-10-31 18:49:55 +00002493 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302494 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002495 s->bufcnt = 0;
2496 s->bufptr = 0;
2497 chr->opaque = s;
2498 chr->chr_write = udp_chr_write;
2499 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002500 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002501 /* be isn't opened until we get a connection */
2502 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002503 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002504}
aliguori6f97dba2008-10-31 18:49:55 +00002505
aliguori6f97dba2008-10-31 18:49:55 +00002506/***********************************************************/
2507/* TCP Net console */
2508
2509typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302510
2511 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302512 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002513 int fd, listen_fd;
2514 int connected;
2515 int max_size;
2516 int do_telnetopt;
2517 int do_nodelay;
2518 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002519 int *read_msgfds;
2520 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002521 int *write_msgfds;
2522 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002523
2524 SocketAddress *addr;
2525 bool is_listen;
2526 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002527
2528 guint reconnect_timer;
2529 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002530 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002531} TCPCharDriver;
2532
Corey Minyard5dd1f022014-10-02 11:17:37 -05002533static gboolean socket_reconnect_timeout(gpointer opaque);
2534
2535static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2536{
2537 TCPCharDriver *s = chr->opaque;
2538 assert(s->connected == 0);
2539 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2540 socket_reconnect_timeout, chr);
2541}
2542
Corey Minyard5008e5b2014-10-08 07:11:55 -05002543static void check_report_connect_error(CharDriverState *chr,
2544 Error *err)
2545{
2546 TCPCharDriver *s = chr->opaque;
2547
2548 if (!s->connect_err_reported) {
2549 error_report("Unable to connect character device %s: %s",
2550 chr->label, error_get_pretty(err));
2551 s->connect_err_reported = true;
2552 }
2553 qemu_chr_socket_restart_timer(chr);
2554}
2555
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302556static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002557
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002558#ifndef _WIN32
2559static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2560{
2561 TCPCharDriver *s = chr->opaque;
2562 struct msghdr msgh;
2563 struct iovec iov;
2564 int r;
2565
2566 size_t fd_size = s->write_msgfds_num * sizeof(int);
2567 char control[CMSG_SPACE(fd_size)];
2568 struct cmsghdr *cmsg;
2569
2570 memset(&msgh, 0, sizeof(msgh));
2571 memset(control, 0, sizeof(control));
2572
2573 /* set the payload */
2574 iov.iov_base = (uint8_t *) buf;
2575 iov.iov_len = len;
2576
2577 msgh.msg_iov = &iov;
2578 msgh.msg_iovlen = 1;
2579
2580 msgh.msg_control = control;
2581 msgh.msg_controllen = sizeof(control);
2582
2583 cmsg = CMSG_FIRSTHDR(&msgh);
2584
2585 cmsg->cmsg_len = CMSG_LEN(fd_size);
2586 cmsg->cmsg_level = SOL_SOCKET;
2587 cmsg->cmsg_type = SCM_RIGHTS;
2588 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2589
2590 do {
2591 r = sendmsg(s->fd, &msgh, 0);
2592 } while (r < 0 && errno == EINTR);
2593
2594 /* free the written msgfds, no matter what */
2595 if (s->write_msgfds_num) {
2596 g_free(s->write_msgfds);
2597 s->write_msgfds = 0;
2598 s->write_msgfds_num = 0;
2599 }
2600
2601 return r;
2602}
2603#endif
2604
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002605/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002606static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2607{
2608 TCPCharDriver *s = chr->opaque;
2609 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002610#ifndef _WIN32
2611 if (s->is_unix && s->write_msgfds_num) {
2612 return unix_send_msgfds(chr, buf, len);
2613 } else
2614#endif
2615 {
2616 return io_channel_send(s->chan, buf, len);
2617 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002618 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002619 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002620 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002621 }
2622}
2623
2624static int tcp_chr_read_poll(void *opaque)
2625{
2626 CharDriverState *chr = opaque;
2627 TCPCharDriver *s = chr->opaque;
2628 if (!s->connected)
2629 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002630 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002631 return s->max_size;
2632}
2633
2634#define IAC 255
2635#define IAC_BREAK 243
2636static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2637 TCPCharDriver *s,
2638 uint8_t *buf, int *size)
2639{
2640 /* Handle any telnet client's basic IAC options to satisfy char by
2641 * char mode with no echo. All IAC options will be removed from
2642 * the buf and the do_telnetopt variable will be used to track the
2643 * state of the width of the IAC information.
2644 *
2645 * IAC commands come in sets of 3 bytes with the exception of the
2646 * "IAC BREAK" command and the double IAC.
2647 */
2648
2649 int i;
2650 int j = 0;
2651
2652 for (i = 0; i < *size; i++) {
2653 if (s->do_telnetopt > 1) {
2654 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2655 /* Double IAC means send an IAC */
2656 if (j != i)
2657 buf[j] = buf[i];
2658 j++;
2659 s->do_telnetopt = 1;
2660 } else {
2661 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2662 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002663 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002664 s->do_telnetopt++;
2665 }
2666 s->do_telnetopt++;
2667 }
2668 if (s->do_telnetopt >= 4) {
2669 s->do_telnetopt = 1;
2670 }
2671 } else {
2672 if ((unsigned char)buf[i] == IAC) {
2673 s->do_telnetopt = 2;
2674 } else {
2675 if (j != i)
2676 buf[j] = buf[i];
2677 j++;
2678 }
2679 }
2680 }
2681 *size = j;
2682}
2683
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002684static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002685{
2686 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002687 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2688
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002689 assert(num <= TCP_MAX_FDS);
2690
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002691 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002692 int i;
2693
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002694 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2695
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002696 /* Close unused fds */
2697 for (i = to_copy; i < s->read_msgfds_num; i++) {
2698 close(s->read_msgfds[i]);
2699 }
2700
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002701 g_free(s->read_msgfds);
2702 s->read_msgfds = 0;
2703 s->read_msgfds_num = 0;
2704 }
2705
2706 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002707}
2708
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002709static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2710{
2711 TCPCharDriver *s = chr->opaque;
2712
2713 /* clear old pending fd array */
2714 if (s->write_msgfds) {
2715 g_free(s->write_msgfds);
2716 }
2717
2718 if (num) {
2719 s->write_msgfds = g_malloc(num * sizeof(int));
2720 memcpy(s->write_msgfds, fds, num * sizeof(int));
2721 }
2722
2723 s->write_msgfds_num = num;
2724
2725 return 0;
2726}
2727
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002728#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002729static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2730{
2731 TCPCharDriver *s = chr->opaque;
2732 struct cmsghdr *cmsg;
2733
2734 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002735 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002736
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002737 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002738 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002739 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002740 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002741 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002742
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002743 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2744
2745 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002746 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002747 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002748
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002749 /* close and clean read_msgfds */
2750 for (i = 0; i < s->read_msgfds_num; i++) {
2751 close(s->read_msgfds[i]);
2752 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002753
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002754 if (s->read_msgfds_num) {
2755 g_free(s->read_msgfds);
2756 }
2757
2758 s->read_msgfds_num = fd_size / sizeof(int);
2759 s->read_msgfds = g_malloc(fd_size);
2760 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2761
2762 for (i = 0; i < s->read_msgfds_num; i++) {
2763 int fd = s->read_msgfds[i];
2764 if (fd < 0) {
2765 continue;
2766 }
2767
2768 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2769 qemu_set_block(fd);
2770
2771 #ifndef MSG_CMSG_CLOEXEC
2772 qemu_set_cloexec(fd);
2773 #endif
2774 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002775 }
2776}
2777
Mark McLoughlin9977c892009-07-22 09:11:38 +01002778static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2779{
2780 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002781 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002782 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002783 union {
2784 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002785 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002786 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002787 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002788 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002789
2790 iov[0].iov_base = buf;
2791 iov[0].iov_len = len;
2792
2793 msg.msg_iov = iov;
2794 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002795 msg.msg_control = &msg_control;
2796 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002797
Corey Bryant06138652012-08-14 16:43:42 -04002798#ifdef MSG_CMSG_CLOEXEC
2799 flags |= MSG_CMSG_CLOEXEC;
2800#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002801 do {
2802 ret = recvmsg(s->fd, &msg, flags);
2803 } while (ret == -1 && errno == EINTR);
2804
Corey Bryant06138652012-08-14 16:43:42 -04002805 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002806 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002807 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002808
2809 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002810}
2811#else
2812static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2813{
2814 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002815 ssize_t ret;
2816
2817 do {
2818 ret = qemu_recv(s->fd, buf, len, 0);
2819 } while (ret == -1 && socket_error() == EINTR);
2820
2821 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002822}
2823#endif
2824
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302825static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2826{
2827 TCPCharDriver *s = chr->opaque;
2828 return g_io_create_watch(s->chan, cond);
2829}
2830
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002831static void tcp_chr_disconnect(CharDriverState *chr)
2832{
2833 TCPCharDriver *s = chr->opaque;
2834
2835 s->connected = 0;
2836 if (s->listen_chan) {
2837 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2838 tcp_chr_accept, chr);
2839 }
2840 remove_fd_in_watch(chr);
2841 g_io_channel_unref(s->chan);
2842 s->chan = NULL;
2843 closesocket(s->fd);
2844 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002845 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2846 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002847 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002848 if (s->reconnect_time) {
2849 qemu_chr_socket_restart_timer(chr);
2850 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002851}
2852
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302853static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002854{
2855 CharDriverState *chr = opaque;
2856 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302857 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002858 int len, size;
2859
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302860 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002861 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302862 }
aliguori6f97dba2008-10-31 18:49:55 +00002863 len = sizeof(buf);
2864 if (len > s->max_size)
2865 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002866 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002867 if (size == 0 ||
2868 (size < 0 &&
2869 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002870 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002871 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002872 } else if (size > 0) {
2873 if (s->do_telnetopt)
2874 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2875 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002876 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002877 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302878
2879 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002880}
2881
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002882static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2883{
2884 TCPCharDriver *s = chr->opaque;
2885 int size;
2886
2887 if (!s->connected) {
2888 return 0;
2889 }
2890
2891 size = tcp_chr_recv(chr, (void *) buf, len);
2892 if (size == 0) {
2893 /* connection closed */
2894 tcp_chr_disconnect(chr);
2895 }
2896
2897 return size;
2898}
2899
Blue Swirl68c18d12010-08-15 09:46:24 +00002900#ifndef _WIN32
2901CharDriverState *qemu_chr_open_eventfd(int eventfd)
2902{
David Marchande9d21c42014-06-11 17:25:16 +02002903 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2904
2905 if (chr) {
2906 chr->avail_connections = 1;
2907 }
2908
2909 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002910}
Blue Swirl68c18d12010-08-15 09:46:24 +00002911#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002912
aliguori6f97dba2008-10-31 18:49:55 +00002913static void tcp_chr_connect(void *opaque)
2914{
2915 CharDriverState *chr = opaque;
2916 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002917 struct sockaddr_storage ss, ps;
2918 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002919
2920 memset(&ss, 0, ss_len);
2921 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2922 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2923 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002924 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2925 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2926 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002927 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002928 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2929 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002930 s->is_listen, s->is_telnet);
2931 }
aliguori6f97dba2008-10-31 18:49:55 +00002932
2933 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302934 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302935 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2936 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002937 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002938 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002939}
2940
Gal Hammerac1b84d2014-02-25 12:12:35 +02002941static void tcp_chr_update_read_handler(CharDriverState *chr)
2942{
2943 TCPCharDriver *s = chr->opaque;
2944
2945 remove_fd_in_watch(chr);
2946 if (s->chan) {
2947 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2948 tcp_chr_read, chr);
2949 }
2950}
2951
aliguori6f97dba2008-10-31 18:49:55 +00002952#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2953static void tcp_chr_telnet_init(int fd)
2954{
2955 char buf[3];
2956 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2957 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2958 send(fd, (char *)buf, 3, 0);
2959 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2960 send(fd, (char *)buf, 3, 0);
2961 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2962 send(fd, (char *)buf, 3, 0);
2963 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2964 send(fd, (char *)buf, 3, 0);
2965}
2966
Daniel P. Berrange13661082011-06-23 13:31:42 +01002967static int tcp_chr_add_client(CharDriverState *chr, int fd)
2968{
2969 TCPCharDriver *s = chr->opaque;
2970 if (s->fd != -1)
2971 return -1;
2972
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002973 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002974 if (s->do_nodelay)
2975 socket_set_nodelay(fd);
2976 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302977 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002978 if (s->listen_tag) {
2979 g_source_remove(s->listen_tag);
2980 s->listen_tag = 0;
2981 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01002982 tcp_chr_connect(chr);
2983
2984 return 0;
2985}
2986
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302987static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002988{
2989 CharDriverState *chr = opaque;
2990 TCPCharDriver *s = chr->opaque;
2991 struct sockaddr_in saddr;
2992#ifndef _WIN32
2993 struct sockaddr_un uaddr;
2994#endif
2995 struct sockaddr *addr;
2996 socklen_t len;
2997 int fd;
2998
2999 for(;;) {
3000#ifndef _WIN32
3001 if (s->is_unix) {
3002 len = sizeof(uaddr);
3003 addr = (struct sockaddr *)&uaddr;
3004 } else
3005#endif
3006 {
3007 len = sizeof(saddr);
3008 addr = (struct sockaddr *)&saddr;
3009 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003010 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003011 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003012 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303013 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003014 } else if (fd >= 0) {
3015 if (s->do_telnetopt)
3016 tcp_chr_telnet_init(fd);
3017 break;
3018 }
3019 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003020 if (tcp_chr_add_client(chr, fd) < 0)
3021 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303022
3023 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003024}
3025
3026static void tcp_chr_close(CharDriverState *chr)
3027{
3028 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003029 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003030
Corey Minyard5dd1f022014-10-02 11:17:37 -05003031 if (s->reconnect_timer) {
3032 g_source_remove(s->reconnect_timer);
3033 s->reconnect_timer = 0;
3034 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003035 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003036 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303037 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303038 if (s->chan) {
3039 g_io_channel_unref(s->chan);
3040 }
aliguori6f97dba2008-10-31 18:49:55 +00003041 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003042 }
3043 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303044 if (s->listen_tag) {
3045 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003046 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303047 }
3048 if (s->listen_chan) {
3049 g_io_channel_unref(s->listen_chan);
3050 }
aliguori6f97dba2008-10-31 18:49:55 +00003051 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003052 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003053 if (s->read_msgfds_num) {
3054 for (i = 0; i < s->read_msgfds_num; i++) {
3055 close(s->read_msgfds[i]);
3056 }
3057 g_free(s->read_msgfds);
3058 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003059 if (s->write_msgfds_num) {
3060 g_free(s->write_msgfds);
3061 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003062 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003063 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003064}
3065
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003066static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003067{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003068 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003069
Corey Minyardcfb429c2014-10-02 11:17:35 -05003070 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003071 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303072 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003073 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3074 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003075 } else {
3076 s->connected = 1;
3077 s->fd = fd;
3078 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303079 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003080 tcp_chr_connect(chr);
3081 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003082}
3083
Corey Minyard51795022014-10-08 07:11:56 -05003084static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003085{
3086 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003087 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003088
3089 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003090 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003091 return;
3092 }
3093
Corey Minyard5008e5b2014-10-08 07:11:55 -05003094 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003095 qemu_chr_finish_socket_connection(chr, fd);
3096}
3097
Corey Minyardcfb429c2014-10-02 11:17:35 -05003098static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003099{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003100 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003101 int fd;
3102
Corey Minyardcfb429c2014-10-02 11:17:35 -05003103 if (s->is_listen) {
3104 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003105 } else if (s->reconnect_time) {
3106 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3107 return fd >= 0;
3108 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003109 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003110 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003111 if (fd < 0) {
3112 return false;
3113 }
3114
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003115 qemu_chr_finish_socket_connection(chr, fd);
3116 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003117}
3118
Lei Li51767e72013-01-25 00:03:19 +08003119/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003120/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003121
3122typedef struct {
3123 size_t size;
3124 size_t prod;
3125 size_t cons;
3126 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003127} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003128
Markus Armbruster3949e592013-02-06 21:27:24 +01003129static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003130{
Markus Armbruster3949e592013-02-06 21:27:24 +01003131 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003132
Markus Armbruster5c230102013-02-06 21:27:23 +01003133 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003134}
3135
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003136/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003137static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003138{
Markus Armbruster3949e592013-02-06 21:27:24 +01003139 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003140 int i;
3141
3142 if (!buf || (len < 0)) {
3143 return -1;
3144 }
3145
3146 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003147 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3148 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003149 d->cons = d->prod - d->size;
3150 }
3151 }
3152
3153 return 0;
3154}
3155
Markus Armbruster3949e592013-02-06 21:27:24 +01003156static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003157{
Markus Armbruster3949e592013-02-06 21:27:24 +01003158 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003159 int i;
3160
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003161 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003162 for (i = 0; i < len && d->cons != d->prod; i++) {
3163 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003164 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003165 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003166
3167 return i;
3168}
3169
Markus Armbruster3949e592013-02-06 21:27:24 +01003170static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003171{
Markus Armbruster3949e592013-02-06 21:27:24 +01003172 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003173
3174 g_free(d->cbuf);
3175 g_free(d);
3176 chr->opaque = NULL;
3177}
3178
Markus Armbruster4f573782013-07-26 16:44:32 +02003179static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
3180 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003181{
3182 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003183 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003184
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003185 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003186 d = g_malloc(sizeof(*d));
3187
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003188 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003189
3190 /* The size must be power of 2 */
3191 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003192 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003193 goto fail;
3194 }
3195
3196 d->prod = 0;
3197 d->cons = 0;
3198 d->cbuf = g_malloc0(d->size);
3199
3200 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003201 chr->chr_write = ringbuf_chr_write;
3202 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003203
3204 return chr;
3205
3206fail:
3207 g_free(d);
3208 g_free(chr);
3209 return NULL;
3210}
3211
Hani Benhabiles8e597772014-05-27 23:39:30 +01003212bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003213{
Markus Armbruster3949e592013-02-06 21:27:24 +01003214 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003215}
3216
Markus Armbruster3949e592013-02-06 21:27:24 +01003217void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003218 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003219 Error **errp)
3220{
3221 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003222 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003223 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003224 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +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 Li1f590cf2013-01-25 00:03:20 +08003229 return;
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 Li1f590cf2013-01-25 00:03:20 +08003234 return;
3235 }
3236
Lei Li1f590cf2013-01-25 00:03:20 +08003237 if (has_format && (format == DATA_FORMAT_BASE64)) {
3238 write_data = g_base64_decode(data, &write_count);
3239 } else {
3240 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003241 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003242 }
3243
Markus Armbruster3949e592013-02-06 21:27:24 +01003244 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003245
Markus Armbruster13289fb2013-02-06 21:27:18 +01003246 if (write_data != (uint8_t *)data) {
3247 g_free((void *)write_data);
3248 }
3249
Lei Li1f590cf2013-01-25 00:03:20 +08003250 if (ret < 0) {
3251 error_setg(errp, "Failed to write to device %s", device);
3252 return;
3253 }
3254}
3255
Markus Armbruster3949e592013-02-06 21:27:24 +01003256char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003257 bool has_format, enum DataFormat format,
3258 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003259{
3260 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003261 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003262 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003263 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003264
3265 chr = qemu_chr_find(device);
3266 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003267 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003268 return NULL;
3269 }
3270
Markus Armbruster3949e592013-02-06 21:27:24 +01003271 if (!chr_is_ringbuf(chr)) {
3272 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003273 return NULL;
3274 }
3275
3276 if (size <= 0) {
3277 error_setg(errp, "size must be greater than zero");
3278 return NULL;
3279 }
3280
Markus Armbruster3949e592013-02-06 21:27:24 +01003281 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003282 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003283 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003284
Markus Armbruster3949e592013-02-06 21:27:24 +01003285 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003286
3287 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003288 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003289 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003290 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003291 /*
3292 * FIXME should read only complete, valid UTF-8 characters up
3293 * to @size bytes. Invalid sequences should be replaced by a
3294 * suitable replacement character. Except when (and only
3295 * when) ring buffer lost characters since last read, initial
3296 * continuation characters should be dropped.
3297 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003298 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003299 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003300 }
3301
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003302 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003303}
3304
Gerd Hoffmann33521632009-12-08 13:11:49 +01003305QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003306{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003307 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003308 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003309 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003310 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003311 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003312
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003313 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003314 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003315 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003316 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003317 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003318
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003319 if (strstart(filename, "mon:", &p)) {
3320 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003321 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003322 if (strcmp(filename, "stdio") == 0) {
3323 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3324 * but pass it to the guest. Handle this only for compat syntax,
3325 * for -chardev syntax we have special option for this.
3326 * This is what -nographic did, redirecting+muxing serial+monitor
3327 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003328 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003329 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003330 }
3331
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003332 if (strcmp(filename, "null") == 0 ||
3333 strcmp(filename, "pty") == 0 ||
3334 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003335 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003336 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003337 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003338 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003339 return opts;
3340 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003341 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003342 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003343 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003344 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003345 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003346 qemu_opt_set(opts, "width", width, &error_abort);
3347 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003348 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003349 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003350 qemu_opt_set(opts, "cols", width, &error_abort);
3351 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003352 } else {
3353 goto fail;
3354 }
3355 }
3356 return opts;
3357 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003358 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003359 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003360 return opts;
3361 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003362 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003363 qemu_opt_set(opts, "backend", "serial", &error_abort);
3364 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003365 return opts;
3366 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003367 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003368 qemu_opt_set(opts, "backend", "file", &error_abort);
3369 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003370 return opts;
3371 }
3372 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003373 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3374 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003375 return opts;
3376 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003377 if (strstart(filename, "tcp:", &p) ||
3378 strstart(filename, "telnet:", &p)) {
3379 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3380 host[0] = 0;
3381 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3382 goto fail;
3383 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003384 qemu_opt_set(opts, "backend", "socket", &error_abort);
3385 qemu_opt_set(opts, "host", host, &error_abort);
3386 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003387 if (p[pos] == ',') {
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003388 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3389 if (local_err) {
3390 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003391 goto fail;
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003392 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003393 }
3394 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003395 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003396 return opts;
3397 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003398 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003399 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003400 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3401 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003402 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003403 goto fail;
3404 }
3405 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003406 qemu_opt_set(opts, "host", host, &error_abort);
3407 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003408 if (p[pos] == '@') {
3409 p += pos + 1;
3410 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3411 host[0] = 0;
3412 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003413 goto fail;
3414 }
3415 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003416 qemu_opt_set(opts, "localaddr", host, &error_abort);
3417 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003418 }
3419 return opts;
3420 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003421 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003422 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003423 qemu_opts_do_parse(opts, p, "path", &local_err);
3424 if (local_err) {
3425 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003426 goto fail;
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003427 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003428 return opts;
3429 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003430 if (strstart(filename, "/dev/parport", NULL) ||
3431 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003432 qemu_opt_set(opts, "backend", "parport", &error_abort);
3433 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003434 return opts;
3435 }
3436 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003437 qemu_opt_set(opts, "backend", "tty", &error_abort);
3438 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003439 return opts;
3440 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003441
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003442fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003443 qemu_opts_del(opts);
3444 return NULL;
3445}
3446
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003447static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3448 Error **errp)
3449{
3450 const char *path = qemu_opt_get(opts, "path");
3451
3452 if (path == NULL) {
3453 error_setg(errp, "chardev: file: no filename given");
3454 return;
3455 }
3456 backend->file = g_new0(ChardevFile, 1);
3457 backend->file->out = g_strdup(path);
3458}
3459
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003460static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3461 Error **errp)
3462{
3463 backend->stdio = g_new0(ChardevStdio, 1);
3464 backend->stdio->has_signal = true;
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003465 backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003466}
3467
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003468static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3469 Error **errp)
3470{
3471 const char *device = qemu_opt_get(opts, "path");
3472
3473 if (device == NULL) {
3474 error_setg(errp, "chardev: serial/tty: no device path given");
3475 return;
3476 }
3477 backend->serial = g_new0(ChardevHostdev, 1);
3478 backend->serial->device = g_strdup(device);
3479}
3480
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003481static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3482 Error **errp)
3483{
3484 const char *device = qemu_opt_get(opts, "path");
3485
3486 if (device == NULL) {
3487 error_setg(errp, "chardev: parallel: no device path given");
3488 return;
3489 }
3490 backend->parallel = g_new0(ChardevHostdev, 1);
3491 backend->parallel->device = g_strdup(device);
3492}
3493
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003494static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3495 Error **errp)
3496{
3497 const char *device = qemu_opt_get(opts, "path");
3498
3499 if (device == NULL) {
3500 error_setg(errp, "chardev: pipe: no device path given");
3501 return;
3502 }
3503 backend->pipe = g_new0(ChardevHostdev, 1);
3504 backend->pipe->device = g_strdup(device);
3505}
3506
Markus Armbruster4f573782013-07-26 16:44:32 +02003507static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3508 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003509{
3510 int val;
3511
Markus Armbruster3a1da422013-07-26 16:44:34 +02003512 backend->ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003513
Markus Armbruster0f953052013-06-27 16:22:07 +02003514 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003515 if (val != 0) {
Markus Armbruster3a1da422013-07-26 16:44:34 +02003516 backend->ringbuf->has_size = true;
3517 backend->ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003518 }
3519}
3520
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003521static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3522 Error **errp)
3523{
3524 const char *chardev = qemu_opt_get(opts, "chardev");
3525
3526 if (chardev == NULL) {
3527 error_setg(errp, "chardev: mux: no chardev given");
3528 return;
3529 }
3530 backend->mux = g_new0(ChardevMux, 1);
3531 backend->mux->chardev = g_strdup(chardev);
3532}
3533
Peter Maydelldafd3252014-09-02 11:24:13 +01003534static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3535 Error **errp)
3536{
3537 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3538 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3539 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3540 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003541 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003542 const char *path = qemu_opt_get(opts, "path");
3543 const char *host = qemu_opt_get(opts, "host");
3544 const char *port = qemu_opt_get(opts, "port");
3545 SocketAddress *addr;
3546
3547 if (!path) {
3548 if (!host) {
3549 error_setg(errp, "chardev: socket: no host given");
3550 return;
3551 }
3552 if (!port) {
3553 error_setg(errp, "chardev: socket: no port given");
3554 return;
3555 }
3556 }
3557
3558 backend->socket = g_new0(ChardevSocket, 1);
3559
3560 backend->socket->has_nodelay = true;
3561 backend->socket->nodelay = do_nodelay;
3562 backend->socket->has_server = true;
3563 backend->socket->server = is_listen;
3564 backend->socket->has_telnet = true;
3565 backend->socket->telnet = is_telnet;
3566 backend->socket->has_wait = true;
3567 backend->socket->wait = is_waitconnect;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003568 backend->socket->has_reconnect = true;
3569 backend->socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003570
3571 addr = g_new0(SocketAddress, 1);
3572 if (path) {
3573 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
3574 addr->q_unix = g_new0(UnixSocketAddress, 1);
3575 addr->q_unix->path = g_strdup(path);
3576 } else {
3577 addr->kind = SOCKET_ADDRESS_KIND_INET;
3578 addr->inet = g_new0(InetSocketAddress, 1);
3579 addr->inet->host = g_strdup(host);
3580 addr->inet->port = g_strdup(port);
3581 addr->inet->has_to = qemu_opt_get(opts, "to");
3582 addr->inet->to = qemu_opt_get_number(opts, "to", 0);
3583 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3584 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3585 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3586 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3587 }
3588 backend->socket->addr = addr;
3589}
3590
Peter Maydell90a14bf2014-09-02 11:24:15 +01003591static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3592 Error **errp)
3593{
3594 const char *host = qemu_opt_get(opts, "host");
3595 const char *port = qemu_opt_get(opts, "port");
3596 const char *localaddr = qemu_opt_get(opts, "localaddr");
3597 const char *localport = qemu_opt_get(opts, "localport");
3598 bool has_local = false;
3599 SocketAddress *addr;
3600
3601 if (host == NULL || strlen(host) == 0) {
3602 host = "localhost";
3603 }
3604 if (port == NULL || strlen(port) == 0) {
3605 error_setg(errp, "chardev: udp: remote port not specified");
3606 return;
3607 }
3608 if (localport == NULL || strlen(localport) == 0) {
3609 localport = "0";
3610 } else {
3611 has_local = true;
3612 }
3613 if (localaddr == NULL || strlen(localaddr) == 0) {
3614 localaddr = "";
3615 } else {
3616 has_local = true;
3617 }
3618
3619 backend->udp = g_new0(ChardevUdp, 1);
3620
3621 addr = g_new0(SocketAddress, 1);
3622 addr->kind = SOCKET_ADDRESS_KIND_INET;
3623 addr->inet = g_new0(InetSocketAddress, 1);
3624 addr->inet->host = g_strdup(host);
3625 addr->inet->port = g_strdup(port);
3626 addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3627 addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3628 addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3629 addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3630 backend->udp->remote = addr;
3631
3632 if (has_local) {
3633 backend->udp->has_local = true;
3634 addr = g_new0(SocketAddress, 1);
3635 addr->kind = SOCKET_ADDRESS_KIND_INET;
3636 addr->inet = g_new0(InetSocketAddress, 1);
3637 addr->inet->host = g_strdup(localaddr);
3638 addr->inet->port = g_strdup(localport);
3639 backend->udp->local = addr;
3640 }
3641}
3642
Anthony Liguorid654f342013-03-05 23:21:28 +05303643typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003644 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003645 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003646 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303647} CharDriver;
3648
3649static GSList *backends;
3650
Peter Maydelle4d50d42014-09-02 11:24:17 +01003651void register_char_driver(const char *name, ChardevBackendKind kind,
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003652 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
3653{
3654 CharDriver *s;
3655
3656 s = g_malloc0(sizeof(*s));
3657 s->name = g_strdup(name);
3658 s->kind = kind;
3659 s->parse = parse;
3660
3661 backends = g_slist_append(backends, s);
3662}
3663
Anthony Liguorif69554b2011-08-15 11:17:37 -05003664CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003665 void (*init)(struct CharDriverState *s),
3666 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003667{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003668 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303669 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003670 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303671 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003672 ChardevReturn *ret = NULL;
3673 ChardevBackend *backend;
3674 const char *id = qemu_opts_id(opts);
3675 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003676
Peter Maydella61ae7f2014-09-02 11:24:16 +01003677 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003678 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003679 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003680 }
3681
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003682 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003683 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003684 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003685 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003686 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303687 for (i = backends; i; i = i->next) {
3688 cd = i->data;
3689
3690 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003691 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303692 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003693 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303694 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003695 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003696 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003697 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003698 }
3699
Peter Maydella61ae7f2014-09-02 11:24:16 +01003700 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003701
3702 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003703 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003704 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003705
3706 chr = NULL;
3707 backend->kind = cd->kind;
3708 if (cd->parse) {
3709 cd->parse(opts, backend, &local_err);
3710 if (local_err) {
3711 error_propagate(errp, local_err);
3712 goto qapi_out;
3713 }
3714 }
3715 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3716 if (!ret) {
3717 goto qapi_out;
3718 }
3719
3720 if (bid) {
3721 qapi_free_ChardevBackend(backend);
3722 qapi_free_ChardevReturn(ret);
3723 backend = g_new0(ChardevBackend, 1);
3724 backend->mux = g_new0(ChardevMux, 1);
3725 backend->kind = CHARDEV_BACKEND_KIND_MUX;
3726 backend->mux->chardev = g_strdup(bid);
3727 ret = qmp_chardev_add(id, backend, errp);
3728 if (!ret) {
3729 chr = qemu_chr_find(bid);
3730 qemu_chr_delete(chr);
3731 chr = NULL;
3732 goto qapi_out;
3733 }
3734 }
3735
3736 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003737 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003738
3739qapi_out:
3740 qapi_free_ChardevBackend(backend);
3741 qapi_free_ChardevReturn(ret);
3742 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003743 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003744
3745err:
3746 qemu_opts_del(opts);
3747 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003748}
3749
Anthony Liguori27143a42011-08-15 11:17:36 -05003750CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003751{
3752 const char *p;
3753 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003754 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003755 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003756
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003757 if (strstart(filename, "chardev:", &p)) {
3758 return qemu_chr_find(p);
3759 }
3760
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003761 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003762 if (!opts)
3763 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003764
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003765 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003766 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003767 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003768 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003769 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003770 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003771 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003772 }
3773 return chr;
3774}
3775
Anthony Liguori15f31512011-08-15 11:17:35 -05003776void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003777{
3778 if (chr->chr_set_echo) {
3779 chr->chr_set_echo(chr, echo);
3780 }
3781}
3782
Hans de Goede8e25daa2013-03-26 11:07:57 +01003783void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003784{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003785 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003786 return;
3787 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003788 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003789 if (chr->chr_set_fe_open) {
3790 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003791 }
3792}
3793
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003794void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3795{
3796 if (chr->chr_fe_event) {
3797 chr->chr_fe_event(chr, event);
3798 }
3799}
3800
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003801int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3802 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303803{
3804 GSource *src;
3805 guint tag;
3806
3807 if (s->chr_add_watch == NULL) {
3808 return -ENOSYS;
3809 }
3810
3811 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003812 if (!src) {
3813 return -EINVAL;
3814 }
3815
Anthony Liguori23673ca2013-03-05 23:21:23 +05303816 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3817 tag = g_source_attach(src, NULL);
3818 g_source_unref(src);
3819
3820 return tag;
3821}
3822
Hans de Goede44c473d2013-03-27 20:29:39 +01003823int qemu_chr_fe_claim(CharDriverState *s)
3824{
3825 if (s->avail_connections < 1) {
3826 return -1;
3827 }
3828 s->avail_connections--;
3829 return 0;
3830}
3831
3832void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3833{
3834 if (qemu_chr_fe_claim(s) != 0) {
3835 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3836 __func__, s->label);
3837 exit(1);
3838 }
3839}
3840
3841void qemu_chr_fe_release(CharDriverState *s)
3842{
3843 s->avail_connections++;
3844}
3845
Anthony Liguori70f24fb2011-08-15 11:17:38 -05003846void qemu_chr_delete(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003847{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003848 QTAILQ_REMOVE(&chardevs, chr, next);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003849 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003850 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003851 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003852 g_free(chr->filename);
3853 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003854 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003855 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003856}
3857
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003858ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003859{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003860 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003861 CharDriverState *chr;
3862
Blue Swirl72cf2d42009-09-12 07:36:22 +00003863 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003864 ChardevInfoList *info = g_malloc0(sizeof(*info));
3865 info->value = g_malloc0(sizeof(*info->value));
3866 info->value->label = g_strdup(chr->label);
3867 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003868 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003869
3870 info->next = chr_list;
3871 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003872 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003873
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003874 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003875}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003876
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003877ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3878{
3879 ChardevBackendInfoList *backend_list = NULL;
3880 CharDriver *c = NULL;
3881 GSList *i = NULL;
3882
3883 for (i = backends; i; i = i->next) {
3884 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3885 c = i->data;
3886 info->value = g_malloc0(sizeof(*info->value));
3887 info->value->name = g_strdup(c->name);
3888
3889 info->next = backend_list;
3890 backend_list = info;
3891 }
3892
3893 return backend_list;
3894}
3895
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003896CharDriverState *qemu_chr_find(const char *name)
3897{
3898 CharDriverState *chr;
3899
Blue Swirl72cf2d42009-09-12 07:36:22 +00003900 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003901 if (strcmp(chr->label, name) != 0)
3902 continue;
3903 return chr;
3904 }
3905 return NULL;
3906}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003907
3908/* Get a character (serial) device interface. */
3909CharDriverState *qemu_char_get_next_serial(void)
3910{
3911 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003912 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003913
3914 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003915
3916 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3917 chr = serial_hds[next_serial++];
3918 qemu_chr_fe_claim_no_fail(chr);
3919 return chr;
3920 }
3921 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003922}
3923
Paolo Bonzini4d454572012-11-26 16:03:42 +01003924QemuOptsList qemu_chardev_opts = {
3925 .name = "chardev",
3926 .implied_opt_name = "backend",
3927 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3928 .desc = {
3929 {
3930 .name = "backend",
3931 .type = QEMU_OPT_STRING,
3932 },{
3933 .name = "path",
3934 .type = QEMU_OPT_STRING,
3935 },{
3936 .name = "host",
3937 .type = QEMU_OPT_STRING,
3938 },{
3939 .name = "port",
3940 .type = QEMU_OPT_STRING,
3941 },{
3942 .name = "localaddr",
3943 .type = QEMU_OPT_STRING,
3944 },{
3945 .name = "localport",
3946 .type = QEMU_OPT_STRING,
3947 },{
3948 .name = "to",
3949 .type = QEMU_OPT_NUMBER,
3950 },{
3951 .name = "ipv4",
3952 .type = QEMU_OPT_BOOL,
3953 },{
3954 .name = "ipv6",
3955 .type = QEMU_OPT_BOOL,
3956 },{
3957 .name = "wait",
3958 .type = QEMU_OPT_BOOL,
3959 },{
3960 .name = "server",
3961 .type = QEMU_OPT_BOOL,
3962 },{
3963 .name = "delay",
3964 .type = QEMU_OPT_BOOL,
3965 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05003966 .name = "reconnect",
3967 .type = QEMU_OPT_NUMBER,
3968 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003969 .name = "telnet",
3970 .type = QEMU_OPT_BOOL,
3971 },{
3972 .name = "width",
3973 .type = QEMU_OPT_NUMBER,
3974 },{
3975 .name = "height",
3976 .type = QEMU_OPT_NUMBER,
3977 },{
3978 .name = "cols",
3979 .type = QEMU_OPT_NUMBER,
3980 },{
3981 .name = "rows",
3982 .type = QEMU_OPT_NUMBER,
3983 },{
3984 .name = "mux",
3985 .type = QEMU_OPT_BOOL,
3986 },{
3987 .name = "signal",
3988 .type = QEMU_OPT_BOOL,
3989 },{
3990 .name = "name",
3991 .type = QEMU_OPT_STRING,
3992 },{
3993 .name = "debug",
3994 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08003995 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01003996 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01003997 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003998 },{
3999 .name = "chardev",
4000 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004001 },
4002 { /* end of list */ }
4003 },
4004};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004005
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004006#ifdef _WIN32
4007
4008static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4009{
4010 HANDLE out;
4011
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004012 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004013 error_setg(errp, "input file not supported");
4014 return NULL;
4015 }
4016
4017 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4018 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4019 if (out == INVALID_HANDLE_VALUE) {
4020 error_setg(errp, "open %s failed", file->out);
4021 return NULL;
4022 }
4023 return qemu_chr_open_win_file(out);
4024}
4025
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004026static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4027 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004028{
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004029 return qemu_chr_open_win_path(serial->device);
4030}
4031
4032static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4033 Error **errp)
4034{
4035 error_setg(errp, "character device backend type 'parallel' not supported");
4036 return NULL;
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004037}
4038
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004039#else /* WIN32 */
4040
4041static int qmp_chardev_open_file_source(char *src, int flags,
4042 Error **errp)
4043{
4044 int fd = -1;
4045
4046 TFR(fd = qemu_open(src, flags, 0666));
4047 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004048 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004049 }
4050 return fd;
4051}
4052
4053static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
4054{
Markus Armbruster5f758362014-05-19 18:57:34 +02004055 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004056
4057 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4058 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004059 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004060 return NULL;
4061 }
4062
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004063 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004064 flags = O_RDONLY;
4065 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004066 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004067 qemu_close(out);
4068 return NULL;
4069 }
4070 }
4071
4072 return qemu_chr_open_fd(in, out);
4073}
4074
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004075static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
4076 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004077{
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004078#ifdef HAVE_CHARDEV_TTY
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004079 int fd;
4080
4081 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004082 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004083 return NULL;
4084 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004085 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004086 return qemu_chr_open_tty_fd(fd);
4087#else
4088 error_setg(errp, "character device backend type 'serial' not supported");
4089 return NULL;
4090#endif
4091}
4092
4093static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
4094 Error **errp)
4095{
4096#ifdef HAVE_CHARDEV_PARPORT
4097 int fd;
4098
4099 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004100 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004101 return NULL;
4102 }
4103 return qemu_chr_open_pp_fd(fd);
4104#else
4105 error_setg(errp, "character device backend type 'parallel' not supported");
4106 return NULL;
4107#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004108}
4109
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004110#endif /* WIN32 */
4111
Corey Minyard5008e5b2014-10-08 07:11:55 -05004112static void socket_try_connect(CharDriverState *chr)
4113{
4114 Error *err = NULL;
4115
4116 if (!qemu_chr_open_socket_fd(chr, &err)) {
4117 check_report_connect_error(chr, err);
4118 }
4119}
4120
Corey Minyard5dd1f022014-10-02 11:17:37 -05004121static gboolean socket_reconnect_timeout(gpointer opaque)
4122{
4123 CharDriverState *chr = opaque;
4124 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004125
4126 s->reconnect_timer = 0;
4127
4128 if (chr->be_open) {
4129 return false;
4130 }
4131
Corey Minyard5008e5b2014-10-08 07:11:55 -05004132 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004133
4134 return false;
4135}
4136
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004137static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
4138 Error **errp)
4139{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004140 CharDriverState *chr;
4141 TCPCharDriver *s;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004142 SocketAddress *addr = sock->addr;
4143 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4144 bool is_listen = sock->has_server ? sock->server : true;
4145 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4146 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004147 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004148
4149 chr = qemu_chr_alloc();
4150 s = g_malloc0(sizeof(TCPCharDriver));
4151
4152 s->fd = -1;
4153 s->listen_fd = -1;
4154 s->is_unix = addr->kind == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004155 s->is_listen = is_listen;
4156 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004157 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004158 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004159
4160 chr->opaque = s;
4161 chr->chr_write = tcp_chr_write;
4162 chr->chr_sync_read = tcp_chr_sync_read;
4163 chr->chr_close = tcp_chr_close;
4164 chr->get_msgfds = tcp_get_msgfds;
4165 chr->set_msgfds = tcp_set_msgfds;
4166 chr->chr_add_client = tcp_chr_add_client;
4167 chr->chr_add_watch = tcp_chr_add_watch;
4168 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4169 /* be isn't opened until we get a connection */
4170 chr->explicit_be_open = true;
4171
4172 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004173 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4174 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004175
4176 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004177 if (is_telnet) {
4178 s->do_telnetopt = 1;
4179 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004180 } else if (reconnect > 0) {
4181 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004182 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004183
Corey Minyard5008e5b2014-10-08 07:11:55 -05004184 if (s->reconnect_time) {
4185 socket_try_connect(chr);
4186 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4187 g_free(s);
4188 g_free(chr->filename);
4189 g_free(chr);
4190 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004191 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004192
4193 if (is_listen && is_waitconnect) {
4194 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4195 chr->filename);
4196 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4197 qemu_set_nonblock(s->listen_fd);
4198 }
4199
4200 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004201}
4202
Lei Li08d0ab32013-05-20 14:51:03 +08004203static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
4204 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004205{
4206 int fd;
4207
Lei Li08d0ab32013-05-20 14:51:03 +08004208 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004209 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004210 return NULL;
4211 }
4212 return qemu_chr_open_udp_fd(fd);
4213}
4214
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004215ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4216 Error **errp)
4217{
4218 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004219 CharDriverState *base, *chr = NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004220
4221 chr = qemu_chr_find(id);
4222 if (chr) {
4223 error_setg(errp, "Chardev '%s' already exists", id);
4224 g_free(ret);
4225 return NULL;
4226 }
4227
4228 switch (backend->kind) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004229 case CHARDEV_BACKEND_KIND_FILE:
4230 chr = qmp_chardev_open_file(backend->file, errp);
4231 break;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004232 case CHARDEV_BACKEND_KIND_SERIAL:
4233 chr = qmp_chardev_open_serial(backend->serial, errp);
4234 break;
4235 case CHARDEV_BACKEND_KIND_PARALLEL:
4236 chr = qmp_chardev_open_parallel(backend->parallel, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004237 break;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01004238 case CHARDEV_BACKEND_KIND_PIPE:
4239 chr = qemu_chr_open_pipe(backend->pipe);
4240 break;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004241 case CHARDEV_BACKEND_KIND_SOCKET:
4242 chr = qmp_chardev_open_socket(backend->socket, errp);
4243 break;
Lei Li08d0ab32013-05-20 14:51:03 +08004244 case CHARDEV_BACKEND_KIND_UDP:
4245 chr = qmp_chardev_open_udp(backend->udp, errp);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004246 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004247#ifdef HAVE_CHARDEV_TTY
4248 case CHARDEV_BACKEND_KIND_PTY:
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01004249 chr = qemu_chr_open_pty(id, ret);
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004250 break;
Gerd Hoffmann0a1a7fa2012-12-20 14:39:13 +01004251#endif
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004252 case CHARDEV_BACKEND_KIND_NULL:
Gerd Hoffmann80dca9e2013-02-21 11:41:26 +01004253 chr = qemu_chr_open_null();
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004254 break;
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004255 case CHARDEV_BACKEND_KIND_MUX:
4256 base = qemu_chr_find(backend->mux->chardev);
4257 if (base == NULL) {
4258 error_setg(errp, "mux: base chardev %s not found",
4259 backend->mux->chardev);
4260 break;
4261 }
4262 chr = qemu_chr_open_mux(base);
4263 break;
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +01004264 case CHARDEV_BACKEND_KIND_MSMOUSE:
4265 chr = qemu_chr_open_msmouse();
4266 break;
Gerd Hoffmann2d572862013-02-21 12:56:10 +01004267#ifdef CONFIG_BRLAPI
4268 case CHARDEV_BACKEND_KIND_BRAILLE:
4269 chr = chr_baum_init();
4270 break;
4271#endif
Paolo Bonzini56923992014-07-11 09:44:26 +02004272 case CHARDEV_BACKEND_KIND_TESTDEV:
4273 chr = chr_testdev_init();
4274 break;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01004275 case CHARDEV_BACKEND_KIND_STDIO:
4276 chr = qemu_chr_open_stdio(backend->stdio);
4277 break;
Gerd Hoffmannd9ac3742013-02-25 11:48:06 +01004278#ifdef _WIN32
4279 case CHARDEV_BACKEND_KIND_CONSOLE:
4280 chr = qemu_chr_open_win_con();
4281 break;
4282#endif
Gerd Hoffmanncd153e22013-02-25 12:39:06 +01004283#ifdef CONFIG_SPICE
4284 case CHARDEV_BACKEND_KIND_SPICEVMC:
4285 chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
4286 break;
4287 case CHARDEV_BACKEND_KIND_SPICEPORT:
4288 chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
4289 break;
4290#endif
Gerd Hoffmann702ec692013-02-25 15:52:32 +01004291 case CHARDEV_BACKEND_KIND_VC:
4292 chr = vc_init(backend->vc);
4293 break;
Markus Armbruster3a1da422013-07-26 16:44:34 +02004294 case CHARDEV_BACKEND_KIND_RINGBUF:
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004295 case CHARDEV_BACKEND_KIND_MEMORY:
Markus Armbruster3a1da422013-07-26 16:44:34 +02004296 chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01004297 break;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004298 default:
4299 error_setg(errp, "unknown chardev backend (%d)", backend->kind);
4300 break;
4301 }
4302
Markus Armbruster3894c782014-05-19 18:57:36 +02004303 /*
4304 * Character backend open hasn't been fully converted to the Error
4305 * API. Some opens fail without setting an error. Set a generic
4306 * error then.
4307 * TODO full conversion to Error API
4308 */
4309 if (chr == NULL && errp && !*errp) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004310 error_setg(errp, "Failed to create chardev");
4311 }
4312 if (chr) {
4313 chr->label = g_strdup(id);
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004314 chr->avail_connections =
4315 (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Gerd Hoffmann60d95382013-05-27 12:41:24 +02004316 if (!chr->filename) {
4317 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
4318 }
Michael Rothbd5c51e2013-06-07 15:19:53 -05004319 if (!chr->explicit_be_open) {
4320 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4321 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004322 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4323 return ret;
4324 } else {
4325 g_free(ret);
4326 return NULL;
4327 }
4328}
4329
4330void qmp_chardev_remove(const char *id, Error **errp)
4331{
4332 CharDriverState *chr;
4333
4334 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004335 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004336 error_setg(errp, "Chardev '%s' not found", id);
4337 return;
4338 }
4339 if (chr->chr_can_read || chr->chr_read ||
4340 chr->chr_event || chr->handler_opaque) {
4341 error_setg(errp, "Chardev '%s' is busy", id);
4342 return;
4343 }
4344 qemu_chr_delete(chr);
4345}
Anthony Liguorid654f342013-03-05 23:21:28 +05304346
4347static void register_types(void)
4348{
Peter Maydelle4d50d42014-09-02 11:24:17 +01004349 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL);
4350 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
4351 qemu_chr_parse_socket);
4352 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp);
4353 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
4354 qemu_chr_parse_ringbuf);
4355 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
4356 qemu_chr_parse_file_out);
4357 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
4358 qemu_chr_parse_stdio);
4359 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
4360 qemu_chr_parse_serial);
4361 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
4362 qemu_chr_parse_serial);
4363 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
4364 qemu_chr_parse_parallel);
4365 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
4366 qemu_chr_parse_parallel);
4367 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
4368 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
4369 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
4370 qemu_chr_parse_pipe);
4371 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004372 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004373 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
4374 qemu_chr_parse_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004375 /* this must be done after machine init, since we register FEs with muxes
4376 * as part of realize functions like serial_isa_realizefn when -nographic
4377 * is specified
4378 */
4379 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304380}
4381
4382type_init(register_types);