MAINTAINERS: PPC: Remove myself
[qemu.git] / chardev / char.c
blob152dde5327d7291a2efe1916f9891b2962db060c
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.
25 #include "qemu/osdep.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 "chardev/char.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-char.h"
34 #include "qapi/qmp/qerror.h"
35 #include "sysemu/replay.h"
36 #include "qemu/help_option.h"
37 #include "qemu/option.h"
39 #include "chardev/char-mux.h"
41 /***********************************************************/
42 /* character device */
44 static Object *get_chardevs_root(void)
46 return container_get(object_get_root(), "/chardevs");
49 static void chr_be_event(Chardev *s, int event)
51 CharBackend *be = s->be;
53 if (!be || !be->chr_event) {
54 return;
57 be->chr_event(be->opaque, event);
60 void qemu_chr_be_event(Chardev *s, int event)
62 /* Keep track if the char device is open */
63 switch (event) {
64 case CHR_EVENT_OPENED:
65 s->be_open = 1;
66 break;
67 case CHR_EVENT_CLOSED:
68 s->be_open = 0;
69 break;
72 CHARDEV_GET_CLASS(s)->chr_be_event(s, event);
75 /* Not reporting errors from writing to logfile, as logs are
76 * defined to be "best effort" only */
77 static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len)
79 size_t done = 0;
80 ssize_t ret;
82 if (s->logfd < 0) {
83 return;
86 while (done < len) {
87 retry:
88 ret = write(s->logfd, buf + done, len - done);
89 if (ret == -1 && errno == EAGAIN) {
90 g_usleep(100);
91 goto retry;
94 if (ret <= 0) {
95 return;
97 done += ret;
101 static int qemu_chr_write_buffer(Chardev *s,
102 const uint8_t *buf, int len,
103 int *offset, bool write_all)
105 ChardevClass *cc = CHARDEV_GET_CLASS(s);
106 int res = 0;
107 *offset = 0;
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 && write_all) {
114 g_usleep(100);
115 goto retry;
118 if (res <= 0) {
119 break;
122 *offset += res;
123 if (!write_all) {
124 break;
127 if (*offset > 0) {
128 qemu_chr_write_log(s, buf, *offset);
130 qemu_mutex_unlock(&s->chr_write_lock);
132 return res;
135 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
137 int offset = 0;
138 int res;
140 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
141 replay_char_write_event_load(&res, &offset);
142 assert(offset <= len);
143 qemu_chr_write_buffer(s, buf, offset, &offset, true);
144 return res;
147 res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
149 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
150 replay_char_write_event_save(res, offset);
153 if (res < 0) {
154 return res;
156 return offset;
159 int qemu_chr_be_can_write(Chardev *s)
161 CharBackend *be = s->be;
163 if (!be || !be->chr_can_read) {
164 return 0;
167 return be->chr_can_read(be->opaque);
170 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
172 CharBackend *be = s->be;
174 if (be && be->chr_read) {
175 be->chr_read(be->opaque, buf, len);
179 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
181 if (qemu_chr_replay(s)) {
182 if (replay_mode == REPLAY_MODE_PLAY) {
183 return;
185 replay_chr_be_write(s, buf, len);
186 } else {
187 qemu_chr_be_write_impl(s, buf, len);
191 void qemu_chr_be_update_read_handlers(Chardev *s,
192 GMainContext *context)
194 ChardevClass *cc = CHARDEV_GET_CLASS(s);
196 s->gcontext = context;
197 if (cc->chr_update_read_handler) {
198 cc->chr_update_read_handler(s);
202 int qemu_chr_add_client(Chardev *s, int fd)
204 return CHARDEV_GET_CLASS(s)->chr_add_client ?
205 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
208 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
209 bool *be_opened, Error **errp)
211 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
212 /* Any ChardevCommon member would work */
213 ChardevCommon *common = backend ? backend->u.null.data : NULL;
215 if (common && common->has_logfile) {
216 int flags = O_WRONLY | O_CREAT;
217 if (common->has_logappend &&
218 common->logappend) {
219 flags |= O_APPEND;
220 } else {
221 flags |= O_TRUNC;
223 chr->logfd = qemu_open(common->logfile, flags, 0666);
224 if (chr->logfd < 0) {
225 error_setg_errno(errp, errno,
226 "Unable to open logfile %s",
227 common->logfile);
228 return;
232 if (cc->open) {
233 cc->open(chr, backend, be_opened, errp);
237 static void char_init(Object *obj)
239 Chardev *chr = CHARDEV(obj);
241 chr->logfd = -1;
242 qemu_mutex_init(&chr->chr_write_lock);
245 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
247 return len;
250 static void char_class_init(ObjectClass *oc, void *data)
252 ChardevClass *cc = CHARDEV_CLASS(oc);
254 cc->chr_write = null_chr_write;
255 cc->chr_be_event = chr_be_event;
258 static void char_finalize(Object *obj)
260 Chardev *chr = CHARDEV(obj);
262 if (chr->be) {
263 chr->be->chr = NULL;
265 g_free(chr->filename);
266 g_free(chr->label);
267 if (chr->logfd != -1) {
268 close(chr->logfd);
270 qemu_mutex_destroy(&chr->chr_write_lock);
273 static const TypeInfo char_type_info = {
274 .name = TYPE_CHARDEV,
275 .parent = TYPE_OBJECT,
276 .instance_size = sizeof(Chardev),
277 .instance_init = char_init,
278 .instance_finalize = char_finalize,
279 .abstract = true,
280 .class_size = sizeof(ChardevClass),
281 .class_init = char_class_init,
284 static int chardev_machine_done_notify_one(Object *child, void *opaque)
286 Chardev *chr = (Chardev *)child;
287 ChardevClass *class = CHARDEV_GET_CLASS(chr);
289 if (class->chr_machine_done) {
290 return class->chr_machine_done(chr);
293 return 0;
296 static void chardev_machine_done_hook(Notifier *notifier, void *unused)
298 int ret = object_child_foreach(get_chardevs_root(),
299 chardev_machine_done_notify_one, NULL);
301 if (ret) {
302 error_report("Failed to call chardev machine_done hooks");
303 exit(1);
307 static Notifier chardev_machine_done_notify = {
308 .notify = chardev_machine_done_hook,
311 static bool qemu_chr_is_busy(Chardev *s)
313 if (CHARDEV_IS_MUX(s)) {
314 MuxChardev *d = MUX_CHARDEV(s);
315 return d->mux_cnt >= 0;
316 } else {
317 return s->be != NULL;
321 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
323 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
325 if (cc->chr_wait_connected) {
326 return cc->chr_wait_connected(chr, errp);
329 return 0;
332 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
333 bool permit_mux_mon)
335 char host[65], port[33], width[8], height[8];
336 int pos;
337 const char *p;
338 QemuOpts *opts;
339 Error *local_err = NULL;
341 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
342 if (local_err) {
343 error_report_err(local_err);
344 return NULL;
347 if (strstart(filename, "mon:", &p)) {
348 if (!permit_mux_mon) {
349 error_report("mon: isn't supported in this context");
350 return NULL;
352 filename = p;
353 qemu_opt_set(opts, "mux", "on", &error_abort);
354 if (strcmp(filename, "stdio") == 0) {
355 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
356 * but pass it to the guest. Handle this only for compat syntax,
357 * for -chardev syntax we have special option for this.
358 * This is what -nographic did, redirecting+muxing serial+monitor
359 * to stdio causing Ctrl+C to be passed to guest. */
360 qemu_opt_set(opts, "signal", "off", &error_abort);
364 if (strcmp(filename, "null") == 0 ||
365 strcmp(filename, "pty") == 0 ||
366 strcmp(filename, "msmouse") == 0 ||
367 strcmp(filename, "wctablet") == 0 ||
368 strcmp(filename, "braille") == 0 ||
369 strcmp(filename, "testdev") == 0 ||
370 strcmp(filename, "stdio") == 0) {
371 qemu_opt_set(opts, "backend", filename, &error_abort);
372 return opts;
374 if (strstart(filename, "vc", &p)) {
375 qemu_opt_set(opts, "backend", "vc", &error_abort);
376 if (*p == ':') {
377 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
378 /* pixels */
379 qemu_opt_set(opts, "width", width, &error_abort);
380 qemu_opt_set(opts, "height", height, &error_abort);
381 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
382 /* chars */
383 qemu_opt_set(opts, "cols", width, &error_abort);
384 qemu_opt_set(opts, "rows", height, &error_abort);
385 } else {
386 goto fail;
389 return opts;
391 if (strcmp(filename, "con:") == 0) {
392 qemu_opt_set(opts, "backend", "console", &error_abort);
393 return opts;
395 if (strstart(filename, "COM", NULL)) {
396 qemu_opt_set(opts, "backend", "serial", &error_abort);
397 qemu_opt_set(opts, "path", filename, &error_abort);
398 return opts;
400 if (strstart(filename, "file:", &p)) {
401 qemu_opt_set(opts, "backend", "file", &error_abort);
402 qemu_opt_set(opts, "path", p, &error_abort);
403 return opts;
405 if (strstart(filename, "pipe:", &p)) {
406 qemu_opt_set(opts, "backend", "pipe", &error_abort);
407 qemu_opt_set(opts, "path", p, &error_abort);
408 return opts;
410 if (strstart(filename, "tcp:", &p) ||
411 strstart(filename, "telnet:", &p) ||
412 strstart(filename, "tn3270:", &p) ||
413 strstart(filename, "websocket:", &p)) {
414 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
415 host[0] = 0;
416 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
417 goto fail;
419 qemu_opt_set(opts, "backend", "socket", &error_abort);
420 qemu_opt_set(opts, "host", host, &error_abort);
421 qemu_opt_set(opts, "port", port, &error_abort);
422 if (p[pos] == ',') {
423 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
424 if (local_err) {
425 error_report_err(local_err);
426 goto fail;
429 if (strstart(filename, "telnet:", &p)) {
430 qemu_opt_set(opts, "telnet", "on", &error_abort);
431 } else if (strstart(filename, "tn3270:", &p)) {
432 qemu_opt_set(opts, "tn3270", "on", &error_abort);
433 } else if (strstart(filename, "websocket:", &p)) {
434 qemu_opt_set(opts, "websocket", "on", &error_abort);
436 return opts;
438 if (strstart(filename, "udp:", &p)) {
439 qemu_opt_set(opts, "backend", "udp", &error_abort);
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;
446 qemu_opt_set(opts, "host", host, &error_abort);
447 qemu_opt_set(opts, "port", port, &error_abort);
448 if (p[pos] == '@') {
449 p += pos + 1;
450 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
451 host[0] = 0;
452 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
453 goto fail;
456 qemu_opt_set(opts, "localaddr", host, &error_abort);
457 qemu_opt_set(opts, "localport", port, &error_abort);
459 return opts;
461 if (strstart(filename, "unix:", &p)) {
462 qemu_opt_set(opts, "backend", "socket", &error_abort);
463 qemu_opts_do_parse(opts, p, "path", &local_err);
464 if (local_err) {
465 error_report_err(local_err);
466 goto fail;
468 return opts;
470 if (strstart(filename, "/dev/parport", NULL) ||
471 strstart(filename, "/dev/ppi", NULL)) {
472 qemu_opt_set(opts, "backend", "parallel", &error_abort);
473 qemu_opt_set(opts, "path", filename, &error_abort);
474 return opts;
476 if (strstart(filename, "/dev/", NULL)) {
477 qemu_opt_set(opts, "backend", "serial", &error_abort);
478 qemu_opt_set(opts, "path", filename, &error_abort);
479 return opts;
482 fail:
483 qemu_opts_del(opts);
484 return NULL;
487 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
489 const char *logfile = qemu_opt_get(opts, "logfile");
491 backend->has_logfile = logfile != NULL;
492 backend->logfile = g_strdup(logfile);
494 backend->has_logappend = true;
495 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
498 static const ChardevClass *char_get_class(const char *driver, Error **errp)
500 ObjectClass *oc;
501 const ChardevClass *cc;
502 char *typename = g_strdup_printf("chardev-%s", driver);
504 oc = object_class_by_name(typename);
505 g_free(typename);
507 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
508 error_setg(errp, "'%s' is not a valid char driver name", driver);
509 return NULL;
512 if (object_class_is_abstract(oc)) {
513 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
514 "abstract device type");
515 return NULL;
518 cc = CHARDEV_CLASS(oc);
519 if (cc->internal) {
520 error_setg(errp, "'%s' is not a valid char driver name", driver);
521 return NULL;
524 return cc;
527 static const struct ChardevAlias {
528 const char *typename;
529 const char *alias;
530 } chardev_alias_table[] = {
531 #ifdef HAVE_CHARDEV_PARPORT
532 { "parallel", "parport" },
533 #endif
534 #ifdef HAVE_CHARDEV_SERIAL
535 { "serial", "tty" },
536 #endif
539 typedef struct ChadevClassFE {
540 void (*fn)(const char *name, void *opaque);
541 void *opaque;
542 } ChadevClassFE;
544 static void
545 chardev_class_foreach(ObjectClass *klass, void *opaque)
547 ChadevClassFE *fe = opaque;
549 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
550 if (CHARDEV_CLASS(klass)->internal) {
551 return;
554 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
557 static void
558 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
560 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
561 int i;
563 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
565 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
566 fn(chardev_alias_table[i].alias, opaque);
570 static void
571 help_string_append(const char *name, void *opaque)
573 GString *str = opaque;
575 g_string_append_printf(str, "\n %s", name);
578 static const char *chardev_alias_translate(const char *name)
580 int i;
581 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
582 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
583 return chardev_alias_table[i].typename;
586 return name;
589 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
591 Error *local_err = NULL;
592 const ChardevClass *cc;
593 ChardevBackend *backend = NULL;
594 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
596 if (name == NULL) {
597 error_setg(errp, "chardev: \"%s\" missing backend",
598 qemu_opts_id(opts));
599 return NULL;
602 cc = char_get_class(name, errp);
603 if (cc == NULL) {
604 return NULL;
607 backend = g_new0(ChardevBackend, 1);
608 backend->type = CHARDEV_BACKEND_KIND_NULL;
610 if (cc->parse) {
611 cc->parse(opts, backend, &local_err);
612 if (local_err) {
613 error_propagate(errp, local_err);
614 qapi_free_ChardevBackend(backend);
615 return NULL;
617 } else {
618 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
619 qemu_chr_parse_common(opts, ccom);
620 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
623 return backend;
626 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
628 const ChardevClass *cc;
629 Chardev *chr = NULL;
630 ChardevBackend *backend = NULL;
631 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
632 const char *id = qemu_opts_id(opts);
633 char *bid = NULL;
635 if (name && is_help_option(name)) {
636 GString *str = g_string_new("");
638 chardev_name_foreach(help_string_append, str);
640 error_printf("Available chardev backend types: %s\n", str->str);
641 g_string_free(str, true);
642 return NULL;
645 if (id == NULL) {
646 error_setg(errp, "chardev: no id specified");
647 return NULL;
650 backend = qemu_chr_parse_opts(opts, errp);
651 if (backend == NULL) {
652 return NULL;
655 cc = char_get_class(name, errp);
656 if (cc == NULL) {
657 goto out;
660 if (qemu_opt_get_bool(opts, "mux", 0)) {
661 bid = g_strdup_printf("%s-base", id);
664 chr = qemu_chardev_new(bid ? bid : id,
665 object_class_get_name(OBJECT_CLASS(cc)),
666 backend, errp);
668 if (chr == NULL) {
669 goto out;
672 if (bid) {
673 Chardev *mux;
674 qapi_free_ChardevBackend(backend);
675 backend = g_new0(ChardevBackend, 1);
676 backend->type = CHARDEV_BACKEND_KIND_MUX;
677 backend->u.mux.data = g_new0(ChardevMux, 1);
678 backend->u.mux.data->chardev = g_strdup(bid);
679 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp);
680 if (mux == NULL) {
681 object_unparent(OBJECT(chr));
682 chr = NULL;
683 goto out;
685 chr = mux;
688 out:
689 qapi_free_ChardevBackend(backend);
690 g_free(bid);
691 return chr;
694 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
695 bool permit_mux_mon)
697 const char *p;
698 Chardev *chr;
699 QemuOpts *opts;
700 Error *err = NULL;
702 if (strstart(filename, "chardev:", &p)) {
703 return qemu_chr_find(p);
706 opts = qemu_chr_parse_compat(label, filename, permit_mux_mon);
707 if (!opts)
708 return NULL;
710 chr = qemu_chr_new_from_opts(opts, &err);
711 if (!chr) {
712 error_report_err(err);
713 goto out;
716 if (qemu_opt_get_bool(opts, "mux", 0)) {
717 assert(permit_mux_mon);
718 monitor_init(chr, MONITOR_USE_READLINE);
721 out:
722 qemu_opts_del(opts);
723 return chr;
726 static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
727 const char *filename,
728 bool permit_mux_mon)
730 Chardev *chr;
731 chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon);
732 if (chr) {
733 if (replay_mode != REPLAY_MODE_NONE) {
734 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
736 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
737 error_report("Replay: ioctl is not supported "
738 "for serial devices yet");
740 replay_register_char_driver(chr);
742 return chr;
745 Chardev *qemu_chr_new(const char *label, const char *filename)
747 return qemu_chr_new_permit_mux_mon(label, filename, false);
750 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename)
752 return qemu_chr_new_permit_mux_mon(label, filename, true);
755 static int qmp_query_chardev_foreach(Object *obj, void *data)
757 Chardev *chr = CHARDEV(obj);
758 ChardevInfoList **list = data;
759 ChardevInfoList *info = g_malloc0(sizeof(*info));
761 info->value = g_malloc0(sizeof(*info->value));
762 info->value->label = g_strdup(chr->label);
763 info->value->filename = g_strdup(chr->filename);
764 info->value->frontend_open = chr->be && chr->be->fe_open;
766 info->next = *list;
767 *list = info;
769 return 0;
772 ChardevInfoList *qmp_query_chardev(Error **errp)
774 ChardevInfoList *chr_list = NULL;
776 object_child_foreach(get_chardevs_root(),
777 qmp_query_chardev_foreach, &chr_list);
779 return chr_list;
782 static void
783 qmp_prepend_backend(const char *name, void *opaque)
785 ChardevBackendInfoList **list = opaque;
786 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
788 info->value = g_malloc0(sizeof(*info->value));
789 info->value->name = g_strdup(name);
790 info->next = *list;
791 *list = info;
794 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
796 ChardevBackendInfoList *backend_list = NULL;
798 chardev_name_foreach(qmp_prepend_backend, &backend_list);
800 return backend_list;
803 Chardev *qemu_chr_find(const char *name)
805 Object *obj = object_resolve_path_component(get_chardevs_root(), name);
807 return obj ? CHARDEV(obj) : NULL;
810 QemuOptsList qemu_chardev_opts = {
811 .name = "chardev",
812 .implied_opt_name = "backend",
813 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
814 .desc = {
816 .name = "backend",
817 .type = QEMU_OPT_STRING,
819 .name = "path",
820 .type = QEMU_OPT_STRING,
822 .name = "host",
823 .type = QEMU_OPT_STRING,
825 .name = "port",
826 .type = QEMU_OPT_STRING,
828 .name = "fd",
829 .type = QEMU_OPT_STRING,
831 .name = "localaddr",
832 .type = QEMU_OPT_STRING,
834 .name = "localport",
835 .type = QEMU_OPT_STRING,
837 .name = "to",
838 .type = QEMU_OPT_NUMBER,
840 .name = "ipv4",
841 .type = QEMU_OPT_BOOL,
843 .name = "ipv6",
844 .type = QEMU_OPT_BOOL,
846 .name = "wait",
847 .type = QEMU_OPT_BOOL,
849 .name = "server",
850 .type = QEMU_OPT_BOOL,
852 .name = "delay",
853 .type = QEMU_OPT_BOOL,
855 .name = "reconnect",
856 .type = QEMU_OPT_NUMBER,
858 .name = "telnet",
859 .type = QEMU_OPT_BOOL,
861 .name = "tn3270",
862 .type = QEMU_OPT_BOOL,
864 .name = "tls-creds",
865 .type = QEMU_OPT_STRING,
867 .name = "websocket",
868 .type = QEMU_OPT_BOOL,
870 .name = "width",
871 .type = QEMU_OPT_NUMBER,
873 .name = "height",
874 .type = QEMU_OPT_NUMBER,
876 .name = "cols",
877 .type = QEMU_OPT_NUMBER,
879 .name = "rows",
880 .type = QEMU_OPT_NUMBER,
882 .name = "mux",
883 .type = QEMU_OPT_BOOL,
885 .name = "signal",
886 .type = QEMU_OPT_BOOL,
888 .name = "name",
889 .type = QEMU_OPT_STRING,
891 .name = "debug",
892 .type = QEMU_OPT_NUMBER,
894 .name = "size",
895 .type = QEMU_OPT_SIZE,
897 .name = "chardev",
898 .type = QEMU_OPT_STRING,
900 .name = "append",
901 .type = QEMU_OPT_BOOL,
903 .name = "logfile",
904 .type = QEMU_OPT_STRING,
906 .name = "logappend",
907 .type = QEMU_OPT_BOOL,
909 { /* end of list */ }
913 bool qemu_chr_has_feature(Chardev *chr,
914 ChardevFeature feature)
916 return test_bit(feature, chr->features);
919 void qemu_chr_set_feature(Chardev *chr,
920 ChardevFeature feature)
922 return set_bit(feature, chr->features);
925 Chardev *qemu_chardev_new(const char *id, const char *typename,
926 ChardevBackend *backend,
927 Error **errp)
929 Object *obj;
930 Chardev *chr = NULL;
931 Error *local_err = NULL;
932 bool be_opened = true;
934 assert(g_str_has_prefix(typename, "chardev-"));
936 obj = object_new(typename);
937 chr = CHARDEV(obj);
938 chr->label = g_strdup(id);
940 qemu_char_open(chr, backend, &be_opened, &local_err);
941 if (local_err) {
942 goto end;
945 if (!chr->filename) {
946 chr->filename = g_strdup(typename + 8);
948 if (be_opened) {
949 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
952 if (id) {
953 object_property_add_child(get_chardevs_root(), id, obj, &local_err);
954 if (local_err) {
955 goto end;
957 object_unref(obj);
960 end:
961 if (local_err) {
962 error_propagate(errp, local_err);
963 object_unref(obj);
964 return NULL;
967 return chr;
970 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
971 Error **errp)
973 const ChardevClass *cc;
974 ChardevReturn *ret;
975 Chardev *chr;
977 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
978 if (!cc) {
979 return NULL;
982 chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
983 backend, errp);
984 if (!chr) {
985 return NULL;
988 ret = g_new0(ChardevReturn, 1);
989 if (CHARDEV_IS_PTY(chr)) {
990 ret->pty = g_strdup(chr->filename + 4);
991 ret->has_pty = true;
994 return ret;
997 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
998 Error **errp)
1000 CharBackend *be;
1001 const ChardevClass *cc;
1002 Chardev *chr, *chr_new;
1003 bool closed_sent = false;
1004 ChardevReturn *ret;
1006 chr = qemu_chr_find(id);
1007 if (!chr) {
1008 error_setg(errp, "Chardev '%s' does not exist", id);
1009 return NULL;
1012 if (CHARDEV_IS_MUX(chr)) {
1013 error_setg(errp, "Mux device hotswap not supported yet");
1014 return NULL;
1017 if (qemu_chr_replay(chr)) {
1018 error_setg(errp,
1019 "Chardev '%s' cannot be changed in record/replay mode", id);
1020 return NULL;
1023 be = chr->be;
1024 if (!be) {
1025 /* easy case */
1026 object_unparent(OBJECT(chr));
1027 return qmp_chardev_add(id, backend, errp);
1030 if (!be->chr_be_change) {
1031 error_setg(errp, "Chardev user does not support chardev hotswap");
1032 return NULL;
1035 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1036 if (!cc) {
1037 return NULL;
1040 chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)),
1041 backend, errp);
1042 if (!chr_new) {
1043 return NULL;
1045 chr_new->label = g_strdup(id);
1047 if (chr->be_open && !chr_new->be_open) {
1048 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1049 closed_sent = true;
1052 chr->be = NULL;
1053 qemu_chr_fe_init(be, chr_new, &error_abort);
1055 if (be->chr_be_change(be->opaque) < 0) {
1056 error_setg(errp, "Chardev '%s' change failed", chr_new->label);
1057 chr_new->be = NULL;
1058 qemu_chr_fe_init(be, chr, &error_abort);
1059 if (closed_sent) {
1060 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1062 object_unref(OBJECT(chr_new));
1063 return NULL;
1066 object_unparent(OBJECT(chr));
1067 object_property_add_child(get_chardevs_root(), chr_new->label,
1068 OBJECT(chr_new), &error_abort);
1069 object_unref(OBJECT(chr_new));
1071 ret = g_new0(ChardevReturn, 1);
1072 if (CHARDEV_IS_PTY(chr_new)) {
1073 ret->pty = g_strdup(chr_new->filename + 4);
1074 ret->has_pty = true;
1077 return ret;
1080 void qmp_chardev_remove(const char *id, Error **errp)
1082 Chardev *chr;
1084 chr = qemu_chr_find(id);
1085 if (chr == NULL) {
1086 error_setg(errp, "Chardev '%s' not found", id);
1087 return;
1089 if (qemu_chr_is_busy(chr)) {
1090 error_setg(errp, "Chardev '%s' is busy", id);
1091 return;
1093 if (qemu_chr_replay(chr)) {
1094 error_setg(errp,
1095 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1096 return;
1098 object_unparent(OBJECT(chr));
1101 void qmp_chardev_send_break(const char *id, Error **errp)
1103 Chardev *chr;
1105 chr = qemu_chr_find(id);
1106 if (chr == NULL) {
1107 error_setg(errp, "Chardev '%s' not found", id);
1108 return;
1110 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
1114 * Add a timeout callback for the chardev (in milliseconds), return
1115 * the GSource object created. Please use this to add timeout hook for
1116 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1117 * make sure the gcontext that the task bound to is correct.
1119 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
1120 GSourceFunc func, void *private)
1122 GSource *source = g_timeout_source_new(ms);
1124 assert(func);
1125 g_source_set_callback(source, func, private, NULL);
1126 g_source_attach(source, chr->gcontext);
1128 return source;
1131 void qemu_chr_cleanup(void)
1133 object_unparent(get_chardevs_root());
1136 static void register_types(void)
1138 type_register_static(&char_type_info);
1140 /* this must be done after machine init, since we register FEs with muxes
1141 * as part of realize functions like serial_isa_realizefn when -nographic
1142 * is specified
1144 qemu_add_machine_init_done_notifier(&chardev_machine_done_notify);
1147 type_init(register_types);