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