aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 25 | #include "qemu-common.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 26 | #include "qemu/cutils.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 27 | #include "monitor/monitor.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 28 | #include "sysemu/sysemu.h" |
Max Reitz | da31d59 | 2016-03-16 19:54:32 +0100 | [diff] [blame] | 29 | #include "sysemu/block-backend.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 30 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 31 | #include "qemu/timer.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 32 | #include "sysemu/char.h" |
aurel32 | cf3ebac | 2008-11-01 00:53:09 +0000 | [diff] [blame] | 33 | #include "hw/usb.h" |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 34 | #include "qmp-commands.h" |
Eric Blake | 37f9e0a | 2016-06-09 10:48:45 -0600 | [diff] [blame] | 35 | #include "qapi/clone-visitor.h" |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 36 | #include "qapi-visit.h" |
Daniel P. Berrange | e9cf2fe | 2015-11-23 15:29:59 +0000 | [diff] [blame] | 37 | #include "qemu/base64.h" |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 38 | #include "io/channel-socket.h" |
| 39 | #include "io/channel-file.h" |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 40 | #include "io/channel-tls.h" |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 41 | #include "sysemu/replay.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 42 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 43 | #include <zlib.h> |
| 44 | |
| 45 | #ifndef _WIN32 |
| 46 | #include <sys/times.h> |
| 47 | #include <sys/wait.h> |
| 48 | #include <termios.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 49 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 50 | #include <sys/resource.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 51 | #include <sys/socket.h> |
| 52 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 53 | #include <net/if.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 54 | #include <arpa/inet.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 55 | #include <netdb.h> |
| 56 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 57 | #ifdef CONFIG_BSD |
Michael Tokarev | 3294ce1 | 2012-06-02 23:43:33 +0400 | [diff] [blame] | 58 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 59 | #include <dev/ppbus/ppi.h> |
| 60 | #include <dev/ppbus/ppbconf.h> |
| 61 | #elif defined(__DragonFly__) |
| 62 | #include <dev/misc/ppi/ppi.h> |
| 63 | #include <bus/ppbus/ppbconf.h> |
| 64 | #endif |
Aurelien Jarno | bbe813a | 2009-11-30 15:42:59 +0100 | [diff] [blame] | 65 | #else |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 66 | #ifdef __linux__ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 67 | #include <linux/ppdev.h> |
| 68 | #include <linux/parport.h> |
| 69 | #endif |
| 70 | #ifdef __sun__ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 71 | #include <sys/ethernet.h> |
| 72 | #include <sys/sockio.h> |
| 73 | #include <netinet/arp.h> |
| 74 | #include <netinet/in.h> |
| 75 | #include <netinet/in_systm.h> |
| 76 | #include <netinet/ip.h> |
| 77 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 78 | #include <netinet/udp.h> |
| 79 | #include <netinet/tcp.h> |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 80 | #endif |
| 81 | #endif |
| 82 | #endif |
| 83 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 84 | #include "qemu/sockets.h" |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 85 | #include "ui/qemu-spice.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 86 | |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 87 | #define READ_BUF_LEN 4096 |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 88 | #define READ_RETRIES 10 |
Michael S. Tsirkin | c409572 | 2014-11-02 18:48:32 +0200 | [diff] [blame] | 89 | #define TCP_MAX_FDS 16 |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 90 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 91 | /***********************************************************/ |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 92 | /* Socket address helpers */ |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 93 | |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 94 | static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr, |
| 95 | bool is_listen, bool is_telnet) |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 96 | { |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 97 | switch (addr->type) { |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 98 | case SOCKET_ADDRESS_KIND_INET: |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 99 | return g_strdup_printf("%s%s:%s:%s%s", prefix, |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 100 | is_telnet ? "telnet" : "tcp", |
| 101 | addr->u.inet.data->host, |
| 102 | addr->u.inet.data->port, |
| 103 | is_listen ? ",server" : ""); |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 104 | break; |
| 105 | case SOCKET_ADDRESS_KIND_UNIX: |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 106 | return g_strdup_printf("%sunix:%s%s", prefix, |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 107 | addr->u.q_unix.data->path, |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 108 | is_listen ? ",server" : ""); |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 109 | break; |
| 110 | case SOCKET_ADDRESS_KIND_FD: |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 111 | return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.data->str, |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 112 | is_listen ? ",server" : ""); |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 113 | break; |
| 114 | default: |
| 115 | abort(); |
| 116 | } |
| 117 | } |
| 118 | |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 119 | static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len, |
| 120 | struct sockaddr_storage *ps, socklen_t ps_len, |
| 121 | bool is_listen, bool is_telnet) |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 122 | { |
Corey Minyard | 01ca519 | 2014-10-02 11:17:38 -0500 | [diff] [blame] | 123 | char shost[NI_MAXHOST], sserv[NI_MAXSERV]; |
| 124 | char phost[NI_MAXHOST], pserv[NI_MAXSERV]; |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 125 | const char *left = "", *right = ""; |
| 126 | |
| 127 | switch (ss->ss_family) { |
| 128 | #ifndef _WIN32 |
| 129 | case AF_UNIX: |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 130 | return g_strdup_printf("unix:%s%s", |
| 131 | ((struct sockaddr_un *)(ss))->sun_path, |
| 132 | is_listen ? ",server" : ""); |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 133 | #endif |
| 134 | case AF_INET6: |
| 135 | left = "["; |
| 136 | right = "]"; |
| 137 | /* fall through */ |
| 138 | case AF_INET: |
Corey Minyard | 01ca519 | 2014-10-02 11:17:38 -0500 | [diff] [blame] | 139 | getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost), |
| 140 | sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV); |
| 141 | getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost), |
| 142 | pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV); |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 143 | return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s", |
| 144 | is_telnet ? "telnet" : "tcp", |
| 145 | left, shost, right, sserv, |
| 146 | is_listen ? ",server" : "", |
| 147 | left, phost, right, pserv); |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 148 | |
| 149 | default: |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 150 | return g_strdup_printf("unknown"); |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 154 | /***********************************************************/ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 155 | /* character device */ |
| 156 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 157 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
| 158 | QTAILQ_HEAD_INITIALIZER(chardevs); |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 159 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 160 | static void qemu_chr_free_common(CharDriverState *chr); |
| 161 | |
| 162 | CharDriverState *qemu_chr_alloc(ChardevCommon *backend, Error **errp) |
Paolo Bonzini | db39fcf | 2014-06-18 08:43:55 +0200 | [diff] [blame] | 163 | { |
| 164 | CharDriverState *chr = g_malloc0(sizeof(CharDriverState)); |
Paolo Bonzini | f3db17b | 2014-06-25 09:04:57 +0200 | [diff] [blame] | 165 | qemu_mutex_init(&chr->chr_write_lock); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 166 | |
| 167 | if (backend->has_logfile) { |
| 168 | int flags = O_WRONLY | O_CREAT; |
| 169 | if (backend->has_logappend && |
| 170 | backend->logappend) { |
| 171 | flags |= O_APPEND; |
| 172 | } else { |
| 173 | flags |= O_TRUNC; |
| 174 | } |
| 175 | chr->logfd = qemu_open(backend->logfile, flags, 0666); |
| 176 | if (chr->logfd < 0) { |
| 177 | error_setg_errno(errp, errno, |
| 178 | "Unable to open logfile %s", |
| 179 | backend->logfile); |
| 180 | g_free(chr); |
| 181 | return NULL; |
| 182 | } |
| 183 | } else { |
| 184 | chr->logfd = -1; |
| 185 | } |
| 186 | |
Paolo Bonzini | db39fcf | 2014-06-18 08:43:55 +0200 | [diff] [blame] | 187 | return chr; |
| 188 | } |
| 189 | |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 190 | void qemu_chr_be_event(CharDriverState *s, int event) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 191 | { |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 192 | /* Keep track if the char device is open */ |
| 193 | switch (event) { |
| 194 | case CHR_EVENT_OPENED: |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 195 | s->be_open = 1; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 196 | break; |
| 197 | case CHR_EVENT_CLOSED: |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 198 | s->be_open = 0; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 202 | if (!s->chr_event) |
| 203 | return; |
| 204 | s->chr_event(s->handler_opaque, event); |
| 205 | } |
| 206 | |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 207 | void qemu_chr_be_generic_open(CharDriverState *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 208 | { |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 209 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 212 | |
| 213 | /* Not reporting errors from writing to logfile, as logs are |
| 214 | * defined to be "best effort" only */ |
| 215 | static void qemu_chr_fe_write_log(CharDriverState *s, |
| 216 | const uint8_t *buf, size_t len) |
| 217 | { |
| 218 | size_t done = 0; |
| 219 | ssize_t ret; |
| 220 | |
| 221 | if (s->logfd < 0) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | while (done < len) { |
Daniel P. Berrange | 53628ef | 2016-03-31 16:29:27 +0100 | [diff] [blame] | 226 | retry: |
| 227 | ret = write(s->logfd, buf + done, len - done); |
| 228 | if (ret == -1 && errno == EAGAIN) { |
| 229 | g_usleep(100); |
| 230 | goto retry; |
| 231 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 232 | |
| 233 | if (ret <= 0) { |
| 234 | return; |
| 235 | } |
| 236 | done += ret; |
| 237 | } |
| 238 | } |
| 239 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 240 | static int qemu_chr_fe_write_buffer(CharDriverState *s, const uint8_t *buf, int len, int *offset) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 241 | { |
Igor Mammedov | 0931304 | 2014-06-25 10:00:41 +0200 | [diff] [blame] | 242 | int res = 0; |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 243 | *offset = 0; |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 244 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 245 | qemu_mutex_lock(&s->chr_write_lock); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 246 | while (*offset < len) { |
Daniel P. Berrange | 53628ef | 2016-03-31 16:29:27 +0100 | [diff] [blame] | 247 | retry: |
| 248 | res = s->chr_write(s, buf + *offset, len - *offset); |
| 249 | if (res < 0 && errno == EAGAIN) { |
| 250 | g_usleep(100); |
| 251 | goto retry; |
| 252 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 253 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 254 | if (res <= 0) { |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 258 | *offset += res; |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 259 | } |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 260 | if (*offset > 0) { |
| 261 | qemu_chr_fe_write_log(s, buf, *offset); |
| 262 | } |
| 263 | qemu_mutex_unlock(&s->chr_write_lock); |
| 264 | |
| 265 | return res; |
| 266 | } |
| 267 | |
| 268 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
| 269 | { |
| 270 | int ret; |
| 271 | |
| 272 | if (s->replay && replay_mode == REPLAY_MODE_PLAY) { |
| 273 | int offset; |
| 274 | replay_char_write_event_load(&ret, &offset); |
| 275 | assert(offset <= len); |
| 276 | qemu_chr_fe_write_buffer(s, buf, offset, &offset); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | qemu_mutex_lock(&s->chr_write_lock); |
| 281 | ret = s->chr_write(s, buf, len); |
| 282 | |
| 283 | if (ret > 0) { |
| 284 | qemu_chr_fe_write_log(s, buf, ret); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 287 | qemu_mutex_unlock(&s->chr_write_lock); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 288 | |
| 289 | if (s->replay && replay_mode == REPLAY_MODE_RECORD) { |
| 290 | replay_char_write_event_save(ret, ret < 0 ? 0 : ret); |
| 291 | } |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) |
| 297 | { |
| 298 | int offset; |
| 299 | int res; |
| 300 | |
| 301 | if (s->replay && replay_mode == REPLAY_MODE_PLAY) { |
| 302 | replay_char_write_event_load(&res, &offset); |
| 303 | assert(offset <= len); |
| 304 | qemu_chr_fe_write_buffer(s, buf, offset, &offset); |
| 305 | return res; |
| 306 | } |
| 307 | |
| 308 | res = qemu_chr_fe_write_buffer(s, buf, len, &offset); |
| 309 | |
| 310 | if (s->replay && replay_mode == REPLAY_MODE_RECORD) { |
| 311 | replay_char_write_event_save(res, offset); |
| 312 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 313 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 314 | if (res < 0) { |
| 315 | return res; |
| 316 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 317 | return offset; |
| 318 | } |
| 319 | |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 320 | int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len) |
| 321 | { |
| 322 | int offset = 0, counter = 10; |
| 323 | int res; |
| 324 | |
| 325 | if (!s->chr_sync_read) { |
| 326 | return 0; |
| 327 | } |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 328 | |
| 329 | if (s->replay && replay_mode == REPLAY_MODE_PLAY) { |
| 330 | return replay_char_read_all_load(buf); |
| 331 | } |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 332 | |
| 333 | while (offset < len) { |
Daniel P. Berrange | 53628ef | 2016-03-31 16:29:27 +0100 | [diff] [blame] | 334 | retry: |
| 335 | res = s->chr_sync_read(s, buf + offset, len - offset); |
| 336 | if (res == -1 && errno == EAGAIN) { |
| 337 | g_usleep(100); |
| 338 | goto retry; |
| 339 | } |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 340 | |
| 341 | if (res == 0) { |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | if (res < 0) { |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 346 | if (s->replay && replay_mode == REPLAY_MODE_RECORD) { |
| 347 | replay_char_read_all_save_error(res); |
| 348 | } |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 349 | return res; |
| 350 | } |
| 351 | |
| 352 | offset += res; |
| 353 | |
| 354 | if (!counter--) { |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 359 | if (s->replay && replay_mode == REPLAY_MODE_RECORD) { |
| 360 | replay_char_read_all_save_buf(buf, offset); |
| 361 | } |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 362 | return offset; |
| 363 | } |
| 364 | |
Anthony Liguori | 41084f1 | 2011-08-15 11:17:34 -0500 | [diff] [blame] | 365 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 366 | { |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 367 | int res; |
| 368 | if (!s->chr_ioctl || s->replay) { |
| 369 | res = -ENOTSUP; |
| 370 | } else { |
| 371 | res = s->chr_ioctl(s, cmd, arg); |
| 372 | } |
| 373 | |
| 374 | return res; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 377 | int qemu_chr_be_can_write(CharDriverState *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 378 | { |
| 379 | if (!s->chr_can_read) |
| 380 | return 0; |
| 381 | return s->chr_can_read(s->handler_opaque); |
| 382 | } |
| 383 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 384 | void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 385 | { |
Stefan Weil | ac31073 | 2012-04-19 22:27:14 +0200 | [diff] [blame] | 386 | if (s->chr_read) { |
| 387 | s->chr_read(s->handler_opaque, buf, len); |
| 388 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 391 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
| 392 | { |
| 393 | if (s->replay) { |
| 394 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 395 | return; |
| 396 | } |
| 397 | replay_chr_be_write(s, buf, len); |
| 398 | } else { |
| 399 | qemu_chr_be_write_impl(s, buf, len); |
| 400 | } |
| 401 | } |
| 402 | |
Anthony Liguori | 74c0d6f | 2011-08-15 11:17:39 -0500 | [diff] [blame] | 403 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 404 | { |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 405 | int fd; |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 406 | int res = (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1; |
| 407 | if (s->replay) { |
| 408 | fprintf(stderr, |
| 409 | "Replay: get msgfd is not supported for serial devices yet\n"); |
| 410 | exit(1); |
| 411 | } |
| 412 | return res; |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len) |
| 416 | { |
| 417 | return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 420 | int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num) |
| 421 | { |
| 422 | return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1; |
| 423 | } |
| 424 | |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 425 | int qemu_chr_add_client(CharDriverState *s, int fd) |
| 426 | { |
| 427 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; |
| 428 | } |
| 429 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 430 | void qemu_chr_accept_input(CharDriverState *s) |
| 431 | { |
| 432 | if (s->chr_accept_input) |
| 433 | s->chr_accept_input(s); |
Jan Kiszka | 98c8ee1 | 2012-03-16 13:18:00 +0100 | [diff] [blame] | 434 | qemu_notify_event(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Anthony Liguori | e7e71b0 | 2011-08-15 11:17:29 -0500 | [diff] [blame] | 437 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 438 | { |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 439 | char buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 440 | va_list ap; |
| 441 | va_start(ap, fmt); |
| 442 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 443 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 444 | va_end(ap); |
| 445 | } |
| 446 | |
Amit Shah | 386a5a1 | 2013-08-28 15:24:05 +0530 | [diff] [blame] | 447 | static void remove_fd_in_watch(CharDriverState *chr); |
| 448 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 449 | void qemu_chr_add_handlers(CharDriverState *s, |
Juan Quintela | 7b27a76 | 2010-03-11 17:55:39 +0100 | [diff] [blame] | 450 | IOCanReadHandler *fd_can_read, |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 451 | IOReadHandler *fd_read, |
| 452 | IOEventHandler *fd_event, |
| 453 | void *opaque) |
| 454 | { |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 455 | int fe_open; |
| 456 | |
Amit Shah | da7d998 | 2011-04-25 15:18:22 +0530 | [diff] [blame] | 457 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 458 | fe_open = 0; |
Amit Shah | 386a5a1 | 2013-08-28 15:24:05 +0530 | [diff] [blame] | 459 | remove_fd_in_watch(s); |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 460 | } else { |
| 461 | fe_open = 1; |
Amit Shah | 2d6c1ef | 2011-02-10 12:55:20 +0530 | [diff] [blame] | 462 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 463 | s->chr_can_read = fd_can_read; |
| 464 | s->chr_read = fd_read; |
| 465 | s->chr_event = fd_event; |
| 466 | s->handler_opaque = opaque; |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 467 | if (fe_open && s->chr_update_read_handler) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 468 | s->chr_update_read_handler(s); |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 469 | |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 470 | if (!s->explicit_fe_open) { |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 471 | qemu_chr_fe_set_open(s, fe_open); |
Hans de Goede | 1908322 | 2013-03-26 11:07:56 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 474 | /* We're connecting to an already opened device, so let's make sure we |
| 475 | also get the open event */ |
Hans de Goede | a59bcd3 | 2013-03-26 11:08:00 +0100 | [diff] [blame] | 476 | if (fe_open && s->be_open) { |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 477 | qemu_chr_be_generic_open(s); |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 478 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 482 | { |
| 483 | return len; |
| 484 | } |
| 485 | |
Paolo Bonzini | 0d64992 | 2015-09-29 15:24:29 +0200 | [diff] [blame] | 486 | static CharDriverState *qemu_chr_open_null(const char *id, |
| 487 | ChardevBackend *backend, |
| 488 | ChardevReturn *ret, |
| 489 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 490 | { |
| 491 | CharDriverState *chr; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 492 | ChardevCommon *common = backend->u.null.data; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 493 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 494 | chr = qemu_chr_alloc(common, errp); |
| 495 | if (!chr) { |
| 496 | return NULL; |
| 497 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 498 | chr->chr_write = null_chr_write; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 499 | chr->explicit_be_open = true; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 500 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* MUX driver for serial I/O splitting */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 504 | #define MAX_MUX 4 |
| 505 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
| 506 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
| 507 | typedef struct { |
Juan Quintela | 7b27a76 | 2010-03-11 17:55:39 +0100 | [diff] [blame] | 508 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 509 | IOReadHandler *chr_read[MAX_MUX]; |
| 510 | IOEventHandler *chr_event[MAX_MUX]; |
| 511 | void *ext_opaque[MAX_MUX]; |
| 512 | CharDriverState *drv; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 513 | int focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 514 | int mux_cnt; |
| 515 | int term_got_escape; |
| 516 | int max_size; |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 517 | /* Intermediate input buffer allows to catch escape sequences even if the |
| 518 | currently active device is not accepting any input - but only until it |
| 519 | is full as well. */ |
| 520 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; |
| 521 | int prod[MAX_MUX]; |
| 522 | int cons[MAX_MUX]; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 523 | int timestamps; |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 524 | |
| 525 | /* Protected by the CharDriverState chr_write_lock. */ |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 526 | int linestart; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 527 | int64_t timestamps_start; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 528 | } MuxDriver; |
| 529 | |
| 530 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 531 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 532 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 533 | { |
| 534 | MuxDriver *d = chr->opaque; |
| 535 | int ret; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 536 | if (!d->timestamps) { |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 537 | ret = qemu_chr_fe_write(d->drv, buf, len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 538 | } else { |
| 539 | int i; |
| 540 | |
| 541 | ret = 0; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 542 | for (i = 0; i < len; i++) { |
| 543 | if (d->linestart) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 544 | char buf1[64]; |
| 545 | int64_t ti; |
| 546 | int secs; |
| 547 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 548 | ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 549 | if (d->timestamps_start == -1) |
| 550 | d->timestamps_start = ti; |
| 551 | ti -= d->timestamps_start; |
aliguori | a4bb1db | 2009-01-22 17:15:16 +0000 | [diff] [blame] | 552 | secs = ti / 1000; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 553 | snprintf(buf1, sizeof(buf1), |
| 554 | "[%02d:%02d:%02d.%03d] ", |
| 555 | secs / 3600, |
| 556 | (secs / 60) % 60, |
| 557 | secs % 60, |
aliguori | a4bb1db | 2009-01-22 17:15:16 +0000 | [diff] [blame] | 558 | (int)(ti % 1000)); |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 559 | qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 560 | d->linestart = 0; |
| 561 | } |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 562 | ret += qemu_chr_fe_write(d->drv, buf+i, 1); |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 563 | if (buf[i] == '\n') { |
| 564 | d->linestart = 1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | } |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | static const char * const mux_help[] = { |
| 572 | "% h print this help\n\r", |
| 573 | "% x exit emulator\n\r", |
| 574 | "% s save disk data back to file (if -snapshot)\n\r", |
Gonglei | 86d1032 | 2014-11-15 18:06:46 +0800 | [diff] [blame] | 575 | "% t toggle console timestamps\n\r", |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 576 | "% b send break (magic sysrq)\n\r", |
| 577 | "% c switch between console and monitor\n\r", |
| 578 | "% % sends %\n\r", |
| 579 | NULL |
| 580 | }; |
| 581 | |
| 582 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 583 | static void mux_print_help(CharDriverState *chr) |
| 584 | { |
| 585 | int i, j; |
| 586 | char ebuf[15] = "Escape-Char"; |
| 587 | char cbuf[50] = "\n\r"; |
| 588 | |
| 589 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 590 | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
| 591 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
| 592 | } else { |
| 593 | snprintf(cbuf, sizeof(cbuf), |
| 594 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", |
| 595 | term_escape_char); |
| 596 | } |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 597 | qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 598 | for (i = 0; mux_help[i] != NULL; i++) { |
| 599 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 600 | if (mux_help[i][j] == '%') |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 601 | qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 602 | else |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 603 | qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 608 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
| 609 | { |
| 610 | if (d->chr_event[mux_nr]) |
| 611 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); |
| 612 | } |
| 613 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 614 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 615 | { |
| 616 | if (d->term_got_escape) { |
| 617 | d->term_got_escape = 0; |
| 618 | if (ch == term_escape_char) |
| 619 | goto send_char; |
| 620 | switch(ch) { |
| 621 | case '?': |
| 622 | case 'h': |
| 623 | mux_print_help(chr); |
| 624 | break; |
| 625 | case 'x': |
| 626 | { |
| 627 | const char *term = "QEMU: Terminated\n\r"; |
Paolo Bonzini | 6975b71 | 2014-06-18 08:43:56 +0200 | [diff] [blame] | 628 | qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 629 | exit(0); |
| 630 | break; |
| 631 | } |
| 632 | case 's': |
Max Reitz | da31d59 | 2016-03-16 19:54:32 +0100 | [diff] [blame] | 633 | blk_commit_all(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 634 | break; |
| 635 | case 'b': |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 636 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 637 | break; |
| 638 | case 'c': |
| 639 | /* Switch to the next registered device */ |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 640 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
| 641 | d->focus++; |
| 642 | if (d->focus >= d->mux_cnt) |
| 643 | d->focus = 0; |
| 644 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 645 | break; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 646 | case 't': |
| 647 | d->timestamps = !d->timestamps; |
| 648 | d->timestamps_start = -1; |
Jan Kiszka | 4ab312f | 2009-06-15 22:25:34 +0200 | [diff] [blame] | 649 | d->linestart = 0; |
Jan Kiszka | 2d22959 | 2009-06-15 22:25:30 +0200 | [diff] [blame] | 650 | break; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 651 | } |
| 652 | } else if (ch == term_escape_char) { |
| 653 | d->term_got_escape = 1; |
| 654 | } else { |
| 655 | send_char: |
| 656 | return 1; |
| 657 | } |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static void mux_chr_accept_input(CharDriverState *chr) |
| 662 | { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 663 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 664 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 665 | |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 666 | while (d->prod[m] != d->cons[m] && |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 667 | d->chr_can_read[m] && |
| 668 | d->chr_can_read[m](d->ext_opaque[m])) { |
| 669 | d->chr_read[m](d->ext_opaque[m], |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 670 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | static int mux_chr_can_read(void *opaque) |
| 675 | { |
| 676 | CharDriverState *chr = opaque; |
| 677 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 678 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 679 | |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 680 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 681 | return 1; |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 682 | if (d->chr_can_read[m]) |
| 683 | return d->chr_can_read[m](d->ext_opaque[m]); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 688 | { |
| 689 | CharDriverState *chr = opaque; |
| 690 | MuxDriver *d = chr->opaque; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 691 | int m = d->focus; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 692 | int i; |
| 693 | |
| 694 | mux_chr_accept_input (opaque); |
| 695 | |
| 696 | for(i = 0; i < size; i++) |
| 697 | if (mux_proc_byte(chr, d, buf[i])) { |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 698 | if (d->prod[m] == d->cons[m] && |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 699 | d->chr_can_read[m] && |
| 700 | d->chr_can_read[m](d->ext_opaque[m])) |
| 701 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); |
| 702 | else |
aliguori | a80bf99 | 2009-03-05 23:00:02 +0000 | [diff] [blame] | 703 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
| 707 | static void mux_chr_event(void *opaque, int event) |
| 708 | { |
| 709 | CharDriverState *chr = opaque; |
| 710 | MuxDriver *d = chr->opaque; |
| 711 | int i; |
| 712 | |
| 713 | /* Send the event to all registered listeners */ |
| 714 | for (i = 0; i < d->mux_cnt; i++) |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 715 | mux_chr_send_event(d, i, event); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 719 | { |
| 720 | MuxDriver *d = chr->opaque; |
| 721 | |
| 722 | if (d->mux_cnt >= MAX_MUX) { |
| 723 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 724 | return; |
| 725 | } |
| 726 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 727 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 728 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 729 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 730 | /* Fix up the real driver with mux routines */ |
| 731 | if (d->mux_cnt == 0) { |
| 732 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 733 | mux_chr_event, chr); |
| 734 | } |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 735 | if (d->focus != -1) { |
| 736 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 737 | } |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 738 | d->focus = d->mux_cnt; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 739 | d->mux_cnt++; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 740 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 743 | static bool muxes_realized; |
| 744 | |
| 745 | /** |
| 746 | * Called after processing of default and command-line-specified |
| 747 | * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached |
| 748 | * to a mux chardev. This is done here to ensure that |
| 749 | * output/prompts/banners are only displayed for the FE that has |
| 750 | * focus when initial command-line processing/machine init is |
| 751 | * completed. |
| 752 | * |
| 753 | * After this point, any new FE attached to any new or existing |
| 754 | * mux will receive CHR_EVENT_OPENED notifications for the BE |
| 755 | * immediately. |
| 756 | */ |
| 757 | static void muxes_realize_done(Notifier *notifier, void *unused) |
| 758 | { |
| 759 | CharDriverState *chr; |
| 760 | |
| 761 | QTAILQ_FOREACH(chr, &chardevs, next) { |
| 762 | if (chr->is_mux) { |
| 763 | MuxDriver *d = chr->opaque; |
| 764 | int i; |
| 765 | |
| 766 | /* send OPENED to all already-attached FEs */ |
| 767 | for (i = 0; i < d->mux_cnt; i++) { |
| 768 | mux_chr_send_event(d, i, CHR_EVENT_OPENED); |
| 769 | } |
| 770 | /* mark mux as OPENED so any new FEs will immediately receive |
| 771 | * OPENED event |
| 772 | */ |
| 773 | qemu_chr_be_generic_open(chr); |
| 774 | } |
| 775 | } |
| 776 | muxes_realized = true; |
| 777 | } |
| 778 | |
| 779 | static Notifier muxes_realize_notify = { |
| 780 | .notify = muxes_realize_done, |
| 781 | }; |
| 782 | |
Kirill Batuzov | 3f0838a | 2014-07-04 16:43:15 +0400 | [diff] [blame] | 783 | static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond) |
| 784 | { |
| 785 | MuxDriver *d = s->opaque; |
| 786 | return d->drv->chr_add_watch(d->drv, cond); |
| 787 | } |
| 788 | |
Paolo Bonzini | 3c0e5a4 | 2015-09-29 15:27:24 +0200 | [diff] [blame] | 789 | static CharDriverState *qemu_chr_open_mux(const char *id, |
| 790 | ChardevBackend *backend, |
| 791 | ChardevReturn *ret, Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 792 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 793 | ChardevMux *mux = backend->u.mux.data; |
Paolo Bonzini | 3c0e5a4 | 2015-09-29 15:27:24 +0200 | [diff] [blame] | 794 | CharDriverState *chr, *drv; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 795 | MuxDriver *d; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 796 | ChardevCommon *common = qapi_ChardevMux_base(mux); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 797 | |
Paolo Bonzini | 3c0e5a4 | 2015-09-29 15:27:24 +0200 | [diff] [blame] | 798 | drv = qemu_chr_find(mux->chardev); |
| 799 | if (drv == NULL) { |
| 800 | error_setg(errp, "mux: base chardev %s not found", mux->chardev); |
| 801 | return NULL; |
| 802 | } |
| 803 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 804 | chr = qemu_chr_alloc(common, errp); |
| 805 | if (!chr) { |
| 806 | return NULL; |
| 807 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 808 | d = g_new0(MuxDriver, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 809 | |
| 810 | chr->opaque = d; |
| 811 | d->drv = drv; |
Gerd Hoffmann | 799f1f2 | 2009-09-10 10:58:55 +0200 | [diff] [blame] | 812 | d->focus = -1; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 813 | chr->chr_write = mux_chr_write; |
| 814 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
| 815 | chr->chr_accept_input = mux_chr_accept_input; |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 816 | /* Frontend guest-open / -close notification is not support with muxes */ |
Hans de Goede | 574b711 | 2013-03-26 11:07:58 +0100 | [diff] [blame] | 817 | chr->chr_set_fe_open = NULL; |
Kirill Batuzov | 3f0838a | 2014-07-04 16:43:15 +0400 | [diff] [blame] | 818 | if (drv->chr_add_watch) { |
| 819 | chr->chr_add_watch = mux_chr_add_watch; |
| 820 | } |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 821 | /* only default to opened state if we've realized the initial |
| 822 | * set of muxes |
| 823 | */ |
| 824 | chr->explicit_be_open = muxes_realized ? 0 : 1; |
| 825 | chr->is_mux = 1; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 826 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 827 | return chr; |
| 828 | } |
| 829 | |
| 830 | |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 831 | typedef struct IOWatchPoll |
| 832 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 833 | GSource parent; |
| 834 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 835 | QIOChannel *ioc; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 836 | GSource *src; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 837 | |
| 838 | IOCanReadHandler *fd_can_read; |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 839 | GSourceFunc fd_read; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 840 | void *opaque; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 841 | } IOWatchPoll; |
| 842 | |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 843 | static IOWatchPoll *io_watch_poll_from_source(GSource *source) |
| 844 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 845 | return container_of(source, IOWatchPoll, parent); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_) |
| 849 | { |
| 850 | IOWatchPoll *iwp = io_watch_poll_from_source(source); |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 851 | bool now_active = iwp->fd_can_read(iwp->opaque) > 0; |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 852 | bool was_active = iwp->src != NULL; |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 853 | if (was_active == now_active) { |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 854 | return FALSE; |
| 855 | } |
| 856 | |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 857 | if (now_active) { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 858 | iwp->src = qio_channel_create_watch( |
| 859 | iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL); |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 860 | g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 861 | g_source_attach(iwp->src, NULL); |
| 862 | } else { |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 863 | g_source_destroy(iwp->src); |
| 864 | g_source_unref(iwp->src); |
| 865 | iwp->src = NULL; |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 866 | } |
| 867 | return FALSE; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static gboolean io_watch_poll_check(GSource *source) |
| 871 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 872 | return FALSE; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, |
| 876 | gpointer user_data) |
| 877 | { |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 878 | abort(); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static void io_watch_poll_finalize(GSource *source) |
| 882 | { |
Paolo Bonzini | 2b31677 | 2013-04-19 17:32:09 +0200 | [diff] [blame] | 883 | /* Due to a glib bug, removing the last reference to a source |
| 884 | * inside a finalize callback causes recursive locking (and a |
| 885 | * deadlock). This is not a problem inside other callbacks, |
| 886 | * including dispatch callbacks, so we call io_remove_watch_poll |
| 887 | * to remove this source. At this point, iwp->src must |
| 888 | * be NULL, or we would leak it. |
| 889 | * |
| 890 | * This would be solved much more elegantly by child sources, |
| 891 | * but we support older glib versions that do not have them. |
| 892 | */ |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 893 | IOWatchPoll *iwp = io_watch_poll_from_source(source); |
Paolo Bonzini | 2b31677 | 2013-04-19 17:32:09 +0200 | [diff] [blame] | 894 | assert(iwp->src == NULL); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static GSourceFuncs io_watch_poll_funcs = { |
| 898 | .prepare = io_watch_poll_prepare, |
| 899 | .check = io_watch_poll_check, |
| 900 | .dispatch = io_watch_poll_dispatch, |
| 901 | .finalize = io_watch_poll_finalize, |
| 902 | }; |
| 903 | |
| 904 | /* Can only be used for read */ |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 905 | static guint io_add_watch_poll(QIOChannel *ioc, |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 906 | IOCanReadHandler *fd_can_read, |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 907 | QIOChannelFunc fd_read, |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 908 | gpointer user_data) |
| 909 | { |
| 910 | IOWatchPoll *iwp; |
Paolo Bonzini | 0ca5aa4 | 2013-04-10 15:23:27 +0200 | [diff] [blame] | 911 | int tag; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 912 | |
Paolo Bonzini | d185c09 | 2013-04-05 17:59:33 +0200 | [diff] [blame] | 913 | iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll)); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 914 | iwp->fd_can_read = fd_can_read; |
| 915 | iwp->opaque = user_data; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 916 | iwp->ioc = ioc; |
Paolo Bonzini | 1e885b2 | 2013-04-08 15:03:15 +0200 | [diff] [blame] | 917 | iwp->fd_read = (GSourceFunc) fd_read; |
| 918 | iwp->src = NULL; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 919 | |
Paolo Bonzini | 0ca5aa4 | 2013-04-10 15:23:27 +0200 | [diff] [blame] | 920 | tag = g_source_attach(&iwp->parent, NULL); |
| 921 | g_source_unref(&iwp->parent); |
| 922 | return tag; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 923 | } |
| 924 | |
Paolo Bonzini | 2b31677 | 2013-04-19 17:32:09 +0200 | [diff] [blame] | 925 | static void io_remove_watch_poll(guint tag) |
| 926 | { |
| 927 | GSource *source; |
| 928 | IOWatchPoll *iwp; |
| 929 | |
| 930 | g_return_if_fail (tag > 0); |
| 931 | |
| 932 | source = g_main_context_find_source_by_id(NULL, tag); |
| 933 | g_return_if_fail (source != NULL); |
| 934 | |
| 935 | iwp = io_watch_poll_from_source(source); |
| 936 | if (iwp->src) { |
| 937 | g_source_destroy(iwp->src); |
| 938 | g_source_unref(iwp->src); |
| 939 | iwp->src = NULL; |
| 940 | } |
| 941 | g_source_destroy(&iwp->parent); |
| 942 | } |
| 943 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 944 | static void remove_fd_in_watch(CharDriverState *chr) |
| 945 | { |
| 946 | if (chr->fd_in_tag) { |
| 947 | io_remove_watch_poll(chr->fd_in_tag); |
| 948 | chr->fd_in_tag = 0; |
| 949 | } |
| 950 | } |
| 951 | |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 952 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 953 | static int io_channel_send_full(QIOChannel *ioc, |
| 954 | const void *buf, size_t len, |
| 955 | int *fds, size_t nfds) |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 956 | { |
Laszlo Ersek | ac8c26f | 2013-07-16 20:19:40 +0200 | [diff] [blame] | 957 | size_t offset = 0; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 958 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 959 | while (offset < len) { |
| 960 | ssize_t ret = 0; |
| 961 | struct iovec iov = { .iov_base = (char *)buf + offset, |
| 962 | .iov_len = len - offset }; |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 963 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 964 | ret = qio_channel_writev_full( |
| 965 | ioc, &iov, 1, |
| 966 | fds, nfds, NULL); |
| 967 | if (ret == QIO_CHANNEL_ERR_BLOCK) { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 968 | if (offset) { |
| 969 | return offset; |
| 970 | } |
| 971 | |
Daniel P. Berrange | e046fb4 | 2016-02-12 14:46:50 +0000 | [diff] [blame] | 972 | errno = EAGAIN; |
| 973 | return -1; |
| 974 | } else if (ret < 0) { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 975 | errno = EINVAL; |
| 976 | return -1; |
| 977 | } |
| 978 | |
| 979 | offset += ret; |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 980 | } |
Anthony Liguori | 684a096 | 2013-03-29 11:39:50 -0500 | [diff] [blame] | 981 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 982 | return offset; |
| 983 | } |
| 984 | |
| 985 | |
| 986 | #ifndef _WIN32 |
| 987 | static int io_channel_send(QIOChannel *ioc, const void *buf, size_t len) |
| 988 | { |
| 989 | return io_channel_send_full(ioc, buf, len, NULL, 0); |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 990 | } |
Anthony Liguori | 96c6384 | 2013-03-05 23:21:18 +0530 | [diff] [blame] | 991 | |
Blue Swirl | 44ab9ed | 2013-03-09 09:56:04 +0000 | [diff] [blame] | 992 | |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 993 | typedef struct FDCharDriver { |
| 994 | CharDriverState *chr; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 995 | QIOChannel *ioc_in, *ioc_out; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 996 | int max_size; |
| 997 | } FDCharDriver; |
| 998 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 999 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1000 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1001 | { |
| 1002 | FDCharDriver *s = chr->opaque; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1003 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1004 | return io_channel_send(s->ioc_out, buf, len); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1005 | } |
| 1006 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1007 | static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1008 | { |
| 1009 | CharDriverState *chr = opaque; |
| 1010 | FDCharDriver *s = chr->opaque; |
| 1011 | int len; |
| 1012 | uint8_t buf[READ_BUF_LEN]; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1013 | ssize_t ret; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1014 | |
| 1015 | len = sizeof(buf); |
| 1016 | if (len > s->max_size) { |
| 1017 | len = s->max_size; |
| 1018 | } |
| 1019 | if (len == 0) { |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 1020 | return TRUE; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1021 | } |
| 1022 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1023 | ret = qio_channel_read( |
| 1024 | chan, (gchar *)buf, len, NULL); |
| 1025 | if (ret == 0) { |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 1026 | remove_fd_in_watch(chr); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1027 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
| 1028 | return FALSE; |
| 1029 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1030 | if (ret > 0) { |
| 1031 | qemu_chr_be_write(chr, buf, ret); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static int fd_chr_read_poll(void *opaque) |
| 1038 | { |
| 1039 | CharDriverState *chr = opaque; |
| 1040 | FDCharDriver *s = chr->opaque; |
| 1041 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 1042 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1043 | return s->max_size; |
| 1044 | } |
| 1045 | |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 1046 | static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
| 1047 | { |
| 1048 | FDCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1049 | return qio_channel_create_watch(s->ioc_out, cond); |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 1050 | } |
| 1051 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1052 | static void fd_chr_update_read_handler(CharDriverState *chr) |
| 1053 | { |
| 1054 | FDCharDriver *s = chr->opaque; |
| 1055 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 1056 | remove_fd_in_watch(chr); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1057 | if (s->ioc_in) { |
| 1058 | chr->fd_in_tag = io_add_watch_poll(s->ioc_in, |
| 1059 | fd_chr_read_poll, |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 1060 | fd_chr_read, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | static void fd_chr_close(struct CharDriverState *chr) |
| 1065 | { |
| 1066 | FDCharDriver *s = chr->opaque; |
| 1067 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 1068 | remove_fd_in_watch(chr); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1069 | if (s->ioc_in) { |
| 1070 | object_unref(OBJECT(s->ioc_in)); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1071 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1072 | if (s->ioc_out) { |
| 1073 | object_unref(OBJECT(s->ioc_out)); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1076 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1077 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /* open a character device to a unix fd */ |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1081 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out, |
| 1082 | ChardevCommon *backend, Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1083 | { |
| 1084 | CharDriverState *chr; |
| 1085 | FDCharDriver *s; |
| 1086 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1087 | chr = qemu_chr_alloc(backend, errp); |
| 1088 | if (!chr) { |
| 1089 | return NULL; |
| 1090 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 1091 | s = g_new0(FDCharDriver, 1); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1092 | s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in)); |
| 1093 | s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out)); |
Gonglei | 4ff12bd | 2014-08-11 17:34:20 +0800 | [diff] [blame] | 1094 | qemu_set_nonblock(fd_out); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1095 | s->chr = chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1096 | chr->opaque = s; |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 1097 | chr->chr_add_watch = fd_chr_add_watch; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1098 | chr->chr_write = fd_chr_write; |
| 1099 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
| 1100 | chr->chr_close = fd_chr_close; |
| 1101 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1102 | return chr; |
| 1103 | } |
| 1104 | |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 1105 | static CharDriverState *qemu_chr_open_pipe(const char *id, |
| 1106 | ChardevBackend *backend, |
| 1107 | ChardevReturn *ret, |
| 1108 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1109 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1110 | ChardevHostdev *opts = backend->u.pipe.data; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1111 | int fd_in, fd_out; |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 1112 | char *filename_in; |
| 1113 | char *filename_out; |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 1114 | const char *filename = opts->device; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 1115 | ChardevCommon *common = qapi_ChardevHostdev_base(opts); |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 1116 | |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 1117 | |
| 1118 | filename_in = g_strdup_printf("%s.in", filename); |
| 1119 | filename_out = g_strdup_printf("%s.out", filename); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1120 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
| 1121 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 1122 | g_free(filename_in); |
| 1123 | g_free(filename_out); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1124 | if (fd_in < 0 || fd_out < 0) { |
| 1125 | if (fd_in >= 0) |
| 1126 | close(fd_in); |
| 1127 | if (fd_out >= 0) |
| 1128 | close(fd_out); |
Markus Armbruster | b181e04 | 2012-02-07 15:09:09 +0100 | [diff] [blame] | 1129 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
Markus Armbruster | a89dd6c | 2012-02-07 15:09:10 +0100 | [diff] [blame] | 1130 | if (fd_in < 0) { |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 1131 | error_setg_file_open(errp, errno, filename); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1132 | return NULL; |
Markus Armbruster | a89dd6c | 2012-02-07 15:09:10 +0100 | [diff] [blame] | 1133 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1134 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1135 | return qemu_chr_open_fd(fd_in, fd_out, common, errp); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1138 | /* init terminal so that we can grab keys */ |
| 1139 | static struct termios oldtty; |
| 1140 | static int old_fd0_flags; |
Li Liu | c88930a | 2014-09-09 19:19:48 +0800 | [diff] [blame] | 1141 | static bool stdio_in_use; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 1142 | static bool stdio_allow_signal; |
Gal Hammer | e76d442 | 2015-01-07 10:38:35 +0200 | [diff] [blame] | 1143 | static bool stdio_echo_state; |
| 1144 | |
| 1145 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1146 | |
| 1147 | static void term_exit(void) |
| 1148 | { |
| 1149 | tcsetattr (0, TCSANOW, &oldtty); |
| 1150 | fcntl(0, F_SETFL, old_fd0_flags); |
| 1151 | } |
| 1152 | |
Gal Hammer | e76d442 | 2015-01-07 10:38:35 +0200 | [diff] [blame] | 1153 | static void term_stdio_handler(int sig) |
| 1154 | { |
| 1155 | /* restore echo after resume from suspend. */ |
| 1156 | qemu_chr_set_echo_stdio(NULL, stdio_echo_state); |
| 1157 | } |
| 1158 | |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 1159 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1160 | { |
| 1161 | struct termios tty; |
| 1162 | |
Gal Hammer | e76d442 | 2015-01-07 10:38:35 +0200 | [diff] [blame] | 1163 | stdio_echo_state = echo; |
Paolo Bonzini | 0369364 | 2010-12-23 13:42:49 +0100 | [diff] [blame] | 1164 | tty = oldtty; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 1165 | if (!echo) { |
| 1166 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1167 | |INLCR|IGNCR|ICRNL|IXON); |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 1168 | tty.c_oflag |= OPOST; |
| 1169 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 1170 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 1171 | tty.c_cflag |= CS8; |
| 1172 | tty.c_cc[VMIN] = 1; |
| 1173 | tty.c_cc[VTIME] = 0; |
| 1174 | } |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 1175 | if (!stdio_allow_signal) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1176 | tty.c_lflag &= ~ISIG; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1177 | |
| 1178 | tcsetattr (0, TCSANOW, &tty); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
| 1182 | { |
| 1183 | term_exit(); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1184 | fd_chr_close(chr); |
| 1185 | } |
| 1186 | |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 1187 | static CharDriverState *qemu_chr_open_stdio(const char *id, |
| 1188 | ChardevBackend *backend, |
| 1189 | ChardevReturn *ret, |
| 1190 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1191 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1192 | ChardevStdio *opts = backend->u.stdio.data; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1193 | CharDriverState *chr; |
Gal Hammer | e76d442 | 2015-01-07 10:38:35 +0200 | [diff] [blame] | 1194 | struct sigaction act; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 1195 | ChardevCommon *common = qapi_ChardevStdio_base(opts); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1196 | |
Michael Tokarev | ab51b1d | 2012-12-30 12:48:14 +0400 | [diff] [blame] | 1197 | if (is_daemonized()) { |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 1198 | error_setg(errp, "cannot use stdio with -daemonize"); |
Michael Tokarev | ab51b1d | 2012-12-30 12:48:14 +0400 | [diff] [blame] | 1199 | return NULL; |
| 1200 | } |
Li Liu | c88930a | 2014-09-09 19:19:48 +0800 | [diff] [blame] | 1201 | |
| 1202 | if (stdio_in_use) { |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 1203 | error_setg(errp, "cannot use stdio by multiple character devices"); |
| 1204 | return NULL; |
Li Liu | c88930a | 2014-09-09 19:19:48 +0800 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | stdio_in_use = true; |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 1208 | old_fd0_flags = fcntl(0, F_GETFL); |
Li Liu | c88930a | 2014-09-09 19:19:48 +0800 | [diff] [blame] | 1209 | tcgetattr(0, &oldtty); |
Gonglei | 4ff12bd | 2014-08-11 17:34:20 +0800 | [diff] [blame] | 1210 | qemu_set_nonblock(0); |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 1211 | atexit(term_exit); |
Paolo Bonzini | 0369364 | 2010-12-23 13:42:49 +0100 | [diff] [blame] | 1212 | |
Gal Hammer | e76d442 | 2015-01-07 10:38:35 +0200 | [diff] [blame] | 1213 | memset(&act, 0, sizeof(act)); |
| 1214 | act.sa_handler = term_stdio_handler; |
| 1215 | sigaction(SIGCONT, &act, NULL); |
| 1216 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1217 | chr = qemu_chr_open_fd(0, 1, common, errp); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1218 | chr->chr_close = qemu_chr_close_stdio; |
Paolo Bonzini | bb00251 | 2010-12-23 13:42:50 +0100 | [diff] [blame] | 1219 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 1220 | if (opts->has_signal) { |
| 1221 | stdio_allow_signal = opts->signal; |
| 1222 | } |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 1223 | qemu_chr_fe_set_echo(chr, false); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1224 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1225 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1228 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 1229 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
| 1230 | || defined(__GLIBC__) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1231 | |
Paolo Bonzini | d809ab9 | 2015-10-12 09:46:23 +0200 | [diff] [blame] | 1232 | #define HAVE_CHARDEV_SERIAL 1 |
| 1233 | #define HAVE_CHARDEV_PTY 1 |
Gerd Hoffmann | e551498 | 2012-12-19 16:35:42 +0100 | [diff] [blame] | 1234 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1235 | typedef struct { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1236 | QIOChannel *ioc; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1237 | int read_bytes; |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1238 | |
| 1239 | /* Protected by the CharDriverState chr_write_lock. */ |
| 1240 | int connected; |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1241 | guint timer_tag; |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1242 | guint open_tag; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1243 | } PtyCharDriver; |
| 1244 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1245 | static void pty_chr_update_read_handler_locked(CharDriverState *chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1246 | static void pty_chr_state(CharDriverState *chr, int connected); |
| 1247 | |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1248 | static gboolean pty_chr_timer(gpointer opaque) |
| 1249 | { |
| 1250 | struct CharDriverState *chr = opaque; |
| 1251 | PtyCharDriver *s = chr->opaque; |
| 1252 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1253 | qemu_mutex_lock(&chr->chr_write_lock); |
Hans de Goede | 79f2007 | 2013-04-25 13:53:02 +0200 | [diff] [blame] | 1254 | s->timer_tag = 0; |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1255 | s->open_tag = 0; |
Gerd Hoffmann | b0d768c | 2013-08-22 11:43:58 +0200 | [diff] [blame] | 1256 | if (!s->connected) { |
| 1257 | /* Next poll ... */ |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1258 | pty_chr_update_read_handler_locked(chr); |
Gerd Hoffmann | b0d768c | 2013-08-22 11:43:58 +0200 | [diff] [blame] | 1259 | } |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1260 | qemu_mutex_unlock(&chr->chr_write_lock); |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1261 | return FALSE; |
| 1262 | } |
| 1263 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1264 | /* Called with chr_write_lock held. */ |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1265 | static void pty_chr_rearm_timer(CharDriverState *chr, int ms) |
| 1266 | { |
| 1267 | PtyCharDriver *s = chr->opaque; |
| 1268 | |
| 1269 | if (s->timer_tag) { |
| 1270 | g_source_remove(s->timer_tag); |
| 1271 | s->timer_tag = 0; |
| 1272 | } |
| 1273 | |
| 1274 | if (ms == 1000) { |
| 1275 | s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); |
| 1276 | } else { |
| 1277 | s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); |
| 1278 | } |
| 1279 | } |
| 1280 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1281 | /* Called with chr_write_lock held. */ |
| 1282 | static void pty_chr_update_read_handler_locked(CharDriverState *chr) |
Paolo Bonzini | 1bb7fe7 | 2014-06-18 08:43:57 +0200 | [diff] [blame] | 1283 | { |
| 1284 | PtyCharDriver *s = chr->opaque; |
| 1285 | GPollFD pfd; |
Paolo Bonzini | c1f2448 | 2015-12-01 11:27:00 +0100 | [diff] [blame] | 1286 | int rc; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1287 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc); |
Paolo Bonzini | 1bb7fe7 | 2014-06-18 08:43:57 +0200 | [diff] [blame] | 1288 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1289 | pfd.fd = fioc->fd; |
Paolo Bonzini | 1bb7fe7 | 2014-06-18 08:43:57 +0200 | [diff] [blame] | 1290 | pfd.events = G_IO_OUT; |
| 1291 | pfd.revents = 0; |
Paolo Bonzini | c1f2448 | 2015-12-01 11:27:00 +0100 | [diff] [blame] | 1292 | do { |
| 1293 | rc = g_poll(&pfd, 1, 0); |
| 1294 | } while (rc == -1 && errno == EINTR); |
| 1295 | assert(rc >= 0); |
| 1296 | |
Paolo Bonzini | 1bb7fe7 | 2014-06-18 08:43:57 +0200 | [diff] [blame] | 1297 | if (pfd.revents & G_IO_HUP) { |
| 1298 | pty_chr_state(chr, 0); |
| 1299 | } else { |
| 1300 | pty_chr_state(chr, 1); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1304 | static void pty_chr_update_read_handler(CharDriverState *chr) |
| 1305 | { |
| 1306 | qemu_mutex_lock(&chr->chr_write_lock); |
| 1307 | pty_chr_update_read_handler_locked(chr); |
| 1308 | qemu_mutex_unlock(&chr->chr_write_lock); |
| 1309 | } |
| 1310 | |
| 1311 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1312 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1313 | { |
| 1314 | PtyCharDriver *s = chr->opaque; |
| 1315 | |
| 1316 | if (!s->connected) { |
| 1317 | /* guest sends data, check for (re-)connect */ |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1318 | pty_chr_update_read_handler_locked(chr); |
Sebastian Tanase | cf7330c | 2014-07-28 13:39:14 +0200 | [diff] [blame] | 1319 | if (!s->connected) { |
| 1320 | return 0; |
| 1321 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1322 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1323 | return io_channel_send(s->ioc, buf, len); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Anthony Liguori | e6a87ed | 2013-03-05 23:21:24 +0530 | [diff] [blame] | 1326 | static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
| 1327 | { |
| 1328 | PtyCharDriver *s = chr->opaque; |
Paolo Bonzini | 62c339c | 2014-07-24 16:08:04 +0200 | [diff] [blame] | 1329 | if (!s->connected) { |
| 1330 | return NULL; |
| 1331 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1332 | return qio_channel_create_watch(s->ioc, cond); |
Anthony Liguori | e6a87ed | 2013-03-05 23:21:24 +0530 | [diff] [blame] | 1333 | } |
| 1334 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1335 | static int pty_chr_read_poll(void *opaque) |
| 1336 | { |
| 1337 | CharDriverState *chr = opaque; |
| 1338 | PtyCharDriver *s = chr->opaque; |
| 1339 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 1340 | s->read_bytes = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1341 | return s->read_bytes; |
| 1342 | } |
| 1343 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1344 | static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1345 | { |
| 1346 | CharDriverState *chr = opaque; |
| 1347 | PtyCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1348 | gsize len; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 1349 | uint8_t buf[READ_BUF_LEN]; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1350 | ssize_t ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1351 | |
| 1352 | len = sizeof(buf); |
| 1353 | if (len > s->read_bytes) |
| 1354 | len = s->read_bytes; |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 1355 | if (len == 0) { |
| 1356 | return TRUE; |
| 1357 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1358 | ret = qio_channel_read(s->ioc, (char *)buf, len, NULL); |
| 1359 | if (ret <= 0) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1360 | pty_chr_state(chr, 0); |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1361 | return FALSE; |
| 1362 | } else { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1363 | pty_chr_state(chr, 1); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1364 | qemu_chr_be_write(chr, buf, ret); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1365 | } |
Anthony Liguori | 093d3a2 | 2013-03-05 23:21:20 +0530 | [diff] [blame] | 1366 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1369 | static gboolean qemu_chr_be_generic_open_func(gpointer opaque) |
| 1370 | { |
| 1371 | CharDriverState *chr = opaque; |
| 1372 | PtyCharDriver *s = chr->opaque; |
| 1373 | |
| 1374 | s->open_tag = 0; |
| 1375 | qemu_chr_be_generic_open(chr); |
| 1376 | return FALSE; |
| 1377 | } |
| 1378 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1379 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1380 | static void pty_chr_state(CharDriverState *chr, int connected) |
| 1381 | { |
| 1382 | PtyCharDriver *s = chr->opaque; |
| 1383 | |
| 1384 | if (!connected) { |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1385 | if (s->open_tag) { |
| 1386 | g_source_remove(s->open_tag); |
| 1387 | s->open_tag = 0; |
| 1388 | } |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 1389 | remove_fd_in_watch(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1390 | s->connected = 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1391 | /* (re-)connect poll interval for idle guests: once per second. |
| 1392 | * We check more frequently in case the guests sends data to |
| 1393 | * the virtual device linked to our pty. */ |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1394 | pty_chr_rearm_timer(chr, 1000); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1395 | } else { |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1396 | if (s->timer_tag) { |
| 1397 | g_source_remove(s->timer_tag); |
| 1398 | s->timer_tag = 0; |
| 1399 | } |
| 1400 | if (!s->connected) { |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1401 | g_assert(s->open_tag == 0); |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1402 | s->connected = 1; |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1403 | s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr); |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 1404 | } |
| 1405 | if (!chr->fd_in_tag) { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1406 | chr->fd_in_tag = io_add_watch_poll(s->ioc, |
| 1407 | pty_chr_read_poll, |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 1408 | pty_chr_read, chr); |
Paolo Bonzini | 85a6769 | 2013-04-19 17:32:07 +0200 | [diff] [blame] | 1409 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1413 | static void pty_chr_close(struct CharDriverState *chr) |
| 1414 | { |
| 1415 | PtyCharDriver *s = chr->opaque; |
| 1416 | |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1417 | qemu_mutex_lock(&chr->chr_write_lock); |
| 1418 | pty_chr_state(chr, 0); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1419 | object_unref(OBJECT(s->ioc)); |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1420 | if (s->timer_tag) { |
| 1421 | g_source_remove(s->timer_tag); |
Paolo Bonzini | 910b636 | 2013-04-19 17:32:06 +0200 | [diff] [blame] | 1422 | s->timer_tag = 0; |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1423 | } |
Paolo Bonzini | 7b3621f | 2014-07-11 12:11:38 +0200 | [diff] [blame] | 1424 | qemu_mutex_unlock(&chr->chr_write_lock); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1425 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1426 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1429 | static CharDriverState *qemu_chr_open_pty(const char *id, |
Paolo Bonzini | c2e75a4 | 2015-09-29 15:23:42 +0200 | [diff] [blame] | 1430 | ChardevBackend *backend, |
| 1431 | ChardevReturn *ret, |
| 1432 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1433 | { |
| 1434 | CharDriverState *chr; |
| 1435 | PtyCharDriver *s; |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1436 | int master_fd, slave_fd; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1437 | char pty_name[PATH_MAX]; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1438 | ChardevCommon *common = backend->u.pty.data; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1439 | |
Michael Tokarev | 4efeabb | 2013-06-05 18:44:54 +0400 | [diff] [blame] | 1440 | master_fd = qemu_openpty_raw(&slave_fd, pty_name); |
| 1441 | if (master_fd < 0) { |
Paolo Bonzini | c2e75a4 | 2015-09-29 15:23:42 +0200 | [diff] [blame] | 1442 | error_setg_errno(errp, errno, "Failed to create PTY"); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1443 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Paolo Bonzini | 837a183 | 2016-02-12 16:05:10 +0100 | [diff] [blame] | 1446 | close(slave_fd); |
Don Slutz | fac6688 | 2014-12-22 10:04:00 -0500 | [diff] [blame] | 1447 | qemu_set_nonblock(master_fd); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1448 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1449 | chr = qemu_chr_alloc(common, errp); |
| 1450 | if (!chr) { |
| 1451 | close(master_fd); |
| 1452 | return NULL; |
| 1453 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1454 | |
Michael Tokarev | 4efeabb | 2013-06-05 18:44:54 +0400 | [diff] [blame] | 1455 | chr->filename = g_strdup_printf("pty:%s", pty_name); |
| 1456 | ret->pty = g_strdup(pty_name); |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1457 | ret->has_pty = true; |
Lei Li | 5865021 | 2012-12-21 12:26:38 +0800 | [diff] [blame] | 1458 | |
Gerd Hoffmann | e68c595 | 2013-02-25 10:16:46 +0100 | [diff] [blame] | 1459 | fprintf(stderr, "char device redirected to %s (label %s)\n", |
Michael Tokarev | 4efeabb | 2013-06-05 18:44:54 +0400 | [diff] [blame] | 1460 | pty_name, id); |
Markus Armbruster | a4e2604 | 2011-11-11 10:40:05 +0100 | [diff] [blame] | 1461 | |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 1462 | s = g_new0(PtyCharDriver, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1463 | chr->opaque = s; |
| 1464 | chr->chr_write = pty_chr_write; |
| 1465 | chr->chr_update_read_handler = pty_chr_update_read_handler; |
| 1466 | chr->chr_close = pty_chr_close; |
Anthony Liguori | e6a87ed | 2013-03-05 23:21:24 +0530 | [diff] [blame] | 1467 | chr->chr_add_watch = pty_chr_add_watch; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 1468 | chr->explicit_be_open = true; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1469 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1470 | s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd)); |
Anthony Liguori | 8aa33ca | 2013-03-05 23:21:26 +0530 | [diff] [blame] | 1471 | s->timer_tag = 0; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1472 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1473 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | static void tty_serial_init(int fd, int speed, |
| 1477 | int parity, int data_bits, int stop_bits) |
| 1478 | { |
| 1479 | struct termios tty; |
| 1480 | speed_t spd; |
| 1481 | |
| 1482 | #if 0 |
| 1483 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1484 | speed, parity, data_bits, stop_bits); |
| 1485 | #endif |
| 1486 | tcgetattr (fd, &tty); |
| 1487 | |
Stefan Weil | 45eea13 | 2009-10-26 16:10:10 +0100 | [diff] [blame] | 1488 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
| 1489 | speed = speed * 10 / 11; |
| 1490 | do { |
| 1491 | check_speed(50); |
| 1492 | check_speed(75); |
| 1493 | check_speed(110); |
| 1494 | check_speed(134); |
| 1495 | check_speed(150); |
| 1496 | check_speed(200); |
| 1497 | check_speed(300); |
| 1498 | check_speed(600); |
| 1499 | check_speed(1200); |
| 1500 | check_speed(1800); |
| 1501 | check_speed(2400); |
| 1502 | check_speed(4800); |
| 1503 | check_speed(9600); |
| 1504 | check_speed(19200); |
| 1505 | check_speed(38400); |
| 1506 | /* Non-Posix values follow. They may be unsupported on some systems. */ |
| 1507 | check_speed(57600); |
| 1508 | check_speed(115200); |
| 1509 | #ifdef B230400 |
| 1510 | check_speed(230400); |
| 1511 | #endif |
| 1512 | #ifdef B460800 |
| 1513 | check_speed(460800); |
| 1514 | #endif |
| 1515 | #ifdef B500000 |
| 1516 | check_speed(500000); |
| 1517 | #endif |
| 1518 | #ifdef B576000 |
| 1519 | check_speed(576000); |
| 1520 | #endif |
| 1521 | #ifdef B921600 |
| 1522 | check_speed(921600); |
| 1523 | #endif |
| 1524 | #ifdef B1000000 |
| 1525 | check_speed(1000000); |
| 1526 | #endif |
| 1527 | #ifdef B1152000 |
| 1528 | check_speed(1152000); |
| 1529 | #endif |
| 1530 | #ifdef B1500000 |
| 1531 | check_speed(1500000); |
| 1532 | #endif |
| 1533 | #ifdef B2000000 |
| 1534 | check_speed(2000000); |
| 1535 | #endif |
| 1536 | #ifdef B2500000 |
| 1537 | check_speed(2500000); |
| 1538 | #endif |
| 1539 | #ifdef B3000000 |
| 1540 | check_speed(3000000); |
| 1541 | #endif |
| 1542 | #ifdef B3500000 |
| 1543 | check_speed(3500000); |
| 1544 | #endif |
| 1545 | #ifdef B4000000 |
| 1546 | check_speed(4000000); |
| 1547 | #endif |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1548 | spd = B115200; |
Stefan Weil | 45eea13 | 2009-10-26 16:10:10 +0100 | [diff] [blame] | 1549 | } while (0); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1550 | |
| 1551 | cfsetispeed(&tty, spd); |
| 1552 | cfsetospeed(&tty, spd); |
| 1553 | |
| 1554 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1555 | |INLCR|IGNCR|ICRNL|IXON); |
| 1556 | tty.c_oflag |= OPOST; |
| 1557 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
| 1558 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
| 1559 | switch(data_bits) { |
| 1560 | default: |
| 1561 | case 8: |
| 1562 | tty.c_cflag |= CS8; |
| 1563 | break; |
| 1564 | case 7: |
| 1565 | tty.c_cflag |= CS7; |
| 1566 | break; |
| 1567 | case 6: |
| 1568 | tty.c_cflag |= CS6; |
| 1569 | break; |
| 1570 | case 5: |
| 1571 | tty.c_cflag |= CS5; |
| 1572 | break; |
| 1573 | } |
| 1574 | switch(parity) { |
| 1575 | default: |
| 1576 | case 'N': |
| 1577 | break; |
| 1578 | case 'E': |
| 1579 | tty.c_cflag |= PARENB; |
| 1580 | break; |
| 1581 | case 'O': |
| 1582 | tty.c_cflag |= PARENB | PARODD; |
| 1583 | break; |
| 1584 | } |
| 1585 | if (stop_bits == 2) |
| 1586 | tty.c_cflag |= CSTOPB; |
| 1587 | |
| 1588 | tcsetattr (fd, TCSANOW, &tty); |
| 1589 | } |
| 1590 | |
| 1591 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1592 | { |
| 1593 | FDCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1594 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1595 | |
| 1596 | switch(cmd) { |
| 1597 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1598 | { |
| 1599 | QEMUSerialSetParams *ssp = arg; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1600 | tty_serial_init(fioc->fd, |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1601 | ssp->speed, ssp->parity, |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1602 | ssp->data_bits, ssp->stop_bits); |
| 1603 | } |
| 1604 | break; |
| 1605 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1606 | { |
| 1607 | int enable = *(int *)arg; |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1608 | if (enable) { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1609 | tcsendbreak(fioc->fd, 1); |
Anthony Liguori | a29753f | 2013-03-05 23:21:19 +0530 | [diff] [blame] | 1610 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1611 | } |
| 1612 | break; |
| 1613 | case CHR_IOCTL_SERIAL_GET_TIOCM: |
| 1614 | { |
| 1615 | int sarg = 0; |
| 1616 | int *targ = (int *)arg; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1617 | ioctl(fioc->fd, TIOCMGET, &sarg); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1618 | *targ = 0; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1619 | if (sarg & TIOCM_CTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1620 | *targ |= CHR_TIOCM_CTS; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1621 | if (sarg & TIOCM_CAR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1622 | *targ |= CHR_TIOCM_CAR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1623 | if (sarg & TIOCM_DSR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1624 | *targ |= CHR_TIOCM_DSR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1625 | if (sarg & TIOCM_RI) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1626 | *targ |= CHR_TIOCM_RI; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1627 | if (sarg & TIOCM_DTR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1628 | *targ |= CHR_TIOCM_DTR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1629 | if (sarg & TIOCM_RTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1630 | *targ |= CHR_TIOCM_RTS; |
| 1631 | } |
| 1632 | break; |
| 1633 | case CHR_IOCTL_SERIAL_SET_TIOCM: |
| 1634 | { |
| 1635 | int sarg = *(int *)arg; |
| 1636 | int targ = 0; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1637 | ioctl(fioc->fd, TIOCMGET, &targ); |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1638 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
| 1639 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); |
| 1640 | if (sarg & CHR_TIOCM_CTS) |
| 1641 | targ |= TIOCM_CTS; |
| 1642 | if (sarg & CHR_TIOCM_CAR) |
| 1643 | targ |= TIOCM_CAR; |
| 1644 | if (sarg & CHR_TIOCM_DSR) |
| 1645 | targ |= TIOCM_DSR; |
| 1646 | if (sarg & CHR_TIOCM_RI) |
| 1647 | targ |= TIOCM_RI; |
| 1648 | if (sarg & CHR_TIOCM_DTR) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1649 | targ |= TIOCM_DTR; |
aurel32 | b4abdfa | 2009-02-08 14:46:17 +0000 | [diff] [blame] | 1650 | if (sarg & CHR_TIOCM_RTS) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1651 | targ |= TIOCM_RTS; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 1652 | ioctl(fioc->fd, TIOCMSET, &targ); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1653 | } |
| 1654 | break; |
| 1655 | default: |
| 1656 | return -ENOTSUP; |
| 1657 | } |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1661 | static void qemu_chr_close_tty(CharDriverState *chr) |
| 1662 | { |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1663 | fd_chr_close(chr); |
David Ahern | 4266a13 | 2010-02-10 18:27:17 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1666 | static CharDriverState *qemu_chr_open_tty_fd(int fd, |
| 1667 | ChardevCommon *backend, |
| 1668 | Error **errp) |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 1669 | { |
| 1670 | CharDriverState *chr; |
| 1671 | |
| 1672 | tty_serial_init(fd, 115200, 'N', 8, 1); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1673 | chr = qemu_chr_open_fd(fd, fd, backend, errp); |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 1674 | chr->chr_ioctl = tty_serial_ioctl; |
| 1675 | chr->chr_close = qemu_chr_close_tty; |
| 1676 | return chr; |
| 1677 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1678 | #endif /* __linux__ || __sun__ */ |
| 1679 | |
| 1680 | #if defined(__linux__) |
Gerd Hoffmann | e551498 | 2012-12-19 16:35:42 +0100 | [diff] [blame] | 1681 | |
| 1682 | #define HAVE_CHARDEV_PARPORT 1 |
| 1683 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1684 | typedef struct { |
| 1685 | int fd; |
| 1686 | int mode; |
| 1687 | } ParallelCharDriver; |
| 1688 | |
| 1689 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 1690 | { |
| 1691 | if (s->mode != mode) { |
| 1692 | int m = mode; |
| 1693 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 1694 | return 0; |
| 1695 | s->mode = mode; |
| 1696 | } |
| 1697 | return 1; |
| 1698 | } |
| 1699 | |
| 1700 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1701 | { |
| 1702 | ParallelCharDriver *drv = chr->opaque; |
| 1703 | int fd = drv->fd; |
| 1704 | uint8_t b; |
| 1705 | |
| 1706 | switch(cmd) { |
| 1707 | case CHR_IOCTL_PP_READ_DATA: |
| 1708 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1709 | return -ENOTSUP; |
| 1710 | *(uint8_t *)arg = b; |
| 1711 | break; |
| 1712 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1713 | b = *(uint8_t *)arg; |
| 1714 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1715 | return -ENOTSUP; |
| 1716 | break; |
| 1717 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1718 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1719 | return -ENOTSUP; |
| 1720 | /* Linux gives only the lowest bits, and no way to know data |
| 1721 | direction! For better compatibility set the fixed upper |
| 1722 | bits. */ |
| 1723 | *(uint8_t *)arg = b | 0xc0; |
| 1724 | break; |
| 1725 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1726 | b = *(uint8_t *)arg; |
| 1727 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1728 | return -ENOTSUP; |
| 1729 | break; |
| 1730 | case CHR_IOCTL_PP_READ_STATUS: |
| 1731 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1732 | return -ENOTSUP; |
| 1733 | *(uint8_t *)arg = b; |
| 1734 | break; |
| 1735 | case CHR_IOCTL_PP_DATA_DIR: |
| 1736 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
| 1737 | return -ENOTSUP; |
| 1738 | break; |
| 1739 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 1740 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1741 | struct ParallelIOArg *parg = arg; |
| 1742 | int n = read(fd, parg->buffer, parg->count); |
| 1743 | if (n != parg->count) { |
| 1744 | return -EIO; |
| 1745 | } |
| 1746 | } |
| 1747 | break; |
| 1748 | case CHR_IOCTL_PP_EPP_READ: |
| 1749 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1750 | struct ParallelIOArg *parg = arg; |
| 1751 | int n = read(fd, parg->buffer, parg->count); |
| 1752 | if (n != parg->count) { |
| 1753 | return -EIO; |
| 1754 | } |
| 1755 | } |
| 1756 | break; |
| 1757 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 1758 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1759 | struct ParallelIOArg *parg = arg; |
| 1760 | int n = write(fd, parg->buffer, parg->count); |
| 1761 | if (n != parg->count) { |
| 1762 | return -EIO; |
| 1763 | } |
| 1764 | } |
| 1765 | break; |
| 1766 | case CHR_IOCTL_PP_EPP_WRITE: |
| 1767 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1768 | struct ParallelIOArg *parg = arg; |
| 1769 | int n = write(fd, parg->buffer, parg->count); |
| 1770 | if (n != parg->count) { |
| 1771 | return -EIO; |
| 1772 | } |
| 1773 | } |
| 1774 | break; |
| 1775 | default: |
| 1776 | return -ENOTSUP; |
| 1777 | } |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | static void pp_close(CharDriverState *chr) |
| 1782 | { |
| 1783 | ParallelCharDriver *drv = chr->opaque; |
| 1784 | int fd = drv->fd; |
| 1785 | |
| 1786 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 1787 | ioctl(fd, PPRELEASE); |
| 1788 | close(fd); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1789 | g_free(drv); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1790 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1793 | static CharDriverState *qemu_chr_open_pp_fd(int fd, |
| 1794 | ChardevCommon *backend, |
| 1795 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1796 | { |
| 1797 | CharDriverState *chr; |
| 1798 | ParallelCharDriver *drv; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1799 | |
| 1800 | if (ioctl(fd, PPCLAIM) < 0) { |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 1801 | error_setg_errno(errp, errno, "not a parallel port"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1802 | close(fd); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1803 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1806 | chr = qemu_chr_alloc(backend, errp); |
| 1807 | if (!chr) { |
| 1808 | return NULL; |
| 1809 | } |
Paolo Bonzini | 27ef9cb | 2016-01-18 11:25:45 +0100 | [diff] [blame] | 1810 | |
| 1811 | drv = g_new0(ParallelCharDriver, 1); |
| 1812 | chr->opaque = drv; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1813 | chr->chr_write = null_chr_write; |
| 1814 | chr->chr_ioctl = pp_ioctl; |
| 1815 | chr->chr_close = pp_close; |
Paolo Bonzini | 27ef9cb | 2016-01-18 11:25:45 +0100 | [diff] [blame] | 1816 | |
| 1817 | drv->fd = fd; |
| 1818 | drv->mode = IEEE1284_MODE_COMPAT; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1819 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1820 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1821 | } |
| 1822 | #endif /* __linux__ */ |
| 1823 | |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 1824 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Gerd Hoffmann | e551498 | 2012-12-19 16:35:42 +0100 | [diff] [blame] | 1825 | |
| 1826 | #define HAVE_CHARDEV_PARPORT 1 |
| 1827 | |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1828 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1829 | { |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 1830 | int fd = (int)(intptr_t)chr->opaque; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1831 | uint8_t b; |
| 1832 | |
| 1833 | switch(cmd) { |
| 1834 | case CHR_IOCTL_PP_READ_DATA: |
| 1835 | if (ioctl(fd, PPIGDATA, &b) < 0) |
| 1836 | return -ENOTSUP; |
| 1837 | *(uint8_t *)arg = b; |
| 1838 | break; |
| 1839 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1840 | b = *(uint8_t *)arg; |
| 1841 | if (ioctl(fd, PPISDATA, &b) < 0) |
| 1842 | return -ENOTSUP; |
| 1843 | break; |
| 1844 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1845 | if (ioctl(fd, PPIGCTRL, &b) < 0) |
| 1846 | return -ENOTSUP; |
| 1847 | *(uint8_t *)arg = b; |
| 1848 | break; |
| 1849 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1850 | b = *(uint8_t *)arg; |
| 1851 | if (ioctl(fd, PPISCTRL, &b) < 0) |
| 1852 | return -ENOTSUP; |
| 1853 | break; |
| 1854 | case CHR_IOCTL_PP_READ_STATUS: |
| 1855 | if (ioctl(fd, PPIGSTATUS, &b) < 0) |
| 1856 | return -ENOTSUP; |
| 1857 | *(uint8_t *)arg = b; |
| 1858 | break; |
| 1859 | default: |
| 1860 | return -ENOTSUP; |
| 1861 | } |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1865 | static CharDriverState *qemu_chr_open_pp_fd(int fd, |
Daniel P. Berrange | 0850d49 | 2016-01-22 17:35:25 +0000 | [diff] [blame] | 1866 | ChardevCommon *backend, |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1867 | Error **errp) |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1868 | { |
| 1869 | CharDriverState *chr; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1870 | |
Daniel P. Berrange | 0850d49 | 2016-01-22 17:35:25 +0000 | [diff] [blame] | 1871 | chr = qemu_chr_alloc(backend, errp); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1872 | if (!chr) { |
| 1873 | return NULL; |
| 1874 | } |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 1875 | chr->opaque = (void *)(intptr_t)fd; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1876 | chr->chr_write = null_chr_write; |
| 1877 | chr->chr_ioctl = pp_ioctl; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 1878 | chr->explicit_be_open = true; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 1879 | return chr; |
blueswir1 | 6972f93 | 2008-11-22 20:49:12 +0000 | [diff] [blame] | 1880 | } |
| 1881 | #endif |
| 1882 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1883 | #else /* _WIN32 */ |
| 1884 | |
Paolo Bonzini | d809ab9 | 2015-10-12 09:46:23 +0200 | [diff] [blame] | 1885 | #define HAVE_CHARDEV_SERIAL 1 |
| 1886 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1887 | typedef struct { |
| 1888 | int max_size; |
| 1889 | HANDLE hcom, hrecv, hsend; |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1890 | OVERLAPPED orecv; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1891 | BOOL fpipe; |
| 1892 | DWORD len; |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 1893 | |
| 1894 | /* Protected by the CharDriverState chr_write_lock. */ |
| 1895 | OVERLAPPED osend; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1896 | } WinCharState; |
| 1897 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 1898 | typedef struct { |
| 1899 | HANDLE hStdIn; |
| 1900 | HANDLE hInputReadyEvent; |
| 1901 | HANDLE hInputDoneEvent; |
| 1902 | HANDLE hInputThread; |
| 1903 | uint8_t win_stdio_buf; |
| 1904 | } WinStdioCharState; |
| 1905 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1906 | #define NSENDBUF 2048 |
| 1907 | #define NRECVBUF 2048 |
| 1908 | #define MAXCONNECT 1 |
| 1909 | #define NTIMEOUT 5000 |
| 1910 | |
| 1911 | static int win_chr_poll(void *opaque); |
| 1912 | static int win_chr_pipe_poll(void *opaque); |
| 1913 | |
| 1914 | static void win_chr_close(CharDriverState *chr) |
| 1915 | { |
| 1916 | WinCharState *s = chr->opaque; |
| 1917 | |
| 1918 | if (s->hsend) { |
| 1919 | CloseHandle(s->hsend); |
| 1920 | s->hsend = NULL; |
| 1921 | } |
| 1922 | if (s->hrecv) { |
| 1923 | CloseHandle(s->hrecv); |
| 1924 | s->hrecv = NULL; |
| 1925 | } |
| 1926 | if (s->hcom) { |
| 1927 | CloseHandle(s->hcom); |
| 1928 | s->hcom = NULL; |
| 1929 | } |
| 1930 | if (s->fpipe) |
| 1931 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
| 1932 | else |
| 1933 | qemu_del_polling_cb(win_chr_poll, chr); |
Amit Shah | 793cbfb | 2009-08-11 21:27:48 +0530 | [diff] [blame] | 1934 | |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 1935 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1938 | static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1939 | { |
| 1940 | WinCharState *s = chr->opaque; |
| 1941 | COMMCONFIG comcfg; |
| 1942 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 1943 | COMSTAT comstat; |
| 1944 | DWORD size; |
| 1945 | DWORD err; |
| 1946 | |
| 1947 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1948 | if (!s->hsend) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1949 | error_setg(errp, "Failed CreateEvent"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1950 | goto fail; |
| 1951 | } |
| 1952 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1953 | if (!s->hrecv) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1954 | error_setg(errp, "Failed CreateEvent"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1955 | goto fail; |
| 1956 | } |
| 1957 | |
| 1958 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 1959 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 1960 | if (s->hcom == INVALID_HANDLE_VALUE) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1961 | error_setg(errp, "Failed CreateFile (%lu)", GetLastError()); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1962 | s->hcom = NULL; |
| 1963 | goto fail; |
| 1964 | } |
| 1965 | |
| 1966 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1967 | error_setg(errp, "Failed SetupComm"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1968 | goto fail; |
| 1969 | } |
| 1970 | |
| 1971 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 1972 | size = sizeof(COMMCONFIG); |
| 1973 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 1974 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 1975 | CommConfigDialog(filename, NULL, &comcfg); |
| 1976 | |
| 1977 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1978 | error_setg(errp, "Failed SetCommState"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1979 | goto fail; |
| 1980 | } |
| 1981 | |
| 1982 | if (!SetCommMask(s->hcom, EV_ERR)) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1983 | error_setg(errp, "Failed SetCommMask"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1984 | goto fail; |
| 1985 | } |
| 1986 | |
| 1987 | cto.ReadIntervalTimeout = MAXDWORD; |
| 1988 | if (!SetCommTimeouts(s->hcom, &cto)) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1989 | error_setg(errp, "Failed SetCommTimeouts"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1990 | goto fail; |
| 1991 | } |
| 1992 | |
| 1993 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 1994 | error_setg(errp, "Failed ClearCommError"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1995 | goto fail; |
| 1996 | } |
| 1997 | qemu_add_polling_cb(win_chr_poll, chr); |
| 1998 | return 0; |
| 1999 | |
| 2000 | fail: |
| 2001 | win_chr_close(chr); |
| 2002 | return -1; |
| 2003 | } |
| 2004 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 2005 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2006 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 2007 | { |
| 2008 | WinCharState *s = chr->opaque; |
| 2009 | DWORD len, ret, size, err; |
| 2010 | |
| 2011 | len = len1; |
| 2012 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 2013 | s->osend.hEvent = s->hsend; |
| 2014 | while (len > 0) { |
| 2015 | if (s->hsend) |
| 2016 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 2017 | else |
| 2018 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 2019 | if (!ret) { |
| 2020 | err = GetLastError(); |
| 2021 | if (err == ERROR_IO_PENDING) { |
| 2022 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 2023 | if (ret) { |
| 2024 | buf += size; |
| 2025 | len -= size; |
| 2026 | } else { |
| 2027 | break; |
| 2028 | } |
| 2029 | } else { |
| 2030 | break; |
| 2031 | } |
| 2032 | } else { |
| 2033 | buf += size; |
| 2034 | len -= size; |
| 2035 | } |
| 2036 | } |
| 2037 | return len1 - len; |
| 2038 | } |
| 2039 | |
| 2040 | static int win_chr_read_poll(CharDriverState *chr) |
| 2041 | { |
| 2042 | WinCharState *s = chr->opaque; |
| 2043 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2044 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2045 | return s->max_size; |
| 2046 | } |
| 2047 | |
| 2048 | static void win_chr_readfile(CharDriverState *chr) |
| 2049 | { |
| 2050 | WinCharState *s = chr->opaque; |
| 2051 | int ret, err; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2052 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2053 | DWORD size; |
| 2054 | |
| 2055 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 2056 | s->orecv.hEvent = s->hrecv; |
| 2057 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 2058 | if (!ret) { |
| 2059 | err = GetLastError(); |
| 2060 | if (err == ERROR_IO_PENDING) { |
| 2061 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | if (size > 0) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2066 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | static void win_chr_read(CharDriverState *chr) |
| 2071 | { |
| 2072 | WinCharState *s = chr->opaque; |
| 2073 | |
| 2074 | if (s->len > s->max_size) |
| 2075 | s->len = s->max_size; |
| 2076 | if (s->len == 0) |
| 2077 | return; |
| 2078 | |
| 2079 | win_chr_readfile(chr); |
| 2080 | } |
| 2081 | |
| 2082 | static int win_chr_poll(void *opaque) |
| 2083 | { |
| 2084 | CharDriverState *chr = opaque; |
| 2085 | WinCharState *s = chr->opaque; |
| 2086 | COMSTAT status; |
| 2087 | DWORD comerr; |
| 2088 | |
| 2089 | ClearCommError(s->hcom, &comerr, &status); |
| 2090 | if (status.cbInQue > 0) { |
| 2091 | s->len = status.cbInQue; |
| 2092 | win_chr_read_poll(chr); |
| 2093 | win_chr_read(chr); |
| 2094 | return 1; |
| 2095 | } |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 2099 | static CharDriverState *qemu_chr_open_win_path(const char *filename, |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2100 | ChardevCommon *backend, |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 2101 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2102 | { |
| 2103 | CharDriverState *chr; |
| 2104 | WinCharState *s; |
| 2105 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2106 | chr = qemu_chr_alloc(backend, errp); |
| 2107 | if (!chr) { |
| 2108 | return NULL; |
| 2109 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 2110 | s = g_new0(WinCharState, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2111 | chr->opaque = s; |
| 2112 | chr->chr_write = win_chr_write; |
| 2113 | chr->chr_close = win_chr_close; |
| 2114 | |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 2115 | if (win_chr_init(chr, filename, errp) < 0) { |
Stefan Weil | 2e02e18 | 2011-10-07 07:38:46 +0200 | [diff] [blame] | 2116 | g_free(s); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2117 | qemu_chr_free_common(chr); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2118 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2119 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2120 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | static int win_chr_pipe_poll(void *opaque) |
| 2124 | { |
| 2125 | CharDriverState *chr = opaque; |
| 2126 | WinCharState *s = chr->opaque; |
| 2127 | DWORD size; |
| 2128 | |
| 2129 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 2130 | if (size > 0) { |
| 2131 | s->len = size; |
| 2132 | win_chr_read_poll(chr); |
| 2133 | win_chr_read(chr); |
| 2134 | return 1; |
| 2135 | } |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2139 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename, |
| 2140 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2141 | { |
| 2142 | WinCharState *s = chr->opaque; |
| 2143 | OVERLAPPED ov; |
| 2144 | int ret; |
| 2145 | DWORD size; |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 2146 | char *openname; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2147 | |
| 2148 | s->fpipe = TRUE; |
| 2149 | |
| 2150 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2151 | if (!s->hsend) { |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2152 | error_setg(errp, "Failed CreateEvent"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2153 | goto fail; |
| 2154 | } |
| 2155 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2156 | if (!s->hrecv) { |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2157 | error_setg(errp, "Failed CreateEvent"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2158 | goto fail; |
| 2159 | } |
| 2160 | |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 2161 | openname = g_strdup_printf("\\\\.\\pipe\\%s", filename); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2162 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 2163 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 2164 | PIPE_WAIT, |
| 2165 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 2166 | g_free(openname); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2167 | if (s->hcom == INVALID_HANDLE_VALUE) { |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2168 | error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError()); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2169 | s->hcom = NULL; |
| 2170 | goto fail; |
| 2171 | } |
| 2172 | |
| 2173 | ZeroMemory(&ov, sizeof(ov)); |
| 2174 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2175 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 2176 | if (ret) { |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2177 | error_setg(errp, "Failed ConnectNamedPipe"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2178 | goto fail; |
| 2179 | } |
| 2180 | |
| 2181 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 2182 | if (!ret) { |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2183 | error_setg(errp, "Failed GetOverlappedResult"); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2184 | if (ov.hEvent) { |
| 2185 | CloseHandle(ov.hEvent); |
| 2186 | ov.hEvent = NULL; |
| 2187 | } |
| 2188 | goto fail; |
| 2189 | } |
| 2190 | |
| 2191 | if (ov.hEvent) { |
| 2192 | CloseHandle(ov.hEvent); |
| 2193 | ov.hEvent = NULL; |
| 2194 | } |
| 2195 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
| 2196 | return 0; |
| 2197 | |
| 2198 | fail: |
| 2199 | win_chr_close(chr); |
| 2200 | return -1; |
| 2201 | } |
| 2202 | |
| 2203 | |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2204 | static CharDriverState *qemu_chr_open_pipe(const char *id, |
| 2205 | ChardevBackend *backend, |
| 2206 | ChardevReturn *ret, |
| 2207 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2208 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2209 | ChardevHostdev *opts = backend->u.pipe.data; |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 2210 | const char *filename = opts->device; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2211 | CharDriverState *chr; |
| 2212 | WinCharState *s; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 2213 | ChardevCommon *common = qapi_ChardevHostdev_base(opts); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2214 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2215 | chr = qemu_chr_alloc(common, errp); |
| 2216 | if (!chr) { |
| 2217 | return NULL; |
| 2218 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 2219 | s = g_new0(WinCharState, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2220 | chr->opaque = s; |
| 2221 | chr->chr_write = win_chr_write; |
| 2222 | chr->chr_close = win_chr_close; |
| 2223 | |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 2224 | if (win_chr_pipe_init(chr, filename, errp) < 0) { |
Stefan Weil | 2e02e18 | 2011-10-07 07:38:46 +0200 | [diff] [blame] | 2225 | g_free(s); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2226 | qemu_chr_free_common(chr); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2227 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2228 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2229 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2232 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out, |
| 2233 | ChardevCommon *backend, |
| 2234 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2235 | { |
| 2236 | CharDriverState *chr; |
| 2237 | WinCharState *s; |
| 2238 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2239 | chr = qemu_chr_alloc(backend, errp); |
| 2240 | if (!chr) { |
| 2241 | return NULL; |
| 2242 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 2243 | s = g_new0(WinCharState, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2244 | s->hcom = fd_out; |
| 2245 | chr->opaque = s; |
| 2246 | chr->chr_write = win_chr_write; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2247 | return chr; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Paolo Bonzini | 122e5ed | 2015-09-29 15:42:04 +0200 | [diff] [blame] | 2250 | static CharDriverState *qemu_chr_open_win_con(const char *id, |
| 2251 | ChardevBackend *backend, |
| 2252 | ChardevReturn *ret, |
| 2253 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2254 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2255 | ChardevCommon *common = backend->u.console.data; |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2256 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE), |
| 2257 | common, errp); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2260 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2261 | { |
| 2262 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 2263 | DWORD dwSize; |
| 2264 | int len1; |
| 2265 | |
| 2266 | len1 = len; |
| 2267 | |
| 2268 | while (len1 > 0) { |
| 2269 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { |
| 2270 | break; |
| 2271 | } |
| 2272 | buf += dwSize; |
| 2273 | len1 -= dwSize; |
| 2274 | } |
| 2275 | |
| 2276 | return len - len1; |
| 2277 | } |
| 2278 | |
| 2279 | static void win_stdio_wait_func(void *opaque) |
| 2280 | { |
| 2281 | CharDriverState *chr = opaque; |
| 2282 | WinStdioCharState *stdio = chr->opaque; |
| 2283 | INPUT_RECORD buf[4]; |
| 2284 | int ret; |
| 2285 | DWORD dwSize; |
| 2286 | int i; |
| 2287 | |
Stefan Weil | dff7424 | 2013-12-07 14:48:04 +0100 | [diff] [blame] | 2288 | ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2289 | |
| 2290 | if (!ret) { |
| 2291 | /* Avoid error storm */ |
| 2292 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); |
| 2293 | return; |
| 2294 | } |
| 2295 | |
| 2296 | for (i = 0; i < dwSize; i++) { |
| 2297 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; |
| 2298 | |
| 2299 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { |
| 2300 | int j; |
| 2301 | if (kev->uChar.AsciiChar != 0) { |
| 2302 | for (j = 0; j < kev->wRepeatCount; j++) { |
| 2303 | if (qemu_chr_be_can_write(chr)) { |
| 2304 | uint8_t c = kev->uChar.AsciiChar; |
| 2305 | qemu_chr_be_write(chr, &c, 1); |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | static DWORD WINAPI win_stdio_thread(LPVOID param) |
| 2314 | { |
| 2315 | CharDriverState *chr = param; |
| 2316 | WinStdioCharState *stdio = chr->opaque; |
| 2317 | int ret; |
| 2318 | DWORD dwSize; |
| 2319 | |
| 2320 | while (1) { |
| 2321 | |
| 2322 | /* Wait for one byte */ |
| 2323 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); |
| 2324 | |
| 2325 | /* Exit in case of error, continue if nothing read */ |
| 2326 | if (!ret) { |
| 2327 | break; |
| 2328 | } |
| 2329 | if (!dwSize) { |
| 2330 | continue; |
| 2331 | } |
| 2332 | |
| 2333 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ |
| 2334 | if (stdio->win_stdio_buf == '\r') { |
| 2335 | continue; |
| 2336 | } |
| 2337 | |
| 2338 | /* Signal the main thread and wait until the byte was eaten */ |
| 2339 | if (!SetEvent(stdio->hInputReadyEvent)) { |
| 2340 | break; |
| 2341 | } |
| 2342 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) |
| 2343 | != WAIT_OBJECT_0) { |
| 2344 | break; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); |
| 2349 | return 0; |
| 2350 | } |
| 2351 | |
| 2352 | static void win_stdio_thread_wait_func(void *opaque) |
| 2353 | { |
| 2354 | CharDriverState *chr = opaque; |
| 2355 | WinStdioCharState *stdio = chr->opaque; |
| 2356 | |
| 2357 | if (qemu_chr_be_can_write(chr)) { |
| 2358 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); |
| 2359 | } |
| 2360 | |
| 2361 | SetEvent(stdio->hInputDoneEvent); |
| 2362 | } |
| 2363 | |
| 2364 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) |
| 2365 | { |
| 2366 | WinStdioCharState *stdio = chr->opaque; |
| 2367 | DWORD dwMode = 0; |
| 2368 | |
| 2369 | GetConsoleMode(stdio->hStdIn, &dwMode); |
| 2370 | |
| 2371 | if (echo) { |
| 2372 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); |
| 2373 | } else { |
| 2374 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | static void win_stdio_close(CharDriverState *chr) |
| 2379 | { |
| 2380 | WinStdioCharState *stdio = chr->opaque; |
| 2381 | |
| 2382 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { |
| 2383 | CloseHandle(stdio->hInputReadyEvent); |
| 2384 | } |
| 2385 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { |
| 2386 | CloseHandle(stdio->hInputDoneEvent); |
| 2387 | } |
| 2388 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { |
| 2389 | TerminateThread(stdio->hInputThread, 0); |
| 2390 | } |
| 2391 | |
| 2392 | g_free(chr->opaque); |
| 2393 | g_free(chr); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2396 | static CharDriverState *qemu_chr_open_stdio(const char *id, |
| 2397 | ChardevBackend *backend, |
| 2398 | ChardevReturn *ret, |
| 2399 | Error **errp) |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2400 | { |
| 2401 | CharDriverState *chr; |
| 2402 | WinStdioCharState *stdio; |
| 2403 | DWORD dwMode; |
| 2404 | int is_console = 0; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2405 | ChardevCommon *common = qapi_ChardevStdio_base(backend->u.stdio.data); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2406 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2407 | chr = qemu_chr_alloc(common, errp); |
| 2408 | if (!chr) { |
| 2409 | return NULL; |
| 2410 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 2411 | stdio = g_new0(WinStdioCharState, 1); |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2412 | |
| 2413 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); |
| 2414 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2415 | error_setg(errp, "cannot open stdio: invalid handle"); |
| 2416 | return NULL; |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; |
| 2420 | |
| 2421 | chr->opaque = stdio; |
| 2422 | chr->chr_write = win_stdio_write; |
| 2423 | chr->chr_close = win_stdio_close; |
| 2424 | |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2425 | if (is_console) { |
| 2426 | if (qemu_add_wait_object(stdio->hStdIn, |
| 2427 | win_stdio_wait_func, chr)) { |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2428 | error_setg(errp, "qemu_add_wait_object: failed"); |
| 2429 | goto err1; |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2430 | } |
| 2431 | } else { |
| 2432 | DWORD dwId; |
| 2433 | |
| 2434 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 2435 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2436 | if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2437 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2438 | error_setg(errp, "cannot create event"); |
| 2439 | goto err2; |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2440 | } |
| 2441 | if (qemu_add_wait_object(stdio->hInputReadyEvent, |
| 2442 | win_stdio_thread_wait_func, chr)) { |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2443 | error_setg(errp, "qemu_add_wait_object: failed"); |
| 2444 | goto err2; |
| 2445 | } |
| 2446 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, |
| 2447 | chr, 0, &dwId); |
| 2448 | |
| 2449 | if (stdio->hInputThread == INVALID_HANDLE_VALUE) { |
| 2450 | error_setg(errp, "cannot create stdio thread"); |
| 2451 | goto err3; |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | dwMode |= ENABLE_LINE_INPUT; |
| 2456 | |
Anthony Liguori | ed7a154 | 2013-03-05 23:21:17 +0530 | [diff] [blame] | 2457 | if (is_console) { |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2458 | /* set the terminal in raw mode */ |
| 2459 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ |
| 2460 | dwMode |= ENABLE_PROCESSED_INPUT; |
| 2461 | } |
| 2462 | |
| 2463 | SetConsoleMode(stdio->hStdIn, dwMode); |
| 2464 | |
| 2465 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; |
| 2466 | qemu_chr_fe_set_echo(chr, false); |
| 2467 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2468 | return chr; |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 2469 | |
| 2470 | err3: |
| 2471 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); |
| 2472 | err2: |
| 2473 | CloseHandle(stdio->hInputReadyEvent); |
| 2474 | CloseHandle(stdio->hInputDoneEvent); |
| 2475 | err1: |
| 2476 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); |
| 2477 | return NULL; |
Fabien Chouteau | db418a0 | 2011-10-06 16:37:51 +0200 | [diff] [blame] | 2478 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2479 | #endif /* !_WIN32 */ |
| 2480 | |
Anthony Liguori | 5ab8211 | 2013-03-05 23:21:31 +0530 | [diff] [blame] | 2481 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2482 | /***********************************************************/ |
| 2483 | /* UDP Net console */ |
| 2484 | |
| 2485 | typedef struct { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2486 | QIOChannel *ioc; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2487 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2488 | int bufcnt; |
| 2489 | int bufptr; |
| 2490 | int max_size; |
| 2491 | } NetCharDriver; |
| 2492 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 2493 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2494 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2495 | { |
| 2496 | NetCharDriver *s = chr->opaque; |
| 2497 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2498 | return qio_channel_write( |
| 2499 | s->ioc, (const char *)buf, len, NULL); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | static int udp_chr_read_poll(void *opaque) |
| 2503 | { |
| 2504 | CharDriverState *chr = opaque; |
| 2505 | NetCharDriver *s = chr->opaque; |
| 2506 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2507 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* If there were any stray characters in the queue process them |
| 2510 | * first |
| 2511 | */ |
| 2512 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2513 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2514 | s->bufptr++; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2515 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2516 | } |
| 2517 | return s->max_size; |
| 2518 | } |
| 2519 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2520 | static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2521 | { |
| 2522 | CharDriverState *chr = opaque; |
| 2523 | NetCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2524 | ssize_t ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2525 | |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 2526 | if (s->max_size == 0) { |
| 2527 | return TRUE; |
| 2528 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2529 | ret = qio_channel_read( |
| 2530 | s->ioc, (char *)s->buf, sizeof(s->buf), NULL); |
| 2531 | if (ret <= 0) { |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2532 | remove_fd_in_watch(chr); |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2533 | return FALSE; |
| 2534 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2535 | s->bufcnt = ret; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2536 | |
| 2537 | s->bufptr = 0; |
| 2538 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2539 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2540 | s->bufptr++; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2541 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2542 | } |
Anthony Liguori | 76a9644 | 2013-03-05 23:21:21 +0530 | [diff] [blame] | 2543 | |
| 2544 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static void udp_chr_update_read_handler(CharDriverState *chr) |
| 2548 | { |
| 2549 | NetCharDriver *s = chr->opaque; |
| 2550 | |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2551 | remove_fd_in_watch(chr); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2552 | if (s->ioc) { |
| 2553 | chr->fd_in_tag = io_add_watch_poll(s->ioc, |
| 2554 | udp_chr_read_poll, |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 2555 | udp_chr_read, chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2559 | static void udp_chr_close(CharDriverState *chr) |
| 2560 | { |
| 2561 | NetCharDriver *s = chr->opaque; |
Amit Shah | 26da70c | 2013-08-28 15:23:37 +0530 | [diff] [blame] | 2562 | |
| 2563 | remove_fd_in_watch(chr); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2564 | if (s->ioc) { |
| 2565 | object_unref(OBJECT(s->ioc)); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2566 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2567 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2568 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2571 | static CharDriverState *qemu_chr_open_udp(QIOChannelSocket *sioc, |
| 2572 | ChardevCommon *backend, |
| 2573 | Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2574 | { |
| 2575 | CharDriverState *chr = NULL; |
| 2576 | NetCharDriver *s = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2577 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2578 | chr = qemu_chr_alloc(backend, errp); |
| 2579 | if (!chr) { |
| 2580 | return NULL; |
| 2581 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 2582 | s = g_new0(NetCharDriver, 1); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2583 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2584 | s->ioc = QIO_CHANNEL(sioc); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2585 | s->bufcnt = 0; |
| 2586 | s->bufptr = 0; |
| 2587 | chr->opaque = s; |
| 2588 | chr->chr_write = udp_chr_write; |
| 2589 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 2590 | chr->chr_close = udp_chr_close; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 2591 | /* be isn't opened until we get a connection */ |
| 2592 | chr->explicit_be_open = true; |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2593 | return chr; |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 2594 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2595 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2596 | /***********************************************************/ |
| 2597 | /* TCP Net console */ |
| 2598 | |
| 2599 | typedef struct { |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 2600 | QIOChannel *ioc; /* Client I/O channel */ |
| 2601 | QIOChannelSocket *sioc; /* Client master channel */ |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2602 | QIOChannelSocket *listen_ioc; |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 2603 | guint listen_tag; |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 2604 | QCryptoTLSCreds *tls_creds; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2605 | int connected; |
| 2606 | int max_size; |
| 2607 | int do_telnetopt; |
| 2608 | int do_nodelay; |
| 2609 | int is_unix; |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2610 | int *read_msgfds; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2611 | size_t read_msgfds_num; |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2612 | int *write_msgfds; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2613 | size_t write_msgfds_num; |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 2614 | |
| 2615 | SocketAddress *addr; |
| 2616 | bool is_listen; |
| 2617 | bool is_telnet; |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 2618 | |
| 2619 | guint reconnect_timer; |
| 2620 | int64_t reconnect_time; |
Corey Minyard | 5008e5b | 2014-10-08 07:11:55 -0500 | [diff] [blame] | 2621 | bool connect_err_reported; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2622 | } TCPCharDriver; |
| 2623 | |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 2624 | static gboolean socket_reconnect_timeout(gpointer opaque); |
| 2625 | |
| 2626 | static void qemu_chr_socket_restart_timer(CharDriverState *chr) |
| 2627 | { |
| 2628 | TCPCharDriver *s = chr->opaque; |
| 2629 | assert(s->connected == 0); |
| 2630 | s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time, |
| 2631 | socket_reconnect_timeout, chr); |
| 2632 | } |
| 2633 | |
Corey Minyard | 5008e5b | 2014-10-08 07:11:55 -0500 | [diff] [blame] | 2634 | static void check_report_connect_error(CharDriverState *chr, |
| 2635 | Error *err) |
| 2636 | { |
| 2637 | TCPCharDriver *s = chr->opaque; |
| 2638 | |
| 2639 | if (!s->connect_err_reported) { |
| 2640 | error_report("Unable to connect character device %s: %s", |
| 2641 | chr->label, error_get_pretty(err)); |
| 2642 | s->connect_err_reported = true; |
| 2643 | } |
| 2644 | qemu_chr_socket_restart_timer(chr); |
| 2645 | } |
| 2646 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2647 | static gboolean tcp_chr_accept(QIOChannel *chan, |
| 2648 | GIOCondition cond, |
| 2649 | void *opaque); |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2650 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 2651 | /* Called with chr_write_lock held. */ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2652 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2653 | { |
| 2654 | TCPCharDriver *s = chr->opaque; |
| 2655 | if (s->connected) { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2656 | int ret = io_channel_send_full(s->ioc, buf, len, |
| 2657 | s->write_msgfds, |
| 2658 | s->write_msgfds_num); |
| 2659 | |
| 2660 | /* free the written msgfds, no matter what */ |
| 2661 | if (s->write_msgfds_num) { |
| 2662 | g_free(s->write_msgfds); |
| 2663 | s->write_msgfds = 0; |
| 2664 | s->write_msgfds_num = 0; |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2665 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2666 | |
| 2667 | return ret; |
Anthony Liguori | 455aa1e | 2012-09-05 13:52:49 -0500 | [diff] [blame] | 2668 | } else { |
Anthony Liguori | 6db0fdc | 2012-09-12 14:34:07 -0500 | [diff] [blame] | 2669 | /* XXX: indicate an error ? */ |
Anthony Liguori | 455aa1e | 2012-09-05 13:52:49 -0500 | [diff] [blame] | 2670 | return len; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | static int tcp_chr_read_poll(void *opaque) |
| 2675 | { |
| 2676 | CharDriverState *chr = opaque; |
| 2677 | TCPCharDriver *s = chr->opaque; |
| 2678 | if (!s->connected) |
| 2679 | return 0; |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 2680 | s->max_size = qemu_chr_be_can_write(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2681 | return s->max_size; |
| 2682 | } |
| 2683 | |
| 2684 | #define IAC 255 |
| 2685 | #define IAC_BREAK 243 |
| 2686 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2687 | TCPCharDriver *s, |
| 2688 | uint8_t *buf, int *size) |
| 2689 | { |
| 2690 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2691 | * char mode with no echo. All IAC options will be removed from |
| 2692 | * the buf and the do_telnetopt variable will be used to track the |
| 2693 | * state of the width of the IAC information. |
| 2694 | * |
| 2695 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2696 | * "IAC BREAK" command and the double IAC. |
| 2697 | */ |
| 2698 | |
| 2699 | int i; |
| 2700 | int j = 0; |
| 2701 | |
| 2702 | for (i = 0; i < *size; i++) { |
| 2703 | if (s->do_telnetopt > 1) { |
| 2704 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2705 | /* Double IAC means send an IAC */ |
| 2706 | if (j != i) |
| 2707 | buf[j] = buf[i]; |
| 2708 | j++; |
| 2709 | s->do_telnetopt = 1; |
| 2710 | } else { |
| 2711 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2712 | /* Handle IAC break commands by sending a serial break */ |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 2713 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2714 | s->do_telnetopt++; |
| 2715 | } |
| 2716 | s->do_telnetopt++; |
| 2717 | } |
| 2718 | if (s->do_telnetopt >= 4) { |
| 2719 | s->do_telnetopt = 1; |
| 2720 | } |
| 2721 | } else { |
| 2722 | if ((unsigned char)buf[i] == IAC) { |
| 2723 | s->do_telnetopt = 2; |
| 2724 | } else { |
| 2725 | if (j != i) |
| 2726 | buf[j] = buf[i]; |
| 2727 | j++; |
| 2728 | } |
| 2729 | } |
| 2730 | } |
| 2731 | *size = j; |
| 2732 | } |
| 2733 | |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2734 | static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num) |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2735 | { |
| 2736 | TCPCharDriver *s = chr->opaque; |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2737 | int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num; |
| 2738 | |
Michael S. Tsirkin | c409572 | 2014-11-02 18:48:32 +0200 | [diff] [blame] | 2739 | assert(num <= TCP_MAX_FDS); |
| 2740 | |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2741 | if (to_copy) { |
Stefan Hajnoczi | d2fc39b | 2014-06-22 10:38:37 +0800 | [diff] [blame] | 2742 | int i; |
| 2743 | |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2744 | memcpy(fds, s->read_msgfds, to_copy * sizeof(int)); |
| 2745 | |
Stefan Hajnoczi | d2fc39b | 2014-06-22 10:38:37 +0800 | [diff] [blame] | 2746 | /* Close unused fds */ |
| 2747 | for (i = to_copy; i < s->read_msgfds_num; i++) { |
| 2748 | close(s->read_msgfds[i]); |
| 2749 | } |
| 2750 | |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2751 | g_free(s->read_msgfds); |
| 2752 | s->read_msgfds = 0; |
| 2753 | s->read_msgfds_num = 0; |
| 2754 | } |
| 2755 | |
| 2756 | return to_copy; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2759 | static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num) |
| 2760 | { |
| 2761 | TCPCharDriver *s = chr->opaque; |
| 2762 | |
| 2763 | /* clear old pending fd array */ |
Daniel P. Berrange | ef1e1e0 | 2015-08-26 12:17:18 +0100 | [diff] [blame] | 2764 | g_free(s->write_msgfds); |
Marc-André Lureau | 869a58a | 2016-02-23 19:10:51 +0100 | [diff] [blame] | 2765 | s->write_msgfds = NULL; |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2766 | |
Marc-André Lureau | 5c7eaab | 2016-07-27 01:15:08 +0400 | [diff] [blame] | 2767 | if (!s->connected || |
| 2768 | !qio_channel_has_feature(s->ioc, |
| 2769 | QIO_CHANNEL_FEATURE_FD_PASS)) { |
| 2770 | return -1; |
| 2771 | } |
| 2772 | |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2773 | if (num) { |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 2774 | s->write_msgfds = g_new(int, num); |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 2775 | memcpy(s->write_msgfds, fds, num * sizeof(int)); |
| 2776 | } |
| 2777 | |
| 2778 | s->write_msgfds_num = num; |
| 2779 | |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2783 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2784 | { |
| 2785 | TCPCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2786 | struct iovec iov = { .iov_base = buf, .iov_len = len }; |
| 2787 | int ret; |
| 2788 | size_t i; |
| 2789 | int *msgfds = NULL; |
| 2790 | size_t msgfds_num = 0; |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2791 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2792 | if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) { |
| 2793 | ret = qio_channel_readv_full(s->ioc, &iov, 1, |
| 2794 | &msgfds, &msgfds_num, |
| 2795 | NULL); |
| 2796 | } else { |
| 2797 | ret = qio_channel_readv_full(s->ioc, &iov, 1, |
| 2798 | NULL, NULL, |
| 2799 | NULL); |
| 2800 | } |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2801 | |
Marc-André Lureau | b6572b4 | 2016-03-11 18:55:24 +0100 | [diff] [blame] | 2802 | if (ret == QIO_CHANNEL_ERR_BLOCK) { |
| 2803 | errno = EAGAIN; |
| 2804 | ret = -1; |
| 2805 | } else if (ret == -1) { |
| 2806 | errno = EIO; |
| 2807 | } |
| 2808 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2809 | if (msgfds_num) { |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2810 | /* close and clean read_msgfds */ |
| 2811 | for (i = 0; i < s->read_msgfds_num; i++) { |
| 2812 | close(s->read_msgfds[i]); |
| 2813 | } |
Stefan Hajnoczi | 9b938c7 | 2013-03-27 10:10:46 +0100 | [diff] [blame] | 2814 | |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2815 | if (s->read_msgfds_num) { |
| 2816 | g_free(s->read_msgfds); |
| 2817 | } |
| 2818 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2819 | s->read_msgfds = msgfds; |
| 2820 | s->read_msgfds_num = msgfds_num; |
| 2821 | } |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2822 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2823 | for (i = 0; i < s->read_msgfds_num; i++) { |
| 2824 | int fd = s->read_msgfds[i]; |
| 2825 | if (fd < 0) { |
| 2826 | continue; |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 2827 | } |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2828 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2829 | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ |
| 2830 | qemu_set_block(fd); |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2831 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2832 | #ifndef MSG_CMSG_CLOEXEC |
| 2833 | qemu_set_cloexec(fd); |
Corey Bryant | 0613865 | 2012-08-14 16:43:42 -0400 | [diff] [blame] | 2834 | #endif |
Corey Bryant | 0613865 | 2012-08-14 16:43:42 -0400 | [diff] [blame] | 2835 | } |
Mark McLoughlin | 7d17405 | 2009-07-22 09:11:39 +0100 | [diff] [blame] | 2836 | |
| 2837 | return ret; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2838 | } |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2839 | |
Amit Shah | d3cc5bc | 2013-03-05 23:21:25 +0530 | [diff] [blame] | 2840 | static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
| 2841 | { |
| 2842 | TCPCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2843 | return qio_channel_create_watch(s->ioc, cond); |
Amit Shah | d3cc5bc | 2013-03-05 23:21:25 +0530 | [diff] [blame] | 2844 | } |
| 2845 | |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2846 | static void tcp_chr_disconnect(CharDriverState *chr) |
| 2847 | { |
| 2848 | TCPCharDriver *s = chr->opaque; |
| 2849 | |
Marc-André Lureau | 342f7a9 | 2016-02-23 19:10:53 +0100 | [diff] [blame] | 2850 | if (!s->connected) { |
| 2851 | return; |
| 2852 | } |
| 2853 | |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2854 | s->connected = 0; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2855 | if (s->listen_ioc) { |
| 2856 | s->listen_tag = qio_channel_add_watch( |
| 2857 | QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2858 | } |
Marc-André Lureau | 6167ebb | 2016-02-23 19:10:52 +0100 | [diff] [blame] | 2859 | tcp_set_msgfds(chr, NULL, 0); |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2860 | remove_fd_in_watch(chr); |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 2861 | object_unref(OBJECT(s->sioc)); |
| 2862 | s->sioc = NULL; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2863 | object_unref(OBJECT(s->ioc)); |
| 2864 | s->ioc = NULL; |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 2865 | g_free(chr->filename); |
| 2866 | chr->filename = SocketAddress_to_str("disconnected:", s->addr, |
| 2867 | s->is_listen, s->is_telnet); |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2868 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 2869 | if (s->reconnect_time) { |
| 2870 | qemu_chr_socket_restart_timer(chr); |
| 2871 | } |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2872 | } |
| 2873 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2874 | static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2875 | { |
| 2876 | CharDriverState *chr = opaque; |
| 2877 | TCPCharDriver *s = chr->opaque; |
Amit Shah | 9bd7854 | 2009-11-03 19:59:54 +0530 | [diff] [blame] | 2878 | uint8_t buf[READ_BUF_LEN]; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2879 | int len, size; |
| 2880 | |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2881 | if (!s->connected || s->max_size <= 0) { |
Paolo Bonzini | cdbf6e1 | 2013-04-19 17:32:08 +0200 | [diff] [blame] | 2882 | return TRUE; |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2883 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2884 | len = sizeof(buf); |
| 2885 | if (len > s->max_size) |
| 2886 | len = s->max_size; |
Mark McLoughlin | 9977c89 | 2009-07-22 09:11:38 +0100 | [diff] [blame] | 2887 | size = tcp_chr_recv(chr, (void *)buf, len); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2888 | if (size == 0 || size == -1) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2889 | /* connection closed */ |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2890 | tcp_chr_disconnect(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2891 | } else if (size > 0) { |
| 2892 | if (s->do_telnetopt) |
| 2893 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2894 | if (size > 0) |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 2895 | qemu_chr_be_write(chr, buf, size); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2896 | } |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 2897 | |
| 2898 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Nikolay Nikolaev | 7b0bfdf | 2014-05-27 15:03:48 +0300 | [diff] [blame] | 2901 | static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len) |
| 2902 | { |
| 2903 | TCPCharDriver *s = chr->opaque; |
| 2904 | int size; |
| 2905 | |
| 2906 | if (!s->connected) { |
| 2907 | return 0; |
| 2908 | } |
| 2909 | |
| 2910 | size = tcp_chr_recv(chr, (void *) buf, len); |
| 2911 | if (size == 0) { |
| 2912 | /* connection closed */ |
| 2913 | tcp_chr_disconnect(chr); |
| 2914 | } |
| 2915 | |
| 2916 | return size; |
| 2917 | } |
| 2918 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2919 | static void tcp_chr_connect(void *opaque) |
| 2920 | { |
| 2921 | CharDriverState *chr = opaque; |
| 2922 | TCPCharDriver *s = chr->opaque; |
Corey Minyard | 16cc4ff | 2014-10-02 11:17:36 -0500 | [diff] [blame] | 2923 | |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 2924 | g_free(chr->filename); |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 2925 | chr->filename = sockaddr_to_str( |
| 2926 | &s->sioc->localAddr, s->sioc->localAddrLen, |
| 2927 | &s->sioc->remoteAddr, s->sioc->remoteAddrLen, |
| 2928 | s->is_listen, s->is_telnet); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2929 | |
| 2930 | s->connected = 1; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2931 | if (s->ioc) { |
| 2932 | chr->fd_in_tag = io_add_watch_poll(s->ioc, |
| 2933 | tcp_chr_read_poll, |
Amit Shah | 7ba9add | 2013-08-28 15:18:29 +0530 | [diff] [blame] | 2934 | tcp_chr_read, chr); |
David Gibson | bbdd2ad | 2012-09-10 12:30:56 +1000 | [diff] [blame] | 2935 | } |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 2936 | qemu_chr_be_generic_open(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 2939 | static void tcp_chr_update_read_handler(CharDriverState *chr) |
| 2940 | { |
| 2941 | TCPCharDriver *s = chr->opaque; |
| 2942 | |
Daniel P. Berrange | 1e94f23 | 2016-02-08 13:55:07 +0000 | [diff] [blame] | 2943 | if (!s->connected) { |
| 2944 | return; |
| 2945 | } |
| 2946 | |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 2947 | remove_fd_in_watch(chr); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 2948 | if (s->ioc) { |
| 2949 | chr->fd_in_tag = io_add_watch_poll(s->ioc, |
| 2950 | tcp_chr_read_poll, |
Gal Hammer | ac1b84d | 2014-02-25 12:12:35 +0200 | [diff] [blame] | 2951 | tcp_chr_read, chr); |
| 2952 | } |
| 2953 | } |
| 2954 | |
Daniel P. Berrange | f2001a7 | 2016-01-19 11:14:30 +0000 | [diff] [blame] | 2955 | typedef struct { |
| 2956 | CharDriverState *chr; |
| 2957 | char buf[12]; |
| 2958 | size_t buflen; |
| 2959 | } TCPCharDriverTelnetInit; |
| 2960 | |
| 2961 | static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc, |
| 2962 | GIOCondition cond G_GNUC_UNUSED, |
| 2963 | gpointer user_data) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 2964 | { |
Daniel P. Berrange | f2001a7 | 2016-01-19 11:14:30 +0000 | [diff] [blame] | 2965 | TCPCharDriverTelnetInit *init = user_data; |
| 2966 | ssize_t ret; |
| 2967 | |
| 2968 | ret = qio_channel_write(ioc, init->buf, init->buflen, NULL); |
| 2969 | if (ret < 0) { |
| 2970 | if (ret == QIO_CHANNEL_ERR_BLOCK) { |
| 2971 | ret = 0; |
| 2972 | } else { |
| 2973 | tcp_chr_disconnect(init->chr); |
| 2974 | return FALSE; |
| 2975 | } |
| 2976 | } |
| 2977 | init->buflen -= ret; |
| 2978 | |
| 2979 | if (init->buflen == 0) { |
| 2980 | tcp_chr_connect(init->chr); |
| 2981 | return FALSE; |
| 2982 | } |
| 2983 | |
| 2984 | memmove(init->buf, init->buf + ret, init->buflen); |
| 2985 | |
| 2986 | return TRUE; |
| 2987 | } |
| 2988 | |
| 2989 | static void tcp_chr_telnet_init(CharDriverState *chr) |
| 2990 | { |
| 2991 | TCPCharDriver *s = chr->opaque; |
| 2992 | TCPCharDriverTelnetInit *init = |
| 2993 | g_new0(TCPCharDriverTelnetInit, 1); |
| 2994 | size_t n = 0; |
| 2995 | |
| 2996 | init->chr = chr; |
| 2997 | init->buflen = 12; |
| 2998 | |
| 2999 | #define IACSET(x, a, b, c) \ |
| 3000 | do { \ |
| 3001 | x[n++] = a; \ |
| 3002 | x[n++] = b; \ |
| 3003 | x[n++] = c; \ |
| 3004 | } while (0) |
| 3005 | |
| 3006 | /* Prep the telnet negotion to put telnet in binary, |
| 3007 | * no echo, single char mode */ |
| 3008 | IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 3009 | IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 3010 | IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 3011 | IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 3012 | |
| 3013 | #undef IACSET |
| 3014 | |
| 3015 | qio_channel_add_watch( |
| 3016 | s->ioc, G_IO_OUT, |
| 3017 | tcp_chr_telnet_init_io, |
| 3018 | init, NULL); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3021 | |
| 3022 | static void tcp_chr_tls_handshake(Object *source, |
| 3023 | Error *err, |
| 3024 | gpointer user_data) |
| 3025 | { |
| 3026 | CharDriverState *chr = user_data; |
| 3027 | TCPCharDriver *s = chr->opaque; |
| 3028 | |
| 3029 | if (err) { |
| 3030 | tcp_chr_disconnect(chr); |
| 3031 | } else { |
| 3032 | if (s->do_telnetopt) { |
| 3033 | tcp_chr_telnet_init(chr); |
| 3034 | } else { |
| 3035 | tcp_chr_connect(chr); |
| 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | |
| 3041 | static void tcp_chr_tls_init(CharDriverState *chr) |
| 3042 | { |
| 3043 | TCPCharDriver *s = chr->opaque; |
| 3044 | QIOChannelTLS *tioc; |
| 3045 | Error *err = NULL; |
| 3046 | |
| 3047 | if (s->is_listen) { |
| 3048 | tioc = qio_channel_tls_new_server( |
| 3049 | s->ioc, s->tls_creds, |
| 3050 | NULL, /* XXX Use an ACL */ |
| 3051 | &err); |
| 3052 | } else { |
| 3053 | tioc = qio_channel_tls_new_client( |
| 3054 | s->ioc, s->tls_creds, |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3055 | s->addr->u.inet.data->host, |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3056 | &err); |
| 3057 | } |
| 3058 | if (tioc == NULL) { |
| 3059 | error_free(err); |
| 3060 | tcp_chr_disconnect(chr); |
| 3061 | } |
| 3062 | object_unref(OBJECT(s->ioc)); |
| 3063 | s->ioc = QIO_CHANNEL(tioc); |
| 3064 | |
| 3065 | qio_channel_tls_handshake(tioc, |
| 3066 | tcp_chr_tls_handshake, |
| 3067 | chr, |
| 3068 | NULL); |
| 3069 | } |
| 3070 | |
| 3071 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3072 | static int tcp_chr_new_client(CharDriverState *chr, QIOChannelSocket *sioc) |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 3073 | { |
| 3074 | TCPCharDriver *s = chr->opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3075 | if (s->ioc != NULL) { |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 3076 | return -1; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3077 | } |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 3078 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3079 | s->ioc = QIO_CHANNEL(sioc); |
| 3080 | object_ref(OBJECT(sioc)); |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3081 | s->sioc = sioc; |
| 3082 | object_ref(OBJECT(sioc)); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3083 | |
Daniel P. Berrange | 64c800f | 2016-03-18 18:00:41 +0000 | [diff] [blame] | 3084 | qio_channel_set_blocking(s->ioc, false, NULL); |
| 3085 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3086 | if (s->do_nodelay) { |
| 3087 | qio_channel_set_delay(s->ioc, false); |
| 3088 | } |
Paolo Bonzini | 910b636 | 2013-04-19 17:32:06 +0200 | [diff] [blame] | 3089 | if (s->listen_tag) { |
| 3090 | g_source_remove(s->listen_tag); |
| 3091 | s->listen_tag = 0; |
| 3092 | } |
Daniel P. Berrange | f2001a7 | 2016-01-19 11:14:30 +0000 | [diff] [blame] | 3093 | |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3094 | if (s->tls_creds) { |
| 3095 | tcp_chr_tls_init(chr); |
Daniel P. Berrange | f2001a7 | 2016-01-19 11:14:30 +0000 | [diff] [blame] | 3096 | } else { |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3097 | if (s->do_telnetopt) { |
| 3098 | tcp_chr_telnet_init(chr); |
| 3099 | } else { |
| 3100 | tcp_chr_connect(chr); |
| 3101 | } |
Daniel P. Berrange | f2001a7 | 2016-01-19 11:14:30 +0000 | [diff] [blame] | 3102 | } |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 3103 | |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3107 | |
| 3108 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
| 3109 | { |
| 3110 | int ret; |
| 3111 | QIOChannelSocket *sioc; |
| 3112 | |
| 3113 | sioc = qio_channel_socket_new_fd(fd, NULL); |
| 3114 | if (!sioc) { |
| 3115 | return -1; |
| 3116 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3117 | ret = tcp_chr_new_client(chr, sioc); |
| 3118 | object_unref(OBJECT(sioc)); |
| 3119 | return ret; |
| 3120 | } |
| 3121 | |
| 3122 | static gboolean tcp_chr_accept(QIOChannel *channel, |
| 3123 | GIOCondition cond, |
| 3124 | void *opaque) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3125 | { |
| 3126 | CharDriverState *chr = opaque; |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3127 | QIOChannelSocket *sioc; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3128 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3129 | sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel), |
| 3130 | NULL); |
| 3131 | if (!sioc) { |
| 3132 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3133 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3134 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3135 | tcp_chr_new_client(chr, sioc); |
| 3136 | |
| 3137 | object_unref(OBJECT(sioc)); |
Anthony Liguori | 2ea5a7a | 2013-03-05 23:21:22 +0530 | [diff] [blame] | 3138 | |
| 3139 | return TRUE; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Marc-André Lureau | d7a04fd | 2016-07-27 01:15:18 +0400 | [diff] [blame] | 3142 | static int tcp_chr_wait_connected(CharDriverState *chr, Error **errp) |
| 3143 | { |
| 3144 | TCPCharDriver *s = chr->opaque; |
| 3145 | QIOChannelSocket *sioc; |
| 3146 | |
| 3147 | while (!s->connected) { |
| 3148 | if (s->is_listen) { |
| 3149 | fprintf(stderr, "QEMU waiting for connection on: %s\n", |
| 3150 | chr->filename); |
| 3151 | qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), true, NULL); |
| 3152 | tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); |
| 3153 | qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), false, NULL); |
| 3154 | } else { |
| 3155 | sioc = qio_channel_socket_new(); |
| 3156 | if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { |
| 3157 | object_unref(OBJECT(sioc)); |
| 3158 | return -1; |
| 3159 | } |
| 3160 | tcp_chr_new_client(chr, sioc); |
| 3161 | object_unref(OBJECT(sioc)); |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | return 0; |
| 3166 | } |
| 3167 | |
Marc-André Lureau | 6b6723c | 2016-07-27 01:15:17 +0400 | [diff] [blame] | 3168 | int qemu_chr_wait_connected(CharDriverState *chr, Error **errp) |
| 3169 | { |
| 3170 | if (chr->chr_wait_connected) { |
| 3171 | return chr->chr_wait_connected(chr, errp); |
| 3172 | } |
| 3173 | |
| 3174 | return 0; |
| 3175 | } |
| 3176 | |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3177 | static void tcp_chr_close(CharDriverState *chr) |
| 3178 | { |
| 3179 | TCPCharDriver *s = chr->opaque; |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 3180 | int i; |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 3181 | |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3182 | if (s->reconnect_timer) { |
| 3183 | g_source_remove(s->reconnect_timer); |
| 3184 | s->reconnect_timer = 0; |
| 3185 | } |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 3186 | qapi_free_SocketAddress(s->addr); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3187 | remove_fd_in_watch(chr); |
| 3188 | if (s->ioc) { |
| 3189 | object_unref(OBJECT(s->ioc)); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 3190 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3191 | if (s->listen_tag) { |
| 3192 | g_source_remove(s->listen_tag); |
| 3193 | s->listen_tag = 0; |
| 3194 | } |
| 3195 | if (s->listen_ioc) { |
| 3196 | object_unref(OBJECT(s->listen_ioc)); |
aliguori | 819f56b | 2009-03-28 17:58:14 +0000 | [diff] [blame] | 3197 | } |
Nikolay Nikolaev | c76bf6b | 2014-05-27 15:04:15 +0300 | [diff] [blame] | 3198 | if (s->read_msgfds_num) { |
| 3199 | for (i = 0; i < s->read_msgfds_num; i++) { |
| 3200 | close(s->read_msgfds[i]); |
| 3201 | } |
| 3202 | g_free(s->read_msgfds); |
| 3203 | } |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3204 | if (s->tls_creds) { |
| 3205 | object_unref(OBJECT(s->tls_creds)); |
| 3206 | } |
Nikolay Nikolaev | d39aac7 | 2014-05-27 15:04:02 +0300 | [diff] [blame] | 3207 | if (s->write_msgfds_num) { |
| 3208 | g_free(s->write_msgfds); |
| 3209 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3210 | g_free(s); |
Hans de Goede | a425d23 | 2011-11-19 10:22:43 +0100 | [diff] [blame] | 3211 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 3214 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3215 | static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque) |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3216 | { |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3217 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(src); |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3218 | CharDriverState *chr = opaque; |
Corey Minyard | 5008e5b | 2014-10-08 07:11:55 -0500 | [diff] [blame] | 3219 | TCPCharDriver *s = chr->opaque; |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3220 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3221 | if (err) { |
Corey Minyard | 5008e5b | 2014-10-08 07:11:55 -0500 | [diff] [blame] | 3222 | check_report_connect_error(chr, err); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 3223 | object_unref(src); |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3224 | return; |
| 3225 | } |
| 3226 | |
Corey Minyard | 5008e5b | 2014-10-08 07:11:55 -0500 | [diff] [blame] | 3227 | s->connect_err_reported = false; |
Daniel P. Berrange | f50dfe4 | 2016-03-09 16:42:28 +0000 | [diff] [blame] | 3228 | tcp_chr_new_client(chr, sioc); |
| 3229 | object_unref(OBJECT(sioc)); |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3230 | } |
| 3231 | |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 3232 | |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3233 | /*********************************************************/ |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3234 | /* Ring buffer chardev */ |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3235 | |
| 3236 | typedef struct { |
| 3237 | size_t size; |
| 3238 | size_t prod; |
| 3239 | size_t cons; |
| 3240 | uint8_t *cbuf; |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3241 | } RingBufCharDriver; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3242 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3243 | static size_t ringbuf_count(const CharDriverState *chr) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3244 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3245 | const RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3246 | |
Markus Armbruster | 5c23010 | 2013-02-06 21:27:23 +0100 | [diff] [blame] | 3247 | return d->prod - d->cons; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3248 | } |
| 3249 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 3250 | /* Called with chr_write_lock held. */ |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3251 | static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3252 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3253 | RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3254 | int i; |
| 3255 | |
| 3256 | if (!buf || (len < 0)) { |
| 3257 | return -1; |
| 3258 | } |
| 3259 | |
| 3260 | for (i = 0; i < len; i++ ) { |
Markus Armbruster | 5c23010 | 2013-02-06 21:27:23 +0100 | [diff] [blame] | 3261 | d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; |
| 3262 | if (d->prod - d->cons > d->size) { |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3263 | d->cons = d->prod - d->size; |
| 3264 | } |
| 3265 | } |
| 3266 | |
| 3267 | return 0; |
| 3268 | } |
| 3269 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3270 | static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3271 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3272 | RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3273 | int i; |
| 3274 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 3275 | qemu_mutex_lock(&chr->chr_write_lock); |
Markus Armbruster | 5c23010 | 2013-02-06 21:27:23 +0100 | [diff] [blame] | 3276 | for (i = 0; i < len && d->cons != d->prod; i++) { |
| 3277 | buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3278 | } |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 3279 | qemu_mutex_unlock(&chr->chr_write_lock); |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3280 | |
| 3281 | return i; |
| 3282 | } |
| 3283 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3284 | static void ringbuf_chr_close(struct CharDriverState *chr) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3285 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3286 | RingBufCharDriver *d = chr->opaque; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3287 | |
| 3288 | g_free(d->cbuf); |
| 3289 | g_free(d); |
| 3290 | chr->opaque = NULL; |
| 3291 | } |
| 3292 | |
Paolo Bonzini | 479f09a | 2015-09-29 15:51:14 +0200 | [diff] [blame] | 3293 | static CharDriverState *qemu_chr_open_ringbuf(const char *id, |
| 3294 | ChardevBackend *backend, |
| 3295 | ChardevReturn *ret, |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 3296 | Error **errp) |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3297 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3298 | ChardevRingbuf *opts = backend->u.ringbuf.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3299 | ChardevCommon *common = qapi_ChardevRingbuf_base(opts); |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3300 | CharDriverState *chr; |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3301 | RingBufCharDriver *d; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3302 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 3303 | chr = qemu_chr_alloc(common, errp); |
| 3304 | if (!chr) { |
| 3305 | return NULL; |
| 3306 | } |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3307 | d = g_malloc(sizeof(*d)); |
| 3308 | |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3309 | d->size = opts->has_size ? opts->size : 65536; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3310 | |
| 3311 | /* The size must be power of 2 */ |
| 3312 | if (d->size & (d->size - 1)) { |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 3313 | error_setg(errp, "size of ringbuf chardev must be power of two"); |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3314 | goto fail; |
| 3315 | } |
| 3316 | |
| 3317 | d->prod = 0; |
| 3318 | d->cons = 0; |
| 3319 | d->cbuf = g_malloc0(d->size); |
| 3320 | |
| 3321 | chr->opaque = d; |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3322 | chr->chr_write = ringbuf_chr_write; |
| 3323 | chr->chr_close = ringbuf_chr_close; |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3324 | |
| 3325 | return chr; |
| 3326 | |
| 3327 | fail: |
| 3328 | g_free(d); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 3329 | qemu_chr_free_common(chr); |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 3330 | return NULL; |
| 3331 | } |
| 3332 | |
Hani Benhabiles | 8e59777 | 2014-05-27 23:39:30 +0100 | [diff] [blame] | 3333 | bool chr_is_ringbuf(const CharDriverState *chr) |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3334 | { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3335 | return chr->chr_write == ringbuf_chr_write; |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3336 | } |
| 3337 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3338 | void qmp_ringbuf_write(const char *device, const char *data, |
Markus Armbruster | 82e59a6 | 2013-02-06 21:27:14 +0100 | [diff] [blame] | 3339 | bool has_format, enum DataFormat format, |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3340 | Error **errp) |
| 3341 | { |
| 3342 | CharDriverState *chr; |
Markus Armbruster | c4f331b | 2013-02-06 21:27:17 +0100 | [diff] [blame] | 3343 | const uint8_t *write_data; |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3344 | int ret; |
Peter Crosthwaite | 3d1bba2 | 2013-05-22 13:01:43 +1000 | [diff] [blame] | 3345 | gsize write_count; |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3346 | |
| 3347 | chr = qemu_chr_find(device); |
| 3348 | if (!chr) { |
Markus Armbruster | 1a69278 | 2013-02-06 21:27:16 +0100 | [diff] [blame] | 3349 | error_setg(errp, "Device '%s' not found", device); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3350 | return; |
| 3351 | } |
| 3352 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3353 | if (!chr_is_ringbuf(chr)) { |
| 3354 | error_setg(errp,"%s is not a ringbuf device", device); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3355 | return; |
| 3356 | } |
| 3357 | |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3358 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
Daniel P. Berrange | e9cf2fe | 2015-11-23 15:29:59 +0000 | [diff] [blame] | 3359 | write_data = qbase64_decode(data, -1, |
| 3360 | &write_count, |
| 3361 | errp); |
| 3362 | if (!write_data) { |
| 3363 | return; |
| 3364 | } |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3365 | } else { |
| 3366 | write_data = (uint8_t *)data; |
Markus Armbruster | 82e59a6 | 2013-02-06 21:27:14 +0100 | [diff] [blame] | 3367 | write_count = strlen(data); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3368 | } |
| 3369 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3370 | ret = ringbuf_chr_write(chr, write_data, write_count); |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3371 | |
Markus Armbruster | 13289fb | 2013-02-06 21:27:18 +0100 | [diff] [blame] | 3372 | if (write_data != (uint8_t *)data) { |
| 3373 | g_free((void *)write_data); |
| 3374 | } |
| 3375 | |
Lei Li | 1f590cf | 2013-01-25 00:03:20 +0800 | [diff] [blame] | 3376 | if (ret < 0) { |
| 3377 | error_setg(errp, "Failed to write to device %s", device); |
| 3378 | return; |
| 3379 | } |
| 3380 | } |
| 3381 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3382 | char *qmp_ringbuf_read(const char *device, int64_t size, |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 3383 | bool has_format, enum DataFormat format, |
| 3384 | Error **errp) |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3385 | { |
| 3386 | CharDriverState *chr; |
Markus Armbruster | c4f331b | 2013-02-06 21:27:17 +0100 | [diff] [blame] | 3387 | uint8_t *read_data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3388 | size_t count; |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 3389 | char *data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3390 | |
| 3391 | chr = qemu_chr_find(device); |
| 3392 | if (!chr) { |
Markus Armbruster | 1a69278 | 2013-02-06 21:27:16 +0100 | [diff] [blame] | 3393 | error_setg(errp, "Device '%s' not found", device); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3394 | return NULL; |
| 3395 | } |
| 3396 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3397 | if (!chr_is_ringbuf(chr)) { |
| 3398 | error_setg(errp,"%s is not a ringbuf device", device); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3399 | return NULL; |
| 3400 | } |
| 3401 | |
| 3402 | if (size <= 0) { |
| 3403 | error_setg(errp, "size must be greater than zero"); |
| 3404 | return NULL; |
| 3405 | } |
| 3406 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3407 | count = ringbuf_count(chr); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3408 | size = size > count ? count : size; |
Markus Armbruster | 44f3bcd | 2013-02-06 21:27:20 +0100 | [diff] [blame] | 3409 | read_data = g_malloc(size + 1); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3410 | |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3411 | ringbuf_chr_read(chr, read_data, size); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3412 | |
| 3413 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 3414 | data = g_base64_encode(read_data, size); |
Markus Armbruster | 13289fb | 2013-02-06 21:27:18 +0100 | [diff] [blame] | 3415 | g_free(read_data); |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3416 | } else { |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 3417 | /* |
| 3418 | * FIXME should read only complete, valid UTF-8 characters up |
| 3419 | * to @size bytes. Invalid sequences should be replaced by a |
| 3420 | * suitable replacement character. Except when (and only |
| 3421 | * when) ring buffer lost characters since last read, initial |
| 3422 | * continuation characters should be dropped. |
| 3423 | */ |
Markus Armbruster | 44f3bcd | 2013-02-06 21:27:20 +0100 | [diff] [blame] | 3424 | read_data[size] = 0; |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 3425 | data = (char *)read_data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3426 | } |
| 3427 | |
Markus Armbruster | 3ab651f | 2013-02-06 21:27:15 +0100 | [diff] [blame] | 3428 | return data; |
Lei Li | 49b6d72 | 2013-01-25 00:03:21 +0800 | [diff] [blame] | 3429 | } |
| 3430 | |
Gerd Hoffmann | 3352163 | 2009-12-08 13:11:49 +0100 | [diff] [blame] | 3431 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3432 | { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 3433 | char host[65], port[33], width[8], height[8]; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3434 | int pos; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 3435 | const char *p; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3436 | QemuOpts *opts; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 3437 | Error *local_err = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3438 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 3439 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3440 | if (local_err) { |
Markus Armbruster | 3339488 | 2015-02-10 15:21:26 +0100 | [diff] [blame] | 3441 | error_report_err(local_err); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3442 | return NULL; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 3443 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3444 | |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3445 | if (strstart(filename, "mon:", &p)) { |
| 3446 | filename = p; |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3447 | qemu_opt_set(opts, "mux", "on", &error_abort); |
Paolo Bonzini | 02c4bdf | 2013-07-03 20:29:45 +0400 | [diff] [blame] | 3448 | if (strcmp(filename, "stdio") == 0) { |
| 3449 | /* Monitor is muxed to stdio: do not exit on Ctrl+C by default |
| 3450 | * but pass it to the guest. Handle this only for compat syntax, |
| 3451 | * for -chardev syntax we have special option for this. |
| 3452 | * This is what -nographic did, redirecting+muxing serial+monitor |
| 3453 | * to stdio causing Ctrl+C to be passed to guest. */ |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3454 | qemu_opt_set(opts, "signal", "off", &error_abort); |
Paolo Bonzini | 02c4bdf | 2013-07-03 20:29:45 +0400 | [diff] [blame] | 3455 | } |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3456 | } |
| 3457 | |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 3458 | if (strcmp(filename, "null") == 0 || |
| 3459 | strcmp(filename, "pty") == 0 || |
| 3460 | strcmp(filename, "msmouse") == 0 || |
Gerd Hoffmann | dc1c21e | 2009-09-10 10:58:46 +0200 | [diff] [blame] | 3461 | strcmp(filename, "braille") == 0 || |
Paolo Bonzini | 5692399 | 2014-07-11 09:44:26 +0200 | [diff] [blame] | 3462 | strcmp(filename, "testdev") == 0 || |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 3463 | strcmp(filename, "stdio") == 0) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3464 | qemu_opt_set(opts, "backend", filename, &error_abort); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3465 | return opts; |
| 3466 | } |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 3467 | if (strstart(filename, "vc", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3468 | qemu_opt_set(opts, "backend", "vc", &error_abort); |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 3469 | if (*p == ':') { |
Stefan Weil | 49aa405 | 2013-09-30 23:04:49 +0200 | [diff] [blame] | 3470 | if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 3471 | /* pixels */ |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3472 | qemu_opt_set(opts, "width", width, &error_abort); |
| 3473 | qemu_opt_set(opts, "height", height, &error_abort); |
Stefan Weil | 49aa405 | 2013-09-30 23:04:49 +0200 | [diff] [blame] | 3474 | } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 3475 | /* chars */ |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3476 | qemu_opt_set(opts, "cols", width, &error_abort); |
| 3477 | qemu_opt_set(opts, "rows", height, &error_abort); |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 3478 | } else { |
| 3479 | goto fail; |
| 3480 | } |
| 3481 | } |
| 3482 | return opts; |
| 3483 | } |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 3484 | if (strcmp(filename, "con:") == 0) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3485 | qemu_opt_set(opts, "backend", "console", &error_abort); |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 3486 | return opts; |
| 3487 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3488 | if (strstart(filename, "COM", NULL)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3489 | qemu_opt_set(opts, "backend", "serial", &error_abort); |
| 3490 | qemu_opt_set(opts, "path", filename, &error_abort); |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3491 | return opts; |
| 3492 | } |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 3493 | if (strstart(filename, "file:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3494 | qemu_opt_set(opts, "backend", "file", &error_abort); |
| 3495 | qemu_opt_set(opts, "path", p, &error_abort); |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 3496 | return opts; |
| 3497 | } |
| 3498 | if (strstart(filename, "pipe:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3499 | qemu_opt_set(opts, "backend", "pipe", &error_abort); |
| 3500 | qemu_opt_set(opts, "path", p, &error_abort); |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 3501 | return opts; |
| 3502 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3503 | if (strstart(filename, "tcp:", &p) || |
| 3504 | strstart(filename, "telnet:", &p)) { |
| 3505 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 3506 | host[0] = 0; |
| 3507 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) |
| 3508 | goto fail; |
| 3509 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3510 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
| 3511 | qemu_opt_set(opts, "host", host, &error_abort); |
| 3512 | qemu_opt_set(opts, "port", port, &error_abort); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3513 | if (p[pos] == ',') { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3514 | qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); |
| 3515 | if (local_err) { |
| 3516 | error_report_err(local_err); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3517 | goto fail; |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3518 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3519 | } |
| 3520 | if (strstart(filename, "telnet:", &p)) |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3521 | qemu_opt_set(opts, "telnet", "on", &error_abort); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3522 | return opts; |
| 3523 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3524 | if (strstart(filename, "udp:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3525 | qemu_opt_set(opts, "backend", "udp", &error_abort); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3526 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
| 3527 | host[0] = 0; |
Jan Kiszka | 39324ca | 2010-03-07 11:28:48 +0100 | [diff] [blame] | 3528 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3529 | goto fail; |
| 3530 | } |
| 3531 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3532 | qemu_opt_set(opts, "host", host, &error_abort); |
| 3533 | qemu_opt_set(opts, "port", port, &error_abort); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3534 | if (p[pos] == '@') { |
| 3535 | p += pos + 1; |
| 3536 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 3537 | host[0] = 0; |
| 3538 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3539 | goto fail; |
| 3540 | } |
| 3541 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3542 | qemu_opt_set(opts, "localaddr", host, &error_abort); |
| 3543 | qemu_opt_set(opts, "localport", port, &error_abort); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3544 | } |
| 3545 | return opts; |
| 3546 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3547 | if (strstart(filename, "unix:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3548 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3549 | qemu_opts_do_parse(opts, p, "path", &local_err); |
| 3550 | if (local_err) { |
| 3551 | error_report_err(local_err); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3552 | goto fail; |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3553 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3554 | return opts; |
| 3555 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3556 | if (strstart(filename, "/dev/parport", NULL) || |
| 3557 | strstart(filename, "/dev/ppi", NULL)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3558 | qemu_opt_set(opts, "backend", "parport", &error_abort); |
| 3559 | qemu_opt_set(opts, "path", filename, &error_abort); |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3560 | return opts; |
| 3561 | } |
| 3562 | if (strstart(filename, "/dev/", NULL)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3563 | qemu_opt_set(opts, "backend", "tty", &error_abort); |
| 3564 | qemu_opt_set(opts, "path", filename, &error_abort); |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 3565 | return opts; |
| 3566 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3567 | |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 3568 | fail: |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3569 | qemu_opts_del(opts); |
| 3570 | return NULL; |
| 3571 | } |
| 3572 | |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 3573 | void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 3574 | { |
| 3575 | const char *logfile = qemu_opt_get(opts, "logfile"); |
| 3576 | |
| 3577 | backend->has_logfile = logfile != NULL; |
| 3578 | backend->logfile = logfile ? g_strdup(logfile) : NULL; |
| 3579 | |
| 3580 | backend->has_logappend = true; |
| 3581 | backend->logappend = qemu_opt_get_bool(opts, "logappend", false); |
| 3582 | } |
| 3583 | |
| 3584 | |
Gerd Hoffmann | 846e2e4 | 2013-02-21 12:07:14 +0100 | [diff] [blame] | 3585 | static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, |
| 3586 | Error **errp) |
| 3587 | { |
| 3588 | const char *path = qemu_opt_get(opts, "path"); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3589 | ChardevFile *file; |
Gerd Hoffmann | 846e2e4 | 2013-02-21 12:07:14 +0100 | [diff] [blame] | 3590 | |
| 3591 | if (path == NULL) { |
| 3592 | error_setg(errp, "chardev: file: no filename given"); |
| 3593 | return; |
| 3594 | } |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3595 | file = backend->u.file.data = g_new0(ChardevFile, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3596 | qemu_chr_parse_common(opts, qapi_ChardevFile_base(file)); |
| 3597 | file->out = g_strdup(path); |
Olga Krishtal | 31e38a2 | 2015-12-04 09:42:04 +0300 | [diff] [blame] | 3598 | |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3599 | file->has_append = true; |
| 3600 | file->append = qemu_opt_get_bool(opts, "append", false); |
Gerd Hoffmann | 846e2e4 | 2013-02-21 12:07:14 +0100 | [diff] [blame] | 3601 | } |
| 3602 | |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 3603 | static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, |
| 3604 | Error **errp) |
| 3605 | { |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3606 | ChardevStdio *stdio; |
| 3607 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3608 | stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3609 | qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio)); |
| 3610 | stdio->has_signal = true; |
| 3611 | stdio->signal = qemu_opt_get_bool(opts, "signal", true); |
Gerd Hoffmann | 7c35803 | 2013-02-21 12:34:58 +0100 | [diff] [blame] | 3612 | } |
| 3613 | |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 3614 | #ifdef HAVE_CHARDEV_SERIAL |
Gerd Hoffmann | 0f1cb51 | 2013-02-22 15:48:05 +0100 | [diff] [blame] | 3615 | static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, |
| 3616 | Error **errp) |
| 3617 | { |
| 3618 | const char *device = qemu_opt_get(opts, "path"); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3619 | ChardevHostdev *serial; |
Gerd Hoffmann | 0f1cb51 | 2013-02-22 15:48:05 +0100 | [diff] [blame] | 3620 | |
| 3621 | if (device == NULL) { |
| 3622 | error_setg(errp, "chardev: serial/tty: no device path given"); |
| 3623 | return; |
| 3624 | } |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3625 | serial = backend->u.serial.data = g_new0(ChardevHostdev, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3626 | qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(serial)); |
| 3627 | serial->device = g_strdup(device); |
Gerd Hoffmann | 0f1cb51 | 2013-02-22 15:48:05 +0100 | [diff] [blame] | 3628 | } |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 3629 | #endif |
Gerd Hoffmann | 0f1cb51 | 2013-02-22 15:48:05 +0100 | [diff] [blame] | 3630 | |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 3631 | #ifdef HAVE_CHARDEV_PARPORT |
Gerd Hoffmann | dc37509 | 2013-02-22 16:17:01 +0100 | [diff] [blame] | 3632 | static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, |
| 3633 | Error **errp) |
| 3634 | { |
| 3635 | const char *device = qemu_opt_get(opts, "path"); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3636 | ChardevHostdev *parallel; |
Gerd Hoffmann | dc37509 | 2013-02-22 16:17:01 +0100 | [diff] [blame] | 3637 | |
| 3638 | if (device == NULL) { |
| 3639 | error_setg(errp, "chardev: parallel: no device path given"); |
| 3640 | return; |
| 3641 | } |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3642 | parallel = backend->u.parallel.data = g_new0(ChardevHostdev, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3643 | qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(parallel)); |
| 3644 | parallel->device = g_strdup(device); |
Gerd Hoffmann | dc37509 | 2013-02-22 16:17:01 +0100 | [diff] [blame] | 3645 | } |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 3646 | #endif |
Gerd Hoffmann | dc37509 | 2013-02-22 16:17:01 +0100 | [diff] [blame] | 3647 | |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 3648 | static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, |
| 3649 | Error **errp) |
| 3650 | { |
| 3651 | const char *device = qemu_opt_get(opts, "path"); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3652 | ChardevHostdev *dev; |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 3653 | |
| 3654 | if (device == NULL) { |
| 3655 | error_setg(errp, "chardev: pipe: no device path given"); |
| 3656 | return; |
| 3657 | } |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3658 | dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3659 | qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev)); |
| 3660 | dev->device = g_strdup(device); |
Gerd Hoffmann | 548cbb3 | 2013-02-25 11:50:55 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
Markus Armbruster | 4f57378 | 2013-07-26 16:44:32 +0200 | [diff] [blame] | 3663 | static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, |
| 3664 | Error **errp) |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3665 | { |
| 3666 | int val; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3667 | ChardevRingbuf *ringbuf; |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3668 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3669 | ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3670 | qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf)); |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3671 | |
Markus Armbruster | 0f95305 | 2013-06-27 16:22:07 +0200 | [diff] [blame] | 3672 | val = qemu_opt_get_size(opts, "size", 0); |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3673 | if (val != 0) { |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3674 | ringbuf->has_size = true; |
| 3675 | ringbuf->size = val; |
Gerd Hoffmann | 1da48c6 | 2013-02-26 16:21:11 +0100 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 3679 | static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend, |
| 3680 | Error **errp) |
| 3681 | { |
| 3682 | const char *chardev = qemu_opt_get(opts, "chardev"); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3683 | ChardevMux *mux; |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 3684 | |
| 3685 | if (chardev == NULL) { |
| 3686 | error_setg(errp, "chardev: mux: no chardev given"); |
| 3687 | return; |
| 3688 | } |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3689 | mux = backend->u.mux.data = g_new0(ChardevMux, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3690 | qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux)); |
| 3691 | mux->chardev = g_strdup(chardev); |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 3692 | } |
| 3693 | |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3694 | static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, |
| 3695 | Error **errp) |
| 3696 | { |
| 3697 | bool is_listen = qemu_opt_get_bool(opts, "server", false); |
| 3698 | bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true); |
| 3699 | bool is_telnet = qemu_opt_get_bool(opts, "telnet", false); |
| 3700 | bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true); |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 3701 | int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0); |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3702 | const char *path = qemu_opt_get(opts, "path"); |
| 3703 | const char *host = qemu_opt_get(opts, "host"); |
| 3704 | const char *port = qemu_opt_get(opts, "port"); |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3705 | const char *tls_creds = qemu_opt_get(opts, "tls-creds"); |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3706 | SocketAddress *addr; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3707 | ChardevSocket *sock; |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3708 | |
| 3709 | if (!path) { |
| 3710 | if (!host) { |
| 3711 | error_setg(errp, "chardev: socket: no host given"); |
| 3712 | return; |
| 3713 | } |
| 3714 | if (!port) { |
| 3715 | error_setg(errp, "chardev: socket: no port given"); |
| 3716 | return; |
| 3717 | } |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 3718 | } else { |
| 3719 | if (tls_creds) { |
| 3720 | error_setg(errp, "TLS can only be used over TCP socket"); |
| 3721 | return; |
| 3722 | } |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3723 | } |
| 3724 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3725 | sock = backend->u.socket.data = g_new0(ChardevSocket, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3726 | qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock)); |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3727 | |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3728 | sock->has_nodelay = true; |
| 3729 | sock->nodelay = do_nodelay; |
| 3730 | sock->has_server = true; |
| 3731 | sock->server = is_listen; |
| 3732 | sock->has_telnet = true; |
| 3733 | sock->telnet = is_telnet; |
| 3734 | sock->has_wait = true; |
| 3735 | sock->wait = is_waitconnect; |
| 3736 | sock->has_reconnect = true; |
| 3737 | sock->reconnect = reconnect; |
| 3738 | sock->tls_creds = g_strdup(tls_creds); |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3739 | |
| 3740 | addr = g_new0(SocketAddress, 1); |
| 3741 | if (path) { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 3742 | UnixSocketAddress *q_unix; |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 3743 | addr->type = SOCKET_ADDRESS_KIND_UNIX; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3744 | q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1); |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 3745 | q_unix->path = g_strdup(path); |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3746 | } else { |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 3747 | addr->type = SOCKET_ADDRESS_KIND_INET; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3748 | addr->u.inet.data = g_new(InetSocketAddress, 1); |
| 3749 | *addr->u.inet.data = (InetSocketAddress) { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 3750 | .host = g_strdup(host), |
| 3751 | .port = g_strdup(port), |
| 3752 | .has_to = qemu_opt_get(opts, "to"), |
| 3753 | .to = qemu_opt_get_number(opts, "to", 0), |
| 3754 | .has_ipv4 = qemu_opt_get(opts, "ipv4"), |
| 3755 | .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), |
| 3756 | .has_ipv6 = qemu_opt_get(opts, "ipv6"), |
| 3757 | .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), |
| 3758 | }; |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3759 | } |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3760 | sock->addr = addr; |
Peter Maydell | dafd325 | 2014-09-02 11:24:13 +0100 | [diff] [blame] | 3761 | } |
| 3762 | |
Peter Maydell | 90a14bf | 2014-09-02 11:24:15 +0100 | [diff] [blame] | 3763 | static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, |
| 3764 | Error **errp) |
| 3765 | { |
| 3766 | const char *host = qemu_opt_get(opts, "host"); |
| 3767 | const char *port = qemu_opt_get(opts, "port"); |
| 3768 | const char *localaddr = qemu_opt_get(opts, "localaddr"); |
| 3769 | const char *localport = qemu_opt_get(opts, "localport"); |
| 3770 | bool has_local = false; |
| 3771 | SocketAddress *addr; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3772 | ChardevUdp *udp; |
Peter Maydell | 90a14bf | 2014-09-02 11:24:15 +0100 | [diff] [blame] | 3773 | |
| 3774 | if (host == NULL || strlen(host) == 0) { |
| 3775 | host = "localhost"; |
| 3776 | } |
| 3777 | if (port == NULL || strlen(port) == 0) { |
| 3778 | error_setg(errp, "chardev: udp: remote port not specified"); |
| 3779 | return; |
| 3780 | } |
| 3781 | if (localport == NULL || strlen(localport) == 0) { |
| 3782 | localport = "0"; |
| 3783 | } else { |
| 3784 | has_local = true; |
| 3785 | } |
| 3786 | if (localaddr == NULL || strlen(localaddr) == 0) { |
| 3787 | localaddr = ""; |
| 3788 | } else { |
| 3789 | has_local = true; |
| 3790 | } |
| 3791 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3792 | udp = backend->u.udp.data = g_new0(ChardevUdp, 1); |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3793 | qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp)); |
Peter Maydell | 90a14bf | 2014-09-02 11:24:15 +0100 | [diff] [blame] | 3794 | |
| 3795 | addr = g_new0(SocketAddress, 1); |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 3796 | addr->type = SOCKET_ADDRESS_KIND_INET; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3797 | addr->u.inet.data = g_new(InetSocketAddress, 1); |
| 3798 | *addr->u.inet.data = (InetSocketAddress) { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 3799 | .host = g_strdup(host), |
| 3800 | .port = g_strdup(port), |
| 3801 | .has_ipv4 = qemu_opt_get(opts, "ipv4"), |
| 3802 | .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), |
| 3803 | .has_ipv6 = qemu_opt_get(opts, "ipv6"), |
| 3804 | .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), |
| 3805 | }; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3806 | udp->remote = addr; |
Peter Maydell | 90a14bf | 2014-09-02 11:24:15 +0100 | [diff] [blame] | 3807 | |
| 3808 | if (has_local) { |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3809 | udp->has_local = true; |
Peter Maydell | 90a14bf | 2014-09-02 11:24:15 +0100 | [diff] [blame] | 3810 | addr = g_new0(SocketAddress, 1); |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 3811 | addr->type = SOCKET_ADDRESS_KIND_INET; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3812 | addr->u.inet.data = g_new(InetSocketAddress, 1); |
| 3813 | *addr->u.inet.data = (InetSocketAddress) { |
Eric Blake | 0399293 | 2016-03-03 09:16:48 -0700 | [diff] [blame] | 3814 | .host = g_strdup(localaddr), |
| 3815 | .port = g_strdup(localport), |
| 3816 | }; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 3817 | udp->local = addr; |
Peter Maydell | 90a14bf | 2014-09-02 11:24:15 +0100 | [diff] [blame] | 3818 | } |
| 3819 | } |
| 3820 | |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3821 | typedef struct CharDriver { |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3822 | const char *name; |
Gerd Hoffmann | 99aec01 | 2013-06-24 08:39:52 +0200 | [diff] [blame] | 3823 | ChardevBackendKind kind; |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3824 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 3825 | CharDriverState *(*create)(const char *id, ChardevBackend *backend, |
| 3826 | ChardevReturn *ret, Error **errp); |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3827 | } CharDriver; |
| 3828 | |
| 3829 | static GSList *backends; |
| 3830 | |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 3831 | void register_char_driver(const char *name, ChardevBackendKind kind, |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 3832 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp), |
| 3833 | CharDriverState *(*create)(const char *id, ChardevBackend *backend, |
| 3834 | ChardevReturn *ret, Error **errp)) |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3835 | { |
| 3836 | CharDriver *s; |
| 3837 | |
| 3838 | s = g_malloc0(sizeof(*s)); |
| 3839 | s->name = g_strdup(name); |
| 3840 | s->kind = kind; |
| 3841 | s->parse = parse; |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 3842 | s->create = create; |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 3843 | |
| 3844 | backends = g_slist_append(backends, s); |
| 3845 | } |
| 3846 | |
Anthony Liguori | f69554b | 2011-08-15 11:17:37 -0500 | [diff] [blame] | 3847 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3848 | void (*init)(struct CharDriverState *s), |
| 3849 | Error **errp) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3850 | { |
Markus Armbruster | 0aff637 | 2014-05-19 18:57:35 +0200 | [diff] [blame] | 3851 | Error *local_err = NULL; |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3852 | CharDriver *cd; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3853 | CharDriverState *chr; |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3854 | GSList *i; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3855 | ChardevReturn *ret = NULL; |
| 3856 | ChardevBackend *backend; |
| 3857 | const char *id = qemu_opts_id(opts); |
| 3858 | char *bid = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3859 | |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3860 | if (id == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3861 | error_setg(errp, "chardev: no id specified"); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3862 | goto err; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3863 | } |
| 3864 | |
Stefan Hajnoczi | 1bbd185 | 2011-01-22 13:07:26 +0000 | [diff] [blame] | 3865 | if (qemu_opt_get(opts, "backend") == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3866 | error_setg(errp, "chardev: \"%s\" missing backend", |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3867 | qemu_opts_id(opts)); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3868 | goto err; |
Stefan Hajnoczi | 1bbd185 | 2011-01-22 13:07:26 +0000 | [diff] [blame] | 3869 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3870 | for (i = backends; i; i = i->next) { |
| 3871 | cd = i->data; |
| 3872 | |
| 3873 | if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) { |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3874 | break; |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3875 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3876 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 3877 | if (i == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 3878 | error_setg(errp, "chardev: backend \"%s\" not found", |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3879 | qemu_opt_get(opts, "backend")); |
Gerd Hoffmann | e668287 | 2013-06-24 08:39:51 +0200 | [diff] [blame] | 3880 | goto err; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3883 | backend = g_new0(ChardevBackend, 1); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3884 | |
| 3885 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3886 | bid = g_strdup_printf("%s-base", id); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 3887 | } |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3888 | |
| 3889 | chr = NULL; |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 3890 | backend->type = cd->kind; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3891 | if (cd->parse) { |
| 3892 | cd->parse(opts, backend, &local_err); |
| 3893 | if (local_err) { |
| 3894 | error_propagate(errp, local_err); |
| 3895 | goto qapi_out; |
| 3896 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 3897 | } else { |
| 3898 | ChardevCommon *cc = g_new0(ChardevCommon, 1); |
| 3899 | qemu_chr_parse_common(opts, cc); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3900 | backend->u.null.data = cc; /* Any ChardevCommon member would work */ |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3901 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 3902 | |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3903 | ret = qmp_chardev_add(bid ? bid : id, backend, errp); |
| 3904 | if (!ret) { |
| 3905 | goto qapi_out; |
| 3906 | } |
| 3907 | |
| 3908 | if (bid) { |
| 3909 | qapi_free_ChardevBackend(backend); |
| 3910 | qapi_free_ChardevReturn(ret); |
| 3911 | backend = g_new0(ChardevBackend, 1); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3912 | backend->u.mux.data = g_new0(ChardevMux, 1); |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 3913 | backend->type = CHARDEV_BACKEND_KIND_MUX; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 3914 | backend->u.mux.data->chardev = g_strdup(bid); |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3915 | ret = qmp_chardev_add(id, backend, errp); |
| 3916 | if (!ret) { |
| 3917 | chr = qemu_chr_find(bid); |
| 3918 | qemu_chr_delete(chr); |
| 3919 | chr = NULL; |
| 3920 | goto qapi_out; |
| 3921 | } |
| 3922 | } |
| 3923 | |
| 3924 | chr = qemu_chr_find(id); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3925 | chr->opts = opts; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 3926 | |
| 3927 | qapi_out: |
| 3928 | qapi_free_ChardevBackend(backend); |
| 3929 | qapi_free_ChardevReturn(ret); |
| 3930 | g_free(bid); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3931 | return chr; |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 3932 | |
| 3933 | err: |
| 3934 | qemu_opts_del(opts); |
| 3935 | return NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3936 | } |
| 3937 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 3938 | CharDriverState *qemu_chr_new_noreplay(const char *label, const char *filename, |
| 3939 | void (*init)(struct CharDriverState *s)) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3940 | { |
| 3941 | const char *p; |
| 3942 | CharDriverState *chr; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3943 | QemuOpts *opts; |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3944 | Error *err = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3945 | |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 3946 | if (strstart(filename, "chardev:", &p)) { |
| 3947 | return qemu_chr_find(p); |
| 3948 | } |
| 3949 | |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 3950 | opts = qemu_chr_parse_compat(label, filename); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3951 | if (!opts) |
| 3952 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3953 | |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3954 | chr = qemu_chr_new_from_opts(opts, init, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3955 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 3956 | error_report_err(err); |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 3957 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3958 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
Hans de Goede | 456d606 | 2013-03-27 20:29:40 +0100 | [diff] [blame] | 3959 | qemu_chr_fe_claim_no_fail(chr); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 3960 | monitor_init(chr, MONITOR_USE_READLINE); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 3961 | } |
| 3962 | return chr; |
| 3963 | } |
| 3964 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 3965 | CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
| 3966 | { |
| 3967 | CharDriverState *chr; |
| 3968 | chr = qemu_chr_new_noreplay(label, filename, init); |
| 3969 | if (chr) { |
| 3970 | chr->replay = replay_mode != REPLAY_MODE_NONE; |
| 3971 | if (chr->replay && chr->chr_ioctl) { |
| 3972 | fprintf(stderr, |
| 3973 | "Replay: ioctl is not supported for serial devices yet\n"); |
| 3974 | } |
| 3975 | replay_register_char_driver(chr); |
| 3976 | } |
| 3977 | return chr; |
| 3978 | } |
| 3979 | |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 3980 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
Paolo Bonzini | c48855e | 2010-12-23 13:42:48 +0100 | [diff] [blame] | 3981 | { |
| 3982 | if (chr->chr_set_echo) { |
| 3983 | chr->chr_set_echo(chr, echo); |
| 3984 | } |
| 3985 | } |
| 3986 | |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 3987 | void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open) |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 3988 | { |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 3989 | if (chr->fe_open == fe_open) { |
Hans de Goede | c0c4bd2 | 2013-03-26 11:07:55 +0100 | [diff] [blame] | 3990 | return; |
| 3991 | } |
Hans de Goede | 8e25daa | 2013-03-26 11:07:57 +0100 | [diff] [blame] | 3992 | chr->fe_open = fe_open; |
Hans de Goede | 574b711 | 2013-03-26 11:07:58 +0100 | [diff] [blame] | 3993 | if (chr->chr_set_fe_open) { |
| 3994 | chr->chr_set_fe_open(chr, fe_open); |
Hans de Goede | 7c32c4f | 2011-03-24 11:12:02 +0100 | [diff] [blame] | 3995 | } |
| 3996 | } |
| 3997 | |
Marc-André Lureau | d61b0c9 | 2013-12-01 22:23:39 +0100 | [diff] [blame] | 3998 | void qemu_chr_fe_event(struct CharDriverState *chr, int event) |
| 3999 | { |
| 4000 | if (chr->chr_fe_event) { |
| 4001 | chr->chr_fe_event(chr, event); |
| 4002 | } |
| 4003 | } |
| 4004 | |
Paolo Bonzini | 6f1de6b | 2016-06-20 15:02:40 +0200 | [diff] [blame] | 4005 | guint qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond, |
| 4006 | GIOFunc func, void *user_data) |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 4007 | { |
| 4008 | GSource *src; |
| 4009 | guint tag; |
| 4010 | |
| 4011 | if (s->chr_add_watch == NULL) { |
Paolo Bonzini | 6f1de6b | 2016-06-20 15:02:40 +0200 | [diff] [blame] | 4012 | return 0; |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | src = s->chr_add_watch(s, cond); |
Paolo Bonzini | 62c339c | 2014-07-24 16:08:04 +0200 | [diff] [blame] | 4016 | if (!src) { |
Paolo Bonzini | 6f1de6b | 2016-06-20 15:02:40 +0200 | [diff] [blame] | 4017 | return 0; |
Paolo Bonzini | 62c339c | 2014-07-24 16:08:04 +0200 | [diff] [blame] | 4018 | } |
| 4019 | |
Anthony Liguori | 23673ca | 2013-03-05 23:21:23 +0530 | [diff] [blame] | 4020 | g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); |
| 4021 | tag = g_source_attach(src, NULL); |
| 4022 | g_source_unref(src); |
| 4023 | |
| 4024 | return tag; |
| 4025 | } |
| 4026 | |
Hans de Goede | 44c473d | 2013-03-27 20:29:39 +0100 | [diff] [blame] | 4027 | int qemu_chr_fe_claim(CharDriverState *s) |
| 4028 | { |
| 4029 | if (s->avail_connections < 1) { |
| 4030 | return -1; |
| 4031 | } |
| 4032 | s->avail_connections--; |
| 4033 | return 0; |
| 4034 | } |
| 4035 | |
| 4036 | void qemu_chr_fe_claim_no_fail(CharDriverState *s) |
| 4037 | { |
| 4038 | if (qemu_chr_fe_claim(s) != 0) { |
| 4039 | fprintf(stderr, "%s: error chardev \"%s\" already used\n", |
| 4040 | __func__, s->label); |
| 4041 | exit(1); |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | void qemu_chr_fe_release(CharDriverState *s) |
| 4046 | { |
| 4047 | s->avail_connections++; |
| 4048 | } |
| 4049 | |
Tetsuya Mukawa | 7d9d17f | 2016-06-06 18:45:02 +0200 | [diff] [blame] | 4050 | void qemu_chr_disconnect(CharDriverState *chr) |
| 4051 | { |
| 4052 | if (chr->chr_disconnect) { |
| 4053 | chr->chr_disconnect(chr); |
| 4054 | } |
| 4055 | } |
| 4056 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4057 | static void qemu_chr_free_common(CharDriverState *chr) |
| 4058 | { |
| 4059 | g_free(chr->filename); |
| 4060 | g_free(chr->label); |
| 4061 | qemu_opts_del(chr->opts); |
| 4062 | if (chr->logfd != -1) { |
| 4063 | close(chr->logfd); |
| 4064 | } |
Paolo Bonzini | fefd749 | 2016-01-15 16:16:25 +0100 | [diff] [blame] | 4065 | qemu_mutex_destroy(&chr->chr_write_lock); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4066 | g_free(chr); |
| 4067 | } |
| 4068 | |
Marc-André Lureau | 1ad78ea | 2015-06-22 18:20:18 +0200 | [diff] [blame] | 4069 | void qemu_chr_free(CharDriverState *chr) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4070 | { |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 4071 | if (chr->chr_close) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4072 | chr->chr_close(chr); |
Gerd Hoffmann | 2274ae9 | 2012-10-15 09:30:59 +0200 | [diff] [blame] | 4073 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4074 | qemu_chr_free_common(chr); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Marc-André Lureau | 1ad78ea | 2015-06-22 18:20:18 +0200 | [diff] [blame] | 4077 | void qemu_chr_delete(CharDriverState *chr) |
| 4078 | { |
| 4079 | QTAILQ_REMOVE(&chardevs, chr, next); |
| 4080 | qemu_chr_free(chr); |
| 4081 | } |
| 4082 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 4083 | ChardevInfoList *qmp_query_chardev(Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4084 | { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 4085 | ChardevInfoList *chr_list = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4086 | CharDriverState *chr; |
| 4087 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4088 | QTAILQ_FOREACH(chr, &chardevs, next) { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 4089 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
| 4090 | info->value = g_malloc0(sizeof(*info->value)); |
| 4091 | info->value->label = g_strdup(chr->label); |
| 4092 | info->value->filename = g_strdup(chr->filename); |
Laszlo Ersek | 32a97ea | 2014-06-26 17:50:03 +0200 | [diff] [blame] | 4093 | info->value->frontend_open = chr->fe_open; |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 4094 | |
| 4095 | info->next = chr_list; |
| 4096 | chr_list = info; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4097 | } |
Luiz Capitulino | 588b383 | 2009-12-10 17:16:08 -0200 | [diff] [blame] | 4098 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 4099 | return chr_list; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 4100 | } |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 4101 | |
Martin Kletzander | 77d1c3c | 2014-02-01 12:52:42 +0100 | [diff] [blame] | 4102 | ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) |
| 4103 | { |
| 4104 | ChardevBackendInfoList *backend_list = NULL; |
| 4105 | CharDriver *c = NULL; |
| 4106 | GSList *i = NULL; |
| 4107 | |
| 4108 | for (i = backends; i; i = i->next) { |
| 4109 | ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); |
| 4110 | c = i->data; |
| 4111 | info->value = g_malloc0(sizeof(*info->value)); |
| 4112 | info->value->name = g_strdup(c->name); |
| 4113 | |
| 4114 | info->next = backend_list; |
| 4115 | backend_list = info; |
| 4116 | } |
| 4117 | |
| 4118 | return backend_list; |
| 4119 | } |
| 4120 | |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 4121 | CharDriverState *qemu_chr_find(const char *name) |
| 4122 | { |
| 4123 | CharDriverState *chr; |
| 4124 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4125 | QTAILQ_FOREACH(chr, &chardevs, next) { |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 4126 | if (strcmp(chr->label, name) != 0) |
| 4127 | continue; |
| 4128 | return chr; |
| 4129 | } |
| 4130 | return NULL; |
| 4131 | } |
Anthony Liguori | 0beb494 | 2011-12-22 15:29:25 -0600 | [diff] [blame] | 4132 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4133 | QemuOptsList qemu_chardev_opts = { |
| 4134 | .name = "chardev", |
| 4135 | .implied_opt_name = "backend", |
| 4136 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), |
| 4137 | .desc = { |
| 4138 | { |
| 4139 | .name = "backend", |
| 4140 | .type = QEMU_OPT_STRING, |
| 4141 | },{ |
| 4142 | .name = "path", |
| 4143 | .type = QEMU_OPT_STRING, |
| 4144 | },{ |
| 4145 | .name = "host", |
| 4146 | .type = QEMU_OPT_STRING, |
| 4147 | },{ |
| 4148 | .name = "port", |
| 4149 | .type = QEMU_OPT_STRING, |
| 4150 | },{ |
| 4151 | .name = "localaddr", |
| 4152 | .type = QEMU_OPT_STRING, |
| 4153 | },{ |
| 4154 | .name = "localport", |
| 4155 | .type = QEMU_OPT_STRING, |
| 4156 | },{ |
| 4157 | .name = "to", |
| 4158 | .type = QEMU_OPT_NUMBER, |
| 4159 | },{ |
| 4160 | .name = "ipv4", |
| 4161 | .type = QEMU_OPT_BOOL, |
| 4162 | },{ |
| 4163 | .name = "ipv6", |
| 4164 | .type = QEMU_OPT_BOOL, |
| 4165 | },{ |
| 4166 | .name = "wait", |
| 4167 | .type = QEMU_OPT_BOOL, |
| 4168 | },{ |
| 4169 | .name = "server", |
| 4170 | .type = QEMU_OPT_BOOL, |
| 4171 | },{ |
| 4172 | .name = "delay", |
| 4173 | .type = QEMU_OPT_BOOL, |
| 4174 | },{ |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 4175 | .name = "reconnect", |
| 4176 | .type = QEMU_OPT_NUMBER, |
| 4177 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4178 | .name = "telnet", |
| 4179 | .type = QEMU_OPT_BOOL, |
| 4180 | },{ |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 4181 | .name = "tls-creds", |
| 4182 | .type = QEMU_OPT_STRING, |
| 4183 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4184 | .name = "width", |
| 4185 | .type = QEMU_OPT_NUMBER, |
| 4186 | },{ |
| 4187 | .name = "height", |
| 4188 | .type = QEMU_OPT_NUMBER, |
| 4189 | },{ |
| 4190 | .name = "cols", |
| 4191 | .type = QEMU_OPT_NUMBER, |
| 4192 | },{ |
| 4193 | .name = "rows", |
| 4194 | .type = QEMU_OPT_NUMBER, |
| 4195 | },{ |
| 4196 | .name = "mux", |
| 4197 | .type = QEMU_OPT_BOOL, |
| 4198 | },{ |
| 4199 | .name = "signal", |
| 4200 | .type = QEMU_OPT_BOOL, |
| 4201 | },{ |
| 4202 | .name = "name", |
| 4203 | .type = QEMU_OPT_STRING, |
| 4204 | },{ |
| 4205 | .name = "debug", |
| 4206 | .type = QEMU_OPT_NUMBER, |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 4207 | },{ |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 4208 | .name = "size", |
Markus Armbruster | de1cc36 | 2013-02-06 21:27:25 +0100 | [diff] [blame] | 4209 | .type = QEMU_OPT_SIZE, |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 4210 | },{ |
| 4211 | .name = "chardev", |
| 4212 | .type = QEMU_OPT_STRING, |
Olga Krishtal | 31e38a2 | 2015-12-04 09:42:04 +0300 | [diff] [blame] | 4213 | },{ |
| 4214 | .name = "append", |
| 4215 | .type = QEMU_OPT_BOOL, |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4216 | },{ |
| 4217 | .name = "logfile", |
| 4218 | .type = QEMU_OPT_STRING, |
| 4219 | },{ |
| 4220 | .name = "logappend", |
| 4221 | .type = QEMU_OPT_BOOL, |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4222 | }, |
| 4223 | { /* end of list */ } |
| 4224 | }, |
| 4225 | }; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4226 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4227 | #ifdef _WIN32 |
| 4228 | |
Paolo Bonzini | fd5b036 | 2015-09-29 15:06:02 +0200 | [diff] [blame] | 4229 | static CharDriverState *qmp_chardev_open_file(const char *id, |
| 4230 | ChardevBackend *backend, |
| 4231 | ChardevReturn *ret, |
| 4232 | Error **errp) |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4233 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4234 | ChardevFile *file = backend->u.file.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4235 | ChardevCommon *common = qapi_ChardevFile_base(file); |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4236 | HANDLE out; |
| 4237 | |
Gerd Hoffmann | e859eda | 2013-06-24 08:39:47 +0200 | [diff] [blame] | 4238 | if (file->has_in) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4239 | error_setg(errp, "input file not supported"); |
| 4240 | return NULL; |
| 4241 | } |
| 4242 | |
| 4243 | out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 4244 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 4245 | if (out == INVALID_HANDLE_VALUE) { |
| 4246 | error_setg(errp, "open %s failed", file->out); |
| 4247 | return NULL; |
| 4248 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4249 | return qemu_chr_open_win_file(out, common, errp); |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 4252 | static CharDriverState *qmp_chardev_open_serial(const char *id, |
| 4253 | ChardevBackend *backend, |
| 4254 | ChardevReturn *ret, |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4255 | Error **errp) |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 4256 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4257 | ChardevHostdev *serial = backend->u.serial.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4258 | ChardevCommon *common = qapi_ChardevHostdev_base(serial); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4259 | return qemu_chr_open_win_path(serial->device, common, errp); |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4260 | } |
| 4261 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4262 | #else /* WIN32 */ |
| 4263 | |
| 4264 | static int qmp_chardev_open_file_source(char *src, int flags, |
| 4265 | Error **errp) |
| 4266 | { |
| 4267 | int fd = -1; |
| 4268 | |
| 4269 | TFR(fd = qemu_open(src, flags, 0666)); |
| 4270 | if (fd == -1) { |
Gerd Hoffmann | 20c3976 | 2013-06-24 08:39:48 +0200 | [diff] [blame] | 4271 | error_setg_file_open(errp, errno, src); |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4272 | } |
| 4273 | return fd; |
| 4274 | } |
| 4275 | |
Paolo Bonzini | fd5b036 | 2015-09-29 15:06:02 +0200 | [diff] [blame] | 4276 | static CharDriverState *qmp_chardev_open_file(const char *id, |
| 4277 | ChardevBackend *backend, |
| 4278 | ChardevReturn *ret, |
| 4279 | Error **errp) |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4280 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4281 | ChardevFile *file = backend->u.file.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4282 | ChardevCommon *common = qapi_ChardevFile_base(file); |
Markus Armbruster | 5f75836 | 2014-05-19 18:57:34 +0200 | [diff] [blame] | 4283 | int flags, in = -1, out; |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4284 | |
Olga Krishtal | 31e38a2 | 2015-12-04 09:42:04 +0300 | [diff] [blame] | 4285 | flags = O_WRONLY | O_CREAT | O_BINARY; |
| 4286 | if (file->has_append && file->append) { |
| 4287 | flags |= O_APPEND; |
| 4288 | } else { |
| 4289 | flags |= O_TRUNC; |
| 4290 | } |
| 4291 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4292 | out = qmp_chardev_open_file_source(file->out, flags, errp); |
Markus Armbruster | 5f75836 | 2014-05-19 18:57:34 +0200 | [diff] [blame] | 4293 | if (out < 0) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4294 | return NULL; |
| 4295 | } |
| 4296 | |
Gerd Hoffmann | e859eda | 2013-06-24 08:39:47 +0200 | [diff] [blame] | 4297 | if (file->has_in) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4298 | flags = O_RDONLY; |
| 4299 | in = qmp_chardev_open_file_source(file->in, flags, errp); |
Markus Armbruster | 5f75836 | 2014-05-19 18:57:34 +0200 | [diff] [blame] | 4300 | if (in < 0) { |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4301 | qemu_close(out); |
| 4302 | return NULL; |
| 4303 | } |
| 4304 | } |
| 4305 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4306 | return qemu_chr_open_fd(in, out, common, errp); |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4307 | } |
| 4308 | |
Paolo Bonzini | d809ab9 | 2015-10-12 09:46:23 +0200 | [diff] [blame] | 4309 | #ifdef HAVE_CHARDEV_SERIAL |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 4310 | static CharDriverState *qmp_chardev_open_serial(const char *id, |
| 4311 | ChardevBackend *backend, |
| 4312 | ChardevReturn *ret, |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4313 | Error **errp) |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 4314 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4315 | ChardevHostdev *serial = backend->u.serial.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4316 | ChardevCommon *common = qapi_ChardevHostdev_base(serial); |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4317 | int fd; |
| 4318 | |
| 4319 | fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); |
Markus Armbruster | 5f75836 | 2014-05-19 18:57:34 +0200 | [diff] [blame] | 4320 | if (fd < 0) { |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 4321 | return NULL; |
| 4322 | } |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 4323 | qemu_set_nonblock(fd); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4324 | return qemu_chr_open_tty_fd(fd, common, errp); |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4325 | } |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 4326 | #endif |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4327 | |
Paolo Bonzini | d809ab9 | 2015-10-12 09:46:23 +0200 | [diff] [blame] | 4328 | #ifdef HAVE_CHARDEV_PARPORT |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 4329 | static CharDriverState *qmp_chardev_open_parallel(const char *id, |
| 4330 | ChardevBackend *backend, |
| 4331 | ChardevReturn *ret, |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4332 | Error **errp) |
| 4333 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4334 | ChardevHostdev *parallel = backend->u.parallel.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4335 | ChardevCommon *common = qapi_ChardevHostdev_base(parallel); |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4336 | int fd; |
| 4337 | |
| 4338 | fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); |
Markus Armbruster | 5f75836 | 2014-05-19 18:57:34 +0200 | [diff] [blame] | 4339 | if (fd < 0) { |
Markus Armbruster | d36b2b9 | 2013-02-13 15:54:16 +0100 | [diff] [blame] | 4340 | return NULL; |
| 4341 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4342 | return qemu_chr_open_pp_fd(fd, common, errp); |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 4343 | } |
Paolo Bonzini | d809ab9 | 2015-10-12 09:46:23 +0200 | [diff] [blame] | 4344 | #endif |
Gerd Hoffmann | d59044e | 2012-12-19 13:50:29 +0100 | [diff] [blame] | 4345 | |
Gerd Hoffmann | ffbdbe5 | 2012-12-19 13:13:57 +0100 | [diff] [blame] | 4346 | #endif /* WIN32 */ |
| 4347 | |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 4348 | static gboolean socket_reconnect_timeout(gpointer opaque) |
| 4349 | { |
| 4350 | CharDriverState *chr = opaque; |
| 4351 | TCPCharDriver *s = chr->opaque; |
Daniel P. Berrange | 317856c | 2016-03-09 16:45:04 +0000 | [diff] [blame] | 4352 | QIOChannelSocket *sioc; |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 4353 | |
| 4354 | s->reconnect_timer = 0; |
| 4355 | |
| 4356 | if (chr->be_open) { |
| 4357 | return false; |
| 4358 | } |
| 4359 | |
Daniel P. Berrange | 317856c | 2016-03-09 16:45:04 +0000 | [diff] [blame] | 4360 | sioc = qio_channel_socket_new(); |
| 4361 | qio_channel_socket_connect_async(sioc, s->addr, |
| 4362 | qemu_chr_socket_connected, |
| 4363 | chr, NULL); |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 4364 | |
| 4365 | return false; |
| 4366 | } |
| 4367 | |
Paolo Bonzini | dbba8d1 | 2015-09-29 15:15:53 +0200 | [diff] [blame] | 4368 | static CharDriverState *qmp_chardev_open_socket(const char *id, |
| 4369 | ChardevBackend *backend, |
| 4370 | ChardevReturn *ret, |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 4371 | Error **errp) |
| 4372 | { |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4373 | CharDriverState *chr; |
| 4374 | TCPCharDriver *s; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4375 | ChardevSocket *sock = backend->u.socket.data; |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 4376 | SocketAddress *addr = sock->addr; |
| 4377 | bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; |
| 4378 | bool is_listen = sock->has_server ? sock->server : true; |
| 4379 | bool is_telnet = sock->has_telnet ? sock->telnet : false; |
| 4380 | bool is_waitconnect = sock->has_wait ? sock->wait : false; |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 4381 | int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4382 | ChardevCommon *common = qapi_ChardevSocket_base(sock); |
Daniel P. Berrange | 317856c | 2016-03-09 16:45:04 +0000 | [diff] [blame] | 4383 | QIOChannelSocket *sioc = NULL; |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4384 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 4385 | chr = qemu_chr_alloc(common, errp); |
| 4386 | if (!chr) { |
| 4387 | return NULL; |
| 4388 | } |
Markus Armbruster | 2d528d4 | 2015-09-14 13:54:03 +0200 | [diff] [blame] | 4389 | s = g_new0(TCPCharDriver, 1); |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4390 | |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 4391 | s->is_unix = addr->type == SOCKET_ADDRESS_KIND_UNIX; |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 4392 | s->is_listen = is_listen; |
| 4393 | s->is_telnet = is_telnet; |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4394 | s->do_nodelay = do_nodelay; |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 4395 | if (sock->tls_creds) { |
| 4396 | Object *creds; |
| 4397 | creds = object_resolve_path_component( |
| 4398 | object_get_objects_root(), sock->tls_creds); |
| 4399 | if (!creds) { |
| 4400 | error_setg(errp, "No TLS credentials with id '%s'", |
| 4401 | sock->tls_creds); |
| 4402 | goto error; |
| 4403 | } |
| 4404 | s->tls_creds = (QCryptoTLSCreds *) |
| 4405 | object_dynamic_cast(creds, |
| 4406 | TYPE_QCRYPTO_TLS_CREDS); |
| 4407 | if (!s->tls_creds) { |
| 4408 | error_setg(errp, "Object with id '%s' is not TLS credentials", |
| 4409 | sock->tls_creds); |
| 4410 | goto error; |
| 4411 | } |
| 4412 | object_ref(OBJECT(s->tls_creds)); |
| 4413 | if (is_listen) { |
| 4414 | if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { |
| 4415 | error_setg(errp, "%s", |
| 4416 | "Expected TLS credentials for server endpoint"); |
| 4417 | goto error; |
| 4418 | } |
| 4419 | } else { |
| 4420 | if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { |
| 4421 | error_setg(errp, "%s", |
| 4422 | "Expected TLS credentials for client endpoint"); |
| 4423 | goto error; |
| 4424 | } |
| 4425 | } |
| 4426 | } |
| 4427 | |
Eric Blake | 37f9e0a | 2016-06-09 10:48:45 -0600 | [diff] [blame] | 4428 | s->addr = QAPI_CLONE(SocketAddress, sock->addr); |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4429 | |
| 4430 | chr->opaque = s; |
Marc-André Lureau | d7a04fd | 2016-07-27 01:15:18 +0400 | [diff] [blame] | 4431 | chr->chr_wait_connected = tcp_chr_wait_connected; |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4432 | chr->chr_write = tcp_chr_write; |
| 4433 | chr->chr_sync_read = tcp_chr_sync_read; |
| 4434 | chr->chr_close = tcp_chr_close; |
Tetsuya Mukawa | 7d9d17f | 2016-06-06 18:45:02 +0200 | [diff] [blame] | 4435 | chr->chr_disconnect = tcp_chr_disconnect; |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4436 | chr->get_msgfds = tcp_get_msgfds; |
| 4437 | chr->set_msgfds = tcp_set_msgfds; |
| 4438 | chr->chr_add_client = tcp_chr_add_client; |
| 4439 | chr->chr_add_watch = tcp_chr_add_watch; |
| 4440 | chr->chr_update_read_handler = tcp_chr_update_read_handler; |
| 4441 | /* be isn't opened until we get a connection */ |
| 4442 | chr->explicit_be_open = true; |
| 4443 | |
Daniel P. Berrange | 0ff0fad | 2016-01-19 11:14:28 +0000 | [diff] [blame] | 4444 | chr->filename = SocketAddress_to_str("disconnected:", |
| 4445 | addr, is_listen, is_telnet); |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 4446 | |
| 4447 | if (is_listen) { |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4448 | if (is_telnet) { |
| 4449 | s->do_telnetopt = 1; |
| 4450 | } |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 4451 | } else if (reconnect > 0) { |
| 4452 | s->reconnect_time = reconnect; |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 4453 | } |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4454 | |
Corey Minyard | 5008e5b | 2014-10-08 07:11:55 -0500 | [diff] [blame] | 4455 | if (s->reconnect_time) { |
Marc-André Lureau | d7a04fd | 2016-07-27 01:15:18 +0400 | [diff] [blame] | 4456 | sioc = qio_channel_socket_new(); |
Daniel P. Berrange | 317856c | 2016-03-09 16:45:04 +0000 | [diff] [blame] | 4457 | qio_channel_socket_connect_async(sioc, s->addr, |
| 4458 | qemu_chr_socket_connected, |
| 4459 | chr, NULL); |
Daniel P. Berrange | 08b758b | 2016-03-09 16:49:56 +0000 | [diff] [blame] | 4460 | } else { |
Marc-André Lureau | d7a04fd | 2016-07-27 01:15:18 +0400 | [diff] [blame] | 4461 | if (s->is_listen) { |
| 4462 | sioc = qio_channel_socket_new(); |
| 4463 | if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { |
| 4464 | goto error; |
| 4465 | } |
| 4466 | s->listen_ioc = sioc; |
| 4467 | if (is_waitconnect && |
| 4468 | qemu_chr_wait_connected(chr, errp) < 0) { |
| 4469 | goto error; |
| 4470 | } |
| 4471 | if (!s->ioc) { |
| 4472 | s->listen_tag = qio_channel_add_watch( |
| 4473 | QIO_CHANNEL(s->listen_ioc), G_IO_IN, |
| 4474 | tcp_chr_accept, chr, NULL); |
| 4475 | } |
| 4476 | } else if (qemu_chr_wait_connected(chr, errp) < 0) { |
Daniel P. Berrange | 08b758b | 2016-03-09 16:49:56 +0000 | [diff] [blame] | 4477 | goto error; |
| 4478 | } |
Corey Minyard | 43ded1a | 2014-10-02 11:17:34 -0500 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | return chr; |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 4482 | |
| 4483 | error: |
Daniel P. Berrange | 08b758b | 2016-03-09 16:49:56 +0000 | [diff] [blame] | 4484 | if (sioc) { |
| 4485 | object_unref(OBJECT(sioc)); |
| 4486 | } |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 4487 | if (s->tls_creds) { |
| 4488 | object_unref(OBJECT(s->tls_creds)); |
| 4489 | } |
| 4490 | g_free(s); |
| 4491 | qemu_chr_free_common(chr); |
| 4492 | return NULL; |
Gerd Hoffmann | f6bd5d6 | 2012-12-20 13:53:12 +0100 | [diff] [blame] | 4493 | } |
| 4494 | |
Paolo Bonzini | e79b80d | 2015-09-29 15:17:20 +0200 | [diff] [blame] | 4495 | static CharDriverState *qmp_chardev_open_udp(const char *id, |
| 4496 | ChardevBackend *backend, |
| 4497 | ChardevReturn *ret, |
Lei Li | 08d0ab3 | 2013-05-20 14:51:03 +0800 | [diff] [blame] | 4498 | Error **errp) |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 4499 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 4500 | ChardevUdp *udp = backend->u.udp.data; |
Eric Blake | f194a1a | 2016-03-03 09:16:47 -0700 | [diff] [blame] | 4501 | ChardevCommon *common = qapi_ChardevUdp_base(udp); |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 4502 | QIOChannelSocket *sioc = qio_channel_socket_new(); |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 4503 | |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 4504 | if (qio_channel_socket_dgram_sync(sioc, |
Paolo Bonzini | 150dcd1 | 2016-02-09 11:59:15 +0100 | [diff] [blame] | 4505 | udp->local, udp->remote, |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 4506 | errp) < 0) { |
| 4507 | object_unref(OBJECT(sioc)); |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 4508 | return NULL; |
| 4509 | } |
Daniel P. Berrange | 9894dc0 | 2016-01-19 11:14:29 +0000 | [diff] [blame] | 4510 | return qemu_chr_open_udp(sioc, common, errp); |
Gerd Hoffmann | 3ecc059 | 2013-02-27 14:10:47 +0100 | [diff] [blame] | 4511 | } |
| 4512 | |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4513 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, |
| 4514 | Error **errp) |
| 4515 | { |
| 4516 | ChardevReturn *ret = g_new0(ChardevReturn, 1); |
Paolo Bonzini | 3c0e5a4 | 2015-09-29 15:27:24 +0200 | [diff] [blame] | 4517 | CharDriverState *chr = NULL; |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 4518 | Error *local_err = NULL; |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4519 | GSList *i; |
| 4520 | CharDriver *cd; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4521 | |
| 4522 | chr = qemu_chr_find(id); |
| 4523 | if (chr) { |
| 4524 | error_setg(errp, "Chardev '%s' already exists", id); |
| 4525 | g_free(ret); |
| 4526 | return NULL; |
| 4527 | } |
| 4528 | |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4529 | for (i = backends; i; i = i->next) { |
| 4530 | cd = i->data; |
| 4531 | |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 4532 | if (cd->kind == backend->type) { |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4533 | chr = cd->create(id, backend, ret, &local_err); |
| 4534 | if (local_err) { |
| 4535 | error_propagate(errp, local_err); |
| 4536 | goto out_error; |
| 4537 | } |
Gerd Hoffmann | edb2fb3 | 2013-02-21 16:16:42 +0100 | [diff] [blame] | 4538 | break; |
| 4539 | } |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4540 | } |
| 4541 | |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 4542 | if (chr == NULL) { |
Paolo Bonzini | 1c3af0f | 2015-10-12 09:51:41 +0200 | [diff] [blame] | 4543 | assert(!i); |
| 4544 | error_setg(errp, "chardev backend not available"); |
| 4545 | goto out_error; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4546 | } |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 4547 | |
| 4548 | chr->label = g_strdup(id); |
| 4549 | chr->avail_connections = |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 4550 | (backend->type == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1; |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 4551 | if (!chr->filename) { |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 4552 | chr->filename = g_strdup(ChardevBackendKind_lookup[backend->type]); |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 4553 | } |
| 4554 | if (!chr->explicit_be_open) { |
| 4555 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 4556 | } |
| 4557 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
| 4558 | return ret; |
| 4559 | |
| 4560 | out_error: |
| 4561 | g_free(ret); |
| 4562 | return NULL; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | void qmp_chardev_remove(const char *id, Error **errp) |
| 4566 | { |
| 4567 | CharDriverState *chr; |
| 4568 | |
| 4569 | chr = qemu_chr_find(id); |
Gonglei | 8108fd3 | 2014-08-11 21:00:55 +0800 | [diff] [blame] | 4570 | if (chr == NULL) { |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4571 | error_setg(errp, "Chardev '%s' not found", id); |
| 4572 | return; |
| 4573 | } |
| 4574 | if (chr->chr_can_read || chr->chr_read || |
| 4575 | chr->chr_event || chr->handler_opaque) { |
| 4576 | error_setg(errp, "Chardev '%s' is busy", id); |
| 4577 | return; |
| 4578 | } |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 4579 | if (chr->replay) { |
| 4580 | error_setg(errp, |
| 4581 | "Chardev '%s' cannot be unplugged in record/replay mode", id); |
| 4582 | return; |
| 4583 | } |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 4584 | qemu_chr_delete(chr); |
| 4585 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 4586 | |
Marc-André Lureau | aa5cb7f | 2016-07-04 17:38:23 +0200 | [diff] [blame] | 4587 | void qemu_chr_cleanup(void) |
Marc-André Lureau | c1111a2 | 2016-06-16 21:28:50 +0200 | [diff] [blame] | 4588 | { |
| 4589 | CharDriverState *chr, *tmp; |
| 4590 | |
| 4591 | QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) { |
| 4592 | qemu_chr_delete(chr); |
| 4593 | } |
| 4594 | } |
| 4595 | |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 4596 | static void register_types(void) |
| 4597 | { |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4598 | register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL, |
Paolo Bonzini | 0d64992 | 2015-09-29 15:24:29 +0200 | [diff] [blame] | 4599 | qemu_chr_open_null); |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4600 | register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET, |
Paolo Bonzini | dbba8d1 | 2015-09-29 15:15:53 +0200 | [diff] [blame] | 4601 | qemu_chr_parse_socket, qmp_chardev_open_socket); |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4602 | register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp, |
Paolo Bonzini | e79b80d | 2015-09-29 15:17:20 +0200 | [diff] [blame] | 4603 | qmp_chardev_open_udp); |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4604 | register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF, |
Paolo Bonzini | 479f09a | 2015-09-29 15:51:14 +0200 | [diff] [blame] | 4605 | qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf); |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4606 | register_char_driver("file", CHARDEV_BACKEND_KIND_FILE, |
Paolo Bonzini | fd5b036 | 2015-09-29 15:06:02 +0200 | [diff] [blame] | 4607 | qemu_chr_parse_file_out, qmp_chardev_open_file); |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4608 | register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO, |
Paolo Bonzini | 8c84b25 | 2015-09-29 15:40:28 +0200 | [diff] [blame] | 4609 | qemu_chr_parse_stdio, qemu_chr_open_stdio); |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 4610 | #if defined HAVE_CHARDEV_SERIAL |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4611 | register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL, |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 4612 | qemu_chr_parse_serial, qmp_chardev_open_serial); |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4613 | register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL, |
Paolo Bonzini | 6511d39 | 2015-09-29 15:08:05 +0200 | [diff] [blame] | 4614 | qemu_chr_parse_serial, qmp_chardev_open_serial); |
| 4615 | #endif |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 4616 | #ifdef HAVE_CHARDEV_PARPORT |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4617 | register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL, |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 4618 | qemu_chr_parse_parallel, qmp_chardev_open_parallel); |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4619 | register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL, |
Paolo Bonzini | 38bfb1a | 2015-10-12 09:49:28 +0200 | [diff] [blame] | 4620 | qemu_chr_parse_parallel, qmp_chardev_open_parallel); |
| 4621 | #endif |
Paolo Bonzini | c2e75a4 | 2015-09-29 15:23:42 +0200 | [diff] [blame] | 4622 | #ifdef HAVE_CHARDEV_PTY |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4623 | register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL, |
Paolo Bonzini | c2e75a4 | 2015-09-29 15:23:42 +0200 | [diff] [blame] | 4624 | qemu_chr_open_pty); |
| 4625 | #endif |
Paolo Bonzini | 122e5ed | 2015-09-29 15:42:04 +0200 | [diff] [blame] | 4626 | #ifdef _WIN32 |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4627 | register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL, |
Paolo Bonzini | 122e5ed | 2015-09-29 15:42:04 +0200 | [diff] [blame] | 4628 | qemu_chr_open_win_con); |
| 4629 | #endif |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4630 | register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE, |
Paolo Bonzini | 20cbe7a | 2015-09-29 15:14:07 +0200 | [diff] [blame] | 4631 | qemu_chr_parse_pipe, qemu_chr_open_pipe); |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 4632 | register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux, |
Paolo Bonzini | 3c0e5a4 | 2015-09-29 15:27:24 +0200 | [diff] [blame] | 4633 | qemu_chr_open_mux); |
Markus Armbruster | c11ed96 | 2013-07-26 16:44:33 +0200 | [diff] [blame] | 4634 | /* Bug-compatibility: */ |
Peter Maydell | e4d50d4 | 2014-09-02 11:24:17 +0100 | [diff] [blame] | 4635 | register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY, |
Paolo Bonzini | 479f09a | 2015-09-29 15:51:14 +0200 | [diff] [blame] | 4636 | qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf); |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 4637 | /* this must be done after machine init, since we register FEs with muxes |
| 4638 | * as part of realize functions like serial_isa_realizefn when -nographic |
| 4639 | * is specified |
| 4640 | */ |
| 4641 | qemu_add_machine_init_done_notifier(&muxes_realize_notify); |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | type_init(register_types); |