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