blob: 00a7526761b3c7f190f4e0021a49954e2ffb4aa1 [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"
Daniel P. Berrangee9cf2fe2015-11-23 15:29:59 +000035#include "qemu/base64.h"
aliguori6f97dba2008-10-31 18:49:55 +000036
37#include <unistd.h>
38#include <fcntl.h>
aliguori6f97dba2008-10-31 18:49:55 +000039#include <time.h>
40#include <errno.h>
41#include <sys/time.h>
42#include <zlib.h>
43
44#ifndef _WIN32
45#include <sys/times.h>
46#include <sys/wait.h>
47#include <termios.h>
48#include <sys/mman.h>
49#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000050#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <sys/socket.h>
52#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000054#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000055#include <netdb.h>
56#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020057#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000058#include <sys/stat.h>
Michael Tokarev3294ce12012-06-02 23:43:33 +040059#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
60#include <dev/ppbus/ppi.h>
61#include <dev/ppbus/ppbconf.h>
62#elif defined(__DragonFly__)
63#include <dev/misc/ppi/ppi.h>
64#include <bus/ppbus/ppbconf.h>
65#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010066#else
aliguori6f97dba2008-10-31 18:49:55 +000067#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000068#include <linux/ppdev.h>
69#include <linux/parport.h>
70#endif
71#ifdef __sun__
72#include <sys/stat.h>
73#include <sys/ethernet.h>
74#include <sys/sockio.h>
75#include <netinet/arp.h>
76#include <netinet/in.h>
77#include <netinet/in_systm.h>
78#include <netinet/ip.h>
79#include <netinet/ip_icmp.h> // must come after ip.h
80#include <netinet/udp.h>
81#include <netinet/tcp.h>
aliguori6f97dba2008-10-31 18:49:55 +000082#endif
83#endif
84#endif
85
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010086#include "qemu/sockets.h"
Alon Levycbcc6332011-01-19 10:49:50 +020087#include "ui/qemu-spice.h"
aliguori6f97dba2008-10-31 18:49:55 +000088
Amit Shah9bd78542009-11-03 19:59:54 +053089#define READ_BUF_LEN 4096
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +030090#define READ_RETRIES 10
Corey Minyard9f781162014-10-02 11:17:33 -050091#define CHR_MAX_FILENAME_SIZE 256
Michael S. Tsirkinc4095722014-11-02 18:48:32 +020092#define TCP_MAX_FDS 16
Amit Shah9bd78542009-11-03 19:59:54 +053093
aliguori6f97dba2008-10-31 18:49:55 +000094/***********************************************************/
Corey Minyardcfb429c2014-10-02 11:17:35 -050095/* Socket address helpers */
Corey Minyardcfb429c2014-10-02 11:17:35 -050096
Corey Minyard16cc4ff2014-10-02 11:17:36 -050097static int SocketAddress_to_str(char *dest, int max_len,
98 const char *prefix, SocketAddress *addr,
99 bool is_listen, bool is_telnet)
100{
Eric Blake130257d2015-10-26 16:34:57 -0600101 switch (addr->type) {
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500102 case SOCKET_ADDRESS_KIND_INET:
103 return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix,
Eric Blake130257d2015-10-26 16:34:57 -0600104 is_telnet ? "telnet" : "tcp", addr->u.inet->host,
105 addr->u.inet->port, is_listen ? ",server" : "");
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500106 break;
107 case SOCKET_ADDRESS_KIND_UNIX:
108 return snprintf(dest, max_len, "%sunix:%s%s", prefix,
Eric Blake130257d2015-10-26 16:34:57 -0600109 addr->u.q_unix->path, is_listen ? ",server" : "");
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500110 break;
111 case SOCKET_ADDRESS_KIND_FD:
Eric Blake130257d2015-10-26 16:34:57 -0600112 return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->u.fd->str,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500113 is_listen ? ",server" : "");
114 break;
115 default:
116 abort();
117 }
118}
119
120static int sockaddr_to_str(char *dest, int max_len,
121 struct sockaddr_storage *ss, socklen_t ss_len,
Corey Minyard01ca5192014-10-02 11:17:38 -0500122 struct sockaddr_storage *ps, socklen_t ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500123 bool is_listen, bool is_telnet)
124{
Corey Minyard01ca5192014-10-02 11:17:38 -0500125 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
126 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500127 const char *left = "", *right = "";
128
129 switch (ss->ss_family) {
130#ifndef _WIN32
131 case AF_UNIX:
132 return snprintf(dest, max_len, "unix:%s%s",
133 ((struct sockaddr_un *)(ss))->sun_path,
134 is_listen ? ",server" : "");
135#endif
136 case AF_INET6:
137 left = "[";
138 right = "]";
139 /* fall through */
140 case AF_INET:
Corey Minyard01ca5192014-10-02 11:17:38 -0500141 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
142 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
143 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
144 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
145 return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500146 is_telnet ? "telnet" : "tcp",
Corey Minyard01ca5192014-10-02 11:17:38 -0500147 left, shost, right, sserv,
148 is_listen ? ",server" : "",
149 left, phost, right, pserv);
Corey Minyard16cc4ff2014-10-02 11:17:36 -0500150
151 default:
152 return snprintf(dest, max_len, "unknown");
153 }
154}
155
Corey Minyardcfb429c2014-10-02 11:17:35 -0500156/***********************************************************/
aliguori6f97dba2008-10-31 18:49:55 +0000157/* character device */
158
Blue Swirl72cf2d42009-09-12 07:36:22 +0000159static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
160 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000161
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200162CharDriverState *qemu_chr_alloc(void)
163{
164 CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
Paolo Bonzinif3db17b2014-06-25 09:04:57 +0200165 qemu_mutex_init(&chr->chr_write_lock);
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200166 return chr;
167}
168
Hans de Goedea425d232011-11-19 10:22:43 +0100169void qemu_chr_be_event(CharDriverState *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +0000170{
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200171 /* Keep track if the char device is open */
172 switch (event) {
173 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +0100174 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200175 break;
176 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +0100177 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200178 break;
179 }
180
aliguori6f97dba2008-10-31 18:49:55 +0000181 if (!s->chr_event)
182 return;
183 s->chr_event(s->handler_opaque, event);
184}
185
Hans de Goedefee204f2013-03-26 11:07:54 +0100186void qemu_chr_be_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000187{
Michael Rothbd5c51e2013-06-07 15:19:53 -0500188 qemu_chr_be_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000189}
190
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500191int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000192{
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200193 int ret;
194
195 qemu_mutex_lock(&s->chr_write_lock);
196 ret = s->chr_write(s, buf, len);
197 qemu_mutex_unlock(&s->chr_write_lock);
198 return ret;
aliguori6f97dba2008-10-31 18:49:55 +0000199}
200
Anthony Liguoricd187202013-03-26 10:04:17 -0500201int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
202{
203 int offset = 0;
Igor Mammedov09313042014-06-25 10:00:41 +0200204 int res = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500205
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200206 qemu_mutex_lock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500207 while (offset < len) {
208 do {
209 res = s->chr_write(s, buf + offset, len - offset);
210 if (res == -1 && errno == EAGAIN) {
211 g_usleep(100);
212 }
213 } while (res == -1 && errno == EAGAIN);
214
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200215 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500216 break;
217 }
218
Anthony Liguoricd187202013-03-26 10:04:17 -0500219 offset += res;
220 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200221 qemu_mutex_unlock(&s->chr_write_lock);
Anthony Liguoricd187202013-03-26 10:04:17 -0500222
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200223 if (res < 0) {
224 return res;
225 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500226 return offset;
227}
228
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +0300229int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
230{
231 int offset = 0, counter = 10;
232 int res;
233
234 if (!s->chr_sync_read) {
235 return 0;
236 }
237
238 while (offset < len) {
239 do {
240 res = s->chr_sync_read(s, buf + offset, len - offset);
241 if (res == -1 && errno == EAGAIN) {
242 g_usleep(100);
243 }
244 } while (res == -1 && errno == EAGAIN);
245
246 if (res == 0) {
247 break;
248 }
249
250 if (res < 0) {
251 return res;
252 }
253
254 offset += res;
255
256 if (!counter--) {
257 break;
258 }
259 }
260
261 return offset;
262}
263
Anthony Liguori41084f12011-08-15 11:17:34 -0500264int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
aliguori6f97dba2008-10-31 18:49:55 +0000265{
266 if (!s->chr_ioctl)
267 return -ENOTSUP;
268 return s->chr_ioctl(s, cmd, arg);
269}
270
Anthony Liguori909cda12011-08-15 11:17:31 -0500271int qemu_chr_be_can_write(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000272{
273 if (!s->chr_can_read)
274 return 0;
275 return s->chr_can_read(s->handler_opaque);
276}
277
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500278void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000279{
Stefan Weilac310732012-04-19 22:27:14 +0200280 if (s->chr_read) {
281 s->chr_read(s->handler_opaque, buf, len);
282 }
aliguori6f97dba2008-10-31 18:49:55 +0000283}
284
Anthony Liguori74c0d6f2011-08-15 11:17:39 -0500285int qemu_chr_fe_get_msgfd(CharDriverState *s)
Mark McLoughlin7d174052009-07-22 09:11:39 +0100286{
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300287 int fd;
Stefan Hajnoczi4f858612014-06-22 10:38:36 +0800288 return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +0300289}
290
291int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
292{
293 return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +0100294}
295
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +0300296int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
297{
298 return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
299}
300
Daniel P. Berrange13661082011-06-23 13:31:42 +0100301int qemu_chr_add_client(CharDriverState *s, int fd)
302{
303 return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
304}
305
aliguori6f97dba2008-10-31 18:49:55 +0000306void qemu_chr_accept_input(CharDriverState *s)
307{
308 if (s->chr_accept_input)
309 s->chr_accept_input(s);
Jan Kiszka98c8ee12012-03-16 13:18:00 +0100310 qemu_notify_event();
aliguori6f97dba2008-10-31 18:49:55 +0000311}
312
Anthony Liguorie7e71b02011-08-15 11:17:29 -0500313void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
aliguori6f97dba2008-10-31 18:49:55 +0000314{
Amit Shah9bd78542009-11-03 19:59:54 +0530315 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000316 va_list ap;
317 va_start(ap, fmt);
318 vsnprintf(buf, sizeof(buf), fmt, ap);
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500319 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
aliguori6f97dba2008-10-31 18:49:55 +0000320 va_end(ap);
321}
322
Amit Shah386a5a12013-08-28 15:24:05 +0530323static void remove_fd_in_watch(CharDriverState *chr);
324
aliguori6f97dba2008-10-31 18:49:55 +0000325void qemu_chr_add_handlers(CharDriverState *s,
Juan Quintela7b27a762010-03-11 17:55:39 +0100326 IOCanReadHandler *fd_can_read,
aliguori6f97dba2008-10-31 18:49:55 +0000327 IOReadHandler *fd_read,
328 IOEventHandler *fd_event,
329 void *opaque)
330{
Hans de Goede19083222013-03-26 11:07:56 +0100331 int fe_open;
332
Amit Shahda7d9982011-04-25 15:18:22 +0530333 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
Hans de Goede19083222013-03-26 11:07:56 +0100334 fe_open = 0;
Amit Shah386a5a12013-08-28 15:24:05 +0530335 remove_fd_in_watch(s);
Hans de Goede19083222013-03-26 11:07:56 +0100336 } else {
337 fe_open = 1;
Amit Shah2d6c1ef2011-02-10 12:55:20 +0530338 }
aliguori6f97dba2008-10-31 18:49:55 +0000339 s->chr_can_read = fd_can_read;
340 s->chr_read = fd_read;
341 s->chr_event = fd_event;
342 s->handler_opaque = opaque;
Gal Hammerac1b84d2014-02-25 12:12:35 +0200343 if (fe_open && s->chr_update_read_handler)
aliguori6f97dba2008-10-31 18:49:55 +0000344 s->chr_update_read_handler(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200345
Hans de Goede19083222013-03-26 11:07:56 +0100346 if (!s->explicit_fe_open) {
Hans de Goede8e25daa2013-03-26 11:07:57 +0100347 qemu_chr_fe_set_open(s, fe_open);
Hans de Goede19083222013-03-26 11:07:56 +0100348 }
349
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200350 /* We're connecting to an already opened device, so let's make sure we
351 also get the open event */
Hans de Goedea59bcd32013-03-26 11:08:00 +0100352 if (fe_open && s->be_open) {
Hans de Goedefee204f2013-03-26 11:07:54 +0100353 qemu_chr_be_generic_open(s);
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200354 }
aliguori6f97dba2008-10-31 18:49:55 +0000355}
356
357static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
358{
359 return len;
360}
361
Paolo Bonzini0d649922015-09-29 15:24:29 +0200362static CharDriverState *qemu_chr_open_null(const char *id,
363 ChardevBackend *backend,
364 ChardevReturn *ret,
365 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000366{
367 CharDriverState *chr;
368
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200369 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +0000370 chr->chr_write = null_chr_write;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500371 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +0100372 return chr;
aliguori6f97dba2008-10-31 18:49:55 +0000373}
374
375/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000376#define MAX_MUX 4
377#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
378#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
379typedef struct {
Juan Quintela7b27a762010-03-11 17:55:39 +0100380 IOCanReadHandler *chr_can_read[MAX_MUX];
aliguori6f97dba2008-10-31 18:49:55 +0000381 IOReadHandler *chr_read[MAX_MUX];
382 IOEventHandler *chr_event[MAX_MUX];
383 void *ext_opaque[MAX_MUX];
384 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200385 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000386 int mux_cnt;
387 int term_got_escape;
388 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000389 /* Intermediate input buffer allows to catch escape sequences even if the
390 currently active device is not accepting any input - but only until it
391 is full as well. */
392 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
393 int prod[MAX_MUX];
394 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200395 int timestamps;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200396
397 /* Protected by the CharDriverState chr_write_lock. */
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200398 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200399 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000400} MuxDriver;
401
402
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200403/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000404static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
405{
406 MuxDriver *d = chr->opaque;
407 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200408 if (!d->timestamps) {
Paolo Bonzini6975b712014-06-18 08:43:56 +0200409 ret = qemu_chr_fe_write(d->drv, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +0000410 } else {
411 int i;
412
413 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200414 for (i = 0; i < len; i++) {
415 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000416 char buf1[64];
417 int64_t ti;
418 int secs;
419
Alex Blighbc72ad62013-08-21 16:03:08 +0100420 ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jan Kiszka2d229592009-06-15 22:25:30 +0200421 if (d->timestamps_start == -1)
422 d->timestamps_start = ti;
423 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000424 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000425 snprintf(buf1, sizeof(buf1),
426 "[%02d:%02d:%02d.%03d] ",
427 secs / 3600,
428 (secs / 60) % 60,
429 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000430 (int)(ti % 1000));
Paolo Bonzini6975b712014-06-18 08:43:56 +0200431 qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200432 d->linestart = 0;
433 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200434 ret += qemu_chr_fe_write(d->drv, buf+i, 1);
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200435 if (buf[i] == '\n') {
436 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000437 }
438 }
439 }
440 return ret;
441}
442
443static const char * const mux_help[] = {
444 "% h print this help\n\r",
445 "% x exit emulator\n\r",
446 "% s save disk data back to file (if -snapshot)\n\r",
Gonglei86d10322014-11-15 18:06:46 +0800447 "% t toggle console timestamps\n\r",
aliguori6f97dba2008-10-31 18:49:55 +0000448 "% b send break (magic sysrq)\n\r",
449 "% c switch between console and monitor\n\r",
450 "% % sends %\n\r",
451 NULL
452};
453
454int term_escape_char = 0x01; /* ctrl-a is used for escape */
455static void mux_print_help(CharDriverState *chr)
456{
457 int i, j;
458 char ebuf[15] = "Escape-Char";
459 char cbuf[50] = "\n\r";
460
461 if (term_escape_char > 0 && term_escape_char < 26) {
462 snprintf(cbuf, sizeof(cbuf), "\n\r");
463 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
464 } else {
465 snprintf(cbuf, sizeof(cbuf),
466 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
467 term_escape_char);
468 }
Paolo Bonzini6975b712014-06-18 08:43:56 +0200469 qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf));
aliguori6f97dba2008-10-31 18:49:55 +0000470 for (i = 0; mux_help[i] != NULL; i++) {
471 for (j=0; mux_help[i][j] != '\0'; j++) {
472 if (mux_help[i][j] == '%')
Paolo Bonzini6975b712014-06-18 08:43:56 +0200473 qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf));
aliguori6f97dba2008-10-31 18:49:55 +0000474 else
Paolo Bonzini6975b712014-06-18 08:43:56 +0200475 qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000476 }
477 }
478}
479
aliguori2724b182009-03-05 23:01:47 +0000480static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
481{
482 if (d->chr_event[mux_nr])
483 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
484}
485
aliguori6f97dba2008-10-31 18:49:55 +0000486static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
487{
488 if (d->term_got_escape) {
489 d->term_got_escape = 0;
490 if (ch == term_escape_char)
491 goto send_char;
492 switch(ch) {
493 case '?':
494 case 'h':
495 mux_print_help(chr);
496 break;
497 case 'x':
498 {
499 const char *term = "QEMU: Terminated\n\r";
Paolo Bonzini6975b712014-06-18 08:43:56 +0200500 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));
aliguori6f97dba2008-10-31 18:49:55 +0000501 exit(0);
502 break;
503 }
504 case 's':
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200505 bdrv_commit_all();
aliguori6f97dba2008-10-31 18:49:55 +0000506 break;
507 case 'b':
Hans de Goedea425d232011-11-19 10:22:43 +0100508 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +0000509 break;
510 case 'c':
511 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200512 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
513 d->focus++;
514 if (d->focus >= d->mux_cnt)
515 d->focus = 0;
516 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000517 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200518 case 't':
519 d->timestamps = !d->timestamps;
520 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200521 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200522 break;
aliguori6f97dba2008-10-31 18:49:55 +0000523 }
524 } else if (ch == term_escape_char) {
525 d->term_got_escape = 1;
526 } else {
527 send_char:
528 return 1;
529 }
530 return 0;
531}
532
533static void mux_chr_accept_input(CharDriverState *chr)
534{
aliguori6f97dba2008-10-31 18:49:55 +0000535 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200536 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000537
aliguoria80bf992009-03-05 23:00:02 +0000538 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000539 d->chr_can_read[m] &&
540 d->chr_can_read[m](d->ext_opaque[m])) {
541 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000542 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000543 }
544}
545
546static int mux_chr_can_read(void *opaque)
547{
548 CharDriverState *chr = opaque;
549 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200550 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000551
aliguoria80bf992009-03-05 23:00:02 +0000552 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000553 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000554 if (d->chr_can_read[m])
555 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000556 return 0;
557}
558
559static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
560{
561 CharDriverState *chr = opaque;
562 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200563 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000564 int i;
565
566 mux_chr_accept_input (opaque);
567
568 for(i = 0; i < size; i++)
569 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000570 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000571 d->chr_can_read[m] &&
572 d->chr_can_read[m](d->ext_opaque[m]))
573 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
574 else
aliguoria80bf992009-03-05 23:00:02 +0000575 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000576 }
577}
578
579static void mux_chr_event(void *opaque, int event)
580{
581 CharDriverState *chr = opaque;
582 MuxDriver *d = chr->opaque;
583 int i;
584
585 /* Send the event to all registered listeners */
586 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000587 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000588}
589
590static void mux_chr_update_read_handler(CharDriverState *chr)
591{
592 MuxDriver *d = chr->opaque;
593
594 if (d->mux_cnt >= MAX_MUX) {
595 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
596 return;
597 }
598 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
599 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
600 d->chr_read[d->mux_cnt] = chr->chr_read;
601 d->chr_event[d->mux_cnt] = chr->chr_event;
602 /* Fix up the real driver with mux routines */
603 if (d->mux_cnt == 0) {
604 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
605 mux_chr_event, chr);
606 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200607 if (d->focus != -1) {
608 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200609 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200610 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000611 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200612 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000613}
614
Michael Roth7b7ab182013-07-30 13:04:22 -0500615static bool muxes_realized;
616
617/**
618 * Called after processing of default and command-line-specified
619 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
620 * to a mux chardev. This is done here to ensure that
621 * output/prompts/banners are only displayed for the FE that has
622 * focus when initial command-line processing/machine init is
623 * completed.
624 *
625 * After this point, any new FE attached to any new or existing
626 * mux will receive CHR_EVENT_OPENED notifications for the BE
627 * immediately.
628 */
629static void muxes_realize_done(Notifier *notifier, void *unused)
630{
631 CharDriverState *chr;
632
633 QTAILQ_FOREACH(chr, &chardevs, next) {
634 if (chr->is_mux) {
635 MuxDriver *d = chr->opaque;
636 int i;
637
638 /* send OPENED to all already-attached FEs */
639 for (i = 0; i < d->mux_cnt; i++) {
640 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
641 }
642 /* mark mux as OPENED so any new FEs will immediately receive
643 * OPENED event
644 */
645 qemu_chr_be_generic_open(chr);
646 }
647 }
648 muxes_realized = true;
649}
650
651static Notifier muxes_realize_notify = {
652 .notify = muxes_realize_done,
653};
654
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400655static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
656{
657 MuxDriver *d = s->opaque;
658 return d->drv->chr_add_watch(d->drv, cond);
659}
660
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200661static CharDriverState *qemu_chr_open_mux(const char *id,
662 ChardevBackend *backend,
663 ChardevReturn *ret, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000664{
Eric Blake130257d2015-10-26 16:34:57 -0600665 ChardevMux *mux = backend->u.mux;
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200666 CharDriverState *chr, *drv;
aliguori6f97dba2008-10-31 18:49:55 +0000667 MuxDriver *d;
668
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +0200669 drv = qemu_chr_find(mux->chardev);
670 if (drv == NULL) {
671 error_setg(errp, "mux: base chardev %s not found", mux->chardev);
672 return NULL;
673 }
674
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200675 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +0200676 d = g_new0(MuxDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +0000677
678 chr->opaque = d;
679 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200680 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000681 chr->chr_write = mux_chr_write;
682 chr->chr_update_read_handler = mux_chr_update_read_handler;
683 chr->chr_accept_input = mux_chr_accept_input;
Hans de Goede7c32c4f2011-03-24 11:12:02 +0100684 /* Frontend guest-open / -close notification is not support with muxes */
Hans de Goede574b7112013-03-26 11:07:58 +0100685 chr->chr_set_fe_open = NULL;
Kirill Batuzov3f0838a2014-07-04 16:43:15 +0400686 if (drv->chr_add_watch) {
687 chr->chr_add_watch = mux_chr_add_watch;
688 }
Michael Roth7b7ab182013-07-30 13:04:22 -0500689 /* only default to opened state if we've realized the initial
690 * set of muxes
691 */
692 chr->explicit_be_open = muxes_realized ? 0 : 1;
693 chr->is_mux = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +0200694
aliguori6f97dba2008-10-31 18:49:55 +0000695 return chr;
696}
697
698
699#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000700int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000701{
702 int ret, len;
703
704 len = len1;
705 while (len > 0) {
706 ret = send(fd, buf, len, 0);
707 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000708 errno = WSAGetLastError();
709 if (errno != WSAEWOULDBLOCK) {
710 return -1;
711 }
712 } else if (ret == 0) {
713 break;
714 } else {
715 buf += ret;
716 len -= ret;
717 }
718 }
719 return len1 - len;
720}
721
722#else
723
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100724int send_all(int fd, const void *_buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000725{
726 int ret, len;
Jes Sorensen5fc9cfe2010-11-01 20:02:23 +0100727 const uint8_t *buf = _buf;
aliguori6f97dba2008-10-31 18:49:55 +0000728
729 len = len1;
730 while (len > 0) {
731 ret = write(fd, buf, len);
732 if (ret < 0) {
733 if (errno != EINTR && errno != EAGAIN)
734 return -1;
735 } else if (ret == 0) {
736 break;
737 } else {
738 buf += ret;
739 len -= ret;
740 }
741 }
742 return len1 - len;
743}
Stefan Berger4549a8b2013-02-27 12:47:53 -0500744
745int recv_all(int fd, void *_buf, int len1, bool single_read)
746{
747 int ret, len;
748 uint8_t *buf = _buf;
749
750 len = len1;
751 while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
752 if (ret < 0) {
753 if (errno != EINTR && errno != EAGAIN) {
754 return -1;
755 }
756 continue;
757 } else {
758 if (single_read) {
759 return ret;
760 }
761 buf += ret;
762 len -= ret;
763 }
764 }
765 return len1 - len;
766}
767
aliguori6f97dba2008-10-31 18:49:55 +0000768#endif /* !_WIN32 */
769
Anthony Liguori96c63842013-03-05 23:21:18 +0530770typedef struct IOWatchPoll
771{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200772 GSource parent;
773
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200774 GIOChannel *channel;
Anthony Liguori96c63842013-03-05 23:21:18 +0530775 GSource *src;
Anthony Liguori96c63842013-03-05 23:21:18 +0530776
777 IOCanReadHandler *fd_can_read;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200778 GSourceFunc fd_read;
Anthony Liguori96c63842013-03-05 23:21:18 +0530779 void *opaque;
Anthony Liguori96c63842013-03-05 23:21:18 +0530780} IOWatchPoll;
781
Anthony Liguori96c63842013-03-05 23:21:18 +0530782static IOWatchPoll *io_watch_poll_from_source(GSource *source)
783{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200784 return container_of(source, IOWatchPoll, parent);
Anthony Liguori96c63842013-03-05 23:21:18 +0530785}
786
787static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
788{
789 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200790 bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200791 bool was_active = iwp->src != NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200792 if (was_active == now_active) {
Anthony Liguori96c63842013-03-05 23:21:18 +0530793 return FALSE;
794 }
795
Paolo Bonzinid185c092013-04-05 17:59:33 +0200796 if (now_active) {
Nils Carlson4bf1cb02015-07-19 20:39:56 +0000797 iwp->src = g_io_create_watch(iwp->channel,
798 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200799 g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
Paolo Bonzinid185c092013-04-05 17:59:33 +0200800 g_source_attach(iwp->src, NULL);
801 } else {
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200802 g_source_destroy(iwp->src);
803 g_source_unref(iwp->src);
804 iwp->src = NULL;
Paolo Bonzinid185c092013-04-05 17:59:33 +0200805 }
806 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530807}
808
809static gboolean io_watch_poll_check(GSource *source)
810{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200811 return FALSE;
Anthony Liguori96c63842013-03-05 23:21:18 +0530812}
813
814static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
815 gpointer user_data)
816{
Paolo Bonzinid185c092013-04-05 17:59:33 +0200817 abort();
Anthony Liguori96c63842013-03-05 23:21:18 +0530818}
819
820static void io_watch_poll_finalize(GSource *source)
821{
Paolo Bonzini2b316772013-04-19 17:32:09 +0200822 /* Due to a glib bug, removing the last reference to a source
823 * inside a finalize callback causes recursive locking (and a
824 * deadlock). This is not a problem inside other callbacks,
825 * including dispatch callbacks, so we call io_remove_watch_poll
826 * to remove this source. At this point, iwp->src must
827 * be NULL, or we would leak it.
828 *
829 * This would be solved much more elegantly by child sources,
830 * but we support older glib versions that do not have them.
831 */
Anthony Liguori96c63842013-03-05 23:21:18 +0530832 IOWatchPoll *iwp = io_watch_poll_from_source(source);
Paolo Bonzini2b316772013-04-19 17:32:09 +0200833 assert(iwp->src == NULL);
Anthony Liguori96c63842013-03-05 23:21:18 +0530834}
835
836static GSourceFuncs io_watch_poll_funcs = {
837 .prepare = io_watch_poll_prepare,
838 .check = io_watch_poll_check,
839 .dispatch = io_watch_poll_dispatch,
840 .finalize = io_watch_poll_finalize,
841};
842
843/* Can only be used for read */
844static guint io_add_watch_poll(GIOChannel *channel,
845 IOCanReadHandler *fd_can_read,
846 GIOFunc fd_read,
847 gpointer user_data)
848{
849 IOWatchPoll *iwp;
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200850 int tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530851
Paolo Bonzinid185c092013-04-05 17:59:33 +0200852 iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
Anthony Liguori96c63842013-03-05 23:21:18 +0530853 iwp->fd_can_read = fd_can_read;
854 iwp->opaque = user_data;
Paolo Bonzini1e885b22013-04-08 15:03:15 +0200855 iwp->channel = channel;
856 iwp->fd_read = (GSourceFunc) fd_read;
857 iwp->src = NULL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530858
Paolo Bonzini0ca5aa42013-04-10 15:23:27 +0200859 tag = g_source_attach(&iwp->parent, NULL);
860 g_source_unref(&iwp->parent);
861 return tag;
Anthony Liguori96c63842013-03-05 23:21:18 +0530862}
863
Paolo Bonzini2b316772013-04-19 17:32:09 +0200864static void io_remove_watch_poll(guint tag)
865{
866 GSource *source;
867 IOWatchPoll *iwp;
868
869 g_return_if_fail (tag > 0);
870
871 source = g_main_context_find_source_by_id(NULL, tag);
872 g_return_if_fail (source != NULL);
873
874 iwp = io_watch_poll_from_source(source);
875 if (iwp->src) {
876 g_source_destroy(iwp->src);
877 g_source_unref(iwp->src);
878 iwp->src = NULL;
879 }
880 g_source_destroy(&iwp->parent);
881}
882
Amit Shah26da70c2013-08-28 15:23:37 +0530883static void remove_fd_in_watch(CharDriverState *chr)
884{
885 if (chr->fd_in_tag) {
886 io_remove_watch_poll(chr->fd_in_tag);
887 chr->fd_in_tag = 0;
888 }
889}
890
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000891#ifndef _WIN32
Anthony Liguori96c63842013-03-05 23:21:18 +0530892static GIOChannel *io_channel_from_fd(int fd)
893{
894 GIOChannel *chan;
895
896 if (fd == -1) {
897 return NULL;
898 }
899
900 chan = g_io_channel_unix_new(fd);
901
902 g_io_channel_set_encoding(chan, NULL, NULL);
903 g_io_channel_set_buffered(chan, FALSE);
904
905 return chan;
906}
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000907#endif
Anthony Liguori96c63842013-03-05 23:21:18 +0530908
Anthony Liguori76a96442013-03-05 23:21:21 +0530909static GIOChannel *io_channel_from_socket(int fd)
910{
911 GIOChannel *chan;
912
913 if (fd == -1) {
914 return NULL;
915 }
916
917#ifdef _WIN32
918 chan = g_io_channel_win32_new_socket(fd);
919#else
920 chan = g_io_channel_unix_new(fd);
921#endif
922
923 g_io_channel_set_encoding(chan, NULL, NULL);
924 g_io_channel_set_buffered(chan, FALSE);
925
926 return chan;
927}
928
Anthony Liguori684a0962013-03-29 11:39:50 -0500929static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
Anthony Liguori96c63842013-03-05 23:21:18 +0530930{
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200931 size_t offset = 0;
932 GIOStatus status = G_IO_STATUS_NORMAL;
Anthony Liguori96c63842013-03-05 23:21:18 +0530933
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200934 while (offset < len && status == G_IO_STATUS_NORMAL) {
935 gsize bytes_written = 0;
Anthony Liguori684a0962013-03-29 11:39:50 -0500936
937 status = g_io_channel_write_chars(fd, buf + offset, len - offset,
Anthony Liguori96c63842013-03-05 23:21:18 +0530938 &bytes_written, NULL);
Anthony Liguori684a0962013-03-29 11:39:50 -0500939 offset += bytes_written;
Anthony Liguori96c63842013-03-05 23:21:18 +0530940 }
Anthony Liguori684a0962013-03-29 11:39:50 -0500941
Laszlo Ersekac8c26f2013-07-16 20:19:40 +0200942 if (offset > 0) {
943 return offset;
944 }
945 switch (status) {
946 case G_IO_STATUS_NORMAL:
947 g_assert(len == 0);
948 return 0;
949 case G_IO_STATUS_AGAIN:
950 errno = EAGAIN;
951 return -1;
952 default:
953 break;
954 }
955 errno = EINVAL;
956 return -1;
Anthony Liguori96c63842013-03-05 23:21:18 +0530957}
Anthony Liguori96c63842013-03-05 23:21:18 +0530958
Blue Swirl44ab9ed2013-03-09 09:56:04 +0000959#ifndef _WIN32
960
Anthony Liguoria29753f2013-03-05 23:21:19 +0530961typedef struct FDCharDriver {
962 CharDriverState *chr;
963 GIOChannel *fd_in, *fd_out;
aliguori6f97dba2008-10-31 18:49:55 +0000964 int max_size;
965} FDCharDriver;
966
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200967/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +0000968static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
969{
970 FDCharDriver *s = chr->opaque;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530971
Anthony Liguori684a0962013-03-29 11:39:50 -0500972 return io_channel_send(s->fd_out, buf, len);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530973}
974
975static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
976{
977 CharDriverState *chr = opaque;
978 FDCharDriver *s = chr->opaque;
979 int len;
980 uint8_t buf[READ_BUF_LEN];
981 GIOStatus status;
982 gsize bytes_read;
983
984 len = sizeof(buf);
985 if (len > s->max_size) {
986 len = s->max_size;
987 }
988 if (len == 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +0200989 return TRUE;
Anthony Liguoria29753f2013-03-05 23:21:19 +0530990 }
991
992 status = g_io_channel_read_chars(chan, (gchar *)buf,
993 len, &bytes_read, NULL);
994 if (status == G_IO_STATUS_EOF) {
Amit Shah26da70c2013-08-28 15:23:37 +0530995 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +0530996 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
997 return FALSE;
998 }
999 if (status == G_IO_STATUS_NORMAL) {
1000 qemu_chr_be_write(chr, buf, bytes_read);
1001 }
1002
1003 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001004}
1005
1006static int fd_chr_read_poll(void *opaque)
1007{
1008 CharDriverState *chr = opaque;
1009 FDCharDriver *s = chr->opaque;
1010
Anthony Liguori909cda12011-08-15 11:17:31 -05001011 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001012 return s->max_size;
1013}
1014
Anthony Liguori23673ca2013-03-05 23:21:23 +05301015static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1016{
1017 FDCharDriver *s = chr->opaque;
1018 return g_io_create_watch(s->fd_out, cond);
1019}
1020
aliguori6f97dba2008-10-31 18:49:55 +00001021static void fd_chr_update_read_handler(CharDriverState *chr)
1022{
1023 FDCharDriver *s = chr->opaque;
1024
Amit Shah26da70c2013-08-28 15:23:37 +05301025 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301026 if (s->fd_in) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301027 chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
1028 fd_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00001029 }
1030}
1031
1032static void fd_chr_close(struct CharDriverState *chr)
1033{
1034 FDCharDriver *s = chr->opaque;
1035
Amit Shah26da70c2013-08-28 15:23:37 +05301036 remove_fd_in_watch(chr);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301037 if (s->fd_in) {
1038 g_io_channel_unref(s->fd_in);
1039 }
1040 if (s->fd_out) {
1041 g_io_channel_unref(s->fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001042 }
1043
Anthony Liguori7267c092011-08-20 22:09:37 -05001044 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001045 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001046}
1047
1048/* open a character device to a unix fd */
1049static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1050{
1051 CharDriverState *chr;
1052 FDCharDriver *s;
1053
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001054 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02001055 s = g_new0(FDCharDriver, 1);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301056 s->fd_in = io_channel_from_fd(fd_in);
1057 s->fd_out = io_channel_from_fd(fd_out);
Gonglei4ff12bd2014-08-11 17:34:20 +08001058 qemu_set_nonblock(fd_out);
Anthony Liguoria29753f2013-03-05 23:21:19 +05301059 s->chr = chr;
aliguori6f97dba2008-10-31 18:49:55 +00001060 chr->opaque = s;
Anthony Liguori23673ca2013-03-05 23:21:23 +05301061 chr->chr_add_watch = fd_chr_add_watch;
aliguori6f97dba2008-10-31 18:49:55 +00001062 chr->chr_write = fd_chr_write;
1063 chr->chr_update_read_handler = fd_chr_update_read_handler;
1064 chr->chr_close = fd_chr_close;
1065
aliguori6f97dba2008-10-31 18:49:55 +00001066 return chr;
1067}
1068
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001069static CharDriverState *qemu_chr_open_pipe(const char *id,
1070 ChardevBackend *backend,
1071 ChardevReturn *ret,
1072 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001073{
Eric Blake130257d2015-10-26 16:34:57 -06001074 ChardevHostdev *opts = backend->u.pipe;
aliguori6f97dba2008-10-31 18:49:55 +00001075 int fd_in, fd_out;
Corey Minyard9f781162014-10-02 11:17:33 -05001076 char filename_in[CHR_MAX_FILENAME_SIZE];
1077 char filename_out[CHR_MAX_FILENAME_SIZE];
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01001078 const char *filename = opts->device;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001079
Corey Minyard9f781162014-10-02 11:17:33 -05001080 snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
1081 snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001082 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
1083 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +00001084 if (fd_in < 0 || fd_out < 0) {
1085 if (fd_in >= 0)
1086 close(fd_in);
1087 if (fd_out >= 0)
1088 close(fd_out);
Markus Armbrusterb181e042012-02-07 15:09:09 +01001089 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001090 if (fd_in < 0) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02001091 error_setg_file_open(errp, errno, filename);
Markus Armbruster1f514702012-02-07 15:09:08 +01001092 return NULL;
Markus Armbrustera89dd6c2012-02-07 15:09:10 +01001093 }
aliguori6f97dba2008-10-31 18:49:55 +00001094 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001095 return qemu_chr_open_fd(fd_in, fd_out);
aliguori6f97dba2008-10-31 18:49:55 +00001096}
1097
aliguori6f97dba2008-10-31 18:49:55 +00001098/* init terminal so that we can grab keys */
1099static struct termios oldtty;
1100static int old_fd0_flags;
Li Liuc88930a2014-09-09 19:19:48 +08001101static bool stdio_in_use;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001102static bool stdio_allow_signal;
Gal Hammere76d4422015-01-07 10:38:35 +02001103static bool stdio_echo_state;
1104
1105static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
aliguori6f97dba2008-10-31 18:49:55 +00001106
1107static void term_exit(void)
1108{
1109 tcsetattr (0, TCSANOW, &oldtty);
1110 fcntl(0, F_SETFL, old_fd0_flags);
1111}
1112
Gal Hammere76d4422015-01-07 10:38:35 +02001113static void term_stdio_handler(int sig)
1114{
1115 /* restore echo after resume from suspend. */
1116 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
1117}
1118
Paolo Bonzinibb002512010-12-23 13:42:50 +01001119static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
aliguori6f97dba2008-10-31 18:49:55 +00001120{
1121 struct termios tty;
1122
Gal Hammere76d4422015-01-07 10:38:35 +02001123 stdio_echo_state = echo;
Paolo Bonzini03693642010-12-23 13:42:49 +01001124 tty = oldtty;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001125 if (!echo) {
1126 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
aliguori6f97dba2008-10-31 18:49:55 +00001127 |INLCR|IGNCR|ICRNL|IXON);
Paolo Bonzinibb002512010-12-23 13:42:50 +01001128 tty.c_oflag |= OPOST;
1129 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1130 tty.c_cflag &= ~(CSIZE|PARENB);
1131 tty.c_cflag |= CS8;
1132 tty.c_cc[VMIN] = 1;
1133 tty.c_cc[VTIME] = 0;
1134 }
Paolo Bonzinibb002512010-12-23 13:42:50 +01001135 if (!stdio_allow_signal)
aliguori6f97dba2008-10-31 18:49:55 +00001136 tty.c_lflag &= ~ISIG;
aliguori6f97dba2008-10-31 18:49:55 +00001137
1138 tcsetattr (0, TCSANOW, &tty);
aliguori6f97dba2008-10-31 18:49:55 +00001139}
1140
1141static void qemu_chr_close_stdio(struct CharDriverState *chr)
1142{
1143 term_exit();
aliguori6f97dba2008-10-31 18:49:55 +00001144 fd_chr_close(chr);
1145}
1146
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001147static CharDriverState *qemu_chr_open_stdio(const char *id,
1148 ChardevBackend *backend,
1149 ChardevReturn *ret,
1150 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001151{
Eric Blake130257d2015-10-26 16:34:57 -06001152 ChardevStdio *opts = backend->u.stdio;
aliguori6f97dba2008-10-31 18:49:55 +00001153 CharDriverState *chr;
Gal Hammere76d4422015-01-07 10:38:35 +02001154 struct sigaction act;
aliguori6f97dba2008-10-31 18:49:55 +00001155
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001156 if (is_daemonized()) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001157 error_setg(errp, "cannot use stdio with -daemonize");
Michael Tokarevab51b1d2012-12-30 12:48:14 +04001158 return NULL;
1159 }
Li Liuc88930a2014-09-09 19:19:48 +08001160
1161 if (stdio_in_use) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02001162 error_setg(errp, "cannot use stdio by multiple character devices");
1163 return NULL;
Li Liuc88930a2014-09-09 19:19:48 +08001164 }
1165
1166 stdio_in_use = true;
Anthony Liguoried7a1542013-03-05 23:21:17 +05301167 old_fd0_flags = fcntl(0, F_GETFL);
Li Liuc88930a2014-09-09 19:19:48 +08001168 tcgetattr(0, &oldtty);
Gonglei4ff12bd2014-08-11 17:34:20 +08001169 qemu_set_nonblock(0);
Anthony Liguoried7a1542013-03-05 23:21:17 +05301170 atexit(term_exit);
Paolo Bonzini03693642010-12-23 13:42:49 +01001171
Gal Hammere76d4422015-01-07 10:38:35 +02001172 memset(&act, 0, sizeof(act));
1173 act.sa_handler = term_stdio_handler;
1174 sigaction(SIGCONT, &act, NULL);
1175
aliguori6f97dba2008-10-31 18:49:55 +00001176 chr = qemu_chr_open_fd(0, 1);
1177 chr->chr_close = qemu_chr_close_stdio;
Paolo Bonzinibb002512010-12-23 13:42:50 +01001178 chr->chr_set_echo = qemu_chr_set_echo_stdio;
Gerd Hoffmann7c358032013-02-21 12:34:58 +01001179 if (opts->has_signal) {
1180 stdio_allow_signal = opts->signal;
1181 }
Anthony Liguori15f31512011-08-15 11:17:35 -05001182 qemu_chr_fe_set_echo(chr, false);
aliguori6f97dba2008-10-31 18:49:55 +00001183
Markus Armbruster1f514702012-02-07 15:09:08 +01001184 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001185}
1186
aliguori6f97dba2008-10-31 18:49:55 +00001187#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001188 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1189 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +00001190
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001191#define HAVE_CHARDEV_SERIAL 1
1192#define HAVE_CHARDEV_PTY 1
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001193
aliguori6f97dba2008-10-31 18:49:55 +00001194typedef struct {
Anthony Liguori093d3a22013-03-05 23:21:20 +05301195 GIOChannel *fd;
aliguori6f97dba2008-10-31 18:49:55 +00001196 int read_bytes;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001197
1198 /* Protected by the CharDriverState chr_write_lock. */
1199 int connected;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301200 guint timer_tag;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001201 guint open_tag;
aliguori6f97dba2008-10-31 18:49:55 +00001202} PtyCharDriver;
1203
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001204static void pty_chr_update_read_handler_locked(CharDriverState *chr);
aliguori6f97dba2008-10-31 18:49:55 +00001205static void pty_chr_state(CharDriverState *chr, int connected);
1206
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301207static gboolean pty_chr_timer(gpointer opaque)
1208{
1209 struct CharDriverState *chr = opaque;
1210 PtyCharDriver *s = chr->opaque;
1211
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001212 qemu_mutex_lock(&chr->chr_write_lock);
Hans de Goede79f20072013-04-25 13:53:02 +02001213 s->timer_tag = 0;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001214 s->open_tag = 0;
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001215 if (!s->connected) {
1216 /* Next poll ... */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001217 pty_chr_update_read_handler_locked(chr);
Gerd Hoffmannb0d768c2013-08-22 11:43:58 +02001218 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001219 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301220 return FALSE;
1221}
1222
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001223/* Called with chr_write_lock held. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301224static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
1225{
1226 PtyCharDriver *s = chr->opaque;
1227
1228 if (s->timer_tag) {
1229 g_source_remove(s->timer_tag);
1230 s->timer_tag = 0;
1231 }
1232
1233 if (ms == 1000) {
1234 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
1235 } else {
1236 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
1237 }
1238}
1239
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001240/* Called with chr_write_lock held. */
1241static void pty_chr_update_read_handler_locked(CharDriverState *chr)
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001242{
1243 PtyCharDriver *s = chr->opaque;
1244 GPollFD pfd;
Paolo Bonzinic1f24482015-12-01 11:27:00 +01001245 int rc;
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001246
1247 pfd.fd = g_io_channel_unix_get_fd(s->fd);
1248 pfd.events = G_IO_OUT;
1249 pfd.revents = 0;
Paolo Bonzinic1f24482015-12-01 11:27:00 +01001250 do {
1251 rc = g_poll(&pfd, 1, 0);
1252 } while (rc == -1 && errno == EINTR);
1253 assert(rc >= 0);
1254
Paolo Bonzini1bb7fe72014-06-18 08:43:57 +02001255 if (pfd.revents & G_IO_HUP) {
1256 pty_chr_state(chr, 0);
1257 } else {
1258 pty_chr_state(chr, 1);
1259 }
1260}
1261
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001262static void pty_chr_update_read_handler(CharDriverState *chr)
1263{
1264 qemu_mutex_lock(&chr->chr_write_lock);
1265 pty_chr_update_read_handler_locked(chr);
1266 qemu_mutex_unlock(&chr->chr_write_lock);
1267}
1268
1269/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001270static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1271{
1272 PtyCharDriver *s = chr->opaque;
1273
1274 if (!s->connected) {
1275 /* guest sends data, check for (re-)connect */
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001276 pty_chr_update_read_handler_locked(chr);
Sebastian Tanasecf7330c2014-07-28 13:39:14 +02001277 if (!s->connected) {
1278 return 0;
1279 }
aliguori6f97dba2008-10-31 18:49:55 +00001280 }
Anthony Liguori684a0962013-03-29 11:39:50 -05001281 return io_channel_send(s->fd, buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001282}
1283
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301284static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
1285{
1286 PtyCharDriver *s = chr->opaque;
Paolo Bonzini62c339c2014-07-24 16:08:04 +02001287 if (!s->connected) {
1288 return NULL;
1289 }
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301290 return g_io_create_watch(s->fd, cond);
1291}
1292
aliguori6f97dba2008-10-31 18:49:55 +00001293static int pty_chr_read_poll(void *opaque)
1294{
1295 CharDriverState *chr = opaque;
1296 PtyCharDriver *s = chr->opaque;
1297
Anthony Liguori909cda12011-08-15 11:17:31 -05001298 s->read_bytes = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001299 return s->read_bytes;
1300}
1301
Anthony Liguori093d3a22013-03-05 23:21:20 +05301302static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00001303{
1304 CharDriverState *chr = opaque;
1305 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301306 gsize size, len;
Amit Shah9bd78542009-11-03 19:59:54 +05301307 uint8_t buf[READ_BUF_LEN];
Anthony Liguori093d3a22013-03-05 23:21:20 +05301308 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00001309
1310 len = sizeof(buf);
1311 if (len > s->read_bytes)
1312 len = s->read_bytes;
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02001313 if (len == 0) {
1314 return TRUE;
1315 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301316 status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
1317 if (status != G_IO_STATUS_NORMAL) {
aliguori6f97dba2008-10-31 18:49:55 +00001318 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301319 return FALSE;
1320 } else {
aliguori6f97dba2008-10-31 18:49:55 +00001321 pty_chr_state(chr, 1);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001322 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00001323 }
Anthony Liguori093d3a22013-03-05 23:21:20 +05301324 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00001325}
1326
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001327static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
1328{
1329 CharDriverState *chr = opaque;
1330 PtyCharDriver *s = chr->opaque;
1331
1332 s->open_tag = 0;
1333 qemu_chr_be_generic_open(chr);
1334 return FALSE;
1335}
1336
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001337/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001338static void pty_chr_state(CharDriverState *chr, int connected)
1339{
1340 PtyCharDriver *s = chr->opaque;
1341
1342 if (!connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001343 if (s->open_tag) {
1344 g_source_remove(s->open_tag);
1345 s->open_tag = 0;
1346 }
Amit Shah26da70c2013-08-28 15:23:37 +05301347 remove_fd_in_watch(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001348 s->connected = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001349 /* (re-)connect poll interval for idle guests: once per second.
1350 * We check more frequently in case the guests sends data to
1351 * the virtual device linked to our pty. */
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301352 pty_chr_rearm_timer(chr, 1000);
aliguori6f97dba2008-10-31 18:49:55 +00001353 } else {
Paolo Bonzini85a67692013-04-19 17:32:07 +02001354 if (s->timer_tag) {
1355 g_source_remove(s->timer_tag);
1356 s->timer_tag = 0;
1357 }
1358 if (!s->connected) {
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001359 g_assert(s->open_tag == 0);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001360 s->connected = 1;
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001361 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
Gal Hammerac1b84d2014-02-25 12:12:35 +02001362 }
1363 if (!chr->fd_in_tag) {
Amit Shah7ba9add2013-08-28 15:18:29 +05301364 chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
1365 pty_chr_read, chr);
Paolo Bonzini85a67692013-04-19 17:32:07 +02001366 }
aliguori6f97dba2008-10-31 18:49:55 +00001367 }
1368}
1369
aliguori6f97dba2008-10-31 18:49:55 +00001370static void pty_chr_close(struct CharDriverState *chr)
1371{
1372 PtyCharDriver *s = chr->opaque;
Anthony Liguori093d3a22013-03-05 23:21:20 +05301373 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001374
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001375 qemu_mutex_lock(&chr->chr_write_lock);
1376 pty_chr_state(chr, 0);
Anthony Liguori093d3a22013-03-05 23:21:20 +05301377 fd = g_io_channel_unix_get_fd(s->fd);
1378 g_io_channel_unref(s->fd);
1379 close(fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301380 if (s->timer_tag) {
1381 g_source_remove(s->timer_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02001382 s->timer_tag = 0;
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301383 }
Paolo Bonzini7b3621f2014-07-11 12:11:38 +02001384 qemu_mutex_unlock(&chr->chr_write_lock);
Anthony Liguori7267c092011-08-20 22:09:37 -05001385 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01001386 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001387}
1388
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001389static CharDriverState *qemu_chr_open_pty(const char *id,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001390 ChardevBackend *backend,
1391 ChardevReturn *ret,
1392 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001393{
1394 CharDriverState *chr;
1395 PtyCharDriver *s;
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001396 int master_fd, slave_fd;
aliguori6f97dba2008-10-31 18:49:55 +00001397 char pty_name[PATH_MAX];
aliguori6f97dba2008-10-31 18:49:55 +00001398
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001399 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
1400 if (master_fd < 0) {
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02001401 error_setg_errno(errp, errno, "Failed to create PTY");
Markus Armbruster1f514702012-02-07 15:09:08 +01001402 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001403 }
1404
aliguori6f97dba2008-10-31 18:49:55 +00001405 close(slave_fd);
Don Slutzfac66882014-12-22 10:04:00 -05001406 qemu_set_nonblock(master_fd);
aliguori6f97dba2008-10-31 18:49:55 +00001407
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001408 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001409
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001410 chr->filename = g_strdup_printf("pty:%s", pty_name);
1411 ret->pty = g_strdup(pty_name);
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001412 ret->has_pty = true;
Lei Li58650212012-12-21 12:26:38 +08001413
Gerd Hoffmanne68c5952013-02-25 10:16:46 +01001414 fprintf(stderr, "char device redirected to %s (label %s)\n",
Michael Tokarev4efeabb2013-06-05 18:44:54 +04001415 pty_name, id);
Markus Armbrustera4e26042011-11-11 10:40:05 +01001416
Markus Armbruster2d528d42015-09-14 13:54:03 +02001417 s = g_new0(PtyCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001418 chr->opaque = s;
1419 chr->chr_write = pty_chr_write;
1420 chr->chr_update_read_handler = pty_chr_update_read_handler;
1421 chr->chr_close = pty_chr_close;
Anthony Liguorie6a87ed2013-03-05 23:21:24 +05301422 chr->chr_add_watch = pty_chr_add_watch;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001423 chr->explicit_be_open = true;
aliguori6f97dba2008-10-31 18:49:55 +00001424
Anthony Liguori093d3a22013-03-05 23:21:20 +05301425 s->fd = io_channel_from_fd(master_fd);
Anthony Liguori8aa33ca2013-03-05 23:21:26 +05301426 s->timer_tag = 0;
aliguori6f97dba2008-10-31 18:49:55 +00001427
Markus Armbruster1f514702012-02-07 15:09:08 +01001428 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001429}
1430
1431static void tty_serial_init(int fd, int speed,
1432 int parity, int data_bits, int stop_bits)
1433{
1434 struct termios tty;
1435 speed_t spd;
1436
1437#if 0
1438 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1439 speed, parity, data_bits, stop_bits);
1440#endif
1441 tcgetattr (fd, &tty);
1442
Stefan Weil45eea132009-10-26 16:10:10 +01001443#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1444 speed = speed * 10 / 11;
1445 do {
1446 check_speed(50);
1447 check_speed(75);
1448 check_speed(110);
1449 check_speed(134);
1450 check_speed(150);
1451 check_speed(200);
1452 check_speed(300);
1453 check_speed(600);
1454 check_speed(1200);
1455 check_speed(1800);
1456 check_speed(2400);
1457 check_speed(4800);
1458 check_speed(9600);
1459 check_speed(19200);
1460 check_speed(38400);
1461 /* Non-Posix values follow. They may be unsupported on some systems. */
1462 check_speed(57600);
1463 check_speed(115200);
1464#ifdef B230400
1465 check_speed(230400);
1466#endif
1467#ifdef B460800
1468 check_speed(460800);
1469#endif
1470#ifdef B500000
1471 check_speed(500000);
1472#endif
1473#ifdef B576000
1474 check_speed(576000);
1475#endif
1476#ifdef B921600
1477 check_speed(921600);
1478#endif
1479#ifdef B1000000
1480 check_speed(1000000);
1481#endif
1482#ifdef B1152000
1483 check_speed(1152000);
1484#endif
1485#ifdef B1500000
1486 check_speed(1500000);
1487#endif
1488#ifdef B2000000
1489 check_speed(2000000);
1490#endif
1491#ifdef B2500000
1492 check_speed(2500000);
1493#endif
1494#ifdef B3000000
1495 check_speed(3000000);
1496#endif
1497#ifdef B3500000
1498 check_speed(3500000);
1499#endif
1500#ifdef B4000000
1501 check_speed(4000000);
1502#endif
aliguori6f97dba2008-10-31 18:49:55 +00001503 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001504 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001505
1506 cfsetispeed(&tty, spd);
1507 cfsetospeed(&tty, spd);
1508
1509 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1510 |INLCR|IGNCR|ICRNL|IXON);
1511 tty.c_oflag |= OPOST;
1512 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1513 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1514 switch(data_bits) {
1515 default:
1516 case 8:
1517 tty.c_cflag |= CS8;
1518 break;
1519 case 7:
1520 tty.c_cflag |= CS7;
1521 break;
1522 case 6:
1523 tty.c_cflag |= CS6;
1524 break;
1525 case 5:
1526 tty.c_cflag |= CS5;
1527 break;
1528 }
1529 switch(parity) {
1530 default:
1531 case 'N':
1532 break;
1533 case 'E':
1534 tty.c_cflag |= PARENB;
1535 break;
1536 case 'O':
1537 tty.c_cflag |= PARENB | PARODD;
1538 break;
1539 }
1540 if (stop_bits == 2)
1541 tty.c_cflag |= CSTOPB;
1542
1543 tcsetattr (fd, TCSANOW, &tty);
1544}
1545
1546static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1547{
1548 FDCharDriver *s = chr->opaque;
1549
1550 switch(cmd) {
1551 case CHR_IOCTL_SERIAL_SET_PARAMS:
1552 {
1553 QEMUSerialSetParams *ssp = arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301554 tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
1555 ssp->speed, ssp->parity,
aliguori6f97dba2008-10-31 18:49:55 +00001556 ssp->data_bits, ssp->stop_bits);
1557 }
1558 break;
1559 case CHR_IOCTL_SERIAL_SET_BREAK:
1560 {
1561 int enable = *(int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301562 if (enable) {
1563 tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
1564 }
aliguori6f97dba2008-10-31 18:49:55 +00001565 }
1566 break;
1567 case CHR_IOCTL_SERIAL_GET_TIOCM:
1568 {
1569 int sarg = 0;
1570 int *targ = (int *)arg;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301571 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
aliguori6f97dba2008-10-31 18:49:55 +00001572 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001573 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001574 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001575 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001576 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001577 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001578 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001579 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001580 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001581 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001582 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001583 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001584 *targ |= CHR_TIOCM_RTS;
1585 }
1586 break;
1587 case CHR_IOCTL_SERIAL_SET_TIOCM:
1588 {
1589 int sarg = *(int *)arg;
1590 int targ = 0;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301591 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32b4abdfa2009-02-08 14:46:17 +00001592 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1593 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1594 if (sarg & CHR_TIOCM_CTS)
1595 targ |= TIOCM_CTS;
1596 if (sarg & CHR_TIOCM_CAR)
1597 targ |= TIOCM_CAR;
1598 if (sarg & CHR_TIOCM_DSR)
1599 targ |= TIOCM_DSR;
1600 if (sarg & CHR_TIOCM_RI)
1601 targ |= TIOCM_RI;
1602 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001603 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001604 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001605 targ |= TIOCM_RTS;
Anthony Liguoria29753f2013-03-05 23:21:19 +05301606 ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
aliguori6f97dba2008-10-31 18:49:55 +00001607 }
1608 break;
1609 default:
1610 return -ENOTSUP;
1611 }
1612 return 0;
1613}
1614
David Ahern4266a132010-02-10 18:27:17 -07001615static void qemu_chr_close_tty(CharDriverState *chr)
1616{
1617 FDCharDriver *s = chr->opaque;
1618 int fd = -1;
1619
1620 if (s) {
Anthony Liguoria29753f2013-03-05 23:21:19 +05301621 fd = g_io_channel_unix_get_fd(s->fd_in);
David Ahern4266a132010-02-10 18:27:17 -07001622 }
1623
1624 fd_chr_close(chr);
1625
1626 if (fd >= 0) {
1627 close(fd);
1628 }
1629}
1630
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01001631static CharDriverState *qemu_chr_open_tty_fd(int fd)
1632{
1633 CharDriverState *chr;
1634
1635 tty_serial_init(fd, 115200, 'N', 8, 1);
1636 chr = qemu_chr_open_fd(fd, fd);
1637 chr->chr_ioctl = tty_serial_ioctl;
1638 chr->chr_close = qemu_chr_close_tty;
1639 return chr;
1640}
aliguori6f97dba2008-10-31 18:49:55 +00001641#endif /* __linux__ || __sun__ */
1642
1643#if defined(__linux__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001644
1645#define HAVE_CHARDEV_PARPORT 1
1646
aliguori6f97dba2008-10-31 18:49:55 +00001647typedef struct {
1648 int fd;
1649 int mode;
1650} ParallelCharDriver;
1651
1652static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1653{
1654 if (s->mode != mode) {
1655 int m = mode;
1656 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1657 return 0;
1658 s->mode = mode;
1659 }
1660 return 1;
1661}
1662
1663static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1664{
1665 ParallelCharDriver *drv = chr->opaque;
1666 int fd = drv->fd;
1667 uint8_t b;
1668
1669 switch(cmd) {
1670 case CHR_IOCTL_PP_READ_DATA:
1671 if (ioctl(fd, PPRDATA, &b) < 0)
1672 return -ENOTSUP;
1673 *(uint8_t *)arg = b;
1674 break;
1675 case CHR_IOCTL_PP_WRITE_DATA:
1676 b = *(uint8_t *)arg;
1677 if (ioctl(fd, PPWDATA, &b) < 0)
1678 return -ENOTSUP;
1679 break;
1680 case CHR_IOCTL_PP_READ_CONTROL:
1681 if (ioctl(fd, PPRCONTROL, &b) < 0)
1682 return -ENOTSUP;
1683 /* Linux gives only the lowest bits, and no way to know data
1684 direction! For better compatibility set the fixed upper
1685 bits. */
1686 *(uint8_t *)arg = b | 0xc0;
1687 break;
1688 case CHR_IOCTL_PP_WRITE_CONTROL:
1689 b = *(uint8_t *)arg;
1690 if (ioctl(fd, PPWCONTROL, &b) < 0)
1691 return -ENOTSUP;
1692 break;
1693 case CHR_IOCTL_PP_READ_STATUS:
1694 if (ioctl(fd, PPRSTATUS, &b) < 0)
1695 return -ENOTSUP;
1696 *(uint8_t *)arg = b;
1697 break;
1698 case CHR_IOCTL_PP_DATA_DIR:
1699 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1700 return -ENOTSUP;
1701 break;
1702 case CHR_IOCTL_PP_EPP_READ_ADDR:
1703 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1704 struct ParallelIOArg *parg = arg;
1705 int n = read(fd, parg->buffer, parg->count);
1706 if (n != parg->count) {
1707 return -EIO;
1708 }
1709 }
1710 break;
1711 case CHR_IOCTL_PP_EPP_READ:
1712 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1713 struct ParallelIOArg *parg = arg;
1714 int n = read(fd, parg->buffer, parg->count);
1715 if (n != parg->count) {
1716 return -EIO;
1717 }
1718 }
1719 break;
1720 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1721 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1722 struct ParallelIOArg *parg = arg;
1723 int n = write(fd, parg->buffer, parg->count);
1724 if (n != parg->count) {
1725 return -EIO;
1726 }
1727 }
1728 break;
1729 case CHR_IOCTL_PP_EPP_WRITE:
1730 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1731 struct ParallelIOArg *parg = arg;
1732 int n = write(fd, parg->buffer, parg->count);
1733 if (n != parg->count) {
1734 return -EIO;
1735 }
1736 }
1737 break;
1738 default:
1739 return -ENOTSUP;
1740 }
1741 return 0;
1742}
1743
1744static void pp_close(CharDriverState *chr)
1745{
1746 ParallelCharDriver *drv = chr->opaque;
1747 int fd = drv->fd;
1748
1749 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1750 ioctl(fd, PPRELEASE);
1751 close(fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001752 g_free(drv);
Hans de Goedea425d232011-11-19 10:22:43 +01001753 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001754}
1755
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001756static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001757{
1758 CharDriverState *chr;
1759 ParallelCharDriver *drv;
aliguori6f97dba2008-10-31 18:49:55 +00001760
1761 if (ioctl(fd, PPCLAIM) < 0) {
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001762 error_setg_errno(errp, errno, "not a parallel port");
aliguori6f97dba2008-10-31 18:49:55 +00001763 close(fd);
Markus Armbruster1f514702012-02-07 15:09:08 +01001764 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00001765 }
1766
Markus Armbruster2d528d42015-09-14 13:54:03 +02001767 drv = g_new0(ParallelCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00001768 drv->fd = fd;
1769 drv->mode = IEEE1284_MODE_COMPAT;
1770
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001771 chr = qemu_chr_alloc();
aliguori6f97dba2008-10-31 18:49:55 +00001772 chr->chr_write = null_chr_write;
1773 chr->chr_ioctl = pp_ioctl;
1774 chr->chr_close = pp_close;
1775 chr->opaque = drv;
1776
Markus Armbruster1f514702012-02-07 15:09:08 +01001777 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00001778}
1779#endif /* __linux__ */
1780
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001781#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Gerd Hoffmanne5514982012-12-19 16:35:42 +01001782
1783#define HAVE_CHARDEV_PARPORT 1
1784
blueswir16972f932008-11-22 20:49:12 +00001785static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1786{
Stefan Weile0efb992011-02-23 19:09:16 +01001787 int fd = (int)(intptr_t)chr->opaque;
blueswir16972f932008-11-22 20:49:12 +00001788 uint8_t b;
1789
1790 switch(cmd) {
1791 case CHR_IOCTL_PP_READ_DATA:
1792 if (ioctl(fd, PPIGDATA, &b) < 0)
1793 return -ENOTSUP;
1794 *(uint8_t *)arg = b;
1795 break;
1796 case CHR_IOCTL_PP_WRITE_DATA:
1797 b = *(uint8_t *)arg;
1798 if (ioctl(fd, PPISDATA, &b) < 0)
1799 return -ENOTSUP;
1800 break;
1801 case CHR_IOCTL_PP_READ_CONTROL:
1802 if (ioctl(fd, PPIGCTRL, &b) < 0)
1803 return -ENOTSUP;
1804 *(uint8_t *)arg = b;
1805 break;
1806 case CHR_IOCTL_PP_WRITE_CONTROL:
1807 b = *(uint8_t *)arg;
1808 if (ioctl(fd, PPISCTRL, &b) < 0)
1809 return -ENOTSUP;
1810 break;
1811 case CHR_IOCTL_PP_READ_STATUS:
1812 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1813 return -ENOTSUP;
1814 *(uint8_t *)arg = b;
1815 break;
1816 default:
1817 return -ENOTSUP;
1818 }
1819 return 0;
1820}
1821
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02001822static CharDriverState *qemu_chr_open_pp_fd(int fd, Error **errp)
blueswir16972f932008-11-22 20:49:12 +00001823{
1824 CharDriverState *chr;
blueswir16972f932008-11-22 20:49:12 +00001825
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001826 chr = qemu_chr_alloc();
Stefan Weile0efb992011-02-23 19:09:16 +01001827 chr->opaque = (void *)(intptr_t)fd;
blueswir16972f932008-11-22 20:49:12 +00001828 chr->chr_write = null_chr_write;
1829 chr->chr_ioctl = pp_ioctl;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001830 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01001831 return chr;
blueswir16972f932008-11-22 20:49:12 +00001832}
1833#endif
1834
aliguori6f97dba2008-10-31 18:49:55 +00001835#else /* _WIN32 */
1836
Paolo Bonzinid809ab92015-10-12 09:46:23 +02001837#define HAVE_CHARDEV_SERIAL 1
1838
aliguori6f97dba2008-10-31 18:49:55 +00001839typedef struct {
1840 int max_size;
1841 HANDLE hcom, hrecv, hsend;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001842 OVERLAPPED orecv;
aliguori6f97dba2008-10-31 18:49:55 +00001843 BOOL fpipe;
1844 DWORD len;
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001845
1846 /* Protected by the CharDriverState chr_write_lock. */
1847 OVERLAPPED osend;
aliguori6f97dba2008-10-31 18:49:55 +00001848} WinCharState;
1849
Fabien Chouteaudb418a02011-10-06 16:37:51 +02001850typedef struct {
1851 HANDLE hStdIn;
1852 HANDLE hInputReadyEvent;
1853 HANDLE hInputDoneEvent;
1854 HANDLE hInputThread;
1855 uint8_t win_stdio_buf;
1856} WinStdioCharState;
1857
aliguori6f97dba2008-10-31 18:49:55 +00001858#define NSENDBUF 2048
1859#define NRECVBUF 2048
1860#define MAXCONNECT 1
1861#define NTIMEOUT 5000
1862
1863static int win_chr_poll(void *opaque);
1864static int win_chr_pipe_poll(void *opaque);
1865
1866static void win_chr_close(CharDriverState *chr)
1867{
1868 WinCharState *s = chr->opaque;
1869
1870 if (s->hsend) {
1871 CloseHandle(s->hsend);
1872 s->hsend = NULL;
1873 }
1874 if (s->hrecv) {
1875 CloseHandle(s->hrecv);
1876 s->hrecv = NULL;
1877 }
1878 if (s->hcom) {
1879 CloseHandle(s->hcom);
1880 s->hcom = NULL;
1881 }
1882 if (s->fpipe)
1883 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1884 else
1885 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301886
Hans de Goedea425d232011-11-19 10:22:43 +01001887 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001888}
1889
Paolo Bonzini6511d392015-09-29 15:08:05 +02001890static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00001891{
1892 WinCharState *s = chr->opaque;
1893 COMMCONFIG comcfg;
1894 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1895 COMSTAT comstat;
1896 DWORD size;
1897 DWORD err;
1898
1899 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1900 if (!s->hsend) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001901 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001902 goto fail;
1903 }
1904 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1905 if (!s->hrecv) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001906 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00001907 goto fail;
1908 }
1909
1910 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1911 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1912 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001913 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00001914 s->hcom = NULL;
1915 goto fail;
1916 }
1917
1918 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001919 error_setg(errp, "Failed SetupComm");
aliguori6f97dba2008-10-31 18:49:55 +00001920 goto fail;
1921 }
1922
1923 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1924 size = sizeof(COMMCONFIG);
1925 GetDefaultCommConfig(filename, &comcfg, &size);
1926 comcfg.dcb.DCBlength = sizeof(DCB);
1927 CommConfigDialog(filename, NULL, &comcfg);
1928
1929 if (!SetCommState(s->hcom, &comcfg.dcb)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001930 error_setg(errp, "Failed SetCommState");
aliguori6f97dba2008-10-31 18:49:55 +00001931 goto fail;
1932 }
1933
1934 if (!SetCommMask(s->hcom, EV_ERR)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001935 error_setg(errp, "Failed SetCommMask");
aliguori6f97dba2008-10-31 18:49:55 +00001936 goto fail;
1937 }
1938
1939 cto.ReadIntervalTimeout = MAXDWORD;
1940 if (!SetCommTimeouts(s->hcom, &cto)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001941 error_setg(errp, "Failed SetCommTimeouts");
aliguori6f97dba2008-10-31 18:49:55 +00001942 goto fail;
1943 }
1944
1945 if (!ClearCommError(s->hcom, &err, &comstat)) {
Paolo Bonzini6511d392015-09-29 15:08:05 +02001946 error_setg(errp, "Failed ClearCommError");
aliguori6f97dba2008-10-31 18:49:55 +00001947 goto fail;
1948 }
1949 qemu_add_polling_cb(win_chr_poll, chr);
1950 return 0;
1951
1952 fail:
1953 win_chr_close(chr);
1954 return -1;
1955}
1956
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02001957/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00001958static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1959{
1960 WinCharState *s = chr->opaque;
1961 DWORD len, ret, size, err;
1962
1963 len = len1;
1964 ZeroMemory(&s->osend, sizeof(s->osend));
1965 s->osend.hEvent = s->hsend;
1966 while (len > 0) {
1967 if (s->hsend)
1968 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1969 else
1970 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1971 if (!ret) {
1972 err = GetLastError();
1973 if (err == ERROR_IO_PENDING) {
1974 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1975 if (ret) {
1976 buf += size;
1977 len -= size;
1978 } else {
1979 break;
1980 }
1981 } else {
1982 break;
1983 }
1984 } else {
1985 buf += size;
1986 len -= size;
1987 }
1988 }
1989 return len1 - len;
1990}
1991
1992static int win_chr_read_poll(CharDriverState *chr)
1993{
1994 WinCharState *s = chr->opaque;
1995
Anthony Liguori909cda12011-08-15 11:17:31 -05001996 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001997 return s->max_size;
1998}
1999
2000static void win_chr_readfile(CharDriverState *chr)
2001{
2002 WinCharState *s = chr->opaque;
2003 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05302004 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002005 DWORD size;
2006
2007 ZeroMemory(&s->orecv, sizeof(s->orecv));
2008 s->orecv.hEvent = s->hrecv;
2009 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2010 if (!ret) {
2011 err = GetLastError();
2012 if (err == ERROR_IO_PENDING) {
2013 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2014 }
2015 }
2016
2017 if (size > 0) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002018 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002019 }
2020}
2021
2022static void win_chr_read(CharDriverState *chr)
2023{
2024 WinCharState *s = chr->opaque;
2025
2026 if (s->len > s->max_size)
2027 s->len = s->max_size;
2028 if (s->len == 0)
2029 return;
2030
2031 win_chr_readfile(chr);
2032}
2033
2034static int win_chr_poll(void *opaque)
2035{
2036 CharDriverState *chr = opaque;
2037 WinCharState *s = chr->opaque;
2038 COMSTAT status;
2039 DWORD comerr;
2040
2041 ClearCommError(s->hcom, &comerr, &status);
2042 if (status.cbInQue > 0) {
2043 s->len = status.cbInQue;
2044 win_chr_read_poll(chr);
2045 win_chr_read(chr);
2046 return 1;
2047 }
2048 return 0;
2049}
2050
Paolo Bonzini6511d392015-09-29 15:08:05 +02002051static CharDriverState *qemu_chr_open_win_path(const char *filename,
2052 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002053{
2054 CharDriverState *chr;
2055 WinCharState *s;
2056
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002057 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002058 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002059 chr->opaque = s;
2060 chr->chr_write = win_chr_write;
2061 chr->chr_close = win_chr_close;
2062
Paolo Bonzini6511d392015-09-29 15:08:05 +02002063 if (win_chr_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002064 g_free(s);
2065 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002066 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002067 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002068 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002069}
2070
2071static int win_chr_pipe_poll(void *opaque)
2072{
2073 CharDriverState *chr = opaque;
2074 WinCharState *s = chr->opaque;
2075 DWORD size;
2076
2077 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2078 if (size > 0) {
2079 s->len = size;
2080 win_chr_read_poll(chr);
2081 win_chr_read(chr);
2082 return 1;
2083 }
2084 return 0;
2085}
2086
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002087static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
2088 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002089{
2090 WinCharState *s = chr->opaque;
2091 OVERLAPPED ov;
2092 int ret;
2093 DWORD size;
Corey Minyard9f781162014-10-02 11:17:33 -05002094 char openname[CHR_MAX_FILENAME_SIZE];
aliguori6f97dba2008-10-31 18:49:55 +00002095
2096 s->fpipe = TRUE;
2097
2098 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2099 if (!s->hsend) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002100 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002101 goto fail;
2102 }
2103 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2104 if (!s->hrecv) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002105 error_setg(errp, "Failed CreateEvent");
aliguori6f97dba2008-10-31 18:49:55 +00002106 goto fail;
2107 }
2108
2109 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2110 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2111 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2112 PIPE_WAIT,
2113 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2114 if (s->hcom == INVALID_HANDLE_VALUE) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002115 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
aliguori6f97dba2008-10-31 18:49:55 +00002116 s->hcom = NULL;
2117 goto fail;
2118 }
2119
2120 ZeroMemory(&ov, sizeof(ov));
2121 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2122 ret = ConnectNamedPipe(s->hcom, &ov);
2123 if (ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002124 error_setg(errp, "Failed ConnectNamedPipe");
aliguori6f97dba2008-10-31 18:49:55 +00002125 goto fail;
2126 }
2127
2128 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2129 if (!ret) {
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002130 error_setg(errp, "Failed GetOverlappedResult");
aliguori6f97dba2008-10-31 18:49:55 +00002131 if (ov.hEvent) {
2132 CloseHandle(ov.hEvent);
2133 ov.hEvent = NULL;
2134 }
2135 goto fail;
2136 }
2137
2138 if (ov.hEvent) {
2139 CloseHandle(ov.hEvent);
2140 ov.hEvent = NULL;
2141 }
2142 qemu_add_polling_cb(win_chr_pipe_poll, chr);
2143 return 0;
2144
2145 fail:
2146 win_chr_close(chr);
2147 return -1;
2148}
2149
2150
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002151static CharDriverState *qemu_chr_open_pipe(const char *id,
2152 ChardevBackend *backend,
2153 ChardevReturn *ret,
2154 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002155{
Eric Blake130257d2015-10-26 16:34:57 -06002156 ChardevHostdev *opts = backend->u.pipe;
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01002157 const char *filename = opts->device;
aliguori6f97dba2008-10-31 18:49:55 +00002158 CharDriverState *chr;
2159 WinCharState *s;
2160
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002161 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002162 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002163 chr->opaque = s;
2164 chr->chr_write = win_chr_write;
2165 chr->chr_close = win_chr_close;
2166
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02002167 if (win_chr_pipe_init(chr, filename, errp) < 0) {
Stefan Weil2e02e182011-10-07 07:38:46 +02002168 g_free(s);
2169 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01002170 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002171 }
Markus Armbruster1f514702012-02-07 15:09:08 +01002172 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002173}
2174
Markus Armbruster1f514702012-02-07 15:09:08 +01002175static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
aliguori6f97dba2008-10-31 18:49:55 +00002176{
2177 CharDriverState *chr;
2178 WinCharState *s;
2179
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002180 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002181 s = g_new0(WinCharState, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002182 s->hcom = fd_out;
2183 chr->opaque = s;
2184 chr->chr_write = win_chr_write;
Markus Armbruster1f514702012-02-07 15:09:08 +01002185 return chr;
aliguori6f97dba2008-10-31 18:49:55 +00002186}
2187
Paolo Bonzini122e5ed2015-09-29 15:42:04 +02002188static CharDriverState *qemu_chr_open_win_con(const char *id,
2189 ChardevBackend *backend,
2190 ChardevReturn *ret,
2191 Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00002192{
Markus Armbruster1f514702012-02-07 15:09:08 +01002193 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
aliguori6f97dba2008-10-31 18:49:55 +00002194}
2195
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002196static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
2197{
2198 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2199 DWORD dwSize;
2200 int len1;
2201
2202 len1 = len;
2203
2204 while (len1 > 0) {
2205 if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
2206 break;
2207 }
2208 buf += dwSize;
2209 len1 -= dwSize;
2210 }
2211
2212 return len - len1;
2213}
2214
2215static void win_stdio_wait_func(void *opaque)
2216{
2217 CharDriverState *chr = opaque;
2218 WinStdioCharState *stdio = chr->opaque;
2219 INPUT_RECORD buf[4];
2220 int ret;
2221 DWORD dwSize;
2222 int i;
2223
Stefan Weildff74242013-12-07 14:48:04 +01002224 ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002225
2226 if (!ret) {
2227 /* Avoid error storm */
2228 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2229 return;
2230 }
2231
2232 for (i = 0; i < dwSize; i++) {
2233 KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
2234
2235 if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
2236 int j;
2237 if (kev->uChar.AsciiChar != 0) {
2238 for (j = 0; j < kev->wRepeatCount; j++) {
2239 if (qemu_chr_be_can_write(chr)) {
2240 uint8_t c = kev->uChar.AsciiChar;
2241 qemu_chr_be_write(chr, &c, 1);
2242 }
2243 }
2244 }
2245 }
2246 }
2247}
2248
2249static DWORD WINAPI win_stdio_thread(LPVOID param)
2250{
2251 CharDriverState *chr = param;
2252 WinStdioCharState *stdio = chr->opaque;
2253 int ret;
2254 DWORD dwSize;
2255
2256 while (1) {
2257
2258 /* Wait for one byte */
2259 ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
2260
2261 /* Exit in case of error, continue if nothing read */
2262 if (!ret) {
2263 break;
2264 }
2265 if (!dwSize) {
2266 continue;
2267 }
2268
2269 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2270 if (stdio->win_stdio_buf == '\r') {
2271 continue;
2272 }
2273
2274 /* Signal the main thread and wait until the byte was eaten */
2275 if (!SetEvent(stdio->hInputReadyEvent)) {
2276 break;
2277 }
2278 if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
2279 != WAIT_OBJECT_0) {
2280 break;
2281 }
2282 }
2283
2284 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2285 return 0;
2286}
2287
2288static void win_stdio_thread_wait_func(void *opaque)
2289{
2290 CharDriverState *chr = opaque;
2291 WinStdioCharState *stdio = chr->opaque;
2292
2293 if (qemu_chr_be_can_write(chr)) {
2294 qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
2295 }
2296
2297 SetEvent(stdio->hInputDoneEvent);
2298}
2299
2300static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
2301{
2302 WinStdioCharState *stdio = chr->opaque;
2303 DWORD dwMode = 0;
2304
2305 GetConsoleMode(stdio->hStdIn, &dwMode);
2306
2307 if (echo) {
2308 SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
2309 } else {
2310 SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
2311 }
2312}
2313
2314static void win_stdio_close(CharDriverState *chr)
2315{
2316 WinStdioCharState *stdio = chr->opaque;
2317
2318 if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
2319 CloseHandle(stdio->hInputReadyEvent);
2320 }
2321 if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
2322 CloseHandle(stdio->hInputDoneEvent);
2323 }
2324 if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
2325 TerminateThread(stdio->hInputThread, 0);
2326 }
2327
2328 g_free(chr->opaque);
2329 g_free(chr);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002330}
2331
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002332static CharDriverState *qemu_chr_open_stdio(const char *id,
2333 ChardevBackend *backend,
2334 ChardevReturn *ret,
2335 Error **errp)
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002336{
2337 CharDriverState *chr;
2338 WinStdioCharState *stdio;
2339 DWORD dwMode;
2340 int is_console = 0;
2341
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002342 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002343 stdio = g_new0(WinStdioCharState, 1);
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002344
2345 stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
2346 if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002347 error_setg(errp, "cannot open stdio: invalid handle");
2348 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002349 }
2350
2351 is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
2352
2353 chr->opaque = stdio;
2354 chr->chr_write = win_stdio_write;
2355 chr->chr_close = win_stdio_close;
2356
Anthony Liguoried7a1542013-03-05 23:21:17 +05302357 if (is_console) {
2358 if (qemu_add_wait_object(stdio->hStdIn,
2359 win_stdio_wait_func, chr)) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002360 error_setg(errp, "qemu_add_wait_object: failed");
2361 goto err1;
Anthony Liguoried7a1542013-03-05 23:21:17 +05302362 }
2363 } else {
2364 DWORD dwId;
2365
2366 stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2367 stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002368 if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
Anthony Liguoried7a1542013-03-05 23:21:17 +05302369 || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002370 error_setg(errp, "cannot create event");
2371 goto err2;
Anthony Liguoried7a1542013-03-05 23:21:17 +05302372 }
2373 if (qemu_add_wait_object(stdio->hInputReadyEvent,
2374 win_stdio_thread_wait_func, chr)) {
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002375 error_setg(errp, "qemu_add_wait_object: failed");
2376 goto err2;
2377 }
2378 stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
2379 chr, 0, &dwId);
2380
2381 if (stdio->hInputThread == INVALID_HANDLE_VALUE) {
2382 error_setg(errp, "cannot create stdio thread");
2383 goto err3;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002384 }
2385 }
2386
2387 dwMode |= ENABLE_LINE_INPUT;
2388
Anthony Liguoried7a1542013-03-05 23:21:17 +05302389 if (is_console) {
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002390 /* set the terminal in raw mode */
2391 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2392 dwMode |= ENABLE_PROCESSED_INPUT;
2393 }
2394
2395 SetConsoleMode(stdio->hStdIn, dwMode);
2396
2397 chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
2398 qemu_chr_fe_set_echo(chr, false);
2399
Markus Armbruster1f514702012-02-07 15:09:08 +01002400 return chr;
Paolo Bonzini8c84b252015-09-29 15:40:28 +02002401
2402err3:
2403 qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
2404err2:
2405 CloseHandle(stdio->hInputReadyEvent);
2406 CloseHandle(stdio->hInputDoneEvent);
2407err1:
2408 qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
2409 return NULL;
Fabien Chouteaudb418a02011-10-06 16:37:51 +02002410}
aliguori6f97dba2008-10-31 18:49:55 +00002411#endif /* !_WIN32 */
2412
Anthony Liguori5ab82112013-03-05 23:21:31 +05302413
aliguori6f97dba2008-10-31 18:49:55 +00002414/***********************************************************/
2415/* UDP Net console */
2416
2417typedef struct {
2418 int fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302419 GIOChannel *chan;
Amit Shah9bd78542009-11-03 19:59:54 +05302420 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002421 int bufcnt;
2422 int bufptr;
2423 int max_size;
2424} NetCharDriver;
2425
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002426/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002427static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2428{
2429 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302430 gsize bytes_written;
2431 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002432
Anthony Liguori76a96442013-03-05 23:21:21 +05302433 status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
2434 if (status == G_IO_STATUS_EOF) {
2435 return 0;
2436 } else if (status != G_IO_STATUS_NORMAL) {
2437 return -1;
2438 }
2439
2440 return bytes_written;
aliguori6f97dba2008-10-31 18:49:55 +00002441}
2442
2443static int udp_chr_read_poll(void *opaque)
2444{
2445 CharDriverState *chr = opaque;
2446 NetCharDriver *s = chr->opaque;
2447
Anthony Liguori909cda12011-08-15 11:17:31 -05002448 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002449
2450 /* If there were any stray characters in the queue process them
2451 * first
2452 */
2453 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002454 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002455 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002456 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002457 }
2458 return s->max_size;
2459}
2460
Anthony Liguori76a96442013-03-05 23:21:21 +05302461static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002462{
2463 CharDriverState *chr = opaque;
2464 NetCharDriver *s = chr->opaque;
Anthony Liguori76a96442013-03-05 23:21:21 +05302465 gsize bytes_read = 0;
2466 GIOStatus status;
aliguori6f97dba2008-10-31 18:49:55 +00002467
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002468 if (s->max_size == 0) {
2469 return TRUE;
2470 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302471 status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
2472 &bytes_read, NULL);
2473 s->bufcnt = bytes_read;
aliguori6f97dba2008-10-31 18:49:55 +00002474 s->bufptr = s->bufcnt;
Anthony Liguori76a96442013-03-05 23:21:21 +05302475 if (status != G_IO_STATUS_NORMAL) {
Amit Shah26da70c2013-08-28 15:23:37 +05302476 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302477 return FALSE;
2478 }
aliguori6f97dba2008-10-31 18:49:55 +00002479
2480 s->bufptr = 0;
2481 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002482 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
aliguori6f97dba2008-10-31 18:49:55 +00002483 s->bufptr++;
Anthony Liguori909cda12011-08-15 11:17:31 -05002484 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002485 }
Anthony Liguori76a96442013-03-05 23:21:21 +05302486
2487 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002488}
2489
2490static void udp_chr_update_read_handler(CharDriverState *chr)
2491{
2492 NetCharDriver *s = chr->opaque;
2493
Amit Shah26da70c2013-08-28 15:23:37 +05302494 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302495 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302496 chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
2497 udp_chr_read, chr);
aliguori6f97dba2008-10-31 18:49:55 +00002498 }
2499}
2500
aliguori819f56b2009-03-28 17:58:14 +00002501static void udp_chr_close(CharDriverState *chr)
2502{
2503 NetCharDriver *s = chr->opaque;
Amit Shah26da70c2013-08-28 15:23:37 +05302504
2505 remove_fd_in_watch(chr);
Anthony Liguori76a96442013-03-05 23:21:21 +05302506 if (s->chan) {
2507 g_io_channel_unref(s->chan);
aliguori819f56b2009-03-28 17:58:14 +00002508 closesocket(s->fd);
2509 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002510 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01002511 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00002512}
2513
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002514static CharDriverState *qemu_chr_open_udp_fd(int fd)
aliguori6f97dba2008-10-31 18:49:55 +00002515{
2516 CharDriverState *chr = NULL;
2517 NetCharDriver *s = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002518
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02002519 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02002520 s = g_new0(NetCharDriver, 1);
aliguori6f97dba2008-10-31 18:49:55 +00002521
aliguori6f97dba2008-10-31 18:49:55 +00002522 s->fd = fd;
Anthony Liguori76a96442013-03-05 23:21:21 +05302523 s->chan = io_channel_from_socket(s->fd);
aliguori6f97dba2008-10-31 18:49:55 +00002524 s->bufcnt = 0;
2525 s->bufptr = 0;
2526 chr->opaque = s;
2527 chr->chr_write = udp_chr_write;
2528 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00002529 chr->chr_close = udp_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -05002530 /* be isn't opened until we get a connection */
2531 chr->explicit_be_open = true;
Markus Armbruster1f514702012-02-07 15:09:08 +01002532 return chr;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01002533}
aliguori6f97dba2008-10-31 18:49:55 +00002534
aliguori6f97dba2008-10-31 18:49:55 +00002535/***********************************************************/
2536/* TCP Net console */
2537
2538typedef struct {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302539
2540 GIOChannel *chan, *listen_chan;
Amit Shah7ba9add2013-08-28 15:18:29 +05302541 guint listen_tag;
aliguori6f97dba2008-10-31 18:49:55 +00002542 int fd, listen_fd;
2543 int connected;
2544 int max_size;
2545 int do_telnetopt;
2546 int do_nodelay;
2547 int is_unix;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002548 int *read_msgfds;
2549 int read_msgfds_num;
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002550 int *write_msgfds;
2551 int write_msgfds_num;
Corey Minyardcfb429c2014-10-02 11:17:35 -05002552
2553 SocketAddress *addr;
2554 bool is_listen;
2555 bool is_telnet;
Corey Minyard5dd1f022014-10-02 11:17:37 -05002556
2557 guint reconnect_timer;
2558 int64_t reconnect_time;
Corey Minyard5008e5b2014-10-08 07:11:55 -05002559 bool connect_err_reported;
aliguori6f97dba2008-10-31 18:49:55 +00002560} TCPCharDriver;
2561
Corey Minyard5dd1f022014-10-02 11:17:37 -05002562static gboolean socket_reconnect_timeout(gpointer opaque);
2563
2564static void qemu_chr_socket_restart_timer(CharDriverState *chr)
2565{
2566 TCPCharDriver *s = chr->opaque;
2567 assert(s->connected == 0);
2568 s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
2569 socket_reconnect_timeout, chr);
2570}
2571
Corey Minyard5008e5b2014-10-08 07:11:55 -05002572static void check_report_connect_error(CharDriverState *chr,
2573 Error *err)
2574{
2575 TCPCharDriver *s = chr->opaque;
2576
2577 if (!s->connect_err_reported) {
2578 error_report("Unable to connect character device %s: %s",
2579 chr->label, error_get_pretty(err));
2580 s->connect_err_reported = true;
2581 }
2582 qemu_chr_socket_restart_timer(chr);
2583}
2584
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302585static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
aliguori6f97dba2008-10-31 18:49:55 +00002586
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002587#ifndef _WIN32
2588static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
2589{
2590 TCPCharDriver *s = chr->opaque;
2591 struct msghdr msgh;
2592 struct iovec iov;
2593 int r;
2594
2595 size_t fd_size = s->write_msgfds_num * sizeof(int);
2596 char control[CMSG_SPACE(fd_size)];
2597 struct cmsghdr *cmsg;
2598
2599 memset(&msgh, 0, sizeof(msgh));
2600 memset(control, 0, sizeof(control));
2601
2602 /* set the payload */
2603 iov.iov_base = (uint8_t *) buf;
2604 iov.iov_len = len;
2605
2606 msgh.msg_iov = &iov;
2607 msgh.msg_iovlen = 1;
2608
2609 msgh.msg_control = control;
2610 msgh.msg_controllen = sizeof(control);
2611
2612 cmsg = CMSG_FIRSTHDR(&msgh);
2613
2614 cmsg->cmsg_len = CMSG_LEN(fd_size);
2615 cmsg->cmsg_level = SOL_SOCKET;
2616 cmsg->cmsg_type = SCM_RIGHTS;
2617 memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size);
2618
2619 do {
2620 r = sendmsg(s->fd, &msgh, 0);
2621 } while (r < 0 && errno == EINTR);
2622
2623 /* free the written msgfds, no matter what */
2624 if (s->write_msgfds_num) {
2625 g_free(s->write_msgfds);
2626 s->write_msgfds = 0;
2627 s->write_msgfds_num = 0;
2628 }
2629
2630 return r;
2631}
2632#endif
2633
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02002634/* Called with chr_write_lock held. */
aliguori6f97dba2008-10-31 18:49:55 +00002635static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2636{
2637 TCPCharDriver *s = chr->opaque;
2638 if (s->connected) {
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002639#ifndef _WIN32
2640 if (s->is_unix && s->write_msgfds_num) {
2641 return unix_send_msgfds(chr, buf, len);
2642 } else
2643#endif
2644 {
2645 return io_channel_send(s->chan, buf, len);
2646 }
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002647 } else {
Anthony Liguori6db0fdc2012-09-12 14:34:07 -05002648 /* XXX: indicate an error ? */
Anthony Liguori455aa1e2012-09-05 13:52:49 -05002649 return len;
aliguori6f97dba2008-10-31 18:49:55 +00002650 }
2651}
2652
2653static int tcp_chr_read_poll(void *opaque)
2654{
2655 CharDriverState *chr = opaque;
2656 TCPCharDriver *s = chr->opaque;
2657 if (!s->connected)
2658 return 0;
Anthony Liguori909cda12011-08-15 11:17:31 -05002659 s->max_size = qemu_chr_be_can_write(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002660 return s->max_size;
2661}
2662
2663#define IAC 255
2664#define IAC_BREAK 243
2665static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2666 TCPCharDriver *s,
2667 uint8_t *buf, int *size)
2668{
2669 /* Handle any telnet client's basic IAC options to satisfy char by
2670 * char mode with no echo. All IAC options will be removed from
2671 * the buf and the do_telnetopt variable will be used to track the
2672 * state of the width of the IAC information.
2673 *
2674 * IAC commands come in sets of 3 bytes with the exception of the
2675 * "IAC BREAK" command and the double IAC.
2676 */
2677
2678 int i;
2679 int j = 0;
2680
2681 for (i = 0; i < *size; i++) {
2682 if (s->do_telnetopt > 1) {
2683 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
2684 /* Double IAC means send an IAC */
2685 if (j != i)
2686 buf[j] = buf[i];
2687 j++;
2688 s->do_telnetopt = 1;
2689 } else {
2690 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
2691 /* Handle IAC break commands by sending a serial break */
Hans de Goedea425d232011-11-19 10:22:43 +01002692 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
aliguori6f97dba2008-10-31 18:49:55 +00002693 s->do_telnetopt++;
2694 }
2695 s->do_telnetopt++;
2696 }
2697 if (s->do_telnetopt >= 4) {
2698 s->do_telnetopt = 1;
2699 }
2700 } else {
2701 if ((unsigned char)buf[i] == IAC) {
2702 s->do_telnetopt = 2;
2703 } else {
2704 if (j != i)
2705 buf[j] = buf[i];
2706 j++;
2707 }
2708 }
2709 }
2710 *size = j;
2711}
2712
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002713static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num)
Mark McLoughlin7d174052009-07-22 09:11:39 +01002714{
2715 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002716 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
2717
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002718 assert(num <= TCP_MAX_FDS);
2719
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002720 if (to_copy) {
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002721 int i;
2722
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002723 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
2724
Stefan Hajnoczid2fc39b2014-06-22 10:38:37 +08002725 /* Close unused fds */
2726 for (i = to_copy; i < s->read_msgfds_num; i++) {
2727 close(s->read_msgfds[i]);
2728 }
2729
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002730 g_free(s->read_msgfds);
2731 s->read_msgfds = 0;
2732 s->read_msgfds_num = 0;
2733 }
2734
2735 return to_copy;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002736}
2737
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002738static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
2739{
2740 TCPCharDriver *s = chr->opaque;
2741
2742 /* clear old pending fd array */
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +01002743 g_free(s->write_msgfds);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002744
2745 if (num) {
Markus Armbruster2d528d42015-09-14 13:54:03 +02002746 s->write_msgfds = g_new(int, num);
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03002747 memcpy(s->write_msgfds, fds, num * sizeof(int));
2748 }
2749
2750 s->write_msgfds_num = num;
2751
2752 return 0;
2753}
2754
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05002755#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01002756static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2757{
2758 TCPCharDriver *s = chr->opaque;
2759 struct cmsghdr *cmsg;
2760
2761 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002762 int fd_size, i;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002763
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002764 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
Mark McLoughlin7d174052009-07-22 09:11:39 +01002765 cmsg->cmsg_level != SOL_SOCKET ||
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002766 cmsg->cmsg_type != SCM_RIGHTS) {
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 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
2771
2772 if (!fd_size) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002773 continue;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002774 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002775
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002776 /* close and clean read_msgfds */
2777 for (i = 0; i < s->read_msgfds_num; i++) {
2778 close(s->read_msgfds[i]);
2779 }
Stefan Hajnoczi9b938c72013-03-27 10:10:46 +01002780
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03002781 if (s->read_msgfds_num) {
2782 g_free(s->read_msgfds);
2783 }
2784
2785 s->read_msgfds_num = fd_size / sizeof(int);
2786 s->read_msgfds = g_malloc(fd_size);
2787 memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size);
2788
2789 for (i = 0; i < s->read_msgfds_num; i++) {
2790 int fd = s->read_msgfds[i];
2791 if (fd < 0) {
2792 continue;
2793 }
2794
2795 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2796 qemu_set_block(fd);
2797
2798 #ifndef MSG_CMSG_CLOEXEC
2799 qemu_set_cloexec(fd);
2800 #endif
2801 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002802 }
2803}
2804
Mark McLoughlin9977c892009-07-22 09:11:38 +01002805static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2806{
2807 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002808 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002809 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002810 union {
2811 struct cmsghdr cmsg;
Michael S. Tsirkinc4095722014-11-02 18:48:32 +02002812 char control[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS)];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002813 } msg_control;
Corey Bryant06138652012-08-14 16:43:42 -04002814 int flags = 0;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002815 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002816
2817 iov[0].iov_base = buf;
2818 iov[0].iov_len = len;
2819
2820 msg.msg_iov = iov;
2821 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002822 msg.msg_control = &msg_control;
2823 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002824
Corey Bryant06138652012-08-14 16:43:42 -04002825#ifdef MSG_CMSG_CLOEXEC
2826 flags |= MSG_CMSG_CLOEXEC;
2827#endif
Paolo Bonzini9172f422015-07-21 09:25:54 +02002828 do {
2829 ret = recvmsg(s->fd, &msg, flags);
2830 } while (ret == -1 && errno == EINTR);
2831
Corey Bryant06138652012-08-14 16:43:42 -04002832 if (ret > 0 && s->is_unix) {
Mark McLoughlin7d174052009-07-22 09:11:39 +01002833 unix_process_msgfd(chr, &msg);
Corey Bryant06138652012-08-14 16:43:42 -04002834 }
Mark McLoughlin7d174052009-07-22 09:11:39 +01002835
2836 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002837}
2838#else
2839static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2840{
2841 TCPCharDriver *s = chr->opaque;
Paolo Bonzini9172f422015-07-21 09:25:54 +02002842 ssize_t ret;
2843
2844 do {
2845 ret = qemu_recv(s->fd, buf, len, 0);
2846 } while (ret == -1 && socket_error() == EINTR);
2847
2848 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002849}
2850#endif
2851
Amit Shahd3cc5bc2013-03-05 23:21:25 +05302852static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
2853{
2854 TCPCharDriver *s = chr->opaque;
2855 return g_io_create_watch(s->chan, cond);
2856}
2857
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002858static void tcp_chr_disconnect(CharDriverState *chr)
2859{
2860 TCPCharDriver *s = chr->opaque;
2861
2862 s->connected = 0;
2863 if (s->listen_chan) {
2864 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
2865 tcp_chr_accept, chr);
2866 }
2867 remove_fd_in_watch(chr);
2868 g_io_channel_unref(s->chan);
2869 s->chan = NULL;
2870 closesocket(s->fd);
2871 s->fd = -1;
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002872 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2873 "disconnected:", s->addr, s->is_listen, s->is_telnet);
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002874 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
Corey Minyard5dd1f022014-10-02 11:17:37 -05002875 if (s->reconnect_time) {
2876 qemu_chr_socket_restart_timer(chr);
2877 }
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002878}
2879
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302880static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002881{
2882 CharDriverState *chr = opaque;
2883 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302884 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002885 int len, size;
2886
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302887 if (!s->connected || s->max_size <= 0) {
Paolo Bonzinicdbf6e12013-04-19 17:32:08 +02002888 return TRUE;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302889 }
aliguori6f97dba2008-10-31 18:49:55 +00002890 len = sizeof(buf);
2891 if (len > s->max_size)
2892 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002893 size = tcp_chr_recv(chr, (void *)buf, len);
Nils Carlson4bf1cb02015-07-19 20:39:56 +00002894 if (size == 0 ||
2895 (size < 0 &&
2896 socket_error() != EAGAIN && socket_error() != EWOULDBLOCK)) {
aliguori6f97dba2008-10-31 18:49:55 +00002897 /* connection closed */
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002898 tcp_chr_disconnect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002899 } else if (size > 0) {
2900 if (s->do_telnetopt)
2901 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2902 if (size > 0)
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002903 qemu_chr_be_write(chr, buf, size);
aliguori6f97dba2008-10-31 18:49:55 +00002904 }
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302905
2906 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00002907}
2908
Nikolay Nikolaev7b0bfdf2014-05-27 15:03:48 +03002909static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len)
2910{
2911 TCPCharDriver *s = chr->opaque;
2912 int size;
2913
2914 if (!s->connected) {
2915 return 0;
2916 }
2917
2918 size = tcp_chr_recv(chr, (void *) buf, len);
2919 if (size == 0) {
2920 /* connection closed */
2921 tcp_chr_disconnect(chr);
2922 }
2923
2924 return size;
2925}
2926
Blue Swirl68c18d12010-08-15 09:46:24 +00002927#ifndef _WIN32
2928CharDriverState *qemu_chr_open_eventfd(int eventfd)
2929{
David Marchande9d21c42014-06-11 17:25:16 +02002930 CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd);
2931
2932 if (chr) {
2933 chr->avail_connections = 1;
2934 }
2935
2936 return chr;
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002937}
Blue Swirl68c18d12010-08-15 09:46:24 +00002938#endif
Cam Macdonell6cbf4c82010-07-27 10:54:13 -06002939
aliguori6f97dba2008-10-31 18:49:55 +00002940static void tcp_chr_connect(void *opaque)
2941{
2942 CharDriverState *chr = opaque;
2943 TCPCharDriver *s = chr->opaque;
Corey Minyard01ca5192014-10-02 11:17:38 -05002944 struct sockaddr_storage ss, ps;
2945 socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002946
2947 memset(&ss, 0, ss_len);
2948 if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
2949 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2950 "Error in getsockname: %s\n", strerror(errno));
Corey Minyard01ca5192014-10-02 11:17:38 -05002951 } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
2952 snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
2953 "Error in getpeername: %s\n", strerror(errno));
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002954 } else {
Corey Minyard01ca5192014-10-02 11:17:38 -05002955 sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
2956 &ss, ss_len, &ps, ps_len,
Corey Minyard16cc4ff2014-10-02 11:17:36 -05002957 s->is_listen, s->is_telnet);
2958 }
aliguori6f97dba2008-10-31 18:49:55 +00002959
2960 s->connected = 1;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05302961 if (s->chan) {
Amit Shah7ba9add2013-08-28 15:18:29 +05302962 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2963 tcp_chr_read, chr);
David Gibsonbbdd2ad2012-09-10 12:30:56 +10002964 }
Hans de Goedefee204f2013-03-26 11:07:54 +01002965 qemu_chr_be_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002966}
2967
Gal Hammerac1b84d2014-02-25 12:12:35 +02002968static void tcp_chr_update_read_handler(CharDriverState *chr)
2969{
2970 TCPCharDriver *s = chr->opaque;
2971
2972 remove_fd_in_watch(chr);
2973 if (s->chan) {
2974 chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
2975 tcp_chr_read, chr);
2976 }
2977}
2978
aliguori6f97dba2008-10-31 18:49:55 +00002979#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2980static void tcp_chr_telnet_init(int fd)
2981{
2982 char buf[3];
2983 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2984 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2985 send(fd, (char *)buf, 3, 0);
2986 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2987 send(fd, (char *)buf, 3, 0);
2988 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2989 send(fd, (char *)buf, 3, 0);
2990 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2991 send(fd, (char *)buf, 3, 0);
2992}
2993
Daniel P. Berrange13661082011-06-23 13:31:42 +01002994static int tcp_chr_add_client(CharDriverState *chr, int fd)
2995{
2996 TCPCharDriver *s = chr->opaque;
2997 if (s->fd != -1)
2998 return -1;
2999
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01003000 qemu_set_nonblock(fd);
Daniel P. Berrange13661082011-06-23 13:31:42 +01003001 if (s->do_nodelay)
3002 socket_set_nodelay(fd);
3003 s->fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303004 s->chan = io_channel_from_socket(fd);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003005 if (s->listen_tag) {
3006 g_source_remove(s->listen_tag);
3007 s->listen_tag = 0;
3008 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003009 tcp_chr_connect(chr);
3010
3011 return 0;
3012}
3013
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303014static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00003015{
3016 CharDriverState *chr = opaque;
3017 TCPCharDriver *s = chr->opaque;
3018 struct sockaddr_in saddr;
3019#ifndef _WIN32
3020 struct sockaddr_un uaddr;
3021#endif
3022 struct sockaddr *addr;
3023 socklen_t len;
3024 int fd;
3025
3026 for(;;) {
3027#ifndef _WIN32
3028 if (s->is_unix) {
3029 len = sizeof(uaddr);
3030 addr = (struct sockaddr *)&uaddr;
3031 } else
3032#endif
3033 {
3034 len = sizeof(saddr);
3035 addr = (struct sockaddr *)&saddr;
3036 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003037 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00003038 if (fd < 0 && errno != EINTR) {
Hans de Goede79f20072013-04-25 13:53:02 +02003039 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303040 return FALSE;
aliguori6f97dba2008-10-31 18:49:55 +00003041 } else if (fd >= 0) {
3042 if (s->do_telnetopt)
3043 tcp_chr_telnet_init(fd);
3044 break;
3045 }
3046 }
Daniel P. Berrange13661082011-06-23 13:31:42 +01003047 if (tcp_chr_add_client(chr, fd) < 0)
3048 close(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303049
3050 return TRUE;
aliguori6f97dba2008-10-31 18:49:55 +00003051}
3052
3053static void tcp_chr_close(CharDriverState *chr)
3054{
3055 TCPCharDriver *s = chr->opaque;
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003056 int i;
Corey Minyardcfb429c2014-10-02 11:17:35 -05003057
Corey Minyard5dd1f022014-10-02 11:17:37 -05003058 if (s->reconnect_timer) {
3059 g_source_remove(s->reconnect_timer);
3060 s->reconnect_timer = 0;
3061 }
Corey Minyardcfb429c2014-10-02 11:17:35 -05003062 qapi_free_SocketAddress(s->addr);
aliguori819f56b2009-03-28 17:58:14 +00003063 if (s->fd >= 0) {
Amit Shah26da70c2013-08-28 15:23:37 +05303064 remove_fd_in_watch(chr);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303065 if (s->chan) {
3066 g_io_channel_unref(s->chan);
3067 }
aliguori6f97dba2008-10-31 18:49:55 +00003068 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00003069 }
3070 if (s->listen_fd >= 0) {
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303071 if (s->listen_tag) {
3072 g_source_remove(s->listen_tag);
Paolo Bonzini910b6362013-04-19 17:32:06 +02003073 s->listen_tag = 0;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303074 }
3075 if (s->listen_chan) {
3076 g_io_channel_unref(s->listen_chan);
3077 }
aliguori6f97dba2008-10-31 18:49:55 +00003078 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00003079 }
Nikolay Nikolaevc76bf6b2014-05-27 15:04:15 +03003080 if (s->read_msgfds_num) {
3081 for (i = 0; i < s->read_msgfds_num; i++) {
3082 close(s->read_msgfds[i]);
3083 }
3084 g_free(s->read_msgfds);
3085 }
Nikolay Nikolaevd39aac72014-05-27 15:04:02 +03003086 if (s->write_msgfds_num) {
3087 g_free(s->write_msgfds);
3088 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003089 g_free(s);
Hans de Goedea425d232011-11-19 10:22:43 +01003090 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00003091}
3092
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003093static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
aliguori6f97dba2008-10-31 18:49:55 +00003094{
Corey Minyard43ded1a2014-10-02 11:17:34 -05003095 TCPCharDriver *s = chr->opaque;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003096
Corey Minyardcfb429c2014-10-02 11:17:35 -05003097 if (s->is_listen) {
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003098 s->listen_fd = fd;
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303099 s->listen_chan = io_channel_from_socket(s->listen_fd);
Corey Minyard43ded1a2014-10-02 11:17:34 -05003100 s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN,
3101 tcp_chr_accept, chr);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003102 } else {
3103 s->connected = 1;
3104 s->fd = fd;
3105 socket_set_nodelay(fd);
Anthony Liguori2ea5a7a2013-03-05 23:21:22 +05303106 s->chan = io_channel_from_socket(s->fd);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003107 tcp_chr_connect(chr);
3108 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003109}
3110
Corey Minyard51795022014-10-08 07:11:56 -05003111static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
Corey Minyard5dd1f022014-10-02 11:17:37 -05003112{
3113 CharDriverState *chr = opaque;
Corey Minyard5008e5b2014-10-08 07:11:55 -05003114 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003115
3116 if (fd < 0) {
Corey Minyard5008e5b2014-10-08 07:11:55 -05003117 check_report_connect_error(chr, err);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003118 return;
3119 }
3120
Corey Minyard5008e5b2014-10-08 07:11:55 -05003121 s->connect_err_reported = false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05003122 qemu_chr_finish_socket_connection(chr, fd);
3123}
3124
Corey Minyardcfb429c2014-10-02 11:17:35 -05003125static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
Corey Minyard43ded1a2014-10-02 11:17:34 -05003126{
Corey Minyardcfb429c2014-10-02 11:17:35 -05003127 TCPCharDriver *s = chr->opaque;
Corey Minyard43ded1a2014-10-02 11:17:34 -05003128 int fd;
3129
Corey Minyardcfb429c2014-10-02 11:17:35 -05003130 if (s->is_listen) {
3131 fd = socket_listen(s->addr, errp);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003132 } else if (s->reconnect_time) {
3133 fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
3134 return fd >= 0;
3135 } else {
Corey Minyardcfb429c2014-10-02 11:17:35 -05003136 fd = socket_connect(s->addr, errp, NULL, NULL);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003137 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05003138 if (fd < 0) {
3139 return false;
3140 }
3141
Corey Minyard16cc4ff2014-10-02 11:17:36 -05003142 qemu_chr_finish_socket_connection(chr, fd);
3143 return true;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01003144}
3145
Lei Li51767e72013-01-25 00:03:19 +08003146/*********************************************************/
Markus Armbruster3949e592013-02-06 21:27:24 +01003147/* Ring buffer chardev */
Lei Li51767e72013-01-25 00:03:19 +08003148
3149typedef struct {
3150 size_t size;
3151 size_t prod;
3152 size_t cons;
3153 uint8_t *cbuf;
Markus Armbruster3949e592013-02-06 21:27:24 +01003154} RingBufCharDriver;
Lei Li51767e72013-01-25 00:03:19 +08003155
Markus Armbruster3949e592013-02-06 21:27:24 +01003156static size_t ringbuf_count(const CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003157{
Markus Armbruster3949e592013-02-06 21:27:24 +01003158 const RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003159
Markus Armbruster5c230102013-02-06 21:27:23 +01003160 return d->prod - d->cons;
Lei Li51767e72013-01-25 00:03:19 +08003161}
3162
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003163/* Called with chr_write_lock held. */
Markus Armbruster3949e592013-02-06 21:27:24 +01003164static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003165{
Markus Armbruster3949e592013-02-06 21:27:24 +01003166 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003167 int i;
3168
3169 if (!buf || (len < 0)) {
3170 return -1;
3171 }
3172
3173 for (i = 0; i < len; i++ ) {
Markus Armbruster5c230102013-02-06 21:27:23 +01003174 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
3175 if (d->prod - d->cons > d->size) {
Lei Li51767e72013-01-25 00:03:19 +08003176 d->cons = d->prod - d->size;
3177 }
3178 }
3179
3180 return 0;
3181}
3182
Markus Armbruster3949e592013-02-06 21:27:24 +01003183static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
Lei Li51767e72013-01-25 00:03:19 +08003184{
Markus Armbruster3949e592013-02-06 21:27:24 +01003185 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003186 int i;
3187
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003188 qemu_mutex_lock(&chr->chr_write_lock);
Markus Armbruster5c230102013-02-06 21:27:23 +01003189 for (i = 0; i < len && d->cons != d->prod; i++) {
3190 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
Lei Li51767e72013-01-25 00:03:19 +08003191 }
Paolo Bonzini9005b2a2014-06-18 08:43:58 +02003192 qemu_mutex_unlock(&chr->chr_write_lock);
Lei Li51767e72013-01-25 00:03:19 +08003193
3194 return i;
3195}
3196
Markus Armbruster3949e592013-02-06 21:27:24 +01003197static void ringbuf_chr_close(struct CharDriverState *chr)
Lei Li51767e72013-01-25 00:03:19 +08003198{
Markus Armbruster3949e592013-02-06 21:27:24 +01003199 RingBufCharDriver *d = chr->opaque;
Lei Li51767e72013-01-25 00:03:19 +08003200
3201 g_free(d->cbuf);
3202 g_free(d);
3203 chr->opaque = NULL;
3204}
3205
Paolo Bonzini479f09a2015-09-29 15:51:14 +02003206static CharDriverState *qemu_chr_open_ringbuf(const char *id,
3207 ChardevBackend *backend,
3208 ChardevReturn *ret,
Markus Armbruster4f573782013-07-26 16:44:32 +02003209 Error **errp)
Lei Li51767e72013-01-25 00:03:19 +08003210{
Eric Blake130257d2015-10-26 16:34:57 -06003211 ChardevRingbuf *opts = backend->u.ringbuf;
Lei Li51767e72013-01-25 00:03:19 +08003212 CharDriverState *chr;
Markus Armbruster3949e592013-02-06 21:27:24 +01003213 RingBufCharDriver *d;
Lei Li51767e72013-01-25 00:03:19 +08003214
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02003215 chr = qemu_chr_alloc();
Lei Li51767e72013-01-25 00:03:19 +08003216 d = g_malloc(sizeof(*d));
3217
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003218 d->size = opts->has_size ? opts->size : 65536;
Lei Li51767e72013-01-25 00:03:19 +08003219
3220 /* The size must be power of 2 */
3221 if (d->size & (d->size - 1)) {
Markus Armbruster4f573782013-07-26 16:44:32 +02003222 error_setg(errp, "size of ringbuf chardev must be power of two");
Lei Li51767e72013-01-25 00:03:19 +08003223 goto fail;
3224 }
3225
3226 d->prod = 0;
3227 d->cons = 0;
3228 d->cbuf = g_malloc0(d->size);
3229
3230 chr->opaque = d;
Markus Armbruster3949e592013-02-06 21:27:24 +01003231 chr->chr_write = ringbuf_chr_write;
3232 chr->chr_close = ringbuf_chr_close;
Lei Li51767e72013-01-25 00:03:19 +08003233
3234 return chr;
3235
3236fail:
3237 g_free(d);
3238 g_free(chr);
3239 return NULL;
3240}
3241
Hani Benhabiles8e597772014-05-27 23:39:30 +01003242bool chr_is_ringbuf(const CharDriverState *chr)
Lei Li1f590cf2013-01-25 00:03:20 +08003243{
Markus Armbruster3949e592013-02-06 21:27:24 +01003244 return chr->chr_write == ringbuf_chr_write;
Lei Li1f590cf2013-01-25 00:03:20 +08003245}
3246
Markus Armbruster3949e592013-02-06 21:27:24 +01003247void qmp_ringbuf_write(const char *device, const char *data,
Markus Armbruster82e59a62013-02-06 21:27:14 +01003248 bool has_format, enum DataFormat format,
Lei Li1f590cf2013-01-25 00:03:20 +08003249 Error **errp)
3250{
3251 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003252 const uint8_t *write_data;
Lei Li1f590cf2013-01-25 00:03:20 +08003253 int ret;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +10003254 gsize write_count;
Lei Li1f590cf2013-01-25 00:03:20 +08003255
3256 chr = qemu_chr_find(device);
3257 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003258 error_setg(errp, "Device '%s' not found", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003259 return;
3260 }
3261
Markus Armbruster3949e592013-02-06 21:27:24 +01003262 if (!chr_is_ringbuf(chr)) {
3263 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li1f590cf2013-01-25 00:03:20 +08003264 return;
3265 }
3266
Lei Li1f590cf2013-01-25 00:03:20 +08003267 if (has_format && (format == DATA_FORMAT_BASE64)) {
Daniel P. Berrangee9cf2fe2015-11-23 15:29:59 +00003268 write_data = qbase64_decode(data, -1,
3269 &write_count,
3270 errp);
3271 if (!write_data) {
3272 return;
3273 }
Lei Li1f590cf2013-01-25 00:03:20 +08003274 } else {
3275 write_data = (uint8_t *)data;
Markus Armbruster82e59a62013-02-06 21:27:14 +01003276 write_count = strlen(data);
Lei Li1f590cf2013-01-25 00:03:20 +08003277 }
3278
Markus Armbruster3949e592013-02-06 21:27:24 +01003279 ret = ringbuf_chr_write(chr, write_data, write_count);
Lei Li1f590cf2013-01-25 00:03:20 +08003280
Markus Armbruster13289fb2013-02-06 21:27:18 +01003281 if (write_data != (uint8_t *)data) {
3282 g_free((void *)write_data);
3283 }
3284
Lei Li1f590cf2013-01-25 00:03:20 +08003285 if (ret < 0) {
3286 error_setg(errp, "Failed to write to device %s", device);
3287 return;
3288 }
3289}
3290
Markus Armbruster3949e592013-02-06 21:27:24 +01003291char *qmp_ringbuf_read(const char *device, int64_t size,
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003292 bool has_format, enum DataFormat format,
3293 Error **errp)
Lei Li49b6d722013-01-25 00:03:21 +08003294{
3295 CharDriverState *chr;
Markus Armbrusterc4f331b2013-02-06 21:27:17 +01003296 uint8_t *read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003297 size_t count;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003298 char *data;
Lei Li49b6d722013-01-25 00:03:21 +08003299
3300 chr = qemu_chr_find(device);
3301 if (!chr) {
Markus Armbruster1a692782013-02-06 21:27:16 +01003302 error_setg(errp, "Device '%s' not found", device);
Lei Li49b6d722013-01-25 00:03:21 +08003303 return NULL;
3304 }
3305
Markus Armbruster3949e592013-02-06 21:27:24 +01003306 if (!chr_is_ringbuf(chr)) {
3307 error_setg(errp,"%s is not a ringbuf device", device);
Lei Li49b6d722013-01-25 00:03:21 +08003308 return NULL;
3309 }
3310
3311 if (size <= 0) {
3312 error_setg(errp, "size must be greater than zero");
3313 return NULL;
3314 }
3315
Markus Armbruster3949e592013-02-06 21:27:24 +01003316 count = ringbuf_count(chr);
Lei Li49b6d722013-01-25 00:03:21 +08003317 size = size > count ? count : size;
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003318 read_data = g_malloc(size + 1);
Lei Li49b6d722013-01-25 00:03:21 +08003319
Markus Armbruster3949e592013-02-06 21:27:24 +01003320 ringbuf_chr_read(chr, read_data, size);
Lei Li49b6d722013-01-25 00:03:21 +08003321
3322 if (has_format && (format == DATA_FORMAT_BASE64)) {
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003323 data = g_base64_encode(read_data, size);
Markus Armbruster13289fb2013-02-06 21:27:18 +01003324 g_free(read_data);
Lei Li49b6d722013-01-25 00:03:21 +08003325 } else {
Markus Armbruster3949e592013-02-06 21:27:24 +01003326 /*
3327 * FIXME should read only complete, valid UTF-8 characters up
3328 * to @size bytes. Invalid sequences should be replaced by a
3329 * suitable replacement character. Except when (and only
3330 * when) ring buffer lost characters since last read, initial
3331 * continuation characters should be dropped.
3332 */
Markus Armbruster44f3bcd2013-02-06 21:27:20 +01003333 read_data[size] = 0;
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003334 data = (char *)read_data;
Lei Li49b6d722013-01-25 00:03:21 +08003335 }
3336
Markus Armbruster3ab651f2013-02-06 21:27:15 +01003337 return data;
Lei Li49b6d722013-01-25 00:03:21 +08003338}
3339
Gerd Hoffmann33521632009-12-08 13:11:49 +01003340QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003341{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003342 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003343 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003344 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003345 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003346 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003347
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003348 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003349 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +01003350 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003351 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03003352 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003353
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003354 if (strstart(filename, "mon:", &p)) {
3355 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003356 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003357 if (strcmp(filename, "stdio") == 0) {
3358 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3359 * but pass it to the guest. Handle this only for compat syntax,
3360 * for -chardev syntax we have special option for this.
3361 * This is what -nographic did, redirecting+muxing serial+monitor
3362 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003363 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +04003364 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003365 }
3366
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003367 if (strcmp(filename, "null") == 0 ||
3368 strcmp(filename, "pty") == 0 ||
3369 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02003370 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +02003371 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02003372 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003373 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003374 return opts;
3375 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003376 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003377 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003378 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +02003379 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003380 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003381 qemu_opt_set(opts, "width", width, &error_abort);
3382 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +02003383 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003384 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003385 qemu_opt_set(opts, "cols", width, &error_abort);
3386 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02003387 } else {
3388 goto fail;
3389 }
3390 }
3391 return opts;
3392 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003393 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003394 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02003395 return opts;
3396 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003397 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003398 qemu_opt_set(opts, "backend", "serial", &error_abort);
3399 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003400 return opts;
3401 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003402 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003403 qemu_opt_set(opts, "backend", "file", &error_abort);
3404 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003405 return opts;
3406 }
3407 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003408 qemu_opt_set(opts, "backend", "pipe", &error_abort);
3409 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +02003410 return opts;
3411 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003412 if (strstart(filename, "tcp:", &p) ||
3413 strstart(filename, "telnet:", &p)) {
3414 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3415 host[0] = 0;
3416 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
3417 goto fail;
3418 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003419 qemu_opt_set(opts, "backend", "socket", &error_abort);
3420 qemu_opt_set(opts, "host", host, &error_abort);
3421 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003422 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003423 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
3424 if (local_err) {
3425 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003426 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003427 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003428 }
3429 if (strstart(filename, "telnet:", &p))
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003430 qemu_opt_set(opts, "telnet", "on", &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003431 return opts;
3432 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003433 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003434 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003435 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
3436 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +01003437 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003438 goto fail;
3439 }
3440 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003441 qemu_opt_set(opts, "host", host, &error_abort);
3442 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003443 if (p[pos] == '@') {
3444 p += pos + 1;
3445 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
3446 host[0] = 0;
3447 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003448 goto fail;
3449 }
3450 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003451 qemu_opt_set(opts, "localaddr", host, &error_abort);
3452 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003453 }
3454 return opts;
3455 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003456 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003457 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003458 qemu_opts_do_parse(opts, p, "path", &local_err);
3459 if (local_err) {
3460 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003461 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003462 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003463 return opts;
3464 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003465 if (strstart(filename, "/dev/parport", NULL) ||
3466 strstart(filename, "/dev/ppi", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003467 qemu_opt_set(opts, "backend", "parport", &error_abort);
3468 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003469 return opts;
3470 }
3471 if (strstart(filename, "/dev/", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003472 qemu_opt_set(opts, "backend", "tty", &error_abort);
3473 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +02003474 return opts;
3475 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003476
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02003477fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003478 qemu_opts_del(opts);
3479 return NULL;
3480}
3481
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003482static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
3483 Error **errp)
3484{
3485 const char *path = qemu_opt_get(opts, "path");
3486
3487 if (path == NULL) {
3488 error_setg(errp, "chardev: file: no filename given");
3489 return;
3490 }
Eric Blake130257d2015-10-26 16:34:57 -06003491 backend->u.file = g_new0(ChardevFile, 1);
3492 backend->u.file->out = g_strdup(path);
Olga Krishtal31e38a22015-12-04 09:42:04 +03003493
3494 backend->u.file->has_append = true;
3495 backend->u.file->append = qemu_opt_get_bool(opts, "append", false);
Gerd Hoffmann846e2e42013-02-21 12:07:14 +01003496}
3497
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003498static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
3499 Error **errp)
3500{
Eric Blake130257d2015-10-26 16:34:57 -06003501 backend->u.stdio = g_new0(ChardevStdio, 1);
3502 backend->u.stdio->has_signal = true;
3503 backend->u.stdio->signal = qemu_opt_get_bool(opts, "signal", true);
Gerd Hoffmann7c358032013-02-21 12:34:58 +01003504}
3505
Paolo Bonzini6511d392015-09-29 15:08:05 +02003506#ifdef HAVE_CHARDEV_SERIAL
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003507static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
3508 Error **errp)
3509{
3510 const char *device = qemu_opt_get(opts, "path");
3511
3512 if (device == NULL) {
3513 error_setg(errp, "chardev: serial/tty: no device path given");
3514 return;
3515 }
Eric Blake130257d2015-10-26 16:34:57 -06003516 backend->u.serial = g_new0(ChardevHostdev, 1);
3517 backend->u.serial->device = g_strdup(device);
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003518}
Paolo Bonzini6511d392015-09-29 15:08:05 +02003519#endif
Gerd Hoffmann0f1cb512013-02-22 15:48:05 +01003520
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003521#ifdef HAVE_CHARDEV_PARPORT
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003522static void qemu_chr_parse_parallel(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: parallel: no device path given");
3529 return;
3530 }
Eric Blake130257d2015-10-26 16:34:57 -06003531 backend->u.parallel = g_new0(ChardevHostdev, 1);
3532 backend->u.parallel->device = g_strdup(device);
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003533}
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02003534#endif
Gerd Hoffmanndc375092013-02-22 16:17:01 +01003535
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003536static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
3537 Error **errp)
3538{
3539 const char *device = qemu_opt_get(opts, "path");
3540
3541 if (device == NULL) {
3542 error_setg(errp, "chardev: pipe: no device path given");
3543 return;
3544 }
Eric Blake130257d2015-10-26 16:34:57 -06003545 backend->u.pipe = g_new0(ChardevHostdev, 1);
3546 backend->u.pipe->device = g_strdup(device);
Gerd Hoffmann548cbb32013-02-25 11:50:55 +01003547}
3548
Markus Armbruster4f573782013-07-26 16:44:32 +02003549static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
3550 Error **errp)
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003551{
3552 int val;
3553
Eric Blake130257d2015-10-26 16:34:57 -06003554 backend->u.ringbuf = g_new0(ChardevRingbuf, 1);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003555
Markus Armbruster0f953052013-06-27 16:22:07 +02003556 val = qemu_opt_get_size(opts, "size", 0);
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003557 if (val != 0) {
Eric Blake130257d2015-10-26 16:34:57 -06003558 backend->u.ringbuf->has_size = true;
3559 backend->u.ringbuf->size = val;
Gerd Hoffmann1da48c62013-02-26 16:21:11 +01003560 }
3561}
3562
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003563static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
3564 Error **errp)
3565{
3566 const char *chardev = qemu_opt_get(opts, "chardev");
3567
3568 if (chardev == NULL) {
3569 error_setg(errp, "chardev: mux: no chardev given");
3570 return;
3571 }
Eric Blake130257d2015-10-26 16:34:57 -06003572 backend->u.mux = g_new0(ChardevMux, 1);
3573 backend->u.mux->chardev = g_strdup(chardev);
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02003574}
3575
Peter Maydelldafd3252014-09-02 11:24:13 +01003576static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
3577 Error **errp)
3578{
3579 bool is_listen = qemu_opt_get_bool(opts, "server", false);
3580 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
3581 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
3582 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
Corey Minyard5dd1f022014-10-02 11:17:37 -05003583 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003584 const char *path = qemu_opt_get(opts, "path");
3585 const char *host = qemu_opt_get(opts, "host");
3586 const char *port = qemu_opt_get(opts, "port");
3587 SocketAddress *addr;
3588
3589 if (!path) {
3590 if (!host) {
3591 error_setg(errp, "chardev: socket: no host given");
3592 return;
3593 }
3594 if (!port) {
3595 error_setg(errp, "chardev: socket: no port given");
3596 return;
3597 }
3598 }
3599
Eric Blake130257d2015-10-26 16:34:57 -06003600 backend->u.socket = g_new0(ChardevSocket, 1);
Peter Maydelldafd3252014-09-02 11:24:13 +01003601
Eric Blake130257d2015-10-26 16:34:57 -06003602 backend->u.socket->has_nodelay = true;
3603 backend->u.socket->nodelay = do_nodelay;
3604 backend->u.socket->has_server = true;
3605 backend->u.socket->server = is_listen;
3606 backend->u.socket->has_telnet = true;
3607 backend->u.socket->telnet = is_telnet;
3608 backend->u.socket->has_wait = true;
3609 backend->u.socket->wait = is_waitconnect;
3610 backend->u.socket->has_reconnect = true;
3611 backend->u.socket->reconnect = reconnect;
Peter Maydelldafd3252014-09-02 11:24:13 +01003612
3613 addr = g_new0(SocketAddress, 1);
3614 if (path) {
Eric Blake130257d2015-10-26 16:34:57 -06003615 addr->type = SOCKET_ADDRESS_KIND_UNIX;
3616 addr->u.q_unix = g_new0(UnixSocketAddress, 1);
3617 addr->u.q_unix->path = g_strdup(path);
Peter Maydelldafd3252014-09-02 11:24:13 +01003618 } else {
Eric Blake130257d2015-10-26 16:34:57 -06003619 addr->type = SOCKET_ADDRESS_KIND_INET;
3620 addr->u.inet = g_new0(InetSocketAddress, 1);
3621 addr->u.inet->host = g_strdup(host);
3622 addr->u.inet->port = g_strdup(port);
3623 addr->u.inet->has_to = qemu_opt_get(opts, "to");
3624 addr->u.inet->to = qemu_opt_get_number(opts, "to", 0);
3625 addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3626 addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3627 addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3628 addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
Peter Maydelldafd3252014-09-02 11:24:13 +01003629 }
Eric Blake130257d2015-10-26 16:34:57 -06003630 backend->u.socket->addr = addr;
Peter Maydelldafd3252014-09-02 11:24:13 +01003631}
3632
Peter Maydell90a14bf2014-09-02 11:24:15 +01003633static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
3634 Error **errp)
3635{
3636 const char *host = qemu_opt_get(opts, "host");
3637 const char *port = qemu_opt_get(opts, "port");
3638 const char *localaddr = qemu_opt_get(opts, "localaddr");
3639 const char *localport = qemu_opt_get(opts, "localport");
3640 bool has_local = false;
3641 SocketAddress *addr;
3642
3643 if (host == NULL || strlen(host) == 0) {
3644 host = "localhost";
3645 }
3646 if (port == NULL || strlen(port) == 0) {
3647 error_setg(errp, "chardev: udp: remote port not specified");
3648 return;
3649 }
3650 if (localport == NULL || strlen(localport) == 0) {
3651 localport = "0";
3652 } else {
3653 has_local = true;
3654 }
3655 if (localaddr == NULL || strlen(localaddr) == 0) {
3656 localaddr = "";
3657 } else {
3658 has_local = true;
3659 }
3660
Eric Blake130257d2015-10-26 16:34:57 -06003661 backend->u.udp = g_new0(ChardevUdp, 1);
Peter Maydell90a14bf2014-09-02 11:24:15 +01003662
3663 addr = g_new0(SocketAddress, 1);
Eric Blake130257d2015-10-26 16:34:57 -06003664 addr->type = SOCKET_ADDRESS_KIND_INET;
3665 addr->u.inet = g_new0(InetSocketAddress, 1);
3666 addr->u.inet->host = g_strdup(host);
3667 addr->u.inet->port = g_strdup(port);
3668 addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4");
3669 addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0);
3670 addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6");
3671 addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0);
3672 backend->u.udp->remote = addr;
Peter Maydell90a14bf2014-09-02 11:24:15 +01003673
3674 if (has_local) {
Eric Blake130257d2015-10-26 16:34:57 -06003675 backend->u.udp->has_local = true;
Peter Maydell90a14bf2014-09-02 11:24:15 +01003676 addr = g_new0(SocketAddress, 1);
Eric Blake130257d2015-10-26 16:34:57 -06003677 addr->type = SOCKET_ADDRESS_KIND_INET;
3678 addr->u.inet = g_new0(InetSocketAddress, 1);
3679 addr->u.inet->host = g_strdup(localaddr);
3680 addr->u.inet->port = g_strdup(localport);
3681 backend->u.udp->local = addr;
Peter Maydell90a14bf2014-09-02 11:24:15 +01003682 }
3683}
3684
Anthony Liguorid654f342013-03-05 23:21:28 +05303685typedef struct CharDriver {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003686 const char *name;
Gerd Hoffmann99aec012013-06-24 08:39:52 +02003687 ChardevBackendKind kind;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003688 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003689 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3690 ChardevReturn *ret, Error **errp);
Anthony Liguorid654f342013-03-05 23:21:28 +05303691} CharDriver;
3692
3693static GSList *backends;
3694
Peter Maydelle4d50d42014-09-02 11:24:17 +01003695void register_char_driver(const char *name, ChardevBackendKind kind,
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003696 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp),
3697 CharDriverState *(*create)(const char *id, ChardevBackend *backend,
3698 ChardevReturn *ret, Error **errp))
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003699{
3700 CharDriver *s;
3701
3702 s = g_malloc0(sizeof(*s));
3703 s->name = g_strdup(name);
3704 s->kind = kind;
3705 s->parse = parse;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02003706 s->create = create;
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +01003707
3708 backends = g_slist_append(backends, s);
3709}
3710
Anthony Liguorif69554b2011-08-15 11:17:37 -05003711CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003712 void (*init)(struct CharDriverState *s),
3713 Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003714{
Markus Armbruster0aff6372014-05-19 18:57:35 +02003715 Error *local_err = NULL;
Anthony Liguorid654f342013-03-05 23:21:28 +05303716 CharDriver *cd;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003717 CharDriverState *chr;
Anthony Liguorid654f342013-03-05 23:21:28 +05303718 GSList *i;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003719 ChardevReturn *ret = NULL;
3720 ChardevBackend *backend;
3721 const char *id = qemu_opts_id(opts);
3722 char *bid = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003723
Peter Maydella61ae7f2014-09-02 11:24:16 +01003724 if (id == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003725 error_setg(errp, "chardev: no id specified");
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003726 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003727 }
3728
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003729 if (qemu_opt_get(opts, "backend") == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003730 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003731 qemu_opts_id(opts));
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003732 goto err;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +00003733 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303734 for (i = backends; i; i = i->next) {
3735 cd = i->data;
3736
3737 if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003738 break;
Anthony Liguorid654f342013-03-05 23:21:28 +05303739 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003740 }
Anthony Liguorid654f342013-03-05 23:21:28 +05303741 if (i == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01003742 error_setg(errp, "chardev: backend \"%s\" not found",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003743 qemu_opt_get(opts, "backend"));
Gerd Hoffmanne6682872013-06-24 08:39:51 +02003744 goto err;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003745 }
3746
Peter Maydella61ae7f2014-09-02 11:24:16 +01003747 backend = g_new0(ChardevBackend, 1);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003748
3749 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +01003750 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02003751 }
Peter Maydella61ae7f2014-09-02 11:24:16 +01003752
3753 chr = NULL;
Eric Blake130257d2015-10-26 16:34:57 -06003754 backend->type = cd->kind;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003755 if (cd->parse) {
3756 cd->parse(opts, backend, &local_err);
3757 if (local_err) {
3758 error_propagate(errp, local_err);
3759 goto qapi_out;
3760 }
3761 }
3762 ret = qmp_chardev_add(bid ? bid : id, backend, errp);
3763 if (!ret) {
3764 goto qapi_out;
3765 }
3766
3767 if (bid) {
3768 qapi_free_ChardevBackend(backend);
3769 qapi_free_ChardevReturn(ret);
3770 backend = g_new0(ChardevBackend, 1);
Eric Blake130257d2015-10-26 16:34:57 -06003771 backend->u.mux = g_new0(ChardevMux, 1);
3772 backend->type = CHARDEV_BACKEND_KIND_MUX;
3773 backend->u.mux->chardev = g_strdup(bid);
Peter Maydella61ae7f2014-09-02 11:24:16 +01003774 ret = qmp_chardev_add(id, backend, errp);
3775 if (!ret) {
3776 chr = qemu_chr_find(bid);
3777 qemu_chr_delete(chr);
3778 chr = NULL;
3779 goto qapi_out;
3780 }
3781 }
3782
3783 chr = qemu_chr_find(id);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003784 chr->opts = opts;
Peter Maydella61ae7f2014-09-02 11:24:16 +01003785
3786qapi_out:
3787 qapi_free_ChardevBackend(backend);
3788 qapi_free_ChardevReturn(ret);
3789 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003790 return chr;
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003791
3792err:
3793 qemu_opts_del(opts);
3794 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003795}
3796
Anthony Liguori27143a42011-08-15 11:17:36 -05003797CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00003798{
3799 const char *p;
3800 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003801 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003802 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003803
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003804 if (strstart(filename, "chardev:", &p)) {
3805 return qemu_chr_find(p);
3806 }
3807
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003808 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003809 if (!opts)
3810 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003811
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003812 chr = qemu_chr_new_from_opts(opts, init, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003813 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003814 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +02003815 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003816 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
Hans de Goede456d6062013-03-27 20:29:40 +01003817 qemu_chr_fe_claim_no_fail(chr);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02003818 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00003819 }
3820 return chr;
3821}
3822
Anthony Liguori15f31512011-08-15 11:17:35 -05003823void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
Paolo Bonzinic48855e2010-12-23 13:42:48 +01003824{
3825 if (chr->chr_set_echo) {
3826 chr->chr_set_echo(chr, echo);
3827 }
3828}
3829
Hans de Goede8e25daa2013-03-26 11:07:57 +01003830void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003831{
Hans de Goede8e25daa2013-03-26 11:07:57 +01003832 if (chr->fe_open == fe_open) {
Hans de Goedec0c4bd22013-03-26 11:07:55 +01003833 return;
3834 }
Hans de Goede8e25daa2013-03-26 11:07:57 +01003835 chr->fe_open = fe_open;
Hans de Goede574b7112013-03-26 11:07:58 +01003836 if (chr->chr_set_fe_open) {
3837 chr->chr_set_fe_open(chr, fe_open);
Hans de Goede7c32c4f2011-03-24 11:12:02 +01003838 }
3839}
3840
Marc-André Lureaud61b0c92013-12-01 22:23:39 +01003841void qemu_chr_fe_event(struct CharDriverState *chr, int event)
3842{
3843 if (chr->chr_fe_event) {
3844 chr->chr_fe_event(chr, event);
3845 }
3846}
3847
Kevin Wolf2c8a5942013-03-19 13:38:09 +01003848int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
3849 GIOFunc func, void *user_data)
Anthony Liguori23673ca2013-03-05 23:21:23 +05303850{
3851 GSource *src;
3852 guint tag;
3853
3854 if (s->chr_add_watch == NULL) {
3855 return -ENOSYS;
3856 }
3857
3858 src = s->chr_add_watch(s, cond);
Paolo Bonzini62c339c2014-07-24 16:08:04 +02003859 if (!src) {
3860 return -EINVAL;
3861 }
3862
Anthony Liguori23673ca2013-03-05 23:21:23 +05303863 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
3864 tag = g_source_attach(src, NULL);
3865 g_source_unref(src);
3866
3867 return tag;
3868}
3869
Hans de Goede44c473d2013-03-27 20:29:39 +01003870int qemu_chr_fe_claim(CharDriverState *s)
3871{
3872 if (s->avail_connections < 1) {
3873 return -1;
3874 }
3875 s->avail_connections--;
3876 return 0;
3877}
3878
3879void qemu_chr_fe_claim_no_fail(CharDriverState *s)
3880{
3881 if (qemu_chr_fe_claim(s) != 0) {
3882 fprintf(stderr, "%s: error chardev \"%s\" already used\n",
3883 __func__, s->label);
3884 exit(1);
3885 }
3886}
3887
3888void qemu_chr_fe_release(CharDriverState *s)
3889{
3890 s->avail_connections++;
3891}
3892
Marc-André Lureau1ad78ea2015-06-22 18:20:18 +02003893void qemu_chr_free(CharDriverState *chr)
aliguori6f97dba2008-10-31 18:49:55 +00003894{
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003895 if (chr->chr_close) {
aliguori6f97dba2008-10-31 18:49:55 +00003896 chr->chr_close(chr);
Gerd Hoffmann2274ae92012-10-15 09:30:59 +02003897 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003898 g_free(chr->filename);
3899 g_free(chr->label);
Markus Armbruster4ad60882014-12-03 11:28:02 +01003900 qemu_opts_del(chr->opts);
Anthony Liguori7267c092011-08-20 22:09:37 -05003901 g_free(chr);
aliguori6f97dba2008-10-31 18:49:55 +00003902}
3903
Marc-André Lureau1ad78ea2015-06-22 18:20:18 +02003904void qemu_chr_delete(CharDriverState *chr)
3905{
3906 QTAILQ_REMOVE(&chardevs, chr, next);
3907 qemu_chr_free(chr);
3908}
3909
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003910ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +00003911{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003912 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +00003913 CharDriverState *chr;
3914
Blue Swirl72cf2d42009-09-12 07:36:22 +00003915 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003916 ChardevInfoList *info = g_malloc0(sizeof(*info));
3917 info->value = g_malloc0(sizeof(*info->value));
3918 info->value->label = g_strdup(chr->label);
3919 info->value->filename = g_strdup(chr->filename);
Laszlo Ersek32a97ea2014-06-26 17:50:03 +02003920 info->value->frontend_open = chr->fe_open;
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003921
3922 info->next = chr_list;
3923 chr_list = info;
aliguori6f97dba2008-10-31 18:49:55 +00003924 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02003925
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03003926 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00003927}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003928
Martin Kletzander77d1c3c2014-02-01 12:52:42 +01003929ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
3930{
3931 ChardevBackendInfoList *backend_list = NULL;
3932 CharDriver *c = NULL;
3933 GSList *i = NULL;
3934
3935 for (i = backends; i; i = i->next) {
3936 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
3937 c = i->data;
3938 info->value = g_malloc0(sizeof(*info->value));
3939 info->value->name = g_strdup(c->name);
3940
3941 info->next = backend_list;
3942 backend_list = info;
3943 }
3944
3945 return backend_list;
3946}
3947
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003948CharDriverState *qemu_chr_find(const char *name)
3949{
3950 CharDriverState *chr;
3951
Blue Swirl72cf2d42009-09-12 07:36:22 +00003952 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02003953 if (strcmp(chr->label, name) != 0)
3954 continue;
3955 return chr;
3956 }
3957 return NULL;
3958}
Anthony Liguori0beb4942011-12-22 15:29:25 -06003959
3960/* Get a character (serial) device interface. */
3961CharDriverState *qemu_char_get_next_serial(void)
3962{
3963 static int next_serial;
Hans de Goede456d6062013-03-27 20:29:40 +01003964 CharDriverState *chr;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003965
3966 /* FIXME: This function needs to go away: use chardev properties! */
Hans de Goede456d6062013-03-27 20:29:40 +01003967
3968 while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
3969 chr = serial_hds[next_serial++];
3970 qemu_chr_fe_claim_no_fail(chr);
3971 return chr;
3972 }
3973 return NULL;
Anthony Liguori0beb4942011-12-22 15:29:25 -06003974}
3975
Paolo Bonzini4d454572012-11-26 16:03:42 +01003976QemuOptsList qemu_chardev_opts = {
3977 .name = "chardev",
3978 .implied_opt_name = "backend",
3979 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
3980 .desc = {
3981 {
3982 .name = "backend",
3983 .type = QEMU_OPT_STRING,
3984 },{
3985 .name = "path",
3986 .type = QEMU_OPT_STRING,
3987 },{
3988 .name = "host",
3989 .type = QEMU_OPT_STRING,
3990 },{
3991 .name = "port",
3992 .type = QEMU_OPT_STRING,
3993 },{
3994 .name = "localaddr",
3995 .type = QEMU_OPT_STRING,
3996 },{
3997 .name = "localport",
3998 .type = QEMU_OPT_STRING,
3999 },{
4000 .name = "to",
4001 .type = QEMU_OPT_NUMBER,
4002 },{
4003 .name = "ipv4",
4004 .type = QEMU_OPT_BOOL,
4005 },{
4006 .name = "ipv6",
4007 .type = QEMU_OPT_BOOL,
4008 },{
4009 .name = "wait",
4010 .type = QEMU_OPT_BOOL,
4011 },{
4012 .name = "server",
4013 .type = QEMU_OPT_BOOL,
4014 },{
4015 .name = "delay",
4016 .type = QEMU_OPT_BOOL,
4017 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -05004018 .name = "reconnect",
4019 .type = QEMU_OPT_NUMBER,
4020 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004021 .name = "telnet",
4022 .type = QEMU_OPT_BOOL,
4023 },{
4024 .name = "width",
4025 .type = QEMU_OPT_NUMBER,
4026 },{
4027 .name = "height",
4028 .type = QEMU_OPT_NUMBER,
4029 },{
4030 .name = "cols",
4031 .type = QEMU_OPT_NUMBER,
4032 },{
4033 .name = "rows",
4034 .type = QEMU_OPT_NUMBER,
4035 },{
4036 .name = "mux",
4037 .type = QEMU_OPT_BOOL,
4038 },{
4039 .name = "signal",
4040 .type = QEMU_OPT_BOOL,
4041 },{
4042 .name = "name",
4043 .type = QEMU_OPT_STRING,
4044 },{
4045 .name = "debug",
4046 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +08004047 },{
Markus Armbruster3949e592013-02-06 21:27:24 +01004048 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +01004049 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +02004050 },{
4051 .name = "chardev",
4052 .type = QEMU_OPT_STRING,
Olga Krishtal31e38a22015-12-04 09:42:04 +03004053 },{
4054 .name = "append",
4055 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004056 },
4057 { /* end of list */ }
4058 },
4059};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004060
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004061#ifdef _WIN32
4062
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004063static CharDriverState *qmp_chardev_open_file(const char *id,
4064 ChardevBackend *backend,
4065 ChardevReturn *ret,
4066 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004067{
Eric Blake130257d2015-10-26 16:34:57 -06004068 ChardevFile *file = backend->u.file;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004069 HANDLE out;
4070
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004071 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004072 error_setg(errp, "input file not supported");
4073 return NULL;
4074 }
4075
4076 out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4077 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
4078 if (out == INVALID_HANDLE_VALUE) {
4079 error_setg(errp, "open %s failed", file->out);
4080 return NULL;
4081 }
4082 return qemu_chr_open_win_file(out);
4083}
4084
Paolo Bonzini6511d392015-09-29 15:08:05 +02004085static CharDriverState *qmp_chardev_open_serial(const char *id,
4086 ChardevBackend *backend,
4087 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004088 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004089{
Eric Blake130257d2015-10-26 16:34:57 -06004090 ChardevHostdev *serial = backend->u.serial;
Paolo Bonzini6511d392015-09-29 15:08:05 +02004091 return qemu_chr_open_win_path(serial->device, errp);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004092}
4093
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004094#else /* WIN32 */
4095
4096static int qmp_chardev_open_file_source(char *src, int flags,
4097 Error **errp)
4098{
4099 int fd = -1;
4100
4101 TFR(fd = qemu_open(src, flags, 0666));
4102 if (fd == -1) {
Gerd Hoffmann20c39762013-06-24 08:39:48 +02004103 error_setg_file_open(errp, errno, src);
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004104 }
4105 return fd;
4106}
4107
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004108static CharDriverState *qmp_chardev_open_file(const char *id,
4109 ChardevBackend *backend,
4110 ChardevReturn *ret,
4111 Error **errp)
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004112{
Eric Blake130257d2015-10-26 16:34:57 -06004113 ChardevFile *file = backend->u.file;
Markus Armbruster5f758362014-05-19 18:57:34 +02004114 int flags, in = -1, out;
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004115
Olga Krishtal31e38a22015-12-04 09:42:04 +03004116 flags = O_WRONLY | O_CREAT | O_BINARY;
4117 if (file->has_append && file->append) {
4118 flags |= O_APPEND;
4119 } else {
4120 flags |= O_TRUNC;
4121 }
4122
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004123 out = qmp_chardev_open_file_source(file->out, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004124 if (out < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004125 return NULL;
4126 }
4127
Gerd Hoffmanne859eda2013-06-24 08:39:47 +02004128 if (file->has_in) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004129 flags = O_RDONLY;
4130 in = qmp_chardev_open_file_source(file->in, flags, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004131 if (in < 0) {
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004132 qemu_close(out);
4133 return NULL;
4134 }
4135 }
4136
4137 return qemu_chr_open_fd(in, out);
4138}
4139
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004140#ifdef HAVE_CHARDEV_SERIAL
Paolo Bonzini6511d392015-09-29 15:08:05 +02004141static CharDriverState *qmp_chardev_open_serial(const char *id,
4142 ChardevBackend *backend,
4143 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004144 Error **errp)
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004145{
Eric Blake130257d2015-10-26 16:34:57 -06004146 ChardevHostdev *serial = backend->u.serial;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004147 int fd;
4148
4149 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004150 if (fd < 0) {
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004151 return NULL;
4152 }
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +01004153 qemu_set_nonblock(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004154 return qemu_chr_open_tty_fd(fd);
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004155}
Paolo Bonzini6511d392015-09-29 15:08:05 +02004156#endif
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004157
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004158#ifdef HAVE_CHARDEV_PARPORT
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004159static CharDriverState *qmp_chardev_open_parallel(const char *id,
4160 ChardevBackend *backend,
4161 ChardevReturn *ret,
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004162 Error **errp)
4163{
Eric Blake130257d2015-10-26 16:34:57 -06004164 ChardevHostdev *parallel = backend->u.parallel;
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004165 int fd;
4166
4167 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004168 if (fd < 0) {
Markus Armbrusterd36b2b92013-02-13 15:54:16 +01004169 return NULL;
4170 }
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004171 return qemu_chr_open_pp_fd(fd, errp);
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004172}
Paolo Bonzinid809ab92015-10-12 09:46:23 +02004173#endif
Gerd Hoffmannd59044e2012-12-19 13:50:29 +01004174
Gerd Hoffmannffbdbe52012-12-19 13:13:57 +01004175#endif /* WIN32 */
4176
Corey Minyard5008e5b2014-10-08 07:11:55 -05004177static void socket_try_connect(CharDriverState *chr)
4178{
4179 Error *err = NULL;
4180
4181 if (!qemu_chr_open_socket_fd(chr, &err)) {
4182 check_report_connect_error(chr, err);
4183 }
4184}
4185
Corey Minyard5dd1f022014-10-02 11:17:37 -05004186static gboolean socket_reconnect_timeout(gpointer opaque)
4187{
4188 CharDriverState *chr = opaque;
4189 TCPCharDriver *s = chr->opaque;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004190
4191 s->reconnect_timer = 0;
4192
4193 if (chr->be_open) {
4194 return false;
4195 }
4196
Corey Minyard5008e5b2014-10-08 07:11:55 -05004197 socket_try_connect(chr);
Corey Minyard5dd1f022014-10-02 11:17:37 -05004198
4199 return false;
4200}
4201
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004202static CharDriverState *qmp_chardev_open_socket(const char *id,
4203 ChardevBackend *backend,
4204 ChardevReturn *ret,
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004205 Error **errp)
4206{
Corey Minyard43ded1a2014-10-02 11:17:34 -05004207 CharDriverState *chr;
4208 TCPCharDriver *s;
Eric Blake130257d2015-10-26 16:34:57 -06004209 ChardevSocket *sock = backend->u.socket;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004210 SocketAddress *addr = sock->addr;
4211 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
4212 bool is_listen = sock->has_server ? sock->server : true;
4213 bool is_telnet = sock->has_telnet ? sock->telnet : false;
4214 bool is_waitconnect = sock->has_wait ? sock->wait : false;
Corey Minyard5dd1f022014-10-02 11:17:37 -05004215 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004216
4217 chr = qemu_chr_alloc();
Markus Armbruster2d528d42015-09-14 13:54:03 +02004218 s = g_new0(TCPCharDriver, 1);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004219
4220 s->fd = -1;
4221 s->listen_fd = -1;
Eric Blake130257d2015-10-26 16:34:57 -06004222 s->is_unix = addr->type == SOCKET_ADDRESS_KIND_UNIX;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004223 s->is_listen = is_listen;
4224 s->is_telnet = is_telnet;
Corey Minyard43ded1a2014-10-02 11:17:34 -05004225 s->do_nodelay = do_nodelay;
Corey Minyardcfb429c2014-10-02 11:17:35 -05004226 qapi_copy_SocketAddress(&s->addr, sock->addr);
Corey Minyard43ded1a2014-10-02 11:17:34 -05004227
4228 chr->opaque = s;
4229 chr->chr_write = tcp_chr_write;
4230 chr->chr_sync_read = tcp_chr_sync_read;
4231 chr->chr_close = tcp_chr_close;
4232 chr->get_msgfds = tcp_get_msgfds;
4233 chr->set_msgfds = tcp_set_msgfds;
4234 chr->chr_add_client = tcp_chr_add_client;
4235 chr->chr_add_watch = tcp_chr_add_watch;
4236 chr->chr_update_read_handler = tcp_chr_update_read_handler;
4237 /* be isn't opened until we get a connection */
4238 chr->explicit_be_open = true;
4239
4240 chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
Corey Minyard16cc4ff2014-10-02 11:17:36 -05004241 SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
4242 addr, is_listen, is_telnet);
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004243
4244 if (is_listen) {
Corey Minyard43ded1a2014-10-02 11:17:34 -05004245 if (is_telnet) {
4246 s->do_telnetopt = 1;
4247 }
Corey Minyard5dd1f022014-10-02 11:17:37 -05004248 } else if (reconnect > 0) {
4249 s->reconnect_time = reconnect;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004250 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004251
Corey Minyard5008e5b2014-10-08 07:11:55 -05004252 if (s->reconnect_time) {
4253 socket_try_connect(chr);
4254 } else if (!qemu_chr_open_socket_fd(chr, errp)) {
4255 g_free(s);
4256 g_free(chr->filename);
4257 g_free(chr);
4258 return NULL;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004259 }
Corey Minyard43ded1a2014-10-02 11:17:34 -05004260
4261 if (is_listen && is_waitconnect) {
4262 fprintf(stderr, "QEMU waiting for connection on: %s\n",
4263 chr->filename);
4264 tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
4265 qemu_set_nonblock(s->listen_fd);
4266 }
4267
4268 return chr;
Gerd Hoffmannf6bd5d62012-12-20 13:53:12 +01004269}
4270
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004271static CharDriverState *qmp_chardev_open_udp(const char *id,
4272 ChardevBackend *backend,
4273 ChardevReturn *ret,
Lei Li08d0ab32013-05-20 14:51:03 +08004274 Error **errp)
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004275{
Eric Blake130257d2015-10-26 16:34:57 -06004276 ChardevUdp *udp = backend->u.udp;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004277 int fd;
4278
Lei Li08d0ab32013-05-20 14:51:03 +08004279 fd = socket_dgram(udp->remote, udp->local, errp);
Markus Armbruster5f758362014-05-19 18:57:34 +02004280 if (fd < 0) {
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +01004281 return NULL;
4282 }
4283 return qemu_chr_open_udp_fd(fd);
4284}
4285
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004286ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
4287 Error **errp)
4288{
4289 ChardevReturn *ret = g_new0(ChardevReturn, 1);
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004290 CharDriverState *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004291 Error *local_err = NULL;
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004292 GSList *i;
4293 CharDriver *cd;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004294
4295 chr = qemu_chr_find(id);
4296 if (chr) {
4297 error_setg(errp, "Chardev '%s' already exists", id);
4298 g_free(ret);
4299 return NULL;
4300 }
4301
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004302 for (i = backends; i; i = i->next) {
4303 cd = i->data;
4304
Eric Blake130257d2015-10-26 16:34:57 -06004305 if (cd->kind == backend->type) {
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004306 chr = cd->create(id, backend, ret, &local_err);
4307 if (local_err) {
4308 error_propagate(errp, local_err);
4309 goto out_error;
4310 }
Gerd Hoffmannedb2fb32013-02-21 16:16:42 +01004311 break;
4312 }
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004313 }
4314
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004315 if (chr == NULL) {
Paolo Bonzini1c3af0f2015-10-12 09:51:41 +02004316 assert(!i);
4317 error_setg(errp, "chardev backend not available");
4318 goto out_error;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004319 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004320
4321 chr->label = g_strdup(id);
4322 chr->avail_connections =
Eric Blake130257d2015-10-26 16:34:57 -06004323 (backend->type == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004324 if (!chr->filename) {
Eric Blake130257d2015-10-26 16:34:57 -06004325 chr->filename = g_strdup(ChardevBackendKind_lookup[backend->type]);
Paolo Bonzinieaeba652015-09-29 14:54:05 +02004326 }
4327 if (!chr->explicit_be_open) {
4328 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
4329 }
4330 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
4331 return ret;
4332
4333out_error:
4334 g_free(ret);
4335 return NULL;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004336}
4337
4338void qmp_chardev_remove(const char *id, Error **errp)
4339{
4340 CharDriverState *chr;
4341
4342 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08004343 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01004344 error_setg(errp, "Chardev '%s' not found", id);
4345 return;
4346 }
4347 if (chr->chr_can_read || chr->chr_read ||
4348 chr->chr_event || chr->handler_opaque) {
4349 error_setg(errp, "Chardev '%s' is busy", id);
4350 return;
4351 }
4352 qemu_chr_delete(chr);
4353}
Anthony Liguorid654f342013-03-05 23:21:28 +05304354
4355static void register_types(void)
4356{
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004357 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL,
Paolo Bonzini0d649922015-09-29 15:24:29 +02004358 qemu_chr_open_null);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004359 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET,
Paolo Bonzinidbba8d12015-09-29 15:15:53 +02004360 qemu_chr_parse_socket, qmp_chardev_open_socket);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004361 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp,
Paolo Bonzinie79b80d2015-09-29 15:17:20 +02004362 qmp_chardev_open_udp);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004363 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
Paolo Bonzini479f09a2015-09-29 15:51:14 +02004364 qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004365 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE,
Paolo Bonzinifd5b0362015-09-29 15:06:02 +02004366 qemu_chr_parse_file_out, qmp_chardev_open_file);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004367 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO,
Paolo Bonzini8c84b252015-09-29 15:40:28 +02004368 qemu_chr_parse_stdio, qemu_chr_open_stdio);
Paolo Bonzini6511d392015-09-29 15:08:05 +02004369#if defined HAVE_CHARDEV_SERIAL
Peter Maydelle4d50d42014-09-02 11:24:17 +01004370 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004371 qemu_chr_parse_serial, qmp_chardev_open_serial);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004372 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL,
Paolo Bonzini6511d392015-09-29 15:08:05 +02004373 qemu_chr_parse_serial, qmp_chardev_open_serial);
4374#endif
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004375#ifdef HAVE_CHARDEV_PARPORT
Peter Maydelle4d50d42014-09-02 11:24:17 +01004376 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004377 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
Peter Maydelle4d50d42014-09-02 11:24:17 +01004378 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL,
Paolo Bonzini38bfb1a2015-10-12 09:49:28 +02004379 qemu_chr_parse_parallel, qmp_chardev_open_parallel);
4380#endif
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004381#ifdef HAVE_CHARDEV_PTY
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004382 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL,
Paolo Bonzinic2e75a42015-09-29 15:23:42 +02004383 qemu_chr_open_pty);
4384#endif
Paolo Bonzini122e5ed2015-09-29 15:42:04 +02004385#ifdef _WIN32
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004386 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL,
Paolo Bonzini122e5ed2015-09-29 15:42:04 +02004387 qemu_chr_open_win_con);
4388#endif
Peter Maydelle4d50d42014-09-02 11:24:17 +01004389 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE,
Paolo Bonzini20cbe7a2015-09-29 15:14:07 +02004390 qemu_chr_parse_pipe, qemu_chr_open_pipe);
Paolo Bonzini4ca17282015-09-29 14:55:59 +02004391 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux,
Paolo Bonzini3c0e5a42015-09-29 15:27:24 +02004392 qemu_chr_open_mux);
Markus Armbrusterc11ed962013-07-26 16:44:33 +02004393 /* Bug-compatibility: */
Peter Maydelle4d50d42014-09-02 11:24:17 +01004394 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY,
Paolo Bonzini479f09a2015-09-29 15:51:14 +02004395 qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf);
Michael Roth7b7ab182013-07-30 13:04:22 -05004396 /* this must be done after machine init, since we register FEs with muxes
4397 * as part of realize functions like serial_isa_realizefn when -nographic
4398 * is specified
4399 */
4400 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05304401}
4402
4403type_init(register_types);