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 #include "qemu/osdep.h" 25 #include "qemu-common.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 "sysemu/char.h" 32 #include "qmp-commands.h" 33 #include "qapi-visit.h" 34 #include "sysemu/replay.h" 35 #include "qemu/help_option.h" 36 37 #include "char-mux.h" 38 #include "char-io.h" 39 #include "char-parallel.h" 40 #include "char-serial.h" 41 42 /***********************************************************/ 43 /* character device */ 44 45 static QTAILQ_HEAD(ChardevHead, Chardev) chardevs = 46 QTAILQ_HEAD_INITIALIZER(chardevs); 47 48 void qemu_chr_be_event(Chardev *s, int event) 49 { 50 CharBackend *be = s->be; 51 52 /* Keep track if the char device is open */ 53 switch (event) { 54 case CHR_EVENT_OPENED: 55 s->be_open = 1; 56 break; 57 case CHR_EVENT_CLOSED: 58 s->be_open = 0; 59 break; 60 } 61 62 if (!be || !be->chr_event) { 63 return; 64 } 65 66 be->chr_event(be->opaque, event); 67 } 68 69 void qemu_chr_be_generic_open(Chardev *s) 70 { 71 qemu_chr_be_event(s, CHR_EVENT_OPENED); 72 } 73 74 75 /* Not reporting errors from writing to logfile, as logs are 76 * defined to be "best effort" only */ 77 static void qemu_chr_fe_write_log(Chardev *s, 78 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_fe_write_buffer(Chardev *s, 103 const uint8_t *buf, int len, int *offset) 104 { 105 ChardevClass *cc = CHARDEV_GET_CLASS(s); 106 int res = 0; 107 *offset = 0; 108 109 qemu_mutex_lock(&s->chr_write_lock); 110 while (*offset < len) { 111 retry: 112 res = cc->chr_write(s, buf + *offset, len - *offset); 113 if (res < 0 && errno == EAGAIN) { 114 g_usleep(100); 115 goto retry; 116 } 117 118 if (res <= 0) { 119 break; 120 } 121 122 *offset += res; 123 } 124 if (*offset > 0) { 125 qemu_chr_fe_write_log(s, buf, *offset); 126 } 127 qemu_mutex_unlock(&s->chr_write_lock); 128 129 return res; 130 } 131 132 static bool qemu_chr_replay(Chardev *chr) 133 { 134 return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY); 135 } 136 137 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) 138 { 139 Chardev *s = be->chr; 140 ChardevClass *cc; 141 int ret; 142 143 if (!s) { 144 return 0; 145 } 146 147 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { 148 int offset; 149 replay_char_write_event_load(&ret, &offset); 150 assert(offset <= len); 151 qemu_chr_fe_write_buffer(s, buf, offset, &offset); 152 return ret; 153 } 154 155 cc = CHARDEV_GET_CLASS(s); 156 qemu_mutex_lock(&s->chr_write_lock); 157 ret = cc->chr_write(s, buf, len); 158 159 if (ret > 0) { 160 qemu_chr_fe_write_log(s, buf, ret); 161 } 162 163 qemu_mutex_unlock(&s->chr_write_lock); 164 165 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { 166 replay_char_write_event_save(ret, ret < 0 ? 0 : ret); 167 } 168 169 return ret; 170 } 171 172 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len) 173 { 174 int offset; 175 int res; 176 177 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { 178 replay_char_write_event_load(&res, &offset); 179 assert(offset <= len); 180 qemu_chr_fe_write_buffer(s, buf, offset, &offset); 181 return res; 182 } 183 184 res = qemu_chr_fe_write_buffer(s, buf, len, &offset); 185 186 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { 187 replay_char_write_event_save(res, offset); 188 } 189 190 if (res < 0) { 191 return res; 192 } 193 return offset; 194 } 195 196 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len) 197 { 198 Chardev *s = be->chr; 199 200 if (!s) { 201 return 0; 202 } 203 204 return qemu_chr_write_all(s, buf, len); 205 } 206 207 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) 208 { 209 Chardev *s = be->chr; 210 int offset = 0, counter = 10; 211 int res; 212 213 if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) { 214 return 0; 215 } 216 217 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { 218 return replay_char_read_all_load(buf); 219 } 220 221 while (offset < len) { 222 retry: 223 res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset, 224 len - offset); 225 if (res == -1 && errno == EAGAIN) { 226 g_usleep(100); 227 goto retry; 228 } 229 230 if (res == 0) { 231 break; 232 } 233 234 if (res < 0) { 235 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { 236 replay_char_read_all_save_error(res); 237 } 238 return res; 239 } 240 241 offset += res; 242 243 if (!counter--) { 244 break; 245 } 246 } 247 248 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { 249 replay_char_read_all_save_buf(buf, offset); 250 } 251 return offset; 252 } 253 254 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg) 255 { 256 Chardev *s = be->chr; 257 int res; 258 259 if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) { 260 res = -ENOTSUP; 261 } else { 262 res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg); 263 } 264 265 return res; 266 } 267 268 int qemu_chr_be_can_write(Chardev *s) 269 { 270 CharBackend *be = s->be; 271 272 if (!be || !be->chr_can_read) { 273 return 0; 274 } 275 276 return be->chr_can_read(be->opaque); 277 } 278 279 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len) 280 { 281 CharBackend *be = s->be; 282 283 if (be && be->chr_read) { 284 be->chr_read(be->opaque, buf, len); 285 } 286 } 287 288 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len) 289 { 290 if (qemu_chr_replay(s)) { 291 if (replay_mode == REPLAY_MODE_PLAY) { 292 return; 293 } 294 replay_chr_be_write(s, buf, len); 295 } else { 296 qemu_chr_be_write_impl(s, buf, len); 297 } 298 } 299 300 int qemu_chr_fe_get_msgfd(CharBackend *be) 301 { 302 Chardev *s = be->chr; 303 int fd; 304 int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1; 305 if (s && qemu_chr_replay(s)) { 306 error_report("Replay: get msgfd is not supported " 307 "for serial devices yet"); 308 exit(1); 309 } 310 return res; 311 } 312 313 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len) 314 { 315 Chardev *s = be->chr; 316 317 if (!s) { 318 return -1; 319 } 320 321 return CHARDEV_GET_CLASS(s)->get_msgfds ? 322 CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1; 323 } 324 325 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num) 326 { 327 Chardev *s = be->chr; 328 329 if (!s) { 330 return -1; 331 } 332 333 return CHARDEV_GET_CLASS(s)->set_msgfds ? 334 CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1; 335 } 336 337 int qemu_chr_add_client(Chardev *s, int fd) 338 { 339 return CHARDEV_GET_CLASS(s)->chr_add_client ? 340 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1; 341 } 342 343 void qemu_chr_fe_accept_input(CharBackend *be) 344 { 345 Chardev *s = be->chr; 346 347 if (!s) { 348 return; 349 } 350 351 if (CHARDEV_GET_CLASS(s)->chr_accept_input) { 352 CHARDEV_GET_CLASS(s)->chr_accept_input(s); 353 } 354 qemu_notify_event(); 355 } 356 357 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) 358 { 359 char buf[CHR_READ_BUF_LEN]; 360 va_list ap; 361 va_start(ap, fmt); 362 vsnprintf(buf, sizeof(buf), fmt, ap); 363 /* XXX this blocks entire thread. Rewrite to use 364 * qemu_chr_fe_write and background I/O callbacks */ 365 qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf)); 366 va_end(ap); 367 } 368 369 static void qemu_char_open(Chardev *chr, ChardevBackend *backend, 370 bool *be_opened, Error **errp) 371 { 372 ChardevClass *cc = CHARDEV_GET_CLASS(chr); 373 /* Any ChardevCommon member would work */ 374 ChardevCommon *common = backend ? backend->u.null.data : NULL; 375 376 if (common && common->has_logfile) { 377 int flags = O_WRONLY | O_CREAT; 378 if (common->has_logappend && 379 common->logappend) { 380 flags |= O_APPEND; 381 } else { 382 flags |= O_TRUNC; 383 } 384 chr->logfd = qemu_open(common->logfile, flags, 0666); 385 if (chr->logfd < 0) { 386 error_setg_errno(errp, errno, 387 "Unable to open logfile %s", 388 common->logfile); 389 return; 390 } 391 } 392 393 if (cc->open) { 394 cc->open(chr, backend, be_opened, errp); 395 } 396 } 397 398 static void char_init(Object *obj) 399 { 400 Chardev *chr = CHARDEV(obj); 401 402 chr->logfd = -1; 403 qemu_mutex_init(&chr->chr_write_lock); 404 } 405 406 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len) 407 { 408 return len; 409 } 410 411 static void char_class_init(ObjectClass *oc, void *data) 412 { 413 ChardevClass *cc = CHARDEV_CLASS(oc); 414 415 cc->chr_write = null_chr_write; 416 } 417 418 static void char_finalize(Object *obj) 419 { 420 Chardev *chr = CHARDEV(obj); 421 422 if (chr->be) { 423 chr->be->chr = NULL; 424 } 425 g_free(chr->filename); 426 g_free(chr->label); 427 if (chr->logfd != -1) { 428 close(chr->logfd); 429 } 430 qemu_mutex_destroy(&chr->chr_write_lock); 431 } 432 433 static const TypeInfo char_type_info = { 434 .name = TYPE_CHARDEV, 435 .parent = TYPE_OBJECT, 436 .instance_size = sizeof(Chardev), 437 .instance_init = char_init, 438 .instance_finalize = char_finalize, 439 .abstract = true, 440 .class_size = sizeof(ChardevClass), 441 .class_init = char_class_init, 442 }; 443 444 /** 445 * Called after processing of default and command-line-specified 446 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached 447 * to a mux chardev. This is done here to ensure that 448 * output/prompts/banners are only displayed for the FE that has 449 * focus when initial command-line processing/machine init is 450 * completed. 451 * 452 * After this point, any new FE attached to any new or existing 453 * mux will receive CHR_EVENT_OPENED notifications for the BE 454 * immediately. 455 */ 456 static void muxes_realize_done(Notifier *notifier, void *unused) 457 { 458 Chardev *chr; 459 460 QTAILQ_FOREACH(chr, &chardevs, next) { 461 if (CHARDEV_IS_MUX(chr)) { 462 MuxChardev *d = MUX_CHARDEV(chr); 463 int i; 464 465 /* send OPENED to all already-attached FEs */ 466 for (i = 0; i < d->mux_cnt; i++) { 467 mux_chr_send_event(d, i, CHR_EVENT_OPENED); 468 } 469 /* mark mux as OPENED so any new FEs will immediately receive 470 * OPENED event 471 */ 472 qemu_chr_be_generic_open(chr); 473 } 474 } 475 muxes_realized = true; 476 } 477 478 static Notifier muxes_realize_notify = { 479 .notify = muxes_realize_done, 480 }; 481 482 Chardev *qemu_chr_fe_get_driver(CharBackend *be) 483 { 484 return be->chr; 485 } 486 487 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) 488 { 489 int tag = 0; 490 491 if (CHARDEV_IS_MUX(s)) { 492 MuxChardev *d = MUX_CHARDEV(s); 493 494 if (d->mux_cnt >= MAX_MUX) { 495 goto unavailable; 496 } 497 498 d->backends[d->mux_cnt] = b; 499 tag = d->mux_cnt++; 500 } else if (s->be) { 501 goto unavailable; 502 } else { 503 s->be = b; 504 } 505 506 b->fe_open = false; 507 b->tag = tag; 508 b->chr = s; 509 return true; 510 511 unavailable: 512 error_setg(errp, QERR_DEVICE_IN_USE, s->label); 513 return false; 514 } 515 516 static bool qemu_chr_is_busy(Chardev *s) 517 { 518 if (CHARDEV_IS_MUX(s)) { 519 MuxChardev *d = MUX_CHARDEV(s); 520 return d->mux_cnt >= 0; 521 } else { 522 return s->be != NULL; 523 } 524 } 525 526 void qemu_chr_fe_deinit(CharBackend *b) 527 { 528 assert(b); 529 530 if (b->chr) { 531 qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true); 532 if (b->chr->be == b) { 533 b->chr->be = NULL; 534 } 535 if (CHARDEV_IS_MUX(b->chr)) { 536 MuxChardev *d = MUX_CHARDEV(b->chr); 537 d->backends[b->tag] = NULL; 538 } 539 b->chr = NULL; 540 } 541 } 542 543 void qemu_chr_fe_set_handlers(CharBackend *b, 544 IOCanReadHandler *fd_can_read, 545 IOReadHandler *fd_read, 546 IOEventHandler *fd_event, 547 void *opaque, 548 GMainContext *context, 549 bool set_open) 550 { 551 Chardev *s; 552 ChardevClass *cc; 553 int fe_open; 554 555 s = b->chr; 556 if (!s) { 557 return; 558 } 559 560 cc = CHARDEV_GET_CLASS(s); 561 if (!opaque && !fd_can_read && !fd_read && !fd_event) { 562 fe_open = 0; 563 remove_fd_in_watch(s); 564 } else { 565 fe_open = 1; 566 } 567 b->chr_can_read = fd_can_read; 568 b->chr_read = fd_read; 569 b->chr_event = fd_event; 570 b->opaque = opaque; 571 if (cc->chr_update_read_handler) { 572 cc->chr_update_read_handler(s, context); 573 } 574 575 if (set_open) { 576 qemu_chr_fe_set_open(b, fe_open); 577 } 578 579 if (fe_open) { 580 qemu_chr_fe_take_focus(b); 581 /* We're connecting to an already opened device, so let's make sure we 582 also get the open event */ 583 if (s->be_open) { 584 qemu_chr_be_generic_open(s); 585 } 586 } 587 588 if (CHARDEV_IS_MUX(s)) { 589 mux_chr_set_handlers(s, context); 590 } 591 } 592 593 void qemu_chr_fe_take_focus(CharBackend *b) 594 { 595 if (!b->chr) { 596 return; 597 } 598 599 if (CHARDEV_IS_MUX(b->chr)) { 600 mux_set_focus(b->chr, b->tag); 601 } 602 } 603 604 int qemu_chr_wait_connected(Chardev *chr, Error **errp) 605 { 606 ChardevClass *cc = CHARDEV_GET_CLASS(chr); 607 608 if (cc->chr_wait_connected) { 609 return cc->chr_wait_connected(chr, errp); 610 } 611 612 return 0; 613 } 614 615 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp) 616 { 617 if (!be->chr) { 618 error_setg(errp, "missing associated backend"); 619 return -1; 620 } 621 622 return qemu_chr_wait_connected(be->chr, errp); 623 } 624 625 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) 626 { 627 char host[65], port[33], width[8], height[8]; 628 int pos; 629 const char *p; 630 QemuOpts *opts; 631 Error *local_err = NULL; 632 633 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); 634 if (local_err) { 635 error_report_err(local_err); 636 return NULL; 637 } 638 639 if (strstart(filename, "mon:", &p)) { 640 filename = p; 641 qemu_opt_set(opts, "mux", "on", &error_abort); 642 if (strcmp(filename, "stdio") == 0) { 643 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default 644 * but pass it to the guest. Handle this only for compat syntax, 645 * for -chardev syntax we have special option for this. 646 * This is what -nographic did, redirecting+muxing serial+monitor 647 * to stdio causing Ctrl+C to be passed to guest. */ 648 qemu_opt_set(opts, "signal", "off", &error_abort); 649 } 650 } 651 652 if (strcmp(filename, "null") == 0 || 653 strcmp(filename, "pty") == 0 || 654 strcmp(filename, "msmouse") == 0 || 655 strcmp(filename, "wctablet") == 0 || 656 strcmp(filename, "braille") == 0 || 657 strcmp(filename, "testdev") == 0 || 658 strcmp(filename, "stdio") == 0) { 659 qemu_opt_set(opts, "backend", filename, &error_abort); 660 return opts; 661 } 662 if (strstart(filename, "vc", &p)) { 663 qemu_opt_set(opts, "backend", "vc", &error_abort); 664 if (*p == ':') { 665 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { 666 /* pixels */ 667 qemu_opt_set(opts, "width", width, &error_abort); 668 qemu_opt_set(opts, "height", height, &error_abort); 669 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { 670 /* chars */ 671 qemu_opt_set(opts, "cols", width, &error_abort); 672 qemu_opt_set(opts, "rows", height, &error_abort); 673 } else { 674 goto fail; 675 } 676 } 677 return opts; 678 } 679 if (strcmp(filename, "con:") == 0) { 680 qemu_opt_set(opts, "backend", "console", &error_abort); 681 return opts; 682 } 683 if (strstart(filename, "COM", NULL)) { 684 qemu_opt_set(opts, "backend", "serial", &error_abort); 685 qemu_opt_set(opts, "path", filename, &error_abort); 686 return opts; 687 } 688 if (strstart(filename, "file:", &p)) { 689 qemu_opt_set(opts, "backend", "file", &error_abort); 690 qemu_opt_set(opts, "path", p, &error_abort); 691 return opts; 692 } 693 if (strstart(filename, "pipe:", &p)) { 694 qemu_opt_set(opts, "backend", "pipe", &error_abort); 695 qemu_opt_set(opts, "path", p, &error_abort); 696 return opts; 697 } 698 if (strstart(filename, "tcp:", &p) || 699 strstart(filename, "telnet:", &p)) { 700 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { 701 host[0] = 0; 702 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) 703 goto fail; 704 } 705 qemu_opt_set(opts, "backend", "socket", &error_abort); 706 qemu_opt_set(opts, "host", host, &error_abort); 707 qemu_opt_set(opts, "port", port, &error_abort); 708 if (p[pos] == ',') { 709 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); 710 if (local_err) { 711 error_report_err(local_err); 712 goto fail; 713 } 714 } 715 if (strstart(filename, "telnet:", &p)) 716 qemu_opt_set(opts, "telnet", "on", &error_abort); 717 return opts; 718 } 719 if (strstart(filename, "udp:", &p)) { 720 qemu_opt_set(opts, "backend", "udp", &error_abort); 721 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { 722 host[0] = 0; 723 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { 724 goto fail; 725 } 726 } 727 qemu_opt_set(opts, "host", host, &error_abort); 728 qemu_opt_set(opts, "port", port, &error_abort); 729 if (p[pos] == '@') { 730 p += pos + 1; 731 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { 732 host[0] = 0; 733 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { 734 goto fail; 735 } 736 } 737 qemu_opt_set(opts, "localaddr", host, &error_abort); 738 qemu_opt_set(opts, "localport", port, &error_abort); 739 } 740 return opts; 741 } 742 if (strstart(filename, "unix:", &p)) { 743 qemu_opt_set(opts, "backend", "socket", &error_abort); 744 qemu_opts_do_parse(opts, p, "path", &local_err); 745 if (local_err) { 746 error_report_err(local_err); 747 goto fail; 748 } 749 return opts; 750 } 751 if (strstart(filename, "/dev/parport", NULL) || 752 strstart(filename, "/dev/ppi", NULL)) { 753 qemu_opt_set(opts, "backend", "parport", &error_abort); 754 qemu_opt_set(opts, "path", filename, &error_abort); 755 return opts; 756 } 757 if (strstart(filename, "/dev/", NULL)) { 758 qemu_opt_set(opts, "backend", "tty", &error_abort); 759 qemu_opt_set(opts, "path", filename, &error_abort); 760 return opts; 761 } 762 763 fail: 764 qemu_opts_del(opts); 765 return NULL; 766 } 767 768 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) 769 { 770 const char *logfile = qemu_opt_get(opts, "logfile"); 771 772 backend->has_logfile = logfile != NULL; 773 backend->logfile = logfile ? g_strdup(logfile) : NULL; 774 775 backend->has_logappend = true; 776 backend->logappend = qemu_opt_get_bool(opts, "logappend", false); 777 } 778 779 static const ChardevClass *char_get_class(const char *driver, Error **errp) 780 { 781 ObjectClass *oc; 782 const ChardevClass *cc; 783 char *typename = g_strdup_printf("chardev-%s", driver); 784 785 oc = object_class_by_name(typename); 786 g_free(typename); 787 788 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) { 789 error_setg(errp, "'%s' is not a valid char driver name", driver); 790 return NULL; 791 } 792 793 if (object_class_is_abstract(oc)) { 794 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", 795 "abstract device type"); 796 return NULL; 797 } 798 799 cc = CHARDEV_CLASS(oc); 800 if (cc->internal) { 801 error_setg(errp, "'%s' is not a valid char driver name", driver); 802 return NULL; 803 } 804 805 return cc; 806 } 807 808 static Chardev *qemu_chardev_add(const char *id, const char *typename, 809 ChardevBackend *backend, Error **errp) 810 { 811 Chardev *chr; 812 813 chr = qemu_chr_find(id); 814 if (chr) { 815 error_setg(errp, "Chardev '%s' already exists", id); 816 return NULL; 817 } 818 819 chr = qemu_chardev_new(id, typename, backend, errp); 820 if (!chr) { 821 return NULL; 822 } 823 824 QTAILQ_INSERT_TAIL(&chardevs, chr, next); 825 return chr; 826 } 827 828 static const struct ChardevAlias { 829 const char *typename; 830 const char *alias; 831 } chardev_alias_table[] = { 832 #ifdef HAVE_CHARDEV_PARPORT 833 { "parallel", "parport" }, 834 #endif 835 #ifdef HAVE_CHARDEV_SERIAL 836 { "serial", "tty" }, 837 #endif 838 }; 839 840 typedef struct ChadevClassFE { 841 void (*fn)(const char *name, void *opaque); 842 void *opaque; 843 } ChadevClassFE; 844 845 static void 846 chardev_class_foreach(ObjectClass *klass, void *opaque) 847 { 848 ChadevClassFE *fe = opaque; 849 850 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-")); 851 if (CHARDEV_CLASS(klass)->internal) { 852 return; 853 } 854 855 fe->fn(object_class_get_name(klass) + 8, fe->opaque); 856 } 857 858 static void 859 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque) 860 { 861 ChadevClassFE fe = { .fn = fn, .opaque = opaque }; 862 int i; 863 864 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe); 865 866 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) { 867 fn(chardev_alias_table[i].alias, opaque); 868 } 869 } 870 871 static void 872 help_string_append(const char *name, void *opaque) 873 { 874 GString *str = opaque; 875 876 g_string_append_printf(str, "\n%s", name); 877 } 878 879 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, 880 Error **errp) 881 { 882 Error *local_err = NULL; 883 const ChardevClass *cc; 884 Chardev *chr; 885 int i; 886 ChardevBackend *backend = NULL; 887 const char *name = qemu_opt_get(opts, "backend"); 888 const char *id = qemu_opts_id(opts); 889 char *bid = NULL; 890 891 if (name == NULL) { 892 error_setg(errp, "chardev: \"%s\" missing backend", 893 qemu_opts_id(opts)); 894 return NULL; 895 } 896 897 if (is_help_option(name)) { 898 GString *str = g_string_new(""); 899 900 chardev_name_foreach(help_string_append, str); 901 902 error_report("Available chardev backend types: %s", str->str); 903 g_string_free(str, true); 904 exit(0); 905 } 906 907 if (id == NULL) { 908 error_setg(errp, "chardev: no id specified"); 909 return NULL; 910 } 911 912 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) { 913 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) { 914 name = chardev_alias_table[i].typename; 915 break; 916 } 917 } 918 919 cc = char_get_class(name, errp); 920 if (cc == NULL) { 921 return NULL; 922 } 923 924 backend = g_new0(ChardevBackend, 1); 925 backend->type = CHARDEV_BACKEND_KIND_NULL; 926 927 if (qemu_opt_get_bool(opts, "mux", 0)) { 928 bid = g_strdup_printf("%s-base", id); 929 } 930 931 chr = NULL; 932 if (cc->parse) { 933 cc->parse(opts, backend, &local_err); 934 if (local_err) { 935 error_propagate(errp, local_err); 936 goto out; 937 } 938 } else { 939 ChardevCommon *ccom = g_new0(ChardevCommon, 1); 940 qemu_chr_parse_common(opts, ccom); 941 backend->u.null.data = ccom; /* Any ChardevCommon member would work */ 942 } 943 944 chr = qemu_chardev_add(bid ? bid : id, 945 object_class_get_name(OBJECT_CLASS(cc)), 946 backend, errp); 947 if (chr == NULL) { 948 goto out; 949 } 950 951 if (bid) { 952 Chardev *mux; 953 qapi_free_ChardevBackend(backend); 954 backend = g_new0(ChardevBackend, 1); 955 backend->type = CHARDEV_BACKEND_KIND_MUX; 956 backend->u.mux.data = g_new0(ChardevMux, 1); 957 backend->u.mux.data->chardev = g_strdup(bid); 958 mux = qemu_chardev_add(id, TYPE_CHARDEV_MUX, backend, errp); 959 if (mux == NULL) { 960 qemu_chr_delete(chr); 961 chr = NULL; 962 goto out; 963 } 964 chr = mux; 965 } 966 967 out: 968 qapi_free_ChardevBackend(backend); 969 g_free(bid); 970 return chr; 971 } 972 973 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename) 974 { 975 const char *p; 976 Chardev *chr; 977 QemuOpts *opts; 978 Error *err = NULL; 979 980 if (strstart(filename, "chardev:", &p)) { 981 return qemu_chr_find(p); 982 } 983 984 opts = qemu_chr_parse_compat(label, filename); 985 if (!opts) 986 return NULL; 987 988 chr = qemu_chr_new_from_opts(opts, &err); 989 if (err) { 990 error_report_err(err); 991 } 992 if (chr && qemu_opt_get_bool(opts, "mux", 0)) { 993 monitor_init(chr, MONITOR_USE_READLINE); 994 } 995 qemu_opts_del(opts); 996 return chr; 997 } 998 999 Chardev *qemu_chr_new(const char *label, const char *filename) 1000 { 1001 Chardev *chr; 1002 chr = qemu_chr_new_noreplay(label, filename); 1003 if (chr) { 1004 if (replay_mode != REPLAY_MODE_NONE) { 1005 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY); 1006 } 1007 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) { 1008 error_report("Replay: ioctl is not supported " 1009 "for serial devices yet"); 1010 } 1011 replay_register_char_driver(chr); 1012 } 1013 return chr; 1014 } 1015 1016 void qemu_chr_fe_set_echo(CharBackend *be, bool echo) 1017 { 1018 Chardev *chr = be->chr; 1019 1020 if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) { 1021 CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo); 1022 } 1023 } 1024 1025 void qemu_chr_fe_set_open(CharBackend *be, int fe_open) 1026 { 1027 Chardev *chr = be->chr; 1028 1029 if (!chr) { 1030 return; 1031 } 1032 1033 if (be->fe_open == fe_open) { 1034 return; 1035 } 1036 be->fe_open = fe_open; 1037 if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) { 1038 CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open); 1039 } 1040 } 1041 1042 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond, 1043 GIOFunc func, void *user_data) 1044 { 1045 Chardev *s = be->chr; 1046 GSource *src; 1047 guint tag; 1048 1049 if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) { 1050 return 0; 1051 } 1052 1053 src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond); 1054 if (!src) { 1055 return 0; 1056 } 1057 1058 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); 1059 tag = g_source_attach(src, NULL); 1060 g_source_unref(src); 1061 1062 return tag; 1063 } 1064 1065 void qemu_chr_fe_disconnect(CharBackend *be) 1066 { 1067 Chardev *chr = be->chr; 1068 1069 if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) { 1070 CHARDEV_GET_CLASS(chr)->chr_disconnect(chr); 1071 } 1072 } 1073 1074 void qemu_chr_delete(Chardev *chr) 1075 { 1076 QTAILQ_REMOVE(&chardevs, chr, next); 1077 object_unref(OBJECT(chr)); 1078 } 1079 1080 ChardevInfoList *qmp_query_chardev(Error **errp) 1081 { 1082 ChardevInfoList *chr_list = NULL; 1083 Chardev *chr; 1084 1085 QTAILQ_FOREACH(chr, &chardevs, next) { 1086 ChardevInfoList *info = g_malloc0(sizeof(*info)); 1087 info->value = g_malloc0(sizeof(*info->value)); 1088 info->value->label = g_strdup(chr->label); 1089 info->value->filename = g_strdup(chr->filename); 1090 info->value->frontend_open = chr->be && chr->be->fe_open; 1091 1092 info->next = chr_list; 1093 chr_list = info; 1094 } 1095 1096 return chr_list; 1097 } 1098 1099 static void 1100 qmp_prepend_backend(const char *name, void *opaque) 1101 { 1102 ChardevBackendInfoList **list = opaque; 1103 ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); 1104 1105 info->value = g_malloc0(sizeof(*info->value)); 1106 info->value->name = g_strdup(name); 1107 info->next = *list; 1108 *list = info; 1109 } 1110 1111 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) 1112 { 1113 ChardevBackendInfoList *backend_list = NULL; 1114 1115 chardev_name_foreach(qmp_prepend_backend, &backend_list); 1116 1117 return backend_list; 1118 } 1119 1120 Chardev *qemu_chr_find(const char *name) 1121 { 1122 Chardev *chr; 1123 1124 QTAILQ_FOREACH(chr, &chardevs, next) { 1125 if (strcmp(chr->label, name) != 0) 1126 continue; 1127 return chr; 1128 } 1129 return NULL; 1130 } 1131 1132 QemuOptsList qemu_chardev_opts = { 1133 .name = "chardev", 1134 .implied_opt_name = "backend", 1135 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), 1136 .desc = { 1137 { 1138 .name = "backend", 1139 .type = QEMU_OPT_STRING, 1140 },{ 1141 .name = "path", 1142 .type = QEMU_OPT_STRING, 1143 },{ 1144 .name = "host", 1145 .type = QEMU_OPT_STRING, 1146 },{ 1147 .name = "port", 1148 .type = QEMU_OPT_STRING, 1149 },{ 1150 .name = "localaddr", 1151 .type = QEMU_OPT_STRING, 1152 },{ 1153 .name = "localport", 1154 .type = QEMU_OPT_STRING, 1155 },{ 1156 .name = "to", 1157 .type = QEMU_OPT_NUMBER, 1158 },{ 1159 .name = "ipv4", 1160 .type = QEMU_OPT_BOOL, 1161 },{ 1162 .name = "ipv6", 1163 .type = QEMU_OPT_BOOL, 1164 },{ 1165 .name = "wait", 1166 .type = QEMU_OPT_BOOL, 1167 },{ 1168 .name = "server", 1169 .type = QEMU_OPT_BOOL, 1170 },{ 1171 .name = "delay", 1172 .type = QEMU_OPT_BOOL, 1173 },{ 1174 .name = "reconnect", 1175 .type = QEMU_OPT_NUMBER, 1176 },{ 1177 .name = "telnet", 1178 .type = QEMU_OPT_BOOL, 1179 },{ 1180 .name = "tls-creds", 1181 .type = QEMU_OPT_STRING, 1182 },{ 1183 .name = "width", 1184 .type = QEMU_OPT_NUMBER, 1185 },{ 1186 .name = "height", 1187 .type = QEMU_OPT_NUMBER, 1188 },{ 1189 .name = "cols", 1190 .type = QEMU_OPT_NUMBER, 1191 },{ 1192 .name = "rows", 1193 .type = QEMU_OPT_NUMBER, 1194 },{ 1195 .name = "mux", 1196 .type = QEMU_OPT_BOOL, 1197 },{ 1198 .name = "signal", 1199 .type = QEMU_OPT_BOOL, 1200 },{ 1201 .name = "name", 1202 .type = QEMU_OPT_STRING, 1203 },{ 1204 .name = "debug", 1205 .type = QEMU_OPT_NUMBER, 1206 },{ 1207 .name = "size", 1208 .type = QEMU_OPT_SIZE, 1209 },{ 1210 .name = "chardev", 1211 .type = QEMU_OPT_STRING, 1212 },{ 1213 .name = "append", 1214 .type = QEMU_OPT_BOOL, 1215 },{ 1216 .name = "logfile", 1217 .type = QEMU_OPT_STRING, 1218 },{ 1219 .name = "logappend", 1220 .type = QEMU_OPT_BOOL, 1221 }, 1222 { /* end of list */ } 1223 }, 1224 }; 1225 1226 bool qemu_chr_has_feature(Chardev *chr, 1227 ChardevFeature feature) 1228 { 1229 return test_bit(feature, chr->features); 1230 } 1231 1232 void qemu_chr_set_feature(Chardev *chr, 1233 ChardevFeature feature) 1234 { 1235 return set_bit(feature, chr->features); 1236 } 1237 1238 Chardev *qemu_chardev_new(const char *id, const char *typename, 1239 ChardevBackend *backend, Error **errp) 1240 { 1241 Chardev *chr = NULL; 1242 Error *local_err = NULL; 1243 bool be_opened = true; 1244 1245 assert(g_str_has_prefix(typename, "chardev-")); 1246 1247 chr = CHARDEV(object_new(typename)); 1248 chr->label = g_strdup(id); 1249 1250 qemu_char_open(chr, backend, &be_opened, &local_err); 1251 if (local_err) { 1252 error_propagate(errp, local_err); 1253 object_unref(OBJECT(chr)); 1254 return NULL; 1255 } 1256 1257 if (!chr->filename) { 1258 chr->filename = g_strdup(typename + 8); 1259 } 1260 if (be_opened) { 1261 qemu_chr_be_event(chr, CHR_EVENT_OPENED); 1262 } 1263 1264 return chr; 1265 } 1266 1267 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, 1268 Error **errp) 1269 { 1270 const ChardevClass *cc; 1271 ChardevReturn *ret; 1272 Chardev *chr; 1273 1274 cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp); 1275 if (!cc) { 1276 return NULL; 1277 } 1278 1279 chr = qemu_chardev_add(id, object_class_get_name(OBJECT_CLASS(cc)), 1280 backend, errp); 1281 if (!chr) { 1282 return NULL; 1283 } 1284 1285 ret = g_new0(ChardevReturn, 1); 1286 if (CHARDEV_IS_PTY(chr)) { 1287 ret->pty = g_strdup(chr->filename + 4); 1288 ret->has_pty = true; 1289 } 1290 1291 return ret; 1292 } 1293 1294 void qmp_chardev_remove(const char *id, Error **errp) 1295 { 1296 Chardev *chr; 1297 1298 chr = qemu_chr_find(id); 1299 if (chr == NULL) { 1300 error_setg(errp, "Chardev '%s' not found", id); 1301 return; 1302 } 1303 if (qemu_chr_is_busy(chr)) { 1304 error_setg(errp, "Chardev '%s' is busy", id); 1305 return; 1306 } 1307 if (qemu_chr_replay(chr)) { 1308 error_setg(errp, 1309 "Chardev '%s' cannot be unplugged in record/replay mode", id); 1310 return; 1311 } 1312 qemu_chr_delete(chr); 1313 } 1314 1315 void qemu_chr_cleanup(void) 1316 { 1317 Chardev *chr, *tmp; 1318 1319 QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) { 1320 qemu_chr_delete(chr); 1321 } 1322 } 1323 1324 static void register_types(void) 1325 { 1326 type_register_static(&char_type_info); 1327 1328 /* this must be done after machine init, since we register FEs with muxes 1329 * as part of realize functions like serial_isa_realizefn when -nographic 1330 * is specified 1331 */ 1332 qemu_add_machine_init_done_notifier(&muxes_realize_notify); 1333 } 1334 1335 type_init(register_types); 1336