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