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