blob: 1f63019520f646b7328e9087c751114f4d0bb0fd [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
Amit Shah9bd78542009-11-03 19:59:54 +0530100#define READ_BUF_LEN 4096
101
aliguori6f97dba2008-10-31 18:49:55 +0000102/***********************************************************/
103/* character device */
104
Blue Swirl72cf2d42009-09-12 07:36:22 +0000105static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000107
aliguori6f97dba2008-10-31 18:49:55 +0000108static void qemu_chr_event(CharDriverState *s, int event)
109{
110 if (!s->chr_event)
111 return;
112 s->chr_event(s->handler_opaque, event);
113}
114
115static void qemu_chr_reset_bh(void *opaque)
116{
117 CharDriverState *s = opaque;
Anthony Liguorif7cbc082009-10-27 10:14:50 -0500118 qemu_chr_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000119 qemu_bh_delete(s->bh);
120 s->bh = NULL;
121}
122
123void qemu_chr_reset(CharDriverState *s)
124{
Amit Shah69795d62009-10-07 18:31:15 +0530125 if (s->bh == NULL) {
aliguori6f97dba2008-10-31 18:49:55 +0000126 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
127 qemu_bh_schedule(s->bh);
128 }
129}
130
aliguori2970a6c2009-03-05 22:59:58 +0000131void qemu_chr_initial_reset(void)
132{
133 CharDriverState *chr;
134
Blue Swirl72cf2d42009-09-12 07:36:22 +0000135 QTAILQ_FOREACH(chr, &chardevs, next) {
aliguori2970a6c2009-03-05 22:59:58 +0000136 qemu_chr_reset(chr);
137 }
138}
139
aliguori6f97dba2008-10-31 18:49:55 +0000140int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
141{
142 return s->chr_write(s, buf, len);
143}
144
145int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
146{
147 if (!s->chr_ioctl)
148 return -ENOTSUP;
149 return s->chr_ioctl(s, cmd, arg);
150}
151
152int qemu_chr_can_read(CharDriverState *s)
153{
154 if (!s->chr_can_read)
155 return 0;
156 return s->chr_can_read(s->handler_opaque);
157}
158
159void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
160{
161 s->chr_read(s->handler_opaque, buf, len);
162}
163
Mark McLoughlin7d174052009-07-22 09:11:39 +0100164int qemu_chr_get_msgfd(CharDriverState *s)
165{
166 return s->get_msgfd ? s->get_msgfd(s) : -1;
167}
168
aliguori6f97dba2008-10-31 18:49:55 +0000169void qemu_chr_accept_input(CharDriverState *s)
170{
171 if (s->chr_accept_input)
172 s->chr_accept_input(s);
173}
174
175void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
176{
Amit Shah9bd78542009-11-03 19:59:54 +0530177 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000178 va_list ap;
179 va_start(ap, fmt);
180 vsnprintf(buf, sizeof(buf), fmt, ap);
181 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
182 va_end(ap);
183}
184
185void qemu_chr_send_event(CharDriverState *s, int event)
186{
187 if (s->chr_send_event)
188 s->chr_send_event(s, event);
189}
190
191void qemu_chr_add_handlers(CharDriverState *s,
192 IOCanRWHandler *fd_can_read,
193 IOReadHandler *fd_read,
194 IOEventHandler *fd_event,
195 void *opaque)
196{
197 s->chr_can_read = fd_can_read;
198 s->chr_read = fd_read;
199 s->chr_event = fd_event;
200 s->handler_opaque = opaque;
201 if (s->chr_update_read_handler)
202 s->chr_update_read_handler(s);
203}
204
205static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
206{
207 return len;
208}
209
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200210static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000211{
212 CharDriverState *chr;
213
214 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000215 chr->chr_write = null_chr_write;
216 return chr;
217}
218
219/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000220#define MAX_MUX 4
221#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
222#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
223typedef struct {
224 IOCanRWHandler *chr_can_read[MAX_MUX];
225 IOReadHandler *chr_read[MAX_MUX];
226 IOEventHandler *chr_event[MAX_MUX];
227 void *ext_opaque[MAX_MUX];
228 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200229 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000230 int mux_cnt;
231 int term_got_escape;
232 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000233 /* Intermediate input buffer allows to catch escape sequences even if the
234 currently active device is not accepting any input - but only until it
235 is full as well. */
236 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
237 int prod[MAX_MUX];
238 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200239 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200240 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200241 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000242} MuxDriver;
243
244
245static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
246{
247 MuxDriver *d = chr->opaque;
248 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200249 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000250 ret = d->drv->chr_write(d->drv, buf, len);
251 } else {
252 int i;
253
254 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200255 for (i = 0; i < len; i++) {
256 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000257 char buf1[64];
258 int64_t ti;
259 int secs;
260
261 ti = qemu_get_clock(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200262 if (d->timestamps_start == -1)
263 d->timestamps_start = ti;
264 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000265 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000266 snprintf(buf1, sizeof(buf1),
267 "[%02d:%02d:%02d.%03d] ",
268 secs / 3600,
269 (secs / 60) % 60,
270 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000271 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000272 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200273 d->linestart = 0;
274 }
275 ret += d->drv->chr_write(d->drv, buf+i, 1);
276 if (buf[i] == '\n') {
277 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000278 }
279 }
280 }
281 return ret;
282}
283
284static const char * const mux_help[] = {
285 "% h print this help\n\r",
286 "% x exit emulator\n\r",
287 "% s save disk data back to file (if -snapshot)\n\r",
288 "% t toggle console timestamps\n\r"
289 "% b send break (magic sysrq)\n\r",
290 "% c switch between console and monitor\n\r",
291 "% % sends %\n\r",
292 NULL
293};
294
295int term_escape_char = 0x01; /* ctrl-a is used for escape */
296static void mux_print_help(CharDriverState *chr)
297{
298 int i, j;
299 char ebuf[15] = "Escape-Char";
300 char cbuf[50] = "\n\r";
301
302 if (term_escape_char > 0 && term_escape_char < 26) {
303 snprintf(cbuf, sizeof(cbuf), "\n\r");
304 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
305 } else {
306 snprintf(cbuf, sizeof(cbuf),
307 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
308 term_escape_char);
309 }
310 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
311 for (i = 0; mux_help[i] != NULL; i++) {
312 for (j=0; mux_help[i][j] != '\0'; j++) {
313 if (mux_help[i][j] == '%')
314 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
315 else
316 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
317 }
318 }
319}
320
aliguori2724b182009-03-05 23:01:47 +0000321static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
322{
323 if (d->chr_event[mux_nr])
324 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
325}
326
aliguori6f97dba2008-10-31 18:49:55 +0000327static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
328{
329 if (d->term_got_escape) {
330 d->term_got_escape = 0;
331 if (ch == term_escape_char)
332 goto send_char;
333 switch(ch) {
334 case '?':
335 case 'h':
336 mux_print_help(chr);
337 break;
338 case 'x':
339 {
340 const char *term = "QEMU: Terminated\n\r";
341 chr->chr_write(chr,(uint8_t *)term,strlen(term));
342 exit(0);
343 break;
344 }
345 case 's':
346 {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200347 DriveInfo *dinfo;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000348 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200349 bdrv_commit(dinfo->bdrv);
aliguori6f97dba2008-10-31 18:49:55 +0000350 }
351 }
352 break;
353 case 'b':
354 qemu_chr_event(chr, CHR_EVENT_BREAK);
355 break;
356 case 'c':
357 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200358 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
359 d->focus++;
360 if (d->focus >= d->mux_cnt)
361 d->focus = 0;
362 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000363 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200364 case 't':
365 d->timestamps = !d->timestamps;
366 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200367 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200368 break;
aliguori6f97dba2008-10-31 18:49:55 +0000369 }
370 } else if (ch == term_escape_char) {
371 d->term_got_escape = 1;
372 } else {
373 send_char:
374 return 1;
375 }
376 return 0;
377}
378
379static void mux_chr_accept_input(CharDriverState *chr)
380{
aliguori6f97dba2008-10-31 18:49:55 +0000381 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200382 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000383
aliguoria80bf992009-03-05 23:00:02 +0000384 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000385 d->chr_can_read[m] &&
386 d->chr_can_read[m](d->ext_opaque[m])) {
387 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000388 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000389 }
390}
391
392static int mux_chr_can_read(void *opaque)
393{
394 CharDriverState *chr = opaque;
395 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200396 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000397
aliguoria80bf992009-03-05 23:00:02 +0000398 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000399 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000400 if (d->chr_can_read[m])
401 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000402 return 0;
403}
404
405static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
406{
407 CharDriverState *chr = opaque;
408 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200409 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000410 int i;
411
412 mux_chr_accept_input (opaque);
413
414 for(i = 0; i < size; i++)
415 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000416 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000417 d->chr_can_read[m] &&
418 d->chr_can_read[m](d->ext_opaque[m]))
419 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
420 else
aliguoria80bf992009-03-05 23:00:02 +0000421 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000422 }
423}
424
425static void mux_chr_event(void *opaque, int event)
426{
427 CharDriverState *chr = opaque;
428 MuxDriver *d = chr->opaque;
429 int i;
430
431 /* Send the event to all registered listeners */
432 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000433 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000434}
435
436static void mux_chr_update_read_handler(CharDriverState *chr)
437{
438 MuxDriver *d = chr->opaque;
439
440 if (d->mux_cnt >= MAX_MUX) {
441 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
442 return;
443 }
444 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
445 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
446 d->chr_read[d->mux_cnt] = chr->chr_read;
447 d->chr_event[d->mux_cnt] = chr->chr_event;
448 /* Fix up the real driver with mux routines */
449 if (d->mux_cnt == 0) {
450 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
451 mux_chr_event, chr);
452 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200453 if (d->focus != -1) {
454 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200455 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200456 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000457 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200458 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000459}
460
461static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
462{
463 CharDriverState *chr;
464 MuxDriver *d;
465
466 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000467 d = qemu_mallocz(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000468
469 chr->opaque = d;
470 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200471 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000472 chr->chr_write = mux_chr_write;
473 chr->chr_update_read_handler = mux_chr_update_read_handler;
474 chr->chr_accept_input = mux_chr_accept_input;
475 return chr;
476}
477
478
479#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000480int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000481{
482 int ret, len;
483
484 len = len1;
485 while (len > 0) {
486 ret = send(fd, buf, len, 0);
487 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000488 errno = WSAGetLastError();
489 if (errno != WSAEWOULDBLOCK) {
490 return -1;
491 }
492 } else if (ret == 0) {
493 break;
494 } else {
495 buf += ret;
496 len -= ret;
497 }
498 }
499 return len1 - len;
500}
501
502#else
503
504static int unix_write(int fd, const uint8_t *buf, int len1)
505{
506 int ret, len;
507
508 len = len1;
509 while (len > 0) {
510 ret = write(fd, buf, len);
511 if (ret < 0) {
512 if (errno != EINTR && errno != EAGAIN)
513 return -1;
514 } else if (ret == 0) {
515 break;
516 } else {
517 buf += ret;
518 len -= ret;
519 }
520 }
521 return len1 - len;
522}
523
aliguorid247d252008-11-11 20:46:40 +0000524int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000525{
526 return unix_write(fd, buf, len1);
527}
528#endif /* !_WIN32 */
529
530#ifndef _WIN32
531
532typedef struct {
533 int fd_in, fd_out;
534 int max_size;
535} FDCharDriver;
536
537#define STDIO_MAX_CLIENTS 1
538static int stdio_nb_clients = 0;
539
540static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
541{
542 FDCharDriver *s = chr->opaque;
543 return send_all(s->fd_out, buf, len);
544}
545
546static int fd_chr_read_poll(void *opaque)
547{
548 CharDriverState *chr = opaque;
549 FDCharDriver *s = chr->opaque;
550
551 s->max_size = qemu_chr_can_read(chr);
552 return s->max_size;
553}
554
555static void fd_chr_read(void *opaque)
556{
557 CharDriverState *chr = opaque;
558 FDCharDriver *s = chr->opaque;
559 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530560 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000561
562 len = sizeof(buf);
563 if (len > s->max_size)
564 len = s->max_size;
565 if (len == 0)
566 return;
567 size = read(s->fd_in, buf, len);
568 if (size == 0) {
569 /* FD has been closed. Remove it from the active list. */
570 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530571 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000572 return;
573 }
574 if (size > 0) {
575 qemu_chr_read(chr, buf, size);
576 }
577}
578
579static void fd_chr_update_read_handler(CharDriverState *chr)
580{
581 FDCharDriver *s = chr->opaque;
582
583 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500584 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000585 } else {
586 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
587 fd_chr_read, NULL, chr);
588 }
589 }
590}
591
592static void fd_chr_close(struct CharDriverState *chr)
593{
594 FDCharDriver *s = chr->opaque;
595
596 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500597 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000598 } else {
599 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
600 }
601 }
602
603 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530604 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000605}
606
607/* open a character device to a unix fd */
608static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
609{
610 CharDriverState *chr;
611 FDCharDriver *s;
612
613 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000614 s = qemu_mallocz(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000615 s->fd_in = fd_in;
616 s->fd_out = fd_out;
617 chr->opaque = s;
618 chr->chr_write = fd_chr_write;
619 chr->chr_update_read_handler = fd_chr_update_read_handler;
620 chr->chr_close = fd_chr_close;
621
622 qemu_chr_reset(chr);
623
624 return chr;
625}
626
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200627static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000628{
629 int fd_out;
630
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200631 TFR(fd_out = open(qemu_opt_get(opts, "path"),
632 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
aliguori6f97dba2008-10-31 18:49:55 +0000633 if (fd_out < 0)
634 return NULL;
635 return qemu_chr_open_fd(-1, fd_out);
636}
637
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200638static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000639{
640 int fd_in, fd_out;
641 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200642 const char *filename = qemu_opt_get(opts, "path");
643
644 if (filename == NULL) {
645 fprintf(stderr, "chardev: pipe: no filename given\n");
646 return NULL;
647 }
aliguori6f97dba2008-10-31 18:49:55 +0000648
649 snprintf(filename_in, 256, "%s.in", filename);
650 snprintf(filename_out, 256, "%s.out", filename);
651 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
652 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
653 if (fd_in < 0 || fd_out < 0) {
654 if (fd_in >= 0)
655 close(fd_in);
656 if (fd_out >= 0)
657 close(fd_out);
658 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
659 if (fd_in < 0)
660 return NULL;
661 }
662 return qemu_chr_open_fd(fd_in, fd_out);
663}
664
665
666/* for STDIO, we handle the case where several clients use it
667 (nographic mode) */
668
669#define TERM_FIFO_MAX_SIZE 1
670
671static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
672static int term_fifo_size;
673
674static int stdio_read_poll(void *opaque)
675{
676 CharDriverState *chr = opaque;
677
678 /* try to flush the queue if needed */
679 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
680 qemu_chr_read(chr, term_fifo, 1);
681 term_fifo_size = 0;
682 }
683 /* see if we can absorb more chars */
684 if (term_fifo_size == 0)
685 return 1;
686 else
687 return 0;
688}
689
690static void stdio_read(void *opaque)
691{
692 int size;
693 uint8_t buf[1];
694 CharDriverState *chr = opaque;
695
696 size = read(0, buf, 1);
697 if (size == 0) {
698 /* stdin has been closed. Remove it from the active list. */
699 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530700 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000701 return;
702 }
703 if (size > 0) {
704 if (qemu_chr_can_read(chr) > 0) {
705 qemu_chr_read(chr, buf, 1);
706 } else if (term_fifo_size == 0) {
707 term_fifo[term_fifo_size++] = buf[0];
708 }
709 }
710}
711
712/* init terminal so that we can grab keys */
713static struct termios oldtty;
714static int old_fd0_flags;
715static int term_atexit_done;
716
717static void term_exit(void)
718{
719 tcsetattr (0, TCSANOW, &oldtty);
720 fcntl(0, F_SETFL, old_fd0_flags);
721}
722
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900723static void term_init(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000724{
725 struct termios tty;
726
727 tcgetattr (0, &tty);
728 oldtty = tty;
729 old_fd0_flags = fcntl(0, F_GETFL);
730
731 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
732 |INLCR|IGNCR|ICRNL|IXON);
733 tty.c_oflag |= OPOST;
734 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
735 /* if graphical mode, we allow Ctrl-C handling */
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900736 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
aliguori6f97dba2008-10-31 18:49:55 +0000737 tty.c_lflag &= ~ISIG;
738 tty.c_cflag &= ~(CSIZE|PARENB);
739 tty.c_cflag |= CS8;
740 tty.c_cc[VMIN] = 1;
741 tty.c_cc[VTIME] = 0;
742
743 tcsetattr (0, TCSANOW, &tty);
744
745 if (!term_atexit_done++)
746 atexit(term_exit);
747
748 fcntl(0, F_SETFL, O_NONBLOCK);
749}
750
751static void qemu_chr_close_stdio(struct CharDriverState *chr)
752{
753 term_exit();
754 stdio_nb_clients--;
755 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
756 fd_chr_close(chr);
757}
758
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +0200759static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000760{
761 CharDriverState *chr;
762
763 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
764 return NULL;
765 chr = qemu_chr_open_fd(0, 1);
766 chr->chr_close = qemu_chr_close_stdio;
767 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
768 stdio_nb_clients++;
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900769 term_init(opts);
aliguori6f97dba2008-10-31 18:49:55 +0000770
771 return chr;
772}
773
774#ifdef __sun__
775/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000776static int openpty(int *amaster, int *aslave, char *name,
777 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000778{
779 const char *slave;
780 int mfd = -1, sfd = -1;
781
782 *amaster = *aslave = -1;
783
784 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
785 if (mfd < 0)
786 goto err;
787
788 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
789 goto err;
790
791 if ((slave = ptsname(mfd)) == NULL)
792 goto err;
793
794 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
795 goto err;
796
797 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
798 (termp != NULL && tcgetattr(sfd, termp) < 0))
799 goto err;
800
801 if (amaster)
802 *amaster = mfd;
803 if (aslave)
804 *aslave = sfd;
805 if (winp)
806 ioctl(sfd, TIOCSWINSZ, winp);
807
808 return 0;
809
810err:
811 if (sfd != -1)
812 close(sfd);
813 close(mfd);
814 return -1;
815}
816
blueswir13f4cb3d2009-04-13 16:31:01 +0000817static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000818{
819 termios_p->c_iflag &=
820 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
821 termios_p->c_oflag &= ~OPOST;
822 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
823 termios_p->c_cflag &= ~(CSIZE|PARENB);
824 termios_p->c_cflag |= CS8;
825
826 termios_p->c_cc[VMIN] = 0;
827 termios_p->c_cc[VTIME] = 0;
828}
829#endif
830
831#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
blueswir1c5e97232009-03-07 20:06:23 +0000832 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000833
834typedef struct {
835 int fd;
836 int connected;
837 int polling;
838 int read_bytes;
839 QEMUTimer *timer;
840} PtyCharDriver;
841
842static void pty_chr_update_read_handler(CharDriverState *chr);
843static void pty_chr_state(CharDriverState *chr, int connected);
844
845static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
846{
847 PtyCharDriver *s = chr->opaque;
848
849 if (!s->connected) {
850 /* guest sends data, check for (re-)connect */
851 pty_chr_update_read_handler(chr);
852 return 0;
853 }
854 return send_all(s->fd, buf, len);
855}
856
857static int pty_chr_read_poll(void *opaque)
858{
859 CharDriverState *chr = opaque;
860 PtyCharDriver *s = chr->opaque;
861
862 s->read_bytes = qemu_chr_can_read(chr);
863 return s->read_bytes;
864}
865
866static void pty_chr_read(void *opaque)
867{
868 CharDriverState *chr = opaque;
869 PtyCharDriver *s = chr->opaque;
870 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530871 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000872
873 len = sizeof(buf);
874 if (len > s->read_bytes)
875 len = s->read_bytes;
876 if (len == 0)
877 return;
878 size = read(s->fd, buf, len);
879 if ((size == -1 && errno == EIO) ||
880 (size == 0)) {
881 pty_chr_state(chr, 0);
882 return;
883 }
884 if (size > 0) {
885 pty_chr_state(chr, 1);
886 qemu_chr_read(chr, buf, size);
887 }
888}
889
890static void pty_chr_update_read_handler(CharDriverState *chr)
891{
892 PtyCharDriver *s = chr->opaque;
893
894 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
895 pty_chr_read, NULL, chr);
896 s->polling = 1;
897 /*
898 * Short timeout here: just need wait long enougth that qemu makes
899 * it through the poll loop once. When reconnected we want a
900 * short timeout so we notice it almost instantly. Otherwise
901 * read() gives us -EIO instantly, making pty_chr_state() reset the
902 * timeout to the normal (much longer) poll interval before the
903 * timer triggers.
904 */
905 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
906}
907
908static void pty_chr_state(CharDriverState *chr, int connected)
909{
910 PtyCharDriver *s = chr->opaque;
911
912 if (!connected) {
913 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
914 s->connected = 0;
915 s->polling = 0;
916 /* (re-)connect poll interval for idle guests: once per second.
917 * We check more frequently in case the guests sends data to
918 * the virtual device linked to our pty. */
919 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
920 } else {
921 if (!s->connected)
922 qemu_chr_reset(chr);
923 s->connected = 1;
924 }
925}
926
927static void pty_chr_timer(void *opaque)
928{
929 struct CharDriverState *chr = opaque;
930 PtyCharDriver *s = chr->opaque;
931
932 if (s->connected)
933 return;
934 if (s->polling) {
935 /* If we arrive here without polling being cleared due
936 * read returning -EIO, then we are (re-)connected */
937 pty_chr_state(chr, 1);
938 return;
939 }
940
941 /* Next poll ... */
942 pty_chr_update_read_handler(chr);
943}
944
945static void pty_chr_close(struct CharDriverState *chr)
946{
947 PtyCharDriver *s = chr->opaque;
948
949 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
950 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000951 qemu_del_timer(s->timer);
952 qemu_free_timer(s->timer);
aliguori6f97dba2008-10-31 18:49:55 +0000953 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530954 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000955}
956
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200957static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000958{
959 CharDriverState *chr;
960 PtyCharDriver *s;
961 struct termios tty;
962 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000963#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000964 char pty_name[PATH_MAX];
965#define q_ptsname(x) pty_name
966#else
967 char *pty_name = NULL;
968#define q_ptsname(x) ptsname(x)
969#endif
970
971 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000972 s = qemu_mallocz(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000973
974 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
975 return NULL;
976 }
977
978 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +0000979 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000980 cfmakeraw(&tty);
981 tcsetattr(slave_fd, TCSAFLUSH, &tty);
982 close(slave_fd);
983
984 len = strlen(q_ptsname(s->fd)) + 5;
985 chr->filename = qemu_malloc(len);
986 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200987 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +0000988 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
989
990 chr->opaque = s;
991 chr->chr_write = pty_chr_write;
992 chr->chr_update_read_handler = pty_chr_update_read_handler;
993 chr->chr_close = pty_chr_close;
994
995 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
996
997 return chr;
998}
999
1000static void tty_serial_init(int fd, int speed,
1001 int parity, int data_bits, int stop_bits)
1002{
1003 struct termios tty;
1004 speed_t spd;
1005
1006#if 0
1007 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1008 speed, parity, data_bits, stop_bits);
1009#endif
1010 tcgetattr (fd, &tty);
1011
Stefan Weil45eea132009-10-26 16:10:10 +01001012#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1013 speed = speed * 10 / 11;
1014 do {
1015 check_speed(50);
1016 check_speed(75);
1017 check_speed(110);
1018 check_speed(134);
1019 check_speed(150);
1020 check_speed(200);
1021 check_speed(300);
1022 check_speed(600);
1023 check_speed(1200);
1024 check_speed(1800);
1025 check_speed(2400);
1026 check_speed(4800);
1027 check_speed(9600);
1028 check_speed(19200);
1029 check_speed(38400);
1030 /* Non-Posix values follow. They may be unsupported on some systems. */
1031 check_speed(57600);
1032 check_speed(115200);
1033#ifdef B230400
1034 check_speed(230400);
1035#endif
1036#ifdef B460800
1037 check_speed(460800);
1038#endif
1039#ifdef B500000
1040 check_speed(500000);
1041#endif
1042#ifdef B576000
1043 check_speed(576000);
1044#endif
1045#ifdef B921600
1046 check_speed(921600);
1047#endif
1048#ifdef B1000000
1049 check_speed(1000000);
1050#endif
1051#ifdef B1152000
1052 check_speed(1152000);
1053#endif
1054#ifdef B1500000
1055 check_speed(1500000);
1056#endif
1057#ifdef B2000000
1058 check_speed(2000000);
1059#endif
1060#ifdef B2500000
1061 check_speed(2500000);
1062#endif
1063#ifdef B3000000
1064 check_speed(3000000);
1065#endif
1066#ifdef B3500000
1067 check_speed(3500000);
1068#endif
1069#ifdef B4000000
1070 check_speed(4000000);
1071#endif
aliguori6f97dba2008-10-31 18:49:55 +00001072 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001073 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001074
1075 cfsetispeed(&tty, spd);
1076 cfsetospeed(&tty, spd);
1077
1078 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1079 |INLCR|IGNCR|ICRNL|IXON);
1080 tty.c_oflag |= OPOST;
1081 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1082 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1083 switch(data_bits) {
1084 default:
1085 case 8:
1086 tty.c_cflag |= CS8;
1087 break;
1088 case 7:
1089 tty.c_cflag |= CS7;
1090 break;
1091 case 6:
1092 tty.c_cflag |= CS6;
1093 break;
1094 case 5:
1095 tty.c_cflag |= CS5;
1096 break;
1097 }
1098 switch(parity) {
1099 default:
1100 case 'N':
1101 break;
1102 case 'E':
1103 tty.c_cflag |= PARENB;
1104 break;
1105 case 'O':
1106 tty.c_cflag |= PARENB | PARODD;
1107 break;
1108 }
1109 if (stop_bits == 2)
1110 tty.c_cflag |= CSTOPB;
1111
1112 tcsetattr (fd, TCSANOW, &tty);
1113}
1114
1115static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1116{
1117 FDCharDriver *s = chr->opaque;
1118
1119 switch(cmd) {
1120 case CHR_IOCTL_SERIAL_SET_PARAMS:
1121 {
1122 QEMUSerialSetParams *ssp = arg;
1123 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1124 ssp->data_bits, ssp->stop_bits);
1125 }
1126 break;
1127 case CHR_IOCTL_SERIAL_SET_BREAK:
1128 {
1129 int enable = *(int *)arg;
1130 if (enable)
1131 tcsendbreak(s->fd_in, 1);
1132 }
1133 break;
1134 case CHR_IOCTL_SERIAL_GET_TIOCM:
1135 {
1136 int sarg = 0;
1137 int *targ = (int *)arg;
1138 ioctl(s->fd_in, TIOCMGET, &sarg);
1139 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001140 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001141 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001142 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001143 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001144 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001145 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001146 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001147 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001148 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001149 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001150 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001151 *targ |= CHR_TIOCM_RTS;
1152 }
1153 break;
1154 case CHR_IOCTL_SERIAL_SET_TIOCM:
1155 {
1156 int sarg = *(int *)arg;
1157 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001158 ioctl(s->fd_in, TIOCMGET, &targ);
1159 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1160 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1161 if (sarg & CHR_TIOCM_CTS)
1162 targ |= TIOCM_CTS;
1163 if (sarg & CHR_TIOCM_CAR)
1164 targ |= TIOCM_CAR;
1165 if (sarg & CHR_TIOCM_DSR)
1166 targ |= TIOCM_DSR;
1167 if (sarg & CHR_TIOCM_RI)
1168 targ |= TIOCM_RI;
1169 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001170 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001171 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001172 targ |= TIOCM_RTS;
1173 ioctl(s->fd_in, TIOCMSET, &targ);
1174 }
1175 break;
1176 default:
1177 return -ENOTSUP;
1178 }
1179 return 0;
1180}
1181
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001182static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001183{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001184 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001185 CharDriverState *chr;
1186 int fd;
1187
1188 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1189 tty_serial_init(fd, 115200, 'N', 8, 1);
1190 chr = qemu_chr_open_fd(fd, fd);
1191 if (!chr) {
1192 close(fd);
1193 return NULL;
1194 }
1195 chr->chr_ioctl = tty_serial_ioctl;
1196 qemu_chr_reset(chr);
1197 return chr;
1198}
1199#else /* ! __linux__ && ! __sun__ */
1200static CharDriverState *qemu_chr_open_pty(void)
1201{
1202 return NULL;
1203}
1204#endif /* __linux__ || __sun__ */
1205
1206#if defined(__linux__)
1207typedef struct {
1208 int fd;
1209 int mode;
1210} ParallelCharDriver;
1211
1212static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1213{
1214 if (s->mode != mode) {
1215 int m = mode;
1216 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1217 return 0;
1218 s->mode = mode;
1219 }
1220 return 1;
1221}
1222
1223static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1224{
1225 ParallelCharDriver *drv = chr->opaque;
1226 int fd = drv->fd;
1227 uint8_t b;
1228
1229 switch(cmd) {
1230 case CHR_IOCTL_PP_READ_DATA:
1231 if (ioctl(fd, PPRDATA, &b) < 0)
1232 return -ENOTSUP;
1233 *(uint8_t *)arg = b;
1234 break;
1235 case CHR_IOCTL_PP_WRITE_DATA:
1236 b = *(uint8_t *)arg;
1237 if (ioctl(fd, PPWDATA, &b) < 0)
1238 return -ENOTSUP;
1239 break;
1240 case CHR_IOCTL_PP_READ_CONTROL:
1241 if (ioctl(fd, PPRCONTROL, &b) < 0)
1242 return -ENOTSUP;
1243 /* Linux gives only the lowest bits, and no way to know data
1244 direction! For better compatibility set the fixed upper
1245 bits. */
1246 *(uint8_t *)arg = b | 0xc0;
1247 break;
1248 case CHR_IOCTL_PP_WRITE_CONTROL:
1249 b = *(uint8_t *)arg;
1250 if (ioctl(fd, PPWCONTROL, &b) < 0)
1251 return -ENOTSUP;
1252 break;
1253 case CHR_IOCTL_PP_READ_STATUS:
1254 if (ioctl(fd, PPRSTATUS, &b) < 0)
1255 return -ENOTSUP;
1256 *(uint8_t *)arg = b;
1257 break;
1258 case CHR_IOCTL_PP_DATA_DIR:
1259 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1260 return -ENOTSUP;
1261 break;
1262 case CHR_IOCTL_PP_EPP_READ_ADDR:
1263 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1264 struct ParallelIOArg *parg = arg;
1265 int n = read(fd, parg->buffer, parg->count);
1266 if (n != parg->count) {
1267 return -EIO;
1268 }
1269 }
1270 break;
1271 case CHR_IOCTL_PP_EPP_READ:
1272 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1273 struct ParallelIOArg *parg = arg;
1274 int n = read(fd, parg->buffer, parg->count);
1275 if (n != parg->count) {
1276 return -EIO;
1277 }
1278 }
1279 break;
1280 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1281 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1282 struct ParallelIOArg *parg = arg;
1283 int n = write(fd, parg->buffer, parg->count);
1284 if (n != parg->count) {
1285 return -EIO;
1286 }
1287 }
1288 break;
1289 case CHR_IOCTL_PP_EPP_WRITE:
1290 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1291 struct ParallelIOArg *parg = arg;
1292 int n = write(fd, parg->buffer, parg->count);
1293 if (n != parg->count) {
1294 return -EIO;
1295 }
1296 }
1297 break;
1298 default:
1299 return -ENOTSUP;
1300 }
1301 return 0;
1302}
1303
1304static void pp_close(CharDriverState *chr)
1305{
1306 ParallelCharDriver *drv = chr->opaque;
1307 int fd = drv->fd;
1308
1309 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1310 ioctl(fd, PPRELEASE);
1311 close(fd);
1312 qemu_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301313 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001314}
1315
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001316static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001317{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001318 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001319 CharDriverState *chr;
1320 ParallelCharDriver *drv;
1321 int fd;
1322
1323 TFR(fd = open(filename, O_RDWR));
1324 if (fd < 0)
1325 return NULL;
1326
1327 if (ioctl(fd, PPCLAIM) < 0) {
1328 close(fd);
1329 return NULL;
1330 }
1331
1332 drv = qemu_mallocz(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001333 drv->fd = fd;
1334 drv->mode = IEEE1284_MODE_COMPAT;
1335
1336 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001337 chr->chr_write = null_chr_write;
1338 chr->chr_ioctl = pp_ioctl;
1339 chr->chr_close = pp_close;
1340 chr->opaque = drv;
1341
1342 qemu_chr_reset(chr);
1343
1344 return chr;
1345}
1346#endif /* __linux__ */
1347
blueswir1c5e97232009-03-07 20:06:23 +00001348#if defined(__FreeBSD__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001349static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1350{
1351 int fd = (int)chr->opaque;
1352 uint8_t b;
1353
1354 switch(cmd) {
1355 case CHR_IOCTL_PP_READ_DATA:
1356 if (ioctl(fd, PPIGDATA, &b) < 0)
1357 return -ENOTSUP;
1358 *(uint8_t *)arg = b;
1359 break;
1360 case CHR_IOCTL_PP_WRITE_DATA:
1361 b = *(uint8_t *)arg;
1362 if (ioctl(fd, PPISDATA, &b) < 0)
1363 return -ENOTSUP;
1364 break;
1365 case CHR_IOCTL_PP_READ_CONTROL:
1366 if (ioctl(fd, PPIGCTRL, &b) < 0)
1367 return -ENOTSUP;
1368 *(uint8_t *)arg = b;
1369 break;
1370 case CHR_IOCTL_PP_WRITE_CONTROL:
1371 b = *(uint8_t *)arg;
1372 if (ioctl(fd, PPISCTRL, &b) < 0)
1373 return -ENOTSUP;
1374 break;
1375 case CHR_IOCTL_PP_READ_STATUS:
1376 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1377 return -ENOTSUP;
1378 *(uint8_t *)arg = b;
1379 break;
1380 default:
1381 return -ENOTSUP;
1382 }
1383 return 0;
1384}
1385
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001386static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001387{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001388 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001389 CharDriverState *chr;
1390 int fd;
1391
1392 fd = open(filename, O_RDWR);
1393 if (fd < 0)
1394 return NULL;
1395
1396 chr = qemu_mallocz(sizeof(CharDriverState));
blueswir16972f932008-11-22 20:49:12 +00001397 chr->opaque = (void *)fd;
1398 chr->chr_write = null_chr_write;
1399 chr->chr_ioctl = pp_ioctl;
1400 return chr;
1401}
1402#endif
1403
aliguori6f97dba2008-10-31 18:49:55 +00001404#else /* _WIN32 */
1405
1406typedef struct {
1407 int max_size;
1408 HANDLE hcom, hrecv, hsend;
1409 OVERLAPPED orecv, osend;
1410 BOOL fpipe;
1411 DWORD len;
1412} WinCharState;
1413
1414#define NSENDBUF 2048
1415#define NRECVBUF 2048
1416#define MAXCONNECT 1
1417#define NTIMEOUT 5000
1418
1419static int win_chr_poll(void *opaque);
1420static int win_chr_pipe_poll(void *opaque);
1421
1422static void win_chr_close(CharDriverState *chr)
1423{
1424 WinCharState *s = chr->opaque;
1425
1426 if (s->hsend) {
1427 CloseHandle(s->hsend);
1428 s->hsend = NULL;
1429 }
1430 if (s->hrecv) {
1431 CloseHandle(s->hrecv);
1432 s->hrecv = NULL;
1433 }
1434 if (s->hcom) {
1435 CloseHandle(s->hcom);
1436 s->hcom = NULL;
1437 }
1438 if (s->fpipe)
1439 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1440 else
1441 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301442
1443 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001444}
1445
1446static int win_chr_init(CharDriverState *chr, const char *filename)
1447{
1448 WinCharState *s = chr->opaque;
1449 COMMCONFIG comcfg;
1450 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1451 COMSTAT comstat;
1452 DWORD size;
1453 DWORD err;
1454
1455 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1456 if (!s->hsend) {
1457 fprintf(stderr, "Failed CreateEvent\n");
1458 goto fail;
1459 }
1460 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1461 if (!s->hrecv) {
1462 fprintf(stderr, "Failed CreateEvent\n");
1463 goto fail;
1464 }
1465
1466 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1467 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1468 if (s->hcom == INVALID_HANDLE_VALUE) {
1469 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1470 s->hcom = NULL;
1471 goto fail;
1472 }
1473
1474 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1475 fprintf(stderr, "Failed SetupComm\n");
1476 goto fail;
1477 }
1478
1479 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1480 size = sizeof(COMMCONFIG);
1481 GetDefaultCommConfig(filename, &comcfg, &size);
1482 comcfg.dcb.DCBlength = sizeof(DCB);
1483 CommConfigDialog(filename, NULL, &comcfg);
1484
1485 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1486 fprintf(stderr, "Failed SetCommState\n");
1487 goto fail;
1488 }
1489
1490 if (!SetCommMask(s->hcom, EV_ERR)) {
1491 fprintf(stderr, "Failed SetCommMask\n");
1492 goto fail;
1493 }
1494
1495 cto.ReadIntervalTimeout = MAXDWORD;
1496 if (!SetCommTimeouts(s->hcom, &cto)) {
1497 fprintf(stderr, "Failed SetCommTimeouts\n");
1498 goto fail;
1499 }
1500
1501 if (!ClearCommError(s->hcom, &err, &comstat)) {
1502 fprintf(stderr, "Failed ClearCommError\n");
1503 goto fail;
1504 }
1505 qemu_add_polling_cb(win_chr_poll, chr);
1506 return 0;
1507
1508 fail:
1509 win_chr_close(chr);
1510 return -1;
1511}
1512
1513static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1514{
1515 WinCharState *s = chr->opaque;
1516 DWORD len, ret, size, err;
1517
1518 len = len1;
1519 ZeroMemory(&s->osend, sizeof(s->osend));
1520 s->osend.hEvent = s->hsend;
1521 while (len > 0) {
1522 if (s->hsend)
1523 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1524 else
1525 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1526 if (!ret) {
1527 err = GetLastError();
1528 if (err == ERROR_IO_PENDING) {
1529 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1530 if (ret) {
1531 buf += size;
1532 len -= size;
1533 } else {
1534 break;
1535 }
1536 } else {
1537 break;
1538 }
1539 } else {
1540 buf += size;
1541 len -= size;
1542 }
1543 }
1544 return len1 - len;
1545}
1546
1547static int win_chr_read_poll(CharDriverState *chr)
1548{
1549 WinCharState *s = chr->opaque;
1550
1551 s->max_size = qemu_chr_can_read(chr);
1552 return s->max_size;
1553}
1554
1555static void win_chr_readfile(CharDriverState *chr)
1556{
1557 WinCharState *s = chr->opaque;
1558 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301559 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001560 DWORD size;
1561
1562 ZeroMemory(&s->orecv, sizeof(s->orecv));
1563 s->orecv.hEvent = s->hrecv;
1564 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1565 if (!ret) {
1566 err = GetLastError();
1567 if (err == ERROR_IO_PENDING) {
1568 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1569 }
1570 }
1571
1572 if (size > 0) {
1573 qemu_chr_read(chr, buf, size);
1574 }
1575}
1576
1577static void win_chr_read(CharDriverState *chr)
1578{
1579 WinCharState *s = chr->opaque;
1580
1581 if (s->len > s->max_size)
1582 s->len = s->max_size;
1583 if (s->len == 0)
1584 return;
1585
1586 win_chr_readfile(chr);
1587}
1588
1589static int win_chr_poll(void *opaque)
1590{
1591 CharDriverState *chr = opaque;
1592 WinCharState *s = chr->opaque;
1593 COMSTAT status;
1594 DWORD comerr;
1595
1596 ClearCommError(s->hcom, &comerr, &status);
1597 if (status.cbInQue > 0) {
1598 s->len = status.cbInQue;
1599 win_chr_read_poll(chr);
1600 win_chr_read(chr);
1601 return 1;
1602 }
1603 return 0;
1604}
1605
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001606static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001607{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001608 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001609 CharDriverState *chr;
1610 WinCharState *s;
1611
1612 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001613 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001614 chr->opaque = s;
1615 chr->chr_write = win_chr_write;
1616 chr->chr_close = win_chr_close;
1617
1618 if (win_chr_init(chr, filename) < 0) {
1619 free(s);
1620 free(chr);
1621 return NULL;
1622 }
1623 qemu_chr_reset(chr);
1624 return chr;
1625}
1626
1627static int win_chr_pipe_poll(void *opaque)
1628{
1629 CharDriverState *chr = opaque;
1630 WinCharState *s = chr->opaque;
1631 DWORD size;
1632
1633 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1634 if (size > 0) {
1635 s->len = size;
1636 win_chr_read_poll(chr);
1637 win_chr_read(chr);
1638 return 1;
1639 }
1640 return 0;
1641}
1642
1643static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1644{
1645 WinCharState *s = chr->opaque;
1646 OVERLAPPED ov;
1647 int ret;
1648 DWORD size;
1649 char openname[256];
1650
1651 s->fpipe = TRUE;
1652
1653 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1654 if (!s->hsend) {
1655 fprintf(stderr, "Failed CreateEvent\n");
1656 goto fail;
1657 }
1658 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1659 if (!s->hrecv) {
1660 fprintf(stderr, "Failed CreateEvent\n");
1661 goto fail;
1662 }
1663
1664 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1665 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1666 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1667 PIPE_WAIT,
1668 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1669 if (s->hcom == INVALID_HANDLE_VALUE) {
1670 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1671 s->hcom = NULL;
1672 goto fail;
1673 }
1674
1675 ZeroMemory(&ov, sizeof(ov));
1676 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1677 ret = ConnectNamedPipe(s->hcom, &ov);
1678 if (ret) {
1679 fprintf(stderr, "Failed ConnectNamedPipe\n");
1680 goto fail;
1681 }
1682
1683 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1684 if (!ret) {
1685 fprintf(stderr, "Failed GetOverlappedResult\n");
1686 if (ov.hEvent) {
1687 CloseHandle(ov.hEvent);
1688 ov.hEvent = NULL;
1689 }
1690 goto fail;
1691 }
1692
1693 if (ov.hEvent) {
1694 CloseHandle(ov.hEvent);
1695 ov.hEvent = NULL;
1696 }
1697 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1698 return 0;
1699
1700 fail:
1701 win_chr_close(chr);
1702 return -1;
1703}
1704
1705
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001706static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001707{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001708 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001709 CharDriverState *chr;
1710 WinCharState *s;
1711
1712 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001713 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001714 chr->opaque = s;
1715 chr->chr_write = win_chr_write;
1716 chr->chr_close = win_chr_close;
1717
1718 if (win_chr_pipe_init(chr, filename) < 0) {
1719 free(s);
1720 free(chr);
1721 return NULL;
1722 }
1723 qemu_chr_reset(chr);
1724 return chr;
1725}
1726
1727static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1728{
1729 CharDriverState *chr;
1730 WinCharState *s;
1731
1732 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001733 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001734 s->hcom = fd_out;
1735 chr->opaque = s;
1736 chr->chr_write = win_chr_write;
1737 qemu_chr_reset(chr);
1738 return chr;
1739}
1740
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02001741static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001742{
1743 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1744}
1745
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001746static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001747{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001748 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001749 HANDLE fd_out;
1750
1751 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1752 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1753 if (fd_out == INVALID_HANDLE_VALUE)
1754 return NULL;
1755
1756 return qemu_chr_open_win_file(fd_out);
1757}
1758#endif /* !_WIN32 */
1759
1760/***********************************************************/
1761/* UDP Net console */
1762
1763typedef struct {
1764 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05301765 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001766 int bufcnt;
1767 int bufptr;
1768 int max_size;
1769} NetCharDriver;
1770
1771static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1772{
1773 NetCharDriver *s = chr->opaque;
1774
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001775 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00001776}
1777
1778static int udp_chr_read_poll(void *opaque)
1779{
1780 CharDriverState *chr = opaque;
1781 NetCharDriver *s = chr->opaque;
1782
1783 s->max_size = qemu_chr_can_read(chr);
1784
1785 /* If there were any stray characters in the queue process them
1786 * first
1787 */
1788 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1789 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1790 s->bufptr++;
1791 s->max_size = qemu_chr_can_read(chr);
1792 }
1793 return s->max_size;
1794}
1795
1796static void udp_chr_read(void *opaque)
1797{
1798 CharDriverState *chr = opaque;
1799 NetCharDriver *s = chr->opaque;
1800
1801 if (s->max_size == 0)
1802 return;
Blue Swirlc5b76b32009-06-13 08:44:31 +00001803 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00001804 s->bufptr = s->bufcnt;
1805 if (s->bufcnt <= 0)
1806 return;
1807
1808 s->bufptr = 0;
1809 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1810 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1811 s->bufptr++;
1812 s->max_size = qemu_chr_can_read(chr);
1813 }
1814}
1815
1816static void udp_chr_update_read_handler(CharDriverState *chr)
1817{
1818 NetCharDriver *s = chr->opaque;
1819
1820 if (s->fd >= 0) {
1821 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1822 udp_chr_read, NULL, chr);
1823 }
1824}
1825
aliguori819f56b2009-03-28 17:58:14 +00001826static void udp_chr_close(CharDriverState *chr)
1827{
1828 NetCharDriver *s = chr->opaque;
1829 if (s->fd >= 0) {
1830 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1831 closesocket(s->fd);
1832 }
1833 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05301834 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00001835}
1836
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001837static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001838{
1839 CharDriverState *chr = NULL;
1840 NetCharDriver *s = NULL;
1841 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00001842
1843 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001844 s = qemu_mallocz(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001845
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001846 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00001847 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001848 fprintf(stderr, "inet_dgram_opts failed\n");
aliguori6f97dba2008-10-31 18:49:55 +00001849 goto return_err;
1850 }
1851
1852 s->fd = fd;
1853 s->bufcnt = 0;
1854 s->bufptr = 0;
1855 chr->opaque = s;
1856 chr->chr_write = udp_chr_write;
1857 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00001858 chr->chr_close = udp_chr_close;
aliguori6f97dba2008-10-31 18:49:55 +00001859 return chr;
1860
1861return_err:
1862 if (chr)
1863 free(chr);
1864 if (s)
1865 free(s);
1866 if (fd >= 0)
1867 closesocket(fd);
1868 return NULL;
1869}
1870
1871/***********************************************************/
1872/* TCP Net console */
1873
1874typedef struct {
1875 int fd, listen_fd;
1876 int connected;
1877 int max_size;
1878 int do_telnetopt;
1879 int do_nodelay;
1880 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001881 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00001882} TCPCharDriver;
1883
1884static void tcp_chr_accept(void *opaque);
1885
1886static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1887{
1888 TCPCharDriver *s = chr->opaque;
1889 if (s->connected) {
1890 return send_all(s->fd, buf, len);
1891 } else {
1892 /* XXX: indicate an error ? */
1893 return len;
1894 }
1895}
1896
1897static int tcp_chr_read_poll(void *opaque)
1898{
1899 CharDriverState *chr = opaque;
1900 TCPCharDriver *s = chr->opaque;
1901 if (!s->connected)
1902 return 0;
1903 s->max_size = qemu_chr_can_read(chr);
1904 return s->max_size;
1905}
1906
1907#define IAC 255
1908#define IAC_BREAK 243
1909static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1910 TCPCharDriver *s,
1911 uint8_t *buf, int *size)
1912{
1913 /* Handle any telnet client's basic IAC options to satisfy char by
1914 * char mode with no echo. All IAC options will be removed from
1915 * the buf and the do_telnetopt variable will be used to track the
1916 * state of the width of the IAC information.
1917 *
1918 * IAC commands come in sets of 3 bytes with the exception of the
1919 * "IAC BREAK" command and the double IAC.
1920 */
1921
1922 int i;
1923 int j = 0;
1924
1925 for (i = 0; i < *size; i++) {
1926 if (s->do_telnetopt > 1) {
1927 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1928 /* Double IAC means send an IAC */
1929 if (j != i)
1930 buf[j] = buf[i];
1931 j++;
1932 s->do_telnetopt = 1;
1933 } else {
1934 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1935 /* Handle IAC break commands by sending a serial break */
1936 qemu_chr_event(chr, CHR_EVENT_BREAK);
1937 s->do_telnetopt++;
1938 }
1939 s->do_telnetopt++;
1940 }
1941 if (s->do_telnetopt >= 4) {
1942 s->do_telnetopt = 1;
1943 }
1944 } else {
1945 if ((unsigned char)buf[i] == IAC) {
1946 s->do_telnetopt = 2;
1947 } else {
1948 if (j != i)
1949 buf[j] = buf[i];
1950 j++;
1951 }
1952 }
1953 }
1954 *size = j;
1955}
1956
Mark McLoughlin7d174052009-07-22 09:11:39 +01001957static int tcp_get_msgfd(CharDriverState *chr)
1958{
1959 TCPCharDriver *s = chr->opaque;
1960
1961 return s->msgfd;
1962}
1963
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05001964#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01001965static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1966{
1967 TCPCharDriver *s = chr->opaque;
1968 struct cmsghdr *cmsg;
1969
1970 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1971 int fd;
1972
1973 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1974 cmsg->cmsg_level != SOL_SOCKET ||
1975 cmsg->cmsg_type != SCM_RIGHTS)
1976 continue;
1977
1978 fd = *((int *)CMSG_DATA(cmsg));
1979 if (fd < 0)
1980 continue;
1981
1982 if (s->msgfd != -1)
1983 close(s->msgfd);
1984 s->msgfd = fd;
1985 }
1986}
1987
Mark McLoughlin9977c892009-07-22 09:11:38 +01001988static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1989{
1990 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00001991 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01001992 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01001993 union {
1994 struct cmsghdr cmsg;
1995 char control[CMSG_SPACE(sizeof(int))];
1996 } msg_control;
1997 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01001998
1999 iov[0].iov_base = buf;
2000 iov[0].iov_len = len;
2001
2002 msg.msg_iov = iov;
2003 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002004 msg.msg_control = &msg_control;
2005 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002006
Mark McLoughlin7d174052009-07-22 09:11:39 +01002007 ret = recvmsg(s->fd, &msg, 0);
2008 if (ret > 0 && s->is_unix)
2009 unix_process_msgfd(chr, &msg);
2010
2011 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002012}
2013#else
2014static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2015{
2016 TCPCharDriver *s = chr->opaque;
2017 return recv(s->fd, buf, len, 0);
2018}
2019#endif
2020
aliguori6f97dba2008-10-31 18:49:55 +00002021static void tcp_chr_read(void *opaque)
2022{
2023 CharDriverState *chr = opaque;
2024 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302025 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002026 int len, size;
2027
2028 if (!s->connected || s->max_size <= 0)
2029 return;
2030 len = sizeof(buf);
2031 if (len > s->max_size)
2032 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002033 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002034 if (size == 0) {
2035 /* connection closed */
2036 s->connected = 0;
2037 if (s->listen_fd >= 0) {
2038 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2039 }
2040 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2041 closesocket(s->fd);
2042 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302043 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002044 } else if (size > 0) {
2045 if (s->do_telnetopt)
2046 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2047 if (size > 0)
2048 qemu_chr_read(chr, buf, size);
Mark McLoughlin7d174052009-07-22 09:11:39 +01002049 if (s->msgfd != -1) {
2050 close(s->msgfd);
2051 s->msgfd = -1;
2052 }
aliguori6f97dba2008-10-31 18:49:55 +00002053 }
2054}
2055
2056static void tcp_chr_connect(void *opaque)
2057{
2058 CharDriverState *chr = opaque;
2059 TCPCharDriver *s = chr->opaque;
2060
2061 s->connected = 1;
2062 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2063 tcp_chr_read, NULL, chr);
2064 qemu_chr_reset(chr);
2065}
2066
2067#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2068static void tcp_chr_telnet_init(int fd)
2069{
2070 char buf[3];
2071 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2072 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2073 send(fd, (char *)buf, 3, 0);
2074 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2075 send(fd, (char *)buf, 3, 0);
2076 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2077 send(fd, (char *)buf, 3, 0);
2078 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2079 send(fd, (char *)buf, 3, 0);
2080}
2081
2082static void socket_set_nodelay(int fd)
2083{
2084 int val = 1;
2085 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2086}
2087
2088static void tcp_chr_accept(void *opaque)
2089{
2090 CharDriverState *chr = opaque;
2091 TCPCharDriver *s = chr->opaque;
2092 struct sockaddr_in saddr;
2093#ifndef _WIN32
2094 struct sockaddr_un uaddr;
2095#endif
2096 struct sockaddr *addr;
2097 socklen_t len;
2098 int fd;
2099
2100 for(;;) {
2101#ifndef _WIN32
2102 if (s->is_unix) {
2103 len = sizeof(uaddr);
2104 addr = (struct sockaddr *)&uaddr;
2105 } else
2106#endif
2107 {
2108 len = sizeof(saddr);
2109 addr = (struct sockaddr *)&saddr;
2110 }
2111 fd = accept(s->listen_fd, addr, &len);
2112 if (fd < 0 && errno != EINTR) {
2113 return;
2114 } else if (fd >= 0) {
2115 if (s->do_telnetopt)
2116 tcp_chr_telnet_init(fd);
2117 break;
2118 }
2119 }
2120 socket_set_nonblock(fd);
2121 if (s->do_nodelay)
2122 socket_set_nodelay(fd);
2123 s->fd = fd;
2124 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2125 tcp_chr_connect(chr);
2126}
2127
2128static void tcp_chr_close(CharDriverState *chr)
2129{
2130 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002131 if (s->fd >= 0) {
2132 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002133 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002134 }
2135 if (s->listen_fd >= 0) {
2136 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002137 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002138 }
aliguori6f97dba2008-10-31 18:49:55 +00002139 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302140 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002141}
2142
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002143static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002144{
2145 CharDriverState *chr = NULL;
2146 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002147 int fd = -1;
2148 int is_listen;
2149 int is_waitconnect;
2150 int do_nodelay;
2151 int is_unix;
2152 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002153
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002154 is_listen = qemu_opt_get_bool(opts, "server", 0);
2155 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2156 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2157 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2158 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002159 if (!is_listen)
2160 is_waitconnect = 0;
2161
2162 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00002163 s = qemu_mallocz(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002164
aliguorif07b6002008-11-11 20:54:09 +00002165 if (is_unix) {
2166 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002167 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002168 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002169 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002170 }
2171 } else {
2172 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002173 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002174 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002175 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002176 }
2177 }
aliguori6f97dba2008-10-31 18:49:55 +00002178 if (fd < 0)
2179 goto fail;
2180
2181 if (!is_waitconnect)
2182 socket_set_nonblock(fd);
2183
2184 s->connected = 0;
2185 s->fd = -1;
2186 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002187 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002188 s->is_unix = is_unix;
2189 s->do_nodelay = do_nodelay && !is_unix;
2190
2191 chr->opaque = s;
2192 chr->chr_write = tcp_chr_write;
2193 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002194 chr->get_msgfd = tcp_get_msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002195
2196 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002197 s->listen_fd = fd;
2198 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2199 if (is_telnet)
2200 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002201
aliguori6f97dba2008-10-31 18:49:55 +00002202 } else {
aliguorif07b6002008-11-11 20:54:09 +00002203 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002204 s->fd = fd;
2205 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002206 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002207 }
2208
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002209 /* for "info chardev" monitor command */
2210 chr->filename = qemu_malloc(256);
2211 if (is_unix) {
2212 snprintf(chr->filename, 256, "unix:%s%s",
2213 qemu_opt_get(opts, "path"),
2214 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2215 } else if (is_telnet) {
2216 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2217 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2218 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2219 } else {
2220 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2221 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2222 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2223 }
2224
aliguori6f97dba2008-10-31 18:49:55 +00002225 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002226 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002227 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002228 tcp_chr_accept(chr);
2229 socket_set_nonblock(s->listen_fd);
2230 }
aliguori6f97dba2008-10-31 18:49:55 +00002231 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002232
aliguori6f97dba2008-10-31 18:49:55 +00002233 fail:
2234 if (fd >= 0)
2235 closesocket(fd);
2236 qemu_free(s);
2237 qemu_free(chr);
2238 return NULL;
2239}
2240
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002241static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2242{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002243 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002244 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002245 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002246 QemuOpts *opts;
2247
2248 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2249 if (NULL == opts)
2250 return NULL;
2251
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002252 if (strstart(filename, "mon:", &p)) {
2253 filename = p;
2254 qemu_opt_set(opts, "mux", "on");
2255 }
2256
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002257 if (strcmp(filename, "null") == 0 ||
2258 strcmp(filename, "pty") == 0 ||
2259 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002260 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002261 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002262 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002263 return opts;
2264 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002265 if (strstart(filename, "vc", &p)) {
2266 qemu_opt_set(opts, "backend", "vc");
2267 if (*p == ':') {
2268 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2269 /* pixels */
2270 qemu_opt_set(opts, "width", width);
2271 qemu_opt_set(opts, "height", height);
2272 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2273 /* chars */
2274 qemu_opt_set(opts, "cols", width);
2275 qemu_opt_set(opts, "rows", height);
2276 } else {
2277 goto fail;
2278 }
2279 }
2280 return opts;
2281 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002282 if (strcmp(filename, "con:") == 0) {
2283 qemu_opt_set(opts, "backend", "console");
2284 return opts;
2285 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002286 if (strstart(filename, "COM", NULL)) {
2287 qemu_opt_set(opts, "backend", "serial");
2288 qemu_opt_set(opts, "path", filename);
2289 return opts;
2290 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002291 if (strstart(filename, "file:", &p)) {
2292 qemu_opt_set(opts, "backend", "file");
2293 qemu_opt_set(opts, "path", p);
2294 return opts;
2295 }
2296 if (strstart(filename, "pipe:", &p)) {
2297 qemu_opt_set(opts, "backend", "pipe");
2298 qemu_opt_set(opts, "path", p);
2299 return opts;
2300 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002301 if (strstart(filename, "tcp:", &p) ||
2302 strstart(filename, "telnet:", &p)) {
2303 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2304 host[0] = 0;
2305 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2306 goto fail;
2307 }
2308 qemu_opt_set(opts, "backend", "socket");
2309 qemu_opt_set(opts, "host", host);
2310 qemu_opt_set(opts, "port", port);
2311 if (p[pos] == ',') {
2312 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2313 goto fail;
2314 }
2315 if (strstart(filename, "telnet:", &p))
2316 qemu_opt_set(opts, "telnet", "on");
2317 return opts;
2318 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002319 if (strstart(filename, "udp:", &p)) {
2320 qemu_opt_set(opts, "backend", "udp");
2321 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2322 host[0] = 0;
2323 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2324 fprintf(stderr, "udp #1\n");
2325 goto fail;
2326 }
2327 }
2328 qemu_opt_set(opts, "host", host);
2329 qemu_opt_set(opts, "port", port);
2330 if (p[pos] == '@') {
2331 p += pos + 1;
2332 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2333 host[0] = 0;
2334 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2335 fprintf(stderr, "udp #2\n");
2336 goto fail;
2337 }
2338 }
2339 qemu_opt_set(opts, "localaddr", host);
2340 qemu_opt_set(opts, "localport", port);
2341 }
2342 return opts;
2343 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002344 if (strstart(filename, "unix:", &p)) {
2345 qemu_opt_set(opts, "backend", "socket");
2346 if (qemu_opts_do_parse(opts, p, "path") != 0)
2347 goto fail;
2348 return opts;
2349 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002350 if (strstart(filename, "/dev/parport", NULL) ||
2351 strstart(filename, "/dev/ppi", NULL)) {
2352 qemu_opt_set(opts, "backend", "parport");
2353 qemu_opt_set(opts, "path", filename);
2354 return opts;
2355 }
2356 if (strstart(filename, "/dev/", NULL)) {
2357 qemu_opt_set(opts, "backend", "tty");
2358 qemu_opt_set(opts, "path", filename);
2359 return opts;
2360 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002361
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002362fail:
2363 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002364 qemu_opts_del(opts);
2365 return NULL;
2366}
2367
2368static const struct {
2369 const char *name;
2370 CharDriverState *(*open)(QemuOpts *opts);
2371} backend_table[] = {
2372 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002373 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002374 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002375 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002376 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002377#ifdef _WIN32
2378 { .name = "file", .open = qemu_chr_open_win_file_out },
2379 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002380 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002381 { .name = "serial", .open = qemu_chr_open_win },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002382#else
2383 { .name = "file", .open = qemu_chr_open_file_out },
2384 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002385 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002386 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002387#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002388#ifdef CONFIG_BRLAPI
2389 { .name = "braille", .open = chr_baum_init },
2390#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002391#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2392 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2393 { .name = "tty", .open = qemu_chr_open_tty },
2394#endif
2395#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2396 { .name = "parport", .open = qemu_chr_open_pp },
2397#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002398};
2399
2400CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2401 void (*init)(struct CharDriverState *s))
2402{
2403 CharDriverState *chr;
2404 int i;
2405
2406 if (qemu_opts_id(opts) == NULL) {
2407 fprintf(stderr, "chardev: no id specified\n");
2408 return NULL;
2409 }
2410
2411 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2412 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2413 break;
2414 }
2415 if (i == ARRAY_SIZE(backend_table)) {
2416 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2417 qemu_opt_get(opts, "backend"));
2418 return NULL;
2419 }
2420
2421 chr = backend_table[i].open(opts);
2422 if (!chr) {
2423 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2424 qemu_opt_get(opts, "backend"));
2425 return NULL;
2426 }
2427
2428 if (!chr->filename)
2429 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2430 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002431 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002432
2433 if (qemu_opt_get_bool(opts, "mux", 0)) {
2434 CharDriverState *base = chr;
2435 int len = strlen(qemu_opts_id(opts)) + 6;
2436 base->label = qemu_malloc(len);
2437 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2438 chr = qemu_chr_open_mux(base);
2439 chr->filename = base->filename;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002440 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002441 }
2442 chr->label = qemu_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002443 return chr;
2444}
2445
aurel32ceecf1d2009-01-18 14:08:04 +00002446CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002447{
2448 const char *p;
2449 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002450 QemuOpts *opts;
2451
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002452 if (strstart(filename, "chardev:", &p)) {
2453 return qemu_chr_find(p);
2454 }
2455
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002456 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002457 if (!opts)
2458 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002459
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002460 chr = qemu_chr_open_opts(opts, init);
2461 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2462 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002463 }
2464 return chr;
2465}
2466
2467void qemu_chr_close(CharDriverState *chr)
2468{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002469 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002470 if (chr->chr_close)
2471 chr->chr_close(chr);
2472 qemu_free(chr->filename);
2473 qemu_free(chr->label);
2474 qemu_free(chr);
2475}
2476
aliguori376253e2009-03-05 23:01:23 +00002477void qemu_chr_info(Monitor *mon)
aliguori6f97dba2008-10-31 18:49:55 +00002478{
2479 CharDriverState *chr;
2480
Blue Swirl72cf2d42009-09-12 07:36:22 +00002481 QTAILQ_FOREACH(chr, &chardevs, next) {
aliguori376253e2009-03-05 23:01:23 +00002482 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002483 }
2484}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002485
2486CharDriverState *qemu_chr_find(const char *name)
2487{
2488 CharDriverState *chr;
2489
Blue Swirl72cf2d42009-09-12 07:36:22 +00002490 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002491 if (strcmp(chr->label, name) != 0)
2492 continue;
2493 return chr;
2494 }
2495 return NULL;
2496}