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 bool permit_mux_mon) 334 { 335 char host[65], port[33], width[8], height[8]; 336 int pos; 337 const char *p; 338 QemuOpts *opts; 339 Error *local_err = NULL; 340 341 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); 342 if (local_err) { 343 error_report_err(local_err); 344 return NULL; 345 } 346 347 if (strstart(filename, "mon:", &p)) { 348 if (!permit_mux_mon) { 349 error_report("mon: isn't supported in this context"); 350 return NULL; 351 } 352 filename = p; 353 qemu_opt_set(opts, "mux", "on", &error_abort); 354 if (strcmp(filename, "stdio") == 0) { 355 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default 356 * but pass it to the guest. Handle this only for compat syntax, 357 * for -chardev syntax we have special option for this. 358 * This is what -nographic did, redirecting+muxing serial+monitor 359 * to stdio causing Ctrl+C to be passed to guest. */ 360 qemu_opt_set(opts, "signal", "off", &error_abort); 361 } 362 } 363 364 if (strcmp(filename, "null") == 0 || 365 strcmp(filename, "pty") == 0 || 366 strcmp(filename, "msmouse") == 0 || 367 strcmp(filename, "wctablet") == 0 || 368 strcmp(filename, "braille") == 0 || 369 strcmp(filename, "testdev") == 0 || 370 strcmp(filename, "stdio") == 0) { 371 qemu_opt_set(opts, "backend", filename, &error_abort); 372 return opts; 373 } 374 if (strstart(filename, "vc", &p)) { 375 qemu_opt_set(opts, "backend", "vc", &error_abort); 376 if (*p == ':') { 377 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { 378 /* pixels */ 379 qemu_opt_set(opts, "width", width, &error_abort); 380 qemu_opt_set(opts, "height", height, &error_abort); 381 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { 382 /* chars */ 383 qemu_opt_set(opts, "cols", width, &error_abort); 384 qemu_opt_set(opts, "rows", height, &error_abort); 385 } else { 386 goto fail; 387 } 388 } 389 return opts; 390 } 391 if (strcmp(filename, "con:") == 0) { 392 qemu_opt_set(opts, "backend", "console", &error_abort); 393 return opts; 394 } 395 if (strstart(filename, "COM", NULL)) { 396 qemu_opt_set(opts, "backend", "serial", &error_abort); 397 qemu_opt_set(opts, "path", filename, &error_abort); 398 return opts; 399 } 400 if (strstart(filename, "file:", &p)) { 401 qemu_opt_set(opts, "backend", "file", &error_abort); 402 qemu_opt_set(opts, "path", p, &error_abort); 403 return opts; 404 } 405 if (strstart(filename, "pipe:", &p)) { 406 qemu_opt_set(opts, "backend", "pipe", &error_abort); 407 qemu_opt_set(opts, "path", p, &error_abort); 408 return opts; 409 } 410 if (strstart(filename, "tcp:", &p) || 411 strstart(filename, "telnet:", &p) || 412 strstart(filename, "tn3270:", &p)) { 413 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { 414 host[0] = 0; 415 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) 416 goto fail; 417 } 418 qemu_opt_set(opts, "backend", "socket", &error_abort); 419 qemu_opt_set(opts, "host", host, &error_abort); 420 qemu_opt_set(opts, "port", port, &error_abort); 421 if (p[pos] == ',') { 422 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); 423 if (local_err) { 424 error_report_err(local_err); 425 goto fail; 426 } 427 } 428 if (strstart(filename, "telnet:", &p)) { 429 qemu_opt_set(opts, "telnet", "on", &error_abort); 430 } else if (strstart(filename, "tn3270:", &p)) { 431 qemu_opt_set(opts, "tn3270", "on", &error_abort); 432 } 433 return opts; 434 } 435 if (strstart(filename, "udp:", &p)) { 436 qemu_opt_set(opts, "backend", "udp", &error_abort); 437 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { 438 host[0] = 0; 439 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { 440 goto fail; 441 } 442 } 443 qemu_opt_set(opts, "host", host, &error_abort); 444 qemu_opt_set(opts, "port", port, &error_abort); 445 if (p[pos] == '@') { 446 p += pos + 1; 447 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { 448 host[0] = 0; 449 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { 450 goto fail; 451 } 452 } 453 qemu_opt_set(opts, "localaddr", host, &error_abort); 454 qemu_opt_set(opts, "localport", port, &error_abort); 455 } 456 return opts; 457 } 458 if (strstart(filename, "unix:", &p)) { 459 qemu_opt_set(opts, "backend", "socket", &error_abort); 460 qemu_opts_do_parse(opts, p, "path", &local_err); 461 if (local_err) { 462 error_report_err(local_err); 463 goto fail; 464 } 465 return opts; 466 } 467 if (strstart(filename, "/dev/parport", NULL) || 468 strstart(filename, "/dev/ppi", NULL)) { 469 qemu_opt_set(opts, "backend", "parallel", &error_abort); 470 qemu_opt_set(opts, "path", filename, &error_abort); 471 return opts; 472 } 473 if (strstart(filename, "/dev/", NULL)) { 474 qemu_opt_set(opts, "backend", "serial", &error_abort); 475 qemu_opt_set(opts, "path", filename, &error_abort); 476 return opts; 477 } 478 479 fail: 480 qemu_opts_del(opts); 481 return NULL; 482 } 483 484 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) 485 { 486 const char *logfile = qemu_opt_get(opts, "logfile"); 487 488 backend->has_logfile = logfile != NULL; 489 backend->logfile = g_strdup(logfile); 490 491 backend->has_logappend = true; 492 backend->logappend = qemu_opt_get_bool(opts, "logappend", false); 493 } 494 495 static const ChardevClass *char_get_class(const char *driver, Error **errp) 496 { 497 ObjectClass *oc; 498 const ChardevClass *cc; 499 char *typename = g_strdup_printf("chardev-%s", driver); 500 501 oc = object_class_by_name(typename); 502 g_free(typename); 503 504 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) { 505 error_setg(errp, "'%s' is not a valid char driver name", driver); 506 return NULL; 507 } 508 509 if (object_class_is_abstract(oc)) { 510 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", 511 "abstract device type"); 512 return NULL; 513 } 514 515 cc = CHARDEV_CLASS(oc); 516 if (cc->internal) { 517 error_setg(errp, "'%s' is not a valid char driver name", driver); 518 return NULL; 519 } 520 521 return cc; 522 } 523 524 static const struct ChardevAlias { 525 const char *typename; 526 const char *alias; 527 } chardev_alias_table[] = { 528 #ifdef HAVE_CHARDEV_PARPORT 529 { "parallel", "parport" }, 530 #endif 531 #ifdef HAVE_CHARDEV_SERIAL 532 { "serial", "tty" }, 533 #endif 534 }; 535 536 typedef struct ChadevClassFE { 537 void (*fn)(const char *name, void *opaque); 538 void *opaque; 539 } ChadevClassFE; 540 541 static void 542 chardev_class_foreach(ObjectClass *klass, void *opaque) 543 { 544 ChadevClassFE *fe = opaque; 545 546 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-")); 547 if (CHARDEV_CLASS(klass)->internal) { 548 return; 549 } 550 551 fe->fn(object_class_get_name(klass) + 8, fe->opaque); 552 } 553 554 static void 555 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque) 556 { 557 ChadevClassFE fe = { .fn = fn, .opaque = opaque }; 558 int i; 559 560 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe); 561 562 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) { 563 fn(chardev_alias_table[i].alias, opaque); 564 } 565 } 566 567 static void 568 help_string_append(const char *name, void *opaque) 569 { 570 GString *str = opaque; 571 572 g_string_append_printf(str, "\n%s", name); 573 } 574 575 static const char *chardev_alias_translate(const char *name) 576 { 577 int i; 578 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) { 579 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) { 580 return chardev_alias_table[i].typename; 581 } 582 } 583 return name; 584 } 585 586 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp) 587 { 588 Error *local_err = NULL; 589 const ChardevClass *cc; 590 ChardevBackend *backend = NULL; 591 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend")); 592 593 if (name == NULL) { 594 error_setg(errp, "chardev: \"%s\" missing backend", 595 qemu_opts_id(opts)); 596 return NULL; 597 } 598 599 cc = char_get_class(name, errp); 600 if (cc == NULL) { 601 return NULL; 602 } 603 604 backend = g_new0(ChardevBackend, 1); 605 backend->type = CHARDEV_BACKEND_KIND_NULL; 606 607 if (cc->parse) { 608 cc->parse(opts, backend, &local_err); 609 if (local_err) { 610 error_propagate(errp, local_err); 611 qapi_free_ChardevBackend(backend); 612 return NULL; 613 } 614 } else { 615 ChardevCommon *ccom = g_new0(ChardevCommon, 1); 616 qemu_chr_parse_common(opts, ccom); 617 backend->u.null.data = ccom; /* Any ChardevCommon member would work */ 618 } 619 620 return backend; 621 } 622 623 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp) 624 { 625 const ChardevClass *cc; 626 Chardev *chr = NULL; 627 ChardevBackend *backend = NULL; 628 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend")); 629 const char *id = qemu_opts_id(opts); 630 char *bid = NULL; 631 632 if (name && is_help_option(name)) { 633 GString *str = g_string_new(""); 634 635 chardev_name_foreach(help_string_append, str); 636 637 error_report("Available chardev backend types: %s", str->str); 638 g_string_free(str, true); 639 return NULL; 640 } 641 642 if (id == NULL) { 643 error_setg(errp, "chardev: no id specified"); 644 return NULL; 645 } 646 647 backend = qemu_chr_parse_opts(opts, errp); 648 if (backend == NULL) { 649 return NULL; 650 } 651 652 cc = char_get_class(name, errp); 653 if (cc == NULL) { 654 goto out; 655 } 656 657 if (qemu_opt_get_bool(opts, "mux", 0)) { 658 bid = g_strdup_printf("%s-base", id); 659 } 660 661 chr = qemu_chardev_new(bid ? bid : id, 662 object_class_get_name(OBJECT_CLASS(cc)), 663 backend, errp); 664 665 if (chr == NULL) { 666 goto out; 667 } 668 669 if (bid) { 670 Chardev *mux; 671 qapi_free_ChardevBackend(backend); 672 backend = g_new0(ChardevBackend, 1); 673 backend->type = CHARDEV_BACKEND_KIND_MUX; 674 backend->u.mux.data = g_new0(ChardevMux, 1); 675 backend->u.mux.data->chardev = g_strdup(bid); 676 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp); 677 if (mux == NULL) { 678 object_unparent(OBJECT(chr)); 679 chr = NULL; 680 goto out; 681 } 682 chr = mux; 683 } 684 685 out: 686 qapi_free_ChardevBackend(backend); 687 g_free(bid); 688 return chr; 689 } 690 691 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename, 692 bool permit_mux_mon) 693 { 694 const char *p; 695 Chardev *chr; 696 QemuOpts *opts; 697 Error *err = NULL; 698 699 if (strstart(filename, "chardev:", &p)) { 700 return qemu_chr_find(p); 701 } 702 703 opts = qemu_chr_parse_compat(label, filename, permit_mux_mon); 704 if (!opts) 705 return NULL; 706 707 chr = qemu_chr_new_from_opts(opts, &err); 708 if (!chr) { 709 error_report_err(err); 710 goto out; 711 } 712 713 if (qemu_opt_get_bool(opts, "mux", 0)) { 714 assert(permit_mux_mon); 715 monitor_init(chr, MONITOR_USE_READLINE); 716 } 717 718 out: 719 qemu_opts_del(opts); 720 return chr; 721 } 722 723 static Chardev *qemu_chr_new_permit_mux_mon(const char *label, 724 const char *filename, 725 bool permit_mux_mon) 726 { 727 Chardev *chr; 728 chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon); 729 if (chr) { 730 if (replay_mode != REPLAY_MODE_NONE) { 731 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY); 732 } 733 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) { 734 error_report("Replay: ioctl is not supported " 735 "for serial devices yet"); 736 } 737 replay_register_char_driver(chr); 738 } 739 return chr; 740 } 741 742 Chardev *qemu_chr_new(const char *label, const char *filename) 743 { 744 return qemu_chr_new_permit_mux_mon(label, filename, false); 745 } 746 747 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename) 748 { 749 return qemu_chr_new_permit_mux_mon(label, filename, true); 750 } 751 752 static int qmp_query_chardev_foreach(Object *obj, void *data) 753 { 754 Chardev *chr = CHARDEV(obj); 755 ChardevInfoList **list = data; 756 ChardevInfoList *info = g_malloc0(sizeof(*info)); 757 758 info->value = g_malloc0(sizeof(*info->value)); 759 info->value->label = g_strdup(chr->label); 760 info->value->filename = g_strdup(chr->filename); 761 info->value->frontend_open = chr->be && chr->be->fe_open; 762 763 info->next = *list; 764 *list = info; 765 766 return 0; 767 } 768 769 ChardevInfoList *qmp_query_chardev(Error **errp) 770 { 771 ChardevInfoList *chr_list = NULL; 772 773 object_child_foreach(get_chardevs_root(), 774 qmp_query_chardev_foreach, &chr_list); 775 776 return chr_list; 777 } 778 779 static void 780 qmp_prepend_backend(const char *name, void *opaque) 781 { 782 ChardevBackendInfoList **list = opaque; 783 ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); 784 785 info->value = g_malloc0(sizeof(*info->value)); 786 info->value->name = g_strdup(name); 787 info->next = *list; 788 *list = info; 789 } 790 791 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) 792 { 793 ChardevBackendInfoList *backend_list = NULL; 794 795 chardev_name_foreach(qmp_prepend_backend, &backend_list); 796 797 return backend_list; 798 } 799 800 Chardev *qemu_chr_find(const char *name) 801 { 802 Object *obj = object_resolve_path_component(get_chardevs_root(), name); 803 804 return obj ? CHARDEV(obj) : NULL; 805 } 806 807 QemuOptsList qemu_chardev_opts = { 808 .name = "chardev", 809 .implied_opt_name = "backend", 810 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), 811 .desc = { 812 { 813 .name = "backend", 814 .type = QEMU_OPT_STRING, 815 },{ 816 .name = "path", 817 .type = QEMU_OPT_STRING, 818 },{ 819 .name = "host", 820 .type = QEMU_OPT_STRING, 821 },{ 822 .name = "port", 823 .type = QEMU_OPT_STRING, 824 },{ 825 .name = "fd", 826 .type = QEMU_OPT_STRING, 827 },{ 828 .name = "localaddr", 829 .type = QEMU_OPT_STRING, 830 },{ 831 .name = "localport", 832 .type = QEMU_OPT_STRING, 833 },{ 834 .name = "to", 835 .type = QEMU_OPT_NUMBER, 836 },{ 837 .name = "ipv4", 838 .type = QEMU_OPT_BOOL, 839 },{ 840 .name = "ipv6", 841 .type = QEMU_OPT_BOOL, 842 },{ 843 .name = "wait", 844 .type = QEMU_OPT_BOOL, 845 },{ 846 .name = "server", 847 .type = QEMU_OPT_BOOL, 848 },{ 849 .name = "delay", 850 .type = QEMU_OPT_BOOL, 851 },{ 852 .name = "reconnect", 853 .type = QEMU_OPT_NUMBER, 854 },{ 855 .name = "telnet", 856 .type = QEMU_OPT_BOOL, 857 },{ 858 .name = "tn3270", 859 .type = QEMU_OPT_BOOL, 860 },{ 861 .name = "tls-creds", 862 .type = QEMU_OPT_STRING, 863 },{ 864 .name = "width", 865 .type = QEMU_OPT_NUMBER, 866 },{ 867 .name = "height", 868 .type = QEMU_OPT_NUMBER, 869 },{ 870 .name = "cols", 871 .type = QEMU_OPT_NUMBER, 872 },{ 873 .name = "rows", 874 .type = QEMU_OPT_NUMBER, 875 },{ 876 .name = "mux", 877 .type = QEMU_OPT_BOOL, 878 },{ 879 .name = "signal", 880 .type = QEMU_OPT_BOOL, 881 },{ 882 .name = "name", 883 .type = QEMU_OPT_STRING, 884 },{ 885 .name = "debug", 886 .type = QEMU_OPT_NUMBER, 887 },{ 888 .name = "size", 889 .type = QEMU_OPT_SIZE, 890 },{ 891 .name = "chardev", 892 .type = QEMU_OPT_STRING, 893 },{ 894 .name = "append", 895 .type = QEMU_OPT_BOOL, 896 },{ 897 .name = "logfile", 898 .type = QEMU_OPT_STRING, 899 },{ 900 .name = "logappend", 901 .type = QEMU_OPT_BOOL, 902 }, 903 { /* end of list */ } 904 }, 905 }; 906 907 bool qemu_chr_has_feature(Chardev *chr, 908 ChardevFeature feature) 909 { 910 return test_bit(feature, chr->features); 911 } 912 913 void qemu_chr_set_feature(Chardev *chr, 914 ChardevFeature feature) 915 { 916 return set_bit(feature, chr->features); 917 } 918 919 Chardev *qemu_chardev_new(const char *id, const char *typename, 920 ChardevBackend *backend, 921 Error **errp) 922 { 923 Object *obj; 924 Chardev *chr = NULL; 925 Error *local_err = NULL; 926 bool be_opened = true; 927 928 assert(g_str_has_prefix(typename, "chardev-")); 929 930 obj = object_new(typename); 931 chr = CHARDEV(obj); 932 chr->label = g_strdup(id); 933 934 qemu_char_open(chr, backend, &be_opened, &local_err); 935 if (local_err) { 936 goto end; 937 } 938 939 if (!chr->filename) { 940 chr->filename = g_strdup(typename + 8); 941 } 942 if (be_opened) { 943 qemu_chr_be_event(chr, CHR_EVENT_OPENED); 944 } 945 946 if (id) { 947 object_property_add_child(get_chardevs_root(), id, obj, &local_err); 948 if (local_err) { 949 goto end; 950 } 951 object_unref(obj); 952 } 953 954 end: 955 if (local_err) { 956 error_propagate(errp, local_err); 957 object_unref(obj); 958 return NULL; 959 } 960 961 return chr; 962 } 963 964 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, 965 Error **errp) 966 { 967 const ChardevClass *cc; 968 ChardevReturn *ret; 969 Chardev *chr; 970 971 cc = char_get_class(ChardevBackendKind_str(backend->type), errp); 972 if (!cc) { 973 return NULL; 974 } 975 976 chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)), 977 backend, errp); 978 if (!chr) { 979 return NULL; 980 } 981 982 ret = g_new0(ChardevReturn, 1); 983 if (CHARDEV_IS_PTY(chr)) { 984 ret->pty = g_strdup(chr->filename + 4); 985 ret->has_pty = true; 986 } 987 988 return ret; 989 } 990 991 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, 992 Error **errp) 993 { 994 CharBackend *be; 995 const ChardevClass *cc; 996 Chardev *chr, *chr_new; 997 bool closed_sent = false; 998 ChardevReturn *ret; 999 1000 chr = qemu_chr_find(id); 1001 if (!chr) { 1002 error_setg(errp, "Chardev '%s' does not exist", id); 1003 return NULL; 1004 } 1005 1006 if (CHARDEV_IS_MUX(chr)) { 1007 error_setg(errp, "Mux device hotswap not supported yet"); 1008 return NULL; 1009 } 1010 1011 if (qemu_chr_replay(chr)) { 1012 error_setg(errp, 1013 "Chardev '%s' cannot be changed in record/replay mode", id); 1014 return NULL; 1015 } 1016 1017 be = chr->be; 1018 if (!be) { 1019 /* easy case */ 1020 object_unparent(OBJECT(chr)); 1021 return qmp_chardev_add(id, backend, errp); 1022 } 1023 1024 if (!be->chr_be_change) { 1025 error_setg(errp, "Chardev user does not support chardev hotswap"); 1026 return NULL; 1027 } 1028 1029 cc = char_get_class(ChardevBackendKind_str(backend->type), errp); 1030 if (!cc) { 1031 return NULL; 1032 } 1033 1034 chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)), 1035 backend, errp); 1036 if (!chr_new) { 1037 return NULL; 1038 } 1039 chr_new->label = g_strdup(id); 1040 1041 if (chr->be_open && !chr_new->be_open) { 1042 qemu_chr_be_event(chr, CHR_EVENT_CLOSED); 1043 closed_sent = true; 1044 } 1045 1046 chr->be = NULL; 1047 qemu_chr_fe_init(be, chr_new, &error_abort); 1048 1049 if (be->chr_be_change(be->opaque) < 0) { 1050 error_setg(errp, "Chardev '%s' change failed", chr_new->label); 1051 chr_new->be = NULL; 1052 qemu_chr_fe_init(be, chr, &error_abort); 1053 if (closed_sent) { 1054 qemu_chr_be_event(chr, CHR_EVENT_OPENED); 1055 } 1056 object_unref(OBJECT(chr_new)); 1057 return NULL; 1058 } 1059 1060 object_unparent(OBJECT(chr)); 1061 object_property_add_child(get_chardevs_root(), chr_new->label, 1062 OBJECT(chr_new), &error_abort); 1063 object_unref(OBJECT(chr_new)); 1064 1065 ret = g_new0(ChardevReturn, 1); 1066 if (CHARDEV_IS_PTY(chr_new)) { 1067 ret->pty = g_strdup(chr_new->filename + 4); 1068 ret->has_pty = true; 1069 } 1070 1071 return ret; 1072 } 1073 1074 void qmp_chardev_remove(const char *id, Error **errp) 1075 { 1076 Chardev *chr; 1077 1078 chr = qemu_chr_find(id); 1079 if (chr == NULL) { 1080 error_setg(errp, "Chardev '%s' not found", id); 1081 return; 1082 } 1083 if (qemu_chr_is_busy(chr)) { 1084 error_setg(errp, "Chardev '%s' is busy", id); 1085 return; 1086 } 1087 if (qemu_chr_replay(chr)) { 1088 error_setg(errp, 1089 "Chardev '%s' cannot be unplugged in record/replay mode", id); 1090 return; 1091 } 1092 object_unparent(OBJECT(chr)); 1093 } 1094 1095 void qmp_chardev_send_break(const char *id, Error **errp) 1096 { 1097 Chardev *chr; 1098 1099 chr = qemu_chr_find(id); 1100 if (chr == NULL) { 1101 error_setg(errp, "Chardev '%s' not found", id); 1102 return; 1103 } 1104 qemu_chr_be_event(chr, CHR_EVENT_BREAK); 1105 } 1106 1107 /* 1108 * Add a timeout callback for the chardev (in milliseconds), return 1109 * the GSource object created. Please use this to add timeout hook for 1110 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to 1111 * make sure the gcontext that the task bound to is correct. 1112 */ 1113 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms, 1114 GSourceFunc func, void *private) 1115 { 1116 GSource *source = g_timeout_source_new(ms); 1117 1118 assert(func); 1119 g_source_set_callback(source, func, private, NULL); 1120 g_source_attach(source, chr->gcontext); 1121 1122 return source; 1123 } 1124 1125 void qemu_chr_cleanup(void) 1126 { 1127 object_unparent(get_chardevs_root()); 1128 } 1129 1130 static void register_types(void) 1131 { 1132 type_register_static(&char_type_info); 1133 1134 /* this must be done after machine init, since we register FEs with muxes 1135 * as part of realize functions like serial_isa_realizefn when -nographic 1136 * is specified 1137 */ 1138 qemu_add_machine_init_done_notifier(&chardev_machine_done_notify); 1139 } 1140 1141 type_init(register_types); 1142