xref: /openbmc/qemu/chardev/char.c (revision 93e0932b)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "monitor/monitor.h"
28 #include "monitor/qmp-helpers.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"
42 #include "qemu/yank.h"
43 
44 #include "chardev-internal.h"
45 
46 /***********************************************************/
47 /* character device */
48 
49 Object *get_chardevs_root(void)
50 {
51     return container_get(object_get_root(), "/chardevs");
52 }
53 
54 static void chr_be_event(Chardev *s, QEMUChrEvent event)
55 {
56     CharBackend *be = s->be;
57 
58     if (!be || !be->chr_event) {
59         return;
60     }
61 
62     be->chr_event(be->opaque, event);
63 }
64 
65 void qemu_chr_be_event(Chardev *s, QEMUChrEvent event)
66 {
67     /* Keep track if the char device is open */
68     switch (event) {
69         case CHR_EVENT_OPENED:
70             s->be_open = 1;
71             break;
72         case CHR_EVENT_CLOSED:
73             s->be_open = 0;
74             break;
75     case CHR_EVENT_BREAK:
76     case CHR_EVENT_MUX_IN:
77     case CHR_EVENT_MUX_OUT:
78         /* Ignore */
79         break;
80     }
81 
82     CHARDEV_GET_CLASS(s)->chr_be_event(s, event);
83 }
84 
85 /* Not reporting errors from writing to logfile, as logs are
86  * defined to be "best effort" only */
87 static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len)
88 {
89     size_t done = 0;
90     ssize_t ret;
91 
92     if (s->logfd < 0) {
93         return;
94     }
95 
96     while (done < len) {
97     retry:
98         ret = write(s->logfd, buf + done, len - done);
99         if (ret == -1 && errno == EAGAIN) {
100             g_usleep(100);
101             goto retry;
102         }
103 
104         if (ret <= 0) {
105             return;
106         }
107         done += ret;
108     }
109 }
110 
111 static int qemu_chr_write_buffer(Chardev *s,
112                                  const uint8_t *buf, int len,
113                                  int *offset, bool write_all)
114 {
115     ChardevClass *cc = CHARDEV_GET_CLASS(s);
116     int res = 0;
117     *offset = 0;
118 
119     qemu_mutex_lock(&s->chr_write_lock);
120     while (*offset < len) {
121     retry:
122         res = cc->chr_write(s, buf + *offset, len - *offset);
123         if (res < 0 && errno == EAGAIN && write_all) {
124             if (qemu_in_coroutine()) {
125                 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
126             } else {
127                 g_usleep(100);
128             }
129             goto retry;
130         }
131 
132         if (res <= 0) {
133             break;
134         }
135 
136         *offset += res;
137         if (!write_all) {
138             break;
139         }
140     }
141     if (*offset > 0) {
142         /*
143          * If some data was written by backend, we should
144          * only log what was actually written. This method
145          * may be invoked again to write the remaining
146          * method, thus we'll log the remainder at that time.
147          */
148         qemu_chr_write_log(s, buf, *offset);
149     } else if (res < 0) {
150         /*
151          * If a fatal error was reported by the backend,
152          * assume this method won't be invoked again with
153          * this buffer, so log it all right away.
154          */
155         qemu_chr_write_log(s, buf, len);
156     }
157     qemu_mutex_unlock(&s->chr_write_lock);
158 
159     return res;
160 }
161 
162 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
163 {
164     int offset = 0;
165     int res;
166 
167     if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
168         replay_char_write_event_load(&res, &offset);
169         assert(offset <= len);
170         qemu_chr_write_buffer(s, buf, offset, &offset, true);
171         return res;
172     }
173 
174     res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
175 
176     if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
177         replay_char_write_event_save(res, offset);
178     }
179 
180     if (res < 0) {
181         return res;
182     }
183     return offset;
184 }
185 
186 int qemu_chr_be_can_write(Chardev *s)
187 {
188     CharBackend *be = s->be;
189 
190     if (!be || !be->chr_can_read) {
191         return 0;
192     }
193 
194     return be->chr_can_read(be->opaque);
195 }
196 
197 void qemu_chr_be_write_impl(Chardev *s, const uint8_t *buf, int len)
198 {
199     CharBackend *be = s->be;
200 
201     if (be && be->chr_read) {
202         be->chr_read(be->opaque, buf, len);
203     }
204 }
205 
206 void qemu_chr_be_write(Chardev *s, const uint8_t *buf, int len)
207 {
208     if (qemu_chr_replay(s)) {
209         if (replay_mode == REPLAY_MODE_PLAY) {
210             return;
211         }
212         replay_chr_be_write(s, buf, len);
213     } else {
214         qemu_chr_be_write_impl(s, buf, len);
215     }
216 }
217 
218 void qemu_chr_be_update_read_handlers(Chardev *s,
219                                       GMainContext *context)
220 {
221     ChardevClass *cc = CHARDEV_GET_CLASS(s);
222 
223     assert(qemu_chr_has_feature(s, QEMU_CHAR_FEATURE_GCONTEXT)
224            || !context);
225     s->gcontext = context;
226     if (cc->chr_update_read_handler) {
227         cc->chr_update_read_handler(s);
228     }
229 }
230 
231 int qemu_chr_add_client(Chardev *s, int fd)
232 {
233     return CHARDEV_GET_CLASS(s)->chr_add_client ?
234         CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
235 }
236 
237 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
238                            bool *be_opened, Error **errp)
239 {
240     ChardevClass *cc = CHARDEV_GET_CLASS(chr);
241     /* Any ChardevCommon member would work */
242     ChardevCommon *common = backend ? backend->u.null.data : NULL;
243 
244     if (common && common->logfile) {
245         int flags = O_WRONLY;
246         if (common->has_logappend &&
247             common->logappend) {
248             flags |= O_APPEND;
249         } else {
250             flags |= O_TRUNC;
251         }
252         chr->logfd = qemu_create(common->logfile, flags, 0666, errp);
253         if (chr->logfd < 0) {
254             return;
255         }
256     }
257 
258     if (cc->open) {
259         cc->open(chr, backend, be_opened, errp);
260     }
261 }
262 
263 static void char_init(Object *obj)
264 {
265     Chardev *chr = CHARDEV(obj);
266 
267     chr->handover_yank_instance = false;
268     chr->logfd = -1;
269     qemu_mutex_init(&chr->chr_write_lock);
270 
271     /*
272      * Assume if chr_update_read_handler is implemented it will
273      * take the updated gcontext into account.
274      */
275     if (CHARDEV_GET_CLASS(chr)->chr_update_read_handler) {
276         qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT);
277     }
278 
279 }
280 
281 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
282 {
283     return len;
284 }
285 
286 static void char_class_init(ObjectClass *oc, void *data)
287 {
288     ChardevClass *cc = CHARDEV_CLASS(oc);
289 
290     cc->chr_write = null_chr_write;
291     cc->chr_be_event = chr_be_event;
292 }
293 
294 static void char_finalize(Object *obj)
295 {
296     Chardev *chr = CHARDEV(obj);
297 
298     if (chr->be) {
299         chr->be->chr = NULL;
300     }
301     g_free(chr->filename);
302     g_free(chr->label);
303     if (chr->logfd != -1) {
304         close(chr->logfd);
305     }
306     qemu_mutex_destroy(&chr->chr_write_lock);
307 }
308 
309 static const TypeInfo char_type_info = {
310     .name = TYPE_CHARDEV,
311     .parent = TYPE_OBJECT,
312     .instance_size = sizeof(Chardev),
313     .instance_init = char_init,
314     .instance_finalize = char_finalize,
315     .abstract = true,
316     .class_size = sizeof(ChardevClass),
317     .class_init = char_class_init,
318 };
319 
320 static bool qemu_chr_is_busy(Chardev *s)
321 {
322     if (CHARDEV_IS_MUX(s)) {
323         MuxChardev *d = MUX_CHARDEV(s);
324         return d->mux_cnt >= 0;
325     } else {
326         return s->be != NULL;
327     }
328 }
329 
330 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
331 {
332     ChardevClass *cc = CHARDEV_GET_CLASS(chr);
333 
334     if (cc->chr_wait_connected) {
335         return cc->chr_wait_connected(chr, errp);
336     }
337 
338     return 0;
339 }
340 
341 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
342                                 bool permit_mux_mon)
343 {
344     char host[65], port[33], width[8], height[8];
345     int pos;
346     const char *p;
347     QemuOpts *opts;
348     Error *local_err = NULL;
349 
350     opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
351     if (local_err) {
352         error_report_err(local_err);
353         return NULL;
354     }
355 
356     if (strstart(filename, "mon:", &p)) {
357         if (!permit_mux_mon) {
358             error_report("mon: isn't supported in this context");
359             return NULL;
360         }
361         filename = p;
362         qemu_opt_set(opts, "mux", "on", &error_abort);
363         if (strcmp(filename, "stdio") == 0) {
364             /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
365              * but pass it to the guest.  Handle this only for compat syntax,
366              * for -chardev syntax we have special option for this.
367              * This is what -nographic did, redirecting+muxing serial+monitor
368              * to stdio causing Ctrl+C to be passed to guest. */
369             qemu_opt_set(opts, "signal", "off", &error_abort);
370         }
371     }
372 
373     if (strcmp(filename, "null")    == 0 ||
374         strcmp(filename, "pty")     == 0 ||
375         strcmp(filename, "msmouse") == 0 ||
376         strcmp(filename, "wctablet") == 0 ||
377         strcmp(filename, "braille") == 0 ||
378         strcmp(filename, "testdev") == 0 ||
379         strcmp(filename, "stdio")   == 0) {
380         qemu_opt_set(opts, "backend", filename, &error_abort);
381         return opts;
382     }
383     if (strstart(filename, "vc", &p)) {
384         qemu_opt_set(opts, "backend", "vc", &error_abort);
385         if (*p == ':') {
386             if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
387                 /* pixels */
388                 qemu_opt_set(opts, "width", width, &error_abort);
389                 qemu_opt_set(opts, "height", height, &error_abort);
390             } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
391                 /* chars */
392                 qemu_opt_set(opts, "cols", width, &error_abort);
393                 qemu_opt_set(opts, "rows", height, &error_abort);
394             } else {
395                 goto fail;
396             }
397         }
398         return opts;
399     }
400     if (strcmp(filename, "con:") == 0) {
401         qemu_opt_set(opts, "backend", "console", &error_abort);
402         return opts;
403     }
404     if (strstart(filename, "COM", NULL)) {
405         qemu_opt_set(opts, "backend", "serial", &error_abort);
406         qemu_opt_set(opts, "path", filename, &error_abort);
407         return opts;
408     }
409     if (strstart(filename, "file:", &p)) {
410         qemu_opt_set(opts, "backend", "file", &error_abort);
411         qemu_opt_set(opts, "path", p, &error_abort);
412         return opts;
413     }
414     if (strstart(filename, "pipe:", &p)) {
415         qemu_opt_set(opts, "backend", "pipe", &error_abort);
416         qemu_opt_set(opts, "path", p, &error_abort);
417         return opts;
418     }
419     if (strstart(filename, "tcp:", &p) ||
420         strstart(filename, "telnet:", &p) ||
421         strstart(filename, "tn3270:", &p) ||
422         strstart(filename, "websocket:", &p)) {
423         if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
424             host[0] = 0;
425             if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
426                 goto fail;
427         }
428         qemu_opt_set(opts, "backend", "socket", &error_abort);
429         qemu_opt_set(opts, "host", host, &error_abort);
430         qemu_opt_set(opts, "port", port, &error_abort);
431         if (p[pos] == ',') {
432             if (!qemu_opts_do_parse(opts, p + pos + 1, NULL, &local_err)) {
433                 error_report_err(local_err);
434                 goto fail;
435             }
436         }
437         if (strstart(filename, "telnet:", &p)) {
438             qemu_opt_set(opts, "telnet", "on", &error_abort);
439         } else if (strstart(filename, "tn3270:", &p)) {
440             qemu_opt_set(opts, "tn3270", "on", &error_abort);
441         } else if (strstart(filename, "websocket:", &p)) {
442             qemu_opt_set(opts, "websocket", "on", &error_abort);
443         }
444         return opts;
445     }
446     if (strstart(filename, "udp:", &p)) {
447         qemu_opt_set(opts, "backend", "udp", &error_abort);
448         if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
449             host[0] = 0;
450             if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
451                 goto fail;
452             }
453         }
454         qemu_opt_set(opts, "host", host, &error_abort);
455         qemu_opt_set(opts, "port", port, &error_abort);
456         if (p[pos] == '@') {
457             p += pos + 1;
458             if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
459                 host[0] = 0;
460                 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
461                     goto fail;
462                 }
463             }
464             qemu_opt_set(opts, "localaddr", host, &error_abort);
465             qemu_opt_set(opts, "localport", port, &error_abort);
466         }
467         return opts;
468     }
469     if (strstart(filename, "unix:", &p)) {
470         qemu_opt_set(opts, "backend", "socket", &error_abort);
471         if (!qemu_opts_do_parse(opts, p, "path", &local_err)) {
472             error_report_err(local_err);
473             goto fail;
474         }
475         return opts;
476     }
477     if (strstart(filename, "/dev/parport", NULL) ||
478         strstart(filename, "/dev/ppi", NULL)) {
479         qemu_opt_set(opts, "backend", "parallel", &error_abort);
480         qemu_opt_set(opts, "path", filename, &error_abort);
481         return opts;
482     }
483     if (strstart(filename, "/dev/", NULL)) {
484         qemu_opt_set(opts, "backend", "serial", &error_abort);
485         qemu_opt_set(opts, "path", filename, &error_abort);
486         return opts;
487     }
488 
489     error_report("'%s' is not a valid char driver", filename);
490 
491 fail:
492     qemu_opts_del(opts);
493     return NULL;
494 }
495 
496 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
497 {
498     const char *logfile = qemu_opt_get(opts, "logfile");
499 
500     backend->logfile = g_strdup(logfile);
501     backend->has_logappend = true;
502     backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
503 }
504 
505 static const ChardevClass *char_get_class(const char *driver, Error **errp)
506 {
507     ObjectClass *oc;
508     const ChardevClass *cc;
509     char *typename = g_strdup_printf("chardev-%s", driver);
510 
511     oc = module_object_class_by_name(typename);
512     g_free(typename);
513 
514     if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
515         error_setg(errp, "'%s' is not a valid char driver name", driver);
516         return NULL;
517     }
518 
519     if (object_class_is_abstract(oc)) {
520         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
521                    "an abstract device type");
522         return NULL;
523     }
524 
525     cc = CHARDEV_CLASS(oc);
526     if (cc->internal) {
527         error_setg(errp, "'%s' is not a valid char driver name", driver);
528         return NULL;
529     }
530 
531     return cc;
532 }
533 
534 typedef struct ChadevClassFE {
535     void (*fn)(const char *name, void *opaque);
536     void *opaque;
537 } ChadevClassFE;
538 
539 static void
540 chardev_class_foreach(ObjectClass *klass, void *opaque)
541 {
542     ChadevClassFE *fe = opaque;
543 
544     assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
545     if (CHARDEV_CLASS(klass)->internal) {
546         return;
547     }
548 
549     fe->fn(object_class_get_name(klass) + 8, fe->opaque);
550 }
551 
552 static void
553 chardev_name_foreach(void (*fn)(const char *name, void *opaque),
554                      void *opaque)
555 {
556     ChadevClassFE fe = { .fn = fn, .opaque = opaque };
557 
558     object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
559 }
560 
561 static void
562 help_string_append(const char *name, void *opaque)
563 {
564     GString *str = opaque;
565 
566     g_string_append_printf(str, "\n  %s", name);
567 }
568 
569 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
570 {
571     Error *local_err = NULL;
572     const ChardevClass *cc;
573     ChardevBackend *backend = NULL;
574     const char *name = qemu_opt_get(opts, "backend");
575 
576     if (name == NULL) {
577         error_setg(errp, "chardev: \"%s\" missing backend",
578                    qemu_opts_id(opts));
579         return NULL;
580     }
581 
582     cc = char_get_class(name, errp);
583     if (cc == NULL) {
584         return NULL;
585     }
586 
587     backend = g_new0(ChardevBackend, 1);
588     backend->type = CHARDEV_BACKEND_KIND_NULL;
589 
590     if (cc->parse) {
591         cc->parse(opts, backend, &local_err);
592         if (local_err) {
593             error_propagate(errp, local_err);
594             qapi_free_ChardevBackend(backend);
595             return NULL;
596         }
597     } else {
598         ChardevCommon *ccom = g_new0(ChardevCommon, 1);
599         qemu_chr_parse_common(opts, ccom);
600         backend->u.null.data = ccom; /* Any ChardevCommon member would work */
601     }
602 
603     return backend;
604 }
605 
606 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
607                                 Error **errp)
608 {
609     const ChardevClass *cc;
610     Chardev *chr = NULL;
611     ChardevBackend *backend = NULL;
612     const char *name = qemu_opt_get(opts, "backend");
613     const char *id = qemu_opts_id(opts);
614     char *bid = NULL;
615 
616     if (name && is_help_option(name)) {
617         GString *str = g_string_new("");
618 
619         chardev_name_foreach(help_string_append, str);
620 
621         qemu_printf("Available chardev backend types: %s\n", str->str);
622         g_string_free(str, true);
623         return NULL;
624     }
625 
626     if (id == NULL) {
627         error_setg(errp, "chardev: no id specified");
628         return NULL;
629     }
630 
631     backend = qemu_chr_parse_opts(opts, errp);
632     if (backend == NULL) {
633         return NULL;
634     }
635 
636     cc = char_get_class(name, errp);
637     if (cc == NULL) {
638         goto out;
639     }
640 
641     if (qemu_opt_get_bool(opts, "mux", 0)) {
642         bid = g_strdup_printf("%s-base", id);
643     }
644 
645     chr = qemu_chardev_new(bid ? bid : id,
646                            object_class_get_name(OBJECT_CLASS(cc)),
647                            backend, context, errp);
648 
649     if (chr == NULL) {
650         goto out;
651     }
652 
653     if (bid) {
654         Chardev *mux;
655         qapi_free_ChardevBackend(backend);
656         backend = g_new0(ChardevBackend, 1);
657         backend->type = CHARDEV_BACKEND_KIND_MUX;
658         backend->u.mux.data = g_new0(ChardevMux, 1);
659         backend->u.mux.data->chardev = g_strdup(bid);
660         mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
661         if (mux == NULL) {
662             object_unparent(OBJECT(chr));
663             chr = NULL;
664             goto out;
665         }
666         chr = mux;
667     }
668 
669 out:
670     qapi_free_ChardevBackend(backend);
671     g_free(bid);
672     return chr;
673 }
674 
675 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
676                                bool permit_mux_mon, GMainContext *context)
677 {
678     const char *p;
679     Chardev *chr;
680     QemuOpts *opts;
681     Error *err = NULL;
682 
683     if (strstart(filename, "chardev:", &p)) {
684         return qemu_chr_find(p);
685     }
686 
687     opts = qemu_chr_parse_compat(label, filename, permit_mux_mon);
688     if (!opts)
689         return NULL;
690 
691     chr = qemu_chr_new_from_opts(opts, context, &err);
692     if (!chr) {
693         error_report_err(err);
694         goto out;
695     }
696 
697     if (qemu_opt_get_bool(opts, "mux", 0)) {
698         assert(permit_mux_mon);
699         monitor_init_hmp(chr, true, &err);
700         if (err) {
701             error_report_err(err);
702             object_unparent(OBJECT(chr));
703             chr = NULL;
704             goto out;
705         }
706     }
707 
708 out:
709     qemu_opts_del(opts);
710     return chr;
711 }
712 
713 static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
714                                           const char *filename,
715                                           bool permit_mux_mon,
716                                           GMainContext *context)
717 {
718     Chardev *chr;
719     chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon, context);
720     if (chr) {
721         if (replay_mode != REPLAY_MODE_NONE) {
722             qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
723         }
724         if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
725             error_report("Replay: ioctl is not supported "
726                          "for serial devices yet");
727         }
728         replay_register_char_driver(chr);
729     }
730     return chr;
731 }
732 
733 Chardev *qemu_chr_new(const char *label, const char *filename,
734                       GMainContext *context)
735 {
736     return qemu_chr_new_permit_mux_mon(label, filename, false, context);
737 }
738 
739 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
740                               GMainContext *context)
741 {
742     return qemu_chr_new_permit_mux_mon(label, filename, true, context);
743 }
744 
745 static int qmp_query_chardev_foreach(Object *obj, void *data)
746 {
747     Chardev *chr = CHARDEV(obj);
748     ChardevInfoList **list = data;
749     ChardevInfo *value = g_malloc0(sizeof(*value));
750 
751     value->label = g_strdup(chr->label);
752     value->filename = g_strdup(chr->filename);
753     value->frontend_open = chr->be && chr->be->fe_open;
754 
755     QAPI_LIST_PREPEND(*list, value);
756 
757     return 0;
758 }
759 
760 ChardevInfoList *qmp_query_chardev(Error **errp)
761 {
762     ChardevInfoList *chr_list = NULL;
763 
764     object_child_foreach(get_chardevs_root(),
765                          qmp_query_chardev_foreach, &chr_list);
766 
767     return chr_list;
768 }
769 
770 static void
771 qmp_prepend_backend(const char *name, void *opaque)
772 {
773     ChardevBackendInfoList **list = opaque;
774     ChardevBackendInfo *value;
775 
776     value = g_new0(ChardevBackendInfo, 1);
777     value->name = g_strdup(name);
778     QAPI_LIST_PREPEND(*list, value);
779 }
780 
781 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
782 {
783     ChardevBackendInfoList *backend_list = NULL;
784 
785     chardev_name_foreach(qmp_prepend_backend, &backend_list);
786 
787     return backend_list;
788 }
789 
790 Chardev *qemu_chr_find(const char *name)
791 {
792     Object *obj = object_resolve_path_component(get_chardevs_root(), name);
793 
794     return obj ? CHARDEV(obj) : NULL;
795 }
796 
797 QemuOptsList qemu_chardev_opts = {
798     .name = "chardev",
799     .implied_opt_name = "backend",
800     .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
801     .desc = {
802         {
803             .name = "backend",
804             .type = QEMU_OPT_STRING,
805         },{
806             .name = "path",
807             .type = QEMU_OPT_STRING,
808         },{
809             .name = "host",
810             .type = QEMU_OPT_STRING,
811         },{
812             .name = "port",
813             .type = QEMU_OPT_STRING,
814         },{
815             .name = "fd",
816             .type = QEMU_OPT_STRING,
817         },{
818             .name = "localaddr",
819             .type = QEMU_OPT_STRING,
820         },{
821             .name = "localport",
822             .type = QEMU_OPT_STRING,
823         },{
824             .name = "to",
825             .type = QEMU_OPT_NUMBER,
826         },{
827             .name = "ipv4",
828             .type = QEMU_OPT_BOOL,
829         },{
830             .name = "ipv6",
831             .type = QEMU_OPT_BOOL,
832         },{
833             .name = "wait",
834             .type = QEMU_OPT_BOOL,
835         },{
836             .name = "server",
837             .type = QEMU_OPT_BOOL,
838         },{
839             .name = "delay",
840             .type = QEMU_OPT_BOOL,
841         },{
842             .name = "nodelay",
843             .type = QEMU_OPT_BOOL,
844         },{
845             .name = "reconnect",
846             .type = QEMU_OPT_NUMBER,
847         },{
848             .name = "telnet",
849             .type = QEMU_OPT_BOOL,
850         },{
851             .name = "tn3270",
852             .type = QEMU_OPT_BOOL,
853         },{
854             .name = "tls-creds",
855             .type = QEMU_OPT_STRING,
856         },{
857             .name = "tls-authz",
858             .type = QEMU_OPT_STRING,
859         },{
860             .name = "websocket",
861             .type = QEMU_OPT_BOOL,
862         },{
863             .name = "width",
864             .type = QEMU_OPT_NUMBER,
865         },{
866             .name = "height",
867             .type = QEMU_OPT_NUMBER,
868         },{
869             .name = "cols",
870             .type = QEMU_OPT_NUMBER,
871         },{
872             .name = "rows",
873             .type = QEMU_OPT_NUMBER,
874         },{
875             .name = "mux",
876             .type = QEMU_OPT_BOOL,
877         },{
878             .name = "signal",
879             .type = QEMU_OPT_BOOL,
880         },{
881             .name = "name",
882             .type = QEMU_OPT_STRING,
883         },{
884             .name = "debug",
885             .type = QEMU_OPT_NUMBER,
886         },{
887             .name = "size",
888             .type = QEMU_OPT_SIZE,
889         },{
890             .name = "chardev",
891             .type = QEMU_OPT_STRING,
892         },{
893             .name = "append",
894             .type = QEMU_OPT_BOOL,
895         },{
896             .name = "logfile",
897             .type = QEMU_OPT_STRING,
898         },{
899             .name = "logappend",
900             .type = QEMU_OPT_BOOL,
901         },{
902             .name = "mouse",
903             .type = QEMU_OPT_BOOL,
904         },{
905             .name = "clipboard",
906             .type = QEMU_OPT_BOOL,
907 #ifdef CONFIG_LINUX
908         },{
909             .name = "tight",
910             .type = QEMU_OPT_BOOL,
911             .def_value_str = "on",
912         },{
913             .name = "abstract",
914             .type = QEMU_OPT_BOOL,
915 #endif
916         },
917         { /* end of list */ }
918     },
919 };
920 
921 bool qemu_chr_has_feature(Chardev *chr,
922                           ChardevFeature feature)
923 {
924     return test_bit(feature, chr->features);
925 }
926 
927 void qemu_chr_set_feature(Chardev *chr,
928                            ChardevFeature feature)
929 {
930     return set_bit(feature, chr->features);
931 }
932 
933 static Chardev *chardev_new(const char *id, const char *typename,
934                             ChardevBackend *backend,
935                             GMainContext *gcontext,
936                             bool handover_yank_instance,
937                             Error **errp)
938 {
939     Object *obj;
940     Chardev *chr = NULL;
941     Error *local_err = NULL;
942     bool be_opened = true;
943 
944     assert(g_str_has_prefix(typename, "chardev-"));
945     assert(id);
946 
947     obj = object_new(typename);
948     chr = CHARDEV(obj);
949     chr->handover_yank_instance = handover_yank_instance;
950     chr->label = g_strdup(id);
951     chr->gcontext = gcontext;
952 
953     qemu_char_open(chr, backend, &be_opened, &local_err);
954     if (local_err) {
955         error_propagate(errp, local_err);
956         object_unref(obj);
957         return NULL;
958     }
959 
960     if (!chr->filename) {
961         chr->filename = g_strdup(typename + 8);
962     }
963     if (be_opened) {
964         qemu_chr_be_event(chr, CHR_EVENT_OPENED);
965     }
966 
967     return chr;
968 }
969 
970 Chardev *qemu_chardev_new(const char *id, const char *typename,
971                           ChardevBackend *backend,
972                           GMainContext *gcontext,
973                           Error **errp)
974 {
975     Chardev *chr;
976     g_autofree char *genid = NULL;
977 
978     if (!id) {
979         genid = id_generate(ID_CHR);
980         id = genid;
981     }
982 
983     chr = chardev_new(id, typename, backend, gcontext, false, errp);
984     if (!chr) {
985         return NULL;
986     }
987 
988     if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
989                                        errp)) {
990         object_unref(OBJECT(chr));
991         return NULL;
992     }
993     object_unref(OBJECT(chr));
994 
995     return chr;
996 }
997 
998 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
999                                Error **errp)
1000 {
1001     ERRP_GUARD();
1002     const ChardevClass *cc;
1003     ChardevReturn *ret;
1004     g_autoptr(Chardev) chr = NULL;
1005 
1006     if (qemu_chr_find(id)) {
1007         error_setg(errp, "Chardev with id '%s' already exists", id);
1008         return NULL;
1009     }
1010 
1011     cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
1012     if (!cc) {
1013         goto err;
1014     }
1015 
1016     chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
1017                       backend, NULL, false, errp);
1018     if (!chr) {
1019         goto err;
1020     }
1021 
1022     if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
1023                                        errp)) {
1024         goto err;
1025     }
1026 
1027     ret = g_new0(ChardevReturn, 1);
1028     if (CHARDEV_IS_PTY(chr)) {
1029         ret->pty = g_strdup(chr->filename + 4);
1030     }
1031 
1032     return ret;
1033 
1034 err:
1035     error_prepend(errp, "Failed to add chardev '%s': ", id);
1036     return NULL;
1037 }
1038 
1039 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
1040                                   Error **errp)
1041 {
1042     CharBackend *be;
1043     const ChardevClass *cc, *cc_new;
1044     Chardev *chr, *chr_new;
1045     bool closed_sent = false;
1046     bool handover_yank_instance;
1047     ChardevReturn *ret;
1048 
1049     chr = qemu_chr_find(id);
1050     if (!chr) {
1051         error_setg(errp, "Chardev '%s' does not exist", id);
1052         return NULL;
1053     }
1054 
1055     if (CHARDEV_IS_MUX(chr)) {
1056         error_setg(errp, "Mux device hotswap not supported yet");
1057         return NULL;
1058     }
1059 
1060     if (qemu_chr_replay(chr)) {
1061         error_setg(errp,
1062             "Chardev '%s' cannot be changed in record/replay mode", id);
1063         return NULL;
1064     }
1065 
1066     be = chr->be;
1067     if (!be) {
1068         /* easy case */
1069         object_unparent(OBJECT(chr));
1070         return qmp_chardev_add(id, backend, errp);
1071     }
1072 
1073     if (!be->chr_be_change) {
1074         error_setg(errp, "Chardev user does not support chardev hotswap");
1075         return NULL;
1076     }
1077 
1078     cc = CHARDEV_GET_CLASS(chr);
1079     cc_new = char_get_class(ChardevBackendKind_str(backend->type), errp);
1080     if (!cc_new) {
1081         return NULL;
1082     }
1083 
1084     /*
1085      * The new chardev should not register a yank instance if the current
1086      * chardev has registered one already.
1087      */
1088     handover_yank_instance = cc->supports_yank && cc_new->supports_yank;
1089 
1090     chr_new = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc_new)),
1091                           backend, chr->gcontext, handover_yank_instance, errp);
1092     if (!chr_new) {
1093         return NULL;
1094     }
1095 
1096     if (chr->be_open && !chr_new->be_open) {
1097         qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1098         closed_sent = true;
1099     }
1100 
1101     chr->be = NULL;
1102     qemu_chr_fe_init(be, chr_new, &error_abort);
1103 
1104     if (be->chr_be_change(be->opaque) < 0) {
1105         error_setg(errp, "Chardev '%s' change failed", chr_new->label);
1106         chr_new->be = NULL;
1107         qemu_chr_fe_init(be, chr, &error_abort);
1108         if (closed_sent) {
1109             qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1110         }
1111         object_unref(OBJECT(chr_new));
1112         return NULL;
1113     }
1114 
1115     /* change successfull, clean up */
1116     chr_new->handover_yank_instance = false;
1117 
1118     /*
1119      * When the old chardev is freed, it should not unregister the yank
1120      * instance if the new chardev needs it.
1121      */
1122     chr->handover_yank_instance = handover_yank_instance;
1123 
1124     object_unparent(OBJECT(chr));
1125     object_property_add_child(get_chardevs_root(), chr_new->label,
1126                               OBJECT(chr_new));
1127     object_unref(OBJECT(chr_new));
1128 
1129     ret = g_new0(ChardevReturn, 1);
1130     if (CHARDEV_IS_PTY(chr_new)) {
1131         ret->pty = g_strdup(chr_new->filename + 4);
1132     }
1133 
1134     return ret;
1135 }
1136 
1137 void qmp_chardev_remove(const char *id, Error **errp)
1138 {
1139     Chardev *chr;
1140 
1141     chr = qemu_chr_find(id);
1142     if (chr == NULL) {
1143         error_setg(errp, "Chardev '%s' not found", id);
1144         return;
1145     }
1146     if (qemu_chr_is_busy(chr)) {
1147         error_setg(errp, "Chardev '%s' is busy", id);
1148         return;
1149     }
1150     if (qemu_chr_replay(chr)) {
1151         error_setg(errp,
1152             "Chardev '%s' cannot be unplugged in record/replay mode", id);
1153         return;
1154     }
1155     object_unparent(OBJECT(chr));
1156 }
1157 
1158 void qmp_chardev_send_break(const char *id, Error **errp)
1159 {
1160     Chardev *chr;
1161 
1162     chr = qemu_chr_find(id);
1163     if (chr == NULL) {
1164         error_setg(errp, "Chardev '%s' not found", id);
1165         return;
1166     }
1167     qemu_chr_be_event(chr, CHR_EVENT_BREAK);
1168 }
1169 
1170 bool qmp_add_client_char(int fd, bool has_skipauth, bool skipauth,
1171                          bool has_tls, bool tls, const char *protocol,
1172                          Error **errp)
1173 {
1174     Chardev *s = qemu_chr_find(protocol);
1175 
1176     if (!s) {
1177         error_setg(errp, "protocol '%s' is invalid", protocol);
1178         close(fd);
1179         return false;
1180     }
1181     if (qemu_chr_add_client(s, fd) < 0) {
1182         error_setg(errp, "failed to add client");
1183         close(fd);
1184         return false;
1185     }
1186     return true;
1187 }
1188 
1189 /*
1190  * Add a timeout callback for the chardev (in milliseconds), return
1191  * the GSource object created. Please use this to add timeout hook for
1192  * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1193  * make sure the gcontext that the task bound to is correct.
1194  */
1195 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
1196                                  GSourceFunc func, void *private)
1197 {
1198     GSource *source = g_timeout_source_new(ms);
1199 
1200     assert(func);
1201     g_source_set_callback(source, func, private, NULL);
1202     g_source_attach(source, chr->gcontext);
1203 
1204     return source;
1205 }
1206 
1207 void qemu_chr_cleanup(void)
1208 {
1209     object_unparent(get_chardevs_root());
1210 }
1211 
1212 static void register_types(void)
1213 {
1214     type_register_static(&char_type_info);
1215 }
1216 
1217 type_init(register_types);
1218