vhost-scsi: fix memleak of vsc->inflight
[qemu.git] / chardev / char.c
blob4c5de164025d15b27233a90337f181dc7032a9fe
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 "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "qemu/qemu-print.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/module.h"
38 #include "qemu/option.h"
39 #include "qemu/id.h"
40 #include "qemu/coroutine.h"
41 #include "qemu/yank.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, const 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, const 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->logfile) {
244 int flags = O_WRONLY;
245 if (common->has_logappend &&
246 common->logappend) {
247 flags |= O_APPEND;
248 } else {
249 flags |= O_TRUNC;
251 chr->logfd = qemu_create(common->logfile, flags, 0666, errp);
252 if (chr->logfd < 0) {
253 return;
257 if (cc->open) {
258 cc->open(chr, backend, be_opened, errp);
262 static void char_init(Object *obj)
264 Chardev *chr = CHARDEV(obj);
266 chr->handover_yank_instance = false;
267 chr->logfd = -1;
268 qemu_mutex_init(&chr->chr_write_lock);
271 * Assume if chr_update_read_handler is implemented it will
272 * take the updated gcontext into account.
274 if (CHARDEV_GET_CLASS(chr)->chr_update_read_handler) {
275 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT);
280 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
282 return len;
285 static void char_class_init(ObjectClass *oc, void *data)
287 ChardevClass *cc = CHARDEV_CLASS(oc);
289 cc->chr_write = null_chr_write;
290 cc->chr_be_event = chr_be_event;
293 static void char_finalize(Object *obj)
295 Chardev *chr = CHARDEV(obj);
297 if (chr->be) {
298 chr->be->chr = NULL;
300 g_free(chr->filename);
301 g_free(chr->label);
302 if (chr->logfd != -1) {
303 close(chr->logfd);
305 qemu_mutex_destroy(&chr->chr_write_lock);
308 static const TypeInfo char_type_info = {
309 .name = TYPE_CHARDEV,
310 .parent = TYPE_OBJECT,
311 .instance_size = sizeof(Chardev),
312 .instance_init = char_init,
313 .instance_finalize = char_finalize,
314 .abstract = true,
315 .class_size = sizeof(ChardevClass),
316 .class_init = char_class_init,
319 static bool qemu_chr_is_busy(Chardev *s)
321 if (CHARDEV_IS_MUX(s)) {
322 MuxChardev *d = MUX_CHARDEV(s);
323 return d->mux_cnt >= 0;
324 } else {
325 return s->be != NULL;
329 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
331 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
333 if (cc->chr_wait_connected) {
334 return cc->chr_wait_connected(chr, errp);
337 return 0;
340 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
341 bool permit_mux_mon)
343 char host[65], port[33], width[8], height[8];
344 int pos;
345 const char *p;
346 QemuOpts *opts;
347 Error *local_err = NULL;
349 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
350 if (local_err) {
351 error_report_err(local_err);
352 return NULL;
355 if (strstart(filename, "mon:", &p)) {
356 if (!permit_mux_mon) {
357 error_report("mon: isn't supported in this context");
358 return NULL;
360 filename = p;
361 qemu_opt_set(opts, "mux", "on", &error_abort);
362 if (strcmp(filename, "stdio") == 0) {
363 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
364 * but pass it to the guest. Handle this only for compat syntax,
365 * for -chardev syntax we have special option for this.
366 * This is what -nographic did, redirecting+muxing serial+monitor
367 * to stdio causing Ctrl+C to be passed to guest. */
368 qemu_opt_set(opts, "signal", "off", &error_abort);
372 if (strcmp(filename, "null") == 0 ||
373 strcmp(filename, "pty") == 0 ||
374 strcmp(filename, "msmouse") == 0 ||
375 strcmp(filename, "wctablet") == 0 ||
376 strcmp(filename, "braille") == 0 ||
377 strcmp(filename, "testdev") == 0 ||
378 strcmp(filename, "stdio") == 0) {
379 qemu_opt_set(opts, "backend", filename, &error_abort);
380 return opts;
382 if (strstart(filename, "vc", &p)) {
383 qemu_opt_set(opts, "backend", "vc", &error_abort);
384 if (*p == ':') {
385 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
386 /* pixels */
387 qemu_opt_set(opts, "width", width, &error_abort);
388 qemu_opt_set(opts, "height", height, &error_abort);
389 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
390 /* chars */
391 qemu_opt_set(opts, "cols", width, &error_abort);
392 qemu_opt_set(opts, "rows", height, &error_abort);
393 } else {
394 goto fail;
397 return opts;
399 if (strcmp(filename, "con:") == 0) {
400 qemu_opt_set(opts, "backend", "console", &error_abort);
401 return opts;
403 if (strstart(filename, "COM", NULL)) {
404 qemu_opt_set(opts, "backend", "serial", &error_abort);
405 qemu_opt_set(opts, "path", filename, &error_abort);
406 return opts;
408 if (strstart(filename, "file:", &p)) {
409 qemu_opt_set(opts, "backend", "file", &error_abort);
410 qemu_opt_set(opts, "path", p, &error_abort);
411 return opts;
413 if (strstart(filename, "pipe:", &p)) {
414 qemu_opt_set(opts, "backend", "pipe", &error_abort);
415 qemu_opt_set(opts, "path", p, &error_abort);
416 return opts;
418 if (strstart(filename, "tcp:", &p) ||
419 strstart(filename, "telnet:", &p) ||
420 strstart(filename, "tn3270:", &p) ||
421 strstart(filename, "websocket:", &p)) {
422 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
423 host[0] = 0;
424 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
425 goto fail;
427 qemu_opt_set(opts, "backend", "socket", &error_abort);
428 qemu_opt_set(opts, "host", host, &error_abort);
429 qemu_opt_set(opts, "port", port, &error_abort);
430 if (p[pos] == ',') {
431 if (!qemu_opts_do_parse(opts, p + pos + 1, NULL, &local_err)) {
432 error_report_err(local_err);
433 goto fail;
436 if (strstart(filename, "telnet:", &p)) {
437 qemu_opt_set(opts, "telnet", "on", &error_abort);
438 } else if (strstart(filename, "tn3270:", &p)) {
439 qemu_opt_set(opts, "tn3270", "on", &error_abort);
440 } else if (strstart(filename, "websocket:", &p)) {
441 qemu_opt_set(opts, "websocket", "on", &error_abort);
443 return opts;
445 if (strstart(filename, "udp:", &p)) {
446 qemu_opt_set(opts, "backend", "udp", &error_abort);
447 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
448 host[0] = 0;
449 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
450 goto fail;
453 qemu_opt_set(opts, "host", host, &error_abort);
454 qemu_opt_set(opts, "port", port, &error_abort);
455 if (p[pos] == '@') {
456 p += pos + 1;
457 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
458 host[0] = 0;
459 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
460 goto fail;
463 qemu_opt_set(opts, "localaddr", host, &error_abort);
464 qemu_opt_set(opts, "localport", port, &error_abort);
466 return opts;
468 if (strstart(filename, "unix:", &p)) {
469 qemu_opt_set(opts, "backend", "socket", &error_abort);
470 if (!qemu_opts_do_parse(opts, p, "path", &local_err)) {
471 error_report_err(local_err);
472 goto fail;
474 return opts;
476 if (strstart(filename, "/dev/parport", NULL) ||
477 strstart(filename, "/dev/ppi", NULL)) {
478 qemu_opt_set(opts, "backend", "parallel", &error_abort);
479 qemu_opt_set(opts, "path", filename, &error_abort);
480 return opts;
482 if (strstart(filename, "/dev/", NULL)) {
483 qemu_opt_set(opts, "backend", "serial", &error_abort);
484 qemu_opt_set(opts, "path", filename, &error_abort);
485 return opts;
488 error_report("'%s' is not a valid char driver", filename);
490 fail:
491 qemu_opts_del(opts);
492 return NULL;
495 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
497 const char *logfile = qemu_opt_get(opts, "logfile");
499 backend->logfile = g_strdup(logfile);
500 backend->has_logappend = true;
501 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
504 static const ChardevClass *char_get_class(const char *driver, Error **errp)
506 ObjectClass *oc;
507 const ChardevClass *cc;
508 char *typename = g_strdup_printf("chardev-%s", driver);
510 oc = module_object_class_by_name(typename);
511 g_free(typename);
513 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
514 error_setg(errp, "'%s' is not a valid char driver name", driver);
515 return NULL;
518 if (object_class_is_abstract(oc)) {
519 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
520 "an abstract device type");
521 return NULL;
524 cc = CHARDEV_CLASS(oc);
525 if (cc->internal) {
526 error_setg(errp, "'%s' is not a valid char driver name", driver);
527 return NULL;
530 return cc;
533 static struct ChardevAlias {
534 const char *typename;
535 const char *alias;
536 bool deprecation_warning_printed;
537 } chardev_alias_table[] = {
538 #ifdef HAVE_CHARDEV_PARPORT
539 { "parallel", "parport" },
540 #endif
541 #ifdef HAVE_CHARDEV_SERIAL
542 { "serial", "tty" },
543 #endif
546 typedef struct ChadevClassFE {
547 void (*fn)(const char *name, void *opaque);
548 void *opaque;
549 } ChadevClassFE;
551 static void
552 chardev_class_foreach(ObjectClass *klass, void *opaque)
554 ChadevClassFE *fe = opaque;
556 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
557 if (CHARDEV_CLASS(klass)->internal) {
558 return;
561 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
564 static void
565 chardev_name_foreach(void (*fn)(const char *name, void *opaque),
566 void *opaque)
568 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
570 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
573 static void
574 help_string_append(const char *name, void *opaque)
576 GString *str = opaque;
578 g_string_append_printf(str, "\n %s", name);
581 static const char *chardev_alias_translate(const char *name)
583 int i;
584 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
585 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
586 if (!chardev_alias_table[i].deprecation_warning_printed) {
587 warn_report("The alias '%s' is deprecated, use '%s' instead",
588 name, chardev_alias_table[i].typename);
589 chardev_alias_table[i].deprecation_warning_printed = true;
591 return chardev_alias_table[i].typename;
594 return name;
597 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
599 Error *local_err = NULL;
600 const ChardevClass *cc;
601 ChardevBackend *backend = NULL;
602 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
604 if (name == NULL) {
605 error_setg(errp, "chardev: \"%s\" missing backend",
606 qemu_opts_id(opts));
607 return NULL;
610 cc = char_get_class(name, errp);
611 if (cc == NULL) {
612 return NULL;
615 backend = g_new0(ChardevBackend, 1);
616 backend->type = CHARDEV_BACKEND_KIND_NULL;
618 if (cc->parse) {
619 cc->parse(opts, backend, &local_err);
620 if (local_err) {
621 error_propagate(errp, local_err);
622 qapi_free_ChardevBackend(backend);
623 return NULL;
625 } else {
626 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
627 qemu_chr_parse_common(opts, ccom);
628 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
631 return backend;
634 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
635 Error **errp)
637 const ChardevClass *cc;
638 Chardev *chr = NULL;
639 ChardevBackend *backend = NULL;
640 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
641 const char *id = qemu_opts_id(opts);
642 char *bid = NULL;
644 if (name && is_help_option(name)) {
645 GString *str = g_string_new("");
647 chardev_name_foreach(help_string_append, str);
649 qemu_printf("Available chardev backend types: %s\n", str->str);
650 g_string_free(str, true);
651 return NULL;
654 if (id == NULL) {
655 error_setg(errp, "chardev: no id specified");
656 return NULL;
659 backend = qemu_chr_parse_opts(opts, errp);
660 if (backend == NULL) {
661 return NULL;
664 cc = char_get_class(name, errp);
665 if (cc == NULL) {
666 goto out;
669 if (qemu_opt_get_bool(opts, "mux", 0)) {
670 bid = g_strdup_printf("%s-base", id);
673 chr = qemu_chardev_new(bid ? bid : id,
674 object_class_get_name(OBJECT_CLASS(cc)),
675 backend, context, errp);
677 if (chr == NULL) {
678 goto out;
681 if (bid) {
682 Chardev *mux;
683 qapi_free_ChardevBackend(backend);
684 backend = g_new0(ChardevBackend, 1);
685 backend->type = CHARDEV_BACKEND_KIND_MUX;
686 backend->u.mux.data = g_new0(ChardevMux, 1);
687 backend->u.mux.data->chardev = g_strdup(bid);
688 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
689 if (mux == NULL) {
690 object_unparent(OBJECT(chr));
691 chr = NULL;
692 goto out;
694 chr = mux;
697 out:
698 qapi_free_ChardevBackend(backend);
699 g_free(bid);
700 return chr;
703 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
704 bool permit_mux_mon, GMainContext *context)
706 const char *p;
707 Chardev *chr;
708 QemuOpts *opts;
709 Error *err = NULL;
711 if (strstart(filename, "chardev:", &p)) {
712 return qemu_chr_find(p);
715 opts = qemu_chr_parse_compat(label, filename, permit_mux_mon);
716 if (!opts)
717 return NULL;
719 chr = qemu_chr_new_from_opts(opts, context, &err);
720 if (!chr) {
721 error_report_err(err);
722 goto out;
725 if (qemu_opt_get_bool(opts, "mux", 0)) {
726 assert(permit_mux_mon);
727 monitor_init_hmp(chr, true, &err);
728 if (err) {
729 error_report_err(err);
730 object_unparent(OBJECT(chr));
731 chr = NULL;
732 goto out;
736 out:
737 qemu_opts_del(opts);
738 return chr;
741 static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
742 const char *filename,
743 bool permit_mux_mon,
744 GMainContext *context)
746 Chardev *chr;
747 chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon, context);
748 if (chr) {
749 if (replay_mode != REPLAY_MODE_NONE) {
750 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
752 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
753 error_report("Replay: ioctl is not supported "
754 "for serial devices yet");
756 replay_register_char_driver(chr);
758 return chr;
761 Chardev *qemu_chr_new(const char *label, const char *filename,
762 GMainContext *context)
764 return qemu_chr_new_permit_mux_mon(label, filename, false, context);
767 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
768 GMainContext *context)
770 return qemu_chr_new_permit_mux_mon(label, filename, true, context);
773 static int qmp_query_chardev_foreach(Object *obj, void *data)
775 Chardev *chr = CHARDEV(obj);
776 ChardevInfoList **list = data;
777 ChardevInfo *value = g_malloc0(sizeof(*value));
779 value->label = g_strdup(chr->label);
780 value->filename = g_strdup(chr->filename);
781 value->frontend_open = chr->be && chr->be->fe_open;
783 QAPI_LIST_PREPEND(*list, value);
785 return 0;
788 ChardevInfoList *qmp_query_chardev(Error **errp)
790 ChardevInfoList *chr_list = NULL;
792 object_child_foreach(get_chardevs_root(),
793 qmp_query_chardev_foreach, &chr_list);
795 return chr_list;
798 static void
799 qmp_prepend_backend(const char *name, void *opaque)
801 ChardevBackendInfoList **list = opaque;
802 ChardevBackendInfo *value;
804 value = g_new0(ChardevBackendInfo, 1);
805 value->name = g_strdup(name);
806 QAPI_LIST_PREPEND(*list, value);
809 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
811 ChardevBackendInfoList *backend_list = NULL;
813 chardev_name_foreach(qmp_prepend_backend, &backend_list);
815 return backend_list;
818 Chardev *qemu_chr_find(const char *name)
820 Object *obj = object_resolve_path_component(get_chardevs_root(), name);
822 return obj ? CHARDEV(obj) : NULL;
825 QemuOptsList qemu_chardev_opts = {
826 .name = "chardev",
827 .implied_opt_name = "backend",
828 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
829 .desc = {
831 .name = "backend",
832 .type = QEMU_OPT_STRING,
834 .name = "path",
835 .type = QEMU_OPT_STRING,
837 .name = "host",
838 .type = QEMU_OPT_STRING,
840 .name = "port",
841 .type = QEMU_OPT_STRING,
843 .name = "fd",
844 .type = QEMU_OPT_STRING,
846 .name = "localaddr",
847 .type = QEMU_OPT_STRING,
849 .name = "localport",
850 .type = QEMU_OPT_STRING,
852 .name = "to",
853 .type = QEMU_OPT_NUMBER,
855 .name = "ipv4",
856 .type = QEMU_OPT_BOOL,
858 .name = "ipv6",
859 .type = QEMU_OPT_BOOL,
861 .name = "wait",
862 .type = QEMU_OPT_BOOL,
864 .name = "server",
865 .type = QEMU_OPT_BOOL,
867 .name = "delay",
868 .type = QEMU_OPT_BOOL,
870 .name = "nodelay",
871 .type = QEMU_OPT_BOOL,
873 .name = "reconnect",
874 .type = QEMU_OPT_NUMBER,
876 .name = "telnet",
877 .type = QEMU_OPT_BOOL,
879 .name = "tn3270",
880 .type = QEMU_OPT_BOOL,
882 .name = "tls-creds",
883 .type = QEMU_OPT_STRING,
885 .name = "tls-authz",
886 .type = QEMU_OPT_STRING,
888 .name = "websocket",
889 .type = QEMU_OPT_BOOL,
891 .name = "width",
892 .type = QEMU_OPT_NUMBER,
894 .name = "height",
895 .type = QEMU_OPT_NUMBER,
897 .name = "cols",
898 .type = QEMU_OPT_NUMBER,
900 .name = "rows",
901 .type = QEMU_OPT_NUMBER,
903 .name = "mux",
904 .type = QEMU_OPT_BOOL,
906 .name = "signal",
907 .type = QEMU_OPT_BOOL,
909 .name = "name",
910 .type = QEMU_OPT_STRING,
912 .name = "debug",
913 .type = QEMU_OPT_NUMBER,
915 .name = "size",
916 .type = QEMU_OPT_SIZE,
918 .name = "chardev",
919 .type = QEMU_OPT_STRING,
921 .name = "append",
922 .type = QEMU_OPT_BOOL,
924 .name = "logfile",
925 .type = QEMU_OPT_STRING,
927 .name = "logappend",
928 .type = QEMU_OPT_BOOL,
930 .name = "mouse",
931 .type = QEMU_OPT_BOOL,
933 .name = "clipboard",
934 .type = QEMU_OPT_BOOL,
935 #ifdef CONFIG_LINUX
937 .name = "tight",
938 .type = QEMU_OPT_BOOL,
939 .def_value_str = "on",
941 .name = "abstract",
942 .type = QEMU_OPT_BOOL,
943 #endif
945 { /* end of list */ }
949 bool qemu_chr_has_feature(Chardev *chr,
950 ChardevFeature feature)
952 return test_bit(feature, chr->features);
955 void qemu_chr_set_feature(Chardev *chr,
956 ChardevFeature feature)
958 return set_bit(feature, chr->features);
961 static Chardev *chardev_new(const char *id, const char *typename,
962 ChardevBackend *backend,
963 GMainContext *gcontext,
964 bool handover_yank_instance,
965 Error **errp)
967 Object *obj;
968 Chardev *chr = NULL;
969 Error *local_err = NULL;
970 bool be_opened = true;
972 assert(g_str_has_prefix(typename, "chardev-"));
973 assert(id);
975 obj = object_new(typename);
976 chr = CHARDEV(obj);
977 chr->handover_yank_instance = handover_yank_instance;
978 chr->label = g_strdup(id);
979 chr->gcontext = gcontext;
981 qemu_char_open(chr, backend, &be_opened, &local_err);
982 if (local_err) {
983 error_propagate(errp, local_err);
984 object_unref(obj);
985 return NULL;
988 if (!chr->filename) {
989 chr->filename = g_strdup(typename + 8);
991 if (be_opened) {
992 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
995 return chr;
998 Chardev *qemu_chardev_new(const char *id, const char *typename,
999 ChardevBackend *backend,
1000 GMainContext *gcontext,
1001 Error **errp)
1003 Chardev *chr;
1004 g_autofree char *genid = NULL;
1006 if (!id) {
1007 genid = id_generate(ID_CHR);
1008 id = genid;
1011 chr = chardev_new(id, typename, backend, gcontext, false, errp);
1012 if (!chr) {
1013 return NULL;
1016 if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
1017 errp)) {
1018 object_unref(OBJECT(chr));
1019 return NULL;
1021 object_unref(OBJECT(chr));
1023 return chr;
1026 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
1027 Error **errp)
1029 ERRP_GUARD();
1030 const ChardevClass *cc;
1031 ChardevReturn *ret;
1032 g_autoptr(Chardev) chr = NULL;
1034 if (qemu_chr_find(id)) {
1035 error_setg(errp, "Chardev with id '%s' already exists", id);
1036 return NULL;
1039 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1040 if (!cc) {
1041 goto err;
1044 chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
1045 backend, NULL, false, errp);
1046 if (!chr) {
1047 goto err;
1050 if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
1051 errp)) {
1052 goto err;
1055 ret = g_new0(ChardevReturn, 1);
1056 if (CHARDEV_IS_PTY(chr)) {
1057 ret->pty = g_strdup(chr->filename + 4);
1060 return ret;
1062 err:
1063 error_prepend(errp, "Failed to add chardev '%s': ", id);
1064 return NULL;
1067 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
1068 Error **errp)
1070 CharBackend *be;
1071 const ChardevClass *cc, *cc_new;
1072 Chardev *chr, *chr_new;
1073 bool closed_sent = false;
1074 bool handover_yank_instance;
1075 ChardevReturn *ret;
1077 chr = qemu_chr_find(id);
1078 if (!chr) {
1079 error_setg(errp, "Chardev '%s' does not exist", id);
1080 return NULL;
1083 if (CHARDEV_IS_MUX(chr)) {
1084 error_setg(errp, "Mux device hotswap not supported yet");
1085 return NULL;
1088 if (qemu_chr_replay(chr)) {
1089 error_setg(errp,
1090 "Chardev '%s' cannot be changed in record/replay mode", id);
1091 return NULL;
1094 be = chr->be;
1095 if (!be) {
1096 /* easy case */
1097 object_unparent(OBJECT(chr));
1098 return qmp_chardev_add(id, backend, errp);
1101 if (!be->chr_be_change) {
1102 error_setg(errp, "Chardev user does not support chardev hotswap");
1103 return NULL;
1106 cc = CHARDEV_GET_CLASS(chr);
1107 cc_new = char_get_class(ChardevBackendKind_str(backend->type), errp);
1108 if (!cc_new) {
1109 return NULL;
1113 * The new chardev should not register a yank instance if the current
1114 * chardev has registered one already.
1116 handover_yank_instance = cc->supports_yank && cc_new->supports_yank;
1118 chr_new = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc_new)),
1119 backend, chr->gcontext, handover_yank_instance, errp);
1120 if (!chr_new) {
1121 return NULL;
1124 if (chr->be_open && !chr_new->be_open) {
1125 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1126 closed_sent = true;
1129 chr->be = NULL;
1130 qemu_chr_fe_init(be, chr_new, &error_abort);
1132 if (be->chr_be_change(be->opaque) < 0) {
1133 error_setg(errp, "Chardev '%s' change failed", chr_new->label);
1134 chr_new->be = NULL;
1135 qemu_chr_fe_init(be, chr, &error_abort);
1136 if (closed_sent) {
1137 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1139 object_unref(OBJECT(chr_new));
1140 return NULL;
1143 /* change successfull, clean up */
1144 chr_new->handover_yank_instance = false;
1147 * When the old chardev is freed, it should not unregister the yank
1148 * instance if the new chardev needs it.
1150 chr->handover_yank_instance = handover_yank_instance;
1152 object_unparent(OBJECT(chr));
1153 object_property_add_child(get_chardevs_root(), chr_new->label,
1154 OBJECT(chr_new));
1155 object_unref(OBJECT(chr_new));
1157 ret = g_new0(ChardevReturn, 1);
1158 if (CHARDEV_IS_PTY(chr_new)) {
1159 ret->pty = g_strdup(chr_new->filename + 4);
1162 return ret;
1165 void qmp_chardev_remove(const char *id, Error **errp)
1167 Chardev *chr;
1169 chr = qemu_chr_find(id);
1170 if (chr == NULL) {
1171 error_setg(errp, "Chardev '%s' not found", id);
1172 return;
1174 if (qemu_chr_is_busy(chr)) {
1175 error_setg(errp, "Chardev '%s' is busy", id);
1176 return;
1178 if (qemu_chr_replay(chr)) {
1179 error_setg(errp,
1180 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1181 return;
1183 object_unparent(OBJECT(chr));
1186 void qmp_chardev_send_break(const char *id, Error **errp)
1188 Chardev *chr;
1190 chr = qemu_chr_find(id);
1191 if (chr == NULL) {
1192 error_setg(errp, "Chardev '%s' not found", id);
1193 return;
1195 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
1199 * Add a timeout callback for the chardev (in milliseconds), return
1200 * the GSource object created. Please use this to add timeout hook for
1201 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1202 * make sure the gcontext that the task bound to is correct.
1204 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
1205 GSourceFunc func, void *private)
1207 GSource *source = g_timeout_source_new(ms);
1209 assert(func);
1210 g_source_set_callback(source, func, private, NULL);
1211 g_source_attach(source, chr->gcontext);
1213 return source;
1216 void qemu_chr_cleanup(void)
1218 object_unparent(get_chardevs_root());
1221 static void register_types(void)
1223 type_register_static(&char_type_info);
1226 type_init(register_types);