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