blob: 0fd402c46d1940e24302d815edf1d586acb775ea [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>
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>
aliguori6f97dba2008-10-31 18:49:55 +000061#include <dirent.h>
62#include <netdb.h>
63#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020064#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000065#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000066#ifdef __FreeBSD__
aliguori6f97dba2008-10-31 18:49:55 +000067#include <libutil.h>
blueswir16972f932008-11-22 20:49:12 +000068#include <dev/ppbus/ppi.h>
69#include <dev/ppbus/ppbconf.h>
blueswir1c5e97232009-03-07 20:06:23 +000070#elif defined(__DragonFly__)
71#include <libutil.h>
72#include <dev/misc/ppi/ppi.h>
73#include <bus/ppbus/ppbconf.h>
blueswir124646c72008-11-07 16:55:48 +000074#else
75#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000076#endif
77#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
78#include <freebsd/stdlib.h>
79#else
80#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000081#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
Blue Swirl72cf2d42009-09-12 07:36:22 +0000109static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
110 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000111
aliguori6f97dba2008-10-31 18:49:55 +0000112static void qemu_chr_event(CharDriverState *s, int event)
113{
114 if (!s->chr_event)
115 return;
116 s->chr_event(s->handler_opaque, event);
117}
118
119static void qemu_chr_reset_bh(void *opaque)
120{
121 CharDriverState *s = opaque;
Amit Shah6cfa64d2009-10-07 18:31:17 +0530122
123 if (s->initial_reset_issued) {
124 qemu_chr_event(s, CHR_EVENT_OPENED);
125 } else {
126 s->initial_reset_issued = true;
127 }
aliguori6f97dba2008-10-31 18:49:55 +0000128 qemu_bh_delete(s->bh);
129 s->bh = NULL;
130}
131
132void qemu_chr_reset(CharDriverState *s)
133{
Amit Shah69795d62009-10-07 18:31:15 +0530134 if (s->bh == NULL) {
aliguori6f97dba2008-10-31 18:49:55 +0000135 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
136 qemu_bh_schedule(s->bh);
137 }
138}
139
aliguori2970a6c2009-03-05 22:59:58 +0000140void qemu_chr_initial_reset(void)
141{
142 CharDriverState *chr;
143
Blue Swirl72cf2d42009-09-12 07:36:22 +0000144 QTAILQ_FOREACH(chr, &chardevs, next) {
aliguori2970a6c2009-03-05 22:59:58 +0000145 qemu_chr_reset(chr);
146 }
147}
148
aliguori6f97dba2008-10-31 18:49:55 +0000149int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
150{
151 return s->chr_write(s, buf, len);
152}
153
154int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
155{
156 if (!s->chr_ioctl)
157 return -ENOTSUP;
158 return s->chr_ioctl(s, cmd, arg);
159}
160
161int qemu_chr_can_read(CharDriverState *s)
162{
163 if (!s->chr_can_read)
164 return 0;
165 return s->chr_can_read(s->handler_opaque);
166}
167
168void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
169{
170 s->chr_read(s->handler_opaque, buf, len);
171}
172
Mark McLoughlin7d174052009-07-22 09:11:39 +0100173int qemu_chr_get_msgfd(CharDriverState *s)
174{
175 return s->get_msgfd ? s->get_msgfd(s) : -1;
176}
177
aliguori6f97dba2008-10-31 18:49:55 +0000178void qemu_chr_accept_input(CharDriverState *s)
179{
180 if (s->chr_accept_input)
181 s->chr_accept_input(s);
182}
183
184void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
185{
186 char buf[4096];
187 va_list ap;
188 va_start(ap, fmt);
189 vsnprintf(buf, sizeof(buf), fmt, ap);
190 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
191 va_end(ap);
192}
193
194void qemu_chr_send_event(CharDriverState *s, int event)
195{
196 if (s->chr_send_event)
197 s->chr_send_event(s, event);
198}
199
200void qemu_chr_add_handlers(CharDriverState *s,
201 IOCanRWHandler *fd_can_read,
202 IOReadHandler *fd_read,
203 IOEventHandler *fd_event,
204 void *opaque)
205{
206 s->chr_can_read = fd_can_read;
207 s->chr_read = fd_read;
208 s->chr_event = fd_event;
209 s->handler_opaque = opaque;
210 if (s->chr_update_read_handler)
211 s->chr_update_read_handler(s);
212}
213
214static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
215{
216 return len;
217}
218
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200219static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000220{
221 CharDriverState *chr;
222
223 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000224 chr->chr_write = null_chr_write;
225 return chr;
226}
227
228/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000229#define MAX_MUX 4
230#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
231#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
232typedef struct {
233 IOCanRWHandler *chr_can_read[MAX_MUX];
234 IOReadHandler *chr_read[MAX_MUX];
235 IOEventHandler *chr_event[MAX_MUX];
236 void *ext_opaque[MAX_MUX];
237 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200238 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000239 int mux_cnt;
240 int term_got_escape;
241 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000242 /* Intermediate input buffer allows to catch escape sequences even if the
243 currently active device is not accepting any input - but only until it
244 is full as well. */
245 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
246 int prod[MAX_MUX];
247 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200248 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200249 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200250 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000251} MuxDriver;
252
253
254static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
255{
256 MuxDriver *d = chr->opaque;
257 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200258 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000259 ret = d->drv->chr_write(d->drv, buf, len);
260 } else {
261 int i;
262
263 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200264 for (i = 0; i < len; i++) {
265 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000266 char buf1[64];
267 int64_t ti;
268 int secs;
269
270 ti = qemu_get_clock(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200271 if (d->timestamps_start == -1)
272 d->timestamps_start = ti;
273 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000274 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000275 snprintf(buf1, sizeof(buf1),
276 "[%02d:%02d:%02d.%03d] ",
277 secs / 3600,
278 (secs / 60) % 60,
279 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000280 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000281 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200282 d->linestart = 0;
283 }
284 ret += d->drv->chr_write(d->drv, buf+i, 1);
285 if (buf[i] == '\n') {
286 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000287 }
288 }
289 }
290 return ret;
291}
292
293static const char * const mux_help[] = {
294 "% h print this help\n\r",
295 "% x exit emulator\n\r",
296 "% s save disk data back to file (if -snapshot)\n\r",
297 "% t toggle console timestamps\n\r"
298 "% b send break (magic sysrq)\n\r",
299 "% c switch between console and monitor\n\r",
300 "% % sends %\n\r",
301 NULL
302};
303
304int term_escape_char = 0x01; /* ctrl-a is used for escape */
305static void mux_print_help(CharDriverState *chr)
306{
307 int i, j;
308 char ebuf[15] = "Escape-Char";
309 char cbuf[50] = "\n\r";
310
311 if (term_escape_char > 0 && term_escape_char < 26) {
312 snprintf(cbuf, sizeof(cbuf), "\n\r");
313 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
314 } else {
315 snprintf(cbuf, sizeof(cbuf),
316 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
317 term_escape_char);
318 }
319 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
320 for (i = 0; mux_help[i] != NULL; i++) {
321 for (j=0; mux_help[i][j] != '\0'; j++) {
322 if (mux_help[i][j] == '%')
323 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
324 else
325 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
326 }
327 }
328}
329
aliguori2724b182009-03-05 23:01:47 +0000330static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
331{
332 if (d->chr_event[mux_nr])
333 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
334}
335
aliguori6f97dba2008-10-31 18:49:55 +0000336static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
337{
338 if (d->term_got_escape) {
339 d->term_got_escape = 0;
340 if (ch == term_escape_char)
341 goto send_char;
342 switch(ch) {
343 case '?':
344 case 'h':
345 mux_print_help(chr);
346 break;
347 case 'x':
348 {
349 const char *term = "QEMU: Terminated\n\r";
350 chr->chr_write(chr,(uint8_t *)term,strlen(term));
351 exit(0);
352 break;
353 }
354 case 's':
355 {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200356 DriveInfo *dinfo;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000357 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200358 bdrv_commit(dinfo->bdrv);
aliguori6f97dba2008-10-31 18:49:55 +0000359 }
360 }
361 break;
362 case 'b':
363 qemu_chr_event(chr, CHR_EVENT_BREAK);
364 break;
365 case 'c':
366 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
368 d->focus++;
369 if (d->focus >= d->mux_cnt)
370 d->focus = 0;
371 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000372 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200373 case 't':
374 d->timestamps = !d->timestamps;
375 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200376 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200377 break;
aliguori6f97dba2008-10-31 18:49:55 +0000378 }
379 } else if (ch == term_escape_char) {
380 d->term_got_escape = 1;
381 } else {
382 send_char:
383 return 1;
384 }
385 return 0;
386}
387
388static void mux_chr_accept_input(CharDriverState *chr)
389{
aliguori6f97dba2008-10-31 18:49:55 +0000390 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200391 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000392
aliguoria80bf992009-03-05 23:00:02 +0000393 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000394 d->chr_can_read[m] &&
395 d->chr_can_read[m](d->ext_opaque[m])) {
396 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000397 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000398 }
399}
400
401static int mux_chr_can_read(void *opaque)
402{
403 CharDriverState *chr = opaque;
404 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200405 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000406
aliguoria80bf992009-03-05 23:00:02 +0000407 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000408 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000409 if (d->chr_can_read[m])
410 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000411 return 0;
412}
413
414static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
415{
416 CharDriverState *chr = opaque;
417 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200418 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000419 int i;
420
421 mux_chr_accept_input (opaque);
422
423 for(i = 0; i < size; i++)
424 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000425 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000426 d->chr_can_read[m] &&
427 d->chr_can_read[m](d->ext_opaque[m]))
428 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
429 else
aliguoria80bf992009-03-05 23:00:02 +0000430 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000431 }
432}
433
434static void mux_chr_event(void *opaque, int event)
435{
436 CharDriverState *chr = opaque;
437 MuxDriver *d = chr->opaque;
438 int i;
439
440 /* Send the event to all registered listeners */
441 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000442 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000443}
444
445static void mux_chr_update_read_handler(CharDriverState *chr)
446{
447 MuxDriver *d = chr->opaque;
448
449 if (d->mux_cnt >= MAX_MUX) {
450 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
451 return;
452 }
453 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
454 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
455 d->chr_read[d->mux_cnt] = chr->chr_read;
456 d->chr_event[d->mux_cnt] = chr->chr_event;
457 /* Fix up the real driver with mux routines */
458 if (d->mux_cnt == 0) {
459 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
460 mux_chr_event, chr);
461 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200462 if (d->focus != -1) {
463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200464 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200465 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000466 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200467 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000468}
469
470static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
471{
472 CharDriverState *chr;
473 MuxDriver *d;
474
475 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000476 d = qemu_mallocz(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000477
478 chr->opaque = d;
479 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200480 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000481 chr->chr_write = mux_chr_write;
482 chr->chr_update_read_handler = mux_chr_update_read_handler;
483 chr->chr_accept_input = mux_chr_accept_input;
484 return chr;
485}
486
487
488#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000489int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000490{
491 int ret, len;
492
493 len = len1;
494 while (len > 0) {
495 ret = send(fd, buf, len, 0);
496 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000497 errno = WSAGetLastError();
498 if (errno != WSAEWOULDBLOCK) {
499 return -1;
500 }
501 } else if (ret == 0) {
502 break;
503 } else {
504 buf += ret;
505 len -= ret;
506 }
507 }
508 return len1 - len;
509}
510
511#else
512
513static int unix_write(int fd, const uint8_t *buf, int len1)
514{
515 int ret, len;
516
517 len = len1;
518 while (len > 0) {
519 ret = write(fd, buf, len);
520 if (ret < 0) {
521 if (errno != EINTR && errno != EAGAIN)
522 return -1;
523 } else if (ret == 0) {
524 break;
525 } else {
526 buf += ret;
527 len -= ret;
528 }
529 }
530 return len1 - len;
531}
532
aliguorid247d252008-11-11 20:46:40 +0000533int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000534{
535 return unix_write(fd, buf, len1);
536}
537#endif /* !_WIN32 */
538
539#ifndef _WIN32
540
541typedef struct {
542 int fd_in, fd_out;
543 int max_size;
544} FDCharDriver;
545
546#define STDIO_MAX_CLIENTS 1
547static int stdio_nb_clients = 0;
548
549static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
550{
551 FDCharDriver *s = chr->opaque;
552 return send_all(s->fd_out, buf, len);
553}
554
555static int fd_chr_read_poll(void *opaque)
556{
557 CharDriverState *chr = opaque;
558 FDCharDriver *s = chr->opaque;
559
560 s->max_size = qemu_chr_can_read(chr);
561 return s->max_size;
562}
563
564static void fd_chr_read(void *opaque)
565{
566 CharDriverState *chr = opaque;
567 FDCharDriver *s = chr->opaque;
568 int size, len;
569 uint8_t buf[1024];
570
571 len = sizeof(buf);
572 if (len > s->max_size)
573 len = s->max_size;
574 if (len == 0)
575 return;
576 size = read(s->fd_in, buf, len);
577 if (size == 0) {
578 /* FD has been closed. Remove it from the active list. */
579 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530580 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000581 return;
582 }
583 if (size > 0) {
584 qemu_chr_read(chr, buf, size);
585 }
586}
587
588static void fd_chr_update_read_handler(CharDriverState *chr)
589{
590 FDCharDriver *s = chr->opaque;
591
592 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500593 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000594 } else {
595 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
596 fd_chr_read, NULL, chr);
597 }
598 }
599}
600
601static void fd_chr_close(struct CharDriverState *chr)
602{
603 FDCharDriver *s = chr->opaque;
604
605 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500606 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000607 } else {
608 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
609 }
610 }
611
612 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530613 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000614}
615
616/* open a character device to a unix fd */
617static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
618{
619 CharDriverState *chr;
620 FDCharDriver *s;
621
622 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000623 s = qemu_mallocz(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000624 s->fd_in = fd_in;
625 s->fd_out = fd_out;
626 chr->opaque = s;
627 chr->chr_write = fd_chr_write;
628 chr->chr_update_read_handler = fd_chr_update_read_handler;
629 chr->chr_close = fd_chr_close;
630
631 qemu_chr_reset(chr);
632
633 return chr;
634}
635
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200636static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000637{
638 int fd_out;
639
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200640 TFR(fd_out = open(qemu_opt_get(opts, "path"),
641 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
aliguori6f97dba2008-10-31 18:49:55 +0000642 if (fd_out < 0)
643 return NULL;
644 return qemu_chr_open_fd(-1, fd_out);
645}
646
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200647static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000648{
649 int fd_in, fd_out;
650 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200651 const char *filename = qemu_opt_get(opts, "path");
652
653 if (filename == NULL) {
654 fprintf(stderr, "chardev: pipe: no filename given\n");
655 return NULL;
656 }
aliguori6f97dba2008-10-31 18:49:55 +0000657
658 snprintf(filename_in, 256, "%s.in", filename);
659 snprintf(filename_out, 256, "%s.out", filename);
660 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
661 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
662 if (fd_in < 0 || fd_out < 0) {
663 if (fd_in >= 0)
664 close(fd_in);
665 if (fd_out >= 0)
666 close(fd_out);
667 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
668 if (fd_in < 0)
669 return NULL;
670 }
671 return qemu_chr_open_fd(fd_in, fd_out);
672}
673
674
675/* for STDIO, we handle the case where several clients use it
676 (nographic mode) */
677
678#define TERM_FIFO_MAX_SIZE 1
679
680static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
681static int term_fifo_size;
682
683static int stdio_read_poll(void *opaque)
684{
685 CharDriverState *chr = opaque;
686
687 /* try to flush the queue if needed */
688 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
689 qemu_chr_read(chr, term_fifo, 1);
690 term_fifo_size = 0;
691 }
692 /* see if we can absorb more chars */
693 if (term_fifo_size == 0)
694 return 1;
695 else
696 return 0;
697}
698
699static void stdio_read(void *opaque)
700{
701 int size;
702 uint8_t buf[1];
703 CharDriverState *chr = opaque;
704
705 size = read(0, buf, 1);
706 if (size == 0) {
707 /* stdin has been closed. Remove it from the active list. */
708 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530709 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000710 return;
711 }
712 if (size > 0) {
713 if (qemu_chr_can_read(chr) > 0) {
714 qemu_chr_read(chr, buf, 1);
715 } else if (term_fifo_size == 0) {
716 term_fifo[term_fifo_size++] = buf[0];
717 }
718 }
719}
720
721/* init terminal so that we can grab keys */
722static struct termios oldtty;
723static int old_fd0_flags;
724static int term_atexit_done;
725
726static void term_exit(void)
727{
728 tcsetattr (0, TCSANOW, &oldtty);
729 fcntl(0, F_SETFL, old_fd0_flags);
730}
731
732static void term_init(void)
733{
734 struct termios tty;
735
736 tcgetattr (0, &tty);
737 oldtty = tty;
738 old_fd0_flags = fcntl(0, F_GETFL);
739
740 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
741 |INLCR|IGNCR|ICRNL|IXON);
742 tty.c_oflag |= OPOST;
743 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
744 /* if graphical mode, we allow Ctrl-C handling */
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500745 if (display_type == DT_NOGRAPHIC)
aliguori6f97dba2008-10-31 18:49:55 +0000746 tty.c_lflag &= ~ISIG;
747 tty.c_cflag &= ~(CSIZE|PARENB);
748 tty.c_cflag |= CS8;
749 tty.c_cc[VMIN] = 1;
750 tty.c_cc[VTIME] = 0;
751
752 tcsetattr (0, TCSANOW, &tty);
753
754 if (!term_atexit_done++)
755 atexit(term_exit);
756
757 fcntl(0, F_SETFL, O_NONBLOCK);
758}
759
760static void qemu_chr_close_stdio(struct CharDriverState *chr)
761{
762 term_exit();
763 stdio_nb_clients--;
764 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
765 fd_chr_close(chr);
766}
767
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +0200768static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000769{
770 CharDriverState *chr;
771
772 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
773 return NULL;
774 chr = qemu_chr_open_fd(0, 1);
775 chr->chr_close = qemu_chr_close_stdio;
776 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
777 stdio_nb_clients++;
778 term_init();
779
780 return chr;
781}
782
783#ifdef __sun__
784/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000785static int openpty(int *amaster, int *aslave, char *name,
786 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000787{
788 const char *slave;
789 int mfd = -1, sfd = -1;
790
791 *amaster = *aslave = -1;
792
793 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
794 if (mfd < 0)
795 goto err;
796
797 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
798 goto err;
799
800 if ((slave = ptsname(mfd)) == NULL)
801 goto err;
802
803 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
804 goto err;
805
806 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
807 (termp != NULL && tcgetattr(sfd, termp) < 0))
808 goto err;
809
810 if (amaster)
811 *amaster = mfd;
812 if (aslave)
813 *aslave = sfd;
814 if (winp)
815 ioctl(sfd, TIOCSWINSZ, winp);
816
817 return 0;
818
819err:
820 if (sfd != -1)
821 close(sfd);
822 close(mfd);
823 return -1;
824}
825
blueswir13f4cb3d2009-04-13 16:31:01 +0000826static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000827{
828 termios_p->c_iflag &=
829 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
830 termios_p->c_oflag &= ~OPOST;
831 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
832 termios_p->c_cflag &= ~(CSIZE|PARENB);
833 termios_p->c_cflag |= CS8;
834
835 termios_p->c_cc[VMIN] = 0;
836 termios_p->c_cc[VTIME] = 0;
837}
838#endif
839
840#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
blueswir1c5e97232009-03-07 20:06:23 +0000841 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000842
843typedef struct {
844 int fd;
845 int connected;
846 int polling;
847 int read_bytes;
848 QEMUTimer *timer;
849} PtyCharDriver;
850
851static void pty_chr_update_read_handler(CharDriverState *chr);
852static void pty_chr_state(CharDriverState *chr, int connected);
853
854static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
855{
856 PtyCharDriver *s = chr->opaque;
857
858 if (!s->connected) {
859 /* guest sends data, check for (re-)connect */
860 pty_chr_update_read_handler(chr);
861 return 0;
862 }
863 return send_all(s->fd, buf, len);
864}
865
866static int pty_chr_read_poll(void *opaque)
867{
868 CharDriverState *chr = opaque;
869 PtyCharDriver *s = chr->opaque;
870
871 s->read_bytes = qemu_chr_can_read(chr);
872 return s->read_bytes;
873}
874
875static void pty_chr_read(void *opaque)
876{
877 CharDriverState *chr = opaque;
878 PtyCharDriver *s = chr->opaque;
879 int size, len;
880 uint8_t buf[1024];
881
882 len = sizeof(buf);
883 if (len > s->read_bytes)
884 len = s->read_bytes;
885 if (len == 0)
886 return;
887 size = read(s->fd, buf, len);
888 if ((size == -1 && errno == EIO) ||
889 (size == 0)) {
890 pty_chr_state(chr, 0);
891 return;
892 }
893 if (size > 0) {
894 pty_chr_state(chr, 1);
895 qemu_chr_read(chr, buf, size);
896 }
897}
898
899static void pty_chr_update_read_handler(CharDriverState *chr)
900{
901 PtyCharDriver *s = chr->opaque;
902
903 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
904 pty_chr_read, NULL, chr);
905 s->polling = 1;
906 /*
907 * Short timeout here: just need wait long enougth that qemu makes
908 * it through the poll loop once. When reconnected we want a
909 * short timeout so we notice it almost instantly. Otherwise
910 * read() gives us -EIO instantly, making pty_chr_state() reset the
911 * timeout to the normal (much longer) poll interval before the
912 * timer triggers.
913 */
914 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
915}
916
917static void pty_chr_state(CharDriverState *chr, int connected)
918{
919 PtyCharDriver *s = chr->opaque;
920
921 if (!connected) {
922 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
923 s->connected = 0;
924 s->polling = 0;
925 /* (re-)connect poll interval for idle guests: once per second.
926 * We check more frequently in case the guests sends data to
927 * the virtual device linked to our pty. */
928 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
929 } else {
930 if (!s->connected)
931 qemu_chr_reset(chr);
932 s->connected = 1;
933 }
934}
935
936static void pty_chr_timer(void *opaque)
937{
938 struct CharDriverState *chr = opaque;
939 PtyCharDriver *s = chr->opaque;
940
941 if (s->connected)
942 return;
943 if (s->polling) {
944 /* If we arrive here without polling being cleared due
945 * read returning -EIO, then we are (re-)connected */
946 pty_chr_state(chr, 1);
947 return;
948 }
949
950 /* Next poll ... */
951 pty_chr_update_read_handler(chr);
952}
953
954static void pty_chr_close(struct CharDriverState *chr)
955{
956 PtyCharDriver *s = chr->opaque;
957
958 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
959 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000960 qemu_del_timer(s->timer);
961 qemu_free_timer(s->timer);
aliguori6f97dba2008-10-31 18:49:55 +0000962 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530963 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000964}
965
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200966static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000967{
968 CharDriverState *chr;
969 PtyCharDriver *s;
970 struct termios tty;
971 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000972#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000973 char pty_name[PATH_MAX];
974#define q_ptsname(x) pty_name
975#else
976 char *pty_name = NULL;
977#define q_ptsname(x) ptsname(x)
978#endif
979
980 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000981 s = qemu_mallocz(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000982
983 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
984 return NULL;
985 }
986
987 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +0000988 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000989 cfmakeraw(&tty);
990 tcsetattr(slave_fd, TCSAFLUSH, &tty);
991 close(slave_fd);
992
993 len = strlen(q_ptsname(s->fd)) + 5;
994 chr->filename = qemu_malloc(len);
995 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200996 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +0000997 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
998
999 chr->opaque = s;
1000 chr->chr_write = pty_chr_write;
1001 chr->chr_update_read_handler = pty_chr_update_read_handler;
1002 chr->chr_close = pty_chr_close;
1003
1004 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
1005
1006 return chr;
1007}
1008
1009static void tty_serial_init(int fd, int speed,
1010 int parity, int data_bits, int stop_bits)
1011{
1012 struct termios tty;
1013 speed_t spd;
1014
1015#if 0
1016 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1017 speed, parity, data_bits, stop_bits);
1018#endif
1019 tcgetattr (fd, &tty);
1020
1021#define MARGIN 1.1
1022 if (speed <= 50 * MARGIN)
1023 spd = B50;
1024 else if (speed <= 75 * MARGIN)
1025 spd = B75;
1026 else if (speed <= 300 * MARGIN)
1027 spd = B300;
1028 else if (speed <= 600 * MARGIN)
1029 spd = B600;
1030 else if (speed <= 1200 * MARGIN)
1031 spd = B1200;
1032 else if (speed <= 2400 * MARGIN)
1033 spd = B2400;
1034 else if (speed <= 4800 * MARGIN)
1035 spd = B4800;
1036 else if (speed <= 9600 * MARGIN)
1037 spd = B9600;
1038 else if (speed <= 19200 * MARGIN)
1039 spd = B19200;
1040 else if (speed <= 38400 * MARGIN)
1041 spd = B38400;
1042 else if (speed <= 57600 * MARGIN)
1043 spd = B57600;
1044 else if (speed <= 115200 * MARGIN)
1045 spd = B115200;
1046 else
1047 spd = B115200;
1048
1049 cfsetispeed(&tty, spd);
1050 cfsetospeed(&tty, spd);
1051
1052 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1053 |INLCR|IGNCR|ICRNL|IXON);
1054 tty.c_oflag |= OPOST;
1055 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1056 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1057 switch(data_bits) {
1058 default:
1059 case 8:
1060 tty.c_cflag |= CS8;
1061 break;
1062 case 7:
1063 tty.c_cflag |= CS7;
1064 break;
1065 case 6:
1066 tty.c_cflag |= CS6;
1067 break;
1068 case 5:
1069 tty.c_cflag |= CS5;
1070 break;
1071 }
1072 switch(parity) {
1073 default:
1074 case 'N':
1075 break;
1076 case 'E':
1077 tty.c_cflag |= PARENB;
1078 break;
1079 case 'O':
1080 tty.c_cflag |= PARENB | PARODD;
1081 break;
1082 }
1083 if (stop_bits == 2)
1084 tty.c_cflag |= CSTOPB;
1085
1086 tcsetattr (fd, TCSANOW, &tty);
1087}
1088
1089static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1090{
1091 FDCharDriver *s = chr->opaque;
1092
1093 switch(cmd) {
1094 case CHR_IOCTL_SERIAL_SET_PARAMS:
1095 {
1096 QEMUSerialSetParams *ssp = arg;
1097 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1098 ssp->data_bits, ssp->stop_bits);
1099 }
1100 break;
1101 case CHR_IOCTL_SERIAL_SET_BREAK:
1102 {
1103 int enable = *(int *)arg;
1104 if (enable)
1105 tcsendbreak(s->fd_in, 1);
1106 }
1107 break;
1108 case CHR_IOCTL_SERIAL_GET_TIOCM:
1109 {
1110 int sarg = 0;
1111 int *targ = (int *)arg;
1112 ioctl(s->fd_in, TIOCMGET, &sarg);
1113 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001114 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001115 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001116 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001117 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001118 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001119 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001120 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001121 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001122 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001123 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001124 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001125 *targ |= CHR_TIOCM_RTS;
1126 }
1127 break;
1128 case CHR_IOCTL_SERIAL_SET_TIOCM:
1129 {
1130 int sarg = *(int *)arg;
1131 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001132 ioctl(s->fd_in, TIOCMGET, &targ);
1133 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1134 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1135 if (sarg & CHR_TIOCM_CTS)
1136 targ |= TIOCM_CTS;
1137 if (sarg & CHR_TIOCM_CAR)
1138 targ |= TIOCM_CAR;
1139 if (sarg & CHR_TIOCM_DSR)
1140 targ |= TIOCM_DSR;
1141 if (sarg & CHR_TIOCM_RI)
1142 targ |= TIOCM_RI;
1143 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001144 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001145 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001146 targ |= TIOCM_RTS;
1147 ioctl(s->fd_in, TIOCMSET, &targ);
1148 }
1149 break;
1150 default:
1151 return -ENOTSUP;
1152 }
1153 return 0;
1154}
1155
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001156static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001157{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001158 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001159 CharDriverState *chr;
1160 int fd;
1161
1162 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1163 tty_serial_init(fd, 115200, 'N', 8, 1);
1164 chr = qemu_chr_open_fd(fd, fd);
1165 if (!chr) {
1166 close(fd);
1167 return NULL;
1168 }
1169 chr->chr_ioctl = tty_serial_ioctl;
1170 qemu_chr_reset(chr);
1171 return chr;
1172}
1173#else /* ! __linux__ && ! __sun__ */
1174static CharDriverState *qemu_chr_open_pty(void)
1175{
1176 return NULL;
1177}
1178#endif /* __linux__ || __sun__ */
1179
1180#if defined(__linux__)
1181typedef struct {
1182 int fd;
1183 int mode;
1184} ParallelCharDriver;
1185
1186static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1187{
1188 if (s->mode != mode) {
1189 int m = mode;
1190 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1191 return 0;
1192 s->mode = mode;
1193 }
1194 return 1;
1195}
1196
1197static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1198{
1199 ParallelCharDriver *drv = chr->opaque;
1200 int fd = drv->fd;
1201 uint8_t b;
1202
1203 switch(cmd) {
1204 case CHR_IOCTL_PP_READ_DATA:
1205 if (ioctl(fd, PPRDATA, &b) < 0)
1206 return -ENOTSUP;
1207 *(uint8_t *)arg = b;
1208 break;
1209 case CHR_IOCTL_PP_WRITE_DATA:
1210 b = *(uint8_t *)arg;
1211 if (ioctl(fd, PPWDATA, &b) < 0)
1212 return -ENOTSUP;
1213 break;
1214 case CHR_IOCTL_PP_READ_CONTROL:
1215 if (ioctl(fd, PPRCONTROL, &b) < 0)
1216 return -ENOTSUP;
1217 /* Linux gives only the lowest bits, and no way to know data
1218 direction! For better compatibility set the fixed upper
1219 bits. */
1220 *(uint8_t *)arg = b | 0xc0;
1221 break;
1222 case CHR_IOCTL_PP_WRITE_CONTROL:
1223 b = *(uint8_t *)arg;
1224 if (ioctl(fd, PPWCONTROL, &b) < 0)
1225 return -ENOTSUP;
1226 break;
1227 case CHR_IOCTL_PP_READ_STATUS:
1228 if (ioctl(fd, PPRSTATUS, &b) < 0)
1229 return -ENOTSUP;
1230 *(uint8_t *)arg = b;
1231 break;
1232 case CHR_IOCTL_PP_DATA_DIR:
1233 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1234 return -ENOTSUP;
1235 break;
1236 case CHR_IOCTL_PP_EPP_READ_ADDR:
1237 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1238 struct ParallelIOArg *parg = arg;
1239 int n = read(fd, parg->buffer, parg->count);
1240 if (n != parg->count) {
1241 return -EIO;
1242 }
1243 }
1244 break;
1245 case CHR_IOCTL_PP_EPP_READ:
1246 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1247 struct ParallelIOArg *parg = arg;
1248 int n = read(fd, parg->buffer, parg->count);
1249 if (n != parg->count) {
1250 return -EIO;
1251 }
1252 }
1253 break;
1254 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1255 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1256 struct ParallelIOArg *parg = arg;
1257 int n = write(fd, parg->buffer, parg->count);
1258 if (n != parg->count) {
1259 return -EIO;
1260 }
1261 }
1262 break;
1263 case CHR_IOCTL_PP_EPP_WRITE:
1264 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1265 struct ParallelIOArg *parg = arg;
1266 int n = write(fd, parg->buffer, parg->count);
1267 if (n != parg->count) {
1268 return -EIO;
1269 }
1270 }
1271 break;
1272 default:
1273 return -ENOTSUP;
1274 }
1275 return 0;
1276}
1277
1278static void pp_close(CharDriverState *chr)
1279{
1280 ParallelCharDriver *drv = chr->opaque;
1281 int fd = drv->fd;
1282
1283 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1284 ioctl(fd, PPRELEASE);
1285 close(fd);
1286 qemu_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301287 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001288}
1289
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001290static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001291{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001292 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001293 CharDriverState *chr;
1294 ParallelCharDriver *drv;
1295 int fd;
1296
1297 TFR(fd = open(filename, O_RDWR));
1298 if (fd < 0)
1299 return NULL;
1300
1301 if (ioctl(fd, PPCLAIM) < 0) {
1302 close(fd);
1303 return NULL;
1304 }
1305
1306 drv = qemu_mallocz(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001307 drv->fd = fd;
1308 drv->mode = IEEE1284_MODE_COMPAT;
1309
1310 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001311 chr->chr_write = null_chr_write;
1312 chr->chr_ioctl = pp_ioctl;
1313 chr->chr_close = pp_close;
1314 chr->opaque = drv;
1315
1316 qemu_chr_reset(chr);
1317
1318 return chr;
1319}
1320#endif /* __linux__ */
1321
blueswir1c5e97232009-03-07 20:06:23 +00001322#if defined(__FreeBSD__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001323static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1324{
1325 int fd = (int)chr->opaque;
1326 uint8_t b;
1327
1328 switch(cmd) {
1329 case CHR_IOCTL_PP_READ_DATA:
1330 if (ioctl(fd, PPIGDATA, &b) < 0)
1331 return -ENOTSUP;
1332 *(uint8_t *)arg = b;
1333 break;
1334 case CHR_IOCTL_PP_WRITE_DATA:
1335 b = *(uint8_t *)arg;
1336 if (ioctl(fd, PPISDATA, &b) < 0)
1337 return -ENOTSUP;
1338 break;
1339 case CHR_IOCTL_PP_READ_CONTROL:
1340 if (ioctl(fd, PPIGCTRL, &b) < 0)
1341 return -ENOTSUP;
1342 *(uint8_t *)arg = b;
1343 break;
1344 case CHR_IOCTL_PP_WRITE_CONTROL:
1345 b = *(uint8_t *)arg;
1346 if (ioctl(fd, PPISCTRL, &b) < 0)
1347 return -ENOTSUP;
1348 break;
1349 case CHR_IOCTL_PP_READ_STATUS:
1350 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1351 return -ENOTSUP;
1352 *(uint8_t *)arg = b;
1353 break;
1354 default:
1355 return -ENOTSUP;
1356 }
1357 return 0;
1358}
1359
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001360static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001361{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001362 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001363 CharDriverState *chr;
1364 int fd;
1365
1366 fd = open(filename, O_RDWR);
1367 if (fd < 0)
1368 return NULL;
1369
1370 chr = qemu_mallocz(sizeof(CharDriverState));
blueswir16972f932008-11-22 20:49:12 +00001371 chr->opaque = (void *)fd;
1372 chr->chr_write = null_chr_write;
1373 chr->chr_ioctl = pp_ioctl;
1374 return chr;
1375}
1376#endif
1377
aliguori6f97dba2008-10-31 18:49:55 +00001378#else /* _WIN32 */
1379
1380typedef struct {
1381 int max_size;
1382 HANDLE hcom, hrecv, hsend;
1383 OVERLAPPED orecv, osend;
1384 BOOL fpipe;
1385 DWORD len;
1386} WinCharState;
1387
1388#define NSENDBUF 2048
1389#define NRECVBUF 2048
1390#define MAXCONNECT 1
1391#define NTIMEOUT 5000
1392
1393static int win_chr_poll(void *opaque);
1394static int win_chr_pipe_poll(void *opaque);
1395
1396static void win_chr_close(CharDriverState *chr)
1397{
1398 WinCharState *s = chr->opaque;
1399
1400 if (s->hsend) {
1401 CloseHandle(s->hsend);
1402 s->hsend = NULL;
1403 }
1404 if (s->hrecv) {
1405 CloseHandle(s->hrecv);
1406 s->hrecv = NULL;
1407 }
1408 if (s->hcom) {
1409 CloseHandle(s->hcom);
1410 s->hcom = NULL;
1411 }
1412 if (s->fpipe)
1413 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1414 else
1415 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301416
1417 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001418}
1419
1420static int win_chr_init(CharDriverState *chr, const char *filename)
1421{
1422 WinCharState *s = chr->opaque;
1423 COMMCONFIG comcfg;
1424 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1425 COMSTAT comstat;
1426 DWORD size;
1427 DWORD err;
1428
1429 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1430 if (!s->hsend) {
1431 fprintf(stderr, "Failed CreateEvent\n");
1432 goto fail;
1433 }
1434 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1435 if (!s->hrecv) {
1436 fprintf(stderr, "Failed CreateEvent\n");
1437 goto fail;
1438 }
1439
1440 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1441 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1442 if (s->hcom == INVALID_HANDLE_VALUE) {
1443 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1444 s->hcom = NULL;
1445 goto fail;
1446 }
1447
1448 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1449 fprintf(stderr, "Failed SetupComm\n");
1450 goto fail;
1451 }
1452
1453 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1454 size = sizeof(COMMCONFIG);
1455 GetDefaultCommConfig(filename, &comcfg, &size);
1456 comcfg.dcb.DCBlength = sizeof(DCB);
1457 CommConfigDialog(filename, NULL, &comcfg);
1458
1459 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1460 fprintf(stderr, "Failed SetCommState\n");
1461 goto fail;
1462 }
1463
1464 if (!SetCommMask(s->hcom, EV_ERR)) {
1465 fprintf(stderr, "Failed SetCommMask\n");
1466 goto fail;
1467 }
1468
1469 cto.ReadIntervalTimeout = MAXDWORD;
1470 if (!SetCommTimeouts(s->hcom, &cto)) {
1471 fprintf(stderr, "Failed SetCommTimeouts\n");
1472 goto fail;
1473 }
1474
1475 if (!ClearCommError(s->hcom, &err, &comstat)) {
1476 fprintf(stderr, "Failed ClearCommError\n");
1477 goto fail;
1478 }
1479 qemu_add_polling_cb(win_chr_poll, chr);
1480 return 0;
1481
1482 fail:
1483 win_chr_close(chr);
1484 return -1;
1485}
1486
1487static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1488{
1489 WinCharState *s = chr->opaque;
1490 DWORD len, ret, size, err;
1491
1492 len = len1;
1493 ZeroMemory(&s->osend, sizeof(s->osend));
1494 s->osend.hEvent = s->hsend;
1495 while (len > 0) {
1496 if (s->hsend)
1497 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1498 else
1499 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1500 if (!ret) {
1501 err = GetLastError();
1502 if (err == ERROR_IO_PENDING) {
1503 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1504 if (ret) {
1505 buf += size;
1506 len -= size;
1507 } else {
1508 break;
1509 }
1510 } else {
1511 break;
1512 }
1513 } else {
1514 buf += size;
1515 len -= size;
1516 }
1517 }
1518 return len1 - len;
1519}
1520
1521static int win_chr_read_poll(CharDriverState *chr)
1522{
1523 WinCharState *s = chr->opaque;
1524
1525 s->max_size = qemu_chr_can_read(chr);
1526 return s->max_size;
1527}
1528
1529static void win_chr_readfile(CharDriverState *chr)
1530{
1531 WinCharState *s = chr->opaque;
1532 int ret, err;
1533 uint8_t buf[1024];
1534 DWORD size;
1535
1536 ZeroMemory(&s->orecv, sizeof(s->orecv));
1537 s->orecv.hEvent = s->hrecv;
1538 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1539 if (!ret) {
1540 err = GetLastError();
1541 if (err == ERROR_IO_PENDING) {
1542 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1543 }
1544 }
1545
1546 if (size > 0) {
1547 qemu_chr_read(chr, buf, size);
1548 }
1549}
1550
1551static void win_chr_read(CharDriverState *chr)
1552{
1553 WinCharState *s = chr->opaque;
1554
1555 if (s->len > s->max_size)
1556 s->len = s->max_size;
1557 if (s->len == 0)
1558 return;
1559
1560 win_chr_readfile(chr);
1561}
1562
1563static int win_chr_poll(void *opaque)
1564{
1565 CharDriverState *chr = opaque;
1566 WinCharState *s = chr->opaque;
1567 COMSTAT status;
1568 DWORD comerr;
1569
1570 ClearCommError(s->hcom, &comerr, &status);
1571 if (status.cbInQue > 0) {
1572 s->len = status.cbInQue;
1573 win_chr_read_poll(chr);
1574 win_chr_read(chr);
1575 return 1;
1576 }
1577 return 0;
1578}
1579
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001580static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001581{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001582 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001583 CharDriverState *chr;
1584 WinCharState *s;
1585
1586 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001587 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001588 chr->opaque = s;
1589 chr->chr_write = win_chr_write;
1590 chr->chr_close = win_chr_close;
1591
1592 if (win_chr_init(chr, filename) < 0) {
1593 free(s);
1594 free(chr);
1595 return NULL;
1596 }
1597 qemu_chr_reset(chr);
1598 return chr;
1599}
1600
1601static int win_chr_pipe_poll(void *opaque)
1602{
1603 CharDriverState *chr = opaque;
1604 WinCharState *s = chr->opaque;
1605 DWORD size;
1606
1607 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1608 if (size > 0) {
1609 s->len = size;
1610 win_chr_read_poll(chr);
1611 win_chr_read(chr);
1612 return 1;
1613 }
1614 return 0;
1615}
1616
1617static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1618{
1619 WinCharState *s = chr->opaque;
1620 OVERLAPPED ov;
1621 int ret;
1622 DWORD size;
1623 char openname[256];
1624
1625 s->fpipe = TRUE;
1626
1627 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1628 if (!s->hsend) {
1629 fprintf(stderr, "Failed CreateEvent\n");
1630 goto fail;
1631 }
1632 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1633 if (!s->hrecv) {
1634 fprintf(stderr, "Failed CreateEvent\n");
1635 goto fail;
1636 }
1637
1638 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1639 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1640 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1641 PIPE_WAIT,
1642 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1643 if (s->hcom == INVALID_HANDLE_VALUE) {
1644 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1645 s->hcom = NULL;
1646 goto fail;
1647 }
1648
1649 ZeroMemory(&ov, sizeof(ov));
1650 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1651 ret = ConnectNamedPipe(s->hcom, &ov);
1652 if (ret) {
1653 fprintf(stderr, "Failed ConnectNamedPipe\n");
1654 goto fail;
1655 }
1656
1657 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1658 if (!ret) {
1659 fprintf(stderr, "Failed GetOverlappedResult\n");
1660 if (ov.hEvent) {
1661 CloseHandle(ov.hEvent);
1662 ov.hEvent = NULL;
1663 }
1664 goto fail;
1665 }
1666
1667 if (ov.hEvent) {
1668 CloseHandle(ov.hEvent);
1669 ov.hEvent = NULL;
1670 }
1671 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1672 return 0;
1673
1674 fail:
1675 win_chr_close(chr);
1676 return -1;
1677}
1678
1679
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001680static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001681{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001682 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001683 CharDriverState *chr;
1684 WinCharState *s;
1685
1686 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001687 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001688 chr->opaque = s;
1689 chr->chr_write = win_chr_write;
1690 chr->chr_close = win_chr_close;
1691
1692 if (win_chr_pipe_init(chr, filename) < 0) {
1693 free(s);
1694 free(chr);
1695 return NULL;
1696 }
1697 qemu_chr_reset(chr);
1698 return chr;
1699}
1700
1701static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1702{
1703 CharDriverState *chr;
1704 WinCharState *s;
1705
1706 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001707 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001708 s->hcom = fd_out;
1709 chr->opaque = s;
1710 chr->chr_write = win_chr_write;
1711 qemu_chr_reset(chr);
1712 return chr;
1713}
1714
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02001715static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001716{
1717 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1718}
1719
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001720static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001721{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001722 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001723 HANDLE fd_out;
1724
1725 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1726 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1727 if (fd_out == INVALID_HANDLE_VALUE)
1728 return NULL;
1729
1730 return qemu_chr_open_win_file(fd_out);
1731}
1732#endif /* !_WIN32 */
1733
1734/***********************************************************/
1735/* UDP Net console */
1736
1737typedef struct {
1738 int fd;
aliguori6f97dba2008-10-31 18:49:55 +00001739 uint8_t buf[1024];
1740 int bufcnt;
1741 int bufptr;
1742 int max_size;
1743} NetCharDriver;
1744
1745static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1746{
1747 NetCharDriver *s = chr->opaque;
1748
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001749 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00001750}
1751
1752static int udp_chr_read_poll(void *opaque)
1753{
1754 CharDriverState *chr = opaque;
1755 NetCharDriver *s = chr->opaque;
1756
1757 s->max_size = qemu_chr_can_read(chr);
1758
1759 /* If there were any stray characters in the queue process them
1760 * first
1761 */
1762 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1763 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1764 s->bufptr++;
1765 s->max_size = qemu_chr_can_read(chr);
1766 }
1767 return s->max_size;
1768}
1769
1770static void udp_chr_read(void *opaque)
1771{
1772 CharDriverState *chr = opaque;
1773 NetCharDriver *s = chr->opaque;
1774
1775 if (s->max_size == 0)
1776 return;
Blue Swirlc5b76b32009-06-13 08:44:31 +00001777 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00001778 s->bufptr = s->bufcnt;
1779 if (s->bufcnt <= 0)
1780 return;
1781
1782 s->bufptr = 0;
1783 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1784 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1785 s->bufptr++;
1786 s->max_size = qemu_chr_can_read(chr);
1787 }
1788}
1789
1790static void udp_chr_update_read_handler(CharDriverState *chr)
1791{
1792 NetCharDriver *s = chr->opaque;
1793
1794 if (s->fd >= 0) {
1795 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1796 udp_chr_read, NULL, chr);
1797 }
1798}
1799
aliguori819f56b2009-03-28 17:58:14 +00001800static void udp_chr_close(CharDriverState *chr)
1801{
1802 NetCharDriver *s = chr->opaque;
1803 if (s->fd >= 0) {
1804 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1805 closesocket(s->fd);
1806 }
1807 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05301808 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00001809}
1810
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001811static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001812{
1813 CharDriverState *chr = NULL;
1814 NetCharDriver *s = NULL;
1815 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00001816
1817 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001818 s = qemu_mallocz(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001819
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001820 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00001821 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001822 fprintf(stderr, "inet_dgram_opts failed\n");
aliguori6f97dba2008-10-31 18:49:55 +00001823 goto return_err;
1824 }
1825
1826 s->fd = fd;
1827 s->bufcnt = 0;
1828 s->bufptr = 0;
1829 chr->opaque = s;
1830 chr->chr_write = udp_chr_write;
1831 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00001832 chr->chr_close = udp_chr_close;
aliguori6f97dba2008-10-31 18:49:55 +00001833 return chr;
1834
1835return_err:
1836 if (chr)
1837 free(chr);
1838 if (s)
1839 free(s);
1840 if (fd >= 0)
1841 closesocket(fd);
1842 return NULL;
1843}
1844
1845/***********************************************************/
1846/* TCP Net console */
1847
1848typedef struct {
1849 int fd, listen_fd;
1850 int connected;
1851 int max_size;
1852 int do_telnetopt;
1853 int do_nodelay;
1854 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001855 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00001856} TCPCharDriver;
1857
1858static void tcp_chr_accept(void *opaque);
1859
1860static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1861{
1862 TCPCharDriver *s = chr->opaque;
1863 if (s->connected) {
1864 return send_all(s->fd, buf, len);
1865 } else {
1866 /* XXX: indicate an error ? */
1867 return len;
1868 }
1869}
1870
1871static int tcp_chr_read_poll(void *opaque)
1872{
1873 CharDriverState *chr = opaque;
1874 TCPCharDriver *s = chr->opaque;
1875 if (!s->connected)
1876 return 0;
1877 s->max_size = qemu_chr_can_read(chr);
1878 return s->max_size;
1879}
1880
1881#define IAC 255
1882#define IAC_BREAK 243
1883static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1884 TCPCharDriver *s,
1885 uint8_t *buf, int *size)
1886{
1887 /* Handle any telnet client's basic IAC options to satisfy char by
1888 * char mode with no echo. All IAC options will be removed from
1889 * the buf and the do_telnetopt variable will be used to track the
1890 * state of the width of the IAC information.
1891 *
1892 * IAC commands come in sets of 3 bytes with the exception of the
1893 * "IAC BREAK" command and the double IAC.
1894 */
1895
1896 int i;
1897 int j = 0;
1898
1899 for (i = 0; i < *size; i++) {
1900 if (s->do_telnetopt > 1) {
1901 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1902 /* Double IAC means send an IAC */
1903 if (j != i)
1904 buf[j] = buf[i];
1905 j++;
1906 s->do_telnetopt = 1;
1907 } else {
1908 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1909 /* Handle IAC break commands by sending a serial break */
1910 qemu_chr_event(chr, CHR_EVENT_BREAK);
1911 s->do_telnetopt++;
1912 }
1913 s->do_telnetopt++;
1914 }
1915 if (s->do_telnetopt >= 4) {
1916 s->do_telnetopt = 1;
1917 }
1918 } else {
1919 if ((unsigned char)buf[i] == IAC) {
1920 s->do_telnetopt = 2;
1921 } else {
1922 if (j != i)
1923 buf[j] = buf[i];
1924 j++;
1925 }
1926 }
1927 }
1928 *size = j;
1929}
1930
Mark McLoughlin7d174052009-07-22 09:11:39 +01001931static int tcp_get_msgfd(CharDriverState *chr)
1932{
1933 TCPCharDriver *s = chr->opaque;
1934
1935 return s->msgfd;
1936}
1937
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05001938#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01001939static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1940{
1941 TCPCharDriver *s = chr->opaque;
1942 struct cmsghdr *cmsg;
1943
1944 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1945 int fd;
1946
1947 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1948 cmsg->cmsg_level != SOL_SOCKET ||
1949 cmsg->cmsg_type != SCM_RIGHTS)
1950 continue;
1951
1952 fd = *((int *)CMSG_DATA(cmsg));
1953 if (fd < 0)
1954 continue;
1955
1956 if (s->msgfd != -1)
1957 close(s->msgfd);
1958 s->msgfd = fd;
1959 }
1960}
1961
Mark McLoughlin9977c892009-07-22 09:11:38 +01001962static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1963{
1964 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00001965 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01001966 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01001967 union {
1968 struct cmsghdr cmsg;
1969 char control[CMSG_SPACE(sizeof(int))];
1970 } msg_control;
1971 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01001972
1973 iov[0].iov_base = buf;
1974 iov[0].iov_len = len;
1975
1976 msg.msg_iov = iov;
1977 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001978 msg.msg_control = &msg_control;
1979 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01001980
Mark McLoughlin7d174052009-07-22 09:11:39 +01001981 ret = recvmsg(s->fd, &msg, 0);
1982 if (ret > 0 && s->is_unix)
1983 unix_process_msgfd(chr, &msg);
1984
1985 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01001986}
1987#else
1988static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1989{
1990 TCPCharDriver *s = chr->opaque;
1991 return recv(s->fd, buf, len, 0);
1992}
1993#endif
1994
aliguori6f97dba2008-10-31 18:49:55 +00001995static void tcp_chr_read(void *opaque)
1996{
1997 CharDriverState *chr = opaque;
1998 TCPCharDriver *s = chr->opaque;
1999 uint8_t buf[1024];
2000 int len, size;
2001
2002 if (!s->connected || s->max_size <= 0)
2003 return;
2004 len = sizeof(buf);
2005 if (len > s->max_size)
2006 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002007 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002008 if (size == 0) {
2009 /* connection closed */
2010 s->connected = 0;
2011 if (s->listen_fd >= 0) {
2012 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2013 }
2014 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2015 closesocket(s->fd);
2016 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302017 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002018 } else if (size > 0) {
2019 if (s->do_telnetopt)
2020 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2021 if (size > 0)
2022 qemu_chr_read(chr, buf, size);
Mark McLoughlin7d174052009-07-22 09:11:39 +01002023 if (s->msgfd != -1) {
2024 close(s->msgfd);
2025 s->msgfd = -1;
2026 }
aliguori6f97dba2008-10-31 18:49:55 +00002027 }
2028}
2029
2030static void tcp_chr_connect(void *opaque)
2031{
2032 CharDriverState *chr = opaque;
2033 TCPCharDriver *s = chr->opaque;
2034
2035 s->connected = 1;
2036 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2037 tcp_chr_read, NULL, chr);
2038 qemu_chr_reset(chr);
2039}
2040
2041#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2042static void tcp_chr_telnet_init(int fd)
2043{
2044 char buf[3];
2045 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2046 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2047 send(fd, (char *)buf, 3, 0);
2048 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2049 send(fd, (char *)buf, 3, 0);
2050 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2051 send(fd, (char *)buf, 3, 0);
2052 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2053 send(fd, (char *)buf, 3, 0);
2054}
2055
2056static void socket_set_nodelay(int fd)
2057{
2058 int val = 1;
2059 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2060}
2061
2062static void tcp_chr_accept(void *opaque)
2063{
2064 CharDriverState *chr = opaque;
2065 TCPCharDriver *s = chr->opaque;
2066 struct sockaddr_in saddr;
2067#ifndef _WIN32
2068 struct sockaddr_un uaddr;
2069#endif
2070 struct sockaddr *addr;
2071 socklen_t len;
2072 int fd;
2073
2074 for(;;) {
2075#ifndef _WIN32
2076 if (s->is_unix) {
2077 len = sizeof(uaddr);
2078 addr = (struct sockaddr *)&uaddr;
2079 } else
2080#endif
2081 {
2082 len = sizeof(saddr);
2083 addr = (struct sockaddr *)&saddr;
2084 }
2085 fd = accept(s->listen_fd, addr, &len);
2086 if (fd < 0 && errno != EINTR) {
2087 return;
2088 } else if (fd >= 0) {
2089 if (s->do_telnetopt)
2090 tcp_chr_telnet_init(fd);
2091 break;
2092 }
2093 }
2094 socket_set_nonblock(fd);
2095 if (s->do_nodelay)
2096 socket_set_nodelay(fd);
2097 s->fd = fd;
2098 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2099 tcp_chr_connect(chr);
2100}
2101
2102static void tcp_chr_close(CharDriverState *chr)
2103{
2104 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002105 if (s->fd >= 0) {
2106 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002107 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002108 }
2109 if (s->listen_fd >= 0) {
2110 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002111 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002112 }
aliguori6f97dba2008-10-31 18:49:55 +00002113 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302114 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002115}
2116
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002117static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002118{
2119 CharDriverState *chr = NULL;
2120 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002121 int fd = -1;
2122 int is_listen;
2123 int is_waitconnect;
2124 int do_nodelay;
2125 int is_unix;
2126 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002127
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002128 is_listen = qemu_opt_get_bool(opts, "server", 0);
2129 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2130 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2131 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2132 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002133 if (!is_listen)
2134 is_waitconnect = 0;
2135
2136 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00002137 s = qemu_mallocz(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002138
aliguorif07b6002008-11-11 20:54:09 +00002139 if (is_unix) {
2140 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002141 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002142 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002143 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002144 }
2145 } else {
2146 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002147 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002148 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002149 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002150 }
2151 }
aliguori6f97dba2008-10-31 18:49:55 +00002152 if (fd < 0)
2153 goto fail;
2154
2155 if (!is_waitconnect)
2156 socket_set_nonblock(fd);
2157
2158 s->connected = 0;
2159 s->fd = -1;
2160 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002161 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002162 s->is_unix = is_unix;
2163 s->do_nodelay = do_nodelay && !is_unix;
2164
2165 chr->opaque = s;
2166 chr->chr_write = tcp_chr_write;
2167 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002168 chr->get_msgfd = tcp_get_msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002169
2170 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002171 s->listen_fd = fd;
2172 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2173 if (is_telnet)
2174 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002175
aliguori6f97dba2008-10-31 18:49:55 +00002176 } else {
aliguorif07b6002008-11-11 20:54:09 +00002177 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002178 s->fd = fd;
2179 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002180 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002181 }
2182
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002183 /* for "info chardev" monitor command */
2184 chr->filename = qemu_malloc(256);
2185 if (is_unix) {
2186 snprintf(chr->filename, 256, "unix:%s%s",
2187 qemu_opt_get(opts, "path"),
2188 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2189 } else if (is_telnet) {
2190 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2191 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2192 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2193 } else {
2194 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2195 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2196 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2197 }
2198
aliguori6f97dba2008-10-31 18:49:55 +00002199 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002200 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002201 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002202 tcp_chr_accept(chr);
2203 socket_set_nonblock(s->listen_fd);
2204 }
aliguori6f97dba2008-10-31 18:49:55 +00002205 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002206
aliguori6f97dba2008-10-31 18:49:55 +00002207 fail:
2208 if (fd >= 0)
2209 closesocket(fd);
2210 qemu_free(s);
2211 qemu_free(chr);
2212 return NULL;
2213}
2214
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002215static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2216{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002217 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002218 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002219 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002220 QemuOpts *opts;
2221
2222 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2223 if (NULL == opts)
2224 return NULL;
2225
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002226 if (strstart(filename, "mon:", &p)) {
2227 filename = p;
2228 qemu_opt_set(opts, "mux", "on");
2229 }
2230
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002231 if (strcmp(filename, "null") == 0 ||
2232 strcmp(filename, "pty") == 0 ||
2233 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002234 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002235 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002236 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002237 return opts;
2238 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002239 if (strstart(filename, "vc", &p)) {
2240 qemu_opt_set(opts, "backend", "vc");
2241 if (*p == ':') {
2242 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2243 /* pixels */
2244 qemu_opt_set(opts, "width", width);
2245 qemu_opt_set(opts, "height", height);
2246 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2247 /* chars */
2248 qemu_opt_set(opts, "cols", width);
2249 qemu_opt_set(opts, "rows", height);
2250 } else {
2251 goto fail;
2252 }
2253 }
2254 return opts;
2255 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002256 if (strcmp(filename, "con:") == 0) {
2257 qemu_opt_set(opts, "backend", "console");
2258 return opts;
2259 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002260 if (strstart(filename, "COM", NULL)) {
2261 qemu_opt_set(opts, "backend", "serial");
2262 qemu_opt_set(opts, "path", filename);
2263 return opts;
2264 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002265 if (strstart(filename, "file:", &p)) {
2266 qemu_opt_set(opts, "backend", "file");
2267 qemu_opt_set(opts, "path", p);
2268 return opts;
2269 }
2270 if (strstart(filename, "pipe:", &p)) {
2271 qemu_opt_set(opts, "backend", "pipe");
2272 qemu_opt_set(opts, "path", p);
2273 return opts;
2274 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002275 if (strstart(filename, "tcp:", &p) ||
2276 strstart(filename, "telnet:", &p)) {
2277 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2278 host[0] = 0;
2279 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2280 goto fail;
2281 }
2282 qemu_opt_set(opts, "backend", "socket");
2283 qemu_opt_set(opts, "host", host);
2284 qemu_opt_set(opts, "port", port);
2285 if (p[pos] == ',') {
2286 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2287 goto fail;
2288 }
2289 if (strstart(filename, "telnet:", &p))
2290 qemu_opt_set(opts, "telnet", "on");
2291 return opts;
2292 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002293 if (strstart(filename, "udp:", &p)) {
2294 qemu_opt_set(opts, "backend", "udp");
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 #1\n");
2299 goto fail;
2300 }
2301 }
2302 qemu_opt_set(opts, "host", host);
2303 qemu_opt_set(opts, "port", port);
2304 if (p[pos] == '@') {
2305 p += pos + 1;
2306 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2307 host[0] = 0;
2308 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2309 fprintf(stderr, "udp #2\n");
2310 goto fail;
2311 }
2312 }
2313 qemu_opt_set(opts, "localaddr", host);
2314 qemu_opt_set(opts, "localport", port);
2315 }
2316 return opts;
2317 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002318 if (strstart(filename, "unix:", &p)) {
2319 qemu_opt_set(opts, "backend", "socket");
2320 if (qemu_opts_do_parse(opts, p, "path") != 0)
2321 goto fail;
2322 return opts;
2323 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002324 if (strstart(filename, "/dev/parport", NULL) ||
2325 strstart(filename, "/dev/ppi", NULL)) {
2326 qemu_opt_set(opts, "backend", "parport");
2327 qemu_opt_set(opts, "path", filename);
2328 return opts;
2329 }
2330 if (strstart(filename, "/dev/", NULL)) {
2331 qemu_opt_set(opts, "backend", "tty");
2332 qemu_opt_set(opts, "path", filename);
2333 return opts;
2334 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002335
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002336fail:
2337 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002338 qemu_opts_del(opts);
2339 return NULL;
2340}
2341
2342static const struct {
2343 const char *name;
2344 CharDriverState *(*open)(QemuOpts *opts);
2345} backend_table[] = {
2346 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002347 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002348 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002349 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002350 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002351#ifdef _WIN32
2352 { .name = "file", .open = qemu_chr_open_win_file_out },
2353 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002354 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002355 { .name = "serial", .open = qemu_chr_open_win },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002356#else
2357 { .name = "file", .open = qemu_chr_open_file_out },
2358 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002359 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002360 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002361#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002362#ifdef CONFIG_BRLAPI
2363 { .name = "braille", .open = chr_baum_init },
2364#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002365#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2366 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2367 { .name = "tty", .open = qemu_chr_open_tty },
2368#endif
2369#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2370 { .name = "parport", .open = qemu_chr_open_pp },
2371#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002372};
2373
2374CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2375 void (*init)(struct CharDriverState *s))
2376{
2377 CharDriverState *chr;
2378 int i;
2379
2380 if (qemu_opts_id(opts) == NULL) {
2381 fprintf(stderr, "chardev: no id specified\n");
2382 return NULL;
2383 }
2384
2385 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2386 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2387 break;
2388 }
2389 if (i == ARRAY_SIZE(backend_table)) {
2390 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2391 qemu_opt_get(opts, "backend"));
2392 return NULL;
2393 }
2394
2395 chr = backend_table[i].open(opts);
2396 if (!chr) {
2397 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2398 qemu_opt_get(opts, "backend"));
2399 return NULL;
2400 }
2401
2402 if (!chr->filename)
2403 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2404 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002405 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002406
2407 if (qemu_opt_get_bool(opts, "mux", 0)) {
2408 CharDriverState *base = chr;
2409 int len = strlen(qemu_opts_id(opts)) + 6;
2410 base->label = qemu_malloc(len);
2411 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2412 chr = qemu_chr_open_mux(base);
2413 chr->filename = base->filename;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002414 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002415 }
2416 chr->label = qemu_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002417 return chr;
2418}
2419
aurel32ceecf1d2009-01-18 14:08:04 +00002420CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002421{
2422 const char *p;
2423 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002424 QemuOpts *opts;
2425
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002426 if (strstart(filename, "chardev:", &p)) {
2427 return qemu_chr_find(p);
2428 }
2429
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002430 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002431 if (!opts)
2432 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002433
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002434 chr = qemu_chr_open_opts(opts, init);
2435 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2436 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002437 }
2438 return chr;
2439}
2440
2441void qemu_chr_close(CharDriverState *chr)
2442{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002443 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002444 if (chr->chr_close)
2445 chr->chr_close(chr);
2446 qemu_free(chr->filename);
2447 qemu_free(chr->label);
2448 qemu_free(chr);
2449}
2450
aliguori376253e2009-03-05 23:01:23 +00002451void qemu_chr_info(Monitor *mon)
aliguori6f97dba2008-10-31 18:49:55 +00002452{
2453 CharDriverState *chr;
2454
Blue Swirl72cf2d42009-09-12 07:36:22 +00002455 QTAILQ_FOREACH(chr, &chardevs, next) {
aliguori376253e2009-03-05 23:01:23 +00002456 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002457 }
2458}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002459
2460CharDriverState *qemu_chr_find(const char *name)
2461{
2462 CharDriverState *chr;
2463
Blue Swirl72cf2d42009-09-12 07:36:22 +00002464 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002465 if (strcmp(chr->label, name) != 0)
2466 continue;
2467 return chr;
2468 }
2469 return NULL;
2470}