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