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