]>
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" | |
25 | #include "net.h" | |
376253ec | 26 | #include "monitor.h" |
6f97dba0 AL |
27 | #include "console.h" |
28 | #include "sysemu.h" | |
29 | #include "qemu-timer.h" | |
30 | #include "qemu-char.h" | |
cf3ebac7 AJ |
31 | #include "hw/usb.h" |
32 | #include "hw/baum.h" | |
aa71cf80 | 33 | #include "hw/msmouse.h" |
c5a415a0 | 34 | #include "qmp-commands.h" |
6f97dba0 AL |
35 | |
36 | #include <unistd.h> | |
37 | #include <fcntl.h> | |
6f97dba0 AL |
38 | #include <time.h> |
39 | #include <errno.h> | |
40 | #include <sys/time.h> | |
41 | #include <zlib.h> | |
42 | ||
43 | #ifndef _WIN32 | |
44 | #include <sys/times.h> | |
45 | #include <sys/wait.h> | |
46 | #include <termios.h> | |
47 | #include <sys/mman.h> | |
48 | #include <sys/ioctl.h> | |
24646c7e | 49 | #include <sys/resource.h> |
6f97dba0 AL |
50 | #include <sys/socket.h> |
51 | #include <netinet/in.h> | |
24646c7e | 52 | #include <net/if.h> |
24646c7e | 53 | #include <arpa/inet.h> |
6f97dba0 AL |
54 | #include <dirent.h> |
55 | #include <netdb.h> | |
56 | #include <sys/select.h> | |
71e72a19 | 57 | #ifdef CONFIG_BSD |
6f97dba0 | 58 | #include <sys/stat.h> |
a167ba50 | 59 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
6f97dba0 | 60 | #include <libutil.h> |
6972f935 BS |
61 | #include <dev/ppbus/ppi.h> |
62 | #include <dev/ppbus/ppbconf.h> | |
a167ba50 AJ |
63 | #if defined(__GLIBC__) |
64 | #include <pty.h> | |
65 | #endif | |
c5e97233 BS |
66 | #elif defined(__DragonFly__) |
67 | #include <libutil.h> | |
68 | #include <dev/misc/ppi/ppi.h> | |
69 | #include <bus/ppbus/ppbconf.h> | |
24646c7e BS |
70 | #else |
71 | #include <util.h> | |
6f97dba0 | 72 | #endif |
bbe813a2 | 73 | #else |
6f97dba0 | 74 | #ifdef __linux__ |
6f97dba0 AL |
75 | #include <pty.h> |
76 | ||
77 | #include <linux/ppdev.h> | |
78 | #include <linux/parport.h> | |
79 | #endif | |
80 | #ifdef __sun__ | |
81 | #include <sys/stat.h> | |
82 | #include <sys/ethernet.h> | |
83 | #include <sys/sockio.h> | |
84 | #include <netinet/arp.h> | |
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_systm.h> | |
87 | #include <netinet/ip.h> | |
88 | #include <netinet/ip_icmp.h> // must come after ip.h | |
89 | #include <netinet/udp.h> | |
90 | #include <netinet/tcp.h> | |
91 | #include <net/if.h> | |
92 | #include <syslog.h> | |
93 | #include <stropts.h> | |
94 | #endif | |
95 | #endif | |
96 | #endif | |
97 | ||
98 | #include "qemu_socket.h" | |
cbcc6336 | 99 | #include "ui/qemu-spice.h" |
6f97dba0 | 100 | |
9bd7854e AS |
101 | #define READ_BUF_LEN 4096 |
102 | ||
6f97dba0 AL |
103 | /***********************************************************/ |
104 | /* character device */ | |
105 | ||
72cf2d4f BS |
106 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
107 | QTAILQ_HEAD_INITIALIZER(chardevs); | |
2970a6c9 | 108 | |
a425d23f | 109 | void qemu_chr_be_event(CharDriverState *s, int event) |
6f97dba0 | 110 | { |
73cdf3f2 AG |
111 | /* Keep track if the char device is open */ |
112 | switch (event) { | |
113 | case CHR_EVENT_OPENED: | |
114 | s->opened = 1; | |
115 | break; | |
116 | case CHR_EVENT_CLOSED: | |
117 | s->opened = 0; | |
118 | break; | |
119 | } | |
120 | ||
6f97dba0 AL |
121 | if (!s->chr_event) |
122 | return; | |
123 | s->chr_event(s->handler_opaque, event); | |
124 | } | |
125 | ||
127338e6 | 126 | static void qemu_chr_generic_open_bh(void *opaque) |
6f97dba0 AL |
127 | { |
128 | CharDriverState *s = opaque; | |
a425d23f | 129 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
6f97dba0 AL |
130 | qemu_bh_delete(s->bh); |
131 | s->bh = NULL; | |
132 | } | |
133 | ||
127338e6 | 134 | void qemu_chr_generic_open(CharDriverState *s) |
6f97dba0 | 135 | { |
69795d67 | 136 | if (s->bh == NULL) { |
127338e6 | 137 | s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s); |
6f97dba0 AL |
138 | qemu_bh_schedule(s->bh); |
139 | } | |
140 | } | |
141 | ||
2cc6e0a1 | 142 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
6f97dba0 AL |
143 | { |
144 | return s->chr_write(s, buf, len); | |
145 | } | |
146 | ||
41084f1b | 147 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
6f97dba0 AL |
148 | { |
149 | if (!s->chr_ioctl) | |
150 | return -ENOTSUP; | |
151 | return s->chr_ioctl(s, cmd, arg); | |
152 | } | |
153 | ||
909cda12 | 154 | int qemu_chr_be_can_write(CharDriverState *s) |
6f97dba0 AL |
155 | { |
156 | if (!s->chr_can_read) | |
157 | return 0; | |
158 | return s->chr_can_read(s->handler_opaque); | |
159 | } | |
160 | ||
fa5efccb | 161 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
6f97dba0 AL |
162 | { |
163 | s->chr_read(s->handler_opaque, buf, len); | |
164 | } | |
165 | ||
74c0d6f0 | 166 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
7d174059 MM |
167 | { |
168 | return s->get_msgfd ? s->get_msgfd(s) : -1; | |
169 | } | |
170 | ||
13661089 DB |
171 | int qemu_chr_add_client(CharDriverState *s, int fd) |
172 | { | |
173 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; | |
174 | } | |
175 | ||
6f97dba0 AL |
176 | void qemu_chr_accept_input(CharDriverState *s) |
177 | { | |
178 | if (s->chr_accept_input) | |
179 | s->chr_accept_input(s); | |
180 | } | |
181 | ||
e7e71b0e | 182 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
6f97dba0 | 183 | { |
9bd7854e | 184 | char buf[READ_BUF_LEN]; |
6f97dba0 AL |
185 | va_list ap; |
186 | va_start(ap, fmt); | |
187 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
2cc6e0a1 | 188 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
6f97dba0 AL |
189 | va_end(ap); |
190 | } | |
191 | ||
6f97dba0 | 192 | void qemu_chr_add_handlers(CharDriverState *s, |
7b27a769 | 193 | IOCanReadHandler *fd_can_read, |
6f97dba0 AL |
194 | IOReadHandler *fd_read, |
195 | IOEventHandler *fd_event, | |
196 | void *opaque) | |
197 | { | |
da7d998b | 198 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
2d6c1ef4 | 199 | /* chr driver being released. */ |
d5b27167 | 200 | ++s->avail_connections; |
2d6c1ef4 | 201 | } |
6f97dba0 AL |
202 | s->chr_can_read = fd_can_read; |
203 | s->chr_read = fd_read; | |
204 | s->chr_event = fd_event; | |
205 | s->handler_opaque = opaque; | |
206 | if (s->chr_update_read_handler) | |
207 | s->chr_update_read_handler(s); | |
73cdf3f2 AG |
208 | |
209 | /* We're connecting to an already opened device, so let's make sure we | |
210 | also get the open event */ | |
211 | if (s->opened) { | |
212 | qemu_chr_generic_open(s); | |
213 | } | |
6f97dba0 AL |
214 | } |
215 | ||
216 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
217 | { | |
218 | return len; | |
219 | } | |
220 | ||
1f51470d | 221 | static CharDriverState *qemu_chr_open_null(QemuOpts *opts) |
6f97dba0 AL |
222 | { |
223 | CharDriverState *chr; | |
224 | ||
7267c094 | 225 | chr = g_malloc0(sizeof(CharDriverState)); |
6f97dba0 | 226 | chr->chr_write = null_chr_write; |
1f51470d | 227 | return chr; |
6f97dba0 AL |
228 | } |
229 | ||
230 | /* MUX driver for serial I/O splitting */ | |
6f97dba0 AL |
231 | #define MAX_MUX 4 |
232 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ | |
233 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) | |
234 | typedef struct { | |
7b27a769 | 235 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
6f97dba0 AL |
236 | IOReadHandler *chr_read[MAX_MUX]; |
237 | IOEventHandler *chr_event[MAX_MUX]; | |
238 | void *ext_opaque[MAX_MUX]; | |
239 | CharDriverState *drv; | |
799f1f23 | 240 | int focus; |
6f97dba0 AL |
241 | int mux_cnt; |
242 | int term_got_escape; | |
243 | int max_size; | |
a80bf99f AL |
244 | /* Intermediate input buffer allows to catch escape sequences even if the |
245 | currently active device is not accepting any input - but only until it | |
246 | is full as well. */ | |
247 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; | |
248 | int prod[MAX_MUX]; | |
249 | int cons[MAX_MUX]; | |
2d22959d | 250 | int timestamps; |
4ab312f7 | 251 | int linestart; |
2d22959d | 252 | int64_t timestamps_start; |
6f97dba0 AL |
253 | } MuxDriver; |
254 | ||
255 | ||
256 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
257 | { | |
258 | MuxDriver *d = chr->opaque; | |
259 | int ret; | |
2d22959d | 260 | if (!d->timestamps) { |
6f97dba0 AL |
261 | ret = d->drv->chr_write(d->drv, buf, len); |
262 | } else { | |
263 | int i; | |
264 | ||
265 | ret = 0; | |
4ab312f7 JK |
266 | for (i = 0; i < len; i++) { |
267 | if (d->linestart) { | |
6f97dba0 AL |
268 | char buf1[64]; |
269 | int64_t ti; | |
270 | int secs; | |
271 | ||
7bd427d8 | 272 | ti = qemu_get_clock_ms(rt_clock); |
2d22959d JK |
273 | if (d->timestamps_start == -1) |
274 | d->timestamps_start = ti; | |
275 | ti -= d->timestamps_start; | |
a4bb1db8 | 276 | secs = ti / 1000; |
6f97dba0 AL |
277 | snprintf(buf1, sizeof(buf1), |
278 | "[%02d:%02d:%02d.%03d] ", | |
279 | secs / 3600, | |
280 | (secs / 60) % 60, | |
281 | secs % 60, | |
a4bb1db8 | 282 | (int)(ti % 1000)); |
6f97dba0 | 283 | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
4ab312f7 JK |
284 | d->linestart = 0; |
285 | } | |
286 | ret += d->drv->chr_write(d->drv, buf+i, 1); | |
287 | if (buf[i] == '\n') { | |
288 | d->linestart = 1; | |
6f97dba0 AL |
289 | } |
290 | } | |
291 | } | |
292 | return ret; | |
293 | } | |
294 | ||
295 | static const char * const mux_help[] = { | |
296 | "% h print this help\n\r", | |
297 | "% x exit emulator\n\r", | |
298 | "% s save disk data back to file (if -snapshot)\n\r", | |
299 | "% t toggle console timestamps\n\r" | |
300 | "% b send break (magic sysrq)\n\r", | |
301 | "% c switch between console and monitor\n\r", | |
302 | "% % sends %\n\r", | |
303 | NULL | |
304 | }; | |
305 | ||
306 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ | |
307 | static void mux_print_help(CharDriverState *chr) | |
308 | { | |
309 | int i, j; | |
310 | char ebuf[15] = "Escape-Char"; | |
311 | char cbuf[50] = "\n\r"; | |
312 | ||
313 | if (term_escape_char > 0 && term_escape_char < 26) { | |
314 | snprintf(cbuf, sizeof(cbuf), "\n\r"); | |
315 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); | |
316 | } else { | |
317 | snprintf(cbuf, sizeof(cbuf), | |
318 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", | |
319 | term_escape_char); | |
320 | } | |
321 | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); | |
322 | for (i = 0; mux_help[i] != NULL; i++) { | |
323 | for (j=0; mux_help[i][j] != '\0'; j++) { | |
324 | if (mux_help[i][j] == '%') | |
325 | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); | |
326 | else | |
327 | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1); | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
2724b180 AL |
332 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
333 | { | |
334 | if (d->chr_event[mux_nr]) | |
335 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); | |
336 | } | |
337 | ||
6f97dba0 AL |
338 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
339 | { | |
340 | if (d->term_got_escape) { | |
341 | d->term_got_escape = 0; | |
342 | if (ch == term_escape_char) | |
343 | goto send_char; | |
344 | switch(ch) { | |
345 | case '?': | |
346 | case 'h': | |
347 | mux_print_help(chr); | |
348 | break; | |
349 | case 'x': | |
350 | { | |
351 | const char *term = "QEMU: Terminated\n\r"; | |
352 | chr->chr_write(chr,(uint8_t *)term,strlen(term)); | |
353 | exit(0); | |
354 | break; | |
355 | } | |
356 | case 's': | |
6ab4b5ab | 357 | bdrv_commit_all(); |
6f97dba0 AL |
358 | break; |
359 | case 'b': | |
a425d23f | 360 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
361 | break; |
362 | case 'c': | |
363 | /* Switch to the next registered device */ | |
799f1f23 GH |
364 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
365 | d->focus++; | |
366 | if (d->focus >= d->mux_cnt) | |
367 | d->focus = 0; | |
368 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); | |
6f97dba0 | 369 | break; |
2d22959d JK |
370 | case 't': |
371 | d->timestamps = !d->timestamps; | |
372 | d->timestamps_start = -1; | |
4ab312f7 | 373 | d->linestart = 0; |
2d22959d | 374 | break; |
6f97dba0 AL |
375 | } |
376 | } else if (ch == term_escape_char) { | |
377 | d->term_got_escape = 1; | |
378 | } else { | |
379 | send_char: | |
380 | return 1; | |
381 | } | |
382 | return 0; | |
383 | } | |
384 | ||
385 | static void mux_chr_accept_input(CharDriverState *chr) | |
386 | { | |
6f97dba0 | 387 | MuxDriver *d = chr->opaque; |
799f1f23 | 388 | int m = d->focus; |
6f97dba0 | 389 | |
a80bf99f | 390 | while (d->prod[m] != d->cons[m] && |
6f97dba0 AL |
391 | d->chr_can_read[m] && |
392 | d->chr_can_read[m](d->ext_opaque[m])) { | |
393 | d->chr_read[m](d->ext_opaque[m], | |
a80bf99f | 394 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
6f97dba0 AL |
395 | } |
396 | } | |
397 | ||
398 | static int mux_chr_can_read(void *opaque) | |
399 | { | |
400 | CharDriverState *chr = opaque; | |
401 | MuxDriver *d = chr->opaque; | |
799f1f23 | 402 | int m = d->focus; |
6f97dba0 | 403 | |
a80bf99f | 404 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
6f97dba0 | 405 | return 1; |
a80bf99f AL |
406 | if (d->chr_can_read[m]) |
407 | return d->chr_can_read[m](d->ext_opaque[m]); | |
6f97dba0 AL |
408 | return 0; |
409 | } | |
410 | ||
411 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) | |
412 | { | |
413 | CharDriverState *chr = opaque; | |
414 | MuxDriver *d = chr->opaque; | |
799f1f23 | 415 | int m = d->focus; |
6f97dba0 AL |
416 | int i; |
417 | ||
418 | mux_chr_accept_input (opaque); | |
419 | ||
420 | for(i = 0; i < size; i++) | |
421 | if (mux_proc_byte(chr, d, buf[i])) { | |
a80bf99f | 422 | if (d->prod[m] == d->cons[m] && |
6f97dba0 AL |
423 | d->chr_can_read[m] && |
424 | d->chr_can_read[m](d->ext_opaque[m])) | |
425 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); | |
426 | else | |
a80bf99f | 427 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
6f97dba0 AL |
428 | } |
429 | } | |
430 | ||
431 | static void mux_chr_event(void *opaque, int event) | |
432 | { | |
433 | CharDriverState *chr = opaque; | |
434 | MuxDriver *d = chr->opaque; | |
435 | int i; | |
436 | ||
437 | /* Send the event to all registered listeners */ | |
438 | for (i = 0; i < d->mux_cnt; i++) | |
2724b180 | 439 | mux_chr_send_event(d, i, event); |
6f97dba0 AL |
440 | } |
441 | ||
442 | static void mux_chr_update_read_handler(CharDriverState *chr) | |
443 | { | |
444 | MuxDriver *d = chr->opaque; | |
445 | ||
446 | if (d->mux_cnt >= MAX_MUX) { | |
447 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); | |
448 | return; | |
449 | } | |
450 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; | |
451 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; | |
452 | d->chr_read[d->mux_cnt] = chr->chr_read; | |
453 | d->chr_event[d->mux_cnt] = chr->chr_event; | |
454 | /* Fix up the real driver with mux routines */ | |
455 | if (d->mux_cnt == 0) { | |
456 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, | |
457 | mux_chr_event, chr); | |
458 | } | |
799f1f23 GH |
459 | if (d->focus != -1) { |
460 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); | |
a7aec5da | 461 | } |
799f1f23 | 462 | d->focus = d->mux_cnt; |
6f97dba0 | 463 | d->mux_cnt++; |
799f1f23 | 464 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
6f97dba0 AL |
465 | } |
466 | ||
467 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) | |
468 | { | |
469 | CharDriverState *chr; | |
470 | MuxDriver *d; | |
471 | ||
7267c094 AL |
472 | chr = g_malloc0(sizeof(CharDriverState)); |
473 | d = g_malloc0(sizeof(MuxDriver)); | |
6f97dba0 AL |
474 | |
475 | chr->opaque = d; | |
476 | d->drv = drv; | |
799f1f23 | 477 | d->focus = -1; |
6f97dba0 AL |
478 | chr->chr_write = mux_chr_write; |
479 | chr->chr_update_read_handler = mux_chr_update_read_handler; | |
480 | chr->chr_accept_input = mux_chr_accept_input; | |
7c32c4fe HG |
481 | /* Frontend guest-open / -close notification is not support with muxes */ |
482 | chr->chr_guest_open = NULL; | |
483 | chr->chr_guest_close = NULL; | |
73cdf3f2 AG |
484 | |
485 | /* Muxes are always open on creation */ | |
486 | qemu_chr_generic_open(chr); | |
487 | ||
6f97dba0 AL |
488 | return chr; |
489 | } | |
490 | ||
491 | ||
492 | #ifdef _WIN32 | |
d247d25f | 493 | int send_all(int fd, const void *buf, int len1) |
6f97dba0 AL |
494 | { |
495 | int ret, len; | |
496 | ||
497 | len = len1; | |
498 | while (len > 0) { | |
499 | ret = send(fd, buf, len, 0); | |
500 | if (ret < 0) { | |
6f97dba0 AL |
501 | errno = WSAGetLastError(); |
502 | if (errno != WSAEWOULDBLOCK) { | |
503 | return -1; | |
504 | } | |
505 | } else if (ret == 0) { | |
506 | break; | |
507 | } else { | |
508 | buf += ret; | |
509 | len -= ret; | |
510 | } | |
511 | } | |
512 | return len1 - len; | |
513 | } | |
514 | ||
515 | #else | |
516 | ||
5fc9cfed | 517 | int send_all(int fd, const void *_buf, int len1) |
6f97dba0 AL |
518 | { |
519 | int ret, len; | |
5fc9cfed | 520 | const uint8_t *buf = _buf; |
6f97dba0 AL |
521 | |
522 | len = len1; | |
523 | while (len > 0) { | |
524 | ret = write(fd, buf, len); | |
525 | if (ret < 0) { | |
526 | if (errno != EINTR && errno != EAGAIN) | |
527 | return -1; | |
528 | } else if (ret == 0) { | |
529 | break; | |
530 | } else { | |
531 | buf += ret; | |
532 | len -= ret; | |
533 | } | |
534 | } | |
535 | return len1 - len; | |
536 | } | |
6f97dba0 AL |
537 | #endif /* !_WIN32 */ |
538 | ||
db418a0a FC |
539 | #define STDIO_MAX_CLIENTS 1 |
540 | static int stdio_nb_clients; | |
541 | ||
6f97dba0 AL |
542 | #ifndef _WIN32 |
543 | ||
544 | typedef struct { | |
545 | int fd_in, fd_out; | |
546 | int max_size; | |
547 | } FDCharDriver; | |
548 | ||
6f97dba0 AL |
549 | |
550 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
551 | { | |
552 | FDCharDriver *s = chr->opaque; | |
553 | return send_all(s->fd_out, buf, len); | |
554 | } | |
555 | ||
556 | static int fd_chr_read_poll(void *opaque) | |
557 | { | |
558 | CharDriverState *chr = opaque; | |
559 | FDCharDriver *s = chr->opaque; | |
560 | ||
909cda12 | 561 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
562 | return s->max_size; |
563 | } | |
564 | ||
565 | static void fd_chr_read(void *opaque) | |
566 | { | |
567 | CharDriverState *chr = opaque; | |
568 | FDCharDriver *s = chr->opaque; | |
569 | int size, len; | |
9bd7854e | 570 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
571 | |
572 | len = sizeof(buf); | |
573 | if (len > s->max_size) | |
574 | len = s->max_size; | |
575 | if (len == 0) | |
576 | return; | |
577 | size = read(s->fd_in, buf, len); | |
578 | if (size == 0) { | |
579 | /* FD has been closed. Remove it from the active list. */ | |
580 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); | |
a425d23f | 581 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
582 | return; |
583 | } | |
584 | if (size > 0) { | |
fa5efccb | 585 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
586 | } |
587 | } | |
588 | ||
589 | static void fd_chr_update_read_handler(CharDriverState *chr) | |
590 | { | |
591 | FDCharDriver *s = chr->opaque; | |
592 | ||
593 | if (s->fd_in >= 0) { | |
993fbfdb | 594 | if (display_type == DT_NOGRAPHIC && s->fd_in == 0) { |
6f97dba0 AL |
595 | } else { |
596 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, | |
597 | fd_chr_read, NULL, chr); | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | static void fd_chr_close(struct CharDriverState *chr) | |
603 | { | |
604 | FDCharDriver *s = chr->opaque; | |
605 | ||
606 | if (s->fd_in >= 0) { | |
993fbfdb | 607 | if (display_type == DT_NOGRAPHIC && s->fd_in == 0) { |
6f97dba0 AL |
608 | } else { |
609 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); | |
610 | } | |
611 | } | |
612 | ||
7267c094 | 613 | g_free(s); |
a425d23f | 614 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
615 | } |
616 | ||
617 | /* open a character device to a unix fd */ | |
618 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) | |
619 | { | |
620 | CharDriverState *chr; | |
621 | FDCharDriver *s; | |
622 | ||
7267c094 AL |
623 | chr = g_malloc0(sizeof(CharDriverState)); |
624 | s = g_malloc0(sizeof(FDCharDriver)); | |
6f97dba0 AL |
625 | s->fd_in = fd_in; |
626 | s->fd_out = fd_out; | |
627 | chr->opaque = s; | |
628 | chr->chr_write = fd_chr_write; | |
629 | chr->chr_update_read_handler = fd_chr_update_read_handler; | |
630 | chr->chr_close = fd_chr_close; | |
631 | ||
127338e6 | 632 | qemu_chr_generic_open(chr); |
6f97dba0 AL |
633 | |
634 | return chr; | |
635 | } | |
636 | ||
1f51470d | 637 | static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts) |
6f97dba0 AL |
638 | { |
639 | int fd_out; | |
640 | ||
40ff6d7e | 641 | TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"), |
7d31544f | 642 | O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666)); |
a89dd6c3 | 643 | if (fd_out < 0) { |
1f51470d | 644 | return NULL; |
a89dd6c3 | 645 | } |
1f51470d | 646 | return qemu_chr_open_fd(-1, fd_out); |
6f97dba0 AL |
647 | } |
648 | ||
1f51470d | 649 | static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts) |
6f97dba0 AL |
650 | { |
651 | int fd_in, fd_out; | |
652 | char filename_in[256], filename_out[256]; | |
7d31544f GH |
653 | const char *filename = qemu_opt_get(opts, "path"); |
654 | ||
655 | if (filename == NULL) { | |
656 | fprintf(stderr, "chardev: pipe: no filename given\n"); | |
1f51470d | 657 | return NULL; |
7d31544f | 658 | } |
6f97dba0 AL |
659 | |
660 | snprintf(filename_in, 256, "%s.in", filename); | |
661 | snprintf(filename_out, 256, "%s.out", filename); | |
40ff6d7e KW |
662 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
663 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); | |
6f97dba0 AL |
664 | if (fd_in < 0 || fd_out < 0) { |
665 | if (fd_in >= 0) | |
666 | close(fd_in); | |
667 | if (fd_out >= 0) | |
668 | close(fd_out); | |
b181e047 | 669 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
a89dd6c3 | 670 | if (fd_in < 0) { |
1f51470d | 671 | return NULL; |
a89dd6c3 | 672 | } |
6f97dba0 | 673 | } |
1f51470d | 674 | return qemu_chr_open_fd(fd_in, fd_out); |
6f97dba0 AL |
675 | } |
676 | ||
677 | ||
678 | /* for STDIO, we handle the case where several clients use it | |
679 | (nographic mode) */ | |
680 | ||
681 | #define TERM_FIFO_MAX_SIZE 1 | |
682 | ||
683 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; | |
684 | static int term_fifo_size; | |
685 | ||
686 | static int stdio_read_poll(void *opaque) | |
687 | { | |
688 | CharDriverState *chr = opaque; | |
689 | ||
690 | /* try to flush the queue if needed */ | |
909cda12 | 691 | if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) { |
fa5efccb | 692 | qemu_chr_be_write(chr, term_fifo, 1); |
6f97dba0 AL |
693 | term_fifo_size = 0; |
694 | } | |
695 | /* see if we can absorb more chars */ | |
696 | if (term_fifo_size == 0) | |
697 | return 1; | |
698 | else | |
699 | return 0; | |
700 | } | |
701 | ||
702 | static void stdio_read(void *opaque) | |
703 | { | |
704 | int size; | |
705 | uint8_t buf[1]; | |
706 | CharDriverState *chr = opaque; | |
707 | ||
708 | size = read(0, buf, 1); | |
709 | if (size == 0) { | |
710 | /* stdin has been closed. Remove it from the active list. */ | |
711 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); | |
a425d23f | 712 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
713 | return; |
714 | } | |
715 | if (size > 0) { | |
909cda12 | 716 | if (qemu_chr_be_can_write(chr) > 0) { |
fa5efccb | 717 | qemu_chr_be_write(chr, buf, 1); |
6f97dba0 AL |
718 | } else if (term_fifo_size == 0) { |
719 | term_fifo[term_fifo_size++] = buf[0]; | |
720 | } | |
721 | } | |
722 | } | |
723 | ||
724 | /* init terminal so that we can grab keys */ | |
725 | static struct termios oldtty; | |
726 | static int old_fd0_flags; | |
bb002513 | 727 | static bool stdio_allow_signal; |
6f97dba0 AL |
728 | |
729 | static void term_exit(void) | |
730 | { | |
731 | tcsetattr (0, TCSANOW, &oldtty); | |
732 | fcntl(0, F_SETFL, old_fd0_flags); | |
733 | } | |
734 | ||
bb002513 | 735 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
6f97dba0 AL |
736 | { |
737 | struct termios tty; | |
738 | ||
0369364b | 739 | tty = oldtty; |
bb002513 PB |
740 | if (!echo) { |
741 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
6f97dba0 | 742 | |INLCR|IGNCR|ICRNL|IXON); |
bb002513 PB |
743 | tty.c_oflag |= OPOST; |
744 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
745 | tty.c_cflag &= ~(CSIZE|PARENB); | |
746 | tty.c_cflag |= CS8; | |
747 | tty.c_cc[VMIN] = 1; | |
748 | tty.c_cc[VTIME] = 0; | |
749 | } | |
6f97dba0 | 750 | /* if graphical mode, we allow Ctrl-C handling */ |
bb002513 | 751 | if (!stdio_allow_signal) |
6f97dba0 | 752 | tty.c_lflag &= ~ISIG; |
6f97dba0 AL |
753 | |
754 | tcsetattr (0, TCSANOW, &tty); | |
6f97dba0 AL |
755 | } |
756 | ||
757 | static void qemu_chr_close_stdio(struct CharDriverState *chr) | |
758 | { | |
759 | term_exit(); | |
760 | stdio_nb_clients--; | |
761 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); | |
762 | fd_chr_close(chr); | |
763 | } | |
764 | ||
1f51470d | 765 | static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts) |
6f97dba0 AL |
766 | { |
767 | CharDriverState *chr; | |
768 | ||
a89dd6c3 | 769 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) { |
1f51470d | 770 | return NULL; |
a89dd6c3 | 771 | } |
0369364b PB |
772 | if (stdio_nb_clients == 0) { |
773 | old_fd0_flags = fcntl(0, F_GETFL); | |
774 | tcgetattr (0, &oldtty); | |
775 | fcntl(0, F_SETFL, O_NONBLOCK); | |
776 | atexit(term_exit); | |
777 | } | |
778 | ||
6f97dba0 AL |
779 | chr = qemu_chr_open_fd(0, 1); |
780 | chr->chr_close = qemu_chr_close_stdio; | |
bb002513 | 781 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
6f97dba0 AL |
782 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
783 | stdio_nb_clients++; | |
bb002513 PB |
784 | stdio_allow_signal = qemu_opt_get_bool(opts, "signal", |
785 | display_type != DT_NOGRAPHIC); | |
15f31519 | 786 | qemu_chr_fe_set_echo(chr, false); |
6f97dba0 | 787 | |
1f51470d | 788 | return chr; |
6f97dba0 AL |
789 | } |
790 | ||
791 | #ifdef __sun__ | |
792 | /* Once Solaris has openpty(), this is going to be removed. */ | |
3f4cb3d3 BS |
793 | static int openpty(int *amaster, int *aslave, char *name, |
794 | struct termios *termp, struct winsize *winp) | |
6f97dba0 AL |
795 | { |
796 | const char *slave; | |
797 | int mfd = -1, sfd = -1; | |
798 | ||
799 | *amaster = *aslave = -1; | |
800 | ||
801 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); | |
802 | if (mfd < 0) | |
803 | goto err; | |
804 | ||
805 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) | |
806 | goto err; | |
807 | ||
808 | if ((slave = ptsname(mfd)) == NULL) | |
809 | goto err; | |
810 | ||
811 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) | |
812 | goto err; | |
813 | ||
814 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || | |
815 | (termp != NULL && tcgetattr(sfd, termp) < 0)) | |
816 | goto err; | |
817 | ||
818 | if (amaster) | |
819 | *amaster = mfd; | |
820 | if (aslave) | |
821 | *aslave = sfd; | |
822 | if (winp) | |
823 | ioctl(sfd, TIOCSWINSZ, winp); | |
824 | ||
825 | return 0; | |
826 | ||
827 | err: | |
828 | if (sfd != -1) | |
829 | close(sfd); | |
830 | close(mfd); | |
831 | return -1; | |
832 | } | |
833 | ||
3f4cb3d3 | 834 | static void cfmakeraw (struct termios *termios_p) |
6f97dba0 AL |
835 | { |
836 | termios_p->c_iflag &= | |
837 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); | |
838 | termios_p->c_oflag &= ~OPOST; | |
839 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); | |
840 | termios_p->c_cflag &= ~(CSIZE|PARENB); | |
841 | termios_p->c_cflag |= CS8; | |
842 | ||
843 | termios_p->c_cc[VMIN] = 0; | |
844 | termios_p->c_cc[VTIME] = 0; | |
845 | } | |
846 | #endif | |
847 | ||
848 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ | |
a167ba50 AJ |
849 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
850 | || defined(__GLIBC__) | |
6f97dba0 AL |
851 | |
852 | typedef struct { | |
853 | int fd; | |
854 | int connected; | |
855 | int polling; | |
856 | int read_bytes; | |
857 | QEMUTimer *timer; | |
858 | } PtyCharDriver; | |
859 | ||
860 | static void pty_chr_update_read_handler(CharDriverState *chr); | |
861 | static void pty_chr_state(CharDriverState *chr, int connected); | |
862 | ||
863 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
864 | { | |
865 | PtyCharDriver *s = chr->opaque; | |
866 | ||
867 | if (!s->connected) { | |
868 | /* guest sends data, check for (re-)connect */ | |
869 | pty_chr_update_read_handler(chr); | |
870 | return 0; | |
871 | } | |
872 | return send_all(s->fd, buf, len); | |
873 | } | |
874 | ||
875 | static int pty_chr_read_poll(void *opaque) | |
876 | { | |
877 | CharDriverState *chr = opaque; | |
878 | PtyCharDriver *s = chr->opaque; | |
879 | ||
909cda12 | 880 | s->read_bytes = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
881 | return s->read_bytes; |
882 | } | |
883 | ||
884 | static void pty_chr_read(void *opaque) | |
885 | { | |
886 | CharDriverState *chr = opaque; | |
887 | PtyCharDriver *s = chr->opaque; | |
888 | int size, len; | |
9bd7854e | 889 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
890 | |
891 | len = sizeof(buf); | |
892 | if (len > s->read_bytes) | |
893 | len = s->read_bytes; | |
894 | if (len == 0) | |
895 | return; | |
896 | size = read(s->fd, buf, len); | |
897 | if ((size == -1 && errno == EIO) || | |
898 | (size == 0)) { | |
899 | pty_chr_state(chr, 0); | |
900 | return; | |
901 | } | |
902 | if (size > 0) { | |
903 | pty_chr_state(chr, 1); | |
fa5efccb | 904 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
905 | } |
906 | } | |
907 | ||
908 | static void pty_chr_update_read_handler(CharDriverState *chr) | |
909 | { | |
910 | PtyCharDriver *s = chr->opaque; | |
911 | ||
912 | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, | |
913 | pty_chr_read, NULL, chr); | |
914 | s->polling = 1; | |
915 | /* | |
916 | * Short timeout here: just need wait long enougth that qemu makes | |
917 | * it through the poll loop once. When reconnected we want a | |
918 | * short timeout so we notice it almost instantly. Otherwise | |
919 | * read() gives us -EIO instantly, making pty_chr_state() reset the | |
920 | * timeout to the normal (much longer) poll interval before the | |
921 | * timer triggers. | |
922 | */ | |
7bd427d8 | 923 | qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10); |
6f97dba0 AL |
924 | } |
925 | ||
926 | static void pty_chr_state(CharDriverState *chr, int connected) | |
927 | { | |
928 | PtyCharDriver *s = chr->opaque; | |
929 | ||
930 | if (!connected) { | |
931 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
932 | s->connected = 0; | |
933 | s->polling = 0; | |
934 | /* (re-)connect poll interval for idle guests: once per second. | |
935 | * We check more frequently in case the guests sends data to | |
936 | * the virtual device linked to our pty. */ | |
7bd427d8 | 937 | qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000); |
6f97dba0 AL |
938 | } else { |
939 | if (!s->connected) | |
127338e6 | 940 | qemu_chr_generic_open(chr); |
6f97dba0 AL |
941 | s->connected = 1; |
942 | } | |
943 | } | |
944 | ||
945 | static void pty_chr_timer(void *opaque) | |
946 | { | |
947 | struct CharDriverState *chr = opaque; | |
948 | PtyCharDriver *s = chr->opaque; | |
949 | ||
950 | if (s->connected) | |
951 | return; | |
952 | if (s->polling) { | |
953 | /* If we arrive here without polling being cleared due | |
954 | * read returning -EIO, then we are (re-)connected */ | |
955 | pty_chr_state(chr, 1); | |
956 | return; | |
957 | } | |
958 | ||
959 | /* Next poll ... */ | |
960 | pty_chr_update_read_handler(chr); | |
961 | } | |
962 | ||
963 | static void pty_chr_close(struct CharDriverState *chr) | |
964 | { | |
965 | PtyCharDriver *s = chr->opaque; | |
966 | ||
967 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
968 | close(s->fd); | |
819f56b7 AL |
969 | qemu_del_timer(s->timer); |
970 | qemu_free_timer(s->timer); | |
7267c094 | 971 | g_free(s); |
a425d23f | 972 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
973 | } |
974 | ||
1f51470d | 975 | static CharDriverState *qemu_chr_open_pty(QemuOpts *opts) |
6f97dba0 AL |
976 | { |
977 | CharDriverState *chr; | |
978 | PtyCharDriver *s; | |
979 | struct termios tty; | |
a4e26048 | 980 | int master_fd, slave_fd, len; |
c5e97233 | 981 | #if defined(__OpenBSD__) || defined(__DragonFly__) |
6f97dba0 AL |
982 | char pty_name[PATH_MAX]; |
983 | #define q_ptsname(x) pty_name | |
984 | #else | |
985 | char *pty_name = NULL; | |
986 | #define q_ptsname(x) ptsname(x) | |
987 | #endif | |
988 | ||
a4e26048 | 989 | if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
1f51470d | 990 | return NULL; |
6f97dba0 AL |
991 | } |
992 | ||
993 | /* Set raw attributes on the pty. */ | |
c3b972c3 | 994 | tcgetattr(slave_fd, &tty); |
6f97dba0 AL |
995 | cfmakeraw(&tty); |
996 | tcsetattr(slave_fd, TCSAFLUSH, &tty); | |
997 | close(slave_fd); | |
998 | ||
a4e26048 MA |
999 | chr = g_malloc0(sizeof(CharDriverState)); |
1000 | ||
1001 | len = strlen(q_ptsname(master_fd)) + 5; | |
7267c094 | 1002 | chr->filename = g_malloc(len); |
a4e26048 MA |
1003 | snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd)); |
1004 | qemu_opt_set(opts, "path", q_ptsname(master_fd)); | |
1005 | fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd)); | |
6f97dba0 | 1006 | |
a4e26048 | 1007 | s = g_malloc0(sizeof(PtyCharDriver)); |
6f97dba0 AL |
1008 | chr->opaque = s; |
1009 | chr->chr_write = pty_chr_write; | |
1010 | chr->chr_update_read_handler = pty_chr_update_read_handler; | |
1011 | chr->chr_close = pty_chr_close; | |
1012 | ||
a4e26048 | 1013 | s->fd = master_fd; |
7bd427d8 | 1014 | s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr); |
6f97dba0 | 1015 | |
1f51470d | 1016 | return chr; |
6f97dba0 AL |
1017 | } |
1018 | ||
1019 | static void tty_serial_init(int fd, int speed, | |
1020 | int parity, int data_bits, int stop_bits) | |
1021 | { | |
1022 | struct termios tty; | |
1023 | speed_t spd; | |
1024 | ||
1025 | #if 0 | |
1026 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", | |
1027 | speed, parity, data_bits, stop_bits); | |
1028 | #endif | |
1029 | tcgetattr (fd, &tty); | |
1030 | ||
45eea13b SW |
1031 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
1032 | speed = speed * 10 / 11; | |
1033 | do { | |
1034 | check_speed(50); | |
1035 | check_speed(75); | |
1036 | check_speed(110); | |
1037 | check_speed(134); | |
1038 | check_speed(150); | |
1039 | check_speed(200); | |
1040 | check_speed(300); | |
1041 | check_speed(600); | |
1042 | check_speed(1200); | |
1043 | check_speed(1800); | |
1044 | check_speed(2400); | |
1045 | check_speed(4800); | |
1046 | check_speed(9600); | |
1047 | check_speed(19200); | |
1048 | check_speed(38400); | |
1049 | /* Non-Posix values follow. They may be unsupported on some systems. */ | |
1050 | check_speed(57600); | |
1051 | check_speed(115200); | |
1052 | #ifdef B230400 | |
1053 | check_speed(230400); | |
1054 | #endif | |
1055 | #ifdef B460800 | |
1056 | check_speed(460800); | |
1057 | #endif | |
1058 | #ifdef B500000 | |
1059 | check_speed(500000); | |
1060 | #endif | |
1061 | #ifdef B576000 | |
1062 | check_speed(576000); | |
1063 | #endif | |
1064 | #ifdef B921600 | |
1065 | check_speed(921600); | |
1066 | #endif | |
1067 | #ifdef B1000000 | |
1068 | check_speed(1000000); | |
1069 | #endif | |
1070 | #ifdef B1152000 | |
1071 | check_speed(1152000); | |
1072 | #endif | |
1073 | #ifdef B1500000 | |
1074 | check_speed(1500000); | |
1075 | #endif | |
1076 | #ifdef B2000000 | |
1077 | check_speed(2000000); | |
1078 | #endif | |
1079 | #ifdef B2500000 | |
1080 | check_speed(2500000); | |
1081 | #endif | |
1082 | #ifdef B3000000 | |
1083 | check_speed(3000000); | |
1084 | #endif | |
1085 | #ifdef B3500000 | |
1086 | check_speed(3500000); | |
1087 | #endif | |
1088 | #ifdef B4000000 | |
1089 | check_speed(4000000); | |
1090 | #endif | |
6f97dba0 | 1091 | spd = B115200; |
45eea13b | 1092 | } while (0); |
6f97dba0 AL |
1093 | |
1094 | cfsetispeed(&tty, spd); | |
1095 | cfsetospeed(&tty, spd); | |
1096 | ||
1097 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
1098 | |INLCR|IGNCR|ICRNL|IXON); | |
1099 | tty.c_oflag |= OPOST; | |
1100 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); | |
1101 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); | |
1102 | switch(data_bits) { | |
1103 | default: | |
1104 | case 8: | |
1105 | tty.c_cflag |= CS8; | |
1106 | break; | |
1107 | case 7: | |
1108 | tty.c_cflag |= CS7; | |
1109 | break; | |
1110 | case 6: | |
1111 | tty.c_cflag |= CS6; | |
1112 | break; | |
1113 | case 5: | |
1114 | tty.c_cflag |= CS5; | |
1115 | break; | |
1116 | } | |
1117 | switch(parity) { | |
1118 | default: | |
1119 | case 'N': | |
1120 | break; | |
1121 | case 'E': | |
1122 | tty.c_cflag |= PARENB; | |
1123 | break; | |
1124 | case 'O': | |
1125 | tty.c_cflag |= PARENB | PARODD; | |
1126 | break; | |
1127 | } | |
1128 | if (stop_bits == 2) | |
1129 | tty.c_cflag |= CSTOPB; | |
1130 | ||
1131 | tcsetattr (fd, TCSANOW, &tty); | |
1132 | } | |
1133 | ||
1134 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1135 | { | |
1136 | FDCharDriver *s = chr->opaque; | |
1137 | ||
1138 | switch(cmd) { | |
1139 | case CHR_IOCTL_SERIAL_SET_PARAMS: | |
1140 | { | |
1141 | QEMUSerialSetParams *ssp = arg; | |
1142 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, | |
1143 | ssp->data_bits, ssp->stop_bits); | |
1144 | } | |
1145 | break; | |
1146 | case CHR_IOCTL_SERIAL_SET_BREAK: | |
1147 | { | |
1148 | int enable = *(int *)arg; | |
1149 | if (enable) | |
1150 | tcsendbreak(s->fd_in, 1); | |
1151 | } | |
1152 | break; | |
1153 | case CHR_IOCTL_SERIAL_GET_TIOCM: | |
1154 | { | |
1155 | int sarg = 0; | |
1156 | int *targ = (int *)arg; | |
1157 | ioctl(s->fd_in, TIOCMGET, &sarg); | |
1158 | *targ = 0; | |
b4abdfa4 | 1159 | if (sarg & TIOCM_CTS) |
6f97dba0 | 1160 | *targ |= CHR_TIOCM_CTS; |
b4abdfa4 | 1161 | if (sarg & TIOCM_CAR) |
6f97dba0 | 1162 | *targ |= CHR_TIOCM_CAR; |
b4abdfa4 | 1163 | if (sarg & TIOCM_DSR) |
6f97dba0 | 1164 | *targ |= CHR_TIOCM_DSR; |
b4abdfa4 | 1165 | if (sarg & TIOCM_RI) |
6f97dba0 | 1166 | *targ |= CHR_TIOCM_RI; |
b4abdfa4 | 1167 | if (sarg & TIOCM_DTR) |
6f97dba0 | 1168 | *targ |= CHR_TIOCM_DTR; |
b4abdfa4 | 1169 | if (sarg & TIOCM_RTS) |
6f97dba0 AL |
1170 | *targ |= CHR_TIOCM_RTS; |
1171 | } | |
1172 | break; | |
1173 | case CHR_IOCTL_SERIAL_SET_TIOCM: | |
1174 | { | |
1175 | int sarg = *(int *)arg; | |
1176 | int targ = 0; | |
b4abdfa4 AJ |
1177 | ioctl(s->fd_in, TIOCMGET, &targ); |
1178 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR | |
1179 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); | |
1180 | if (sarg & CHR_TIOCM_CTS) | |
1181 | targ |= TIOCM_CTS; | |
1182 | if (sarg & CHR_TIOCM_CAR) | |
1183 | targ |= TIOCM_CAR; | |
1184 | if (sarg & CHR_TIOCM_DSR) | |
1185 | targ |= TIOCM_DSR; | |
1186 | if (sarg & CHR_TIOCM_RI) | |
1187 | targ |= TIOCM_RI; | |
1188 | if (sarg & CHR_TIOCM_DTR) | |
6f97dba0 | 1189 | targ |= TIOCM_DTR; |
b4abdfa4 | 1190 | if (sarg & CHR_TIOCM_RTS) |
6f97dba0 AL |
1191 | targ |= TIOCM_RTS; |
1192 | ioctl(s->fd_in, TIOCMSET, &targ); | |
1193 | } | |
1194 | break; | |
1195 | default: | |
1196 | return -ENOTSUP; | |
1197 | } | |
1198 | return 0; | |
1199 | } | |
1200 | ||
4266a134 DA |
1201 | static void qemu_chr_close_tty(CharDriverState *chr) |
1202 | { | |
1203 | FDCharDriver *s = chr->opaque; | |
1204 | int fd = -1; | |
1205 | ||
1206 | if (s) { | |
1207 | fd = s->fd_in; | |
1208 | } | |
1209 | ||
1210 | fd_chr_close(chr); | |
1211 | ||
1212 | if (fd >= 0) { | |
1213 | close(fd); | |
1214 | } | |
1215 | } | |
1216 | ||
1f51470d | 1217 | static CharDriverState *qemu_chr_open_tty(QemuOpts *opts) |
6f97dba0 | 1218 | { |
48b76496 | 1219 | const char *filename = qemu_opt_get(opts, "path"); |
6f97dba0 AL |
1220 | CharDriverState *chr; |
1221 | int fd; | |
1222 | ||
b181e047 | 1223 | TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK)); |
afc535ac | 1224 | if (fd < 0) { |
1f51470d | 1225 | return NULL; |
afc535ac | 1226 | } |
6f97dba0 AL |
1227 | tty_serial_init(fd, 115200, 'N', 8, 1); |
1228 | chr = qemu_chr_open_fd(fd, fd); | |
6f97dba0 | 1229 | chr->chr_ioctl = tty_serial_ioctl; |
4266a134 | 1230 | chr->chr_close = qemu_chr_close_tty; |
1f51470d | 1231 | return chr; |
6f97dba0 AL |
1232 | } |
1233 | #else /* ! __linux__ && ! __sun__ */ | |
1f51470d | 1234 | static CharDriverState *qemu_chr_open_pty(QemuOpts *opts) |
6f97dba0 | 1235 | { |
1f51470d | 1236 | return NULL; |
6f97dba0 AL |
1237 | } |
1238 | #endif /* __linux__ || __sun__ */ | |
1239 | ||
1240 | #if defined(__linux__) | |
1241 | typedef struct { | |
1242 | int fd; | |
1243 | int mode; | |
1244 | } ParallelCharDriver; | |
1245 | ||
1246 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) | |
1247 | { | |
1248 | if (s->mode != mode) { | |
1249 | int m = mode; | |
1250 | if (ioctl(s->fd, PPSETMODE, &m) < 0) | |
1251 | return 0; | |
1252 | s->mode = mode; | |
1253 | } | |
1254 | return 1; | |
1255 | } | |
1256 | ||
1257 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1258 | { | |
1259 | ParallelCharDriver *drv = chr->opaque; | |
1260 | int fd = drv->fd; | |
1261 | uint8_t b; | |
1262 | ||
1263 | switch(cmd) { | |
1264 | case CHR_IOCTL_PP_READ_DATA: | |
1265 | if (ioctl(fd, PPRDATA, &b) < 0) | |
1266 | return -ENOTSUP; | |
1267 | *(uint8_t *)arg = b; | |
1268 | break; | |
1269 | case CHR_IOCTL_PP_WRITE_DATA: | |
1270 | b = *(uint8_t *)arg; | |
1271 | if (ioctl(fd, PPWDATA, &b) < 0) | |
1272 | return -ENOTSUP; | |
1273 | break; | |
1274 | case CHR_IOCTL_PP_READ_CONTROL: | |
1275 | if (ioctl(fd, PPRCONTROL, &b) < 0) | |
1276 | return -ENOTSUP; | |
1277 | /* Linux gives only the lowest bits, and no way to know data | |
1278 | direction! For better compatibility set the fixed upper | |
1279 | bits. */ | |
1280 | *(uint8_t *)arg = b | 0xc0; | |
1281 | break; | |
1282 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1283 | b = *(uint8_t *)arg; | |
1284 | if (ioctl(fd, PPWCONTROL, &b) < 0) | |
1285 | return -ENOTSUP; | |
1286 | break; | |
1287 | case CHR_IOCTL_PP_READ_STATUS: | |
1288 | if (ioctl(fd, PPRSTATUS, &b) < 0) | |
1289 | return -ENOTSUP; | |
1290 | *(uint8_t *)arg = b; | |
1291 | break; | |
1292 | case CHR_IOCTL_PP_DATA_DIR: | |
1293 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) | |
1294 | return -ENOTSUP; | |
1295 | break; | |
1296 | case CHR_IOCTL_PP_EPP_READ_ADDR: | |
1297 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1298 | struct ParallelIOArg *parg = arg; | |
1299 | int n = read(fd, parg->buffer, parg->count); | |
1300 | if (n != parg->count) { | |
1301 | return -EIO; | |
1302 | } | |
1303 | } | |
1304 | break; | |
1305 | case CHR_IOCTL_PP_EPP_READ: | |
1306 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1307 | struct ParallelIOArg *parg = arg; | |
1308 | int n = read(fd, parg->buffer, parg->count); | |
1309 | if (n != parg->count) { | |
1310 | return -EIO; | |
1311 | } | |
1312 | } | |
1313 | break; | |
1314 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: | |
1315 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1316 | struct ParallelIOArg *parg = arg; | |
1317 | int n = write(fd, parg->buffer, parg->count); | |
1318 | if (n != parg->count) { | |
1319 | return -EIO; | |
1320 | } | |
1321 | } | |
1322 | break; | |
1323 | case CHR_IOCTL_PP_EPP_WRITE: | |
1324 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1325 | struct ParallelIOArg *parg = arg; | |
1326 | int n = write(fd, parg->buffer, parg->count); | |
1327 | if (n != parg->count) { | |
1328 | return -EIO; | |
1329 | } | |
1330 | } | |
1331 | break; | |
1332 | default: | |
1333 | return -ENOTSUP; | |
1334 | } | |
1335 | return 0; | |
1336 | } | |
1337 | ||
1338 | static void pp_close(CharDriverState *chr) | |
1339 | { | |
1340 | ParallelCharDriver *drv = chr->opaque; | |
1341 | int fd = drv->fd; | |
1342 | ||
1343 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); | |
1344 | ioctl(fd, PPRELEASE); | |
1345 | close(fd); | |
7267c094 | 1346 | g_free(drv); |
a425d23f | 1347 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1348 | } |
1349 | ||
1f51470d | 1350 | static CharDriverState *qemu_chr_open_pp(QemuOpts *opts) |
6f97dba0 | 1351 | { |
48b76496 | 1352 | const char *filename = qemu_opt_get(opts, "path"); |
6f97dba0 AL |
1353 | CharDriverState *chr; |
1354 | ParallelCharDriver *drv; | |
1355 | int fd; | |
1356 | ||
b181e047 | 1357 | TFR(fd = qemu_open(filename, O_RDWR)); |
a89dd6c3 | 1358 | if (fd < 0) { |
1f51470d | 1359 | return NULL; |
a89dd6c3 | 1360 | } |
6f97dba0 AL |
1361 | |
1362 | if (ioctl(fd, PPCLAIM) < 0) { | |
1363 | close(fd); | |
1f51470d | 1364 | return NULL; |
6f97dba0 AL |
1365 | } |
1366 | ||
7267c094 | 1367 | drv = g_malloc0(sizeof(ParallelCharDriver)); |
6f97dba0 AL |
1368 | drv->fd = fd; |
1369 | drv->mode = IEEE1284_MODE_COMPAT; | |
1370 | ||
7267c094 | 1371 | chr = g_malloc0(sizeof(CharDriverState)); |
6f97dba0 AL |
1372 | chr->chr_write = null_chr_write; |
1373 | chr->chr_ioctl = pp_ioctl; | |
1374 | chr->chr_close = pp_close; | |
1375 | chr->opaque = drv; | |
1376 | ||
127338e6 | 1377 | qemu_chr_generic_open(chr); |
6f97dba0 | 1378 | |
1f51470d | 1379 | return chr; |
6f97dba0 AL |
1380 | } |
1381 | #endif /* __linux__ */ | |
1382 | ||
a167ba50 | 1383 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
6972f935 BS |
1384 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1385 | { | |
e0efb993 | 1386 | int fd = (int)(intptr_t)chr->opaque; |
6972f935 BS |
1387 | uint8_t b; |
1388 | ||
1389 | switch(cmd) { | |
1390 | case CHR_IOCTL_PP_READ_DATA: | |
1391 | if (ioctl(fd, PPIGDATA, &b) < 0) | |
1392 | return -ENOTSUP; | |
1393 | *(uint8_t *)arg = b; | |
1394 | break; | |
1395 | case CHR_IOCTL_PP_WRITE_DATA: | |
1396 | b = *(uint8_t *)arg; | |
1397 | if (ioctl(fd, PPISDATA, &b) < 0) | |
1398 | return -ENOTSUP; | |
1399 | break; | |
1400 | case CHR_IOCTL_PP_READ_CONTROL: | |
1401 | if (ioctl(fd, PPIGCTRL, &b) < 0) | |
1402 | return -ENOTSUP; | |
1403 | *(uint8_t *)arg = b; | |
1404 | break; | |
1405 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1406 | b = *(uint8_t *)arg; | |
1407 | if (ioctl(fd, PPISCTRL, &b) < 0) | |
1408 | return -ENOTSUP; | |
1409 | break; | |
1410 | case CHR_IOCTL_PP_READ_STATUS: | |
1411 | if (ioctl(fd, PPIGSTATUS, &b) < 0) | |
1412 | return -ENOTSUP; | |
1413 | *(uint8_t *)arg = b; | |
1414 | break; | |
1415 | default: | |
1416 | return -ENOTSUP; | |
1417 | } | |
1418 | return 0; | |
1419 | } | |
1420 | ||
1f51470d | 1421 | static CharDriverState *qemu_chr_open_pp(QemuOpts *opts) |
6972f935 | 1422 | { |
48b76496 | 1423 | const char *filename = qemu_opt_get(opts, "path"); |
6972f935 BS |
1424 | CharDriverState *chr; |
1425 | int fd; | |
1426 | ||
b181e047 | 1427 | fd = qemu_open(filename, O_RDWR); |
a89dd6c3 | 1428 | if (fd < 0) { |
1f51470d | 1429 | return NULL; |
a89dd6c3 | 1430 | } |
6972f935 | 1431 | |
7267c094 | 1432 | chr = g_malloc0(sizeof(CharDriverState)); |
e0efb993 | 1433 | chr->opaque = (void *)(intptr_t)fd; |
6972f935 BS |
1434 | chr->chr_write = null_chr_write; |
1435 | chr->chr_ioctl = pp_ioctl; | |
1f51470d | 1436 | return chr; |
6972f935 BS |
1437 | } |
1438 | #endif | |
1439 | ||
6f97dba0 AL |
1440 | #else /* _WIN32 */ |
1441 | ||
db418a0a FC |
1442 | static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS]; |
1443 | ||
6f97dba0 AL |
1444 | typedef struct { |
1445 | int max_size; | |
1446 | HANDLE hcom, hrecv, hsend; | |
1447 | OVERLAPPED orecv, osend; | |
1448 | BOOL fpipe; | |
1449 | DWORD len; | |
1450 | } WinCharState; | |
1451 | ||
db418a0a FC |
1452 | typedef struct { |
1453 | HANDLE hStdIn; | |
1454 | HANDLE hInputReadyEvent; | |
1455 | HANDLE hInputDoneEvent; | |
1456 | HANDLE hInputThread; | |
1457 | uint8_t win_stdio_buf; | |
1458 | } WinStdioCharState; | |
1459 | ||
6f97dba0 AL |
1460 | #define NSENDBUF 2048 |
1461 | #define NRECVBUF 2048 | |
1462 | #define MAXCONNECT 1 | |
1463 | #define NTIMEOUT 5000 | |
1464 | ||
1465 | static int win_chr_poll(void *opaque); | |
1466 | static int win_chr_pipe_poll(void *opaque); | |
1467 | ||
1468 | static void win_chr_close(CharDriverState *chr) | |
1469 | { | |
1470 | WinCharState *s = chr->opaque; | |
1471 | ||
1472 | if (s->hsend) { | |
1473 | CloseHandle(s->hsend); | |
1474 | s->hsend = NULL; | |
1475 | } | |
1476 | if (s->hrecv) { | |
1477 | CloseHandle(s->hrecv); | |
1478 | s->hrecv = NULL; | |
1479 | } | |
1480 | if (s->hcom) { | |
1481 | CloseHandle(s->hcom); | |
1482 | s->hcom = NULL; | |
1483 | } | |
1484 | if (s->fpipe) | |
1485 | qemu_del_polling_cb(win_chr_pipe_poll, chr); | |
1486 | else | |
1487 | qemu_del_polling_cb(win_chr_poll, chr); | |
793cbfb5 | 1488 | |
a425d23f | 1489 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1490 | } |
1491 | ||
1492 | static int win_chr_init(CharDriverState *chr, const char *filename) | |
1493 | { | |
1494 | WinCharState *s = chr->opaque; | |
1495 | COMMCONFIG comcfg; | |
1496 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; | |
1497 | COMSTAT comstat; | |
1498 | DWORD size; | |
1499 | DWORD err; | |
1500 | ||
1501 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1502 | if (!s->hsend) { | |
1503 | fprintf(stderr, "Failed CreateEvent\n"); | |
1504 | goto fail; | |
1505 | } | |
1506 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1507 | if (!s->hrecv) { | |
1508 | fprintf(stderr, "Failed CreateEvent\n"); | |
1509 | goto fail; | |
1510 | } | |
1511 | ||
1512 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, | |
1513 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); | |
1514 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1515 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); | |
1516 | s->hcom = NULL; | |
1517 | goto fail; | |
1518 | } | |
1519 | ||
1520 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { | |
1521 | fprintf(stderr, "Failed SetupComm\n"); | |
1522 | goto fail; | |
1523 | } | |
1524 | ||
1525 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); | |
1526 | size = sizeof(COMMCONFIG); | |
1527 | GetDefaultCommConfig(filename, &comcfg, &size); | |
1528 | comcfg.dcb.DCBlength = sizeof(DCB); | |
1529 | CommConfigDialog(filename, NULL, &comcfg); | |
1530 | ||
1531 | if (!SetCommState(s->hcom, &comcfg.dcb)) { | |
1532 | fprintf(stderr, "Failed SetCommState\n"); | |
1533 | goto fail; | |
1534 | } | |
1535 | ||
1536 | if (!SetCommMask(s->hcom, EV_ERR)) { | |
1537 | fprintf(stderr, "Failed SetCommMask\n"); | |
1538 | goto fail; | |
1539 | } | |
1540 | ||
1541 | cto.ReadIntervalTimeout = MAXDWORD; | |
1542 | if (!SetCommTimeouts(s->hcom, &cto)) { | |
1543 | fprintf(stderr, "Failed SetCommTimeouts\n"); | |
1544 | goto fail; | |
1545 | } | |
1546 | ||
1547 | if (!ClearCommError(s->hcom, &err, &comstat)) { | |
1548 | fprintf(stderr, "Failed ClearCommError\n"); | |
1549 | goto fail; | |
1550 | } | |
1551 | qemu_add_polling_cb(win_chr_poll, chr); | |
1552 | return 0; | |
1553 | ||
1554 | fail: | |
1555 | win_chr_close(chr); | |
1556 | return -1; | |
1557 | } | |
1558 | ||
1559 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) | |
1560 | { | |
1561 | WinCharState *s = chr->opaque; | |
1562 | DWORD len, ret, size, err; | |
1563 | ||
1564 | len = len1; | |
1565 | ZeroMemory(&s->osend, sizeof(s->osend)); | |
1566 | s->osend.hEvent = s->hsend; | |
1567 | while (len > 0) { | |
1568 | if (s->hsend) | |
1569 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); | |
1570 | else | |
1571 | ret = WriteFile(s->hcom, buf, len, &size, NULL); | |
1572 | if (!ret) { | |
1573 | err = GetLastError(); | |
1574 | if (err == ERROR_IO_PENDING) { | |
1575 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); | |
1576 | if (ret) { | |
1577 | buf += size; | |
1578 | len -= size; | |
1579 | } else { | |
1580 | break; | |
1581 | } | |
1582 | } else { | |
1583 | break; | |
1584 | } | |
1585 | } else { | |
1586 | buf += size; | |
1587 | len -= size; | |
1588 | } | |
1589 | } | |
1590 | return len1 - len; | |
1591 | } | |
1592 | ||
1593 | static int win_chr_read_poll(CharDriverState *chr) | |
1594 | { | |
1595 | WinCharState *s = chr->opaque; | |
1596 | ||
909cda12 | 1597 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1598 | return s->max_size; |
1599 | } | |
1600 | ||
1601 | static void win_chr_readfile(CharDriverState *chr) | |
1602 | { | |
1603 | WinCharState *s = chr->opaque; | |
1604 | int ret, err; | |
9bd7854e | 1605 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
1606 | DWORD size; |
1607 | ||
1608 | ZeroMemory(&s->orecv, sizeof(s->orecv)); | |
1609 | s->orecv.hEvent = s->hrecv; | |
1610 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); | |
1611 | if (!ret) { | |
1612 | err = GetLastError(); | |
1613 | if (err == ERROR_IO_PENDING) { | |
1614 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | if (size > 0) { | |
fa5efccb | 1619 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
1620 | } |
1621 | } | |
1622 | ||
1623 | static void win_chr_read(CharDriverState *chr) | |
1624 | { | |
1625 | WinCharState *s = chr->opaque; | |
1626 | ||
1627 | if (s->len > s->max_size) | |
1628 | s->len = s->max_size; | |
1629 | if (s->len == 0) | |
1630 | return; | |
1631 | ||
1632 | win_chr_readfile(chr); | |
1633 | } | |
1634 | ||
1635 | static int win_chr_poll(void *opaque) | |
1636 | { | |
1637 | CharDriverState *chr = opaque; | |
1638 | WinCharState *s = chr->opaque; | |
1639 | COMSTAT status; | |
1640 | DWORD comerr; | |
1641 | ||
1642 | ClearCommError(s->hcom, &comerr, &status); | |
1643 | if (status.cbInQue > 0) { | |
1644 | s->len = status.cbInQue; | |
1645 | win_chr_read_poll(chr); | |
1646 | win_chr_read(chr); | |
1647 | return 1; | |
1648 | } | |
1649 | return 0; | |
1650 | } | |
1651 | ||
1f51470d | 1652 | static CharDriverState *qemu_chr_open_win(QemuOpts *opts) |
6f97dba0 | 1653 | { |
48b76496 | 1654 | const char *filename = qemu_opt_get(opts, "path"); |
6f97dba0 AL |
1655 | CharDriverState *chr; |
1656 | WinCharState *s; | |
1657 | ||
7267c094 AL |
1658 | chr = g_malloc0(sizeof(CharDriverState)); |
1659 | s = g_malloc0(sizeof(WinCharState)); | |
6f97dba0 AL |
1660 | chr->opaque = s; |
1661 | chr->chr_write = win_chr_write; | |
1662 | chr->chr_close = win_chr_close; | |
1663 | ||
1664 | if (win_chr_init(chr, filename) < 0) { | |
2e02e18b SW |
1665 | g_free(s); |
1666 | g_free(chr); | |
1f51470d | 1667 | return NULL; |
6f97dba0 | 1668 | } |
127338e6 | 1669 | qemu_chr_generic_open(chr); |
1f51470d | 1670 | return chr; |
6f97dba0 AL |
1671 | } |
1672 | ||
1673 | static int win_chr_pipe_poll(void *opaque) | |
1674 | { | |
1675 | CharDriverState *chr = opaque; | |
1676 | WinCharState *s = chr->opaque; | |
1677 | DWORD size; | |
1678 | ||
1679 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); | |
1680 | if (size > 0) { | |
1681 | s->len = size; | |
1682 | win_chr_read_poll(chr); | |
1683 | win_chr_read(chr); | |
1684 | return 1; | |
1685 | } | |
1686 | return 0; | |
1687 | } | |
1688 | ||
1689 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) | |
1690 | { | |
1691 | WinCharState *s = chr->opaque; | |
1692 | OVERLAPPED ov; | |
1693 | int ret; | |
1694 | DWORD size; | |
1695 | char openname[256]; | |
1696 | ||
1697 | s->fpipe = TRUE; | |
1698 | ||
1699 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1700 | if (!s->hsend) { | |
1701 | fprintf(stderr, "Failed CreateEvent\n"); | |
1702 | goto fail; | |
1703 | } | |
1704 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1705 | if (!s->hrecv) { | |
1706 | fprintf(stderr, "Failed CreateEvent\n"); | |
1707 | goto fail; | |
1708 | } | |
1709 | ||
1710 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); | |
1711 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, | |
1712 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | | |
1713 | PIPE_WAIT, | |
1714 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); | |
1715 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1716 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); | |
1717 | s->hcom = NULL; | |
1718 | goto fail; | |
1719 | } | |
1720 | ||
1721 | ZeroMemory(&ov, sizeof(ov)); | |
1722 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1723 | ret = ConnectNamedPipe(s->hcom, &ov); | |
1724 | if (ret) { | |
1725 | fprintf(stderr, "Failed ConnectNamedPipe\n"); | |
1726 | goto fail; | |
1727 | } | |
1728 | ||
1729 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); | |
1730 | if (!ret) { | |
1731 | fprintf(stderr, "Failed GetOverlappedResult\n"); | |
1732 | if (ov.hEvent) { | |
1733 | CloseHandle(ov.hEvent); | |
1734 | ov.hEvent = NULL; | |
1735 | } | |
1736 | goto fail; | |
1737 | } | |
1738 | ||
1739 | if (ov.hEvent) { | |
1740 | CloseHandle(ov.hEvent); | |
1741 | ov.hEvent = NULL; | |
1742 | } | |
1743 | qemu_add_polling_cb(win_chr_pipe_poll, chr); | |
1744 | return 0; | |
1745 | ||
1746 | fail: | |
1747 | win_chr_close(chr); | |
1748 | return -1; | |
1749 | } | |
1750 | ||
1751 | ||
1f51470d | 1752 | static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts) |
6f97dba0 | 1753 | { |
7d31544f | 1754 | const char *filename = qemu_opt_get(opts, "path"); |
6f97dba0 AL |
1755 | CharDriverState *chr; |
1756 | WinCharState *s; | |
1757 | ||
7267c094 AL |
1758 | chr = g_malloc0(sizeof(CharDriverState)); |
1759 | s = g_malloc0(sizeof(WinCharState)); | |
6f97dba0 AL |
1760 | chr->opaque = s; |
1761 | chr->chr_write = win_chr_write; | |
1762 | chr->chr_close = win_chr_close; | |
1763 | ||
1764 | if (win_chr_pipe_init(chr, filename) < 0) { | |
2e02e18b SW |
1765 | g_free(s); |
1766 | g_free(chr); | |
1f51470d | 1767 | return NULL; |
6f97dba0 | 1768 | } |
127338e6 | 1769 | qemu_chr_generic_open(chr); |
1f51470d | 1770 | return chr; |
6f97dba0 AL |
1771 | } |
1772 | ||
1f51470d | 1773 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
6f97dba0 AL |
1774 | { |
1775 | CharDriverState *chr; | |
1776 | WinCharState *s; | |
1777 | ||
7267c094 AL |
1778 | chr = g_malloc0(sizeof(CharDriverState)); |
1779 | s = g_malloc0(sizeof(WinCharState)); | |
6f97dba0 AL |
1780 | s->hcom = fd_out; |
1781 | chr->opaque = s; | |
1782 | chr->chr_write = win_chr_write; | |
127338e6 | 1783 | qemu_chr_generic_open(chr); |
1f51470d | 1784 | return chr; |
6f97dba0 AL |
1785 | } |
1786 | ||
1f51470d | 1787 | static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts) |
6f97dba0 | 1788 | { |
1f51470d | 1789 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
6f97dba0 AL |
1790 | } |
1791 | ||
1f51470d | 1792 | static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts) |
6f97dba0 | 1793 | { |
7d31544f | 1794 | const char *file_out = qemu_opt_get(opts, "path"); |
6f97dba0 AL |
1795 | HANDLE fd_out; |
1796 | ||
1797 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, | |
1798 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
a89dd6c3 | 1799 | if (fd_out == INVALID_HANDLE_VALUE) { |
1f51470d | 1800 | return NULL; |
a89dd6c3 | 1801 | } |
6f97dba0 | 1802 | |
1f51470d | 1803 | return qemu_chr_open_win_file(fd_out); |
6f97dba0 | 1804 | } |
db418a0a FC |
1805 | |
1806 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) | |
1807 | { | |
1808 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
1809 | DWORD dwSize; | |
1810 | int len1; | |
1811 | ||
1812 | len1 = len; | |
1813 | ||
1814 | while (len1 > 0) { | |
1815 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { | |
1816 | break; | |
1817 | } | |
1818 | buf += dwSize; | |
1819 | len1 -= dwSize; | |
1820 | } | |
1821 | ||
1822 | return len - len1; | |
1823 | } | |
1824 | ||
1825 | static void win_stdio_wait_func(void *opaque) | |
1826 | { | |
1827 | CharDriverState *chr = opaque; | |
1828 | WinStdioCharState *stdio = chr->opaque; | |
1829 | INPUT_RECORD buf[4]; | |
1830 | int ret; | |
1831 | DWORD dwSize; | |
1832 | int i; | |
1833 | ||
1834 | ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf), | |
1835 | &dwSize); | |
1836 | ||
1837 | if (!ret) { | |
1838 | /* Avoid error storm */ | |
1839 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); | |
1840 | return; | |
1841 | } | |
1842 | ||
1843 | for (i = 0; i < dwSize; i++) { | |
1844 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; | |
1845 | ||
1846 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { | |
1847 | int j; | |
1848 | if (kev->uChar.AsciiChar != 0) { | |
1849 | for (j = 0; j < kev->wRepeatCount; j++) { | |
1850 | if (qemu_chr_be_can_write(chr)) { | |
1851 | uint8_t c = kev->uChar.AsciiChar; | |
1852 | qemu_chr_be_write(chr, &c, 1); | |
1853 | } | |
1854 | } | |
1855 | } | |
1856 | } | |
1857 | } | |
1858 | } | |
1859 | ||
1860 | static DWORD WINAPI win_stdio_thread(LPVOID param) | |
1861 | { | |
1862 | CharDriverState *chr = param; | |
1863 | WinStdioCharState *stdio = chr->opaque; | |
1864 | int ret; | |
1865 | DWORD dwSize; | |
1866 | ||
1867 | while (1) { | |
1868 | ||
1869 | /* Wait for one byte */ | |
1870 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); | |
1871 | ||
1872 | /* Exit in case of error, continue if nothing read */ | |
1873 | if (!ret) { | |
1874 | break; | |
1875 | } | |
1876 | if (!dwSize) { | |
1877 | continue; | |
1878 | } | |
1879 | ||
1880 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ | |
1881 | if (stdio->win_stdio_buf == '\r') { | |
1882 | continue; | |
1883 | } | |
1884 | ||
1885 | /* Signal the main thread and wait until the byte was eaten */ | |
1886 | if (!SetEvent(stdio->hInputReadyEvent)) { | |
1887 | break; | |
1888 | } | |
1889 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) | |
1890 | != WAIT_OBJECT_0) { | |
1891 | break; | |
1892 | } | |
1893 | } | |
1894 | ||
1895 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); | |
1896 | return 0; | |
1897 | } | |
1898 | ||
1899 | static void win_stdio_thread_wait_func(void *opaque) | |
1900 | { | |
1901 | CharDriverState *chr = opaque; | |
1902 | WinStdioCharState *stdio = chr->opaque; | |
1903 | ||
1904 | if (qemu_chr_be_can_write(chr)) { | |
1905 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); | |
1906 | } | |
1907 | ||
1908 | SetEvent(stdio->hInputDoneEvent); | |
1909 | } | |
1910 | ||
1911 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) | |
1912 | { | |
1913 | WinStdioCharState *stdio = chr->opaque; | |
1914 | DWORD dwMode = 0; | |
1915 | ||
1916 | GetConsoleMode(stdio->hStdIn, &dwMode); | |
1917 | ||
1918 | if (echo) { | |
1919 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); | |
1920 | } else { | |
1921 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); | |
1922 | } | |
1923 | } | |
1924 | ||
1925 | static void win_stdio_close(CharDriverState *chr) | |
1926 | { | |
1927 | WinStdioCharState *stdio = chr->opaque; | |
1928 | ||
1929 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { | |
1930 | CloseHandle(stdio->hInputReadyEvent); | |
1931 | } | |
1932 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { | |
1933 | CloseHandle(stdio->hInputDoneEvent); | |
1934 | } | |
1935 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { | |
1936 | TerminateThread(stdio->hInputThread, 0); | |
1937 | } | |
1938 | ||
1939 | g_free(chr->opaque); | |
1940 | g_free(chr); | |
1941 | stdio_nb_clients--; | |
1942 | } | |
1943 | ||
1f51470d | 1944 | static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts) |
db418a0a FC |
1945 | { |
1946 | CharDriverState *chr; | |
1947 | WinStdioCharState *stdio; | |
1948 | DWORD dwMode; | |
1949 | int is_console = 0; | |
1950 | ||
1951 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS | |
1952 | || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) { | |
1f51470d | 1953 | return NULL; |
db418a0a FC |
1954 | } |
1955 | ||
1956 | chr = g_malloc0(sizeof(CharDriverState)); | |
1957 | stdio = g_malloc0(sizeof(WinStdioCharState)); | |
1958 | ||
1959 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); | |
1960 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { | |
1961 | fprintf(stderr, "cannot open stdio: invalid handle\n"); | |
1962 | exit(1); | |
1963 | } | |
1964 | ||
1965 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; | |
1966 | ||
1967 | chr->opaque = stdio; | |
1968 | chr->chr_write = win_stdio_write; | |
1969 | chr->chr_close = win_stdio_close; | |
1970 | ||
1971 | if (stdio_nb_clients == 0) { | |
1972 | if (is_console) { | |
1973 | if (qemu_add_wait_object(stdio->hStdIn, | |
1974 | win_stdio_wait_func, chr)) { | |
1975 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
1976 | } | |
1977 | } else { | |
1978 | DWORD dwId; | |
1979 | ||
1980 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
1981 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
1982 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, | |
1983 | chr, 0, &dwId); | |
1984 | ||
1985 | if (stdio->hInputThread == INVALID_HANDLE_VALUE | |
1986 | || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE | |
1987 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { | |
1988 | fprintf(stderr, "cannot create stdio thread or event\n"); | |
1989 | exit(1); | |
1990 | } | |
1991 | if (qemu_add_wait_object(stdio->hInputReadyEvent, | |
1992 | win_stdio_thread_wait_func, chr)) { | |
1993 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
1994 | } | |
1995 | } | |
1996 | } | |
1997 | ||
1998 | dwMode |= ENABLE_LINE_INPUT; | |
1999 | ||
2000 | stdio_clients[stdio_nb_clients++] = chr; | |
2001 | if (stdio_nb_clients == 1 && is_console) { | |
2002 | /* set the terminal in raw mode */ | |
2003 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ | |
2004 | dwMode |= ENABLE_PROCESSED_INPUT; | |
2005 | } | |
2006 | ||
2007 | SetConsoleMode(stdio->hStdIn, dwMode); | |
2008 | ||
2009 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; | |
2010 | qemu_chr_fe_set_echo(chr, false); | |
2011 | ||
1f51470d | 2012 | return chr; |
db418a0a | 2013 | } |
6f97dba0 AL |
2014 | #endif /* !_WIN32 */ |
2015 | ||
2016 | /***********************************************************/ | |
2017 | /* UDP Net console */ | |
2018 | ||
2019 | typedef struct { | |
2020 | int fd; | |
9bd7854e | 2021 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2022 | int bufcnt; |
2023 | int bufptr; | |
2024 | int max_size; | |
2025 | } NetCharDriver; | |
2026 | ||
2027 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2028 | { | |
2029 | NetCharDriver *s = chr->opaque; | |
2030 | ||
7e1b35b4 | 2031 | return send(s->fd, (const void *)buf, len, 0); |
6f97dba0 AL |
2032 | } |
2033 | ||
2034 | static int udp_chr_read_poll(void *opaque) | |
2035 | { | |
2036 | CharDriverState *chr = opaque; | |
2037 | NetCharDriver *s = chr->opaque; | |
2038 | ||
909cda12 | 2039 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2040 | |
2041 | /* If there were any stray characters in the queue process them | |
2042 | * first | |
2043 | */ | |
2044 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2045 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2046 | s->bufptr++; |
909cda12 | 2047 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2048 | } |
2049 | return s->max_size; | |
2050 | } | |
2051 | ||
2052 | static void udp_chr_read(void *opaque) | |
2053 | { | |
2054 | CharDriverState *chr = opaque; | |
2055 | NetCharDriver *s = chr->opaque; | |
2056 | ||
2057 | if (s->max_size == 0) | |
2058 | return; | |
00aa0040 | 2059 | s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0); |
6f97dba0 AL |
2060 | s->bufptr = s->bufcnt; |
2061 | if (s->bufcnt <= 0) | |
2062 | return; | |
2063 | ||
2064 | s->bufptr = 0; | |
2065 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2066 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2067 | s->bufptr++; |
909cda12 | 2068 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2069 | } |
2070 | } | |
2071 | ||
2072 | static void udp_chr_update_read_handler(CharDriverState *chr) | |
2073 | { | |
2074 | NetCharDriver *s = chr->opaque; | |
2075 | ||
2076 | if (s->fd >= 0) { | |
2077 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, | |
2078 | udp_chr_read, NULL, chr); | |
2079 | } | |
2080 | } | |
2081 | ||
819f56b7 AL |
2082 | static void udp_chr_close(CharDriverState *chr) |
2083 | { | |
2084 | NetCharDriver *s = chr->opaque; | |
2085 | if (s->fd >= 0) { | |
069c159e | 2086 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
819f56b7 AL |
2087 | closesocket(s->fd); |
2088 | } | |
7267c094 | 2089 | g_free(s); |
a425d23f | 2090 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
819f56b7 AL |
2091 | } |
2092 | ||
1f51470d | 2093 | static CharDriverState *qemu_chr_open_udp(QemuOpts *opts) |
6f97dba0 AL |
2094 | { |
2095 | CharDriverState *chr = NULL; | |
2096 | NetCharDriver *s = NULL; | |
2097 | int fd = -1; | |
6f97dba0 | 2098 | |
7267c094 AL |
2099 | chr = g_malloc0(sizeof(CharDriverState)); |
2100 | s = g_malloc0(sizeof(NetCharDriver)); | |
6f97dba0 | 2101 | |
7e1b35b4 | 2102 | fd = inet_dgram_opts(opts); |
6f97dba0 | 2103 | if (fd < 0) { |
7e1b35b4 | 2104 | fprintf(stderr, "inet_dgram_opts failed\n"); |
6f97dba0 AL |
2105 | goto return_err; |
2106 | } | |
2107 | ||
2108 | s->fd = fd; | |
2109 | s->bufcnt = 0; | |
2110 | s->bufptr = 0; | |
2111 | chr->opaque = s; | |
2112 | chr->chr_write = udp_chr_write; | |
2113 | chr->chr_update_read_handler = udp_chr_update_read_handler; | |
819f56b7 | 2114 | chr->chr_close = udp_chr_close; |
1f51470d | 2115 | return chr; |
6f97dba0 AL |
2116 | |
2117 | return_err: | |
7267c094 AL |
2118 | g_free(chr); |
2119 | g_free(s); | |
6e1db57b | 2120 | if (fd >= 0) { |
6f97dba0 | 2121 | closesocket(fd); |
6e1db57b | 2122 | } |
1f51470d | 2123 | return NULL; |
6f97dba0 AL |
2124 | } |
2125 | ||
2126 | /***********************************************************/ | |
2127 | /* TCP Net console */ | |
2128 | ||
2129 | typedef struct { | |
2130 | int fd, listen_fd; | |
2131 | int connected; | |
2132 | int max_size; | |
2133 | int do_telnetopt; | |
2134 | int do_nodelay; | |
2135 | int is_unix; | |
7d174059 | 2136 | int msgfd; |
6f97dba0 AL |
2137 | } TCPCharDriver; |
2138 | ||
2139 | static void tcp_chr_accept(void *opaque); | |
2140 | ||
2141 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2142 | { | |
2143 | TCPCharDriver *s = chr->opaque; | |
2144 | if (s->connected) { | |
2145 | return send_all(s->fd, buf, len); | |
2146 | } else { | |
2147 | /* XXX: indicate an error ? */ | |
2148 | return len; | |
2149 | } | |
2150 | } | |
2151 | ||
2152 | static int tcp_chr_read_poll(void *opaque) | |
2153 | { | |
2154 | CharDriverState *chr = opaque; | |
2155 | TCPCharDriver *s = chr->opaque; | |
2156 | if (!s->connected) | |
2157 | return 0; | |
909cda12 | 2158 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2159 | return s->max_size; |
2160 | } | |
2161 | ||
2162 | #define IAC 255 | |
2163 | #define IAC_BREAK 243 | |
2164 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, | |
2165 | TCPCharDriver *s, | |
2166 | uint8_t *buf, int *size) | |
2167 | { | |
2168 | /* Handle any telnet client's basic IAC options to satisfy char by | |
2169 | * char mode with no echo. All IAC options will be removed from | |
2170 | * the buf and the do_telnetopt variable will be used to track the | |
2171 | * state of the width of the IAC information. | |
2172 | * | |
2173 | * IAC commands come in sets of 3 bytes with the exception of the | |
2174 | * "IAC BREAK" command and the double IAC. | |
2175 | */ | |
2176 | ||
2177 | int i; | |
2178 | int j = 0; | |
2179 | ||
2180 | for (i = 0; i < *size; i++) { | |
2181 | if (s->do_telnetopt > 1) { | |
2182 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { | |
2183 | /* Double IAC means send an IAC */ | |
2184 | if (j != i) | |
2185 | buf[j] = buf[i]; | |
2186 | j++; | |
2187 | s->do_telnetopt = 1; | |
2188 | } else { | |
2189 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { | |
2190 | /* Handle IAC break commands by sending a serial break */ | |
a425d23f | 2191 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
2192 | s->do_telnetopt++; |
2193 | } | |
2194 | s->do_telnetopt++; | |
2195 | } | |
2196 | if (s->do_telnetopt >= 4) { | |
2197 | s->do_telnetopt = 1; | |
2198 | } | |
2199 | } else { | |
2200 | if ((unsigned char)buf[i] == IAC) { | |
2201 | s->do_telnetopt = 2; | |
2202 | } else { | |
2203 | if (j != i) | |
2204 | buf[j] = buf[i]; | |
2205 | j++; | |
2206 | } | |
2207 | } | |
2208 | } | |
2209 | *size = j; | |
2210 | } | |
2211 | ||
7d174059 MM |
2212 | static int tcp_get_msgfd(CharDriverState *chr) |
2213 | { | |
2214 | TCPCharDriver *s = chr->opaque; | |
e53f27b9 PB |
2215 | int fd = s->msgfd; |
2216 | s->msgfd = -1; | |
2217 | return fd; | |
7d174059 MM |
2218 | } |
2219 | ||
73bcc2ac | 2220 | #ifndef _WIN32 |
7d174059 MM |
2221 | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
2222 | { | |
2223 | TCPCharDriver *s = chr->opaque; | |
2224 | struct cmsghdr *cmsg; | |
2225 | ||
2226 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
2227 | int fd; | |
2228 | ||
2229 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || | |
2230 | cmsg->cmsg_level != SOL_SOCKET || | |
2231 | cmsg->cmsg_type != SCM_RIGHTS) | |
2232 | continue; | |
2233 | ||
2234 | fd = *((int *)CMSG_DATA(cmsg)); | |
2235 | if (fd < 0) | |
2236 | continue; | |
2237 | ||
2238 | if (s->msgfd != -1) | |
2239 | close(s->msgfd); | |
2240 | s->msgfd = fd; | |
2241 | } | |
2242 | } | |
2243 | ||
9977c894 MM |
2244 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
2245 | { | |
2246 | TCPCharDriver *s = chr->opaque; | |
7cba04f6 | 2247 | struct msghdr msg = { NULL, }; |
9977c894 | 2248 | struct iovec iov[1]; |
7d174059 MM |
2249 | union { |
2250 | struct cmsghdr cmsg; | |
2251 | char control[CMSG_SPACE(sizeof(int))]; | |
2252 | } msg_control; | |
2253 | ssize_t ret; | |
9977c894 MM |
2254 | |
2255 | iov[0].iov_base = buf; | |
2256 | iov[0].iov_len = len; | |
2257 | ||
2258 | msg.msg_iov = iov; | |
2259 | msg.msg_iovlen = 1; | |
7d174059 MM |
2260 | msg.msg_control = &msg_control; |
2261 | msg.msg_controllen = sizeof(msg_control); | |
2262 | ||
2263 | ret = recvmsg(s->fd, &msg, 0); | |
2264 | if (ret > 0 && s->is_unix) | |
2265 | unix_process_msgfd(chr, &msg); | |
9977c894 | 2266 | |
7d174059 | 2267 | return ret; |
9977c894 MM |
2268 | } |
2269 | #else | |
2270 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) | |
2271 | { | |
2272 | TCPCharDriver *s = chr->opaque; | |
00aa0040 | 2273 | return qemu_recv(s->fd, buf, len, 0); |
9977c894 MM |
2274 | } |
2275 | #endif | |
2276 | ||
6f97dba0 AL |
2277 | static void tcp_chr_read(void *opaque) |
2278 | { | |
2279 | CharDriverState *chr = opaque; | |
2280 | TCPCharDriver *s = chr->opaque; | |
9bd7854e | 2281 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2282 | int len, size; |
2283 | ||
2284 | if (!s->connected || s->max_size <= 0) | |
2285 | return; | |
2286 | len = sizeof(buf); | |
2287 | if (len > s->max_size) | |
2288 | len = s->max_size; | |
9977c894 | 2289 | size = tcp_chr_recv(chr, (void *)buf, len); |
6f97dba0 AL |
2290 | if (size == 0) { |
2291 | /* connection closed */ | |
2292 | s->connected = 0; | |
2293 | if (s->listen_fd >= 0) { | |
069c159e | 2294 | qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr); |
6f97dba0 | 2295 | } |
069c159e | 2296 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
6f97dba0 AL |
2297 | closesocket(s->fd); |
2298 | s->fd = -1; | |
a425d23f | 2299 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
2300 | } else if (size > 0) { |
2301 | if (s->do_telnetopt) | |
2302 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); | |
2303 | if (size > 0) | |
fa5efccb | 2304 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
2305 | } |
2306 | } | |
2307 | ||
68c18d1c BS |
2308 | #ifndef _WIN32 |
2309 | CharDriverState *qemu_chr_open_eventfd(int eventfd) | |
2310 | { | |
6cbf4c8c | 2311 | return qemu_chr_open_fd(eventfd, eventfd); |
6cbf4c8c | 2312 | } |
68c18d1c | 2313 | #endif |
6cbf4c8c | 2314 | |
6f97dba0 AL |
2315 | static void tcp_chr_connect(void *opaque) |
2316 | { | |
2317 | CharDriverState *chr = opaque; | |
2318 | TCPCharDriver *s = chr->opaque; | |
2319 | ||
2320 | s->connected = 1; | |
2321 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, | |
2322 | tcp_chr_read, NULL, chr); | |
127338e6 | 2323 | qemu_chr_generic_open(chr); |
6f97dba0 AL |
2324 | } |
2325 | ||
2326 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; | |
2327 | static void tcp_chr_telnet_init(int fd) | |
2328 | { | |
2329 | char buf[3]; | |
2330 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ | |
2331 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ | |
2332 | send(fd, (char *)buf, 3, 0); | |
2333 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ | |
2334 | send(fd, (char *)buf, 3, 0); | |
2335 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ | |
2336 | send(fd, (char *)buf, 3, 0); | |
2337 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ | |
2338 | send(fd, (char *)buf, 3, 0); | |
2339 | } | |
2340 | ||
2341 | static void socket_set_nodelay(int fd) | |
2342 | { | |
2343 | int val = 1; | |
2344 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); | |
2345 | } | |
2346 | ||
13661089 DB |
2347 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
2348 | { | |
2349 | TCPCharDriver *s = chr->opaque; | |
2350 | if (s->fd != -1) | |
2351 | return -1; | |
2352 | ||
2353 | socket_set_nonblock(fd); | |
2354 | if (s->do_nodelay) | |
2355 | socket_set_nodelay(fd); | |
2356 | s->fd = fd; | |
069c159e | 2357 | qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL); |
13661089 DB |
2358 | tcp_chr_connect(chr); |
2359 | ||
2360 | return 0; | |
2361 | } | |
2362 | ||
6f97dba0 AL |
2363 | static void tcp_chr_accept(void *opaque) |
2364 | { | |
2365 | CharDriverState *chr = opaque; | |
2366 | TCPCharDriver *s = chr->opaque; | |
2367 | struct sockaddr_in saddr; | |
2368 | #ifndef _WIN32 | |
2369 | struct sockaddr_un uaddr; | |
2370 | #endif | |
2371 | struct sockaddr *addr; | |
2372 | socklen_t len; | |
2373 | int fd; | |
2374 | ||
2375 | for(;;) { | |
2376 | #ifndef _WIN32 | |
2377 | if (s->is_unix) { | |
2378 | len = sizeof(uaddr); | |
2379 | addr = (struct sockaddr *)&uaddr; | |
2380 | } else | |
2381 | #endif | |
2382 | { | |
2383 | len = sizeof(saddr); | |
2384 | addr = (struct sockaddr *)&saddr; | |
2385 | } | |
40ff6d7e | 2386 | fd = qemu_accept(s->listen_fd, addr, &len); |
6f97dba0 AL |
2387 | if (fd < 0 && errno != EINTR) { |
2388 | return; | |
2389 | } else if (fd >= 0) { | |
2390 | if (s->do_telnetopt) | |
2391 | tcp_chr_telnet_init(fd); | |
2392 | break; | |
2393 | } | |
2394 | } | |
13661089 DB |
2395 | if (tcp_chr_add_client(chr, fd) < 0) |
2396 | close(fd); | |
6f97dba0 AL |
2397 | } |
2398 | ||
2399 | static void tcp_chr_close(CharDriverState *chr) | |
2400 | { | |
2401 | TCPCharDriver *s = chr->opaque; | |
819f56b7 | 2402 | if (s->fd >= 0) { |
069c159e | 2403 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
6f97dba0 | 2404 | closesocket(s->fd); |
819f56b7 AL |
2405 | } |
2406 | if (s->listen_fd >= 0) { | |
069c159e | 2407 | qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL); |
6f97dba0 | 2408 | closesocket(s->listen_fd); |
819f56b7 | 2409 | } |
7267c094 | 2410 | g_free(s); |
a425d23f | 2411 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
2412 | } |
2413 | ||
1f51470d | 2414 | static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) |
6f97dba0 AL |
2415 | { |
2416 | CharDriverState *chr = NULL; | |
2417 | TCPCharDriver *s = NULL; | |
aeb2c47a GH |
2418 | int fd = -1; |
2419 | int is_listen; | |
2420 | int is_waitconnect; | |
2421 | int do_nodelay; | |
2422 | int is_unix; | |
2423 | int is_telnet; | |
2424 | ||
2425 | is_listen = qemu_opt_get_bool(opts, "server", 0); | |
2426 | is_waitconnect = qemu_opt_get_bool(opts, "wait", 1); | |
2427 | is_telnet = qemu_opt_get_bool(opts, "telnet", 0); | |
2428 | do_nodelay = !qemu_opt_get_bool(opts, "delay", 1); | |
2429 | is_unix = qemu_opt_get(opts, "path") != NULL; | |
6f97dba0 AL |
2430 | if (!is_listen) |
2431 | is_waitconnect = 0; | |
2432 | ||
7267c094 AL |
2433 | chr = g_malloc0(sizeof(CharDriverState)); |
2434 | s = g_malloc0(sizeof(TCPCharDriver)); | |
6f97dba0 | 2435 | |
f07b6003 AL |
2436 | if (is_unix) { |
2437 | if (is_listen) { | |
aeb2c47a | 2438 | fd = unix_listen_opts(opts); |
f07b6003 | 2439 | } else { |
aeb2c47a | 2440 | fd = unix_connect_opts(opts); |
f07b6003 AL |
2441 | } |
2442 | } else { | |
2443 | if (is_listen) { | |
aeb2c47a | 2444 | fd = inet_listen_opts(opts, 0); |
f07b6003 | 2445 | } else { |
aeb2c47a | 2446 | fd = inet_connect_opts(opts); |
f07b6003 AL |
2447 | } |
2448 | } | |
a89dd6c3 | 2449 | if (fd < 0) { |
6f97dba0 | 2450 | goto fail; |
a89dd6c3 | 2451 | } |
6f97dba0 AL |
2452 | |
2453 | if (!is_waitconnect) | |
2454 | socket_set_nonblock(fd); | |
2455 | ||
2456 | s->connected = 0; | |
2457 | s->fd = -1; | |
2458 | s->listen_fd = -1; | |
7d174059 | 2459 | s->msgfd = -1; |
6f97dba0 AL |
2460 | s->is_unix = is_unix; |
2461 | s->do_nodelay = do_nodelay && !is_unix; | |
2462 | ||
2463 | chr->opaque = s; | |
2464 | chr->chr_write = tcp_chr_write; | |
2465 | chr->chr_close = tcp_chr_close; | |
7d174059 | 2466 | chr->get_msgfd = tcp_get_msgfd; |
13661089 | 2467 | chr->chr_add_client = tcp_chr_add_client; |
6f97dba0 AL |
2468 | |
2469 | if (is_listen) { | |
6f97dba0 | 2470 | s->listen_fd = fd; |
069c159e | 2471 | qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr); |
6f97dba0 AL |
2472 | if (is_telnet) |
2473 | s->do_telnetopt = 1; | |
aeb2c47a | 2474 | |
6f97dba0 | 2475 | } else { |
f07b6003 | 2476 | s->connected = 1; |
6f97dba0 AL |
2477 | s->fd = fd; |
2478 | socket_set_nodelay(fd); | |
f07b6003 | 2479 | tcp_chr_connect(chr); |
6f97dba0 AL |
2480 | } |
2481 | ||
aeb2c47a | 2482 | /* for "info chardev" monitor command */ |
7267c094 | 2483 | chr->filename = g_malloc(256); |
aeb2c47a GH |
2484 | if (is_unix) { |
2485 | snprintf(chr->filename, 256, "unix:%s%s", | |
2486 | qemu_opt_get(opts, "path"), | |
2487 | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); | |
2488 | } else if (is_telnet) { | |
2489 | snprintf(chr->filename, 256, "telnet:%s:%s%s", | |
2490 | qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"), | |
2491 | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); | |
2492 | } else { | |
2493 | snprintf(chr->filename, 256, "tcp:%s:%s%s", | |
2494 | qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"), | |
2495 | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); | |
2496 | } | |
2497 | ||
6f97dba0 | 2498 | if (is_listen && is_waitconnect) { |
f07b6003 | 2499 | printf("QEMU waiting for connection on: %s\n", |
aeb2c47a | 2500 | chr->filename); |
6f97dba0 AL |
2501 | tcp_chr_accept(chr); |
2502 | socket_set_nonblock(s->listen_fd); | |
2503 | } | |
1f51470d | 2504 | return chr; |
aeb2c47a | 2505 | |
6f97dba0 AL |
2506 | fail: |
2507 | if (fd >= 0) | |
2508 | closesocket(fd); | |
7267c094 AL |
2509 | g_free(s); |
2510 | g_free(chr); | |
1f51470d | 2511 | return NULL; |
6f97dba0 AL |
2512 | } |
2513 | ||
999bd67c LC |
2514 | /***********************************************************/ |
2515 | /* Memory chardev */ | |
2516 | typedef struct { | |
2517 | size_t outbuf_size; | |
2518 | size_t outbuf_capacity; | |
2519 | uint8_t *outbuf; | |
2520 | } MemoryDriver; | |
2521 | ||
2522 | static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2523 | { | |
2524 | MemoryDriver *d = chr->opaque; | |
2525 | ||
2526 | /* TODO: the QString implementation has the same code, we should | |
2527 | * introduce a generic way to do this in cutils.c */ | |
2528 | if (d->outbuf_capacity < d->outbuf_size + len) { | |
2529 | /* grow outbuf */ | |
2530 | d->outbuf_capacity += len; | |
2531 | d->outbuf_capacity *= 2; | |
7267c094 | 2532 | d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity); |
999bd67c LC |
2533 | } |
2534 | ||
2535 | memcpy(d->outbuf + d->outbuf_size, buf, len); | |
2536 | d->outbuf_size += len; | |
2537 | ||
2538 | return len; | |
2539 | } | |
2540 | ||
2541 | void qemu_chr_init_mem(CharDriverState *chr) | |
2542 | { | |
2543 | MemoryDriver *d; | |
2544 | ||
7267c094 | 2545 | d = g_malloc(sizeof(*d)); |
999bd67c LC |
2546 | d->outbuf_size = 0; |
2547 | d->outbuf_capacity = 4096; | |
7267c094 | 2548 | d->outbuf = g_malloc0(d->outbuf_capacity); |
999bd67c LC |
2549 | |
2550 | memset(chr, 0, sizeof(*chr)); | |
2551 | chr->opaque = d; | |
2552 | chr->chr_write = mem_chr_write; | |
2553 | } | |
2554 | ||
2555 | QString *qemu_chr_mem_to_qs(CharDriverState *chr) | |
2556 | { | |
2557 | MemoryDriver *d = chr->opaque; | |
2558 | return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1); | |
2559 | } | |
2560 | ||
70f24fb6 | 2561 | /* NOTE: this driver can not be closed with qemu_chr_delete()! */ |
999bd67c LC |
2562 | void qemu_chr_close_mem(CharDriverState *chr) |
2563 | { | |
2564 | MemoryDriver *d = chr->opaque; | |
2565 | ||
7267c094 AL |
2566 | g_free(d->outbuf); |
2567 | g_free(chr->opaque); | |
999bd67c LC |
2568 | chr->opaque = NULL; |
2569 | chr->chr_write = NULL; | |
2570 | } | |
2571 | ||
2572 | size_t qemu_chr_mem_osize(const CharDriverState *chr) | |
2573 | { | |
2574 | const MemoryDriver *d = chr->opaque; | |
2575 | return d->outbuf_size; | |
2576 | } | |
2577 | ||
33521634 | 2578 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
191bc01b | 2579 | { |
6ea314d9 | 2580 | char host[65], port[33], width[8], height[8]; |
aeb2c47a | 2581 | int pos; |
7d31544f | 2582 | const char *p; |
191bc01b GH |
2583 | QemuOpts *opts; |
2584 | ||
3329f07b | 2585 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1); |
191bc01b GH |
2586 | if (NULL == opts) |
2587 | return NULL; | |
2588 | ||
7591c5c1 GH |
2589 | if (strstart(filename, "mon:", &p)) { |
2590 | filename = p; | |
2591 | qemu_opt_set(opts, "mux", "on"); | |
2592 | } | |
2593 | ||
f0457e8d GH |
2594 | if (strcmp(filename, "null") == 0 || |
2595 | strcmp(filename, "pty") == 0 || | |
2596 | strcmp(filename, "msmouse") == 0 || | |
dc1c21e6 | 2597 | strcmp(filename, "braille") == 0 || |
f0457e8d | 2598 | strcmp(filename, "stdio") == 0) { |
4490dadf | 2599 | qemu_opt_set(opts, "backend", filename); |
191bc01b GH |
2600 | return opts; |
2601 | } | |
6ea314d9 GH |
2602 | if (strstart(filename, "vc", &p)) { |
2603 | qemu_opt_set(opts, "backend", "vc"); | |
2604 | if (*p == ':') { | |
2605 | if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) { | |
2606 | /* pixels */ | |
2607 | qemu_opt_set(opts, "width", width); | |
2608 | qemu_opt_set(opts, "height", height); | |
2609 | } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) { | |
2610 | /* chars */ | |
2611 | qemu_opt_set(opts, "cols", width); | |
2612 | qemu_opt_set(opts, "rows", height); | |
2613 | } else { | |
2614 | goto fail; | |
2615 | } | |
2616 | } | |
2617 | return opts; | |
2618 | } | |
d6c983cd GH |
2619 | if (strcmp(filename, "con:") == 0) { |
2620 | qemu_opt_set(opts, "backend", "console"); | |
2621 | return opts; | |
2622 | } | |
48b76496 GH |
2623 | if (strstart(filename, "COM", NULL)) { |
2624 | qemu_opt_set(opts, "backend", "serial"); | |
2625 | qemu_opt_set(opts, "path", filename); | |
2626 | return opts; | |
2627 | } | |
7d31544f GH |
2628 | if (strstart(filename, "file:", &p)) { |
2629 | qemu_opt_set(opts, "backend", "file"); | |
2630 | qemu_opt_set(opts, "path", p); | |
2631 | return opts; | |
2632 | } | |
2633 | if (strstart(filename, "pipe:", &p)) { | |
2634 | qemu_opt_set(opts, "backend", "pipe"); | |
2635 | qemu_opt_set(opts, "path", p); | |
2636 | return opts; | |
2637 | } | |
aeb2c47a GH |
2638 | if (strstart(filename, "tcp:", &p) || |
2639 | strstart(filename, "telnet:", &p)) { | |
2640 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
2641 | host[0] = 0; | |
2642 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) | |
2643 | goto fail; | |
2644 | } | |
2645 | qemu_opt_set(opts, "backend", "socket"); | |
2646 | qemu_opt_set(opts, "host", host); | |
2647 | qemu_opt_set(opts, "port", port); | |
2648 | if (p[pos] == ',') { | |
2649 | if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0) | |
2650 | goto fail; | |
2651 | } | |
2652 | if (strstart(filename, "telnet:", &p)) | |
2653 | qemu_opt_set(opts, "telnet", "on"); | |
2654 | return opts; | |
2655 | } | |
7e1b35b4 GH |
2656 | if (strstart(filename, "udp:", &p)) { |
2657 | qemu_opt_set(opts, "backend", "udp"); | |
2658 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { | |
2659 | host[0] = 0; | |
39324ca4 | 2660 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
7e1b35b4 GH |
2661 | goto fail; |
2662 | } | |
2663 | } | |
2664 | qemu_opt_set(opts, "host", host); | |
2665 | qemu_opt_set(opts, "port", port); | |
2666 | if (p[pos] == '@') { | |
2667 | p += pos + 1; | |
2668 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
2669 | host[0] = 0; | |
2670 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { | |
7e1b35b4 GH |
2671 | goto fail; |
2672 | } | |
2673 | } | |
2674 | qemu_opt_set(opts, "localaddr", host); | |
2675 | qemu_opt_set(opts, "localport", port); | |
2676 | } | |
2677 | return opts; | |
2678 | } | |
aeb2c47a GH |
2679 | if (strstart(filename, "unix:", &p)) { |
2680 | qemu_opt_set(opts, "backend", "socket"); | |
2681 | if (qemu_opts_do_parse(opts, p, "path") != 0) | |
2682 | goto fail; | |
2683 | return opts; | |
2684 | } | |
48b76496 GH |
2685 | if (strstart(filename, "/dev/parport", NULL) || |
2686 | strstart(filename, "/dev/ppi", NULL)) { | |
2687 | qemu_opt_set(opts, "backend", "parport"); | |
2688 | qemu_opt_set(opts, "path", filename); | |
2689 | return opts; | |
2690 | } | |
2691 | if (strstart(filename, "/dev/", NULL)) { | |
2692 | qemu_opt_set(opts, "backend", "tty"); | |
2693 | qemu_opt_set(opts, "path", filename); | |
2694 | return opts; | |
2695 | } | |
191bc01b | 2696 | |
aeb2c47a | 2697 | fail: |
191bc01b GH |
2698 | qemu_opts_del(opts); |
2699 | return NULL; | |
2700 | } | |
2701 | ||
2702 | static const struct { | |
2703 | const char *name; | |
1f51470d | 2704 | CharDriverState *(*open)(QemuOpts *opts); |
191bc01b GH |
2705 | } backend_table[] = { |
2706 | { .name = "null", .open = qemu_chr_open_null }, | |
aeb2c47a | 2707 | { .name = "socket", .open = qemu_chr_open_socket }, |
7e1b35b4 | 2708 | { .name = "udp", .open = qemu_chr_open_udp }, |
f0457e8d | 2709 | { .name = "msmouse", .open = qemu_chr_open_msmouse }, |
6ea314d9 | 2710 | { .name = "vc", .open = text_console_init }, |
7d31544f GH |
2711 | #ifdef _WIN32 |
2712 | { .name = "file", .open = qemu_chr_open_win_file_out }, | |
2713 | { .name = "pipe", .open = qemu_chr_open_win_pipe }, | |
d6c983cd | 2714 | { .name = "console", .open = qemu_chr_open_win_con }, |
48b76496 | 2715 | { .name = "serial", .open = qemu_chr_open_win }, |
db418a0a | 2716 | { .name = "stdio", .open = qemu_chr_open_win_stdio }, |
7d31544f GH |
2717 | #else |
2718 | { .name = "file", .open = qemu_chr_open_file_out }, | |
2719 | { .name = "pipe", .open = qemu_chr_open_pipe }, | |
4490dadf | 2720 | { .name = "pty", .open = qemu_chr_open_pty }, |
3c17affb | 2721 | { .name = "stdio", .open = qemu_chr_open_stdio }, |
7d31544f | 2722 | #endif |
dc1c21e6 GH |
2723 | #ifdef CONFIG_BRLAPI |
2724 | { .name = "braille", .open = chr_baum_init }, | |
2725 | #endif | |
48b76496 | 2726 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ |
a167ba50 AJ |
2727 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
2728 | || defined(__FreeBSD_kernel__) | |
48b76496 GH |
2729 | { .name = "tty", .open = qemu_chr_open_tty }, |
2730 | #endif | |
a167ba50 AJ |
2731 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \ |
2732 | || defined(__FreeBSD_kernel__) | |
48b76496 GH |
2733 | { .name = "parport", .open = qemu_chr_open_pp }, |
2734 | #endif | |
cbcc6336 AL |
2735 | #ifdef CONFIG_SPICE |
2736 | { .name = "spicevmc", .open = qemu_chr_open_spice }, | |
2737 | #endif | |
191bc01b GH |
2738 | }; |
2739 | ||
f69554b9 | 2740 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
191bc01b GH |
2741 | void (*init)(struct CharDriverState *s)) |
2742 | { | |
2743 | CharDriverState *chr; | |
2744 | int i; | |
2745 | ||
2746 | if (qemu_opts_id(opts) == NULL) { | |
2747 | fprintf(stderr, "chardev: no id specified\n"); | |
2748 | return NULL; | |
2749 | } | |
2750 | ||
1bbd185f SH |
2751 | if (qemu_opt_get(opts, "backend") == NULL) { |
2752 | fprintf(stderr, "chardev: \"%s\" missing backend\n", | |
2753 | qemu_opts_id(opts)); | |
2754 | return NULL; | |
2755 | } | |
191bc01b GH |
2756 | for (i = 0; i < ARRAY_SIZE(backend_table); i++) { |
2757 | if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0) | |
2758 | break; | |
2759 | } | |
2760 | if (i == ARRAY_SIZE(backend_table)) { | |
2761 | fprintf(stderr, "chardev: backend \"%s\" not found\n", | |
2762 | qemu_opt_get(opts, "backend")); | |
2763 | return NULL; | |
2764 | } | |
2765 | ||
1f51470d MA |
2766 | chr = backend_table[i].open(opts); |
2767 | if (!chr) { | |
2768 | fprintf(stderr, "chardev: opening backend \"%s\" failed\n", | |
2769 | qemu_opt_get(opts, "backend")); | |
191bc01b GH |
2770 | return NULL; |
2771 | } | |
2772 | ||
2773 | if (!chr->filename) | |
7267c094 | 2774 | chr->filename = g_strdup(qemu_opt_get(opts, "backend")); |
191bc01b | 2775 | chr->init = init; |
72cf2d4f | 2776 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
7591c5c1 GH |
2777 | |
2778 | if (qemu_opt_get_bool(opts, "mux", 0)) { | |
2779 | CharDriverState *base = chr; | |
2780 | int len = strlen(qemu_opts_id(opts)) + 6; | |
7267c094 | 2781 | base->label = g_malloc(len); |
7591c5c1 GH |
2782 | snprintf(base->label, len, "%s-base", qemu_opts_id(opts)); |
2783 | chr = qemu_chr_open_mux(base); | |
2784 | chr->filename = base->filename; | |
d5b27167 | 2785 | chr->avail_connections = MAX_MUX; |
72cf2d4f | 2786 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
d5b27167 KK |
2787 | } else { |
2788 | chr->avail_connections = 1; | |
7591c5c1 | 2789 | } |
7267c094 | 2790 | chr->label = g_strdup(qemu_opts_id(opts)); |
191bc01b GH |
2791 | return chr; |
2792 | } | |
2793 | ||
27143a44 | 2794 | CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
6f97dba0 AL |
2795 | { |
2796 | const char *p; | |
2797 | CharDriverState *chr; | |
191bc01b GH |
2798 | QemuOpts *opts; |
2799 | ||
c845f401 GH |
2800 | if (strstart(filename, "chardev:", &p)) { |
2801 | return qemu_chr_find(p); | |
2802 | } | |
2803 | ||
191bc01b | 2804 | opts = qemu_chr_parse_compat(label, filename); |
7e1b35b4 GH |
2805 | if (!opts) |
2806 | return NULL; | |
6f97dba0 | 2807 | |
f69554b9 | 2808 | chr = qemu_chr_new_from_opts(opts, init); |
7e1b35b4 GH |
2809 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
2810 | monitor_init(chr, MONITOR_USE_READLINE); | |
6f97dba0 | 2811 | } |
363f8cb9 | 2812 | qemu_opts_del(opts); |
6f97dba0 AL |
2813 | return chr; |
2814 | } | |
2815 | ||
15f31519 | 2816 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
c48855e1 PB |
2817 | { |
2818 | if (chr->chr_set_echo) { | |
2819 | chr->chr_set_echo(chr, echo); | |
2820 | } | |
2821 | } | |
2822 | ||
c9d830ed | 2823 | void qemu_chr_fe_open(struct CharDriverState *chr) |
7c32c4fe HG |
2824 | { |
2825 | if (chr->chr_guest_open) { | |
2826 | chr->chr_guest_open(chr); | |
2827 | } | |
2828 | } | |
2829 | ||
2817822d | 2830 | void qemu_chr_fe_close(struct CharDriverState *chr) |
7c32c4fe HG |
2831 | { |
2832 | if (chr->chr_guest_close) { | |
2833 | chr->chr_guest_close(chr); | |
2834 | } | |
2835 | } | |
2836 | ||
70f24fb6 | 2837 | void qemu_chr_delete(CharDriverState *chr) |
6f97dba0 | 2838 | { |
72cf2d4f | 2839 | QTAILQ_REMOVE(&chardevs, chr, next); |
6f97dba0 AL |
2840 | if (chr->chr_close) |
2841 | chr->chr_close(chr); | |
7267c094 AL |
2842 | g_free(chr->filename); |
2843 | g_free(chr->label); | |
2844 | g_free(chr); | |
6f97dba0 AL |
2845 | } |
2846 | ||
c5a415a0 | 2847 | ChardevInfoList *qmp_query_chardev(Error **errp) |
6f97dba0 | 2848 | { |
c5a415a0 | 2849 | ChardevInfoList *chr_list = NULL; |
6f97dba0 AL |
2850 | CharDriverState *chr; |
2851 | ||
72cf2d4f | 2852 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c5a415a0 LC |
2853 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
2854 | info->value = g_malloc0(sizeof(*info->value)); | |
2855 | info->value->label = g_strdup(chr->label); | |
2856 | info->value->filename = g_strdup(chr->filename); | |
2857 | ||
2858 | info->next = chr_list; | |
2859 | chr_list = info; | |
6f97dba0 | 2860 | } |
588b3832 | 2861 | |
c5a415a0 | 2862 | return chr_list; |
6f97dba0 | 2863 | } |
c845f401 GH |
2864 | |
2865 | CharDriverState *qemu_chr_find(const char *name) | |
2866 | { | |
2867 | CharDriverState *chr; | |
2868 | ||
72cf2d4f | 2869 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c845f401 GH |
2870 | if (strcmp(chr->label, name) != 0) |
2871 | continue; | |
2872 | return chr; | |
2873 | } | |
2874 | return NULL; | |
2875 | } | |
0beb4942 AL |
2876 | |
2877 | /* Get a character (serial) device interface. */ | |
2878 | CharDriverState *qemu_char_get_next_serial(void) | |
2879 | { | |
2880 | static int next_serial; | |
2881 | ||
2882 | /* FIXME: This function needs to go away: use chardev properties! */ | |
2883 | return serial_hds[next_serial++]; | |
2884 | } | |
2885 |