blob: 2ae4f465ecf891b3ad0314c93fea8f0cc7abffdb [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
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 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020025#include "qemu/cutils.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010026#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030028#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040030#include "chardev/char.h"
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030031#include "qmp-commands.h"
Corey Minyardcfb429c2014-10-02 11:17:35 -050032#include "qapi-visit.h"
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +030033#include "sysemu/replay.h"
Lin Ma517b3d42016-08-17 01:13:52 +080034#include "qemu/help_option.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
Marc-André Lureau8228e352017-01-26 17:19:46 +040036#include "chardev/char-mux.h"
Marc-André Lureaudf85a782016-12-12 15:06:14 +030037
Corey Minyardcfb429c2014-10-02 11:17:35 -050038/***********************************************************/
aliguori6f97dba2008-10-31 18:49:55 +000039/* character device */
40
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +030041static Object *get_chardevs_root(void)
42{
43 return container_get(object_get_root(), "/chardevs");
44}
aliguori2970a6c2009-03-05 22:59:58 +000045
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030046void qemu_chr_be_event(Chardev *s, int event)
aliguori6f97dba2008-10-31 18:49:55 +000047{
Marc-André Lureaua4afa542016-10-22 12:53:01 +030048 CharBackend *be = s->be;
49
Alexander Graf73cdf3f2010-04-01 18:42:39 +020050 /* Keep track if the char device is open */
51 switch (event) {
52 case CHR_EVENT_OPENED:
Hans de Goede16665b92013-03-26 11:07:53 +010053 s->be_open = 1;
Alexander Graf73cdf3f2010-04-01 18:42:39 +020054 break;
55 case CHR_EVENT_CLOSED:
Hans de Goede16665b92013-03-26 11:07:53 +010056 s->be_open = 0;
Alexander Graf73cdf3f2010-04-01 18:42:39 +020057 break;
58 }
59
Marc-André Lureaua4afa542016-10-22 12:53:01 +030060 if (!be || !be->chr_event) {
aliguori6f97dba2008-10-31 18:49:55 +000061 return;
Marc-André Lureaua4afa542016-10-22 12:53:01 +030062 }
63
64 be->chr_event(be->opaque, event);
aliguori6f97dba2008-10-31 18:49:55 +000065}
66
Daniel P. Berranged0d77082016-01-11 12:44:41 +000067/* Not reporting errors from writing to logfile, as logs are
68 * defined to be "best effort" only */
Marc-André Lureaua9b1ca32017-01-26 23:38:22 +040069static void qemu_chr_write_log(Chardev *s, const uint8_t *buf, size_t len)
Daniel P. Berranged0d77082016-01-11 12:44:41 +000070{
71 size_t done = 0;
72 ssize_t ret;
73
74 if (s->logfd < 0) {
75 return;
76 }
77
78 while (done < len) {
Daniel P. Berrange53628ef2016-03-31 16:29:27 +010079 retry:
80 ret = write(s->logfd, buf + done, len - done);
81 if (ret == -1 && errno == EAGAIN) {
82 g_usleep(100);
83 goto retry;
84 }
Daniel P. Berranged0d77082016-01-11 12:44:41 +000085
86 if (ret <= 0) {
87 return;
88 }
89 done += ret;
90 }
91}
92
Marc-André Lureaua9b1ca32017-01-26 23:38:22 +040093static int qemu_chr_write_buffer(Chardev *s,
94 const uint8_t *buf, int len,
95 int *offset, bool write_all)
aliguori6f97dba2008-10-31 18:49:55 +000096{
Marc-André Lureau777357d2016-12-07 18:39:10 +030097 ChardevClass *cc = CHARDEV_GET_CLASS(s);
Igor Mammedov09313042014-06-25 10:00:41 +020098 int res = 0;
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +030099 *offset = 0;
Anthony Liguoricd187202013-03-26 10:04:17 -0500100
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200101 qemu_mutex_lock(&s->chr_write_lock);
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300102 while (*offset < len) {
Daniel P. Berrange53628ef2016-03-31 16:29:27 +0100103 retry:
Marc-André Lureau777357d2016-12-07 18:39:10 +0300104 res = cc->chr_write(s, buf + *offset, len - *offset);
Marc-André Lureauc90e9392017-01-26 22:48:37 +0400105 if (res < 0 && errno == EAGAIN && write_all) {
Daniel P. Berrange53628ef2016-03-31 16:29:27 +0100106 g_usleep(100);
107 goto retry;
108 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500109
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200110 if (res <= 0) {
Anthony Liguoricd187202013-03-26 10:04:17 -0500111 break;
112 }
113
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300114 *offset += res;
Marc-André Lureauc90e9392017-01-26 22:48:37 +0400115 if (!write_all) {
116 break;
117 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500118 }
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300119 if (*offset > 0) {
Marc-André Lureaua9b1ca32017-01-26 23:38:22 +0400120 qemu_chr_write_log(s, buf, *offset);
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300121 }
122 qemu_mutex_unlock(&s->chr_write_lock);
123
124 return res;
125}
126
Marc-André Lureau4d43a602017-01-26 18:26:44 +0400127int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300128{
Marc-André Lureauc90e9392017-01-26 22:48:37 +0400129 int offset = 0;
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300130 int res;
131
Marc-André Lureau5ebd6702016-10-21 22:58:45 +0300132 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300133 replay_char_write_event_load(&res, &offset);
134 assert(offset <= len);
Marc-André Lureaua9b1ca32017-01-26 23:38:22 +0400135 qemu_chr_write_buffer(s, buf, offset, &offset, true);
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300136 return res;
137 }
138
Marc-André Lureaua9b1ca32017-01-26 23:38:22 +0400139 res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300140
Marc-André Lureau5ebd6702016-10-21 22:58:45 +0300141 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300142 replay_char_write_event_save(res, offset);
143 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500144
Paolo Bonzini9005b2a2014-06-18 08:43:58 +0200145 if (res < 0) {
146 return res;
147 }
Anthony Liguoricd187202013-03-26 10:04:17 -0500148 return offset;
149}
150
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300151int qemu_chr_be_can_write(Chardev *s)
aliguori6f97dba2008-10-31 18:49:55 +0000152{
Marc-André Lureaua4afa542016-10-22 12:53:01 +0300153 CharBackend *be = s->be;
154
155 if (!be || !be->chr_can_read) {
aliguori6f97dba2008-10-31 18:49:55 +0000156 return 0;
Marc-André Lureaua4afa542016-10-22 12:53:01 +0300157 }
158
159 return be->chr_can_read(be->opaque);
aliguori6f97dba2008-10-31 18:49:55 +0000160}
161
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300162void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
aliguori6f97dba2008-10-31 18:49:55 +0000163{
Marc-André Lureaua4afa542016-10-22 12:53:01 +0300164 CharBackend *be = s->be;
165
166 if (be && be->chr_read) {
167 be->chr_read(be->opaque, buf, len);
Stefan Weilac310732012-04-19 22:27:14 +0200168 }
aliguori6f97dba2008-10-31 18:49:55 +0000169}
170
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300171void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300172{
Marc-André Lureau5ebd6702016-10-21 22:58:45 +0300173 if (qemu_chr_replay(s)) {
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300174 if (replay_mode == REPLAY_MODE_PLAY) {
175 return;
176 }
177 replay_chr_be_write(s, buf, len);
178 } else {
179 qemu_chr_be_write_impl(s, buf, len);
180 }
181}
182
Peter Xu07241c22017-09-21 14:35:51 +0800183void qemu_chr_be_update_read_handlers(Chardev *s,
184 GMainContext *context)
185{
186 ChardevClass *cc = CHARDEV_GET_CLASS(s);
187
Peter Xu95eeeba2017-09-21 14:35:52 +0800188 s->gcontext = context;
Peter Xu07241c22017-09-21 14:35:51 +0800189 if (cc->chr_update_read_handler) {
Peter Xubb86d052017-09-21 14:35:54 +0800190 cc->chr_update_read_handler(s);
Peter Xu07241c22017-09-21 14:35:51 +0800191 }
192}
193
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300194int qemu_chr_add_client(Chardev *s, int fd)
Daniel P. Berrange13661082011-06-23 13:31:42 +0100195{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300196 return CHARDEV_GET_CLASS(s)->chr_add_client ?
197 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
Daniel P. Berrange13661082011-06-23 13:31:42 +0100198}
199
Marc-André Lureau777357d2016-12-07 18:39:10 +0300200static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
201 bool *be_opened, Error **errp)
202{
203 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
204 /* Any ChardevCommon member would work */
205 ChardevCommon *common = backend ? backend->u.null.data : NULL;
206
207 if (common && common->has_logfile) {
208 int flags = O_WRONLY | O_CREAT;
209 if (common->has_logappend &&
210 common->logappend) {
211 flags |= O_APPEND;
212 } else {
213 flags |= O_TRUNC;
214 }
215 chr->logfd = qemu_open(common->logfile, flags, 0666);
216 if (chr->logfd < 0) {
217 error_setg_errno(errp, errno,
218 "Unable to open logfile %s",
219 common->logfile);
220 return;
221 }
222 }
223
224 if (cc->open) {
225 cc->open(chr, backend, be_opened, errp);
226 }
227}
228
229static void char_init(Object *obj)
230{
231 Chardev *chr = CHARDEV(obj);
232
233 chr->logfd = -1;
234 qemu_mutex_init(&chr->chr_write_lock);
235}
236
Marc-André Lureaueb314a92016-12-12 13:41:40 +0300237static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
238{
239 return len;
240}
241
242static void char_class_init(ObjectClass *oc, void *data)
243{
244 ChardevClass *cc = CHARDEV_CLASS(oc);
245
246 cc->chr_write = null_chr_write;
247}
248
Marc-André Lureau777357d2016-12-07 18:39:10 +0300249static void char_finalize(Object *obj)
250{
251 Chardev *chr = CHARDEV(obj);
252
253 if (chr->be) {
254 chr->be->chr = NULL;
255 }
256 g_free(chr->filename);
257 g_free(chr->label);
258 if (chr->logfd != -1) {
259 close(chr->logfd);
260 }
261 qemu_mutex_destroy(&chr->chr_write_lock);
262}
263
264static const TypeInfo char_type_info = {
265 .name = TYPE_CHARDEV,
266 .parent = TYPE_OBJECT,
267 .instance_size = sizeof(Chardev),
268 .instance_init = char_init,
269 .instance_finalize = char_finalize,
270 .abstract = true,
271 .class_size = sizeof(ChardevClass),
Marc-André Lureaueb314a92016-12-12 13:41:40 +0300272 .class_init = char_class_init,
Marc-André Lureau777357d2016-12-07 18:39:10 +0300273};
274
Michael Roth7b7ab182013-07-30 13:04:22 -0500275/**
276 * Called after processing of default and command-line-specified
277 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
278 * to a mux chardev. This is done here to ensure that
279 * output/prompts/banners are only displayed for the FE that has
280 * focus when initial command-line processing/machine init is
281 * completed.
282 *
283 * After this point, any new FE attached to any new or existing
284 * mux will receive CHR_EVENT_OPENED notifications for the BE
285 * immediately.
286 */
Marc-André Lureau60611622016-12-14 16:44:19 +0300287static int open_muxes(Object *child, void *opaque)
288{
289 if (CHARDEV_IS_MUX(child)) {
290 /* send OPENED to all already-attached FEs */
291 mux_chr_send_all_event(CHARDEV(child), CHR_EVENT_OPENED);
292 /* mark mux as OPENED so any new FEs will immediately receive
293 * OPENED event
294 */
295 qemu_chr_be_event(CHARDEV(child), CHR_EVENT_OPENED);
296 }
297
298 return 0;
299}
300
Michael Roth7b7ab182013-07-30 13:04:22 -0500301static void muxes_realize_done(Notifier *notifier, void *unused)
302{
Michael Roth7b7ab182013-07-30 13:04:22 -0500303 muxes_realized = true;
Marc-André Lureau60611622016-12-14 16:44:19 +0300304 object_child_foreach(get_chardevs_root(), open_muxes, NULL);
Michael Roth7b7ab182013-07-30 13:04:22 -0500305}
306
307static Notifier muxes_realize_notify = {
308 .notify = muxes_realize_done,
309};
310
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300311static bool qemu_chr_is_busy(Chardev *s)
Marc-André Lureaua4afa542016-10-22 12:53:01 +0300312{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300313 if (CHARDEV_IS_MUX(s)) {
314 MuxChardev *d = MUX_CHARDEV(s);
Marc-André Lureaua4afa542016-10-22 12:53:01 +0300315 return d->mux_cnt >= 0;
316 } else {
317 return s->be != NULL;
318 }
319}
320
Marc-André Lureaud24ca4b2016-12-12 18:41:00 +0300321int qemu_chr_wait_connected(Chardev *chr, Error **errp)
Marc-André Lureau6b6723c2016-07-27 01:15:17 +0400322{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300323 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
324
325 if (cc->chr_wait_connected) {
326 return cc->chr_wait_connected(chr, errp);
Marc-André Lureau6b6723c2016-07-27 01:15:17 +0400327 }
328
329 return 0;
330}
331
Gerd Hoffmann33521632009-12-08 13:11:49 +0100332QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200333{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200334 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200335 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200336 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200337 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300338 Error *local_err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200339
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300340 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100341 if (local_err) {
Markus Armbruster33394882015-02-10 15:21:26 +0100342 error_report_err(local_err);
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200343 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300344 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200345
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +0200346 if (strstart(filename, "mon:", &p)) {
347 filename = p;
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100348 qemu_opt_set(opts, "mux", "on", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +0400349 if (strcmp(filename, "stdio") == 0) {
350 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
351 * but pass it to the guest. Handle this only for compat syntax,
352 * for -chardev syntax we have special option for this.
353 * This is what -nographic did, redirecting+muxing serial+monitor
354 * to stdio causing Ctrl+C to be passed to guest. */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100355 qemu_opt_set(opts, "signal", "off", &error_abort);
Paolo Bonzini02c4bdf2013-07-03 20:29:45 +0400356 }
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +0200357 }
358
Gerd Hoffmannf0457e82009-09-10 10:58:45 +0200359 if (strcmp(filename, "null") == 0 ||
360 strcmp(filename, "pty") == 0 ||
361 strcmp(filename, "msmouse") == 0 ||
Anatoli Huseu1378af962017-02-06 15:23:27 +0100362 strcmp(filename, "wctablet") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +0200363 strcmp(filename, "braille") == 0 ||
Paolo Bonzini56923992014-07-11 09:44:26 +0200364 strcmp(filename, "testdev") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +0200365 strcmp(filename, "stdio") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100366 qemu_opt_set(opts, "backend", filename, &error_abort);
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200367 return opts;
368 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200369 if (strstart(filename, "vc", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100370 qemu_opt_set(opts, "backend", "vc", &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200371 if (*p == ':') {
Stefan Weil49aa4052013-09-30 23:04:49 +0200372 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200373 /* pixels */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100374 qemu_opt_set(opts, "width", width, &error_abort);
375 qemu_opt_set(opts, "height", height, &error_abort);
Stefan Weil49aa4052013-09-30 23:04:49 +0200376 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200377 /* chars */
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100378 qemu_opt_set(opts, "cols", width, &error_abort);
379 qemu_opt_set(opts, "rows", height, &error_abort);
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200380 } else {
381 goto fail;
382 }
383 }
384 return opts;
385 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +0200386 if (strcmp(filename, "con:") == 0) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100387 qemu_opt_set(opts, "backend", "console", &error_abort);
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +0200388 return opts;
389 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +0200390 if (strstart(filename, "COM", NULL)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100391 qemu_opt_set(opts, "backend", "serial", &error_abort);
392 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +0200393 return opts;
394 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200395 if (strstart(filename, "file:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100396 qemu_opt_set(opts, "backend", "file", &error_abort);
397 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200398 return opts;
399 }
400 if (strstart(filename, "pipe:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100401 qemu_opt_set(opts, "backend", "pipe", &error_abort);
402 qemu_opt_set(opts, "path", p, &error_abort);
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200403 return opts;
404 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200405 if (strstart(filename, "tcp:", &p) ||
Jing Liuae92cbd2016-09-23 08:06:11 +0200406 strstart(filename, "telnet:", &p) ||
407 strstart(filename, "tn3270:", &p)) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200408 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
409 host[0] = 0;
410 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
411 goto fail;
412 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100413 qemu_opt_set(opts, "backend", "socket", &error_abort);
414 qemu_opt_set(opts, "host", host, &error_abort);
415 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200416 if (p[pos] == ',') {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +0100417 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
418 if (local_err) {
419 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200420 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +0100421 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200422 }
Jing Liuae92cbd2016-09-23 08:06:11 +0200423 if (strstart(filename, "telnet:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100424 qemu_opt_set(opts, "telnet", "on", &error_abort);
Jing Liuae92cbd2016-09-23 08:06:11 +0200425 } else if (strstart(filename, "tn3270:", &p)) {
426 qemu_opt_set(opts, "tn3270", "on", &error_abort);
427 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200428 return opts;
429 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200430 if (strstart(filename, "udp:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100431 qemu_opt_set(opts, "backend", "udp", &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200432 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
433 host[0] = 0;
Jan Kiszka39324ca2010-03-07 11:28:48 +0100434 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200435 goto fail;
436 }
437 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100438 qemu_opt_set(opts, "host", host, &error_abort);
439 qemu_opt_set(opts, "port", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200440 if (p[pos] == '@') {
441 p += pos + 1;
442 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
443 host[0] = 0;
444 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200445 goto fail;
446 }
447 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100448 qemu_opt_set(opts, "localaddr", host, &error_abort);
449 qemu_opt_set(opts, "localport", port, &error_abort);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200450 }
451 return opts;
452 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200453 if (strstart(filename, "unix:", &p)) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100454 qemu_opt_set(opts, "backend", "socket", &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +0100455 qemu_opts_do_parse(opts, p, "path", &local_err);
456 if (local_err) {
457 error_report_err(local_err);
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200458 goto fail;
Markus Armbrusterdc523cd342015-02-12 18:37:11 +0100459 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200460 return opts;
461 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +0200462 if (strstart(filename, "/dev/parport", NULL) ||
463 strstart(filename, "/dev/ppi", NULL)) {
Marc-André Lureau73119c22017-06-07 21:41:15 +0400464 qemu_opt_set(opts, "backend", "parallel", &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100465 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +0200466 return opts;
467 }
468 if (strstart(filename, "/dev/", NULL)) {
Marc-André Lureau73119c22017-06-07 21:41:15 +0400469 qemu_opt_set(opts, "backend", "serial", &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100470 qemu_opt_set(opts, "path", filename, &error_abort);
Gerd Hoffmann48b76492009-09-10 10:58:48 +0200471 return opts;
472 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200473
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200474fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200475 qemu_opts_del(opts);
476 return NULL;
477}
478
Eric Blake21a933e2016-02-19 17:19:31 -0700479void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000480{
481 const char *logfile = qemu_opt_get(opts, "logfile");
482
483 backend->has_logfile = logfile != NULL;
Marc-André Lureaue1f98fe2016-12-14 23:15:44 +0300484 backend->logfile = g_strdup(logfile);
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000485
486 backend->has_logappend = true;
487 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
488}
489
Marc-André Lureau88cace92016-12-09 00:50:12 +0300490static const ChardevClass *char_get_class(const char *driver, Error **errp)
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +0100491{
Marc-André Lureau88cace92016-12-09 00:50:12 +0300492 ObjectClass *oc;
493 const ChardevClass *cc;
494 char *typename = g_strdup_printf("chardev-%s", driver);
495
496 oc = object_class_by_name(typename);
497 g_free(typename);
498
499 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
500 error_setg(errp, "'%s' is not a valid char driver name", driver);
501 return NULL;
502 }
503
504 if (object_class_is_abstract(oc)) {
505 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
506 "abstract device type");
507 return NULL;
508 }
509
510 cc = CHARDEV_CLASS(oc);
511 if (cc->internal) {
512 error_setg(errp, "'%s' is not a valid char driver name", driver);
513 return NULL;
514 }
515
516 return cc;
517}
518
519static const struct ChardevAlias {
520 const char *typename;
521 const char *alias;
522} chardev_alias_table[] = {
523#ifdef HAVE_CHARDEV_PARPORT
524 { "parallel", "parport" },
525#endif
526#ifdef HAVE_CHARDEV_SERIAL
527 { "serial", "tty" },
528#endif
529};
530
531typedef struct ChadevClassFE {
532 void (*fn)(const char *name, void *opaque);
533 void *opaque;
534} ChadevClassFE;
535
536static void
537chardev_class_foreach(ObjectClass *klass, void *opaque)
538{
539 ChadevClassFE *fe = opaque;
540
541 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
542 if (CHARDEV_CLASS(klass)->internal) {
543 return;
544 }
545
546 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
547}
548
549static void
550chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
551{
552 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
553 int i;
554
555 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
556
Philippe Mathieu-Daudéc7e47c62017-05-30 09:09:19 -0300557 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
Marc-André Lureau88cace92016-12-09 00:50:12 +0300558 fn(chardev_alias_table[i].alias, opaque);
559 }
560}
561
562static void
563help_string_append(const char *name, void *opaque)
564{
565 GString *str = opaque;
566
567 g_string_append_printf(str, "\n%s", name);
Gerd Hoffmann2c5f4882013-02-21 11:39:12 +0100568}
569
Anton Nefedov313e45b2017-07-06 15:08:48 +0300570static const char *chardev_alias_translate(const char *name)
571{
572 int i;
573 for (i = 0; i < (int)ARRAY_SIZE(chardev_alias_table); i++) {
574 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
575 return chardev_alias_table[i].typename;
576 }
577 }
578 return name;
579}
580
Anton Nefedov75b60162017-07-06 15:08:57 +0300581ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200582{
Markus Armbruster0aff6372014-05-19 18:57:35 +0200583 Error *local_err = NULL;
Marc-André Lureau88cace92016-12-09 00:50:12 +0300584 const ChardevClass *cc;
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300585 ChardevBackend *backend = NULL;
Anton Nefedov313e45b2017-07-06 15:08:48 +0300586 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200587
Marc-André Lureau0b812f32016-10-21 16:07:45 +0300588 if (name == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100589 error_setg(errp, "chardev: \"%s\" missing backend",
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +0200590 qemu_opts_id(opts));
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300591 return NULL;
Stefan Hajnoczi1bbd1852011-01-22 13:07:26 +0000592 }
Lin Ma517b3d42016-08-17 01:13:52 +0800593
Anton Nefedov313e45b2017-07-06 15:08:48 +0300594 cc = char_get_class(name, errp);
595 if (cc == NULL) {
596 return NULL;
597 }
598
599 backend = g_new0(ChardevBackend, 1);
600 backend->type = CHARDEV_BACKEND_KIND_NULL;
601
602 if (cc->parse) {
603 cc->parse(opts, backend, &local_err);
604 if (local_err) {
605 error_propagate(errp, local_err);
606 qapi_free_ChardevBackend(backend);
607 return NULL;
608 }
609 } else {
610 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
611 qemu_chr_parse_common(opts, ccom);
612 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
613 }
614
615 return backend;
616}
617
618Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
619{
620 const ChardevClass *cc;
621 Chardev *chr = NULL;
622 ChardevBackend *backend = NULL;
623 const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
624 const char *id = qemu_opts_id(opts);
625 char *bid = NULL;
626
627 if (name && is_help_option(name)) {
Marc-André Lureau776a32a2016-11-30 22:57:24 +0400628 GString *str = g_string_new("");
Marc-André Lureau88cace92016-12-09 00:50:12 +0300629
630 chardev_name_foreach(help_string_append, str);
Marc-André Lureau776a32a2016-11-30 22:57:24 +0400631
632 error_report("Available chardev backend types: %s", str->str);
633 g_string_free(str, true);
Anton Nefedov0ec846b2017-07-25 13:04:41 +0300634 return NULL;
Lin Ma517b3d42016-08-17 01:13:52 +0800635 }
636
637 if (id == NULL) {
638 error_setg(errp, "chardev: no id specified");
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300639 return NULL;
Lin Ma517b3d42016-08-17 01:13:52 +0800640 }
641
Anton Nefedov313e45b2017-07-06 15:08:48 +0300642 backend = qemu_chr_parse_opts(opts, errp);
643 if (backend == NULL) {
644 return NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200645 }
Marc-André Lureau88cace92016-12-09 00:50:12 +0300646
647 cc = char_get_class(name, errp);
648 if (cc == NULL) {
Anton Nefedov313e45b2017-07-06 15:08:48 +0300649 goto out;
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200650 }
651
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +0200652 if (qemu_opt_get_bool(opts, "mux", 0)) {
Peter Maydella61ae7f2014-09-02 11:24:16 +0100653 bid = g_strdup_printf("%s-base", id);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +0200654 }
Peter Maydella61ae7f2014-09-02 11:24:16 +0100655
Marc-André Lureauc5749f72016-12-14 20:58:50 +0300656 chr = qemu_chardev_new(bid ? bid : id,
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300657 object_class_get_name(OBJECT_CLASS(cc)),
658 backend, errp);
Marc-André Lureauc5749f72016-12-14 20:58:50 +0300659
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300660 if (chr == NULL) {
661 goto out;
Peter Maydella61ae7f2014-09-02 11:24:16 +0100662 }
663
664 if (bid) {
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300665 Chardev *mux;
Peter Maydella61ae7f2014-09-02 11:24:16 +0100666 qapi_free_ChardevBackend(backend);
Peter Maydella61ae7f2014-09-02 11:24:16 +0100667 backend = g_new0(ChardevBackend, 1);
Eric Blake130257d2015-10-26 16:34:57 -0600668 backend->type = CHARDEV_BACKEND_KIND_MUX;
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300669 backend->u.mux.data = g_new0(ChardevMux, 1);
Eric Blake32bafa82016-03-17 16:48:37 -0600670 backend->u.mux.data->chardev = g_strdup(bid);
Marc-André Lureauc5749f72016-12-14 20:58:50 +0300671 mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp);
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300672 if (mux == NULL) {
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +0300673 object_unparent(OBJECT(chr));
Peter Maydella61ae7f2014-09-02 11:24:16 +0100674 chr = NULL;
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300675 goto out;
Peter Maydella61ae7f2014-09-02 11:24:16 +0100676 }
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300677 chr = mux;
Peter Maydella61ae7f2014-09-02 11:24:16 +0100678 }
679
Marc-André Lureau0b663b72016-12-09 11:04:51 +0300680out:
Peter Maydella61ae7f2014-09-02 11:24:16 +0100681 qapi_free_ChardevBackend(backend);
Peter Maydella61ae7f2014-09-02 11:24:16 +0100682 g_free(bid);
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200683 return chr;
684}
685
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300686Chardev *qemu_chr_new_noreplay(const char *label, const char *filename)
aliguori6f97dba2008-10-31 18:49:55 +0000687{
688 const char *p;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300689 Chardev *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200690 QemuOpts *opts;
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +0200691 Error *err = NULL;
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200692
Gerd Hoffmannc845f402009-09-10 10:58:52 +0200693 if (strstart(filename, "chardev:", &p)) {
694 return qemu_chr_find(p);
695 }
696
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200697 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200698 if (!opts)
699 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000700
Marc-André Lureaub4948be2016-10-22 12:52:46 +0300701 chr = qemu_chr_new_from_opts(opts, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100702 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100703 error_report_err(err);
Gerd Hoffmannbd2d80b2012-10-15 09:28:05 +0200704 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200705 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
706 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +0000707 }
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100708 qemu_opts_del(opts);
aliguori6f97dba2008-10-31 18:49:55 +0000709 return chr;
710}
711
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300712Chardev *qemu_chr_new(const char *label, const char *filename)
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300713{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300714 Chardev *chr;
Marc-André Lureaub4948be2016-10-22 12:52:46 +0300715 chr = qemu_chr_new_noreplay(label, filename);
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300716 if (chr) {
Marc-André Lureau5ebd6702016-10-21 22:58:45 +0300717 if (replay_mode != REPLAY_MODE_NONE) {
718 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
719 }
Marc-André Lureau777357d2016-12-07 18:39:10 +0300720 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
Marc-André Lureau776a32a2016-11-30 22:57:24 +0400721 error_report("Replay: ioctl is not supported "
722 "for serial devices yet");
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +0300723 }
724 replay_register_char_driver(chr);
725 }
726 return chr;
727}
728
Marc-André Lureau60611622016-12-14 16:44:19 +0300729static int qmp_query_chardev_foreach(Object *obj, void *data)
Marc-André Lureau1ad78ea2015-06-22 18:20:18 +0200730{
Marc-André Lureau60611622016-12-14 16:44:19 +0300731 Chardev *chr = CHARDEV(obj);
732 ChardevInfoList **list = data;
733 ChardevInfoList *info = g_malloc0(sizeof(*info));
734
735 info->value = g_malloc0(sizeof(*info->value));
736 info->value->label = g_strdup(chr->label);
737 info->value->filename = g_strdup(chr->filename);
738 info->value->frontend_open = chr->be && chr->be->fe_open;
739
740 info->next = *list;
741 *list = info;
742
743 return 0;
Marc-André Lureau1ad78ea2015-06-22 18:20:18 +0200744}
745
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300746ChardevInfoList *qmp_query_chardev(Error **errp)
aliguori6f97dba2008-10-31 18:49:55 +0000747{
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300748 ChardevInfoList *chr_list = NULL;
aliguori6f97dba2008-10-31 18:49:55 +0000749
Marc-André Lureau60611622016-12-14 16:44:19 +0300750 object_child_foreach(get_chardevs_root(),
751 qmp_query_chardev_foreach, &chr_list);
Luiz Capitulino588b3832009-12-10 17:16:08 -0200752
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300753 return chr_list;
aliguori6f97dba2008-10-31 18:49:55 +0000754}
Gerd Hoffmannc845f402009-09-10 10:58:52 +0200755
Marc-André Lureau88cace92016-12-09 00:50:12 +0300756static void
757qmp_prepend_backend(const char *name, void *opaque)
Marc-André Lureau0b812f32016-10-21 16:07:45 +0300758{
Marc-André Lureau88cace92016-12-09 00:50:12 +0300759 ChardevBackendInfoList **list = opaque;
Marc-André Lureau0b812f32016-10-21 16:07:45 +0300760 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
Marc-André Lureau88cace92016-12-09 00:50:12 +0300761
Marc-André Lureau0b812f32016-10-21 16:07:45 +0300762 info->value = g_malloc0(sizeof(*info->value));
763 info->value->name = g_strdup(name);
Marc-André Lureau88cace92016-12-09 00:50:12 +0300764 info->next = *list;
765 *list = info;
Marc-André Lureau0b812f32016-10-21 16:07:45 +0300766}
767
Martin Kletzander77d1c3c2014-02-01 12:52:42 +0100768ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
769{
770 ChardevBackendInfoList *backend_list = NULL;
Martin Kletzander77d1c3c2014-02-01 12:52:42 +0100771
Marc-André Lureau88cace92016-12-09 00:50:12 +0300772 chardev_name_foreach(qmp_prepend_backend, &backend_list);
Martin Kletzander77d1c3c2014-02-01 12:52:42 +0100773
774 return backend_list;
775}
776
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300777Chardev *qemu_chr_find(const char *name)
Gerd Hoffmannc845f402009-09-10 10:58:52 +0200778{
Marc-André Lureau60611622016-12-14 16:44:19 +0300779 Object *obj = object_resolve_path_component(get_chardevs_root(), name);
Gerd Hoffmannc845f402009-09-10 10:58:52 +0200780
Marc-André Lureau60611622016-12-14 16:44:19 +0300781 return obj ? CHARDEV(obj) : NULL;
Gerd Hoffmannc845f402009-09-10 10:58:52 +0200782}
Anthony Liguori0beb4942011-12-22 15:29:25 -0600783
Paolo Bonzini4d454572012-11-26 16:03:42 +0100784QemuOptsList qemu_chardev_opts = {
785 .name = "chardev",
786 .implied_opt_name = "backend",
787 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
788 .desc = {
789 {
790 .name = "backend",
791 .type = QEMU_OPT_STRING,
792 },{
793 .name = "path",
794 .type = QEMU_OPT_STRING,
795 },{
796 .name = "host",
797 .type = QEMU_OPT_STRING,
798 },{
799 .name = "port",
800 .type = QEMU_OPT_STRING,
801 },{
802 .name = "localaddr",
803 .type = QEMU_OPT_STRING,
804 },{
805 .name = "localport",
806 .type = QEMU_OPT_STRING,
807 },{
808 .name = "to",
809 .type = QEMU_OPT_NUMBER,
810 },{
811 .name = "ipv4",
812 .type = QEMU_OPT_BOOL,
813 },{
814 .name = "ipv6",
815 .type = QEMU_OPT_BOOL,
816 },{
817 .name = "wait",
818 .type = QEMU_OPT_BOOL,
819 },{
820 .name = "server",
821 .type = QEMU_OPT_BOOL,
822 },{
823 .name = "delay",
824 .type = QEMU_OPT_BOOL,
825 },{
Corey Minyard5dd1f022014-10-02 11:17:37 -0500826 .name = "reconnect",
827 .type = QEMU_OPT_NUMBER,
828 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +0100829 .name = "telnet",
830 .type = QEMU_OPT_BOOL,
831 },{
Jing Liuae92cbd2016-09-23 08:06:11 +0200832 .name = "tn3270",
833 .type = QEMU_OPT_BOOL,
834 },{
Daniel P. Berrangea8fb5422016-01-19 11:14:31 +0000835 .name = "tls-creds",
836 .type = QEMU_OPT_STRING,
837 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +0100838 .name = "width",
839 .type = QEMU_OPT_NUMBER,
840 },{
841 .name = "height",
842 .type = QEMU_OPT_NUMBER,
843 },{
844 .name = "cols",
845 .type = QEMU_OPT_NUMBER,
846 },{
847 .name = "rows",
848 .type = QEMU_OPT_NUMBER,
849 },{
850 .name = "mux",
851 .type = QEMU_OPT_BOOL,
852 },{
853 .name = "signal",
854 .type = QEMU_OPT_BOOL,
855 },{
856 .name = "name",
857 .type = QEMU_OPT_STRING,
858 },{
859 .name = "debug",
860 .type = QEMU_OPT_NUMBER,
Lei Li51767e72013-01-25 00:03:19 +0800861 },{
Markus Armbruster3949e592013-02-06 21:27:24 +0100862 .name = "size",
Markus Armbrusterde1cc362013-02-06 21:27:25 +0100863 .type = QEMU_OPT_SIZE,
Gerd Hoffmannbb6fb7c2013-06-24 08:39:54 +0200864 },{
865 .name = "chardev",
866 .type = QEMU_OPT_STRING,
Olga Krishtal31e38a22015-12-04 09:42:04 +0300867 },{
868 .name = "append",
869 .type = QEMU_OPT_BOOL,
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000870 },{
871 .name = "logfile",
872 .type = QEMU_OPT_STRING,
873 },{
874 .name = "logappend",
875 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100876 },
877 { /* end of list */ }
878 },
879};
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +0100880
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300881bool qemu_chr_has_feature(Chardev *chr,
Marc-André Lureau279b0662016-12-14 13:27:58 +0300882 ChardevFeature feature)
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100883{
884 return test_bit(feature, chr->features);
885}
886
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300887void qemu_chr_set_feature(Chardev *chr,
Marc-André Lureau279b0662016-12-14 13:27:58 +0300888 ChardevFeature feature)
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100889{
890 return set_bit(feature, chr->features);
891}
892
Marc-André Lureau777357d2016-12-07 18:39:10 +0300893Chardev *qemu_chardev_new(const char *id, const char *typename,
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +0300894 ChardevBackend *backend,
895 Error **errp)
Marc-André Lureau777357d2016-12-07 18:39:10 +0300896{
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +0300897 Object *obj;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300898 Chardev *chr = NULL;
Paolo Bonzinieaeba652015-09-29 14:54:05 +0200899 Error *local_err = NULL;
Marc-André Lureau82878da2016-10-22 13:09:43 +0300900 bool be_opened = true;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +0100901
Marc-André Lureau777357d2016-12-07 18:39:10 +0300902 assert(g_str_has_prefix(typename, "chardev-"));
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +0100903
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +0300904 obj = object_new(typename);
905 chr = CHARDEV(obj);
Marc-André Lureau777357d2016-12-07 18:39:10 +0300906 chr->label = g_strdup(id);
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +0100907
Marc-André Lureau777357d2016-12-07 18:39:10 +0300908 qemu_char_open(chr, backend, &be_opened, &local_err);
Marc-André Lureaua1698bf2016-10-21 16:30:29 +0300909 if (local_err) {
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +0300910 goto end;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +0100911 }
Paolo Bonzinieaeba652015-09-29 14:54:05 +0200912
Paolo Bonzinieaeba652015-09-29 14:54:05 +0200913 if (!chr->filename) {
Marc-André Lureau777357d2016-12-07 18:39:10 +0300914 chr->filename = g_strdup(typename + 8);
Paolo Bonzinieaeba652015-09-29 14:54:05 +0200915 }
Marc-André Lureau82878da2016-10-22 13:09:43 +0300916 if (be_opened) {
Paolo Bonzinieaeba652015-09-29 14:54:05 +0200917 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
918 }
Marc-André Lureau777357d2016-12-07 18:39:10 +0300919
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +0300920 if (id) {
921 object_property_add_child(get_chardevs_root(), id, obj, &local_err);
922 if (local_err) {
923 goto end;
924 }
925 object_unref(obj);
926 }
927
928end:
929 if (local_err) {
930 error_propagate(errp, local_err);
931 object_unref(obj);
932 return NULL;
933 }
934
Marc-André Lureau777357d2016-12-07 18:39:10 +0300935 return chr;
936}
937
938ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
939 Error **errp)
940{
941 const ChardevClass *cc;
942 ChardevReturn *ret;
943 Chardev *chr;
944
Markus Armbruster977c7362017-08-24 10:46:08 +0200945 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
Marc-André Lureau777357d2016-12-07 18:39:10 +0300946 if (!cc) {
947 return NULL;
948 }
949
Marc-André Lureauc5749f72016-12-14 20:58:50 +0300950 chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
Marc-André Lureau777357d2016-12-07 18:39:10 +0300951 backend, errp);
952 if (!chr) {
953 return NULL;
954 }
955
956 ret = g_new0(ChardevReturn, 1);
957 if (CHARDEV_IS_PTY(chr)) {
958 ret->pty = g_strdup(chr->filename + 4);
959 ret->has_pty = true;
960 }
961
Paolo Bonzinieaeba652015-09-29 14:54:05 +0200962 return ret;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +0100963}
964
Anton Nefedov7bb86082017-07-06 15:08:50 +0300965ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
966 Error **errp)
967{
968 CharBackend *be;
969 const ChardevClass *cc;
970 Chardev *chr, *chr_new;
971 bool closed_sent = false;
972 ChardevReturn *ret;
973
974 chr = qemu_chr_find(id);
975 if (!chr) {
976 error_setg(errp, "Chardev '%s' does not exist", id);
977 return NULL;
978 }
979
980 if (CHARDEV_IS_MUX(chr)) {
981 error_setg(errp, "Mux device hotswap not supported yet");
982 return NULL;
983 }
984
985 if (qemu_chr_replay(chr)) {
986 error_setg(errp,
987 "Chardev '%s' cannot be changed in record/replay mode", id);
988 return NULL;
989 }
990
991 be = chr->be;
992 if (!be) {
993 /* easy case */
994 object_unparent(OBJECT(chr));
995 return qmp_chardev_add(id, backend, errp);
996 }
997
998 if (!be->chr_be_change) {
999 error_setg(errp, "Chardev user does not support chardev hotswap");
1000 return NULL;
1001 }
1002
Markus Armbruster977c7362017-08-24 10:46:08 +02001003 cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
Anton Nefedov7bb86082017-07-06 15:08:50 +03001004 if (!cc) {
1005 return NULL;
1006 }
1007
1008 chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)),
1009 backend, errp);
1010 if (!chr_new) {
1011 return NULL;
1012 }
1013 chr_new->label = g_strdup(id);
1014
1015 if (chr->be_open && !chr_new->be_open) {
1016 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1017 closed_sent = true;
1018 }
1019
1020 chr->be = NULL;
1021 qemu_chr_fe_init(be, chr_new, &error_abort);
1022
1023 if (be->chr_be_change(be->opaque) < 0) {
1024 error_setg(errp, "Chardev '%s' change failed", chr_new->label);
1025 chr_new->be = NULL;
1026 qemu_chr_fe_init(be, chr, &error_abort);
1027 if (closed_sent) {
1028 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1029 }
1030 object_unref(OBJECT(chr_new));
1031 return NULL;
1032 }
1033
1034 object_unparent(OBJECT(chr));
1035 object_property_add_child(get_chardevs_root(), chr_new->label,
1036 OBJECT(chr_new), &error_abort);
1037 object_unref(OBJECT(chr_new));
1038
1039 ret = g_new0(ChardevReturn, 1);
1040 if (CHARDEV_IS_PTY(chr_new)) {
1041 ret->pty = g_strdup(chr_new->filename + 4);
1042 ret->has_pty = true;
1043 }
1044
1045 return ret;
1046}
1047
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01001048void qmp_chardev_remove(const char *id, Error **errp)
1049{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001050 Chardev *chr;
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01001051
1052 chr = qemu_chr_find(id);
Gonglei8108fd32014-08-11 21:00:55 +08001053 if (chr == NULL) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01001054 error_setg(errp, "Chardev '%s' not found", id);
1055 return;
1056 }
Marc-André Lureaua4afa542016-10-22 12:53:01 +03001057 if (qemu_chr_is_busy(chr)) {
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01001058 error_setg(errp, "Chardev '%s' is busy", id);
1059 return;
1060 }
Marc-André Lureau5ebd6702016-10-21 22:58:45 +03001061 if (qemu_chr_replay(chr)) {
Pavel Dovgalyuk33577b42016-03-14 10:44:36 +03001062 error_setg(errp,
1063 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1064 return;
1065 }
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +03001066 object_unparent(OBJECT(chr));
Gerd Hoffmannf1a1a352012-12-19 10:33:56 +01001067}
Anthony Liguorid654f342013-03-05 23:21:28 +05301068
Stefan Fritschbd1d5ad2017-06-11 09:48:17 +02001069void qmp_chardev_send_break(const char *id, Error **errp)
1070{
1071 Chardev *chr;
1072
1073 chr = qemu_chr_find(id);
1074 if (chr == NULL) {
1075 error_setg(errp, "Chardev '%s' not found", id);
1076 return;
1077 }
1078 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
1079}
1080
Marc-André Lureauaa5cb7f2016-07-04 17:38:23 +02001081void qemu_chr_cleanup(void)
Marc-André Lureauc1111a22016-06-16 21:28:50 +02001082{
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +03001083 object_unparent(get_chardevs_root());
Marc-André Lureauc1111a22016-06-16 21:28:50 +02001084}
1085
Anthony Liguorid654f342013-03-05 23:21:28 +05301086static void register_types(void)
1087{
Marc-André Lureau777357d2016-12-07 18:39:10 +03001088 type_register_static(&char_type_info);
Marc-André Lureau0b812f32016-10-21 16:07:45 +03001089
Michael Roth7b7ab182013-07-30 13:04:22 -05001090 /* this must be done after machine init, since we register FEs with muxes
1091 * as part of realize functions like serial_isa_realizefn when -nographic
1092 * is specified
1093 */
1094 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
Anthony Liguorid654f342013-03-05 23:21:28 +05301095}
1096
1097type_init(register_types);