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