]>
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 | */ | |
24 | #include "qemu-common.h" | |
83c9089e | 25 | #include "monitor/monitor.h" |
9c17d615 | 26 | #include "sysemu/sysemu.h" |
1de7afc9 | 27 | #include "qemu/timer.h" |
dccfcd0e | 28 | #include "sysemu/char.h" |
cf3ebac7 | 29 | #include "hw/usb.h" |
c5a415a0 | 30 | #include "qmp-commands.h" |
6f97dba0 AL |
31 | |
32 | #include <unistd.h> | |
33 | #include <fcntl.h> | |
6f97dba0 AL |
34 | #include <time.h> |
35 | #include <errno.h> | |
36 | #include <sys/time.h> | |
37 | #include <zlib.h> | |
38 | ||
39 | #ifndef _WIN32 | |
40 | #include <sys/times.h> | |
41 | #include <sys/wait.h> | |
42 | #include <termios.h> | |
43 | #include <sys/mman.h> | |
44 | #include <sys/ioctl.h> | |
24646c7e | 45 | #include <sys/resource.h> |
6f97dba0 AL |
46 | #include <sys/socket.h> |
47 | #include <netinet/in.h> | |
24646c7e | 48 | #include <net/if.h> |
24646c7e | 49 | #include <arpa/inet.h> |
6f97dba0 AL |
50 | #include <dirent.h> |
51 | #include <netdb.h> | |
52 | #include <sys/select.h> | |
71e72a19 | 53 | #ifdef CONFIG_BSD |
6f97dba0 | 54 | #include <sys/stat.h> |
3294ce18 MT |
55 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
56 | #include <dev/ppbus/ppi.h> | |
57 | #include <dev/ppbus/ppbconf.h> | |
c5e97233 | 58 | #elif defined(__DragonFly__) |
c5e97233 BS |
59 | #include <dev/misc/ppi/ppi.h> |
60 | #include <bus/ppbus/ppbconf.h> | |
6f97dba0 | 61 | #endif |
bbe813a2 | 62 | #else |
6f97dba0 | 63 | #ifdef __linux__ |
6f97dba0 AL |
64 | #include <linux/ppdev.h> |
65 | #include <linux/parport.h> | |
66 | #endif | |
67 | #ifdef __sun__ | |
68 | #include <sys/stat.h> | |
69 | #include <sys/ethernet.h> | |
70 | #include <sys/sockio.h> | |
71 | #include <netinet/arp.h> | |
72 | #include <netinet/in.h> | |
73 | #include <netinet/in_systm.h> | |
74 | #include <netinet/ip.h> | |
75 | #include <netinet/ip_icmp.h> // must come after ip.h | |
76 | #include <netinet/udp.h> | |
77 | #include <netinet/tcp.h> | |
6f97dba0 AL |
78 | #endif |
79 | #endif | |
80 | #endif | |
81 | ||
1de7afc9 | 82 | #include "qemu/sockets.h" |
cbcc6336 | 83 | #include "ui/qemu-spice.h" |
6f97dba0 | 84 | |
9bd7854e | 85 | #define READ_BUF_LEN 4096 |
7b0bfdf5 | 86 | #define READ_RETRIES 10 |
9bd7854e | 87 | |
6f97dba0 AL |
88 | /***********************************************************/ |
89 | /* character device */ | |
90 | ||
72cf2d4f BS |
91 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
92 | QTAILQ_HEAD_INITIALIZER(chardevs); | |
2970a6c9 | 93 | |
db39fcf1 PB |
94 | CharDriverState *qemu_chr_alloc(void) |
95 | { | |
96 | CharDriverState *chr = g_malloc0(sizeof(CharDriverState)); | |
f3db17b9 | 97 | qemu_mutex_init(&chr->chr_write_lock); |
db39fcf1 PB |
98 | return chr; |
99 | } | |
100 | ||
a425d23f | 101 | void qemu_chr_be_event(CharDriverState *s, int event) |
6f97dba0 | 102 | { |
73cdf3f2 AG |
103 | /* Keep track if the char device is open */ |
104 | switch (event) { | |
105 | case CHR_EVENT_OPENED: | |
16665b94 | 106 | s->be_open = 1; |
73cdf3f2 AG |
107 | break; |
108 | case CHR_EVENT_CLOSED: | |
16665b94 | 109 | s->be_open = 0; |
73cdf3f2 AG |
110 | break; |
111 | } | |
112 | ||
6f97dba0 AL |
113 | if (!s->chr_event) |
114 | return; | |
115 | s->chr_event(s->handler_opaque, event); | |
116 | } | |
117 | ||
fee204fd | 118 | void qemu_chr_be_generic_open(CharDriverState *s) |
6f97dba0 | 119 | { |
bd5c51ee | 120 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
6f97dba0 AL |
121 | } |
122 | ||
2cc6e0a1 | 123 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
6f97dba0 | 124 | { |
9005b2a7 PB |
125 | int ret; |
126 | ||
127 | qemu_mutex_lock(&s->chr_write_lock); | |
128 | ret = s->chr_write(s, buf, len); | |
129 | qemu_mutex_unlock(&s->chr_write_lock); | |
130 | return ret; | |
6f97dba0 AL |
131 | } |
132 | ||
cd18720a AL |
133 | int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) |
134 | { | |
135 | int offset = 0; | |
09313047 | 136 | int res = 0; |
cd18720a | 137 | |
9005b2a7 | 138 | qemu_mutex_lock(&s->chr_write_lock); |
cd18720a AL |
139 | while (offset < len) { |
140 | do { | |
141 | res = s->chr_write(s, buf + offset, len - offset); | |
142 | if (res == -1 && errno == EAGAIN) { | |
143 | g_usleep(100); | |
144 | } | |
145 | } while (res == -1 && errno == EAGAIN); | |
146 | ||
9005b2a7 | 147 | if (res <= 0) { |
cd18720a AL |
148 | break; |
149 | } | |
150 | ||
cd18720a AL |
151 | offset += res; |
152 | } | |
9005b2a7 | 153 | qemu_mutex_unlock(&s->chr_write_lock); |
cd18720a | 154 | |
9005b2a7 PB |
155 | if (res < 0) { |
156 | return res; | |
157 | } | |
cd18720a AL |
158 | return offset; |
159 | } | |
160 | ||
7b0bfdf5 NN |
161 | int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len) |
162 | { | |
163 | int offset = 0, counter = 10; | |
164 | int res; | |
165 | ||
166 | if (!s->chr_sync_read) { | |
167 | return 0; | |
168 | } | |
169 | ||
170 | while (offset < len) { | |
171 | do { | |
172 | res = s->chr_sync_read(s, buf + offset, len - offset); | |
173 | if (res == -1 && errno == EAGAIN) { | |
174 | g_usleep(100); | |
175 | } | |
176 | } while (res == -1 && errno == EAGAIN); | |
177 | ||
178 | if (res == 0) { | |
179 | break; | |
180 | } | |
181 | ||
182 | if (res < 0) { | |
183 | return res; | |
184 | } | |
185 | ||
186 | offset += res; | |
187 | ||
188 | if (!counter--) { | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | return offset; | |
194 | } | |
195 | ||
41084f1b | 196 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
6f97dba0 AL |
197 | { |
198 | if (!s->chr_ioctl) | |
199 | return -ENOTSUP; | |
200 | return s->chr_ioctl(s, cmd, arg); | |
201 | } | |
202 | ||
909cda12 | 203 | int qemu_chr_be_can_write(CharDriverState *s) |
6f97dba0 AL |
204 | { |
205 | if (!s->chr_can_read) | |
206 | return 0; | |
207 | return s->chr_can_read(s->handler_opaque); | |
208 | } | |
209 | ||
fa5efccb | 210 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
6f97dba0 | 211 | { |
ac310734 SW |
212 | if (s->chr_read) { |
213 | s->chr_read(s->handler_opaque, buf, len); | |
214 | } | |
6f97dba0 AL |
215 | } |
216 | ||
74c0d6f0 | 217 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
7d174059 | 218 | { |
c76bf6bb | 219 | int fd; |
4f858614 | 220 | return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1; |
c76bf6bb NN |
221 | } |
222 | ||
223 | int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len) | |
224 | { | |
225 | return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1; | |
7d174059 MM |
226 | } |
227 | ||
d39aac7a NN |
228 | int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num) |
229 | { | |
230 | return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1; | |
231 | } | |
232 | ||
13661089 DB |
233 | int qemu_chr_add_client(CharDriverState *s, int fd) |
234 | { | |
235 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; | |
236 | } | |
237 | ||
6f97dba0 AL |
238 | void qemu_chr_accept_input(CharDriverState *s) |
239 | { | |
240 | if (s->chr_accept_input) | |
241 | s->chr_accept_input(s); | |
98c8ee1d | 242 | qemu_notify_event(); |
6f97dba0 AL |
243 | } |
244 | ||
e7e71b0e | 245 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
6f97dba0 | 246 | { |
9bd7854e | 247 | char buf[READ_BUF_LEN]; |
6f97dba0 AL |
248 | va_list ap; |
249 | va_start(ap, fmt); | |
250 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
2cc6e0a1 | 251 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
6f97dba0 AL |
252 | va_end(ap); |
253 | } | |
254 | ||
386a5a1e AS |
255 | static void remove_fd_in_watch(CharDriverState *chr); |
256 | ||
6f97dba0 | 257 | void qemu_chr_add_handlers(CharDriverState *s, |
7b27a769 | 258 | IOCanReadHandler *fd_can_read, |
6f97dba0 AL |
259 | IOReadHandler *fd_read, |
260 | IOEventHandler *fd_event, | |
261 | void *opaque) | |
262 | { | |
19083228 HG |
263 | int fe_open; |
264 | ||
da7d998b | 265 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
19083228 | 266 | fe_open = 0; |
386a5a1e | 267 | remove_fd_in_watch(s); |
19083228 HG |
268 | } else { |
269 | fe_open = 1; | |
2d6c1ef4 | 270 | } |
6f97dba0 AL |
271 | s->chr_can_read = fd_can_read; |
272 | s->chr_read = fd_read; | |
273 | s->chr_event = fd_event; | |
274 | s->handler_opaque = opaque; | |
ac1b84dd | 275 | if (fe_open && s->chr_update_read_handler) |
6f97dba0 | 276 | s->chr_update_read_handler(s); |
73cdf3f2 | 277 | |
19083228 | 278 | if (!s->explicit_fe_open) { |
8e25daa8 | 279 | qemu_chr_fe_set_open(s, fe_open); |
19083228 HG |
280 | } |
281 | ||
73cdf3f2 AG |
282 | /* We're connecting to an already opened device, so let's make sure we |
283 | also get the open event */ | |
a59bcd31 | 284 | if (fe_open && s->be_open) { |
fee204fd | 285 | qemu_chr_be_generic_open(s); |
73cdf3f2 | 286 | } |
6f97dba0 AL |
287 | } |
288 | ||
289 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
290 | { | |
291 | return len; | |
292 | } | |
293 | ||
80dca9e6 | 294 | static CharDriverState *qemu_chr_open_null(void) |
6f97dba0 AL |
295 | { |
296 | CharDriverState *chr; | |
297 | ||
db39fcf1 | 298 | chr = qemu_chr_alloc(); |
6f97dba0 | 299 | chr->chr_write = null_chr_write; |
bd5c51ee | 300 | chr->explicit_be_open = true; |
1f51470d | 301 | return chr; |
6f97dba0 AL |
302 | } |
303 | ||
304 | /* MUX driver for serial I/O splitting */ | |
6f97dba0 AL |
305 | #define MAX_MUX 4 |
306 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ | |
307 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) | |
308 | typedef struct { | |
7b27a769 | 309 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
6f97dba0 AL |
310 | IOReadHandler *chr_read[MAX_MUX]; |
311 | IOEventHandler *chr_event[MAX_MUX]; | |
312 | void *ext_opaque[MAX_MUX]; | |
313 | CharDriverState *drv; | |
799f1f23 | 314 | int focus; |
6f97dba0 AL |
315 | int mux_cnt; |
316 | int term_got_escape; | |
317 | int max_size; | |
a80bf99f AL |
318 | /* Intermediate input buffer allows to catch escape sequences even if the |
319 | currently active device is not accepting any input - but only until it | |
320 | is full as well. */ | |
321 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; | |
322 | int prod[MAX_MUX]; | |
323 | int cons[MAX_MUX]; | |
2d22959d | 324 | int timestamps; |
9005b2a7 PB |
325 | |
326 | /* Protected by the CharDriverState chr_write_lock. */ | |
4ab312f7 | 327 | int linestart; |
2d22959d | 328 | int64_t timestamps_start; |
6f97dba0 AL |
329 | } MuxDriver; |
330 | ||
331 | ||
9005b2a7 | 332 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
333 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
334 | { | |
335 | MuxDriver *d = chr->opaque; | |
336 | int ret; | |
2d22959d | 337 | if (!d->timestamps) { |
6975b713 | 338 | ret = qemu_chr_fe_write(d->drv, buf, len); |
6f97dba0 AL |
339 | } else { |
340 | int i; | |
341 | ||
342 | ret = 0; | |
4ab312f7 JK |
343 | for (i = 0; i < len; i++) { |
344 | if (d->linestart) { | |
6f97dba0 AL |
345 | char buf1[64]; |
346 | int64_t ti; | |
347 | int secs; | |
348 | ||
bc72ad67 | 349 | ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
2d22959d JK |
350 | if (d->timestamps_start == -1) |
351 | d->timestamps_start = ti; | |
352 | ti -= d->timestamps_start; | |
a4bb1db8 | 353 | secs = ti / 1000; |
6f97dba0 AL |
354 | snprintf(buf1, sizeof(buf1), |
355 | "[%02d:%02d:%02d.%03d] ", | |
356 | secs / 3600, | |
357 | (secs / 60) % 60, | |
358 | secs % 60, | |
a4bb1db8 | 359 | (int)(ti % 1000)); |
6975b713 | 360 | qemu_chr_fe_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
4ab312f7 JK |
361 | d->linestart = 0; |
362 | } | |
6975b713 | 363 | ret += qemu_chr_fe_write(d->drv, buf+i, 1); |
4ab312f7 JK |
364 | if (buf[i] == '\n') { |
365 | d->linestart = 1; | |
6f97dba0 AL |
366 | } |
367 | } | |
368 | } | |
369 | return ret; | |
370 | } | |
371 | ||
372 | static const char * const mux_help[] = { | |
373 | "% h print this help\n\r", | |
374 | "% x exit emulator\n\r", | |
375 | "% s save disk data back to file (if -snapshot)\n\r", | |
376 | "% t toggle console timestamps\n\r" | |
377 | "% b send break (magic sysrq)\n\r", | |
378 | "% c switch between console and monitor\n\r", | |
379 | "% % sends %\n\r", | |
380 | NULL | |
381 | }; | |
382 | ||
383 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ | |
384 | static void mux_print_help(CharDriverState *chr) | |
385 | { | |
386 | int i, j; | |
387 | char ebuf[15] = "Escape-Char"; | |
388 | char cbuf[50] = "\n\r"; | |
389 | ||
390 | if (term_escape_char > 0 && term_escape_char < 26) { | |
391 | snprintf(cbuf, sizeof(cbuf), "\n\r"); | |
392 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); | |
393 | } else { | |
394 | snprintf(cbuf, sizeof(cbuf), | |
395 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", | |
396 | term_escape_char); | |
397 | } | |
6975b713 | 398 | qemu_chr_fe_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
6f97dba0 AL |
399 | for (i = 0; mux_help[i] != NULL; i++) { |
400 | for (j=0; mux_help[i][j] != '\0'; j++) { | |
401 | if (mux_help[i][j] == '%') | |
6975b713 | 402 | qemu_chr_fe_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
6f97dba0 | 403 | else |
6975b713 | 404 | qemu_chr_fe_write(chr, (uint8_t *)&mux_help[i][j], 1); |
6f97dba0 AL |
405 | } |
406 | } | |
407 | } | |
408 | ||
2724b180 AL |
409 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
410 | { | |
411 | if (d->chr_event[mux_nr]) | |
412 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); | |
413 | } | |
414 | ||
6f97dba0 AL |
415 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
416 | { | |
417 | if (d->term_got_escape) { | |
418 | d->term_got_escape = 0; | |
419 | if (ch == term_escape_char) | |
420 | goto send_char; | |
421 | switch(ch) { | |
422 | case '?': | |
423 | case 'h': | |
424 | mux_print_help(chr); | |
425 | break; | |
426 | case 'x': | |
427 | { | |
428 | const char *term = "QEMU: Terminated\n\r"; | |
6975b713 | 429 | qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term)); |
6f97dba0 AL |
430 | exit(0); |
431 | break; | |
432 | } | |
433 | case 's': | |
6ab4b5ab | 434 | bdrv_commit_all(); |
6f97dba0 AL |
435 | break; |
436 | case 'b': | |
a425d23f | 437 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
438 | break; |
439 | case 'c': | |
440 | /* Switch to the next registered device */ | |
799f1f23 GH |
441 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
442 | d->focus++; | |
443 | if (d->focus >= d->mux_cnt) | |
444 | d->focus = 0; | |
445 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); | |
6f97dba0 | 446 | break; |
2d22959d JK |
447 | case 't': |
448 | d->timestamps = !d->timestamps; | |
449 | d->timestamps_start = -1; | |
4ab312f7 | 450 | d->linestart = 0; |
2d22959d | 451 | break; |
6f97dba0 AL |
452 | } |
453 | } else if (ch == term_escape_char) { | |
454 | d->term_got_escape = 1; | |
455 | } else { | |
456 | send_char: | |
457 | return 1; | |
458 | } | |
459 | return 0; | |
460 | } | |
461 | ||
462 | static void mux_chr_accept_input(CharDriverState *chr) | |
463 | { | |
6f97dba0 | 464 | MuxDriver *d = chr->opaque; |
799f1f23 | 465 | int m = d->focus; |
6f97dba0 | 466 | |
a80bf99f | 467 | while (d->prod[m] != d->cons[m] && |
6f97dba0 AL |
468 | d->chr_can_read[m] && |
469 | d->chr_can_read[m](d->ext_opaque[m])) { | |
470 | d->chr_read[m](d->ext_opaque[m], | |
a80bf99f | 471 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
6f97dba0 AL |
472 | } |
473 | } | |
474 | ||
475 | static int mux_chr_can_read(void *opaque) | |
476 | { | |
477 | CharDriverState *chr = opaque; | |
478 | MuxDriver *d = chr->opaque; | |
799f1f23 | 479 | int m = d->focus; |
6f97dba0 | 480 | |
a80bf99f | 481 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
6f97dba0 | 482 | return 1; |
a80bf99f AL |
483 | if (d->chr_can_read[m]) |
484 | return d->chr_can_read[m](d->ext_opaque[m]); | |
6f97dba0 AL |
485 | return 0; |
486 | } | |
487 | ||
488 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) | |
489 | { | |
490 | CharDriverState *chr = opaque; | |
491 | MuxDriver *d = chr->opaque; | |
799f1f23 | 492 | int m = d->focus; |
6f97dba0 AL |
493 | int i; |
494 | ||
495 | mux_chr_accept_input (opaque); | |
496 | ||
497 | for(i = 0; i < size; i++) | |
498 | if (mux_proc_byte(chr, d, buf[i])) { | |
a80bf99f | 499 | if (d->prod[m] == d->cons[m] && |
6f97dba0 AL |
500 | d->chr_can_read[m] && |
501 | d->chr_can_read[m](d->ext_opaque[m])) | |
502 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); | |
503 | else | |
a80bf99f | 504 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
6f97dba0 AL |
505 | } |
506 | } | |
507 | ||
508 | static void mux_chr_event(void *opaque, int event) | |
509 | { | |
510 | CharDriverState *chr = opaque; | |
511 | MuxDriver *d = chr->opaque; | |
512 | int i; | |
513 | ||
514 | /* Send the event to all registered listeners */ | |
515 | for (i = 0; i < d->mux_cnt; i++) | |
2724b180 | 516 | mux_chr_send_event(d, i, event); |
6f97dba0 AL |
517 | } |
518 | ||
519 | static void mux_chr_update_read_handler(CharDriverState *chr) | |
520 | { | |
521 | MuxDriver *d = chr->opaque; | |
522 | ||
523 | if (d->mux_cnt >= MAX_MUX) { | |
524 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); | |
525 | return; | |
526 | } | |
527 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; | |
528 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; | |
529 | d->chr_read[d->mux_cnt] = chr->chr_read; | |
530 | d->chr_event[d->mux_cnt] = chr->chr_event; | |
531 | /* Fix up the real driver with mux routines */ | |
532 | if (d->mux_cnt == 0) { | |
533 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, | |
534 | mux_chr_event, chr); | |
535 | } | |
799f1f23 GH |
536 | if (d->focus != -1) { |
537 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); | |
a7aec5da | 538 | } |
799f1f23 | 539 | d->focus = d->mux_cnt; |
6f97dba0 | 540 | d->mux_cnt++; |
799f1f23 | 541 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
6f97dba0 AL |
542 | } |
543 | ||
7b7ab18d MR |
544 | static bool muxes_realized; |
545 | ||
546 | /** | |
547 | * Called after processing of default and command-line-specified | |
548 | * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached | |
549 | * to a mux chardev. This is done here to ensure that | |
550 | * output/prompts/banners are only displayed for the FE that has | |
551 | * focus when initial command-line processing/machine init is | |
552 | * completed. | |
553 | * | |
554 | * After this point, any new FE attached to any new or existing | |
555 | * mux will receive CHR_EVENT_OPENED notifications for the BE | |
556 | * immediately. | |
557 | */ | |
558 | static void muxes_realize_done(Notifier *notifier, void *unused) | |
559 | { | |
560 | CharDriverState *chr; | |
561 | ||
562 | QTAILQ_FOREACH(chr, &chardevs, next) { | |
563 | if (chr->is_mux) { | |
564 | MuxDriver *d = chr->opaque; | |
565 | int i; | |
566 | ||
567 | /* send OPENED to all already-attached FEs */ | |
568 | for (i = 0; i < d->mux_cnt; i++) { | |
569 | mux_chr_send_event(d, i, CHR_EVENT_OPENED); | |
570 | } | |
571 | /* mark mux as OPENED so any new FEs will immediately receive | |
572 | * OPENED event | |
573 | */ | |
574 | qemu_chr_be_generic_open(chr); | |
575 | } | |
576 | } | |
577 | muxes_realized = true; | |
578 | } | |
579 | ||
580 | static Notifier muxes_realize_notify = { | |
581 | .notify = muxes_realize_done, | |
582 | }; | |
583 | ||
3f0838ab KB |
584 | static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond) |
585 | { | |
586 | MuxDriver *d = s->opaque; | |
587 | return d->drv->chr_add_watch(d->drv, cond); | |
588 | } | |
589 | ||
6f97dba0 AL |
590 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
591 | { | |
592 | CharDriverState *chr; | |
593 | MuxDriver *d; | |
594 | ||
db39fcf1 | 595 | chr = qemu_chr_alloc(); |
7267c094 | 596 | d = g_malloc0(sizeof(MuxDriver)); |
6f97dba0 AL |
597 | |
598 | chr->opaque = d; | |
599 | d->drv = drv; | |
799f1f23 | 600 | d->focus = -1; |
6f97dba0 AL |
601 | chr->chr_write = mux_chr_write; |
602 | chr->chr_update_read_handler = mux_chr_update_read_handler; | |
603 | chr->chr_accept_input = mux_chr_accept_input; | |
7c32c4fe | 604 | /* Frontend guest-open / -close notification is not support with muxes */ |
574b711a | 605 | chr->chr_set_fe_open = NULL; |
3f0838ab KB |
606 | if (drv->chr_add_watch) { |
607 | chr->chr_add_watch = mux_chr_add_watch; | |
608 | } | |
7b7ab18d MR |
609 | /* only default to opened state if we've realized the initial |
610 | * set of muxes | |
611 | */ | |
612 | chr->explicit_be_open = muxes_realized ? 0 : 1; | |
613 | chr->is_mux = 1; | |
73cdf3f2 | 614 | |
6f97dba0 AL |
615 | return chr; |
616 | } | |
617 | ||
618 | ||
619 | #ifdef _WIN32 | |
d247d25f | 620 | int send_all(int fd, const void *buf, int len1) |
6f97dba0 AL |
621 | { |
622 | int ret, len; | |
623 | ||
624 | len = len1; | |
625 | while (len > 0) { | |
626 | ret = send(fd, buf, len, 0); | |
627 | if (ret < 0) { | |
6f97dba0 AL |
628 | errno = WSAGetLastError(); |
629 | if (errno != WSAEWOULDBLOCK) { | |
630 | return -1; | |
631 | } | |
632 | } else if (ret == 0) { | |
633 | break; | |
634 | } else { | |
635 | buf += ret; | |
636 | len -= ret; | |
637 | } | |
638 | } | |
639 | return len1 - len; | |
640 | } | |
641 | ||
642 | #else | |
643 | ||
5fc9cfed | 644 | int send_all(int fd, const void *_buf, int len1) |
6f97dba0 AL |
645 | { |
646 | int ret, len; | |
5fc9cfed | 647 | const uint8_t *buf = _buf; |
6f97dba0 AL |
648 | |
649 | len = len1; | |
650 | while (len > 0) { | |
651 | ret = write(fd, buf, len); | |
652 | if (ret < 0) { | |
653 | if (errno != EINTR && errno != EAGAIN) | |
654 | return -1; | |
655 | } else if (ret == 0) { | |
656 | break; | |
657 | } else { | |
658 | buf += ret; | |
659 | len -= ret; | |
660 | } | |
661 | } | |
662 | return len1 - len; | |
663 | } | |
4549a8b7 SB |
664 | |
665 | int recv_all(int fd, void *_buf, int len1, bool single_read) | |
666 | { | |
667 | int ret, len; | |
668 | uint8_t *buf = _buf; | |
669 | ||
670 | len = len1; | |
671 | while ((len > 0) && (ret = read(fd, buf, len)) != 0) { | |
672 | if (ret < 0) { | |
673 | if (errno != EINTR && errno != EAGAIN) { | |
674 | return -1; | |
675 | } | |
676 | continue; | |
677 | } else { | |
678 | if (single_read) { | |
679 | return ret; | |
680 | } | |
681 | buf += ret; | |
682 | len -= ret; | |
683 | } | |
684 | } | |
685 | return len1 - len; | |
686 | } | |
687 | ||
6f97dba0 AL |
688 | #endif /* !_WIN32 */ |
689 | ||
96c63847 AL |
690 | typedef struct IOWatchPoll |
691 | { | |
d185c094 PB |
692 | GSource parent; |
693 | ||
1e885b25 | 694 | GIOChannel *channel; |
96c63847 | 695 | GSource *src; |
96c63847 AL |
696 | |
697 | IOCanReadHandler *fd_can_read; | |
1e885b25 | 698 | GSourceFunc fd_read; |
96c63847 | 699 | void *opaque; |
96c63847 AL |
700 | } IOWatchPoll; |
701 | ||
96c63847 AL |
702 | static IOWatchPoll *io_watch_poll_from_source(GSource *source) |
703 | { | |
d185c094 | 704 | return container_of(source, IOWatchPoll, parent); |
96c63847 AL |
705 | } |
706 | ||
707 | static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_) | |
708 | { | |
709 | IOWatchPoll *iwp = io_watch_poll_from_source(source); | |
d185c094 | 710 | bool now_active = iwp->fd_can_read(iwp->opaque) > 0; |
1e885b25 | 711 | bool was_active = iwp->src != NULL; |
d185c094 | 712 | if (was_active == now_active) { |
96c63847 AL |
713 | return FALSE; |
714 | } | |
715 | ||
d185c094 | 716 | if (now_active) { |
1e885b25 PB |
717 | iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP); |
718 | g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); | |
d185c094 PB |
719 | g_source_attach(iwp->src, NULL); |
720 | } else { | |
1e885b25 PB |
721 | g_source_destroy(iwp->src); |
722 | g_source_unref(iwp->src); | |
723 | iwp->src = NULL; | |
d185c094 PB |
724 | } |
725 | return FALSE; | |
96c63847 AL |
726 | } |
727 | ||
728 | static gboolean io_watch_poll_check(GSource *source) | |
729 | { | |
d185c094 | 730 | return FALSE; |
96c63847 AL |
731 | } |
732 | ||
733 | static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, | |
734 | gpointer user_data) | |
735 | { | |
d185c094 | 736 | abort(); |
96c63847 AL |
737 | } |
738 | ||
739 | static void io_watch_poll_finalize(GSource *source) | |
740 | { | |
2b316774 PB |
741 | /* Due to a glib bug, removing the last reference to a source |
742 | * inside a finalize callback causes recursive locking (and a | |
743 | * deadlock). This is not a problem inside other callbacks, | |
744 | * including dispatch callbacks, so we call io_remove_watch_poll | |
745 | * to remove this source. At this point, iwp->src must | |
746 | * be NULL, or we would leak it. | |
747 | * | |
748 | * This would be solved much more elegantly by child sources, | |
749 | * but we support older glib versions that do not have them. | |
750 | */ | |
96c63847 | 751 | IOWatchPoll *iwp = io_watch_poll_from_source(source); |
2b316774 | 752 | assert(iwp->src == NULL); |
96c63847 AL |
753 | } |
754 | ||
755 | static GSourceFuncs io_watch_poll_funcs = { | |
756 | .prepare = io_watch_poll_prepare, | |
757 | .check = io_watch_poll_check, | |
758 | .dispatch = io_watch_poll_dispatch, | |
759 | .finalize = io_watch_poll_finalize, | |
760 | }; | |
761 | ||
762 | /* Can only be used for read */ | |
763 | static guint io_add_watch_poll(GIOChannel *channel, | |
764 | IOCanReadHandler *fd_can_read, | |
765 | GIOFunc fd_read, | |
766 | gpointer user_data) | |
767 | { | |
768 | IOWatchPoll *iwp; | |
0ca5aa4f | 769 | int tag; |
96c63847 | 770 | |
d185c094 | 771 | iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll)); |
96c63847 AL |
772 | iwp->fd_can_read = fd_can_read; |
773 | iwp->opaque = user_data; | |
1e885b25 PB |
774 | iwp->channel = channel; |
775 | iwp->fd_read = (GSourceFunc) fd_read; | |
776 | iwp->src = NULL; | |
96c63847 | 777 | |
0ca5aa4f PB |
778 | tag = g_source_attach(&iwp->parent, NULL); |
779 | g_source_unref(&iwp->parent); | |
780 | return tag; | |
96c63847 AL |
781 | } |
782 | ||
2b316774 PB |
783 | static void io_remove_watch_poll(guint tag) |
784 | { | |
785 | GSource *source; | |
786 | IOWatchPoll *iwp; | |
787 | ||
788 | g_return_if_fail (tag > 0); | |
789 | ||
790 | source = g_main_context_find_source_by_id(NULL, tag); | |
791 | g_return_if_fail (source != NULL); | |
792 | ||
793 | iwp = io_watch_poll_from_source(source); | |
794 | if (iwp->src) { | |
795 | g_source_destroy(iwp->src); | |
796 | g_source_unref(iwp->src); | |
797 | iwp->src = NULL; | |
798 | } | |
799 | g_source_destroy(&iwp->parent); | |
800 | } | |
801 | ||
26da70c7 AS |
802 | static void remove_fd_in_watch(CharDriverState *chr) |
803 | { | |
804 | if (chr->fd_in_tag) { | |
805 | io_remove_watch_poll(chr->fd_in_tag); | |
806 | chr->fd_in_tag = 0; | |
807 | } | |
808 | } | |
809 | ||
44ab9ed4 | 810 | #ifndef _WIN32 |
96c63847 AL |
811 | static GIOChannel *io_channel_from_fd(int fd) |
812 | { | |
813 | GIOChannel *chan; | |
814 | ||
815 | if (fd == -1) { | |
816 | return NULL; | |
817 | } | |
818 | ||
819 | chan = g_io_channel_unix_new(fd); | |
820 | ||
821 | g_io_channel_set_encoding(chan, NULL, NULL); | |
822 | g_io_channel_set_buffered(chan, FALSE); | |
823 | ||
824 | return chan; | |
825 | } | |
44ab9ed4 | 826 | #endif |
96c63847 | 827 | |
76a9644b AL |
828 | static GIOChannel *io_channel_from_socket(int fd) |
829 | { | |
830 | GIOChannel *chan; | |
831 | ||
832 | if (fd == -1) { | |
833 | return NULL; | |
834 | } | |
835 | ||
836 | #ifdef _WIN32 | |
837 | chan = g_io_channel_win32_new_socket(fd); | |
838 | #else | |
839 | chan = g_io_channel_unix_new(fd); | |
840 | #endif | |
841 | ||
842 | g_io_channel_set_encoding(chan, NULL, NULL); | |
843 | g_io_channel_set_buffered(chan, FALSE); | |
844 | ||
845 | return chan; | |
846 | } | |
847 | ||
684a096e | 848 | static int io_channel_send(GIOChannel *fd, const void *buf, size_t len) |
96c63847 | 849 | { |
ac8c26f6 LE |
850 | size_t offset = 0; |
851 | GIOStatus status = G_IO_STATUS_NORMAL; | |
96c63847 | 852 | |
ac8c26f6 LE |
853 | while (offset < len && status == G_IO_STATUS_NORMAL) { |
854 | gsize bytes_written = 0; | |
684a096e AL |
855 | |
856 | status = g_io_channel_write_chars(fd, buf + offset, len - offset, | |
96c63847 | 857 | &bytes_written, NULL); |
684a096e | 858 | offset += bytes_written; |
96c63847 | 859 | } |
684a096e | 860 | |
ac8c26f6 LE |
861 | if (offset > 0) { |
862 | return offset; | |
863 | } | |
864 | switch (status) { | |
865 | case G_IO_STATUS_NORMAL: | |
866 | g_assert(len == 0); | |
867 | return 0; | |
868 | case G_IO_STATUS_AGAIN: | |
869 | errno = EAGAIN; | |
870 | return -1; | |
871 | default: | |
872 | break; | |
873 | } | |
874 | errno = EINVAL; | |
875 | return -1; | |
96c63847 | 876 | } |
96c63847 | 877 | |
44ab9ed4 BS |
878 | #ifndef _WIN32 |
879 | ||
a29753f8 AL |
880 | typedef struct FDCharDriver { |
881 | CharDriverState *chr; | |
882 | GIOChannel *fd_in, *fd_out; | |
6f97dba0 | 883 | int max_size; |
a29753f8 | 884 | QTAILQ_ENTRY(FDCharDriver) node; |
6f97dba0 AL |
885 | } FDCharDriver; |
886 | ||
9005b2a7 | 887 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
888 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
889 | { | |
890 | FDCharDriver *s = chr->opaque; | |
a29753f8 | 891 | |
684a096e | 892 | return io_channel_send(s->fd_out, buf, len); |
6f97dba0 AL |
893 | } |
894 | ||
a29753f8 | 895 | static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
896 | { |
897 | CharDriverState *chr = opaque; | |
898 | FDCharDriver *s = chr->opaque; | |
a29753f8 | 899 | int len; |
9bd7854e | 900 | uint8_t buf[READ_BUF_LEN]; |
a29753f8 AL |
901 | GIOStatus status; |
902 | gsize bytes_read; | |
6f97dba0 AL |
903 | |
904 | len = sizeof(buf); | |
a29753f8 | 905 | if (len > s->max_size) { |
6f97dba0 | 906 | len = s->max_size; |
a29753f8 AL |
907 | } |
908 | if (len == 0) { | |
cdbf6e16 | 909 | return TRUE; |
a29753f8 AL |
910 | } |
911 | ||
912 | status = g_io_channel_read_chars(chan, (gchar *)buf, | |
913 | len, &bytes_read, NULL); | |
914 | if (status == G_IO_STATUS_EOF) { | |
26da70c7 | 915 | remove_fd_in_watch(chr); |
a425d23f | 916 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
a29753f8 | 917 | return FALSE; |
6f97dba0 | 918 | } |
a29753f8 AL |
919 | if (status == G_IO_STATUS_NORMAL) { |
920 | qemu_chr_be_write(chr, buf, bytes_read); | |
6f97dba0 | 921 | } |
a29753f8 AL |
922 | |
923 | return TRUE; | |
924 | } | |
925 | ||
926 | static int fd_chr_read_poll(void *opaque) | |
927 | { | |
928 | CharDriverState *chr = opaque; | |
929 | FDCharDriver *s = chr->opaque; | |
930 | ||
931 | s->max_size = qemu_chr_be_can_write(chr); | |
932 | return s->max_size; | |
6f97dba0 AL |
933 | } |
934 | ||
23673ca7 AL |
935 | static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
936 | { | |
937 | FDCharDriver *s = chr->opaque; | |
938 | return g_io_create_watch(s->fd_out, cond); | |
939 | } | |
940 | ||
6f97dba0 AL |
941 | static void fd_chr_update_read_handler(CharDriverState *chr) |
942 | { | |
943 | FDCharDriver *s = chr->opaque; | |
944 | ||
26da70c7 | 945 | remove_fd_in_watch(chr); |
a29753f8 | 946 | if (s->fd_in) { |
7ba9addc AS |
947 | chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, |
948 | fd_chr_read, chr); | |
6f97dba0 AL |
949 | } |
950 | } | |
951 | ||
952 | static void fd_chr_close(struct CharDriverState *chr) | |
953 | { | |
954 | FDCharDriver *s = chr->opaque; | |
955 | ||
26da70c7 | 956 | remove_fd_in_watch(chr); |
a29753f8 AL |
957 | if (s->fd_in) { |
958 | g_io_channel_unref(s->fd_in); | |
959 | } | |
960 | if (s->fd_out) { | |
961 | g_io_channel_unref(s->fd_out); | |
6f97dba0 AL |
962 | } |
963 | ||
7267c094 | 964 | g_free(s); |
a425d23f | 965 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
966 | } |
967 | ||
968 | /* open a character device to a unix fd */ | |
969 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) | |
970 | { | |
971 | CharDriverState *chr; | |
972 | FDCharDriver *s; | |
973 | ||
db39fcf1 | 974 | chr = qemu_chr_alloc(); |
7267c094 | 975 | s = g_malloc0(sizeof(FDCharDriver)); |
a29753f8 AL |
976 | s->fd_in = io_channel_from_fd(fd_in); |
977 | s->fd_out = io_channel_from_fd(fd_out); | |
4ff12bdb | 978 | qemu_set_nonblock(fd_out); |
a29753f8 | 979 | s->chr = chr; |
6f97dba0 | 980 | chr->opaque = s; |
23673ca7 | 981 | chr->chr_add_watch = fd_chr_add_watch; |
6f97dba0 AL |
982 | chr->chr_write = fd_chr_write; |
983 | chr->chr_update_read_handler = fd_chr_update_read_handler; | |
984 | chr->chr_close = fd_chr_close; | |
985 | ||
6f97dba0 AL |
986 | return chr; |
987 | } | |
988 | ||
548cbb36 | 989 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
6f97dba0 AL |
990 | { |
991 | int fd_in, fd_out; | |
992 | char filename_in[256], filename_out[256]; | |
548cbb36 | 993 | const char *filename = opts->device; |
7d31544f GH |
994 | |
995 | if (filename == NULL) { | |
996 | fprintf(stderr, "chardev: pipe: no filename given\n"); | |
1f51470d | 997 | return NULL; |
7d31544f | 998 | } |
6f97dba0 AL |
999 | |
1000 | snprintf(filename_in, 256, "%s.in", filename); | |
1001 | snprintf(filename_out, 256, "%s.out", filename); | |
40ff6d7e KW |
1002 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
1003 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); | |
6f97dba0 AL |
1004 | if (fd_in < 0 || fd_out < 0) { |
1005 | if (fd_in >= 0) | |
1006 | close(fd_in); | |
1007 | if (fd_out >= 0) | |
1008 | close(fd_out); | |
b181e047 | 1009 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
a89dd6c3 | 1010 | if (fd_in < 0) { |
1f51470d | 1011 | return NULL; |
a89dd6c3 | 1012 | } |
6f97dba0 | 1013 | } |
1f51470d | 1014 | return qemu_chr_open_fd(fd_in, fd_out); |
6f97dba0 AL |
1015 | } |
1016 | ||
6f97dba0 AL |
1017 | /* init terminal so that we can grab keys */ |
1018 | static struct termios oldtty; | |
1019 | static int old_fd0_flags; | |
bb002513 | 1020 | static bool stdio_allow_signal; |
6f97dba0 AL |
1021 | |
1022 | static void term_exit(void) | |
1023 | { | |
1024 | tcsetattr (0, TCSANOW, &oldtty); | |
1025 | fcntl(0, F_SETFL, old_fd0_flags); | |
1026 | } | |
1027 | ||
bb002513 | 1028 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
6f97dba0 AL |
1029 | { |
1030 | struct termios tty; | |
1031 | ||
0369364b | 1032 | tty = oldtty; |
bb002513 PB |
1033 | if (!echo) { |
1034 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
6f97dba0 | 1035 | |INLCR|IGNCR|ICRNL|IXON); |
bb002513 PB |
1036 | tty.c_oflag |= OPOST; |
1037 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
1038 | tty.c_cflag &= ~(CSIZE|PARENB); | |
1039 | tty.c_cflag |= CS8; | |
1040 | tty.c_cc[VMIN] = 1; | |
1041 | tty.c_cc[VTIME] = 0; | |
1042 | } | |
bb002513 | 1043 | if (!stdio_allow_signal) |
6f97dba0 | 1044 | tty.c_lflag &= ~ISIG; |
6f97dba0 AL |
1045 | |
1046 | tcsetattr (0, TCSANOW, &tty); | |
6f97dba0 AL |
1047 | } |
1048 | ||
1049 | static void qemu_chr_close_stdio(struct CharDriverState *chr) | |
1050 | { | |
1051 | term_exit(); | |
6f97dba0 AL |
1052 | fd_chr_close(chr); |
1053 | } | |
1054 | ||
7c358031 | 1055 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
6f97dba0 AL |
1056 | { |
1057 | CharDriverState *chr; | |
1058 | ||
ab51b1d5 MT |
1059 | if (is_daemonized()) { |
1060 | error_report("cannot use stdio with -daemonize"); | |
1061 | return NULL; | |
1062 | } | |
ed7a1540 AL |
1063 | old_fd0_flags = fcntl(0, F_GETFL); |
1064 | tcgetattr (0, &oldtty); | |
4ff12bdb | 1065 | qemu_set_nonblock(0); |
ed7a1540 | 1066 | atexit(term_exit); |
0369364b | 1067 | |
6f97dba0 AL |
1068 | chr = qemu_chr_open_fd(0, 1); |
1069 | chr->chr_close = qemu_chr_close_stdio; | |
bb002513 | 1070 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
7c358031 GH |
1071 | if (opts->has_signal) { |
1072 | stdio_allow_signal = opts->signal; | |
1073 | } | |
15f31519 | 1074 | qemu_chr_fe_set_echo(chr, false); |
6f97dba0 | 1075 | |
1f51470d | 1076 | return chr; |
6f97dba0 AL |
1077 | } |
1078 | ||
6f97dba0 | 1079 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
a167ba50 AJ |
1080 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
1081 | || defined(__GLIBC__) | |
6f97dba0 | 1082 | |
e551498e GH |
1083 | #define HAVE_CHARDEV_TTY 1 |
1084 | ||
6f97dba0 | 1085 | typedef struct { |
093d3a20 | 1086 | GIOChannel *fd; |
6f97dba0 | 1087 | int read_bytes; |
9005b2a7 PB |
1088 | |
1089 | /* Protected by the CharDriverState chr_write_lock. */ | |
1090 | int connected; | |
8aa33caf | 1091 | guint timer_tag; |
7b3621f4 | 1092 | guint open_tag; |
6f97dba0 AL |
1093 | } PtyCharDriver; |
1094 | ||
9005b2a7 | 1095 | static void pty_chr_update_read_handler_locked(CharDriverState *chr); |
6f97dba0 AL |
1096 | static void pty_chr_state(CharDriverState *chr, int connected); |
1097 | ||
8aa33caf AL |
1098 | static gboolean pty_chr_timer(gpointer opaque) |
1099 | { | |
1100 | struct CharDriverState *chr = opaque; | |
1101 | PtyCharDriver *s = chr->opaque; | |
1102 | ||
9005b2a7 | 1103 | qemu_mutex_lock(&chr->chr_write_lock); |
79f20075 | 1104 | s->timer_tag = 0; |
7b3621f4 | 1105 | s->open_tag = 0; |
b0d768c3 GH |
1106 | if (!s->connected) { |
1107 | /* Next poll ... */ | |
9005b2a7 | 1108 | pty_chr_update_read_handler_locked(chr); |
b0d768c3 | 1109 | } |
9005b2a7 | 1110 | qemu_mutex_unlock(&chr->chr_write_lock); |
8aa33caf AL |
1111 | return FALSE; |
1112 | } | |
1113 | ||
9005b2a7 | 1114 | /* Called with chr_write_lock held. */ |
8aa33caf AL |
1115 | static void pty_chr_rearm_timer(CharDriverState *chr, int ms) |
1116 | { | |
1117 | PtyCharDriver *s = chr->opaque; | |
1118 | ||
1119 | if (s->timer_tag) { | |
1120 | g_source_remove(s->timer_tag); | |
1121 | s->timer_tag = 0; | |
1122 | } | |
1123 | ||
1124 | if (ms == 1000) { | |
1125 | s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); | |
1126 | } else { | |
1127 | s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); | |
1128 | } | |
1129 | } | |
1130 | ||
9005b2a7 PB |
1131 | /* Called with chr_write_lock held. */ |
1132 | static void pty_chr_update_read_handler_locked(CharDriverState *chr) | |
1bb7fe72 PB |
1133 | { |
1134 | PtyCharDriver *s = chr->opaque; | |
1135 | GPollFD pfd; | |
1136 | ||
1137 | pfd.fd = g_io_channel_unix_get_fd(s->fd); | |
1138 | pfd.events = G_IO_OUT; | |
1139 | pfd.revents = 0; | |
1140 | g_poll(&pfd, 1, 0); | |
1141 | if (pfd.revents & G_IO_HUP) { | |
1142 | pty_chr_state(chr, 0); | |
1143 | } else { | |
1144 | pty_chr_state(chr, 1); | |
1145 | } | |
1146 | } | |
1147 | ||
9005b2a7 PB |
1148 | static void pty_chr_update_read_handler(CharDriverState *chr) |
1149 | { | |
1150 | qemu_mutex_lock(&chr->chr_write_lock); | |
1151 | pty_chr_update_read_handler_locked(chr); | |
1152 | qemu_mutex_unlock(&chr->chr_write_lock); | |
1153 | } | |
1154 | ||
1155 | /* Called with chr_write_lock held. */ | |
6f97dba0 AL |
1156 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1157 | { | |
1158 | PtyCharDriver *s = chr->opaque; | |
1159 | ||
1160 | if (!s->connected) { | |
1161 | /* guest sends data, check for (re-)connect */ | |
9005b2a7 | 1162 | pty_chr_update_read_handler_locked(chr); |
cf7330c7 ST |
1163 | if (!s->connected) { |
1164 | return 0; | |
1165 | } | |
6f97dba0 | 1166 | } |
684a096e | 1167 | return io_channel_send(s->fd, buf, len); |
6f97dba0 AL |
1168 | } |
1169 | ||
e6a87ed8 AL |
1170 | static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
1171 | { | |
1172 | PtyCharDriver *s = chr->opaque; | |
62c339c5 PB |
1173 | if (!s->connected) { |
1174 | return NULL; | |
1175 | } | |
e6a87ed8 AL |
1176 | return g_io_create_watch(s->fd, cond); |
1177 | } | |
1178 | ||
6f97dba0 AL |
1179 | static int pty_chr_read_poll(void *opaque) |
1180 | { | |
1181 | CharDriverState *chr = opaque; | |
1182 | PtyCharDriver *s = chr->opaque; | |
1183 | ||
909cda12 | 1184 | s->read_bytes = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1185 | return s->read_bytes; |
1186 | } | |
1187 | ||
093d3a20 | 1188 | static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
1189 | { |
1190 | CharDriverState *chr = opaque; | |
1191 | PtyCharDriver *s = chr->opaque; | |
093d3a20 | 1192 | gsize size, len; |
9bd7854e | 1193 | uint8_t buf[READ_BUF_LEN]; |
093d3a20 | 1194 | GIOStatus status; |
6f97dba0 AL |
1195 | |
1196 | len = sizeof(buf); | |
1197 | if (len > s->read_bytes) | |
1198 | len = s->read_bytes; | |
cdbf6e16 PB |
1199 | if (len == 0) { |
1200 | return TRUE; | |
1201 | } | |
093d3a20 AL |
1202 | status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL); |
1203 | if (status != G_IO_STATUS_NORMAL) { | |
6f97dba0 | 1204 | pty_chr_state(chr, 0); |
093d3a20 AL |
1205 | return FALSE; |
1206 | } else { | |
6f97dba0 | 1207 | pty_chr_state(chr, 1); |
fa5efccb | 1208 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 | 1209 | } |
093d3a20 | 1210 | return TRUE; |
6f97dba0 AL |
1211 | } |
1212 | ||
7b3621f4 PB |
1213 | static gboolean qemu_chr_be_generic_open_func(gpointer opaque) |
1214 | { | |
1215 | CharDriverState *chr = opaque; | |
1216 | PtyCharDriver *s = chr->opaque; | |
1217 | ||
1218 | s->open_tag = 0; | |
1219 | qemu_chr_be_generic_open(chr); | |
1220 | return FALSE; | |
1221 | } | |
1222 | ||
9005b2a7 | 1223 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
1224 | static void pty_chr_state(CharDriverState *chr, int connected) |
1225 | { | |
1226 | PtyCharDriver *s = chr->opaque; | |
1227 | ||
1228 | if (!connected) { | |
7b3621f4 PB |
1229 | if (s->open_tag) { |
1230 | g_source_remove(s->open_tag); | |
1231 | s->open_tag = 0; | |
1232 | } | |
26da70c7 | 1233 | remove_fd_in_watch(chr); |
6f97dba0 | 1234 | s->connected = 0; |
6f97dba0 AL |
1235 | /* (re-)connect poll interval for idle guests: once per second. |
1236 | * We check more frequently in case the guests sends data to | |
1237 | * the virtual device linked to our pty. */ | |
8aa33caf | 1238 | pty_chr_rearm_timer(chr, 1000); |
6f97dba0 | 1239 | } else { |
85a67692 PB |
1240 | if (s->timer_tag) { |
1241 | g_source_remove(s->timer_tag); | |
1242 | s->timer_tag = 0; | |
1243 | } | |
1244 | if (!s->connected) { | |
7b3621f4 | 1245 | g_assert(s->open_tag == 0); |
85a67692 | 1246 | s->connected = 1; |
7b3621f4 | 1247 | s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr); |
ac1b84dd GH |
1248 | } |
1249 | if (!chr->fd_in_tag) { | |
7ba9addc AS |
1250 | chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, |
1251 | pty_chr_read, chr); | |
85a67692 | 1252 | } |
6f97dba0 AL |
1253 | } |
1254 | } | |
1255 | ||
6f97dba0 AL |
1256 | static void pty_chr_close(struct CharDriverState *chr) |
1257 | { | |
1258 | PtyCharDriver *s = chr->opaque; | |
093d3a20 | 1259 | int fd; |
6f97dba0 | 1260 | |
7b3621f4 PB |
1261 | qemu_mutex_lock(&chr->chr_write_lock); |
1262 | pty_chr_state(chr, 0); | |
093d3a20 AL |
1263 | fd = g_io_channel_unix_get_fd(s->fd); |
1264 | g_io_channel_unref(s->fd); | |
1265 | close(fd); | |
8aa33caf AL |
1266 | if (s->timer_tag) { |
1267 | g_source_remove(s->timer_tag); | |
910b6368 | 1268 | s->timer_tag = 0; |
8aa33caf | 1269 | } |
7b3621f4 | 1270 | qemu_mutex_unlock(&chr->chr_write_lock); |
7267c094 | 1271 | g_free(s); |
a425d23f | 1272 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1273 | } |
1274 | ||
e68c5958 GH |
1275 | static CharDriverState *qemu_chr_open_pty(const char *id, |
1276 | ChardevReturn *ret) | |
6f97dba0 AL |
1277 | { |
1278 | CharDriverState *chr; | |
1279 | PtyCharDriver *s; | |
e68c5958 | 1280 | int master_fd, slave_fd; |
6f97dba0 | 1281 | char pty_name[PATH_MAX]; |
6f97dba0 | 1282 | |
4efeabbb MT |
1283 | master_fd = qemu_openpty_raw(&slave_fd, pty_name); |
1284 | if (master_fd < 0) { | |
1f51470d | 1285 | return NULL; |
6f97dba0 AL |
1286 | } |
1287 | ||
6f97dba0 AL |
1288 | close(slave_fd); |
1289 | ||
db39fcf1 | 1290 | chr = qemu_chr_alloc(); |
a4e26048 | 1291 | |
4efeabbb MT |
1292 | chr->filename = g_strdup_printf("pty:%s", pty_name); |
1293 | ret->pty = g_strdup(pty_name); | |
e68c5958 | 1294 | ret->has_pty = true; |
58650218 | 1295 | |
e68c5958 | 1296 | fprintf(stderr, "char device redirected to %s (label %s)\n", |
4efeabbb | 1297 | pty_name, id); |
6f97dba0 | 1298 | |
a4e26048 | 1299 | s = g_malloc0(sizeof(PtyCharDriver)); |
6f97dba0 AL |
1300 | chr->opaque = s; |
1301 | chr->chr_write = pty_chr_write; | |
1302 | chr->chr_update_read_handler = pty_chr_update_read_handler; | |
1303 | chr->chr_close = pty_chr_close; | |
e6a87ed8 | 1304 | chr->chr_add_watch = pty_chr_add_watch; |
bd5c51ee | 1305 | chr->explicit_be_open = true; |
6f97dba0 | 1306 | |
093d3a20 | 1307 | s->fd = io_channel_from_fd(master_fd); |
8aa33caf | 1308 | s->timer_tag = 0; |
6f97dba0 | 1309 | |
1f51470d | 1310 | return chr; |
6f97dba0 AL |
1311 | } |
1312 | ||
1313 | static void tty_serial_init(int fd, int speed, | |
1314 | int parity, int data_bits, int stop_bits) | |
1315 | { | |
1316 | struct termios tty; | |
1317 | speed_t spd; | |
1318 | ||
1319 | #if 0 | |
1320 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", | |
1321 | speed, parity, data_bits, stop_bits); | |
1322 | #endif | |
1323 | tcgetattr (fd, &tty); | |
1324 | ||
45eea13b SW |
1325 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
1326 | speed = speed * 10 / 11; | |
1327 | do { | |
1328 | check_speed(50); | |
1329 | check_speed(75); | |
1330 | check_speed(110); | |
1331 | check_speed(134); | |
1332 | check_speed(150); | |
1333 | check_speed(200); | |
1334 | check_speed(300); | |
1335 | check_speed(600); | |
1336 | check_speed(1200); | |
1337 | check_speed(1800); | |
1338 | check_speed(2400); | |
1339 | check_speed(4800); | |
1340 | check_speed(9600); | |
1341 | check_speed(19200); | |
1342 | check_speed(38400); | |
1343 | /* Non-Posix values follow. They may be unsupported on some systems. */ | |
1344 | check_speed(57600); | |
1345 | check_speed(115200); | |
1346 | #ifdef B230400 | |
1347 | check_speed(230400); | |
1348 | #endif | |
1349 | #ifdef B460800 | |
1350 | check_speed(460800); | |
1351 | #endif | |
1352 | #ifdef B500000 | |
1353 | check_speed(500000); | |
1354 | #endif | |
1355 | #ifdef B576000 | |
1356 | check_speed(576000); | |
1357 | #endif | |
1358 | #ifdef B921600 | |
1359 | check_speed(921600); | |
1360 | #endif | |
1361 | #ifdef B1000000 | |
1362 | check_speed(1000000); | |
1363 | #endif | |
1364 | #ifdef B1152000 | |
1365 | check_speed(1152000); | |
1366 | #endif | |
1367 | #ifdef B1500000 | |
1368 | check_speed(1500000); | |
1369 | #endif | |
1370 | #ifdef B2000000 | |
1371 | check_speed(2000000); | |
1372 | #endif | |
1373 | #ifdef B2500000 | |
1374 | check_speed(2500000); | |
1375 | #endif | |
1376 | #ifdef B3000000 | |
1377 | check_speed(3000000); | |
1378 | #endif | |
1379 | #ifdef B3500000 | |
1380 | check_speed(3500000); | |
1381 | #endif | |
1382 | #ifdef B4000000 | |
1383 | check_speed(4000000); | |
1384 | #endif | |
6f97dba0 | 1385 | spd = B115200; |
45eea13b | 1386 | } while (0); |
6f97dba0 AL |
1387 | |
1388 | cfsetispeed(&tty, spd); | |
1389 | cfsetospeed(&tty, spd); | |
1390 | ||
1391 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
1392 | |INLCR|IGNCR|ICRNL|IXON); | |
1393 | tty.c_oflag |= OPOST; | |
1394 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); | |
1395 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); | |
1396 | switch(data_bits) { | |
1397 | default: | |
1398 | case 8: | |
1399 | tty.c_cflag |= CS8; | |
1400 | break; | |
1401 | case 7: | |
1402 | tty.c_cflag |= CS7; | |
1403 | break; | |
1404 | case 6: | |
1405 | tty.c_cflag |= CS6; | |
1406 | break; | |
1407 | case 5: | |
1408 | tty.c_cflag |= CS5; | |
1409 | break; | |
1410 | } | |
1411 | switch(parity) { | |
1412 | default: | |
1413 | case 'N': | |
1414 | break; | |
1415 | case 'E': | |
1416 | tty.c_cflag |= PARENB; | |
1417 | break; | |
1418 | case 'O': | |
1419 | tty.c_cflag |= PARENB | PARODD; | |
1420 | break; | |
1421 | } | |
1422 | if (stop_bits == 2) | |
1423 | tty.c_cflag |= CSTOPB; | |
1424 | ||
1425 | tcsetattr (fd, TCSANOW, &tty); | |
1426 | } | |
1427 | ||
1428 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1429 | { | |
1430 | FDCharDriver *s = chr->opaque; | |
1431 | ||
1432 | switch(cmd) { | |
1433 | case CHR_IOCTL_SERIAL_SET_PARAMS: | |
1434 | { | |
1435 | QEMUSerialSetParams *ssp = arg; | |
a29753f8 AL |
1436 | tty_serial_init(g_io_channel_unix_get_fd(s->fd_in), |
1437 | ssp->speed, ssp->parity, | |
6f97dba0 AL |
1438 | ssp->data_bits, ssp->stop_bits); |
1439 | } | |
1440 | break; | |
1441 | case CHR_IOCTL_SERIAL_SET_BREAK: | |
1442 | { | |
1443 | int enable = *(int *)arg; | |
a29753f8 AL |
1444 | if (enable) { |
1445 | tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1); | |
1446 | } | |
6f97dba0 AL |
1447 | } |
1448 | break; | |
1449 | case CHR_IOCTL_SERIAL_GET_TIOCM: | |
1450 | { | |
1451 | int sarg = 0; | |
1452 | int *targ = (int *)arg; | |
a29753f8 | 1453 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg); |
6f97dba0 | 1454 | *targ = 0; |
b4abdfa4 | 1455 | if (sarg & TIOCM_CTS) |
6f97dba0 | 1456 | *targ |= CHR_TIOCM_CTS; |
b4abdfa4 | 1457 | if (sarg & TIOCM_CAR) |
6f97dba0 | 1458 | *targ |= CHR_TIOCM_CAR; |
b4abdfa4 | 1459 | if (sarg & TIOCM_DSR) |
6f97dba0 | 1460 | *targ |= CHR_TIOCM_DSR; |
b4abdfa4 | 1461 | if (sarg & TIOCM_RI) |
6f97dba0 | 1462 | *targ |= CHR_TIOCM_RI; |
b4abdfa4 | 1463 | if (sarg & TIOCM_DTR) |
6f97dba0 | 1464 | *targ |= CHR_TIOCM_DTR; |
b4abdfa4 | 1465 | if (sarg & TIOCM_RTS) |
6f97dba0 AL |
1466 | *targ |= CHR_TIOCM_RTS; |
1467 | } | |
1468 | break; | |
1469 | case CHR_IOCTL_SERIAL_SET_TIOCM: | |
1470 | { | |
1471 | int sarg = *(int *)arg; | |
1472 | int targ = 0; | |
a29753f8 | 1473 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ); |
b4abdfa4 AJ |
1474 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
1475 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); | |
1476 | if (sarg & CHR_TIOCM_CTS) | |
1477 | targ |= TIOCM_CTS; | |
1478 | if (sarg & CHR_TIOCM_CAR) | |
1479 | targ |= TIOCM_CAR; | |
1480 | if (sarg & CHR_TIOCM_DSR) | |
1481 | targ |= TIOCM_DSR; | |
1482 | if (sarg & CHR_TIOCM_RI) | |
1483 | targ |= TIOCM_RI; | |
1484 | if (sarg & CHR_TIOCM_DTR) | |
6f97dba0 | 1485 | targ |= TIOCM_DTR; |
b4abdfa4 | 1486 | if (sarg & CHR_TIOCM_RTS) |
6f97dba0 | 1487 | targ |= TIOCM_RTS; |
a29753f8 | 1488 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ); |
6f97dba0 AL |
1489 | } |
1490 | break; | |
1491 | default: | |
1492 | return -ENOTSUP; | |
1493 | } | |
1494 | return 0; | |
1495 | } | |
1496 | ||
4266a134 DA |
1497 | static void qemu_chr_close_tty(CharDriverState *chr) |
1498 | { | |
1499 | FDCharDriver *s = chr->opaque; | |
1500 | int fd = -1; | |
1501 | ||
1502 | if (s) { | |
a29753f8 | 1503 | fd = g_io_channel_unix_get_fd(s->fd_in); |
4266a134 DA |
1504 | } |
1505 | ||
1506 | fd_chr_close(chr); | |
1507 | ||
1508 | if (fd >= 0) { | |
1509 | close(fd); | |
1510 | } | |
1511 | } | |
1512 | ||
d59044ef GH |
1513 | static CharDriverState *qemu_chr_open_tty_fd(int fd) |
1514 | { | |
1515 | CharDriverState *chr; | |
1516 | ||
1517 | tty_serial_init(fd, 115200, 'N', 8, 1); | |
1518 | chr = qemu_chr_open_fd(fd, fd); | |
1519 | chr->chr_ioctl = tty_serial_ioctl; | |
1520 | chr->chr_close = qemu_chr_close_tty; | |
1521 | return chr; | |
1522 | } | |
6f97dba0 AL |
1523 | #endif /* __linux__ || __sun__ */ |
1524 | ||
1525 | #if defined(__linux__) | |
e551498e GH |
1526 | |
1527 | #define HAVE_CHARDEV_PARPORT 1 | |
1528 | ||
6f97dba0 AL |
1529 | typedef struct { |
1530 | int fd; | |
1531 | int mode; | |
1532 | } ParallelCharDriver; | |
1533 | ||
1534 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) | |
1535 | { | |
1536 | if (s->mode != mode) { | |
1537 | int m = mode; | |
1538 | if (ioctl(s->fd, PPSETMODE, &m) < 0) | |
1539 | return 0; | |
1540 | s->mode = mode; | |
1541 | } | |
1542 | return 1; | |
1543 | } | |
1544 | ||
1545 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1546 | { | |
1547 | ParallelCharDriver *drv = chr->opaque; | |
1548 | int fd = drv->fd; | |
1549 | uint8_t b; | |
1550 | ||
1551 | switch(cmd) { | |
1552 | case CHR_IOCTL_PP_READ_DATA: | |
1553 | if (ioctl(fd, PPRDATA, &b) < 0) | |
1554 | return -ENOTSUP; | |
1555 | *(uint8_t *)arg = b; | |
1556 | break; | |
1557 | case CHR_IOCTL_PP_WRITE_DATA: | |
1558 | b = *(uint8_t *)arg; | |
1559 | if (ioctl(fd, PPWDATA, &b) < 0) | |
1560 | return -ENOTSUP; | |
1561 | break; | |
1562 | case CHR_IOCTL_PP_READ_CONTROL: | |
1563 | if (ioctl(fd, PPRCONTROL, &b) < 0) | |
1564 | return -ENOTSUP; | |
1565 | /* Linux gives only the lowest bits, and no way to know data | |
1566 | direction! For better compatibility set the fixed upper | |
1567 | bits. */ | |
1568 | *(uint8_t *)arg = b | 0xc0; | |
1569 | break; | |
1570 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1571 | b = *(uint8_t *)arg; | |
1572 | if (ioctl(fd, PPWCONTROL, &b) < 0) | |
1573 | return -ENOTSUP; | |
1574 | break; | |
1575 | case CHR_IOCTL_PP_READ_STATUS: | |
1576 | if (ioctl(fd, PPRSTATUS, &b) < 0) | |
1577 | return -ENOTSUP; | |
1578 | *(uint8_t *)arg = b; | |
1579 | break; | |
1580 | case CHR_IOCTL_PP_DATA_DIR: | |
1581 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) | |
1582 | return -ENOTSUP; | |
1583 | break; | |
1584 | case CHR_IOCTL_PP_EPP_READ_ADDR: | |
1585 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1586 | struct ParallelIOArg *parg = arg; | |
1587 | int n = read(fd, parg->buffer, parg->count); | |
1588 | if (n != parg->count) { | |
1589 | return -EIO; | |
1590 | } | |
1591 | } | |
1592 | break; | |
1593 | case CHR_IOCTL_PP_EPP_READ: | |
1594 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1595 | struct ParallelIOArg *parg = arg; | |
1596 | int n = read(fd, parg->buffer, parg->count); | |
1597 | if (n != parg->count) { | |
1598 | return -EIO; | |
1599 | } | |
1600 | } | |
1601 | break; | |
1602 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: | |
1603 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1604 | struct ParallelIOArg *parg = arg; | |
1605 | int n = write(fd, parg->buffer, parg->count); | |
1606 | if (n != parg->count) { | |
1607 | return -EIO; | |
1608 | } | |
1609 | } | |
1610 | break; | |
1611 | case CHR_IOCTL_PP_EPP_WRITE: | |
1612 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1613 | struct ParallelIOArg *parg = arg; | |
1614 | int n = write(fd, parg->buffer, parg->count); | |
1615 | if (n != parg->count) { | |
1616 | return -EIO; | |
1617 | } | |
1618 | } | |
1619 | break; | |
1620 | default: | |
1621 | return -ENOTSUP; | |
1622 | } | |
1623 | return 0; | |
1624 | } | |
1625 | ||
1626 | static void pp_close(CharDriverState *chr) | |
1627 | { | |
1628 | ParallelCharDriver *drv = chr->opaque; | |
1629 | int fd = drv->fd; | |
1630 | ||
1631 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); | |
1632 | ioctl(fd, PPRELEASE); | |
1633 | close(fd); | |
7267c094 | 1634 | g_free(drv); |
a425d23f | 1635 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1636 | } |
1637 | ||
88a946d3 | 1638 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
6f97dba0 AL |
1639 | { |
1640 | CharDriverState *chr; | |
1641 | ParallelCharDriver *drv; | |
6f97dba0 AL |
1642 | |
1643 | if (ioctl(fd, PPCLAIM) < 0) { | |
1644 | close(fd); | |
1f51470d | 1645 | return NULL; |
6f97dba0 AL |
1646 | } |
1647 | ||
7267c094 | 1648 | drv = g_malloc0(sizeof(ParallelCharDriver)); |
6f97dba0 AL |
1649 | drv->fd = fd; |
1650 | drv->mode = IEEE1284_MODE_COMPAT; | |
1651 | ||
db39fcf1 | 1652 | chr = qemu_chr_alloc(); |
6f97dba0 AL |
1653 | chr->chr_write = null_chr_write; |
1654 | chr->chr_ioctl = pp_ioctl; | |
1655 | chr->chr_close = pp_close; | |
1656 | chr->opaque = drv; | |
1657 | ||
1f51470d | 1658 | return chr; |
6f97dba0 AL |
1659 | } |
1660 | #endif /* __linux__ */ | |
1661 | ||
a167ba50 | 1662 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
e551498e GH |
1663 | |
1664 | #define HAVE_CHARDEV_PARPORT 1 | |
1665 | ||
6972f935 BS |
1666 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1667 | { | |
e0efb993 | 1668 | int fd = (int)(intptr_t)chr->opaque; |
6972f935 BS |
1669 | uint8_t b; |
1670 | ||
1671 | switch(cmd) { | |
1672 | case CHR_IOCTL_PP_READ_DATA: | |
1673 | if (ioctl(fd, PPIGDATA, &b) < 0) | |
1674 | return -ENOTSUP; | |
1675 | *(uint8_t *)arg = b; | |
1676 | break; | |
1677 | case CHR_IOCTL_PP_WRITE_DATA: | |
1678 | b = *(uint8_t *)arg; | |
1679 | if (ioctl(fd, PPISDATA, &b) < 0) | |
1680 | return -ENOTSUP; | |
1681 | break; | |
1682 | case CHR_IOCTL_PP_READ_CONTROL: | |
1683 | if (ioctl(fd, PPIGCTRL, &b) < 0) | |
1684 | return -ENOTSUP; | |
1685 | *(uint8_t *)arg = b; | |
1686 | break; | |
1687 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1688 | b = *(uint8_t *)arg; | |
1689 | if (ioctl(fd, PPISCTRL, &b) < 0) | |
1690 | return -ENOTSUP; | |
1691 | break; | |
1692 | case CHR_IOCTL_PP_READ_STATUS: | |
1693 | if (ioctl(fd, PPIGSTATUS, &b) < 0) | |
1694 | return -ENOTSUP; | |
1695 | *(uint8_t *)arg = b; | |
1696 | break; | |
1697 | default: | |
1698 | return -ENOTSUP; | |
1699 | } | |
1700 | return 0; | |
1701 | } | |
1702 | ||
88a946d3 | 1703 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
6972f935 BS |
1704 | { |
1705 | CharDriverState *chr; | |
6972f935 | 1706 | |
db39fcf1 | 1707 | chr = qemu_chr_alloc(); |
e0efb993 | 1708 | chr->opaque = (void *)(intptr_t)fd; |
6972f935 BS |
1709 | chr->chr_write = null_chr_write; |
1710 | chr->chr_ioctl = pp_ioctl; | |
bd5c51ee | 1711 | chr->explicit_be_open = true; |
1f51470d | 1712 | return chr; |
6972f935 BS |
1713 | } |
1714 | #endif | |
1715 | ||
6f97dba0 AL |
1716 | #else /* _WIN32 */ |
1717 | ||
1718 | typedef struct { | |
1719 | int max_size; | |
1720 | HANDLE hcom, hrecv, hsend; | |
9005b2a7 | 1721 | OVERLAPPED orecv; |
6f97dba0 AL |
1722 | BOOL fpipe; |
1723 | DWORD len; | |
9005b2a7 PB |
1724 | |
1725 | /* Protected by the CharDriverState chr_write_lock. */ | |
1726 | OVERLAPPED osend; | |
6f97dba0 AL |
1727 | } WinCharState; |
1728 | ||
db418a0a FC |
1729 | typedef struct { |
1730 | HANDLE hStdIn; | |
1731 | HANDLE hInputReadyEvent; | |
1732 | HANDLE hInputDoneEvent; | |
1733 | HANDLE hInputThread; | |
1734 | uint8_t win_stdio_buf; | |
1735 | } WinStdioCharState; | |
1736 | ||
6f97dba0 AL |
1737 | #define NSENDBUF 2048 |
1738 | #define NRECVBUF 2048 | |
1739 | #define MAXCONNECT 1 | |
1740 | #define NTIMEOUT 5000 | |
1741 | ||
1742 | static int win_chr_poll(void *opaque); | |
1743 | static int win_chr_pipe_poll(void *opaque); | |
1744 | ||
1745 | static void win_chr_close(CharDriverState *chr) | |
1746 | { | |
1747 | WinCharState *s = chr->opaque; | |
1748 | ||
1749 | if (s->hsend) { | |
1750 | CloseHandle(s->hsend); | |
1751 | s->hsend = NULL; | |
1752 | } | |
1753 | if (s->hrecv) { | |
1754 | CloseHandle(s->hrecv); | |
1755 | s->hrecv = NULL; | |
1756 | } | |
1757 | if (s->hcom) { | |
1758 | CloseHandle(s->hcom); | |
1759 | s->hcom = NULL; | |
1760 | } | |
1761 | if (s->fpipe) | |
1762 | qemu_del_polling_cb(win_chr_pipe_poll, chr); | |
1763 | else | |
1764 | qemu_del_polling_cb(win_chr_poll, chr); | |
793cbfb5 | 1765 | |
a425d23f | 1766 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1767 | } |
1768 | ||
1769 | static int win_chr_init(CharDriverState *chr, const char *filename) | |
1770 | { | |
1771 | WinCharState *s = chr->opaque; | |
1772 | COMMCONFIG comcfg; | |
1773 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; | |
1774 | COMSTAT comstat; | |
1775 | DWORD size; | |
1776 | DWORD err; | |
1777 | ||
1778 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1779 | if (!s->hsend) { | |
1780 | fprintf(stderr, "Failed CreateEvent\n"); | |
1781 | goto fail; | |
1782 | } | |
1783 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1784 | if (!s->hrecv) { | |
1785 | fprintf(stderr, "Failed CreateEvent\n"); | |
1786 | goto fail; | |
1787 | } | |
1788 | ||
1789 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, | |
1790 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); | |
1791 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1792 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); | |
1793 | s->hcom = NULL; | |
1794 | goto fail; | |
1795 | } | |
1796 | ||
1797 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { | |
1798 | fprintf(stderr, "Failed SetupComm\n"); | |
1799 | goto fail; | |
1800 | } | |
1801 | ||
1802 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); | |
1803 | size = sizeof(COMMCONFIG); | |
1804 | GetDefaultCommConfig(filename, &comcfg, &size); | |
1805 | comcfg.dcb.DCBlength = sizeof(DCB); | |
1806 | CommConfigDialog(filename, NULL, &comcfg); | |
1807 | ||
1808 | if (!SetCommState(s->hcom, &comcfg.dcb)) { | |
1809 | fprintf(stderr, "Failed SetCommState\n"); | |
1810 | goto fail; | |
1811 | } | |
1812 | ||
1813 | if (!SetCommMask(s->hcom, EV_ERR)) { | |
1814 | fprintf(stderr, "Failed SetCommMask\n"); | |
1815 | goto fail; | |
1816 | } | |
1817 | ||
1818 | cto.ReadIntervalTimeout = MAXDWORD; | |
1819 | if (!SetCommTimeouts(s->hcom, &cto)) { | |
1820 | fprintf(stderr, "Failed SetCommTimeouts\n"); | |
1821 | goto fail; | |
1822 | } | |
1823 | ||
1824 | if (!ClearCommError(s->hcom, &err, &comstat)) { | |
1825 | fprintf(stderr, "Failed ClearCommError\n"); | |
1826 | goto fail; | |
1827 | } | |
1828 | qemu_add_polling_cb(win_chr_poll, chr); | |
1829 | return 0; | |
1830 | ||
1831 | fail: | |
1832 | win_chr_close(chr); | |
1833 | return -1; | |
1834 | } | |
1835 | ||
9005b2a7 | 1836 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
1837 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
1838 | { | |
1839 | WinCharState *s = chr->opaque; | |
1840 | DWORD len, ret, size, err; | |
1841 | ||
1842 | len = len1; | |
1843 | ZeroMemory(&s->osend, sizeof(s->osend)); | |
1844 | s->osend.hEvent = s->hsend; | |
1845 | while (len > 0) { | |
1846 | if (s->hsend) | |
1847 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); | |
1848 | else | |
1849 | ret = WriteFile(s->hcom, buf, len, &size, NULL); | |
1850 | if (!ret) { | |
1851 | err = GetLastError(); | |
1852 | if (err == ERROR_IO_PENDING) { | |
1853 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); | |
1854 | if (ret) { | |
1855 | buf += size; | |
1856 | len -= size; | |
1857 | } else { | |
1858 | break; | |
1859 | } | |
1860 | } else { | |
1861 | break; | |
1862 | } | |
1863 | } else { | |
1864 | buf += size; | |
1865 | len -= size; | |
1866 | } | |
1867 | } | |
1868 | return len1 - len; | |
1869 | } | |
1870 | ||
1871 | static int win_chr_read_poll(CharDriverState *chr) | |
1872 | { | |
1873 | WinCharState *s = chr->opaque; | |
1874 | ||
909cda12 | 1875 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1876 | return s->max_size; |
1877 | } | |
1878 | ||
1879 | static void win_chr_readfile(CharDriverState *chr) | |
1880 | { | |
1881 | WinCharState *s = chr->opaque; | |
1882 | int ret, err; | |
9bd7854e | 1883 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
1884 | DWORD size; |
1885 | ||
1886 | ZeroMemory(&s->orecv, sizeof(s->orecv)); | |
1887 | s->orecv.hEvent = s->hrecv; | |
1888 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); | |
1889 | if (!ret) { | |
1890 | err = GetLastError(); | |
1891 | if (err == ERROR_IO_PENDING) { | |
1892 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); | |
1893 | } | |
1894 | } | |
1895 | ||
1896 | if (size > 0) { | |
fa5efccb | 1897 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
1898 | } |
1899 | } | |
1900 | ||
1901 | static void win_chr_read(CharDriverState *chr) | |
1902 | { | |
1903 | WinCharState *s = chr->opaque; | |
1904 | ||
1905 | if (s->len > s->max_size) | |
1906 | s->len = s->max_size; | |
1907 | if (s->len == 0) | |
1908 | return; | |
1909 | ||
1910 | win_chr_readfile(chr); | |
1911 | } | |
1912 | ||
1913 | static int win_chr_poll(void *opaque) | |
1914 | { | |
1915 | CharDriverState *chr = opaque; | |
1916 | WinCharState *s = chr->opaque; | |
1917 | COMSTAT status; | |
1918 | DWORD comerr; | |
1919 | ||
1920 | ClearCommError(s->hcom, &comerr, &status); | |
1921 | if (status.cbInQue > 0) { | |
1922 | s->len = status.cbInQue; | |
1923 | win_chr_read_poll(chr); | |
1924 | win_chr_read(chr); | |
1925 | return 1; | |
1926 | } | |
1927 | return 0; | |
1928 | } | |
1929 | ||
d59044ef | 1930 | static CharDriverState *qemu_chr_open_win_path(const char *filename) |
6f97dba0 AL |
1931 | { |
1932 | CharDriverState *chr; | |
1933 | WinCharState *s; | |
1934 | ||
db39fcf1 | 1935 | chr = qemu_chr_alloc(); |
7267c094 | 1936 | s = g_malloc0(sizeof(WinCharState)); |
6f97dba0 AL |
1937 | chr->opaque = s; |
1938 | chr->chr_write = win_chr_write; | |
1939 | chr->chr_close = win_chr_close; | |
1940 | ||
1941 | if (win_chr_init(chr, filename) < 0) { | |
2e02e18b SW |
1942 | g_free(s); |
1943 | g_free(chr); | |
1f51470d | 1944 | return NULL; |
6f97dba0 | 1945 | } |
1f51470d | 1946 | return chr; |
6f97dba0 AL |
1947 | } |
1948 | ||
1949 | static int win_chr_pipe_poll(void *opaque) | |
1950 | { | |
1951 | CharDriverState *chr = opaque; | |
1952 | WinCharState *s = chr->opaque; | |
1953 | DWORD size; | |
1954 | ||
1955 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); | |
1956 | if (size > 0) { | |
1957 | s->len = size; | |
1958 | win_chr_read_poll(chr); | |
1959 | win_chr_read(chr); | |
1960 | return 1; | |
1961 | } | |
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) | |
1966 | { | |
1967 | WinCharState *s = chr->opaque; | |
1968 | OVERLAPPED ov; | |
1969 | int ret; | |
1970 | DWORD size; | |
1971 | char openname[256]; | |
1972 | ||
1973 | s->fpipe = TRUE; | |
1974 | ||
1975 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1976 | if (!s->hsend) { | |
1977 | fprintf(stderr, "Failed CreateEvent\n"); | |
1978 | goto fail; | |
1979 | } | |
1980 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1981 | if (!s->hrecv) { | |
1982 | fprintf(stderr, "Failed CreateEvent\n"); | |
1983 | goto fail; | |
1984 | } | |
1985 | ||
1986 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); | |
1987 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, | |
1988 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | | |
1989 | PIPE_WAIT, | |
1990 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); | |
1991 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1992 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); | |
1993 | s->hcom = NULL; | |
1994 | goto fail; | |
1995 | } | |
1996 | ||
1997 | ZeroMemory(&ov, sizeof(ov)); | |
1998 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1999 | ret = ConnectNamedPipe(s->hcom, &ov); | |
2000 | if (ret) { | |
2001 | fprintf(stderr, "Failed ConnectNamedPipe\n"); | |
2002 | goto fail; | |
2003 | } | |
2004 | ||
2005 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); | |
2006 | if (!ret) { | |
2007 | fprintf(stderr, "Failed GetOverlappedResult\n"); | |
2008 | if (ov.hEvent) { | |
2009 | CloseHandle(ov.hEvent); | |
2010 | ov.hEvent = NULL; | |
2011 | } | |
2012 | goto fail; | |
2013 | } | |
2014 | ||
2015 | if (ov.hEvent) { | |
2016 | CloseHandle(ov.hEvent); | |
2017 | ov.hEvent = NULL; | |
2018 | } | |
2019 | qemu_add_polling_cb(win_chr_pipe_poll, chr); | |
2020 | return 0; | |
2021 | ||
2022 | fail: | |
2023 | win_chr_close(chr); | |
2024 | return -1; | |
2025 | } | |
2026 | ||
2027 | ||
548cbb36 | 2028 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
6f97dba0 | 2029 | { |
548cbb36 | 2030 | const char *filename = opts->device; |
6f97dba0 AL |
2031 | CharDriverState *chr; |
2032 | WinCharState *s; | |
2033 | ||
db39fcf1 | 2034 | chr = qemu_chr_alloc(); |
7267c094 | 2035 | s = g_malloc0(sizeof(WinCharState)); |
6f97dba0 AL |
2036 | chr->opaque = s; |
2037 | chr->chr_write = win_chr_write; | |
2038 | chr->chr_close = win_chr_close; | |
2039 | ||
2040 | if (win_chr_pipe_init(chr, filename) < 0) { | |
2e02e18b SW |
2041 | g_free(s); |
2042 | g_free(chr); | |
1f51470d | 2043 | return NULL; |
6f97dba0 | 2044 | } |
1f51470d | 2045 | return chr; |
6f97dba0 AL |
2046 | } |
2047 | ||
1f51470d | 2048 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
6f97dba0 AL |
2049 | { |
2050 | CharDriverState *chr; | |
2051 | WinCharState *s; | |
2052 | ||
db39fcf1 | 2053 | chr = qemu_chr_alloc(); |
7267c094 | 2054 | s = g_malloc0(sizeof(WinCharState)); |
6f97dba0 AL |
2055 | s->hcom = fd_out; |
2056 | chr->opaque = s; | |
2057 | chr->chr_write = win_chr_write; | |
1f51470d | 2058 | return chr; |
6f97dba0 AL |
2059 | } |
2060 | ||
d9ac374f | 2061 | static CharDriverState *qemu_chr_open_win_con(void) |
6f97dba0 | 2062 | { |
1f51470d | 2063 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
6f97dba0 AL |
2064 | } |
2065 | ||
db418a0a FC |
2066 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
2067 | { | |
2068 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
2069 | DWORD dwSize; | |
2070 | int len1; | |
2071 | ||
2072 | len1 = len; | |
2073 | ||
2074 | while (len1 > 0) { | |
2075 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { | |
2076 | break; | |
2077 | } | |
2078 | buf += dwSize; | |
2079 | len1 -= dwSize; | |
2080 | } | |
2081 | ||
2082 | return len - len1; | |
2083 | } | |
2084 | ||
2085 | static void win_stdio_wait_func(void *opaque) | |
2086 | { | |
2087 | CharDriverState *chr = opaque; | |
2088 | WinStdioCharState *stdio = chr->opaque; | |
2089 | INPUT_RECORD buf[4]; | |
2090 | int ret; | |
2091 | DWORD dwSize; | |
2092 | int i; | |
2093 | ||
dff7424d | 2094 | ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize); |
db418a0a FC |
2095 | |
2096 | if (!ret) { | |
2097 | /* Avoid error storm */ | |
2098 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); | |
2099 | return; | |
2100 | } | |
2101 | ||
2102 | for (i = 0; i < dwSize; i++) { | |
2103 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; | |
2104 | ||
2105 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { | |
2106 | int j; | |
2107 | if (kev->uChar.AsciiChar != 0) { | |
2108 | for (j = 0; j < kev->wRepeatCount; j++) { | |
2109 | if (qemu_chr_be_can_write(chr)) { | |
2110 | uint8_t c = kev->uChar.AsciiChar; | |
2111 | qemu_chr_be_write(chr, &c, 1); | |
2112 | } | |
2113 | } | |
2114 | } | |
2115 | } | |
2116 | } | |
2117 | } | |
2118 | ||
2119 | static DWORD WINAPI win_stdio_thread(LPVOID param) | |
2120 | { | |
2121 | CharDriverState *chr = param; | |
2122 | WinStdioCharState *stdio = chr->opaque; | |
2123 | int ret; | |
2124 | DWORD dwSize; | |
2125 | ||
2126 | while (1) { | |
2127 | ||
2128 | /* Wait for one byte */ | |
2129 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); | |
2130 | ||
2131 | /* Exit in case of error, continue if nothing read */ | |
2132 | if (!ret) { | |
2133 | break; | |
2134 | } | |
2135 | if (!dwSize) { | |
2136 | continue; | |
2137 | } | |
2138 | ||
2139 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ | |
2140 | if (stdio->win_stdio_buf == '\r') { | |
2141 | continue; | |
2142 | } | |
2143 | ||
2144 | /* Signal the main thread and wait until the byte was eaten */ | |
2145 | if (!SetEvent(stdio->hInputReadyEvent)) { | |
2146 | break; | |
2147 | } | |
2148 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) | |
2149 | != WAIT_OBJECT_0) { | |
2150 | break; | |
2151 | } | |
2152 | } | |
2153 | ||
2154 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); | |
2155 | return 0; | |
2156 | } | |
2157 | ||
2158 | static void win_stdio_thread_wait_func(void *opaque) | |
2159 | { | |
2160 | CharDriverState *chr = opaque; | |
2161 | WinStdioCharState *stdio = chr->opaque; | |
2162 | ||
2163 | if (qemu_chr_be_can_write(chr)) { | |
2164 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); | |
2165 | } | |
2166 | ||
2167 | SetEvent(stdio->hInputDoneEvent); | |
2168 | } | |
2169 | ||
2170 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) | |
2171 | { | |
2172 | WinStdioCharState *stdio = chr->opaque; | |
2173 | DWORD dwMode = 0; | |
2174 | ||
2175 | GetConsoleMode(stdio->hStdIn, &dwMode); | |
2176 | ||
2177 | if (echo) { | |
2178 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); | |
2179 | } else { | |
2180 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | static void win_stdio_close(CharDriverState *chr) | |
2185 | { | |
2186 | WinStdioCharState *stdio = chr->opaque; | |
2187 | ||
2188 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { | |
2189 | CloseHandle(stdio->hInputReadyEvent); | |
2190 | } | |
2191 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { | |
2192 | CloseHandle(stdio->hInputDoneEvent); | |
2193 | } | |
2194 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { | |
2195 | TerminateThread(stdio->hInputThread, 0); | |
2196 | } | |
2197 | ||
2198 | g_free(chr->opaque); | |
2199 | g_free(chr); | |
db418a0a FC |
2200 | } |
2201 | ||
7c358031 | 2202 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
db418a0a FC |
2203 | { |
2204 | CharDriverState *chr; | |
2205 | WinStdioCharState *stdio; | |
2206 | DWORD dwMode; | |
2207 | int is_console = 0; | |
2208 | ||
db39fcf1 | 2209 | chr = qemu_chr_alloc(); |
db418a0a FC |
2210 | stdio = g_malloc0(sizeof(WinStdioCharState)); |
2211 | ||
2212 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); | |
2213 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { | |
2214 | fprintf(stderr, "cannot open stdio: invalid handle\n"); | |
2215 | exit(1); | |
2216 | } | |
2217 | ||
2218 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; | |
2219 | ||
2220 | chr->opaque = stdio; | |
2221 | chr->chr_write = win_stdio_write; | |
2222 | chr->chr_close = win_stdio_close; | |
2223 | ||
ed7a1540 AL |
2224 | if (is_console) { |
2225 | if (qemu_add_wait_object(stdio->hStdIn, | |
2226 | win_stdio_wait_func, chr)) { | |
2227 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
2228 | } | |
2229 | } else { | |
2230 | DWORD dwId; | |
2231 | ||
2232 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2233 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2234 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, | |
2235 | chr, 0, &dwId); | |
2236 | ||
2237 | if (stdio->hInputThread == INVALID_HANDLE_VALUE | |
2238 | || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE | |
2239 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { | |
2240 | fprintf(stderr, "cannot create stdio thread or event\n"); | |
2241 | exit(1); | |
2242 | } | |
2243 | if (qemu_add_wait_object(stdio->hInputReadyEvent, | |
2244 | win_stdio_thread_wait_func, chr)) { | |
2245 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
db418a0a FC |
2246 | } |
2247 | } | |
2248 | ||
2249 | dwMode |= ENABLE_LINE_INPUT; | |
2250 | ||
ed7a1540 | 2251 | if (is_console) { |
db418a0a FC |
2252 | /* set the terminal in raw mode */ |
2253 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ | |
2254 | dwMode |= ENABLE_PROCESSED_INPUT; | |
2255 | } | |
2256 | ||
2257 | SetConsoleMode(stdio->hStdIn, dwMode); | |
2258 | ||
2259 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; | |
2260 | qemu_chr_fe_set_echo(chr, false); | |
2261 | ||
1f51470d | 2262 | return chr; |
db418a0a | 2263 | } |
6f97dba0 AL |
2264 | #endif /* !_WIN32 */ |
2265 | ||
5ab8211b | 2266 | |
6f97dba0 AL |
2267 | /***********************************************************/ |
2268 | /* UDP Net console */ | |
2269 | ||
2270 | typedef struct { | |
2271 | int fd; | |
76a9644b | 2272 | GIOChannel *chan; |
9bd7854e | 2273 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2274 | int bufcnt; |
2275 | int bufptr; | |
2276 | int max_size; | |
2277 | } NetCharDriver; | |
2278 | ||
9005b2a7 | 2279 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
2280 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2281 | { | |
2282 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2283 | gsize bytes_written; |
2284 | GIOStatus status; | |
2285 | ||
2286 | status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL); | |
2287 | if (status == G_IO_STATUS_EOF) { | |
2288 | return 0; | |
2289 | } else if (status != G_IO_STATUS_NORMAL) { | |
2290 | return -1; | |
2291 | } | |
6f97dba0 | 2292 | |
76a9644b | 2293 | return bytes_written; |
6f97dba0 AL |
2294 | } |
2295 | ||
2296 | static int udp_chr_read_poll(void *opaque) | |
2297 | { | |
2298 | CharDriverState *chr = opaque; | |
2299 | NetCharDriver *s = chr->opaque; | |
2300 | ||
909cda12 | 2301 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2302 | |
2303 | /* If there were any stray characters in the queue process them | |
2304 | * first | |
2305 | */ | |
2306 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2307 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2308 | s->bufptr++; |
909cda12 | 2309 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2310 | } |
2311 | return s->max_size; | |
2312 | } | |
2313 | ||
76a9644b | 2314 | static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2315 | { |
2316 | CharDriverState *chr = opaque; | |
2317 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2318 | gsize bytes_read = 0; |
2319 | GIOStatus status; | |
6f97dba0 | 2320 | |
cdbf6e16 PB |
2321 | if (s->max_size == 0) { |
2322 | return TRUE; | |
2323 | } | |
76a9644b AL |
2324 | status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf), |
2325 | &bytes_read, NULL); | |
2326 | s->bufcnt = bytes_read; | |
6f97dba0 | 2327 | s->bufptr = s->bufcnt; |
76a9644b | 2328 | if (status != G_IO_STATUS_NORMAL) { |
26da70c7 | 2329 | remove_fd_in_watch(chr); |
76a9644b AL |
2330 | return FALSE; |
2331 | } | |
6f97dba0 AL |
2332 | |
2333 | s->bufptr = 0; | |
2334 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2335 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2336 | s->bufptr++; |
909cda12 | 2337 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 | 2338 | } |
76a9644b AL |
2339 | |
2340 | return TRUE; | |
6f97dba0 AL |
2341 | } |
2342 | ||
2343 | static void udp_chr_update_read_handler(CharDriverState *chr) | |
2344 | { | |
2345 | NetCharDriver *s = chr->opaque; | |
2346 | ||
26da70c7 | 2347 | remove_fd_in_watch(chr); |
76a9644b | 2348 | if (s->chan) { |
7ba9addc AS |
2349 | chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll, |
2350 | udp_chr_read, chr); | |
6f97dba0 AL |
2351 | } |
2352 | } | |
2353 | ||
819f56b7 AL |
2354 | static void udp_chr_close(CharDriverState *chr) |
2355 | { | |
2356 | NetCharDriver *s = chr->opaque; | |
26da70c7 AS |
2357 | |
2358 | remove_fd_in_watch(chr); | |
76a9644b AL |
2359 | if (s->chan) { |
2360 | g_io_channel_unref(s->chan); | |
819f56b7 AL |
2361 | closesocket(s->fd); |
2362 | } | |
7267c094 | 2363 | g_free(s); |
a425d23f | 2364 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
819f56b7 AL |
2365 | } |
2366 | ||
3ecc059d | 2367 | static CharDriverState *qemu_chr_open_udp_fd(int fd) |
6f97dba0 AL |
2368 | { |
2369 | CharDriverState *chr = NULL; | |
2370 | NetCharDriver *s = NULL; | |
6f97dba0 | 2371 | |
db39fcf1 | 2372 | chr = qemu_chr_alloc(); |
7267c094 | 2373 | s = g_malloc0(sizeof(NetCharDriver)); |
6f97dba0 | 2374 | |
6f97dba0 | 2375 | s->fd = fd; |
76a9644b | 2376 | s->chan = io_channel_from_socket(s->fd); |
6f97dba0 AL |
2377 | s->bufcnt = 0; |
2378 | s->bufptr = 0; | |
2379 | chr->opaque = s; | |
2380 | chr->chr_write = udp_chr_write; | |
2381 | chr->chr_update_read_handler = udp_chr_update_read_handler; | |
819f56b7 | 2382 | chr->chr_close = udp_chr_close; |
bd5c51ee MR |
2383 | /* be isn't opened until we get a connection */ |
2384 | chr->explicit_be_open = true; | |
1f51470d | 2385 | return chr; |
3ecc059d | 2386 | } |
6f97dba0 | 2387 | |
6f97dba0 AL |
2388 | /***********************************************************/ |
2389 | /* TCP Net console */ | |
2390 | ||
2391 | typedef struct { | |
2ea5a7af AL |
2392 | |
2393 | GIOChannel *chan, *listen_chan; | |
7ba9addc | 2394 | guint listen_tag; |
6f97dba0 AL |
2395 | int fd, listen_fd; |
2396 | int connected; | |
2397 | int max_size; | |
2398 | int do_telnetopt; | |
2399 | int do_nodelay; | |
2400 | int is_unix; | |
c76bf6bb NN |
2401 | int *read_msgfds; |
2402 | int read_msgfds_num; | |
d39aac7a NN |
2403 | int *write_msgfds; |
2404 | int write_msgfds_num; | |
6f97dba0 AL |
2405 | } TCPCharDriver; |
2406 | ||
2ea5a7af | 2407 | static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque); |
6f97dba0 | 2408 | |
d39aac7a NN |
2409 | #ifndef _WIN32 |
2410 | static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len) | |
2411 | { | |
2412 | TCPCharDriver *s = chr->opaque; | |
2413 | struct msghdr msgh; | |
2414 | struct iovec iov; | |
2415 | int r; | |
2416 | ||
2417 | size_t fd_size = s->write_msgfds_num * sizeof(int); | |
2418 | char control[CMSG_SPACE(fd_size)]; | |
2419 | struct cmsghdr *cmsg; | |
2420 | ||
2421 | memset(&msgh, 0, sizeof(msgh)); | |
2422 | memset(control, 0, sizeof(control)); | |
2423 | ||
2424 | /* set the payload */ | |
2425 | iov.iov_base = (uint8_t *) buf; | |
2426 | iov.iov_len = len; | |
2427 | ||
2428 | msgh.msg_iov = &iov; | |
2429 | msgh.msg_iovlen = 1; | |
2430 | ||
2431 | msgh.msg_control = control; | |
2432 | msgh.msg_controllen = sizeof(control); | |
2433 | ||
2434 | cmsg = CMSG_FIRSTHDR(&msgh); | |
2435 | ||
2436 | cmsg->cmsg_len = CMSG_LEN(fd_size); | |
2437 | cmsg->cmsg_level = SOL_SOCKET; | |
2438 | cmsg->cmsg_type = SCM_RIGHTS; | |
2439 | memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size); | |
2440 | ||
2441 | do { | |
2442 | r = sendmsg(s->fd, &msgh, 0); | |
2443 | } while (r < 0 && errno == EINTR); | |
2444 | ||
2445 | /* free the written msgfds, no matter what */ | |
2446 | if (s->write_msgfds_num) { | |
2447 | g_free(s->write_msgfds); | |
2448 | s->write_msgfds = 0; | |
2449 | s->write_msgfds_num = 0; | |
2450 | } | |
2451 | ||
2452 | return r; | |
2453 | } | |
2454 | #endif | |
2455 | ||
9005b2a7 | 2456 | /* Called with chr_write_lock held. */ |
6f97dba0 AL |
2457 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2458 | { | |
2459 | TCPCharDriver *s = chr->opaque; | |
2460 | if (s->connected) { | |
d39aac7a NN |
2461 | #ifndef _WIN32 |
2462 | if (s->is_unix && s->write_msgfds_num) { | |
2463 | return unix_send_msgfds(chr, buf, len); | |
2464 | } else | |
2465 | #endif | |
2466 | { | |
2467 | return io_channel_send(s->chan, buf, len); | |
2468 | } | |
455aa1e0 | 2469 | } else { |
6db0fdce | 2470 | /* XXX: indicate an error ? */ |
455aa1e0 | 2471 | return len; |
6f97dba0 AL |
2472 | } |
2473 | } | |
2474 | ||
2475 | static int tcp_chr_read_poll(void *opaque) | |
2476 | { | |
2477 | CharDriverState *chr = opaque; | |
2478 | TCPCharDriver *s = chr->opaque; | |
2479 | if (!s->connected) | |
2480 | return 0; | |
909cda12 | 2481 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2482 | return s->max_size; |
2483 | } | |
2484 | ||
2485 | #define IAC 255 | |
2486 | #define IAC_BREAK 243 | |
2487 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, | |
2488 | TCPCharDriver *s, | |
2489 | uint8_t *buf, int *size) | |
2490 | { | |
2491 | /* Handle any telnet client's basic IAC options to satisfy char by | |
2492 | * char mode with no echo. All IAC options will be removed from | |
2493 | * the buf and the do_telnetopt variable will be used to track the | |
2494 | * state of the width of the IAC information. | |
2495 | * | |
2496 | * IAC commands come in sets of 3 bytes with the exception of the | |
2497 | * "IAC BREAK" command and the double IAC. | |
2498 | */ | |
2499 | ||
2500 | int i; | |
2501 | int j = 0; | |
2502 | ||
2503 | for (i = 0; i < *size; i++) { | |
2504 | if (s->do_telnetopt > 1) { | |
2505 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { | |
2506 | /* Double IAC means send an IAC */ | |
2507 | if (j != i) | |
2508 | buf[j] = buf[i]; | |
2509 | j++; | |
2510 | s->do_telnetopt = 1; | |
2511 | } else { | |
2512 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { | |
2513 | /* Handle IAC break commands by sending a serial break */ | |
a425d23f | 2514 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
2515 | s->do_telnetopt++; |
2516 | } | |
2517 | s->do_telnetopt++; | |
2518 | } | |
2519 | if (s->do_telnetopt >= 4) { | |
2520 | s->do_telnetopt = 1; | |
2521 | } | |
2522 | } else { | |
2523 | if ((unsigned char)buf[i] == IAC) { | |
2524 | s->do_telnetopt = 2; | |
2525 | } else { | |
2526 | if (j != i) | |
2527 | buf[j] = buf[i]; | |
2528 | j++; | |
2529 | } | |
2530 | } | |
2531 | } | |
2532 | *size = j; | |
2533 | } | |
2534 | ||
c76bf6bb | 2535 | static int tcp_get_msgfds(CharDriverState *chr, int *fds, int num) |
7d174059 MM |
2536 | { |
2537 | TCPCharDriver *s = chr->opaque; | |
c76bf6bb NN |
2538 | int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num; |
2539 | ||
2540 | if (to_copy) { | |
d2fc39b4 SH |
2541 | int i; |
2542 | ||
c76bf6bb NN |
2543 | memcpy(fds, s->read_msgfds, to_copy * sizeof(int)); |
2544 | ||
d2fc39b4 SH |
2545 | /* Close unused fds */ |
2546 | for (i = to_copy; i < s->read_msgfds_num; i++) { | |
2547 | close(s->read_msgfds[i]); | |
2548 | } | |
2549 | ||
c76bf6bb NN |
2550 | g_free(s->read_msgfds); |
2551 | s->read_msgfds = 0; | |
2552 | s->read_msgfds_num = 0; | |
2553 | } | |
2554 | ||
2555 | return to_copy; | |
7d174059 MM |
2556 | } |
2557 | ||
d39aac7a NN |
2558 | static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num) |
2559 | { | |
2560 | TCPCharDriver *s = chr->opaque; | |
2561 | ||
2562 | /* clear old pending fd array */ | |
2563 | if (s->write_msgfds) { | |
2564 | g_free(s->write_msgfds); | |
2565 | } | |
2566 | ||
2567 | if (num) { | |
2568 | s->write_msgfds = g_malloc(num * sizeof(int)); | |
2569 | memcpy(s->write_msgfds, fds, num * sizeof(int)); | |
2570 | } | |
2571 | ||
2572 | s->write_msgfds_num = num; | |
2573 | ||
2574 | return 0; | |
2575 | } | |
2576 | ||
73bcc2ac | 2577 | #ifndef _WIN32 |
7d174059 MM |
2578 | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
2579 | { | |
2580 | TCPCharDriver *s = chr->opaque; | |
2581 | struct cmsghdr *cmsg; | |
2582 | ||
2583 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
c76bf6bb | 2584 | int fd_size, i; |
7d174059 | 2585 | |
c76bf6bb | 2586 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) || |
7d174059 | 2587 | cmsg->cmsg_level != SOL_SOCKET || |
c76bf6bb | 2588 | cmsg->cmsg_type != SCM_RIGHTS) { |
7d174059 | 2589 | continue; |
c76bf6bb | 2590 | } |
7d174059 | 2591 | |
c76bf6bb NN |
2592 | fd_size = cmsg->cmsg_len - CMSG_LEN(0); |
2593 | ||
2594 | if (!fd_size) { | |
7d174059 | 2595 | continue; |
c76bf6bb | 2596 | } |
7d174059 | 2597 | |
c76bf6bb NN |
2598 | /* close and clean read_msgfds */ |
2599 | for (i = 0; i < s->read_msgfds_num; i++) { | |
2600 | close(s->read_msgfds[i]); | |
2601 | } | |
9b938c72 | 2602 | |
c76bf6bb NN |
2603 | if (s->read_msgfds_num) { |
2604 | g_free(s->read_msgfds); | |
2605 | } | |
2606 | ||
2607 | s->read_msgfds_num = fd_size / sizeof(int); | |
2608 | s->read_msgfds = g_malloc(fd_size); | |
2609 | memcpy(s->read_msgfds, CMSG_DATA(cmsg), fd_size); | |
2610 | ||
2611 | for (i = 0; i < s->read_msgfds_num; i++) { | |
2612 | int fd = s->read_msgfds[i]; | |
2613 | if (fd < 0) { | |
2614 | continue; | |
2615 | } | |
2616 | ||
2617 | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ | |
2618 | qemu_set_block(fd); | |
2619 | ||
2620 | #ifndef MSG_CMSG_CLOEXEC | |
2621 | qemu_set_cloexec(fd); | |
2622 | #endif | |
2623 | } | |
7d174059 MM |
2624 | } |
2625 | } | |
2626 | ||
9977c894 MM |
2627 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
2628 | { | |
2629 | TCPCharDriver *s = chr->opaque; | |
7cba04f6 | 2630 | struct msghdr msg = { NULL, }; |
9977c894 | 2631 | struct iovec iov[1]; |
7d174059 MM |
2632 | union { |
2633 | struct cmsghdr cmsg; | |
2634 | char control[CMSG_SPACE(sizeof(int))]; | |
2635 | } msg_control; | |
06138651 | 2636 | int flags = 0; |
7d174059 | 2637 | ssize_t ret; |
9977c894 MM |
2638 | |
2639 | iov[0].iov_base = buf; | |
2640 | iov[0].iov_len = len; | |
2641 | ||
2642 | msg.msg_iov = iov; | |
2643 | msg.msg_iovlen = 1; | |
7d174059 MM |
2644 | msg.msg_control = &msg_control; |
2645 | msg.msg_controllen = sizeof(msg_control); | |
2646 | ||
06138651 CB |
2647 | #ifdef MSG_CMSG_CLOEXEC |
2648 | flags |= MSG_CMSG_CLOEXEC; | |
2649 | #endif | |
2650 | ret = recvmsg(s->fd, &msg, flags); | |
2651 | if (ret > 0 && s->is_unix) { | |
7d174059 | 2652 | unix_process_msgfd(chr, &msg); |
06138651 | 2653 | } |
9977c894 | 2654 | |
7d174059 | 2655 | return ret; |
9977c894 MM |
2656 | } |
2657 | #else | |
2658 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) | |
2659 | { | |
2660 | TCPCharDriver *s = chr->opaque; | |
00aa0040 | 2661 | return qemu_recv(s->fd, buf, len, 0); |
9977c894 MM |
2662 | } |
2663 | #endif | |
2664 | ||
d3cc5bc4 AS |
2665 | static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
2666 | { | |
2667 | TCPCharDriver *s = chr->opaque; | |
2668 | return g_io_create_watch(s->chan, cond); | |
2669 | } | |
2670 | ||
7b0bfdf5 NN |
2671 | static void tcp_chr_disconnect(CharDriverState *chr) |
2672 | { | |
2673 | TCPCharDriver *s = chr->opaque; | |
2674 | ||
2675 | s->connected = 0; | |
2676 | if (s->listen_chan) { | |
2677 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, | |
2678 | tcp_chr_accept, chr); | |
2679 | } | |
2680 | remove_fd_in_watch(chr); | |
2681 | g_io_channel_unref(s->chan); | |
2682 | s->chan = NULL; | |
2683 | closesocket(s->fd); | |
2684 | s->fd = -1; | |
2685 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); | |
2686 | } | |
2687 | ||
2ea5a7af | 2688 | static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2689 | { |
2690 | CharDriverState *chr = opaque; | |
2691 | TCPCharDriver *s = chr->opaque; | |
9bd7854e | 2692 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2693 | int len, size; |
2694 | ||
812c1057 KB |
2695 | if (cond & G_IO_HUP) { |
2696 | /* connection closed */ | |
2697 | tcp_chr_disconnect(chr); | |
2698 | return TRUE; | |
2699 | } | |
2700 | ||
2ea5a7af | 2701 | if (!s->connected || s->max_size <= 0) { |
cdbf6e16 | 2702 | return TRUE; |
2ea5a7af | 2703 | } |
6f97dba0 AL |
2704 | len = sizeof(buf); |
2705 | if (len > s->max_size) | |
2706 | len = s->max_size; | |
9977c894 | 2707 | size = tcp_chr_recv(chr, (void *)buf, len); |
6f97dba0 AL |
2708 | if (size == 0) { |
2709 | /* connection closed */ | |
7b0bfdf5 | 2710 | tcp_chr_disconnect(chr); |
6f97dba0 AL |
2711 | } else if (size > 0) { |
2712 | if (s->do_telnetopt) | |
2713 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); | |
2714 | if (size > 0) | |
fa5efccb | 2715 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 | 2716 | } |
2ea5a7af AL |
2717 | |
2718 | return TRUE; | |
6f97dba0 AL |
2719 | } |
2720 | ||
7b0bfdf5 NN |
2721 | static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len) |
2722 | { | |
2723 | TCPCharDriver *s = chr->opaque; | |
2724 | int size; | |
2725 | ||
2726 | if (!s->connected) { | |
2727 | return 0; | |
2728 | } | |
2729 | ||
2730 | size = tcp_chr_recv(chr, (void *) buf, len); | |
2731 | if (size == 0) { | |
2732 | /* connection closed */ | |
2733 | tcp_chr_disconnect(chr); | |
2734 | } | |
2735 | ||
2736 | return size; | |
2737 | } | |
2738 | ||
68c18d1c BS |
2739 | #ifndef _WIN32 |
2740 | CharDriverState *qemu_chr_open_eventfd(int eventfd) | |
2741 | { | |
e9d21c43 DM |
2742 | CharDriverState *chr = qemu_chr_open_fd(eventfd, eventfd); |
2743 | ||
2744 | if (chr) { | |
2745 | chr->avail_connections = 1; | |
2746 | } | |
2747 | ||
2748 | return chr; | |
6cbf4c8c | 2749 | } |
68c18d1c | 2750 | #endif |
6cbf4c8c | 2751 | |
6f97dba0 AL |
2752 | static void tcp_chr_connect(void *opaque) |
2753 | { | |
2754 | CharDriverState *chr = opaque; | |
2755 | TCPCharDriver *s = chr->opaque; | |
2756 | ||
2757 | s->connected = 1; | |
2ea5a7af | 2758 | if (s->chan) { |
7ba9addc AS |
2759 | chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, |
2760 | tcp_chr_read, chr); | |
bbdd2ad0 | 2761 | } |
fee204fd | 2762 | qemu_chr_be_generic_open(chr); |
6f97dba0 AL |
2763 | } |
2764 | ||
ac1b84dd GH |
2765 | static void tcp_chr_update_read_handler(CharDriverState *chr) |
2766 | { | |
2767 | TCPCharDriver *s = chr->opaque; | |
2768 | ||
2769 | remove_fd_in_watch(chr); | |
2770 | if (s->chan) { | |
2771 | chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, | |
2772 | tcp_chr_read, chr); | |
2773 | } | |
2774 | } | |
2775 | ||
6f97dba0 AL |
2776 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
2777 | static void tcp_chr_telnet_init(int fd) | |
2778 | { | |
2779 | char buf[3]; | |
2780 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ | |
2781 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ | |
2782 | send(fd, (char *)buf, 3, 0); | |
2783 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ | |
2784 | send(fd, (char *)buf, 3, 0); | |
2785 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ | |
2786 | send(fd, (char *)buf, 3, 0); | |
2787 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ | |
2788 | send(fd, (char *)buf, 3, 0); | |
2789 | } | |
2790 | ||
13661089 DB |
2791 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
2792 | { | |
2793 | TCPCharDriver *s = chr->opaque; | |
2794 | if (s->fd != -1) | |
2795 | return -1; | |
2796 | ||
f9e8cacc | 2797 | qemu_set_nonblock(fd); |
13661089 DB |
2798 | if (s->do_nodelay) |
2799 | socket_set_nodelay(fd); | |
2800 | s->fd = fd; | |
2ea5a7af | 2801 | s->chan = io_channel_from_socket(fd); |
910b6368 PB |
2802 | if (s->listen_tag) { |
2803 | g_source_remove(s->listen_tag); | |
2804 | s->listen_tag = 0; | |
2805 | } | |
13661089 DB |
2806 | tcp_chr_connect(chr); |
2807 | ||
2808 | return 0; | |
2809 | } | |
2810 | ||
2ea5a7af | 2811 | static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2812 | { |
2813 | CharDriverState *chr = opaque; | |
2814 | TCPCharDriver *s = chr->opaque; | |
2815 | struct sockaddr_in saddr; | |
2816 | #ifndef _WIN32 | |
2817 | struct sockaddr_un uaddr; | |
2818 | #endif | |
2819 | struct sockaddr *addr; | |
2820 | socklen_t len; | |
2821 | int fd; | |
2822 | ||
2823 | for(;;) { | |
2824 | #ifndef _WIN32 | |
2825 | if (s->is_unix) { | |
2826 | len = sizeof(uaddr); | |
2827 | addr = (struct sockaddr *)&uaddr; | |
2828 | } else | |
2829 | #endif | |
2830 | { | |
2831 | len = sizeof(saddr); | |
2832 | addr = (struct sockaddr *)&saddr; | |
2833 | } | |
40ff6d7e | 2834 | fd = qemu_accept(s->listen_fd, addr, &len); |
6f97dba0 | 2835 | if (fd < 0 && errno != EINTR) { |
79f20075 | 2836 | s->listen_tag = 0; |
2ea5a7af | 2837 | return FALSE; |
6f97dba0 AL |
2838 | } else if (fd >= 0) { |
2839 | if (s->do_telnetopt) | |
2840 | tcp_chr_telnet_init(fd); | |
2841 | break; | |
2842 | } | |
2843 | } | |
13661089 DB |
2844 | if (tcp_chr_add_client(chr, fd) < 0) |
2845 | close(fd); | |
2ea5a7af AL |
2846 | |
2847 | return TRUE; | |
6f97dba0 AL |
2848 | } |
2849 | ||
2850 | static void tcp_chr_close(CharDriverState *chr) | |
2851 | { | |
2852 | TCPCharDriver *s = chr->opaque; | |
c76bf6bb | 2853 | int i; |
819f56b7 | 2854 | if (s->fd >= 0) { |
26da70c7 | 2855 | remove_fd_in_watch(chr); |
2ea5a7af AL |
2856 | if (s->chan) { |
2857 | g_io_channel_unref(s->chan); | |
2858 | } | |
6f97dba0 | 2859 | closesocket(s->fd); |
819f56b7 AL |
2860 | } |
2861 | if (s->listen_fd >= 0) { | |
2ea5a7af AL |
2862 | if (s->listen_tag) { |
2863 | g_source_remove(s->listen_tag); | |
910b6368 | 2864 | s->listen_tag = 0; |
2ea5a7af AL |
2865 | } |
2866 | if (s->listen_chan) { | |
2867 | g_io_channel_unref(s->listen_chan); | |
2868 | } | |
6f97dba0 | 2869 | closesocket(s->listen_fd); |
819f56b7 | 2870 | } |
c76bf6bb NN |
2871 | if (s->read_msgfds_num) { |
2872 | for (i = 0; i < s->read_msgfds_num; i++) { | |
2873 | close(s->read_msgfds[i]); | |
2874 | } | |
2875 | g_free(s->read_msgfds); | |
2876 | } | |
d39aac7a NN |
2877 | if (s->write_msgfds_num) { |
2878 | g_free(s->write_msgfds); | |
2879 | } | |
7267c094 | 2880 | g_free(s); |
a425d23f | 2881 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
2882 | } |
2883 | ||
f6bd5d6e GH |
2884 | static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay, |
2885 | bool is_listen, bool is_telnet, | |
2886 | bool is_waitconnect, | |
2887 | Error **errp) | |
6f97dba0 AL |
2888 | { |
2889 | CharDriverState *chr = NULL; | |
2890 | TCPCharDriver *s = NULL; | |
f6bd5d6e GH |
2891 | char host[NI_MAXHOST], serv[NI_MAXSERV]; |
2892 | const char *left = "", *right = ""; | |
2893 | struct sockaddr_storage ss; | |
2894 | socklen_t ss_len = sizeof(ss); | |
2895 | ||
2896 | memset(&ss, 0, ss_len); | |
2897 | if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) { | |
20c39760 | 2898 | error_setg_errno(errp, errno, "getsockname"); |
f6bd5d6e GH |
2899 | return NULL; |
2900 | } | |
2901 | ||
db39fcf1 | 2902 | chr = qemu_chr_alloc(); |
f6bd5d6e GH |
2903 | s = g_malloc0(sizeof(TCPCharDriver)); |
2904 | ||
2905 | s->connected = 0; | |
2906 | s->fd = -1; | |
2907 | s->listen_fd = -1; | |
c76bf6bb NN |
2908 | s->read_msgfds = 0; |
2909 | s->read_msgfds_num = 0; | |
d39aac7a NN |
2910 | s->write_msgfds = 0; |
2911 | s->write_msgfds_num = 0; | |
f6bd5d6e GH |
2912 | |
2913 | chr->filename = g_malloc(256); | |
2914 | switch (ss.ss_family) { | |
2915 | #ifndef _WIN32 | |
2916 | case AF_UNIX: | |
2917 | s->is_unix = 1; | |
2918 | snprintf(chr->filename, 256, "unix:%s%s", | |
2919 | ((struct sockaddr_un *)(&ss))->sun_path, | |
2920 | is_listen ? ",server" : ""); | |
2921 | break; | |
2922 | #endif | |
2923 | case AF_INET6: | |
2924 | left = "["; | |
2925 | right = "]"; | |
2926 | /* fall through */ | |
2927 | case AF_INET: | |
2928 | s->do_nodelay = do_nodelay; | |
2929 | getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host), | |
2930 | serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV); | |
e5545854 | 2931 | snprintf(chr->filename, 256, "%s:%s%s%s:%s%s", |
f6bd5d6e GH |
2932 | is_telnet ? "telnet" : "tcp", |
2933 | left, host, right, serv, | |
2934 | is_listen ? ",server" : ""); | |
2935 | break; | |
2936 | } | |
2937 | ||
2938 | chr->opaque = s; | |
2939 | chr->chr_write = tcp_chr_write; | |
7b0bfdf5 | 2940 | chr->chr_sync_read = tcp_chr_sync_read; |
f6bd5d6e | 2941 | chr->chr_close = tcp_chr_close; |
c76bf6bb | 2942 | chr->get_msgfds = tcp_get_msgfds; |
d39aac7a | 2943 | chr->set_msgfds = tcp_set_msgfds; |
f6bd5d6e | 2944 | chr->chr_add_client = tcp_chr_add_client; |
d3cc5bc4 | 2945 | chr->chr_add_watch = tcp_chr_add_watch; |
ac1b84dd | 2946 | chr->chr_update_read_handler = tcp_chr_update_read_handler; |
bd5c51ee MR |
2947 | /* be isn't opened until we get a connection */ |
2948 | chr->explicit_be_open = true; | |
f6bd5d6e GH |
2949 | |
2950 | if (is_listen) { | |
2951 | s->listen_fd = fd; | |
2ea5a7af AL |
2952 | s->listen_chan = io_channel_from_socket(s->listen_fd); |
2953 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr); | |
f6bd5d6e GH |
2954 | if (is_telnet) { |
2955 | s->do_telnetopt = 1; | |
2956 | } | |
2957 | } else { | |
2958 | s->connected = 1; | |
2959 | s->fd = fd; | |
2960 | socket_set_nodelay(fd); | |
2ea5a7af | 2961 | s->chan = io_channel_from_socket(s->fd); |
f6bd5d6e GH |
2962 | tcp_chr_connect(chr); |
2963 | } | |
2964 | ||
2965 | if (is_listen && is_waitconnect) { | |
fdca2124 GH |
2966 | fprintf(stderr, "QEMU waiting for connection on: %s\n", |
2967 | chr->filename); | |
2ea5a7af | 2968 | tcp_chr_accept(s->listen_chan, G_IO_IN, chr); |
f9e8cacc | 2969 | qemu_set_nonblock(s->listen_fd); |
f6bd5d6e GH |
2970 | } |
2971 | return chr; | |
2972 | } | |
2973 | ||
51767e7c | 2974 | /*********************************************************/ |
3949e594 | 2975 | /* Ring buffer chardev */ |
51767e7c LL |
2976 | |
2977 | typedef struct { | |
2978 | size_t size; | |
2979 | size_t prod; | |
2980 | size_t cons; | |
2981 | uint8_t *cbuf; | |
3949e594 | 2982 | } RingBufCharDriver; |
51767e7c | 2983 | |
3949e594 | 2984 | static size_t ringbuf_count(const CharDriverState *chr) |
51767e7c | 2985 | { |
3949e594 | 2986 | const RingBufCharDriver *d = chr->opaque; |
51767e7c | 2987 | |
5c230105 | 2988 | return d->prod - d->cons; |
51767e7c LL |
2989 | } |
2990 | ||
9005b2a7 | 2991 | /* Called with chr_write_lock held. */ |
3949e594 | 2992 | static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
51767e7c | 2993 | { |
3949e594 | 2994 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
2995 | int i; |
2996 | ||
2997 | if (!buf || (len < 0)) { | |
2998 | return -1; | |
2999 | } | |
3000 | ||
3001 | for (i = 0; i < len; i++ ) { | |
5c230105 MA |
3002 | d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; |
3003 | if (d->prod - d->cons > d->size) { | |
51767e7c LL |
3004 | d->cons = d->prod - d->size; |
3005 | } | |
3006 | } | |
3007 | ||
3008 | return 0; | |
3009 | } | |
3010 | ||
3949e594 | 3011 | static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len) |
51767e7c | 3012 | { |
3949e594 | 3013 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
3014 | int i; |
3015 | ||
9005b2a7 | 3016 | qemu_mutex_lock(&chr->chr_write_lock); |
5c230105 MA |
3017 | for (i = 0; i < len && d->cons != d->prod; i++) { |
3018 | buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; | |
51767e7c | 3019 | } |
9005b2a7 | 3020 | qemu_mutex_unlock(&chr->chr_write_lock); |
51767e7c LL |
3021 | |
3022 | return i; | |
3023 | } | |
3024 | ||
3949e594 | 3025 | static void ringbuf_chr_close(struct CharDriverState *chr) |
51767e7c | 3026 | { |
3949e594 | 3027 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
3028 | |
3029 | g_free(d->cbuf); | |
3030 | g_free(d); | |
3031 | chr->opaque = NULL; | |
3032 | } | |
3033 | ||
4f57378f MA |
3034 | static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts, |
3035 | Error **errp) | |
51767e7c LL |
3036 | { |
3037 | CharDriverState *chr; | |
3949e594 | 3038 | RingBufCharDriver *d; |
51767e7c | 3039 | |
db39fcf1 | 3040 | chr = qemu_chr_alloc(); |
51767e7c LL |
3041 | d = g_malloc(sizeof(*d)); |
3042 | ||
1da48c65 | 3043 | d->size = opts->has_size ? opts->size : 65536; |
51767e7c LL |
3044 | |
3045 | /* The size must be power of 2 */ | |
3046 | if (d->size & (d->size - 1)) { | |
4f57378f | 3047 | error_setg(errp, "size of ringbuf chardev must be power of two"); |
51767e7c LL |
3048 | goto fail; |
3049 | } | |
3050 | ||
3051 | d->prod = 0; | |
3052 | d->cons = 0; | |
3053 | d->cbuf = g_malloc0(d->size); | |
3054 | ||
3055 | chr->opaque = d; | |
3949e594 MA |
3056 | chr->chr_write = ringbuf_chr_write; |
3057 | chr->chr_close = ringbuf_chr_close; | |
51767e7c LL |
3058 | |
3059 | return chr; | |
3060 | ||
3061 | fail: | |
3062 | g_free(d); | |
3063 | g_free(chr); | |
3064 | return NULL; | |
3065 | } | |
3066 | ||
8e597779 | 3067 | bool chr_is_ringbuf(const CharDriverState *chr) |
1f590cf9 | 3068 | { |
3949e594 | 3069 | return chr->chr_write == ringbuf_chr_write; |
1f590cf9 LL |
3070 | } |
3071 | ||
3949e594 | 3072 | void qmp_ringbuf_write(const char *device, const char *data, |
82e59a67 | 3073 | bool has_format, enum DataFormat format, |
1f590cf9 LL |
3074 | Error **errp) |
3075 | { | |
3076 | CharDriverState *chr; | |
c4f331b6 | 3077 | const uint8_t *write_data; |
1f590cf9 | 3078 | int ret; |
3d1bba20 | 3079 | gsize write_count; |
1f590cf9 LL |
3080 | |
3081 | chr = qemu_chr_find(device); | |
3082 | if (!chr) { | |
1a69278e | 3083 | error_setg(errp, "Device '%s' not found", device); |
1f590cf9 LL |
3084 | return; |
3085 | } | |
3086 | ||
3949e594 MA |
3087 | if (!chr_is_ringbuf(chr)) { |
3088 | error_setg(errp,"%s is not a ringbuf device", device); | |
1f590cf9 LL |
3089 | return; |
3090 | } | |
3091 | ||
1f590cf9 LL |
3092 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
3093 | write_data = g_base64_decode(data, &write_count); | |
3094 | } else { | |
3095 | write_data = (uint8_t *)data; | |
82e59a67 | 3096 | write_count = strlen(data); |
1f590cf9 LL |
3097 | } |
3098 | ||
3949e594 | 3099 | ret = ringbuf_chr_write(chr, write_data, write_count); |
1f590cf9 | 3100 | |
13289fb5 MA |
3101 | if (write_data != (uint8_t *)data) { |
3102 | g_free((void *)write_data); | |
3103 | } | |
3104 | ||
1f590cf9 LL |
3105 | if (ret < 0) { |
3106 | error_setg(errp, "Failed to write to device %s", device); | |
3107 | return; | |
3108 | } | |
3109 | } | |
3110 | ||
3949e594 | 3111 | char *qmp_ringbuf_read(const char *device, int64_t size, |
3ab651fc MA |
3112 | bool has_format, enum DataFormat format, |
3113 | Error **errp) | |
49b6d722 LL |
3114 | { |
3115 | CharDriverState *chr; | |
c4f331b6 | 3116 | uint8_t *read_data; |
49b6d722 | 3117 | size_t count; |
3ab651fc | 3118 | char *data; |
49b6d722 LL |
3119 | |
3120 | chr = qemu_chr_find(device); | |
3121 | if (!chr) { | |
1a69278e | 3122 | error_setg(errp, "Device '%s' not found", device); |
49b6d722 LL |
3123 | return NULL; |
3124 | } | |
3125 | ||
3949e594 MA |
3126 | if (!chr_is_ringbuf(chr)) { |
3127 | error_setg(errp,"%s is not a ringbuf device", device); | |
49b6d722 LL |
3128 | return NULL; |
3129 | } | |
3130 | ||
3131 | if (size <= 0) { | |
3132 | error_setg(errp, "size must be greater than zero"); | |
3133 | return NULL; | |
3134 | } | |
3135 | ||
3949e594 | 3136 | count = ringbuf_count(chr); |
49b6d722 | 3137 | size = size > count ? count : size; |
44f3bcd2 | 3138 | read_data = g_malloc(size + 1); |
49b6d722 | 3139 | |
3949e594 | 3140 | ringbuf_chr_read(chr, read_data, size); |
49b6d722 LL |
3141 | |
3142 | if (has_format && (format == DATA_FORMAT_BASE64)) { | |
3ab651fc | 3143 | data = g_base64_encode(read_data, size); |
13289fb5 | 3144 | g_free(read_data); |
49b6d722 | 3145 | } else { |
3949e594 MA |
3146 | /* |
3147 | * FIXME should read only complete, valid UTF-8 characters up | |
3148 | * to @size bytes. Invalid sequences should be replaced by a | |
3149 | * suitable replacement character. Except when (and only | |
3150 | * when) ring buffer lost characters since last read, initial | |
3151 | * continuation characters should be dropped. | |
3152 | */ | |
44f3bcd2 | 3153 | read_data[size] = 0; |
3ab651fc | 3154 | data = (char *)read_data; |
49b6d722 LL |
3155 | } |
3156 | ||
3ab651fc | 3157 | return data; |
49b6d722 LL |
3158 | } |
3159 | ||
33521634 | 3160 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
191bc01b | 3161 | { |
6ea314d9 | 3162 | char host[65], port[33], width[8], height[8]; |
aeb2c47a | 3163 | int pos; |
7d31544f | 3164 | const char *p; |
191bc01b | 3165 | QemuOpts *opts; |
8be7e7e4 | 3166 | Error *local_err = NULL; |
191bc01b | 3167 | |
8be7e7e4 | 3168 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
84d18f06 | 3169 | if (local_err) { |
8be7e7e4 LC |
3170 | qerror_report_err(local_err); |
3171 | error_free(local_err); | |
191bc01b | 3172 | return NULL; |
8be7e7e4 | 3173 | } |
191bc01b | 3174 | |
7591c5c1 GH |
3175 | if (strstart(filename, "mon:", &p)) { |
3176 | filename = p; | |
3177 | qemu_opt_set(opts, "mux", "on"); | |
02c4bdf1 PB |
3178 | if (strcmp(filename, "stdio") == 0) { |
3179 | /* Monitor is muxed to stdio: do not exit on Ctrl+C by default | |
3180 | * but pass it to the guest. Handle this only for compat syntax, | |
3181 | * for -chardev syntax we have special option for this. | |
3182 | * This is what -nographic did, redirecting+muxing serial+monitor | |
3183 | * to stdio causing Ctrl+C to be passed to guest. */ | |
3184 | qemu_opt_set(opts, "signal", "off"); | |
3185 | } | |
7591c5c1 GH |
3186 | } |
3187 | ||
f0457e8d GH |
3188 | if (strcmp(filename, "null") == 0 || |
3189 | strcmp(filename, "pty") == 0 || | |
3190 | strcmp(filename, "msmouse") == 0 || | |
dc1c21e6 | 3191 | strcmp(filename, "braille") == 0 || |
5692399f | 3192 | strcmp(filename, "testdev") == 0 || |
f0457e8d | 3193 | strcmp(filename, "stdio") == 0) { |
4490dadf | 3194 | qemu_opt_set(opts, "backend", filename); |
191bc01b GH |
3195 | return opts; |
3196 | } | |
6ea314d9 GH |
3197 | if (strstart(filename, "vc", &p)) { |
3198 | qemu_opt_set(opts, "backend", "vc"); | |
3199 | if (*p == ':') { | |
49aa4058 | 3200 | if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { |
6ea314d9 GH |
3201 | /* pixels */ |
3202 | qemu_opt_set(opts, "width", width); | |
3203 | qemu_opt_set(opts, "height", height); | |
49aa4058 | 3204 | } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { |
6ea314d9 GH |
3205 | /* chars */ |
3206 | qemu_opt_set(opts, "cols", width); | |
3207 | qemu_opt_set(opts, "rows", height); | |
3208 | } else { | |
3209 | goto fail; | |
3210 | } | |
3211 | } | |
3212 | return opts; | |
3213 | } | |
d6c983cd GH |
3214 | if (strcmp(filename, "con:") == 0) { |
3215 | qemu_opt_set(opts, "backend", "console"); | |
3216 | return opts; | |
3217 | } | |
48b76496 GH |
3218 | if (strstart(filename, "COM", NULL)) { |
3219 | qemu_opt_set(opts, "backend", "serial"); | |
3220 | qemu_opt_set(opts, "path", filename); | |
3221 | return opts; | |
3222 | } | |
7d31544f GH |
3223 | if (strstart(filename, "file:", &p)) { |
3224 | qemu_opt_set(opts, "backend", "file"); | |
3225 | qemu_opt_set(opts, "path", p); | |
3226 | return opts; | |
3227 | } | |
3228 | if (strstart(filename, "pipe:", &p)) { | |
3229 | qemu_opt_set(opts, "backend", "pipe"); | |
3230 | qemu_opt_set(opts, "path", p); | |
3231 | return opts; | |
3232 | } | |
aeb2c47a GH |
3233 | if (strstart(filename, "tcp:", &p) || |
3234 | strstart(filename, "telnet:", &p)) { | |
3235 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
3236 | host[0] = 0; | |
3237 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) | |
3238 | goto fail; | |
3239 | } | |
3240 | qemu_opt_set(opts, "backend", "socket"); | |
3241 | qemu_opt_set(opts, "host", host); | |
3242 | qemu_opt_set(opts, "port", port); | |
3243 | if (p[pos] == ',') { | |
3244 | if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0) | |
3245 | goto fail; | |
3246 | } | |
3247 | if (strstart(filename, "telnet:", &p)) | |
3248 | qemu_opt_set(opts, "telnet", "on"); | |
3249 | return opts; | |
3250 | } | |
7e1b35b4 GH |
3251 | if (strstart(filename, "udp:", &p)) { |
3252 | qemu_opt_set(opts, "backend", "udp"); | |
3253 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { | |
3254 | host[0] = 0; | |
39324ca4 | 3255 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
7e1b35b4 GH |
3256 | goto fail; |
3257 | } | |
3258 | } | |
3259 | qemu_opt_set(opts, "host", host); | |
3260 | qemu_opt_set(opts, "port", port); | |
3261 | if (p[pos] == '@') { | |
3262 | p += pos + 1; | |
3263 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
3264 | host[0] = 0; | |
3265 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { | |
7e1b35b4 GH |
3266 | goto fail; |
3267 | } | |
3268 | } | |
3269 | qemu_opt_set(opts, "localaddr", host); | |
3270 | qemu_opt_set(opts, "localport", port); | |
3271 | } | |
3272 | return opts; | |
3273 | } | |
aeb2c47a GH |
3274 | if (strstart(filename, "unix:", &p)) { |
3275 | qemu_opt_set(opts, "backend", "socket"); | |
3276 | if (qemu_opts_do_parse(opts, p, "path") != 0) | |
3277 | goto fail; | |
3278 | return opts; | |
3279 | } | |
48b76496 GH |
3280 | if (strstart(filename, "/dev/parport", NULL) || |
3281 | strstart(filename, "/dev/ppi", NULL)) { | |
3282 | qemu_opt_set(opts, "backend", "parport"); | |
3283 | qemu_opt_set(opts, "path", filename); | |
3284 | return opts; | |
3285 | } | |
3286 | if (strstart(filename, "/dev/", NULL)) { | |
3287 | qemu_opt_set(opts, "backend", "tty"); | |
3288 | qemu_opt_set(opts, "path", filename); | |
3289 | return opts; | |
3290 | } | |
191bc01b | 3291 | |
aeb2c47a | 3292 | fail: |
191bc01b GH |
3293 | qemu_opts_del(opts); |
3294 | return NULL; | |
3295 | } | |
3296 | ||
846e2e49 GH |
3297 | static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, |
3298 | Error **errp) | |
3299 | { | |
3300 | const char *path = qemu_opt_get(opts, "path"); | |
3301 | ||
3302 | if (path == NULL) { | |
3303 | error_setg(errp, "chardev: file: no filename given"); | |
3304 | return; | |
3305 | } | |
3306 | backend->file = g_new0(ChardevFile, 1); | |
3307 | backend->file->out = g_strdup(path); | |
3308 | } | |
3309 | ||
7c358031 GH |
3310 | static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, |
3311 | Error **errp) | |
3312 | { | |
3313 | backend->stdio = g_new0(ChardevStdio, 1); | |
3314 | backend->stdio->has_signal = true; | |
02c4bdf1 | 3315 | backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true); |
7c358031 GH |
3316 | } |
3317 | ||
0f1cb51d GH |
3318 | static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, |
3319 | Error **errp) | |
3320 | { | |
3321 | const char *device = qemu_opt_get(opts, "path"); | |
3322 | ||
3323 | if (device == NULL) { | |
3324 | error_setg(errp, "chardev: serial/tty: no device path given"); | |
3325 | return; | |
3326 | } | |
3327 | backend->serial = g_new0(ChardevHostdev, 1); | |
3328 | backend->serial->device = g_strdup(device); | |
3329 | } | |
3330 | ||
dc375097 GH |
3331 | static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, |
3332 | Error **errp) | |
3333 | { | |
3334 | const char *device = qemu_opt_get(opts, "path"); | |
3335 | ||
3336 | if (device == NULL) { | |
3337 | error_setg(errp, "chardev: parallel: no device path given"); | |
3338 | return; | |
3339 | } | |
3340 | backend->parallel = g_new0(ChardevHostdev, 1); | |
3341 | backend->parallel->device = g_strdup(device); | |
3342 | } | |
3343 | ||
548cbb36 GH |
3344 | static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, |
3345 | Error **errp) | |
3346 | { | |
3347 | const char *device = qemu_opt_get(opts, "path"); | |
3348 | ||
3349 | if (device == NULL) { | |
3350 | error_setg(errp, "chardev: pipe: no device path given"); | |
3351 | return; | |
3352 | } | |
3353 | backend->pipe = g_new0(ChardevHostdev, 1); | |
3354 | backend->pipe->device = g_strdup(device); | |
3355 | } | |
3356 | ||
4f57378f MA |
3357 | static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, |
3358 | Error **errp) | |
1da48c65 GH |
3359 | { |
3360 | int val; | |
3361 | ||
3a1da42e | 3362 | backend->ringbuf = g_new0(ChardevRingbuf, 1); |
1da48c65 | 3363 | |
0f953051 | 3364 | val = qemu_opt_get_size(opts, "size", 0); |
1da48c65 | 3365 | if (val != 0) { |
3a1da42e MA |
3366 | backend->ringbuf->has_size = true; |
3367 | backend->ringbuf->size = val; | |
1da48c65 GH |
3368 | } |
3369 | } | |
3370 | ||
bb6fb7c0 GH |
3371 | static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend, |
3372 | Error **errp) | |
3373 | { | |
3374 | const char *chardev = qemu_opt_get(opts, "chardev"); | |
3375 | ||
3376 | if (chardev == NULL) { | |
3377 | error_setg(errp, "chardev: mux: no chardev given"); | |
3378 | return; | |
3379 | } | |
3380 | backend->mux = g_new0(ChardevMux, 1); | |
3381 | backend->mux->chardev = g_strdup(chardev); | |
3382 | } | |
3383 | ||
dafd325d PM |
3384 | static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, |
3385 | Error **errp) | |
3386 | { | |
3387 | bool is_listen = qemu_opt_get_bool(opts, "server", false); | |
3388 | bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true); | |
3389 | bool is_telnet = qemu_opt_get_bool(opts, "telnet", false); | |
3390 | bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true); | |
3391 | const char *path = qemu_opt_get(opts, "path"); | |
3392 | const char *host = qemu_opt_get(opts, "host"); | |
3393 | const char *port = qemu_opt_get(opts, "port"); | |
3394 | SocketAddress *addr; | |
3395 | ||
3396 | if (!path) { | |
3397 | if (!host) { | |
3398 | error_setg(errp, "chardev: socket: no host given"); | |
3399 | return; | |
3400 | } | |
3401 | if (!port) { | |
3402 | error_setg(errp, "chardev: socket: no port given"); | |
3403 | return; | |
3404 | } | |
3405 | } | |
3406 | ||
3407 | backend->socket = g_new0(ChardevSocket, 1); | |
3408 | ||
3409 | backend->socket->has_nodelay = true; | |
3410 | backend->socket->nodelay = do_nodelay; | |
3411 | backend->socket->has_server = true; | |
3412 | backend->socket->server = is_listen; | |
3413 | backend->socket->has_telnet = true; | |
3414 | backend->socket->telnet = is_telnet; | |
3415 | backend->socket->has_wait = true; | |
3416 | backend->socket->wait = is_waitconnect; | |
3417 | ||
3418 | addr = g_new0(SocketAddress, 1); | |
3419 | if (path) { | |
3420 | addr->kind = SOCKET_ADDRESS_KIND_UNIX; | |
3421 | addr->q_unix = g_new0(UnixSocketAddress, 1); | |
3422 | addr->q_unix->path = g_strdup(path); | |
3423 | } else { | |
3424 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
3425 | addr->inet = g_new0(InetSocketAddress, 1); | |
3426 | addr->inet->host = g_strdup(host); | |
3427 | addr->inet->port = g_strdup(port); | |
3428 | addr->inet->has_to = qemu_opt_get(opts, "to"); | |
3429 | addr->inet->to = qemu_opt_get_number(opts, "to", 0); | |
3430 | addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); | |
3431 | addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); | |
3432 | addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); | |
3433 | addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); | |
3434 | } | |
3435 | backend->socket->addr = addr; | |
3436 | } | |
3437 | ||
90a14bfe PM |
3438 | static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, |
3439 | Error **errp) | |
3440 | { | |
3441 | const char *host = qemu_opt_get(opts, "host"); | |
3442 | const char *port = qemu_opt_get(opts, "port"); | |
3443 | const char *localaddr = qemu_opt_get(opts, "localaddr"); | |
3444 | const char *localport = qemu_opt_get(opts, "localport"); | |
3445 | bool has_local = false; | |
3446 | SocketAddress *addr; | |
3447 | ||
3448 | if (host == NULL || strlen(host) == 0) { | |
3449 | host = "localhost"; | |
3450 | } | |
3451 | if (port == NULL || strlen(port) == 0) { | |
3452 | error_setg(errp, "chardev: udp: remote port not specified"); | |
3453 | return; | |
3454 | } | |
3455 | if (localport == NULL || strlen(localport) == 0) { | |
3456 | localport = "0"; | |
3457 | } else { | |
3458 | has_local = true; | |
3459 | } | |
3460 | if (localaddr == NULL || strlen(localaddr) == 0) { | |
3461 | localaddr = ""; | |
3462 | } else { | |
3463 | has_local = true; | |
3464 | } | |
3465 | ||
3466 | backend->udp = g_new0(ChardevUdp, 1); | |
3467 | ||
3468 | addr = g_new0(SocketAddress, 1); | |
3469 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
3470 | addr->inet = g_new0(InetSocketAddress, 1); | |
3471 | addr->inet->host = g_strdup(host); | |
3472 | addr->inet->port = g_strdup(port); | |
3473 | addr->inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); | |
3474 | addr->inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); | |
3475 | addr->inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); | |
3476 | addr->inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); | |
3477 | backend->udp->remote = addr; | |
3478 | ||
3479 | if (has_local) { | |
3480 | backend->udp->has_local = true; | |
3481 | addr = g_new0(SocketAddress, 1); | |
3482 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
3483 | addr->inet = g_new0(InetSocketAddress, 1); | |
3484 | addr->inet->host = g_strdup(localaddr); | |
3485 | addr->inet->port = g_strdup(localport); | |
3486 | backend->udp->local = addr; | |
3487 | } | |
3488 | } | |
3489 | ||
d654f34e | 3490 | typedef struct CharDriver { |
191bc01b | 3491 | const char *name; |
2c5f4882 | 3492 | /* old, pre qapi */ |
1f51470d | 3493 | CharDriverState *(*open)(QemuOpts *opts); |
2c5f4882 | 3494 | /* new, qapi-based */ |
99aec012 | 3495 | ChardevBackendKind kind; |
2c5f4882 | 3496 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); |
d654f34e AL |
3497 | } CharDriver; |
3498 | ||
3499 | static GSList *backends; | |
3500 | ||
3501 | void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *)) | |
3502 | { | |
3503 | CharDriver *s; | |
3504 | ||
3505 | s = g_malloc0(sizeof(*s)); | |
3506 | s->name = g_strdup(name); | |
3507 | s->open = open; | |
3508 | ||
3509 | backends = g_slist_append(backends, s); | |
3510 | } | |
191bc01b | 3511 | |
99aec012 | 3512 | void register_char_driver_qapi(const char *name, ChardevBackendKind kind, |
2c5f4882 GH |
3513 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp)) |
3514 | { | |
3515 | CharDriver *s; | |
3516 | ||
3517 | s = g_malloc0(sizeof(*s)); | |
3518 | s->name = g_strdup(name); | |
3519 | s->kind = kind; | |
3520 | s->parse = parse; | |
3521 | ||
3522 | backends = g_slist_append(backends, s); | |
3523 | } | |
3524 | ||
f69554b9 | 3525 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
bd2d80b2 GH |
3526 | void (*init)(struct CharDriverState *s), |
3527 | Error **errp) | |
191bc01b | 3528 | { |
0aff637e | 3529 | Error *local_err = NULL; |
d654f34e | 3530 | CharDriver *cd; |
191bc01b | 3531 | CharDriverState *chr; |
d654f34e | 3532 | GSList *i; |
191bc01b GH |
3533 | |
3534 | if (qemu_opts_id(opts) == NULL) { | |
312fd5f2 | 3535 | error_setg(errp, "chardev: no id specified"); |
2274ae9d | 3536 | goto err; |
191bc01b GH |
3537 | } |
3538 | ||
1bbd185f | 3539 | if (qemu_opt_get(opts, "backend") == NULL) { |
312fd5f2 | 3540 | error_setg(errp, "chardev: \"%s\" missing backend", |
bd2d80b2 | 3541 | qemu_opts_id(opts)); |
2274ae9d | 3542 | goto err; |
1bbd185f | 3543 | } |
d654f34e AL |
3544 | for (i = backends; i; i = i->next) { |
3545 | cd = i->data; | |
3546 | ||
3547 | if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) { | |
191bc01b | 3548 | break; |
d654f34e | 3549 | } |
191bc01b | 3550 | } |
d654f34e | 3551 | if (i == NULL) { |
312fd5f2 | 3552 | error_setg(errp, "chardev: backend \"%s\" not found", |
bd2d80b2 | 3553 | qemu_opt_get(opts, "backend")); |
e668287d | 3554 | goto err; |
191bc01b GH |
3555 | } |
3556 | ||
2c5f4882 GH |
3557 | if (!cd->open) { |
3558 | /* using new, qapi init */ | |
3559 | ChardevBackend *backend = g_new0(ChardevBackend, 1); | |
3560 | ChardevReturn *ret = NULL; | |
3561 | const char *id = qemu_opts_id(opts); | |
dc2c4eca | 3562 | char *bid = NULL; |
edb2fb3c GH |
3563 | |
3564 | if (qemu_opt_get_bool(opts, "mux", 0)) { | |
3565 | bid = g_strdup_printf("%s-base", id); | |
3566 | } | |
2c5f4882 GH |
3567 | |
3568 | chr = NULL; | |
3569 | backend->kind = cd->kind; | |
3570 | if (cd->parse) { | |
0aff637e MA |
3571 | cd->parse(opts, backend, &local_err); |
3572 | if (local_err) { | |
3573 | error_propagate(errp, local_err); | |
2c5f4882 GH |
3574 | goto qapi_out; |
3575 | } | |
3576 | } | |
edb2fb3c | 3577 | ret = qmp_chardev_add(bid ? bid : id, backend, errp); |
5f758366 | 3578 | if (!ret) { |
2c5f4882 GH |
3579 | goto qapi_out; |
3580 | } | |
edb2fb3c GH |
3581 | |
3582 | if (bid) { | |
3583 | qapi_free_ChardevBackend(backend); | |
3584 | qapi_free_ChardevReturn(ret); | |
3585 | backend = g_new0(ChardevBackend, 1); | |
3586 | backend->mux = g_new0(ChardevMux, 1); | |
3587 | backend->kind = CHARDEV_BACKEND_KIND_MUX; | |
3588 | backend->mux->chardev = g_strdup(bid); | |
3589 | ret = qmp_chardev_add(id, backend, errp); | |
5f758366 | 3590 | if (!ret) { |
ee6ee83d GH |
3591 | chr = qemu_chr_find(bid); |
3592 | qemu_chr_delete(chr); | |
3593 | chr = NULL; | |
3594 | goto qapi_out; | |
3595 | } | |
edb2fb3c GH |
3596 | } |
3597 | ||
2c5f4882 | 3598 | chr = qemu_chr_find(id); |
2ea3e2c1 | 3599 | chr->opts = opts; |
2c5f4882 GH |
3600 | |
3601 | qapi_out: | |
3602 | qapi_free_ChardevBackend(backend); | |
3603 | qapi_free_ChardevReturn(ret); | |
dc2c4eca | 3604 | g_free(bid); |
2c5f4882 GH |
3605 | return chr; |
3606 | } | |
3607 | ||
d654f34e | 3608 | chr = cd->open(opts); |
1f51470d | 3609 | if (!chr) { |
312fd5f2 | 3610 | error_setg(errp, "chardev: opening backend \"%s\" failed", |
bd2d80b2 | 3611 | qemu_opt_get(opts, "backend")); |
2274ae9d | 3612 | goto err; |
191bc01b GH |
3613 | } |
3614 | ||
3615 | if (!chr->filename) | |
7267c094 | 3616 | chr->filename = g_strdup(qemu_opt_get(opts, "backend")); |
191bc01b | 3617 | chr->init = init; |
bd5c51ee MR |
3618 | /* if we didn't create the chardev via qmp_chardev_add, we |
3619 | * need to send the OPENED event here | |
3620 | */ | |
3621 | if (!chr->explicit_be_open) { | |
3622 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); | |
3623 | } | |
72cf2d4f | 3624 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
7591c5c1 GH |
3625 | |
3626 | if (qemu_opt_get_bool(opts, "mux", 0)) { | |
3627 | CharDriverState *base = chr; | |
3628 | int len = strlen(qemu_opts_id(opts)) + 6; | |
7267c094 | 3629 | base->label = g_malloc(len); |
7591c5c1 GH |
3630 | snprintf(base->label, len, "%s-base", qemu_opts_id(opts)); |
3631 | chr = qemu_chr_open_mux(base); | |
3632 | chr->filename = base->filename; | |
d5b27167 | 3633 | chr->avail_connections = MAX_MUX; |
72cf2d4f | 3634 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
d5b27167 KK |
3635 | } else { |
3636 | chr->avail_connections = 1; | |
7591c5c1 | 3637 | } |
7267c094 | 3638 | chr->label = g_strdup(qemu_opts_id(opts)); |
2274ae9d | 3639 | chr->opts = opts; |
191bc01b | 3640 | return chr; |
2274ae9d GH |
3641 | |
3642 | err: | |
3643 | qemu_opts_del(opts); | |
3644 | return NULL; | |
191bc01b GH |
3645 | } |
3646 | ||
27143a44 | 3647 | CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
6f97dba0 AL |
3648 | { |
3649 | const char *p; | |
3650 | CharDriverState *chr; | |
191bc01b | 3651 | QemuOpts *opts; |
bd2d80b2 | 3652 | Error *err = NULL; |
191bc01b | 3653 | |
c845f401 GH |
3654 | if (strstart(filename, "chardev:", &p)) { |
3655 | return qemu_chr_find(p); | |
3656 | } | |
3657 | ||
191bc01b | 3658 | opts = qemu_chr_parse_compat(label, filename); |
7e1b35b4 GH |
3659 | if (!opts) |
3660 | return NULL; | |
6f97dba0 | 3661 | |
bd2d80b2 | 3662 | chr = qemu_chr_new_from_opts(opts, init, &err); |
84d18f06 | 3663 | if (err) { |
4a44d85e | 3664 | error_report("%s", error_get_pretty(err)); |
bd2d80b2 GH |
3665 | error_free(err); |
3666 | } | |
7e1b35b4 | 3667 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
456d6069 | 3668 | qemu_chr_fe_claim_no_fail(chr); |
7e1b35b4 | 3669 | monitor_init(chr, MONITOR_USE_READLINE); |
6f97dba0 AL |
3670 | } |
3671 | return chr; | |
3672 | } | |
3673 | ||
15f31519 | 3674 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
c48855e1 PB |
3675 | { |
3676 | if (chr->chr_set_echo) { | |
3677 | chr->chr_set_echo(chr, echo); | |
3678 | } | |
3679 | } | |
3680 | ||
8e25daa8 | 3681 | void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open) |
7c32c4fe | 3682 | { |
8e25daa8 | 3683 | if (chr->fe_open == fe_open) { |
c0c4bd2c HG |
3684 | return; |
3685 | } | |
8e25daa8 | 3686 | chr->fe_open = fe_open; |
574b711a HG |
3687 | if (chr->chr_set_fe_open) { |
3688 | chr->chr_set_fe_open(chr, fe_open); | |
7c32c4fe HG |
3689 | } |
3690 | } | |
3691 | ||
d61b0c9a MAL |
3692 | void qemu_chr_fe_event(struct CharDriverState *chr, int event) |
3693 | { | |
3694 | if (chr->chr_fe_event) { | |
3695 | chr->chr_fe_event(chr, event); | |
3696 | } | |
3697 | } | |
3698 | ||
2c8a5942 KW |
3699 | int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond, |
3700 | GIOFunc func, void *user_data) | |
23673ca7 AL |
3701 | { |
3702 | GSource *src; | |
3703 | guint tag; | |
3704 | ||
3705 | if (s->chr_add_watch == NULL) { | |
3706 | return -ENOSYS; | |
3707 | } | |
3708 | ||
3709 | src = s->chr_add_watch(s, cond); | |
62c339c5 PB |
3710 | if (!src) { |
3711 | return -EINVAL; | |
3712 | } | |
3713 | ||
23673ca7 AL |
3714 | g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); |
3715 | tag = g_source_attach(src, NULL); | |
3716 | g_source_unref(src); | |
3717 | ||
3718 | return tag; | |
3719 | } | |
3720 | ||
44c473de HG |
3721 | int qemu_chr_fe_claim(CharDriverState *s) |
3722 | { | |
3723 | if (s->avail_connections < 1) { | |
3724 | return -1; | |
3725 | } | |
3726 | s->avail_connections--; | |
3727 | return 0; | |
3728 | } | |
3729 | ||
3730 | void qemu_chr_fe_claim_no_fail(CharDriverState *s) | |
3731 | { | |
3732 | if (qemu_chr_fe_claim(s) != 0) { | |
3733 | fprintf(stderr, "%s: error chardev \"%s\" already used\n", | |
3734 | __func__, s->label); | |
3735 | exit(1); | |
3736 | } | |
3737 | } | |
3738 | ||
3739 | void qemu_chr_fe_release(CharDriverState *s) | |
3740 | { | |
3741 | s->avail_connections++; | |
3742 | } | |
3743 | ||
70f24fb6 | 3744 | void qemu_chr_delete(CharDriverState *chr) |
6f97dba0 | 3745 | { |
72cf2d4f | 3746 | QTAILQ_REMOVE(&chardevs, chr, next); |
2274ae9d | 3747 | if (chr->chr_close) { |
6f97dba0 | 3748 | chr->chr_close(chr); |
2274ae9d | 3749 | } |
7267c094 AL |
3750 | g_free(chr->filename); |
3751 | g_free(chr->label); | |
2274ae9d GH |
3752 | if (chr->opts) { |
3753 | qemu_opts_del(chr->opts); | |
3754 | } | |
7267c094 | 3755 | g_free(chr); |
6f97dba0 AL |
3756 | } |
3757 | ||
c5a415a0 | 3758 | ChardevInfoList *qmp_query_chardev(Error **errp) |
6f97dba0 | 3759 | { |
c5a415a0 | 3760 | ChardevInfoList *chr_list = NULL; |
6f97dba0 AL |
3761 | CharDriverState *chr; |
3762 | ||
72cf2d4f | 3763 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c5a415a0 LC |
3764 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
3765 | info->value = g_malloc0(sizeof(*info->value)); | |
3766 | info->value->label = g_strdup(chr->label); | |
3767 | info->value->filename = g_strdup(chr->filename); | |
32a97ea1 | 3768 | info->value->frontend_open = chr->fe_open; |
c5a415a0 LC |
3769 | |
3770 | info->next = chr_list; | |
3771 | chr_list = info; | |
6f97dba0 | 3772 | } |
588b3832 | 3773 | |
c5a415a0 | 3774 | return chr_list; |
6f97dba0 | 3775 | } |
c845f401 | 3776 | |
77d1c3c6 MK |
3777 | ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) |
3778 | { | |
3779 | ChardevBackendInfoList *backend_list = NULL; | |
3780 | CharDriver *c = NULL; | |
3781 | GSList *i = NULL; | |
3782 | ||
3783 | for (i = backends; i; i = i->next) { | |
3784 | ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); | |
3785 | c = i->data; | |
3786 | info->value = g_malloc0(sizeof(*info->value)); | |
3787 | info->value->name = g_strdup(c->name); | |
3788 | ||
3789 | info->next = backend_list; | |
3790 | backend_list = info; | |
3791 | } | |
3792 | ||
3793 | return backend_list; | |
3794 | } | |
3795 | ||
c845f401 GH |
3796 | CharDriverState *qemu_chr_find(const char *name) |
3797 | { | |
3798 | CharDriverState *chr; | |
3799 | ||
72cf2d4f | 3800 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c845f401 GH |
3801 | if (strcmp(chr->label, name) != 0) |
3802 | continue; | |
3803 | return chr; | |
3804 | } | |
3805 | return NULL; | |
3806 | } | |
0beb4942 AL |
3807 | |
3808 | /* Get a character (serial) device interface. */ | |
3809 | CharDriverState *qemu_char_get_next_serial(void) | |
3810 | { | |
3811 | static int next_serial; | |
456d6069 | 3812 | CharDriverState *chr; |
0beb4942 AL |
3813 | |
3814 | /* FIXME: This function needs to go away: use chardev properties! */ | |
456d6069 HG |
3815 | |
3816 | while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) { | |
3817 | chr = serial_hds[next_serial++]; | |
3818 | qemu_chr_fe_claim_no_fail(chr); | |
3819 | return chr; | |
3820 | } | |
3821 | return NULL; | |
0beb4942 AL |
3822 | } |
3823 | ||
4d454574 PB |
3824 | QemuOptsList qemu_chardev_opts = { |
3825 | .name = "chardev", | |
3826 | .implied_opt_name = "backend", | |
3827 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), | |
3828 | .desc = { | |
3829 | { | |
3830 | .name = "backend", | |
3831 | .type = QEMU_OPT_STRING, | |
3832 | },{ | |
3833 | .name = "path", | |
3834 | .type = QEMU_OPT_STRING, | |
3835 | },{ | |
3836 | .name = "host", | |
3837 | .type = QEMU_OPT_STRING, | |
3838 | },{ | |
3839 | .name = "port", | |
3840 | .type = QEMU_OPT_STRING, | |
3841 | },{ | |
3842 | .name = "localaddr", | |
3843 | .type = QEMU_OPT_STRING, | |
3844 | },{ | |
3845 | .name = "localport", | |
3846 | .type = QEMU_OPT_STRING, | |
3847 | },{ | |
3848 | .name = "to", | |
3849 | .type = QEMU_OPT_NUMBER, | |
3850 | },{ | |
3851 | .name = "ipv4", | |
3852 | .type = QEMU_OPT_BOOL, | |
3853 | },{ | |
3854 | .name = "ipv6", | |
3855 | .type = QEMU_OPT_BOOL, | |
3856 | },{ | |
3857 | .name = "wait", | |
3858 | .type = QEMU_OPT_BOOL, | |
3859 | },{ | |
3860 | .name = "server", | |
3861 | .type = QEMU_OPT_BOOL, | |
3862 | },{ | |
3863 | .name = "delay", | |
3864 | .type = QEMU_OPT_BOOL, | |
3865 | },{ | |
3866 | .name = "telnet", | |
3867 | .type = QEMU_OPT_BOOL, | |
3868 | },{ | |
3869 | .name = "width", | |
3870 | .type = QEMU_OPT_NUMBER, | |
3871 | },{ | |
3872 | .name = "height", | |
3873 | .type = QEMU_OPT_NUMBER, | |
3874 | },{ | |
3875 | .name = "cols", | |
3876 | .type = QEMU_OPT_NUMBER, | |
3877 | },{ | |
3878 | .name = "rows", | |
3879 | .type = QEMU_OPT_NUMBER, | |
3880 | },{ | |
3881 | .name = "mux", | |
3882 | .type = QEMU_OPT_BOOL, | |
3883 | },{ | |
3884 | .name = "signal", | |
3885 | .type = QEMU_OPT_BOOL, | |
3886 | },{ | |
3887 | .name = "name", | |
3888 | .type = QEMU_OPT_STRING, | |
3889 | },{ | |
3890 | .name = "debug", | |
3891 | .type = QEMU_OPT_NUMBER, | |
51767e7c | 3892 | },{ |
3949e594 | 3893 | .name = "size", |
de1cc36e | 3894 | .type = QEMU_OPT_SIZE, |
bb6fb7c0 GH |
3895 | },{ |
3896 | .name = "chardev", | |
3897 | .type = QEMU_OPT_STRING, | |
4d454574 PB |
3898 | }, |
3899 | { /* end of list */ } | |
3900 | }, | |
3901 | }; | |
f1a1a356 | 3902 | |
ffbdbe59 GH |
3903 | #ifdef _WIN32 |
3904 | ||
3905 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) | |
3906 | { | |
3907 | HANDLE out; | |
3908 | ||
e859eda5 | 3909 | if (file->has_in) { |
ffbdbe59 GH |
3910 | error_setg(errp, "input file not supported"); |
3911 | return NULL; | |
3912 | } | |
3913 | ||
3914 | out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, | |
3915 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
3916 | if (out == INVALID_HANDLE_VALUE) { | |
3917 | error_setg(errp, "open %s failed", file->out); | |
3918 | return NULL; | |
3919 | } | |
3920 | return qemu_chr_open_win_file(out); | |
3921 | } | |
3922 | ||
d36b2b90 MA |
3923 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
3924 | Error **errp) | |
d59044ef | 3925 | { |
d36b2b90 MA |
3926 | return qemu_chr_open_win_path(serial->device); |
3927 | } | |
3928 | ||
3929 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, | |
3930 | Error **errp) | |
3931 | { | |
3932 | error_setg(errp, "character device backend type 'parallel' not supported"); | |
3933 | return NULL; | |
d59044ef GH |
3934 | } |
3935 | ||
ffbdbe59 GH |
3936 | #else /* WIN32 */ |
3937 | ||
3938 | static int qmp_chardev_open_file_source(char *src, int flags, | |
3939 | Error **errp) | |
3940 | { | |
3941 | int fd = -1; | |
3942 | ||
3943 | TFR(fd = qemu_open(src, flags, 0666)); | |
3944 | if (fd == -1) { | |
20c39760 | 3945 | error_setg_file_open(errp, errno, src); |
ffbdbe59 GH |
3946 | } |
3947 | return fd; | |
3948 | } | |
3949 | ||
3950 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) | |
3951 | { | |
5f758366 | 3952 | int flags, in = -1, out; |
ffbdbe59 GH |
3953 | |
3954 | flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY; | |
3955 | out = qmp_chardev_open_file_source(file->out, flags, errp); | |
5f758366 | 3956 | if (out < 0) { |
ffbdbe59 GH |
3957 | return NULL; |
3958 | } | |
3959 | ||
e859eda5 | 3960 | if (file->has_in) { |
ffbdbe59 GH |
3961 | flags = O_RDONLY; |
3962 | in = qmp_chardev_open_file_source(file->in, flags, errp); | |
5f758366 | 3963 | if (in < 0) { |
ffbdbe59 GH |
3964 | qemu_close(out); |
3965 | return NULL; | |
3966 | } | |
3967 | } | |
3968 | ||
3969 | return qemu_chr_open_fd(in, out); | |
3970 | } | |
3971 | ||
d36b2b90 MA |
3972 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
3973 | Error **errp) | |
d59044ef | 3974 | { |
d59044ef | 3975 | #ifdef HAVE_CHARDEV_TTY |
d36b2b90 MA |
3976 | int fd; |
3977 | ||
3978 | fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); | |
5f758366 | 3979 | if (fd < 0) { |
d36b2b90 | 3980 | return NULL; |
39099991 | 3981 | } |
f9e8cacc | 3982 | qemu_set_nonblock(fd); |
d36b2b90 MA |
3983 | return qemu_chr_open_tty_fd(fd); |
3984 | #else | |
3985 | error_setg(errp, "character device backend type 'serial' not supported"); | |
3986 | return NULL; | |
88a946d3 | 3987 | #endif |
d36b2b90 MA |
3988 | } |
3989 | ||
3990 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, | |
3991 | Error **errp) | |
3992 | { | |
88a946d3 | 3993 | #ifdef HAVE_CHARDEV_PARPORT |
d36b2b90 MA |
3994 | int fd; |
3995 | ||
3996 | fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); | |
5f758366 | 3997 | if (fd < 0) { |
d59044ef GH |
3998 | return NULL; |
3999 | } | |
d36b2b90 MA |
4000 | return qemu_chr_open_pp_fd(fd); |
4001 | #else | |
4002 | error_setg(errp, "character device backend type 'parallel' not supported"); | |
4003 | return NULL; | |
4004 | #endif | |
d59044ef GH |
4005 | } |
4006 | ||
ffbdbe59 GH |
4007 | #endif /* WIN32 */ |
4008 | ||
f6bd5d6e GH |
4009 | static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock, |
4010 | Error **errp) | |
4011 | { | |
4012 | SocketAddress *addr = sock->addr; | |
4013 | bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; | |
4014 | bool is_listen = sock->has_server ? sock->server : true; | |
4015 | bool is_telnet = sock->has_telnet ? sock->telnet : false; | |
4016 | bool is_waitconnect = sock->has_wait ? sock->wait : false; | |
4017 | int fd; | |
4018 | ||
4019 | if (is_listen) { | |
4020 | fd = socket_listen(addr, errp); | |
4021 | } else { | |
4022 | fd = socket_connect(addr, errp, NULL, NULL); | |
4023 | } | |
5f758366 | 4024 | if (fd < 0) { |
f6bd5d6e GH |
4025 | return NULL; |
4026 | } | |
4027 | return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, | |
4028 | is_telnet, is_waitconnect, errp); | |
4029 | } | |
4030 | ||
08d0ab3f LL |
4031 | static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp, |
4032 | Error **errp) | |
3ecc059d GH |
4033 | { |
4034 | int fd; | |
4035 | ||
08d0ab3f | 4036 | fd = socket_dgram(udp->remote, udp->local, errp); |
5f758366 | 4037 | if (fd < 0) { |
3ecc059d GH |
4038 | return NULL; |
4039 | } | |
4040 | return qemu_chr_open_udp_fd(fd); | |
4041 | } | |
4042 | ||
f1a1a356 GH |
4043 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, |
4044 | Error **errp) | |
4045 | { | |
4046 | ChardevReturn *ret = g_new0(ChardevReturn, 1); | |
edb2fb3c | 4047 | CharDriverState *base, *chr = NULL; |
f1a1a356 GH |
4048 | |
4049 | chr = qemu_chr_find(id); | |
4050 | if (chr) { | |
4051 | error_setg(errp, "Chardev '%s' already exists", id); | |
4052 | g_free(ret); | |
4053 | return NULL; | |
4054 | } | |
4055 | ||
4056 | switch (backend->kind) { | |
ffbdbe59 GH |
4057 | case CHARDEV_BACKEND_KIND_FILE: |
4058 | chr = qmp_chardev_open_file(backend->file, errp); | |
4059 | break; | |
d36b2b90 MA |
4060 | case CHARDEV_BACKEND_KIND_SERIAL: |
4061 | chr = qmp_chardev_open_serial(backend->serial, errp); | |
4062 | break; | |
4063 | case CHARDEV_BACKEND_KIND_PARALLEL: | |
4064 | chr = qmp_chardev_open_parallel(backend->parallel, errp); | |
d59044ef | 4065 | break; |
548cbb36 GH |
4066 | case CHARDEV_BACKEND_KIND_PIPE: |
4067 | chr = qemu_chr_open_pipe(backend->pipe); | |
4068 | break; | |
f6bd5d6e GH |
4069 | case CHARDEV_BACKEND_KIND_SOCKET: |
4070 | chr = qmp_chardev_open_socket(backend->socket, errp); | |
4071 | break; | |
08d0ab3f LL |
4072 | case CHARDEV_BACKEND_KIND_UDP: |
4073 | chr = qmp_chardev_open_udp(backend->udp, errp); | |
3ecc059d | 4074 | break; |
0a1a7fab GH |
4075 | #ifdef HAVE_CHARDEV_TTY |
4076 | case CHARDEV_BACKEND_KIND_PTY: | |
e68c5958 | 4077 | chr = qemu_chr_open_pty(id, ret); |
0a1a7fab | 4078 | break; |
0a1a7fab | 4079 | #endif |
f1a1a356 | 4080 | case CHARDEV_BACKEND_KIND_NULL: |
80dca9e6 | 4081 | chr = qemu_chr_open_null(); |
f1a1a356 | 4082 | break; |
edb2fb3c GH |
4083 | case CHARDEV_BACKEND_KIND_MUX: |
4084 | base = qemu_chr_find(backend->mux->chardev); | |
4085 | if (base == NULL) { | |
4086 | error_setg(errp, "mux: base chardev %s not found", | |
4087 | backend->mux->chardev); | |
4088 | break; | |
4089 | } | |
4090 | chr = qemu_chr_open_mux(base); | |
4091 | break; | |
f5a51cab GH |
4092 | case CHARDEV_BACKEND_KIND_MSMOUSE: |
4093 | chr = qemu_chr_open_msmouse(); | |
4094 | break; | |
2d57286d GH |
4095 | #ifdef CONFIG_BRLAPI |
4096 | case CHARDEV_BACKEND_KIND_BRAILLE: | |
4097 | chr = chr_baum_init(); | |
4098 | break; | |
4099 | #endif | |
5692399f PB |
4100 | case CHARDEV_BACKEND_KIND_TESTDEV: |
4101 | chr = chr_testdev_init(); | |
4102 | break; | |
7c358031 GH |
4103 | case CHARDEV_BACKEND_KIND_STDIO: |
4104 | chr = qemu_chr_open_stdio(backend->stdio); | |
4105 | break; | |
d9ac374f GH |
4106 | #ifdef _WIN32 |
4107 | case CHARDEV_BACKEND_KIND_CONSOLE: | |
4108 | chr = qemu_chr_open_win_con(); | |
4109 | break; | |
cd153e2a GH |
4110 | #endif |
4111 | #ifdef CONFIG_SPICE | |
4112 | case CHARDEV_BACKEND_KIND_SPICEVMC: | |
4113 | chr = qemu_chr_open_spice_vmc(backend->spicevmc->type); | |
4114 | break; | |
4115 | case CHARDEV_BACKEND_KIND_SPICEPORT: | |
4116 | chr = qemu_chr_open_spice_port(backend->spiceport->fqdn); | |
4117 | break; | |
d9ac374f | 4118 | #endif |
702ec69c GH |
4119 | case CHARDEV_BACKEND_KIND_VC: |
4120 | chr = vc_init(backend->vc); | |
4121 | break; | |
3a1da42e | 4122 | case CHARDEV_BACKEND_KIND_RINGBUF: |
1da48c65 | 4123 | case CHARDEV_BACKEND_KIND_MEMORY: |
3a1da42e | 4124 | chr = qemu_chr_open_ringbuf(backend->ringbuf, errp); |
1da48c65 | 4125 | break; |
f1a1a356 GH |
4126 | default: |
4127 | error_setg(errp, "unknown chardev backend (%d)", backend->kind); | |
4128 | break; | |
4129 | } | |
4130 | ||
3894c787 MA |
4131 | /* |
4132 | * Character backend open hasn't been fully converted to the Error | |
4133 | * API. Some opens fail without setting an error. Set a generic | |
4134 | * error then. | |
4135 | * TODO full conversion to Error API | |
4136 | */ | |
4137 | if (chr == NULL && errp && !*errp) { | |
f1a1a356 GH |
4138 | error_setg(errp, "Failed to create chardev"); |
4139 | } | |
4140 | if (chr) { | |
4141 | chr->label = g_strdup(id); | |
edb2fb3c GH |
4142 | chr->avail_connections = |
4143 | (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1; | |
60d95386 GH |
4144 | if (!chr->filename) { |
4145 | chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]); | |
4146 | } | |
bd5c51ee MR |
4147 | if (!chr->explicit_be_open) { |
4148 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); | |
4149 | } | |
f1a1a356 GH |
4150 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
4151 | return ret; | |
4152 | } else { | |
4153 | g_free(ret); | |
4154 | return NULL; | |
4155 | } | |
4156 | } | |
4157 | ||
4158 | void qmp_chardev_remove(const char *id, Error **errp) | |
4159 | { | |
4160 | CharDriverState *chr; | |
4161 | ||
4162 | chr = qemu_chr_find(id); | |
8108fd3e | 4163 | if (chr == NULL) { |
f1a1a356 GH |
4164 | error_setg(errp, "Chardev '%s' not found", id); |
4165 | return; | |
4166 | } | |
4167 | if (chr->chr_can_read || chr->chr_read || | |
4168 | chr->chr_event || chr->handler_opaque) { | |
4169 | error_setg(errp, "Chardev '%s' is busy", id); | |
4170 | return; | |
4171 | } | |
4172 | qemu_chr_delete(chr); | |
4173 | } | |
d654f34e AL |
4174 | |
4175 | static void register_types(void) | |
4176 | { | |
80dca9e6 | 4177 | register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL); |
dafd325d PM |
4178 | register_char_driver_qapi("socket", CHARDEV_BACKEND_KIND_SOCKET, |
4179 | qemu_chr_parse_socket); | |
90a14bfe PM |
4180 | register_char_driver_qapi("udp", CHARDEV_BACKEND_KIND_UDP, |
4181 | qemu_chr_parse_udp); | |
3a1da42e | 4182 | register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF, |
4f57378f | 4183 | qemu_chr_parse_ringbuf); |
846e2e49 GH |
4184 | register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE, |
4185 | qemu_chr_parse_file_out); | |
7c358031 GH |
4186 | register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO, |
4187 | qemu_chr_parse_stdio); | |
0f1cb51d GH |
4188 | register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL, |
4189 | qemu_chr_parse_serial); | |
4190 | register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL, | |
4191 | qemu_chr_parse_serial); | |
dc375097 GH |
4192 | register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL, |
4193 | qemu_chr_parse_parallel); | |
4194 | register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL, | |
4195 | qemu_chr_parse_parallel); | |
e68c5958 | 4196 | register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL); |
d9ac374f | 4197 | register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL); |
548cbb36 GH |
4198 | register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE, |
4199 | qemu_chr_parse_pipe); | |
bb6fb7c0 GH |
4200 | register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX, |
4201 | qemu_chr_parse_mux); | |
c11ed966 MA |
4202 | /* Bug-compatibility: */ |
4203 | register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY, | |
4204 | qemu_chr_parse_ringbuf); | |
7b7ab18d MR |
4205 | /* this must be done after machine init, since we register FEs with muxes |
4206 | * as part of realize functions like serial_isa_realizefn when -nographic | |
4207 | * is specified | |
4208 | */ | |
4209 | qemu_add_machine_init_done_notifier(&muxes_realize_notify); | |
d654f34e AL |
4210 | } |
4211 | ||
4212 | type_init(register_types); |