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