]>
Commit | Line | Data |
---|---|---|
6f97dba0 AL |
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 | */ | |
d38ea87a | 24 | #include "qemu/osdep.h" |
6f97dba0 | 25 | #include "qemu-common.h" |
f348b6d1 | 26 | #include "qemu/cutils.h" |
83c9089e | 27 | #include "monitor/monitor.h" |
9c17d615 | 28 | #include "sysemu/sysemu.h" |
213dcb06 | 29 | #include "qemu/config-file.h" |
d49b6836 | 30 | #include "qemu/error-report.h" |
dccfcd0e | 31 | #include "sysemu/char.h" |
c5a415a0 | 32 | #include "qmp-commands.h" |
cfb429cb | 33 | #include "qapi-visit.h" |
33577b47 | 34 | #include "sysemu/replay.h" |
517b3d40 | 35 | #include "qemu/help_option.h" |
6f97dba0 | 36 | |
df85a78b | 37 | #include "char-mux.h" |
a6da7ffa | 38 | #include "char-io.h" |
6aa0f0c9 | 39 | #include "char-parallel.h" |
2b2f23da | 40 | #include "char-serial.h" |
df85a78b | 41 | |
6f97dba0 AL |
42 | /***********************************************************/ |
43 | /* character device */ | |
44 | ||
0ec7b3e7 | 45 | static QTAILQ_HEAD(ChardevHead, Chardev) chardevs = |
72cf2d4f | 46 | QTAILQ_HEAD_INITIALIZER(chardevs); |
2970a6c9 | 47 | |
0ec7b3e7 | 48 | void qemu_chr_be_event(Chardev *s, int event) |
6f97dba0 | 49 | { |
a4afa548 MAL |
50 | CharBackend *be = s->be; |
51 | ||
73cdf3f2 AG |
52 | /* Keep track if the char device is open */ |
53 | switch (event) { | |
54 | case CHR_EVENT_OPENED: | |
16665b94 | 55 | s->be_open = 1; |
73cdf3f2 AG |
56 | break; |
57 | case CHR_EVENT_CLOSED: | |
16665b94 | 58 | s->be_open = 0; |
73cdf3f2 AG |
59 | break; |
60 | } | |
61 | ||
a4afa548 | 62 | if (!be || !be->chr_event) { |
6f97dba0 | 63 | return; |
a4afa548 MAL |
64 | } |
65 | ||
66 | be->chr_event(be->opaque, event); | |
6f97dba0 AL |
67 | } |
68 | ||
0ec7b3e7 | 69 | void qemu_chr_be_generic_open(Chardev *s) |
6f97dba0 | 70 | { |
bd5c51ee | 71 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
6f97dba0 AL |
72 | } |
73 | ||
d0d7708b DB |
74 | |
75 | /* Not reporting errors from writing to logfile, as logs are | |
76 | * defined to be "best effort" only */ | |
0ec7b3e7 | 77 | static void qemu_chr_fe_write_log(Chardev *s, |
d0d7708b DB |
78 | const uint8_t *buf, size_t len) |
79 | { | |
80 | size_t done = 0; | |
81 | ssize_t ret; | |
82 | ||
83 | if (s->logfd < 0) { | |
84 | return; | |
85 | } | |
86 | ||
87 | while (done < len) { | |
53628efb DB |
88 | retry: |
89 | ret = write(s->logfd, buf + done, len - done); | |
90 | if (ret == -1 && errno == EAGAIN) { | |
91 | g_usleep(100); | |
92 | goto retry; | |
93 | } | |
d0d7708b DB |
94 | |
95 | if (ret <= 0) { | |
96 | return; | |
97 | } | |
98 | done += ret; | |
99 | } | |
100 | } | |
101 | ||
0ec7b3e7 MAL |
102 | static int qemu_chr_fe_write_buffer(Chardev *s, |
103 | const uint8_t *buf, int len, int *offset) | |
33577b47 | 104 | { |
777357d7 | 105 | ChardevClass *cc = CHARDEV_GET_CLASS(s); |
33577b47 PD |
106 | int res = 0; |
107 | *offset = 0; | |
108 | ||
109 | qemu_mutex_lock(&s->chr_write_lock); | |
110 | while (*offset < len) { | |
53628efb | 111 | retry: |
777357d7 | 112 | res = cc->chr_write(s, buf + *offset, len - *offset); |
53628efb DB |
113 | if (res < 0 && errno == EAGAIN) { |
114 | g_usleep(100); | |
115 | goto retry; | |
116 | } | |
33577b47 PD |
117 | |
118 | if (res <= 0) { | |
119 | break; | |
120 | } | |
121 | ||
122 | *offset += res; | |
123 | } | |
124 | if (*offset > 0) { | |
125 | qemu_chr_fe_write_log(s, buf, *offset); | |
126 | } | |
127 | qemu_mutex_unlock(&s->chr_write_lock); | |
128 | ||
129 | return res; | |
130 | } | |
131 | ||
0ec7b3e7 | 132 | static bool qemu_chr_replay(Chardev *chr) |
5ebd6703 MAL |
133 | { |
134 | return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY); | |
135 | } | |
136 | ||
5345fdb4 | 137 | int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) |
6f97dba0 | 138 | { |
0ec7b3e7 | 139 | Chardev *s = be->chr; |
777357d7 | 140 | ChardevClass *cc; |
9005b2a7 PB |
141 | int ret; |
142 | ||
fa394ed6 MAL |
143 | if (!s) { |
144 | return 0; | |
145 | } | |
146 | ||
5ebd6703 | 147 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { |
33577b47 PD |
148 | int offset; |
149 | replay_char_write_event_load(&ret, &offset); | |
150 | assert(offset <= len); | |
151 | qemu_chr_fe_write_buffer(s, buf, offset, &offset); | |
152 | return ret; | |
153 | } | |
154 | ||
777357d7 | 155 | cc = CHARDEV_GET_CLASS(s); |
9005b2a7 | 156 | qemu_mutex_lock(&s->chr_write_lock); |
777357d7 | 157 | ret = cc->chr_write(s, buf, len); |
d0d7708b DB |
158 | |
159 | if (ret > 0) { | |
160 | qemu_chr_fe_write_log(s, buf, ret); | |
161 | } | |
162 | ||
9005b2a7 | 163 | qemu_mutex_unlock(&s->chr_write_lock); |
33577b47 | 164 | |
5ebd6703 | 165 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { |
33577b47 PD |
166 | replay_char_write_event_save(ret, ret < 0 ? 0 : ret); |
167 | } | |
168 | ||
9005b2a7 | 169 | return ret; |
6f97dba0 AL |
170 | } |
171 | ||
df85a78b | 172 | int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len) |
cd18720a | 173 | { |
33577b47 PD |
174 | int offset; |
175 | int res; | |
cd18720a | 176 | |
5ebd6703 | 177 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { |
33577b47 PD |
178 | replay_char_write_event_load(&res, &offset); |
179 | assert(offset <= len); | |
180 | qemu_chr_fe_write_buffer(s, buf, offset, &offset); | |
181 | return res; | |
182 | } | |
cd18720a | 183 | |
33577b47 | 184 | res = qemu_chr_fe_write_buffer(s, buf, len, &offset); |
cd18720a | 185 | |
5ebd6703 | 186 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { |
33577b47 | 187 | replay_char_write_event_save(res, offset); |
cd18720a AL |
188 | } |
189 | ||
9005b2a7 PB |
190 | if (res < 0) { |
191 | return res; | |
192 | } | |
cd18720a AL |
193 | return offset; |
194 | } | |
195 | ||
5345fdb4 | 196 | int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len) |
7b0bfdf5 | 197 | { |
0ec7b3e7 | 198 | Chardev *s = be->chr; |
5345fdb4 | 199 | |
fa394ed6 MAL |
200 | if (!s) { |
201 | return 0; | |
202 | } | |
203 | ||
5345fdb4 MAL |
204 | return qemu_chr_write_all(s, buf, len); |
205 | } | |
206 | ||
207 | int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) | |
208 | { | |
0ec7b3e7 | 209 | Chardev *s = be->chr; |
7b0bfdf5 NN |
210 | int offset = 0, counter = 10; |
211 | int res; | |
212 | ||
777357d7 | 213 | if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) { |
7b0bfdf5 NN |
214 | return 0; |
215 | } | |
fa394ed6 | 216 | |
5ebd6703 | 217 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { |
33577b47 PD |
218 | return replay_char_read_all_load(buf); |
219 | } | |
7b0bfdf5 NN |
220 | |
221 | while (offset < len) { | |
53628efb | 222 | retry: |
777357d7 MAL |
223 | res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset, |
224 | len - offset); | |
53628efb DB |
225 | if (res == -1 && errno == EAGAIN) { |
226 | g_usleep(100); | |
227 | goto retry; | |
228 | } | |
7b0bfdf5 NN |
229 | |
230 | if (res == 0) { | |
231 | break; | |
232 | } | |
233 | ||
234 | if (res < 0) { | |
5ebd6703 | 235 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { |
33577b47 PD |
236 | replay_char_read_all_save_error(res); |
237 | } | |
7b0bfdf5 NN |
238 | return res; |
239 | } | |
240 | ||
241 | offset += res; | |
242 | ||
243 | if (!counter--) { | |
244 | break; | |
245 | } | |
246 | } | |
247 | ||
5ebd6703 | 248 | if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { |
33577b47 PD |
249 | replay_char_read_all_save_buf(buf, offset); |
250 | } | |
7b0bfdf5 NN |
251 | return offset; |
252 | } | |
253 | ||
5345fdb4 | 254 | int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg) |
6f97dba0 | 255 | { |
0ec7b3e7 | 256 | Chardev *s = be->chr; |
33577b47 | 257 | int res; |
fa394ed6 | 258 | |
777357d7 | 259 | if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) { |
33577b47 PD |
260 | res = -ENOTSUP; |
261 | } else { | |
777357d7 | 262 | res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg); |
33577b47 PD |
263 | } |
264 | ||
265 | return res; | |
6f97dba0 AL |
266 | } |
267 | ||
0ec7b3e7 | 268 | int qemu_chr_be_can_write(Chardev *s) |
6f97dba0 | 269 | { |
a4afa548 MAL |
270 | CharBackend *be = s->be; |
271 | ||
272 | if (!be || !be->chr_can_read) { | |
6f97dba0 | 273 | return 0; |
a4afa548 MAL |
274 | } |
275 | ||
276 | return be->chr_can_read(be->opaque); | |
6f97dba0 AL |
277 | } |
278 | ||
0ec7b3e7 | 279 | void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len) |
6f97dba0 | 280 | { |
a4afa548 MAL |
281 | CharBackend *be = s->be; |
282 | ||
283 | if (be && be->chr_read) { | |
284 | be->chr_read(be->opaque, buf, len); | |
ac310734 | 285 | } |
6f97dba0 AL |
286 | } |
287 | ||
0ec7b3e7 | 288 | void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len) |
33577b47 | 289 | { |
5ebd6703 | 290 | if (qemu_chr_replay(s)) { |
33577b47 PD |
291 | if (replay_mode == REPLAY_MODE_PLAY) { |
292 | return; | |
293 | } | |
294 | replay_chr_be_write(s, buf, len); | |
295 | } else { | |
296 | qemu_chr_be_write_impl(s, buf, len); | |
297 | } | |
298 | } | |
299 | ||
5345fdb4 | 300 | int qemu_chr_fe_get_msgfd(CharBackend *be) |
7d174059 | 301 | { |
0ec7b3e7 | 302 | Chardev *s = be->chr; |
c76bf6bb | 303 | int fd; |
5345fdb4 | 304 | int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1; |
5ebd6703 | 305 | if (s && qemu_chr_replay(s)) { |
776a32a0 MAL |
306 | error_report("Replay: get msgfd is not supported " |
307 | "for serial devices yet"); | |
33577b47 PD |
308 | exit(1); |
309 | } | |
310 | return res; | |
c76bf6bb NN |
311 | } |
312 | ||
5345fdb4 | 313 | int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len) |
c76bf6bb | 314 | { |
0ec7b3e7 | 315 | Chardev *s = be->chr; |
5345fdb4 | 316 | |
fa394ed6 MAL |
317 | if (!s) { |
318 | return -1; | |
319 | } | |
320 | ||
777357d7 MAL |
321 | return CHARDEV_GET_CLASS(s)->get_msgfds ? |
322 | CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1; | |
7d174059 MM |
323 | } |
324 | ||
5345fdb4 | 325 | int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num) |
d39aac7a | 326 | { |
0ec7b3e7 | 327 | Chardev *s = be->chr; |
5345fdb4 | 328 | |
fa394ed6 MAL |
329 | if (!s) { |
330 | return -1; | |
331 | } | |
332 | ||
777357d7 MAL |
333 | return CHARDEV_GET_CLASS(s)->set_msgfds ? |
334 | CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1; | |
d39aac7a NN |
335 | } |
336 | ||
0ec7b3e7 | 337 | int qemu_chr_add_client(Chardev *s, int fd) |
13661089 | 338 | { |
777357d7 MAL |
339 | return CHARDEV_GET_CLASS(s)->chr_add_client ? |
340 | CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1; | |
13661089 DB |
341 | } |
342 | ||
5345fdb4 | 343 | void qemu_chr_fe_accept_input(CharBackend *be) |
6f97dba0 | 344 | { |
0ec7b3e7 | 345 | Chardev *s = be->chr; |
5345fdb4 | 346 | |
fa394ed6 MAL |
347 | if (!s) { |
348 | return; | |
349 | } | |
350 | ||
777357d7 MAL |
351 | if (CHARDEV_GET_CLASS(s)->chr_accept_input) { |
352 | CHARDEV_GET_CLASS(s)->chr_accept_input(s); | |
b68e956a | 353 | } |
98c8ee1d | 354 | qemu_notify_event(); |
6f97dba0 AL |
355 | } |
356 | ||
5345fdb4 | 357 | void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) |
6f97dba0 | 358 | { |
f612143a | 359 | char buf[CHR_READ_BUF_LEN]; |
6f97dba0 AL |
360 | va_list ap; |
361 | va_start(ap, fmt); | |
362 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
90f998f5 DB |
363 | /* XXX this blocks entire thread. Rewrite to use |
364 | * qemu_chr_fe_write and background I/O callbacks */ | |
5345fdb4 | 365 | qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf)); |
6f97dba0 AL |
366 | va_end(ap); |
367 | } | |
368 | ||
777357d7 MAL |
369 | static void qemu_char_open(Chardev *chr, ChardevBackend *backend, |
370 | bool *be_opened, Error **errp) | |
6f97dba0 | 371 | { |
777357d7 MAL |
372 | ChardevClass *cc = CHARDEV_GET_CLASS(chr); |
373 | /* Any ChardevCommon member would work */ | |
374 | ChardevCommon *common = backend ? backend->u.null.data : NULL; | |
375 | ||
376 | if (common && common->has_logfile) { | |
377 | int flags = O_WRONLY | O_CREAT; | |
378 | if (common->has_logappend && | |
379 | common->logappend) { | |
380 | flags |= O_APPEND; | |
381 | } else { | |
382 | flags |= O_TRUNC; | |
383 | } | |
384 | chr->logfd = qemu_open(common->logfile, flags, 0666); | |
385 | if (chr->logfd < 0) { | |
386 | error_setg_errno(errp, errno, | |
387 | "Unable to open logfile %s", | |
388 | common->logfile); | |
389 | return; | |
390 | } | |
391 | } | |
392 | ||
393 | if (cc->open) { | |
394 | cc->open(chr, backend, be_opened, errp); | |
395 | } | |
6f97dba0 AL |
396 | } |
397 | ||
777357d7 | 398 | static void char_init(Object *obj) |
6f97dba0 | 399 | { |
777357d7 | 400 | Chardev *chr = CHARDEV(obj); |
6f97dba0 | 401 | |
777357d7 MAL |
402 | chr->logfd = -1; |
403 | qemu_mutex_init(&chr->chr_write_lock); | |
404 | } | |
405 | ||
eb314a94 MAL |
406 | static int null_chr_write(Chardev *chr, const uint8_t *buf, int len) |
407 | { | |
408 | return len; | |
409 | } | |
410 | ||
411 | static void char_class_init(ObjectClass *oc, void *data) | |
412 | { | |
413 | ChardevClass *cc = CHARDEV_CLASS(oc); | |
414 | ||
415 | cc->chr_write = null_chr_write; | |
416 | } | |
417 | ||
777357d7 MAL |
418 | static void char_finalize(Object *obj) |
419 | { | |
420 | Chardev *chr = CHARDEV(obj); | |
421 | ||
422 | if (chr->be) { | |
423 | chr->be->chr = NULL; | |
424 | } | |
425 | g_free(chr->filename); | |
426 | g_free(chr->label); | |
427 | if (chr->logfd != -1) { | |
428 | close(chr->logfd); | |
d0d7708b | 429 | } |
777357d7 MAL |
430 | qemu_mutex_destroy(&chr->chr_write_lock); |
431 | } | |
432 | ||
433 | static const TypeInfo char_type_info = { | |
434 | .name = TYPE_CHARDEV, | |
435 | .parent = TYPE_OBJECT, | |
436 | .instance_size = sizeof(Chardev), | |
437 | .instance_init = char_init, | |
438 | .instance_finalize = char_finalize, | |
439 | .abstract = true, | |
440 | .class_size = sizeof(ChardevClass), | |
eb314a94 | 441 | .class_init = char_class_init, |
777357d7 MAL |
442 | }; |
443 | ||
7b7ab18d MR |
444 | /** |
445 | * Called after processing of default and command-line-specified | |
446 | * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached | |
447 | * to a mux chardev. This is done here to ensure that | |
448 | * output/prompts/banners are only displayed for the FE that has | |
449 | * focus when initial command-line processing/machine init is | |
450 | * completed. | |
451 | * | |
452 | * After this point, any new FE attached to any new or existing | |
453 | * mux will receive CHR_EVENT_OPENED notifications for the BE | |
454 | * immediately. | |
455 | */ | |
456 | static void muxes_realize_done(Notifier *notifier, void *unused) | |
457 | { | |
0ec7b3e7 | 458 | Chardev *chr; |
7b7ab18d MR |
459 | |
460 | QTAILQ_FOREACH(chr, &chardevs, next) { | |
777357d7 MAL |
461 | if (CHARDEV_IS_MUX(chr)) { |
462 | MuxChardev *d = MUX_CHARDEV(chr); | |
7b7ab18d MR |
463 | int i; |
464 | ||
465 | /* send OPENED to all already-attached FEs */ | |
466 | for (i = 0; i < d->mux_cnt; i++) { | |
467 | mux_chr_send_event(d, i, CHR_EVENT_OPENED); | |
468 | } | |
469 | /* mark mux as OPENED so any new FEs will immediately receive | |
470 | * OPENED event | |
471 | */ | |
472 | qemu_chr_be_generic_open(chr); | |
473 | } | |
474 | } | |
475 | muxes_realized = true; | |
476 | } | |
477 | ||
478 | static Notifier muxes_realize_notify = { | |
479 | .notify = muxes_realize_done, | |
480 | }; | |
481 | ||
0ec7b3e7 | 482 | Chardev *qemu_chr_fe_get_driver(CharBackend *be) |
94a40fc5 MAL |
483 | { |
484 | return be->chr; | |
485 | } | |
486 | ||
0ec7b3e7 | 487 | bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) |
94a40fc5 MAL |
488 | { |
489 | int tag = 0; | |
490 | ||
777357d7 MAL |
491 | if (CHARDEV_IS_MUX(s)) { |
492 | MuxChardev *d = MUX_CHARDEV(s); | |
3aef23d7 MAL |
493 | |
494 | if (d->mux_cnt >= MAX_MUX) { | |
495 | goto unavailable; | |
94a40fc5 | 496 | } |
3aef23d7 MAL |
497 | |
498 | d->backends[d->mux_cnt] = b; | |
499 | tag = d->mux_cnt++; | |
500 | } else if (s->be) { | |
501 | goto unavailable; | |
a4afa548 MAL |
502 | } else { |
503 | s->be = b; | |
94a40fc5 MAL |
504 | } |
505 | ||
830896af | 506 | b->fe_open = false; |
94a40fc5 MAL |
507 | b->tag = tag; |
508 | b->chr = s; | |
94a40fc5 | 509 | return true; |
3aef23d7 MAL |
510 | |
511 | unavailable: | |
512 | error_setg(errp, QERR_DEVICE_IN_USE, s->label); | |
513 | return false; | |
94a40fc5 MAL |
514 | } |
515 | ||
0ec7b3e7 | 516 | static bool qemu_chr_is_busy(Chardev *s) |
a4afa548 | 517 | { |
777357d7 MAL |
518 | if (CHARDEV_IS_MUX(s)) { |
519 | MuxChardev *d = MUX_CHARDEV(s); | |
a4afa548 MAL |
520 | return d->mux_cnt >= 0; |
521 | } else { | |
522 | return s->be != NULL; | |
523 | } | |
524 | } | |
525 | ||
c39860e6 MAL |
526 | void qemu_chr_fe_deinit(CharBackend *b) |
527 | { | |
528 | assert(b); | |
529 | ||
530 | if (b->chr) { | |
39ab61c6 | 531 | qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true); |
fb5e19d2 MAL |
532 | if (b->chr->be == b) { |
533 | b->chr->be = NULL; | |
534 | } | |
777357d7 MAL |
535 | if (CHARDEV_IS_MUX(b->chr)) { |
536 | MuxChardev *d = MUX_CHARDEV(b->chr); | |
a4afa548 MAL |
537 | d->backends[b->tag] = NULL; |
538 | } | |
c39860e6 MAL |
539 | b->chr = NULL; |
540 | } | |
541 | } | |
542 | ||
94a40fc5 MAL |
543 | void qemu_chr_fe_set_handlers(CharBackend *b, |
544 | IOCanReadHandler *fd_can_read, | |
545 | IOReadHandler *fd_read, | |
546 | IOEventHandler *fd_event, | |
547 | void *opaque, | |
39ab61c6 MAL |
548 | GMainContext *context, |
549 | bool set_open) | |
94a40fc5 | 550 | { |
0ec7b3e7 | 551 | Chardev *s; |
777357d7 | 552 | ChardevClass *cc; |
386f07d1 MAL |
553 | int fe_open; |
554 | ||
555 | s = b->chr; | |
556 | if (!s) { | |
94a40fc5 MAL |
557 | return; |
558 | } | |
559 | ||
777357d7 | 560 | cc = CHARDEV_GET_CLASS(s); |
386f07d1 MAL |
561 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
562 | fe_open = 0; | |
8487ce45 | 563 | remove_fd_in_watch(s, context); |
386f07d1 MAL |
564 | } else { |
565 | fe_open = 1; | |
566 | } | |
a4afa548 MAL |
567 | b->chr_can_read = fd_can_read; |
568 | b->chr_read = fd_read; | |
569 | b->chr_event = fd_event; | |
570 | b->opaque = opaque; | |
777357d7 MAL |
571 | if (cc->chr_update_read_handler) { |
572 | cc->chr_update_read_handler(s, context); | |
386f07d1 MAL |
573 | } |
574 | ||
39ab61c6 | 575 | if (set_open) { |
386f07d1 MAL |
576 | qemu_chr_fe_set_open(b, fe_open); |
577 | } | |
94a40fc5 | 578 | |
386f07d1 MAL |
579 | if (fe_open) { |
580 | qemu_chr_fe_take_focus(b); | |
581 | /* We're connecting to an already opened device, so let's make sure we | |
582 | also get the open event */ | |
583 | if (s->be_open) { | |
584 | qemu_chr_be_generic_open(s); | |
585 | } | |
586 | } | |
587 | ||
777357d7 | 588 | if (CHARDEV_IS_MUX(s)) { |
386f07d1 | 589 | mux_chr_set_handlers(s, context); |
94a40fc5 MAL |
590 | } |
591 | } | |
592 | ||
593 | void qemu_chr_fe_take_focus(CharBackend *b) | |
594 | { | |
fa394ed6 MAL |
595 | if (!b->chr) { |
596 | return; | |
597 | } | |
598 | ||
777357d7 | 599 | if (CHARDEV_IS_MUX(b->chr)) { |
fb5e19d2 | 600 | mux_set_focus(b->chr, b->tag); |
94a40fc5 MAL |
601 | } |
602 | } | |
6f97dba0 | 603 | |
d24ca4b8 | 604 | int qemu_chr_wait_connected(Chardev *chr, Error **errp) |
6b6723c3 | 605 | { |
777357d7 MAL |
606 | ChardevClass *cc = CHARDEV_GET_CLASS(chr); |
607 | ||
608 | if (cc->chr_wait_connected) { | |
609 | return cc->chr_wait_connected(chr, errp); | |
6b6723c3 MAL |
610 | } |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
5345fdb4 MAL |
615 | int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp) |
616 | { | |
fa394ed6 MAL |
617 | if (!be->chr) { |
618 | error_setg(errp, "missing associated backend"); | |
619 | return -1; | |
620 | } | |
621 | ||
5345fdb4 MAL |
622 | return qemu_chr_wait_connected(be->chr, errp); |
623 | } | |
624 | ||
33521634 | 625 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
191bc01b | 626 | { |
6ea314d9 | 627 | char host[65], port[33], width[8], height[8]; |
aeb2c47a | 628 | int pos; |
7d31544f | 629 | const char *p; |
191bc01b | 630 | QemuOpts *opts; |
8be7e7e4 | 631 | Error *local_err = NULL; |
191bc01b | 632 | |
8be7e7e4 | 633 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
84d18f06 | 634 | if (local_err) { |
33394884 | 635 | error_report_err(local_err); |
191bc01b | 636 | return NULL; |
8be7e7e4 | 637 | } |
191bc01b | 638 | |
7591c5c1 GH |
639 | if (strstart(filename, "mon:", &p)) { |
640 | filename = p; | |
f43e47db | 641 | qemu_opt_set(opts, "mux", "on", &error_abort); |
02c4bdf1 PB |
642 | if (strcmp(filename, "stdio") == 0) { |
643 | /* Monitor is muxed to stdio: do not exit on Ctrl+C by default | |
644 | * but pass it to the guest. Handle this only for compat syntax, | |
645 | * for -chardev syntax we have special option for this. | |
646 | * This is what -nographic did, redirecting+muxing serial+monitor | |
647 | * to stdio causing Ctrl+C to be passed to guest. */ | |
f43e47db | 648 | qemu_opt_set(opts, "signal", "off", &error_abort); |
02c4bdf1 | 649 | } |
7591c5c1 GH |
650 | } |
651 | ||
f0457e8d GH |
652 | if (strcmp(filename, "null") == 0 || |
653 | strcmp(filename, "pty") == 0 || | |
654 | strcmp(filename, "msmouse") == 0 || | |
378af961 | 655 | strcmp(filename, "wctablet") == 0 || |
dc1c21e6 | 656 | strcmp(filename, "braille") == 0 || |
5692399f | 657 | strcmp(filename, "testdev") == 0 || |
f0457e8d | 658 | strcmp(filename, "stdio") == 0) { |
f43e47db | 659 | qemu_opt_set(opts, "backend", filename, &error_abort); |
191bc01b GH |
660 | return opts; |
661 | } | |
6ea314d9 | 662 | if (strstart(filename, "vc", &p)) { |
f43e47db | 663 | qemu_opt_set(opts, "backend", "vc", &error_abort); |
6ea314d9 | 664 | if (*p == ':') { |
49aa4058 | 665 | if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { |
6ea314d9 | 666 | /* pixels */ |
f43e47db MA |
667 | qemu_opt_set(opts, "width", width, &error_abort); |
668 | qemu_opt_set(opts, "height", height, &error_abort); | |
49aa4058 | 669 | } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { |
6ea314d9 | 670 | /* chars */ |
f43e47db MA |
671 | qemu_opt_set(opts, "cols", width, &error_abort); |
672 | qemu_opt_set(opts, "rows", height, &error_abort); | |
6ea314d9 GH |
673 | } else { |
674 | goto fail; | |
675 | } | |
676 | } | |
677 | return opts; | |
678 | } | |
d6c983cd | 679 | if (strcmp(filename, "con:") == 0) { |
f43e47db | 680 | qemu_opt_set(opts, "backend", "console", &error_abort); |
d6c983cd GH |
681 | return opts; |
682 | } | |
48b76496 | 683 | if (strstart(filename, "COM", NULL)) { |
f43e47db MA |
684 | qemu_opt_set(opts, "backend", "serial", &error_abort); |
685 | qemu_opt_set(opts, "path", filename, &error_abort); | |
48b76496 GH |
686 | return opts; |
687 | } | |
7d31544f | 688 | if (strstart(filename, "file:", &p)) { |
f43e47db MA |
689 | qemu_opt_set(opts, "backend", "file", &error_abort); |
690 | qemu_opt_set(opts, "path", p, &error_abort); | |
7d31544f GH |
691 | return opts; |
692 | } | |
693 | if (strstart(filename, "pipe:", &p)) { | |
f43e47db MA |
694 | qemu_opt_set(opts, "backend", "pipe", &error_abort); |
695 | qemu_opt_set(opts, "path", p, &error_abort); | |
7d31544f GH |
696 | return opts; |
697 | } | |
aeb2c47a GH |
698 | if (strstart(filename, "tcp:", &p) || |
699 | strstart(filename, "telnet:", &p)) { | |
700 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
701 | host[0] = 0; | |
702 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) | |
703 | goto fail; | |
704 | } | |
f43e47db MA |
705 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
706 | qemu_opt_set(opts, "host", host, &error_abort); | |
707 | qemu_opt_set(opts, "port", port, &error_abort); | |
aeb2c47a | 708 | if (p[pos] == ',') { |
dc523cd3 MA |
709 | qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); |
710 | if (local_err) { | |
711 | error_report_err(local_err); | |
aeb2c47a | 712 | goto fail; |
dc523cd3 | 713 | } |
aeb2c47a GH |
714 | } |
715 | if (strstart(filename, "telnet:", &p)) | |
f43e47db | 716 | qemu_opt_set(opts, "telnet", "on", &error_abort); |
aeb2c47a GH |
717 | return opts; |
718 | } | |
7e1b35b4 | 719 | if (strstart(filename, "udp:", &p)) { |
f43e47db | 720 | qemu_opt_set(opts, "backend", "udp", &error_abort); |
7e1b35b4 GH |
721 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
722 | host[0] = 0; | |
39324ca4 | 723 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
7e1b35b4 GH |
724 | goto fail; |
725 | } | |
726 | } | |
f43e47db MA |
727 | qemu_opt_set(opts, "host", host, &error_abort); |
728 | qemu_opt_set(opts, "port", port, &error_abort); | |
7e1b35b4 GH |
729 | if (p[pos] == '@') { |
730 | p += pos + 1; | |
731 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
732 | host[0] = 0; | |
733 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { | |
7e1b35b4 GH |
734 | goto fail; |
735 | } | |
736 | } | |
f43e47db MA |
737 | qemu_opt_set(opts, "localaddr", host, &error_abort); |
738 | qemu_opt_set(opts, "localport", port, &error_abort); | |
7e1b35b4 GH |
739 | } |
740 | return opts; | |
741 | } | |
aeb2c47a | 742 | if (strstart(filename, "unix:", &p)) { |
f43e47db | 743 | qemu_opt_set(opts, "backend", "socket", &error_abort); |
dc523cd3 MA |
744 | qemu_opts_do_parse(opts, p, "path", &local_err); |
745 | if (local_err) { | |
746 | error_report_err(local_err); | |
aeb2c47a | 747 | goto fail; |
dc523cd3 | 748 | } |
aeb2c47a GH |
749 | return opts; |
750 | } | |
48b76496 GH |
751 | if (strstart(filename, "/dev/parport", NULL) || |
752 | strstart(filename, "/dev/ppi", NULL)) { | |
f43e47db MA |
753 | qemu_opt_set(opts, "backend", "parport", &error_abort); |
754 | qemu_opt_set(opts, "path", filename, &error_abort); | |
48b76496 GH |
755 | return opts; |
756 | } | |
757 | if (strstart(filename, "/dev/", NULL)) { | |
f43e47db MA |
758 | qemu_opt_set(opts, "backend", "tty", &error_abort); |
759 | qemu_opt_set(opts, "path", filename, &error_abort); | |
48b76496 GH |
760 | return opts; |
761 | } | |
191bc01b | 762 | |
aeb2c47a | 763 | fail: |
191bc01b GH |
764 | qemu_opts_del(opts); |
765 | return NULL; | |
766 | } | |
767 | ||
21a933ea | 768 | void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) |
d0d7708b DB |
769 | { |
770 | const char *logfile = qemu_opt_get(opts, "logfile"); | |
771 | ||
772 | backend->has_logfile = logfile != NULL; | |
773 | backend->logfile = logfile ? g_strdup(logfile) : NULL; | |
774 | ||
775 | backend->has_logappend = true; | |
776 | backend->logappend = qemu_opt_get_bool(opts, "logappend", false); | |
777 | } | |
778 | ||
88cace9f MAL |
779 | static const ChardevClass *char_get_class(const char *driver, Error **errp) |
780 | { | |
781 | ObjectClass *oc; | |
782 | const ChardevClass *cc; | |
783 | char *typename = g_strdup_printf("chardev-%s", driver); | |
784 | ||
785 | oc = object_class_by_name(typename); | |
786 | g_free(typename); | |
787 | ||
788 | if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) { | |
789 | error_setg(errp, "'%s' is not a valid char driver name", driver); | |
790 | return NULL; | |
791 | } | |
d654f34e | 792 | |
88cace9f MAL |
793 | if (object_class_is_abstract(oc)) { |
794 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", | |
795 | "abstract device type"); | |
796 | return NULL; | |
797 | } | |
798 | ||
799 | cc = CHARDEV_CLASS(oc); | |
800 | if (cc->internal) { | |
801 | error_setg(errp, "'%s' is not a valid char driver name", driver); | |
802 | return NULL; | |
803 | } | |
804 | ||
805 | return cc; | |
806 | } | |
807 | ||
0b663b7d MAL |
808 | static Chardev *qemu_chardev_add(const char *id, const char *typename, |
809 | ChardevBackend *backend, Error **errp) | |
810 | { | |
811 | Chardev *chr; | |
812 | ||
813 | chr = qemu_chr_find(id); | |
814 | if (chr) { | |
815 | error_setg(errp, "Chardev '%s' already exists", id); | |
816 | return NULL; | |
817 | } | |
818 | ||
819 | chr = qemu_chardev_new(id, typename, backend, errp); | |
820 | if (!chr) { | |
821 | return NULL; | |
822 | } | |
823 | ||
824 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); | |
825 | return chr; | |
826 | } | |
827 | ||
88cace9f MAL |
828 | static const struct ChardevAlias { |
829 | const char *typename; | |
830 | const char *alias; | |
831 | } chardev_alias_table[] = { | |
832 | #ifdef HAVE_CHARDEV_PARPORT | |
833 | { "parallel", "parport" }, | |
834 | #endif | |
835 | #ifdef HAVE_CHARDEV_SERIAL | |
836 | { "serial", "tty" }, | |
837 | #endif | |
838 | }; | |
839 | ||
840 | typedef struct ChadevClassFE { | |
841 | void (*fn)(const char *name, void *opaque); | |
842 | void *opaque; | |
843 | } ChadevClassFE; | |
844 | ||
845 | static void | |
846 | chardev_class_foreach(ObjectClass *klass, void *opaque) | |
2c5f4882 | 847 | { |
88cace9f MAL |
848 | ChadevClassFE *fe = opaque; |
849 | ||
850 | assert(g_str_has_prefix(object_class_get_name(klass), "chardev-")); | |
851 | if (CHARDEV_CLASS(klass)->internal) { | |
852 | return; | |
853 | } | |
854 | ||
855 | fe->fn(object_class_get_name(klass) + 8, fe->opaque); | |
856 | } | |
857 | ||
858 | static void | |
859 | chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque) | |
860 | { | |
861 | ChadevClassFE fe = { .fn = fn, .opaque = opaque }; | |
862 | int i; | |
863 | ||
864 | object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe); | |
865 | ||
866 | for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) { | |
867 | fn(chardev_alias_table[i].alias, opaque); | |
868 | } | |
869 | } | |
870 | ||
871 | static void | |
872 | help_string_append(const char *name, void *opaque) | |
873 | { | |
874 | GString *str = opaque; | |
875 | ||
876 | g_string_append_printf(str, "\n%s", name); | |
2c5f4882 GH |
877 | } |
878 | ||
0ec7b3e7 MAL |
879 | Chardev *qemu_chr_new_from_opts(QemuOpts *opts, |
880 | Error **errp) | |
191bc01b | 881 | { |
0aff637e | 882 | Error *local_err = NULL; |
88cace9f | 883 | const ChardevClass *cc; |
0ec7b3e7 | 884 | Chardev *chr; |
a1698bf1 | 885 | int i; |
0b663b7d | 886 | ChardevBackend *backend = NULL; |
0b812f31 | 887 | const char *name = qemu_opt_get(opts, "backend"); |
a61ae7f8 PM |
888 | const char *id = qemu_opts_id(opts); |
889 | char *bid = NULL; | |
191bc01b | 890 | |
0b812f31 | 891 | if (name == NULL) { |
312fd5f2 | 892 | error_setg(errp, "chardev: \"%s\" missing backend", |
bd2d80b2 | 893 | qemu_opts_id(opts)); |
0b663b7d | 894 | return NULL; |
1bbd185f | 895 | } |
517b3d40 | 896 | |
0b812f31 | 897 | if (is_help_option(name)) { |
776a32a0 | 898 | GString *str = g_string_new(""); |
88cace9f MAL |
899 | |
900 | chardev_name_foreach(help_string_append, str); | |
776a32a0 MAL |
901 | |
902 | error_report("Available chardev backend types: %s", str->str); | |
903 | g_string_free(str, true); | |
0b812f31 | 904 | exit(0); |
517b3d40 LM |
905 | } |
906 | ||
907 | if (id == NULL) { | |
908 | error_setg(errp, "chardev: no id specified"); | |
0b663b7d | 909 | return NULL; |
517b3d40 LM |
910 | } |
911 | ||
88cace9f MAL |
912 | for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) { |
913 | if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) { | |
914 | name = chardev_alias_table[i].typename; | |
191bc01b | 915 | break; |
d654f34e | 916 | } |
191bc01b | 917 | } |
88cace9f MAL |
918 | |
919 | cc = char_get_class(name, errp); | |
920 | if (cc == NULL) { | |
0b663b7d | 921 | return NULL; |
191bc01b GH |
922 | } |
923 | ||
a61ae7f8 | 924 | backend = g_new0(ChardevBackend, 1); |
0b663b7d | 925 | backend->type = CHARDEV_BACKEND_KIND_NULL; |
edb2fb3c | 926 | |
a61ae7f8 PM |
927 | if (qemu_opt_get_bool(opts, "mux", 0)) { |
928 | bid = g_strdup_printf("%s-base", id); | |
929 | } | |
2c5f4882 | 930 | |
a61ae7f8 | 931 | chr = NULL; |
88cace9f MAL |
932 | if (cc->parse) { |
933 | cc->parse(opts, backend, &local_err); | |
a61ae7f8 PM |
934 | if (local_err) { |
935 | error_propagate(errp, local_err); | |
0b663b7d | 936 | goto out; |
2c5f4882 | 937 | } |
d0d7708b | 938 | } else { |
88cace9f MAL |
939 | ChardevCommon *ccom = g_new0(ChardevCommon, 1); |
940 | qemu_chr_parse_common(opts, ccom); | |
941 | backend->u.null.data = ccom; /* Any ChardevCommon member would work */ | |
2c5f4882 | 942 | } |
d0d7708b | 943 | |
0b663b7d MAL |
944 | chr = qemu_chardev_add(bid ? bid : id, |
945 | object_class_get_name(OBJECT_CLASS(cc)), | |
946 | backend, errp); | |
947 | if (chr == NULL) { | |
948 | goto out; | |
191bc01b GH |
949 | } |
950 | ||
a61ae7f8 | 951 | if (bid) { |
0b663b7d | 952 | Chardev *mux; |
a61ae7f8 | 953 | qapi_free_ChardevBackend(backend); |
a61ae7f8 | 954 | backend = g_new0(ChardevBackend, 1); |
130257dc | 955 | backend->type = CHARDEV_BACKEND_KIND_MUX; |
0b663b7d | 956 | backend->u.mux.data = g_new0(ChardevMux, 1); |
32bafa8f | 957 | backend->u.mux.data->chardev = g_strdup(bid); |
0b663b7d MAL |
958 | mux = qemu_chardev_add(id, TYPE_CHARDEV_MUX, backend, errp); |
959 | if (mux == NULL) { | |
a61ae7f8 PM |
960 | qemu_chr_delete(chr); |
961 | chr = NULL; | |
0b663b7d | 962 | goto out; |
a61ae7f8 | 963 | } |
0b663b7d | 964 | chr = mux; |
bd5c51ee | 965 | } |
7591c5c1 | 966 | |
0b663b7d | 967 | out: |
a61ae7f8 | 968 | qapi_free_ChardevBackend(backend); |
a61ae7f8 | 969 | g_free(bid); |
191bc01b GH |
970 | return chr; |
971 | } | |
972 | ||
0ec7b3e7 | 973 | Chardev *qemu_chr_new_noreplay(const char *label, const char *filename) |
6f97dba0 AL |
974 | { |
975 | const char *p; | |
0ec7b3e7 | 976 | Chardev *chr; |
191bc01b | 977 | QemuOpts *opts; |
bd2d80b2 | 978 | Error *err = NULL; |
191bc01b | 979 | |
c845f401 GH |
980 | if (strstart(filename, "chardev:", &p)) { |
981 | return qemu_chr_find(p); | |
982 | } | |
983 | ||
191bc01b | 984 | opts = qemu_chr_parse_compat(label, filename); |
7e1b35b4 GH |
985 | if (!opts) |
986 | return NULL; | |
6f97dba0 | 987 | |
b4948be9 | 988 | chr = qemu_chr_new_from_opts(opts, &err); |
84d18f06 | 989 | if (err) { |
565f65d2 | 990 | error_report_err(err); |
bd2d80b2 | 991 | } |
7e1b35b4 GH |
992 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
993 | monitor_init(chr, MONITOR_USE_READLINE); | |
6f97dba0 | 994 | } |
0a73336d | 995 | qemu_opts_del(opts); |
6f97dba0 AL |
996 | return chr; |
997 | } | |
998 | ||
0ec7b3e7 | 999 | Chardev *qemu_chr_new(const char *label, const char *filename) |
33577b47 | 1000 | { |
0ec7b3e7 | 1001 | Chardev *chr; |
b4948be9 | 1002 | chr = qemu_chr_new_noreplay(label, filename); |
33577b47 | 1003 | if (chr) { |
5ebd6703 MAL |
1004 | if (replay_mode != REPLAY_MODE_NONE) { |
1005 | qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY); | |
1006 | } | |
777357d7 | 1007 | if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) { |
776a32a0 MAL |
1008 | error_report("Replay: ioctl is not supported " |
1009 | "for serial devices yet"); | |
33577b47 PD |
1010 | } |
1011 | replay_register_char_driver(chr); | |
1012 | } | |
1013 | return chr; | |
1014 | } | |
1015 | ||
5345fdb4 | 1016 | void qemu_chr_fe_set_echo(CharBackend *be, bool echo) |
c48855e1 | 1017 | { |
0ec7b3e7 | 1018 | Chardev *chr = be->chr; |
5345fdb4 | 1019 | |
777357d7 MAL |
1020 | if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) { |
1021 | CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo); | |
c48855e1 PB |
1022 | } |
1023 | } | |
1024 | ||
5345fdb4 | 1025 | void qemu_chr_fe_set_open(CharBackend *be, int fe_open) |
7c32c4fe | 1026 | { |
0ec7b3e7 | 1027 | Chardev *chr = be->chr; |
5345fdb4 | 1028 | |
fa394ed6 MAL |
1029 | if (!chr) { |
1030 | return; | |
1031 | } | |
1032 | ||
830896af | 1033 | if (be->fe_open == fe_open) { |
c0c4bd2c HG |
1034 | return; |
1035 | } | |
830896af | 1036 | be->fe_open = fe_open; |
777357d7 MAL |
1037 | if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) { |
1038 | CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open); | |
7c32c4fe HG |
1039 | } |
1040 | } | |
1041 | ||
5345fdb4 | 1042 | guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond, |
6f1de6b7 | 1043 | GIOFunc func, void *user_data) |
23673ca7 | 1044 | { |
0ec7b3e7 | 1045 | Chardev *s = be->chr; |
23673ca7 AL |
1046 | GSource *src; |
1047 | guint tag; | |
1048 | ||
777357d7 | 1049 | if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) { |
6f1de6b7 | 1050 | return 0; |
23673ca7 AL |
1051 | } |
1052 | ||
777357d7 | 1053 | src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond); |
62c339c5 | 1054 | if (!src) { |
6f1de6b7 | 1055 | return 0; |
62c339c5 PB |
1056 | } |
1057 | ||
23673ca7 AL |
1058 | g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); |
1059 | tag = g_source_attach(src, NULL); | |
1060 | g_source_unref(src); | |
1061 | ||
1062 | return tag; | |
1063 | } | |
1064 | ||
5345fdb4 | 1065 | void qemu_chr_fe_disconnect(CharBackend *be) |
7d9d17f7 | 1066 | { |
0ec7b3e7 | 1067 | Chardev *chr = be->chr; |
5345fdb4 | 1068 | |
777357d7 MAL |
1069 | if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) { |
1070 | CHARDEV_GET_CLASS(chr)->chr_disconnect(chr); | |
7d9d17f7 TM |
1071 | } |
1072 | } | |
1073 | ||
0ec7b3e7 | 1074 | void qemu_chr_delete(Chardev *chr) |
1ad78ea5 MAL |
1075 | { |
1076 | QTAILQ_REMOVE(&chardevs, chr, next); | |
8cddc469 | 1077 | object_unref(OBJECT(chr)); |
1ad78ea5 MAL |
1078 | } |
1079 | ||
c5a415a0 | 1080 | ChardevInfoList *qmp_query_chardev(Error **errp) |
6f97dba0 | 1081 | { |
c5a415a0 | 1082 | ChardevInfoList *chr_list = NULL; |
0ec7b3e7 | 1083 | Chardev *chr; |
6f97dba0 | 1084 | |
72cf2d4f | 1085 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c5a415a0 LC |
1086 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
1087 | info->value = g_malloc0(sizeof(*info->value)); | |
1088 | info->value->label = g_strdup(chr->label); | |
1089 | info->value->filename = g_strdup(chr->filename); | |
830896af | 1090 | info->value->frontend_open = chr->be && chr->be->fe_open; |
c5a415a0 LC |
1091 | |
1092 | info->next = chr_list; | |
1093 | chr_list = info; | |
6f97dba0 | 1094 | } |
588b3832 | 1095 | |
c5a415a0 | 1096 | return chr_list; |
6f97dba0 | 1097 | } |
c845f401 | 1098 | |
88cace9f MAL |
1099 | static void |
1100 | qmp_prepend_backend(const char *name, void *opaque) | |
0b812f31 | 1101 | { |
88cace9f | 1102 | ChardevBackendInfoList **list = opaque; |
0b812f31 | 1103 | ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); |
88cace9f | 1104 | |
0b812f31 MAL |
1105 | info->value = g_malloc0(sizeof(*info->value)); |
1106 | info->value->name = g_strdup(name); | |
88cace9f MAL |
1107 | info->next = *list; |
1108 | *list = info; | |
0b812f31 MAL |
1109 | } |
1110 | ||
77d1c3c6 MK |
1111 | ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) |
1112 | { | |
1113 | ChardevBackendInfoList *backend_list = NULL; | |
a1698bf1 | 1114 | |
88cace9f | 1115 | chardev_name_foreach(qmp_prepend_backend, &backend_list); |
77d1c3c6 MK |
1116 | |
1117 | return backend_list; | |
1118 | } | |
1119 | ||
0ec7b3e7 | 1120 | Chardev *qemu_chr_find(const char *name) |
c845f401 | 1121 | { |
0ec7b3e7 | 1122 | Chardev *chr; |
c845f401 | 1123 | |
72cf2d4f | 1124 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c845f401 GH |
1125 | if (strcmp(chr->label, name) != 0) |
1126 | continue; | |
1127 | return chr; | |
1128 | } | |
1129 | return NULL; | |
1130 | } | |
0beb4942 | 1131 | |
4d454574 PB |
1132 | QemuOptsList qemu_chardev_opts = { |
1133 | .name = "chardev", | |
1134 | .implied_opt_name = "backend", | |
1135 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), | |
1136 | .desc = { | |
1137 | { | |
1138 | .name = "backend", | |
1139 | .type = QEMU_OPT_STRING, | |
1140 | },{ | |
1141 | .name = "path", | |
1142 | .type = QEMU_OPT_STRING, | |
1143 | },{ | |
1144 | .name = "host", | |
1145 | .type = QEMU_OPT_STRING, | |
1146 | },{ | |
1147 | .name = "port", | |
1148 | .type = QEMU_OPT_STRING, | |
1149 | },{ | |
1150 | .name = "localaddr", | |
1151 | .type = QEMU_OPT_STRING, | |
1152 | },{ | |
1153 | .name = "localport", | |
1154 | .type = QEMU_OPT_STRING, | |
1155 | },{ | |
1156 | .name = "to", | |
1157 | .type = QEMU_OPT_NUMBER, | |
1158 | },{ | |
1159 | .name = "ipv4", | |
1160 | .type = QEMU_OPT_BOOL, | |
1161 | },{ | |
1162 | .name = "ipv6", | |
1163 | .type = QEMU_OPT_BOOL, | |
1164 | },{ | |
1165 | .name = "wait", | |
1166 | .type = QEMU_OPT_BOOL, | |
1167 | },{ | |
1168 | .name = "server", | |
1169 | .type = QEMU_OPT_BOOL, | |
1170 | },{ | |
1171 | .name = "delay", | |
1172 | .type = QEMU_OPT_BOOL, | |
5dd1f02b CM |
1173 | },{ |
1174 | .name = "reconnect", | |
1175 | .type = QEMU_OPT_NUMBER, | |
4d454574 PB |
1176 | },{ |
1177 | .name = "telnet", | |
1178 | .type = QEMU_OPT_BOOL, | |
a8fb5427 DB |
1179 | },{ |
1180 | .name = "tls-creds", | |
1181 | .type = QEMU_OPT_STRING, | |
4d454574 PB |
1182 | },{ |
1183 | .name = "width", | |
1184 | .type = QEMU_OPT_NUMBER, | |
1185 | },{ | |
1186 | .name = "height", | |
1187 | .type = QEMU_OPT_NUMBER, | |
1188 | },{ | |
1189 | .name = "cols", | |
1190 | .type = QEMU_OPT_NUMBER, | |
1191 | },{ | |
1192 | .name = "rows", | |
1193 | .type = QEMU_OPT_NUMBER, | |
1194 | },{ | |
1195 | .name = "mux", | |
1196 | .type = QEMU_OPT_BOOL, | |
1197 | },{ | |
1198 | .name = "signal", | |
1199 | .type = QEMU_OPT_BOOL, | |
1200 | },{ | |
1201 | .name = "name", | |
1202 | .type = QEMU_OPT_STRING, | |
1203 | },{ | |
1204 | .name = "debug", | |
1205 | .type = QEMU_OPT_NUMBER, | |
51767e7c | 1206 | },{ |
3949e594 | 1207 | .name = "size", |
de1cc36e | 1208 | .type = QEMU_OPT_SIZE, |
bb6fb7c0 GH |
1209 | },{ |
1210 | .name = "chardev", | |
1211 | .type = QEMU_OPT_STRING, | |
31e38a22 OK |
1212 | },{ |
1213 | .name = "append", | |
1214 | .type = QEMU_OPT_BOOL, | |
d0d7708b DB |
1215 | },{ |
1216 | .name = "logfile", | |
1217 | .type = QEMU_OPT_STRING, | |
1218 | },{ | |
1219 | .name = "logappend", | |
1220 | .type = QEMU_OPT_BOOL, | |
4d454574 PB |
1221 | }, |
1222 | { /* end of list */ } | |
1223 | }, | |
1224 | }; | |
f1a1a356 | 1225 | |
0ec7b3e7 | 1226 | bool qemu_chr_has_feature(Chardev *chr, |
279b066e | 1227 | ChardevFeature feature) |
0a73336d DB |
1228 | { |
1229 | return test_bit(feature, chr->features); | |
1230 | } | |
1231 | ||
0ec7b3e7 | 1232 | void qemu_chr_set_feature(Chardev *chr, |
279b066e | 1233 | ChardevFeature feature) |
0a73336d DB |
1234 | { |
1235 | return set_bit(feature, chr->features); | |
1236 | } | |
1237 | ||
777357d7 MAL |
1238 | Chardev *qemu_chardev_new(const char *id, const char *typename, |
1239 | ChardevBackend *backend, Error **errp) | |
f1a1a356 | 1240 | { |
0ec7b3e7 | 1241 | Chardev *chr = NULL; |
eaeba653 | 1242 | Error *local_err = NULL; |
82878dac | 1243 | bool be_opened = true; |
f1a1a356 | 1244 | |
777357d7 | 1245 | assert(g_str_has_prefix(typename, "chardev-")); |
f1a1a356 | 1246 | |
777357d7 MAL |
1247 | chr = CHARDEV(object_new(typename)); |
1248 | chr->label = g_strdup(id); | |
4ca17281 | 1249 | |
777357d7 | 1250 | qemu_char_open(chr, backend, &be_opened, &local_err); |
a1698bf1 MAL |
1251 | if (local_err) { |
1252 | error_propagate(errp, local_err); | |
777357d7 MAL |
1253 | object_unref(OBJECT(chr)); |
1254 | return NULL; | |
eaeba653 PB |
1255 | } |
1256 | ||
eaeba653 | 1257 | if (!chr->filename) { |
777357d7 | 1258 | chr->filename = g_strdup(typename + 8); |
f1a1a356 | 1259 | } |
82878dac | 1260 | if (be_opened) { |
eaeba653 PB |
1261 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
1262 | } | |
777357d7 MAL |
1263 | |
1264 | return chr; | |
1265 | } | |
1266 | ||
1267 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, | |
1268 | Error **errp) | |
1269 | { | |
1270 | const ChardevClass *cc; | |
1271 | ChardevReturn *ret; | |
1272 | Chardev *chr; | |
1273 | ||
777357d7 MAL |
1274 | cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp); |
1275 | if (!cc) { | |
1276 | return NULL; | |
1277 | } | |
1278 | ||
0b663b7d | 1279 | chr = qemu_chardev_add(id, object_class_get_name(OBJECT_CLASS(cc)), |
777357d7 MAL |
1280 | backend, errp); |
1281 | if (!chr) { | |
1282 | return NULL; | |
1283 | } | |
1284 | ||
1285 | ret = g_new0(ChardevReturn, 1); | |
1286 | if (CHARDEV_IS_PTY(chr)) { | |
1287 | ret->pty = g_strdup(chr->filename + 4); | |
1288 | ret->has_pty = true; | |
1289 | } | |
1290 | ||
eaeba653 | 1291 | return ret; |
f1a1a356 GH |
1292 | } |
1293 | ||
1294 | void qmp_chardev_remove(const char *id, Error **errp) | |
1295 | { | |
0ec7b3e7 | 1296 | Chardev *chr; |
f1a1a356 GH |
1297 | |
1298 | chr = qemu_chr_find(id); | |
8108fd3e | 1299 | if (chr == NULL) { |
f1a1a356 GH |
1300 | error_setg(errp, "Chardev '%s' not found", id); |
1301 | return; | |
1302 | } | |
a4afa548 | 1303 | if (qemu_chr_is_busy(chr)) { |
f1a1a356 GH |
1304 | error_setg(errp, "Chardev '%s' is busy", id); |
1305 | return; | |
1306 | } | |
5ebd6703 | 1307 | if (qemu_chr_replay(chr)) { |
33577b47 PD |
1308 | error_setg(errp, |
1309 | "Chardev '%s' cannot be unplugged in record/replay mode", id); | |
1310 | return; | |
1311 | } | |
f1a1a356 GH |
1312 | qemu_chr_delete(chr); |
1313 | } | |
d654f34e | 1314 | |
aa5cb7f5 | 1315 | void qemu_chr_cleanup(void) |
c1111a24 | 1316 | { |
0ec7b3e7 | 1317 | Chardev *chr, *tmp; |
c1111a24 MAL |
1318 | |
1319 | QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) { | |
1320 | qemu_chr_delete(chr); | |
1321 | } | |
1322 | } | |
1323 | ||
d654f34e AL |
1324 | static void register_types(void) |
1325 | { | |
88cace9f | 1326 | type_register_static(&char_type_info); |
0b812f31 | 1327 | |
7b7ab18d MR |
1328 | /* this must be done after machine init, since we register FEs with muxes |
1329 | * as part of realize functions like serial_isa_realizefn when -nographic | |
1330 | * is specified | |
1331 | */ | |
1332 | qemu_add_machine_init_done_notifier(&muxes_realize_notify); | |
d654f34e AL |
1333 | } |
1334 | ||
1335 | type_init(register_types); |