qapi2texi: Plainer enum value and member name formatting
[qemu/ar7.git] / chardev / char.c
blob3df116350bd08aed665afe014b98cc3969e4e3eb
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-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"
37 #include "char-mux.h"
38 #include "char-io.h"
39 #include "char-parallel.h"
40 #include "char-serial.h"
42 /***********************************************************/
43 /* character device */
45 static QTAILQ_HEAD(ChardevHead, Chardev) chardevs =
46 QTAILQ_HEAD_INITIALIZER(chardevs);
48 void qemu_chr_be_event(Chardev *s, int event)
50 CharBackend *be = s->be;
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;
62 if (!be || !be->chr_event) {
63 return;
66 be->chr_event(be->opaque, event);
69 void qemu_chr_be_generic_open(Chardev *s)
71 qemu_chr_be_event(s, CHR_EVENT_OPENED);
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)
80 size_t done = 0;
81 ssize_t ret;
83 if (s->logfd < 0) {
84 return;
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;
95 if (ret <= 0) {
96 return;
98 done += ret;
102 static int qemu_chr_fe_write_buffer(Chardev *s,
103 const uint8_t *buf, int len, int *offset)
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) {
114 g_usleep(100);
115 goto retry;
118 if (res <= 0) {
119 break;
122 *offset += res;
124 if (*offset > 0) {
125 qemu_chr_fe_write_log(s, buf, *offset);
127 qemu_mutex_unlock(&s->chr_write_lock);
129 return res;
132 static bool qemu_chr_replay(Chardev *chr)
134 return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
137 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
139 Chardev *s = be->chr;
140 ChardevClass *cc;
141 int ret;
143 if (!s) {
144 return 0;
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;
155 cc = CHARDEV_GET_CLASS(s);
156 qemu_mutex_lock(&s->chr_write_lock);
157 ret = cc->chr_write(s, buf, len);
159 if (ret > 0) {
160 qemu_chr_fe_write_log(s, buf, ret);
163 qemu_mutex_unlock(&s->chr_write_lock);
165 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
166 replay_char_write_event_save(ret, ret < 0 ? 0 : ret);
169 return ret;
172 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len)
174 int offset;
175 int res;
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;
184 res = qemu_chr_fe_write_buffer(s, buf, len, &offset);
186 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
187 replay_char_write_event_save(res, offset);
190 if (res < 0) {
191 return res;
193 return offset;
196 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
198 Chardev *s = be->chr;
200 if (!s) {
201 return 0;
204 return qemu_chr_write_all(s, buf, len);
207 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
209 Chardev *s = be->chr;
210 int offset = 0, counter = 10;
211 int res;
213 if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) {
214 return 0;
217 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
218 return replay_char_read_all_load(buf);
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;
230 if (res == 0) {
231 break;
234 if (res < 0) {
235 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
236 replay_char_read_all_save_error(res);
238 return res;
241 offset += res;
243 if (!counter--) {
244 break;
248 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
249 replay_char_read_all_save_buf(buf, offset);
251 return offset;
254 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
256 Chardev *s = be->chr;
257 int res;
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);
265 return res;
268 int qemu_chr_be_can_write(Chardev *s)
270 CharBackend *be = s->be;
272 if (!be || !be->chr_can_read) {
273 return 0;
276 return be->chr_can_read(be->opaque);
279 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
281 CharBackend *be = s->be;
283 if (be && be->chr_read) {
284 be->chr_read(be->opaque, buf, len);
288 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
290 if (qemu_chr_replay(s)) {
291 if (replay_mode == REPLAY_MODE_PLAY) {
292 return;
294 replay_chr_be_write(s, buf, len);
295 } else {
296 qemu_chr_be_write_impl(s, buf, len);
300 int qemu_chr_fe_get_msgfd(CharBackend *be)
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);
310 return res;
313 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
315 Chardev *s = be->chr;
317 if (!s) {
318 return -1;
321 return CHARDEV_GET_CLASS(s)->get_msgfds ?
322 CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
325 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
327 Chardev *s = be->chr;
329 if (!s) {
330 return -1;
333 return CHARDEV_GET_CLASS(s)->set_msgfds ?
334 CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
337 int qemu_chr_add_client(Chardev *s, int fd)
339 return CHARDEV_GET_CLASS(s)->chr_add_client ?
340 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
343 void qemu_chr_fe_accept_input(CharBackend *be)
345 Chardev *s = be->chr;
347 if (!s) {
348 return;
351 if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
352 CHARDEV_GET_CLASS(s)->chr_accept_input(s);
354 qemu_notify_event();
357 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
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);
369 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
370 bool *be_opened, Error **errp)
372 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
373 /* Any ChardevCommon member would work */
374 ChardevCommon *common = backend ? backend->u.null.data : NULL;
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;
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;
393 if (cc->open) {
394 cc->open(chr, backend, be_opened, errp);
398 static void char_init(Object *obj)
400 Chardev *chr = CHARDEV(obj);
402 chr->logfd = -1;
403 qemu_mutex_init(&chr->chr_write_lock);
406 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
408 return len;
411 static void char_class_init(ObjectClass *oc, void *data)
413 ChardevClass *cc = CHARDEV_CLASS(oc);
415 cc->chr_write = null_chr_write;
418 static void char_finalize(Object *obj)
420 Chardev *chr = CHARDEV(obj);
422 if (chr->be) {
423 chr->be->chr = NULL;
425 g_free(chr->filename);
426 g_free(chr->label);
427 if (chr->logfd != -1) {
428 close(chr->logfd);
430 qemu_mutex_destroy(&chr->chr_write_lock);
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,
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.
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.
456 static void muxes_realize_done(Notifier *notifier, void *unused)
458 Chardev *chr;
460 QTAILQ_FOREACH(chr, &chardevs, next) {
461 if (CHARDEV_IS_MUX(chr)) {
462 MuxChardev *d = MUX_CHARDEV(chr);
463 int i;
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);
469 /* mark mux as OPENED so any new FEs will immediately receive
470 * OPENED event
472 qemu_chr_be_generic_open(chr);
475 muxes_realized = true;
478 static Notifier muxes_realize_notify = {
479 .notify = muxes_realize_done,
482 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
484 return be->chr;
487 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
489 int tag = 0;
491 if (CHARDEV_IS_MUX(s)) {
492 MuxChardev *d = MUX_CHARDEV(s);
494 if (d->mux_cnt >= MAX_MUX) {
495 goto unavailable;
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;
506 b->fe_open = false;
507 b->tag = tag;
508 b->chr = s;
509 return true;
511 unavailable:
512 error_setg(errp, QERR_DEVICE_IN_USE, s->label);
513 return false;
516 static bool qemu_chr_is_busy(Chardev *s)
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;
526 void qemu_chr_fe_deinit(CharBackend *b)
528 assert(b);
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;
535 if (CHARDEV_IS_MUX(b->chr)) {
536 MuxChardev *d = MUX_CHARDEV(b->chr);
537 d->backends[b->tag] = NULL;
539 b->chr = NULL;
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)
551 Chardev *s;
552 ChardevClass *cc;
553 int fe_open;
555 s = b->chr;
556 if (!s) {
557 return;
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, context);
564 } else {
565 fe_open = 1;
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);
575 if (set_open) {
576 qemu_chr_fe_set_open(b, fe_open);
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);
588 if (CHARDEV_IS_MUX(s)) {
589 mux_chr_set_handlers(s, context);
593 void qemu_chr_fe_take_focus(CharBackend *b)
595 if (!b->chr) {
596 return;
599 if (CHARDEV_IS_MUX(b->chr)) {
600 mux_set_focus(b->chr, b->tag);
604 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
606 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
608 if (cc->chr_wait_connected) {
609 return cc->chr_wait_connected(chr, errp);
612 return 0;
615 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
617 if (!be->chr) {
618 error_setg(errp, "missing associated backend");
619 return -1;
622 return qemu_chr_wait_connected(be->chr, errp);
625 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
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;
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;
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);
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;
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;
677 return opts;
679 if (strcmp(filename, "con:") == 0) {
680 qemu_opt_set(opts, "backend", "console", &error_abort);
681 return opts;
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;
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;
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;
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;
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;
715 if (strstart(filename, "telnet:", &p))
716 qemu_opt_set(opts, "telnet", "on", &error_abort);
717 return opts;
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;
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;
737 qemu_opt_set(opts, "localaddr", host, &error_abort);
738 qemu_opt_set(opts, "localport", port, &error_abort);
740 return opts;
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;
749 return opts;
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;
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;
763 fail:
764 qemu_opts_del(opts);
765 return NULL;
768 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
770 const char *logfile = qemu_opt_get(opts, "logfile");
772 backend->has_logfile = logfile != NULL;
773 backend->logfile = logfile ? g_strdup(logfile) : NULL;
775 backend->has_logappend = true;
776 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
779 static const ChardevClass *char_get_class(const char *driver, Error **errp)
781 ObjectClass *oc;
782 const ChardevClass *cc;
783 char *typename = g_strdup_printf("chardev-%s", driver);
785 oc = object_class_by_name(typename);
786 g_free(typename);
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;
793 if (object_class_is_abstract(oc)) {
794 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
795 "abstract device type");
796 return NULL;
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;
805 return cc;
808 static Chardev *qemu_chardev_add(const char *id, const char *typename,
809 ChardevBackend *backend, Error **errp)
811 Chardev *chr;
813 chr = qemu_chr_find(id);
814 if (chr) {
815 error_setg(errp, "Chardev '%s' already exists", id);
816 return NULL;
819 chr = qemu_chardev_new(id, typename, backend, errp);
820 if (!chr) {
821 return NULL;
824 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
825 return chr;
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
840 typedef struct ChadevClassFE {
841 void (*fn)(const char *name, void *opaque);
842 void *opaque;
843 } ChadevClassFE;
845 static void
846 chardev_class_foreach(ObjectClass *klass, void *opaque)
848 ChadevClassFE *fe = opaque;
850 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
851 if (CHARDEV_CLASS(klass)->internal) {
852 return;
855 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
858 static void
859 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
861 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
862 int i;
864 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
866 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) {
867 fn(chardev_alias_table[i].alias, opaque);
871 static void
872 help_string_append(const char *name, void *opaque)
874 GString *str = opaque;
876 g_string_append_printf(str, "\n%s", name);
879 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
880 Error **errp)
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;
891 if (name == NULL) {
892 error_setg(errp, "chardev: \"%s\" missing backend",
893 qemu_opts_id(opts));
894 return NULL;
897 if (is_help_option(name)) {
898 GString *str = g_string_new("");
900 chardev_name_foreach(help_string_append, str);
902 error_report("Available chardev backend types: %s", str->str);
903 g_string_free(str, true);
904 exit(0);
907 if (id == NULL) {
908 error_setg(errp, "chardev: no id specified");
909 return NULL;
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;
919 cc = char_get_class(name, errp);
920 if (cc == NULL) {
921 return NULL;
924 backend = g_new0(ChardevBackend, 1);
925 backend->type = CHARDEV_BACKEND_KIND_NULL;
927 if (qemu_opt_get_bool(opts, "mux", 0)) {
928 bid = g_strdup_printf("%s-base", id);
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;
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 */
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;
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;
964 chr = mux;
967 out:
968 qapi_free_ChardevBackend(backend);
969 g_free(bid);
970 return chr;
973 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename)
975 const char *p;
976 Chardev *chr;
977 QemuOpts *opts;
978 Error *err = NULL;
980 if (strstart(filename, "chardev:", &p)) {
981 return qemu_chr_find(p);
984 opts = qemu_chr_parse_compat(label, filename);
985 if (!opts)
986 return NULL;
988 chr = qemu_chr_new_from_opts(opts, &err);
989 if (err) {
990 error_report_err(err);
992 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
993 monitor_init(chr, MONITOR_USE_READLINE);
995 qemu_opts_del(opts);
996 return chr;
999 Chardev *qemu_chr_new(const char *label, const char *filename)
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);
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");
1011 replay_register_char_driver(chr);
1013 return chr;
1016 void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
1018 Chardev *chr = be->chr;
1020 if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
1021 CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
1025 void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
1027 Chardev *chr = be->chr;
1029 if (!chr) {
1030 return;
1033 if (be->fe_open == fe_open) {
1034 return;
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);
1042 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
1043 GIOFunc func, void *user_data)
1045 Chardev *s = be->chr;
1046 GSource *src;
1047 guint tag;
1049 if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
1050 return 0;
1053 src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
1054 if (!src) {
1055 return 0;
1058 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
1059 tag = g_source_attach(src, NULL);
1060 g_source_unref(src);
1062 return tag;
1065 void qemu_chr_fe_disconnect(CharBackend *be)
1067 Chardev *chr = be->chr;
1069 if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
1070 CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);
1074 void qemu_chr_delete(Chardev *chr)
1076 QTAILQ_REMOVE(&chardevs, chr, next);
1077 object_unref(OBJECT(chr));
1080 ChardevInfoList *qmp_query_chardev(Error **errp)
1082 ChardevInfoList *chr_list = NULL;
1083 Chardev *chr;
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;
1092 info->next = chr_list;
1093 chr_list = info;
1096 return chr_list;
1099 static void
1100 qmp_prepend_backend(const char *name, void *opaque)
1102 ChardevBackendInfoList **list = opaque;
1103 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
1105 info->value = g_malloc0(sizeof(*info->value));
1106 info->value->name = g_strdup(name);
1107 info->next = *list;
1108 *list = info;
1111 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
1113 ChardevBackendInfoList *backend_list = NULL;
1115 chardev_name_foreach(qmp_prepend_backend, &backend_list);
1117 return backend_list;
1120 Chardev *qemu_chr_find(const char *name)
1122 Chardev *chr;
1124 QTAILQ_FOREACH(chr, &chardevs, next) {
1125 if (strcmp(chr->label, name) != 0)
1126 continue;
1127 return chr;
1129 return NULL;
1132 QemuOptsList qemu_chardev_opts = {
1133 .name = "chardev",
1134 .implied_opt_name = "backend",
1135 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
1136 .desc = {
1138 .name = "backend",
1139 .type = QEMU_OPT_STRING,
1141 .name = "path",
1142 .type = QEMU_OPT_STRING,
1144 .name = "host",
1145 .type = QEMU_OPT_STRING,
1147 .name = "port",
1148 .type = QEMU_OPT_STRING,
1150 .name = "localaddr",
1151 .type = QEMU_OPT_STRING,
1153 .name = "localport",
1154 .type = QEMU_OPT_STRING,
1156 .name = "to",
1157 .type = QEMU_OPT_NUMBER,
1159 .name = "ipv4",
1160 .type = QEMU_OPT_BOOL,
1162 .name = "ipv6",
1163 .type = QEMU_OPT_BOOL,
1165 .name = "wait",
1166 .type = QEMU_OPT_BOOL,
1168 .name = "server",
1169 .type = QEMU_OPT_BOOL,
1171 .name = "delay",
1172 .type = QEMU_OPT_BOOL,
1174 .name = "reconnect",
1175 .type = QEMU_OPT_NUMBER,
1177 .name = "telnet",
1178 .type = QEMU_OPT_BOOL,
1180 .name = "tls-creds",
1181 .type = QEMU_OPT_STRING,
1183 .name = "width",
1184 .type = QEMU_OPT_NUMBER,
1186 .name = "height",
1187 .type = QEMU_OPT_NUMBER,
1189 .name = "cols",
1190 .type = QEMU_OPT_NUMBER,
1192 .name = "rows",
1193 .type = QEMU_OPT_NUMBER,
1195 .name = "mux",
1196 .type = QEMU_OPT_BOOL,
1198 .name = "signal",
1199 .type = QEMU_OPT_BOOL,
1201 .name = "name",
1202 .type = QEMU_OPT_STRING,
1204 .name = "debug",
1205 .type = QEMU_OPT_NUMBER,
1207 .name = "size",
1208 .type = QEMU_OPT_SIZE,
1210 .name = "chardev",
1211 .type = QEMU_OPT_STRING,
1213 .name = "append",
1214 .type = QEMU_OPT_BOOL,
1216 .name = "logfile",
1217 .type = QEMU_OPT_STRING,
1219 .name = "logappend",
1220 .type = QEMU_OPT_BOOL,
1222 { /* end of list */ }
1226 bool qemu_chr_has_feature(Chardev *chr,
1227 ChardevFeature feature)
1229 return test_bit(feature, chr->features);
1232 void qemu_chr_set_feature(Chardev *chr,
1233 ChardevFeature feature)
1235 return set_bit(feature, chr->features);
1238 Chardev *qemu_chardev_new(const char *id, const char *typename,
1239 ChardevBackend *backend, Error **errp)
1241 Chardev *chr = NULL;
1242 Error *local_err = NULL;
1243 bool be_opened = true;
1245 assert(g_str_has_prefix(typename, "chardev-"));
1247 chr = CHARDEV(object_new(typename));
1248 chr->label = g_strdup(id);
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;
1257 if (!chr->filename) {
1258 chr->filename = g_strdup(typename + 8);
1260 if (be_opened) {
1261 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1264 return chr;
1267 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
1268 Error **errp)
1270 const ChardevClass *cc;
1271 ChardevReturn *ret;
1272 Chardev *chr;
1274 cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp);
1275 if (!cc) {
1276 return NULL;
1279 chr = qemu_chardev_add(id, object_class_get_name(OBJECT_CLASS(cc)),
1280 backend, errp);
1281 if (!chr) {
1282 return NULL;
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;
1291 return ret;
1294 void qmp_chardev_remove(const char *id, Error **errp)
1296 Chardev *chr;
1298 chr = qemu_chr_find(id);
1299 if (chr == NULL) {
1300 error_setg(errp, "Chardev '%s' not found", id);
1301 return;
1303 if (qemu_chr_is_busy(chr)) {
1304 error_setg(errp, "Chardev '%s' is busy", id);
1305 return;
1307 if (qemu_chr_replay(chr)) {
1308 error_setg(errp,
1309 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1310 return;
1312 qemu_chr_delete(chr);
1315 void qemu_chr_cleanup(void)
1317 Chardev *chr, *tmp;
1319 QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) {
1320 qemu_chr_delete(chr);
1324 static void register_types(void)
1326 type_register_static(&char_type_info);
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
1332 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
1335 type_init(register_types);