blob: 86c7c5a3f5de98ea86fd80692f935f40b9bcc5e4 [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"
Luiz Capitulino588b3832009-12-10 17:16:08 -020035#include "qemu-objects.h"
aliguori6f97dba2008-10-31 18:49:55 +000036
37#include <unistd.h>
38#include <fcntl.h>
39#include <signal.h>
40#include <time.h>
41#include <errno.h>
42#include <sys/time.h>
43#include <zlib.h>
44
45#ifndef _WIN32
46#include <sys/times.h>
47#include <sys/wait.h>
48#include <termios.h>
49#include <sys/mman.h>
50#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000051#include <sys/resource.h>
aliguori6f97dba2008-10-31 18:49:55 +000052#include <sys/socket.h>
53#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000054#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000055#include <arpa/inet.h>
aliguori6f97dba2008-10-31 18:49:55 +000056#include <dirent.h>
57#include <netdb.h>
58#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020059#ifdef CONFIG_BSD
aliguori6f97dba2008-10-31 18:49:55 +000060#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010061#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
aliguori6f97dba2008-10-31 18:49:55 +000062#include <libutil.h>
blueswir16972f932008-11-22 20:49:12 +000063#include <dev/ppbus/ppi.h>
64#include <dev/ppbus/ppbconf.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010065#if defined(__GLIBC__)
66#include <pty.h>
67#endif
blueswir1c5e97232009-03-07 20:06:23 +000068#elif defined(__DragonFly__)
69#include <libutil.h>
70#include <dev/misc/ppi/ppi.h>
71#include <bus/ppbus/ppbconf.h>
blueswir124646c72008-11-07 16:55:48 +000072#else
73#include <util.h>
aliguori6f97dba2008-10-31 18:49:55 +000074#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010075#else
aliguori6f97dba2008-10-31 18:49:55 +000076#ifdef __linux__
aliguori6f97dba2008-10-31 18:49:55 +000077#include <pty.h>
78
79#include <linux/ppdev.h>
80#include <linux/parport.h>
81#endif
82#ifdef __sun__
83#include <sys/stat.h>
84#include <sys/ethernet.h>
85#include <sys/sockio.h>
86#include <netinet/arp.h>
87#include <netinet/in.h>
88#include <netinet/in_systm.h>
89#include <netinet/ip.h>
90#include <netinet/ip_icmp.h> // must come after ip.h
91#include <netinet/udp.h>
92#include <netinet/tcp.h>
93#include <net/if.h>
94#include <syslog.h>
95#include <stropts.h>
96#endif
97#endif
98#endif
99
100#include "qemu_socket.h"
101
Amit Shah9bd78542009-11-03 19:59:54 +0530102#define READ_BUF_LEN 4096
103
aliguori6f97dba2008-10-31 18:49:55 +0000104/***********************************************************/
105/* character device */
106
Blue Swirl72cf2d42009-09-12 07:36:22 +0000107static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
108 QTAILQ_HEAD_INITIALIZER(chardevs);
aliguori2970a6c2009-03-05 22:59:58 +0000109
aliguori6f97dba2008-10-31 18:49:55 +0000110static void qemu_chr_event(CharDriverState *s, int event)
111{
112 if (!s->chr_event)
113 return;
114 s->chr_event(s->handler_opaque, event);
115}
116
Amit Shah127338e2009-11-03 19:59:56 +0530117static void qemu_chr_generic_open_bh(void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +0000118{
119 CharDriverState *s = opaque;
Anthony Liguorif7cbc082009-10-27 10:14:50 -0500120 qemu_chr_event(s, CHR_EVENT_OPENED);
aliguori6f97dba2008-10-31 18:49:55 +0000121 qemu_bh_delete(s->bh);
122 s->bh = NULL;
123}
124
Amit Shah127338e2009-11-03 19:59:56 +0530125void qemu_chr_generic_open(CharDriverState *s)
aliguori6f97dba2008-10-31 18:49:55 +0000126{
Amit Shah69795d62009-10-07 18:31:15 +0530127 if (s->bh == NULL) {
Amit Shah127338e2009-11-03 19:59:56 +0530128 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
aliguori6f97dba2008-10-31 18:49:55 +0000129 qemu_bh_schedule(s->bh);
130 }
131}
132
133int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
134{
135 return s->chr_write(s, buf, len);
136}
137
138int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
139{
140 if (!s->chr_ioctl)
141 return -ENOTSUP;
142 return s->chr_ioctl(s, cmd, arg);
143}
144
145int qemu_chr_can_read(CharDriverState *s)
146{
147 if (!s->chr_can_read)
148 return 0;
149 return s->chr_can_read(s->handler_opaque);
150}
151
152void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
153{
154 s->chr_read(s->handler_opaque, buf, len);
155}
156
Mark McLoughlin7d174052009-07-22 09:11:39 +0100157int qemu_chr_get_msgfd(CharDriverState *s)
158{
159 return s->get_msgfd ? s->get_msgfd(s) : -1;
160}
161
aliguori6f97dba2008-10-31 18:49:55 +0000162void qemu_chr_accept_input(CharDriverState *s)
163{
164 if (s->chr_accept_input)
165 s->chr_accept_input(s);
166}
167
168void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
169{
Amit Shah9bd78542009-11-03 19:59:54 +0530170 char buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000171 va_list ap;
172 va_start(ap, fmt);
173 vsnprintf(buf, sizeof(buf), fmt, ap);
174 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
175 va_end(ap);
176}
177
178void qemu_chr_send_event(CharDriverState *s, int event)
179{
180 if (s->chr_send_event)
181 s->chr_send_event(s, event);
182}
183
184void qemu_chr_add_handlers(CharDriverState *s,
185 IOCanRWHandler *fd_can_read,
186 IOReadHandler *fd_read,
187 IOEventHandler *fd_event,
188 void *opaque)
189{
190 s->chr_can_read = fd_can_read;
191 s->chr_read = fd_read;
192 s->chr_event = fd_event;
193 s->handler_opaque = opaque;
194 if (s->chr_update_read_handler)
195 s->chr_update_read_handler(s);
196}
197
198static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
199{
200 return len;
201}
202
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200203static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000204{
205 CharDriverState *chr;
206
207 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000208 chr->chr_write = null_chr_write;
209 return chr;
210}
211
212/* MUX driver for serial I/O splitting */
aliguori6f97dba2008-10-31 18:49:55 +0000213#define MAX_MUX 4
214#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
215#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
216typedef struct {
217 IOCanRWHandler *chr_can_read[MAX_MUX];
218 IOReadHandler *chr_read[MAX_MUX];
219 IOEventHandler *chr_event[MAX_MUX];
220 void *ext_opaque[MAX_MUX];
221 CharDriverState *drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200222 int focus;
aliguori6f97dba2008-10-31 18:49:55 +0000223 int mux_cnt;
224 int term_got_escape;
225 int max_size;
aliguoria80bf992009-03-05 23:00:02 +0000226 /* Intermediate input buffer allows to catch escape sequences even if the
227 currently active device is not accepting any input - but only until it
228 is full as well. */
229 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
230 int prod[MAX_MUX];
231 int cons[MAX_MUX];
Jan Kiszka2d229592009-06-15 22:25:30 +0200232 int timestamps;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200233 int linestart;
Jan Kiszka2d229592009-06-15 22:25:30 +0200234 int64_t timestamps_start;
aliguori6f97dba2008-10-31 18:49:55 +0000235} MuxDriver;
236
237
238static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
239{
240 MuxDriver *d = chr->opaque;
241 int ret;
Jan Kiszka2d229592009-06-15 22:25:30 +0200242 if (!d->timestamps) {
aliguori6f97dba2008-10-31 18:49:55 +0000243 ret = d->drv->chr_write(d->drv, buf, len);
244 } else {
245 int i;
246
247 ret = 0;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200248 for (i = 0; i < len; i++) {
249 if (d->linestart) {
aliguori6f97dba2008-10-31 18:49:55 +0000250 char buf1[64];
251 int64_t ti;
252 int secs;
253
254 ti = qemu_get_clock(rt_clock);
Jan Kiszka2d229592009-06-15 22:25:30 +0200255 if (d->timestamps_start == -1)
256 d->timestamps_start = ti;
257 ti -= d->timestamps_start;
aliguoria4bb1db2009-01-22 17:15:16 +0000258 secs = ti / 1000;
aliguori6f97dba2008-10-31 18:49:55 +0000259 snprintf(buf1, sizeof(buf1),
260 "[%02d:%02d:%02d.%03d] ",
261 secs / 3600,
262 (secs / 60) % 60,
263 secs % 60,
aliguoria4bb1db2009-01-22 17:15:16 +0000264 (int)(ti % 1000));
aliguori6f97dba2008-10-31 18:49:55 +0000265 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200266 d->linestart = 0;
267 }
268 ret += d->drv->chr_write(d->drv, buf+i, 1);
269 if (buf[i] == '\n') {
270 d->linestart = 1;
aliguori6f97dba2008-10-31 18:49:55 +0000271 }
272 }
273 }
274 return ret;
275}
276
277static const char * const mux_help[] = {
278 "% h print this help\n\r",
279 "% x exit emulator\n\r",
280 "% s save disk data back to file (if -snapshot)\n\r",
281 "% t toggle console timestamps\n\r"
282 "% b send break (magic sysrq)\n\r",
283 "% c switch between console and monitor\n\r",
284 "% % sends %\n\r",
285 NULL
286};
287
288int term_escape_char = 0x01; /* ctrl-a is used for escape */
289static void mux_print_help(CharDriverState *chr)
290{
291 int i, j;
292 char ebuf[15] = "Escape-Char";
293 char cbuf[50] = "\n\r";
294
295 if (term_escape_char > 0 && term_escape_char < 26) {
296 snprintf(cbuf, sizeof(cbuf), "\n\r");
297 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
298 } else {
299 snprintf(cbuf, sizeof(cbuf),
300 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
301 term_escape_char);
302 }
303 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
304 for (i = 0; mux_help[i] != NULL; i++) {
305 for (j=0; mux_help[i][j] != '\0'; j++) {
306 if (mux_help[i][j] == '%')
307 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
308 else
309 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
310 }
311 }
312}
313
aliguori2724b182009-03-05 23:01:47 +0000314static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
315{
316 if (d->chr_event[mux_nr])
317 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
318}
319
aliguori6f97dba2008-10-31 18:49:55 +0000320static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
321{
322 if (d->term_got_escape) {
323 d->term_got_escape = 0;
324 if (ch == term_escape_char)
325 goto send_char;
326 switch(ch) {
327 case '?':
328 case 'h':
329 mux_print_help(chr);
330 break;
331 case 'x':
332 {
333 const char *term = "QEMU: Terminated\n\r";
334 chr->chr_write(chr,(uint8_t *)term,strlen(term));
335 exit(0);
336 break;
337 }
338 case 's':
339 {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200340 DriveInfo *dinfo;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000341 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200342 bdrv_commit(dinfo->bdrv);
aliguori6f97dba2008-10-31 18:49:55 +0000343 }
344 }
345 break;
346 case 'b':
347 qemu_chr_event(chr, CHR_EVENT_BREAK);
348 break;
349 case 'c':
350 /* Switch to the next registered device */
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200351 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
352 d->focus++;
353 if (d->focus >= d->mux_cnt)
354 d->focus = 0;
355 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000356 break;
Jan Kiszka2d229592009-06-15 22:25:30 +0200357 case 't':
358 d->timestamps = !d->timestamps;
359 d->timestamps_start = -1;
Jan Kiszka4ab312f2009-06-15 22:25:34 +0200360 d->linestart = 0;
Jan Kiszka2d229592009-06-15 22:25:30 +0200361 break;
aliguori6f97dba2008-10-31 18:49:55 +0000362 }
363 } else if (ch == term_escape_char) {
364 d->term_got_escape = 1;
365 } else {
366 send_char:
367 return 1;
368 }
369 return 0;
370}
371
372static void mux_chr_accept_input(CharDriverState *chr)
373{
aliguori6f97dba2008-10-31 18:49:55 +0000374 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200375 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000376
aliguoria80bf992009-03-05 23:00:02 +0000377 while (d->prod[m] != d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000378 d->chr_can_read[m] &&
379 d->chr_can_read[m](d->ext_opaque[m])) {
380 d->chr_read[m](d->ext_opaque[m],
aliguoria80bf992009-03-05 23:00:02 +0000381 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
aliguori6f97dba2008-10-31 18:49:55 +0000382 }
383}
384
385static int mux_chr_can_read(void *opaque)
386{
387 CharDriverState *chr = opaque;
388 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200389 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000390
aliguoria80bf992009-03-05 23:00:02 +0000391 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
aliguori6f97dba2008-10-31 18:49:55 +0000392 return 1;
aliguoria80bf992009-03-05 23:00:02 +0000393 if (d->chr_can_read[m])
394 return d->chr_can_read[m](d->ext_opaque[m]);
aliguori6f97dba2008-10-31 18:49:55 +0000395 return 0;
396}
397
398static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
399{
400 CharDriverState *chr = opaque;
401 MuxDriver *d = chr->opaque;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200402 int m = d->focus;
aliguori6f97dba2008-10-31 18:49:55 +0000403 int i;
404
405 mux_chr_accept_input (opaque);
406
407 for(i = 0; i < size; i++)
408 if (mux_proc_byte(chr, d, buf[i])) {
aliguoria80bf992009-03-05 23:00:02 +0000409 if (d->prod[m] == d->cons[m] &&
aliguori6f97dba2008-10-31 18:49:55 +0000410 d->chr_can_read[m] &&
411 d->chr_can_read[m](d->ext_opaque[m]))
412 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
413 else
aliguoria80bf992009-03-05 23:00:02 +0000414 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
aliguori6f97dba2008-10-31 18:49:55 +0000415 }
416}
417
418static void mux_chr_event(void *opaque, int event)
419{
420 CharDriverState *chr = opaque;
421 MuxDriver *d = chr->opaque;
422 int i;
423
424 /* Send the event to all registered listeners */
425 for (i = 0; i < d->mux_cnt; i++)
aliguori2724b182009-03-05 23:01:47 +0000426 mux_chr_send_event(d, i, event);
aliguori6f97dba2008-10-31 18:49:55 +0000427}
428
429static void mux_chr_update_read_handler(CharDriverState *chr)
430{
431 MuxDriver *d = chr->opaque;
432
433 if (d->mux_cnt >= MAX_MUX) {
434 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
435 return;
436 }
437 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
438 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
439 d->chr_read[d->mux_cnt] = chr->chr_read;
440 d->chr_event[d->mux_cnt] = chr->chr_event;
441 /* Fix up the real driver with mux routines */
442 if (d->mux_cnt == 0) {
443 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
444 mux_chr_event, chr);
445 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200446 if (d->focus != -1) {
447 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200448 }
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200449 d->focus = d->mux_cnt;
aliguori6f97dba2008-10-31 18:49:55 +0000450 d->mux_cnt++;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200451 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
aliguori6f97dba2008-10-31 18:49:55 +0000452}
453
454static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
455{
456 CharDriverState *chr;
457 MuxDriver *d;
458
459 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000460 d = qemu_mallocz(sizeof(MuxDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000461
462 chr->opaque = d;
463 d->drv = drv;
Gerd Hoffmann799f1f22009-09-10 10:58:55 +0200464 d->focus = -1;
aliguori6f97dba2008-10-31 18:49:55 +0000465 chr->chr_write = mux_chr_write;
466 chr->chr_update_read_handler = mux_chr_update_read_handler;
467 chr->chr_accept_input = mux_chr_accept_input;
468 return chr;
469}
470
471
472#ifdef _WIN32
aliguorid247d252008-11-11 20:46:40 +0000473int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000474{
475 int ret, len;
476
477 len = len1;
478 while (len > 0) {
479 ret = send(fd, buf, len, 0);
480 if (ret < 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000481 errno = WSAGetLastError();
482 if (errno != WSAEWOULDBLOCK) {
483 return -1;
484 }
485 } else if (ret == 0) {
486 break;
487 } else {
488 buf += ret;
489 len -= ret;
490 }
491 }
492 return len1 - len;
493}
494
495#else
496
497static int unix_write(int fd, const uint8_t *buf, int len1)
498{
499 int ret, len;
500
501 len = len1;
502 while (len > 0) {
503 ret = write(fd, buf, len);
504 if (ret < 0) {
505 if (errno != EINTR && errno != EAGAIN)
506 return -1;
507 } else if (ret == 0) {
508 break;
509 } else {
510 buf += ret;
511 len -= ret;
512 }
513 }
514 return len1 - len;
515}
516
aliguorid247d252008-11-11 20:46:40 +0000517int send_all(int fd, const void *buf, int len1)
aliguori6f97dba2008-10-31 18:49:55 +0000518{
519 return unix_write(fd, buf, len1);
520}
521#endif /* !_WIN32 */
522
523#ifndef _WIN32
524
525typedef struct {
526 int fd_in, fd_out;
527 int max_size;
528} FDCharDriver;
529
530#define STDIO_MAX_CLIENTS 1
531static int stdio_nb_clients = 0;
532
533static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
534{
535 FDCharDriver *s = chr->opaque;
536 return send_all(s->fd_out, buf, len);
537}
538
539static int fd_chr_read_poll(void *opaque)
540{
541 CharDriverState *chr = opaque;
542 FDCharDriver *s = chr->opaque;
543
544 s->max_size = qemu_chr_can_read(chr);
545 return s->max_size;
546}
547
548static void fd_chr_read(void *opaque)
549{
550 CharDriverState *chr = opaque;
551 FDCharDriver *s = chr->opaque;
552 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530553 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000554
555 len = sizeof(buf);
556 if (len > s->max_size)
557 len = s->max_size;
558 if (len == 0)
559 return;
560 size = read(s->fd_in, buf, len);
561 if (size == 0) {
562 /* FD has been closed. Remove it from the active list. */
563 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530564 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000565 return;
566 }
567 if (size > 0) {
568 qemu_chr_read(chr, buf, size);
569 }
570}
571
572static void fd_chr_update_read_handler(CharDriverState *chr)
573{
574 FDCharDriver *s = chr->opaque;
575
576 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500577 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000578 } else {
579 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
580 fd_chr_read, NULL, chr);
581 }
582 }
583}
584
585static void fd_chr_close(struct CharDriverState *chr)
586{
587 FDCharDriver *s = chr->opaque;
588
589 if (s->fd_in >= 0) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500590 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
aliguori6f97dba2008-10-31 18:49:55 +0000591 } else {
592 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
593 }
594 }
595
596 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530597 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000598}
599
600/* open a character device to a unix fd */
601static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
602{
603 CharDriverState *chr;
604 FDCharDriver *s;
605
606 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000607 s = qemu_mallocz(sizeof(FDCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000608 s->fd_in = fd_in;
609 s->fd_out = fd_out;
610 chr->opaque = s;
611 chr->chr_write = fd_chr_write;
612 chr->chr_update_read_handler = fd_chr_update_read_handler;
613 chr->chr_close = fd_chr_close;
614
Amit Shah127338e2009-11-03 19:59:56 +0530615 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000616
617 return chr;
618}
619
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200620static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000621{
622 int fd_out;
623
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100624 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200625 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
aliguori6f97dba2008-10-31 18:49:55 +0000626 if (fd_out < 0)
627 return NULL;
628 return qemu_chr_open_fd(-1, fd_out);
629}
630
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200631static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000632{
633 int fd_in, fd_out;
634 char filename_in[256], filename_out[256];
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200635 const char *filename = qemu_opt_get(opts, "path");
636
637 if (filename == NULL) {
638 fprintf(stderr, "chardev: pipe: no filename given\n");
639 return NULL;
640 }
aliguori6f97dba2008-10-31 18:49:55 +0000641
642 snprintf(filename_in, 256, "%s.in", filename);
643 snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100644 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
645 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
aliguori6f97dba2008-10-31 18:49:55 +0000646 if (fd_in < 0 || fd_out < 0) {
647 if (fd_in >= 0)
648 close(fd_in);
649 if (fd_out >= 0)
650 close(fd_out);
651 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
652 if (fd_in < 0)
653 return NULL;
654 }
655 return qemu_chr_open_fd(fd_in, fd_out);
656}
657
658
659/* for STDIO, we handle the case where several clients use it
660 (nographic mode) */
661
662#define TERM_FIFO_MAX_SIZE 1
663
664static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
665static int term_fifo_size;
666
667static int stdio_read_poll(void *opaque)
668{
669 CharDriverState *chr = opaque;
670
671 /* try to flush the queue if needed */
672 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
673 qemu_chr_read(chr, term_fifo, 1);
674 term_fifo_size = 0;
675 }
676 /* see if we can absorb more chars */
677 if (term_fifo_size == 0)
678 return 1;
679 else
680 return 0;
681}
682
683static void stdio_read(void *opaque)
684{
685 int size;
686 uint8_t buf[1];
687 CharDriverState *chr = opaque;
688
689 size = read(0, buf, 1);
690 if (size == 0) {
691 /* stdin has been closed. Remove it from the active list. */
692 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
Amit Shah793cbfb2009-08-11 21:27:48 +0530693 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000694 return;
695 }
696 if (size > 0) {
697 if (qemu_chr_can_read(chr) > 0) {
698 qemu_chr_read(chr, buf, 1);
699 } else if (term_fifo_size == 0) {
700 term_fifo[term_fifo_size++] = buf[0];
701 }
702 }
703}
704
705/* init terminal so that we can grab keys */
706static struct termios oldtty;
707static int old_fd0_flags;
708static int term_atexit_done;
709
710static void term_exit(void)
711{
712 tcsetattr (0, TCSANOW, &oldtty);
713 fcntl(0, F_SETFL, old_fd0_flags);
714}
715
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900716static void term_init(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000717{
718 struct termios tty;
719
720 tcgetattr (0, &tty);
721 oldtty = tty;
722 old_fd0_flags = fcntl(0, F_GETFL);
723
724 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
725 |INLCR|IGNCR|ICRNL|IXON);
726 tty.c_oflag |= OPOST;
727 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
728 /* if graphical mode, we allow Ctrl-C handling */
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900729 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
aliguori6f97dba2008-10-31 18:49:55 +0000730 tty.c_lflag &= ~ISIG;
731 tty.c_cflag &= ~(CSIZE|PARENB);
732 tty.c_cflag |= CS8;
733 tty.c_cc[VMIN] = 1;
734 tty.c_cc[VTIME] = 0;
735
736 tcsetattr (0, TCSANOW, &tty);
737
738 if (!term_atexit_done++)
739 atexit(term_exit);
740
741 fcntl(0, F_SETFL, O_NONBLOCK);
742}
743
744static void qemu_chr_close_stdio(struct CharDriverState *chr)
745{
746 term_exit();
747 stdio_nb_clients--;
748 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
749 fd_chr_close(chr);
750}
751
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +0200752static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000753{
754 CharDriverState *chr;
755
756 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
757 return NULL;
758 chr = qemu_chr_open_fd(0, 1);
759 chr->chr_close = qemu_chr_close_stdio;
760 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
761 stdio_nb_clients++;
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900762 term_init(opts);
aliguori6f97dba2008-10-31 18:49:55 +0000763
764 return chr;
765}
766
767#ifdef __sun__
768/* Once Solaris has openpty(), this is going to be removed. */
blueswir13f4cb3d2009-04-13 16:31:01 +0000769static int openpty(int *amaster, int *aslave, char *name,
770 struct termios *termp, struct winsize *winp)
aliguori6f97dba2008-10-31 18:49:55 +0000771{
772 const char *slave;
773 int mfd = -1, sfd = -1;
774
775 *amaster = *aslave = -1;
776
777 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
778 if (mfd < 0)
779 goto err;
780
781 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
782 goto err;
783
784 if ((slave = ptsname(mfd)) == NULL)
785 goto err;
786
787 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
788 goto err;
789
790 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
791 (termp != NULL && tcgetattr(sfd, termp) < 0))
792 goto err;
793
794 if (amaster)
795 *amaster = mfd;
796 if (aslave)
797 *aslave = sfd;
798 if (winp)
799 ioctl(sfd, TIOCSWINSZ, winp);
800
801 return 0;
802
803err:
804 if (sfd != -1)
805 close(sfd);
806 close(mfd);
807 return -1;
808}
809
blueswir13f4cb3d2009-04-13 16:31:01 +0000810static void cfmakeraw (struct termios *termios_p)
aliguori6f97dba2008-10-31 18:49:55 +0000811{
812 termios_p->c_iflag &=
813 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
814 termios_p->c_oflag &= ~OPOST;
815 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
816 termios_p->c_cflag &= ~(CSIZE|PARENB);
817 termios_p->c_cflag |= CS8;
818
819 termios_p->c_cc[VMIN] = 0;
820 termios_p->c_cc[VTIME] = 0;
821}
822#endif
823
824#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100825 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
826 || defined(__GLIBC__)
aliguori6f97dba2008-10-31 18:49:55 +0000827
828typedef struct {
829 int fd;
830 int connected;
831 int polling;
832 int read_bytes;
833 QEMUTimer *timer;
834} PtyCharDriver;
835
836static void pty_chr_update_read_handler(CharDriverState *chr);
837static void pty_chr_state(CharDriverState *chr, int connected);
838
839static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
840{
841 PtyCharDriver *s = chr->opaque;
842
843 if (!s->connected) {
844 /* guest sends data, check for (re-)connect */
845 pty_chr_update_read_handler(chr);
846 return 0;
847 }
848 return send_all(s->fd, buf, len);
849}
850
851static int pty_chr_read_poll(void *opaque)
852{
853 CharDriverState *chr = opaque;
854 PtyCharDriver *s = chr->opaque;
855
856 s->read_bytes = qemu_chr_can_read(chr);
857 return s->read_bytes;
858}
859
860static void pty_chr_read(void *opaque)
861{
862 CharDriverState *chr = opaque;
863 PtyCharDriver *s = chr->opaque;
864 int size, len;
Amit Shah9bd78542009-11-03 19:59:54 +0530865 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +0000866
867 len = sizeof(buf);
868 if (len > s->read_bytes)
869 len = s->read_bytes;
870 if (len == 0)
871 return;
872 size = read(s->fd, buf, len);
873 if ((size == -1 && errno == EIO) ||
874 (size == 0)) {
875 pty_chr_state(chr, 0);
876 return;
877 }
878 if (size > 0) {
879 pty_chr_state(chr, 1);
880 qemu_chr_read(chr, buf, size);
881 }
882}
883
884static void pty_chr_update_read_handler(CharDriverState *chr)
885{
886 PtyCharDriver *s = chr->opaque;
887
888 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
889 pty_chr_read, NULL, chr);
890 s->polling = 1;
891 /*
892 * Short timeout here: just need wait long enougth that qemu makes
893 * it through the poll loop once. When reconnected we want a
894 * short timeout so we notice it almost instantly. Otherwise
895 * read() gives us -EIO instantly, making pty_chr_state() reset the
896 * timeout to the normal (much longer) poll interval before the
897 * timer triggers.
898 */
899 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
900}
901
902static void pty_chr_state(CharDriverState *chr, int connected)
903{
904 PtyCharDriver *s = chr->opaque;
905
906 if (!connected) {
907 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
908 s->connected = 0;
909 s->polling = 0;
910 /* (re-)connect poll interval for idle guests: once per second.
911 * We check more frequently in case the guests sends data to
912 * the virtual device linked to our pty. */
913 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
914 } else {
915 if (!s->connected)
Amit Shah127338e2009-11-03 19:59:56 +0530916 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +0000917 s->connected = 1;
918 }
919}
920
921static void pty_chr_timer(void *opaque)
922{
923 struct CharDriverState *chr = opaque;
924 PtyCharDriver *s = chr->opaque;
925
926 if (s->connected)
927 return;
928 if (s->polling) {
929 /* If we arrive here without polling being cleared due
930 * read returning -EIO, then we are (re-)connected */
931 pty_chr_state(chr, 1);
932 return;
933 }
934
935 /* Next poll ... */
936 pty_chr_update_read_handler(chr);
937}
938
939static void pty_chr_close(struct CharDriverState *chr)
940{
941 PtyCharDriver *s = chr->opaque;
942
943 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
944 close(s->fd);
aliguori819f56b2009-03-28 17:58:14 +0000945 qemu_del_timer(s->timer);
946 qemu_free_timer(s->timer);
aliguori6f97dba2008-10-31 18:49:55 +0000947 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +0530948 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +0000949}
950
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200951static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +0000952{
953 CharDriverState *chr;
954 PtyCharDriver *s;
955 struct termios tty;
956 int slave_fd, len;
blueswir1c5e97232009-03-07 20:06:23 +0000957#if defined(__OpenBSD__) || defined(__DragonFly__)
aliguori6f97dba2008-10-31 18:49:55 +0000958 char pty_name[PATH_MAX];
959#define q_ptsname(x) pty_name
960#else
961 char *pty_name = NULL;
962#define q_ptsname(x) ptsname(x)
963#endif
964
965 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +0000966 s = qemu_mallocz(sizeof(PtyCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +0000967
968 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
969 return NULL;
970 }
971
972 /* Set raw attributes on the pty. */
aliguoric3b972c2008-11-12 15:00:36 +0000973 tcgetattr(slave_fd, &tty);
aliguori6f97dba2008-10-31 18:49:55 +0000974 cfmakeraw(&tty);
975 tcsetattr(slave_fd, TCSAFLUSH, &tty);
976 close(slave_fd);
977
978 len = strlen(q_ptsname(s->fd)) + 5;
979 chr->filename = qemu_malloc(len);
980 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
Gerd Hoffmann4490dad2009-09-10 10:58:43 +0200981 qemu_opt_set(opts, "path", q_ptsname(s->fd));
aliguori6f97dba2008-10-31 18:49:55 +0000982 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
983
984 chr->opaque = s;
985 chr->chr_write = pty_chr_write;
986 chr->chr_update_read_handler = pty_chr_update_read_handler;
987 chr->chr_close = pty_chr_close;
988
989 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
990
991 return chr;
992}
993
994static void tty_serial_init(int fd, int speed,
995 int parity, int data_bits, int stop_bits)
996{
997 struct termios tty;
998 speed_t spd;
999
1000#if 0
1001 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1002 speed, parity, data_bits, stop_bits);
1003#endif
1004 tcgetattr (fd, &tty);
1005
Stefan Weil45eea132009-10-26 16:10:10 +01001006#define check_speed(val) if (speed <= val) { spd = B##val; break; }
1007 speed = speed * 10 / 11;
1008 do {
1009 check_speed(50);
1010 check_speed(75);
1011 check_speed(110);
1012 check_speed(134);
1013 check_speed(150);
1014 check_speed(200);
1015 check_speed(300);
1016 check_speed(600);
1017 check_speed(1200);
1018 check_speed(1800);
1019 check_speed(2400);
1020 check_speed(4800);
1021 check_speed(9600);
1022 check_speed(19200);
1023 check_speed(38400);
1024 /* Non-Posix values follow. They may be unsupported on some systems. */
1025 check_speed(57600);
1026 check_speed(115200);
1027#ifdef B230400
1028 check_speed(230400);
1029#endif
1030#ifdef B460800
1031 check_speed(460800);
1032#endif
1033#ifdef B500000
1034 check_speed(500000);
1035#endif
1036#ifdef B576000
1037 check_speed(576000);
1038#endif
1039#ifdef B921600
1040 check_speed(921600);
1041#endif
1042#ifdef B1000000
1043 check_speed(1000000);
1044#endif
1045#ifdef B1152000
1046 check_speed(1152000);
1047#endif
1048#ifdef B1500000
1049 check_speed(1500000);
1050#endif
1051#ifdef B2000000
1052 check_speed(2000000);
1053#endif
1054#ifdef B2500000
1055 check_speed(2500000);
1056#endif
1057#ifdef B3000000
1058 check_speed(3000000);
1059#endif
1060#ifdef B3500000
1061 check_speed(3500000);
1062#endif
1063#ifdef B4000000
1064 check_speed(4000000);
1065#endif
aliguori6f97dba2008-10-31 18:49:55 +00001066 spd = B115200;
Stefan Weil45eea132009-10-26 16:10:10 +01001067 } while (0);
aliguori6f97dba2008-10-31 18:49:55 +00001068
1069 cfsetispeed(&tty, spd);
1070 cfsetospeed(&tty, spd);
1071
1072 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1073 |INLCR|IGNCR|ICRNL|IXON);
1074 tty.c_oflag |= OPOST;
1075 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1076 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1077 switch(data_bits) {
1078 default:
1079 case 8:
1080 tty.c_cflag |= CS8;
1081 break;
1082 case 7:
1083 tty.c_cflag |= CS7;
1084 break;
1085 case 6:
1086 tty.c_cflag |= CS6;
1087 break;
1088 case 5:
1089 tty.c_cflag |= CS5;
1090 break;
1091 }
1092 switch(parity) {
1093 default:
1094 case 'N':
1095 break;
1096 case 'E':
1097 tty.c_cflag |= PARENB;
1098 break;
1099 case 'O':
1100 tty.c_cflag |= PARENB | PARODD;
1101 break;
1102 }
1103 if (stop_bits == 2)
1104 tty.c_cflag |= CSTOPB;
1105
1106 tcsetattr (fd, TCSANOW, &tty);
1107}
1108
1109static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1110{
1111 FDCharDriver *s = chr->opaque;
1112
1113 switch(cmd) {
1114 case CHR_IOCTL_SERIAL_SET_PARAMS:
1115 {
1116 QEMUSerialSetParams *ssp = arg;
1117 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1118 ssp->data_bits, ssp->stop_bits);
1119 }
1120 break;
1121 case CHR_IOCTL_SERIAL_SET_BREAK:
1122 {
1123 int enable = *(int *)arg;
1124 if (enable)
1125 tcsendbreak(s->fd_in, 1);
1126 }
1127 break;
1128 case CHR_IOCTL_SERIAL_GET_TIOCM:
1129 {
1130 int sarg = 0;
1131 int *targ = (int *)arg;
1132 ioctl(s->fd_in, TIOCMGET, &sarg);
1133 *targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001134 if (sarg & TIOCM_CTS)
aliguori6f97dba2008-10-31 18:49:55 +00001135 *targ |= CHR_TIOCM_CTS;
aurel32b4abdfa2009-02-08 14:46:17 +00001136 if (sarg & TIOCM_CAR)
aliguori6f97dba2008-10-31 18:49:55 +00001137 *targ |= CHR_TIOCM_CAR;
aurel32b4abdfa2009-02-08 14:46:17 +00001138 if (sarg & TIOCM_DSR)
aliguori6f97dba2008-10-31 18:49:55 +00001139 *targ |= CHR_TIOCM_DSR;
aurel32b4abdfa2009-02-08 14:46:17 +00001140 if (sarg & TIOCM_RI)
aliguori6f97dba2008-10-31 18:49:55 +00001141 *targ |= CHR_TIOCM_RI;
aurel32b4abdfa2009-02-08 14:46:17 +00001142 if (sarg & TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001143 *targ |= CHR_TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001144 if (sarg & TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001145 *targ |= CHR_TIOCM_RTS;
1146 }
1147 break;
1148 case CHR_IOCTL_SERIAL_SET_TIOCM:
1149 {
1150 int sarg = *(int *)arg;
1151 int targ = 0;
aurel32b4abdfa2009-02-08 14:46:17 +00001152 ioctl(s->fd_in, TIOCMGET, &targ);
1153 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1154 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1155 if (sarg & CHR_TIOCM_CTS)
1156 targ |= TIOCM_CTS;
1157 if (sarg & CHR_TIOCM_CAR)
1158 targ |= TIOCM_CAR;
1159 if (sarg & CHR_TIOCM_DSR)
1160 targ |= TIOCM_DSR;
1161 if (sarg & CHR_TIOCM_RI)
1162 targ |= TIOCM_RI;
1163 if (sarg & CHR_TIOCM_DTR)
aliguori6f97dba2008-10-31 18:49:55 +00001164 targ |= TIOCM_DTR;
aurel32b4abdfa2009-02-08 14:46:17 +00001165 if (sarg & CHR_TIOCM_RTS)
aliguori6f97dba2008-10-31 18:49:55 +00001166 targ |= TIOCM_RTS;
1167 ioctl(s->fd_in, TIOCMSET, &targ);
1168 }
1169 break;
1170 default:
1171 return -ENOTSUP;
1172 }
1173 return 0;
1174}
1175
David Ahern4266a132010-02-10 18:27:17 -07001176static void qemu_chr_close_tty(CharDriverState *chr)
1177{
1178 FDCharDriver *s = chr->opaque;
1179 int fd = -1;
1180
1181 if (s) {
1182 fd = s->fd_in;
1183 }
1184
1185 fd_chr_close(chr);
1186
1187 if (fd >= 0) {
1188 close(fd);
1189 }
1190}
1191
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001192static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001193{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001194 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001195 CharDriverState *chr;
1196 int fd;
1197
1198 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
Evgeniy Dushistovafc535a2010-01-28 21:44:46 +03001199 if (fd < 0) {
1200 return NULL;
1201 }
aliguori6f97dba2008-10-31 18:49:55 +00001202 tty_serial_init(fd, 115200, 'N', 8, 1);
1203 chr = qemu_chr_open_fd(fd, fd);
1204 if (!chr) {
1205 close(fd);
1206 return NULL;
1207 }
1208 chr->chr_ioctl = tty_serial_ioctl;
David Ahern4266a132010-02-10 18:27:17 -07001209 chr->chr_close = qemu_chr_close_tty;
aliguori6f97dba2008-10-31 18:49:55 +00001210 return chr;
1211}
1212#else /* ! __linux__ && ! __sun__ */
1213static CharDriverState *qemu_chr_open_pty(void)
1214{
1215 return NULL;
1216}
1217#endif /* __linux__ || __sun__ */
1218
1219#if defined(__linux__)
1220typedef struct {
1221 int fd;
1222 int mode;
1223} ParallelCharDriver;
1224
1225static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1226{
1227 if (s->mode != mode) {
1228 int m = mode;
1229 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1230 return 0;
1231 s->mode = mode;
1232 }
1233 return 1;
1234}
1235
1236static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1237{
1238 ParallelCharDriver *drv = chr->opaque;
1239 int fd = drv->fd;
1240 uint8_t b;
1241
1242 switch(cmd) {
1243 case CHR_IOCTL_PP_READ_DATA:
1244 if (ioctl(fd, PPRDATA, &b) < 0)
1245 return -ENOTSUP;
1246 *(uint8_t *)arg = b;
1247 break;
1248 case CHR_IOCTL_PP_WRITE_DATA:
1249 b = *(uint8_t *)arg;
1250 if (ioctl(fd, PPWDATA, &b) < 0)
1251 return -ENOTSUP;
1252 break;
1253 case CHR_IOCTL_PP_READ_CONTROL:
1254 if (ioctl(fd, PPRCONTROL, &b) < 0)
1255 return -ENOTSUP;
1256 /* Linux gives only the lowest bits, and no way to know data
1257 direction! For better compatibility set the fixed upper
1258 bits. */
1259 *(uint8_t *)arg = b | 0xc0;
1260 break;
1261 case CHR_IOCTL_PP_WRITE_CONTROL:
1262 b = *(uint8_t *)arg;
1263 if (ioctl(fd, PPWCONTROL, &b) < 0)
1264 return -ENOTSUP;
1265 break;
1266 case CHR_IOCTL_PP_READ_STATUS:
1267 if (ioctl(fd, PPRSTATUS, &b) < 0)
1268 return -ENOTSUP;
1269 *(uint8_t *)arg = b;
1270 break;
1271 case CHR_IOCTL_PP_DATA_DIR:
1272 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1273 return -ENOTSUP;
1274 break;
1275 case CHR_IOCTL_PP_EPP_READ_ADDR:
1276 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1277 struct ParallelIOArg *parg = arg;
1278 int n = read(fd, parg->buffer, parg->count);
1279 if (n != parg->count) {
1280 return -EIO;
1281 }
1282 }
1283 break;
1284 case CHR_IOCTL_PP_EPP_READ:
1285 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1286 struct ParallelIOArg *parg = arg;
1287 int n = read(fd, parg->buffer, parg->count);
1288 if (n != parg->count) {
1289 return -EIO;
1290 }
1291 }
1292 break;
1293 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1294 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1295 struct ParallelIOArg *parg = arg;
1296 int n = write(fd, parg->buffer, parg->count);
1297 if (n != parg->count) {
1298 return -EIO;
1299 }
1300 }
1301 break;
1302 case CHR_IOCTL_PP_EPP_WRITE:
1303 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1304 struct ParallelIOArg *parg = arg;
1305 int n = write(fd, parg->buffer, parg->count);
1306 if (n != parg->count) {
1307 return -EIO;
1308 }
1309 }
1310 break;
1311 default:
1312 return -ENOTSUP;
1313 }
1314 return 0;
1315}
1316
1317static void pp_close(CharDriverState *chr)
1318{
1319 ParallelCharDriver *drv = chr->opaque;
1320 int fd = drv->fd;
1321
1322 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1323 ioctl(fd, PPRELEASE);
1324 close(fd);
1325 qemu_free(drv);
Amit Shah793cbfb2009-08-11 21:27:48 +05301326 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001327}
1328
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001329static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001330{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001331 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001332 CharDriverState *chr;
1333 ParallelCharDriver *drv;
1334 int fd;
1335
1336 TFR(fd = open(filename, O_RDWR));
1337 if (fd < 0)
1338 return NULL;
1339
1340 if (ioctl(fd, PPCLAIM) < 0) {
1341 close(fd);
1342 return NULL;
1343 }
1344
1345 drv = qemu_mallocz(sizeof(ParallelCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001346 drv->fd = fd;
1347 drv->mode = IEEE1284_MODE_COMPAT;
1348
1349 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001350 chr->chr_write = null_chr_write;
1351 chr->chr_ioctl = pp_ioctl;
1352 chr->chr_close = pp_close;
1353 chr->opaque = drv;
1354
Amit Shah127338e2009-11-03 19:59:56 +05301355 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001356
1357 return chr;
1358}
1359#endif /* __linux__ */
1360
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01001361#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
blueswir16972f932008-11-22 20:49:12 +00001362static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1363{
1364 int fd = (int)chr->opaque;
1365 uint8_t b;
1366
1367 switch(cmd) {
1368 case CHR_IOCTL_PP_READ_DATA:
1369 if (ioctl(fd, PPIGDATA, &b) < 0)
1370 return -ENOTSUP;
1371 *(uint8_t *)arg = b;
1372 break;
1373 case CHR_IOCTL_PP_WRITE_DATA:
1374 b = *(uint8_t *)arg;
1375 if (ioctl(fd, PPISDATA, &b) < 0)
1376 return -ENOTSUP;
1377 break;
1378 case CHR_IOCTL_PP_READ_CONTROL:
1379 if (ioctl(fd, PPIGCTRL, &b) < 0)
1380 return -ENOTSUP;
1381 *(uint8_t *)arg = b;
1382 break;
1383 case CHR_IOCTL_PP_WRITE_CONTROL:
1384 b = *(uint8_t *)arg;
1385 if (ioctl(fd, PPISCTRL, &b) < 0)
1386 return -ENOTSUP;
1387 break;
1388 case CHR_IOCTL_PP_READ_STATUS:
1389 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1390 return -ENOTSUP;
1391 *(uint8_t *)arg = b;
1392 break;
1393 default:
1394 return -ENOTSUP;
1395 }
1396 return 0;
1397}
1398
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001399static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
blueswir16972f932008-11-22 20:49:12 +00001400{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001401 const char *filename = qemu_opt_get(opts, "path");
blueswir16972f932008-11-22 20:49:12 +00001402 CharDriverState *chr;
1403 int fd;
1404
1405 fd = open(filename, O_RDWR);
1406 if (fd < 0)
1407 return NULL;
1408
1409 chr = qemu_mallocz(sizeof(CharDriverState));
blueswir16972f932008-11-22 20:49:12 +00001410 chr->opaque = (void *)fd;
1411 chr->chr_write = null_chr_write;
1412 chr->chr_ioctl = pp_ioctl;
1413 return chr;
1414}
1415#endif
1416
aliguori6f97dba2008-10-31 18:49:55 +00001417#else /* _WIN32 */
1418
1419typedef struct {
1420 int max_size;
1421 HANDLE hcom, hrecv, hsend;
1422 OVERLAPPED orecv, osend;
1423 BOOL fpipe;
1424 DWORD len;
1425} WinCharState;
1426
1427#define NSENDBUF 2048
1428#define NRECVBUF 2048
1429#define MAXCONNECT 1
1430#define NTIMEOUT 5000
1431
1432static int win_chr_poll(void *opaque);
1433static int win_chr_pipe_poll(void *opaque);
1434
1435static void win_chr_close(CharDriverState *chr)
1436{
1437 WinCharState *s = chr->opaque;
1438
1439 if (s->hsend) {
1440 CloseHandle(s->hsend);
1441 s->hsend = NULL;
1442 }
1443 if (s->hrecv) {
1444 CloseHandle(s->hrecv);
1445 s->hrecv = NULL;
1446 }
1447 if (s->hcom) {
1448 CloseHandle(s->hcom);
1449 s->hcom = NULL;
1450 }
1451 if (s->fpipe)
1452 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1453 else
1454 qemu_del_polling_cb(win_chr_poll, chr);
Amit Shah793cbfb2009-08-11 21:27:48 +05301455
1456 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00001457}
1458
1459static int win_chr_init(CharDriverState *chr, const char *filename)
1460{
1461 WinCharState *s = chr->opaque;
1462 COMMCONFIG comcfg;
1463 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1464 COMSTAT comstat;
1465 DWORD size;
1466 DWORD err;
1467
1468 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1469 if (!s->hsend) {
1470 fprintf(stderr, "Failed CreateEvent\n");
1471 goto fail;
1472 }
1473 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1474 if (!s->hrecv) {
1475 fprintf(stderr, "Failed CreateEvent\n");
1476 goto fail;
1477 }
1478
1479 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1480 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1481 if (s->hcom == INVALID_HANDLE_VALUE) {
1482 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1483 s->hcom = NULL;
1484 goto fail;
1485 }
1486
1487 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1488 fprintf(stderr, "Failed SetupComm\n");
1489 goto fail;
1490 }
1491
1492 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1493 size = sizeof(COMMCONFIG);
1494 GetDefaultCommConfig(filename, &comcfg, &size);
1495 comcfg.dcb.DCBlength = sizeof(DCB);
1496 CommConfigDialog(filename, NULL, &comcfg);
1497
1498 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1499 fprintf(stderr, "Failed SetCommState\n");
1500 goto fail;
1501 }
1502
1503 if (!SetCommMask(s->hcom, EV_ERR)) {
1504 fprintf(stderr, "Failed SetCommMask\n");
1505 goto fail;
1506 }
1507
1508 cto.ReadIntervalTimeout = MAXDWORD;
1509 if (!SetCommTimeouts(s->hcom, &cto)) {
1510 fprintf(stderr, "Failed SetCommTimeouts\n");
1511 goto fail;
1512 }
1513
1514 if (!ClearCommError(s->hcom, &err, &comstat)) {
1515 fprintf(stderr, "Failed ClearCommError\n");
1516 goto fail;
1517 }
1518 qemu_add_polling_cb(win_chr_poll, chr);
1519 return 0;
1520
1521 fail:
1522 win_chr_close(chr);
1523 return -1;
1524}
1525
1526static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1527{
1528 WinCharState *s = chr->opaque;
1529 DWORD len, ret, size, err;
1530
1531 len = len1;
1532 ZeroMemory(&s->osend, sizeof(s->osend));
1533 s->osend.hEvent = s->hsend;
1534 while (len > 0) {
1535 if (s->hsend)
1536 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1537 else
1538 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1539 if (!ret) {
1540 err = GetLastError();
1541 if (err == ERROR_IO_PENDING) {
1542 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1543 if (ret) {
1544 buf += size;
1545 len -= size;
1546 } else {
1547 break;
1548 }
1549 } else {
1550 break;
1551 }
1552 } else {
1553 buf += size;
1554 len -= size;
1555 }
1556 }
1557 return len1 - len;
1558}
1559
1560static int win_chr_read_poll(CharDriverState *chr)
1561{
1562 WinCharState *s = chr->opaque;
1563
1564 s->max_size = qemu_chr_can_read(chr);
1565 return s->max_size;
1566}
1567
1568static void win_chr_readfile(CharDriverState *chr)
1569{
1570 WinCharState *s = chr->opaque;
1571 int ret, err;
Amit Shah9bd78542009-11-03 19:59:54 +05301572 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001573 DWORD size;
1574
1575 ZeroMemory(&s->orecv, sizeof(s->orecv));
1576 s->orecv.hEvent = s->hrecv;
1577 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1578 if (!ret) {
1579 err = GetLastError();
1580 if (err == ERROR_IO_PENDING) {
1581 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1582 }
1583 }
1584
1585 if (size > 0) {
1586 qemu_chr_read(chr, buf, size);
1587 }
1588}
1589
1590static void win_chr_read(CharDriverState *chr)
1591{
1592 WinCharState *s = chr->opaque;
1593
1594 if (s->len > s->max_size)
1595 s->len = s->max_size;
1596 if (s->len == 0)
1597 return;
1598
1599 win_chr_readfile(chr);
1600}
1601
1602static int win_chr_poll(void *opaque)
1603{
1604 CharDriverState *chr = opaque;
1605 WinCharState *s = chr->opaque;
1606 COMSTAT status;
1607 DWORD comerr;
1608
1609 ClearCommError(s->hcom, &comerr, &status);
1610 if (status.cbInQue > 0) {
1611 s->len = status.cbInQue;
1612 win_chr_read_poll(chr);
1613 win_chr_read(chr);
1614 return 1;
1615 }
1616 return 0;
1617}
1618
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001619static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001620{
Gerd Hoffmann48b76492009-09-10 10:58:48 +02001621 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001622 CharDriverState *chr;
1623 WinCharState *s;
1624
1625 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001626 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001627 chr->opaque = s;
1628 chr->chr_write = win_chr_write;
1629 chr->chr_close = win_chr_close;
1630
1631 if (win_chr_init(chr, filename) < 0) {
1632 free(s);
1633 free(chr);
1634 return NULL;
1635 }
Amit Shah127338e2009-11-03 19:59:56 +05301636 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001637 return chr;
1638}
1639
1640static int win_chr_pipe_poll(void *opaque)
1641{
1642 CharDriverState *chr = opaque;
1643 WinCharState *s = chr->opaque;
1644 DWORD size;
1645
1646 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1647 if (size > 0) {
1648 s->len = size;
1649 win_chr_read_poll(chr);
1650 win_chr_read(chr);
1651 return 1;
1652 }
1653 return 0;
1654}
1655
1656static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1657{
1658 WinCharState *s = chr->opaque;
1659 OVERLAPPED ov;
1660 int ret;
1661 DWORD size;
1662 char openname[256];
1663
1664 s->fpipe = TRUE;
1665
1666 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1667 if (!s->hsend) {
1668 fprintf(stderr, "Failed CreateEvent\n");
1669 goto fail;
1670 }
1671 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1672 if (!s->hrecv) {
1673 fprintf(stderr, "Failed CreateEvent\n");
1674 goto fail;
1675 }
1676
1677 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1678 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1679 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1680 PIPE_WAIT,
1681 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1682 if (s->hcom == INVALID_HANDLE_VALUE) {
1683 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1684 s->hcom = NULL;
1685 goto fail;
1686 }
1687
1688 ZeroMemory(&ov, sizeof(ov));
1689 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1690 ret = ConnectNamedPipe(s->hcom, &ov);
1691 if (ret) {
1692 fprintf(stderr, "Failed ConnectNamedPipe\n");
1693 goto fail;
1694 }
1695
1696 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1697 if (!ret) {
1698 fprintf(stderr, "Failed GetOverlappedResult\n");
1699 if (ov.hEvent) {
1700 CloseHandle(ov.hEvent);
1701 ov.hEvent = NULL;
1702 }
1703 goto fail;
1704 }
1705
1706 if (ov.hEvent) {
1707 CloseHandle(ov.hEvent);
1708 ov.hEvent = NULL;
1709 }
1710 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1711 return 0;
1712
1713 fail:
1714 win_chr_close(chr);
1715 return -1;
1716}
1717
1718
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001719static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001720{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001721 const char *filename = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001722 CharDriverState *chr;
1723 WinCharState *s;
1724
1725 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001726 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001727 chr->opaque = s;
1728 chr->chr_write = win_chr_write;
1729 chr->chr_close = win_chr_close;
1730
1731 if (win_chr_pipe_init(chr, filename) < 0) {
1732 free(s);
1733 free(chr);
1734 return NULL;
1735 }
Amit Shah127338e2009-11-03 19:59:56 +05301736 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001737 return chr;
1738}
1739
1740static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1741{
1742 CharDriverState *chr;
1743 WinCharState *s;
1744
1745 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001746 s = qemu_mallocz(sizeof(WinCharState));
aliguori6f97dba2008-10-31 18:49:55 +00001747 s->hcom = fd_out;
1748 chr->opaque = s;
1749 chr->chr_write = win_chr_write;
Amit Shah127338e2009-11-03 19:59:56 +05301750 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00001751 return chr;
1752}
1753
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02001754static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001755{
1756 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1757}
1758
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001759static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001760{
Gerd Hoffmann7d315442009-09-10 10:58:36 +02001761 const char *file_out = qemu_opt_get(opts, "path");
aliguori6f97dba2008-10-31 18:49:55 +00001762 HANDLE fd_out;
1763
1764 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1765 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1766 if (fd_out == INVALID_HANDLE_VALUE)
1767 return NULL;
1768
1769 return qemu_chr_open_win_file(fd_out);
1770}
1771#endif /* !_WIN32 */
1772
1773/***********************************************************/
1774/* UDP Net console */
1775
1776typedef struct {
1777 int fd;
Amit Shah9bd78542009-11-03 19:59:54 +05301778 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00001779 int bufcnt;
1780 int bufptr;
1781 int max_size;
1782} NetCharDriver;
1783
1784static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1785{
1786 NetCharDriver *s = chr->opaque;
1787
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001788 return send(s->fd, (const void *)buf, len, 0);
aliguori6f97dba2008-10-31 18:49:55 +00001789}
1790
1791static int udp_chr_read_poll(void *opaque)
1792{
1793 CharDriverState *chr = opaque;
1794 NetCharDriver *s = chr->opaque;
1795
1796 s->max_size = qemu_chr_can_read(chr);
1797
1798 /* If there were any stray characters in the queue process them
1799 * first
1800 */
1801 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1802 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1803 s->bufptr++;
1804 s->max_size = qemu_chr_can_read(chr);
1805 }
1806 return s->max_size;
1807}
1808
1809static void udp_chr_read(void *opaque)
1810{
1811 CharDriverState *chr = opaque;
1812 NetCharDriver *s = chr->opaque;
1813
1814 if (s->max_size == 0)
1815 return;
Blue Swirlc5b76b32009-06-13 08:44:31 +00001816 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
aliguori6f97dba2008-10-31 18:49:55 +00001817 s->bufptr = s->bufcnt;
1818 if (s->bufcnt <= 0)
1819 return;
1820
1821 s->bufptr = 0;
1822 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1823 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1824 s->bufptr++;
1825 s->max_size = qemu_chr_can_read(chr);
1826 }
1827}
1828
1829static void udp_chr_update_read_handler(CharDriverState *chr)
1830{
1831 NetCharDriver *s = chr->opaque;
1832
1833 if (s->fd >= 0) {
1834 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1835 udp_chr_read, NULL, chr);
1836 }
1837}
1838
aliguori819f56b2009-03-28 17:58:14 +00001839static void udp_chr_close(CharDriverState *chr)
1840{
1841 NetCharDriver *s = chr->opaque;
1842 if (s->fd >= 0) {
1843 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1844 closesocket(s->fd);
1845 }
1846 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05301847 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori819f56b2009-03-28 17:58:14 +00001848}
1849
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001850static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00001851{
1852 CharDriverState *chr = NULL;
1853 NetCharDriver *s = NULL;
1854 int fd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00001855
1856 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00001857 s = qemu_mallocz(sizeof(NetCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00001858
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001859 fd = inet_dgram_opts(opts);
aliguori6f97dba2008-10-31 18:49:55 +00001860 if (fd < 0) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02001861 fprintf(stderr, "inet_dgram_opts failed\n");
aliguori6f97dba2008-10-31 18:49:55 +00001862 goto return_err;
1863 }
1864
1865 s->fd = fd;
1866 s->bufcnt = 0;
1867 s->bufptr = 0;
1868 chr->opaque = s;
1869 chr->chr_write = udp_chr_write;
1870 chr->chr_update_read_handler = udp_chr_update_read_handler;
aliguori819f56b2009-03-28 17:58:14 +00001871 chr->chr_close = udp_chr_close;
aliguori6f97dba2008-10-31 18:49:55 +00001872 return chr;
1873
1874return_err:
1875 if (chr)
1876 free(chr);
1877 if (s)
1878 free(s);
1879 if (fd >= 0)
1880 closesocket(fd);
1881 return NULL;
1882}
1883
1884/***********************************************************/
1885/* TCP Net console */
1886
1887typedef struct {
1888 int fd, listen_fd;
1889 int connected;
1890 int max_size;
1891 int do_telnetopt;
1892 int do_nodelay;
1893 int is_unix;
Mark McLoughlin7d174052009-07-22 09:11:39 +01001894 int msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00001895} TCPCharDriver;
1896
1897static void tcp_chr_accept(void *opaque);
1898
1899static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1900{
1901 TCPCharDriver *s = chr->opaque;
1902 if (s->connected) {
1903 return send_all(s->fd, buf, len);
1904 } else {
1905 /* XXX: indicate an error ? */
1906 return len;
1907 }
1908}
1909
1910static int tcp_chr_read_poll(void *opaque)
1911{
1912 CharDriverState *chr = opaque;
1913 TCPCharDriver *s = chr->opaque;
1914 if (!s->connected)
1915 return 0;
1916 s->max_size = qemu_chr_can_read(chr);
1917 return s->max_size;
1918}
1919
1920#define IAC 255
1921#define IAC_BREAK 243
1922static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1923 TCPCharDriver *s,
1924 uint8_t *buf, int *size)
1925{
1926 /* Handle any telnet client's basic IAC options to satisfy char by
1927 * char mode with no echo. All IAC options will be removed from
1928 * the buf and the do_telnetopt variable will be used to track the
1929 * state of the width of the IAC information.
1930 *
1931 * IAC commands come in sets of 3 bytes with the exception of the
1932 * "IAC BREAK" command and the double IAC.
1933 */
1934
1935 int i;
1936 int j = 0;
1937
1938 for (i = 0; i < *size; i++) {
1939 if (s->do_telnetopt > 1) {
1940 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1941 /* Double IAC means send an IAC */
1942 if (j != i)
1943 buf[j] = buf[i];
1944 j++;
1945 s->do_telnetopt = 1;
1946 } else {
1947 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1948 /* Handle IAC break commands by sending a serial break */
1949 qemu_chr_event(chr, CHR_EVENT_BREAK);
1950 s->do_telnetopt++;
1951 }
1952 s->do_telnetopt++;
1953 }
1954 if (s->do_telnetopt >= 4) {
1955 s->do_telnetopt = 1;
1956 }
1957 } else {
1958 if ((unsigned char)buf[i] == IAC) {
1959 s->do_telnetopt = 2;
1960 } else {
1961 if (j != i)
1962 buf[j] = buf[i];
1963 j++;
1964 }
1965 }
1966 }
1967 *size = j;
1968}
1969
Mark McLoughlin7d174052009-07-22 09:11:39 +01001970static int tcp_get_msgfd(CharDriverState *chr)
1971{
1972 TCPCharDriver *s = chr->opaque;
1973
1974 return s->msgfd;
1975}
1976
Anthony Liguori73bcc2a2009-07-27 14:55:25 -05001977#ifndef _WIN32
Mark McLoughlin7d174052009-07-22 09:11:39 +01001978static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1979{
1980 TCPCharDriver *s = chr->opaque;
1981 struct cmsghdr *cmsg;
1982
1983 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1984 int fd;
1985
1986 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1987 cmsg->cmsg_level != SOL_SOCKET ||
1988 cmsg->cmsg_type != SCM_RIGHTS)
1989 continue;
1990
1991 fd = *((int *)CMSG_DATA(cmsg));
1992 if (fd < 0)
1993 continue;
1994
1995 if (s->msgfd != -1)
1996 close(s->msgfd);
1997 s->msgfd = fd;
1998 }
1999}
2000
Mark McLoughlin9977c892009-07-22 09:11:38 +01002001static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2002{
2003 TCPCharDriver *s = chr->opaque;
Blue Swirl7cba04f2009-08-01 10:13:20 +00002004 struct msghdr msg = { NULL, };
Mark McLoughlin9977c892009-07-22 09:11:38 +01002005 struct iovec iov[1];
Mark McLoughlin7d174052009-07-22 09:11:39 +01002006 union {
2007 struct cmsghdr cmsg;
2008 char control[CMSG_SPACE(sizeof(int))];
2009 } msg_control;
2010 ssize_t ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002011
2012 iov[0].iov_base = buf;
2013 iov[0].iov_len = len;
2014
2015 msg.msg_iov = iov;
2016 msg.msg_iovlen = 1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002017 msg.msg_control = &msg_control;
2018 msg.msg_controllen = sizeof(msg_control);
Mark McLoughlin9977c892009-07-22 09:11:38 +01002019
Mark McLoughlin7d174052009-07-22 09:11:39 +01002020 ret = recvmsg(s->fd, &msg, 0);
2021 if (ret > 0 && s->is_unix)
2022 unix_process_msgfd(chr, &msg);
2023
2024 return ret;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002025}
2026#else
2027static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2028{
2029 TCPCharDriver *s = chr->opaque;
2030 return recv(s->fd, buf, len, 0);
2031}
2032#endif
2033
aliguori6f97dba2008-10-31 18:49:55 +00002034static void tcp_chr_read(void *opaque)
2035{
2036 CharDriverState *chr = opaque;
2037 TCPCharDriver *s = chr->opaque;
Amit Shah9bd78542009-11-03 19:59:54 +05302038 uint8_t buf[READ_BUF_LEN];
aliguori6f97dba2008-10-31 18:49:55 +00002039 int len, size;
2040
2041 if (!s->connected || s->max_size <= 0)
2042 return;
2043 len = sizeof(buf);
2044 if (len > s->max_size)
2045 len = s->max_size;
Mark McLoughlin9977c892009-07-22 09:11:38 +01002046 size = tcp_chr_recv(chr, (void *)buf, len);
aliguori6f97dba2008-10-31 18:49:55 +00002047 if (size == 0) {
2048 /* connection closed */
2049 s->connected = 0;
2050 if (s->listen_fd >= 0) {
2051 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2052 }
2053 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2054 closesocket(s->fd);
2055 s->fd = -1;
Amit Shah793cbfb2009-08-11 21:27:48 +05302056 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002057 } else if (size > 0) {
2058 if (s->do_telnetopt)
2059 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2060 if (size > 0)
2061 qemu_chr_read(chr, buf, size);
Mark McLoughlin7d174052009-07-22 09:11:39 +01002062 if (s->msgfd != -1) {
2063 close(s->msgfd);
2064 s->msgfd = -1;
2065 }
aliguori6f97dba2008-10-31 18:49:55 +00002066 }
2067}
2068
2069static void tcp_chr_connect(void *opaque)
2070{
2071 CharDriverState *chr = opaque;
2072 TCPCharDriver *s = chr->opaque;
2073
2074 s->connected = 1;
2075 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2076 tcp_chr_read, NULL, chr);
Amit Shah127338e2009-11-03 19:59:56 +05302077 qemu_chr_generic_open(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002078}
2079
2080#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2081static void tcp_chr_telnet_init(int fd)
2082{
2083 char buf[3];
2084 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2085 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2086 send(fd, (char *)buf, 3, 0);
2087 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2088 send(fd, (char *)buf, 3, 0);
2089 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2090 send(fd, (char *)buf, 3, 0);
2091 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2092 send(fd, (char *)buf, 3, 0);
2093}
2094
2095static void socket_set_nodelay(int fd)
2096{
2097 int val = 1;
2098 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2099}
2100
2101static void tcp_chr_accept(void *opaque)
2102{
2103 CharDriverState *chr = opaque;
2104 TCPCharDriver *s = chr->opaque;
2105 struct sockaddr_in saddr;
2106#ifndef _WIN32
2107 struct sockaddr_un uaddr;
2108#endif
2109 struct sockaddr *addr;
2110 socklen_t len;
2111 int fd;
2112
2113 for(;;) {
2114#ifndef _WIN32
2115 if (s->is_unix) {
2116 len = sizeof(uaddr);
2117 addr = (struct sockaddr *)&uaddr;
2118 } else
2119#endif
2120 {
2121 len = sizeof(saddr);
2122 addr = (struct sockaddr *)&saddr;
2123 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01002124 fd = qemu_accept(s->listen_fd, addr, &len);
aliguori6f97dba2008-10-31 18:49:55 +00002125 if (fd < 0 && errno != EINTR) {
2126 return;
2127 } else if (fd >= 0) {
2128 if (s->do_telnetopt)
2129 tcp_chr_telnet_init(fd);
2130 break;
2131 }
2132 }
2133 socket_set_nonblock(fd);
2134 if (s->do_nodelay)
2135 socket_set_nodelay(fd);
2136 s->fd = fd;
2137 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2138 tcp_chr_connect(chr);
2139}
2140
2141static void tcp_chr_close(CharDriverState *chr)
2142{
2143 TCPCharDriver *s = chr->opaque;
aliguori819f56b2009-03-28 17:58:14 +00002144 if (s->fd >= 0) {
2145 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002146 closesocket(s->fd);
aliguori819f56b2009-03-28 17:58:14 +00002147 }
2148 if (s->listen_fd >= 0) {
2149 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
aliguori6f97dba2008-10-31 18:49:55 +00002150 closesocket(s->listen_fd);
aliguori819f56b2009-03-28 17:58:14 +00002151 }
aliguori6f97dba2008-10-31 18:49:55 +00002152 qemu_free(s);
Amit Shah793cbfb2009-08-11 21:27:48 +05302153 qemu_chr_event(chr, CHR_EVENT_CLOSED);
aliguori6f97dba2008-10-31 18:49:55 +00002154}
2155
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002156static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
aliguori6f97dba2008-10-31 18:49:55 +00002157{
2158 CharDriverState *chr = NULL;
2159 TCPCharDriver *s = NULL;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002160 int fd = -1;
2161 int is_listen;
2162 int is_waitconnect;
2163 int do_nodelay;
2164 int is_unix;
2165 int is_telnet;
aliguori6f97dba2008-10-31 18:49:55 +00002166
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002167 is_listen = qemu_opt_get_bool(opts, "server", 0);
2168 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2169 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2170 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2171 is_unix = qemu_opt_get(opts, "path") != NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002172 if (!is_listen)
2173 is_waitconnect = 0;
2174
2175 chr = qemu_mallocz(sizeof(CharDriverState));
aliguori6f97dba2008-10-31 18:49:55 +00002176 s = qemu_mallocz(sizeof(TCPCharDriver));
aliguori6f97dba2008-10-31 18:49:55 +00002177
aliguorif07b6002008-11-11 20:54:09 +00002178 if (is_unix) {
2179 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002180 fd = unix_listen_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002181 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002182 fd = unix_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002183 }
2184 } else {
2185 if (is_listen) {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002186 fd = inet_listen_opts(opts, 0);
aliguorif07b6002008-11-11 20:54:09 +00002187 } else {
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002188 fd = inet_connect_opts(opts);
aliguorif07b6002008-11-11 20:54:09 +00002189 }
2190 }
aliguori6f97dba2008-10-31 18:49:55 +00002191 if (fd < 0)
2192 goto fail;
2193
2194 if (!is_waitconnect)
2195 socket_set_nonblock(fd);
2196
2197 s->connected = 0;
2198 s->fd = -1;
2199 s->listen_fd = -1;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002200 s->msgfd = -1;
aliguori6f97dba2008-10-31 18:49:55 +00002201 s->is_unix = is_unix;
2202 s->do_nodelay = do_nodelay && !is_unix;
2203
2204 chr->opaque = s;
2205 chr->chr_write = tcp_chr_write;
2206 chr->chr_close = tcp_chr_close;
Mark McLoughlin7d174052009-07-22 09:11:39 +01002207 chr->get_msgfd = tcp_get_msgfd;
aliguori6f97dba2008-10-31 18:49:55 +00002208
2209 if (is_listen) {
aliguori6f97dba2008-10-31 18:49:55 +00002210 s->listen_fd = fd;
2211 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2212 if (is_telnet)
2213 s->do_telnetopt = 1;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002214
aliguori6f97dba2008-10-31 18:49:55 +00002215 } else {
aliguorif07b6002008-11-11 20:54:09 +00002216 s->connected = 1;
aliguori6f97dba2008-10-31 18:49:55 +00002217 s->fd = fd;
2218 socket_set_nodelay(fd);
aliguorif07b6002008-11-11 20:54:09 +00002219 tcp_chr_connect(chr);
aliguori6f97dba2008-10-31 18:49:55 +00002220 }
2221
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002222 /* for "info chardev" monitor command */
2223 chr->filename = qemu_malloc(256);
2224 if (is_unix) {
2225 snprintf(chr->filename, 256, "unix:%s%s",
2226 qemu_opt_get(opts, "path"),
2227 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2228 } else if (is_telnet) {
2229 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2230 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2231 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2232 } else {
2233 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2234 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2235 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2236 }
2237
aliguori6f97dba2008-10-31 18:49:55 +00002238 if (is_listen && is_waitconnect) {
aliguorif07b6002008-11-11 20:54:09 +00002239 printf("QEMU waiting for connection on: %s\n",
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002240 chr->filename);
aliguori6f97dba2008-10-31 18:49:55 +00002241 tcp_chr_accept(chr);
2242 socket_set_nonblock(s->listen_fd);
2243 }
aliguori6f97dba2008-10-31 18:49:55 +00002244 return chr;
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002245
aliguori6f97dba2008-10-31 18:49:55 +00002246 fail:
2247 if (fd >= 0)
2248 closesocket(fd);
2249 qemu_free(s);
2250 qemu_free(chr);
2251 return NULL;
2252}
2253
Gerd Hoffmann33521632009-12-08 13:11:49 +01002254QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002255{
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002256 char host[65], port[33], width[8], height[8];
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002257 int pos;
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002258 const char *p;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002259 QemuOpts *opts;
2260
2261 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2262 if (NULL == opts)
2263 return NULL;
2264
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002265 if (strstart(filename, "mon:", &p)) {
2266 filename = p;
2267 qemu_opt_set(opts, "mux", "on");
2268 }
2269
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002270 if (strcmp(filename, "null") == 0 ||
2271 strcmp(filename, "pty") == 0 ||
2272 strcmp(filename, "msmouse") == 0 ||
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002273 strcmp(filename, "braille") == 0 ||
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002274 strcmp(filename, "stdio") == 0) {
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002275 qemu_opt_set(opts, "backend", filename);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002276 return opts;
2277 }
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002278 if (strstart(filename, "vc", &p)) {
2279 qemu_opt_set(opts, "backend", "vc");
2280 if (*p == ':') {
2281 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2282 /* pixels */
2283 qemu_opt_set(opts, "width", width);
2284 qemu_opt_set(opts, "height", height);
2285 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2286 /* chars */
2287 qemu_opt_set(opts, "cols", width);
2288 qemu_opt_set(opts, "rows", height);
2289 } else {
2290 goto fail;
2291 }
2292 }
2293 return opts;
2294 }
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002295 if (strcmp(filename, "con:") == 0) {
2296 qemu_opt_set(opts, "backend", "console");
2297 return opts;
2298 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002299 if (strstart(filename, "COM", NULL)) {
2300 qemu_opt_set(opts, "backend", "serial");
2301 qemu_opt_set(opts, "path", filename);
2302 return opts;
2303 }
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002304 if (strstart(filename, "file:", &p)) {
2305 qemu_opt_set(opts, "backend", "file");
2306 qemu_opt_set(opts, "path", p);
2307 return opts;
2308 }
2309 if (strstart(filename, "pipe:", &p)) {
2310 qemu_opt_set(opts, "backend", "pipe");
2311 qemu_opt_set(opts, "path", p);
2312 return opts;
2313 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002314 if (strstart(filename, "tcp:", &p) ||
2315 strstart(filename, "telnet:", &p)) {
2316 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2317 host[0] = 0;
2318 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2319 goto fail;
2320 }
2321 qemu_opt_set(opts, "backend", "socket");
2322 qemu_opt_set(opts, "host", host);
2323 qemu_opt_set(opts, "port", port);
2324 if (p[pos] == ',') {
2325 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2326 goto fail;
2327 }
2328 if (strstart(filename, "telnet:", &p))
2329 qemu_opt_set(opts, "telnet", "on");
2330 return opts;
2331 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002332 if (strstart(filename, "udp:", &p)) {
2333 qemu_opt_set(opts, "backend", "udp");
2334 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2335 host[0] = 0;
2336 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002337 goto fail;
2338 }
2339 }
2340 qemu_opt_set(opts, "host", host);
2341 qemu_opt_set(opts, "port", port);
2342 if (p[pos] == '@') {
2343 p += pos + 1;
2344 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2345 host[0] = 0;
2346 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002347 goto fail;
2348 }
2349 }
2350 qemu_opt_set(opts, "localaddr", host);
2351 qemu_opt_set(opts, "localport", port);
2352 }
2353 return opts;
2354 }
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002355 if (strstart(filename, "unix:", &p)) {
2356 qemu_opt_set(opts, "backend", "socket");
2357 if (qemu_opts_do_parse(opts, p, "path") != 0)
2358 goto fail;
2359 return opts;
2360 }
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002361 if (strstart(filename, "/dev/parport", NULL) ||
2362 strstart(filename, "/dev/ppi", NULL)) {
2363 qemu_opt_set(opts, "backend", "parport");
2364 qemu_opt_set(opts, "path", filename);
2365 return opts;
2366 }
2367 if (strstart(filename, "/dev/", NULL)) {
2368 qemu_opt_set(opts, "backend", "tty");
2369 qemu_opt_set(opts, "path", filename);
2370 return opts;
2371 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002372
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002373fail:
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002374 qemu_opts_del(opts);
2375 return NULL;
2376}
2377
2378static const struct {
2379 const char *name;
2380 CharDriverState *(*open)(QemuOpts *opts);
2381} backend_table[] = {
2382 { .name = "null", .open = qemu_chr_open_null },
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +02002383 { .name = "socket", .open = qemu_chr_open_socket },
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002384 { .name = "udp", .open = qemu_chr_open_udp },
Gerd Hoffmannf0457e82009-09-10 10:58:45 +02002385 { .name = "msmouse", .open = qemu_chr_open_msmouse },
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02002386 { .name = "vc", .open = text_console_init },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002387#ifdef _WIN32
2388 { .name = "file", .open = qemu_chr_open_win_file_out },
2389 { .name = "pipe", .open = qemu_chr_open_win_pipe },
Gerd Hoffmannd6c983c2009-09-10 10:58:47 +02002390 { .name = "console", .open = qemu_chr_open_win_con },
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002391 { .name = "serial", .open = qemu_chr_open_win },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002392#else
2393 { .name = "file", .open = qemu_chr_open_file_out },
2394 { .name = "pipe", .open = qemu_chr_open_pipe },
Gerd Hoffmann4490dad2009-09-10 10:58:43 +02002395 { .name = "pty", .open = qemu_chr_open_pty },
Gerd Hoffmann3c17aff2009-09-10 10:58:44 +02002396 { .name = "stdio", .open = qemu_chr_open_stdio },
Gerd Hoffmann7d315442009-09-10 10:58:36 +02002397#endif
Gerd Hoffmanndc1c21e2009-09-10 10:58:46 +02002398#ifdef CONFIG_BRLAPI
2399 { .name = "braille", .open = chr_baum_init },
2400#endif
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002401#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002402 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2403 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002404 { .name = "tty", .open = qemu_chr_open_tty },
2405#endif
Aurelien Jarnoa167ba52009-11-29 18:00:41 +01002406#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2407 || defined(__FreeBSD_kernel__)
Gerd Hoffmann48b76492009-09-10 10:58:48 +02002408 { .name = "parport", .open = qemu_chr_open_pp },
2409#endif
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002410};
2411
2412CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2413 void (*init)(struct CharDriverState *s))
2414{
2415 CharDriverState *chr;
2416 int i;
2417
2418 if (qemu_opts_id(opts) == NULL) {
2419 fprintf(stderr, "chardev: no id specified\n");
2420 return NULL;
2421 }
2422
2423 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2424 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2425 break;
2426 }
2427 if (i == ARRAY_SIZE(backend_table)) {
2428 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2429 qemu_opt_get(opts, "backend"));
2430 return NULL;
2431 }
2432
2433 chr = backend_table[i].open(opts);
2434 if (!chr) {
2435 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2436 qemu_opt_get(opts, "backend"));
2437 return NULL;
2438 }
2439
2440 if (!chr->filename)
2441 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2442 chr->init = init;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002443 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002444
2445 if (qemu_opt_get_bool(opts, "mux", 0)) {
2446 CharDriverState *base = chr;
2447 int len = strlen(qemu_opts_id(opts)) + 6;
2448 base->label = qemu_malloc(len);
2449 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2450 chr = qemu_chr_open_mux(base);
2451 chr->filename = base->filename;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002452 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
Gerd Hoffmann7591c5c2009-09-10 10:58:50 +02002453 }
2454 chr->label = qemu_strdup(qemu_opts_id(opts));
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002455 return chr;
2456}
2457
aurel32ceecf1d2009-01-18 14:08:04 +00002458CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
aliguori6f97dba2008-10-31 18:49:55 +00002459{
2460 const char *p;
2461 CharDriverState *chr;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002462 QemuOpts *opts;
2463
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002464 if (strstart(filename, "chardev:", &p)) {
2465 return qemu_chr_find(p);
2466 }
2467
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002468 opts = qemu_chr_parse_compat(label, filename);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002469 if (!opts)
2470 return NULL;
aliguori6f97dba2008-10-31 18:49:55 +00002471
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +02002472 chr = qemu_chr_open_opts(opts, init);
2473 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2474 monitor_init(chr, MONITOR_USE_READLINE);
aliguori6f97dba2008-10-31 18:49:55 +00002475 }
2476 return chr;
2477}
2478
2479void qemu_chr_close(CharDriverState *chr)
2480{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002481 QTAILQ_REMOVE(&chardevs, chr, next);
aliguori6f97dba2008-10-31 18:49:55 +00002482 if (chr->chr_close)
2483 chr->chr_close(chr);
2484 qemu_free(chr->filename);
2485 qemu_free(chr->label);
2486 qemu_free(chr);
2487}
2488
Luiz Capitulino588b3832009-12-10 17:16:08 -02002489static void qemu_chr_qlist_iter(QObject *obj, void *opaque)
aliguori6f97dba2008-10-31 18:49:55 +00002490{
Luiz Capitulino588b3832009-12-10 17:16:08 -02002491 QDict *chr_dict;
2492 Monitor *mon = opaque;
2493
2494 chr_dict = qobject_to_qdict(obj);
2495 monitor_printf(mon, "%s: filename=%s\n", qdict_get_str(chr_dict, "label"),
2496 qdict_get_str(chr_dict, "filename"));
2497}
2498
2499void qemu_chr_info_print(Monitor *mon, const QObject *ret_data)
2500{
2501 qlist_iter(qobject_to_qlist(ret_data), qemu_chr_qlist_iter, mon);
2502}
2503
2504/**
2505 * qemu_chr_info(): Character devices information
2506 *
2507 * Each device is represented by a QDict. The returned QObject is a QList
2508 * of all devices.
2509 *
2510 * The QDict contains the following:
2511 *
2512 * - "label": device's label
2513 * - "filename": device's file
2514 *
2515 * Example:
2516 *
2517 * [ { "label": "monitor", "filename", "stdio" },
2518 * { "label": "serial0", "filename": "vc" } ]
2519 */
2520void qemu_chr_info(Monitor *mon, QObject **ret_data)
2521{
2522 QList *chr_list;
aliguori6f97dba2008-10-31 18:49:55 +00002523 CharDriverState *chr;
2524
Luiz Capitulino588b3832009-12-10 17:16:08 -02002525 chr_list = qlist_new();
2526
Blue Swirl72cf2d42009-09-12 07:36:22 +00002527 QTAILQ_FOREACH(chr, &chardevs, next) {
Luiz Capitulino588b3832009-12-10 17:16:08 -02002528 QObject *obj = qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2529 chr->label, chr->filename);
2530 qlist_append_obj(chr_list, obj);
aliguori6f97dba2008-10-31 18:49:55 +00002531 }
Luiz Capitulino588b3832009-12-10 17:16:08 -02002532
2533 *ret_data = QOBJECT(chr_list);
aliguori6f97dba2008-10-31 18:49:55 +00002534}
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002535
2536CharDriverState *qemu_chr_find(const char *name)
2537{
2538 CharDriverState *chr;
2539
Blue Swirl72cf2d42009-09-12 07:36:22 +00002540 QTAILQ_FOREACH(chr, &chardevs, next) {
Gerd Hoffmannc845f402009-09-10 10:58:52 +02002541 if (strcmp(chr->label, name) != 0)
2542 continue;
2543 return chr;
2544 }
2545 return NULL;
2546}