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