blob: 25abdc8d345f0845661cdf875b3c957689c885ca [file] [log] [blame]
aliguori6f97dba2008-10-31 18:49:55 +00001/*
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"
aliguori376253e2009-03-05 23:01:23 +000026#include "monitor.h"
aliguori6f97dba2008-10-31 18:49:55 +000027#include "console.h"
28#include "sysemu.h"
29#include "qemu-timer.h"
30#include "qemu-char.h"
31#include "block.h"
aurel32cf3ebac2008-11-01 00:53:09 +000032#include "hw/usb.h"
33#include "hw/baum.h"
aurel32aa71cf82009-02-08 15:53:20 +000034#include "hw/msmouse.h"
aliguori6f97dba2008-10-31 18:49:55 +000035
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>
blueswir124646c72008-11-07 16:55:48 +000050#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000051#include <sys/socket.h>
52#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000053#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000054#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000055#include <dirent.h>
56#include <netdb.h>
57#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020058#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000059#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000060#ifdef __FreeBSD__
aliguori6f97dba2008-10-31 18:49:55 +000061#include <libutil.h>
blueswir16972f932008-11-22 20:49:12 +000062#include <dev/ppbus/ppi.h>
63#include <dev/ppbus/ppbconf.h>
blueswir1c5e97232009-03-07 20:06:23 +000064#elif defined(__DragonFly__)
65#include <libutil.h>
66#include <dev/misc/ppi/ppi.h>
67#include <bus/ppbus/ppbconf.h>
blueswir124646c72008-11-07 16:55:48 +000068#else
69#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000070#endif
71#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72#include <freebsd/stdlib.h>
73#else
74#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000075#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
Blue Swirl72cf2d42009-09-12 07:36:22 +0000103static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000105
aliguori6f97dba2008-10-31 18:49:55 +0000106static void qemu_chr_event(CharDriverState *s, int event)
107{
108 if (!s->chr_event)
109 return;
110 s->chr_event(s->handler_opaque, event);
111}
112
113static void qemu_chr_reset_bh(void *opaque)
114{
115 CharDriverState *s = opaque;
Anthony Liguorif7cbc082009-10-27 10:14:50 -0500116 qemu_chr_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000117 qemu_bh_delete(s->bh);
118 s->bh = NULL;
119}
120
121void qemu_chr_reset(CharDriverState *s)
122{
Amit Shah69795d62009-10-07 18:31:15 +0530123 if (s->bh == NULL) {
aliguori6f97dba2008-10-31 18:49:55 +0000124 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
125 qemu_bh_schedule(s->bh);
126 }
127}
128
aliguori2970a6c2009-03-05 22:59:58 +0000129void qemu_chr_initial_reset(void)
130{
131 CharDriverState *chr;
132
Blue Swirl72cf2d42009-09-12 07:36:22 +0000133 QTAILQ_FOREACH(chr, &chardevs, next) {
aliguori2970a6c2009-03-05 22:59:58 +0000134 qemu_chr_reset(chr);
135 }
136}
137
aliguori6f97dba2008-10-31 18:49:55 +0000138int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
139{
140 return s->chr_write(s, buf, len);
141}
142
143int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
144{
145 if (!s->chr_ioctl)
146 return -ENOTSUP;
147 return s->chr_ioctl(s, cmd, arg);
148}
149
150int qemu_chr_can_read(CharDriverState *s)
151{
152 if (!s->chr_can_read)
153 return 0;
154 return s->chr_can_read(s->handler_opaque);
155}
156
157void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
158{
159 s->chr_read(s->handler_opaque, buf, len);
160}
161
Mark McLoughlin7d174052009-07-22 09:11:39 +0100162int qemu_chr_get_msgfd(CharDriverState *s)
163{
164 return s->get_msgfd ? s->get_msgfd(s) : -1;
165}
166
aliguori6f97dba2008-10-31 18:49:55 +0000167void qemu_chr_accept_input(CharDriverState *s)
168{
169 if (s->chr_accept_input)
170 s->chr_accept_input(s);
171}
172
173void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
174{
175 char buf[4096];
176 va_list ap;
177 va_start(ap, fmt);
178 vsnprintf(buf, sizeof(buf), fmt, ap);
179 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
180 va_end(ap);
181}
182
183void qemu_chr_send_event(CharDriverState *s, int event)
184{
185 if (s->chr_send_event)
186 s->chr_send_event(s, event);
187}
188
189void qemu_chr_add_handlers(CharDriverState *s,
190 IOCanRWHandler *fd_can_read,
191 IOReadHandler *fd_read,
192 IOEventHandler *fd_event,
193 void *opaque)
194{
195 s->chr_can_read = fd_can_read;
196 s->chr_read = fd_read;
197 s->chr_event = fd_event;
198 s->handler_opaque = opaque;
199 if (s->chr_update_read_handler)
200 s->chr_update_read_handler(s);
201}
202
203static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
204{
205 return len;
206}
207
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200208static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000209{
210 CharDriverState *chr;
211
212 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000213 chr->chr_write = null_chr_write;
214 return chr;
215}
216
217/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000218#define MAX_MUX 4
219#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
220#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
221typedef struct {
222 IOCanRWHandler *chr_can_read[MAX_MUX];
223 IOReadHandler *chr_read[MAX_MUX];
224 IOEventHandler *chr_event[MAX_MUX];
225 void *ext_opaque[MAX_MUX];
226 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200227 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000228 int mux_cnt;
229 int term_got_escape;
230 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000231 /* Intermediate input buffer allows to catch escape sequences even if the
232 currently active device is not accepting any input - but only until it
233 is full as well. */
234 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
235 int prod[MAX_MUX];
236 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200237 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200238 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200239 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000240} MuxDriver;
241
242
243static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
244{
245 MuxDriver *d = chr->opaque;
246 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200247 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000248 ret = d->drv->chr_write(d->drv, buf, len);
249 } else {
250 int i;
251
252 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200253 for (i = 0; i < len; i++) {
254 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000255 char buf1[64];
256 int64_t ti;
257 int secs;
258
259 ti = qemu_get_clock(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200260 if (d->timestamps_start == -1)
261 d->timestamps_start = ti;
262 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000263 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000264 snprintf(buf1, sizeof(buf1),
265 "[%02d:%02d:%02d.%03d] ",
266 secs / 3600,
267 (secs / 60) % 60,
268 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000269 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000270 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200271 d->linestart = 0;
272 }
273 ret += d->drv->chr_write(d->drv, buf+i, 1);
274 if (buf[i] == '\n') {
275 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000276 }
277 }
278 }
279 return ret;
280}
281
282static const char * const mux_help[] = {
283 "% h print this help\n\r",
284 "% x exit emulator\n\r",
285 "% s save disk data back to file (if -snapshot)\n\r",
286 "% t toggle console timestamps\n\r"
287 "% b send break (magic sysrq)\n\r",
288 "% c switch between console and monitor\n\r",
289 "% % sends %\n\r",
290 NULL
291};
292
293int term_escape_char = 0x01; /* ctrl-a is used for escape */
294static void mux_print_help(CharDriverState *chr)
295{
296 int i, j;
297 char ebuf[15] = "Escape-Char";
298 char cbuf[50] = "\n\r";
299
300 if (term_escape_char > 0 && term_escape_char < 26) {
301 snprintf(cbuf, sizeof(cbuf), "\n\r");
302 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
303 } else {
304 snprintf(cbuf, sizeof(cbuf),
305 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
306 term_escape_char);
307 }
308 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
309 for (i = 0; mux_help[i] != NULL; i++) {
310 for (j=0; mux_help[i][j] != '\0'; j++) {
311 if (mux_help[i][j] == '%')
312 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
313 else
314 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
315 }
316 }
317}
318
aliguori2724b182009-03-05 23:01:47 +0000319static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
320{
321 if (d->chr_event[mux_nr])
322 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
323}
324
aliguori6f97dba2008-10-31 18:49:55 +0000325static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
326{
327 if (d->term_got_escape) {
328 d->term_got_escape = 0;
329 if (ch == term_escape_char)
330 goto send_char;
331 switch(ch) {
332 case '?':
333 case 'h':
334 mux_print_help(chr);
335 break;
336 case 'x':
337 {
338 const char *term = "QEMU: Terminated\n\r";
339 chr->chr_write(chr,(uint8_t *)term,strlen(term));
340 exit(0);
341 break;
342 }
343 case 's':
344 {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200345 DriveInfo *dinfo;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000346 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200347 bdrv_commit(dinfo->bdrv);
aliguori6f97dba2008-10-31 18:49:55 +0000348 }
349 }
350 break;
351 case 'b':
352 qemu_chr_event(chr, CHR_EVENT_BREAK);
353 break;
354 case 'c':
355 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200356 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
357 d->focus++;
358 if (d->focus >= d->mux_cnt)
359 d->focus = 0;
360 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000361 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200362 case 't':
363 d->timestamps = !d->timestamps;
364 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200365 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200366 break;
aliguori6f97dba2008-10-31 18:49:55 +0000367 }
368 } else if (ch == term_escape_char) {
369 d->term_got_escape = 1;
370 } else {
371 send_char:
372 return 1;
373 }
374 return 0;
375}
376
377static void mux_chr_accept_input(CharDriverState *chr)
378{
aliguori6f97dba2008-10-31 18:49:55 +0000379 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200380 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000381
aliguoria80bf992009-03-05 23:00:02 +0000382 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000383 d->chr_can_read[m] &&
384 d->chr_can_read[m](d->ext_opaque[m])) {
385 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000386 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000387 }
388}
389
390static int mux_chr_can_read(void *opaque)
391{
392 CharDriverState *chr = opaque;
393 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200394 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000395
aliguoria80bf992009-03-05 23:00:02 +0000396 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000397 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000398 if (d->chr_can_read[m])
399 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000400 return 0;
401}
402
403static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
404{
405 CharDriverState *chr = opaque;
406 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200407 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000408 int i;
409
410 mux_chr_accept_input (opaque);
411
412 for(i = 0; i < size; i++)
413 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000414 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000415 d->chr_can_read[m] &&
416 d->chr_can_read[m](d->ext_opaque[m]))
417 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
418 else
aliguoria80bf992009-03-05 23:00:02 +0000419 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000420 }
421}
422
423static void mux_chr_event(void *opaque, int event)
424{
425 CharDriverState *chr = opaque;
426 MuxDriver *d = chr->opaque;
427 int i;
428
429 /* Send the event to all registered listeners */
430 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000431 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000432}
433
434static void mux_chr_update_read_handler(CharDriverState *chr)
435{
436 MuxDriver *d = chr->opaque;
437
438 if (d->mux_cnt >= MAX_MUX) {
439 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
440 return;
441 }
442 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
443 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
444 d->chr_read[d->mux_cnt] = chr->chr_read;
445 d->chr_event[d->mux_cnt] = chr->chr_event;
446 /* Fix up the real driver with mux routines */
447 if (d->mux_cnt == 0) {
448 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
449 mux_chr_event, chr);
450 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200451 if (d->focus != -1) {
452 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200453 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200454 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000455 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200456 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000457}
458
459static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
460{
461 CharDriverState *chr;
462 MuxDriver *d;
463
464 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000465 d = qemu_mallocz(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000466
467 chr->opaque = d;
468 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200469 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000470 chr->chr_write = mux_chr_write;
471 chr->chr_update_read_handler = mux_chr_update_read_handler;
472 chr->chr_accept_input = mux_chr_accept_input;
473 return chr;
474}
475
476
477#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000478int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000479{
480 int ret, len;
481
482 len = len1;
483 while (len > 0) {
484 ret = send(fd, buf, len, 0);
485 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000486 errno = WSAGetLastError();
487 if (errno != WSAEWOULDBLOCK) {
488 return -1;
489 }
490 } else if (ret == 0) {
491 break;
492 } else {
493 buf += ret;
494 len -= ret;
495 }
496 }
497 return len1 - len;
498}
499
500#else
501
502static int unix_write(int fd, const uint8_t *buf, int len1)
503{
504 int ret, len;
505
506 len = len1;
507 while (len > 0) {
508 ret = write(fd, buf, len);
509 if (ret < 0) {
510 if (errno != EINTR && errno != EAGAIN)
511 return -1;
512 } else if (ret == 0) {
513 break;
514 } else {
515 buf += ret;
516 len -= ret;
517 }
518 }
519 return len1 - len;
520}
521
aliguorid247d252008-11-11 20:46:40 +0000522int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000523{
524 return unix_write(fd, buf, len1);
525}
526#endif /* !_WIN32 */
527
528#ifndef _WIN32
529
530typedef struct {
531 int fd_in, fd_out;
532 int max_size;
533} FDCharDriver;
534
535#define STDIO_MAX_CLIENTS 1
536static int stdio_nb_clients = 0;
537
538static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
539{
540 FDCharDriver *s = chr->opaque;
541 return send_all(s->fd_out, buf, len);
542}
543
544static int fd_chr_read_poll(void *opaque)
545{
546 CharDriverState *chr = opaque;
547 FDCharDriver *s = chr->opaque;
548
549 s->max_size = qemu_chr_can_read(chr);
550 return s->max_size;
551}
552
553static void fd_chr_read(void *opaque)
554{
555 CharDriverState *chr = opaque;
556 FDCharDriver *s = chr->opaque;
557 int size, len;
558 uint8_t buf[1024];
559
560 len = sizeof(buf);
561 if (len > s->max_size)
562 len = s->max_size;
563 if (len == 0)
564 return;
565 size = read(s->fd_in, buf, len);
566 if (size == 0) {
567 /* FD has been closed. Remove it from the active list. */
568 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530569 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000570 return;
571 }
572 if (size > 0) {
573 qemu_chr_read(chr, buf, size);
574 }
575}
576
577static void fd_chr_update_read_handler(CharDriverState *chr)
578{
579 FDCharDriver *s = chr->opaque;
580
581 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500582 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000583 } else {
584 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
585 fd_chr_read, NULL, chr);
586 }
587 }
588}
589
590static void fd_chr_close(struct CharDriverState *chr)
591{
592 FDCharDriver *s = chr->opaque;
593
594 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500595 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000596 } else {
597 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
598 }
599 }
600
601 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530602 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000603}
604
605/* open a character device to a unix fd */
606static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
607{
608 CharDriverState *chr;
609 FDCharDriver *s;
610
611 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000612 s = qemu_mallocz(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000613 s->fd_in = fd_in;
614 s->fd_out = fd_out;
615 chr->opaque = s;
616 chr->chr_write = fd_chr_write;
617 chr->chr_update_read_handler = fd_chr_update_read_handler;
618 chr->chr_close = fd_chr_close;
619
620 qemu_chr_reset(chr);
621
622 return chr;
623}
624
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200625static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000626{
627 int fd_out;
628
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200629 TFR(fd_out = open(qemu_opt_get(opts, "path"),
630 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
aliguori6f97dba2008-10-31 18:49:55 +0000631 if (fd_out < 0)
632 return NULL;
633 return qemu_chr_open_fd(-1, fd_out);
634}
635
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200636static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000637{
638 int fd_in, fd_out;
639 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200640 const char *filename = qemu_opt_get(opts, "path");
641
642 if (filename == NULL) {
643 fprintf(stderr, "chardev: pipe: no filename given\n");
644 return NULL;
645 }
aliguori6f97dba2008-10-31 18:49:55 +0000646
647 snprintf(filename_in, 256, "%s.in", filename);
648 snprintf(filename_out, 256, "%s.out", filename);
649 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
650 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
651 if (fd_in < 0 || fd_out < 0) {
652 if (fd_in >= 0)
653 close(fd_in);
654 if (fd_out >= 0)
655 close(fd_out);
656 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
657 if (fd_in < 0)
658 return NULL;
659 }
660 return qemu_chr_open_fd(fd_in, fd_out);
661}
662
663
664/* for STDIO, we handle the case where several clients use it
665 (nographic mode) */
666
667#define TERM_FIFO_MAX_SIZE 1
668
669static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
670static int term_fifo_size;
671
672static int stdio_read_poll(void *opaque)
673{
674 CharDriverState *chr = opaque;
675
676 /* try to flush the queue if needed */
677 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
678 qemu_chr_read(chr, term_fifo, 1);
679 term_fifo_size = 0;
680 }
681 /* see if we can absorb more chars */
682 if (term_fifo_size == 0)
683 return 1;
684 else
685 return 0;
686}
687
688static void stdio_read(void *opaque)
689{
690 int size;
691 uint8_t buf[1];
692 CharDriverState *chr = opaque;
693
694 size = read(0, buf, 1);
695 if (size == 0) {
696 /* stdin has been closed. Remove it from the active list. */
697 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530698 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000699 return;
700 }
701 if (size > 0) {
702 if (qemu_chr_can_read(chr) > 0) {
703 qemu_chr_read(chr, buf, 1);
704 } else if (term_fifo_size == 0) {
705 term_fifo[term_fifo_size++] = buf[0];
706 }
707 }
708}
709
710/* init terminal so that we can grab keys */
711static struct termios oldtty;
712static int old_fd0_flags;
713static int term_atexit_done;
714
715static void term_exit(void)
716{
717 tcsetattr (0, TCSANOW, &oldtty);
718 fcntl(0, F_SETFL, old_fd0_flags);
719}
720
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900721static void term_init(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000722{
723 struct termios tty;
724
725 tcgetattr (0, &tty);
726 oldtty = tty;
727 old_fd0_flags = fcntl(0, F_GETFL);
728
729 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
730 |INLCR|IGNCR|ICRNL|IXON);
731 tty.c_oflag |= OPOST;
732 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
733 /* if graphical mode, we allow Ctrl-C handling */
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900734 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
aliguori6f97dba2008-10-31 18:49:55 +0000735 tty.c_lflag &= ~ISIG;
736 tty.c_cflag &= ~(CSIZE|PARENB);
737 tty.c_cflag |= CS8;
738 tty.c_cc[VMIN] = 1;
739 tty.c_cc[VTIME] = 0;
740
741 tcsetattr (0, TCSANOW, &tty);
742
743 if (!term_atexit_done++)
744 atexit(term_exit);
745
746 fcntl(0, F_SETFL, O_NONBLOCK);
747}
748
749static void qemu_chr_close_stdio(struct CharDriverState *chr)
750{
751 term_exit();
752 stdio_nb_clients--;
753 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
754 fd_chr_close(chr);
755}
756
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +0200757static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000758{
759 CharDriverState *chr;
760
761 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
762 return NULL;
763 chr = qemu_chr_open_fd(0, 1);
764 chr->chr_close = qemu_chr_close_stdio;
765 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
766 stdio_nb_clients++;
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900767 term_init(opts);
aliguori6f97dba2008-10-31 18:49:55 +0000768
769 return chr;
770}
771
772#ifdef __sun__
773/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000774static int openpty(int *amaster, int *aslave, char *name,
775 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000776{
777 const char *slave;
778 int mfd = -1, sfd = -1;
779
780 *amaster = *aslave = -1;
781
782 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
783 if (mfd < 0)
784 goto err;
785
786 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
787 goto err;
788
789 if ((slave = ptsname(mfd)) == NULL)
790 goto err;
791
792 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
793 goto err;
794
795 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
796 (termp != NULL && tcgetattr(sfd, termp) < 0))
797 goto err;
798
799 if (amaster)
800 *amaster = mfd;
801 if (aslave)
802 *aslave = sfd;
803 if (winp)
804 ioctl(sfd, TIOCSWINSZ, winp);
805
806 return 0;
807
808err:
809 if (sfd != -1)
810 close(sfd);
811 close(mfd);
812 return -1;
813}
814
blueswir13f4cb3d2009-04-13 16:31:01 +0000815static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000816{
817 termios_p->c_iflag &=
818 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
819 termios_p->c_oflag &= ~OPOST;
820 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
821 termios_p->c_cflag &= ~(CSIZE|PARENB);
822 termios_p->c_cflag |= CS8;
823
824 termios_p->c_cc[VMIN] = 0;
825 termios_p->c_cc[VTIME] = 0;
826}
827#endif
828
829#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
blueswir1c5e97232009-03-07 20:06:23 +0000830 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000831
832typedef struct {
833 int fd;
834 int connected;
835 int polling;
836 int read_bytes;
837 QEMUTimer *timer;
838} PtyCharDriver;
839
840static void pty_chr_update_read_handler(CharDriverState *chr);
841static void pty_chr_state(CharDriverState *chr, int connected);
842
843static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
844{
845 PtyCharDriver *s = chr->opaque;
846
847 if (!s->connected) {
848 /* guest sends data, check for (re-)connect */
849 pty_chr_update_read_handler(chr);
850 return 0;
851 }
852 return send_all(s->fd, buf, len);
853}
854
855static int pty_chr_read_poll(void *opaque)
856{
857 CharDriverState *chr = opaque;
858 PtyCharDriver *s = chr->opaque;
859
860 s->read_bytes = qemu_chr_can_read(chr);
861 return s->read_bytes;
862}
863
864static void pty_chr_read(void *opaque)
865{
866 CharDriverState *chr = opaque;
867 PtyCharDriver *s = chr->opaque;
868 int size, len;
869 uint8_t buf[1024];
870
871 len = sizeof(buf);
872 if (len > s->read_bytes)
873 len = s->read_bytes;
874 if (len == 0)
875 return;
876 size = read(s->fd, buf, len);
877 if ((size == -1 && errno == EIO) ||
878 (size == 0)) {
879 pty_chr_state(chr, 0);
880 return;
881 }
882 if (size > 0) {
883 pty_chr_state(chr, 1);
884 qemu_chr_read(chr, buf, size);
885 }
886}
887
888static void pty_chr_update_read_handler(CharDriverState *chr)
889{
890 PtyCharDriver *s = chr->opaque;
891
892 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
893 pty_chr_read, NULL, chr);
894 s->polling = 1;
895 /*
896 * Short timeout here: just need wait long enougth that qemu makes
897 * it through the poll loop once. When reconnected we want a
898 * short timeout so we notice it almost instantly. Otherwise
899 * read() gives us -EIO instantly, making pty_chr_state() reset the
900 * timeout to the normal (much longer) poll interval before the
901 * timer triggers.
902 */
903 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
904}
905
906static void pty_chr_state(CharDriverState *chr, int connected)
907{
908 PtyCharDriver *s = chr->opaque;
909
910 if (!connected) {
911 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
912 s->connected = 0;
913 s->polling = 0;
914 /* (re-)connect poll interval for idle guests: once per second.
915 * We check more frequently in case the guests sends data to
916 * the virtual device linked to our pty. */
917 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
918 } else {
919 if (!s->connected)
920 qemu_chr_reset(chr);
921 s->connected = 1;
922 }
923}
924
925static void pty_chr_timer(void *opaque)
926{
927 struct CharDriverState *chr = opaque;
928 PtyCharDriver *s = chr->opaque;
929
930 if (s->connected)
931 return;
932 if (s->polling) {
933 /* If we arrive here without polling being cleared due
934 * read returning -EIO, then we are (re-)connected */
935 pty_chr_state(chr, 1);
936 return;
937 }
938
939 /* Next poll ... */
940 pty_chr_update_read_handler(chr);
941}
942
943static void pty_chr_close(struct CharDriverState *chr)
944{
945 PtyCharDriver *s = chr->opaque;
946
947 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
948 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000949 qemu_del_timer(s->timer);
950 qemu_free_timer(s->timer);
aliguori6f97dba2008-10-31 18:49:55 +0000951 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530952 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000953}
954
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200955static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000956{
957 CharDriverState *chr;
958 PtyCharDriver *s;
959 struct termios tty;
960 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000961#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000962 char pty_name[PATH_MAX];
963#define q_ptsname(x) pty_name
964#else
965 char *pty_name = NULL;
966#define q_ptsname(x) ptsname(x)
967#endif
968
969 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000970 s = qemu_mallocz(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000971
972 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
973 return NULL;
974 }
975
976 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +0000977 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000978 cfmakeraw(&tty);
979 tcsetattr(slave_fd, TCSAFLUSH, &tty);
980 close(slave_fd);
981
982 len = strlen(q_ptsname(s->fd)) + 5;
983 chr->filename = qemu_malloc(len);
984 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200985 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +0000986 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
987
988 chr->opaque = s;
989 chr->chr_write = pty_chr_write;
990 chr->chr_update_read_handler = pty_chr_update_read_handler;
991 chr->chr_close = pty_chr_close;
992
993 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
994
995 return chr;
996}
997
998static void tty_serial_init(int fd, int speed,
999 int parity, int data_bits, int stop_bits)
1000{
1001 struct termios tty;
1002 speed_t spd;
1003
1004#if 0
1005 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1006 speed, parity, data_bits, stop_bits);
1007#endif
1008 tcgetattr (fd, &tty);
1009
1010#define MARGIN 1.1
1011 if (speed <= 50 * MARGIN)
1012 spd = B50;
1013 else if (speed <= 75 * MARGIN)
1014 spd = B75;
1015 else if (speed <= 300 * MARGIN)
1016 spd = B300;
1017 else if (speed <= 600 * MARGIN)
1018 spd = B600;
1019 else if (speed <= 1200 * MARGIN)
1020 spd = B1200;
1021 else if (speed <= 2400 * MARGIN)
1022 spd = B2400;
1023 else if (speed <= 4800 * MARGIN)
1024 spd = B4800;
1025 else if (speed <= 9600 * MARGIN)
1026 spd = B9600;
1027 else if (speed <= 19200 * MARGIN)
1028 spd = B19200;
1029 else if (speed <= 38400 * MARGIN)
1030 spd = B38400;
1031 else if (speed <= 57600 * MARGIN)
1032 spd = B57600;
1033 else if (speed <= 115200 * MARGIN)
1034 spd = B115200;
1035 else
1036 spd = B115200;
1037
1038 cfsetispeed(&tty, spd);
1039 cfsetospeed(&tty, spd);
1040
1041 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1042 |INLCR|IGNCR|ICRNL|IXON);
1043 tty.c_oflag |= OPOST;
1044 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1045 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1046 switch(data_bits) {
1047 default:
1048 case 8:
1049 tty.c_cflag |= CS8;
1050 break;
1051 case 7:
1052 tty.c_cflag |= CS7;
1053 break;
1054 case 6:
1055 tty.c_cflag |= CS6;
1056 break;
1057 case 5:
1058 tty.c_cflag |= CS5;
1059 break;
1060 }
1061 switch(parity) {
1062 default:
1063 case 'N':
1064 break;
1065 case 'E':
1066 tty.c_cflag |= PARENB;
1067 break;
1068 case 'O':
1069 tty.c_cflag |= PARENB | PARODD;
1070 break;
1071 }
1072 if (stop_bits == 2)
1073 tty.c_cflag |= CSTOPB;
1074
1075 tcsetattr (fd, TCSANOW, &tty);
1076}
1077
1078static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1079{
1080 FDCharDriver *s = chr->opaque;
1081
1082 switch(cmd) {
1083 case CHR_IOCTL_SERIAL_SET_PARAMS:
1084 {
1085 QEMUSerialSetParams *ssp = arg;
1086 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1087 ssp->data_bits, ssp->stop_bits);
1088 }
1089 break;
1090 case CHR_IOCTL_SERIAL_SET_BREAK:
1091 {
1092 int enable = *(int *)arg;
1093 if (enable)
1094 tcsendbreak(s->fd_in, 1);
1095 }
1096 break;
1097 case CHR_IOCTL_SERIAL_GET_TIOCM:
1098 {
1099 int sarg = 0;
1100 int *targ = (int *)arg;
1101 ioctl(s->fd_in, TIOCMGET, &sarg);
1102 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001103 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001104 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001105 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001106 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001107 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001108 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001109 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001110 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001111 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001112 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001113 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001114 *targ |= CHR_TIOCM_RTS;
1115 }
1116 break;
1117 case CHR_IOCTL_SERIAL_SET_TIOCM:
1118 {
1119 int sarg = *(int *)arg;
1120 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001121 ioctl(s->fd_in, TIOCMGET, &targ);
1122 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1123 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1124 if (sarg & CHR_TIOCM_CTS)
1125 targ |= TIOCM_CTS;
1126 if (sarg & CHR_TIOCM_CAR)
1127 targ |= TIOCM_CAR;
1128 if (sarg & CHR_TIOCM_DSR)
1129 targ |= TIOCM_DSR;
1130 if (sarg & CHR_TIOCM_RI)
1131 targ |= TIOCM_RI;
1132 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001133 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001134 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001135 targ |= TIOCM_RTS;
1136 ioctl(s->fd_in, TIOCMSET, &targ);
1137 }
1138 break;
1139 default:
1140 return -ENOTSUP;
1141 }
1142 return 0;
1143}
1144
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001145static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001146{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001147 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001148 CharDriverState *chr;
1149 int fd;
1150
1151 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1152 tty_serial_init(fd, 115200, 'N', 8, 1);
1153 chr = qemu_chr_open_fd(fd, fd);
1154 if (!chr) {
1155 close(fd);
1156 return NULL;
1157 }
1158 chr->chr_ioctl = tty_serial_ioctl;
1159 qemu_chr_reset(chr);
1160 return chr;
1161}
1162#else /* ! __linux__ && ! __sun__ */
1163static CharDriverState *qemu_chr_open_pty(void)
1164{
1165 return NULL;
1166}
1167#endif /* __linux__ || __sun__ */
1168
1169#if defined(__linux__)
1170typedef struct {
1171 int fd;
1172 int mode;
1173} ParallelCharDriver;
1174
1175static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1176{
1177 if (s->mode != mode) {
1178 int m = mode;
1179 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1180 return 0;
1181 s->mode = mode;
1182 }
1183 return 1;
1184}
1185
1186static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1187{
1188 ParallelCharDriver *drv = chr->opaque;
1189 int fd = drv->fd;
1190 uint8_t b;
1191
1192 switch(cmd) {
1193 case CHR_IOCTL_PP_READ_DATA:
1194 if (ioctl(fd, PPRDATA, &b) < 0)
1195 return -ENOTSUP;
1196 *(uint8_t *)arg = b;
1197 break;
1198 case CHR_IOCTL_PP_WRITE_DATA:
1199 b = *(uint8_t *)arg;
1200 if (ioctl(fd, PPWDATA, &b) < 0)
1201 return -ENOTSUP;
1202 break;
1203 case CHR_IOCTL_PP_READ_CONTROL:
1204 if (ioctl(fd, PPRCONTROL, &b) < 0)
1205 return -ENOTSUP;
1206 /* Linux gives only the lowest bits, and no way to know data
1207 direction! For better compatibility set the fixed upper
1208 bits. */
1209 *(uint8_t *)arg = b | 0xc0;
1210 break;
1211 case CHR_IOCTL_PP_WRITE_CONTROL:
1212 b = *(uint8_t *)arg;
1213 if (ioctl(fd, PPWCONTROL, &b) < 0)
1214 return -ENOTSUP;
1215 break;
1216 case CHR_IOCTL_PP_READ_STATUS:
1217 if (ioctl(fd, PPRSTATUS, &b) < 0)
1218 return -ENOTSUP;
1219 *(uint8_t *)arg = b;
1220 break;
1221 case CHR_IOCTL_PP_DATA_DIR:
1222 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1223 return -ENOTSUP;
1224 break;
1225 case CHR_IOCTL_PP_EPP_READ_ADDR:
1226 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1227 struct ParallelIOArg *parg = arg;
1228 int n = read(fd, parg->buffer, parg->count);
1229 if (n != parg->count) {
1230 return -EIO;
1231 }
1232 }
1233 break;
1234 case CHR_IOCTL_PP_EPP_READ:
1235 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1236 struct ParallelIOArg *parg = arg;
1237 int n = read(fd, parg->buffer, parg->count);
1238 if (n != parg->count) {
1239 return -EIO;
1240 }
1241 }
1242 break;
1243 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1244 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1245 struct ParallelIOArg *parg = arg;
1246 int n = write(fd, parg->buffer, parg->count);
1247 if (n != parg->count) {
1248 return -EIO;
1249 }
1250 }
1251 break;
1252 case CHR_IOCTL_PP_EPP_WRITE:
1253 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1254 struct ParallelIOArg *parg = arg;
1255 int n = write(fd, parg->buffer, parg->count);
1256 if (n != parg->count) {
1257 return -EIO;
1258 }
1259 }
1260 break;
1261 default:
1262 return -ENOTSUP;
1263 }
1264 return 0;
1265}
1266
1267static void pp_close(CharDriverState *chr)
1268{
1269 ParallelCharDriver *drv = chr->opaque;
1270 int fd = drv->fd;
1271
1272 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1273 ioctl(fd, PPRELEASE);
1274 close(fd);
1275 qemu_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301276 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001277}
1278
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001279static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001280{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001281 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001282 CharDriverState *chr;
1283 ParallelCharDriver *drv;
1284 int fd;
1285
1286 TFR(fd = open(filename, O_RDWR));
1287 if (fd < 0)
1288 return NULL;
1289
1290 if (ioctl(fd, PPCLAIM) < 0) {
1291 close(fd);
1292 return NULL;
1293 }
1294
1295 drv = qemu_mallocz(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001296 drv->fd = fd;
1297 drv->mode = IEEE1284_MODE_COMPAT;
1298
1299 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001300 chr->chr_write = null_chr_write;
1301 chr->chr_ioctl = pp_ioctl;
1302 chr->chr_close = pp_close;
1303 chr->opaque = drv;
1304
1305 qemu_chr_reset(chr);
1306
1307 return chr;
1308}
1309#endif /* __linux__ */
1310
blueswir1c5e97232009-03-07 20:06:23 +00001311#if defined(__FreeBSD__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001312static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1313{
1314 int fd = (int)chr->opaque;
1315 uint8_t b;
1316
1317 switch(cmd) {
1318 case CHR_IOCTL_PP_READ_DATA:
1319 if (ioctl(fd, PPIGDATA, &b) < 0)
1320 return -ENOTSUP;
1321 *(uint8_t *)arg = b;
1322 break;
1323 case CHR_IOCTL_PP_WRITE_DATA:
1324 b = *(uint8_t *)arg;
1325 if (ioctl(fd, PPISDATA, &b) < 0)
1326 return -ENOTSUP;
1327 break;
1328 case CHR_IOCTL_PP_READ_CONTROL:
1329 if (ioctl(fd, PPIGCTRL, &b) < 0)
1330 return -ENOTSUP;
1331 *(uint8_t *)arg = b;
1332 break;
1333 case CHR_IOCTL_PP_WRITE_CONTROL:
1334 b = *(uint8_t *)arg;
1335 if (ioctl(fd, PPISCTRL, &b) < 0)
1336 return -ENOTSUP;
1337 break;
1338 case CHR_IOCTL_PP_READ_STATUS:
1339 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1340 return -ENOTSUP;
1341 *(uint8_t *)arg = b;
1342 break;
1343 default:
1344 return -ENOTSUP;
1345 }
1346 return 0;
1347}
1348
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001349static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001350{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001351 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001352 CharDriverState *chr;
1353 int fd;
1354
1355 fd = open(filename, O_RDWR);
1356 if (fd < 0)
1357 return NULL;
1358
1359 chr = qemu_mallocz(sizeof(CharDriverState));
blueswir16972f932008-11-22 20:49:12 +00001360 chr->opaque = (void *)fd;
1361 chr->chr_write = null_chr_write;
1362 chr->chr_ioctl = pp_ioctl;
1363 return chr;
1364}
1365#endif
1366
aliguori6f97dba2008-10-31 18:49:55 +00001367#else /* _WIN32 */
1368
1369typedef struct {
1370 int max_size;
1371 HANDLE hcom, hrecv, hsend;
1372 OVERLAPPED orecv, osend;
1373 BOOL fpipe;
1374 DWORD len;
1375} WinCharState;
1376
1377#define NSENDBUF 2048
1378#define NRECVBUF 2048
1379#define MAXCONNECT 1
1380#define NTIMEOUT 5000
1381
1382static int win_chr_poll(void *opaque);
1383static int win_chr_pipe_poll(void *opaque);
1384
1385static void win_chr_close(CharDriverState *chr)
1386{
1387 WinCharState *s = chr->opaque;
1388
1389 if (s->hsend) {
1390 CloseHandle(s->hsend);
1391 s->hsend = NULL;
1392 }
1393 if (s->hrecv) {
1394 CloseHandle(s->hrecv);
1395 s->hrecv = NULL;
1396 }
1397 if (s->hcom) {
1398 CloseHandle(s->hcom);
1399 s->hcom = NULL;
1400 }
1401 if (s->fpipe)
1402 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1403 else
1404 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301405
1406 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001407}
1408
1409static int win_chr_init(CharDriverState *chr, const char *filename)
1410{
1411 WinCharState *s = chr->opaque;
1412 COMMCONFIG comcfg;
1413 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1414 COMSTAT comstat;
1415 DWORD size;
1416 DWORD err;
1417
1418 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1419 if (!s->hsend) {
1420 fprintf(stderr, "Failed CreateEvent\n");
1421 goto fail;
1422 }
1423 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1424 if (!s->hrecv) {
1425 fprintf(stderr, "Failed CreateEvent\n");
1426 goto fail;
1427 }
1428
1429 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1430 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1431 if (s->hcom == INVALID_HANDLE_VALUE) {
1432 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1433 s->hcom = NULL;
1434 goto fail;
1435 }
1436
1437 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1438 fprintf(stderr, "Failed SetupComm\n");
1439 goto fail;
1440 }
1441
1442 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1443 size = sizeof(COMMCONFIG);
1444 GetDefaultCommConfig(filename, &comcfg, &size);
1445 comcfg.dcb.DCBlength = sizeof(DCB);
1446 CommConfigDialog(filename, NULL, &comcfg);
1447
1448 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1449 fprintf(stderr, "Failed SetCommState\n");
1450 goto fail;
1451 }
1452
1453 if (!SetCommMask(s->hcom, EV_ERR)) {
1454 fprintf(stderr, "Failed SetCommMask\n");
1455 goto fail;
1456 }
1457
1458 cto.ReadIntervalTimeout = MAXDWORD;
1459 if (!SetCommTimeouts(s->hcom, &cto)) {
1460 fprintf(stderr, "Failed SetCommTimeouts\n");
1461 goto fail;
1462 }
1463
1464 if (!ClearCommError(s->hcom, &err, &comstat)) {
1465 fprintf(stderr, "Failed ClearCommError\n");
1466 goto fail;
1467 }
1468 qemu_add_polling_cb(win_chr_poll, chr);
1469 return 0;
1470
1471 fail:
1472 win_chr_close(chr);
1473 return -1;
1474}
1475
1476static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1477{
1478 WinCharState *s = chr->opaque;
1479 DWORD len, ret, size, err;
1480
1481 len = len1;
1482 ZeroMemory(&s->osend, sizeof(s->osend));
1483 s->osend.hEvent = s->hsend;
1484 while (len > 0) {
1485 if (s->hsend)
1486 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1487 else
1488 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1489 if (!ret) {
1490 err = GetLastError();
1491 if (err == ERROR_IO_PENDING) {
1492 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1493 if (ret) {
1494 buf += size;
1495 len -= size;
1496 } else {
1497 break;
1498 }
1499 } else {
1500 break;
1501 }
1502 } else {
1503 buf += size;
1504 len -= size;
1505 }
1506 }
1507 return len1 - len;
1508}
1509
1510static int win_chr_read_poll(CharDriverState *chr)
1511{
1512 WinCharState *s = chr->opaque;
1513
1514 s->max_size = qemu_chr_can_read(chr);
1515 return s->max_size;
1516}
1517
1518static void win_chr_readfile(CharDriverState *chr)
1519{
1520 WinCharState *s = chr->opaque;
1521 int ret, err;
1522 uint8_t buf[1024];
1523 DWORD size;
1524
1525 ZeroMemory(&s->orecv, sizeof(s->orecv));
1526 s->orecv.hEvent = s->hrecv;
1527 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1528 if (!ret) {
1529 err = GetLastError();
1530 if (err == ERROR_IO_PENDING) {
1531 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1532 }
1533 }
1534
1535 if (size > 0) {
1536 qemu_chr_read(chr, buf, size);
1537 }
1538}
1539
1540static void win_chr_read(CharDriverState *chr)
1541{
1542 WinCharState *s = chr->opaque;
1543
1544 if (s->len > s->max_size)
1545 s->len = s->max_size;
1546 if (s->len == 0)
1547 return;
1548
1549 win_chr_readfile(chr);
1550}
1551
1552static int win_chr_poll(void *opaque)
1553{
1554 CharDriverState *chr = opaque;
1555 WinCharState *s = chr->opaque;
1556 COMSTAT status;
1557 DWORD comerr;
1558
1559 ClearCommError(s->hcom, &comerr, &status);
1560 if (status.cbInQue > 0) {
1561 s->len = status.cbInQue;
1562 win_chr_read_poll(chr);
1563 win_chr_read(chr);
1564 return 1;
1565 }
1566 return 0;
1567}
1568
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001569static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001570{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001571 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001572 CharDriverState *chr;
1573 WinCharState *s;
1574
1575 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001576 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001577 chr->opaque = s;
1578 chr->chr_write = win_chr_write;
1579 chr->chr_close = win_chr_close;
1580
1581 if (win_chr_init(chr, filename) < 0) {
1582 free(s);
1583 free(chr);
1584 return NULL;
1585 }
1586 qemu_chr_reset(chr);
1587 return chr;
1588}
1589
1590static int win_chr_pipe_poll(void *opaque)
1591{
1592 CharDriverState *chr = opaque;
1593 WinCharState *s = chr->opaque;
1594 DWORD size;
1595
1596 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1597 if (size > 0) {
1598 s->len = size;
1599 win_chr_read_poll(chr);
1600 win_chr_read(chr);
1601 return 1;
1602 }
1603 return 0;
1604}
1605
1606static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1607{
1608 WinCharState *s = chr->opaque;
1609 OVERLAPPED ov;
1610 int ret;
1611 DWORD size;
1612 char openname[256];
1613
1614 s->fpipe = TRUE;
1615
1616 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1617 if (!s->hsend) {
1618 fprintf(stderr, "Failed CreateEvent\n");
1619 goto fail;
1620 }
1621 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1622 if (!s->hrecv) {
1623 fprintf(stderr, "Failed CreateEvent\n");
1624 goto fail;
1625 }
1626
1627 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1628 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1629 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1630 PIPE_WAIT,
1631 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1632 if (s->hcom == INVALID_HANDLE_VALUE) {
1633 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1634 s->hcom = NULL;
1635 goto fail;
1636 }
1637
1638 ZeroMemory(&ov, sizeof(ov));
1639 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1640 ret = ConnectNamedPipe(s->hcom, &ov);
1641 if (ret) {
1642 fprintf(stderr, "Failed ConnectNamedPipe\n");
1643 goto fail;
1644 }
1645
1646 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1647 if (!ret) {
1648 fprintf(stderr, "Failed GetOverlappedResult\n");
1649 if (ov.hEvent) {
1650 CloseHandle(ov.hEvent);
1651 ov.hEvent = NULL;
1652 }
1653 goto fail;
1654 }
1655
1656 if (ov.hEvent) {
1657 CloseHandle(ov.hEvent);
1658 ov.hEvent = NULL;
1659 }
1660 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1661 return 0;
1662
1663 fail:
1664 win_chr_close(chr);
1665 return -1;
1666}
1667
1668
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001669static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001670{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001671 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001672 CharDriverState *chr;
1673 WinCharState *s;
1674
1675 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001676 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001677 chr->opaque = s;
1678 chr->chr_write = win_chr_write;
1679 chr->chr_close = win_chr_close;
1680
1681 if (win_chr_pipe_init(chr, filename) < 0) {
1682 free(s);
1683 free(chr);
1684 return NULL;
1685 }
1686 qemu_chr_reset(chr);
1687 return chr;
1688}
1689
1690static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1691{
1692 CharDriverState *chr;
1693 WinCharState *s;
1694
1695 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001696 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001697 s->hcom = fd_out;
1698 chr->opaque = s;
1699 chr->chr_write = win_chr_write;
1700 qemu_chr_reset(chr);
1701 return chr;
1702}
1703
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02001704static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001705{
1706 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1707}
1708
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001709static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001710{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001711 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001712 HANDLE fd_out;
1713
1714 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1715 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1716 if (fd_out == INVALID_HANDLE_VALUE)
1717 return NULL;
1718
1719 return qemu_chr_open_win_file(fd_out);
1720}
1721#endif /* !_WIN32 */
1722
1723/***********************************************************/
1724/* UDP Net console */
1725
1726typedef struct {
1727 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001728 uint8_t buf[1024];
1729 int bufcnt;
1730 int bufptr;
1731 int max_size;
1732} NetCharDriver;
1733
1734static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1735{
1736 NetCharDriver *s = chr->opaque;
1737
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001738 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00001739}
1740
1741static int udp_chr_read_poll(void *opaque)
1742{
1743 CharDriverState *chr = opaque;
1744 NetCharDriver *s = chr->opaque;
1745
1746 s->max_size = qemu_chr_can_read(chr);
1747
1748 /* If there were any stray characters in the queue process them
1749 * first
1750 */
1751 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1752 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1753 s->bufptr++;
1754 s->max_size = qemu_chr_can_read(chr);
1755 }
1756 return s->max_size;
1757}
1758
1759static void udp_chr_read(void *opaque)
1760{
1761 CharDriverState *chr = opaque;
1762 NetCharDriver *s = chr->opaque;
1763
1764 if (s->max_size == 0)
1765 return;
Blue Swirlc5b76b32009-06-13 08:44:31 +00001766 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00001767 s->bufptr = s->bufcnt;
1768 if (s->bufcnt <= 0)
1769 return;
1770
1771 s->bufptr = 0;
1772 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1773 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1774 s->bufptr++;
1775 s->max_size = qemu_chr_can_read(chr);
1776 }
1777}
1778
1779static void udp_chr_update_read_handler(CharDriverState *chr)
1780{
1781 NetCharDriver *s = chr->opaque;
1782
1783 if (s->fd >= 0) {
1784 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1785 udp_chr_read, NULL, chr);
1786 }
1787}
1788
aliguori819f56b2009-03-28 17:58:14 +00001789static void udp_chr_close(CharDriverState *chr)
1790{
1791 NetCharDriver *s = chr->opaque;
1792 if (s->fd >= 0) {
1793 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1794 closesocket(s->fd);
1795 }
1796 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05301797 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00001798}
1799
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001800static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001801{
1802 CharDriverState *chr = NULL;
1803 NetCharDriver *s = NULL;
1804 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00001805
1806 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001807 s = qemu_mallocz(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001808
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001809 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00001810 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001811 fprintf(stderr, "inet_dgram_opts failed\n");
aliguori6f97dba2008-10-31 18:49:55 +00001812 goto return_err;
1813 }
1814
1815 s->fd = fd;
1816 s->bufcnt = 0;
1817 s->bufptr = 0;
1818 chr->opaque = s;
1819 chr->chr_write = udp_chr_write;
1820 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00001821 chr->chr_close = udp_chr_close;
aliguori6f97dba2008-10-31 18:49:55 +00001822 return chr;
1823
1824return_err:
1825 if (chr)
1826 free(chr);
1827 if (s)
1828 free(s);
1829 if (fd >= 0)
1830 closesocket(fd);
1831 return NULL;
1832}
1833
1834/***********************************************************/
1835/* TCP Net console */
1836
1837typedef struct {
1838 int fd, listen_fd;
1839 int connected;
1840 int max_size;
1841 int do_telnetopt;
1842 int do_nodelay;
1843 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001844 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00001845} TCPCharDriver;
1846
1847static void tcp_chr_accept(void *opaque);
1848
1849static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1850{
1851 TCPCharDriver *s = chr->opaque;
1852 if (s->connected) {
1853 return send_all(s->fd, buf, len);
1854 } else {
1855 /* XXX: indicate an error ? */
1856 return len;
1857 }
1858}
1859
1860static int tcp_chr_read_poll(void *opaque)
1861{
1862 CharDriverState *chr = opaque;
1863 TCPCharDriver *s = chr->opaque;
1864 if (!s->connected)
1865 return 0;
1866 s->max_size = qemu_chr_can_read(chr);
1867 return s->max_size;
1868}
1869
1870#define IAC 255
1871#define IAC_BREAK 243
1872static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1873 TCPCharDriver *s,
1874 uint8_t *buf, int *size)
1875{
1876 /* Handle any telnet client's basic IAC options to satisfy char by
1877 * char mode with no echo. All IAC options will be removed from
1878 * the buf and the do_telnetopt variable will be used to track the
1879 * state of the width of the IAC information.
1880 *
1881 * IAC commands come in sets of 3 bytes with the exception of the
1882 * "IAC BREAK" command and the double IAC.
1883 */
1884
1885 int i;
1886 int j = 0;
1887
1888 for (i = 0; i < *size; i++) {
1889 if (s->do_telnetopt > 1) {
1890 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1891 /* Double IAC means send an IAC */
1892 if (j != i)
1893 buf[j] = buf[i];
1894 j++;
1895 s->do_telnetopt = 1;
1896 } else {
1897 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1898 /* Handle IAC break commands by sending a serial break */
1899 qemu_chr_event(chr, CHR_EVENT_BREAK);
1900 s->do_telnetopt++;
1901 }
1902 s->do_telnetopt++;
1903 }
1904 if (s->do_telnetopt >= 4) {
1905 s->do_telnetopt = 1;
1906 }
1907 } else {
1908 if ((unsigned char)buf[i] == IAC) {
1909 s->do_telnetopt = 2;
1910 } else {
1911 if (j != i)
1912 buf[j] = buf[i];
1913 j++;
1914 }
1915 }
1916 }
1917 *size = j;
1918}
1919
Mark McLoughlin7d174052009-07-22 09:11:39 +01001920static int tcp_get_msgfd(CharDriverState *chr)
1921{
1922 TCPCharDriver *s = chr->opaque;
1923
1924 return s->msgfd;
1925}
1926
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05001927#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01001928static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1929{
1930 TCPCharDriver *s = chr->opaque;
1931 struct cmsghdr *cmsg;
1932
1933 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1934 int fd;
1935
1936 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1937 cmsg->cmsg_level != SOL_SOCKET ||
1938 cmsg->cmsg_type != SCM_RIGHTS)
1939 continue;
1940
1941 fd = *((int *)CMSG_DATA(cmsg));
1942 if (fd < 0)
1943 continue;
1944
1945 if (s->msgfd != -1)
1946 close(s->msgfd);
1947 s->msgfd = fd;
1948 }
1949}
1950
Mark McLoughlin9977c892009-07-22 09:11:38 +01001951static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1952{
1953 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00001954 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01001955 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01001956 union {
1957 struct cmsghdr cmsg;
1958 char control[CMSG_SPACE(sizeof(int))];
1959 } msg_control;
1960 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01001961
1962 iov[0].iov_base = buf;
1963 iov[0].iov_len = len;
1964
1965 msg.msg_iov = iov;
1966 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001967 msg.msg_control = &msg_control;
1968 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01001969
Mark McLoughlin7d174052009-07-22 09:11:39 +01001970 ret = recvmsg(s->fd, &msg, 0);
1971 if (ret > 0 && s->is_unix)
1972 unix_process_msgfd(chr, &msg);
1973
1974 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01001975}
1976#else
1977static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1978{
1979 TCPCharDriver *s = chr->opaque;
1980 return recv(s->fd, buf, len, 0);
1981}
1982#endif
1983
aliguori6f97dba2008-10-31 18:49:55 +00001984static void tcp_chr_read(void *opaque)
1985{
1986 CharDriverState *chr = opaque;
1987 TCPCharDriver *s = chr->opaque;
1988 uint8_t buf[1024];
1989 int len, size;
1990
1991 if (!s->connected || s->max_size <= 0)
1992 return;
1993 len = sizeof(buf);
1994 if (len > s->max_size)
1995 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01001996 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00001997 if (size == 0) {
1998 /* connection closed */
1999 s->connected = 0;
2000 if (s->listen_fd >= 0) {
2001 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2002 }
2003 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2004 closesocket(s->fd);
2005 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302006 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002007 } else if (size > 0) {
2008 if (s->do_telnetopt)
2009 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2010 if (size > 0)
2011 qemu_chr_read(chr, buf, size);
Mark McLoughlin7d174052009-07-22 09:11:39 +01002012 if (s->msgfd != -1) {
2013 close(s->msgfd);
2014 s->msgfd = -1;
2015 }
aliguori6f97dba2008-10-31 18:49:55 +00002016 }
2017}
2018
2019static void tcp_chr_connect(void *opaque)
2020{
2021 CharDriverState *chr = opaque;
2022 TCPCharDriver *s = chr->opaque;
2023
2024 s->connected = 1;
2025 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2026 tcp_chr_read, NULL, chr);
2027 qemu_chr_reset(chr);
2028}
2029
2030#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2031static void tcp_chr_telnet_init(int fd)
2032{
2033 char buf[3];
2034 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2035 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2036 send(fd, (char *)buf, 3, 0);
2037 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2038 send(fd, (char *)buf, 3, 0);
2039 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2040 send(fd, (char *)buf, 3, 0);
2041 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2042 send(fd, (char *)buf, 3, 0);
2043}
2044
2045static void socket_set_nodelay(int fd)
2046{
2047 int val = 1;
2048 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2049}
2050
2051static void tcp_chr_accept(void *opaque)
2052{
2053 CharDriverState *chr = opaque;
2054 TCPCharDriver *s = chr->opaque;
2055 struct sockaddr_in saddr;
2056#ifndef _WIN32
2057 struct sockaddr_un uaddr;
2058#endif
2059 struct sockaddr *addr;
2060 socklen_t len;
2061 int fd;
2062
2063 for(;;) {
2064#ifndef _WIN32
2065 if (s->is_unix) {
2066 len = sizeof(uaddr);
2067 addr = (struct sockaddr *)&uaddr;
2068 } else
2069#endif
2070 {
2071 len = sizeof(saddr);
2072 addr = (struct sockaddr *)&saddr;
2073 }
2074 fd = accept(s->listen_fd, addr, &len);
2075 if (fd < 0 && errno != EINTR) {
2076 return;
2077 } else if (fd >= 0) {
2078 if (s->do_telnetopt)
2079 tcp_chr_telnet_init(fd);
2080 break;
2081 }
2082 }
2083 socket_set_nonblock(fd);
2084 if (s->do_nodelay)
2085 socket_set_nodelay(fd);
2086 s->fd = fd;
2087 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2088 tcp_chr_connect(chr);
2089}
2090
2091static void tcp_chr_close(CharDriverState *chr)
2092{
2093 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002094 if (s->fd >= 0) {
2095 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002096 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002097 }
2098 if (s->listen_fd >= 0) {
2099 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002100 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002101 }
aliguori6f97dba2008-10-31 18:49:55 +00002102 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302103 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002104}
2105
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002106static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002107{
2108 CharDriverState *chr = NULL;
2109 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002110 int fd = -1;
2111 int is_listen;
2112 int is_waitconnect;
2113 int do_nodelay;
2114 int is_unix;
2115 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002116
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002117 is_listen = qemu_opt_get_bool(opts, "server", 0);
2118 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2119 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2120 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2121 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002122 if (!is_listen)
2123 is_waitconnect = 0;
2124
2125 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00002126 s = qemu_mallocz(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002127
aliguorif07b6002008-11-11 20:54:09 +00002128 if (is_unix) {
2129 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002130 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002131 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002132 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002133 }
2134 } else {
2135 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002136 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002137 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002138 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002139 }
2140 }
aliguori6f97dba2008-10-31 18:49:55 +00002141 if (fd < 0)
2142 goto fail;
2143
2144 if (!is_waitconnect)
2145 socket_set_nonblock(fd);
2146
2147 s->connected = 0;
2148 s->fd = -1;
2149 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002150 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002151 s->is_unix = is_unix;
2152 s->do_nodelay = do_nodelay && !is_unix;
2153
2154 chr->opaque = s;
2155 chr->chr_write = tcp_chr_write;
2156 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002157 chr->get_msgfd = tcp_get_msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002158
2159 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002160 s->listen_fd = fd;
2161 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2162 if (is_telnet)
2163 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002164
aliguori6f97dba2008-10-31 18:49:55 +00002165 } else {
aliguorif07b6002008-11-11 20:54:09 +00002166 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002167 s->fd = fd;
2168 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002169 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002170 }
2171
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002172 /* for "info chardev" monitor command */
2173 chr->filename = qemu_malloc(256);
2174 if (is_unix) {
2175 snprintf(chr->filename, 256, "unix:%s%s",
2176 qemu_opt_get(opts, "path"),
2177 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2178 } else if (is_telnet) {
2179 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2180 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2181 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2182 } else {
2183 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2184 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2185 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2186 }
2187
aliguori6f97dba2008-10-31 18:49:55 +00002188 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002189 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002190 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002191 tcp_chr_accept(chr);
2192 socket_set_nonblock(s->listen_fd);
2193 }
aliguori6f97dba2008-10-31 18:49:55 +00002194 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002195
aliguori6f97dba2008-10-31 18:49:55 +00002196 fail:
2197 if (fd >= 0)
2198 closesocket(fd);
2199 qemu_free(s);
2200 qemu_free(chr);
2201 return NULL;
2202}
2203
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002204static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2205{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002206 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002207 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002208 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002209 QemuOpts *opts;
2210
2211 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2212 if (NULL == opts)
2213 return NULL;
2214
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002215 if (strstart(filename, "mon:", &p)) {
2216 filename = p;
2217 qemu_opt_set(opts, "mux", "on");
2218 }
2219
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002220 if (strcmp(filename, "null") == 0 ||
2221 strcmp(filename, "pty") == 0 ||
2222 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002223 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002224 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002225 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002226 return opts;
2227 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002228 if (strstart(filename, "vc", &p)) {
2229 qemu_opt_set(opts, "backend", "vc");
2230 if (*p == ':') {
2231 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2232 /* pixels */
2233 qemu_opt_set(opts, "width", width);
2234 qemu_opt_set(opts, "height", height);
2235 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2236 /* chars */
2237 qemu_opt_set(opts, "cols", width);
2238 qemu_opt_set(opts, "rows", height);
2239 } else {
2240 goto fail;
2241 }
2242 }
2243 return opts;
2244 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002245 if (strcmp(filename, "con:") == 0) {
2246 qemu_opt_set(opts, "backend", "console");
2247 return opts;
2248 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002249 if (strstart(filename, "COM", NULL)) {
2250 qemu_opt_set(opts, "backend", "serial");
2251 qemu_opt_set(opts, "path", filename);
2252 return opts;
2253 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002254 if (strstart(filename, "file:", &p)) {
2255 qemu_opt_set(opts, "backend", "file");
2256 qemu_opt_set(opts, "path", p);
2257 return opts;
2258 }
2259 if (strstart(filename, "pipe:", &p)) {
2260 qemu_opt_set(opts, "backend", "pipe");
2261 qemu_opt_set(opts, "path", p);
2262 return opts;
2263 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002264 if (strstart(filename, "tcp:", &p) ||
2265 strstart(filename, "telnet:", &p)) {
2266 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2267 host[0] = 0;
2268 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2269 goto fail;
2270 }
2271 qemu_opt_set(opts, "backend", "socket");
2272 qemu_opt_set(opts, "host", host);
2273 qemu_opt_set(opts, "port", port);
2274 if (p[pos] == ',') {
2275 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2276 goto fail;
2277 }
2278 if (strstart(filename, "telnet:", &p))
2279 qemu_opt_set(opts, "telnet", "on");
2280 return opts;
2281 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002282 if (strstart(filename, "udp:", &p)) {
2283 qemu_opt_set(opts, "backend", "udp");
2284 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2285 host[0] = 0;
2286 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2287 fprintf(stderr, "udp #1\n");
2288 goto fail;
2289 }
2290 }
2291 qemu_opt_set(opts, "host", host);
2292 qemu_opt_set(opts, "port", port);
2293 if (p[pos] == '@') {
2294 p += pos + 1;
2295 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2296 host[0] = 0;
2297 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2298 fprintf(stderr, "udp #2\n");
2299 goto fail;
2300 }
2301 }
2302 qemu_opt_set(opts, "localaddr", host);
2303 qemu_opt_set(opts, "localport", port);
2304 }
2305 return opts;
2306 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002307 if (strstart(filename, "unix:", &p)) {
2308 qemu_opt_set(opts, "backend", "socket");
2309 if (qemu_opts_do_parse(opts, p, "path") != 0)
2310 goto fail;
2311 return opts;
2312 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002313 if (strstart(filename, "/dev/parport", NULL) ||
2314 strstart(filename, "/dev/ppi", NULL)) {
2315 qemu_opt_set(opts, "backend", "parport");
2316 qemu_opt_set(opts, "path", filename);
2317 return opts;
2318 }
2319 if (strstart(filename, "/dev/", NULL)) {
2320 qemu_opt_set(opts, "backend", "tty");
2321 qemu_opt_set(opts, "path", filename);
2322 return opts;
2323 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002324
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002325fail:
2326 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002327 qemu_opts_del(opts);
2328 return NULL;
2329}
2330
2331static const struct {
2332 const char *name;
2333 CharDriverState *(*open)(QemuOpts *opts);
2334} backend_table[] = {
2335 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002336 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002337 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002338 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002339 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002340#ifdef _WIN32
2341 { .name = "file", .open = qemu_chr_open_win_file_out },
2342 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002343 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002344 { .name = "serial", .open = qemu_chr_open_win },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002345#else
2346 { .name = "file", .open = qemu_chr_open_file_out },
2347 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002348 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002349 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002350#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002351#ifdef CONFIG_BRLAPI
2352 { .name = "braille", .open = chr_baum_init },
2353#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002354#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2355 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2356 { .name = "tty", .open = qemu_chr_open_tty },
2357#endif
2358#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2359 { .name = "parport", .open = qemu_chr_open_pp },
2360#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002361};
2362
2363CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2364 void (*init)(struct CharDriverState *s))
2365{
2366 CharDriverState *chr;
2367 int i;
2368
2369 if (qemu_opts_id(opts) == NULL) {
2370 fprintf(stderr, "chardev: no id specified\n");
2371 return NULL;
2372 }
2373
2374 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2375 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2376 break;
2377 }
2378 if (i == ARRAY_SIZE(backend_table)) {
2379 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2380 qemu_opt_get(opts, "backend"));
2381 return NULL;
2382 }
2383
2384 chr = backend_table[i].open(opts);
2385 if (!chr) {
2386 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2387 qemu_opt_get(opts, "backend"));
2388 return NULL;
2389 }
2390
2391 if (!chr->filename)
2392 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2393 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002394 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002395
2396 if (qemu_opt_get_bool(opts, "mux", 0)) {
2397 CharDriverState *base = chr;
2398 int len = strlen(qemu_opts_id(opts)) + 6;
2399 base->label = qemu_malloc(len);
2400 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2401 chr = qemu_chr_open_mux(base);
2402 chr->filename = base->filename;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002403 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002404 }
2405 chr->label = qemu_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002406 return chr;
2407}
2408
aurel32ceecf1d2009-01-18 14:08:04 +00002409CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002410{
2411 const char *p;
2412 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002413 QemuOpts *opts;
2414
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002415 if (strstart(filename, "chardev:", &p)) {
2416 return qemu_chr_find(p);
2417 }
2418
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002419 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002420 if (!opts)
2421 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002422
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002423 chr = qemu_chr_open_opts(opts, init);
2424 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2425 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002426 }
2427 return chr;
2428}
2429
2430void qemu_chr_close(CharDriverState *chr)
2431{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002432 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002433 if (chr->chr_close)
2434 chr->chr_close(chr);
2435 qemu_free(chr->filename);
2436 qemu_free(chr->label);
2437 qemu_free(chr);
2438}
2439
aliguori376253e2009-03-05 23:01:23 +00002440void qemu_chr_info(Monitor *mon)
aliguori6f97dba2008-10-31 18:49:55 +00002441{
2442 CharDriverState *chr;
2443
Blue Swirl72cf2d42009-09-12 07:36:22 +00002444 QTAILQ_FOREACH(chr, &chardevs, next) {
aliguori376253e2009-03-05 23:01:23 +00002445 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002446 }
2447}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002448
2449CharDriverState *qemu_chr_find(const char *name)
2450{
2451 CharDriverState *chr;
2452
Blue Swirl72cf2d42009-09-12 07:36:22 +00002453 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002454 if (strcmp(chr->label, name) != 0)
2455 continue;
2456 return chr;
2457 }
2458 return NULL;
2459}