Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20170613' into...
[qemu/ar7.git] / chardev / char.c
blobbcfc065d16f7567d0d82f81b7a902a97d070004f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
36 #include "chardev/char-mux.h"
38 /***********************************************************/
39 /* character device */
41 static Object *get_chardevs_root(void)
43 return container_get(object_get_root(), "/chardevs");
46 void qemu_chr_be_event(Chardev *s, int event)
48 CharBackend *be = s->be;
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;
60 if (!be || !be->chr_event) {
61 return;
64 be->chr_event(be->opaque, event);
67 /* Not reporting errors from writing to logfile, as logs are
68 * defined to be "best effort" only */
69 static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len)
71 size_t done = 0;
72 ssize_t ret;
74 if (s->logfd < 0) {
75 return;
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;
86 if (ret <= 0) {
87 return;
89 done += ret;
93 static int qemu_chr_write_buffer(Chardev *s,
94 const uint8_t *buf, int len,
95 int *offset, bool write_all)
97 ChardevClass *cc = CHARDEV_GET_CLASS(s);
98 int res = 0;
99 *offset = 0;
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;
110 if (res <= 0) {
111 break;
114 *offset += res;
115 if (!write_all) {
116 break;
119 if (*offset > 0) {
120 qemu_chr_write_log(s, buf, *offset);
122 qemu_mutex_unlock(&s->chr_write_lock);
124 return res;
127 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
129 int offset = 0;
130 int res;
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;
139 res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
141 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
142 replay_char_write_event_save(res, offset);
145 if (res < 0) {
146 return res;
148 return offset;
151 int qemu_chr_be_can_write(Chardev *s)
153 CharBackend *be = s->be;
155 if (!be || !be->chr_can_read) {
156 return 0;
159 return be->chr_can_read(be->opaque);
162 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
164 CharBackend *be = s->be;
166 if (be && be->chr_read) {
167 be->chr_read(be->opaque, buf, len);
171 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
173 if (qemu_chr_replay(s)) {
174 if (replay_mode == REPLAY_MODE_PLAY) {
175 return;
177 replay_chr_be_write(s, buf, len);
178 } else {
179 qemu_chr_be_write_impl(s, buf, len);
183 int qemu_chr_add_client(Chardev *s, int fd)
185 return CHARDEV_GET_CLASS(s)->chr_add_client ?
186 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
189 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
190 bool *be_opened, Error **errp)
192 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
193 /* Any ChardevCommon member would work */
194 ChardevCommon *common = backend ? backend->u.null.data : NULL;
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;
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;
213 if (cc->open) {
214 cc->open(chr, backend, be_opened, errp);
218 static void char_init(Object *obj)
220 Chardev *chr = CHARDEV(obj);
222 chr->logfd = -1;
223 qemu_mutex_init(&chr->chr_write_lock);
226 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
228 return len;
231 static void char_class_init(ObjectClass *oc, void *data)
233 ChardevClass *cc = CHARDEV_CLASS(oc);
235 cc->chr_write = null_chr_write;
238 static void char_finalize(Object *obj)
240 Chardev *chr = CHARDEV(obj);
242 if (chr->be) {
243 chr->be->chr = NULL;
245 g_free(chr->filename);
246 g_free(chr->label);
247 if (chr->logfd != -1) {
248 close(chr->logfd);
250 qemu_mutex_destroy(&chr->chr_write_lock);
253 static 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,
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.
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.
276 static int open_muxes(Object *child, void *opaque)
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
284 qemu_chr_be_event(CHARDEV(child), CHR_EVENT_OPENED);
287 return 0;
290 static void muxes_realize_done(Notifier *notifier, void *unused)
292 muxes_realized = true;
293 object_child_foreach(get_chardevs_root(), open_muxes, NULL);
296 static Notifier muxes_realize_notify = {
297 .notify = muxes_realize_done,
300 static bool qemu_chr_is_busy(Chardev *s)
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;
310 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
312 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
314 if (cc->chr_wait_connected) {
315 return cc->chr_wait_connected(chr, errp);
318 return 0;
321 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
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;
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;
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);
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;
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;
373 return opts;
375 if (strcmp(filename, "con:") == 0) {
376 qemu_opt_set(opts, "backend", "console", &error_abort);
377 return opts;
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;
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;
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;
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;
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;
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);
417 return opts;
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;
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;
437 qemu_opt_set(opts, "localaddr", host, &error_abort);
438 qemu_opt_set(opts, "localport", port, &error_abort);
440 return opts;
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;
449 return opts;
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;
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;
463 fail:
464 qemu_opts_del(opts);
465 return NULL;
468 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
470 const char *logfile = qemu_opt_get(opts, "logfile");
472 backend->has_logfile = logfile != NULL;
473 backend->logfile = g_strdup(logfile);
475 backend->has_logappend = true;
476 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
479 static const ChardevClass *char_get_class(const char *driver, Error **errp)
481 ObjectClass *oc;
482 const ChardevClass *cc;
483 char *typename = g_strdup_printf("chardev-%s", driver);
485 oc = object_class_by_name(typename);
486 g_free(typename);
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;
493 if (object_class_is_abstract(oc)) {
494 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
495 "abstract device type");
496 return NULL;
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;
505 return cc;
508 static 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
520 typedef struct ChadevClassFE {
521 void (*fn)(const char *name, void *opaque);
522 void *opaque;
523 } ChadevClassFE;
525 static void
526 chardev_class_foreach(ObjectClass *klass, void *opaque)
528 ChadevClassFE *fe = opaque;
530 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
531 if (CHARDEV_CLASS(klass)->internal) {
532 return;
535 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
538 static void
539 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
541 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
542 int i;
544 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
546 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
547 fn(chardev_alias_table[i].alias, opaque);
551 static void
552 help_string_append(const char *name, void *opaque)
554 GString *str = opaque;
556 g_string_append_printf(str, "\n%s", name);
559 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
560 Error **errp)
562 Error *local_err = NULL;
563 const ChardevClass *cc;
564 Chardev *chr;
565 int i;
566 ChardevBackend *backend = NULL;
567 const char *name = qemu_opt_get(opts, "backend");
568 const char *id = qemu_opts_id(opts);
569 char *bid = NULL;
571 if (name == NULL) {
572 error_setg(errp, "chardev: \"%s\" missing backend",
573 qemu_opts_id(opts));
574 return NULL;
577 if (is_help_option(name)) {
578 GString *str = g_string_new("");
580 chardev_name_foreach(help_string_append, str);
582 error_report("Available chardev backend types: %s", str->str);
583 g_string_free(str, true);
584 exit(0);
587 if (id == NULL) {
588 error_setg(errp, "chardev: no id specified");
589 return NULL;
592 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
593 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
594 name = chardev_alias_table[i].typename;
595 break;
599 cc = char_get_class(name, errp);
600 if (cc == NULL) {
601 return NULL;
604 backend = g_new0(ChardevBackend, 1);
605 backend->type = CHARDEV_BACKEND_KIND_NULL;
607 if (qemu_opt_get_bool(opts, "mux", 0)) {
608 bid = g_strdup_printf("%s-base", id);
611 chr = NULL;
612 if (cc->parse) {
613 cc->parse(opts, backend, &local_err);
614 if (local_err) {
615 error_propagate(errp, local_err);
616 goto out;
618 } else {
619 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
620 qemu_chr_parse_common(opts, ccom);
621 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
624 chr = qemu_chardev_new(bid ? bid : id,
625 object_class_get_name(OBJECT_CLASS(cc)),
626 backend, errp);
628 if (chr == NULL) {
629 goto out;
632 if (bid) {
633 Chardev *mux;
634 qapi_free_ChardevBackend(backend);
635 backend = g_new0(ChardevBackend, 1);
636 backend->type = CHARDEV_BACKEND_KIND_MUX;
637 backend->u.mux.data = g_new0(ChardevMux, 1);
638 backend->u.mux.data->chardev = g_strdup(bid);
639 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp);
640 if (mux == NULL) {
641 object_unparent(OBJECT(chr));
642 chr = NULL;
643 goto out;
645 chr = mux;
648 out:
649 qapi_free_ChardevBackend(backend);
650 g_free(bid);
651 return chr;
654 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename)
656 const char *p;
657 Chardev *chr;
658 QemuOpts *opts;
659 Error *err = NULL;
661 if (strstart(filename, "chardev:", &p)) {
662 return qemu_chr_find(p);
665 opts = qemu_chr_parse_compat(label, filename);
666 if (!opts)
667 return NULL;
669 chr = qemu_chr_new_from_opts(opts, &err);
670 if (err) {
671 error_report_err(err);
673 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
674 monitor_init(chr, MONITOR_USE_READLINE);
676 qemu_opts_del(opts);
677 return chr;
680 Chardev *qemu_chr_new(const char *label, const char *filename)
682 Chardev *chr;
683 chr = qemu_chr_new_noreplay(label, filename);
684 if (chr) {
685 if (replay_mode != REPLAY_MODE_NONE) {
686 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
688 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
689 error_report("Replay: ioctl is not supported "
690 "for serial devices yet");
692 replay_register_char_driver(chr);
694 return chr;
697 static int qmp_query_chardev_foreach(Object *obj, void *data)
699 Chardev *chr = CHARDEV(obj);
700 ChardevInfoList **list = data;
701 ChardevInfoList *info = g_malloc0(sizeof(*info));
703 info->value = g_malloc0(sizeof(*info->value));
704 info->value->label = g_strdup(chr->label);
705 info->value->filename = g_strdup(chr->filename);
706 info->value->frontend_open = chr->be && chr->be->fe_open;
708 info->next = *list;
709 *list = info;
711 return 0;
714 ChardevInfoList *qmp_query_chardev(Error **errp)
716 ChardevInfoList *chr_list = NULL;
718 object_child_foreach(get_chardevs_root(),
719 qmp_query_chardev_foreach, &chr_list);
721 return chr_list;
724 static void
725 qmp_prepend_backend(const char *name, void *opaque)
727 ChardevBackendInfoList **list = opaque;
728 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
730 info->value = g_malloc0(sizeof(*info->value));
731 info->value->name = g_strdup(name);
732 info->next = *list;
733 *list = info;
736 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
738 ChardevBackendInfoList *backend_list = NULL;
740 chardev_name_foreach(qmp_prepend_backend, &backend_list);
742 return backend_list;
745 Chardev *qemu_chr_find(const char *name)
747 Object *obj = object_resolve_path_component(get_chardevs_root(), name);
749 return obj ? CHARDEV(obj) : NULL;
752 QemuOptsList qemu_chardev_opts = {
753 .name = "chardev",
754 .implied_opt_name = "backend",
755 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
756 .desc = {
758 .name = "backend",
759 .type = QEMU_OPT_STRING,
761 .name = "path",
762 .type = QEMU_OPT_STRING,
764 .name = "host",
765 .type = QEMU_OPT_STRING,
767 .name = "port",
768 .type = QEMU_OPT_STRING,
770 .name = "localaddr",
771 .type = QEMU_OPT_STRING,
773 .name = "localport",
774 .type = QEMU_OPT_STRING,
776 .name = "to",
777 .type = QEMU_OPT_NUMBER,
779 .name = "ipv4",
780 .type = QEMU_OPT_BOOL,
782 .name = "ipv6",
783 .type = QEMU_OPT_BOOL,
785 .name = "wait",
786 .type = QEMU_OPT_BOOL,
788 .name = "server",
789 .type = QEMU_OPT_BOOL,
791 .name = "delay",
792 .type = QEMU_OPT_BOOL,
794 .name = "reconnect",
795 .type = QEMU_OPT_NUMBER,
797 .name = "telnet",
798 .type = QEMU_OPT_BOOL,
800 .name = "tn3270",
801 .type = QEMU_OPT_BOOL,
803 .name = "tls-creds",
804 .type = QEMU_OPT_STRING,
806 .name = "width",
807 .type = QEMU_OPT_NUMBER,
809 .name = "height",
810 .type = QEMU_OPT_NUMBER,
812 .name = "cols",
813 .type = QEMU_OPT_NUMBER,
815 .name = "rows",
816 .type = QEMU_OPT_NUMBER,
818 .name = "mux",
819 .type = QEMU_OPT_BOOL,
821 .name = "signal",
822 .type = QEMU_OPT_BOOL,
824 .name = "name",
825 .type = QEMU_OPT_STRING,
827 .name = "debug",
828 .type = QEMU_OPT_NUMBER,
830 .name = "size",
831 .type = QEMU_OPT_SIZE,
833 .name = "chardev",
834 .type = QEMU_OPT_STRING,
836 .name = "append",
837 .type = QEMU_OPT_BOOL,
839 .name = "logfile",
840 .type = QEMU_OPT_STRING,
842 .name = "logappend",
843 .type = QEMU_OPT_BOOL,
845 { /* end of list */ }
849 bool qemu_chr_has_feature(Chardev *chr,
850 ChardevFeature feature)
852 return test_bit(feature, chr->features);
855 void qemu_chr_set_feature(Chardev *chr,
856 ChardevFeature feature)
858 return set_bit(feature, chr->features);
861 Chardev *qemu_chardev_new(const char *id, const char *typename,
862 ChardevBackend *backend,
863 Error **errp)
865 Object *obj;
866 Chardev *chr = NULL;
867 Error *local_err = NULL;
868 bool be_opened = true;
870 assert(g_str_has_prefix(typename, "chardev-"));
872 obj = object_new(typename);
873 chr = CHARDEV(obj);
874 chr->label = g_strdup(id);
876 qemu_char_open(chr, backend, &be_opened, &local_err);
877 if (local_err) {
878 goto end;
881 if (!chr->filename) {
882 chr->filename = g_strdup(typename + 8);
884 if (be_opened) {
885 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
888 if (id) {
889 object_property_add_child(get_chardevs_root(), id, obj, &local_err);
890 if (local_err) {
891 goto end;
893 object_unref(obj);
896 end:
897 if (local_err) {
898 error_propagate(errp, local_err);
899 object_unref(obj);
900 return NULL;
903 return chr;
906 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
907 Error **errp)
909 const ChardevClass *cc;
910 ChardevReturn *ret;
911 Chardev *chr;
913 cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp);
914 if (!cc) {
915 return NULL;
918 chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
919 backend, errp);
920 if (!chr) {
921 return NULL;
924 ret = g_new0(ChardevReturn, 1);
925 if (CHARDEV_IS_PTY(chr)) {
926 ret->pty = g_strdup(chr->filename + 4);
927 ret->has_pty = true;
930 return ret;
933 void qmp_chardev_remove(const char *id, Error **errp)
935 Chardev *chr;
937 chr = qemu_chr_find(id);
938 if (chr == NULL) {
939 error_setg(errp, "Chardev '%s' not found", id);
940 return;
942 if (qemu_chr_is_busy(chr)) {
943 error_setg(errp, "Chardev '%s' is busy", id);
944 return;
946 if (qemu_chr_replay(chr)) {
947 error_setg(errp,
948 "Chardev '%s' cannot be unplugged in record/replay mode", id);
949 return;
951 object_unparent(OBJECT(chr));
954 void qemu_chr_cleanup(void)
956 object_unparent(get_chardevs_root());
959 static void register_types(void)
961 type_register_static(&char_type_info);
963 /* this must be done after machine init, since we register FEs with muxes
964 * as part of realize functions like serial_isa_realizefn when -nographic
965 * is specified
967 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
970 type_init(register_types);