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