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