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