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