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