qapi: Restrict Xen migration commands to migration.json
[qemu/armbru.git] / chardev / char.c
blob78553125d31103f630e1207b962ab1e900fd9408
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 "qemu/qemu-print.h"
32 #include "chardev/char.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-char.h"
35 #include "qapi/qmp/qerror.h"
36 #include "sysemu/replay.h"
37 #include "qemu/help_option.h"
38 #include "qemu/module.h"
39 #include "qemu/option.h"
40 #include "qemu/id.h"
41 #include "qemu/coroutine.h"
43 #include "chardev-internal.h"
45 /***********************************************************/
46 /* character device */
48 Object *get_chardevs_root(void)
50 return container_get(object_get_root(), "/chardevs");
53 static void chr_be_event(Chardev *s, QEMUChrEvent event)
55 CharBackend *be = s->be;
57 if (!be || !be->chr_event) {
58 return;
61 be->chr_event(be->opaque, event);
64 void qemu_chr_be_event(Chardev *s, QEMUChrEvent event)
66 /* Keep track if the char device is open */
67 switch (event) {
68 case CHR_EVENT_OPENED:
69 s->be_open = 1;
70 break;
71 case CHR_EVENT_CLOSED:
72 s->be_open = 0;
73 break;
74 case CHR_EVENT_BREAK:
75 case CHR_EVENT_MUX_IN:
76 case CHR_EVENT_MUX_OUT:
77 /* Ignore */
78 break;
81 CHARDEV_GET_CLASS(s)->chr_be_event(s, event);
84 /* Not reporting errors from writing to logfile, as logs are
85 * defined to be "best effort" only */
86 static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len)
88 size_t done = 0;
89 ssize_t ret;
91 if (s->logfd < 0) {
92 return;
95 while (done < len) {
96 retry:
97 ret = write(s->logfd, buf + done, len - done);
98 if (ret == -1 && errno == EAGAIN) {
99 g_usleep(100);
100 goto retry;
103 if (ret <= 0) {
104 return;
106 done += ret;
110 static int qemu_chr_write_buffer(Chardev *s,
111 const uint8_t *buf, int len,
112 int *offset, bool write_all)
114 ChardevClass *cc = CHARDEV_GET_CLASS(s);
115 int res = 0;
116 *offset = 0;
118 qemu_mutex_lock(&s->chr_write_lock);
119 while (*offset < len) {
120 retry:
121 res = cc->chr_write(s, buf + *offset, len - *offset);
122 if (res < 0 && errno == EAGAIN && write_all) {
123 if (qemu_in_coroutine()) {
124 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
125 } else {
126 g_usleep(100);
128 goto retry;
131 if (res <= 0) {
132 break;
135 *offset += res;
136 if (!write_all) {
137 break;
140 if (*offset > 0) {
142 * If some data was written by backend, we should
143 * only log what was actually written. This method
144 * may be invoked again to write the remaining
145 * method, thus we'll log the remainder at that time.
147 qemu_chr_write_log(s, buf, *offset);
148 } else if (res < 0) {
150 * If a fatal error was reported by the backend,
151 * assume this method won't be invoked again with
152 * this buffer, so log it all right away.
154 qemu_chr_write_log(s, buf, len);
156 qemu_mutex_unlock(&s->chr_write_lock);
158 return res;
161 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
163 int offset = 0;
164 int res;
166 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
167 replay_char_write_event_load(&res, &offset);
168 assert(offset <= len);
169 qemu_chr_write_buffer(s, buf, offset, &offset, true);
170 return res;
173 res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
175 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
176 replay_char_write_event_save(res, offset);
179 if (res < 0) {
180 return res;
182 return offset;
185 int qemu_chr_be_can_write(Chardev *s)
187 CharBackend *be = s->be;
189 if (!be || !be->chr_can_read) {
190 return 0;
193 return be->chr_can_read(be->opaque);
196 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
198 CharBackend *be = s->be;
200 if (be && be->chr_read) {
201 be->chr_read(be->opaque, buf, len);
205 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
207 if (qemu_chr_replay(s)) {
208 if (replay_mode == REPLAY_MODE_PLAY) {
209 return;
211 replay_chr_be_write(s, buf, len);
212 } else {
213 qemu_chr_be_write_impl(s, buf, len);
217 void qemu_chr_be_update_read_handlers(Chardev *s,
218 GMainContext *context)
220 ChardevClass *cc = CHARDEV_GET_CLASS(s);
222 assert(qemu_chr_has_feature(s, QEMU_CHAR_FEATURE_GCONTEXT)
223 || !context);
224 s->gcontext = context;
225 if (cc->chr_update_read_handler) {
226 cc->chr_update_read_handler(s);
230 int qemu_chr_add_client(Chardev *s, int fd)
232 return CHARDEV_GET_CLASS(s)->chr_add_client ?
233 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
236 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
237 bool *be_opened, Error **errp)
239 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
240 /* Any ChardevCommon member would work */
241 ChardevCommon *common = backend ? backend->u.null.data : NULL;
243 if (common && common->has_logfile) {
244 int flags = O_WRONLY | O_CREAT;
245 if (common->has_logappend &&
246 common->logappend) {
247 flags |= O_APPEND;
248 } else {
249 flags |= O_TRUNC;
251 chr->logfd = qemu_open_old(common->logfile, flags, 0666);
252 if (chr->logfd < 0) {
253 error_setg_errno(errp, errno,
254 "Unable to open logfile %s",
255 common->logfile);
256 return;
260 if (cc->open) {
261 cc->open(chr, backend, be_opened, errp);
265 static void char_init(Object *obj)
267 Chardev *chr = CHARDEV(obj);
269 chr->logfd = -1;
270 qemu_mutex_init(&chr->chr_write_lock);
273 * Assume if chr_update_read_handler is implemented it will
274 * take the updated gcontext into account.
276 if (CHARDEV_GET_CLASS(chr)->chr_update_read_handler) {
277 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT);
282 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
284 return len;
287 static void char_class_init(ObjectClass *oc, void *data)
289 ChardevClass *cc = CHARDEV_CLASS(oc);
291 cc->chr_write = null_chr_write;
292 cc->chr_be_event = chr_be_event;
295 static void char_finalize(Object *obj)
297 Chardev *chr = CHARDEV(obj);
299 if (chr->be) {
300 chr->be->chr = NULL;
302 g_free(chr->filename);
303 g_free(chr->label);
304 if (chr->logfd != -1) {
305 close(chr->logfd);
307 qemu_mutex_destroy(&chr->chr_write_lock);
310 static const TypeInfo char_type_info = {
311 .name = TYPE_CHARDEV,
312 .parent = TYPE_OBJECT,
313 .instance_size = sizeof(Chardev),
314 .instance_init = char_init,
315 .instance_finalize = char_finalize,
316 .abstract = true,
317 .class_size = sizeof(ChardevClass),
318 .class_init = char_class_init,
321 static bool qemu_chr_is_busy(Chardev *s)
323 if (CHARDEV_IS_MUX(s)) {
324 MuxChardev *d = MUX_CHARDEV(s);
325 return d->mux_cnt >= 0;
326 } else {
327 return s->be != NULL;
331 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
333 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
335 if (cc->chr_wait_connected) {
336 return cc->chr_wait_connected(chr, errp);
339 return 0;
342 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
343 bool permit_mux_mon)
345 char host[65], port[33], width[8], height[8];
346 int pos;
347 const char *p;
348 QemuOpts *opts;
349 Error *local_err = NULL;
351 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
352 if (local_err) {
353 error_report_err(local_err);
354 return NULL;
357 if (strstart(filename, "mon:", &p)) {
358 if (!permit_mux_mon) {
359 error_report("mon: isn't supported in this context");
360 return NULL;
362 filename = p;
363 qemu_opt_set(opts, "mux", "on", &error_abort);
364 if (strcmp(filename, "stdio") == 0) {
365 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
366 * but pass it to the guest. Handle this only for compat syntax,
367 * for -chardev syntax we have special option for this.
368 * This is what -nographic did, redirecting+muxing serial+monitor
369 * to stdio causing Ctrl+C to be passed to guest. */
370 qemu_opt_set(opts, "signal", "off", &error_abort);
374 if (strcmp(filename, "null") == 0 ||
375 strcmp(filename, "pty") == 0 ||
376 strcmp(filename, "msmouse") == 0 ||
377 strcmp(filename, "wctablet") == 0 ||
378 strcmp(filename, "braille") == 0 ||
379 strcmp(filename, "testdev") == 0 ||
380 strcmp(filename, "stdio") == 0) {
381 qemu_opt_set(opts, "backend", filename, &error_abort);
382 return opts;
384 if (strstart(filename, "vc", &p)) {
385 qemu_opt_set(opts, "backend", "vc", &error_abort);
386 if (*p == ':') {
387 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
388 /* pixels */
389 qemu_opt_set(opts, "width", width, &error_abort);
390 qemu_opt_set(opts, "height", height, &error_abort);
391 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
392 /* chars */
393 qemu_opt_set(opts, "cols", width, &error_abort);
394 qemu_opt_set(opts, "rows", height, &error_abort);
395 } else {
396 goto fail;
399 return opts;
401 if (strcmp(filename, "con:") == 0) {
402 qemu_opt_set(opts, "backend", "console", &error_abort);
403 return opts;
405 if (strstart(filename, "COM", NULL)) {
406 qemu_opt_set(opts, "backend", "serial", &error_abort);
407 qemu_opt_set(opts, "path", filename, &error_abort);
408 return opts;
410 if (strstart(filename, "file:", &p)) {
411 qemu_opt_set(opts, "backend", "file", &error_abort);
412 qemu_opt_set(opts, "path", p, &error_abort);
413 return opts;
415 if (strstart(filename, "pipe:", &p)) {
416 qemu_opt_set(opts, "backend", "pipe", &error_abort);
417 qemu_opt_set(opts, "path", p, &error_abort);
418 return opts;
420 if (strstart(filename, "tcp:", &p) ||
421 strstart(filename, "telnet:", &p) ||
422 strstart(filename, "tn3270:", &p) ||
423 strstart(filename, "websocket:", &p)) {
424 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
425 host[0] = 0;
426 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
427 goto fail;
429 qemu_opt_set(opts, "backend", "socket", &error_abort);
430 qemu_opt_set(opts, "host", host, &error_abort);
431 qemu_opt_set(opts, "port", port, &error_abort);
432 if (p[pos] == ',') {
433 if (!qemu_opts_do_parse(opts, p + pos + 1, NULL, &local_err)) {
434 error_report_err(local_err);
435 goto fail;
438 if (strstart(filename, "telnet:", &p)) {
439 qemu_opt_set(opts, "telnet", "on", &error_abort);
440 } else if (strstart(filename, "tn3270:", &p)) {
441 qemu_opt_set(opts, "tn3270", "on", &error_abort);
442 } else if (strstart(filename, "websocket:", &p)) {
443 qemu_opt_set(opts, "websocket", "on", &error_abort);
445 return opts;
447 if (strstart(filename, "udp:", &p)) {
448 qemu_opt_set(opts, "backend", "udp", &error_abort);
449 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
450 host[0] = 0;
451 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
452 goto fail;
455 qemu_opt_set(opts, "host", host, &error_abort);
456 qemu_opt_set(opts, "port", port, &error_abort);
457 if (p[pos] == '@') {
458 p += pos + 1;
459 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
460 host[0] = 0;
461 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
462 goto fail;
465 qemu_opt_set(opts, "localaddr", host, &error_abort);
466 qemu_opt_set(opts, "localport", port, &error_abort);
468 return opts;
470 if (strstart(filename, "unix:", &p)) {
471 qemu_opt_set(opts, "backend", "socket", &error_abort);
472 if (!qemu_opts_do_parse(opts, p, "path", &local_err)) {
473 error_report_err(local_err);
474 goto fail;
476 return opts;
478 if (strstart(filename, "/dev/parport", NULL) ||
479 strstart(filename, "/dev/ppi", NULL)) {
480 qemu_opt_set(opts, "backend", "parallel", &error_abort);
481 qemu_opt_set(opts, "path", filename, &error_abort);
482 return opts;
484 if (strstart(filename, "/dev/", NULL)) {
485 qemu_opt_set(opts, "backend", "serial", &error_abort);
486 qemu_opt_set(opts, "path", filename, &error_abort);
487 return opts;
490 error_report("'%s' is not a valid char driver", filename);
492 fail:
493 qemu_opts_del(opts);
494 return NULL;
497 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
499 const char *logfile = qemu_opt_get(opts, "logfile");
501 backend->has_logfile = logfile != NULL;
502 backend->logfile = g_strdup(logfile);
504 backend->has_logappend = true;
505 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
508 static const ChardevClass *char_get_class(const char *driver, Error **errp)
510 ObjectClass *oc;
511 const ChardevClass *cc;
512 char *typename = g_strdup_printf("chardev-%s", driver);
514 oc = module_object_class_by_name(typename);
515 g_free(typename);
517 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
518 error_setg(errp, "'%s' is not a valid char driver name", driver);
519 return NULL;
522 if (object_class_is_abstract(oc)) {
523 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
524 "abstract device type");
525 return NULL;
528 cc = CHARDEV_CLASS(oc);
529 if (cc->internal) {
530 error_setg(errp, "'%s' is not a valid char driver name", driver);
531 return NULL;
534 return cc;
537 static const struct ChardevAlias {
538 const char *typename;
539 const char *alias;
540 } chardev_alias_table[] = {
541 #ifdef HAVE_CHARDEV_PARPORT
542 { "parallel", "parport" },
543 #endif
544 #ifdef HAVE_CHARDEV_SERIAL
545 { "serial", "tty" },
546 #endif
549 typedef struct ChadevClassFE {
550 void (*fn)(const char *name, void *opaque);
551 void *opaque;
552 } ChadevClassFE;
554 static void
555 chardev_class_foreach(ObjectClass *klass, void *opaque)
557 ChadevClassFE *fe = opaque;
559 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
560 if (CHARDEV_CLASS(klass)->internal) {
561 return;
564 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
567 static void
568 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
570 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
571 int i;
573 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
575 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
576 fn(chardev_alias_table[i].alias, opaque);
580 static void
581 help_string_append(const char *name, void *opaque)
583 GString *str = opaque;
585 g_string_append_printf(str, "\n %s", name);
588 static const char *chardev_alias_translate(const char *name)
590 int i;
591 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
592 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
593 return chardev_alias_table[i].typename;
596 return name;
599 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
601 Error *local_err = NULL;
602 const ChardevClass *cc;
603 ChardevBackend *backend = NULL;
604 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
606 if (name == NULL) {
607 error_setg(errp, "chardev: \"%s\" missing backend",
608 qemu_opts_id(opts));
609 return NULL;
612 cc = char_get_class(name, errp);
613 if (cc == NULL) {
614 return NULL;
617 backend = g_new0(ChardevBackend, 1);
618 backend->type = CHARDEV_BACKEND_KIND_NULL;
620 if (cc->parse) {
621 cc->parse(opts, backend, &local_err);
622 if (local_err) {
623 error_propagate(errp, local_err);
624 qapi_free_ChardevBackend(backend);
625 return NULL;
627 } else {
628 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
629 qemu_chr_parse_common(opts, ccom);
630 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
633 return backend;
636 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
637 Error **errp)
639 const ChardevClass *cc;
640 Chardev *chr = NULL;
641 ChardevBackend *backend = NULL;
642 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
643 const char *id = qemu_opts_id(opts);
644 char *bid = NULL;
646 if (name && is_help_option(name)) {
647 GString *str = g_string_new("");
649 chardev_name_foreach(help_string_append, str);
651 qemu_printf("Available chardev backend types: %s\n", str->str);
652 g_string_free(str, true);
653 return NULL;
656 if (id == NULL) {
657 error_setg(errp, "chardev: no id specified");
658 return NULL;
661 backend = qemu_chr_parse_opts(opts, errp);
662 if (backend == NULL) {
663 return NULL;
666 cc = char_get_class(name, errp);
667 if (cc == NULL) {
668 goto out;
671 if (qemu_opt_get_bool(opts, "mux", 0)) {
672 bid = g_strdup_printf("%s-base", id);
675 chr = qemu_chardev_new(bid ? bid : id,
676 object_class_get_name(OBJECT_CLASS(cc)),
677 backend, context, errp);
679 if (chr == NULL) {
680 goto out;
683 if (bid) {
684 Chardev *mux;
685 qapi_free_ChardevBackend(backend);
686 backend = g_new0(ChardevBackend, 1);
687 backend->type = CHARDEV_BACKEND_KIND_MUX;
688 backend->u.mux.data = g_new0(ChardevMux, 1);
689 backend->u.mux.data->chardev = g_strdup(bid);
690 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
691 if (mux == NULL) {
692 object_unparent(OBJECT(chr));
693 chr = NULL;
694 goto out;
696 chr = mux;
699 out:
700 qapi_free_ChardevBackend(backend);
701 g_free(bid);
702 return chr;
705 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
706 bool permit_mux_mon, GMainContext *context)
708 const char *p;
709 Chardev *chr;
710 QemuOpts *opts;
711 Error *err = NULL;
713 if (strstart(filename, "chardev:", &p)) {
714 return qemu_chr_find(p);
717 opts = qemu_chr_parse_compat(label, filename, permit_mux_mon);
718 if (!opts)
719 return NULL;
721 chr = qemu_chr_new_from_opts(opts, context, &err);
722 if (!chr) {
723 error_report_err(err);
724 goto out;
727 if (qemu_opt_get_bool(opts, "mux", 0)) {
728 assert(permit_mux_mon);
729 monitor_init_hmp(chr, true, &err);
730 if (err) {
731 error_report_err(err);
732 object_unparent(OBJECT(chr));
733 chr = NULL;
734 goto out;
738 out:
739 qemu_opts_del(opts);
740 return chr;
743 static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
744 const char *filename,
745 bool permit_mux_mon,
746 GMainContext *context)
748 Chardev *chr;
749 chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon, context);
750 if (chr) {
751 if (replay_mode != REPLAY_MODE_NONE) {
752 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
754 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
755 error_report("Replay: ioctl is not supported "
756 "for serial devices yet");
758 replay_register_char_driver(chr);
760 return chr;
763 Chardev *qemu_chr_new(const char *label, const char *filename,
764 GMainContext *context)
766 return qemu_chr_new_permit_mux_mon(label, filename, false, context);
769 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
770 GMainContext *context)
772 return qemu_chr_new_permit_mux_mon(label, filename, true, context);
775 static int qmp_query_chardev_foreach(Object *obj, void *data)
777 Chardev *chr = CHARDEV(obj);
778 ChardevInfoList **list = data;
779 ChardevInfoList *info = g_malloc0(sizeof(*info));
781 info->value = g_malloc0(sizeof(*info->value));
782 info->value->label = g_strdup(chr->label);
783 info->value->filename = g_strdup(chr->filename);
784 info->value->frontend_open = chr->be && chr->be->fe_open;
786 info->next = *list;
787 *list = info;
789 return 0;
792 ChardevInfoList *qmp_query_chardev(Error **errp)
794 ChardevInfoList *chr_list = NULL;
796 object_child_foreach(get_chardevs_root(),
797 qmp_query_chardev_foreach, &chr_list);
799 return chr_list;
802 static void
803 qmp_prepend_backend(const char *name, void *opaque)
805 ChardevBackendInfoList **list = opaque;
806 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
808 info->value = g_malloc0(sizeof(*info->value));
809 info->value->name = g_strdup(name);
810 info->next = *list;
811 *list = info;
814 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
816 ChardevBackendInfoList *backend_list = NULL;
818 chardev_name_foreach(qmp_prepend_backend, &backend_list);
820 return backend_list;
823 Chardev *qemu_chr_find(const char *name)
825 Object *obj = object_resolve_path_component(get_chardevs_root(), name);
827 return obj ? CHARDEV(obj) : NULL;
830 QemuOptsList qemu_chardev_opts = {
831 .name = "chardev",
832 .implied_opt_name = "backend",
833 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
834 .desc = {
836 .name = "backend",
837 .type = QEMU_OPT_STRING,
839 .name = "path",
840 .type = QEMU_OPT_STRING,
842 .name = "host",
843 .type = QEMU_OPT_STRING,
845 .name = "port",
846 .type = QEMU_OPT_STRING,
848 .name = "fd",
849 .type = QEMU_OPT_STRING,
851 .name = "localaddr",
852 .type = QEMU_OPT_STRING,
854 .name = "localport",
855 .type = QEMU_OPT_STRING,
857 .name = "to",
858 .type = QEMU_OPT_NUMBER,
860 .name = "ipv4",
861 .type = QEMU_OPT_BOOL,
863 .name = "ipv6",
864 .type = QEMU_OPT_BOOL,
866 .name = "wait",
867 .type = QEMU_OPT_BOOL,
869 .name = "server",
870 .type = QEMU_OPT_BOOL,
872 .name = "delay",
873 .type = QEMU_OPT_BOOL,
875 .name = "reconnect",
876 .type = QEMU_OPT_NUMBER,
878 .name = "telnet",
879 .type = QEMU_OPT_BOOL,
881 .name = "tn3270",
882 .type = QEMU_OPT_BOOL,
884 .name = "tls-creds",
885 .type = QEMU_OPT_STRING,
887 .name = "tls-authz",
888 .type = QEMU_OPT_STRING,
890 .name = "websocket",
891 .type = QEMU_OPT_BOOL,
893 .name = "width",
894 .type = QEMU_OPT_NUMBER,
896 .name = "height",
897 .type = QEMU_OPT_NUMBER,
899 .name = "cols",
900 .type = QEMU_OPT_NUMBER,
902 .name = "rows",
903 .type = QEMU_OPT_NUMBER,
905 .name = "mux",
906 .type = QEMU_OPT_BOOL,
908 .name = "signal",
909 .type = QEMU_OPT_BOOL,
911 .name = "name",
912 .type = QEMU_OPT_STRING,
914 .name = "debug",
915 .type = QEMU_OPT_NUMBER,
917 .name = "size",
918 .type = QEMU_OPT_SIZE,
920 .name = "chardev",
921 .type = QEMU_OPT_STRING,
923 .name = "append",
924 .type = QEMU_OPT_BOOL,
926 .name = "logfile",
927 .type = QEMU_OPT_STRING,
929 .name = "logappend",
930 .type = QEMU_OPT_BOOL,
932 .name = "tight",
933 .type = QEMU_OPT_BOOL,
934 .def_value_str = "on",
936 .name = "abstract",
937 .type = QEMU_OPT_BOOL,
939 { /* end of list */ }
943 bool qemu_chr_has_feature(Chardev *chr,
944 ChardevFeature feature)
946 return test_bit(feature, chr->features);
949 void qemu_chr_set_feature(Chardev *chr,
950 ChardevFeature feature)
952 return set_bit(feature, chr->features);
955 static Chardev *chardev_new(const char *id, const char *typename,
956 ChardevBackend *backend,
957 GMainContext *gcontext,
958 Error **errp)
960 Object *obj;
961 Chardev *chr = NULL;
962 Error *local_err = NULL;
963 bool be_opened = true;
965 assert(g_str_has_prefix(typename, "chardev-"));
967 obj = object_new(typename);
968 chr = CHARDEV(obj);
969 chr->label = g_strdup(id);
970 chr->gcontext = gcontext;
972 qemu_char_open(chr, backend, &be_opened, &local_err);
973 if (local_err) {
974 goto end;
977 if (!chr->filename) {
978 chr->filename = g_strdup(typename + 8);
980 if (be_opened) {
981 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
984 if (id) {
985 object_property_try_add_child(get_chardevs_root(), id, obj,
986 &local_err);
987 if (local_err) {
988 goto end;
990 object_unref(obj);
993 end:
994 if (local_err) {
995 error_propagate(errp, local_err);
996 object_unref(obj);
997 return NULL;
1000 return chr;
1003 Chardev *qemu_chardev_new(const char *id, const char *typename,
1004 ChardevBackend *backend,
1005 GMainContext *gcontext,
1006 Error **errp)
1008 g_autofree char *genid = NULL;
1010 if (!id) {
1011 genid = id_generate(ID_CHR);
1012 id = genid;
1015 return chardev_new(id, typename, backend, gcontext, errp);
1018 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
1019 Error **errp)
1021 const ChardevClass *cc;
1022 ChardevReturn *ret;
1023 Chardev *chr;
1025 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1026 if (!cc) {
1027 return NULL;
1030 chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
1031 backend, NULL, errp);
1032 if (!chr) {
1033 return NULL;
1036 ret = g_new0(ChardevReturn, 1);
1037 if (CHARDEV_IS_PTY(chr)) {
1038 ret->pty = g_strdup(chr->filename + 4);
1039 ret->has_pty = true;
1042 return ret;
1045 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
1046 Error **errp)
1048 CharBackend *be;
1049 const ChardevClass *cc;
1050 Chardev *chr, *chr_new;
1051 bool closed_sent = false;
1052 ChardevReturn *ret;
1054 chr = qemu_chr_find(id);
1055 if (!chr) {
1056 error_setg(errp, "Chardev '%s' does not exist", id);
1057 return NULL;
1060 if (CHARDEV_IS_MUX(chr)) {
1061 error_setg(errp, "Mux device hotswap not supported yet");
1062 return NULL;
1065 if (qemu_chr_replay(chr)) {
1066 error_setg(errp,
1067 "Chardev '%s' cannot be changed in record/replay mode", id);
1068 return NULL;
1071 be = chr->be;
1072 if (!be) {
1073 /* easy case */
1074 object_unparent(OBJECT(chr));
1075 return qmp_chardev_add(id, backend, errp);
1078 if (!be->chr_be_change) {
1079 error_setg(errp, "Chardev user does not support chardev hotswap");
1080 return NULL;
1083 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1084 if (!cc) {
1085 return NULL;
1088 chr_new = chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)),
1089 backend, chr->gcontext, errp);
1090 if (!chr_new) {
1091 return NULL;
1093 chr_new->label = g_strdup(id);
1095 if (chr->be_open && !chr_new->be_open) {
1096 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1097 closed_sent = true;
1100 chr->be = NULL;
1101 qemu_chr_fe_init(be, chr_new, &error_abort);
1103 if (be->chr_be_change(be->opaque) < 0) {
1104 error_setg(errp, "Chardev '%s' change failed", chr_new->label);
1105 chr_new->be = NULL;
1106 qemu_chr_fe_init(be, chr, &error_abort);
1107 if (closed_sent) {
1108 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1110 object_unref(OBJECT(chr_new));
1111 return NULL;
1114 object_unparent(OBJECT(chr));
1115 object_property_add_child(get_chardevs_root(), chr_new->label,
1116 OBJECT(chr_new));
1117 object_unref(OBJECT(chr_new));
1119 ret = g_new0(ChardevReturn, 1);
1120 if (CHARDEV_IS_PTY(chr_new)) {
1121 ret->pty = g_strdup(chr_new->filename + 4);
1122 ret->has_pty = true;
1125 return ret;
1128 void qmp_chardev_remove(const char *id, Error **errp)
1130 Chardev *chr;
1132 chr = qemu_chr_find(id);
1133 if (chr == NULL) {
1134 error_setg(errp, "Chardev '%s' not found", id);
1135 return;
1137 if (qemu_chr_is_busy(chr)) {
1138 error_setg(errp, "Chardev '%s' is busy", id);
1139 return;
1141 if (qemu_chr_replay(chr)) {
1142 error_setg(errp,
1143 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1144 return;
1146 object_unparent(OBJECT(chr));
1149 void qmp_chardev_send_break(const char *id, Error **errp)
1151 Chardev *chr;
1153 chr = qemu_chr_find(id);
1154 if (chr == NULL) {
1155 error_setg(errp, "Chardev '%s' not found", id);
1156 return;
1158 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
1162 * Add a timeout callback for the chardev (in milliseconds), return
1163 * the GSource object created. Please use this to add timeout hook for
1164 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1165 * make sure the gcontext that the task bound to is correct.
1167 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
1168 GSourceFunc func, void *private)
1170 GSource *source = g_timeout_source_new(ms);
1172 assert(func);
1173 g_source_set_callback(source, func, private, NULL);
1174 g_source_attach(source, chr->gcontext);
1176 return source;
1179 void qemu_chr_cleanup(void)
1181 object_unparent(get_chardevs_root());
1184 static void register_types(void)
1186 type_register_static(&char_type_info);
1189 type_init(register_types);