blob: 5448b0f30b74d35f4b653d57cf15827735084c11 [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010027#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
aurel32cf3ebac2008-11-01 00:53:09 +000030#include "hw/usb.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
Corey Minyardcfb429c2014-10-02 11:17:35 -050032#include "qapi/qmp-input-visitor.h"
33#include "qapi/qmp-output-visitor.h"
34#include "qapi-visit.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
36#include <unistd.h>
37#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000038#include <time.h>
39#include <errno.h>
40#include <sys/time.h>
41#include <zlib.h>
42
43#ifndef _WIN32
44#include <sys/times.h>
45#include <sys/wait.h>
46#include <termios.h>
47#include <sys/mman.h>
48#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000049#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000050#include <sys/socket.h>
51#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000052#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000054#include <netdb.h>
55#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020056#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000057#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040058#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
59#include <dev/ppbus/ppi.h>
60#include <dev/ppbus/ppbconf.h>
61#elif defined(__DragonFly__)
62#include <dev/misc/ppi/ppi.h>
63#include <bus/ppbus/ppbconf.h>
64#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010065#else
aliguori6f97dba2008-10-31 18:49:55 +000066#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000067#include <linux/ppdev.h>
68#include <linux/parport.h>
69#endif
70#ifdef __sun__
71#include <sys/stat.h>
72#include <sys/ethernet.h>
73#include <sys/sockio.h>
74#include <netinet/arp.h>
75#include <netinet/in.h>
76#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#include <netinet/ip_icmp.h> // must come after ip.h
79#include <netinet/udp.h>
80#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000081#endif
82#endif
83#endif
84
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010085#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020086#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000087
Amit Shah9bd78542009-11-03 19:59:54 +053088#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030089#define READ_RETRIES 10
Corey Minyard9f781162014-10-02 11:17:33 -050090#define CHR_MAX_FILENAME_SIZE 256
Michael S. Tsirkinc4095722014-11-02 18:48:32 +020091#define TCP_MAX_FDS 16
Amit Shah9bd78542009-11-03 19:59:54 +053092
aliguori6f97dba2008-10-31 18:49:55 +000093/***********************************************************/
Corey Minyardcfb429c2014-10-02 11:17:35 -050094/* Socket address helpers */
Corey Minyardcfb429c2014-10-02 11:17:35 -050095
Corey Minyard16cc4ff2014-10-02 11:17:36 -050096static int SocketAddress_to_str(char *dest, int max_len,
97 const char *prefix, SocketAddress *addr,
98 bool is_listen, bool is_telnet)
99{
Eric Blake130257d2015-10-26 16:34:57 -0600100 switch (addr->type) {
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500101 case SOCKET_ADDRESS_KIND_INET:
102 return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix,
Eric Blake130257d2015-10-26 16:34:57 -0600103 is_telnet ? "telnet" : "tcp", addr->u.inet->host,
104 addr->u.inet->port, is_listen ? ",server" : "");
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500105 break;
106 case SOCKET_ADDRESS_KIND_UNIX:
107 return snprintf(dest, max_len, "%sunix:%s%s", prefix,
Eric Blake130257d2015-10-26 16:34:57 -0600108 addr->u.q_unix->path, is_listen ? ",server" : "");
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500109 break;
110 case SOCKET_ADDRESS_KIND_FD:
Eric Blake130257d2015-10-26 16:34:57 -0600111 return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->u.fd->str,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500112 is_listen ? ",server" : "");
113 break;
114 default:
115 abort();
116 }
117}
118
119static int sockaddr_to_str(char *dest, int max_len,
120 struct sockaddr_storage *ss, socklen_t ss_len,
Corey Minyard01ca5192014-10-02 11:17:38 -0500121 struct sockaddr_storage *ps, socklen_t ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500122 bool is_listen, bool is_telnet)
123{
Corey Minyard01ca5192014-10-02 11:17:38 -0500124 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
125 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500126 const char *left = "", *right = "";
127
128 switch (ss->ss_family) {
129#ifndef _WIN32
130 case AF_UNIX:
131 return snprintf(dest, max_len, "unix:%s%s",
132 ((struct sockaddr_un *)(ss))->sun_path,
133 is_listen ? ",server" : "");
134#endif
135 case AF_INET6:
136 left = "[";
137 right = "]";
138 /* fall through */
139 case AF_INET:
Corey Minyard01ca5192014-10-02 11:17:38 -0500140 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
141 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
142 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
143 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
144 return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500145 is_telnet ? "telnet" : "tcp",
Corey Minyard01ca5192014-10-02 11:17:38 -0500146 left, shost, right, sserv,
147 is_listen ? ",server" : "",
148 left, phost, right, pserv);
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500149
150 default:
151 return snprintf(dest, max_len, "unknown");
152 }
153}
154
Corey Minyardcfb429c2014-10-02 11:17:35 -0500155/***********************************************************/
aliguori6f97dba2008-10-31 18:49:55 +0000156/* character device */
157
Blue Swirl72cf2d42009-09-12 07:36:22 +0000158static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
159 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000160
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200161CharDriverState *qemu_chr_alloc(void)
162{
163 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +0200164 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200165 return chr;
166}
167
Hans de Goedea425d232011-11-19 10:22:43 +0100168void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000169{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200170 /* Keep track if the char device is open */
171 switch (event) {
172 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100173 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200174 break;
175 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100176 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200177 break;
178 }
179
aliguori6f97dba2008-10-31 18:49:55 +0000180 if (!s->chr_event)
181 return;
182 s->chr_event(s->handler_opaque, event);
183}
184
Hans de Goedefee204f2013-03-26 11:07:54 +0100185void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000186{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500187 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000188}
189
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500190int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000191{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200192 int ret;
193
194 qemu_mutex_lock(&s->chr_write_lock);
195 ret = s->chr_write(s, buf, len);
196 qemu_mutex_unlock(&s->chr_write_lock);
197 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000198}
199
Anthony Liguoricd187202013-03-26 10:04:17 -0500200int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
201{
202 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200203 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500204
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200205 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500206 while (offset < len) {
207 do {
208 res = s->chr_write(s, buf + offset, len - offset);
209 if (res == -1 && errno == EAGAIN) {
210 g_usleep(100);
211 }
212 } while (res == -1 && errno == EAGAIN);
213
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200214 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500215 break;
216 }
217
Anthony Liguoricd187202013-03-26 10:04:17 -0500218 offset += res;
219 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200220 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500221
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200222 if (res < 0) {
223 return res;
224 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500225 return offset;
226}
227
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300228int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
229{
230 int offset = 0, counter = 10;
231 int res;
232
233 if (!s->chr_sync_read) {
234 return 0;
235 }
236
237 while (offset < len) {
238 do {
239 res = s->chr_sync_read(s, buf + offset, len - offset);
240 if (res == -1 && errno == EAGAIN) {
241 g_usleep(100);
242 }
243 } while (res == -1 && errno == EAGAIN);
244
245 if (res == 0) {
246 break;
247 }
248
249 if (res < 0) {
250 return res;
251 }
252
253 offset += res;
254
255 if (!counter--) {
256 break;
257 }
258 }
259
260 return offset;
261}
262
Anthony Liguori41084f12011-08-15 11:17:34 -0500263int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000264{
265 if (!s->chr_ioctl)
266 return -ENOTSUP;
267 return s->chr_ioctl(s, cmd, arg);
268}
269
Anthony Liguori909cda12011-08-15 11:17:31 -0500270int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000271{
272 if (!s->chr_can_read)
273 return 0;
274 return s->chr_can_read(s->handler_opaque);
275}
276
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500277void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000278{
Stefan Weilac310732012-04-19 22:27:14 +0200279 if (s->chr_read) {
280 s->chr_read(s->handler_opaque, buf, len);
281 }
aliguori6f97dba2008-10-31 18:49:55 +0000282}
283
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500284int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100285{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300286 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800287 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300288}
289
290int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
291{
292 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100293}
294
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300295int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
296{
297 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
298}
299
Daniel P. Berrange13661082011-06-23 13:31:42 +0100300int qemu_chr_add_client(CharDriverState *s, int fd)
301{
302 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
303}
304
aliguori6f97dba2008-10-31 18:49:55 +0000305void qemu_chr_accept_input(CharDriverState *s)
306{
307 if (s->chr_accept_input)
308 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100309 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000310}
311
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500312void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000313{
Amit Shah9bd78542009-11-03 19:59:54 +0530314 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000315 va_list ap;
316 va_start(ap, fmt);
317 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500318 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000319 va_end(ap);
320}
321
Amit Shah386a5a12013-08-28 15:24:05 +0530322static void remove_fd_in_watch(CharDriverState *chr);
323
aliguori6f97dba2008-10-31 18:49:55 +0000324void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100325 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000326 IOReadHandler *fd_read,
327 IOEventHandler *fd_event,
328 void *opaque)
329{
Hans de Goede19083222013-03-26 11:07:56 +0100330 int fe_open;
331
Amit Shahda7d9982011-04-25 15:18:22 +0530332 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100333 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530334 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100335 } else {
336 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530337 }
aliguori6f97dba2008-10-31 18:49:55 +0000338 s->chr_can_read = fd_can_read;
339 s->chr_read = fd_read;
340 s->chr_event = fd_event;
341 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200342 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000343 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200344
Hans de Goede19083222013-03-26 11:07:56 +0100345 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100346 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100347 }
348
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200349 /* We're connecting to an already opened device, so let's make sure we
350 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100351 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100352 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200353 }
aliguori6f97dba2008-10-31 18:49:55 +0000354}
355
356static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
357{
358 return len;
359}
360
Paolo Bonzini0d649922015-09-29 15:24:29 +0200361static CharDriverState *qemu_chr_open_null(const char *id,
362 ChardevBackend *backend,
363 ChardevReturn *ret,
364 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000365{
366 CharDriverState *chr;
367
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200368 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000369 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500370 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100371 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000372}
373
374/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000375#define MAX_MUX 4
376#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
377#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
378typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100379 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000380 IOReadHandler *chr_read[MAX_MUX];
381 IOEventHandler *chr_event[MAX_MUX];
382 void *ext_opaque[MAX_MUX];
383 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200384 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000385 int mux_cnt;
386 int term_got_escape;
387 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000388 /* Intermediate input buffer allows to catch escape sequences even if the
389 currently active device is not accepting any input - but only until it
390 is full as well. */
391 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
392 int prod[MAX_MUX];
393 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200394 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200395
396 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200397 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200398 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000399} MuxDriver;
400
401
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200402/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000403static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
404{
405 MuxDriver *d = chr->opaque;
406 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200407 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200408 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000409 } else {
410 int i;
411
412 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200413 for (i = 0; i < len; i++) {
414 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000415 char buf1[64];
416 int64_t ti;
417 int secs;
418
Alex Blighbc72ad62013-08-21 16:03:08 +0100419 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200420 if (d->timestamps_start == -1)
421 d->timestamps_start = ti;
422 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000423 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000424 snprintf(buf1, sizeof(buf1),
425 "[%02d:%02d:%02d.%03d] ",
426 secs / 3600,
427 (secs / 60) % 60,
428 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000429 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200430 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200431 d->linestart = 0;
432 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200433 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200434 if (buf[i] == '\n') {
435 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000436 }
437 }
438 }
439 return ret;
440}
441
442static const char * const mux_help[] = {
443 "% h print this help\n\r",
444 "% x exit emulator\n\r",
445 "% s save disk data back to file (if -snapshot)\n\r",
Gonglei86d10322014-11-15 18:06:46 +0800446 "% t toggle console timestamps\n\r",
aliguori6f97dba2008-10-31 18:49:55 +0000447 "% b send break (magic sysrq)\n\r",
448 "% c switch between console and monitor\n\r",
449 "% % sends %\n\r",
450 NULL
451};
452
453int term_escape_char = 0x01; /* ctrl-a is used for escape */
454static void mux_print_help(CharDriverState *chr)
455{
456 int i, j;
457 char ebuf[15] = "Escape-Char";
458 char cbuf[50] = "\n\r";
459
460 if (term_escape_char > 0 && term_escape_char < 26) {
461 snprintf(cbuf, sizeof(cbuf), "\n\r");
462 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
463 } else {
464 snprintf(cbuf, sizeof(cbuf),
465 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
466 term_escape_char);
467 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200468 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000469 for (i = 0; mux_help[i] != NULL; i++) {
470 for (j=0; mux_help[i][j] != '\0'; j++) {
471 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200472 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000473 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200474 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000475 }
476 }
477}
478
aliguori2724b182009-03-05 23:01:47 +0000479static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
480{
481 if (d->chr_event[mux_nr])
482 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
483}
484
aliguori6f97dba2008-10-31 18:49:55 +0000485static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
486{
487 if (d->term_got_escape) {
488 d->term_got_escape = 0;
489 if (ch == term_escape_char)
490 goto send_char;
491 switch(ch) {
492 case '?':
493 case 'h':
494 mux_print_help(chr);
495 break;
496 case 'x':
497 {
498 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200499 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000500 exit(0);
501 break;
502 }
503 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200504 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000505 break;
506 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100507 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000508 break;
509 case 'c':
510 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200511 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
512 d->focus++;
513 if (d->focus >= d->mux_cnt)
514 d->focus = 0;
515 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000516 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200517 case 't':
518 d->timestamps = !d->timestamps;
519 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200520 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200521 break;
aliguori6f97dba2008-10-31 18:49:55 +0000522 }
523 } else if (ch == term_escape_char) {
524 d->term_got_escape = 1;
525 } else {
526 send_char:
527 return 1;
528 }
529 return 0;
530}
531
532static void mux_chr_accept_input(CharDriverState *chr)
533{
aliguori6f97dba2008-10-31 18:49:55 +0000534 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200535 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000536
aliguoria80bf992009-03-05 23:00:02 +0000537 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000538 d->chr_can_read[m] &&
539 d->chr_can_read[m](d->ext_opaque[m])) {
540 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000541 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000542 }
543}
544
545static int mux_chr_can_read(void *opaque)
546{
547 CharDriverState *chr = opaque;
548 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200549 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000550
aliguoria80bf992009-03-05 23:00:02 +0000551 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000552 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000553 if (d->chr_can_read[m])
554 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000555 return 0;
556}
557
558static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
559{
560 CharDriverState *chr = opaque;
561 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200562 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000563 int i;
564
565 mux_chr_accept_input (opaque);
566
567 for(i = 0; i < size; i++)
568 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000569 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000570 d->chr_can_read[m] &&
571 d->chr_can_read[m](d->ext_opaque[m]))
572 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
573 else
aliguoria80bf992009-03-05 23:00:02 +0000574 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000575 }
576}
577
578static void mux_chr_event(void *opaque, int event)
579{
580 CharDriverState *chr = opaque;
581 MuxDriver *d = chr->opaque;
582 int i;
583
584 /* Send the event to all registered listeners */
585 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000586 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000587}
588
589static void mux_chr_update_read_handler(CharDriverState *chr)
590{
591 MuxDriver *d = chr->opaque;
592
593 if (d->mux_cnt >= MAX_MUX) {
594 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
595 return;
596 }
597 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
598 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
599 d->chr_read[d->mux_cnt] = chr->chr_read;
600 d->chr_event[d->mux_cnt] = chr->chr_event;
601 /* Fix up the real driver with mux routines */
602 if (d->mux_cnt == 0) {
603 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
604 mux_chr_event, chr);
605 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200606 if (d->focus != -1) {
607 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200608 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200609 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000610 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200611 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000612}
613
Michael Roth7b7ab182013-07-30 13:04:22 -0500614static bool muxes_realized;
615
616/**
617 * Called after processing of default and command-line-specified
618 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
619 * to a mux chardev. This is done here to ensure that
620 * output/prompts/banners are only displayed for the FE that has
621 * focus when initial command-line processing/machine init is
622 * completed.
623 *
624 * After this point, any new FE attached to any new or existing
625 * mux will receive CHR_EVENT_OPENED notifications for the BE
626 * immediately.
627 */
628static void muxes_realize_done(Notifier *notifier, void *unused)
629{
630 CharDriverState *chr;
631
632 QTAILQ_FOREACH(chr, &chardevs, next) {
633 if (chr->is_mux) {
634 MuxDriver *d = chr->opaque;
635 int i;
636
637 /* send OPENED to all already-attached FEs */
638 for (i = 0; i < d->mux_cnt; i++) {
639 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
640 }
641 /* mark mux as OPENED so any new FEs will immediately receive
642 * OPENED event
643 */
644 qemu_chr_be_generic_open(chr);
645 }
646 }
647 muxes_realized = true;
648}
649
650static Notifier muxes_realize_notify = {
651 .notify = muxes_realize_done,
652};
653
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400654static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
655{
656 MuxDriver *d = s->opaque;
657 return d->drv->chr_add_watch(d->drv, cond);
658}
659
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200660static CharDriverState *qemu_chr_open_mux(const char *id,
661 ChardevBackend *backend,
662 ChardevReturn *ret, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000663{
Eric Blake130257d2015-10-26 16:34:57 -0600664 ChardevMux *mux = backend->u.mux;
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200665 CharDriverState *chr, *drv;
aliguori6f97dba2008-10-31 18:49:55 +0000666 MuxDriver *d;
667
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200668 drv = qemu_chr_find(mux->chardev);
669 if (drv == NULL) {
670 error_setg(errp, "mux: base chardev %s not found", mux->chardev);
671 return NULL;
672 }
673
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200674 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +0200675 d = g_new0(MuxDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000676
677 chr->opaque = d;
678 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200679 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000680 chr->chr_write = mux_chr_write;
681 chr->chr_update_read_handler = mux_chr_update_read_handler;
682 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100683 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100684 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400685 if (drv->chr_add_watch) {
686 chr->chr_add_watch = mux_chr_add_watch;
687 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500688 /* only default to opened state if we've realized the initial
689 * set of muxes
690 */
691 chr->explicit_be_open = muxes_realized ? 0 : 1;
692 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200693
aliguori6f97dba2008-10-31 18:49:55 +0000694 return chr;
695}
696
697
698#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000699int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000700{
701 int ret, len;
702
703 len = len1;
704 while (len > 0) {
705 ret = send(fd, buf, len, 0);
706 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000707 errno = WSAGetLastError();
708 if (errno != WSAEWOULDBLOCK) {
709 return -1;
710 }
711 } else if (ret == 0) {
712 break;
713 } else {
714 buf += ret;
715 len -= ret;
716 }
717 }
718 return len1 - len;
719}
720
721#else
722
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100723int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000724{
725 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100726 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000727
728 len = len1;
729 while (len > 0) {
730 ret = write(fd, buf, len);
731 if (ret < 0) {
732 if (errno != EINTR && errno != EAGAIN)
733 return -1;
734 } else if (ret == 0) {
735 break;
736 } else {
737 buf += ret;
738 len -= ret;
739 }
740 }
741 return len1 - len;
742}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500743
744int recv_all(int fd, void *_buf, int len1, bool single_read)
745{
746 int ret, len;
747 uint8_t *buf = _buf;
748
749 len = len1;
750 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
751 if (ret < 0) {
752 if (errno != EINTR && errno != EAGAIN) {
753 return -1;
754 }
755 continue;
756 } else {
757 if (single_read) {
758 return ret;
759 }
760 buf += ret;
761 len -= ret;
762 }
763 }
764 return len1 - len;
765}
766
aliguori6f97dba2008-10-31 18:49:55 +0000767#endif /* !_WIN32 */
768
Anthony Liguori96c63842013-03-05 23:21:18 +0530769typedef struct IOWatchPoll
770{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200771 GSource parent;
772
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200773 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530774 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530775
776 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200777 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530778 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530779} IOWatchPoll;
780
Anthony Liguori96c63842013-03-05 23:21:18 +0530781static IOWatchPoll *io_watch_poll_from_source(GSource *source)
782{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200783 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530784}
785
786static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
787{
788 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200789 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200790 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200791 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530792 return FALSE;
793 }
794
Paolo Bonzinid185c092013-04-05 17:59:33 +0200795 if (now_active) {
Nils Carlson4bf1cb02015-07-19 20:39:56 +0000796 iwp->src = g_io_create_watch(iwp->channel,
797 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200798 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200799 g_source_attach(iwp->src, NULL);
800 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200801 g_source_destroy(iwp->src);
802 g_source_unref(iwp->src);
803 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200804 }
805 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530806}
807
808static gboolean io_watch_poll_check(GSource *source)
809{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200810 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530811}
812
813static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
814 gpointer user_data)
815{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200816 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530817}
818
819static void io_watch_poll_finalize(GSource *source)
820{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200821 /* Due to a glib bug, removing the last reference to a source
822 * inside a finalize callback causes recursive locking (and a
823 * deadlock). This is not a problem inside other callbacks,
824 * including dispatch callbacks, so we call io_remove_watch_poll
825 * to remove this source. At this point, iwp->src must
826 * be NULL, or we would leak it.
827 *
828 * This would be solved much more elegantly by child sources,
829 * but we support older glib versions that do not have them.
830 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530831 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200832 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530833}
834
835static GSourceFuncs io_watch_poll_funcs = {
836 .prepare = io_watch_poll_prepare,
837 .check = io_watch_poll_check,
838 .dispatch = io_watch_poll_dispatch,
839 .finalize = io_watch_poll_finalize,
840};
841
842/* Can only be used for read */
843static guint io_add_watch_poll(GIOChannel *channel,
844 IOCanReadHandler *fd_can_read,
845 GIOFunc fd_read,
846 gpointer user_data)
847{
848 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200849 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530850
Paolo Bonzinid185c092013-04-05 17:59:33 +0200851 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530852 iwp->fd_can_read = fd_can_read;
853 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200854 iwp->channel = channel;
855 iwp->fd_read = (GSourceFunc) fd_read;
856 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530857
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200858 tag = g_source_attach(&iwp->parent, NULL);
859 g_source_unref(&iwp->parent);
860 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530861}
862
Paolo Bonzini2b316772013-04-19 17:32:09 +0200863static void io_remove_watch_poll(guint tag)
864{
865 GSource *source;
866 IOWatchPoll *iwp;
867
868 g_return_if_fail (tag > 0);
869
870 source = g_main_context_find_source_by_id(NULL, tag);
871 g_return_if_fail (source != NULL);
872
873 iwp = io_watch_poll_from_source(source);
874 if (iwp->src) {
875 g_source_destroy(iwp->src);
876 g_source_unref(iwp->src);
877 iwp->src = NULL;
878 }
879 g_source_destroy(&iwp->parent);
880}
881
Amit Shah26da70c2013-08-28 15:23:37 +0530882static void remove_fd_in_watch(CharDriverState *chr)
883{
884 if (chr->fd_in_tag) {
885 io_remove_watch_poll(chr->fd_in_tag);
886 chr->fd_in_tag = 0;
887 }
888}
889
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000890#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530891static GIOChannel *io_channel_from_fd(int fd)
892{
893 GIOChannel *chan;
894
895 if (fd == -1) {
896 return NULL;
897 }
898
899 chan = g_io_channel_unix_new(fd);
900
901 g_io_channel_set_encoding(chan, NULL, NULL);
902 g_io_channel_set_buffered(chan, FALSE);
903
904 return chan;
905}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000906#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530907
Anthony Liguori76a96442013-03-05 23:21:21 +0530908static GIOChannel *io_channel_from_socket(int fd)
909{
910 GIOChannel *chan;
911
912 if (fd == -1) {
913 return NULL;
914 }
915
916#ifdef _WIN32
917 chan = g_io_channel_win32_new_socket(fd);
918#else
919 chan = g_io_channel_unix_new(fd);
920#endif
921
922 g_io_channel_set_encoding(chan, NULL, NULL);
923 g_io_channel_set_buffered(chan, FALSE);
924
925 return chan;
926}
927
Anthony Liguori684a0962013-03-29 11:39:50 -0500928static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530929{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200930 size_t offset = 0;
931 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530932
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200933 while (offset < len && status == G_IO_STATUS_NORMAL) {
934 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500935
936 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530937 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500938 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530939 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500940
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200941 if (offset > 0) {
942 return offset;
943 }
944 switch (status) {
945 case G_IO_STATUS_NORMAL:
946 g_assert(len == 0);
947 return 0;
948 case G_IO_STATUS_AGAIN:
949 errno = EAGAIN;
950 return -1;
951 default:
952 break;
953 }
954 errno = EINVAL;
955 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530956}
Anthony Liguori96c63842013-03-05 23:21:18 +0530957
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000958#ifndef _WIN32
959
Anthony Liguoria29753f2013-03-05 23:21:19 +0530960typedef struct FDCharDriver {
961 CharDriverState *chr;
962 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000963 int max_size;
964} FDCharDriver;
965
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200966/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000967static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
968{
969 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530970
Anthony Liguori684a0962013-03-29 11:39:50 -0500971 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530972}
973
974static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
975{
976 CharDriverState *chr = opaque;
977 FDCharDriver *s = chr->opaque;
978 int len;
979 uint8_t buf[READ_BUF_LEN];
980 GIOStatus status;
981 gsize bytes_read;
982
983 len = sizeof(buf);
984 if (len > s->max_size) {
985 len = s->max_size;
986 }
987 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200988 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530989 }
990
991 status = g_io_channel_read_chars(chan, (gchar *)buf,
992 len, &bytes_read, NULL);
993 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530994 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530995 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
996 return FALSE;
997 }
998 if (status == G_IO_STATUS_NORMAL) {
999 qemu_chr_be_write(chr, buf, bytes_read);
1000 }
1001
1002 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001003}
1004
1005static int fd_chr_read_poll(void *opaque)
1006{
1007 CharDriverState *chr = opaque;
1008 FDCharDriver *s = chr->opaque;
1009
Anthony Liguori909cda12011-08-15 11:17:31 -05001010 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001011 return s->max_size;
1012}
1013
Anthony Liguori23673ca2013-03-05 23:21:23 +05301014static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1015{
1016 FDCharDriver *s = chr->opaque;
1017 return g_io_create_watch(s->fd_out, cond);
1018}
1019
aliguori6f97dba2008-10-31 18:49:55 +00001020static void fd_chr_update_read_handler(CharDriverState *chr)
1021{
1022 FDCharDriver *s = chr->opaque;
1023
Amit Shah26da70c2013-08-28 15:23:37 +05301024 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301025 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301026 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1027 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001028 }
1029}
1030
1031static void fd_chr_close(struct CharDriverState *chr)
1032{
1033 FDCharDriver *s = chr->opaque;
1034
Amit Shah26da70c2013-08-28 15:23:37 +05301035 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301036 if (s->fd_in) {
1037 g_io_channel_unref(s->fd_in);
1038 }
1039 if (s->fd_out) {
1040 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001041 }
1042
Anthony Liguori7267c092011-08-20 22:09:37 -05001043 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001044 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001045}
1046
1047/* open a character device to a unix fd */
1048static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1049{
1050 CharDriverState *chr;
1051 FDCharDriver *s;
1052
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001053 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02001054 s = g_new0(FDCharDriver, 1);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301055 s->fd_in = io_channel_from_fd(fd_in);
1056 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001057 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301058 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001059 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301060 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001061 chr->chr_write = fd_chr_write;
1062 chr->chr_update_read_handler = fd_chr_update_read_handler;
1063 chr->chr_close = fd_chr_close;
1064
aliguori6f97dba2008-10-31 18:49:55 +00001065 return chr;
1066}
1067
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001068static CharDriverState *qemu_chr_open_pipe(const char *id,
1069 ChardevBackend *backend,
1070 ChardevReturn *ret,
1071 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001072{
Eric Blake130257d2015-10-26 16:34:57 -06001073 ChardevHostdev *opts = backend->u.pipe;
aliguori6f97dba2008-10-31 18:49:55 +00001074 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001075 char filename_in[CHR_MAX_FILENAME_SIZE];
1076 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001077 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001078
Corey Minyard9f781162014-10-02 11:17:33 -05001079 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1080 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001081 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1082 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001083 if (fd_in < 0 || fd_out < 0) {
1084 if (fd_in >= 0)
1085 close(fd_in);
1086 if (fd_out >= 0)
1087 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001088 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001089 if (fd_in < 0) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001090 error_setg_file_open(errp, errno, filename);
Markus Armbruster1f514702012-02-07 15:09:08 +01001091 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001092 }
aliguori6f97dba2008-10-31 18:49:55 +00001093 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001094 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001095}
1096
aliguori6f97dba2008-10-31 18:49:55 +00001097/* init terminal so that we can grab keys */
1098static struct termios oldtty;
1099static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001100static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001101static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001102static bool stdio_echo_state;
1103
1104static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001105
1106static void term_exit(void)
1107{
1108 tcsetattr (0, TCSANOW, &oldtty);
1109 fcntl(0, F_SETFL, old_fd0_flags);
1110}
1111
Gal Hammere76d4422015-01-07 10:38:35 +02001112static void term_stdio_handler(int sig)
1113{
1114 /* restore echo after resume from suspend. */
1115 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1116}
1117
Paolo Bonzinibb002512010-12-23 13:42:50 +01001118static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001119{
1120 struct termios tty;
1121
Gal Hammere76d4422015-01-07 10:38:35 +02001122 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001123 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001124 if (!echo) {
1125 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001126 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001127 tty.c_oflag |= OPOST;
1128 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1129 tty.c_cflag &= ~(CSIZE|PARENB);
1130 tty.c_cflag |= CS8;
1131 tty.c_cc[VMIN] = 1;
1132 tty.c_cc[VTIME] = 0;
1133 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001134 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001135 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001136
1137 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001138}
1139
1140static void qemu_chr_close_stdio(struct CharDriverState *chr)
1141{
1142 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001143 fd_chr_close(chr);
1144}
1145
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001146static CharDriverState *qemu_chr_open_stdio(const char *id,
1147 ChardevBackend *backend,
1148 ChardevReturn *ret,
1149 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001150{
Eric Blake130257d2015-10-26 16:34:57 -06001151 ChardevStdio *opts = backend->u.stdio;
aliguori6f97dba2008-10-31 18:49:55 +00001152 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001153 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001154
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001155 if (is_daemonized()) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001156 error_setg(errp, "cannot use stdio with -daemonize");
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001157 return NULL;
1158 }
Li Liuc88930a2014-09-09 19:19:48 +08001159
1160 if (stdio_in_use) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001161 error_setg(errp, "cannot use stdio by multiple character devices");
1162 return NULL;
Li Liuc88930a2014-09-09 19:19:48 +08001163 }
1164
1165 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301166 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001167 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001168 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301169 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001170
Gal Hammere76d4422015-01-07 10:38:35 +02001171 memset(&act, 0, sizeof(act));
1172 act.sa_handler = term_stdio_handler;
1173 sigaction(SIGCONT, &act, NULL);
1174
aliguori6f97dba2008-10-31 18:49:55 +00001175 chr = qemu_chr_open_fd(0, 1);
1176 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001177 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001178 if (opts->has_signal) {
1179 stdio_allow_signal = opts->signal;
1180 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001181 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001182
Markus Armbruster1f514702012-02-07 15:09:08 +01001183 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001184}
1185
aliguori6f97dba2008-10-31 18:49:55 +00001186#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001187 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1188 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001189
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001190#define HAVE_CHARDEV_SERIAL 1
1191#define HAVE_CHARDEV_PTY 1
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001192
aliguori6f97dba2008-10-31 18:49:55 +00001193typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301194 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001195 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001196
1197 /* Protected by the CharDriverState chr_write_lock. */
1198 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301199 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001200 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001201} PtyCharDriver;
1202
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001203static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001204static void pty_chr_state(CharDriverState *chr, int connected);
1205
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301206static gboolean pty_chr_timer(gpointer opaque)
1207{
1208 struct CharDriverState *chr = opaque;
1209 PtyCharDriver *s = chr->opaque;
1210
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001211 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001212 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001213 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001214 if (!s->connected) {
1215 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001216 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001217 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001218 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301219 return FALSE;
1220}
1221
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001222/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301223static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1224{
1225 PtyCharDriver *s = chr->opaque;
1226
1227 if (s->timer_tag) {
1228 g_source_remove(s->timer_tag);
1229 s->timer_tag = 0;
1230 }
1231
1232 if (ms == 1000) {
1233 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1234 } else {
1235 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1236 }
1237}
1238
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001239/* Called with chr_write_lock held. */
1240static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001241{
1242 PtyCharDriver *s = chr->opaque;
1243 GPollFD pfd;
1244
1245 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1246 pfd.events = G_IO_OUT;
1247 pfd.revents = 0;
1248 g_poll(&pfd, 1, 0);
1249 if (pfd.revents & G_IO_HUP) {
1250 pty_chr_state(chr, 0);
1251 } else {
1252 pty_chr_state(chr, 1);
1253 }
1254}
1255
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001256static void pty_chr_update_read_handler(CharDriverState *chr)
1257{
1258 qemu_mutex_lock(&chr->chr_write_lock);
1259 pty_chr_update_read_handler_locked(chr);
1260 qemu_mutex_unlock(&chr->chr_write_lock);
1261}
1262
1263/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001264static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1265{
1266 PtyCharDriver *s = chr->opaque;
1267
1268 if (!s->connected) {
1269 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001270 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001271 if (!s->connected) {
1272 return 0;
1273 }
aliguori6f97dba2008-10-31 18:49:55 +00001274 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001275 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001276}
1277
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301278static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1279{
1280 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001281 if (!s->connected) {
1282 return NULL;
1283 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301284 return g_io_create_watch(s->fd, cond);
1285}
1286
aliguori6f97dba2008-10-31 18:49:55 +00001287static int pty_chr_read_poll(void *opaque)
1288{
1289 CharDriverState *chr = opaque;
1290 PtyCharDriver *s = chr->opaque;
1291
Anthony Liguori909cda12011-08-15 11:17:31 -05001292 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001293 return s->read_bytes;
1294}
1295
Anthony Liguori093d3a22013-03-05 23:21:20 +05301296static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001297{
1298 CharDriverState *chr = opaque;
1299 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301300 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301301 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301302 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001303
1304 len = sizeof(buf);
1305 if (len > s->read_bytes)
1306 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001307 if (len == 0) {
1308 return TRUE;
1309 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301310 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1311 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001312 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301313 return FALSE;
1314 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001315 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001316 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001317 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301318 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001319}
1320
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001321static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1322{
1323 CharDriverState *chr = opaque;
1324 PtyCharDriver *s = chr->opaque;
1325
1326 s->open_tag = 0;
1327 qemu_chr_be_generic_open(chr);
1328 return FALSE;
1329}
1330
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001331/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001332static void pty_chr_state(CharDriverState *chr, int connected)
1333{
1334 PtyCharDriver *s = chr->opaque;
1335
1336 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001337 if (s->open_tag) {
1338 g_source_remove(s->open_tag);
1339 s->open_tag = 0;
1340 }
Amit Shah26da70c2013-08-28 15:23:37 +05301341 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001342 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001343 /* (re-)connect poll interval for idle guests: once per second.
1344 * We check more frequently in case the guests sends data to
1345 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301346 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001347 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001348 if (s->timer_tag) {
1349 g_source_remove(s->timer_tag);
1350 s->timer_tag = 0;
1351 }
1352 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001353 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001354 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001355 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001356 }
1357 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301358 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1359 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001360 }
aliguori6f97dba2008-10-31 18:49:55 +00001361 }
1362}
1363
aliguori6f97dba2008-10-31 18:49:55 +00001364static void pty_chr_close(struct CharDriverState *chr)
1365{
1366 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301367 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001368
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001369 qemu_mutex_lock(&chr->chr_write_lock);
1370 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301371 fd = g_io_channel_unix_get_fd(s->fd);
1372 g_io_channel_unref(s->fd);
1373 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301374 if (s->timer_tag) {
1375 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001376 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301377 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001378 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001379 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001380 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001381}
1382
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001383static CharDriverState *qemu_chr_open_pty(const char *id,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001384 ChardevBackend *backend,
1385 ChardevReturn *ret,
1386 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001387{
1388 CharDriverState *chr;
1389 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001390 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001391 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001392
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001393 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1394 if (master_fd < 0) {
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001395 error_setg_errno(errp, errno, "Failed to create PTY");
Markus Armbruster1f514702012-02-07 15:09:08 +01001396 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001397 }
1398
aliguori6f97dba2008-10-31 18:49:55 +00001399 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001400 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001401
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001402 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001403
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001404 chr->filename = g_strdup_printf("pty:%s", pty_name);
1405 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001406 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001407
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001408 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001409 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001410
Markus Armbruster2d528d42015-09-14 13:54:03 +02001411 s = g_new0(PtyCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001412 chr->opaque = s;
1413 chr->chr_write = pty_chr_write;
1414 chr->chr_update_read_handler = pty_chr_update_read_handler;
1415 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301416 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001417 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001418
Anthony Liguori093d3a22013-03-05 23:21:20 +05301419 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301420 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001421
Markus Armbruster1f514702012-02-07 15:09:08 +01001422 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001423}
1424
1425static void tty_serial_init(int fd, int speed,
1426 int parity, int data_bits, int stop_bits)
1427{
1428 struct termios tty;
1429 speed_t spd;
1430
1431#if 0
1432 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1433 speed, parity, data_bits, stop_bits);
1434#endif
1435 tcgetattr (fd, &tty);
1436
Stefan Weil45eea132009-10-26 16:10:10 +01001437#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1438 speed = speed * 10 / 11;
1439 do {
1440 check_speed(50);
1441 check_speed(75);
1442 check_speed(110);
1443 check_speed(134);
1444 check_speed(150);
1445 check_speed(200);
1446 check_speed(300);
1447 check_speed(600);
1448 check_speed(1200);
1449 check_speed(1800);
1450 check_speed(2400);
1451 check_speed(4800);
1452 check_speed(9600);
1453 check_speed(19200);
1454 check_speed(38400);
1455 /* Non-Posix values follow. They may be unsupported on some systems. */
1456 check_speed(57600);
1457 check_speed(115200);
1458#ifdef B230400
1459 check_speed(230400);
1460#endif
1461#ifdef B460800
1462 check_speed(460800);
1463#endif
1464#ifdef B500000
1465 check_speed(500000);
1466#endif
1467#ifdef B576000
1468 check_speed(576000);
1469#endif
1470#ifdef B921600
1471 check_speed(921600);
1472#endif
1473#ifdef B1000000
1474 check_speed(1000000);
1475#endif
1476#ifdef B1152000
1477 check_speed(1152000);
1478#endif
1479#ifdef B1500000
1480 check_speed(1500000);
1481#endif
1482#ifdef B2000000
1483 check_speed(2000000);
1484#endif
1485#ifdef B2500000
1486 check_speed(2500000);
1487#endif
1488#ifdef B3000000
1489 check_speed(3000000);
1490#endif
1491#ifdef B3500000
1492 check_speed(3500000);
1493#endif
1494#ifdef B4000000
1495 check_speed(4000000);
1496#endif
aliguori6f97dba2008-10-31 18:49:55 +00001497 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001498 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001499
1500 cfsetispeed(&tty, spd);
1501 cfsetospeed(&tty, spd);
1502
1503 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1504 |INLCR|IGNCR|ICRNL|IXON);
1505 tty.c_oflag |= OPOST;
1506 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1507 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1508 switch(data_bits) {
1509 default:
1510 case 8:
1511 tty.c_cflag |= CS8;
1512 break;
1513 case 7:
1514 tty.c_cflag |= CS7;
1515 break;
1516 case 6:
1517 tty.c_cflag |= CS6;
1518 break;
1519 case 5:
1520 tty.c_cflag |= CS5;
1521 break;
1522 }
1523 switch(parity) {
1524 default:
1525 case 'N':
1526 break;
1527 case 'E':
1528 tty.c_cflag |= PARENB;
1529 break;
1530 case 'O':
1531 tty.c_cflag |= PARENB | PARODD;
1532 break;
1533 }
1534 if (stop_bits == 2)
1535 tty.c_cflag |= CSTOPB;
1536
1537 tcsetattr (fd, TCSANOW, &tty);
1538}
1539
1540static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1541{
1542 FDCharDriver *s = chr->opaque;
1543
1544 switch(cmd) {
1545 case CHR_IOCTL_SERIAL_SET_PARAMS:
1546 {
1547 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301548 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1549 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001550 ssp->data_bits, ssp->stop_bits);
1551 }
1552 break;
1553 case CHR_IOCTL_SERIAL_SET_BREAK:
1554 {
1555 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301556 if (enable) {
1557 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1558 }
aliguori6f97dba2008-10-31 18:49:55 +00001559 }
1560 break;
1561 case CHR_IOCTL_SERIAL_GET_TIOCM:
1562 {
1563 int sarg = 0;
1564 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301565 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001566 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001567 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001568 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001569 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001570 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001571 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001572 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001573 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001574 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001575 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001576 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001577 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001578 *targ |= CHR_TIOCM_RTS;
1579 }
1580 break;
1581 case CHR_IOCTL_SERIAL_SET_TIOCM:
1582 {
1583 int sarg = *(int *)arg;
1584 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301585 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001586 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1587 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1588 if (sarg & CHR_TIOCM_CTS)
1589 targ |= TIOCM_CTS;
1590 if (sarg & CHR_TIOCM_CAR)
1591 targ |= TIOCM_CAR;
1592 if (sarg & CHR_TIOCM_DSR)
1593 targ |= TIOCM_DSR;
1594 if (sarg & CHR_TIOCM_RI)
1595 targ |= TIOCM_RI;
1596 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001597 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001598 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001599 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301600 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001601 }
1602 break;
1603 default:
1604 return -ENOTSUP;
1605 }
1606 return 0;
1607}
1608
David Ahern4266a132010-02-10 18:27:17 -07001609static void qemu_chr_close_tty(CharDriverState *chr)
1610{
1611 FDCharDriver *s = chr->opaque;
1612 int fd = -1;
1613
1614 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301615 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001616 }
1617
1618 fd_chr_close(chr);
1619
1620 if (fd >= 0) {
1621 close(fd);
1622 }
1623}
1624
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001625static CharDriverState *qemu_chr_open_tty_fd(int fd)
1626{
1627 CharDriverState *chr;
1628
1629 tty_serial_init(fd, 115200, 'N', 8, 1);
1630 chr = qemu_chr_open_fd(fd, fd);
1631 chr->chr_ioctl = tty_serial_ioctl;
1632 chr->chr_close = qemu_chr_close_tty;
1633 return chr;
1634}
aliguori6f97dba2008-10-31 18:49:55 +00001635#endif /* __linux__ || __sun__ */
1636
1637#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001638
1639#define HAVE_CHARDEV_PARPORT 1
1640
aliguori6f97dba2008-10-31 18:49:55 +00001641typedef struct {
1642 int fd;
1643 int mode;
1644} ParallelCharDriver;
1645
1646static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1647{
1648 if (s->mode != mode) {
1649 int m = mode;
1650 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1651 return 0;
1652 s->mode = mode;
1653 }
1654 return 1;
1655}
1656
1657static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1658{
1659 ParallelCharDriver *drv = chr->opaque;
1660 int fd = drv->fd;
1661 uint8_t b;
1662
1663 switch(cmd) {
1664 case CHR_IOCTL_PP_READ_DATA:
1665 if (ioctl(fd, PPRDATA, &b) < 0)
1666 return -ENOTSUP;
1667 *(uint8_t *)arg = b;
1668 break;
1669 case CHR_IOCTL_PP_WRITE_DATA:
1670 b = *(uint8_t *)arg;
1671 if (ioctl(fd, PPWDATA, &b) < 0)
1672 return -ENOTSUP;
1673 break;
1674 case CHR_IOCTL_PP_READ_CONTROL:
1675 if (ioctl(fd, PPRCONTROL, &b) < 0)
1676 return -ENOTSUP;
1677 /* Linux gives only the lowest bits, and no way to know data
1678 direction! For better compatibility set the fixed upper
1679 bits. */
1680 *(uint8_t *)arg = b | 0xc0;
1681 break;
1682 case CHR_IOCTL_PP_WRITE_CONTROL:
1683 b = *(uint8_t *)arg;
1684 if (ioctl(fd, PPWCONTROL, &b) < 0)
1685 return -ENOTSUP;
1686 break;
1687 case CHR_IOCTL_PP_READ_STATUS:
1688 if (ioctl(fd, PPRSTATUS, &b) < 0)
1689 return -ENOTSUP;
1690 *(uint8_t *)arg = b;
1691 break;
1692 case CHR_IOCTL_PP_DATA_DIR:
1693 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1694 return -ENOTSUP;
1695 break;
1696 case CHR_IOCTL_PP_EPP_READ_ADDR:
1697 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1698 struct ParallelIOArg *parg = arg;
1699 int n = read(fd, parg->buffer, parg->count);
1700 if (n != parg->count) {
1701 return -EIO;
1702 }
1703 }
1704 break;
1705 case CHR_IOCTL_PP_EPP_READ:
1706 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1707 struct ParallelIOArg *parg = arg;
1708 int n = read(fd, parg->buffer, parg->count);
1709 if (n != parg->count) {
1710 return -EIO;
1711 }
1712 }
1713 break;
1714 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1715 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1716 struct ParallelIOArg *parg = arg;
1717 int n = write(fd, parg->buffer, parg->count);
1718 if (n != parg->count) {
1719 return -EIO;
1720 }
1721 }
1722 break;
1723 case CHR_IOCTL_PP_EPP_WRITE:
1724 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1725 struct ParallelIOArg *parg = arg;
1726 int n = write(fd, parg->buffer, parg->count);
1727 if (n != parg->count) {
1728 return -EIO;
1729 }
1730 }
1731 break;
1732 default:
1733 return -ENOTSUP;
1734 }
1735 return 0;
1736}
1737
1738static void pp_close(CharDriverState *chr)
1739{
1740 ParallelCharDriver *drv = chr->opaque;
1741 int fd = drv->fd;
1742
1743 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1744 ioctl(fd, PPRELEASE);
1745 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001746 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001747 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001748}
1749
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001750static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001751{
1752 CharDriverState *chr;
1753 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001754
1755 if (ioctl(fd, PPCLAIM) < 0) {
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001756 error_setg_errno(errp, errno, "not a parallel port");
aliguori6f97dba2008-10-31 18:49:55 +00001757 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001758 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001759 }
1760
Markus Armbruster2d528d42015-09-14 13:54:03 +02001761 drv = g_new0(ParallelCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001762 drv->fd = fd;
1763 drv->mode = IEEE1284_MODE_COMPAT;
1764
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001765 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001766 chr->chr_write = null_chr_write;
1767 chr->chr_ioctl = pp_ioctl;
1768 chr->chr_close = pp_close;
1769 chr->opaque = drv;
1770
Markus Armbruster1f514702012-02-07 15:09:08 +01001771 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001772}
1773#endif /* __linux__ */
1774
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001775#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001776
1777#define HAVE_CHARDEV_PARPORT 1
1778
blueswir16972f932008-11-22 20:49:12 +00001779static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1780{
Stefan Weile0efb992011-02-23 19:09:16 +01001781 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001782 uint8_t b;
1783
1784 switch(cmd) {
1785 case CHR_IOCTL_PP_READ_DATA:
1786 if (ioctl(fd, PPIGDATA, &b) < 0)
1787 return -ENOTSUP;
1788 *(uint8_t *)arg = b;
1789 break;
1790 case CHR_IOCTL_PP_WRITE_DATA:
1791 b = *(uint8_t *)arg;
1792 if (ioctl(fd, PPISDATA, &b) < 0)
1793 return -ENOTSUP;
1794 break;
1795 case CHR_IOCTL_PP_READ_CONTROL:
1796 if (ioctl(fd, PPIGCTRL, &b) < 0)
1797 return -ENOTSUP;
1798 *(uint8_t *)arg = b;
1799 break;
1800 case CHR_IOCTL_PP_WRITE_CONTROL:
1801 b = *(uint8_t *)arg;
1802 if (ioctl(fd, PPISCTRL, &b) < 0)
1803 return -ENOTSUP;
1804 break;
1805 case CHR_IOCTL_PP_READ_STATUS:
1806 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1807 return -ENOTSUP;
1808 *(uint8_t *)arg = b;
1809 break;
1810 default:
1811 return -ENOTSUP;
1812 }
1813 return 0;
1814}
1815
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001816static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
blueswir16972f932008-11-22 20:49:12 +00001817{
1818 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001819
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001820 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001821 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001822 chr->chr_write = null_chr_write;
1823 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001824 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001825 return chr;
blueswir16972f932008-11-22 20:49:12 +00001826}
1827#endif
1828
aliguori6f97dba2008-10-31 18:49:55 +00001829#else /* _WIN32 */
1830
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001831#define HAVE_CHARDEV_SERIAL 1
1832
aliguori6f97dba2008-10-31 18:49:55 +00001833typedef struct {
1834 int max_size;
1835 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001836 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001837 BOOL fpipe;
1838 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001839
1840 /* Protected by the CharDriverState chr_write_lock. */
1841 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001842} WinCharState;
1843
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001844typedef struct {
1845 HANDLE hStdIn;
1846 HANDLE hInputReadyEvent;
1847 HANDLE hInputDoneEvent;
1848 HANDLE hInputThread;
1849 uint8_t win_stdio_buf;
1850} WinStdioCharState;
1851
aliguori6f97dba2008-10-31 18:49:55 +00001852#define NSENDBUF 2048
1853#define NRECVBUF 2048
1854#define MAXCONNECT 1
1855#define NTIMEOUT 5000
1856
1857static int win_chr_poll(void *opaque);
1858static int win_chr_pipe_poll(void *opaque);
1859
1860static void win_chr_close(CharDriverState *chr)
1861{
1862 WinCharState *s = chr->opaque;
1863
1864 if (s->hsend) {
1865 CloseHandle(s->hsend);
1866 s->hsend = NULL;
1867 }
1868 if (s->hrecv) {
1869 CloseHandle(s->hrecv);
1870 s->hrecv = NULL;
1871 }
1872 if (s->hcom) {
1873 CloseHandle(s->hcom);
1874 s->hcom = NULL;
1875 }
1876 if (s->fpipe)
1877 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1878 else
1879 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301880
Hans de Goedea425d232011-11-19 10:22:43 +01001881 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001882}
1883
Paolo Bonzini6511d392015-09-29 15:08:05 +02001884static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001885{
1886 WinCharState *s = chr->opaque;
1887 COMMCONFIG comcfg;
1888 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1889 COMSTAT comstat;
1890 DWORD size;
1891 DWORD err;
1892
1893 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1894 if (!s->hsend) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001895 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001896 goto fail;
1897 }
1898 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1899 if (!s->hrecv) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001900 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001901 goto fail;
1902 }
1903
1904 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1905 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1906 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001907 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00001908 s->hcom = NULL;
1909 goto fail;
1910 }
1911
1912 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001913 error_setg(errp, "Failed SetupComm");
aliguori6f97dba2008-10-31 18:49:55 +00001914 goto fail;
1915 }
1916
1917 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1918 size = sizeof(COMMCONFIG);
1919 GetDefaultCommConfig(filename, &comcfg, &size);
1920 comcfg.dcb.DCBlength = sizeof(DCB);
1921 CommConfigDialog(filename, NULL, &comcfg);
1922
1923 if (!SetCommState(s->hcom, &comcfg.dcb)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001924 error_setg(errp, "Failed SetCommState");
aliguori6f97dba2008-10-31 18:49:55 +00001925 goto fail;
1926 }
1927
1928 if (!SetCommMask(s->hcom, EV_ERR)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001929 error_setg(errp, "Failed SetCommMask");
aliguori6f97dba2008-10-31 18:49:55 +00001930 goto fail;
1931 }
1932
1933 cto.ReadIntervalTimeout = MAXDWORD;
1934 if (!SetCommTimeouts(s->hcom, &cto)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001935 error_setg(errp, "Failed SetCommTimeouts");
aliguori6f97dba2008-10-31 18:49:55 +00001936 goto fail;
1937 }
1938
1939 if (!ClearCommError(s->hcom, &err, &comstat)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001940 error_setg(errp, "Failed ClearCommError");
aliguori6f97dba2008-10-31 18:49:55 +00001941 goto fail;
1942 }
1943 qemu_add_polling_cb(win_chr_poll, chr);
1944 return 0;
1945
1946 fail:
1947 win_chr_close(chr);
1948 return -1;
1949}
1950
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001951/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001952static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1953{
1954 WinCharState *s = chr->opaque;
1955 DWORD len, ret, size, err;
1956
1957 len = len1;
1958 ZeroMemory(&s->osend, sizeof(s->osend));
1959 s->osend.hEvent = s->hsend;
1960 while (len > 0) {
1961 if (s->hsend)
1962 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1963 else
1964 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1965 if (!ret) {
1966 err = GetLastError();
1967 if (err == ERROR_IO_PENDING) {
1968 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1969 if (ret) {
1970 buf += size;
1971 len -= size;
1972 } else {
1973 break;
1974 }
1975 } else {
1976 break;
1977 }
1978 } else {
1979 buf += size;
1980 len -= size;
1981 }
1982 }
1983 return len1 - len;
1984}
1985
1986static int win_chr_read_poll(CharDriverState *chr)
1987{
1988 WinCharState *s = chr->opaque;
1989
Anthony Liguori909cda12011-08-15 11:17:31 -05001990 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001991 return s->max_size;
1992}
1993
1994static void win_chr_readfile(CharDriverState *chr)
1995{
1996 WinCharState *s = chr->opaque;
1997 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301998 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001999 DWORD size;
2000
2001 ZeroMemory(&s->orecv, sizeof(s->orecv));
2002 s->orecv.hEvent = s->hrecv;
2003 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2004 if (!ret) {
2005 err = GetLastError();
2006 if (err == ERROR_IO_PENDING) {
2007 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2008 }
2009 }
2010
2011 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002012 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002013 }
2014}
2015
2016static void win_chr_read(CharDriverState *chr)
2017{
2018 WinCharState *s = chr->opaque;
2019
2020 if (s->len > s->max_size)
2021 s->len = s->max_size;
2022 if (s->len == 0)
2023 return;
2024
2025 win_chr_readfile(chr);
2026}
2027
2028static int win_chr_poll(void *opaque)
2029{
2030 CharDriverState *chr = opaque;
2031 WinCharState *s = chr->opaque;
2032 COMSTAT status;
2033 DWORD comerr;
2034
2035 ClearCommError(s->hcom, &comerr, &status);
2036 if (status.cbInQue > 0) {
2037 s->len = status.cbInQue;
2038 win_chr_read_poll(chr);
2039 win_chr_read(chr);
2040 return 1;
2041 }
2042 return 0;
2043}
2044
Paolo Bonzini6511d392015-09-29 15:08:05 +02002045static CharDriverState *qemu_chr_open_win_path(const char *filename,
2046 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002047{
2048 CharDriverState *chr;
2049 WinCharState *s;
2050
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002051 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002052 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002053 chr->opaque = s;
2054 chr->chr_write = win_chr_write;
2055 chr->chr_close = win_chr_close;
2056
Paolo Bonzini6511d392015-09-29 15:08:05 +02002057 if (win_chr_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002058 g_free(s);
2059 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002060 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002061 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002062 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002063}
2064
2065static int win_chr_pipe_poll(void *opaque)
2066{
2067 CharDriverState *chr = opaque;
2068 WinCharState *s = chr->opaque;
2069 DWORD size;
2070
2071 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2072 if (size > 0) {
2073 s->len = size;
2074 win_chr_read_poll(chr);
2075 win_chr_read(chr);
2076 return 1;
2077 }
2078 return 0;
2079}
2080
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002081static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
2082 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002083{
2084 WinCharState *s = chr->opaque;
2085 OVERLAPPED ov;
2086 int ret;
2087 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002088 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002089
2090 s->fpipe = TRUE;
2091
2092 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2093 if (!s->hsend) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002094 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002095 goto fail;
2096 }
2097 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2098 if (!s->hrecv) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002099 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002100 goto fail;
2101 }
2102
2103 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2104 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2105 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2106 PIPE_WAIT,
2107 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2108 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002109 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00002110 s->hcom = NULL;
2111 goto fail;
2112 }
2113
2114 ZeroMemory(&ov, sizeof(ov));
2115 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2116 ret = ConnectNamedPipe(s->hcom, &ov);
2117 if (ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002118 error_setg(errp, "Failed ConnectNamedPipe");
aliguori6f97dba2008-10-31 18:49:55 +00002119 goto fail;
2120 }
2121
2122 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2123 if (!ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002124 error_setg(errp, "Failed GetOverlappedResult");
aliguori6f97dba2008-10-31 18:49:55 +00002125 if (ov.hEvent) {
2126 CloseHandle(ov.hEvent);
2127 ov.hEvent = NULL;
2128 }
2129 goto fail;
2130 }
2131
2132 if (ov.hEvent) {
2133 CloseHandle(ov.hEvent);
2134 ov.hEvent = NULL;
2135 }
2136 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2137 return 0;
2138
2139 fail:
2140 win_chr_close(chr);
2141 return -1;
2142}
2143
2144
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002145static CharDriverState *qemu_chr_open_pipe(const char *id,
2146 ChardevBackend *backend,
2147 ChardevReturn *ret,
2148 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002149{
Eric Blake130257d2015-10-26 16:34:57 -06002150 ChardevHostdev *opts = backend->u.pipe;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002151 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002152 CharDriverState *chr;
2153 WinCharState *s;
2154
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002155 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002156 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002157 chr->opaque = s;
2158 chr->chr_write = win_chr_write;
2159 chr->chr_close = win_chr_close;
2160
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002161 if (win_chr_pipe_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002162 g_free(s);
2163 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002164 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002165 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002166 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002167}
2168
Markus Armbruster1f514702012-02-07 15:09:08 +01002169static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002170{
2171 CharDriverState *chr;
2172 WinCharState *s;
2173
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002174 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002175 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002176 s->hcom = fd_out;
2177 chr->opaque = s;
2178 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002179 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002180}
2181
Paolo Bonzini122e5ed2015-09-29 15:42:04 +02002182static CharDriverState *qemu_chr_open_win_con(const char *id,
2183 ChardevBackend *backend,
2184 ChardevReturn *ret,
2185 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002186{
Markus Armbruster1f514702012-02-07 15:09:08 +01002187 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002188}
2189
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002190static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2191{
2192 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2193 DWORD dwSize;
2194 int len1;
2195
2196 len1 = len;
2197
2198 while (len1 > 0) {
2199 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2200 break;
2201 }
2202 buf += dwSize;
2203 len1 -= dwSize;
2204 }
2205
2206 return len - len1;
2207}
2208
2209static void win_stdio_wait_func(void *opaque)
2210{
2211 CharDriverState *chr = opaque;
2212 WinStdioCharState *stdio = chr->opaque;
2213 INPUT_RECORD buf[4];
2214 int ret;
2215 DWORD dwSize;
2216 int i;
2217
Stefan Weildff74242013-12-07 14:48:04 +01002218 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002219
2220 if (!ret) {
2221 /* Avoid error storm */
2222 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2223 return;
2224 }
2225
2226 for (i = 0; i < dwSize; i++) {
2227 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2228
2229 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2230 int j;
2231 if (kev->uChar.AsciiChar != 0) {
2232 for (j = 0; j < kev->wRepeatCount; j++) {
2233 if (qemu_chr_be_can_write(chr)) {
2234 uint8_t c = kev->uChar.AsciiChar;
2235 qemu_chr_be_write(chr, &c, 1);
2236 }
2237 }
2238 }
2239 }
2240 }
2241}
2242
2243static DWORD WINAPI win_stdio_thread(LPVOID param)
2244{
2245 CharDriverState *chr = param;
2246 WinStdioCharState *stdio = chr->opaque;
2247 int ret;
2248 DWORD dwSize;
2249
2250 while (1) {
2251
2252 /* Wait for one byte */
2253 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2254
2255 /* Exit in case of error, continue if nothing read */
2256 if (!ret) {
2257 break;
2258 }
2259 if (!dwSize) {
2260 continue;
2261 }
2262
2263 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2264 if (stdio->win_stdio_buf == '\r') {
2265 continue;
2266 }
2267
2268 /* Signal the main thread and wait until the byte was eaten */
2269 if (!SetEvent(stdio->hInputReadyEvent)) {
2270 break;
2271 }
2272 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2273 != WAIT_OBJECT_0) {
2274 break;
2275 }
2276 }
2277
2278 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2279 return 0;
2280}
2281
2282static void win_stdio_thread_wait_func(void *opaque)
2283{
2284 CharDriverState *chr = opaque;
2285 WinStdioCharState *stdio = chr->opaque;
2286
2287 if (qemu_chr_be_can_write(chr)) {
2288 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2289 }
2290
2291 SetEvent(stdio->hInputDoneEvent);
2292}
2293
2294static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2295{
2296 WinStdioCharState *stdio = chr->opaque;
2297 DWORD dwMode = 0;
2298
2299 GetConsoleMode(stdio->hStdIn, &dwMode);
2300
2301 if (echo) {
2302 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2303 } else {
2304 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2305 }
2306}
2307
2308static void win_stdio_close(CharDriverState *chr)
2309{
2310 WinStdioCharState *stdio = chr->opaque;
2311
2312 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2313 CloseHandle(stdio->hInputReadyEvent);
2314 }
2315 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2316 CloseHandle(stdio->hInputDoneEvent);
2317 }
2318 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2319 TerminateThread(stdio->hInputThread, 0);
2320 }
2321
2322 g_free(chr->opaque);
2323 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002324}
2325
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002326static CharDriverState *qemu_chr_open_stdio(const char *id,
2327 ChardevBackend *backend,
2328 ChardevReturn *ret,
2329 Error **errp)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002330{
2331 CharDriverState *chr;
2332 WinStdioCharState *stdio;
2333 DWORD dwMode;
2334 int is_console = 0;
2335
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002336 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002337 stdio = g_new0(WinStdioCharState, 1);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002338
2339 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2340 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002341 error_setg(errp, "cannot open stdio: invalid handle");
2342 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002343 }
2344
2345 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2346
2347 chr->opaque = stdio;
2348 chr->chr_write = win_stdio_write;
2349 chr->chr_close = win_stdio_close;
2350
Anthony Liguoried7a1542013-03-05 23:21:17 +05302351 if (is_console) {
2352 if (qemu_add_wait_object(stdio->hStdIn,
2353 win_stdio_wait_func, chr)) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002354 error_setg(errp, "qemu_add_wait_object: failed");
2355 goto err1;
Anthony Liguoried7a1542013-03-05 23:21:17 +05302356 }
2357 } else {
2358 DWORD dwId;
2359
2360 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2361 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002362 if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
Anthony Liguoried7a1542013-03-05 23:21:17 +05302363 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002364 error_setg(errp, "cannot create event");
2365 goto err2;
Anthony Liguoried7a1542013-03-05 23:21:17 +05302366 }
2367 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2368 win_stdio_thread_wait_func, chr)) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002369 error_setg(errp, "qemu_add_wait_object: failed");
2370 goto err2;
2371 }
2372 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2373 chr, 0, &dwId);
2374
2375 if (stdio->hInputThread == INVALID_HANDLE_VALUE) {
2376 error_setg(errp, "cannot create stdio thread");
2377 goto err3;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002378 }
2379 }
2380
2381 dwMode |= ENABLE_LINE_INPUT;
2382
Anthony Liguoried7a1542013-03-05 23:21:17 +05302383 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002384 /* set the terminal in raw mode */
2385 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2386 dwMode |= ENABLE_PROCESSED_INPUT;
2387 }
2388
2389 SetConsoleMode(stdio->hStdIn, dwMode);
2390
2391 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2392 qemu_chr_fe_set_echo(chr, false);
2393
Markus Armbruster1f514702012-02-07 15:09:08 +01002394 return chr;
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002395
2396err3:
2397 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2398err2:
2399 CloseHandle(stdio->hInputReadyEvent);
2400 CloseHandle(stdio->hInputDoneEvent);
2401err1:
2402 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2403 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002404}
aliguori6f97dba2008-10-31 18:49:55 +00002405#endif /* !_WIN32 */
2406
Anthony Liguori5ab82112013-03-05 23:21:31 +05302407
aliguori6f97dba2008-10-31 18:49:55 +00002408/***********************************************************/
2409/* UDP Net console */
2410
2411typedef struct {
2412 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302413 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302414 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002415 int bufcnt;
2416 int bufptr;
2417 int max_size;
2418} NetCharDriver;
2419
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002420/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002421static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2422{
2423 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302424 gsize bytes_written;
2425 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002426
Anthony Liguori76a96442013-03-05 23:21:21 +05302427 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2428 if (status == G_IO_STATUS_EOF) {
2429 return 0;
2430 } else if (status != G_IO_STATUS_NORMAL) {
2431 return -1;
2432 }
2433
2434 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002435}
2436
2437static int udp_chr_read_poll(void *opaque)
2438{
2439 CharDriverState *chr = opaque;
2440 NetCharDriver *s = chr->opaque;
2441
Anthony Liguori909cda12011-08-15 11:17:31 -05002442 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002443
2444 /* If there were any stray characters in the queue process them
2445 * first
2446 */
2447 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002448 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002449 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002450 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002451 }
2452 return s->max_size;
2453}
2454
Anthony Liguori76a96442013-03-05 23:21:21 +05302455static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002456{
2457 CharDriverState *chr = opaque;
2458 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302459 gsize bytes_read = 0;
2460 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002461
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002462 if (s->max_size == 0) {
2463 return TRUE;
2464 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302465 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2466 &bytes_read, NULL);
2467 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002468 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302469 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302470 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302471 return FALSE;
2472 }
aliguori6f97dba2008-10-31 18:49:55 +00002473
2474 s->bufptr = 0;
2475 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002476 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002477 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002478 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002479 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302480
2481 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002482}
2483
2484static void udp_chr_update_read_handler(CharDriverState *chr)
2485{
2486 NetCharDriver *s = chr->opaque;
2487
Amit Shah26da70c2013-08-28 15:23:37 +05302488 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302489 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302490 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2491 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002492 }
2493}
2494
aliguori819f56b2009-03-28 17:58:14 +00002495static void udp_chr_close(CharDriverState *chr)
2496{
2497 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302498
2499 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302500 if (s->chan) {
2501 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002502 closesocket(s->fd);
2503 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002504 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002505 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002506}
2507
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002508static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002509{
2510 CharDriverState *chr = NULL;
2511 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002512
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002513 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002514 s = g_new0(NetCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002515
aliguori6f97dba2008-10-31 18:49:55 +00002516 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302517 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002518 s->bufcnt = 0;
2519 s->bufptr = 0;
2520 chr->opaque = s;
2521 chr->chr_write = udp_chr_write;
2522 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002523 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002524 /* be isn't opened until we get a connection */
2525 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002526 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002527}
aliguori6f97dba2008-10-31 18:49:55 +00002528
aliguori6f97dba2008-10-31 18:49:55 +00002529/***********************************************************/
2530/* TCP Net console */
2531
2532typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302533
2534 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302535 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002536 int fd, listen_fd;
2537 int connected;
2538 int max_size;
2539 int do_telnetopt;
2540 int do_nodelay;
2541 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002542 int *read_msgfds;
2543 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002544 int *write_msgfds;
2545 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002546
2547 SocketAddress *addr;
2548 bool is_listen;
2549 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002550
2551 guint reconnect_timer;
2552 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002553 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002554} TCPCharDriver;
2555
Corey Minyard5dd1f022014-10-02 11:17:37 -05002556static gboolean socket_reconnect_timeout(gpointer opaque);
2557
2558static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2559{
2560 TCPCharDriver *s = chr->opaque;
2561 assert(s->connected == 0);
2562 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2563 socket_reconnect_timeout, chr);
2564}
2565
Corey Minyard5008e5b2014-10-08 07:11:55 -05002566static void check_report_connect_error(CharDriverState *chr,
2567 Error *err)
2568{
2569 TCPCharDriver *s = chr->opaque;
2570
2571 if (!s->connect_err_reported) {
2572 error_report("Unable to connect character device %s: %s",
2573 chr->label, error_get_pretty(err));
2574 s->connect_err_reported = true;
2575 }
2576 qemu_chr_socket_restart_timer(chr);
2577}
2578
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302579static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002580
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002581#ifndef _WIN32
2582static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2583{
2584 TCPCharDriver *s = chr->opaque;
2585 struct msghdr msgh;
2586 struct iovec iov;
2587 int r;
2588
2589 size_t fd_size = s->write_msgfds_num * sizeof(int);
2590 char control[CMSG_SPACE(fd_size)];
2591 struct cmsghdr *cmsg;
2592
2593 memset(&msgh, 0, sizeof(msgh));
2594 memset(control, 0, sizeof(control));
2595
2596 /* set the payload */
2597 iov.iov_base = (uint8_t *) buf;
2598 iov.iov_len = len;
2599
2600 msgh.msg_iov = &iov;
2601 msgh.msg_iovlen = 1;
2602
2603 msgh.msg_control = control;
2604 msgh.msg_controllen = sizeof(control);
2605
2606 cmsg = CMSG_FIRSTHDR(&msgh);
2607
2608 cmsg->cmsg_len = CMSG_LEN(fd_size);
2609 cmsg->cmsg_level = SOL_SOCKET;
2610 cmsg->cmsg_type = SCM_RIGHTS;
2611 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2612
2613 do {
2614 r = sendmsg(s->fd, &msgh, 0);
2615 } while (r < 0 && errno == EINTR);
2616
2617 /* free the written msgfds, no matter what */
2618 if (s->write_msgfds_num) {
2619 g_free(s->write_msgfds);
2620 s->write_msgfds = 0;
2621 s->write_msgfds_num = 0;
2622 }
2623
2624 return r;
2625}
2626#endif
2627
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002628/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002629static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2630{
2631 TCPCharDriver *s = chr->opaque;
2632 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002633#ifndef _WIN32
2634 if (s->is_unix && s->write_msgfds_num) {
2635 return unix_send_msgfds(chr, buf, len);
2636 } else
2637#endif
2638 {
2639 return io_channel_send(s->chan, buf, len);
2640 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002641 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002642 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002643 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002644 }
2645}
2646
2647static int tcp_chr_read_poll(void *opaque)
2648{
2649 CharDriverState *chr = opaque;
2650 TCPCharDriver *s = chr->opaque;
2651 if (!s->connected)
2652 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002653 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002654 return s->max_size;
2655}
2656
2657#define IAC 255
2658#define IAC_BREAK 243
2659static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2660 TCPCharDriver *s,
2661 uint8_t *buf, int *size)
2662{
2663 /* Handle any telnet client's basic IAC options to satisfy char by
2664 * char mode with no echo. All IAC options will be removed from
2665 * the buf and the do_telnetopt variable will be used to track the
2666 * state of the width of the IAC information.
2667 *
2668 * IAC commands come in sets of 3 bytes with the exception of the
2669 * "IAC BREAK" command and the double IAC.
2670 */
2671
2672 int i;
2673 int j = 0;
2674
2675 for (i = 0; i < *size; i++) {
2676 if (s->do_telnetopt > 1) {
2677 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2678 /* Double IAC means send an IAC */
2679 if (j != i)
2680 buf[j] = buf[i];
2681 j++;
2682 s->do_telnetopt = 1;
2683 } else {
2684 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2685 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002686 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002687 s->do_telnetopt++;
2688 }
2689 s->do_telnetopt++;
2690 }
2691 if (s->do_telnetopt >= 4) {
2692 s->do_telnetopt = 1;
2693 }
2694 } else {
2695 if ((unsigned char)buf[i] == IAC) {
2696 s->do_telnetopt = 2;
2697 } else {
2698 if (j != i)
2699 buf[j] = buf[i];
2700 j++;
2701 }
2702 }
2703 }
2704 *size = j;
2705}
2706
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002707static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002708{
2709 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002710 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2711
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002712 assert(num <= TCP_MAX_FDS);
2713
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002714 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002715 int i;
2716
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002717 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2718
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002719 /* Close unused fds */
2720 for (i = to_copy; i < s->read_msgfds_num; i++) {
2721 close(s->read_msgfds[i]);
2722 }
2723
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002724 g_free(s->read_msgfds);
2725 s->read_msgfds = 0;
2726 s->read_msgfds_num = 0;
2727 }
2728
2729 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002730}
2731
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002732static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2733{
2734 TCPCharDriver *s = chr->opaque;
2735
2736 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002737 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002738
2739 if (num) {
Markus Armbruster2d528d42015-09-14 13:54:03 +02002740 s->write_msgfds = g_new(int, num);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002741 memcpy(s->write_msgfds, fds, num * sizeof(int));
2742 }
2743
2744 s->write_msgfds_num = num;
2745
2746 return 0;
2747}
2748
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002749#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002750static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2751{
2752 TCPCharDriver *s = chr->opaque;
2753 struct cmsghdr *cmsg;
2754
2755 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002756 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002757
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002758 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002759 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002760 cmsg->cmsg_type != SCM_RIGHTS) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002761 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002762 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002763
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002764 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2765
2766 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002767 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002768 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002769
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002770 /* close and clean read_msgfds */
2771 for (i = 0; i < s->read_msgfds_num; i++) {
2772 close(s->read_msgfds[i]);
2773 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002774
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002775 if (s->read_msgfds_num) {
2776 g_free(s->read_msgfds);
2777 }
2778
2779 s->read_msgfds_num = fd_size / sizeof(int);
2780 s->read_msgfds = g_malloc(fd_size);
2781 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2782
2783 for (i = 0; i < s->read_msgfds_num; i++) {
2784 int fd = s->read_msgfds[i];
2785 if (fd < 0) {
2786 continue;
2787 }
2788
2789 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2790 qemu_set_block(fd);
2791
2792 #ifndef MSG_CMSG_CLOEXEC
2793 qemu_set_cloexec(fd);
2794 #endif
2795 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002796 }
2797}
2798
Mark McLoughlin9977c892009-07-22 09:11:38 +01002799static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2800{
2801 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002802 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002803 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002804 union {
2805 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002806 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002807 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002808 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002809 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002810
2811 iov[0].iov_base = buf;
2812 iov[0].iov_len = len;
2813
2814 msg.msg_iov = iov;
2815 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002816 msg.msg_control = &msg_control;
2817 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002818
Corey Bryant06138652012-08-14 16:43:42 -04002819#ifdef MSG_CMSG_CLOEXEC
2820 flags |= MSG_CMSG_CLOEXEC;
2821#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002822 do {
2823 ret = recvmsg(s->fd, &msg, flags);
2824 } while (ret == -1 && errno == EINTR);
2825
Corey Bryant06138652012-08-14 16:43:42 -04002826 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002827 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002828 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002829
2830 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002831}
2832#else
2833static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2834{
2835 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002836 ssize_t ret;
2837
2838 do {
2839 ret = qemu_recv(s->fd, buf, len, 0);
2840 } while (ret == -1 && socket_error() == EINTR);
2841
2842 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002843}
2844#endif
2845
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302846static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2847{
2848 TCPCharDriver *s = chr->opaque;
2849 return g_io_create_watch(s->chan, cond);
2850}
2851
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002852static void tcp_chr_disconnect(CharDriverState *chr)
2853{
2854 TCPCharDriver *s = chr->opaque;
2855
2856 s->connected = 0;
2857 if (s->listen_chan) {
2858 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2859 tcp_chr_accept, chr);
2860 }
2861 remove_fd_in_watch(chr);
2862 g_io_channel_unref(s->chan);
2863 s->chan = NULL;
2864 closesocket(s->fd);
2865 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002866 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2867 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002868 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002869 if (s->reconnect_time) {
2870 qemu_chr_socket_restart_timer(chr);
2871 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002872}
2873
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302874static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002875{
2876 CharDriverState *chr = opaque;
2877 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302878 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002879 int len, size;
2880
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302881 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002882 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302883 }
aliguori6f97dba2008-10-31 18:49:55 +00002884 len = sizeof(buf);
2885 if (len > s->max_size)
2886 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002887 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002888 if (size == 0 ||
2889 (size < 0 &&
2890 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002891 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002892 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002893 } else if (size > 0) {
2894 if (s->do_telnetopt)
2895 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2896 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002897 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002898 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302899
2900 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002901}
2902
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002903static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2904{
2905 TCPCharDriver *s = chr->opaque;
2906 int size;
2907
2908 if (!s->connected) {
2909 return 0;
2910 }
2911
2912 size = tcp_chr_recv(chr, (void *) buf, len);
2913 if (size == 0) {
2914 /* connection closed */
2915 tcp_chr_disconnect(chr);
2916 }
2917
2918 return size;
2919}
2920
Blue Swirl68c18d12010-08-15 09:46:24 +00002921#ifndef _WIN32
2922CharDriverState *qemu_chr_open_eventfd(int eventfd)
2923{
David Marchande9d21c42014-06-11 17:25:16 +02002924 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2925
2926 if (chr) {
2927 chr->avail_connections = 1;
2928 }
2929
2930 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002931}
Blue Swirl68c18d12010-08-15 09:46:24 +00002932#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002933
aliguori6f97dba2008-10-31 18:49:55 +00002934static void tcp_chr_connect(void *opaque)
2935{
2936 CharDriverState *chr = opaque;
2937 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002938 struct sockaddr_storage ss, ps;
2939 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002940
2941 memset(&ss, 0, ss_len);
2942 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2943 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2944 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002945 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2946 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2947 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002948 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002949 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2950 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002951 s->is_listen, s->is_telnet);
2952 }
aliguori6f97dba2008-10-31 18:49:55 +00002953
2954 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302955 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302956 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2957 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002958 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002959 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002960}
2961
Gal Hammerac1b84d2014-02-25 12:12:35 +02002962static void tcp_chr_update_read_handler(CharDriverState *chr)
2963{
2964 TCPCharDriver *s = chr->opaque;
2965
2966 remove_fd_in_watch(chr);
2967 if (s->chan) {
2968 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2969 tcp_chr_read, chr);
2970 }
2971}
2972
aliguori6f97dba2008-10-31 18:49:55 +00002973#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2974static void tcp_chr_telnet_init(int fd)
2975{
2976 char buf[3];
2977 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2978 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2979 send(fd, (char *)buf, 3, 0);
2980 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2981 send(fd, (char *)buf, 3, 0);
2982 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2983 send(fd, (char *)buf, 3, 0);
2984 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2985 send(fd, (char *)buf, 3, 0);
2986}
2987
Daniel P. Berrange13661082011-06-23 13:31:42 +01002988static int tcp_chr_add_client(CharDriverState *chr, int fd)
2989{
2990 TCPCharDriver *s = chr->opaque;
2991 if (s->fd != -1)
2992 return -1;
2993
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01002994 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01002995 if (s->do_nodelay)
2996 socket_set_nodelay(fd);
2997 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302998 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02002999 if (s->listen_tag) {
3000 g_source_remove(s->listen_tag);
3001 s->listen_tag = 0;
3002 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003003 tcp_chr_connect(chr);
3004
3005 return 0;
3006}
3007
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303008static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00003009{
3010 CharDriverState *chr = opaque;
3011 TCPCharDriver *s = chr->opaque;
3012 struct sockaddr_in saddr;
3013#ifndef _WIN32
3014 struct sockaddr_un uaddr;
3015#endif
3016 struct sockaddr *addr;
3017 socklen_t len;
3018 int fd;
3019
3020 for(;;) {
3021#ifndef _WIN32
3022 if (s->is_unix) {
3023 len = sizeof(uaddr);
3024 addr = (struct sockaddr *)&uaddr;
3025 } else
3026#endif
3027 {
3028 len = sizeof(saddr);
3029 addr = (struct sockaddr *)&saddr;
3030 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003031 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003032 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003033 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303034 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003035 } else if (fd >= 0) {
3036 if (s->do_telnetopt)
3037 tcp_chr_telnet_init(fd);
3038 break;
3039 }
3040 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003041 if (tcp_chr_add_client(chr, fd) < 0)
3042 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303043
3044 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003045}
3046
3047static void tcp_chr_close(CharDriverState *chr)
3048{
3049 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003050 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003051
Corey Minyard5dd1f022014-10-02 11:17:37 -05003052 if (s->reconnect_timer) {
3053 g_source_remove(s->reconnect_timer);
3054 s->reconnect_timer = 0;
3055 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003056 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003057 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303058 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303059 if (s->chan) {
3060 g_io_channel_unref(s->chan);
3061 }
aliguori6f97dba2008-10-31 18:49:55 +00003062 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003063 }
3064 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303065 if (s->listen_tag) {
3066 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003067 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303068 }
3069 if (s->listen_chan) {
3070 g_io_channel_unref(s->listen_chan);
3071 }
aliguori6f97dba2008-10-31 18:49:55 +00003072 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003073 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003074 if (s->read_msgfds_num) {
3075 for (i = 0; i < s->read_msgfds_num; i++) {
3076 close(s->read_msgfds[i]);
3077 }
3078 g_free(s->read_msgfds);
3079 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003080 if (s->write_msgfds_num) {
3081 g_free(s->write_msgfds);
3082 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003083 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003084 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003085}
3086
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003087static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003088{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003089 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003090
Corey Minyardcfb429c2014-10-02 11:17:35 -05003091 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003092 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303093 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003094 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3095 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003096 } else {
3097 s->connected = 1;
3098 s->fd = fd;
3099 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303100 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003101 tcp_chr_connect(chr);
3102 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003103}
3104
Corey Minyard51795022014-10-08 07:11:56 -05003105static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003106{
3107 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003108 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003109
3110 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003111 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003112 return;
3113 }
3114
Corey Minyard5008e5b2014-10-08 07:11:55 -05003115 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003116 qemu_chr_finish_socket_connection(chr, fd);
3117}
3118
Corey Minyardcfb429c2014-10-02 11:17:35 -05003119static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003120{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003121 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003122 int fd;
3123
Corey Minyardcfb429c2014-10-02 11:17:35 -05003124 if (s->is_listen) {
3125 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003126 } else if (s->reconnect_time) {
3127 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3128 return fd >= 0;
3129 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003130 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003131 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003132 if (fd < 0) {
3133 return false;
3134 }
3135
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003136 qemu_chr_finish_socket_connection(chr, fd);
3137 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003138}
3139
Lei Li51767e72013-01-25 00:03:19 +08003140/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003141/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003142
3143typedef struct {
3144 size_t size;
3145 size_t prod;
3146 size_t cons;
3147 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003148} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003149
Markus Armbruster3949e592013-02-06 21:27:24 +01003150static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003151{
Markus Armbruster3949e592013-02-06 21:27:24 +01003152 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003153
Markus Armbruster5c230102013-02-06 21:27:23 +01003154 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003155}
3156
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003157/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003158static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003159{
Markus Armbruster3949e592013-02-06 21:27:24 +01003160 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003161 int i;
3162
3163 if (!buf || (len < 0)) {
3164 return -1;
3165 }
3166
3167 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003168 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3169 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003170 d->cons = d->prod - d->size;
3171 }
3172 }
3173
3174 return 0;
3175}
3176
Markus Armbruster3949e592013-02-06 21:27:24 +01003177static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003178{
Markus Armbruster3949e592013-02-06 21:27:24 +01003179 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003180 int i;
3181
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003182 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003183 for (i = 0; i < len && d->cons != d->prod; i++) {
3184 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003185 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003186 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003187
3188 return i;
3189}
3190
Markus Armbruster3949e592013-02-06 21:27:24 +01003191static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003192{
Markus Armbruster3949e592013-02-06 21:27:24 +01003193 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003194
3195 g_free(d->cbuf);
3196 g_free(d);
3197 chr->opaque = NULL;
3198}
3199
Paolo Bonzini479f09a2015-09-29 15:51:14 +02003200static CharDriverState *qemu_chr_open_ringbuf(const char *id,
3201 ChardevBackend *backend,
3202 ChardevReturn *ret,
Markus Armbruster4f573782013-07-26 16:44:32 +02003203 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003204{
Eric Blake130257d2015-10-26 16:34:57 -06003205 ChardevRingbuf *opts = backend->u.ringbuf;
Lei Li51767e72013-01-25 00:03:19 +08003206 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003207 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003208
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003209 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003210 d = g_malloc(sizeof(*d));
3211
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003212 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003213
3214 /* The size must be power of 2 */
3215 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003216 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003217 goto fail;
3218 }
3219
3220 d->prod = 0;
3221 d->cons = 0;
3222 d->cbuf = g_malloc0(d->size);
3223
3224 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003225 chr->chr_write = ringbuf_chr_write;
3226 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003227
3228 return chr;
3229
3230fail:
3231 g_free(d);
3232 g_free(chr);
3233 return NULL;
3234}
3235
Hani Benhabiles8e597772014-05-27 23:39:30 +01003236bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003237{
Markus Armbruster3949e592013-02-06 21:27:24 +01003238 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003239}
3240
Markus Armbruster3949e592013-02-06 21:27:24 +01003241void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003242 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003243 Error **errp)
3244{
3245 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003246 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003247 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003248 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003249
3250 chr = qemu_chr_find(device);
3251 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003252 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003253 return;
3254 }
3255
Markus Armbruster3949e592013-02-06 21:27:24 +01003256 if (!chr_is_ringbuf(chr)) {
3257 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003258 return;
3259 }
3260
Lei Li1f590cf2013-01-25 00:03:20 +08003261 if (has_format && (format == DATA_FORMAT_BASE64)) {
3262 write_data = g_base64_decode(data, &write_count);
3263 } else {
3264 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003265 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003266 }
3267
Markus Armbruster3949e592013-02-06 21:27:24 +01003268 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003269
Markus Armbruster13289fb2013-02-06 21:27:18 +01003270 if (write_data != (uint8_t *)data) {
3271 g_free((void *)write_data);
3272 }
3273
Lei Li1f590cf2013-01-25 00:03:20 +08003274 if (ret < 0) {
3275 error_setg(errp, "Failed to write to device %s", device);
3276 return;
3277 }
3278}
3279
Markus Armbruster3949e592013-02-06 21:27:24 +01003280char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003281 bool has_format, enum DataFormat format,
3282 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003283{
3284 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003285 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003286 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003287 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003288
3289 chr = qemu_chr_find(device);
3290 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003291 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003292 return NULL;
3293 }
3294
Markus Armbruster3949e592013-02-06 21:27:24 +01003295 if (!chr_is_ringbuf(chr)) {
3296 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003297 return NULL;
3298 }
3299
3300 if (size <= 0) {
3301 error_setg(errp, "size must be greater than zero");
3302 return NULL;
3303 }
3304
Markus Armbruster3949e592013-02-06 21:27:24 +01003305 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003306 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003307 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003308
Markus Armbruster3949e592013-02-06 21:27:24 +01003309 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003310
3311 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003312 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003313 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003314 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003315 /*
3316 * FIXME should read only complete, valid UTF-8 characters up
3317 * to @size bytes. Invalid sequences should be replaced by a
3318 * suitable replacement character. Except when (and only
3319 * when) ring buffer lost characters since last read, initial
3320 * continuation characters should be dropped.
3321 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003322 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003323 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003324 }
3325
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003326 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003327}
3328
Gerd Hoffmann33521632009-12-08 13:11:49 +01003329QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003330{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003331 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003332 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003333 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003334 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003335 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003336
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003337 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003338 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003339 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003340 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003341 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003342
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003343 if (strstart(filename, "mon:", &p)) {
3344 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003345 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003346 if (strcmp(filename, "stdio") == 0) {
3347 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3348 * but pass it to the guest. Handle this only for compat syntax,
3349 * for -chardev syntax we have special option for this.
3350 * This is what -nographic did, redirecting+muxing serial+monitor
3351 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003352 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003353 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003354 }
3355
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003356 if (strcmp(filename, "null") == 0 ||
3357 strcmp(filename, "pty") == 0 ||
3358 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003359 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003360 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003361 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003362 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003363 return opts;
3364 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003365 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003366 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003367 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003368 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003369 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003370 qemu_opt_set(opts, "width", width, &error_abort);
3371 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003372 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003373 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003374 qemu_opt_set(opts, "cols", width, &error_abort);
3375 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003376 } else {
3377 goto fail;
3378 }
3379 }
3380 return opts;
3381 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003382 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003383 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003384 return opts;
3385 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003386 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003387 qemu_opt_set(opts, "backend", "serial", &error_abort);
3388 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003389 return opts;
3390 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003391 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003392 qemu_opt_set(opts, "backend", "file", &error_abort);
3393 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003394 return opts;
3395 }
3396 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003397 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3398 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003399 return opts;
3400 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003401 if (strstart(filename, "tcp:", &p) ||
3402 strstart(filename, "telnet:", &p)) {
3403 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3404 host[0] = 0;
3405 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3406 goto fail;
3407 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003408 qemu_opt_set(opts, "backend", "socket", &error_abort);
3409 qemu_opt_set(opts, "host", host, &error_abort);
3410 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003411 if (p[pos] == ',') {
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003412 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3413 if (local_err) {
3414 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003415 goto fail;
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003416 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003417 }
3418 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003419 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003420 return opts;
3421 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003422 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003423 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003424 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3425 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003426 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003427 goto fail;
3428 }
3429 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003430 qemu_opt_set(opts, "host", host, &error_abort);
3431 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003432 if (p[pos] == '@') {
3433 p += pos + 1;
3434 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3435 host[0] = 0;
3436 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003437 goto fail;
3438 }
3439 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003440 qemu_opt_set(opts, "localaddr", host, &error_abort);
3441 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003442 }
3443 return opts;
3444 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003445 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003446 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003447 qemu_opts_do_parse(opts, p, "path", &local_err);
3448 if (local_err) {
3449 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003450 goto fail;
Markus Armbrusterdc523cd2015-02-12 18:37:11 +01003451 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003452 return opts;
3453 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003454 if (strstart(filename, "/dev/parport", NULL) ||
3455 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003456 qemu_opt_set(opts, "backend", "parport", &error_abort);
3457 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003458 return opts;
3459 }
3460 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003461 qemu_opt_set(opts, "backend", "tty", &error_abort);
3462 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003463 return opts;
3464 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003465
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003466fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003467 qemu_opts_del(opts);
3468 return NULL;
3469}
3470
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003471static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3472 Error **errp)
3473{
3474 const char *path = qemu_opt_get(opts, "path");
3475
3476 if (path == NULL) {
3477 error_setg(errp, "chardev: file: no filename given");
3478 return;
3479 }
Eric Blake130257d2015-10-26 16:34:57 -06003480 backend->u.file = g_new0(ChardevFile, 1);
3481 backend->u.file->out = g_strdup(path);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003482}
3483
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003484static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3485 Error **errp)
3486{
Eric Blake130257d2015-10-26 16:34:57 -06003487 backend->u.stdio = g_new0(ChardevStdio, 1);
3488 backend->u.stdio->has_signal = true;
3489 backend->u.stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003490}
3491
Paolo Bonzini6511d392015-09-29 15:08:05 +02003492#ifdef HAVE_CHARDEV_SERIAL
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003493static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3494 Error **errp)
3495{
3496 const char *device = qemu_opt_get(opts, "path");
3497
3498 if (device == NULL) {
3499 error_setg(errp, "chardev: serial/tty: no device path given");
3500 return;
3501 }
Eric Blake130257d2015-10-26 16:34:57 -06003502 backend->u.serial = g_new0(ChardevHostdev, 1);
3503 backend->u.serial->device = g_strdup(device);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003504}
Paolo Bonzini6511d392015-09-29 15:08:05 +02003505#endif
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003506
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003507#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003508static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
3509 Error **errp)
3510{
3511 const char *device = qemu_opt_get(opts, "path");
3512
3513 if (device == NULL) {
3514 error_setg(errp, "chardev: parallel: no device path given");
3515 return;
3516 }
Eric Blake130257d2015-10-26 16:34:57 -06003517 backend->u.parallel = g_new0(ChardevHostdev, 1);
3518 backend->u.parallel->device = g_strdup(device);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003519}
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003520#endif
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003521
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003522static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3523 Error **errp)
3524{
3525 const char *device = qemu_opt_get(opts, "path");
3526
3527 if (device == NULL) {
3528 error_setg(errp, "chardev: pipe: no device path given");
3529 return;
3530 }
Eric Blake130257d2015-10-26 16:34:57 -06003531 backend->u.pipe = g_new0(ChardevHostdev, 1);
3532 backend->u.pipe->device = g_strdup(device);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003533}
3534
Markus Armbruster4f573782013-07-26 16:44:32 +02003535static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3536 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003537{
3538 int val;
3539
Eric Blake130257d2015-10-26 16:34:57 -06003540 backend->u.ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003541
Markus Armbruster0f953052013-06-27 16:22:07 +02003542 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003543 if (val != 0) {
Eric Blake130257d2015-10-26 16:34:57 -06003544 backend->u.ringbuf->has_size = true;
3545 backend->u.ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003546 }
3547}
3548
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003549static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3550 Error **errp)
3551{
3552 const char *chardev = qemu_opt_get(opts, "chardev");
3553
3554 if (chardev == NULL) {
3555 error_setg(errp, "chardev: mux: no chardev given");
3556 return;
3557 }
Eric Blake130257d2015-10-26 16:34:57 -06003558 backend->u.mux = g_new0(ChardevMux, 1);
3559 backend->u.mux->chardev = g_strdup(chardev);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003560}
3561
Peter Maydelldafd3252014-09-02 11:24:13 +01003562static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3563 Error **errp)
3564{
3565 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3566 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3567 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3568 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003569 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003570 const char *path = qemu_opt_get(opts, "path");
3571 const char *host = qemu_opt_get(opts, "host");
3572 const char *port = qemu_opt_get(opts, "port");
3573 SocketAddress *addr;
3574
3575 if (!path) {
3576 if (!host) {
3577 error_setg(errp, "chardev: socket: no host given");
3578 return;
3579 }
3580 if (!port) {
3581 error_setg(errp, "chardev: socket: no port given");
3582 return;
3583 }
3584 }
3585
Eric Blake130257d2015-10-26 16:34:57 -06003586 backend->u.socket = g_new0(ChardevSocket, 1);
Peter Maydelldafd3252014-09-02 11:24:13 +01003587
Eric Blake130257d2015-10-26 16:34:57 -06003588 backend->u.socket->has_nodelay = true;
3589 backend->u.socket->nodelay = do_nodelay;
3590 backend->u.socket->has_server = true;
3591 backend->u.socket->server = is_listen;
3592 backend->u.socket->has_telnet = true;
3593 backend->u.socket->telnet = is_telnet;
3594 backend->u.socket->has_wait = true;
3595 backend->u.socket->wait = is_waitconnect;
3596 backend->u.socket->has_reconnect = true;
3597 backend->u.socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003598
3599 addr = g_new0(SocketAddress, 1);
3600 if (path) {
Eric Blake130257d2015-10-26 16:34:57 -06003601 addr->type = SOCKET_ADDRESS_KIND_UNIX;
3602 addr->u.q_unix = g_new0(UnixSocketAddress, 1);
3603 addr->u.q_unix->path = g_strdup(path);
Peter Maydelldafd3252014-09-02 11:24:13 +01003604 } else {
Eric Blake130257d2015-10-26 16:34:57 -06003605 addr->type = SOCKET_ADDRESS_KIND_INET;
3606 addr->u.inet = g_new0(InetSocketAddress, 1);
3607 addr->u.inet->host = g_strdup(host);
3608 addr->u.inet->port = g_strdup(port);
3609 addr->u.inet->has_to = qemu_opt_get(opts, "to");
3610 addr->u.inet->to = qemu_opt_get_number(opts, "to", 0);
3611 addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3612 addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3613 addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3614 addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003615 }
Eric Blake130257d2015-10-26 16:34:57 -06003616 backend->u.socket->addr = addr;
Peter Maydelldafd3252014-09-02 11:24:13 +01003617}
3618
Peter Maydell90a14bf2014-09-02 11:24:15 +01003619static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3620 Error **errp)
3621{
3622 const char *host = qemu_opt_get(opts, "host");
3623 const char *port = qemu_opt_get(opts, "port");
3624 const char *localaddr = qemu_opt_get(opts, "localaddr");
3625 const char *localport = qemu_opt_get(opts, "localport");
3626 bool has_local = false;
3627 SocketAddress *addr;
3628
3629 if (host == NULL || strlen(host) == 0) {
3630 host = "localhost";
3631 }
3632 if (port == NULL || strlen(port) == 0) {
3633 error_setg(errp, "chardev: udp: remote port not specified");
3634 return;
3635 }
3636 if (localport == NULL || strlen(localport) == 0) {
3637 localport = "0";
3638 } else {
3639 has_local = true;
3640 }
3641 if (localaddr == NULL || strlen(localaddr) == 0) {
3642 localaddr = "";
3643 } else {
3644 has_local = true;
3645 }
3646
Eric Blake130257d2015-10-26 16:34:57 -06003647 backend->u.udp = g_new0(ChardevUdp, 1);
Peter Maydell90a14bf2014-09-02 11:24:15 +01003648
3649 addr = g_new0(SocketAddress, 1);
Eric Blake130257d2015-10-26 16:34:57 -06003650 addr->type = SOCKET_ADDRESS_KIND_INET;
3651 addr->u.inet = g_new0(InetSocketAddress, 1);
3652 addr->u.inet->host = g_strdup(host);
3653 addr->u.inet->port = g_strdup(port);
3654 addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3655 addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3656 addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3657 addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3658 backend->u.udp->remote = addr;
Peter Maydell90a14bf2014-09-02 11:24:15 +01003659
3660 if (has_local) {
Eric Blake130257d2015-10-26 16:34:57 -06003661 backend->u.udp->has_local = true;
Peter Maydell90a14bf2014-09-02 11:24:15 +01003662 addr = g_new0(SocketAddress, 1);
Eric Blake130257d2015-10-26 16:34:57 -06003663 addr->type = SOCKET_ADDRESS_KIND_INET;
3664 addr->u.inet = g_new0(InetSocketAddress, 1);
3665 addr->u.inet->host = g_strdup(localaddr);
3666 addr->u.inet->port = g_strdup(localport);
3667 backend->u.udp->local = addr;
Peter Maydell90a14bf2014-09-02 11:24:15 +01003668 }
3669}
3670
Anthony Liguorid654f342013-03-05 23:21:28 +05303671typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003672 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003673 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003674 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003675 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3676 ChardevReturn *ret, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303677} CharDriver;
3678
3679static GSList *backends;
3680
Peter Maydelle4d50d42014-09-02 11:24:17 +01003681void register_char_driver(const char *name, ChardevBackendKind kind,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003682 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp),
3683 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3684 ChardevReturn *ret, Error **errp))
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003685{
3686 CharDriver *s;
3687
3688 s = g_malloc0(sizeof(*s));
3689 s->name = g_strdup(name);
3690 s->kind = kind;
3691 s->parse = parse;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003692 s->create = create;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003693
3694 backends = g_slist_append(backends, s);
3695}
3696
Anthony Liguorif69554b2011-08-15 11:17:37 -05003697CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003698 void (*init)(struct CharDriverState *s),
3699 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003700{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003701 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303702 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003703 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303704 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003705 ChardevReturn *ret = NULL;
3706 ChardevBackend *backend;
3707 const char *id = qemu_opts_id(opts);
3708 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003709
Peter Maydella61ae7f2014-09-02 11:24:16 +01003710 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003711 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003712 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003713 }
3714
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003715 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003716 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003717 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003718 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003719 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303720 for (i = backends; i; i = i->next) {
3721 cd = i->data;
3722
3723 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003724 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303725 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003726 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303727 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003728 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003729 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003730 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003731 }
3732
Peter Maydella61ae7f2014-09-02 11:24:16 +01003733 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003734
3735 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003736 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003737 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003738
3739 chr = NULL;
Eric Blake130257d2015-10-26 16:34:57 -06003740 backend->type = cd->kind;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003741 if (cd->parse) {
3742 cd->parse(opts, backend, &local_err);
3743 if (local_err) {
3744 error_propagate(errp, local_err);
3745 goto qapi_out;
3746 }
3747 }
3748 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3749 if (!ret) {
3750 goto qapi_out;
3751 }
3752
3753 if (bid) {
3754 qapi_free_ChardevBackend(backend);
3755 qapi_free_ChardevReturn(ret);
3756 backend = g_new0(ChardevBackend, 1);
Eric Blake130257d2015-10-26 16:34:57 -06003757 backend->u.mux = g_new0(ChardevMux, 1);
3758 backend->type = CHARDEV_BACKEND_KIND_MUX;
3759 backend->u.mux->chardev = g_strdup(bid);
Peter Maydella61ae7f2014-09-02 11:24:16 +01003760 ret = qmp_chardev_add(id, backend, errp);
3761 if (!ret) {
3762 chr = qemu_chr_find(bid);
3763 qemu_chr_delete(chr);
3764 chr = NULL;
3765 goto qapi_out;
3766 }
3767 }
3768
3769 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003770 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003771
3772qapi_out:
3773 qapi_free_ChardevBackend(backend);
3774 qapi_free_ChardevReturn(ret);
3775 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003776 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003777
3778err:
3779 qemu_opts_del(opts);
3780 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003781}
3782
Anthony Liguori27143a42011-08-15 11:17:36 -05003783CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003784{
3785 const char *p;
3786 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003787 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003788 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003789
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003790 if (strstart(filename, "chardev:", &p)) {
3791 return qemu_chr_find(p);
3792 }
3793
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003794 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003795 if (!opts)
3796 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003797
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003798 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003799 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003800 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003801 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003802 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003803 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003804 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003805 }
3806 return chr;
3807}
3808
Anthony Liguori15f31512011-08-15 11:17:35 -05003809void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003810{
3811 if (chr->chr_set_echo) {
3812 chr->chr_set_echo(chr, echo);
3813 }
3814}
3815
Hans de Goede8e25daa2013-03-26 11:07:57 +01003816void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003817{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003818 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003819 return;
3820 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003821 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003822 if (chr->chr_set_fe_open) {
3823 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003824 }
3825}
3826
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003827void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3828{
3829 if (chr->chr_fe_event) {
3830 chr->chr_fe_event(chr, event);
3831 }
3832}
3833
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003834int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3835 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303836{
3837 GSource *src;
3838 guint tag;
3839
3840 if (s->chr_add_watch == NULL) {
3841 return -ENOSYS;
3842 }
3843
3844 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003845 if (!src) {
3846 return -EINVAL;
3847 }
3848
Anthony Liguori23673ca2013-03-05 23:21:23 +05303849 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3850 tag = g_source_attach(src, NULL);
3851 g_source_unref(src);
3852
3853 return tag;
3854}
3855
Hans de Goede44c473d2013-03-27 20:29:39 +01003856int qemu_chr_fe_claim(CharDriverState *s)
3857{
3858 if (s->avail_connections < 1) {
3859 return -1;
3860 }
3861 s->avail_connections--;
3862 return 0;
3863}
3864
3865void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3866{
3867 if (qemu_chr_fe_claim(s) != 0) {
3868 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3869 __func__, s->label);
3870 exit(1);
3871 }
3872}
3873
3874void qemu_chr_fe_release(CharDriverState *s)
3875{
3876 s->avail_connections++;
3877}
3878
Marc-André Lureau1ad78ea2015-06-22 18:20:18 +02003879void qemu_chr_free(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003880{
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003881 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003882 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003883 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003884 g_free(chr->filename);
3885 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003886 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003887 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003888}
3889
Marc-André Lureau1ad78ea2015-06-22 18:20:18 +02003890void qemu_chr_delete(CharDriverState *chr)
3891{
3892 QTAILQ_REMOVE(&chardevs, chr, next);
3893 qemu_chr_free(chr);
3894}
3895
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003896ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003897{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003898 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003899 CharDriverState *chr;
3900
Blue Swirl72cf2d42009-09-12 07:36:22 +00003901 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003902 ChardevInfoList *info = g_malloc0(sizeof(*info));
3903 info->value = g_malloc0(sizeof(*info->value));
3904 info->value->label = g_strdup(chr->label);
3905 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003906 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003907
3908 info->next = chr_list;
3909 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003910 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003911
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003912 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003913}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003914
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003915ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3916{
3917 ChardevBackendInfoList *backend_list = NULL;
3918 CharDriver *c = NULL;
3919 GSList *i = NULL;
3920
3921 for (i = backends; i; i = i->next) {
3922 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3923 c = i->data;
3924 info->value = g_malloc0(sizeof(*info->value));
3925 info->value->name = g_strdup(c->name);
3926
3927 info->next = backend_list;
3928 backend_list = info;
3929 }
3930
3931 return backend_list;
3932}
3933
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003934CharDriverState *qemu_chr_find(const char *name)
3935{
3936 CharDriverState *chr;
3937
Blue Swirl72cf2d42009-09-12 07:36:22 +00003938 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003939 if (strcmp(chr->label, name) != 0)
3940 continue;
3941 return chr;
3942 }
3943 return NULL;
3944}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003945
3946/* Get a character (serial) device interface. */
3947CharDriverState *qemu_char_get_next_serial(void)
3948{
3949 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003950 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003951
3952 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003953
3954 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3955 chr = serial_hds[next_serial++];
3956 qemu_chr_fe_claim_no_fail(chr);
3957 return chr;
3958 }
3959 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003960}
3961
Paolo Bonzini4d454572012-11-26 16:03:42 +01003962QemuOptsList qemu_chardev_opts = {
3963 .name = "chardev",
3964 .implied_opt_name = "backend",
3965 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3966 .desc = {
3967 {
3968 .name = "backend",
3969 .type = QEMU_OPT_STRING,
3970 },{
3971 .name = "path",
3972 .type = QEMU_OPT_STRING,
3973 },{
3974 .name = "host",
3975 .type = QEMU_OPT_STRING,
3976 },{
3977 .name = "port",
3978 .type = QEMU_OPT_STRING,
3979 },{
3980 .name = "localaddr",
3981 .type = QEMU_OPT_STRING,
3982 },{
3983 .name = "localport",
3984 .type = QEMU_OPT_STRING,
3985 },{
3986 .name = "to",
3987 .type = QEMU_OPT_NUMBER,
3988 },{
3989 .name = "ipv4",
3990 .type = QEMU_OPT_BOOL,
3991 },{
3992 .name = "ipv6",
3993 .type = QEMU_OPT_BOOL,
3994 },{
3995 .name = "wait",
3996 .type = QEMU_OPT_BOOL,
3997 },{
3998 .name = "server",
3999 .type = QEMU_OPT_BOOL,
4000 },{
4001 .name = "delay",
4002 .type = QEMU_OPT_BOOL,
4003 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05004004 .name = "reconnect",
4005 .type = QEMU_OPT_NUMBER,
4006 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004007 .name = "telnet",
4008 .type = QEMU_OPT_BOOL,
4009 },{
4010 .name = "width",
4011 .type = QEMU_OPT_NUMBER,
4012 },{
4013 .name = "height",
4014 .type = QEMU_OPT_NUMBER,
4015 },{
4016 .name = "cols",
4017 .type = QEMU_OPT_NUMBER,
4018 },{
4019 .name = "rows",
4020 .type = QEMU_OPT_NUMBER,
4021 },{
4022 .name = "mux",
4023 .type = QEMU_OPT_BOOL,
4024 },{
4025 .name = "signal",
4026 .type = QEMU_OPT_BOOL,
4027 },{
4028 .name = "name",
4029 .type = QEMU_OPT_STRING,
4030 },{
4031 .name = "debug",
4032 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08004033 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01004034 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01004035 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004036 },{
4037 .name = "chardev",
4038 .type = QEMU_OPT_STRING,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004039 },
4040 { /* end of list */ }
4041 },
4042};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004043
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004044#ifdef _WIN32
4045
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004046static CharDriverState *qmp_chardev_open_file(const char *id,
4047 ChardevBackend *backend,
4048 ChardevReturn *ret,
4049 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004050{
Eric Blake130257d2015-10-26 16:34:57 -06004051 ChardevFile *file = backend->u.file;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004052 HANDLE out;
4053
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004054 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004055 error_setg(errp, "input file not supported");
4056 return NULL;
4057 }
4058
4059 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4060 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4061 if (out == INVALID_HANDLE_VALUE) {
4062 error_setg(errp, "open %s failed", file->out);
4063 return NULL;
4064 }
4065 return qemu_chr_open_win_file(out);
4066}
4067
Paolo Bonzini6511d392015-09-29 15:08:05 +02004068static CharDriverState *qmp_chardev_open_serial(const char *id,
4069 ChardevBackend *backend,
4070 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004071 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004072{
Eric Blake130257d2015-10-26 16:34:57 -06004073 ChardevHostdev *serial = backend->u.serial;
Paolo Bonzini6511d392015-09-29 15:08:05 +02004074 return qemu_chr_open_win_path(serial->device, errp);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004075}
4076
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004077#else /* WIN32 */
4078
4079static int qmp_chardev_open_file_source(char *src, int flags,
4080 Error **errp)
4081{
4082 int fd = -1;
4083
4084 TFR(fd = qemu_open(src, flags, 0666));
4085 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004086 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004087 }
4088 return fd;
4089}
4090
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004091static CharDriverState *qmp_chardev_open_file(const char *id,
4092 ChardevBackend *backend,
4093 ChardevReturn *ret,
4094 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004095{
Eric Blake130257d2015-10-26 16:34:57 -06004096 ChardevFile *file = backend->u.file;
Markus Armbruster5f758362014-05-19 18:57:34 +02004097 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004098
4099 flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
4100 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004101 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004102 return NULL;
4103 }
4104
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004105 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004106 flags = O_RDONLY;
4107 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004108 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004109 qemu_close(out);
4110 return NULL;
4111 }
4112 }
4113
4114 return qemu_chr_open_fd(in, out);
4115}
4116
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004117#ifdef HAVE_CHARDEV_SERIAL
Paolo Bonzini6511d392015-09-29 15:08:05 +02004118static CharDriverState *qmp_chardev_open_serial(const char *id,
4119 ChardevBackend *backend,
4120 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004121 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004122{
Eric Blake130257d2015-10-26 16:34:57 -06004123 ChardevHostdev *serial = backend->u.serial;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004124 int fd;
4125
4126 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004127 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004128 return NULL;
4129 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004130 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004131 return qemu_chr_open_tty_fd(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004132}
Paolo Bonzini6511d392015-09-29 15:08:05 +02004133#endif
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004134
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004135#ifdef HAVE_CHARDEV_PARPORT
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004136static CharDriverState *qmp_chardev_open_parallel(const char *id,
4137 ChardevBackend *backend,
4138 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004139 Error **errp)
4140{
Eric Blake130257d2015-10-26 16:34:57 -06004141 ChardevHostdev *parallel = backend->u.parallel;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004142 int fd;
4143
4144 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004145 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004146 return NULL;
4147 }
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004148 return qemu_chr_open_pp_fd(fd, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004149}
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004150#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004151
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004152#endif /* WIN32 */
4153
Corey Minyard5008e5b2014-10-08 07:11:55 -05004154static void socket_try_connect(CharDriverState *chr)
4155{
4156 Error *err = NULL;
4157
4158 if (!qemu_chr_open_socket_fd(chr, &err)) {
4159 check_report_connect_error(chr, err);
4160 }
4161}
4162
Corey Minyard5dd1f022014-10-02 11:17:37 -05004163static gboolean socket_reconnect_timeout(gpointer opaque)
4164{
4165 CharDriverState *chr = opaque;
4166 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004167
4168 s->reconnect_timer = 0;
4169
4170 if (chr->be_open) {
4171 return false;
4172 }
4173
Corey Minyard5008e5b2014-10-08 07:11:55 -05004174 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004175
4176 return false;
4177}
4178
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004179static CharDriverState *qmp_chardev_open_socket(const char *id,
4180 ChardevBackend *backend,
4181 ChardevReturn *ret,
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004182 Error **errp)
4183{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004184 CharDriverState *chr;
4185 TCPCharDriver *s;
Eric Blake130257d2015-10-26 16:34:57 -06004186 ChardevSocket *sock = backend->u.socket;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004187 SocketAddress *addr = sock->addr;
4188 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4189 bool is_listen = sock->has_server ? sock->server : true;
4190 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4191 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004192 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004193
4194 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02004195 s = g_new0(TCPCharDriver, 1);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004196
4197 s->fd = -1;
4198 s->listen_fd = -1;
Eric Blake130257d2015-10-26 16:34:57 -06004199 s->is_unix = addr->type == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004200 s->is_listen = is_listen;
4201 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004202 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004203 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004204
4205 chr->opaque = s;
4206 chr->chr_write = tcp_chr_write;
4207 chr->chr_sync_read = tcp_chr_sync_read;
4208 chr->chr_close = tcp_chr_close;
4209 chr->get_msgfds = tcp_get_msgfds;
4210 chr->set_msgfds = tcp_set_msgfds;
4211 chr->chr_add_client = tcp_chr_add_client;
4212 chr->chr_add_watch = tcp_chr_add_watch;
4213 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4214 /* be isn't opened until we get a connection */
4215 chr->explicit_be_open = true;
4216
4217 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004218 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4219 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004220
4221 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004222 if (is_telnet) {
4223 s->do_telnetopt = 1;
4224 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004225 } else if (reconnect > 0) {
4226 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004227 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004228
Corey Minyard5008e5b2014-10-08 07:11:55 -05004229 if (s->reconnect_time) {
4230 socket_try_connect(chr);
4231 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4232 g_free(s);
4233 g_free(chr->filename);
4234 g_free(chr);
4235 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004236 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004237
4238 if (is_listen && is_waitconnect) {
4239 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4240 chr->filename);
4241 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4242 qemu_set_nonblock(s->listen_fd);
4243 }
4244
4245 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004246}
4247
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004248static CharDriverState *qmp_chardev_open_udp(const char *id,
4249 ChardevBackend *backend,
4250 ChardevReturn *ret,
Lei Li08d0ab32013-05-20 14:51:03 +08004251 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004252{
Eric Blake130257d2015-10-26 16:34:57 -06004253 ChardevUdp *udp = backend->u.udp;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004254 int fd;
4255
Lei Li08d0ab32013-05-20 14:51:03 +08004256 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004257 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004258 return NULL;
4259 }
4260 return qemu_chr_open_udp_fd(fd);
4261}
4262
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004263ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4264 Error **errp)
4265{
4266 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004267 CharDriverState *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004268 Error *local_err = NULL;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004269 GSList *i;
4270 CharDriver *cd;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004271
4272 chr = qemu_chr_find(id);
4273 if (chr) {
4274 error_setg(errp, "Chardev '%s' already exists", id);
4275 g_free(ret);
4276 return NULL;
4277 }
4278
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004279 for (i = backends; i; i = i->next) {
4280 cd = i->data;
4281
Eric Blake130257d2015-10-26 16:34:57 -06004282 if (cd->kind == backend->type) {
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004283 chr = cd->create(id, backend, ret, &local_err);
4284 if (local_err) {
4285 error_propagate(errp, local_err);
4286 goto out_error;
4287 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004288 break;
4289 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004290 }
4291
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004292 if (chr == NULL) {
Paolo Bonzini1c3af0f2015-10-12 09:51:41 +02004293 assert(!i);
4294 error_setg(errp, "chardev backend not available");
4295 goto out_error;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004296 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004297
4298 chr->label = g_strdup(id);
4299 chr->avail_connections =
Eric Blake130257d2015-10-26 16:34:57 -06004300 (backend->type == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004301 if (!chr->filename) {
Eric Blake130257d2015-10-26 16:34:57 -06004302 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->type]);
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004303 }
4304 if (!chr->explicit_be_open) {
4305 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4306 }
4307 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4308 return ret;
4309
4310out_error:
4311 g_free(ret);
4312 return NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004313}
4314
4315void qmp_chardev_remove(const char *id, Error **errp)
4316{
4317 CharDriverState *chr;
4318
4319 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004320 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004321 error_setg(errp, "Chardev '%s' not found", id);
4322 return;
4323 }
4324 if (chr->chr_can_read || chr->chr_read ||
4325 chr->chr_event || chr->handler_opaque) {
4326 error_setg(errp, "Chardev '%s' is busy", id);
4327 return;
4328 }
4329 qemu_chr_delete(chr);
4330}
Anthony Liguorid654f342013-03-05 23:21:28 +05304331
4332static void register_types(void)
4333{
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004334 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
Paolo Bonzini0d649922015-09-29 15:24:29 +02004335 qemu_chr_open_null);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004336 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004337 qemu_chr_parse_socket, qmp_chardev_open_socket);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004338 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp,
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004339 qmp_chardev_open_udp);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004340 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Paolo Bonzini479f09a2015-09-29 15:51:14 +02004341 qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004342 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004343 qemu_chr_parse_file_out, qmp_chardev_open_file);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004344 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
Paolo Bonzini8c84b252015-09-29 15:40:28 +02004345 qemu_chr_parse_stdio, qemu_chr_open_stdio);
Paolo Bonzini6511d392015-09-29 15:08:05 +02004346#if defined HAVE_CHARDEV_SERIAL
Peter Maydelle4d50d42014-09-02 11:24:17 +01004347 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004348 qemu_chr_parse_serial, qmp_chardev_open_serial);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004349 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004350 qemu_chr_parse_serial, qmp_chardev_open_serial);
4351#endif
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004352#ifdef HAVE_CHARDEV_PARPORT
Peter Maydelle4d50d42014-09-02 11:24:17 +01004353 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004354 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004355 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004356 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
4357#endif
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004358#ifdef HAVE_CHARDEV_PTY
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004359 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004360 qemu_chr_open_pty);
4361#endif
Paolo Bonzini122e5ed2015-09-29 15:42:04 +02004362#ifdef _WIN32
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004363 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL,
Paolo Bonzini122e5ed2015-09-29 15:42:04 +02004364 qemu_chr_open_win_con);
4365#endif
Peter Maydelle4d50d42014-09-02 11:24:17 +01004366 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004367 qemu_chr_parse_pipe, qemu_chr_open_pipe);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004368 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux,
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004369 qemu_chr_open_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004370 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004371 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
Paolo Bonzini479f09a2015-09-29 15:51:14 +02004372 qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004373 /* this must be done after machine init, since we register FEs with muxes
4374 * as part of realize functions like serial_isa_realizefn when -nographic
4375 * is specified
4376 */
4377 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304378}
4379
4380type_init(register_types);