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