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