aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 25 | #include "qemu/cutils.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 26 | #include "monitor/monitor.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 27 | #include "sysemu/sysemu.h" |
Marc-André Lureau | 213dcb0 | 2016-12-12 20:22:24 +0300 | [diff] [blame] | 28 | #include "qemu/config-file.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 29 | #include "qemu/error-report.h" |
Marc-André Lureau | 8228e35 | 2017-01-26 17:19:46 +0400 | [diff] [blame] | 30 | #include "chardev/char.h" |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 31 | #include "qmp-commands.h" |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 32 | #include "qapi-visit.h" |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 33 | #include "sysemu/replay.h" |
Lin Ma | 517b3d4 | 2016-08-17 01:13:52 +0800 | [diff] [blame] | 34 | #include "qemu/help_option.h" |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 35 | |
Marc-André Lureau | 8228e35 | 2017-01-26 17:19:46 +0400 | [diff] [blame] | 36 | #include "chardev/char-mux.h" |
Marc-André Lureau | df85a78 | 2016-12-12 15:06:14 +0300 | [diff] [blame] | 37 | |
Corey Minyard | cfb429c | 2014-10-02 11:17:35 -0500 | [diff] [blame] | 38 | /***********************************************************/ |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 39 | /* character device */ |
| 40 | |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 41 | static Object *get_chardevs_root(void) |
| 42 | { |
| 43 | return container_get(object_get_root(), "/chardevs"); |
| 44 | } |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 45 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 46 | void qemu_chr_be_event(Chardev *s, int event) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 47 | { |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 48 | CharBackend *be = s->be; |
| 49 | |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 50 | /* Keep track if the char device is open */ |
| 51 | switch (event) { |
| 52 | case CHR_EVENT_OPENED: |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 53 | s->be_open = 1; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 54 | break; |
| 55 | case CHR_EVENT_CLOSED: |
Hans de Goede | 16665b9 | 2013-03-26 11:07:53 +0100 | [diff] [blame] | 56 | s->be_open = 0; |
Alexander Graf | 73cdf3f | 2010-04-01 18:42:39 +0200 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 60 | if (!be || !be->chr_event) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 61 | return; |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | be->chr_event(be->opaque, event); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 67 | /* Not reporting errors from writing to logfile, as logs are |
| 68 | * defined to be "best effort" only */ |
Marc-André Lureau | a9b1ca3 | 2017-01-26 23:38:22 +0400 | [diff] [blame] | 69 | static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len) |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 70 | { |
| 71 | size_t done = 0; |
| 72 | ssize_t ret; |
| 73 | |
| 74 | if (s->logfd < 0) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | while (done < len) { |
Daniel P. Berrange | 53628ef | 2016-03-31 16:29:27 +0100 | [diff] [blame] | 79 | retry: |
| 80 | ret = write(s->logfd, buf + done, len - done); |
| 81 | if (ret == -1 && errno == EAGAIN) { |
| 82 | g_usleep(100); |
| 83 | goto retry; |
| 84 | } |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 85 | |
| 86 | if (ret <= 0) { |
| 87 | return; |
| 88 | } |
| 89 | done += ret; |
| 90 | } |
| 91 | } |
| 92 | |
Marc-André Lureau | a9b1ca3 | 2017-01-26 23:38:22 +0400 | [diff] [blame] | 93 | static int qemu_chr_write_buffer(Chardev *s, |
| 94 | const uint8_t *buf, int len, |
| 95 | int *offset, bool write_all) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 96 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 97 | ChardevClass *cc = CHARDEV_GET_CLASS(s); |
Igor Mammedov | 0931304 | 2014-06-25 10:00:41 +0200 | [diff] [blame] | 98 | int res = 0; |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 99 | *offset = 0; |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 100 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 101 | qemu_mutex_lock(&s->chr_write_lock); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 102 | while (*offset < len) { |
Daniel P. Berrange | 53628ef | 2016-03-31 16:29:27 +0100 | [diff] [blame] | 103 | retry: |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 104 | res = cc->chr_write(s, buf + *offset, len - *offset); |
Marc-André Lureau | c90e939 | 2017-01-26 22:48:37 +0400 | [diff] [blame] | 105 | if (res < 0 && errno == EAGAIN && write_all) { |
Daniel P. Berrange | 53628ef | 2016-03-31 16:29:27 +0100 | [diff] [blame] | 106 | g_usleep(100); |
| 107 | goto retry; |
| 108 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 109 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 110 | if (res <= 0) { |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 114 | *offset += res; |
Marc-André Lureau | c90e939 | 2017-01-26 22:48:37 +0400 | [diff] [blame] | 115 | if (!write_all) { |
| 116 | break; |
| 117 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 118 | } |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 119 | if (*offset > 0) { |
Marc-André Lureau | a9b1ca3 | 2017-01-26 23:38:22 +0400 | [diff] [blame] | 120 | qemu_chr_write_log(s, buf, *offset); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 121 | } |
| 122 | qemu_mutex_unlock(&s->chr_write_lock); |
| 123 | |
| 124 | return res; |
| 125 | } |
| 126 | |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 127 | int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all) |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 128 | { |
Marc-André Lureau | c90e939 | 2017-01-26 22:48:37 +0400 | [diff] [blame] | 129 | int offset = 0; |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 130 | int res; |
| 131 | |
Marc-André Lureau | 5ebd670 | 2016-10-21 22:58:45 +0300 | [diff] [blame] | 132 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 133 | replay_char_write_event_load(&res, &offset); |
| 134 | assert(offset <= len); |
Marc-André Lureau | a9b1ca3 | 2017-01-26 23:38:22 +0400 | [diff] [blame] | 135 | qemu_chr_write_buffer(s, buf, offset, &offset, true); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 136 | return res; |
| 137 | } |
| 138 | |
Marc-André Lureau | a9b1ca3 | 2017-01-26 23:38:22 +0400 | [diff] [blame] | 139 | res = qemu_chr_write_buffer(s, buf, len, &offset, write_all); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 140 | |
Marc-André Lureau | 5ebd670 | 2016-10-21 22:58:45 +0300 | [diff] [blame] | 141 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 142 | replay_char_write_event_save(res, offset); |
| 143 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 144 | |
Paolo Bonzini | 9005b2a | 2014-06-18 08:43:58 +0200 | [diff] [blame] | 145 | if (res < 0) { |
| 146 | return res; |
| 147 | } |
Anthony Liguori | cd18720 | 2013-03-26 10:04:17 -0500 | [diff] [blame] | 148 | return offset; |
| 149 | } |
| 150 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 151 | int qemu_chr_be_can_write(Chardev *s) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 152 | { |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 153 | CharBackend *be = s->be; |
| 154 | |
| 155 | if (!be || !be->chr_can_read) { |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 156 | return 0; |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return be->chr_can_read(be->opaque); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 162 | void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 163 | { |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 164 | CharBackend *be = s->be; |
| 165 | |
| 166 | if (be && be->chr_read) { |
| 167 | be->chr_read(be->opaque, buf, len); |
Stefan Weil | ac31073 | 2012-04-19 22:27:14 +0200 | [diff] [blame] | 168 | } |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 171 | void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len) |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 172 | { |
Marc-André Lureau | 5ebd670 | 2016-10-21 22:58:45 +0300 | [diff] [blame] | 173 | if (qemu_chr_replay(s)) { |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 174 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 175 | return; |
| 176 | } |
| 177 | replay_chr_be_write(s, buf, len); |
| 178 | } else { |
| 179 | qemu_chr_be_write_impl(s, buf, len); |
| 180 | } |
| 181 | } |
| 182 | |
Peter Xu | 07241c2 | 2017-09-21 14:35:51 +0800 | [diff] [blame] | 183 | void qemu_chr_be_update_read_handlers(Chardev *s, |
| 184 | GMainContext *context) |
| 185 | { |
| 186 | ChardevClass *cc = CHARDEV_GET_CLASS(s); |
| 187 | |
Peter Xu | 95eeeba | 2017-09-21 14:35:52 +0800 | [diff] [blame] | 188 | s->gcontext = context; |
Peter Xu | 07241c2 | 2017-09-21 14:35:51 +0800 | [diff] [blame] | 189 | if (cc->chr_update_read_handler) { |
Peter Xu | bb86d05 | 2017-09-21 14:35:54 +0800 | [diff] [blame] | 190 | cc->chr_update_read_handler(s); |
Peter Xu | 07241c2 | 2017-09-21 14:35:51 +0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 194 | int qemu_chr_add_client(Chardev *s, int fd) |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 195 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 196 | return CHARDEV_GET_CLASS(s)->chr_add_client ? |
| 197 | CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1; |
Daniel P. Berrange | 1366108 | 2011-06-23 13:31:42 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 200 | static void qemu_char_open(Chardev *chr, ChardevBackend *backend, |
| 201 | bool *be_opened, Error **errp) |
| 202 | { |
| 203 | ChardevClass *cc = CHARDEV_GET_CLASS(chr); |
| 204 | /* Any ChardevCommon member would work */ |
| 205 | ChardevCommon *common = backend ? backend->u.null.data : NULL; |
| 206 | |
| 207 | if (common && common->has_logfile) { |
| 208 | int flags = O_WRONLY | O_CREAT; |
| 209 | if (common->has_logappend && |
| 210 | common->logappend) { |
| 211 | flags |= O_APPEND; |
| 212 | } else { |
| 213 | flags |= O_TRUNC; |
| 214 | } |
| 215 | chr->logfd = qemu_open(common->logfile, flags, 0666); |
| 216 | if (chr->logfd < 0) { |
| 217 | error_setg_errno(errp, errno, |
| 218 | "Unable to open logfile %s", |
| 219 | common->logfile); |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (cc->open) { |
| 225 | cc->open(chr, backend, be_opened, errp); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void char_init(Object *obj) |
| 230 | { |
| 231 | Chardev *chr = CHARDEV(obj); |
| 232 | |
| 233 | chr->logfd = -1; |
| 234 | qemu_mutex_init(&chr->chr_write_lock); |
| 235 | } |
| 236 | |
Marc-André Lureau | eb314a9 | 2016-12-12 13:41:40 +0300 | [diff] [blame] | 237 | static int null_chr_write(Chardev *chr, const uint8_t *buf, int len) |
| 238 | { |
| 239 | return len; |
| 240 | } |
| 241 | |
| 242 | static void char_class_init(ObjectClass *oc, void *data) |
| 243 | { |
| 244 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 245 | |
| 246 | cc->chr_write = null_chr_write; |
| 247 | } |
| 248 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 249 | static void char_finalize(Object *obj) |
| 250 | { |
| 251 | Chardev *chr = CHARDEV(obj); |
| 252 | |
| 253 | if (chr->be) { |
| 254 | chr->be->chr = NULL; |
| 255 | } |
| 256 | g_free(chr->filename); |
| 257 | g_free(chr->label); |
| 258 | if (chr->logfd != -1) { |
| 259 | close(chr->logfd); |
| 260 | } |
| 261 | qemu_mutex_destroy(&chr->chr_write_lock); |
| 262 | } |
| 263 | |
| 264 | static const TypeInfo char_type_info = { |
| 265 | .name = TYPE_CHARDEV, |
| 266 | .parent = TYPE_OBJECT, |
| 267 | .instance_size = sizeof(Chardev), |
| 268 | .instance_init = char_init, |
| 269 | .instance_finalize = char_finalize, |
| 270 | .abstract = true, |
| 271 | .class_size = sizeof(ChardevClass), |
Marc-André Lureau | eb314a9 | 2016-12-12 13:41:40 +0300 | [diff] [blame] | 272 | .class_init = char_class_init, |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 273 | }; |
| 274 | |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 275 | /** |
| 276 | * Called after processing of default and command-line-specified |
| 277 | * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached |
| 278 | * to a mux chardev. This is done here to ensure that |
| 279 | * output/prompts/banners are only displayed for the FE that has |
| 280 | * focus when initial command-line processing/machine init is |
| 281 | * completed. |
| 282 | * |
| 283 | * After this point, any new FE attached to any new or existing |
| 284 | * mux will receive CHR_EVENT_OPENED notifications for the BE |
| 285 | * immediately. |
| 286 | */ |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 287 | static int open_muxes(Object *child, void *opaque) |
| 288 | { |
| 289 | if (CHARDEV_IS_MUX(child)) { |
| 290 | /* send OPENED to all already-attached FEs */ |
| 291 | mux_chr_send_all_event(CHARDEV(child), CHR_EVENT_OPENED); |
| 292 | /* mark mux as OPENED so any new FEs will immediately receive |
| 293 | * OPENED event |
| 294 | */ |
| 295 | qemu_chr_be_event(CHARDEV(child), CHR_EVENT_OPENED); |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 301 | static void muxes_realize_done(Notifier *notifier, void *unused) |
| 302 | { |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 303 | muxes_realized = true; |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 304 | object_child_foreach(get_chardevs_root(), open_muxes, NULL); |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static Notifier muxes_realize_notify = { |
| 308 | .notify = muxes_realize_done, |
| 309 | }; |
| 310 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 311 | static bool qemu_chr_is_busy(Chardev *s) |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 312 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 313 | if (CHARDEV_IS_MUX(s)) { |
| 314 | MuxChardev *d = MUX_CHARDEV(s); |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 315 | return d->mux_cnt >= 0; |
| 316 | } else { |
| 317 | return s->be != NULL; |
| 318 | } |
| 319 | } |
| 320 | |
Marc-André Lureau | d24ca4b | 2016-12-12 18:41:00 +0300 | [diff] [blame] | 321 | int qemu_chr_wait_connected(Chardev *chr, Error **errp) |
Marc-André Lureau | 6b6723c | 2016-07-27 01:15:17 +0400 | [diff] [blame] | 322 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 323 | ChardevClass *cc = CHARDEV_GET_CLASS(chr); |
| 324 | |
| 325 | if (cc->chr_wait_connected) { |
| 326 | return cc->chr_wait_connected(chr, errp); |
Marc-André Lureau | 6b6723c | 2016-07-27 01:15:17 +0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Gerd Hoffmann | 3352163 | 2009-12-08 13:11:49 +0100 | [diff] [blame] | 332 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 333 | { |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 334 | char host[65], port[33], width[8], height[8]; |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 335 | int pos; |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 336 | const char *p; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 337 | QemuOpts *opts; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 338 | Error *local_err = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 339 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 340 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 341 | if (local_err) { |
Markus Armbruster | 3339488 | 2015-02-10 15:21:26 +0100 | [diff] [blame] | 342 | error_report_err(local_err); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 343 | return NULL; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 344 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 345 | |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 346 | if (strstart(filename, "mon:", &p)) { |
| 347 | filename = p; |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 348 | qemu_opt_set(opts, "mux", "on", &error_abort); |
Paolo Bonzini | 02c4bdf | 2013-07-03 20:29:45 +0400 | [diff] [blame] | 349 | if (strcmp(filename, "stdio") == 0) { |
| 350 | /* Monitor is muxed to stdio: do not exit on Ctrl+C by default |
| 351 | * but pass it to the guest. Handle this only for compat syntax, |
| 352 | * for -chardev syntax we have special option for this. |
| 353 | * This is what -nographic did, redirecting+muxing serial+monitor |
| 354 | * to stdio causing Ctrl+C to be passed to guest. */ |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 355 | qemu_opt_set(opts, "signal", "off", &error_abort); |
Paolo Bonzini | 02c4bdf | 2013-07-03 20:29:45 +0400 | [diff] [blame] | 356 | } |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 359 | if (strcmp(filename, "null") == 0 || |
| 360 | strcmp(filename, "pty") == 0 || |
| 361 | strcmp(filename, "msmouse") == 0 || |
Anatoli Huseu1 | 378af96 | 2017-02-06 15:23:27 +0100 | [diff] [blame] | 362 | strcmp(filename, "wctablet") == 0 || |
Gerd Hoffmann | dc1c21e | 2009-09-10 10:58:46 +0200 | [diff] [blame] | 363 | strcmp(filename, "braille") == 0 || |
Paolo Bonzini | 5692399 | 2014-07-11 09:44:26 +0200 | [diff] [blame] | 364 | strcmp(filename, "testdev") == 0 || |
Gerd Hoffmann | f0457e8 | 2009-09-10 10:58:45 +0200 | [diff] [blame] | 365 | strcmp(filename, "stdio") == 0) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 366 | qemu_opt_set(opts, "backend", filename, &error_abort); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 367 | return opts; |
| 368 | } |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 369 | if (strstart(filename, "vc", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 370 | qemu_opt_set(opts, "backend", "vc", &error_abort); |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 371 | if (*p == ':') { |
Stefan Weil | 49aa405 | 2013-09-30 23:04:49 +0200 | [diff] [blame] | 372 | 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] | 373 | /* pixels */ |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 374 | qemu_opt_set(opts, "width", width, &error_abort); |
| 375 | qemu_opt_set(opts, "height", height, &error_abort); |
Stefan Weil | 49aa405 | 2013-09-30 23:04:49 +0200 | [diff] [blame] | 376 | } 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] | 377 | /* chars */ |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 378 | qemu_opt_set(opts, "cols", width, &error_abort); |
| 379 | qemu_opt_set(opts, "rows", height, &error_abort); |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 380 | } else { |
| 381 | goto fail; |
| 382 | } |
| 383 | } |
| 384 | return opts; |
| 385 | } |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 386 | if (strcmp(filename, "con:") == 0) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 387 | qemu_opt_set(opts, "backend", "console", &error_abort); |
Gerd Hoffmann | d6c983c | 2009-09-10 10:58:47 +0200 | [diff] [blame] | 388 | return opts; |
| 389 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 390 | if (strstart(filename, "COM", NULL)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 391 | qemu_opt_set(opts, "backend", "serial", &error_abort); |
| 392 | qemu_opt_set(opts, "path", filename, &error_abort); |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 393 | return opts; |
| 394 | } |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 395 | if (strstart(filename, "file:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 396 | qemu_opt_set(opts, "backend", "file", &error_abort); |
| 397 | qemu_opt_set(opts, "path", p, &error_abort); |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 398 | return opts; |
| 399 | } |
| 400 | if (strstart(filename, "pipe:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 401 | qemu_opt_set(opts, "backend", "pipe", &error_abort); |
| 402 | qemu_opt_set(opts, "path", p, &error_abort); |
Gerd Hoffmann | 7d31544 | 2009-09-10 10:58:36 +0200 | [diff] [blame] | 403 | return opts; |
| 404 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 405 | if (strstart(filename, "tcp:", &p) || |
Jing Liu | ae92cbd | 2016-09-23 08:06:11 +0200 | [diff] [blame] | 406 | strstart(filename, "telnet:", &p) || |
| 407 | strstart(filename, "tn3270:", &p)) { |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 408 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 409 | host[0] = 0; |
| 410 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) |
| 411 | goto fail; |
| 412 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 413 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
| 414 | qemu_opt_set(opts, "host", host, &error_abort); |
| 415 | qemu_opt_set(opts, "port", port, &error_abort); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 416 | if (p[pos] == ',') { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 417 | qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); |
| 418 | if (local_err) { |
| 419 | error_report_err(local_err); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 420 | goto fail; |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 421 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 422 | } |
Jing Liu | ae92cbd | 2016-09-23 08:06:11 +0200 | [diff] [blame] | 423 | if (strstart(filename, "telnet:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 424 | qemu_opt_set(opts, "telnet", "on", &error_abort); |
Jing Liu | ae92cbd | 2016-09-23 08:06:11 +0200 | [diff] [blame] | 425 | } else if (strstart(filename, "tn3270:", &p)) { |
| 426 | qemu_opt_set(opts, "tn3270", "on", &error_abort); |
| 427 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 428 | return opts; |
| 429 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 430 | if (strstart(filename, "udp:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 431 | qemu_opt_set(opts, "backend", "udp", &error_abort); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 432 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
| 433 | host[0] = 0; |
Jan Kiszka | 39324ca | 2010-03-07 11:28:48 +0100 | [diff] [blame] | 434 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 435 | goto fail; |
| 436 | } |
| 437 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 438 | qemu_opt_set(opts, "host", host, &error_abort); |
| 439 | qemu_opt_set(opts, "port", port, &error_abort); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 440 | if (p[pos] == '@') { |
| 441 | p += pos + 1; |
| 442 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
| 443 | host[0] = 0; |
| 444 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 445 | goto fail; |
| 446 | } |
| 447 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 448 | qemu_opt_set(opts, "localaddr", host, &error_abort); |
| 449 | qemu_opt_set(opts, "localport", port, &error_abort); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 450 | } |
| 451 | return opts; |
| 452 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 453 | if (strstart(filename, "unix:", &p)) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 454 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 455 | qemu_opts_do_parse(opts, p, "path", &local_err); |
| 456 | if (local_err) { |
| 457 | error_report_err(local_err); |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 458 | goto fail; |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 459 | } |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 460 | return opts; |
| 461 | } |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 462 | if (strstart(filename, "/dev/parport", NULL) || |
| 463 | strstart(filename, "/dev/ppi", NULL)) { |
Marc-André Lureau | 73119c2 | 2017-06-07 21:41:15 +0400 | [diff] [blame] | 464 | qemu_opt_set(opts, "backend", "parallel", &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 465 | qemu_opt_set(opts, "path", filename, &error_abort); |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 466 | return opts; |
| 467 | } |
| 468 | if (strstart(filename, "/dev/", NULL)) { |
Marc-André Lureau | 73119c2 | 2017-06-07 21:41:15 +0400 | [diff] [blame] | 469 | qemu_opt_set(opts, "backend", "serial", &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 470 | qemu_opt_set(opts, "path", filename, &error_abort); |
Gerd Hoffmann | 48b7649 | 2009-09-10 10:58:48 +0200 | [diff] [blame] | 471 | return opts; |
| 472 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 473 | |
Gerd Hoffmann | aeb2c47 | 2009-09-10 10:58:42 +0200 | [diff] [blame] | 474 | fail: |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 475 | qemu_opts_del(opts); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 479 | void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 480 | { |
| 481 | const char *logfile = qemu_opt_get(opts, "logfile"); |
| 482 | |
| 483 | backend->has_logfile = logfile != NULL; |
Marc-André Lureau | e1f98fe | 2016-12-14 23:15:44 +0300 | [diff] [blame] | 484 | backend->logfile = g_strdup(logfile); |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 485 | |
| 486 | backend->has_logappend = true; |
| 487 | backend->logappend = qemu_opt_get_bool(opts, "logappend", false); |
| 488 | } |
| 489 | |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 490 | static const ChardevClass *char_get_class(const char *driver, Error **errp) |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 491 | { |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 492 | ObjectClass *oc; |
| 493 | const ChardevClass *cc; |
| 494 | char *typename = g_strdup_printf("chardev-%s", driver); |
| 495 | |
| 496 | oc = object_class_by_name(typename); |
| 497 | g_free(typename); |
| 498 | |
| 499 | if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) { |
| 500 | error_setg(errp, "'%s' is not a valid char driver name", driver); |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | if (object_class_is_abstract(oc)) { |
| 505 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
| 506 | "abstract device type"); |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | cc = CHARDEV_CLASS(oc); |
| 511 | if (cc->internal) { |
| 512 | error_setg(errp, "'%s' is not a valid char driver name", driver); |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | return cc; |
| 517 | } |
| 518 | |
| 519 | static const struct ChardevAlias { |
| 520 | const char *typename; |
| 521 | const char *alias; |
| 522 | } chardev_alias_table[] = { |
| 523 | #ifdef HAVE_CHARDEV_PARPORT |
| 524 | { "parallel", "parport" }, |
| 525 | #endif |
| 526 | #ifdef HAVE_CHARDEV_SERIAL |
| 527 | { "serial", "tty" }, |
| 528 | #endif |
| 529 | }; |
| 530 | |
| 531 | typedef struct ChadevClassFE { |
| 532 | void (*fn)(const char *name, void *opaque); |
| 533 | void *opaque; |
| 534 | } ChadevClassFE; |
| 535 | |
| 536 | static void |
| 537 | chardev_class_foreach(ObjectClass *klass, void *opaque) |
| 538 | { |
| 539 | ChadevClassFE *fe = opaque; |
| 540 | |
| 541 | assert(g_str_has_prefix(object_class_get_name(klass), "chardev-")); |
| 542 | if (CHARDEV_CLASS(klass)->internal) { |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | fe->fn(object_class_get_name(klass) + 8, fe->opaque); |
| 547 | } |
| 548 | |
| 549 | static void |
| 550 | chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque) |
| 551 | { |
| 552 | ChadevClassFE fe = { .fn = fn, .opaque = opaque }; |
| 553 | int i; |
| 554 | |
| 555 | object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe); |
| 556 | |
Philippe Mathieu-Daudé | c7e47c6 | 2017-05-30 09:09:19 -0300 | [diff] [blame] | 557 | for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) { |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 558 | fn(chardev_alias_table[i].alias, opaque); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static void |
| 563 | help_string_append(const char *name, void *opaque) |
| 564 | { |
| 565 | GString *str = opaque; |
| 566 | |
| 567 | g_string_append_printf(str, "\n%s", name); |
Gerd Hoffmann | 2c5f488 | 2013-02-21 11:39:12 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Anton Nefedov | 313e45b | 2017-07-06 15:08:48 +0300 | [diff] [blame] | 570 | static const char *chardev_alias_translate(const char *name) |
| 571 | { |
| 572 | int i; |
| 573 | for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) { |
| 574 | if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) { |
| 575 | return chardev_alias_table[i].typename; |
| 576 | } |
| 577 | } |
| 578 | return name; |
| 579 | } |
| 580 | |
Anton Nefedov | 75b6016 | 2017-07-06 15:08:57 +0300 | [diff] [blame] | 581 | ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp) |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 582 | { |
Markus Armbruster | 0aff637 | 2014-05-19 18:57:35 +0200 | [diff] [blame] | 583 | Error *local_err = NULL; |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 584 | const ChardevClass *cc; |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 585 | ChardevBackend *backend = NULL; |
Anton Nefedov | 313e45b | 2017-07-06 15:08:48 +0300 | [diff] [blame] | 586 | const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend")); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 587 | |
Marc-André Lureau | 0b812f3 | 2016-10-21 16:07:45 +0300 | [diff] [blame] | 588 | if (name == NULL) { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 589 | error_setg(errp, "chardev: \"%s\" missing backend", |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 590 | qemu_opts_id(opts)); |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 591 | return NULL; |
Stefan Hajnoczi | 1bbd185 | 2011-01-22 13:07:26 +0000 | [diff] [blame] | 592 | } |
Lin Ma | 517b3d4 | 2016-08-17 01:13:52 +0800 | [diff] [blame] | 593 | |
Anton Nefedov | 313e45b | 2017-07-06 15:08:48 +0300 | [diff] [blame] | 594 | cc = char_get_class(name, errp); |
| 595 | if (cc == NULL) { |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | backend = g_new0(ChardevBackend, 1); |
| 600 | backend->type = CHARDEV_BACKEND_KIND_NULL; |
| 601 | |
| 602 | if (cc->parse) { |
| 603 | cc->parse(opts, backend, &local_err); |
| 604 | if (local_err) { |
| 605 | error_propagate(errp, local_err); |
| 606 | qapi_free_ChardevBackend(backend); |
| 607 | return NULL; |
| 608 | } |
| 609 | } else { |
| 610 | ChardevCommon *ccom = g_new0(ChardevCommon, 1); |
| 611 | qemu_chr_parse_common(opts, ccom); |
| 612 | backend->u.null.data = ccom; /* Any ChardevCommon member would work */ |
| 613 | } |
| 614 | |
| 615 | return backend; |
| 616 | } |
| 617 | |
| 618 | Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp) |
| 619 | { |
| 620 | const ChardevClass *cc; |
| 621 | Chardev *chr = NULL; |
| 622 | ChardevBackend *backend = NULL; |
| 623 | const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend")); |
| 624 | const char *id = qemu_opts_id(opts); |
| 625 | char *bid = NULL; |
| 626 | |
| 627 | if (name && is_help_option(name)) { |
Marc-André Lureau | 776a32a | 2016-11-30 22:57:24 +0400 | [diff] [blame] | 628 | GString *str = g_string_new(""); |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 629 | |
| 630 | chardev_name_foreach(help_string_append, str); |
Marc-André Lureau | 776a32a | 2016-11-30 22:57:24 +0400 | [diff] [blame] | 631 | |
| 632 | error_report("Available chardev backend types: %s", str->str); |
| 633 | g_string_free(str, true); |
Anton Nefedov | 0ec846b | 2017-07-25 13:04:41 +0300 | [diff] [blame] | 634 | return NULL; |
Lin Ma | 517b3d4 | 2016-08-17 01:13:52 +0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (id == NULL) { |
| 638 | error_setg(errp, "chardev: no id specified"); |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 639 | return NULL; |
Lin Ma | 517b3d4 | 2016-08-17 01:13:52 +0800 | [diff] [blame] | 640 | } |
| 641 | |
Anton Nefedov | 313e45b | 2017-07-06 15:08:48 +0300 | [diff] [blame] | 642 | backend = qemu_chr_parse_opts(opts, errp); |
| 643 | if (backend == NULL) { |
| 644 | return NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 645 | } |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 646 | |
| 647 | cc = char_get_class(name, errp); |
| 648 | if (cc == NULL) { |
Anton Nefedov | 313e45b | 2017-07-06 15:08:48 +0300 | [diff] [blame] | 649 | goto out; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 652 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 653 | bid = g_strdup_printf("%s-base", id); |
Gerd Hoffmann | 7591c5c | 2009-09-10 10:58:50 +0200 | [diff] [blame] | 654 | } |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 655 | |
Marc-André Lureau | c5749f7 | 2016-12-14 20:58:50 +0300 | [diff] [blame] | 656 | chr = qemu_chardev_new(bid ? bid : id, |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 657 | object_class_get_name(OBJECT_CLASS(cc)), |
| 658 | backend, errp); |
Marc-André Lureau | c5749f7 | 2016-12-14 20:58:50 +0300 | [diff] [blame] | 659 | |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 660 | if (chr == NULL) { |
| 661 | goto out; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | if (bid) { |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 665 | Chardev *mux; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 666 | qapi_free_ChardevBackend(backend); |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 667 | backend = g_new0(ChardevBackend, 1); |
Eric Blake | 130257d | 2015-10-26 16:34:57 -0600 | [diff] [blame] | 668 | backend->type = CHARDEV_BACKEND_KIND_MUX; |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 669 | backend->u.mux.data = g_new0(ChardevMux, 1); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 670 | backend->u.mux.data->chardev = g_strdup(bid); |
Marc-André Lureau | c5749f7 | 2016-12-14 20:58:50 +0300 | [diff] [blame] | 671 | mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp); |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 672 | if (mux == NULL) { |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 673 | object_unparent(OBJECT(chr)); |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 674 | chr = NULL; |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 675 | goto out; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 676 | } |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 677 | chr = mux; |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 680 | out: |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 681 | qapi_free_ChardevBackend(backend); |
Peter Maydell | a61ae7f | 2014-09-02 11:24:16 +0100 | [diff] [blame] | 682 | g_free(bid); |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 683 | return chr; |
| 684 | } |
| 685 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 686 | Chardev *qemu_chr_new_noreplay(const char *label, const char *filename) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 687 | { |
| 688 | const char *p; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 689 | Chardev *chr; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 690 | QemuOpts *opts; |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 691 | Error *err = NULL; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 692 | |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 693 | if (strstart(filename, "chardev:", &p)) { |
| 694 | return qemu_chr_find(p); |
| 695 | } |
| 696 | |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 697 | opts = qemu_chr_parse_compat(label, filename); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 698 | if (!opts) |
| 699 | return NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 700 | |
Marc-André Lureau | b4948be | 2016-10-22 12:52:46 +0300 | [diff] [blame] | 701 | chr = qemu_chr_new_from_opts(opts, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 702 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 703 | error_report_err(err); |
Gerd Hoffmann | bd2d80b | 2012-10-15 09:28:05 +0200 | [diff] [blame] | 704 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 705 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
| 706 | monitor_init(chr, MONITOR_USE_READLINE); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 707 | } |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 708 | qemu_opts_del(opts); |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 709 | return chr; |
| 710 | } |
| 711 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 712 | Chardev *qemu_chr_new(const char *label, const char *filename) |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 713 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 714 | Chardev *chr; |
Marc-André Lureau | b4948be | 2016-10-22 12:52:46 +0300 | [diff] [blame] | 715 | chr = qemu_chr_new_noreplay(label, filename); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 716 | if (chr) { |
Marc-André Lureau | 5ebd670 | 2016-10-21 22:58:45 +0300 | [diff] [blame] | 717 | if (replay_mode != REPLAY_MODE_NONE) { |
| 718 | qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY); |
| 719 | } |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 720 | if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) { |
Marc-André Lureau | 776a32a | 2016-11-30 22:57:24 +0400 | [diff] [blame] | 721 | error_report("Replay: ioctl is not supported " |
| 722 | "for serial devices yet"); |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 723 | } |
| 724 | replay_register_char_driver(chr); |
| 725 | } |
| 726 | return chr; |
| 727 | } |
| 728 | |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 729 | static int qmp_query_chardev_foreach(Object *obj, void *data) |
Marc-André Lureau | 1ad78ea | 2015-06-22 18:20:18 +0200 | [diff] [blame] | 730 | { |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 731 | Chardev *chr = CHARDEV(obj); |
| 732 | ChardevInfoList **list = data; |
| 733 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
| 734 | |
| 735 | info->value = g_malloc0(sizeof(*info->value)); |
| 736 | info->value->label = g_strdup(chr->label); |
| 737 | info->value->filename = g_strdup(chr->filename); |
| 738 | info->value->frontend_open = chr->be && chr->be->fe_open; |
| 739 | |
| 740 | info->next = *list; |
| 741 | *list = info; |
| 742 | |
| 743 | return 0; |
Marc-André Lureau | 1ad78ea | 2015-06-22 18:20:18 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 746 | ChardevInfoList *qmp_query_chardev(Error **errp) |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 747 | { |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 748 | ChardevInfoList *chr_list = NULL; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 749 | |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 750 | object_child_foreach(get_chardevs_root(), |
| 751 | qmp_query_chardev_foreach, &chr_list); |
Luiz Capitulino | 588b383 | 2009-12-10 17:16:08 -0200 | [diff] [blame] | 752 | |
Luiz Capitulino | c5a415a | 2011-09-14 16:05:49 -0300 | [diff] [blame] | 753 | return chr_list; |
aliguori | 6f97dba | 2008-10-31 18:49:55 +0000 | [diff] [blame] | 754 | } |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 755 | |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 756 | static void |
| 757 | qmp_prepend_backend(const char *name, void *opaque) |
Marc-André Lureau | 0b812f3 | 2016-10-21 16:07:45 +0300 | [diff] [blame] | 758 | { |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 759 | ChardevBackendInfoList **list = opaque; |
Marc-André Lureau | 0b812f3 | 2016-10-21 16:07:45 +0300 | [diff] [blame] | 760 | ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 761 | |
Marc-André Lureau | 0b812f3 | 2016-10-21 16:07:45 +0300 | [diff] [blame] | 762 | info->value = g_malloc0(sizeof(*info->value)); |
| 763 | info->value->name = g_strdup(name); |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 764 | info->next = *list; |
| 765 | *list = info; |
Marc-André Lureau | 0b812f3 | 2016-10-21 16:07:45 +0300 | [diff] [blame] | 766 | } |
| 767 | |
Martin Kletzander | 77d1c3c | 2014-02-01 12:52:42 +0100 | [diff] [blame] | 768 | ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) |
| 769 | { |
| 770 | ChardevBackendInfoList *backend_list = NULL; |
Martin Kletzander | 77d1c3c | 2014-02-01 12:52:42 +0100 | [diff] [blame] | 771 | |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 772 | chardev_name_foreach(qmp_prepend_backend, &backend_list); |
Martin Kletzander | 77d1c3c | 2014-02-01 12:52:42 +0100 | [diff] [blame] | 773 | |
| 774 | return backend_list; |
| 775 | } |
| 776 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 777 | Chardev *qemu_chr_find(const char *name) |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 778 | { |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 779 | Object *obj = object_resolve_path_component(get_chardevs_root(), name); |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 780 | |
Marc-André Lureau | 6061162 | 2016-12-14 16:44:19 +0300 | [diff] [blame] | 781 | return obj ? CHARDEV(obj) : NULL; |
Gerd Hoffmann | c845f40 | 2009-09-10 10:58:52 +0200 | [diff] [blame] | 782 | } |
Anthony Liguori | 0beb494 | 2011-12-22 15:29:25 -0600 | [diff] [blame] | 783 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 784 | QemuOptsList qemu_chardev_opts = { |
| 785 | .name = "chardev", |
| 786 | .implied_opt_name = "backend", |
| 787 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), |
| 788 | .desc = { |
| 789 | { |
| 790 | .name = "backend", |
| 791 | .type = QEMU_OPT_STRING, |
| 792 | },{ |
| 793 | .name = "path", |
| 794 | .type = QEMU_OPT_STRING, |
| 795 | },{ |
| 796 | .name = "host", |
| 797 | .type = QEMU_OPT_STRING, |
| 798 | },{ |
| 799 | .name = "port", |
| 800 | .type = QEMU_OPT_STRING, |
| 801 | },{ |
| 802 | .name = "localaddr", |
| 803 | .type = QEMU_OPT_STRING, |
| 804 | },{ |
| 805 | .name = "localport", |
| 806 | .type = QEMU_OPT_STRING, |
| 807 | },{ |
| 808 | .name = "to", |
| 809 | .type = QEMU_OPT_NUMBER, |
| 810 | },{ |
| 811 | .name = "ipv4", |
| 812 | .type = QEMU_OPT_BOOL, |
| 813 | },{ |
| 814 | .name = "ipv6", |
| 815 | .type = QEMU_OPT_BOOL, |
| 816 | },{ |
| 817 | .name = "wait", |
| 818 | .type = QEMU_OPT_BOOL, |
| 819 | },{ |
| 820 | .name = "server", |
| 821 | .type = QEMU_OPT_BOOL, |
| 822 | },{ |
| 823 | .name = "delay", |
| 824 | .type = QEMU_OPT_BOOL, |
| 825 | },{ |
Corey Minyard | 5dd1f02 | 2014-10-02 11:17:37 -0500 | [diff] [blame] | 826 | .name = "reconnect", |
| 827 | .type = QEMU_OPT_NUMBER, |
| 828 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 829 | .name = "telnet", |
| 830 | .type = QEMU_OPT_BOOL, |
| 831 | },{ |
Jing Liu | ae92cbd | 2016-09-23 08:06:11 +0200 | [diff] [blame] | 832 | .name = "tn3270", |
| 833 | .type = QEMU_OPT_BOOL, |
| 834 | },{ |
Daniel P. Berrange | a8fb542 | 2016-01-19 11:14:31 +0000 | [diff] [blame] | 835 | .name = "tls-creds", |
| 836 | .type = QEMU_OPT_STRING, |
| 837 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 838 | .name = "width", |
| 839 | .type = QEMU_OPT_NUMBER, |
| 840 | },{ |
| 841 | .name = "height", |
| 842 | .type = QEMU_OPT_NUMBER, |
| 843 | },{ |
| 844 | .name = "cols", |
| 845 | .type = QEMU_OPT_NUMBER, |
| 846 | },{ |
| 847 | .name = "rows", |
| 848 | .type = QEMU_OPT_NUMBER, |
| 849 | },{ |
| 850 | .name = "mux", |
| 851 | .type = QEMU_OPT_BOOL, |
| 852 | },{ |
| 853 | .name = "signal", |
| 854 | .type = QEMU_OPT_BOOL, |
| 855 | },{ |
| 856 | .name = "name", |
| 857 | .type = QEMU_OPT_STRING, |
| 858 | },{ |
| 859 | .name = "debug", |
| 860 | .type = QEMU_OPT_NUMBER, |
Lei Li | 51767e7 | 2013-01-25 00:03:19 +0800 | [diff] [blame] | 861 | },{ |
Markus Armbruster | 3949e59 | 2013-02-06 21:27:24 +0100 | [diff] [blame] | 862 | .name = "size", |
Markus Armbruster | de1cc36 | 2013-02-06 21:27:25 +0100 | [diff] [blame] | 863 | .type = QEMU_OPT_SIZE, |
Gerd Hoffmann | bb6fb7c | 2013-06-24 08:39:54 +0200 | [diff] [blame] | 864 | },{ |
| 865 | .name = "chardev", |
| 866 | .type = QEMU_OPT_STRING, |
Olga Krishtal | 31e38a2 | 2015-12-04 09:42:04 +0300 | [diff] [blame] | 867 | },{ |
| 868 | .name = "append", |
| 869 | .type = QEMU_OPT_BOOL, |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 870 | },{ |
| 871 | .name = "logfile", |
| 872 | .type = QEMU_OPT_STRING, |
| 873 | },{ |
| 874 | .name = "logappend", |
| 875 | .type = QEMU_OPT_BOOL, |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 876 | }, |
| 877 | { /* end of list */ } |
| 878 | }, |
| 879 | }; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 880 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 881 | bool qemu_chr_has_feature(Chardev *chr, |
Marc-André Lureau | 279b066 | 2016-12-14 13:27:58 +0300 | [diff] [blame] | 882 | ChardevFeature feature) |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 883 | { |
| 884 | return test_bit(feature, chr->features); |
| 885 | } |
| 886 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 887 | void qemu_chr_set_feature(Chardev *chr, |
Marc-André Lureau | 279b066 | 2016-12-14 13:27:58 +0300 | [diff] [blame] | 888 | ChardevFeature feature) |
Daniel P. Berrange | 0a73336 | 2016-10-07 13:18:34 +0100 | [diff] [blame] | 889 | { |
| 890 | return set_bit(feature, chr->features); |
| 891 | } |
| 892 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 893 | Chardev *qemu_chardev_new(const char *id, const char *typename, |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 894 | ChardevBackend *backend, |
| 895 | Error **errp) |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 896 | { |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 897 | Object *obj; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 898 | Chardev *chr = NULL; |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 899 | Error *local_err = NULL; |
Marc-André Lureau | 82878da | 2016-10-22 13:09:43 +0300 | [diff] [blame] | 900 | bool be_opened = true; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 901 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 902 | assert(g_str_has_prefix(typename, "chardev-")); |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 903 | |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 904 | obj = object_new(typename); |
| 905 | chr = CHARDEV(obj); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 906 | chr->label = g_strdup(id); |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 907 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 908 | qemu_char_open(chr, backend, &be_opened, &local_err); |
Marc-André Lureau | a1698bf | 2016-10-21 16:30:29 +0300 | [diff] [blame] | 909 | if (local_err) { |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 910 | goto end; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 911 | } |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 912 | |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 913 | if (!chr->filename) { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 914 | chr->filename = g_strdup(typename + 8); |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 915 | } |
Marc-André Lureau | 82878da | 2016-10-22 13:09:43 +0300 | [diff] [blame] | 916 | if (be_opened) { |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 917 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 918 | } |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 919 | |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 920 | if (id) { |
| 921 | object_property_add_child(get_chardevs_root(), id, obj, &local_err); |
| 922 | if (local_err) { |
| 923 | goto end; |
| 924 | } |
| 925 | object_unref(obj); |
| 926 | } |
| 927 | |
| 928 | end: |
| 929 | if (local_err) { |
| 930 | error_propagate(errp, local_err); |
| 931 | object_unref(obj); |
| 932 | return NULL; |
| 933 | } |
| 934 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 935 | return chr; |
| 936 | } |
| 937 | |
| 938 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, |
| 939 | Error **errp) |
| 940 | { |
| 941 | const ChardevClass *cc; |
| 942 | ChardevReturn *ret; |
| 943 | Chardev *chr; |
| 944 | |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 945 | cc = char_get_class(ChardevBackendKind_str(backend->type), errp); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 946 | if (!cc) { |
| 947 | return NULL; |
| 948 | } |
| 949 | |
Marc-André Lureau | c5749f7 | 2016-12-14 20:58:50 +0300 | [diff] [blame] | 950 | chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)), |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 951 | backend, errp); |
| 952 | if (!chr) { |
| 953 | return NULL; |
| 954 | } |
| 955 | |
| 956 | ret = g_new0(ChardevReturn, 1); |
| 957 | if (CHARDEV_IS_PTY(chr)) { |
| 958 | ret->pty = g_strdup(chr->filename + 4); |
| 959 | ret->has_pty = true; |
| 960 | } |
| 961 | |
Paolo Bonzini | eaeba65 | 2015-09-29 14:54:05 +0200 | [diff] [blame] | 962 | return ret; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Anton Nefedov | 7bb8608 | 2017-07-06 15:08:50 +0300 | [diff] [blame] | 965 | ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, |
| 966 | Error **errp) |
| 967 | { |
| 968 | CharBackend *be; |
| 969 | const ChardevClass *cc; |
| 970 | Chardev *chr, *chr_new; |
| 971 | bool closed_sent = false; |
| 972 | ChardevReturn *ret; |
| 973 | |
| 974 | chr = qemu_chr_find(id); |
| 975 | if (!chr) { |
| 976 | error_setg(errp, "Chardev '%s' does not exist", id); |
| 977 | return NULL; |
| 978 | } |
| 979 | |
| 980 | if (CHARDEV_IS_MUX(chr)) { |
| 981 | error_setg(errp, "Mux device hotswap not supported yet"); |
| 982 | return NULL; |
| 983 | } |
| 984 | |
| 985 | if (qemu_chr_replay(chr)) { |
| 986 | error_setg(errp, |
| 987 | "Chardev '%s' cannot be changed in record/replay mode", id); |
| 988 | return NULL; |
| 989 | } |
| 990 | |
| 991 | be = chr->be; |
| 992 | if (!be) { |
| 993 | /* easy case */ |
| 994 | object_unparent(OBJECT(chr)); |
| 995 | return qmp_chardev_add(id, backend, errp); |
| 996 | } |
| 997 | |
| 998 | if (!be->chr_be_change) { |
| 999 | error_setg(errp, "Chardev user does not support chardev hotswap"); |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1003 | cc = char_get_class(ChardevBackendKind_str(backend->type), errp); |
Anton Nefedov | 7bb8608 | 2017-07-06 15:08:50 +0300 | [diff] [blame] | 1004 | if (!cc) { |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
| 1008 | chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)), |
| 1009 | backend, errp); |
| 1010 | if (!chr_new) { |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | chr_new->label = g_strdup(id); |
| 1014 | |
| 1015 | if (chr->be_open && !chr_new->be_open) { |
| 1016 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
| 1017 | closed_sent = true; |
| 1018 | } |
| 1019 | |
| 1020 | chr->be = NULL; |
| 1021 | qemu_chr_fe_init(be, chr_new, &error_abort); |
| 1022 | |
| 1023 | if (be->chr_be_change(be->opaque) < 0) { |
| 1024 | error_setg(errp, "Chardev '%s' change failed", chr_new->label); |
| 1025 | chr_new->be = NULL; |
| 1026 | qemu_chr_fe_init(be, chr, &error_abort); |
| 1027 | if (closed_sent) { |
| 1028 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 1029 | } |
| 1030 | object_unref(OBJECT(chr_new)); |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | |
| 1034 | object_unparent(OBJECT(chr)); |
| 1035 | object_property_add_child(get_chardevs_root(), chr_new->label, |
| 1036 | OBJECT(chr_new), &error_abort); |
| 1037 | object_unref(OBJECT(chr_new)); |
| 1038 | |
| 1039 | ret = g_new0(ChardevReturn, 1); |
| 1040 | if (CHARDEV_IS_PTY(chr_new)) { |
| 1041 | ret->pty = g_strdup(chr_new->filename + 4); |
| 1042 | ret->has_pty = true; |
| 1043 | } |
| 1044 | |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 1048 | void qmp_chardev_remove(const char *id, Error **errp) |
| 1049 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 1050 | Chardev *chr; |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 1051 | |
| 1052 | chr = qemu_chr_find(id); |
Gonglei | 8108fd3 | 2014-08-11 21:00:55 +0800 | [diff] [blame] | 1053 | if (chr == NULL) { |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 1054 | error_setg(errp, "Chardev '%s' not found", id); |
| 1055 | return; |
| 1056 | } |
Marc-André Lureau | a4afa54 | 2016-10-22 12:53:01 +0300 | [diff] [blame] | 1057 | if (qemu_chr_is_busy(chr)) { |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 1058 | error_setg(errp, "Chardev '%s' is busy", id); |
| 1059 | return; |
| 1060 | } |
Marc-André Lureau | 5ebd670 | 2016-10-21 22:58:45 +0300 | [diff] [blame] | 1061 | if (qemu_chr_replay(chr)) { |
Pavel Dovgalyuk | 33577b4 | 2016-03-14 10:44:36 +0300 | [diff] [blame] | 1062 | error_setg(errp, |
| 1063 | "Chardev '%s' cannot be unplugged in record/replay mode", id); |
| 1064 | return; |
| 1065 | } |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 1066 | object_unparent(OBJECT(chr)); |
Gerd Hoffmann | f1a1a35 | 2012-12-19 10:33:56 +0100 | [diff] [blame] | 1067 | } |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 1068 | |
Stefan Fritsch | bd1d5ad | 2017-06-11 09:48:17 +0200 | [diff] [blame] | 1069 | void qmp_chardev_send_break(const char *id, Error **errp) |
| 1070 | { |
| 1071 | Chardev *chr; |
| 1072 | |
| 1073 | chr = qemu_chr_find(id); |
| 1074 | if (chr == NULL) { |
| 1075 | error_setg(errp, "Chardev '%s' not found", id); |
| 1076 | return; |
| 1077 | } |
| 1078 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
| 1079 | } |
| 1080 | |
Marc-André Lureau | aa5cb7f | 2016-07-04 17:38:23 +0200 | [diff] [blame] | 1081 | void qemu_chr_cleanup(void) |
Marc-André Lureau | c1111a2 | 2016-06-16 21:28:50 +0200 | [diff] [blame] | 1082 | { |
Marc-André Lureau | 2f5d45a | 2016-12-14 15:23:36 +0300 | [diff] [blame] | 1083 | object_unparent(get_chardevs_root()); |
Marc-André Lureau | c1111a2 | 2016-06-16 21:28:50 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 1086 | static void register_types(void) |
| 1087 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 1088 | type_register_static(&char_type_info); |
Marc-André Lureau | 0b812f3 | 2016-10-21 16:07:45 +0300 | [diff] [blame] | 1089 | |
Michael Roth | 7b7ab18 | 2013-07-30 13:04:22 -0500 | [diff] [blame] | 1090 | /* this must be done after machine init, since we register FEs with muxes |
| 1091 | * as part of realize functions like serial_isa_realizefn when -nographic |
| 1092 | * is specified |
| 1093 | */ |
| 1094 | qemu_add_machine_init_done_notifier(&muxes_realize_notify); |
Anthony Liguori | d654f34 | 2013-03-05 23:21:28 +0530 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | type_init(register_types); |