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